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