Skip to content

Commit

Permalink
v1.0.10 in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
szczepanskiNicolas committed Nov 28, 2023
1 parent 438f0ca commit 4f3b8b4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
1 change: 0 additions & 1 deletion pyxai/examples/RF/theories-types-dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@
print("contrastive:", contrastive)
print("features contrastive:", features)

explainer.show()
20 changes: 12 additions & 8 deletions pyxai/sources/core/explainer/Explainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ def get_PILImage(self, instance, reason, image=None, time_series=None, contrasti
image = pyplot_image_generator.generate_explanation(instance, self.to_features(reason, details=True, contrastive=contrastive), pil_image=True)
return [instance_image, image]
else:

from pyxai.sources.core.tools.vizualisation import PyPlotDiagramGenerator
pyplot_image_generator = PyPlotDiagramGenerator(image, time_series=time_series)
pyplot_image_generator = PyPlotDiagramGenerator(time_series=time_series)

feature_values = dict()
for i, value in enumerate(instance):
feature_values[self.feature_names[i]] = value
feature_values[feature_names[i]] = value

image = pyplot_image_generator.generate_explanation(feature_values, instance, self.to_features(reason, details=True, contrastive=contrastive), pil_image=True)
return [image]

def save_png(self, file, instance, reason, image=None, time_series=None, contrastive=False):
def save_png(self, file, instance, reason, image=None, time_series=None, contrastive=False, width=250):
PILImage_list = self.get_PILImage(instance, reason, image, time_series, contrastive)
for i, image in enumerate(PILImage_list):
PILImage_list[i] = self.resize_PILimage(PILImage_list[i], width)

if len(PILImage_list) == 1:
PILImage_list[0].save(file)
else:
Expand All @@ -78,10 +80,12 @@ def show_in_notebook(self, instance, reason, image=None, time_series=None, contr
else:
display(*PILImage_list)


def show_on_screen(self, instance, reason, image=None, time_series=None, contrastive=False):
PILImage = self.get_PILImage(instance, reason, image, time_series, contrastive)
PILImage.show()
def show_on_screen(self, instance, reason, image=None, time_series=None, contrastive=False, width=250):
PILImage_list = self.get_PILImage(instance, reason, image, time_series, contrastive)
for i, image in enumerate(PILImage_list):
PILImage_list[i] = self.resize_PILimage(PILImage_list[i], width)
for image in PILImage_list:
image.show()

def open_GUI(self, image=None, time_series=None):
feature_names = self.get_feature_names()
Expand Down
1 change: 1 addition & 0 deletions pyxai/sources/core/tools/GUIQT.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, explainer, image=None, feature_names=None, time_series=None):
self.explainer = explainer
self.image = image
self.time_series = time_series
self.pressed = False
if feature_names is not None:
self.feature_names = feature_names
elif explainer is not None:
Expand Down
15 changes: 7 additions & 8 deletions pyxai/sources/core/tools/vizualisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def generate_explanation(self, feature_values, instance, reason, pil_image=False
mpl.rcParams['axes.spines.bottom'] = True
dict_features = copy.deepcopy(reason)

print("self.time_series:", self.time_series)
n_subplots_time_series = 0
_time_series = copy.deepcopy(self.time_series)
if _time_series is not None:
Expand All @@ -52,12 +51,13 @@ def generate_explanation(self, feature_values, instance, reason, pil_image=False

n_subplots = len(dict_features.keys()) + n_subplots_time_series
ratio_subplots = [4]*n_subplots_time_series+[1]*len(dict_features.keys())
if len(dict_features.keys()) == 1:

if n_subplots == 1:
fig, axes = pyplot.subplots(n_subplots, figsize=(6,1))
axes = [axes] # To solve a bug when axes is not a list.
else:
fig, axes = pyplot.subplots(n_subplots, figsize=(6,n_subplots+3), gridspec_kw={'height_ratios': ratio_subplots})
print(feature_values)

if _time_series is not None:
# time_series graphs

Expand Down Expand Up @@ -87,8 +87,8 @@ def generate_explanation(self, feature_values, instance, reason, pil_image=False
if theory is not None and (theory[0] == "binary" or theory[0] == "categorical"):
raise ValueError("The feature of time series must be None or Numerical.")

print("string_view:", string_view)
print("feature_name:", feature_name)
#print("string_view:", string_view)
#print("feature_name:", feature_name)
if "in [" in string_view or "in ]" in string_view:
feature_str, interval = string_view.split("in")
feature_str = feature_str.lstrip().rstrip()
Expand Down Expand Up @@ -126,10 +126,9 @@ def generate_explanation(self, feature_values, instance, reason, pil_image=False
explanation_max_values.append(threshold_max)


print("instance_values:", instance_values)
#print("instance_values:", instance_values)

midle = (0+len(instance_values)-1)/2
print("here:", midle)
to_min = [x for x in instance_values if x != "inf"] + [x for x in explanation_min_values if x != "inf"] + [x for x in explanation_max_values if x != "inf"]
to_max = [x for x in instance_values if x != "inf"] + [x for x in explanation_min_values if x != "inf"] + [x for x in explanation_max_values if x != "inf"]
min_y = numpy.min(to_min)
Expand Down Expand Up @@ -429,7 +428,7 @@ def generate_explanation(self, instance, reason, pil_image=False):
fusion = PILImage.blend(new_image_negative, new_image_positive, 0.5)
if instance is not None:
array_image = self.instance_to_numpy(instance)
x_3 = pyplot.imshow(numpy.uint8(array_image), alpha=0.6, vmin=0, vmax=255)
x_3 = pyplot.imshow(numpy.uint8(array_image), alpha=0.2, cmap='Greys', vmin=0, vmax=255)
new_image_x_3 = x_3.make_image(pyplot.gcf().canvas.get_renderer(), unsampled=True)
new_image_x_3 = PILImage.fromarray(new_image_x_3[0])
fusion = PILImage.blend(fusion, new_image_x_3, 0.4)
Expand Down

0 comments on commit 4f3b8b4

Please sign in to comment.