2 Module : Gargantext.Core.Viz.Graph.Tools
3 Description : Tools to build Graph
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 module Gargantext.Core.Viz.Graph.Tools
16 import Debug.Trace (trace)
17 import Data.Graph.Clustering.Louvain.Utils (LouvainNode(..))
18 -- import Data.Graph.Clustering.Louvain (hLouvain, {-iLouvainMap-})
19 import Data.Graph.Clustering.Louvain.CplusPlus (cLouvain)
21 import qualified Data.Set as Set
22 import Data.Text (Text)
23 import Gargantext.Prelude
24 import Gargantext.Core.Statistics
25 import Gargantext.Core.Viz.Graph
26 import Gargantext.Core.Viz.Graph.Bridgeness (bridgeness)
27 import Gargantext.Core.Viz.Graph.Distances (Distance(..), measure)
28 import Gargantext.Core.Viz.Graph.Index (createIndices, toIndex, map2mat, mat2map, Index)
29 import Gargantext.Core.Viz.Graph.IGraph (mkGraphUfromEdges)
30 import Gargantext.Core.Viz.Graph.Proxemy (confluence)
31 import GHC.Float (sin, cos)
32 import qualified IGraph as Igraph
33 import IGraph.Random -- (Gen(..))
34 import qualified IGraph.Algorithms.Layout as Layout
35 import qualified Data.Vector.Storable as Vec
36 import qualified Data.Map as Map
37 import qualified Data.List as List
38 -- import Debug.Trace (trace)
40 type Threshold = Double
43 cooc2graph' :: Ord t => Distance
46 -> Map (Index, Index) Double
47 cooc2graph' distance threshold myCooc = distanceMap
49 (ti, _) = createIndices myCooc
50 myCooc' = toIndex ti myCooc
51 matCooc = map2mat 0 (Map.size ti) $ Map.filter (> 1) myCooc'
52 distanceMat = measure distance matCooc
53 distanceMap = Map.filter (> threshold) $ mat2map distanceMat
56 cooc2graph'' :: Ord t => Distance
59 -> Map (Index, Index) Double
60 cooc2graph'' distance threshold myCooc = neighbouMap
62 (ti, _) = createIndices myCooc
63 myCooc' = toIndex ti myCooc
64 matCooc = map2mat 0 (Map.size ti) $ Map.filter (> 1) myCooc'
65 distanceMat = measure distance matCooc
66 neighbouMap = filterByNeighbours threshold
71 filterByNeighbours :: Double -> Map (Index, Index) Double -> Map (Index, Index) Double
72 filterByNeighbours threshold distanceMap = filteredMap
75 indexes = List.nub $ List.concat $ map (\(idx,idx') -> [idx,idx'] ) $ Map.keys distanceMap
76 filteredMap :: Map (Index, Index) Double
77 filteredMap = Map.fromList
80 let selected = List.reverse
84 $ Map.filterWithKey (\(from,_) _ -> idx == from) distanceMap
85 in List.take (round threshold) selected
89 cooc2graph :: Distance
91 -> (Map (Text, Text) Int)
93 cooc2graph distance threshold myCooc = do
94 printDebug "cooc2graph" distance
96 (ti, _) = createIndices myCooc
97 myCooc' = toIndex ti myCooc
98 matCooc = map2mat 0 (Map.size ti)
99 $ Map.filterWithKey (\(a,b) _ -> a /= b)
100 $ Map.filter (> 1) myCooc'
101 distanceMat = measure distance matCooc
102 distanceMap = Map.filter (> threshold) $ mat2map distanceMat
107 (as, bs) = List.unzip $ Map.keys distanceMap
108 n' = Set.size $ Set.fromList $ as <> bs
109 ClustersParams rivers level = clustersParams nodesApprox
112 partitions <- if (Map.size distanceMap > 0)
113 -- then iLouvainMap 100 10 distanceMap
114 -- then hLouvain distanceMap
115 then cLouvain level distanceMap
116 else panic "Text.Flow: DistanceMap is empty"
119 -- bridgeness' = distanceMap
120 bridgeness' = trace ("Rivers: " <> show rivers)
121 $ bridgeness rivers partitions distanceMap
122 confluence' = confluence (Map.keys bridgeness') 3 True False
124 pure $ data2graph (Map.toList ti) myCooc' bridgeness' confluence' partitions
128 data ClustersParams = ClustersParams { bridgness :: Double
132 clustersParams :: Int -> ClustersParams
133 clustersParams x = ClustersParams (fromIntegral x) "0.00000001" -- y
135 y | x < 100 = "0.000001"
136 | x < 350 = "0.000001"
137 | x < 500 = "0.000001"
138 | x < 1000 = "0.000001"
142 ----------------------------------------------------------
143 -- | From data to Graph
144 data2graph :: [(Text, Int)]
145 -> Map (Int, Int) Int
146 -> Map (Int, Int) Double
147 -> Map (Int, Int) Double
150 data2graph labels coocs bridge conf partitions = Graph nodes edges Nothing
153 community_id_by_node_id = Map.fromList [ (n, c) | LouvainNode n c <- partitions ]
155 nodes = map (setCoord ForceAtlas labels bridge)
156 [ (n, Node { node_size = maybe 0 identity (Map.lookup (n,n) coocs)
157 , node_type = Terms -- or Unknown
158 , node_id = cs (show n)
163 Attributes { clust_default = maybe 0 identity
164 (Map.lookup n community_id_by_node_id) } }
167 , Set.member n $ Set.fromList
169 $ map (\((s,t),d) -> if d > 0 && s /=t then [s,t] else [])
173 edges = [ Edge { edge_source = cs (show s)
174 , edge_target = cs (show t)
176 , edge_confluence = maybe 0 identity $ Map.lookup (s,t) conf
177 -- , edge_confluence = maybe (panic "E: data2graph edges") identity $ Map.lookup (s,t) conf
178 , edge_id = cs (show i) }
179 | (i, ((s,t), d)) <- zip ([0..]::[Integer]) (Map.toList bridge), s /= t, d > 0
183 ------------------------------------------------------------------------
185 data Layout = KamadaKawai | ACP | ForceAtlas
188 setCoord' :: (Int -> (Double, Double)) -> (Int, Node) -> Node
189 setCoord' f (i,n) = n { node_x_coord = x, node_y_coord = y }
195 setCoord :: Ord a => Layout -> [(a, Int)] -> Map (Int, Int) Double -> (Int, Node) -> Node
196 setCoord l labels m (n,node) = node { node_x_coord = x
200 (x,y) = getCoord l labels m n
206 -> Map (Int, Int) Double
209 getCoord KamadaKawai _ _m _n = undefined -- layout m n
211 getCoord ForceAtlas _ _ n = (sin d, cos d)
215 getCoord ACP labels m n = to2d $ maybe (panic "Graph.Tools no coordinate") identity
217 $ pcaReduceTo (Dimension 2)
220 to2d :: Vec.Vector Double -> (Double, Double)
223 ds = take 2 $ Vec.toList v
227 mapArray :: Ord a => [(a, Int)] -> Map (Int, Int) Double -> Map Int (Vec.Vector Double)
228 mapArray items m' = Map.fromList [ toVec n' ns m' | n' <- ns ]
232 toVec :: Int -> [Int] -> Map (Int,Int) Double -> (Int, Vec.Vector Double)
233 toVec n' ns' m' = (n', Vec.fromList $ map (\n'' -> maybe 0 identity $ Map.lookup (n',n'') m') ns')
234 ------------------------------------------------------------------------
236 -- | KamadaKawai Layout
237 -- TODO TEST: check labels, nodeId and coordinates
238 layout :: Map (Int, Int) Double -> Int -> Gen -> (Double, Double)
239 layout m n gen = maybe (panic "") identity $ Map.lookup n $ coord
241 coord :: (Map Int (Double,Double))
242 coord = Map.fromList $ List.zip (Igraph.nodes g) $ (Layout.layout g p gen)
243 --p = Layout.defaultLGL
244 p = Layout.kamadaKawai
245 g = mkGraphUfromEdges $ map fst $ List.filter (\e -> snd e > 0) $ Map.toList m