2 Module : Gargantext.Viz.Phylo.PhyloTools
3 Description : Module dedicated to all the tools needed for making a Phylo
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# LANGUAGE ViewPatterns #-}
13 module Gargantext.Viz.Phylo.PhyloTools where
15 import Data.Vector (Vector, elemIndex)
16 import Data.List (sort, concat, null, union, (++), tails, sortOn, nub, init, tail, partition, tails, nubBy)
17 import Data.Set (Set, disjoint)
18 import Data.Map (Map, elems, fromList, unionWith, keys, member, (!), filterWithKey, fromListWith, empty)
19 import Data.String (String)
20 import Data.Text (Text, unwords)
22 import Gargantext.Prelude
23 import Gargantext.Viz.AdaptativePhylo
27 import Debug.Trace (trace)
28 import Control.Lens hiding (Level)
30 import qualified Data.Vector as Vector
31 import qualified Data.List as List
32 import qualified Data.Set as Set
33 import qualified Data.Map as Map
39 -- | To print an important message as an IO()
40 printIOMsg :: String -> IO ()
45 <> "-- | " <> msg <> "\n" )
48 -- | To print a comment as an IO()
49 printIOComment :: String -> IO ()
51 putStrLn ( "\n" <> cmt <> "\n" )
59 roundToStr :: (PrintfArg a, Floating a) => Int -> a -> String
60 roundToStr = printf "%0.*f"
63 countSup :: Double -> [Double] -> Int
64 countSup s l = length $ filter (>s) l
67 dropByIdx :: Int -> [a] -> [a]
68 dropByIdx k l = take k l ++ drop (k+1) l
71 elemIndex' :: Eq a => a -> [a] -> Int
72 elemIndex' e l = case (List.elemIndex e l) of
73 Nothing -> panic ("[ERR][Viz.Phylo.PhyloTools] element not in list")
77 commonPrefix :: Eq a => [a] -> [a] -> [a] -> [a]
78 commonPrefix lst lst' acc =
79 if (null lst || null lst')
81 else if (head' "commonPrefix" lst == head' "commonPrefix" lst')
82 then commonPrefix (tail lst) (tail lst') (acc ++ [head' "commonPrefix" lst])
91 -- | Is this Ngrams a Foundations Root ?
92 isRoots :: Ngrams -> Vector Ngrams -> Bool
93 isRoots n ns = Vector.elem n ns
95 -- | To transform a list of nrams into a list of foundation's index
96 ngramsToIdx :: [Ngrams] -> Vector Ngrams -> [Int]
97 ngramsToIdx ns fdt = map (\n -> fromJust $ elemIndex n fdt) ns
99 -- | To transform a list of Ngrams Indexes into a Label
100 ngramsToLabel :: Vector Ngrams -> [Int] -> Text
101 ngramsToLabel ngrams l = unwords $ tail' "ngramsToLabel" $ concat $ map (\n -> ["|",n]) $ ngramsToText ngrams l
104 -- | To transform a list of Ngrams Indexes into a list of Text
105 ngramsToText :: Vector Ngrams -> [Int] -> [Text]
106 ngramsToText ngrams l = map (\idx -> ngrams Vector.! idx) l
113 -- | To transform a list of periods into a set of Dates
114 periodsToYears :: [(Date,Date)] -> Set Date
115 periodsToYears periods = (Set.fromList . sort . concat)
116 $ map (\(d,d') -> [d..d']) periods
119 findBounds :: [Date] -> (Date,Date)
121 let dates' = sort dates
122 in (head' "findBounds" dates', last' "findBounds" dates')
125 toPeriods :: [Date] -> Int -> Int -> [(Date,Date)]
126 toPeriods dates p s =
127 let (start,end) = findBounds dates
128 in map (\dates' -> (head' "toPeriods" dates', last' "toPeriods" dates'))
129 $ chunkAlong p s [start .. end]
132 -- | Get a regular & ascendante timeScale from a given list of dates
133 toTimeScale :: [Date] -> Int -> [Date]
134 toTimeScale dates step =
135 let (start,end) = findBounds dates
136 in [start, (start + step) .. end]
139 getTimeStep :: TimeUnit -> Int
140 getTimeStep time = case time of
143 getTimePeriod :: TimeUnit -> Int
144 getTimePeriod time = case time of
147 getTimeFrame :: TimeUnit -> Int
148 getTimeFrame time = case time of
156 -- | To find if l' is nested in l
157 isNested :: Eq a => [a] -> [a] -> Bool
160 | length l' > length l = False
161 | (union l l') == l = True
165 -- | To filter Fis with small Support but by keeping non empty Periods
166 keepFilled :: (Int -> [a] -> [a]) -> Int -> [a] -> [a]
167 keepFilled f thr l = if (null $ f thr l) && (not $ null l)
168 then keepFilled f (thr - 1) l
172 traceClique :: Map (Date, Date) [PhyloClique] -> String
173 traceClique mFis = foldl (\msg cpt -> msg <> show (countSup cpt cliques) <> " (>" <> show (cpt) <> ") " ) "" [1..6]
175 --------------------------------------
177 cliques = sort $ map (fromIntegral . length . _phyloClique_nodes) $ concat $ elems mFis
178 --------------------------------------
181 traceSupport :: Map (Date, Date) [PhyloClique] -> String
182 traceSupport mFis = foldl (\msg cpt -> msg <> show (countSup cpt supports) <> " (>" <> show (cpt) <> ") " ) "" [1..6]
184 --------------------------------------
186 supports = sort $ map (fromIntegral . _phyloClique_support) $ concat $ elems mFis
187 --------------------------------------
190 traceFis :: [Char] -> Map (Date, Date) [PhyloClique] -> Map (Date, Date) [PhyloClique]
191 traceFis msg mFis = trace ( "\n" <> "-- | " <> msg <> " : " <> show (sum $ map length $ elems mFis) <> "\n"
192 <> "Support : " <> (traceSupport mFis) <> "\n"
193 <> "Nb Ngrams : " <> (traceClique mFis) <> "\n" ) mFis
201 getCliqueSupport :: Clique -> Int
202 getCliqueSupport unit = case unit of
206 getCliqueSize :: Clique -> Int
207 getCliqueSize unit = case unit of
216 listToCombi' :: [a] -> [(a,a)]
217 listToCombi' l = [(x,y) | (x:rest) <- tails l, y <- rest]
219 listToEqual' :: Eq a => [a] -> [(a,a)]
220 listToEqual' l = [(x,y) | x <- l, y <- l, x == y]
222 listToKeys :: Eq a => [a] -> [(a,a)]
223 listToKeys lst = (listToCombi' lst) ++ (listToEqual' lst)
225 listToMatrix :: [Int] -> Map (Int,Int) Double
226 listToMatrix lst = fromList $ map (\k -> (k,1)) $ listToKeys $ sort lst
228 listToMatrix' :: [Ngrams] -> Map (Ngrams,Ngrams) Int
229 listToMatrix' lst = fromList $ map (\k -> (k,1)) $ listToKeys $ sort lst
231 listToSeq :: Eq a => [a] -> [(a,a)]
232 listToSeq l = nubBy (\x y -> fst x == fst y) $ [ (x,y) | (x:rest) <- tails l, y <- rest ]
234 sumCooc :: Cooc -> Cooc -> Cooc
235 sumCooc cooc cooc' = unionWith (+) cooc cooc'
237 getTrace :: Cooc -> Double
238 getTrace cooc = sum $ elems $ filterWithKey (\(k,k') _ -> k == k') cooc
240 coocToDiago :: Cooc -> Cooc
241 coocToDiago cooc = filterWithKey (\(k,k') _ -> k == k') cooc
243 -- | To build the local cooc matrix of each phylogroup
244 ngramsToCooc :: [Int] -> [Cooc] -> Cooc
245 ngramsToCooc ngrams coocs =
246 let cooc = foldl (\acc cooc' -> sumCooc acc cooc') empty coocs
247 pairs = listToKeys ngrams
248 in filterWithKey (\k _ -> elem k pairs) cooc
255 getGroupId :: PhyloGroup -> PhyloGroupId
256 getGroupId group = ((group ^. phylo_groupPeriod, group ^. phylo_groupLevel), group ^. phylo_groupIndex)
258 idToPrd :: PhyloGroupId -> PhyloPeriodId
259 idToPrd id = (fst . fst) id
261 getGroupThr :: PhyloGroup -> Double
262 getGroupThr group = last' "getGroupThr" ((group ^. phylo_groupMeta) ! "breaks")
264 groupByField :: Ord a => (PhyloGroup -> a) -> [PhyloGroup] -> Map a [PhyloGroup]
265 groupByField toField groups = fromListWith (++) $ map (\g -> (toField g, [g])) groups
267 getPeriodPointers :: Filiation -> PhyloGroup -> [Pointer]
268 getPeriodPointers fil group =
270 ToChilds -> group ^. phylo_groupPeriodChilds
271 ToParents -> group ^. phylo_groupPeriodParents
273 filterProximity :: Proximity -> Double -> Double -> Bool
274 filterProximity proximity thr local =
276 WeightedLogJaccard _ -> local >= thr
279 getProximityName :: Proximity -> String
280 getProximityName proximity =
282 WeightedLogJaccard _ -> "WLJaccard"
289 addPointers :: Filiation -> PointerType -> [Pointer] -> PhyloGroup -> PhyloGroup
290 addPointers fil pty pointers group =
292 TemporalPointer -> case fil of
293 ToChilds -> group & phylo_groupPeriodChilds .~ pointers
294 ToParents -> group & phylo_groupPeriodParents .~ pointers
295 LevelPointer -> case fil of
296 ToChilds -> group & phylo_groupLevelChilds .~ pointers
297 ToParents -> group & phylo_groupLevelParents .~ pointers
300 getPeriodIds :: Phylo -> [(Date,Date)]
301 getPeriodIds phylo = sortOn fst
303 $ phylo ^. phylo_periods
305 getLevelParentId :: PhyloGroup -> PhyloGroupId
306 getLevelParentId g = fst $ head' "getLevelParentId" $ g ^. phylo_groupLevelParents
308 getLastLevel :: Phylo -> Level
309 getLastLevel phylo = last' "lastLevel" $ getLevels phylo
311 getLevels :: Phylo -> [Level]
312 getLevels phylo = nub
314 $ keys $ view ( phylo_periods
316 . phylo_periodLevels ) phylo
318 getSeaElevation :: Phylo -> SeaElevation
319 getSeaElevation phylo = seaElevation (getConfig phylo)
322 getConfig :: Phylo -> Config
323 getConfig phylo = (phylo ^. phylo_param) ^. phyloParam_config
326 getRoots :: Phylo -> Vector Ngrams
327 getRoots phylo = (phylo ^. phylo_foundations) ^. foundations_roots
329 phyloToLastBranches :: Phylo -> [[PhyloGroup]]
330 phyloToLastBranches phylo = elems
332 $ map (\g -> (g ^. phylo_groupBranchId, [g]))
333 $ getGroupsFromLevel (last' "byBranches" $ getLevels phylo) phylo
335 getGroupsFromLevel :: Level -> Phylo -> [PhyloGroup]
336 getGroupsFromLevel lvl phylo =
337 elems $ view ( phylo_periods
341 . filtered (\phyloLvl -> phyloLvl ^. phylo_levelLevel == lvl)
342 . phylo_levelGroups ) phylo
345 getGroupsFromLevelPeriods :: Level -> [PhyloPeriodId] -> Phylo -> [PhyloGroup]
346 getGroupsFromLevelPeriods lvl periods phylo =
347 elems $ view ( phylo_periods
349 . filtered (\phyloPrd -> elem (phyloPrd ^. phylo_periodPeriod) periods)
352 . filtered (\phyloLvl -> phyloLvl ^. phylo_levelLevel == lvl)
353 . phylo_levelGroups ) phylo
356 getGroupsFromPeriods :: Level -> Map PhyloPeriodId PhyloPeriod -> [PhyloGroup]
357 getGroupsFromPeriods lvl periods =
358 elems $ view ( traverse
361 . filtered (\phyloLvl -> phyloLvl ^. phylo_levelLevel == lvl)
362 . phylo_levelGroups ) periods
365 updatePhyloGroups :: Level -> Map PhyloGroupId PhyloGroup -> Phylo -> Phylo
366 updatePhyloGroups lvl m phylo =
371 . filtered (\phyloLvl -> phyloLvl ^. phylo_levelLevel == lvl)
375 let id = getGroupId group
382 traceToPhylo :: Level -> Phylo -> Phylo
383 traceToPhylo lvl phylo =
384 trace ("\n" <> "-- | End of phylo making at level " <> show (lvl) <> " with "
385 <> show (length $ getGroupsFromLevel lvl phylo) <> " groups and "
386 <> show (length $ nub $ map _phylo_groupBranchId $ getGroupsFromLevel lvl phylo) <> " branches" <> "\n") phylo
392 relatedComponents :: Ord a => [[a]] -> [[a]]
393 relatedComponents graph = foldl' (\acc groups ->
397 let acc' = partition (\groups' -> disjoint (Set.fromList groups') (Set.fromList groups)) acc
398 in (fst acc') ++ [nub $ concat $ (snd acc') ++ [groups]]) [] graph
400 toRelatedComponents :: [PhyloGroup] -> [((PhyloGroup,PhyloGroup),Double)] -> [[PhyloGroup]]
401 toRelatedComponents nodes edges =
402 let ref = fromList $ map (\g -> (getGroupId g, g)) nodes
403 clusters = relatedComponents $ ((map (\((g,g'),_) -> [getGroupId g, getGroupId g']) edges) ++ (map (\g -> [getGroupId g]) nodes))
404 in map (\cluster -> map (\gId -> ref ! gId) cluster) clusters
407 traceSynchronyEnd :: Phylo -> Phylo
408 traceSynchronyEnd phylo =
409 trace ( "\n" <> "-- | End synchronic clustering at level " <> show (getLastLevel phylo)
410 <> " with " <> show (length $ getGroupsFromLevel (getLastLevel phylo) phylo) <> " groups"
411 <> " and " <> show (length $ nub $ map _phylo_groupBranchId $ getGroupsFromLevel (getLastLevel phylo) phylo) <> " branches"
414 traceSynchronyStart :: Phylo -> Phylo
415 traceSynchronyStart phylo =
416 trace ( "\n" <> "-- | Start synchronic clustering at level " <> show (getLastLevel phylo)
417 <> " with " <> show (length $ getGroupsFromLevel (getLastLevel phylo) phylo) <> " groups"
418 <> " and " <> show (length $ nub $ map _phylo_groupBranchId $ getGroupsFromLevel (getLastLevel phylo) phylo) <> " branches"
426 getSensibility :: Proximity -> Double
427 getSensibility proxi = case proxi of
428 WeightedLogJaccard s -> s
435 intersectInit :: Eq a => [a] -> [a] -> [a] -> [a]
436 intersectInit acc lst lst' =
437 if (null lst) || (null lst')
439 else if (head' "intersectInit" lst) == (head' "intersectInit" lst')
440 then intersectInit (acc ++ [head' "intersectInit" lst]) (tail lst) (tail lst')
443 branchIdsToProximity :: PhyloBranchId -> PhyloBranchId -> Double -> Double -> Double
444 branchIdsToProximity id id' thrInit thrStep = thrInit + thrStep * (fromIntegral $ length $ intersectInit [] (snd id) (snd id'))
446 ngramsInBranches :: [[PhyloGroup]] -> [Int]
447 ngramsInBranches branches = nub $ foldl (\acc g -> acc ++ (g ^. phylo_groupNgrams)) [] $ concat branches
450 traceMatchSuccess :: Double -> Double -> Double -> [[[PhyloGroup]]] -> [[[PhyloGroup]]]
451 traceMatchSuccess thr qua qua' nextBranches =
452 trace ( "\n" <> "-- local branches : " <> (init $ show ((init . init . snd)
453 $ (head' "trace" $ head' "trace" $ head' "trace" nextBranches) ^. phylo_groupBranchId))
454 <> ",(1.." <> show (length nextBranches) <> ")]"
455 <> " | " <> show ((length . concat . concat) nextBranches) <> " groups" <> "\n"
456 <> " - splited with success in " <> show (map length nextBranches) <> " sub-branches" <> "\n"
457 <> " - for the local threshold " <> show (thr) <> " ( quality : " <> show (qua) <> " < " <> show(qua') <> ")\n" ) nextBranches
460 traceMatchFailure :: Double -> Double -> Double -> [[PhyloGroup]] -> [[PhyloGroup]]
461 traceMatchFailure thr qua qua' branches =
462 trace ( "\n" <> "-- local branches : " <> (init $ show ((init . snd) $ (head' "trace" $ head' "trace" branches) ^. phylo_groupBranchId))
463 <> ",(1.." <> show (length branches) <> ")]"
464 <> " | " <> show (length $ concat branches) <> " groups" <> "\n"
465 <> " - split with failure for the local threshold " <> show (thr) <> " ( quality : " <> show (qua) <> " > " <> show(qua') <> ")\n"
469 traceMatchNoSplit :: [[PhyloGroup]] -> [[PhyloGroup]]
470 traceMatchNoSplit branches =
471 trace ( "\n" <> "-- local branches : " <> (init $ show ((init . snd) $ (head' "trace" $ head' "trace" branches) ^. phylo_groupBranchId))
472 <> ",(1.." <> show (length branches) <> ")]"
473 <> " | " <> show (length $ concat branches) <> " groups" <> "\n"
474 <> " - unable to split in smaller branches" <> "\n"
478 traceMatchLimit :: [[PhyloGroup]] -> [[PhyloGroup]]
479 traceMatchLimit branches =
480 trace ( "\n" <> "-- local branches : " <> (init $ show ((init . snd) $ (head' "trace" $ head' "trace" branches) ^. phylo_groupBranchId))
481 <> ",(1.." <> show (length branches) <> ")]"
482 <> " | " <> show (length $ concat branches) <> " groups" <> "\n"
483 <> " - unable to increase the threshold above 1" <> "\n"
487 traceMatchEnd :: [PhyloGroup] -> [PhyloGroup]
488 traceMatchEnd groups =
489 trace ("\n" <> "-- | End temporal matching with " <> show (length $ nub $ map (\g -> g ^. phylo_groupBranchId) groups)
490 <> " branches and " <> show (length groups) <> " groups" <> "\n") groups
493 traceTemporalMatching :: [PhyloGroup] -> [PhyloGroup]
494 traceTemporalMatching groups =
495 trace ( "\n" <> "-- | Start temporal matching for " <> show(length groups) <> " groups" <> "\n") groups
498 traceGroupsProxi :: Map (PhyloGroupId,PhyloGroupId) Double -> Map (PhyloGroupId,PhyloGroupId) Double
500 trace ( "\n" <> "-- | " <> show(Map.size m) <> " computed pairs of groups proximity" <> "\n") m