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