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