]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Graph/FGL.hs
[GRAPH] API update (WIP).
[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
29 type Edge = FGL.Edge
30
31 ------------------------------------------------------------------
32 -- | Main Functions
33
34 mkGraph :: [Node] -> [Edge] -> Graph_Undirected
35 mkGraph = FGL.mkUGraph
36
37 neighbors :: Graph gr => gr a b -> Node -> [Node]
38 neighbors = FGL.neighbors
39
40 -- | TODO bug: if graph is undirected, we need to filter
41 -- nub . (map (\(n1,n2) -> if n1 < n2 then (n1,n2) else (n2,n1))) . FGL.edges
42 edges :: Graph gr => gr a b -> [Edge]
43 edges = FGL.edges
44
45 nodes :: Graph gr => gr a b -> [Node]
46 nodes = FGL.nodes
47
48 ------------------------------------------------------------------------
49 -- | Graph Tools
50
51 filterNeighbors :: Graph_Undirected -> Node -> [Node]
52 filterNeighbors g n = List.nub $ neighbors g n
53
54 -- Q: why not D.G.I.deg ? (Int as result)
55 degree :: Graph_Undirected -> Node -> Double
56 degree g n = fromIntegral $ List.length (filterNeighbors g n)
57
58 vcount :: Graph_Undirected -> Double
59 vcount = fromIntegral . List.length . List.nub . nodes
60
61 -- | TODO tests, optim and use IGraph library, fix IO ?
62 ecount :: Graph_Undirected -> Double
63 ecount = fromIntegral . List.length . List.nub . edges
64
65
66 ------------------------------------------------------------------
67 -- | Main sugared functions
68
69 mkGraphUfromEdges :: [(Int, Int)] -> Graph_Undirected
70 mkGraphUfromEdges es = mkGraph ns es
71 where
72 ns = List.nub (a <> b)
73 where
74 (a, b) = List.unzip es
75