module Criterion.Analysis.Types
(
Outliers (..)
, OutlierEffect(..)
, OutlierVariance(..)
, SampleAnalysis(..)
) where
import Control.Applicative ((<$>), (<*>), empty, pure)
import Control.DeepSeq (NFData(rnf))
import Data.Aeson.Types
import Data.Data (Data)
import Data.Int (Int64)
import Data.Monoid (Monoid(..))
import Data.Typeable (Typeable)
import qualified Statistics.Resampling.Bootstrap as B
data Outliers = Outliers {
samplesSeen :: !Int64
, lowSevere :: !Int64
, lowMild :: !Int64
, highMild :: !Int64
, highSevere :: !Int64
} deriving (Eq, Read, Show, Typeable, Data)
instance NFData Outliers
instance ToJSON Outliers where
toJSON Outliers{..} = object [
"samplesSeen" .= samplesSeen
, "lowSevere" .= lowSevere
, "lowMild" .= lowMild
, "highMild" .= highMild
, "highSevere" .= highSevere
]
instance FromJSON Outliers where
parseJSON (Object v) = Outliers <$>
v .: "samplesSeen" <*>
v .: "lowSevere" <*>
v .: "lowMild" <*>
v .: "highMild" <*>
v .: "highSevere"
parseJSON _ = empty
data OutlierEffect = Unaffected
| Slight
| Moderate
| Severe
deriving (Eq, Ord, Read, Show, Typeable, Data)
instance NFData OutlierEffect
instance ToJSON OutlierEffect where
toJSON Unaffected = "unaffected"
toJSON Slight = "slight"
toJSON Moderate = "moderate"
toJSON Severe = "severe"
instance FromJSON OutlierEffect where
parseJSON (String t) = case t of
_| t== "unaffected" -> pure Unaffected
_| t== "slight" -> pure Slight
_| t== "moderate" -> pure Moderate
_| t== "severe" -> pure Severe
| otherwise -> empty
parseJSON _ = empty
instance Monoid Outliers where
mempty = Outliers 0 0 0 0 0
mappend = addOutliers
addOutliers :: Outliers -> Outliers -> Outliers
addOutliers (Outliers s a b c d) (Outliers t w x y z) =
Outliers (s+t) (a+w) (b+x) (c+y) (d+z)
data OutlierVariance = OutlierVariance {
ovEffect :: OutlierEffect
, ovFraction :: Double
} deriving (Eq, Read, Show, Typeable, Data)
instance NFData OutlierVariance where
rnf OutlierVariance{..} = rnf ovEffect `seq` rnf ovFraction `seq` ()
instance ToJSON OutlierVariance where
toJSON OutlierVariance{..} = object [
"effect" .= ovEffect
, "fraction" .= ovFraction
]
instance FromJSON OutlierVariance where
parseJSON (Object v) = OutlierVariance <$>
v .: "effect" <*>
v .: "fraction"
parseJSON _ = empty
data SampleAnalysis = SampleAnalysis {
anMean :: B.Estimate
, anStdDev :: B.Estimate
, anOutliers :: OutlierVariance
} deriving (Eq, Show, Typeable, Data)
instance NFData SampleAnalysis where
rnf SampleAnalysis{..} =
rnf anMean `seq` rnf anStdDev `seq` rnf anOutliers `seq` ()
instance ToJSON SampleAnalysis where
toJSON SampleAnalysis{..} = object [
"mean" .= anMean
, "stdDev" .= anStdDev
, "outliers" .= anOutliers
]
instance FromJSON SampleAnalysis where
parseJSON (Object v) = SampleAnalysis <$>
v .: "mean" <*>
v .: "stdDev" <*>
v .: "outliers"
parseJSON _ = empty