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)
23 {-# LANGUAGE NoImplicitPrelude #-}
25 module Gargantext.Viz.Graph.Bridgeness (bridgeness)
28 import Data.Ord (Down(..))
29 import Gargantext.Prelude
30 import Data.Map (Map, fromListWith, lookup, fromList, toList, mapWithKey, elems)
31 import qualified Data.Map as DM
32 import Data.Maybe (catMaybes)
33 import Data.List (concat, sortOn)
34 import Data.Graph.Clustering.Louvain.Utils (LouvainNode(..))
37 -- TODO mv in Louvain Lib
38 type LouvainNodeId = Int
39 type CommunityId = Int
41 type Bridgeness = Double
44 bridgeness :: Bridgeness
46 -> Map (LouvainNodeId, LouvainNodeId) Double
47 -> Map (LouvainNodeId, LouvainNodeId) Double
48 bridgeness b ns = DM.fromList
52 . groupEdges (nodeId2comId ns)
55 nodeId2comId :: [LouvainNode] -> Map LouvainNodeId CommunityId
56 nodeId2comId ns = fromList [(nId,cId) | LouvainNode nId cId <- ns]
59 groupEdges :: Map LouvainNodeId CommunityId
60 -> Map (LouvainNodeId, LouvainNodeId) Double
61 -> Map (CommunityId, CommunityId) [((LouvainNodeId, LouvainNodeId), Double)]
62 groupEdges m = fromListWith (<>)
66 n1n2_m = (,) <$> lookup n1 m <*> lookup n2 m
67 n1n2_d = Just [((n1,n2),d)]
68 in (,) <$> n1n2_m <*> n1n2_d
72 -- | TODO : sortOn Confluence
73 filterComs :: Bridgeness
74 -> Map (CommunityId, CommunityId) [((LouvainNodeId, LouvainNodeId), Double)]
75 -> Map (CommunityId, CommunityId) [((LouvainNodeId, LouvainNodeId), Double)]
76 filterComs _b m = DM.filter (\n -> length n > 0) $ mapWithKey filter' m
81 | otherwise = take 1 $ sortOn (Down . snd) a
84 _n = round $ 100 * a' / t
85 a'= fromIntegral $ length a
87 t = fromIntegral $ length $ concat $ elems m