]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Graph/Distances/Distributional.hs
[FEAT] specificity / genericity and inclusion / exclusion metric.
[gargantext.git] / src / Gargantext / Viz / Graph / Distances / Distributional.hs
1 {-|
2 Module : Gargantext.Graph.Distances.Distributional
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Motivation and definition of the @Distributional@ distance.
11 -}
12
13 {-# LANGUAGE BangPatterns #-}
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# LANGUAGE FlexibleContexts #-}
16 {-# LANGUAGE Strict #-}
17
18
19 module Gargantext.Viz.Graph.Distances.Distributional
20 where
21
22 import Data.Matrix hiding (identity)
23 import Data.String.Conversions (ConvertibleStrings(..))
24
25 import Data.Map (Map)
26 import qualified Data.Map as M
27
28 import Data.Set (Set)
29 import qualified Data.Set as S
30
31 import Data.Vector (Vector)
32 import qualified Data.Vector as V
33
34 import Gargantext.Prelude
35 import Gargantext.Viz.Graph.Utils
36
37
38 distributional :: (Floating a, Ord a) => Matrix a -> [((Int, Int), a)]
39 distributional m = filter (\((x,y), d) -> foldl' (&&) True (conditions x y d) ) distriList
40 where
41 conditions x y d = [ (x /= y)
42 , (d > miniMax')
43 , ((M.lookup (x,y) distriMap) > (M.lookup (y,x) distriMap))
44 ]
45 distriList = toListsWithIndex distriMatrix
46 distriMatrix = ri (mi m)
47
48 distriMap = M.fromList $ distriList
49 miniMax' = miniMax distriMatrix
50
51 ri :: (Ord a, Fractional a) => Matrix a -> Matrix a
52 ri m = matrix c r doRi
53 where
54 doRi (x,y) = doRi' x y m
55 doRi' x y mi'' = sumMin x y mi'' / (V.sum $ ax Col x y mi'')
56
57 sumMin x y mi' = V.sum $ V.map (\(a,b) -> min a b )
58 $ V.zip (ax Col x y mi') (ax Row x y mi')
59 (c,r) = (nOf Col m, nOf Row m)
60
61
62 mi :: (Ord a, Floating a) => Matrix a -> Matrix a
63 mi m = matrix c r createMat
64 where
65 (c,r) = (nOf Col m, nOf Row m)
66 createMat (x,y) = doMi x y m
67 doMi x y m = if x == y then 0 else (max (log (doMi' x y m)) 0 )
68
69 doMi' x y m = (getElem x y m) / ( cross x y m / total m )
70
71 cross x y m = (V.sum $ ax Col x y m) * (V.sum $ ax Row x y m)
72
73
74
75 ax :: Axis -> Int -> Int -> Matrix a -> Vector a
76 ax a i j m = dropAt j' $ axis a i' m
77 where
78 i' = div i c + 1
79 j' = mod r j + 1
80 (c,r) = (nOf Col m, nOf Row m)
81
82 miniMax :: (Ord a) => Matrix a -> a
83 miniMax m = V.minimum $ V.map (\c -> V.maximum $ getCol c m) (V.enumFromTo 1 (nOf Col m))
84
85