]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Graph/FGL.hs
[DB/FACTO] G.D.S.Prelude (with issue)
[gargantext.git] / src / Gargantext / Viz / Graph / FGL.hs
1 {-| Module : Gargantext.Viz.Graph.FGL
2 Description : FGL main functions used in Garg
3 Copyright : (c) CNRS, 2017-Present
4 License : AGPL + CECILL v3
5 Maintainer : team@gargantext.org
6 Stability : experimental
7 Portability : POSIX
8
9 Main FGL funs/types to ease portability with IGraph.
10
11 -}
12
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE ConstraintKinds #-}
15
16 module Gargantext.Viz.Graph.FGL where
17
18 import Gargantext.Prelude
19 import qualified Data.Graph.Inductive as FGL
20 import Data.List as List
21 ------------------------------------------------------------------
22 -- | Main Types
23
24 type Graph_Undirected = FGL.Gr () ()
25 type Graph_Directed = FGL.Gr () ()
26
27 type Graph = FGL.Graph
28 type Node = FGL.Node -- Int
29 type Edge = FGL.Edge -- (Int, Int)
30
31 ------------------------------------------------------------------
32 -- | Main Functions
33 mkGraph :: [Node] -> [Edge] -> Graph_Undirected
34 mkGraph = FGL.mkUGraph
35
36 neighbors :: Graph gr => gr a b -> Node -> [Node]
37 neighbors = FGL.neighbors
38
39 -- | TODO bug: if graph is undirected, we need to filter
40 -- nub . (map (\(n1,n2) -> if n1 < n2 then (n1,n2) else (n2,n1))) . FGL.edges
41 edges :: Graph gr => gr a b -> [Edge]
42 edges = FGL.edges
43
44 nodes :: Graph gr => gr a b -> [Node]
45 nodes = FGL.nodes
46
47 ------------------------------------------------------------------------
48 -- | Graph Tools
49
50 filterNeighbors :: Graph_Undirected -> Node -> [Node]
51 filterNeighbors g n = List.nub $ neighbors g n
52
53 -- Q: why not D.G.I.deg ? (Int as result)
54 degree :: Graph_Undirected -> Node -> Double
55 degree g n = fromIntegral $ List.length (filterNeighbors g n)
56
57 vcount :: Graph_Undirected -> Double
58 vcount = fromIntegral . List.length . List.nub . nodes
59
60 -- | TODO tests, optim and use IGraph library, fix IO ?
61 ecount :: Graph_Undirected -> Double
62 ecount = fromIntegral . List.length . List.nub . edges
63
64
65 ------------------------------------------------------------------
66 -- | Main sugared functions
67
68 mkGraphUfromEdges :: [(Int, Int)] -> Graph_Undirected
69 mkGraphUfromEdges es = mkGraph ns es
70 where
71 ns = List.nub (a <> b)
72 where
73 (a, b) = List.unzip es
74