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 #-}
18 {-# LANGUAGE OverloadedStrings #-}
20 module Gargantext.Prelude
21 ( module Gargantext.Prelude
24 , module GHC.Err.Located
34 import GHC.Exts (sortWith)
35 import GHC.Err.Located (undefined)
36 import GHC.Real (round)
37 import Control.Monad.IO.Class (MonadIO)
38 import Data.Maybe (isJust, fromJust, maybe)
39 import Protolude ( Bool(True, False), Int, Int64, Double, Integer
40 , Fractional, Num, Maybe(Just,Nothing)
41 , Enum, Bounded, Float
43 , pure, (>>=), (=<<), (<*>), (<$>)
46 , Ord, Integral, Foldable, RealFrac, Monad, filter
47 , reverse, map, mapM, zip, drop, take, zipWith
48 , sum, fromIntegral, length, fmap, foldl, foldl'
49 , takeWhile, sqrt, identity
50 , abs, min, max, maximum, minimum, return, snd, truncate
51 , (+), (*), (/), (-), (.), ($), (&), (**), (^), (<), (>), log
52 , Eq, (==), (>=), (<=), (<>), (/=)
53 , (&&), (||), not, any, all
55 , elem, die, mod, div, const, either
56 , curry, uncurry, repeat
64 -- TODO import functions optimized in Utils.Count
65 -- import Protolude hiding (head, last, all, any, sum, product, length)
66 -- import Gargantext.Utils.Count
67 import qualified Data.List as L hiding (head, sum)
68 import qualified Control.Monad as M
71 import qualified Data.Map as M
73 import Data.Map.Strict (insertWith)
74 import qualified Data.Vector as V
75 import Safe (headMay, lastMay)
76 import Text.Show (Show(), show)
77 import Text.Read (Read())
78 import Data.String.Conversions (cs)
81 printDebug :: (Show a, MonadIO m) => [Char] -> a -> m ()
82 printDebug msg x = putStrLn $ msg <> " " <> show x
83 -- printDebug _ _ = pure ()
86 map2 :: (t -> b) -> [[t]] -> [[b]]
87 map2 fun = map (map fun)
90 -- Some Statistics sugar functions
91 -- Exponential Average
92 eavg :: [Double] -> Double
93 eavg (x:xs) = a*x + (1-a)*(eavg xs)
98 mean :: Fractional a => [a] -> a
99 mean xs = if L.null xs then 0.0
100 else sum xs / fromIntegral (length xs)
103 sumMaybe :: Num a => [Maybe a] -> Maybe a
104 sumMaybe = fmap sum . M.sequence
106 variance :: Floating a => [a] -> a
107 variance xs = mean $ map (\x -> (x - m) ** 2) xs where
110 deviation :: [Double] -> Double
111 deviation = sqrt . variance
113 movingAverage :: (Eq b, Fractional b) => Int -> [b] -> [b]
114 movingAverage steps xs = map mean $ chunkAlong steps 1 xs
116 ma :: [Double] -> [Double]
119 -- | splitEvery n == chunkAlong n n
120 splitEvery :: Int -> [a] -> [[a]]
123 let (h,t) = L.splitAt n xs
124 in h : splitEvery n t
129 -- | Function to split a range into chunks
130 -- if step == grain then linearity
131 -- elif step < grain then overlapping
132 -- else dotted with holes
133 chunkAlong :: Eq a => Grain -> Step -> [a] -> [[a]]
134 chunkAlong a b l = case a > 0 && b > 0 of
135 True -> chunkAlong_ a b l
136 False -> panic "ChunkAlong: Parameters should be > 0 and Grain > Step"
138 chunkAlong_ :: Eq a => Int -> Int -> [a] -> [[a]]
139 chunkAlong_ a b l = filter (/= []) $ only (while dropAlong)
142 while = takeWhile (\x -> length x >= a)
143 dropAlong = L.scanl (\x _y -> drop b x) l ([1..] :: [Integer])
145 -- | Optimized version (Vector)
146 chunkAlong' :: Int -> Int -> V.Vector a -> V.Vector (V.Vector a)
147 chunkAlong' a b l = only (while dropAlong)
149 only = V.map (V.take a)
150 while = V.takeWhile (\x -> V.length x >= a)
151 dropAlong = V.scanl (\x _y -> V.drop b x) l (V.fromList [1..])
153 -- | TODO Inverse of chunk ? unchunkAlong ?
154 -- unchunkAlong :: Int -> Int -> [[a]] -> [a]
155 -- unchunkAlong = undefined
158 -- splitAlong [2,3,4] ("helloworld" :: [Char]) == ["he", "llo", "worl", "d"]
159 splitAlong :: [Int] -> [Char] -> [[Char]]
160 splitAlong _ [] = [] -- No list? done
161 splitAlong [] xs = [xs] -- No place to split at? Return the remainder
162 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
164 takeWhileM :: (Monad m) => (a -> Bool) -> [m a] -> m [a]
165 takeWhileM _ [] = return []
166 takeWhileM p (a:as) = do
170 vs <- takeWhileM p as
175 -- To select the right algorithme according to the type:
176 -- https://github.com/mikeizbicki/ifcxt
178 sumSimple :: Num a => [a] -> a
179 sumSimple = L.foldl' (+) 0
181 -- | https://en.wikipedia.org/wiki/Kahan_summation_algorithm
182 sumKahan :: Num a => [a] -> a
183 sumKahan = snd . L.foldl' go (0,0)
185 go (c,t) i = ((t'-t)-y,t')
190 -- | compute part of the dict
191 count2map :: (Ord k, Foldable t) => t k -> Map k Double
192 count2map xs = M.map (/ (fromIntegral (length xs))) (count2map' xs)
194 -- | insert in a dict
195 count2map' :: (Ord k, Foldable t) => t k -> Map k Double
196 count2map' xs = L.foldl' (\x y -> insertWith (+) y 1 x) M.empty xs
199 trunc :: (RealFrac a, Integral c, Integral b) => b -> a -> c
200 trunc n = truncate . (* 10^n)
202 trunc' :: Int -> Double -> Double
203 trunc' n x = fromIntegral $ truncate $ (x * 10^n)
206 ------------------------------------------------------------------------
207 bool2num :: Num a => Bool -> a
211 bool2double :: Bool -> Double
212 bool2double = bool2num
214 bool2int :: Bool -> Int
216 ------------------------------------------------------------------------
218 -- Normalizing && scaling data
219 scale :: [Double] -> [Double]
222 scaleMinMax :: [Double] -> [Double]
223 scaleMinMax xs = map (\x -> (x - mi / (ma - mi + 1) )) xs'
229 scaleNormalize :: [Double] -> [Double]
230 scaleNormalize xs = map (\x -> (x - v / (m + 1))) xs'
236 normalize :: [Double] -> [Double]
237 normalize as = normalizeWith identity as
239 normalizeWith :: Fractional b => (a -> b) -> [a] -> [b]
240 normalizeWith extract bs = map (\x -> x/(sum bs')) bs'
244 -- Zip functions to add
245 zipFst :: ([b] -> [a]) -> [b] -> [(a, b)]
246 zipFst f xs = zip (f xs) xs
248 zipSnd :: ([a] -> [b]) -> [a] -> [(a, b)]
249 zipSnd f xs = zip xs (f xs)
252 maximumWith :: (Ord a1, Foldable t) => (a2 -> a1) -> t a2 -> a2
253 maximumWith f = L.maximumBy (compare `on` f)