-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
diagnose
method to CmdStanModel
.
- Loading branch information
1 parent
9a59a1a
commit 09ce0bb
Showing
5 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import io | ||
from typing import Optional | ||
|
||
import pandas as pd | ||
import re | ||
|
||
from ..cmdstan_args import Method | ||
from ..utils import scan_config | ||
|
||
from .runset import RunSet | ||
|
||
|
||
class CmdStanDiagnose: | ||
""" | ||
Container for outputs from CmdStan diagnostics. Created by | ||
:meth:`CmdStanModel.diagnose`. | ||
""" | ||
|
||
def __init__(self, runset: RunSet) -> None: | ||
if not runset.method == Method.DIAGNOSE: | ||
raise ValueError( | ||
"Wrong runset method, expecting diagnose runset, found method " | ||
f"{runset.method}." | ||
) | ||
self.runset = runset | ||
self.gradients_ok = runset._check_retcodes() | ||
|
||
# Split the csv into header and gradient table parts. | ||
with open(self.runset.csv_files[0]) as fp: | ||
text = fp.read() | ||
header, table = re.split(r"#\s+Log probability=.*", text, re.M) | ||
self.config = {} | ||
scan_config(io.StringIO(header), self.config, 0) | ||
|
||
# Remove comment characters, leading whitespace, and empty lines. | ||
lines = [] | ||
for line in table.splitlines(): | ||
line = re.sub(r"^#\s+", "", line) | ||
if not line: | ||
continue | ||
# If this is the first line, remove whitespace from column names. | ||
if not lines: | ||
line = ( | ||
line | ||
.replace("param idx", "param_idx") | ||
.replace("finite diff", "finite_diff") | ||
) | ||
lines.append(line) | ||
self._gradients = pd.read_csv(io.StringIO("\n".join(lines)), sep=r"\s+") | ||
|
||
def __repr__(self) -> str: | ||
cmd = self.runset._args.method_args.compose(0, cmd=[]) | ||
lines = [ | ||
f"CmdStanDiagnose: model={self.runset.model}{cmd}", | ||
f"\tcsv_file: {self.runset.csv_files[0]}", | ||
f"\toutput_file: {self.runset.stdout_files[0]}", | ||
] | ||
if not self.gradients_ok: | ||
lines.append( | ||
"Warning: autodiff and finite difference gradients may not " | ||
"agree." | ||
) | ||
return "\n".join(lines) | ||
|
||
@property | ||
def gradients(self) -> pd.DataFrame: | ||
""" | ||
Dataframe of parameter names, autodiff gradients, finite difference | ||
gradients and the delta between the two gradient estimates. | ||
""" | ||
return self._gradients | ||
|
||
def save_csvfiles(self, dir: Optional[str] = None) -> None: | ||
""" | ||
Move output CSV files to specified directory. If files were | ||
written to the temporary session directory, clean filename. | ||
E.g., save 'bernoulli-201912081451-1-5nm6as7u.csv' as | ||
'bernoulli-201912081451-1.csv'. | ||
:param dir: directory path | ||
See Also | ||
-------- | ||
stanfit.RunSet.save_csvfiles | ||
cmdstanpy.from_csv | ||
""" | ||
self.runset.save_csvfiles(dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters