2 Module : Gargantext.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
12 {-# LANGUAGE NoImplicitPrelude #-}
13 {-# LANGUAGE OverloadedStrings #-}
15 module Gargantext.Viz.Graph.Tools
18 -- import Debug.Trace (trace)
19 import Data.Graph.Clustering.Louvain.CplusPlus (LouvainNode(..))
20 import Data.Graph.Clustering.Louvain.CplusPlus (cLouvain)
22 import Data.Text (Text)
23 import Gargantext.Prelude
24 import Gargantext.Core.Statistics
25 import Gargantext.Viz.Graph
26 --import Gargantext.Viz.Graph.Bridgeness (bridgeness)
27 import Gargantext.Viz.Graph.Distances.Matrice (measureConditional)
28 import Gargantext.Viz.Graph.Index (createIndices, toIndex, map2mat, mat2map)
29 import Gargantext.Viz.Graph.Proxemy (mkGraphUfromEdges)
30 import GHC.Float (sin, cos)
31 import qualified IGraph as Igraph
32 import qualified IGraph.Algorithms.Layout as Layout
33 import qualified Data.Vector.Storable as Vec
34 import qualified Data.Map as Map
35 import qualified Data.List as List
37 cooc2graph :: (Map (Text, Text) Int) -> IO Graph
38 cooc2graph myCooc = do
39 let (ti, _) = createIndices myCooc
40 myCooc4 = toIndex ti myCooc
41 matCooc = map2mat (0) (Map.size ti) myCooc4
42 distanceMat = measureConditional matCooc
43 distanceMap = Map.map (\_ -> 1) $ Map.filter (>0) $ mat2map distanceMat
45 partitions <- case Map.size distanceMap > 0 of
46 True -> cLouvain distanceMap
47 False -> panic "Text.Flow: DistanceMap is empty"
49 let distanceMap' = distanceMap -- bridgeness 300 partitions distanceMap
51 data2graph (Map.toList ti) myCooc4 distanceMap' partitions
54 ----------------------------------------------------------
55 -- | From data to Graph
56 -- FIXME: distance should not be a map since we just "toList" it (same as cLouvain)
57 data2graph :: [(Text, Int)] -> Map (Int, Int) Int
58 -> Map (Int, Int) Double
61 data2graph labels coocs distance partitions = do
63 let community_id_by_node_id = Map.fromList [ (n, c) | LouvainNode n c <- partitions ]
65 nodes <- mapM (setCoord ForceAtlas labels distance)
66 [ (n, Node { node_size = maybe 0 identity (Map.lookup (n,n) coocs)
67 , node_type = Terms -- or Unknown
68 , node_id = cs (show n)
73 Attributes { clust_default = maybe 0 identity
74 (Map.lookup n community_id_by_node_id) } }
79 let edges = [ Edge { edge_source = cs (show s)
80 , edge_target = cs (show t)
82 , edge_id = cs (show i) }
83 | (i, ((s,t), w)) <- zip ([0..]::[Integer]) (Map.toList distance) ]
85 pure $ Graph nodes edges Nothing
87 ------------------------------------------------------------------------
89 data Layout = KamadaKawai | ACP | ForceAtlas
92 setCoord' :: (Int -> (Double, Double)) -> (Int, Node) -> Node
93 setCoord' f (i,n) = n { node_x_coord = x, node_y_coord = y }
99 setCoord :: Ord a => Layout -> [(a, Int)] -> Map (Int, Int) Double -> (Int, Node) -> IO Node
100 setCoord l labels m (n,node) = getCoord l labels m n
101 >>= \(x,y) -> pure $ node { node_x_coord = x
106 getCoord :: Ord a => Layout
107 -> [(a, Int)] -> Map (Int, Int) Double -> Int -> IO (Double, Double)
108 getCoord KamadaKawai _ m n = layout m n
110 getCoord ForceAtlas _ _ n = pure (sin d, cos d)
114 getCoord ACP labels m n = pure $ to2d $ maybe (panic "Graph.Tools no coordinate") identity
116 $ pcaReduceTo (Dimension 2)
119 to2d :: Vec.Vector Double -> (Double, Double)
122 ds = take 2 $ Vec.toList v
126 mapArray :: Ord a => [(a, Int)] -> Map (Int, Int) Double -> Map Int (Vec.Vector Double)
127 mapArray items m' = Map.fromList [ toVec n' ns m' | n' <- ns ]
131 toVec :: Int -> [Int] -> Map (Int,Int) Double -> (Int, Vec.Vector Double)
132 toVec n' ns' m' = (n', Vec.fromList $ map (\n'' -> maybe 0 identity $ Map.lookup (n',n'') m') ns')
133 ------------------------------------------------------------------------
135 -- | KamadaKawai Layout
136 -- TODO TEST: check labels, nodeId and coordinates
137 layout :: Map (Int, Int) Double -> Int -> IO (Double, Double)
138 layout m n = maybe (panic "") identity <$> Map.lookup n <$> coord
140 coord :: IO (Map Int (Double,Double))
141 coord = Map.fromList <$> List.zip (Igraph.nodes g) <$> (Layout.getLayout g p)
142 --p = Layout.defaultLGL
143 p = Layout.defaultKamadaKawai
144 g = mkGraphUfromEdges $ map fst $ List.filter (\e -> snd e > 0) $ Map.toList m