2 Module : Gargantext.Viz.Phylo.Tools
3 Description : Phylomemy Tools to build/manage it
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 {-# LANGUAGE FlexibleContexts #-}
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE RankNTypes #-}
17 {-# LANGUAGE ViewPatterns #-}
19 module Gargantext.Viz.Phylo.Tools
22 import Control.Lens hiding (both, Level, Empty)
23 import Data.List (filter, intersect, (++), sort, null, tail, last, tails, delete, nub, sortOn, nubBy, concat)
24 import Data.Maybe (mapMaybe,fromMaybe)
25 import Data.Map (Map, mapKeys, member, (!), restrictKeys, elems, empty, filterWithKey, unionWith)
27 import Data.Text (Text,toLower,unwords)
28 import Data.Tuple.Extra
29 import Data.Vector (Vector,elemIndex)
30 import Gargantext.Prelude
31 import Gargantext.Viz.Phylo
32 import qualified Data.Map as Map
33 import qualified Data.Set as Set
34 import qualified Data.Vector as Vector
42 -- | Define a default value
43 def :: a -> Maybe a -> a
47 -- | Does a List of Sets contains at least one Set of an other List
48 doesAnySetContains :: Eq a => Set a -> [Set a] -> [Set a] -> Bool
49 doesAnySetContains h l l' = any (\c -> doesContains (Set.toList c) (Set.toList h)) (l' ++ l)
52 -- | Does a list of A contains an other list of A
53 doesContains :: Eq a => [a] -> [a] -> Bool
56 | length l' > length l = False
57 | elem (head' "doesContains" l') l = doesContains l (tail l')
61 -- | Does a list of ordered A contains an other list of ordered A
62 doesContainsOrd :: Eq a => Ord a => [a] -> [a] -> Bool
65 | last l < (head' "doesContainsOrd" l') = False
66 | (head' "doesContainsOrd" l') `elem` l = True
67 | otherwise = doesContainsOrd l (tail l')
70 -- | To filter nested Sets of a
71 filterNestedSets :: Eq a => Set a -> [Set a] -> [Set a] -> [Set a]
72 filterNestedSets h l l'
73 | null l = if doesAnySetContains h l l'
76 | doesAnySetContains h l l' = filterNestedSets (head' "filterNestedSets1" l) (tail l) l'
77 | otherwise = filterNestedSets (head' "filterNestedSets2" l) (tail l) (h : l')
81 -- | To get the good pair of keys (x,y) or (y,x) in a given Map (a,b) c
82 getKeyPair :: (Int,Int) -> Map (Int,Int) a -> (Int,Int)
83 getKeyPair (x,y) m = case findPair (x,y) m of
84 Nothing -> panic "[ERR][Viz.Phylo.Example.getKeyPair] Nothing"
87 --------------------------------------
88 findPair :: (Int,Int) -> Map (Int,Int) a -> Maybe (Int,Int)
90 | member (x',y') m' = Just (x',y')
91 | member (y',x') m' = Just (y',x')
93 --------------------------------------
96 -- | To filter Fis with small Support but by keeping non empty Periods
97 keepFilled :: (Int -> [a] -> [a]) -> Int -> [a] -> [a]
98 keepFilled f thr l = if (null $ f thr l) && (not $ null l)
99 then keepFilled f (thr - 1) l
103 -- | To get all combinations of a list
104 listToFullCombi :: Eq a => [a] -> [(a,a)]
105 listToFullCombi l = [(x,y) | x <- l, y <- l]
108 -- | To get all combinations of a list
109 listToDirectedCombi :: Eq a => [a] -> [(a,a)]
110 listToDirectedCombi l = [(x,y) | x <- l, y <- l, x /= y]
113 listToEqualCombi :: Eq a => [a] -> [(a,a)]
114 listToEqualCombi l = [(x,y) | x <- l, y <- l, x == y]
116 listToPairs :: Eq a => [a] -> [(a,a)]
117 listToPairs l = (listToEqualCombi l) ++ (listToUnDirectedCombi l)
120 -- | To get all combinations of a list and apply a function to the resulting list of pairs
121 listToDirectedCombiWith :: Eq a => forall b. (a -> b) -> [a] -> [(b,b)]
122 listToDirectedCombiWith f l = [(f x,f y) | x <- l, y <- l, x /= y]
125 -- | To get the sequential combinations of an order list
126 listToSequentialCombi :: Eq a => [a] -> [(a,a)]
127 listToSequentialCombi l = nubBy (\x y -> fst x == fst y) $ listToUnDirectedCombi l
130 -- | To get all combinations of a list with no repetition
131 listToUnDirectedCombi :: [a] -> [(a,a)]
132 listToUnDirectedCombi l = [ (x,y) | (x:rest) <- tails l, y <- rest ]
135 -- | To get all combinations of a list with no repetition and apply a function to the resulting list of pairs
136 listToUnDirectedCombiWith :: forall a b. (a -> b) -> [a] -> [(b,b)]
137 listToUnDirectedCombiWith f l = [ (f x, f y) | (x:rest) <- tails l, y <- rest ]
140 -- | To transform a list of Ngrams Indexes into a Label
141 ngramsToLabel :: Vector Ngrams -> [Int] -> Text
142 ngramsToLabel ngrams l = unwords $ tail' "ngramsToLabel" $ concat $ map (\n -> ["|",n]) $ ngramsToText ngrams l
145 -- | To transform a list of Ngrams Indexes into a list of Text
146 ngramsToText :: Vector Ngrams -> [Int] -> [Text]
147 ngramsToText ngrams l = map (\idx -> ngrams Vector.! idx) l
150 -- | To transform a list of ngrams into a list of indexes
151 ngramsToIdx :: [Ngrams] -> Vector Ngrams -> [Int]
152 ngramsToIdx ns v = sort $ map (\n -> getIdxInVector n v) ns
155 -- | To unify the keys (x,y) that Map 1 share with Map 2 such as: (x,y) <=> (y,x)
156 unifySharedKeys :: Eq a => Ord a => Map (a,a) b -> Map (a,a) b -> Map (a,a) b
157 unifySharedKeys m1 m2 = mapKeys (\(x,y) -> if member (y,x) m2
166 -- | An analyzer ingests a Ngrams and generates a modified version of it
167 phyloAnalyzer :: Ngrams -> Ngrams
168 phyloAnalyzer n = toLower n
170 -- | To init the foundation roots of the Phylo as a Vector of Ngrams
171 initFoundationsRoots :: [Ngrams] -> Vector Ngrams
172 initFoundationsRoots l = Vector.fromList $ map phyloAnalyzer l
174 -- | To init the base of a Phylo from a List of Periods and Foundations
175 initPhyloBase :: [(Date, Date)] -> PhyloFoundations -> Map Date Double -> Map Date (Map (Int,Int) Double) -> Map (Date,Date) [PhyloFis] -> PhyloParam -> Phylo
176 initPhyloBase pds fds nbDocs cooc fis prm = Phylo ((fst . (head' "initPhyloBase")) pds, (snd . last) pds) fds (map (\pd -> initPhyloPeriod pd []) pds) nbDocs cooc fis prm
178 -- | To init the param of a Phylo
179 initPhyloParam :: Maybe Text -> Maybe Software -> Maybe PhyloQueryBuild -> PhyloParam
180 initPhyloParam (def defaultPhyloVersion -> v) (def defaultSoftware -> s) (def defaultQueryBuild -> q) = PhyloParam v s q
182 -- | To get the last computed Level in a Phylo
183 getLastLevel :: Phylo -> Level
184 getLastLevel p = (last . sort)
185 $ map (snd . getPhyloLevelId)
186 $ view ( phylo_periods
188 . phylo_periodLevels ) p
190 -- | To get all the coocurency matrix of a phylo
191 getPhyloCooc :: Phylo -> Map Date (Map (Int,Int) Double)
192 getPhyloCooc p = p ^. phylo_cooc
195 -- | To get the PhyloParam of a Phylo
196 getPhyloParams :: Phylo -> PhyloParam
197 getPhyloParams = _phylo_param
199 -- | To get the title of a Phylo
200 getPhyloTitle :: Phylo -> Text
201 getPhyloTitle p = _q_phyloTitle $ _phyloParam_query $ getPhyloParams p
203 -- | To get the desc of a Phylo
204 getPhyloDescription :: Phylo -> Text
205 getPhyloDescription p = _q_phyloTitle $ _phyloParam_query $ getPhyloParams p
207 getPhyloMatchingFrame :: Phylo -> Int
208 getPhyloMatchingFrame p = _q_interTemporalMatchingFrame $ _phyloParam_query $ getPhyloParams p
210 getPhyloMatchingFrameTh :: Phylo -> Double
211 getPhyloMatchingFrameTh p = _q_interTemporalMatchingFrameTh $ _phyloParam_query $ getPhyloParams p
213 getPhyloProximity :: Phylo -> Proximity
214 getPhyloProximity p = _q_interTemporalMatching $ _phyloParam_query $ getPhyloParams p
216 getPhyloReBranchThr :: Phylo -> Double
217 getPhyloReBranchThr p = _q_reBranchThr $ _phyloParam_query $ getPhyloParams p
219 getPhyloReBranchNth :: Phylo -> Int
220 getPhyloReBranchNth p = _q_reBranchNth $ _phyloParam_query $ getPhyloParams p
222 getPhyloFis :: Phylo -> Map (Date,Date) [PhyloFis]
223 getPhyloFis = _phylo_fis
230 -- | To get the foundations of a Phylo
231 getFoundations :: Phylo -> PhyloFoundations
232 getFoundations = _phylo_foundations
234 -- | To get the foundations roots of a Phylo
235 getFoundationsRoots :: Phylo -> Vector Ngrams
236 getFoundationsRoots p = (getFoundations p) ^. phylo_foundationsRoots
238 -- | To get the Index of a Ngrams in the foundationsRoots of a Phylo
239 getIdxInRoots :: Ngrams -> Phylo -> Int
240 getIdxInRoots n p = case (elemIndex n (getFoundationsRoots p)) of
241 Nothing -> panic $ "[ERR][Viz.Phylo.Tools.getIdxInRoots] Ngrams not in foundationsRoots: " <> cs n
244 getIdxInVector :: Ngrams -> Vector Ngrams -> Int
245 getIdxInVector n ns = case (elemIndex n ns) of
246 Nothing -> panic $ "[ERR][Viz.Phylo.Tools.getIdxInVector] Ngrams not in foundationsRoots: " <> cs n
253 -- | To alter a PhyloGroup matching a given Level
254 alterGroupWithLevel :: (PhyloGroup -> PhyloGroup) -> Level -> Phylo -> Phylo
255 alterGroupWithLevel f lvl p = over ( phylo_periods
261 ) (\g -> if getGroupLevel g == lvl
266 -- | To alter each list of PhyloGroups following a given function
267 alterPhyloGroups :: ([PhyloGroup] -> [PhyloGroup]) -> Phylo -> Phylo
268 alterPhyloGroups f p = over ( phylo_periods
276 -- | To filter the PhyloGroup of a Phylo according to a function and a value
277 filterGroups :: Eq a => (PhyloGroup -> a) -> a -> [PhyloGroup] -> [PhyloGroup]
278 filterGroups f x l = filter (\g -> (f g) == x) l
281 -- | To maybe get the PhyloBranchId of a PhyloGroup
282 getGroupBranchId :: PhyloGroup -> Maybe PhyloBranchId
283 getGroupBranchId = _phylo_groupBranchId
286 -- | To get the PhyloGroups Childs of a PhyloGroup
287 getGroupChilds :: PhyloGroup -> Phylo -> [PhyloGroup]
288 getGroupChilds g p = getGroupsFromIds (getGroupPeriodChildsId g) p
291 -- | To get the id of a PhyloGroup
292 getGroupId :: PhyloGroup -> PhyloGroupId
293 getGroupId = _phylo_groupId
296 getGroupCooc :: PhyloGroup -> Map (Int,Int) Double
297 getGroupCooc = _phylo_groupCooc
300 -- | To get the level out of the id of a PhyloGroup
301 getGroupLevel :: PhyloGroup -> Int
302 getGroupLevel = snd . fst . getGroupId
305 -- | To get the level child pointers of a PhyloGroup
306 getGroupLevelChilds :: PhyloGroup -> [Pointer]
307 getGroupLevelChilds = _phylo_groupLevelChilds
310 -- | To get the PhyloGroups Level Childs Ids of a PhyloGroup
311 getGroupLevelChildsId :: PhyloGroup -> [PhyloGroupId]
312 getGroupLevelChildsId g = map fst $ getGroupLevelChilds g
315 -- | To get the level parent pointers of a PhyloGroup
316 getGroupLevelParents :: PhyloGroup -> [Pointer]
317 getGroupLevelParents = _phylo_groupLevelParents
320 -- | To get the PhyloGroups Level Parents Ids of a PhyloGroup
321 getGroupLevelParentsId :: PhyloGroup -> [PhyloGroupId]
322 getGroupLevelParentsId g = map fst $ getGroupLevelParents g
325 -- | To get the PhyloGroups Level Parents Ids of a PhyloGroup
326 getGroupLevelParentId :: PhyloGroup -> PhyloGroupId
327 getGroupLevelParentId g = (head' "getGroupLevelParentId") $ getGroupLevelParentsId g
329 -- | To get the Meta value of a PhyloGroup
330 getGroupMeta :: Text -> PhyloGroup -> Double
331 getGroupMeta k g = (g ^. phylo_groupMeta) ! k
334 -- | To get the Ngrams of a PhyloGroup
335 getGroupNgrams :: PhyloGroup -> [Int]
336 getGroupNgrams = _phylo_groupNgrams
339 -- | To get the list of pairs (Childs & Parents) of a PhyloGroup
340 getGroupPairs :: PhyloGroup -> Phylo -> [PhyloGroup]
341 getGroupPairs g p = (getGroupChilds g p) ++ (getGroupParents g p)
344 -- | To get the PhyloGroups Parents of a PhyloGroup
345 getGroupParents :: PhyloGroup -> Phylo -> [PhyloGroup]
346 getGroupParents g p = getGroupsFromIds (getGroupPeriodParentsId g) p
349 -- | To get the period out of the id of a PhyloGroup
350 getGroupPeriod :: PhyloGroup -> (Date,Date)
351 getGroupPeriod = fst . fst . getGroupId
354 -- | To get the period child pointers of a PhyloGroup
355 getGroupPeriodChilds :: PhyloGroup -> [Pointer]
356 getGroupPeriodChilds = _phylo_groupPeriodChilds
359 -- | To get the PhyloGroups Period Parents Ids of a PhyloGroup
360 getGroupPeriodChildsId :: PhyloGroup -> [PhyloGroupId]
361 getGroupPeriodChildsId g = map fst $ getGroupPeriodChilds g
364 -- | To get the period parent pointers of a PhyloGroup
365 getGroupPeriodParents :: PhyloGroup -> [Pointer]
366 getGroupPeriodParents = _phylo_groupPeriodParents
369 -- | To get the PhyloGroups Period Parents Ids of a PhyloGroup
370 getGroupPeriodParentsId :: PhyloGroup -> [PhyloGroupId]
371 getGroupPeriodParentsId g = map fst $ getGroupPeriodParents g
374 -- | To get the pointers of a given Phylogroup
375 getGroupPointers :: EdgeType -> Filiation -> PhyloGroup -> [Pointer]
376 getGroupPointers t f g = case t of
377 PeriodEdge -> case f of
378 Ascendant -> getGroupPeriodParents g
379 Descendant -> getGroupPeriodChilds g
380 _ -> panic "[ERR][Viz.Phylo.Tools.getGroupPointers] wrong filiation"
381 LevelEdge -> case f of
382 Ascendant -> getGroupLevelParents g
383 Descendant -> getGroupLevelChilds g
384 _ -> panic "[ERR][Viz.Phylo.Tools.getGroupPointers] wrong filiation"
387 -- | To get the roots labels of a list of group ngrams
388 getGroupText :: PhyloGroup -> Phylo -> [Text]
389 getGroupText g p = ngramsToText (getFoundationsRoots p) (getGroupNgrams g)
392 -- | To get all the PhyloGroup of a Phylo
393 getGroups :: Phylo -> [PhyloGroup]
394 getGroups = view ( phylo_periods
402 -- | To get all PhyloGroups matching a list of PhyloGoupIds in a Phylo
403 -- getGroupsFromIds :: [PhyloGroupId] -> Phylo -> [PhyloGroup]
404 -- getGroupsFromIds ids p = filter (\g -> elem (getGroupId g) ids) $ getGroups p
406 getGroupFromId :: PhyloGroupId -> Phylo -> PhyloGroup
407 getGroupFromId id p =
408 let groups = Map.fromList $ map (\g -> (getGroupId g, g)) $ getGroups p
411 getGroupsFromIds :: [PhyloGroupId] -> Phylo -> [PhyloGroup]
412 getGroupsFromIds ids p =
413 let groups = Map.fromList $ map (\g -> (getGroupId g, g)) $ getGroups p
414 in elems $ restrictKeys groups (Set.fromList ids)
417 -- | To get the corresponding list of PhyloGroups from a list of PhyloNodes
418 getGroupsFromNodes :: [PhyloNode] -> Phylo -> [PhyloGroup]
419 getGroupsFromNodes ns p = getGroupsFromIds (map getNodeId ns) p
422 -- | To get all the PhyloGroup of a Phylo with a given level and period
423 getGroupsWithFilters :: Int -> (Date,Date) -> Phylo -> [PhyloGroup]
424 getGroupsWithFilters lvl prd p = (getGroupsWithLevel lvl p)
426 (getGroupsWithPeriod prd p)
429 -- | To get all the PhyloGroup of a Phylo with a given Level
430 getGroupsWithLevel :: Int -> Phylo -> [PhyloGroup]
431 getGroupsWithLevel lvl p = filterGroups getGroupLevel lvl (getGroups p)
434 -- | To get all the PhyloGroup of a Phylo with a given Period
435 getGroupsWithPeriod :: (Date,Date) -> Phylo -> [PhyloGroup]
436 getGroupsWithPeriod prd p = filterGroups getGroupPeriod prd (getGroups p)
439 -- | To create a PhyloGroup in a Phylo out of a list of Ngrams and a set of parameters
440 initGroup :: [Ngrams] -> Text -> Int -> Int -> Int -> Int -> Phylo -> PhyloGroup
441 initGroup ngrams lbl idx lvl from' to' p = PhyloGroup
442 (((from', to'), lvl), idx)
448 (getMiniCooc (listToFullCombi idxs) (periodsToYears [(from', to')]) (getPhyloCooc p))
451 idxs = sort $ map (\x -> getIdxInRoots x p) ngrams
454 -- | To sum two coocurency Matrix
455 sumCooc :: Map (Int, Int) Double -> Map (Int, Int) Double -> Map (Int, Int) Double
456 sumCooc m m' = unionWith (+) m m'
458 -- | To build the mini cooc matrix of each group
459 getMiniCooc :: [(Int,Int)] -> Set Date -> Map Date (Map (Int,Int) Double) -> Map (Int,Int) Double
460 getMiniCooc pairs years cooc = filterWithKey (\(n,n') _ -> elem (n,n') pairs) cooc'
462 --------------------------------------
463 cooc' :: Map (Int,Int) Double
464 cooc' = foldl (\m m' -> sumCooc m m') empty
466 $ restrictKeys cooc years
467 --------------------------------------
470 ---------------------
471 -- | PhyloPeriod | --
472 ---------------------
475 -- | To alter each PhyloPeriod of a Phylo following a given function
476 alterPhyloPeriods :: (PhyloPeriod -> PhyloPeriod) -> Phylo -> Phylo
477 alterPhyloPeriods f p = over ( phylo_periods
481 -- | To append a list of PhyloPeriod to a Phylo
482 appendToPhyloPeriods :: [PhyloPeriod] -> Phylo -> Phylo
483 appendToPhyloPeriods l p = over (phylo_periods) (++ l) p
486 -- | To get all the PhyloPeriodIds of a Phylo
487 getPhyloPeriods :: Phylo -> [PhyloPeriodId]
488 getPhyloPeriods p = map _phylo_periodId
489 $ view (phylo_periods) p
492 -- | To get the id of a given PhyloPeriod
493 getPhyloPeriodId :: PhyloPeriod -> PhyloPeriodId
494 getPhyloPeriodId prd = _phylo_periodId prd
497 -- | To create a PhyloPeriod
498 initPhyloPeriod :: PhyloPeriodId -> [PhyloLevel] -> PhyloPeriod
499 initPhyloPeriod id l = PhyloPeriod id l
502 -- | To transform a list of periods into a set of Dates
503 periodsToYears :: [(Date,Date)] -> Set Date
504 periodsToYears periods = (Set.fromList . sort . concat)
505 $ map (\(d,d') -> [d..d']) periods
513 -- | To alter a list of PhyloLevels following a given function
514 alterPhyloLevels :: ([PhyloLevel] -> [PhyloLevel]) -> Phylo -> Phylo
515 alterPhyloLevels f p = over ( phylo_periods
517 . phylo_periodLevels) f p
520 -- | To get the PhylolevelId of a given PhyloLevel
521 getPhyloLevelId :: PhyloLevel -> PhyloLevelId
522 getPhyloLevelId = _phylo_levelId
525 -- | To get all the Phylolevels of a given PhyloPeriod
526 getPhyloLevels :: PhyloPeriod -> [PhyloLevel]
527 getPhyloLevels = view (phylo_periodLevels)
530 -- | To create a PhyloLevel
531 initPhyloLevel :: PhyloLevelId -> [PhyloGroup] -> PhyloLevel
532 initPhyloLevel id groups = PhyloLevel id groups
535 -- | To set the LevelId of a PhyloLevel and of all its PhyloGroups
536 setPhyloLevelId :: Int -> PhyloLevel -> PhyloLevel
537 setPhyloLevelId lvl' (PhyloLevel (id, _lvl) groups)
538 = PhyloLevel (id, lvl') groups'
540 groups' = over (traverse . phylo_groupId)
541 (\((period, _lvl), idx) -> ((period, lvl'), idx))
550 -- | To get the clique of a PhyloFis
551 getClique :: PhyloFis -> Clique
552 getClique = _phyloFis_clique
554 -- | To get the support of a PhyloFis
555 getSupport :: PhyloFis -> Support
556 getSupport = _phyloFis_support
558 -- | To get the period of a PhyloFis
559 getFisPeriod :: PhyloFis -> (Date,Date)
560 getFisPeriod = _phyloFis_period
563 ----------------------------
564 -- | PhyloNodes & Edges | --
565 ----------------------------
568 -- | To filter some GroupEdges with a given threshold
569 filterGroupEdges :: Double -> [GroupEdge] -> [GroupEdge]
570 filterGroupEdges thr edges = filter (\((_s,_t),w) -> w > thr) edges
573 -- | To get the neighbours (directed/undirected) of a PhyloGroup from a list of GroupEdges
574 getNeighbours :: Bool -> PhyloGroup -> [GroupEdge] -> [PhyloGroup]
575 getNeighbours directed g e = case directed of
576 True -> map (\((_s,t),_w) -> t)
577 $ filter (\((s,_t),_w) -> s == g) e
578 False -> map (\((s,t),_w) -> (head' "getNeighbours") $ delete g $ nub [s,t,g])
579 $ filter (\((s,t),_w) -> s == g || t == g) e
582 -- | To get the PhyloBranchId of PhyloNode if it exists
583 getNodeBranchId :: PhyloNode -> PhyloBranchId
584 getNodeBranchId n = case n ^. pn_bid of
585 Nothing -> panic "[ERR][Viz.Phylo.Tools.getNodeBranchId] branchId not found"
589 -- | To get the PhyloGroupId of a PhyloNode
590 getNodeId :: PhyloNode -> PhyloGroupId
591 getNodeId n = n ^. pn_id
594 -- | To get the Level of a PhyloNode
595 getNodeLevel :: PhyloNode -> Level
596 getNodeLevel n = (snd . fst) $ getNodeId n
599 -- | To get the Parent Node of a PhyloNode in a PhyloView
600 getNodeParent :: PhyloNode -> PhyloView -> [PhyloNode]
601 getNodeParent n v = filter (\n' -> elem (getNodeId n') (getNodeParentsId n))
605 -- | To get the Parent Node id of a PhyloNode if it exists
606 getNodeParentsId :: PhyloNode -> [PhyloGroupId]
607 getNodeParentsId n = case n ^. pn_parents of
608 Nothing -> panic "[ERR][Viz.Phylo.Tools.getNodeParentsId] node parent not found"
612 -- | To get a list of PhyloNodes grouped by PhyloBranch in a PhyloView
613 getNodesByBranches :: PhyloView -> [(PhyloBranchId,[PhyloNode])]
614 getNodesByBranches v = zip bIds $ map (\id -> filter (\n -> (getNodeBranchId n) == id)
615 $ getNodesInBranches v ) bIds
617 --------------------------------------
618 bIds :: [PhyloBranchId]
619 bIds = getViewBranchIds v
620 --------------------------------------
623 -- | To get a list of PhyloNodes owned by any PhyloBranches in a PhyloView
624 getNodesInBranches :: PhyloView -> [PhyloNode]
625 getNodesInBranches v = filter (\n -> isJust $ n ^. pn_bid)
629 -- | To get the PhyloGroupId of the Source of a PhyloEdge
630 getSourceId :: PhyloEdge -> PhyloGroupId
631 getSourceId e = e ^. pe_source
634 -- | To get the PhyloGroupId of the Target of a PhyloEdge
635 getTargetId :: PhyloEdge -> PhyloGroupId
636 getTargetId e = e ^. pe_target
639 ---------------------
640 -- | PhyloBranch | --
641 ---------------------
644 -- | To get the PhyloBranchId of a PhyloBranch
645 getBranchId :: PhyloBranch -> PhyloBranchId
646 getBranchId b = b ^. pb_id
648 -- | To get a list of PhyloBranchIds
649 getBranchIds :: Phylo -> [PhyloBranchId]
650 getBranchIds p = sortOn snd
652 $ mapMaybe getGroupBranchId
656 -- | To get a list of PhyloBranchIds given a Level in a Phylo
657 getBranchIdsWith :: Level -> Phylo -> [PhyloBranchId]
658 getBranchIdsWith lvl p = sortOn snd
659 $ mapMaybe getGroupBranchId
660 $ getGroupsWithLevel lvl p
663 -- | To get the Meta value of a PhyloBranch
664 getBranchMeta :: Text -> PhyloBranch -> [Double]
665 getBranchMeta k b = (b ^. pb_metrics) ! k
668 -- | To get all the PhyloBranchIds of a PhyloView
669 getViewBranchIds :: PhyloView -> [PhyloBranchId]
670 getViewBranchIds v = map getBranchId $ v ^. pv_branches
673 -- | To get a list of PhyloGroup sharing the same PhyloBranchId
674 getGroupsByBranches :: Phylo -> [(PhyloBranchId,[PhyloGroup])]
675 getGroupsByBranches p = zip (getBranchIds p)
676 $ map (\id -> filter (\g -> (fromJust $ getGroupBranchId g) == id)
677 $ getGroupsInBranches p)
681 -- | To get the sublist of all the PhyloGroups linked to a branch
682 getGroupsInBranches :: Phylo -> [PhyloGroup]
683 getGroupsInBranches p = filter (\g -> isJust $ g ^. phylo_groupBranchId)
687 --------------------------------
688 -- | PhyloQuery & QueryView | --
689 --------------------------------
692 -- | To filter PhyloView's Branches by level
693 filterBranchesByLevel :: Level -> PhyloView -> [PhyloBranch]
694 filterBranchesByLevel lvl pv = filter (\pb -> lvl == (fst $ pb ^. pb_id))
698 -- | To filter PhyloView's Edges by level
699 filterEdgesByLevel :: Level -> [PhyloEdge] -> [PhyloEdge]
700 filterEdgesByLevel lvl pes = filter (\pe -> (lvl == ((snd . fst) $ pe ^. pe_source))
701 && (lvl == ((snd . fst) $ pe ^. pe_target))) pes
704 -- | To filter PhyloView's Edges by type
705 filterEdgesByType :: EdgeType -> [PhyloEdge] -> [PhyloEdge]
706 filterEdgesByType t pes = filter (\pe -> t == (pe ^. pe_type)) pes
709 -- | To filter PhyloView's Nodes by the oldest Period
710 filterNodesByFirstPeriod :: [PhyloNode] -> [PhyloNode]
711 filterNodesByFirstPeriod pns = filter (\pn -> fstPrd == ((fst . fst) $ pn ^. pn_id)) pns
713 --------------------------------------
714 fstPrd :: (Date,Date)
715 fstPrd = (head' "filterNodesByFirstPeriod")
717 $ map (\pn -> (fst . fst) $ pn ^. pn_id) pns
718 --------------------------------------
721 -- | To filter PhyloView's Nodes by Branch
722 filterNodesByBranch :: PhyloBranchId -> [PhyloNode] -> [PhyloNode]
723 filterNodesByBranch bId pns = filter (\pn -> if isJust $ pn ^. pn_bid
724 then if bId == (fromJust $ pn ^. pn_bid)
730 -- | To filter PhyloView's Nodes by level
731 filterNodesByLevel :: Level -> [PhyloNode] -> [PhyloNode]
732 filterNodesByLevel lvl pns = filter (\pn -> lvl == ((snd . fst) $ pn ^. pn_id)) pns
735 -- | To filter PhyloView's Nodes by Period
736 filterNodesByPeriod :: PhyloPeriodId -> [PhyloNode] -> [PhyloNode]
737 filterNodesByPeriod prd pns = filter (\pn -> prd == ((fst . fst) $ pn ^. pn_id)) pns
740 -- | To get the first clustering method to apply to get the contextual units of a Phylo
741 getContextualUnit :: PhyloQueryBuild -> Cluster
742 getContextualUnit q = q ^. q_contextualUnit
745 -- | To get the metrics to apply to contextual units
746 getContextualUnitMetrics :: PhyloQueryBuild -> [Metric]
747 getContextualUnitMetrics q = q ^. q_contextualUnitMetrics
750 -- | To get the filters to apply to contextual units
751 getContextualUnitFilters :: PhyloQueryBuild -> [Filter]
752 getContextualUnitFilters q = q ^. q_contextualUnitFilters
755 -- | To get the cluster methods to apply to the Nths levels of a Phylo
756 getNthCluster :: PhyloQueryBuild -> Cluster
757 getNthCluster q = q ^. q_nthCluster
760 -- | To get the Sup Level of a reconstruction of a Phylo from a PhyloQuery
761 getNthLevel :: PhyloQueryBuild -> Level
762 getNthLevel q = q ^. q_nthLevel
765 -- | To get the Grain of the PhyloPeriods from a PhyloQuery
766 getPeriodGrain :: PhyloQueryBuild -> Int
767 getPeriodGrain q = q ^. q_periodGrain
770 -- | To get the intertemporal matching strategy to apply to a Phylo from a PhyloQuery
771 getInterTemporalMatching :: PhyloQueryBuild -> Proximity
772 getInterTemporalMatching q = q ^. q_interTemporalMatching
775 -- | To get the Steps of the PhyloPeriods from a PhyloQuery
776 getPeriodSteps :: PhyloQueryBuild -> Int
777 getPeriodSteps q = q ^. q_periodSteps
780 --------------------------------------------------
781 -- | PhyloQueryBuild & PhyloQueryView Constructors | --
782 --------------------------------------------------
784 -- | To get the threshold of a Proximity
785 getThreshold :: Proximity -> Double
786 getThreshold prox = case prox of
787 WeightedLogJaccard (WLJParams thr _) -> thr
788 Hamming (HammingParams thr) -> thr
789 Filiation -> panic "[ERR][Viz.Phylo.Tools.getThreshold] Filiation"
792 -- | To get the Proximity associated to a given Clustering method
793 getProximity :: Cluster -> Proximity
794 getProximity cluster = case cluster of
795 Louvain (LouvainParams proxi) -> proxi
796 RelatedComponents (RCParams proxi) -> proxi
797 _ -> panic "[ERR][Viz.Phylo.Tools.getProximity] this cluster has no associated Proximity"
800 -- | To initialize all the Cluster / Proximity with their default parameters
801 initFis :: Maybe Bool -> Maybe Support -> Maybe Int -> FisParams
802 initFis (def True -> kmf) (def 1 -> min') (def 1 -> thr) = FisParams kmf min' thr
804 initHamming :: Maybe Double -> HammingParams
805 initHamming (def 0.01 -> sens) = HammingParams sens
807 initLonelyBranch :: Maybe Int -> Maybe Int -> Maybe Int -> LBParams
808 initLonelyBranch (def 2 -> periodsInf) (def 2 -> periodsSup) (def 1 -> minNodes) = LBParams periodsInf periodsSup minNodes
810 initSizeBranch :: Maybe Int -> SBParams
811 initSizeBranch (def 1 -> minSize) = SBParams minSize
813 initLonelyBranch' :: Maybe Int -> Maybe Int -> Maybe Int -> LBParams
814 initLonelyBranch' (def 0 -> periodsInf) (def 0 -> periodsSup) (def 1 -> minNodes) = LBParams periodsInf periodsSup minNodes
816 initLouvain :: Maybe Proximity -> LouvainParams
817 initLouvain (def defaultWeightedLogJaccard -> proxi) = LouvainParams proxi
819 initRelatedComponents :: Maybe Proximity -> RCParams
820 initRelatedComponents (def Filiation -> proxi) = RCParams proxi
822 initWeightedLogJaccard :: Maybe Double -> Maybe Double -> WLJParams
823 initWeightedLogJaccard (def 0 -> thr) (def 0.01 -> sens) = WLJParams thr sens
826 -- | To initialize a PhyloQueryBuild from given and default parameters
827 initPhyloQueryBuild :: Text -> Text -> Maybe Int -> Maybe Int -> Maybe Cluster -> Maybe [Metric] -> Maybe [Filter] -> Maybe Proximity -> Maybe Int -> Maybe Double -> Maybe Double -> Maybe Int -> Maybe Level -> Maybe Cluster -> PhyloQueryBuild
828 initPhyloQueryBuild name desc (def 5 -> grain) (def 3 -> steps) (def defaultFis -> cluster) (def [] -> metrics) (def [] -> filters)
829 (def defaultWeightedLogJaccard -> matching') (def 5 -> frame) (def 0.8 -> frameThr) (def 0.5 -> reBranchThr) (def 4 -> reBranchNth) (def 2 -> nthLevel) (def defaultRelatedComponents -> nthCluster) =
830 PhyloQueryBuild name desc grain steps cluster metrics filters matching' frame frameThr reBranchThr reBranchNth nthLevel nthCluster
833 -- | To initialize a PhyloQueryView default parameters
834 initPhyloQueryView :: Maybe Level -> Maybe Filiation -> Maybe Bool -> Maybe Level -> Maybe [Metric] -> Maybe [Filter] -> Maybe [Tagger] -> Maybe (Sort, Order) -> Maybe ExportMode -> Maybe DisplayMode -> Maybe Bool -> PhyloQueryView
835 initPhyloQueryView (def 2 -> lvl) (def Descendant -> f) (def False -> c) (def 1 -> d) (def [] -> ms) (def [] -> fs) (def [] -> ts) s (def Json -> em) (def Flat -> dm) (def True -> v) =
836 PhyloQueryView lvl f c d ms fs ts s em dm v
839 -- | To define some obvious boolean getters
840 shouldKeepMinorFis :: FisParams -> Bool
841 shouldKeepMinorFis = _fis_keepMinorFis
843 ----------------------------
844 -- | Default ressources | --
845 ----------------------------
849 defaultFis :: Cluster
850 defaultFis = Fis (initFis Nothing Nothing Nothing)
852 defaultLouvain :: Cluster
853 defaultLouvain = Louvain (initLouvain Nothing)
855 defaultRelatedComponents :: Cluster
856 defaultRelatedComponents = RelatedComponents (initRelatedComponents Nothing)
860 defaultLonelyBranch :: Filter
861 defaultLonelyBranch = LonelyBranch (initLonelyBranch Nothing Nothing Nothing)
863 defaultSizeBranch :: Filter
864 defaultSizeBranch = SizeBranch (initSizeBranch Nothing)
868 defaultPhyloParam :: PhyloParam
869 defaultPhyloParam = initPhyloParam Nothing Nothing Nothing
873 defaultHamming :: Proximity
874 defaultHamming = Hamming (initHamming Nothing)
876 defaultWeightedLogJaccard :: Proximity
877 defaultWeightedLogJaccard = WeightedLogJaccard (initWeightedLogJaccard Nothing Nothing)
883 defaultQueryBuild :: PhyloQueryBuild
884 defaultQueryBuild = defaultQueryBuild'
886 "An example of Phylomemy (french without accent)"
888 defaultQueryBuild' :: Title -> Desc -> PhyloQueryBuild
889 defaultQueryBuild' t d = initPhyloQueryBuild t d
890 Nothing Nothing Nothing
891 Nothing Nothing Nothing
892 Nothing Nothing Nothing
893 Nothing Nothing Nothing
895 defaultQueryView :: PhyloQueryView
896 defaultQueryView = initPhyloQueryView
897 Nothing Nothing Nothing
898 Nothing Nothing Nothing
899 Nothing Nothing Nothing
904 defaultSoftware :: Software
905 defaultSoftware = Software "Gargantext" "v4"
909 defaultPhyloVersion :: Text
910 defaultPhyloVersion = "v1"