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