]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Prelude.hs
[LOGS] adding log to dev app.
[gargantext.git] / src / Gargantext / Prelude.hs
1 {-|
2 Module : Gargantext.Prelude
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 Here is a longer description of this module, containing some
11 commentary with @some markup@.
12 -}
13
14 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# OPTIONS_GHC -fno-warn-type-defaults #-}
16
17 {-# LANGUAGE NoImplicitPrelude #-}
18
19 module Gargantext.Prelude
20 ( module Gargantext.Prelude
21 , module Protolude
22 , headMay, lastMay
23 , module Text.Show
24 , module Text.Read
25 , cs
26 , module Data.Maybe
27 , sortWith
28 )
29 where
30
31 import GHC.Exts (sortWith)
32
33 import Data.Maybe (isJust, fromJust, maybe)
34 import Protolude ( Bool(True, False), Int, Int64, Double, Integer
35 , Fractional, Num, Maybe(Just,Nothing)
36 , Enum, Bounded, Float
37 , Floating, Char, IO
38 , pure, (>>=), (=<<), (<*>), (<$>), panic
39 , putStrLn
40 , head, flip
41 , Ord, Integral, Foldable, RealFrac, Monad, filter
42 , reverse, map, mapM, zip, drop, take, zipWith
43 , sum, fromIntegral, length, fmap, foldl, foldl'
44 , takeWhile, sqrt, undefined, identity
45 , abs, min, max, maximum, minimum, return, snd, truncate
46 , (+), (*), (/), (-), (.), ($), (&), (**), (^), (<), (>), log
47 , Eq, (==), (>=), (<=), (<>), (/=)
48 , (&&), (||), not, any
49 , fst, snd, toS
50 , elem, die, mod, div, const, either
51 , curry, uncurry, repeat
52 , otherwise, when
53 , undefined
54 , IO()
55 , compare
56 , on
57 )
58
59 -- TODO import functions optimized in Utils.Count
60 -- import Protolude hiding (head, last, all, any, sum, product, length)
61 -- import Gargantext.Utils.Count
62 import qualified Data.List as L hiding (head, sum)
63 import qualified Control.Monad as M
64
65 import Data.Map (Map)
66 import qualified Data.Map as M
67
68 import Data.Map.Strict (insertWith)
69 import qualified Data.Vector as V
70 import Safe (headMay, lastMay)
71 import Text.Show (Show(), show)
72 import Text.Read (Read())
73 import Data.String.Conversions (cs)
74
75 --pf :: (a -> Bool) -> [a] -> [a]
76 --pf = filter
77
78 pr :: [a] -> [a]
79 pr = reverse
80
81 --pm :: (a -> b) -> [a] -> [b]
82 --pm = map
83
84 map2 :: (t -> b) -> [[t]] -> [[b]]
85 map2 fun = map (map fun)
86
87 -- Exponential Average
88 eavg :: [Double] -> Double
89 eavg (x:xs) = a*x + (1-a)*(eavg xs)
90 where a = 0.70
91 eavg [] = 0
92
93 -- Simple Average
94 mean :: Fractional a => [a] -> a
95 mean xs = if L.null xs then 0.0
96 else sum xs / fromIntegral (length xs)
97
98 sumMaybe :: Num a => [Maybe a] -> Maybe a
99 sumMaybe = fmap sum . M.sequence
100
101 variance :: Floating a => [a] -> a
102 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
103 m = mean xs
104
105 deviation :: [Double] -> Double
106 deviation = sqrt . variance
107
108 movingAverage :: Fractional b => Int -> [b] -> [b]
109 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
110
111 ma :: [Double] -> [Double]
112 ma = movingAverage 3
113
114 -- | splitEvery n == chunkAlong n n
115 splitEvery :: Int -> [a] -> [[a]]
116 splitEvery _ [] = []
117 splitEvery n xs =
118 let (h,t) = L.splitAt n xs
119 in h : splitEvery n t
120
121 -- | Function to split a range into chunks
122 chunkAlong :: Int -> Int -> [a] -> [[a]]
123 chunkAlong a b l = only (while dropAlong)
124 where
125 only = map (take a)
126 while = takeWhile (\x -> length x >= a)
127 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
128
129 -- | Optimized version (Vector)
130 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
131 chunkAlong' a b l = only (while dropAlong)
132 where
133 only = V.map (V.take a)
134 while = V.takeWhile (\x -> V.length x >= a)
135 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
136
137 -- | TODO Inverse of chunk ? unchunkAlong ?
138 unchunkAlong :: Int -> Int -> [[a]] -> [a]
139 unchunkAlong = undefined
140
141
142 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
143 splitAlong :: [Int] -> [Char] -> [[Char]]
144 splitAlong _ [] = [] -- No list? done
145 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
146 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
147
148 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
149 takeWhileM _ [] = return []
150 takeWhileM p (a:as) = do
151 v <- a
152 if p v
153 then do
154 vs <- takeWhileM p as
155 return (v:vs)
156 else return []
157
158 -- SUMS
159 -- To select the right algorithme according to the type:
160 -- https://github.com/mikeizbicki/ifcxt
161
162 sumSimple :: Num a => [a] -> a
163 sumSimple = L.foldl' (+) 0
164
165 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
166 sumKahan :: Num a => [a] -> a
167 sumKahan = snd . L.foldl' go (0,0)
168 where
169 go (c,t) i = ((t'-t)-y,t')
170 where
171 y = i-c
172 t' = t+y
173
174 -- | compute part of the dict
175 count2map :: (Ord k, Foldable t) => t k -> Map k Double
176 count2map xs = M.map (/ (fromIntegral (length xs))) (count2map' xs)
177
178 -- | insert in a dict
179 count2map' :: (Ord k, Foldable t) => t k -> Map k Double
180 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) M.empty xs
181
182
183 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
184 trunc n = truncate . (* 10^n)
185
186 trunc' :: Int -> Double -> Double
187 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
188
189
190 bool2int :: Num a => Bool -> a
191 bool2int b = case b of
192 True -> 1
193 False -> 0
194
195 bool2double :: Bool -> Double
196 bool2double bool = case bool of
197 True -> 1.0
198 False -> 0.0
199
200
201
202 -- Normalizing && scaling data
203 scale :: [Double] -> [Double]
204 scale = scaleMinMax
205
206 scaleMinMax :: [Double] -> [Double]
207 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
208 where
209 ma = maximum xs'
210 mi = minimum xs'
211 xs' = map abs xs
212
213 scaleNormalize :: [Double] -> [Double]
214 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
215 where
216 v = variance xs'
217 m = mean xs'
218 xs' = map abs xs
219
220
221
222 normalize :: [Double] -> [Double]
223 normalize as = normalizeWith identity as
224
225 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
226 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
227 where
228 bs' = map extract bs
229
230 -- Zip functions to add
231 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
232 zipFst f xs = zip (f xs) xs
233
234 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
235 zipSnd f xs = zip xs (f xs)
236
237 -- Just
238 unMaybe :: [Maybe a] -> [a]
239 unMaybe = map fromJust . L.filter isJust
240
241 -- maximumWith
242 maximumWith f = L.maximumBy (compare `on` f)
243