Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

738 text lime in dashboard returns an error #799

Merged
merged 16 commits into from
Jun 18, 2024
Merged
12 changes: 5 additions & 7 deletions dianna/dashboard/_models_image.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import tempfile
import streamlit as st
from _model_utils import fill_segmentation
from _model_utils import preprocess_function
from onnx_tf.backend import prepare
from dianna import explain_image
Expand Down Expand Up @@ -42,12 +41,11 @@ def _run_kernelshap_image(model, image, i, **kwargs):
with tempfile.NamedTemporaryFile() as f:
f.write(model)
f.flush()
shap_values, segments_slic = explain_image(f.name,
image,
method='KernelSHAP',
**kwargs)

return fill_segmentation(shap_values[i][0], segments_slic)
relevances = explain_image(f.name,
image,
method='KernelSHAP',
**kwargs)
return relevances[0]


explain_image_dispatcher = {
Expand Down
2 changes: 1 addition & 1 deletion dianna/dashboard/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _get_params(method: str):

elif method == 'LIME':
return {
'rand_state': st.number_input('Random state', value=2),
'random_state': st.number_input('Random state', value=2),
}

else:
Expand Down
2 changes: 2 additions & 0 deletions dianna/methods/lime_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(
verbose=False,
preprocess_function=None,
feature_selection='auto',
random_state = None
):
"""Initializes Lime explainer for timeseries.

Expand All @@ -32,6 +33,7 @@ def __init__(
feature_selection (str): Feature selection method to be used by explainer.
preprocess_function (callable, optional): Function to preprocess the time series data before passing it
to the explainer. Defaults to None.
random_state (int or np.RandomState, optional): seed or random state. Unused variable for current ts method
"""

def kernel(d):
Expand Down
35 changes: 26 additions & 9 deletions tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,24 @@ def test_text_page(page: Page):
expect(page.get_by_text('Select a method to continue')).to_be_visible()

page.locator('label').filter(has_text='RISE').locator('span').click()
page.locator('label').filter(has_text='LIME').locator('span').click()

page.get_by_text('Running...').wait_for(state='detached', timeout=45_000)
page.get_by_text('Running...').wait_for(state='detached', timeout=100_000)

for selector in (
page.get_by_role('heading', name='RISE').get_by_text('RISE'),
# first image
page.get_by_role('heading', name='LIME').get_by_text('LIME'),
# Images for positive (RISE/LIME)
page.get_by_role('heading',
name='positive').get_by_text('positive'),
page.get_by_role('img', name='0').first,
# second image
page.get_by_role('img', name='0').nth(1),

# Images for negative (RISE/LIME)
page.get_by_role('heading',
name='negative').get_by_text('negative'),
page.get_by_role('img', name='0').nth(1),
page.get_by_role('img', name='0').nth(2),
page.get_by_role('img', name='0').nth(3),
):
print(selector)
expect(selector).to_be_visible()
Expand All @@ -131,27 +136,35 @@ def test_image_page(page: Page):

expect(
page.get_by_text('Add your input data in the left panel to continue')
).to_be_visible(timeout=30_000)
).to_be_visible(timeout=100_000)

page.locator('label').filter(
has_text='Load example data').locator('span').click()

expect(page.get_by_text('Select a method to continue')).to_be_visible()

page.locator('label').filter(has_text='RISE').locator('span').click()
page.locator('label').filter(has_text='KernelSHAP').locator('span').click()
page.locator('label').filter(has_text='LIME').locator('span').click()

page.get_by_text('Running...').wait_for(state='detached', timeout=45_000)

for selector in (
page.get_by_role('heading', name='RISE').get_by_text('RISE'),
page.get_by_role('heading', name='KernelSHAP').get_by_text('KernelSHAP'),
page.get_by_role('heading', name='LIME').get_by_text('LIME'),
# first image
page.get_by_role('heading', name='0').get_by_text('0'),
page.get_by_role('img', name='0').first,
page.get_by_role('img', name='0').nth(1),
page.get_by_role('img', name='0').nth(2),
# second image
page.get_by_role('heading', name='1').get_by_text('1'),
page.get_by_role('img', name='0').nth(1),
page.get_by_role('img', name='0').nth(3),
page.get_by_role('img', name='0').nth(4),
page.get_by_role('img', name='0').nth(5),
):
expect(selector).to_be_visible(timeout=45_000)
expect(selector).to_be_visible(timeout=100_000)


def test_timeseries_page(page: Page):
Expand All @@ -171,17 +184,21 @@ def test_timeseries_page(page: Page):

expect(page.get_by_text('Select a method to continue')).to_be_visible()

page.locator('label').filter(has_text='LIME').locator('span').click()
page.locator('label').filter(has_text='RISE').locator('span').click()

page.get_by_text('Running...').wait_for(state='detached', timeout=45_000)
page.get_by_text('Running...').wait_for(state='detached', timeout=100_000)

for selector in (
page.get_by_role('heading', name='LIME').get_by_text('LIME'),
page.get_by_role('heading', name='RISE').get_by_text('RISE'),
# first image
page.get_by_role('heading', name='winter').get_by_text('winter'),
page.get_by_role('img', name='0').first,
page.get_by_role('img', name='0').nth(1),
# second image
page.get_by_role('heading', name='summer').get_by_text('summer'),
page.get_by_role('img', name='0').nth(1),
page.get_by_role('img', name='0').nth(2),
page.get_by_role('img', name='0').nth(3),
):
expect(selector).to_be_visible()
Loading