Skip to content

Commit

Permalink
locli: factor rendering & granular plain text output
Browse files Browse the repository at this point in the history
  • Loading branch information
deepfire committed Jun 21, 2022
1 parent 85c1d4f commit 0252818
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
58 changes: 45 additions & 13 deletions bench/locli/src/Cardano/Unlog/Render.hs
Expand Up @@ -101,12 +101,54 @@ renderTimeline run flt withCommentary xs =
renderLine' lpfn wfn rfn = renderField lpfn wfn rfn <$> fields
renderField lpfn wfn rfn f = T.replicate (lpfn f) " " <> T.center (wfn f) ' ' (rfn f)

renderFields :: a p -> (forall v. Divisible v => CDF p v -> [v]) -> Field DSelect p a -> [Text]
renderFields x cdfProj =
\Field{..} ->
T.pack <$> case fSelect of
DInt (cdfProj . ($x) ->ds) -> printf "%*d" fWidth <$> ds
DWord64 (cdfProj . ($x) ->ds) -> printf "%*d" fWidth <$> ds
DFloat (cdfProj . ($x) ->ds) -> take fWidth . printf "%*F" (fWidth - 2) <$> ds
DDeltaT (cdfProj . ($x) ->ds) -> take fWidth . dropWhileEnd (== 's') . show <$> ds

mapRenderCDF :: forall p a. (RenderCDFs a p, KnownCDF p) => (Field DSelect p a -> Bool) -> Maybe [Centile] -> a p -> [[Text]]
mapRenderCDF select mCentis x =
fields <&> (renderFields x $ index)
where
-- Pick relevant fields:
fields :: [Field DSelect p a]
fields = centiField : filter select rdFields

-- Pick relevant centiles:
subset :: CDF p b -> CDF p b
subset = maybe id subsetCDF mCentis

-- Get relevant values
index :: Divisible c => CDF p c -> [c]
index = fmap (unliftCDFVal cdfIx . snd) . cdfSamples . subset

-- The leftmost index "CDF": just list the centiles
centiField = Field 6 0 "centi" "" "centi" (DFloat $ const centiCDF)
centiCDF = mapSomeFieldDirectCDF (cdfCentilesCDF . subset) x (fSelect $ head rdFields)

renderCDFsAsText :: (RenderCDFs a p, KnownCDF p) => Anchor -> (Field DSelect p a -> Bool) -> a p -> [(Text, [Text])]
renderCDFsAsText a select x =
filter select rdFields <&>
\Field{fId=cdfField} ->
(,) cdfField $
renderAnchor a :
(mapRenderCDF ((== cdfField) . fId) Nothing x
& fmap (T.intercalate " "))

renderCDFs :: forall p a. (RenderCDFs a p, KnownCDF p) => Anchor -> RenderMode -> (Field DSelect p a -> Bool) -> Maybe [Centile] -> a p
-> [Text]
renderCDFs a mode flt mCentis x =
case mode of
RenderPretty -> renderAnchor a : catMaybes [head1, head2] <> pLines <> sizeAvg
RenderCsv -> headCsv : pLines
RenderPretty -> renderAnchor a
: catMaybes [head1, head2]
<> pLines
<> sizeAvg
RenderCsv -> headCsv
: pLines
where headCsv = T.intercalate "," $ fId <$> fields

where
Expand Down Expand Up @@ -183,17 +225,7 @@ renderCDFs a mode flt mCentis x =
percField :: Field DSelect p a
percField = Field 6 0 "%tile" "" "%tile" (DFloat $ const percsDistrib)
nCentis = length $ cdfSamples percsDistrib
percsDistrib = mapSomeFieldDirectCDF
(distribCentisAsDistrib . subsetDistrib) x (fSelect $ head rdFields)
distribCentisAsDistrib :: CDF p b -> CDF p Double
distribCentisAsDistrib CDF{..} =
CDF
(length cdfSamples)
0.5
0.5
(head cdfSamples & unCentile . fst,
last cdfSamples & unCentile . fst)
$ (id &&& flip liftCDFVal cdfIx . unCentile) . fst <$> cdfSamples
percsDistrib = mapSomeFieldDirectCDF (cdfCentilesCDF . subsetDistrib) x (fSelect $ head rdFields)

nsamplesField :: Field DSelect p a
nsamplesField = Field 6 0 "Nsamp" "" "Nsamp" (DInt $ const nsamplesDistrib)
Expand Down
11 changes: 10 additions & 1 deletion bench/locli/src/Data/CDF.hs
Expand Up @@ -27,13 +27,15 @@ module Data.CDF
, unliftCDFVal
, centilesCDF
, filterCDF
, subsetCDF
, zeroCDF
, projectCDF
, projectCDF'
, indexCDF
, DirectCDF
, cdf
, mapToCDF
, cdfCentilesCDF
, Divisible (..)
, Combine (..)
, stdCombine1
Expand Down Expand Up @@ -156,6 +158,9 @@ filterCDF :: ((Centile, p a) -> Bool) -> CDF p a -> CDF p a
filterCDF f d =
d { cdfSamples = cdfSamples d & filter f }

subsetCDF :: [Centile] -> CDF p b -> CDF p b
subsetCDF = filterCDF . \cs c -> elem (fst c) cs

indexCDF :: Int -> CDF p a -> p a
indexCDF i d = snd $ cdfSamples d !! i

Expand Down Expand Up @@ -212,9 +217,13 @@ cdf centiles (sort -> sorted) =
then (0, 0)
else (vec Vec.! 0, Vec.last vec)

mapToCDF :: Real a => (b -> a) -> [Centile] -> [b] -> DirectCDF a
mapToCDF :: (Real a, KnownCDF p) => (b -> a) -> [Centile] -> [b] -> CDF p a
mapToCDF f pspecs xs = cdf pspecs (f <$> xs)

cdfCentilesCDF :: KnownCDF p => CDF p a -> CDF p Double
cdfCentilesCDF c =
mapToCDF unCentile (centilesCDF c) (centilesCDF c)

type CDF2 a = CDF (CDF I) a

data CDFError
Expand Down

0 comments on commit 0252818

Please sign in to comment.