{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

-- |
-- Module      :  Mcmc.Settings
-- Description :  Settings of Markov chain Monte Carlo samplers
-- Copyright   :  2021 Dominik Schrempf
-- License     :  GPL-3.0-or-later
--
-- Maintainer  :  dominik.schrempf@gmail.com
-- Stability   :  unstable
-- Portability :  portable
--
-- Creation date: Mon Nov 16 11:13:01 2020.
module Mcmc.Settings
  ( -- * Data types
    AnalysisName (..),
    HasAnalysisName (..),
    BurnInSettings (..),
    burnInIterations,
    Iterations (..),
    TraceLength (..),
    ExecutionMode (..),
    HasExecutionMode (..),
    openWithExecutionMode,
    ParallelizationMode (..),
    SaveMode (..),
    LogMode (..),
    Verbosity (..),

    -- * Settings
    Settings (..),
    settingsSave,
    settingsLoad,
    settingsCheck,
    settingsPrettyPrint,
  )
where

import Data.Aeson
import Data.Aeson.TH
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Lazy.Char8 as BL
import Mcmc.Logger
import System.Directory
import System.IO

bsInt :: Int -> BL.ByteString
bsInt :: Int -> ByteString
bsInt = Builder -> ByteString
BB.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Builder
BB.intDec

-- | Analysis name of the MCMC sampler.
newtype AnalysisName = AnalysisName {AnalysisName -> [Char]
fromAnalysisName :: String}
  deriving (AnalysisName -> AnalysisName -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AnalysisName -> AnalysisName -> Bool
$c/= :: AnalysisName -> AnalysisName -> Bool
== :: AnalysisName -> AnalysisName -> Bool
$c== :: AnalysisName -> AnalysisName -> Bool
Eq, ReadPrec [AnalysisName]
ReadPrec AnalysisName
Int -> ReadS AnalysisName
ReadS [AnalysisName]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [AnalysisName]
$creadListPrec :: ReadPrec [AnalysisName]
readPrec :: ReadPrec AnalysisName
$creadPrec :: ReadPrec AnalysisName
readList :: ReadS [AnalysisName]
$creadList :: ReadS [AnalysisName]
readsPrec :: Int -> ReadS AnalysisName
$creadsPrec :: Int -> ReadS AnalysisName
Read, Int -> AnalysisName -> ShowS
[AnalysisName] -> ShowS
AnalysisName -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [AnalysisName] -> ShowS
$cshowList :: [AnalysisName] -> ShowS
show :: AnalysisName -> [Char]
$cshow :: AnalysisName -> [Char]
showsPrec :: Int -> AnalysisName -> ShowS
$cshowsPrec :: Int -> AnalysisName -> ShowS
Show)
  deriving (Semigroup AnalysisName
AnalysisName
[AnalysisName] -> AnalysisName
AnalysisName -> AnalysisName -> AnalysisName
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [AnalysisName] -> AnalysisName
$cmconcat :: [AnalysisName] -> AnalysisName
mappend :: AnalysisName -> AnalysisName -> AnalysisName
$cmappend :: AnalysisName -> AnalysisName -> AnalysisName
mempty :: AnalysisName
$cmempty :: AnalysisName
Monoid, NonEmpty AnalysisName -> AnalysisName
AnalysisName -> AnalysisName -> AnalysisName
forall b. Integral b => b -> AnalysisName -> AnalysisName
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: forall b. Integral b => b -> AnalysisName -> AnalysisName
$cstimes :: forall b. Integral b => b -> AnalysisName -> AnalysisName
sconcat :: NonEmpty AnalysisName -> AnalysisName
$csconcat :: NonEmpty AnalysisName -> AnalysisName
<> :: AnalysisName -> AnalysisName -> AnalysisName
$c<> :: AnalysisName -> AnalysisName -> AnalysisName
Semigroup) via String

$(deriveJSON defaultOptions ''AnalysisName)

-- | Types with analysis names.
class HasAnalysisName s where
  getAnalysisName :: s -> AnalysisName

-- | Burn in specification.
data BurnInSettings
  = -- | No burn in.
    NoBurnIn
  | -- | Burn in for a given number of iterations.
    BurnInWithoutAutoTuning Int
  | -- | Burn in for a given number of iterations. Enable auto tuning with a
    -- given period.
    BurnInWithAutoTuning Int Int
  | -- | Burn in with the given list of fast and full auto tuning periods.
    --
    -- The list of fast auto tuning periods may be empty. All periods have to be
    -- strictly positive.
    --
    -- See also 'Mcmc.Proposals.PSpeed'.
    --
    -- For example, @BurnInWithCustomAutoTuning [50] [100,200]@ performs
    -- 1a. 50 iterations without any slow proposals such as Hamiltonian proposals;
    -- 1b. Auto tuning;
    -- 2a. 100 iterations with all proposals;
    -- 2b Auto tuning;
    -- 3a. 200 iterations with all proposals;
    -- 3b. Auto tuning.
    --
    -- Usually it is useful to auto tune more frequently in the beginning of the
    -- MCMC run.
    BurnInWithCustomAutoTuning [Int] [Int]
  deriving (BurnInSettings -> BurnInSettings -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BurnInSettings -> BurnInSettings -> Bool
$c/= :: BurnInSettings -> BurnInSettings -> Bool
== :: BurnInSettings -> BurnInSettings -> Bool
$c== :: BurnInSettings -> BurnInSettings -> Bool
Eq, ReadPrec [BurnInSettings]
ReadPrec BurnInSettings
Int -> ReadS BurnInSettings
ReadS [BurnInSettings]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BurnInSettings]
$creadListPrec :: ReadPrec [BurnInSettings]
readPrec :: ReadPrec BurnInSettings
$creadPrec :: ReadPrec BurnInSettings
readList :: ReadS [BurnInSettings]
$creadList :: ReadS [BurnInSettings]
readsPrec :: Int -> ReadS BurnInSettings
$creadsPrec :: Int -> ReadS BurnInSettings
Read, Int -> BurnInSettings -> ShowS
[BurnInSettings] -> ShowS
BurnInSettings -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [BurnInSettings] -> ShowS
$cshowList :: [BurnInSettings] -> ShowS
show :: BurnInSettings -> [Char]
$cshow :: BurnInSettings -> [Char]
showsPrec :: Int -> BurnInSettings -> ShowS
$cshowsPrec :: Int -> BurnInSettings -> ShowS
Show)

$(deriveJSON defaultOptions ''BurnInSettings)

burnInPrettyPrint :: BurnInSettings -> BL.ByteString
burnInPrettyPrint :: BurnInSettings -> ByteString
burnInPrettyPrint BurnInSettings
NoBurnIn =
  ByteString
"None."
burnInPrettyPrint (BurnInWithoutAutoTuning Int
x) =
  Int -> ByteString
bsInt Int
x forall a. Semigroup a => a -> a -> a
<> ByteString
" iterations; no auto tune."
burnInPrettyPrint (BurnInWithAutoTuning Int
x Int
y) =
  Int -> ByteString
bsInt Int
x forall a. Semigroup a => a -> a -> a
<> ByteString
" iterations; auto tune with a period of " forall a. Semigroup a => a -> a -> a
<> Int -> ByteString
bsInt Int
y forall a. Semigroup a => a -> a -> a
<> ByteString
"."
burnInPrettyPrint (BurnInWithCustomAutoTuning [Int]
xs [Int]
ys) =
  Int -> ByteString
bsInt (forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
xs) forall a. Semigroup a => a -> a -> a
<> ByteString
" fast, " forall a. Semigroup a => a -> a -> a
<> Int -> ByteString
bsInt (forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
ys) forall a. Semigroup a => a -> a -> a
<> ByteString
" slow iterations; custom auto tune periods."

-- Check if the burn in settings are valid.
burnInValid :: BurnInSettings -> Bool
burnInValid :: BurnInSettings -> Bool
burnInValid BurnInSettings
NoBurnIn = Bool
True
burnInValid (BurnInWithoutAutoTuning Int
n) = Int
n forall a. Ord a => a -> a -> Bool
> Int
0
burnInValid (BurnInWithAutoTuning Int
n Int
t) = Int
n forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
t forall a. Ord a => a -> a -> Bool
> Int
0
-- The list of fast auto tuning periods may be empty, the list of full auto
-- tuning periods must be non-empty. All periods have to be strictly positive.
burnInValid (BurnInWithCustomAutoTuning [Int]
xs [Int]
ys) = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall a. Ord a => a -> a -> Bool
> Int
0) [Int]
xs Bool -> Bool -> Bool
&& Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
ys) Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall a. Ord a => a -> a -> Bool
> Int
0) [Int]
ys

-- | Get the number of burn in iterations.
burnInIterations :: BurnInSettings -> Int
burnInIterations :: BurnInSettings -> Int
burnInIterations BurnInSettings
NoBurnIn = Int
0
burnInIterations (BurnInWithoutAutoTuning Int
n) = Int
n
burnInIterations (BurnInWithAutoTuning Int
n Int
_) = Int
n
burnInIterations (BurnInWithCustomAutoTuning [Int]
xs [Int]
ys) = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
xs forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
ys

-- | Number of normal iterations after burn in.
--
-- Note that auto tuning only happens during burn in.
newtype Iterations = Iterations {Iterations -> Int
fromIterations :: Int}
  deriving (Iterations -> Iterations -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Iterations -> Iterations -> Bool
$c/= :: Iterations -> Iterations -> Bool
== :: Iterations -> Iterations -> Bool
$c== :: Iterations -> Iterations -> Bool
Eq, ReadPrec [Iterations]
ReadPrec Iterations
Int -> ReadS Iterations
ReadS [Iterations]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Iterations]
$creadListPrec :: ReadPrec [Iterations]
readPrec :: ReadPrec Iterations
$creadPrec :: ReadPrec Iterations
readList :: ReadS [Iterations]
$creadList :: ReadS [Iterations]
readsPrec :: Int -> ReadS Iterations
$creadsPrec :: Int -> ReadS Iterations
Read, Int -> Iterations -> ShowS
[Iterations] -> ShowS
Iterations -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [Iterations] -> ShowS
$cshowList :: [Iterations] -> ShowS
show :: Iterations -> [Char]
$cshow :: Iterations -> [Char]
showsPrec :: Int -> Iterations -> ShowS
$cshowsPrec :: Int -> Iterations -> ShowS
Show)

$(deriveJSON defaultOptions ''Iterations)

-- | The length of the stored "Mcmc.Chain.Trace".
--
-- Be careful, this setting determines the memory requirement of the MCMC chain.
data TraceLength
  = -- | Automatically determine the minimum length of the trace. The value is
    -- the maximum of used
    --
    -- - 'Mcmc.Monitor.MonitorBatch' sizes
    --
    -- - auto tune intervals during burn in
    TraceAuto
  | -- | Store a given minimum number of iterations of the chain. Store more
    --  iterations if required (see 'TraceAuto').
    TraceMinimum Int
  deriving (TraceLength -> TraceLength -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TraceLength -> TraceLength -> Bool
$c/= :: TraceLength -> TraceLength -> Bool
== :: TraceLength -> TraceLength -> Bool
$c== :: TraceLength -> TraceLength -> Bool
Eq, Int -> TraceLength -> ShowS
[TraceLength] -> ShowS
TraceLength -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [TraceLength] -> ShowS
$cshowList :: [TraceLength] -> ShowS
show :: TraceLength -> [Char]
$cshow :: TraceLength -> [Char]
showsPrec :: Int -> TraceLength -> ShowS
$cshowsPrec :: Int -> TraceLength -> ShowS
Show)

$(deriveJSON defaultOptions ''TraceLength)

traceLengthPrettyPrint :: TraceLength -> BL.ByteString
traceLengthPrettyPrint :: TraceLength -> ByteString
traceLengthPrettyPrint TraceLength
TraceAuto = ByteString
"Determined automatically."
traceLengthPrettyPrint (TraceMinimum Int
x) = ByteString
"Minimum length of " forall a. Semigroup a => a -> a -> a
<> Int -> ByteString
bsInt Int
x forall a. Semigroup a => a -> a -> a
<> ByteString
"."

validTraceLength :: TraceLength -> Bool
validTraceLength :: TraceLength -> Bool
validTraceLength (TraceMinimum Int
n) = Int
n forall a. Ord a => a -> a -> Bool
> Int
0
validTraceLength TraceLength
_ = Bool
True

-- | Execution mode.
data ExecutionMode
  = -- | Perform new run.
    --
    -- Call 'error' if an output files exists.
    Fail
  | -- | Perform new run.
    --
    -- Overwrite existing output files.
    Overwrite
  | -- | Continue a previous run and append to output files.
    --
    -- Call 'error' if an output file does not exist.
    Continue
  deriving (ExecutionMode -> ExecutionMode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ExecutionMode -> ExecutionMode -> Bool
$c/= :: ExecutionMode -> ExecutionMode -> Bool
== :: ExecutionMode -> ExecutionMode -> Bool
$c== :: ExecutionMode -> ExecutionMode -> Bool
Eq, ReadPrec [ExecutionMode]
ReadPrec ExecutionMode
Int -> ReadS ExecutionMode
ReadS [ExecutionMode]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ExecutionMode]
$creadListPrec :: ReadPrec [ExecutionMode]
readPrec :: ReadPrec ExecutionMode
$creadPrec :: ReadPrec ExecutionMode
readList :: ReadS [ExecutionMode]
$creadList :: ReadS [ExecutionMode]
readsPrec :: Int -> ReadS ExecutionMode
$creadsPrec :: Int -> ReadS ExecutionMode
Read, Int -> ExecutionMode -> ShowS
[ExecutionMode] -> ShowS
ExecutionMode -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [ExecutionMode] -> ShowS
$cshowList :: [ExecutionMode] -> ShowS
show :: ExecutionMode -> [Char]
$cshow :: ExecutionMode -> [Char]
showsPrec :: Int -> ExecutionMode -> ShowS
$cshowsPrec :: Int -> ExecutionMode -> ShowS
Show)

$(deriveJSON defaultOptions ''ExecutionMode)

-- | Types with execution modes.
class HasExecutionMode s where
  getExecutionMode :: s -> ExecutionMode

executionModePrettyPrint :: ExecutionMode -> BL.ByteString
executionModePrettyPrint :: ExecutionMode -> ByteString
executionModePrettyPrint ExecutionMode
Fail = ByteString
"Fail if output files exist."
executionModePrettyPrint ExecutionMode
Overwrite = ByteString
"Overwrite existing output files."
executionModePrettyPrint ExecutionMode
Continue = ByteString
"Expect output files exist."

-- | Open a file honoring the execution mode.
--
-- Call 'error' if execution mode is
--
-- - 'Continue' and file does not exist.
--
-- - 'Fail' and file exists.
openWithExecutionMode :: ExecutionMode -> FilePath -> IO Handle
openWithExecutionMode :: ExecutionMode -> [Char] -> IO Handle
openWithExecutionMode ExecutionMode
em [Char]
fn = do
  Bool
fe <- [Char] -> IO Bool
doesFileExist [Char]
fn
  case (ExecutionMode
em, Bool
fe) of
    (ExecutionMode
Continue, Bool
False) ->
      forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"openWithExecutionMode: Cannot continue; file does not exist: " forall a. [a] -> [a] -> [a]
++ [Char]
fn forall a. [a] -> [a] -> [a]
++ [Char]
"."
    (ExecutionMode
Continue, Bool
True) ->
      [Char] -> IOMode -> IO Handle
openFile [Char]
fn IOMode
AppendMode
    (ExecutionMode
Fail, Bool
True) ->
      forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"openWithExecutionMode: File exists: " forall a. [a] -> [a] -> [a]
++ [Char]
fn forall a. [a] -> [a] -> [a]
++ [Char]
"; use 'Overwrite'?"
    (ExecutionMode, Bool)
_ -> do
      Handle
h <- [Char] -> IOMode -> IO Handle
openFile [Char]
fn IOMode
WriteMode
      Handle -> BufferMode -> IO ()
hSetBuffering Handle
h BufferMode
LineBuffering
      forall (m :: * -> *) a. Monad m => a -> m a
return Handle
h

-- One could automatically select 'Parallel' or 'Sequential' according to the
-- number of capabilities when initializing the environment or according to the
-- iteration time in dependence of the number of used capabilities. However, I
-- decided to opt for a manual configuration, because more capabilities may be
-- available and other parts of the program may be executed in parallel even if
-- sequential execution of the MCMC sampler is beneficial.

-- | Parallelization mode.
--
-- Parallel execution of the chains is only beneficial when the algorithm allows
-- for parallelization, and if computation of the next iteration takes some
-- time. If the calculation of the next state is fast, sequential execution is
-- usually beneficial, even for algorithms involving parallel chains.
--
-- - The "Mcmc.Algorithm.MHG" algorithm is inherently sequential.
--
-- - The "Mcmc.Algorithm.MC3" algorithm works well with parallelization.
--
-- Of course, also the prior or likelihood functions can be computed in
-- parallel. However, this library is unaware about how these functions are
-- computed.
data ParallelizationMode
  = Sequential
  | Parallel
  deriving (ParallelizationMode -> ParallelizationMode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParallelizationMode -> ParallelizationMode -> Bool
$c/= :: ParallelizationMode -> ParallelizationMode -> Bool
== :: ParallelizationMode -> ParallelizationMode -> Bool
$c== :: ParallelizationMode -> ParallelizationMode -> Bool
Eq, ReadPrec [ParallelizationMode]
ReadPrec ParallelizationMode
Int -> ReadS ParallelizationMode
ReadS [ParallelizationMode]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ParallelizationMode]
$creadListPrec :: ReadPrec [ParallelizationMode]
readPrec :: ReadPrec ParallelizationMode
$creadPrec :: ReadPrec ParallelizationMode
readList :: ReadS [ParallelizationMode]
$creadList :: ReadS [ParallelizationMode]
readsPrec :: Int -> ReadS ParallelizationMode
$creadsPrec :: Int -> ReadS ParallelizationMode
Read, Int -> ParallelizationMode -> ShowS
[ParallelizationMode] -> ShowS
ParallelizationMode -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [ParallelizationMode] -> ShowS
$cshowList :: [ParallelizationMode] -> ShowS
show :: ParallelizationMode -> [Char]
$cshow :: ParallelizationMode -> [Char]
showsPrec :: Int -> ParallelizationMode -> ShowS
$cshowsPrec :: Int -> ParallelizationMode -> ShowS
Show)

$(deriveJSON defaultOptions ''ParallelizationMode)

-- | Define information stored on disk.
data SaveMode
  = -- | Do not save the MCMC analysis. The analysis can not be continued.
    NoSave
  | -- | Save the MCMC analysis so that it can be continued. This can be slow,
    -- if the trace is long, or if the states are large objects. See
    -- 'TraceLength'.
    Save
  deriving (SaveMode -> SaveMode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SaveMode -> SaveMode -> Bool
$c/= :: SaveMode -> SaveMode -> Bool
== :: SaveMode -> SaveMode -> Bool
$c== :: SaveMode -> SaveMode -> Bool
Eq, ReadPrec [SaveMode]
ReadPrec SaveMode
Int -> ReadS SaveMode
ReadS [SaveMode]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [SaveMode]
$creadListPrec :: ReadPrec [SaveMode]
readPrec :: ReadPrec SaveMode
$creadPrec :: ReadPrec SaveMode
readList :: ReadS [SaveMode]
$creadList :: ReadS [SaveMode]
readsPrec :: Int -> ReadS SaveMode
$creadsPrec :: Int -> ReadS SaveMode
Read, Int -> SaveMode -> ShowS
[SaveMode] -> ShowS
SaveMode -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [SaveMode] -> ShowS
$cshowList :: [SaveMode] -> ShowS
show :: SaveMode -> [Char]
$cshow :: SaveMode -> [Char]
showsPrec :: Int -> SaveMode -> ShowS
$cshowsPrec :: Int -> SaveMode -> ShowS
Show)

$(deriveJSON defaultOptions ''SaveMode)

saveModePrettyPrint :: SaveMode -> BL.ByteString
saveModePrettyPrint :: SaveMode -> ByteString
saveModePrettyPrint SaveMode
NoSave = ByteString
"Do not save analysis."
saveModePrettyPrint SaveMode
Save = ByteString
"Save analysis."

-- | Settings of an MCMC sampler.
data Settings = Settings
  { Settings -> AnalysisName
sAnalysisName :: AnalysisName,
    Settings -> BurnInSettings
sBurnIn :: BurnInSettings,
    Settings -> Iterations
sIterations :: Iterations,
    Settings -> TraceLength
sTraceLength :: TraceLength,
    Settings -> ExecutionMode
sExecutionMode :: ExecutionMode,
    Settings -> ParallelizationMode
sParallelizationMode :: ParallelizationMode,
    Settings -> SaveMode
sSaveMode :: SaveMode,
    Settings -> LogMode
sLogMode :: LogMode,
    Settings -> Verbosity
sVerbosity :: Verbosity
  }
  deriving (Settings -> Settings -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Settings -> Settings -> Bool
$c/= :: Settings -> Settings -> Bool
== :: Settings -> Settings -> Bool
$c== :: Settings -> Settings -> Bool
Eq, Int -> Settings -> ShowS
[Settings] -> ShowS
Settings -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [Settings] -> ShowS
$cshowList :: [Settings] -> ShowS
show :: Settings -> [Char]
$cshow :: Settings -> [Char]
showsPrec :: Int -> Settings -> ShowS
$cshowsPrec :: Int -> Settings -> ShowS
Show)

instance HasAnalysisName Settings where
  getAnalysisName :: Settings -> AnalysisName
getAnalysisName = Settings -> AnalysisName
sAnalysisName

instance HasExecutionMode Settings where
  getExecutionMode :: Settings -> ExecutionMode
getExecutionMode = Settings -> ExecutionMode
sExecutionMode

instance HasLogMode Settings where
  getLogMode :: Settings -> LogMode
getLogMode = Settings -> LogMode
sLogMode

instance HasVerbosity Settings where
  getVerbosity :: Settings -> Verbosity
getVerbosity = Settings -> Verbosity
sVerbosity

$(deriveJSON defaultOptions ''Settings)

settingsFn :: String -> FilePath
settingsFn :: ShowS
settingsFn [Char]
n = [Char]
n forall a. [a] -> [a] -> [a]
++ [Char]
".mcmc.settings"

-- | Save settings to a file determined by the analysis name.
settingsSave :: Settings -> IO ()
settingsSave :: Settings -> IO ()
settingsSave Settings
s = [Char] -> ByteString -> IO ()
BL.writeFile [Char]
fn forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> ByteString
encode Settings
s
  where
    fn :: [Char]
fn = ShowS
settingsFn forall a b. (a -> b) -> a -> b
$ AnalysisName -> [Char]
fromAnalysisName forall a b. (a -> b) -> a -> b
$ Settings -> AnalysisName
sAnalysisName Settings
s

-- | Load settings.
settingsLoad :: AnalysisName -> IO Settings
settingsLoad :: AnalysisName -> IO Settings
settingsLoad (AnalysisName [Char]
n) = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. HasCallStack => [Char] -> a
error forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> IO ByteString
BL.readFile [Char]
fn
  where
    fn :: [Char]
fn = ShowS
settingsFn [Char]
n

-- Show settings and call 'error'.
settingsError :: Settings -> Int -> String -> a
settingsError :: forall a. Settings -> Int -> [Char] -> a
settingsError Settings
s Int
i [Char]
err =
  forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
    forall a. Show a => a -> [Char]
show Settings
s
      forall a. [a] -> [a] -> [a]
++ [Char]
"\n"
      forall a. [a] -> [a] -> [a]
++ [Char]
"Current iteration: "
      forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
i
      forall a. [a] -> [a] -> [a]
++ [Char]
"\n"
      forall a. [a] -> [a] -> [a]
++ [Char]
"settingsError: "
      forall a. [a] -> [a] -> [a]
++ [Char]
err

-- | Check settings.
--
-- Call 'error' if:
--
-- - The analysis name is the empty string.
--
-- - The number of burn in iterations is negative.
--
-- - Auto tuning period is zero or negative.
--
-- - The number of iterations is negative.
--
-- - The current iteration is larger than the total number of iterations.
--
-- - The current iteration is non-zero but the execution mode is not 'Continue'.
--
-- - The current iteration is zero but the execution mode is 'Continue'.
settingsCheck ::
  Settings ->
  -- | Current iteration.
  Int ->
  IO ()
settingsCheck :: Settings -> Int -> IO ()
settingsCheck s :: Settings
s@(Settings AnalysisName
nm BurnInSettings
bi Iterations
i TraceLength
tl ExecutionMode
em ParallelizationMode
_ SaveMode
_ LogMode
_ Verbosity
_) Int
iCurrent
  | forall (t :: * -> *) a. Foldable t => t a -> Bool
null (AnalysisName -> [Char]
fromAnalysisName AnalysisName
nm) = forall {a}. [Char] -> a
serr [Char]
"Analysis name is the empty string."
  | BurnInSettings -> Int
burnInIterations BurnInSettings
bi forall a. Ord a => a -> a -> Bool
< Int
0 = forall {a}. [Char] -> a
serr [Char]
"Number of burn in iterations is negative."
  | Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ BurnInSettings -> Bool
burnInValid BurnInSettings
bi = forall {a}. [Char] -> a
serr forall a b. (a -> b) -> a -> b
$ [Char]
"Burn in setting invalid: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show BurnInSettings
bi forall a. Semigroup a => a -> a -> a
<> [Char]
"."
  | Iterations -> Int
fromIterations Iterations
i forall a. Ord a => a -> a -> Bool
< Int
0 = forall {a}. [Char] -> a
serr [Char]
"Number of iterations is negative."
  | BurnInSettings -> Int
burnInIterations BurnInSettings
bi forall a. Num a => a -> a -> a
+ Iterations -> Int
fromIterations Iterations
i forall a. Num a => a -> a -> a
- Int
iCurrent forall a. Ord a => a -> a -> Bool
< Int
0 =
      forall {a}. [Char] -> a
serr [Char]
"Current iteration is larger than the total number of iterations."
  | Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ TraceLength -> Bool
validTraceLength TraceLength
tl = forall {a}. [Char] -> a
serr forall a b. (a -> b) -> a -> b
$ [Char]
"Trace length invalid: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> [Char]
show TraceLength
tl forall a. Semigroup a => a -> a -> a
<> [Char]
"."
  | Int
iCurrent forall a. Eq a => a -> a -> Bool
/= Int
0 Bool -> Bool -> Bool
&& ExecutionMode
em forall a. Eq a => a -> a -> Bool
/= ExecutionMode
Continue =
      forall {a}. [Char] -> a
serr [Char]
"Current iteration is non-zero but execution mode is not 'Continue'."
  | Int
iCurrent forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& ExecutionMode
em forall a. Eq a => a -> a -> Bool
== ExecutionMode
Continue =
      forall {a}. [Char] -> a
serr [Char]
"Current iteration is zero but execution mode is 'Continue'."
  | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    serr :: [Char] -> a
serr = forall a. Settings -> Int -> [Char] -> a
settingsError Settings
s Int
iCurrent

logModePrettyPrint :: LogMode -> BL.ByteString
logModePrettyPrint :: LogMode -> ByteString
logModePrettyPrint LogMode
LogStdOutAndFile = ByteString
"Log to standard output and file."
logModePrettyPrint LogMode
LogStdOutOnly = ByteString
"Log to standard output only."
logModePrettyPrint LogMode
LogFileOnly = ByteString
"Log to file only."

-- | Pretty print settings.
settingsPrettyPrint :: Settings -> BL.ByteString
settingsPrettyPrint :: Settings -> ByteString
settingsPrettyPrint (Settings AnalysisName
nm BurnInSettings
bi Iterations
is TraceLength
tl ExecutionMode
em ParallelizationMode
pm SaveMode
sm LogMode
lm Verbosity
vb) =
  [ByteString] -> ByteString
BL.unlines
    [ ByteString
"The MCMC settings are:",
      ByteString
"  Analysis name:        " forall a. Semigroup a => a -> a -> a
<> [Char] -> ByteString
BL.pack (AnalysisName -> [Char]
fromAnalysisName AnalysisName
nm) forall a. Semigroup a => a -> a -> a
<> ByteString
".",
      ByteString
"  Burn in:              " forall a. Semigroup a => a -> a -> a
<> BurnInSettings -> ByteString
burnInPrettyPrint BurnInSettings
bi,
      ByteString
"  Iterations:           " forall a. Semigroup a => a -> a -> a
<> Int -> ByteString
bsInt (Iterations -> Int
fromIterations Iterations
is) forall a. Semigroup a => a -> a -> a
<> ByteString
" iterations.",
      ByteString
"  Trace length:         " forall a. Semigroup a => a -> a -> a
<> TraceLength -> ByteString
traceLengthPrettyPrint TraceLength
tl,
      ByteString
"  Execution mode:       " forall a. Semigroup a => a -> a -> a
<> ExecutionMode -> ByteString
executionModePrettyPrint ExecutionMode
em,
      ByteString
"  Parallelization mode: " forall a. Semigroup a => a -> a -> a
<> [Char] -> ByteString
BL.pack (forall a. Show a => a -> [Char]
show ParallelizationMode
pm) forall a. Semigroup a => a -> a -> a
<> ByteString
".",
      ByteString
"  Save mode:            " forall a. Semigroup a => a -> a -> a
<> SaveMode -> ByteString
saveModePrettyPrint SaveMode
sm,
      ByteString
"  Log mode:             " forall a. Semigroup a => a -> a -> a
<> LogMode -> ByteString
logModePrettyPrint LogMode
lm,
      ByteString
"  Verbosity:            " forall a. Semigroup a => a -> a -> a
<> [Char] -> ByteString
BL.pack (forall a. Show a => a -> [Char]
show Verbosity
vb) forall a. Semigroup a => a -> a -> a
<> ByteString
"."
    ]