]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Prelude.hs
[TYPES] Adding HyperdataDocumentV3 type for compatibility with previous version of...
[gargantext.git] / src / Gargantext / Prelude.hs
1 {-|
2 Module : Gargantext.Prelude
3 Description : Specific Prelude of the project
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
76 map2 :: (t -> b) -> [[t]] -> [[b]]
77 map2 fun = map (map fun)
78
79
80 -- Some Statistics sugar functions
81 -- Exponential Average
82 eavg :: [Double] -> Double
83 eavg (x:xs) = a*x + (1-a)*(eavg xs)
84 where a = 0.70
85 eavg [] = 0
86
87 -- Simple Average
88 mean :: Fractional a => [a] -> a
89 mean xs = if L.null xs then 0.0
90 else sum xs / fromIntegral (length xs)
91
92
93 sumMaybe :: Num a => [Maybe a] -> Maybe a
94 sumMaybe = fmap sum . M.sequence
95
96 variance :: Floating a => [a] -> a
97 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
98 m = mean xs
99
100 deviation :: [Double] -> Double
101 deviation = sqrt . variance
102
103 movingAverage :: Fractional b => Int -> [b] -> [b]
104 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
105
106 ma :: [Double] -> [Double]
107 ma = movingAverage 3
108
109 -- | splitEvery n == chunkAlong n n
110 splitEvery :: Int -> [a] -> [[a]]
111 splitEvery _ [] = []
112 splitEvery n xs =
113 let (h,t) = L.splitAt n xs
114 in h : splitEvery n t
115
116 -- | Function to split a range into chunks
117 chunkAlong :: Int -> Int -> [a] -> [[a]]
118 chunkAlong a b l = only (while dropAlong)
119 where
120 only = map (take a)
121 while = takeWhile (\x -> length x >= a)
122 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
123
124 -- | Optimized version (Vector)
125 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
126 chunkAlong' a b l = only (while dropAlong)
127 where
128 only = V.map (V.take a)
129 while = V.takeWhile (\x -> V.length x >= a)
130 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
131
132 -- | TODO Inverse of chunk ? unchunkAlong ?
133 unchunkAlong :: Int -> Int -> [[a]] -> [a]
134 unchunkAlong = undefined
135
136
137 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
138 splitAlong :: [Int] -> [Char] -> [[Char]]
139 splitAlong _ [] = [] -- No list? done
140 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
141 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
142
143 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
144 takeWhileM _ [] = return []
145 takeWhileM p (a:as) = do
146 v <- a
147 if p v
148 then do
149 vs <- takeWhileM p as
150 return (v:vs)
151 else return []
152
153 -- SUMS
154 -- To select the right algorithme according to the type:
155 -- https://github.com/mikeizbicki/ifcxt
156
157 sumSimple :: Num a => [a] -> a
158 sumSimple = L.foldl' (+) 0
159
160 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
161 sumKahan :: Num a => [a] -> a
162 sumKahan = snd . L.foldl' go (0,0)
163 where
164 go (c,t) i = ((t'-t)-y,t')
165 where
166 y = i-c
167 t' = t+y
168
169 -- | compute part of the dict
170 count2map :: (Ord k, Foldable t) => t k -> Map k Double
171 count2map xs = M.map (/ (fromIntegral (length xs))) (count2map' xs)
172
173 -- | insert in a dict
174 count2map' :: (Ord k, Foldable t) => t k -> Map k Double
175 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) M.empty xs
176
177
178 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
179 trunc n = truncate . (* 10^n)
180
181 trunc' :: Int -> Double -> Double
182 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
183
184
185 ------------------------------------------------------------------------
186 bool2num :: Num a => Bool -> a
187 bool2num True = 1
188 bool2num False = 0
189
190 bool2double :: Bool -> Double
191 bool2double = bool2num
192
193 bool2int :: Bool -> Int
194 bool2int = bool2num
195 ------------------------------------------------------------------------
196
197 -- Normalizing && scaling data
198 scale :: [Double] -> [Double]
199 scale = scaleMinMax
200
201 scaleMinMax :: [Double] -> [Double]
202 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
203 where
204 ma = maximum xs'
205 mi = minimum xs'
206 xs' = map abs xs
207
208 scaleNormalize :: [Double] -> [Double]
209 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
210 where
211 v = variance xs'
212 m = mean xs'
213 xs' = map abs xs
214
215 normalize :: [Double] -> [Double]
216 normalize as = normalizeWith identity as
217
218 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
219 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
220 where
221 bs' = map extract bs
222
223 -- Zip functions to add
224 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
225 zipFst f xs = zip (f xs) xs
226
227 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
228 zipSnd f xs = zip xs (f xs)
229
230 -- | maximumWith
231 maximumWith :: (Ord a1, Foldable t) => (a2 -> a1) -> t a2 -> a2
232 maximumWith f = L.maximumBy (compare `on` f)
233