2 Module : Gargantext.Viz.Graph.Bridgeness
3 Description : Bridgeness filter
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Let be a graph with partitions (from Louvain algo), Bridgeness uniformly
11 filters inter-communities links.
13 TODO rewrite Bridgeness with "equivalence structurale" metrics (Confluence)
15 TODO use Map LouvainNodeId (Map LouvainNodeId)
24 module Gargantext.Viz.Graph.Bridgeness (bridgeness)
27 import Data.Ord (Down(..))
28 import Gargantext.Prelude
29 import Data.Map (Map, fromListWith, lookup, fromList, toList, mapWithKey, elems)
30 import qualified Data.Map as DM
31 import Data.Maybe (catMaybes)
32 import Data.List (concat, sortOn)
33 import Data.Graph.Clustering.Louvain.Utils (LouvainNode(..))
36 -- TODO mv in Louvain Lib
37 type LouvainNodeId = Int
38 type CommunityId = Int
40 type Bridgeness = Double
43 bridgeness :: Bridgeness
45 -> Map (LouvainNodeId, LouvainNodeId) Double
46 -> Map (LouvainNodeId, LouvainNodeId) Double
47 bridgeness b ns = DM.fromList
51 . groupEdges (nodeId2comId ns)
54 nodeId2comId :: [LouvainNode] -> Map LouvainNodeId CommunityId
55 nodeId2comId ns = fromList [(nId,cId) | LouvainNode nId cId <- ns]
58 groupEdges :: Map LouvainNodeId CommunityId
59 -> Map (LouvainNodeId, LouvainNodeId) Double
60 -> Map (CommunityId, CommunityId) [((LouvainNodeId, LouvainNodeId), Double)]
61 groupEdges m = fromListWith (<>)
65 n1n2_m = (,) <$> lookup n1 m <*> lookup n2 m
66 n1n2_d = Just [((n1,n2),d)]
67 in (,) <$> n1n2_m <*> n1n2_d
71 -- | TODO : sortOn Confluence
72 filterComs :: Bridgeness
73 -> Map (CommunityId, CommunityId) [((LouvainNodeId, LouvainNodeId), Double)]
74 -> Map (CommunityId, CommunityId) [((LouvainNodeId, LouvainNodeId), Double)]
75 filterComs _b m = DM.filter (\n -> length n > 0) $ mapWithKey filter' m
80 | otherwise = take 1 $ sortOn (Down . snd) a
83 _n = round $ 100 * a' / t
84 a'= fromIntegral $ length a
86 t = fromIntegral $ length $ concat $ elems m