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 FlexibleContexts #-}
12 {-# LANGUAGE NoImplicitPrelude #-}
13 {-# LANGUAGE OverloadedStrings #-}
14 {-# LANGUAGE RankNTypes #-}
15 {-# LANGUAGE ViewPatterns #-}
17 module Gargantext.Viz.Phylo.PhyloTools where
19 import Data.Vector (Vector, elemIndex)
20 import Data.List (sort, concat, null, union, (++), tails, sortOn)
21 import Data.Set (size)
22 import Data.Map (Map, elems, fromList, unionWith, keys)
23 import Data.String (String)
25 import Gargantext.Prelude
26 import Gargantext.Viz.AdaptativePhylo
28 import Debug.Trace (trace)
31 import qualified Data.Vector as Vector
38 countSup :: Double -> [Double] -> Int
39 countSup s l = length $ filter (>s) l
47 -- | Is this Ngrams a Foundations Root ?
48 isRoots :: Ngrams -> Vector Ngrams -> Bool
49 isRoots n ns = Vector.elem n ns
51 -- | To transform a list of nrams into a list of foundation's index
52 ngramsToIdx :: [Ngrams] -> Vector Ngrams -> [Int]
53 ngramsToIdx ns fdt = map (\n -> fromJust $ elemIndex n fdt) ns
61 findBounds :: [Date] -> (Date,Date)
63 let dates' = sort dates
64 in (head' "findBounds" dates', last' "findBounds" dates')
67 toPeriods :: [Date] -> Int -> Int -> [(Date,Date)]
69 let (start,end) = findBounds dates
70 in map (\dates' -> (head' "toPeriods" dates', last' "toPeriods" dates'))
71 $ chunkAlong p s [start .. end]
74 -- | Get a regular & ascendante timeScale from a given list of dates
75 toTimeScale :: [Date] -> Int -> [Date]
76 toTimeScale dates step =
77 let (start,end) = findBounds dates
78 in [start, (start + step) .. end]
86 -- | To find if l' is nested in l
87 isNested :: Eq a => [a] -> [a] -> Bool
90 | length l' > length l = False
91 | (union l l') == l = True
95 -- | To filter Fis with small Support but by keeping non empty Periods
96 keepFilled :: (Int -> [a] -> [a]) -> Int -> [a] -> [a]
97 keepFilled f thr l = if (null $ f thr l) && (not $ null l)
98 then keepFilled f (thr - 1) l
102 traceClique :: Map (Date, Date) [PhyloFis] -> String
103 traceClique mFis = foldl (\msg cpt -> msg <> show (countSup cpt cliques) <> " (>" <> show (cpt) <> ") " ) "" [1..6]
105 --------------------------------------
107 cliques = sort $ map (fromIntegral . size . _phyloFis_clique) $ concat $ elems mFis
108 --------------------------------------
111 traceSupport :: Map (Date, Date) [PhyloFis] -> String
112 traceSupport mFis = foldl (\msg cpt -> msg <> show (countSup cpt supports) <> " (>" <> show (cpt) <> ") " ) "" [1..6]
114 --------------------------------------
116 supports = sort $ map (fromIntegral . _phyloFis_support) $ concat $ elems mFis
117 --------------------------------------
120 traceFis :: [Char] -> Map (Date, Date) [PhyloFis] -> Map (Date, Date) [PhyloFis]
121 traceFis msg mFis = trace ( "\n" <> "-- | " <> msg <> " : " <> show (sum $ map length $ elems mFis) <> "\n"
122 <> "Support : " <> (traceSupport mFis) <> "\n"
123 <> "Clique : " <> (traceClique mFis) <> "\n" ) mFis
131 listToCombi' :: [a] -> [(a,a)]
132 listToCombi' l = [(x,y) | (x:rest) <- tails l, y <- rest]
134 listToEqual' :: Eq a => [a] -> [(a,a)]
135 listToEqual' l = [(x,y) | x <- l, y <- l, x == y]
137 listToKeys :: [Int] -> [(Int,Int)]
138 listToKeys lst = (listToCombi' lst) ++ (listToEqual' lst)
140 listToMatrix :: [Int] -> Map (Int,Int) Double
141 listToMatrix lst = fromList $ map (\k -> (k,1)) $ listToKeys $ sort lst
143 sumCooc :: Cooc -> Cooc -> Cooc
144 sumCooc cooc cooc' = unionWith (+) cooc cooc'
151 getPeriodIds :: Phylo -> [(Date,Date)]
152 getPeriodIds phylo = sortOn fst
154 $ phylo ^. phylo_periods
157 getConfig :: Phylo -> Config
158 getConfig phylo = (phylo ^. phylo_param) ^. phyloParam_config
161 getRoots :: Phylo -> Vector Ngrams
162 getRoots phylo = (phylo ^. phylo_foundations) ^. foundations_roots