2 Module : Gargantext.Core.Methods.Similarities
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Motivation and definition of the @Conditional@ distance.
13 {-# LANGUAGE BangPatterns #-}
14 {-# LANGUAGE Strict #-}
15 module Gargantext.Core.Methods.Similarities.Conditional
18 import Control.DeepSeq (NFData)
19 import Control.Parallel.Strategies (parList, rdeepseq, using)
20 import Data.Hashable (Hashable)
21 import Data.List (unzip)
22 import Data.Maybe (catMaybes)
23 import Gargantext.Prelude
24 import Gargantext.Core.Viz.Graph.Utils (getMax)
25 import qualified Data.HashMap.Strict as Map
26 import qualified Data.Set as Set
29 type HashMap = Map.HashMap
30 ------------------------------------------------------------------------
31 -- First version as first implementation
32 -- - qualitatively verified
33 -- - parallized as main optimization
34 conditional :: (Ord a, Hashable a, NFData a)
36 -> HashMap (a,a) Double
37 conditional m' = Map.fromList $ ((catMaybes results') `using` parList rdeepseq)
40 ij = (/) <$> Map.lookup (i,j) m <*> Map.lookup (i,i) m
41 ji = (/) <$> Map.lookup (j,i) m <*> Map.lookup (j,j) m
48 -- Converting from Int to Double
49 m = Map.map fromIntegral m'
51 -- Get the matrix coordinates, removing duplicates
52 keys = Set.toList $ Set.fromList (x <> y)
53 (x,y) = unzip $ Map.keys m