]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Phylo/Metrics/Proximity.hs
[FIX] heads.
[gargantext.git] / src / Gargantext / Viz / Phylo / Metrics / Proximity.hs
1 {-|
2 Module : Gargantext.Viz.Phylo.Tools
3 Description : Phylomemy Tools to build/manage it
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10
11 -}
12
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE FlexibleContexts #-}
15 {-# LANGUAGE OverloadedStrings #-}
16
17 module Gargantext.Viz.Phylo.Metrics.Proximity
18 where
19
20 import Data.List (null)
21 import Data.Map (Map,elems,unionWith,intersectionWith,intersection,size)
22 import Gargantext.Prelude
23
24 -- | To process the weightedLogJaccard between two PhyloGroup fields
25 weightedLogJaccard :: Double -> Map (Int, Int) Double -> Map (Int, Int) Double -> Double
26 weightedLogJaccard s f1 f2
27 | null wUnion = 0
28 | wUnion == wInter = 1
29 | s == 0 = (fromIntegral $ length wInter)/(fromIntegral $ length wUnion)
30 | s > 0 = (sumInvLog wInter)/(sumInvLog wUnion)
31 | otherwise = (sumLog wInter)/(sumLog wUnion)
32 where
33 --------------------------------------
34 wInter :: [Double]
35 wInter = elems $ intersectionWith (+) f1 f2
36 --------------------------------------
37 wUnion :: [Double]
38 wUnion = elems $ unionWith (+) f1 f2
39 --------------------------------------
40 sumInvLog :: [Double] -> Double
41 sumInvLog l = foldl (\mem x -> mem + (1 / log (s + x))) 0 l
42 --------------------------------------
43 sumLog :: [Double] -> Double
44 sumLog l = foldl (\mem x -> mem + log (s + x)) 0 l
45 --------------------------------------
46
47
48 -- | To process the Hamming distance between two PhyloGroup fields
49 hamming :: Map (Int, Int) Double -> Map (Int, Int) Double -> Double
50 hamming f1 f2 = fromIntegral $ max ((size inter) - (size f1)) ((size inter) - (size f2))
51 where
52 --------------------------------------
53 inter :: Map (Int, Int) Double
54 inter = intersection f1 f2
55 --------------------------------------