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
23 , module GHC.Err.Located
32 import GHC.Exts (sortWith)
33 import GHC.Err.Located (undefined)
34 import Control.Monad.IO.Class (MonadIO)
35 import Data.Maybe (isJust, fromJust, maybe)
36 import Protolude ( Bool(True, False), Int, Int64, Double, Integer
37 , Fractional, Num, Maybe(Just,Nothing)
38 , Enum, Bounded, Float
40 , pure, (>>=), (=<<), (<*>), (<$>)
43 , Ord, Integral, Foldable, RealFrac, Monad, filter
44 , reverse, map, mapM, zip, drop, take, zipWith
45 , sum, fromIntegral, length, fmap, foldl, foldl'
46 , takeWhile, sqrt, identity
47 , abs, min, max, maximum, minimum, return, snd, truncate
48 , (+), (*), (/), (-), (.), ($), (&), (**), (^), (<), (>), log
49 , Eq, (==), (>=), (<=), (<>), (/=)
50 , (&&), (||), not, any, all
52 , elem, die, mod, div, const, either
53 , curry, uncurry, repeat
61 -- TODO import functions optimized in Utils.Count
62 -- import Protolude hiding (head, last, all, any, sum, product, length)
63 -- import Gargantext.Utils.Count
64 import qualified Data.List as L hiding (head, sum)
65 import qualified Control.Monad as M
68 import qualified Data.Map as M
70 import Data.Map.Strict (insertWith)
71 import qualified Data.Vector as V
72 import Safe (headMay, lastMay)
73 import Text.Show (Show(), show)
74 import Text.Read (Read())
75 import Data.String.Conversions (cs)
78 printDebug :: (Show a, MonadIO m) => [Char] -> a -> m ()
79 printDebug msg x = putStrLn $ msg <> " " <> show x
80 -- printDebug _ _ = pure ()
83 map2 :: (t -> b) -> [[t]] -> [[b]]
84 map2 fun = map (map fun)
87 -- Some Statistics sugar functions
88 -- Exponential Average
89 eavg :: [Double] -> Double
90 eavg (x:xs) = a*x + (1-a)*(eavg xs)
95 mean :: Fractional a => [a] -> a
96 mean xs = if L.null xs then 0.0
97 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 :: (Eq b, Fractional b) => Int -> [b] -> [b]
111 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
113 ma :: [Double] -> [Double]
116 -- | splitEvery n == chunkAlong n n
117 splitEvery :: Int -> [a] -> [[a]]
120 let (h,t) = L.splitAt n xs
121 in h : splitEvery n t
126 -- | Function to split a range into chunks
127 -- if step == grain then linearity
128 -- elif step < grain then overlapping
129 -- else dotted with holes
130 chunkAlong :: Eq a => Grain -> Step -> [a] -> [[a]]
131 chunkAlong a b l = case a > 0 && b > 0 of
132 True -> chunkAlong_ a b l
133 False -> panic "ChunkAlong: Parameters should be > 0 and Grain > Step"
135 chunkAlong_ :: Eq a => Int -> Int -> [a] -> [[a]]
136 chunkAlong_ a b l = filter (/= []) $ only (while dropAlong)
139 while = takeWhile (\x -> length x >= a)
140 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
142 -- | Optimized version (Vector)
143 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
144 chunkAlong' a b l = only (while dropAlong)
146 only = V.map (V.take a)
147 while = V.takeWhile (\x -> V.length x >= a)
148 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
150 -- | TODO Inverse of chunk ? unchunkAlong ?
151 -- unchunkAlong :: Int -> Int -> [[a]] -> [a]
152 -- unchunkAlong = undefined
155 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
156 splitAlong :: [Int] -> [Char] -> [[Char]]
157 splitAlong _ [] = [] -- No list? done
158 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
159 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
161 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
162 takeWhileM _ [] = return []
163 takeWhileM p (a:as) = do
167 vs <- takeWhileM p as
172 -- To select the right algorithme according to the type:
173 -- https://github.com/mikeizbicki/ifcxt
175 sumSimple :: Num a => [a] -> a
176 sumSimple = L.foldl' (+) 0
178 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
179 sumKahan :: Num a => [a] -> a
180 sumKahan = snd . L.foldl' go (0,0)
182 go (c,t) i = ((t'-t)-y,t')
187 -- | compute part of the dict
188 count2map :: (Ord k, Foldable t) => t k -> Map k Double
189 count2map xs = M.map (/ (fromIntegral (length xs))) (count2map' xs)
191 -- | insert in a dict
192 count2map' :: (Ord k, Foldable t) => t k -> Map k Double
193 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) M.empty xs
196 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
197 trunc n = truncate . (* 10^n)
199 trunc' :: Int -> Double -> Double
200 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
203 ------------------------------------------------------------------------
204 bool2num :: Num a => Bool -> a
208 bool2double :: Bool -> Double
209 bool2double = bool2num
211 bool2int :: Bool -> Int
213 ------------------------------------------------------------------------
215 -- Normalizing && scaling data
216 scale :: [Double] -> [Double]
219 scaleMinMax :: [Double] -> [Double]
220 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
226 scaleNormalize :: [Double] -> [Double]
227 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
233 normalize :: [Double] -> [Double]
234 normalize as = normalizeWith identity as
236 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
237 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
241 -- Zip functions to add
242 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
243 zipFst f xs = zip (f xs) xs
245 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
246 zipSnd f xs = zip xs (f xs)
249 maximumWith :: (Ord a1, Foldable t) => (a2 -> a1) -> t a2 -> a2
250 maximumWith f = L.maximumBy (compare `on` f)