{-# LANGUAGE BangPatterns #-}
module Filter
( filterEntitiesBy
) where
import Data.Bool
import Data.Maybe
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.Sequence as Seq
import qualified Data.Foldable as F
import Data.Function (on)
import qualified Data.Vector as V
import qualified Data.Text as T
import Control.Lens
import qualified Statistics.Sample as Stat
import Types
filterValue :: ValueThreshold -> V.Vector Entity -> V.Vector Entity
filterValue (ValueThreshold x) = V.filter ((>= x) . _value)
stdDevP :: StdDevThreshold -> V.Vector Entity -> Bool
stdDevP (StdDevThreshold x) = (>= x) . Stat.stdDev . fmap _value
getViableEntities
:: Maybe ValueThreshold
-> Maybe StdDevThreshold
-> NumSamples
-> [Entity]
-> [Entity]
getViableEntities vt st (NumSamples n) =
concatMap (\xs -> fmap (set numSamples . length $ xs) xs)
. filter (pass . V.fromList)
. groupBy ((==) `on` _entity)
. sortBy (compare `on` _entity)
where
pass xs = ((>= n) . length . maybe id filterValue vt $ xs)
&& (maybe True (flip stdDevP xs) st)
filterEntitiesBy
:: Maybe ValueThreshold
-> Maybe StdDevThreshold
-> NumSamples
-> Map.Map Sample (V.Vector Entity)
-> Map.Map Sample (V.Vector Entity)
filterEntitiesBy valueThresh stdDevThresh numSamples =
Map.fromList
. fmap (\xs -> (Sample . _sample . head $ xs, V.fromList xs))
. groupBy ((==) `on` _sample)
. sortBy (compare `on` _sample)
. getViableEntities valueThresh stdDevThresh numSamples
. concatMap (V.toList . snd)
. Map.toAscList