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
10 Basically @compute@ takes an accelerate function as first input, a Map
11 of coccurrences as second input and outputs a Map automatically using
15 --cooc2fgl :: Ord t, Integral n => Map (t, t) n -> Graph
20 {-# LANGUAGE BangPatterns #-}
21 {-# LANGUAGE TypeOperators #-}
22 {-# LANGUAGE MonoLocalBinds #-}
24 module Gargantext.Core.Viz.Graph.Index
27 import qualified Data.Array.Accelerate as A
28 import qualified Data.Array.Accelerate.Interpreter as A
29 import Data.Array.Accelerate (Matrix, Elt, Shape, (:.)(..), Z(..))
31 import Data.Maybe (fromMaybe)
34 import qualified Data.Set as S
37 import qualified Data.Map.Strict as M
39 -- import Data.Vector (Vector)
41 import Gargantext.Prelude
45 -------------------------------------------------------------------------------
46 -------------------------------------------------------------------------------
47 score :: (Ord t) => MatrixShape
48 -> (A.Matrix Int -> A.Matrix Double)
51 score s f m = fromIndex fromI . mat2map . f $ cooc2mat s toI m
53 (toI, fromI) = createIndices m
55 -------------------------------------------------------------------------------
56 -------------------------------------------------------------------------------
57 cooc2mat :: Ord t => MatrixShape -> Map t Index -> Map (t, t) Int -> Matrix Int
58 cooc2mat sym ti m = map2mat sym 0 n idx
61 idx = toIndex ti m -- it is important to make sure that toIndex is ran only once.
63 data MatrixShape = Triangular | Square
65 map2mat :: Elt a => MatrixShape -> a -> Int -> Map (Index, Index) a -> Matrix a
66 map2mat sym def n m = A.fromFunction shape getData
68 getData = (\(Z :. x :. y) ->
70 Triangular -> fromMaybe def (M.lookup (x,y) m)
71 Square -> fromMaybe (fromMaybe def $ M.lookup (y,x) m)
76 mat2map :: (Elt a, Shape (Z :. Index)) =>
77 A.Array (Z :. Index :. Index) a -> Map (Index, Index) a
78 mat2map m = M.fromList . map f . A.toList . A.run . A.indexed $ A.use m
80 -- Z :. _ :. n = A.arrayShape m
81 f ((Z :. i :. j), x) = ((i, j), x)
83 -------------------------------------------------------------------------------
84 -------------------------------------------------------------------------------
88 -> Map (Index,Index) a
89 toIndex = indexConversion
91 fromIndex :: Ord t => Map Index t -> Map (Index, Index) a -> Map (t,t) a
92 fromIndex ni ns = indexConversion ni ns
94 indexConversion :: (Ord b, Ord k) => Map k b -> Map (k,k) a -> Map (b, b) a
95 indexConversion index ms = M.fromList
96 $ map (\((k1,k2),c) -> ( ((M.!) index k1, (M.!) index k2), c))
98 ---------------------------------------------------------------------------------
100 -------------------------------------------------------------------------------
101 --fromIndex' :: Ord t => Vector t -> Map (Index, Index) a -> Map (t,t) a
102 --fromIndex' vi ns = undefined
104 -- TODO: returing a Vector should be faster than a Map
105 -- createIndices' :: Ord t => Map (t, t) b -> (Map t Index, Vector t)
106 -- createIndices' = undefined
108 createIndices :: Ord t => Map (t, t) b -> (Map t Index, Map Index t)
109 createIndices = set2indices . map2set
111 map2set :: Ord t => Map (t, t) a -> Set t
112 map2set cs' = foldl' (\s ((t1,t2),_) -> insert [t1,t2] s ) S.empty (M.toList cs')
114 insert as s = foldl' (\s' t -> S.insert t s') s as
116 set2indices :: Ord t => Set t -> (Map t Index, Map Index t)
117 set2indices s = (M.fromList toIndex', M.fromList fromIndex')
119 fromIndex' = zip [0..] xs
120 toIndex' = zip xs [0..]