2 Module : Gargantext.Core.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)
14 TODO use Map LouvainNodeId (Map LouvainNodeId)
18 module Gargantext.Core.Viz.Graph.Bridgeness -- (bridgeness)
21 import Data.Graph.Clustering.Louvain.Utils (LouvainNode(..))
22 import Data.List (concat, sortOn)
23 import Data.Map (Map, fromListWith, lookup, toList, mapWithKey, elems)
24 import Data.Maybe (catMaybes)
25 import Data.Ord (Down(..))
26 import Gargantext.Prelude
27 import qualified Data.Map as DM
28 import Gargantext.Core.Viz.Graph.Tools.IGraph (ClusterNode(..))
30 ----------------------------------------------------------------------
31 type Partitions a = Map (Int, Int) Double -> IO [a]
32 ----------------------------------------------------------------------
34 nodeId2comId :: a -> (NodeId,CommunityId)
37 type CommunityId = Int
39 ----------------------------------------------------------------------
40 instance ToComId LouvainNode where
41 nodeId2comId (LouvainNode i1 i2) = (i1, i2)
43 instance ToComId ClusterNode where
44 nodeId2comId (ClusterNode i1 i2) = (i1, i2)
46 ----------------------------------------------------------------------
47 ----------------------------------------------------------------------
48 type Bridgeness = Double
50 bridgeness :: ToComId a => Bridgeness
52 -> Map (NodeId, NodeId) Double
53 -> Map (NodeId, NodeId) Double
54 bridgeness = bridgeness' nodeId2comId
57 bridgeness' :: (a -> (Int, Int))
60 -> Map (Int, Int) Double
61 -> Map (Int, Int) Double
62 bridgeness' f b ns = DM.fromList
66 . groupEdges (DM.fromList $ map f ns)
69 groupEdges :: (Ord a, Ord b1)
72 -> Map (a, a) [((b1, b1), b2)]
73 groupEdges m = fromListWith (<>)
77 n1n2_m = (,) <$> lookup n1 m <*> lookup n2 m
78 n1n2_d = Just [((n1,n2),d)]
79 in (,) <$> n1n2_m <*> n1n2_d
83 -- | TODO : sortOn Confluence
85 filterComs :: (Ord n1, Eq n2)
87 -> Map (n2, n2) [(a3, n1)]
88 -> Map (n2, n2) [(a3, n1)]
89 filterComs _b m = DM.filter (\n -> length n > 0) $ mapWithKey filter' m
94 | otherwise = take 1 $ sortOn (Down . snd) a
97 _n = round $ 100 * a' / t
98 a'= fromIntegral $ length a
100 t = fromIntegral $ length $ concat $ elems m