Skip to content

Commit eeef092

Browse files
authored
FAI-892: Tyrus TempDirectory (#121)
* tyrus temp file * black * fixed black moving a pylint disable comment * os path join
1 parent f97c9f4 commit eeef092

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/trustyai/utils/tyrus.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# pylint: disable = too-few-public-methods, wrong-import-order, protected-access, cell-var-from-loop
33
# pylint: disable = too-many-instance-attributes, import-error. too-many-locals
44
# pylint: disable = consider-using-f-string
5+
import os
6+
import tempfile
7+
58
import numpy as np
69
import pandas as pd
710
from bokeh.io import show, output_file, output_notebook, reset_output
@@ -110,13 +113,20 @@ def __init__(
110113
The set of background datapoints as a: {}
111114
Keyword Arguments:
112115
* fraction_counterfactuals_to_display : float
113-
(Default=0.1) The fraction of found byproduct counterfactuals to display in the
116+
(default=`0.1`) The fraction of found byproduct counterfactuals to display in the
114117
dashboard, as a float between 0 and 1. Choose a larger number to see more,
115118
but this will make plot rendering more expensive.
116-
* notebook : bool
117-
(Default=False) If true, Tyrus will launch the visualizations inline in a
119+
* notebook : `bool
120+
(default=`False`) If true, Tyrus will launch the visualizations inline in a
118121
Jupyter notebook. If false, the visualizations will be saved as HTML and opened
119122
automatically in your default browser.
123+
* filepath : str
124+
(default=`None`) If `notebook==False`, the Tyrus HTML will be generated in
125+
a temporary directory, the path of which can be accessed by `Tyrus.filepath`. Note
126+
that this temporary directory will be *deleted* when the Tyrus object is deleted/
127+
goes out of scope. Passing a value to `filepath` will manually specify the location
128+
which to generate the Tyrus HTML file, which will
129+
remain there after execution is finished.
120130
"""
121131
self.model = model
122132
self.inputs = one_input_convert(inputs)
@@ -131,7 +141,17 @@ def __init__(
131141
if self.notebook:
132142
output_notebook()
133143
else:
134-
output_file(filename="trustyai_dashboard.html", title="TrustyAI Dashboard")
144+
if kwargs.get("filepath") is None:
145+
self.tmp_dir = (
146+
tempfile.TemporaryDirectory() # pylint: disable=consider-using-with
147+
)
148+
self.filepath = self.tmp_dir.name
149+
else:
150+
self.filepath = kwargs["filepath"]
151+
output_file(
152+
filename=os.path.join(self.filepath, "trustyai_dashboard.html"),
153+
title="TrustyAI Dashboard",
154+
)
135155

136156
self.cfdict = None
137157
self.shap_saliencies = None

tests/general/test_tyrus.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def predict_function(x):
2929
model,
3030
data.iloc[100],
3131
predictions.iloc[100],
32-
background=data.iloc[:100]
32+
background=data.iloc[:100],
33+
filepath=os.getcwd()
3334
)
3435

3536
# launch dashboard
@@ -66,7 +67,4 @@ def predict_function(x):
6667
tyrus.run()
6768

6869
# see if dashboard html exists
69-
assert "trustyai_dashboard.html" in os.listdir()
70-
71-
# cleanup
72-
os.remove("trustyai_dashboard.html")
70+
assert "trustyai_dashboard.html" in os.listdir(tyrus.filepath)

0 commit comments

Comments
 (0)