Skip to content

Commit

Permalink
FAI-892: Tyrus TempDirectory (#121)
Browse files Browse the repository at this point in the history
* tyrus temp file

* black

* fixed black moving a pylint disable comment

* os path join
  • Loading branch information
RobGeada authored Nov 30, 2022
1 parent f97c9f4 commit eeef092
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
28 changes: 24 additions & 4 deletions src/trustyai/utils/tyrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# pylint: disable = too-few-public-methods, wrong-import-order, protected-access, cell-var-from-loop
# pylint: disable = too-many-instance-attributes, import-error. too-many-locals
# pylint: disable = consider-using-f-string
import os
import tempfile

import numpy as np
import pandas as pd
from bokeh.io import show, output_file, output_notebook, reset_output
Expand Down Expand Up @@ -110,13 +113,20 @@ def __init__(
The set of background datapoints as a: {}
Keyword Arguments:
* fraction_counterfactuals_to_display : float
(Default=0.1) The fraction of found byproduct counterfactuals to display in the
(default=`0.1`) The fraction of found byproduct counterfactuals to display in the
dashboard, as a float between 0 and 1. Choose a larger number to see more,
but this will make plot rendering more expensive.
* notebook : bool
(Default=False) If true, Tyrus will launch the visualizations inline in a
* notebook : `bool
(default=`False`) If true, Tyrus will launch the visualizations inline in a
Jupyter notebook. If false, the visualizations will be saved as HTML and opened
automatically in your default browser.
* filepath : str
(default=`None`) If `notebook==False`, the Tyrus HTML will be generated in
a temporary directory, the path of which can be accessed by `Tyrus.filepath`. Note
that this temporary directory will be *deleted* when the Tyrus object is deleted/
goes out of scope. Passing a value to `filepath` will manually specify the location
which to generate the Tyrus HTML file, which will
remain there after execution is finished.
"""
self.model = model
self.inputs = one_input_convert(inputs)
Expand All @@ -131,7 +141,17 @@ def __init__(
if self.notebook:
output_notebook()
else:
output_file(filename="trustyai_dashboard.html", title="TrustyAI Dashboard")
if kwargs.get("filepath") is None:
self.tmp_dir = (
tempfile.TemporaryDirectory() # pylint: disable=consider-using-with
)
self.filepath = self.tmp_dir.name
else:
self.filepath = kwargs["filepath"]
output_file(
filename=os.path.join(self.filepath, "trustyai_dashboard.html"),
title="TrustyAI Dashboard",
)

self.cfdict = None
self.shap_saliencies = None
Expand Down
8 changes: 3 additions & 5 deletions tests/general/test_tyrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def predict_function(x):
model,
data.iloc[100],
predictions.iloc[100],
background=data.iloc[:100]
background=data.iloc[:100],
filepath=os.getcwd()
)

# launch dashboard
Expand Down Expand Up @@ -66,7 +67,4 @@ def predict_function(x):
tyrus.run()

# see if dashboard html exists
assert "trustyai_dashboard.html" in os.listdir()

# cleanup
os.remove("trustyai_dashboard.html")
assert "trustyai_dashboard.html" in os.listdir(tyrus.filepath)

0 comments on commit eeef092

Please sign in to comment.