{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module      :  Mcmc.Monitor
-- Description :  Monitor a Markov chain
-- Copyright   :  2021 Dominik Schrempf
-- License     :  GPL-3.0-or-later
--
-- Maintainer  :  dominik.schrempf@gmail.com
-- Stability   :  unstable
-- Portability :  portable
--
-- Creation date: Thu May 21 14:35:11 2020.
module Mcmc.Monitor
  ( -- * Create monitors
    Monitor (..),
    Period,
    simpleMonitor,
    MonitorStdOut,
    monitorStdOut,
    msHeader,
    MonitorFile,
    monitorFile,
    BatchSize,
    MonitorBatch,
    monitorBatch,
    getMonitorBatchSize,

    -- * Use monitors
    mOpen,
    mExec,
    mClose,
  )
where

import Control.Monad
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Int
import Data.List hiding (sum)
import Data.Time.Clock
import qualified Data.Vector as VB
import Mcmc.Chain.Link
import Mcmc.Chain.Trace
import Mcmc.Internal.ByteString
import Mcmc.Monitor.Log
import Mcmc.Monitor.Parameter
import Mcmc.Monitor.ParameterBatch
import Mcmc.Monitor.Time
import Mcmc.Settings
import Numeric.Log
import System.IO
import Prelude hiding (sum)

-- | A 'Monitor' observing the chain.
--
-- A 'Monitor' describes which part of the Markov chain should be logged and
-- where. Further, they allow output of summary statistics per iteration in a
-- flexible way.
data Monitor a = Monitor
  { -- | Monitor writing to standard output.
    forall a. Monitor a -> MonitorStdOut a
mStdOut :: MonitorStdOut a,
    -- | Monitors writing to files.
    forall a. Monitor a -> [MonitorFile a]
mFiles :: [MonitorFile a],
    -- | Monitors calculating summary statistics over the last batch of
    -- iterations and writing to files.
    forall a. Monitor a -> [MonitorBatch a]
mBatches :: [MonitorBatch a]
  }

-- | Execute monitor every given number of iterations.
type Period = Int

-- | Only monitor prior, likelihood and posterior functions with given period.
-- Do not monitor parameters.
simpleMonitor :: Period -> Monitor a
simpleMonitor :: forall a. Int -> Monitor a
simpleMonitor Int
p
  | Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 = [Char] -> Monitor a
forall a. HasCallStack => [Char] -> a
error [Char]
"simpleMonitor: Monitor period must be 1 or larger."
  | Bool
otherwise =
      MonitorStdOut a -> [MonitorFile a] -> [MonitorBatch a] -> Monitor a
forall a.
MonitorStdOut a -> [MonitorFile a] -> [MonitorBatch a] -> Monitor a
Monitor ([MonitorParameter a] -> Int -> MonitorStdOut a
forall a. [MonitorParameter a] -> Int -> MonitorStdOut a
MonitorStdOut [] Int
p) [] []

-- | Monitor to standard output; construct with 'monitorStdOut'.
data MonitorStdOut a = MonitorStdOut
  { forall a. MonitorStdOut a -> [MonitorParameter a]
msParams :: [MonitorParameter a],
    forall a. MonitorStdOut a -> Int
msPeriod :: Period
  }

-- | Monitor to standard output.
monitorStdOut ::
  [MonitorParameter a] ->
  Period ->
  MonitorStdOut a
monitorStdOut :: forall a. [MonitorParameter a] -> Int -> MonitorStdOut a
monitorStdOut [MonitorParameter a]
ps Int
p
  | Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 = [Char] -> MonitorStdOut a
forall a. HasCallStack => [Char] -> a
error [Char]
"monitorStdOut: Monitor period must be 1 or larger."
  | Bool
otherwise = [MonitorParameter a] -> Int -> MonitorStdOut a
forall a. [MonitorParameter a] -> Int -> MonitorStdOut a
MonitorStdOut [MonitorParameter a]
ps Int
p

msIWidth :: Int
msIWidth :: Int
msIWidth = Int
9

msWidth :: Int
msWidth :: Int
msWidth = Int
22

msRenderRow :: [BL.ByteString] -> BL.ByteString
msRenderRow :: [ByteString] -> ByteString
msRenderRow [ByteString]
xs = Int -> ByteString -> ByteString
alignRight Int
msIWidth ([ByteString] -> ByteString
forall a. HasCallStack => [a] -> a
head [ByteString]
xs) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> [ByteString] -> ByteString
BL.concat [ByteString]
vals
  where
    vals :: [ByteString]
vals = (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> ByteString -> ByteString
alignRight Int
msWidth) ([ByteString] -> [ByteString]
forall a. HasCallStack => [a] -> [a]
tail [ByteString]
xs)

-- | Header of monitor to standard output.
msHeader :: MonitorStdOut a -> BL.ByteString
msHeader :: forall a. MonitorStdOut a -> ByteString
msHeader MonitorStdOut a
m = ByteString
row ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\n" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
sep
  where
    row :: ByteString
row =
      [ByteString] -> ByteString
msRenderRow ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$
        [ByteString
"Iteration", ByteString
"Log-Prior", ByteString
"Log-Likelihood", ByteString
"Log-Posterior"]
          [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [ByteString]
nms
          [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [ByteString
"Runtime", ByteString
"ETA"]
    sep :: ByteString
sep = Int64 -> Char -> ByteString
BL.replicate (ByteString -> Int64
BL.length ByteString
row) Char
'-'
    nms :: [ByteString]
nms = [[Char] -> ByteString
BL.pack ([Char] -> ByteString) -> [Char] -> ByteString
forall a b. (a -> b) -> a -> b
$ MonitorParameter a -> [Char]
forall a. MonitorParameter a -> [Char]
mpName MonitorParameter a
p | MonitorParameter a
p <- MonitorStdOut a -> [MonitorParameter a]
forall a. MonitorStdOut a -> [MonitorParameter a]
msParams MonitorStdOut a
m]

msDataLine ::
  Int ->
  Link a ->
  Int ->
  UTCTime ->
  Int ->
  MonitorStdOut a ->
  IO BL.ByteString
msDataLine :: forall a.
Int
-> Link a
-> Int
-> UTCTime
-> Int
-> MonitorStdOut a
-> IO ByteString
msDataLine Int
i (Link a
x Log Double
p Log Double
l) Int
ss UTCTime
st Int
j MonitorStdOut a
m = do
  UTCTime
ct <- IO UTCTime
getCurrentTime
  let dt :: NominalDiffTime
dt = UTCTime
ct UTCTime -> UTCTime -> NominalDiffTime
`diffUTCTime` UTCTime
st
      -- NOTE: Don't evaluate this when i == ss.
      timePerIter :: NominalDiffTime
timePerIter = NominalDiffTime
dt NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Fractional a => a -> a -> a
/ Int -> NominalDiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
ss)
      eta :: ByteString
eta =
        if (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
ss) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
10
          then ByteString
""
          else NominalDiffTime -> ByteString
renderDuration (NominalDiffTime -> ByteString) -> NominalDiffTime -> ByteString
forall a b. (a -> b) -> a -> b
$ NominalDiffTime
timePerIter NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* Int -> NominalDiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i)
  ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> IO ByteString) -> ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$
    [ByteString] -> ByteString
msRenderRow ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$
      [[Char] -> ByteString
BL.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i), Log Double -> ByteString
renderLog Log Double
p, Log Double -> ByteString
renderLog Log Double
l, Log Double -> ByteString
renderLog (Log Double
p Log Double -> Log Double -> Log Double
forall a. Num a => a -> a -> a
* Log Double
l)]
        [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [Builder -> ByteString
BB.toLazyByteString (Builder -> ByteString) -> Builder -> ByteString
forall a b. (a -> b) -> a -> b
$ MonitorParameter a -> a -> Builder
forall a. MonitorParameter a -> a -> Builder
mpFunc MonitorParameter a
mp a
x | MonitorParameter a
mp <- MonitorStdOut a -> [MonitorParameter a]
forall a. MonitorStdOut a -> [MonitorParameter a]
msParams MonitorStdOut a
m]
        [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [NominalDiffTime -> ByteString
renderDuration NominalDiffTime
dt, ByteString
eta]

msExec ::
  Int ->
  Link a ->
  Int ->
  UTCTime ->
  Int ->
  MonitorStdOut a ->
  IO (Maybe BL.ByteString)
msExec :: forall a.
Int
-> Link a
-> Int
-> UTCTime
-> Int
-> MonitorStdOut a
-> IO (Maybe ByteString)
msExec Int
i Link a
it Int
ss UTCTime
st Int
j MonitorStdOut a
m
  | Int
i Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` MonitorStdOut a -> Int
forall a. MonitorStdOut a -> Int
msPeriod MonitorStdOut a
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 = Maybe ByteString -> IO (Maybe ByteString)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ByteString
forall a. Maybe a
Nothing
  | Bool
otherwise = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> IO ByteString -> IO (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> Link a
-> Int
-> UTCTime
-> Int
-> MonitorStdOut a
-> IO ByteString
forall a.
Int
-> Link a
-> Int
-> UTCTime
-> Int
-> MonitorStdOut a
-> IO ByteString
msDataLine Int
i Link a
it Int
ss UTCTime
st Int
j MonitorStdOut a
m

-- | Monitor to a file; constructed with 'monitorFile'.
data MonitorFile a = MonitorFile
  { forall a. MonitorFile a -> [Char]
mfName :: String,
    forall a. MonitorFile a -> Maybe Handle
mfHandle :: Maybe Handle,
    forall a. MonitorFile a -> [MonitorParameter a]
mfParams :: [MonitorParameter a],
    forall a. MonitorFile a -> Int
mfPeriod :: Period
  }

-- | Monitor parameters to a file.
monitorFile ::
  -- | Name; used as part of the file name.
  String ->
  [MonitorParameter a] ->
  Period ->
  MonitorFile a
monitorFile :: forall a. [Char] -> [MonitorParameter a] -> Int -> MonitorFile a
monitorFile [Char]
n [MonitorParameter a]
ps Int
p
  | Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 = [Char] -> MonitorFile a
forall a. HasCallStack => [Char] -> a
error [Char]
"monitorFile: Monitor period must be 1 or larger."
  | Bool
otherwise = [Char]
-> Maybe Handle -> [MonitorParameter a] -> Int -> MonitorFile a
forall a.
[Char]
-> Maybe Handle -> [MonitorParameter a] -> Int -> MonitorFile a
MonitorFile [Char]
n Maybe Handle
forall a. Maybe a
Nothing [MonitorParameter a]
ps Int
p

mfRenderRow :: [BL.ByteString] -> BL.ByteString
mfRenderRow :: [ByteString] -> ByteString
mfRenderRow = ByteString -> [ByteString] -> ByteString
BL.intercalate ByteString
"\t"

mfOpen :: String -> String -> ExecutionMode -> MonitorFile a -> IO (MonitorFile a)
mfOpen :: forall a.
[Char]
-> [Char] -> ExecutionMode -> MonitorFile a -> IO (MonitorFile a)
mfOpen [Char]
pre [Char]
suf ExecutionMode
em MonitorFile a
m = do
  let fn :: [Char]
fn = [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"." ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Char] -> Bool) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) [[Char]
pre, MonitorFile a -> [Char]
forall a. MonitorFile a -> [Char]
mfName MonitorFile a
m, [Char]
suf, [Char]
"monitor"]
  Handle
h <- ExecutionMode -> [Char] -> IO Handle
openWithExecutionMode ExecutionMode
em [Char]
fn
  MonitorFile a -> IO (MonitorFile a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (MonitorFile a -> IO (MonitorFile a))
-> MonitorFile a -> IO (MonitorFile a)
forall a b. (a -> b) -> a -> b
$ MonitorFile a
m {mfHandle = Just h}

mfHeader :: MonitorFile a -> IO ()
mfHeader :: forall a. MonitorFile a -> IO ()
mfHeader MonitorFile a
m = case MonitorFile a -> Maybe Handle
forall a. MonitorFile a -> Maybe Handle
mfHandle MonitorFile a
m of
  Maybe Handle
Nothing ->
    [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
      [Char]
"mfHeader: No handle available for monitor with name "
        [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> MonitorFile a -> [Char]
forall a. MonitorFile a -> [Char]
mfName MonitorFile a
m
        [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"."
  Just Handle
h ->
    Handle -> ByteString -> IO ()
BL.hPutStrLn Handle
h (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$
      [ByteString] -> ByteString
mfRenderRow ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$
        [ByteString
"Iteration", ByteString
"Log-Prior", ByteString
"Log-Likelihood", ByteString
"Log-Posterior"]
          [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [[Char] -> ByteString
BL.pack ([Char] -> ByteString) -> [Char] -> ByteString
forall a b. (a -> b) -> a -> b
$ MonitorParameter a -> [Char]
forall a. MonitorParameter a -> [Char]
mpName MonitorParameter a
p | MonitorParameter a
p <- MonitorFile a -> [MonitorParameter a]
forall a. MonitorFile a -> [MonitorParameter a]
mfParams MonitorFile a
m]

mfExec ::
  Int ->
  Link a ->
  MonitorFile a ->
  IO ()
mfExec :: forall a. Int -> Link a -> MonitorFile a -> IO ()
mfExec Int
i (Link a
x Log Double
p Log Double
l) MonitorFile a
m
  | Int
i Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` MonitorFile a -> Int
forall a. MonitorFile a -> Int
mfPeriod MonitorFile a
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  | Bool
otherwise = case MonitorFile a -> Maybe Handle
forall a. MonitorFile a -> Maybe Handle
mfHandle MonitorFile a
m of
      Maybe Handle
Nothing ->
        [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
          [Char]
"mfExec: No handle available for monitor with name "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> MonitorFile a -> [Char]
forall a. MonitorFile a -> [Char]
mfName MonitorFile a
m
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"."
      Just Handle
h ->
        Handle -> ByteString -> IO ()
BL.hPutStrLn Handle
h (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$
          [ByteString] -> ByteString
mfRenderRow ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$
            [Char] -> ByteString
BL.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i)
              ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: Log Double -> ByteString
renderLog Log Double
p
              ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: Log Double -> ByteString
renderLog Log Double
l
              ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: Log Double -> ByteString
renderLog (Log Double
p Log Double -> Log Double -> Log Double
forall a. Num a => a -> a -> a
* Log Double
l)
              ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [Builder -> ByteString
BB.toLazyByteString (Builder -> ByteString) -> Builder -> ByteString
forall a b. (a -> b) -> a -> b
$ MonitorParameter a -> a -> Builder
forall a. MonitorParameter a -> a -> Builder
mpFunc MonitorParameter a
mp a
x | MonitorParameter a
mp <- MonitorFile a -> [MonitorParameter a]
forall a. MonitorFile a -> [MonitorParameter a]
mfParams MonitorFile a
m]

mfClose :: MonitorFile a -> IO ()
mfClose :: forall a. MonitorFile a -> IO ()
mfClose MonitorFile a
m = case MonitorFile a -> Maybe Handle
forall a. MonitorFile a -> Maybe Handle
mfHandle MonitorFile a
m of
  Just Handle
h -> Handle -> IO ()
hClose Handle
h
  Maybe Handle
Nothing -> [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"mfClose: File was not opened for monitor " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> MonitorFile a -> [Char]
forall a. MonitorFile a -> [Char]
mfName MonitorFile a
m [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"."

-- | Size of the batch used to calculate summary statistics.
type BatchSize = Int

-- | Batch monitor to a file.
--
-- Calculate summary statistics over the last given number of iterations (batch
-- size). Construct with 'monitorBatch'.
data MonitorBatch a = MonitorBatch
  { forall a. MonitorBatch a -> [Char]
mbName :: String,
    forall a. MonitorBatch a -> Maybe Handle
mbHandle :: Maybe Handle,
    forall a. MonitorBatch a -> [MonitorParameterBatch a]
mbParams :: [MonitorParameterBatch a],
    forall a. MonitorBatch a -> Int
mbSize :: BatchSize
  }

-- | Batch monitor parameters to a file, see 'MonitorBatch'.
monitorBatch ::
  -- | Name; used as part of the file name.
  String ->
  [MonitorParameterBatch a] ->
  BatchSize ->
  MonitorBatch a
monitorBatch :: forall a.
[Char] -> [MonitorParameterBatch a] -> Int -> MonitorBatch a
monitorBatch [Char]
n [MonitorParameterBatch a]
ps Int
b
  | Int
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2 = [Char] -> MonitorBatch a
forall a. HasCallStack => [Char] -> a
error [Char]
"monitorBatch: Batch size must be 2 or larger."
  | Bool
otherwise = [Char]
-> Maybe Handle
-> [MonitorParameterBatch a]
-> Int
-> MonitorBatch a
forall a.
[Char]
-> Maybe Handle
-> [MonitorParameterBatch a]
-> Int
-> MonitorBatch a
MonitorBatch [Char]
n Maybe Handle
forall a. Maybe a
Nothing [MonitorParameterBatch a]
ps Int
b

-- | Batch monitor size.
--
-- Useful to determine the trace length.
getMonitorBatchSize :: MonitorBatch a -> BatchSize
getMonitorBatchSize :: forall a. MonitorBatch a -> Int
getMonitorBatchSize = MonitorBatch a -> Int
forall a. MonitorBatch a -> Int
mbSize

mbOpen :: String -> String -> ExecutionMode -> MonitorBatch a -> IO (MonitorBatch a)
mbOpen :: forall a.
[Char]
-> [Char] -> ExecutionMode -> MonitorBatch a -> IO (MonitorBatch a)
mbOpen [Char]
pre [Char]
suf ExecutionMode
em MonitorBatch a
m = do
  let fn :: [Char]
fn = [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"." ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Char] -> Bool) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) [[Char]
pre, MonitorBatch a -> [Char]
forall a. MonitorBatch a -> [Char]
mbName MonitorBatch a
m, [Char]
suf, [Char]
"batch"]
  Handle
h <- ExecutionMode -> [Char] -> IO Handle
openWithExecutionMode ExecutionMode
em [Char]
fn
  MonitorBatch a -> IO (MonitorBatch a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (MonitorBatch a -> IO (MonitorBatch a))
-> MonitorBatch a -> IO (MonitorBatch a)
forall a b. (a -> b) -> a -> b
$ MonitorBatch a
m {mbHandle = Just h}

mbHeader :: MonitorBatch a -> IO ()
mbHeader :: forall a. MonitorBatch a -> IO ()
mbHeader MonitorBatch a
m = case MonitorBatch a -> Maybe Handle
forall a. MonitorBatch a -> Maybe Handle
mbHandle MonitorBatch a
m of
  Maybe Handle
Nothing ->
    [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
      [Char]
"mbHeader: No handle available for batch monitor with name "
        [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> MonitorBatch a -> [Char]
forall a. MonitorBatch a -> [Char]
mbName MonitorBatch a
m
        [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"."
  Just Handle
h ->
    Handle -> ByteString -> IO ()
BL.hPutStrLn Handle
h (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$
      [ByteString] -> ByteString
mfRenderRow ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$
        [ByteString
"Iteration", ByteString
"Mean log-Prior", ByteString
"Mean log-Likelihood", ByteString
"Mean log-Posterior"]
          [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [[Char] -> ByteString
BL.pack ([Char] -> ByteString) -> [Char] -> ByteString
forall a b. (a -> b) -> a -> b
$ MonitorParameterBatch a -> [Char]
forall a. MonitorParameterBatch a -> [Char]
mbpName MonitorParameterBatch a
mbp | MonitorParameterBatch a
mbp <- MonitorBatch a -> [MonitorParameterBatch a]
forall a. MonitorBatch a -> [MonitorParameterBatch a]
mbParams MonitorBatch a
m]

mean :: VB.Vector (Log Double) -> Log Double
mean :: Vector (Log Double) -> Log Double
mean Vector (Log Double)
xs = Vector (Log Double) -> Log Double
forall a. Num a => Vector a -> a
VB.sum Vector (Log Double)
xs Log Double -> Log Double -> Log Double
forall a. Fractional a => a -> a -> a
/ Int -> Log Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector (Log Double) -> Int
forall a. Vector a -> Int
VB.length Vector (Log Double)
xs)

mbExec ::
  Int ->
  Trace a ->
  MonitorBatch a ->
  IO ()
mbExec :: forall a. Int -> Trace a -> MonitorBatch a -> IO ()
mbExec Int
i Trace a
t MonitorBatch a
m
  | (Int
i Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` MonitorBatch a -> Int
forall a. MonitorBatch a -> Int
mbSize MonitorBatch a
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) Bool -> Bool -> Bool
|| (Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  | Bool
otherwise = case MonitorBatch a -> Maybe Handle
forall a. MonitorBatch a -> Maybe Handle
mbHandle MonitorBatch a
m of
      Maybe Handle
Nothing ->
        [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
          [Char]
"mbExec: No handle available for batch monitor with name "
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> MonitorBatch a -> [Char]
forall a. MonitorBatch a -> [Char]
mbName MonitorBatch a
m
            [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"."
      Just Handle
h -> do
        Vector (Link a)
xs <- Int -> Trace a -> IO (Vector (Link a))
forall a. Int -> Trace a -> IO (Vector (Link a))
takeT (MonitorBatch a -> Int
forall a. MonitorBatch a -> Int
mbSize MonitorBatch a
m) Trace a
t
        let lps :: Vector (Log Double)
lps = (Link a -> Log Double) -> Vector (Link a) -> Vector (Log Double)
forall a b. (a -> b) -> Vector a -> Vector b
VB.map Link a -> Log Double
forall a. Link a -> Log Double
prior Vector (Link a)
xs
            lls :: Vector (Log Double)
lls = (Link a -> Log Double) -> Vector (Link a) -> Vector (Log Double)
forall a b. (a -> b) -> Vector a -> Vector b
VB.map Link a -> Log Double
forall a. Link a -> Log Double
likelihood Vector (Link a)
xs
            los :: Vector (Log Double)
los = (Log Double -> Log Double -> Log Double)
-> Vector (Log Double)
-> Vector (Log Double)
-> Vector (Log Double)
forall a b c. (a -> b -> c) -> Vector a -> Vector b -> Vector c
VB.zipWith Log Double -> Log Double -> Log Double
forall a. Num a => a -> a -> a
(*) Vector (Log Double)
lps Vector (Log Double)
lls
            mlps :: Log Double
mlps = Vector (Log Double) -> Log Double
mean Vector (Log Double)
lps
            mlls :: Log Double
mlls = Vector (Log Double) -> Log Double
mean Vector (Log Double)
lls
            mlos :: Log Double
mlos = Vector (Log Double) -> Log Double
mean Vector (Log Double)
los
        Handle -> ByteString -> IO ()
BL.hPutStrLn Handle
h (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$
          [ByteString] -> ByteString
mfRenderRow ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$
            [Char] -> ByteString
BL.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
i)
              ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: Log Double -> ByteString
renderLog Log Double
mlps
              ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: Log Double -> ByteString
renderLog Log Double
mlls
              ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: Log Double -> ByteString
renderLog Log Double
mlos
              ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [Builder -> ByteString
BB.toLazyByteString (Builder -> ByteString) -> Builder -> ByteString
forall a b. (a -> b) -> a -> b
$ MonitorParameterBatch a -> Vector a -> Builder
forall a. MonitorParameterBatch a -> Vector a -> Builder
mbpFunc MonitorParameterBatch a
mbp ((Link a -> a) -> Vector (Link a) -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
VB.map Link a -> a
forall a. Link a -> a
state Vector (Link a)
xs) | MonitorParameterBatch a
mbp <- MonitorBatch a -> [MonitorParameterBatch a]
forall a. MonitorBatch a -> [MonitorParameterBatch a]
mbParams MonitorBatch a
m]

mbClose :: MonitorBatch a -> IO ()
mbClose :: forall a. MonitorBatch a -> IO ()
mbClose MonitorBatch a
m = case MonitorBatch a -> Maybe Handle
forall a. MonitorBatch a -> Maybe Handle
mbHandle MonitorBatch a
m of
  Just Handle
h -> Handle -> IO ()
hClose Handle
h
  Maybe Handle
Nothing -> [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"mfClose: File was not opened for batch monitor: " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> MonitorBatch a -> [Char]
forall a. MonitorBatch a -> [Char]
mbName MonitorBatch a
m [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"."

-- | Open the files associated with the 'Monitor'.
mOpen ::
  -- Base name prefix.
  String ->
  -- Base name suffix.
  String ->
  ExecutionMode ->
  Monitor a ->
  IO (Monitor a)
mOpen :: forall a.
[Char] -> [Char] -> ExecutionMode -> Monitor a -> IO (Monitor a)
mOpen [Char]
pre [Char]
suf ExecutionMode
em (Monitor MonitorStdOut a
s [MonitorFile a]
fs [MonitorBatch a]
bs) = do
  [MonitorFile a]
fs' <- (MonitorFile a -> IO (MonitorFile a))
-> [MonitorFile a] -> IO [MonitorFile a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Char]
-> [Char] -> ExecutionMode -> MonitorFile a -> IO (MonitorFile a)
forall a.
[Char]
-> [Char] -> ExecutionMode -> MonitorFile a -> IO (MonitorFile a)
mfOpen [Char]
pre [Char]
suf ExecutionMode
em) [MonitorFile a]
fs
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ExecutionMode
em ExecutionMode -> ExecutionMode -> Bool
forall a. Eq a => a -> a -> Bool
== ExecutionMode
Continue) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ (MonitorFile a -> IO ()) -> [MonitorFile a] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MonitorFile a -> IO ()
forall a. MonitorFile a -> IO ()
mfHeader [MonitorFile a]
fs'
  [MonitorBatch a]
bs' <- (MonitorBatch a -> IO (MonitorBatch a))
-> [MonitorBatch a] -> IO [MonitorBatch a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Char]
-> [Char] -> ExecutionMode -> MonitorBatch a -> IO (MonitorBatch a)
forall a.
[Char]
-> [Char] -> ExecutionMode -> MonitorBatch a -> IO (MonitorBatch a)
mbOpen [Char]
pre [Char]
suf ExecutionMode
em) [MonitorBatch a]
bs
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ExecutionMode
em ExecutionMode -> ExecutionMode -> Bool
forall a. Eq a => a -> a -> Bool
== ExecutionMode
Continue) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ (MonitorBatch a -> IO ()) -> [MonitorBatch a] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MonitorBatch a -> IO ()
forall a. MonitorBatch a -> IO ()
mbHeader [MonitorBatch a]
bs'
  Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdout BufferMode
LineBuffering
  Monitor a -> IO (Monitor a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Monitor a -> IO (Monitor a)) -> Monitor a -> IO (Monitor a)
forall a b. (a -> b) -> a -> b
$ MonitorStdOut a -> [MonitorFile a] -> [MonitorBatch a] -> Monitor a
forall a.
MonitorStdOut a -> [MonitorFile a] -> [MonitorBatch a] -> Monitor a
Monitor MonitorStdOut a
s [MonitorFile a]
fs' [MonitorBatch a]
bs'

-- | Execute monitors; print status information to files and return text to be
-- printed to standard output and log file.
mExec ::
  Verbosity ->
  -- | Iteration.
  Int ->
  -- | Starting state.
  Int ->
  -- | Starting time.
  UTCTime ->
  Trace a ->
  -- | Total number of iterations; to calculate ETA.
  Int ->
  Monitor a ->
  IO (Maybe BL.ByteString)
mExec :: forall a.
Verbosity
-> Int
-> Int
-> UTCTime
-> Trace a
-> Int
-> Monitor a
-> IO (Maybe ByteString)
mExec Verbosity
v Int
i Int
ss UTCTime
st Trace a
xs Int
j (Monitor MonitorStdOut a
s [MonitorFile a]
fs [MonitorBatch a]
bs) = do
  Link a
x <- Trace a -> IO (Link a)
forall a. Trace a -> IO (Link a)
headT Trace a
xs
  (MonitorFile a -> IO ()) -> [MonitorFile a] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Int -> Link a -> MonitorFile a -> IO ()
forall a. Int -> Link a -> MonitorFile a -> IO ()
mfExec Int
i Link a
x) [MonitorFile a]
fs
  -- NOTE: Batch monitors are slow because separate batch monitors will extract
  -- separate immutable stacks from the trace. However, using folds on the
  -- mutable stack only could be an option! But then, we require two polymorphic
  -- types (for the fold).
  (MonitorBatch a -> IO ()) -> [MonitorBatch a] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Int -> Trace a -> MonitorBatch a -> IO ()
forall a. Int -> Trace a -> MonitorBatch a -> IO ()
mbExec Int
i Trace a
xs) [MonitorBatch a]
bs
  if Verbosity
v Verbosity -> Verbosity -> Bool
forall a. Eq a => a -> a -> Bool
== Verbosity
Quiet
    then Maybe ByteString -> IO (Maybe ByteString)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ByteString
forall a. Maybe a
Nothing
    else Int
-> Link a
-> Int
-> UTCTime
-> Int
-> MonitorStdOut a
-> IO (Maybe ByteString)
forall a.
Int
-> Link a
-> Int
-> UTCTime
-> Int
-> MonitorStdOut a
-> IO (Maybe ByteString)
msExec Int
i Link a
x Int
ss UTCTime
st Int
j MonitorStdOut a
s

-- | Close the files associated with the 'Monitor'.
mClose :: Monitor a -> IO (Monitor a)
mClose :: forall a. Monitor a -> IO (Monitor a)
mClose m :: Monitor a
m@(Monitor MonitorStdOut a
_ [MonitorFile a]
fms [MonitorBatch a]
bms) = do
  (MonitorFile a -> IO ()) -> [MonitorFile a] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MonitorFile a -> IO ()
forall a. MonitorFile a -> IO ()
mfClose [MonitorFile a]
fms
  (MonitorBatch a -> IO ()) -> [MonitorBatch a] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MonitorBatch a -> IO ()
forall a. MonitorBatch a -> IO ()
mbClose [MonitorBatch a]
bms
  let fms' :: [MonitorFile a]
fms' = (MonitorFile a -> MonitorFile a)
-> [MonitorFile a] -> [MonitorFile a]
forall a b. (a -> b) -> [a] -> [b]
map (\MonitorFile a
fm -> MonitorFile a
fm {mfHandle = Nothing}) [MonitorFile a]
fms
  let bms' :: [MonitorBatch a]
bms' = (MonitorBatch a -> MonitorBatch a)
-> [MonitorBatch a] -> [MonitorBatch a]
forall a b. (a -> b) -> [a] -> [b]
map (\MonitorBatch a
bm -> MonitorBatch a
bm {mbHandle = Nothing}) [MonitorBatch a]
bms
  Monitor a -> IO (Monitor a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Monitor a
m {mFiles = fms', mBatches = bms'}