]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Graph/Index.hs
[PIPELINE] adding clustering louvain.
[gargantext.git] / src / Gargantext / Viz / Graph / Index.hs
1 {-|
2 Module : Gargantext.Graph.Distances.Utils
3 Description : Tools to compute distances from Cooccurrences
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Basically @compute@ takes an accelerate function as first input, a Map
11 of coccurrences as second input and outputs a Map automatically using
12 indexes.
13
14 TODO:
15 --cooc2fgl :: Ord t, Integral n => Map (t, t) n -> Graph
16 --fgl2json
17
18 -}
19
20 {-# LANGUAGE BangPatterns #-}
21 {-# LANGUAGE FlexibleContexts #-}
22 {-# LANGUAGE NoImplicitPrelude #-}
23 {-# LANGUAGE TypeOperators #-}
24
25
26 module Gargantext.Viz.Graph.Index
27 where
28
29 import qualified Data.Array.Accelerate as A
30 import qualified Data.Array.Accelerate.Interpreter as A
31 import Data.Array.Accelerate (Matrix, Elt, Shape, (:.)(..), Z(..))
32
33 import qualified Data.Vector.Unboxed as DVU
34 import Data.Maybe (fromMaybe)
35
36 import Data.Set (Set)
37 import qualified Data.Set as S
38
39 import Data.Map (Map)
40 import qualified Data.Map.Strict as M
41
42 import Gargantext.Prelude
43
44 type Index = Int
45
46 -------------------------------------------------------------------------------
47 -------------------------------------------------------------------------------
48 score :: (Ord t) => (A.Matrix Int -> A.Matrix Double)
49 -> Map (t, t) Int
50 -> Map (t, t) Double
51 score f m = fromIndex fromI . mat2map . f $ cooc2mat toI m
52 where
53 (toI, fromI) = createIndexes m
54
55 -------------------------------------------------------------------------------
56 -------------------------------------------------------------------------------
57 cooc2mat :: Ord t => Map t Index -> Map (t, t) Int -> Matrix Int
58 cooc2mat ti m = map2mat 0 n idx
59 where
60 n = M.size ti
61 idx = toIndex ti m -- it is important to make sure that toIndex is ran only once.
62
63 map2mat :: Elt a => a -> Int -> Map (Index, Index) a -> Matrix a
64 map2mat def n m = A.fromFunction shape (\(Z :. x :. y) -> fromMaybe def $ M.lookup (x, y) m)
65 where
66 shape = (Z :. n :. n)
67
68 mat2map :: (Elt a, Shape (Z :. Index)) =>
69 A.Array (Z :. Index :. Index) a -> Map (Index, Index) a
70 mat2map m = M.fromList . map f . A.toList . A.run . A.indexed $ A.use m
71 where
72 Z :. _ :. n = A.arrayShape m
73 f ((Z :. i :. j), x) = ((i, j), x)
74
75 -------------------------------------------------------------------------------
76 -------------------------------------------------------------------------------
77 toIndex :: Ord t => Map t Index -> Map (t,t) a -> Map (Index,Index) a
78 toIndex ni ns = indexConversion ni ns
79
80 fromIndex :: Ord t => Map Index t -> Map (Index, Index) a -> Map (t,t) a
81 fromIndex ni ns = indexConversion ni ns
82 ---------------------------------------------------------------------------------
83 indexConversion :: (Ord b, Ord k) => Map k b -> Map (k,k) a -> Map (b, b) a
84 indexConversion index ms = M.fromList $ map (\((k1,k2),c) -> ( ((M.!) index k1, (M.!) index k2), c)) (M.toList ms)
85 -------------------------------------------------------------------------------
86 -------------------------------------------------------------------------------
87 createIndexes :: Ord t => Map (t, t) b -> (Map t Index, Map Index t)
88 createIndexes = set2indexes . cooc2set
89 where
90 cooc2set :: Ord t => Map (t, t) a -> Set t
91 cooc2set cs' = foldl' (\s ((t1,t2),_) -> insert [t1,t2] s ) S.empty (M.toList cs')
92 where
93 insert as s = foldl' (\s' t -> S.insert t s') s as
94
95 set2indexes :: Ord t => Set t -> (Map t Index, Map Index t)
96 set2indexes s = (M.fromList toIndex', M.fromList fromIndex')
97 where
98 fromIndex' = zip [0..] xs
99 toIndex' = zip xs [0..]
100 xs = S.toList s
101
102