]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Prelude.hs
[CLEAN] Facet.hs
[gargantext.git] / src / Gargantext / Prelude.hs
1 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
2 {-# OPTIONS_GHC -fno-warn-type-defaults #-}
3 {-# LANGUAGE NoImplicitPrelude #-}
4
5 {-
6 TODO: import head impossible from Protolude: why ?
7 -}
8
9 module Gargantext.Prelude
10 ( module Gargantext.Prelude
11 , module Protolude
12 , headMay
13 , module Text.Show
14 , module Text.Read
15 )
16 where
17
18 import Protolude ( Bool(True, False), Int, Double, Integer
19 , Fractional, Num, Maybe(Just,Nothing)
20 , Floating, Char, IO
21 , pure, (<$>), panic
22 , Ord, Integral, Foldable, RealFrac, Monad, filter
23 , reverse, map, zip, drop, take, zipWith
24 , sum, fromIntegral, length, fmap
25 , takeWhile, sqrt, undefined, identity
26 , abs, maximum, minimum, return, snd, truncate
27 , (+), (*), (/), (-), (.), (>=), ($), (**), (^), (<), (>), (==), (<>)
28 , toS
29 )
30
31 -- TODO import functions optimized in Utils.Count
32 -- import Protolude hiding (head, last, all, any, sum, product, length)
33 -- import Gargantext.Utils.Count
34
35 import qualified Data.List as L hiding (head, sum)
36 import qualified Control.Monad as M
37 import qualified Data.Map as Map
38 import Data.Map.Strict (insertWith)
39 import qualified Data.Vector as V
40 import Safe (headMay)
41 import Text.Show (Show(), show)
42 import Text.Read (Read())
43 --pf :: (a -> Bool) -> [a] -> [a]
44 --pf = filter
45
46 pr :: [a] -> [a]
47 pr = reverse
48
49 --pm :: (a -> b) -> [a] -> [b]
50 --pm = map
51
52 map2 :: (t -> b) -> [[t]] -> [[b]]
53 map2 fun = map (map fun)
54
55 pz :: [a] -> [b] -> [(a, b)]
56 pz = zip
57
58 pd :: Int -> [a] -> [a]
59 pd = drop
60
61 ptk :: Int -> [a] -> [a]
62 ptk = take
63
64 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
65 pzw = zipWith
66
67 -- Exponential Average
68 eavg :: [Double] -> Double
69 eavg (x:xs) = a*x + (1-a)*(eavg xs)
70 where a = 0.70
71 eavg [] = 0
72
73 -- Simple Average
74 mean :: Fractional a => [a] -> a
75 mean xs = if L.null xs then 0.0
76 else sum xs / fromIntegral (length xs)
77
78 sumMaybe :: Num a => [Maybe a] -> Maybe a
79 sumMaybe = fmap sum . M.sequence
80
81 variance :: Floating a => [a] -> a
82 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
83 m = mean xs
84
85 deviation :: [Double] -> Double
86 deviation = sqrt . variance
87
88 movingAverage :: Fractional b => Int -> [b] -> [b]
89 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
90
91 ma :: [Double] -> [Double]
92 ma = movingAverage 3
93
94
95 -- | Function to split a range into chunks
96 chunkAlong :: Int -> Int -> [a] -> [[a]]
97 chunkAlong a b l = only (while dropAlong)
98 where
99 only = map (take a)
100 while = takeWhile (\x -> length x >= a)
101 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
102
103 -- | Optimized version (Vector)
104 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
105 chunkAlong' a b l = only (while dropAlong)
106 where
107 only = V.map (V.take a)
108 while = V.takeWhile (\x -> V.length x >= a)
109 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
110
111 -- | TODO Inverse of chunk ? unchunkAlong ?
112 unchunkAlong :: Int -> Int -> [[a]] -> [a]
113 unchunkAlong = undefined
114
115
116 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
117 splitAlong :: [Int] -> [Char] -> [[Char]]
118 splitAlong _ [] = [] -- No list? done
119 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
120 splitAlong (x:xs) ys = take x ys : splitAlong xs (drop x ys) -- take until our split spot, recurse with next split spot and list remainder
121
122 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
123 takeWhileM _ [] = return []
124 takeWhileM p (a:as) = do
125 v <- a
126 if p v
127 then do
128 vs <- takeWhileM p as
129 return (v:vs)
130 else return []
131
132 -- SUMS
133 -- To select the right algorithme according to the type:
134 -- https://github.com/mikeizbicki/ifcxt
135
136 sumSimple :: Num a => [a] -> a
137 sumSimple = L.foldl' (+) 0
138
139 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
140 sumKahan :: Num a => [a] -> a
141 sumKahan = snd . L.foldl' go (0,0)
142 where
143 go (c,t) i = ((t'-t)-y,t')
144 where
145 y = i-c
146 t' = t+y
147
148 -- | compute part of the dict
149 count2map :: (Ord k, Foldable t) => t k -> Map.Map k Double
150 count2map xs = Map.map (/ (fromIntegral (length xs))) (count2map' xs)
151
152 -- | insert in a dict
153 count2map' :: (Ord k, Foldable t) => t k -> Map.Map k Double
154 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) Map.empty xs
155
156
157 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
158 trunc n = truncate . (* 10^n)
159
160 trunc' :: Int -> Double -> Double
161 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
162
163
164 bool2int :: Num a => Bool -> a
165 bool2int b = case b of
166 True -> 1
167 False -> 0
168
169 bool2double :: Bool -> Double
170 bool2double bool = case bool of
171 True -> 1.0
172 False -> 0.0
173
174
175
176 -- Normalizing && scaling data
177 scale :: [Double] -> [Double]
178 scale = scaleMinMax
179
180 scaleMinMax :: [Double] -> [Double]
181 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
182 where
183 ma = maximum xs'
184 mi = minimum xs'
185 xs' = map abs xs
186
187 scaleNormalize :: [Double] -> [Double]
188 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
189 where
190 v = variance xs'
191 m = mean xs'
192 xs' = map abs xs
193
194
195
196 normalize :: [Double] -> [Double]
197 normalize as = normalizeWith identity as
198
199 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
200 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
201 where
202 bs' = map extract bs
203
204 -- Zip functions to add
205 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
206 zipFst f xs = zip (f xs) xs
207
208 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
209 zipSnd f xs = zip xs (f xs)