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
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
31 import GHC.Exts (sortWith)
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
38 , pure, (>>=), (=<<), (<*>), (<$>), panic
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
50 , elem, die, mod, div, const, either
51 , curry, uncurry, repeat
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
66 import qualified Data.Map as M
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)
76 map2 :: (t -> b) -> [[t]] -> [[b]]
77 map2 fun = map (map fun)
80 -- Some Statistics sugar functions
81 -- Exponential Average
82 eavg :: [Double] -> Double
83 eavg (x:xs) = a*x + (1-a)*(eavg xs)
88 mean :: Fractional a => [a] -> a
89 mean xs = if L.null xs then 0.0
90 else sum xs / fromIntegral (length xs)
93 sumMaybe :: Num a => [Maybe a] -> Maybe a
94 sumMaybe = fmap sum . M.sequence
96 variance :: Floating a => [a] -> a
97 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
100 deviation :: [Double] -> Double
101 deviation = sqrt . variance
103 movingAverage :: Fractional b => Int -> [b] -> [b]
104 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
106 ma :: [Double] -> [Double]
109 -- | splitEvery n == chunkAlong n n
110 splitEvery :: Int -> [a] -> [[a]]
113 let (h,t) = L.splitAt n xs
114 in h : splitEvery n t
116 -- | Function to split a range into chunks
117 chunkAlong :: Int -> Int -> [a] -> [[a]]
118 chunkAlong a b l = only (while dropAlong)
121 while = takeWhile (\x -> length x >= a)
122 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
124 -- | Optimized version (Vector)
125 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
126 chunkAlong' a b l = only (while dropAlong)
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..])
132 -- | TODO Inverse of chunk ? unchunkAlong ?
133 unchunkAlong :: Int -> Int -> [[a]] -> [a]
134 unchunkAlong = undefined
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
143 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
144 takeWhileM _ [] = return []
145 takeWhileM p (a:as) = do
149 vs <- takeWhileM p as
154 -- To select the right algorithme according to the type:
155 -- https://github.com/mikeizbicki/ifcxt
157 sumSimple :: Num a => [a] -> a
158 sumSimple = L.foldl' (+) 0
160 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
161 sumKahan :: Num a => [a] -> a
162 sumKahan = snd . L.foldl' go (0,0)
164 go (c,t) i = ((t'-t)-y,t')
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)
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
178 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
179 trunc n = truncate . (* 10^n)
181 trunc' :: Int -> Double -> Double
182 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
185 ------------------------------------------------------------------------
186 bool2num :: Num a => Bool -> a
190 bool2double :: Bool -> Double
191 bool2double = bool2num
193 bool2int :: Bool -> Int
195 ------------------------------------------------------------------------
197 -- Normalizing && scaling data
198 scale :: [Double] -> [Double]
201 scaleMinMax :: [Double] -> [Double]
202 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
208 scaleNormalize :: [Double] -> [Double]
209 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
215 normalize :: [Double] -> [Double]
216 normalize as = normalizeWith identity as
218 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
219 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
223 -- Zip functions to add
224 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
225 zipFst f xs = zip (f xs) xs
227 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
228 zipSnd f xs = zip xs (f xs)
231 maximumWith :: (Ord a1, Foldable t) => (a2 -> a1) -> t a2 -> a2
232 maximumWith f = L.maximumBy (compare `on` f)