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