sinapsis-data-visualization 0.1.12__tar.gz → 0.1.14__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.14}/PKG-INFO +2 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/README.md +1 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/pyproject.toml +1 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/helpers/annotation_drawer_tools.py +14 -13
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/helpers/plot_distributions.py +4 -3
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/helpers/scikit_pca_analysis.py +1 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/helpers/tags.py +1 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/__init__.py +1 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/base_annotation_drawer.py +5 -2
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/base_visualization_template.py +11 -10
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/bbox_drawer.py +2 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/data_distribution_visualization.py +17 -10
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/key_points_drawer.py +2 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/label_drawer.py +5 -2
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/oriented_bbox_drawer.py +2 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/segmentation_mask_drawer.py +4 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/templates/tabular_data_visualization.py +29 -25
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14/src/sinapsis_data_visualization.egg-info}/PKG-INFO +2 -1
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/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.14}/LICENSE +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/setup.cfg +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/helpers/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/helpers/annotation_drawer_types.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/helpers/color_utils.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization/helpers/detection_utils.py +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization.egg-info/dependency_links.txt +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/src/sinapsis_data_visualization.egg-info/requires.txt +0 -0
- {sinapsis_data_visualization-0.1.12 → sinapsis_data_visualization-0.1.14}/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.14
|
|
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
|
|
@@ -19,6 +19,7 @@ Provides-Extra: all
|
|
|
19
19
|
Requires-Dist: sinapsis-data-visualization[visualization-matplotlib]; extra == "all"
|
|
20
20
|
Dynamic: license-file
|
|
21
21
|
|
|
22
|
+
[](https://github.com/Sinapsis-AI/sinapsis-data-tools/blob/main/README.es.md)
|
|
22
23
|
<h1 align="center">
|
|
23
24
|
<br>
|
|
24
25
|
<a href="https://sinapsis.tech/">
|
|
@@ -236,17 +236,18 @@ def get_extra_ann_labels(
|
|
|
236
236
|
"""
|
|
237
237
|
text_y_offsets = []
|
|
238
238
|
extra_str_repr = []
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
239
|
+
if annotation.extra_labels is not None:
|
|
240
|
+
for label_k, label_v in annotation.extra_labels.items():
|
|
241
|
+
extra_text: str = label_k
|
|
242
|
+
if isinstance(label_v, (float, int)):
|
|
243
|
+
extra_text = get_numeric_str(extra_text, label_v)
|
|
244
|
+
else:
|
|
245
|
+
extra_text = f"{label_k}: {label_v}"
|
|
246
|
+
extra_text_w, extra_text_h = get_text_size(extra_text, text_style)
|
|
247
|
+
|
|
248
|
+
extra_text_h += spacing
|
|
249
|
+
text_h += extra_text_h
|
|
250
|
+
text_w = max(extra_text_w, text_w)
|
|
251
|
+
text_y_offsets.append(extra_text_h)
|
|
252
|
+
extra_str_repr.append(extra_text)
|
|
252
253
|
return text_y_offsets, extra_str_repr, text_h, text_w
|
|
@@ -4,17 +4,18 @@ from typing import Sequence
|
|
|
4
4
|
import matplotlib
|
|
5
5
|
import matplotlib.pyplot as plt
|
|
6
6
|
import numpy as np
|
|
7
|
+
from matplotlib.figure import Figure
|
|
7
8
|
from sinapsis_core.utils.logging_utils import sinapsis_logger
|
|
8
9
|
from sklearn.metrics import ConfusionMatrixDisplay
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
def plot_distribution(
|
|
12
|
-
figure:
|
|
13
|
+
figure: Figure,
|
|
13
14
|
labels: Sequence[str] | np.ndarray,
|
|
14
15
|
counts: list[float] | np.ndarray,
|
|
15
16
|
plot_type: str,
|
|
16
17
|
kwargs: dict,
|
|
17
|
-
) ->
|
|
18
|
+
) -> Figure:
|
|
18
19
|
"""
|
|
19
20
|
Base method to plot a distribution.
|
|
20
21
|
|
|
@@ -168,7 +169,7 @@ def plot_confusion_matrix(labels: Sequence[str], counts: np.ndarray, kwargs: dic
|
|
|
168
169
|
kwargs (dict): Additional keyword arguments (cmap, etc.)
|
|
169
170
|
"""
|
|
170
171
|
|
|
171
|
-
cmap = kwargs.pop("cmap",
|
|
172
|
+
cmap = kwargs.pop("cmap", matplotlib.colormaps["Blues"])
|
|
172
173
|
disp = ConfusionMatrixDisplay(confusion_matrix=counts, display_labels=labels)
|
|
173
174
|
disp.plot(cmap=cmap, **kwargs)
|
|
174
175
|
plt.tight_layout()
|
|
@@ -29,7 +29,7 @@ def perform_k_means_analysis(feature_vec: np.ndarray) -> tuple[np.ndarray, np.nd
|
|
|
29
29
|
return labels, reduced_vectors
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
def pre_process_images(images: list[ImagePacket], size: int = 28) -> np.ndarray
|
|
32
|
+
def pre_process_images(images: list[ImagePacket], size: int = 28) -> np.ndarray:
|
|
33
33
|
"""Resizes and flatten images to homogenize them
|
|
34
34
|
|
|
35
35
|
Args:
|
|
@@ -53,6 +53,8 @@ class BaseAnnotationDrawer(Template, abc.ABC):
|
|
|
53
53
|
overwrite: bool = False
|
|
54
54
|
randomized_color: bool = True
|
|
55
55
|
|
|
56
|
+
attributes: AttributesBaseModel
|
|
57
|
+
|
|
56
58
|
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
57
59
|
"""Initializes the Image Annotation Drawer with the given attributes."""
|
|
58
60
|
super().__init__(attributes)
|
|
@@ -88,7 +90,7 @@ class BaseAnnotationDrawer(Template, abc.ABC):
|
|
|
88
90
|
"""
|
|
89
91
|
return get_color_rgb_tuple(
|
|
90
92
|
color_map=self.COLOR_MAP,
|
|
91
|
-
class_id=ann.label,
|
|
93
|
+
class_id=int(ann.label) if ann.label is not None else None,
|
|
92
94
|
randomized=self.attributes.randomized_color,
|
|
93
95
|
)
|
|
94
96
|
|
|
@@ -154,7 +156,7 @@ class BaseAnnotationDrawer(Template, abc.ABC):
|
|
|
154
156
|
ImagePacket(
|
|
155
157
|
content=annotated_image,
|
|
156
158
|
color_space=image_packet.color_space,
|
|
157
|
-
source=f"a_{Path(image_packet
|
|
159
|
+
source=f"a_{Path(getattr(image_packet, 'source')).name}",
|
|
158
160
|
)
|
|
159
161
|
)
|
|
160
162
|
container.images.extend(annotated_images)
|
|
@@ -169,6 +171,7 @@ class BaseAnnotationDrawer(Template, abc.ABC):
|
|
|
169
171
|
Returns:
|
|
170
172
|
DataContainer: The updated data container with images annotated.
|
|
171
173
|
"""
|
|
174
|
+
|
|
172
175
|
self.process_images(container)
|
|
173
176
|
|
|
174
177
|
return container
|
|
@@ -5,10 +5,10 @@ from abc import ABC, abstractmethod
|
|
|
5
5
|
from dataclasses import dataclass
|
|
6
6
|
from typing import Any
|
|
7
7
|
|
|
8
|
-
import matplotlib
|
|
9
8
|
import matplotlib.pyplot as plt
|
|
10
9
|
import numpy as np
|
|
11
10
|
import PIL.Image
|
|
11
|
+
from matplotlib.figure import Figure
|
|
12
12
|
from pydantic import Field
|
|
13
13
|
from sinapsis_core.data_containers.data_packet import DataContainer, ImagePacket
|
|
14
14
|
from sinapsis_core.template_base import Template
|
|
@@ -90,6 +90,7 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
90
90
|
tags=[Tags.IMAGE, Tags.MATPLOTLIB, Tags.VISUALIZATION],
|
|
91
91
|
)
|
|
92
92
|
AttributesBaseModel = BasePlotAttributes
|
|
93
|
+
attributes: BasePlotAttributes
|
|
93
94
|
|
|
94
95
|
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
95
96
|
super().__init__(attributes)
|
|
@@ -97,7 +98,7 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
97
98
|
if self.attributes.save_image_dir and not os.path.exists(self.attributes.save_image_dir):
|
|
98
99
|
os.makedirs(self.attributes.save_image_dir)
|
|
99
100
|
|
|
100
|
-
def create_figure(self, text: str | None = None, rotate_x_ticks: bool = False) ->
|
|
101
|
+
def create_figure(self, text: str | None = None, rotate_x_ticks: bool = False) -> Figure:
|
|
101
102
|
"""
|
|
102
103
|
Creates a matplotlib figure with basic configuration.
|
|
103
104
|
|
|
@@ -106,7 +107,7 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
106
107
|
rotate_x_ticks (bool): Whether to rotate x-axis tick labels.
|
|
107
108
|
|
|
108
109
|
Returns:
|
|
109
|
-
|
|
110
|
+
Figure: The configured figure.
|
|
110
111
|
"""
|
|
111
112
|
figure = plt.figure(figsize=(self.attributes.fig_width, self.attributes.fig_height))
|
|
112
113
|
plt.xlabel(self.attributes.x_label or "X-Axis")
|
|
@@ -123,7 +124,7 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
123
124
|
|
|
124
125
|
def save_figure(
|
|
125
126
|
self,
|
|
126
|
-
figure:
|
|
127
|
+
figure: Figure,
|
|
127
128
|
name: str,
|
|
128
129
|
container_id: str | None = None,
|
|
129
130
|
) -> str:
|
|
@@ -131,7 +132,7 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
131
132
|
Saves the figure to disk and records the path.
|
|
132
133
|
|
|
133
134
|
Args:
|
|
134
|
-
figure (
|
|
135
|
+
figure (Figure): The figure to save.
|
|
135
136
|
name (str): Base name for the figure file.
|
|
136
137
|
container_id (str | None): Optional container ID to append to filename.
|
|
137
138
|
|
|
@@ -156,13 +157,13 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
156
157
|
|
|
157
158
|
return fig_path
|
|
158
159
|
|
|
159
|
-
def add_figure_to_container(self, container: DataContainer, figure:
|
|
160
|
+
def add_figure_to_container(self, container: DataContainer, figure: Figure, name: str) -> None:
|
|
160
161
|
"""
|
|
161
162
|
Adds the figure to the container as an image packet if requested.
|
|
162
163
|
|
|
163
164
|
Args:
|
|
164
165
|
container (DataContainer): The data container to update.
|
|
165
|
-
figure (
|
|
166
|
+
figure (Figure): The figure to add.
|
|
166
167
|
name (str): Name key for the figure.
|
|
167
168
|
"""
|
|
168
169
|
if self.attributes.add_to_image_packets:
|
|
@@ -185,7 +186,7 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
185
186
|
counts: list[float] | np.ndarray,
|
|
186
187
|
text: str | None = None,
|
|
187
188
|
rotate_x_ticks: bool = False,
|
|
188
|
-
) ->
|
|
189
|
+
) -> Figure:
|
|
189
190
|
"""
|
|
190
191
|
Creates a figure, plots data, saves it, and adds it to the container.
|
|
191
192
|
|
|
@@ -198,7 +199,7 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
198
199
|
rotate_x_ticks (bool): Whether to rotate x-axis tick labels.
|
|
199
200
|
|
|
200
201
|
Returns:
|
|
201
|
-
|
|
202
|
+
Figure: The created figure.
|
|
202
203
|
"""
|
|
203
204
|
figure = self.create_figure(text=text, rotate_x_ticks=rotate_x_ticks)
|
|
204
205
|
|
|
@@ -211,7 +212,7 @@ class BaseVisualizationTemplate(Template, ABC):
|
|
|
211
212
|
)
|
|
212
213
|
|
|
213
214
|
plot_name = f"{plot_type}" if not text else f"{plot_type}_{text}"
|
|
214
|
-
self.save_figure(figure, plot_name, container.container_id)
|
|
215
|
+
self.save_figure(figure, plot_name, str(container.container_id))
|
|
215
216
|
self.add_figure_to_container(container, figure, plot_name)
|
|
216
217
|
return figure
|
|
217
218
|
|
|
@@ -8,7 +8,8 @@ from sinapsis_data_visualization.helpers.tags import Tags
|
|
|
8
8
|
from sinapsis_data_visualization.templates.label_drawer import LabelDrawer
|
|
9
9
|
|
|
10
10
|
BBoxDrawerUIProperties = LabelDrawer.UIProperties
|
|
11
|
-
BBoxDrawerUIProperties.tags
|
|
11
|
+
if BBoxDrawerUIProperties.tags is not None:
|
|
12
|
+
BBoxDrawerUIProperties.tags.extend([Tags.BBOX])
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
class BBoxDrawer(LabelDrawer):
|
|
@@ -31,7 +31,8 @@ class DataDistributionAttributes(BasePlotAttributes):
|
|
|
31
31
|
|
|
32
32
|
|
|
33
33
|
DataDistributionUIProperties = BaseVisualizationTemplate.UIProperties
|
|
34
|
-
DataDistributionUIProperties.tags
|
|
34
|
+
if DataDistributionUIProperties.tags is not None:
|
|
35
|
+
DataDistributionUIProperties.tags.extend([Tags.CHARTS, Tags.CLUSTERING, Tags.DISTRIBUTION, Tags.PLOTS])
|
|
35
36
|
|
|
36
37
|
|
|
37
38
|
class DataDistributionVisualization(BaseVisualizationTemplate):
|
|
@@ -46,8 +47,9 @@ class DataDistributionVisualization(BaseVisualizationTemplate):
|
|
|
46
47
|
|
|
47
48
|
AttributesBaseModel = DataDistributionAttributes
|
|
48
49
|
UIProperties = DataDistributionUIProperties
|
|
50
|
+
attributes: DataDistributionAttributes
|
|
49
51
|
|
|
50
|
-
def retrieve_labels_from_images(self, container: DataContainer) -> tuple[list[str
|
|
52
|
+
def retrieve_labels_from_images(self, container: DataContainer) -> tuple[list[str], list[int]]:
|
|
51
53
|
"""
|
|
52
54
|
Iterates through the ImagePackets, extracting each of the
|
|
53
55
|
annotations for all of them.
|
|
@@ -55,18 +57,23 @@ class DataDistributionVisualization(BaseVisualizationTemplate):
|
|
|
55
57
|
with that label.
|
|
56
58
|
|
|
57
59
|
Returns:
|
|
58
|
-
tuple[list[str
|
|
60
|
+
tuple[list[str], list[int]]: list of labels and number of coincidences for each label
|
|
59
61
|
"""
|
|
60
|
-
labels: list[str | int] = [ann.label_str for image in container.images for ann in image.annotations]
|
|
61
62
|
|
|
62
|
-
|
|
63
|
+
labels: list[str] = []
|
|
64
|
+
for image in container.images:
|
|
65
|
+
for ann in image.annotations:
|
|
66
|
+
if ann.label_str is not None:
|
|
67
|
+
labels.extend([ann.label_str])
|
|
68
|
+
|
|
69
|
+
label_count: dict[str, int] = {}
|
|
63
70
|
for label in labels:
|
|
64
71
|
label_count[label] = label_count.get(label, 0) + 1
|
|
65
72
|
|
|
66
73
|
labels, counts = list(label_count.keys()), list(label_count.values())
|
|
67
74
|
return labels, counts
|
|
68
75
|
|
|
69
|
-
def process_cluster(self, container: DataContainer) -> tuple[list[
|
|
76
|
+
def process_cluster(self, container: DataContainer) -> tuple[list[str], np.ndarray]:
|
|
70
77
|
"""
|
|
71
78
|
For the k-means clustering, the images need to be flattened first
|
|
72
79
|
and then returned as a reduced vector. This method calls the pre_process_images
|
|
@@ -79,15 +86,15 @@ class DataDistributionVisualization(BaseVisualizationTemplate):
|
|
|
79
86
|
container (DataContainer): Container to extract the images from
|
|
80
87
|
|
|
81
88
|
Returns:
|
|
82
|
-
list[str
|
|
89
|
+
list[str]: list of labels
|
|
83
90
|
np.ndarray : transformed values from the pca analysis
|
|
84
91
|
"""
|
|
85
92
|
images = container.images
|
|
86
93
|
feature_arr = pre_process_images(images)
|
|
87
94
|
label, counts = perform_k_means_analysis(feature_arr)
|
|
88
|
-
return label, counts
|
|
95
|
+
return label.tolist(), counts.tolist()
|
|
89
96
|
|
|
90
|
-
def get_data_for_visualization(self, container: DataContainer) -> tuple[list[str
|
|
97
|
+
def get_data_for_visualization(self, container: DataContainer) -> tuple[list[str], list[int]]:
|
|
91
98
|
"""
|
|
92
99
|
Gets labels and counts from images in the container.
|
|
93
100
|
|
|
@@ -99,7 +106,7 @@ class DataDistributionVisualization(BaseVisualizationTemplate):
|
|
|
99
106
|
"""
|
|
100
107
|
return self.retrieve_labels_from_images(container)
|
|
101
108
|
|
|
102
|
-
def generate_visualizations(self, container: DataContainer, data: tuple[list[str
|
|
109
|
+
def generate_visualizations(self, container: DataContainer, data: tuple[list[str], list[int | float]]) -> None:
|
|
103
110
|
"""
|
|
104
111
|
Generates all requested visualizations.
|
|
105
112
|
|
|
@@ -11,7 +11,8 @@ from sinapsis_data_visualization.helpers.tags import Tags
|
|
|
11
11
|
from sinapsis_data_visualization.templates.bbox_drawer import BBoxDrawer
|
|
12
12
|
|
|
13
13
|
KeyPointsDrawerUIProperties = BBoxDrawer.UIProperties
|
|
14
|
-
KeyPointsDrawerUIProperties.tags
|
|
14
|
+
if KeyPointsDrawerUIProperties.tags is not None:
|
|
15
|
+
KeyPointsDrawerUIProperties.tags.extend([Tags.KEYPOINTS])
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
class KeyPointsDrawer(BBoxDrawer):
|
|
@@ -28,7 +28,8 @@ from sinapsis_data_visualization.templates.base_annotation_drawer import (
|
|
|
28
28
|
)
|
|
29
29
|
|
|
30
30
|
LabelDrawerUIProperties = BaseAnnotationDrawer.UIProperties
|
|
31
|
-
LabelDrawerUIProperties.tags
|
|
31
|
+
if LabelDrawerUIProperties.tags is not None:
|
|
32
|
+
LabelDrawerUIProperties.tags.extend([Tags.LABELS, Tags.CLASSIFICATION])
|
|
32
33
|
|
|
33
34
|
|
|
34
35
|
class LabelDrawer(BaseAnnotationDrawer):
|
|
@@ -97,6 +98,8 @@ class LabelDrawer(BaseAnnotationDrawer):
|
|
|
97
98
|
if isinstance(self.text_style, dict):
|
|
98
99
|
self.text_style = TextStyle(**self.text_style)
|
|
99
100
|
|
|
101
|
+
attributes: AttributesBaseModel
|
|
102
|
+
|
|
100
103
|
def set_drawing_strategy(self) -> None:
|
|
101
104
|
"""Appends the annotation to be drawn (i.e., label)
|
|
102
105
|
to the list of tasks in the execute method"""
|
|
@@ -270,7 +273,7 @@ class LabelDrawer(BaseAnnotationDrawer):
|
|
|
270
273
|
image = image_info[0]
|
|
271
274
|
font_scale, font_thickness = get_dynamic_text_properties(image)
|
|
272
275
|
self.attributes.text_style.font_scale = font_scale
|
|
273
|
-
self.attributes.text_style.thickness = font_thickness
|
|
276
|
+
self.attributes.text_style.thickness = int(font_thickness)
|
|
274
277
|
|
|
275
278
|
image = self.add_label(image_info=image_info, class_label=class_label, spacing=int(5 * font_scale))
|
|
276
279
|
|
|
@@ -12,7 +12,8 @@ from sinapsis_data_visualization.helpers.tags import Tags
|
|
|
12
12
|
from sinapsis_data_visualization.templates.bbox_drawer import BBoxDrawer
|
|
13
13
|
|
|
14
14
|
OrientedBBoxDrawerUIProperties = BBoxDrawer.UIProperties
|
|
15
|
-
OrientedBBoxDrawerUIProperties.tags
|
|
15
|
+
if OrientedBBoxDrawerUIProperties.tags is not None:
|
|
16
|
+
OrientedBBoxDrawerUIProperties.tags.extend([Tags.ORIENTED_BBOX])
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
class OrientedBBoxDrawer(BBoxDrawer):
|
|
@@ -7,7 +7,8 @@ from sinapsis_data_visualization.helpers.tags import Tags
|
|
|
7
7
|
from sinapsis_data_visualization.templates.bbox_drawer import BBoxDrawer
|
|
8
8
|
|
|
9
9
|
SegmentationMaskDrawerUIProperties = BBoxDrawer.UIProperties
|
|
10
|
-
SegmentationMaskDrawerUIProperties.tags
|
|
10
|
+
if SegmentationMaskDrawerUIProperties.tags is not None:
|
|
11
|
+
SegmentationMaskDrawerUIProperties.tags.extend([Tags.SEGMENTATION, Tags.MASKS])
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
class SegmentationMaskDrawer(BBoxDrawer):
|
|
@@ -59,6 +60,8 @@ class SegmentationMaskDrawer(BBoxDrawer):
|
|
|
59
60
|
|
|
60
61
|
alpha: float = 0.5
|
|
61
62
|
|
|
63
|
+
attributes: AttributesBaseModel
|
|
64
|
+
|
|
62
65
|
def set_drawing_strategy(self) -> None:
|
|
63
66
|
"""
|
|
64
67
|
Appends the method to draw the segmentation masks on the image, to
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
2
|
from typing import Sequence
|
|
3
3
|
|
|
4
|
-
import matplotlib
|
|
5
4
|
import matplotlib.pyplot as plt
|
|
6
5
|
import numpy as np
|
|
7
6
|
import pandas as pd
|
|
7
|
+
from matplotlib.figure import Figure
|
|
8
8
|
from pydantic import Field
|
|
9
9
|
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
10
10
|
from sinapsis_data_visualization.helpers.tags import Tags
|
|
@@ -40,7 +40,8 @@ class TabularDataVisualizationAttributes(BasePlotAttributes):
|
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
TabularDataVisualizationUIProperties = BaseVisualizationTemplate.UIProperties
|
|
43
|
-
TabularDataVisualizationUIProperties.tags
|
|
43
|
+
if TabularDataVisualizationUIProperties.tags is not None:
|
|
44
|
+
TabularDataVisualizationUIProperties.tags.extend([Tags.CHARTS, Tags.PLOTS, Tags.CORRELATION, Tags.PANDAS])
|
|
44
45
|
|
|
45
46
|
|
|
46
47
|
class TabularDataVisualization(BaseVisualizationTemplate):
|
|
@@ -53,6 +54,8 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
53
54
|
|
|
54
55
|
AttributesBaseModel = TabularDataVisualizationAttributes
|
|
55
56
|
|
|
57
|
+
attributes: TabularDataVisualizationAttributes
|
|
58
|
+
|
|
56
59
|
def get_data_for_visualization(self, container: DataContainer) -> pd.DataFrame | None:
|
|
57
60
|
"""
|
|
58
61
|
Extracts and converts tabular data from the container.
|
|
@@ -67,20 +70,21 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
67
70
|
self.logger.warning("No generic_field_key specified")
|
|
68
71
|
return None
|
|
69
72
|
|
|
70
|
-
|
|
73
|
+
dataframes = getattr(container, "data_frames", None)
|
|
71
74
|
|
|
72
|
-
if
|
|
75
|
+
if dataframes is None:
|
|
73
76
|
self.logger.warning(f"No data found with key '{self.attributes.generic_field_key}'")
|
|
74
77
|
return None
|
|
78
|
+
dataframe = dataframes[0].content
|
|
75
79
|
|
|
76
|
-
if isinstance(
|
|
77
|
-
|
|
80
|
+
if isinstance(dataframe, pd.DataFrame):
|
|
81
|
+
data = dataframe
|
|
78
82
|
else:
|
|
79
|
-
self.logger.warning(f"Data format not recognized: {type(
|
|
83
|
+
self.logger.warning(f"Data format not recognized: {type(dataframe)}")
|
|
80
84
|
return None
|
|
81
85
|
|
|
82
|
-
self.logger.info(f"Available columns: {
|
|
83
|
-
return
|
|
86
|
+
self.logger.info(f"Available columns: {data.columns}")
|
|
87
|
+
return data
|
|
84
88
|
|
|
85
89
|
def identify_feature_types(self, df: pd.DataFrame) -> tuple[list[str], list[str]]:
|
|
86
90
|
"""
|
|
@@ -114,7 +118,7 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
114
118
|
|
|
115
119
|
return numeric_cols, categorical_cols
|
|
116
120
|
|
|
117
|
-
def prepare_histogram_data(self, df: pd.DataFrame, feature: str) -> tuple[list[
|
|
121
|
+
def prepare_histogram_data(self, df: pd.DataFrame, feature: str) -> tuple[list[str], list[float]]:
|
|
118
122
|
"""
|
|
119
123
|
Prepares data for a histogram plot.
|
|
120
124
|
|
|
@@ -131,9 +135,9 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
131
135
|
bins = np.linspace(min_val, max_val, 20)
|
|
132
136
|
counts, edges = np.histogram(df[feature].to_numpy(), bins=bins)
|
|
133
137
|
|
|
134
|
-
labels = [(edges[i] + edges[i + 1]) / 2 for i in range(len(edges) - 1)]
|
|
138
|
+
labels = [str((edges[i] + edges[i + 1]) / 2) for i in range(len(edges) - 1)]
|
|
135
139
|
|
|
136
|
-
return labels, counts
|
|
140
|
+
return labels, counts.tolist()
|
|
137
141
|
|
|
138
142
|
def prepare_boxplot_data(self, df: pd.DataFrame, feature: str) -> tuple[list[str], list[np.ndarray]]:
|
|
139
143
|
"""
|
|
@@ -187,7 +191,7 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
187
191
|
|
|
188
192
|
return features_to_correlate, corr_matrix
|
|
189
193
|
|
|
190
|
-
def prepare_categorical_data(self, df: pd.DataFrame, feature: str) -> tuple[list[str], list[int]]:
|
|
194
|
+
def prepare_categorical_data(self, df: pd.DataFrame, feature: str) -> tuple[list[str], list[int | float]]:
|
|
191
195
|
"""
|
|
192
196
|
Prepares data for a categorical distribution plot.
|
|
193
197
|
|
|
@@ -203,7 +207,7 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
203
207
|
counts = value_counts.values.tolist()
|
|
204
208
|
return labels, counts
|
|
205
209
|
|
|
206
|
-
def prepare_target_pie_data(self, df: pd.DataFrame) -> tuple[list[str], list[int]]:
|
|
210
|
+
def prepare_target_pie_data(self, df: pd.DataFrame) -> tuple[list[str], list[int | float]]:
|
|
207
211
|
"""
|
|
208
212
|
Prepares data for a target distribution pie chart.
|
|
209
213
|
|
|
@@ -224,7 +228,7 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
224
228
|
|
|
225
229
|
def create_boxplot(
|
|
226
230
|
self, labels: Sequence[str], data_points: Sequence[np.ndarray], title: str | None = None
|
|
227
|
-
) ->
|
|
231
|
+
) -> Figure:
|
|
228
232
|
"""
|
|
229
233
|
Creates a boxplot figure.
|
|
230
234
|
|
|
@@ -237,21 +241,21 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
237
241
|
matplotlib.figure.Figure: The created boxplot.
|
|
238
242
|
"""
|
|
239
243
|
fig = self.create_figure(text=title)
|
|
240
|
-
plt.boxplot(data_points, labels=labels)
|
|
244
|
+
plt.boxplot(data_points, labels=labels) # ty: ignore[unknown-argument]
|
|
241
245
|
plt.ylabel("Value")
|
|
242
246
|
plt.xticks(rotation=45, ha="right")
|
|
243
247
|
plt.tight_layout()
|
|
244
248
|
return fig
|
|
245
249
|
|
|
246
|
-
def generate_visualizations(self, container: DataContainer,
|
|
250
|
+
def generate_visualizations(self, container: DataContainer, data: pd.DataFrame) -> None:
|
|
247
251
|
"""
|
|
248
252
|
Generates all requested visualizations for the tabular data.
|
|
249
253
|
|
|
250
254
|
Args:
|
|
251
255
|
container (DataContainer): Data container.
|
|
252
|
-
|
|
256
|
+
data (pd.DataFrame): The tabular data.
|
|
253
257
|
"""
|
|
254
|
-
numeric_cols, categorical_cols = self.identify_feature_types(
|
|
258
|
+
numeric_cols, categorical_cols = self.identify_feature_types(data)
|
|
255
259
|
self.logger.info(f"Found {len(numeric_cols)} numeric features and {len(categorical_cols)} categorical features")
|
|
256
260
|
|
|
257
261
|
if not numeric_cols and not categorical_cols:
|
|
@@ -260,7 +264,7 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
260
264
|
|
|
261
265
|
if self.attributes.histogram:
|
|
262
266
|
for feature in numeric_cols:
|
|
263
|
-
labels, counts = self.prepare_histogram_data(
|
|
267
|
+
labels, counts = self.prepare_histogram_data(data, feature)
|
|
264
268
|
if len(labels) > 0 and len(counts) > 0:
|
|
265
269
|
self.plot_and_save(
|
|
266
270
|
container=container,
|
|
@@ -272,14 +276,14 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
272
276
|
|
|
273
277
|
if self.attributes.box_plot:
|
|
274
278
|
for feature in numeric_cols:
|
|
275
|
-
box_labels, data_points = self.prepare_boxplot_data(
|
|
279
|
+
box_labels, data_points = self.prepare_boxplot_data(data, feature)
|
|
276
280
|
if len(box_labels) > 0 and len(data_points) > 0:
|
|
277
281
|
fig = self.create_boxplot(labels=box_labels, data_points=data_points, title=f"{feature}")
|
|
278
|
-
self.save_figure(fig, f"boxplot_{feature}", container.container_id)
|
|
282
|
+
self.save_figure(fig, f"boxplot_{feature}", str(container.container_id))
|
|
279
283
|
self.add_figure_to_container(container, fig, f"boxplot_{feature}")
|
|
280
284
|
|
|
281
285
|
if self.attributes.correlation and len(numeric_cols) > 1:
|
|
282
|
-
corr_labels, corr_matrix = self.prepare_correlation_data(
|
|
286
|
+
corr_labels, corr_matrix = self.prepare_correlation_data(data, numeric_cols)
|
|
283
287
|
if len(corr_labels) > 0 and corr_matrix.size > 0:
|
|
284
288
|
self.plot_and_save(
|
|
285
289
|
container=container,
|
|
@@ -291,7 +295,7 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
291
295
|
|
|
292
296
|
if self.attributes.categorical_dist and categorical_cols:
|
|
293
297
|
for feature in categorical_cols:
|
|
294
|
-
cat_labels, cat_counts = self.prepare_categorical_data(
|
|
298
|
+
cat_labels, cat_counts = self.prepare_categorical_data(data, feature)
|
|
295
299
|
if len(cat_labels) > 0 and len(cat_counts) > 0:
|
|
296
300
|
self.plot_and_save(
|
|
297
301
|
container=container,
|
|
@@ -303,7 +307,7 @@ class TabularDataVisualization(BaseVisualizationTemplate):
|
|
|
303
307
|
)
|
|
304
308
|
|
|
305
309
|
if self.attributes.pie_chart:
|
|
306
|
-
pie_labels, pie_counts = self.prepare_target_pie_data(
|
|
310
|
+
pie_labels, pie_counts = self.prepare_target_pie_data(data)
|
|
307
311
|
if len(pie_labels) > 0 and len(pie_counts) > 0:
|
|
308
312
|
self.plot_and_save(
|
|
309
313
|
container=container,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sinapsis-data-visualization
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.14
|
|
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
|
|
@@ -19,6 +19,7 @@ Provides-Extra: all
|
|
|
19
19
|
Requires-Dist: sinapsis-data-visualization[visualization-matplotlib]; extra == "all"
|
|
20
20
|
Dynamic: license-file
|
|
21
21
|
|
|
22
|
+
[](https://github.com/Sinapsis-AI/sinapsis-data-tools/blob/main/README.es.md)
|
|
22
23
|
<h1 align="center">
|
|
23
24
|
<br>
|
|
24
25
|
<a href="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
|