Skip to content

Commit 0362ad4

Browse files
jim-smithpre-commit-ci[bot]rpreen
authored
feat: make finalise non-interactive by default (#309)
* make interactivity optional in finalise(); add more tests * update demo to make finalise interactive * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * linting * more linting * really annoying how the checks are different in pre-commit and on GitHub * Update acro/acro.py Co-authored-by: Richard Preen <rpreen@gmail.com> Signed-off-by: Jim-smith <jim-smith@users.noreply.github.com> * addressing reviewer comments --------- Signed-off-by: Jim-smith <jim-smith@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Richard Preen <rpreen@gmail.com>
1 parent b132be7 commit 0362ad4

File tree

7 files changed

+234
-622
lines changed

7 files changed

+234
-622
lines changed

acro/acro.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def __init__(self, config: str = "default", suppress: bool = False) -> None:
7878
# set globals for survival analysis
7979
acro_tables.SURVIVAL_THRESHOLD = self.config["survival_safe_threshold"]
8080

81-
def finalise(self, path: str = "outputs", ext="json") -> Records | None:
81+
def finalise(
82+
self, path: str = "outputs", ext: str = "json", interactive: bool = False
83+
) -> Records | None:
8284
"""Create a results file for checking.
8385
8486
Parameters
@@ -87,6 +89,8 @@ def finalise(self, path: str = "outputs", ext="json") -> Records | None:
8789
Name of a folder to save outputs.
8890
ext : str
8991
Extension of the results file. Valid extensions: {json, xlsx}.
92+
interactive : bool
93+
Whether to prompt the user to request exceptions for failing outputs.
9094
9195
Returns
9296
-------
@@ -101,7 +105,7 @@ def finalise(self, path: str = "outputs", ext="json") -> Records | None:
101105
path,
102106
)
103107
return None
104-
self.results.finalise(path, ext)
108+
self.results.finalise(path, ext, interactive)
105109
config_filename: str = os.path.normpath(f"{path}/config.json")
106110
try:
107111
with open(config_filename, "w", newline="", encoding="utf-8") as file:

acro/acro_tables.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def crosstab( # pylint: disable=too-many-arguments,too-many-locals
214214
)
215215

216216
# record output
217+
217218
self.results.add(
218219
status=status,
219220
output_type="table",
@@ -225,6 +226,11 @@ def crosstab( # pylint: disable=too-many-arguments,too-many-locals
225226
output=[table],
226227
comments=comments,
227228
)
229+
if self.suppress:
230+
justadded = f"output_{self.results.output_id - 1}"
231+
self.results.add_exception(
232+
justadded, "Suppression automatically applied where needed"
233+
)
228234
return table
229235

230236
def pivot_table( # pylint: disable=too-many-arguments,too-many-locals
@@ -398,6 +404,11 @@ def pivot_table( # pylint: disable=too-many-arguments,too-many-locals
398404
output=[table],
399405
comments=comments,
400406
)
407+
if self.suppress:
408+
justadded = f"output_{self.results.output_id - 1}"
409+
self.results.add_exception(
410+
justadded, "Suppression automatically applied where needed"
411+
)
401412
return table
402413

403414
def surv_func( # pylint: disable=too-many-arguments,too-many-locals
@@ -1367,7 +1378,7 @@ def create_dataframe(index, columns) -> DataFrame:
13671378
index_df = pd.concat(index, axis=1)
13681379
elif isinstance(index, pd.Series):
13691380
index_df = pd.DataFrame({index.name: index})
1370-
except ValueError:
1381+
except (ValueError, TypeError):
13711382
index_df = empty_dataframe
13721383

13731384
columns_df = empty_dataframe
@@ -1376,12 +1387,12 @@ def create_dataframe(index, columns) -> DataFrame:
13761387
columns_df = pd.concat(columns, axis=1)
13771388
elif isinstance(columns, pd.Series):
13781389
columns_df = pd.DataFrame({columns.name: columns})
1379-
except ValueError:
1390+
except (ValueError, TypeError):
13801391
columns_df = empty_dataframe
13811392

13821393
try:
13831394
data = pd.concat([index_df, columns_df], axis=1)
1384-
except ValueError:
1395+
except (ValueError, TypeError):
13851396
data = empty_dataframe
13861397

13871398
return data

acro/record.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def validate_outputs(self) -> None:
430430
)
431431
record.exception = input("")
432432

433-
def finalise(self, path: str, ext: str) -> None:
433+
def finalise(self, path: str, ext: str, interactive: bool = False) -> None:
434434
"""Create a results file for checking.
435435
436436
Parameters
@@ -439,9 +439,12 @@ def finalise(self, path: str, ext: str) -> None:
439439
Name of a folder to save outputs.
440440
ext : str
441441
Extension of the results file. Valid extensions: {json, xlsx}.
442+
interactive : Bool
443+
Whether to prompt the user to request exceptions for failing outputs.
442444
"""
443445
logger.debug("finalise()")
444-
self.validate_outputs()
446+
if interactive:
447+
self.validate_outputs()
445448
if ext == "json":
446449
self.finalise_json(path)
447450
elif ext == "xlsx":

0 commit comments

Comments
 (0)