sinapsis-data-visualization 0.1.12__tar.gz → 0.1.13__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sinapsis_data_visualization-0.1.12/src/sinapsis_data_visualization.egg-info → sinapsis_data_visualization-0.1.13}/PKG-INFO +1 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/pyproject.toml +1 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/base_annotation_drawer.py +1 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13/src/sinapsis_data_visualization.egg-info}/PKG-INFO +1 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization.egg-info/SOURCES.txt +0 -2
- sinapsis_data_visualization-0.1.12/src/sinapsis_data_visualization/templates/data_distribution_visualization_template.py +0 -248
- sinapsis_data_visualization-0.1.12/src/sinapsis_data_visualization/templates/prediction_visualization.py +0 -213
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/LICENSE +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/README.md +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/setup.cfg +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/helpers/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/helpers/annotation_drawer_tools.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/helpers/annotation_drawer_types.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/helpers/color_utils.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/helpers/detection_utils.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/helpers/plot_distributions.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/helpers/scikit_pca_analysis.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/helpers/tags.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/base_visualization_template.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/bbox_drawer.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/data_distribution_visualization.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/key_points_drawer.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/label_drawer.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/oriented_bbox_drawer.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/segmentation_mask_drawer.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization/templates/tabular_data_visualization.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization.egg-info/dependency_links.txt +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization.egg-info/requires.txt +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.13}/src/sinapsis_data_visualization.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sinapsis-data-visualization
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.13
|
|
4
4
|
Summary: Templates to perform visualization of data distributions and image annotations.
|
|
5
5
|
Author-email: SinapsisAI <dev@sinapsis.tech>
|
|
6
6
|
Project-URL: Homepage, https://sinapsis.tech
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sinapsis-data-visualization
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.13
|
|
4
4
|
Summary: Templates to perform visualization of data distributions and image annotations.
|
|
5
5
|
Author-email: SinapsisAI <dev@sinapsis.tech>
|
|
6
6
|
Project-URL: Homepage, https://sinapsis.tech
|
|
@@ -21,10 +21,8 @@ src/sinapsis_data_visualization/templates/base_annotation_drawer.py
|
|
|
21
21
|
src/sinapsis_data_visualization/templates/base_visualization_template.py
|
|
22
22
|
src/sinapsis_data_visualization/templates/bbox_drawer.py
|
|
23
23
|
src/sinapsis_data_visualization/templates/data_distribution_visualization.py
|
|
24
|
-
src/sinapsis_data_visualization/templates/data_distribution_visualization_template.py
|
|
25
24
|
src/sinapsis_data_visualization/templates/key_points_drawer.py
|
|
26
25
|
src/sinapsis_data_visualization/templates/label_drawer.py
|
|
27
26
|
src/sinapsis_data_visualization/templates/oriented_bbox_drawer.py
|
|
28
|
-
src/sinapsis_data_visualization/templates/prediction_visualization.py
|
|
29
27
|
src/sinapsis_data_visualization/templates/segmentation_mask_drawer.py
|
|
30
28
|
src/sinapsis_data_visualization/templates/tabular_data_visualization.py
|
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""Data distribution visualization template"""
|
|
3
|
-
|
|
4
|
-
import os
|
|
5
|
-
from typing import Any
|
|
6
|
-
|
|
7
|
-
import matplotlib
|
|
8
|
-
import matplotlib.pyplot as plt
|
|
9
|
-
import numpy as np
|
|
10
|
-
from pydantic import Field
|
|
11
|
-
from pydantic.dataclasses import dataclass
|
|
12
|
-
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
13
|
-
from sinapsis_core.template_base import Template
|
|
14
|
-
from sinapsis_core.template_base.base_models import (
|
|
15
|
-
OutputTypes,
|
|
16
|
-
TemplateAttributes,
|
|
17
|
-
TemplateAttributeType,
|
|
18
|
-
UIPropertiesMetadata,
|
|
19
|
-
)
|
|
20
|
-
from sinapsis_core.utils.env_var_keys import SINAPSIS_CACHE_DIR
|
|
21
|
-
from sinapsis_data_visualization.helpers.plot_distributions import (
|
|
22
|
-
plot_distribution,
|
|
23
|
-
plot_map,
|
|
24
|
-
)
|
|
25
|
-
from sinapsis_data_visualization.helpers.scikit_pca_analysis import (
|
|
26
|
-
perform_k_means_analysis,
|
|
27
|
-
pre_process_images,
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
class PlotAttributes(TemplateAttributes):
|
|
32
|
-
"""Base attributes for all plot types.
|
|
33
|
-
|
|
34
|
-
Attributes:
|
|
35
|
-
fig_title (str|None): Title for the figure.
|
|
36
|
-
y_label (str|None): Label of the y-axis.
|
|
37
|
-
x_label (str|None): Label of the x-axis.
|
|
38
|
-
fig_width (int): Width of the figure.
|
|
39
|
-
fig_height (int): Height of the figure.
|
|
40
|
-
x_position (float): X-position for the legend and/or texts.
|
|
41
|
-
y_position (float): Y-position for the legend and/or texts.
|
|
42
|
-
save_image_dir (str): Directory to save the image.
|
|
43
|
-
fig_name (str): Name of the figure.
|
|
44
|
-
"""
|
|
45
|
-
|
|
46
|
-
fig_title: str | None = None
|
|
47
|
-
y_label: str | None = None
|
|
48
|
-
x_label: str | None = None
|
|
49
|
-
fig_width: int = 10
|
|
50
|
-
fig_height: int = 10
|
|
51
|
-
x_position: float = 0.5
|
|
52
|
-
y_position: float = 0.5
|
|
53
|
-
text: str | None = None
|
|
54
|
-
save_image_dir: str | None = SINAPSIS_CACHE_DIR
|
|
55
|
-
fig_name: str
|
|
56
|
-
kwargs: dict[str, Any] = Field(default_factory=dict)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
@dataclass
|
|
60
|
-
class PlotTypes:
|
|
61
|
-
"""Dataclass with different types of plots.
|
|
62
|
-
HISTOGRAM: Key for the histogram plot
|
|
63
|
-
BOX_PLOT: Key for the box plot
|
|
64
|
-
PIE_CHART: Key for the pie chart plot
|
|
65
|
-
CLUSTERING: Key for the k-means custering plot
|
|
66
|
-
"""
|
|
67
|
-
|
|
68
|
-
HISTOGRAM: str = "histogram"
|
|
69
|
-
BOX_PLOT: str = "box_plot"
|
|
70
|
-
PIE_CHART: str = "pie_chart"
|
|
71
|
-
CLUSTERING: str = "k_means_clustering"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
class DataDistributionVisualization(Template):
|
|
75
|
-
"""
|
|
76
|
-
This template plots the distribution for image data, and can be
|
|
77
|
-
extended to plot the distribution of different Packets, provided
|
|
78
|
-
they have labels.
|
|
79
|
-
The template allows for the drawing of histograms, box plots, pie charts
|
|
80
|
-
and PCA from k-means cluster
|
|
81
|
-
The template returns a figure in the container as an ImagePacket.
|
|
82
|
-
|
|
83
|
-
Usage example:
|
|
84
|
-
|
|
85
|
-
agent:
|
|
86
|
-
name: my_test_agent
|
|
87
|
-
templates:
|
|
88
|
-
- template_name: InputTemplate
|
|
89
|
-
class_name: InputTemplate
|
|
90
|
-
attributes: {}
|
|
91
|
-
- template_name: DataDistributionVisualization
|
|
92
|
-
class_name: DataDistributionVisualization
|
|
93
|
-
template_input: InputTemplate
|
|
94
|
-
attributes:
|
|
95
|
-
fig_title: null
|
|
96
|
-
y_label: null
|
|
97
|
-
x_label: null
|
|
98
|
-
fig_width: 10
|
|
99
|
-
fig_height: 10
|
|
100
|
-
x_position: 0.5
|
|
101
|
-
y_position: 0.5
|
|
102
|
-
text: null
|
|
103
|
-
save_image_dir: $SINAPSIS_CACHE_DIR
|
|
104
|
-
fig_name: 'desired_name_for_the_figure'
|
|
105
|
-
kwargs: '{}'
|
|
106
|
-
histogram: false
|
|
107
|
-
box_plot: false
|
|
108
|
-
pie_chart: false
|
|
109
|
-
k_means_clustering: false
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
"""
|
|
113
|
-
|
|
114
|
-
UIProperties = UIPropertiesMetadata(output_type=OutputTypes.IMAGE)
|
|
115
|
-
|
|
116
|
-
class AttributesBaseModel(PlotAttributes):
|
|
117
|
-
"""
|
|
118
|
-
Attributes for data analysis using matplotlib.
|
|
119
|
-
|
|
120
|
-
Attributes:
|
|
121
|
-
histogram (bool): Whether to plot a histogram. Defaults to False.
|
|
122
|
-
box_plot (bool): Whether to plot a box plot. Defaults to False.
|
|
123
|
-
pie_chart (bool): Whether to plot a pie chart. Defaults to False.
|
|
124
|
-
k_means_clustering (bool): Whether to plot k-means clustering. Defaults to False.
|
|
125
|
-
"""
|
|
126
|
-
|
|
127
|
-
histogram: bool = False
|
|
128
|
-
box_plot: bool = False
|
|
129
|
-
pie_chart: bool = False
|
|
130
|
-
k_means_clustering: bool = False
|
|
131
|
-
|
|
132
|
-
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
133
|
-
"""Initializes the DataDistributionVisualization
|
|
134
|
-
template with given attributes.
|
|
135
|
-
|
|
136
|
-
Args:
|
|
137
|
-
attributes (TemplateAttributeType): The attributes for
|
|
138
|
-
configuring the data analysis.
|
|
139
|
-
"""
|
|
140
|
-
super().__init__(attributes)
|
|
141
|
-
|
|
142
|
-
if not os.path.exists(self.attributes.save_image_dir):
|
|
143
|
-
os.makedirs(self.attributes.save_image_dir)
|
|
144
|
-
|
|
145
|
-
def create_figure(self, text: str | None = None, rotate_x_ticks: bool = False) -> matplotlib.figure.Figure:
|
|
146
|
-
"""
|
|
147
|
-
Creates the matplotlib.figure.Figure object and adds relevant
|
|
148
|
-
information, such as the x-label, y-label and title.
|
|
149
|
-
It also adds available text to the desired x and y positions and rotated
|
|
150
|
-
the x-ticks is flag is True
|
|
151
|
-
|
|
152
|
-
Args:
|
|
153
|
-
text (str | None): Optional text to add to the figure. Defaults to None.
|
|
154
|
-
rotate_x_ticks (bool): Flag to rotate x-ticks 90 degrees. Defaults to False
|
|
155
|
-
Returns:
|
|
156
|
-
The initialized figure with corresponding parameters
|
|
157
|
-
|
|
158
|
-
"""
|
|
159
|
-
figure = plt.figure(figsize=(self.attributes.fig_width, self.attributes.fig_height))
|
|
160
|
-
plt.xlabel(self.attributes.x_label or "X-Axis")
|
|
161
|
-
plt.ylabel(self.attributes.y_label or "Y-Axis")
|
|
162
|
-
plt.title(self.attributes.fig_title or "Plot Title")
|
|
163
|
-
|
|
164
|
-
# Prepare the text for annotation
|
|
165
|
-
if text:
|
|
166
|
-
plt.text(self.attributes.x_position, self.attributes.y_position, text)
|
|
167
|
-
|
|
168
|
-
if rotate_x_ticks:
|
|
169
|
-
plt.xticks(rotation=90)
|
|
170
|
-
return figure
|
|
171
|
-
|
|
172
|
-
@staticmethod
|
|
173
|
-
def retrieve_labels_from_images(
|
|
174
|
-
container: DataContainer,
|
|
175
|
-
) -> tuple[list[str | int], list[int]]:
|
|
176
|
-
"""
|
|
177
|
-
Iterates through the ImagePackets, extracting each of the
|
|
178
|
-
annotations for all of them.
|
|
179
|
-
Generates a dictionary with the label and number of points associated
|
|
180
|
-
with that label.
|
|
181
|
-
Returns:
|
|
182
|
-
tuple[list[str | int], list[int]]: list of labels and number of coincidences for each label
|
|
183
|
-
"""
|
|
184
|
-
labels: list[str | int] = [ann.label_str for image in container.images for ann in image.annotations]
|
|
185
|
-
|
|
186
|
-
label_count: dict[str | int, int] = {}
|
|
187
|
-
for label in labels:
|
|
188
|
-
label_count[label] = label_count.get(label, 0) + 1
|
|
189
|
-
labels, counts = list(label_count.keys()), list(label_count.values())
|
|
190
|
-
return labels, counts
|
|
191
|
-
|
|
192
|
-
@staticmethod
|
|
193
|
-
def process_cluster(container: DataContainer) -> tuple[list[int | str], np.ndarray]:
|
|
194
|
-
"""
|
|
195
|
-
For the k-means clustering, the images need to be flattened first
|
|
196
|
-
and then returned as a reduced vector. This method calls the pre_process_images
|
|
197
|
-
method from scikit-pca analysis to perform such preprocessing.
|
|
198
|
-
Then, perform_k_means_analysis instantiates a KMeans class from scikit-learn
|
|
199
|
-
and perform Principal Component Analysis (PCA) on the reduced vector.
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
Args:
|
|
203
|
-
container (DataContainer): Container to extract the images from
|
|
204
|
-
|
|
205
|
-
Returns:
|
|
206
|
-
list[str | int]: list of labels
|
|
207
|
-
np.ndarray : transformed values from the pca analysis
|
|
208
|
-
"""
|
|
209
|
-
images = container.images
|
|
210
|
-
feature_arr = pre_process_images(images)
|
|
211
|
-
label, counts = perform_k_means_analysis(feature_arr)
|
|
212
|
-
return label, counts
|
|
213
|
-
|
|
214
|
-
def execute(self, container: DataContainer) -> DataContainer:
|
|
215
|
-
figure = self.create_figure()
|
|
216
|
-
labels_to_plot, counts_to_plot = self.retrieve_labels_from_images(container)
|
|
217
|
-
fig_name = self.attributes.fig_name
|
|
218
|
-
|
|
219
|
-
if self.attributes.k_means_clustering:
|
|
220
|
-
plot_type = PlotTypes.CLUSTERING
|
|
221
|
-
fig_name = f"{PlotTypes.CLUSTERING}_{self.attributes.fig_name}"
|
|
222
|
-
labels, feature_vec = self.process_cluster(container)
|
|
223
|
-
figure = plot_distribution(
|
|
224
|
-
figure=figure,
|
|
225
|
-
labels=labels,
|
|
226
|
-
counts=feature_vec,
|
|
227
|
-
plot_type=plot_type,
|
|
228
|
-
kwargs=self.attributes.kwargs,
|
|
229
|
-
)
|
|
230
|
-
else:
|
|
231
|
-
for plot_type in plot_map:
|
|
232
|
-
if not getattr(self.attributes, plot_type):
|
|
233
|
-
continue
|
|
234
|
-
figure = plot_distribution(
|
|
235
|
-
figure=figure,
|
|
236
|
-
labels=labels_to_plot,
|
|
237
|
-
counts=counts_to_plot,
|
|
238
|
-
plot_type=plot_type,
|
|
239
|
-
kwargs=self.attributes.kwargs,
|
|
240
|
-
)
|
|
241
|
-
|
|
242
|
-
fig_name = f"{plot_type}_{self.attributes.fig_name}"
|
|
243
|
-
fig_path = os.path.join(self.attributes.save_image_dir, fig_name)
|
|
244
|
-
if figure:
|
|
245
|
-
plt.savefig(f"{fig_path}_{container.container_id}")
|
|
246
|
-
plt.close()
|
|
247
|
-
container.generic_data[plot_type] = figure
|
|
248
|
-
return container
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
import numpy as np
|
|
3
|
-
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
4
|
-
from sinapsis_data_visualization.helpers.tags import Tags
|
|
5
|
-
from sinapsis_data_visualization.templates.base_visualization_template import (
|
|
6
|
-
BasePlotAttributes,
|
|
7
|
-
BaseVisualizationTemplate,
|
|
8
|
-
PlotTypes,
|
|
9
|
-
)
|
|
10
|
-
from sklearn.metrics import auc, confusion_matrix, precision_recall_curve, roc_curve
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class PredictionPlotTypes(PlotTypes):
|
|
14
|
-
"""Extends plot types with model prediction visualization types"""
|
|
15
|
-
|
|
16
|
-
CONFUSION_MATRIX = "confusion_matrix"
|
|
17
|
-
ROC_CURVE = "roc_curve"
|
|
18
|
-
PRECISION_RECALL_CURVE = "precision_recall_curve"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class PredictionVisualizationAttributes(BasePlotAttributes):
|
|
22
|
-
"""
|
|
23
|
-
Attributes for prediction visualization.
|
|
24
|
-
|
|
25
|
-
Attributes:
|
|
26
|
-
generic_field_key (str): Key to access prediction results in generic_data (inherited from BasePlotAttributes).
|
|
27
|
-
generic_training_key (str): Key to access training data in generic_data.
|
|
28
|
-
target_column (str): Name of the target column if needed.
|
|
29
|
-
confusion_matrix (bool): Whether to create a confusion matrix.
|
|
30
|
-
roc_curve (bool): Whether to create a ROC curve.
|
|
31
|
-
precision_recall_curve (bool): Whether to create a precision-recall curve.
|
|
32
|
-
"""
|
|
33
|
-
|
|
34
|
-
generic_training_key: str = "fetch_openmlWrapper_viz"
|
|
35
|
-
target_column: str = "target"
|
|
36
|
-
confusion_matrix: bool = True
|
|
37
|
-
roc_curve: bool = True
|
|
38
|
-
precision_recall_curve: bool = True
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
PredictionVisualizationUIProperties = BaseVisualizationTemplate.UIProperties
|
|
42
|
-
PredictionVisualizationUIProperties.tags.extend([Tags.CONFUSION, Tags.CURVES, Tags.METRICS])
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
class PredictionVisualization(BaseVisualizationTemplate):
|
|
46
|
-
"""
|
|
47
|
-
Template for visualizing model predictions with evaluation metrics.
|
|
48
|
-
|
|
49
|
-
This template supports confusion matrices, ROC curves, and precision-recall curves
|
|
50
|
-
for classification models.
|
|
51
|
-
"""
|
|
52
|
-
|
|
53
|
-
AttributesBaseModel = PredictionVisualizationAttributes
|
|
54
|
-
UIProperties = PredictionVisualizationUIProperties
|
|
55
|
-
|
|
56
|
-
def get_data_for_visualization(self, container: DataContainer) -> tuple[object, object]:
|
|
57
|
-
"""
|
|
58
|
-
Extracts prediction and training data from the container.
|
|
59
|
-
|
|
60
|
-
Args:
|
|
61
|
-
container (DataContainer): Data container.
|
|
62
|
-
|
|
63
|
-
Returns:
|
|
64
|
-
tuple[object, object]: Tuple containing prediction results and training data.
|
|
65
|
-
"""
|
|
66
|
-
predictions_data = self._get_generic_data(container, self.attributes.generic_field_key)
|
|
67
|
-
|
|
68
|
-
if not predictions_data:
|
|
69
|
-
self.logger.warning(f"No prediction data found with key '{self.attributes.generic_field_key}'")
|
|
70
|
-
return (None, None)
|
|
71
|
-
|
|
72
|
-
training_data = self._get_generic_data(container, self.attributes.generic_training_key)
|
|
73
|
-
|
|
74
|
-
return predictions_data, training_data
|
|
75
|
-
|
|
76
|
-
def _extract_true_and_predicted_values(self, data: tuple[object, object]) -> tuple[np.ndarray, np.ndarray]:
|
|
77
|
-
"""
|
|
78
|
-
Extracts true and predicted values from data in a flexible way.
|
|
79
|
-
|
|
80
|
-
Args:
|
|
81
|
-
data (tuple[object, object]): Tuple containing prediction results and training data.
|
|
82
|
-
|
|
83
|
-
Returns:
|
|
84
|
-
tuple[np.ndarray, np.ndarray]: True values and predicted values.
|
|
85
|
-
"""
|
|
86
|
-
predictions, training_data = data
|
|
87
|
-
y_true = None
|
|
88
|
-
y_pred = None
|
|
89
|
-
|
|
90
|
-
if hasattr(predictions, "predictions"):
|
|
91
|
-
y_pred = predictions.predictions
|
|
92
|
-
|
|
93
|
-
if training_data is not None and hasattr(training_data, "y_test"):
|
|
94
|
-
y_test = training_data.y_test
|
|
95
|
-
y_true = y_test.values.flatten() if hasattr(y_test, "values") else np.array(y_test)
|
|
96
|
-
|
|
97
|
-
return y_true, y_pred
|
|
98
|
-
|
|
99
|
-
def prepare_confusion_matrix_data(self, data: tuple[object, object]) -> tuple[list[str], np.ndarray]:
|
|
100
|
-
"""
|
|
101
|
-
Prepares data for a confusion matrix plot.
|
|
102
|
-
|
|
103
|
-
Args:
|
|
104
|
-
data (tuple[object, object]): Tuple containing prediction results and training data.
|
|
105
|
-
|
|
106
|
-
Returns:
|
|
107
|
-
tuple[list[str], np.ndarray]: Class labels and confusion matrix array.
|
|
108
|
-
"""
|
|
109
|
-
y_true, y_pred = self._extract_true_and_predicted_values(data)
|
|
110
|
-
|
|
111
|
-
if y_true is None or y_pred is None:
|
|
112
|
-
self.logger.warning("Could not extract true and predicted values for confusion matrix")
|
|
113
|
-
return [], np.array([])
|
|
114
|
-
|
|
115
|
-
class_labels = sorted(set(np.concatenate([np.unique(y_true), np.unique(y_pred)])))
|
|
116
|
-
class_labels = [str(label) for label in class_labels]
|
|
117
|
-
|
|
118
|
-
cm = confusion_matrix(y_true, y_pred)
|
|
119
|
-
|
|
120
|
-
return class_labels, cm
|
|
121
|
-
|
|
122
|
-
def prepare_roc_curve_data(self, data: tuple[object, object]) -> tuple[list[str], np.ndarray]:
|
|
123
|
-
"""
|
|
124
|
-
Prepares data for a ROC curve plot.
|
|
125
|
-
|
|
126
|
-
Args:
|
|
127
|
-
data (tuple[object, object]): Tuple containing prediction results and training data.
|
|
128
|
-
|
|
129
|
-
Returns:
|
|
130
|
-
tuple[list[str], np.ndarray]: Curve label and FPR/TPR values.
|
|
131
|
-
"""
|
|
132
|
-
y_true, y_pred = self._extract_true_and_predicted_values(data)
|
|
133
|
-
|
|
134
|
-
if y_true is None or y_pred is None:
|
|
135
|
-
self.logger.warning("Could not extract true and predicted values for ROC curve")
|
|
136
|
-
return [], np.array([])
|
|
137
|
-
|
|
138
|
-
fpr, tpr, _ = roc_curve(y_true, y_pred)
|
|
139
|
-
roc_auc = auc(fpr, tpr)
|
|
140
|
-
|
|
141
|
-
curve_label = [f"ROC Curve (AUC = {roc_auc:.3f})"]
|
|
142
|
-
|
|
143
|
-
curve_data = np.column_stack((fpr, tpr))
|
|
144
|
-
|
|
145
|
-
return curve_label, curve_data
|
|
146
|
-
|
|
147
|
-
def prepare_precision_recall_data(self, data: tuple[object, object]) -> tuple[list[str], np.ndarray]:
|
|
148
|
-
"""
|
|
149
|
-
Prepares data f or a precision-recall curve plot.
|
|
150
|
-
|
|
151
|
-
Args:
|
|
152
|
-
data (tuple[object, object]): Tuple containing prediction results and training data.
|
|
153
|
-
|
|
154
|
-
Returns:
|
|
155
|
-
tuple[list[str], np.ndarray]: Curve label and precision/recall values.
|
|
156
|
-
"""
|
|
157
|
-
y_true, y_pred = self._extract_true_and_predicted_values(data)
|
|
158
|
-
|
|
159
|
-
if y_true is None or y_pred is None:
|
|
160
|
-
self.logger.warning("Could not extract true and predicted values for precision-recall curve")
|
|
161
|
-
return [], np.array([])
|
|
162
|
-
|
|
163
|
-
precision, recall, _ = precision_recall_curve(y_true, y_pred)
|
|
164
|
-
|
|
165
|
-
ap = np.mean(precision)
|
|
166
|
-
|
|
167
|
-
curve_label = [f"Precision-Recall (AP = {ap:.3f})"]
|
|
168
|
-
|
|
169
|
-
curve_data = np.column_stack((precision, recall))
|
|
170
|
-
|
|
171
|
-
return curve_label, curve_data
|
|
172
|
-
|
|
173
|
-
def generate_visualizations(self, container: DataContainer, data: tuple[object, object]) -> None:
|
|
174
|
-
"""
|
|
175
|
-
Generates all requested visualizations for model predictions.
|
|
176
|
-
|
|
177
|
-
Args:
|
|
178
|
-
container (DataContainer): Data container.
|
|
179
|
-
data (tuple[object, object]): Tuple containing prediction results and training data.
|
|
180
|
-
"""
|
|
181
|
-
if not data or len(data) != 2 or not hasattr(data[0], "predictions"):
|
|
182
|
-
self.logger.warning("No prediction data available for visualization")
|
|
183
|
-
return
|
|
184
|
-
|
|
185
|
-
if self.attributes.confusion_matrix:
|
|
186
|
-
labels, cm_data = self.prepare_confusion_matrix_data(data)
|
|
187
|
-
if len(labels) > 0 and cm_data.size > 0:
|
|
188
|
-
self.plot_and_save(
|
|
189
|
-
container=container,
|
|
190
|
-
plot_type=PredictionPlotTypes.CONFUSION_MATRIX,
|
|
191
|
-
labels=labels,
|
|
192
|
-
counts=cm_data,
|
|
193
|
-
)
|
|
194
|
-
|
|
195
|
-
if self.attributes.roc_curve:
|
|
196
|
-
labels, roc_data = self.prepare_roc_curve_data(data)
|
|
197
|
-
if len(labels) > 0 and roc_data.size > 0:
|
|
198
|
-
self.plot_and_save(
|
|
199
|
-
container=container,
|
|
200
|
-
plot_type=PredictionPlotTypes.ROC_CURVE,
|
|
201
|
-
labels=labels,
|
|
202
|
-
counts=roc_data,
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
if self.attributes.precision_recall_curve:
|
|
206
|
-
labels, pr_data = self.prepare_precision_recall_data(data)
|
|
207
|
-
if len(labels) > 0 and pr_data.size > 0:
|
|
208
|
-
self.plot_and_save(
|
|
209
|
-
container=container,
|
|
210
|
-
plot_type=PredictionPlotTypes.PRECISION_RECALL_CURVE,
|
|
211
|
-
labels=labels,
|
|
212
|
-
counts=pr_data,
|
|
213
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|