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