]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Methods/Distances/Conditional.hs
[OPTIM] Order1 similarity optimized (parallel computation)
[gargantext.git] / src / Gargantext / Core / Methods / Distances / Conditional.hs
1 {-|
2 Module : Gargantext.Core.Methods.Distances
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 @Conditional@ distance.
11 -}
12
13 {-# LANGUAGE BangPatterns #-}
14 {-# LANGUAGE Strict #-}
15 module Gargantext.Core.Methods.Distances.Conditional
16 where
17
18 import Control.Parallel.Strategies (parList, rdeepseq, using)
19 import Data.Hashable (Hashable)
20 import Data.List (unzip)
21 import Data.Maybe (catMaybes)
22 import Gargantext.Prelude
23 import qualified Data.HashMap.Strict as Map
24 import qualified Data.Set as Set
25 import Control.DeepSeq (NFData)
26 type HashMap = Map.HashMap
27
28 ------------------------------------------------------------------------
29 -- First version as first implementation
30 -- - qualitatively verified
31 -- - parallized as main optimization
32 conditional :: (Ord a, Hashable a, NFData a)
33 => HashMap (a,a) Int
34 -> HashMap (a,a) Double
35 conditional m' = Map.fromList $ ((catMaybes results') `using` parList rdeepseq)
36 where
37 results' = [ let
38 ij = (/) <$> Map.lookup (i,j) m <*> Map.lookup (i,i) m
39 ji = (/) <$> Map.lookup (j,i) m <*> Map.lookup (j,j) m
40 in getMax (i,j) ij ji
41
42 | i <- keys
43 , j <- keys
44 , i < j
45 ]
46 -- Converting from Int to Double
47 m = Map.map fromIntegral m'
48
49 -- Get the matrix coordinates, removing duplicates
50 keys = Set.toList $ Set.fromList (x <> y)
51 (x,y) = unzip $ Map.keys m
52
53
54 getMax :: (a,a)
55 -> Maybe Double
56 -> Maybe Double
57 -> Maybe ((a,a), Double)
58 getMax (i,j) (Just d) Nothing = Just ((i,j), d)
59 getMax (i,j) Nothing (Just d) = Just ((j,i), d)
60 getMax ij (Just di) (Just dj) = if di >= dj then getMax ij (Just di) Nothing
61 else getMax ij Nothing (Just dj)
62 getMax _ _ _ = Nothing
63
64