2 Module : Gargantext.Prelude
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# OPTIONS_GHC -fno-warn-type-defaults #-}
17 {-# LANGUAGE NoImplicitPrelude #-}
19 module Gargantext.Prelude
20 ( module Gargantext.Prelude
30 import Data.Maybe (isJust, fromJust, maybe)
31 import Protolude ( Bool(True, False), Int, Double, Integer
32 , Fractional, Num, Maybe(Just,Nothing)
34 , pure, (<$>), (<&>), panic
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, (==), (>=), (<=), (<>), (/=)
46 , curry, uncurry, repeat
49 -- TODO import functions optimized in Utils.Count
50 -- import Protolude hiding (head, last, all, any, sum, product, length)
51 -- import Gargantext.Utils.Count
52 import qualified Data.List as L hiding (head, sum)
53 import qualified Control.Monad as M
56 import qualified Data.Map as M
58 import Data.Map.Strict (insertWith)
59 import qualified Data.Vector as V
61 import Text.Show (Show(), show)
62 import Text.Read (Read())
63 import Data.String.Conversions (cs)
65 --pf :: (a -> Bool) -> [a] -> [a]
71 --pm :: (a -> b) -> [a] -> [b]
74 map2 :: (t -> b) -> [[t]] -> [[b]]
75 map2 fun = map (map fun)
77 pz :: [a] -> [b] -> [(a, b)]
80 pd :: Int -> [a] -> [a]
83 ptk :: Int -> [a] -> [a]
86 pzw :: (a -> b -> c) -> [a] -> [b] -> [c]
89 -- Exponential Average
90 eavg :: [Double] -> Double
91 eavg (x:xs) = a*x + (1-a)*(eavg xs)
96 mean :: Fractional a => [a] -> a
97 mean xs = if L.null xs then 0.0
98 else sum xs / fromIntegral (length xs)
100 sumMaybe :: Num a => [Maybe a] -> Maybe a
101 sumMaybe = fmap sum . M.sequence
103 variance :: Floating a => [a] -> a
104 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
107 deviation :: [Double] -> Double
108 deviation = sqrt . variance
110 movingAverage :: Fractional b => Int -> [b] -> [b]
111 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
113 ma :: [Double] -> [Double]
117 -- | Function to split a range into chunks
118 chunkAlong :: Int -> Int -> [a] -> [[a]]
119 chunkAlong a b l = only (while dropAlong)
122 while = takeWhile (\x -> length x >= a)
123 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
125 -- | Optimized version (Vector)
126 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
127 chunkAlong' a b l = only (while dropAlong)
129 only = V.map (V.take a)
130 while = V.takeWhile (\x -> V.length x >= a)
131 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
133 -- | TODO Inverse of chunk ? unchunkAlong ?
134 unchunkAlong :: Int -> Int -> [[a]] -> [a]
135 unchunkAlong = undefined
138 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
139 splitAlong :: [Int] -> [Char] -> [[Char]]
140 splitAlong _ [] = [] -- No list? done
141 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
142 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 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
145 takeWhileM _ [] = return []
146 takeWhileM p (a:as) = do
150 vs <- takeWhileM p as
155 -- To select the right algorithme according to the type:
156 -- https://github.com/mikeizbicki/ifcxt
158 sumSimple :: Num a => [a] -> a
159 sumSimple = L.foldl' (+) 0
161 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
162 sumKahan :: Num a => [a] -> a
163 sumKahan = snd . L.foldl' go (0,0)
165 go (c,t) i = ((t'-t)-y,t')
170 -- | compute part of the dict
171 count2map :: (Ord k, Foldable t) => t k -> Map k Double
172 count2map xs = M.map (/ (fromIntegral (length xs))) (count2map' xs)
174 -- | insert in a dict
175 count2map' :: (Ord k, Foldable t) => t k -> Map k Double
176 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) M.empty xs
179 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
180 trunc n = truncate . (* 10^n)
182 trunc' :: Int -> Double -> Double
183 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
186 bool2int :: Num a => Bool -> a
187 bool2int b = case b of
191 bool2double :: Bool -> Double
192 bool2double bool = case bool of
198 -- Normalizing && scaling data
199 scale :: [Double] -> [Double]
202 scaleMinMax :: [Double] -> [Double]
203 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
209 scaleNormalize :: [Double] -> [Double]
210 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
218 normalize :: [Double] -> [Double]
219 normalize as = normalizeWith identity as
221 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
222 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
226 -- Zip functions to add
227 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
228 zipFst f xs = zip (f xs) xs
230 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
231 zipSnd f xs = zip xs (f xs)
235 unMaybe :: [Maybe a] -> [a]
236 unMaybe = map fromJust . L.filter isJust