sinapsis-data-visualization 0.1.6__tar.gz → 0.1.8__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.6/src/sinapsis_data_visualization.egg-info → sinapsis_data_visualization-0.1.8}/PKG-INFO +3 -1
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/pyproject.toml +3 -1
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/helpers/plot_distributions.py +105 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/helpers/scikit_pca_analysis.py +1 -1
- sinapsis_data_visualization-0.1.8/src/sinapsis_data_visualization/helpers/tags.py +28 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/templates/__init__.py +3 -1
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/templates/base_annotation_drawer.py +5 -1
- sinapsis_data_visualization-0.1.8/src/sinapsis_data_visualization/templates/base_visualization_template.py +256 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/templates/bbox_drawer.py +6 -0
- sinapsis_data_visualization-0.1.8/src/sinapsis_data_visualization/templates/data_distribution_visualization.py +147 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/templates/key_points_drawer.py +6 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/templates/label_drawer.py +13 -3
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/templates/oriented_bbox_drawer.py +6 -0
- sinapsis_data_visualization-0.1.8/src/sinapsis_data_visualization/templates/prediction_visualization.py +213 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/templates/segmentation_mask_drawer.py +6 -0
- sinapsis_data_visualization-0.1.8/src/sinapsis_data_visualization/templates/tabular_data_visualization.py +313 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8/src/sinapsis_data_visualization.egg-info}/PKG-INFO +3 -1
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization.egg-info/SOURCES.txt +6 -1
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization.egg-info/requires.txt +2 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/LICENSE +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/README.md +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/setup.cfg +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/helpers/__init__.py +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/helpers/annotation_drawer_tools.py +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/helpers/annotation_drawer_types.py +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/helpers/color_utils.py +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/helpers/detection_utils.py +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization/templates/data_distribution_visualization_template.py +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/src/sinapsis_data_visualization.egg-info/dependency_links.txt +0 -0
- {sinapsis_data_visualization-0.1.6 → sinapsis_data_visualization-0.1.8}/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.8
|
|
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
|
|
@@ -10,6 +10,8 @@ Project-URL: Repository, https://github.com/Sinapsis-AI/sinapsis-data-tools.git
|
|
|
10
10
|
Requires-Python: >=3.10
|
|
11
11
|
Description-Content-Type: text/markdown
|
|
12
12
|
License-File: LICENSE
|
|
13
|
+
Requires-Dist: numpy>=2.2.5
|
|
14
|
+
Requires-Dist: pandas>=2.2.3
|
|
13
15
|
Requires-Dist: sinapsis>=0.1.1
|
|
14
16
|
Provides-Extra: visualization-matplotlib
|
|
15
17
|
Requires-Dist: matplotlib>=3.9.2; extra == "visualization-matplotlib"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "sinapsis-data-visualization"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.8"
|
|
4
4
|
description = "Templates to perform visualization of data distributions and image annotations."
|
|
5
5
|
authors = [{ name = "SinapsisAI", email = "dev@sinapsis.tech" }]
|
|
6
6
|
|
|
@@ -8,6 +8,8 @@ readme = "README.md"
|
|
|
8
8
|
license-files = ["LICENSE"]
|
|
9
9
|
requires-python = ">=3.10"
|
|
10
10
|
dependencies = [
|
|
11
|
+
"numpy>=2.2.5",
|
|
12
|
+
"pandas>=2.2.3",
|
|
11
13
|
"sinapsis>=0.1.1",
|
|
12
14
|
]
|
|
13
15
|
|
|
@@ -5,6 +5,7 @@ import matplotlib
|
|
|
5
5
|
import matplotlib.pyplot as plt
|
|
6
6
|
import numpy as np
|
|
7
7
|
from sinapsis_core.utils.logging_utils import sinapsis_logger
|
|
8
|
+
from sklearn.metrics import ConfusionMatrixDisplay
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
def plot_distribution(
|
|
@@ -122,9 +123,113 @@ def plot_cluster(labels: Sequence[str], counts: list[float] | np.ndarray, kwargs
|
|
|
122
123
|
plt.scatter(counts[:, 0], counts[:, 1], c=labels, **kwargs)
|
|
123
124
|
|
|
124
125
|
|
|
126
|
+
def plot_correlation(labels: Sequence[str], counts: list[float] | np.ndarray, kwargs: dict) -> None:
|
|
127
|
+
"""
|
|
128
|
+
Plots a correlation heatmap for numeric features.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
labels (Sequence[str]): Column names for the correlation matrix
|
|
132
|
+
counts (list[float] | np.ndarray): Correlation matrix values
|
|
133
|
+
kwargs (dict): Additional keyword arguments (cmap, etc.)
|
|
134
|
+
"""
|
|
135
|
+
plt.imshow(counts)
|
|
136
|
+
plt.colorbar()
|
|
137
|
+
|
|
138
|
+
plt.xticks(np.arange(len(labels)), labels, rotation=45, ha="right")
|
|
139
|
+
plt.yticks(np.arange(len(labels)), labels)
|
|
140
|
+
|
|
141
|
+
counts_array = np.asarray(counts)
|
|
142
|
+
for i in range(len(labels)):
|
|
143
|
+
for j in range(len(labels)):
|
|
144
|
+
color = "white" if abs(counts_array[i, j]) > 0.5 else "black"
|
|
145
|
+
plt.text(j, i, f"{counts_array[i, j]:.2f}", ha="center", va="center", color=color, **kwargs)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def plot_categorical_dist(labels: Sequence[str], counts: list[float] | np.ndarray, kwargs: dict) -> None:
|
|
149
|
+
"""
|
|
150
|
+
Plots a bar chart for categorical data distribution.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
labels (Sequence[str]): Category labels
|
|
154
|
+
counts (list[float]): Frequency of each category
|
|
155
|
+
kwargs (dict): Additional keyword arguments (color, etc.)
|
|
156
|
+
"""
|
|
157
|
+
plt.bar(range(len(labels)), counts, **kwargs)
|
|
158
|
+
plt.xticks(range(len(labels)), labels, rotation=45, ha="right")
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def plot_confusion_matrix(labels: Sequence[str], counts: np.ndarray, kwargs: dict) -> None:
|
|
162
|
+
"""
|
|
163
|
+
Plots a confusion matrix for model evaluation.
|
|
164
|
+
|
|
165
|
+
Args:
|
|
166
|
+
labels (Sequence[str]): Class labels (typically ["0", "1"] for binary classification)
|
|
167
|
+
counts (np.ndarray): Confusion matrix array
|
|
168
|
+
kwargs (dict): Additional keyword arguments (cmap, etc.)
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
cmap = kwargs.pop("cmap", plt.cm.Blues)
|
|
172
|
+
disp = ConfusionMatrixDisplay(confusion_matrix=counts, display_labels=labels)
|
|
173
|
+
disp.plot(cmap=cmap, **kwargs)
|
|
174
|
+
plt.tight_layout()
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def plot_roc_curve(labels: Sequence[str], counts: np.ndarray, kwargs: dict) -> None:
|
|
178
|
+
"""
|
|
179
|
+
Plots a ROC curve for model evaluation.
|
|
180
|
+
|
|
181
|
+
Args:
|
|
182
|
+
labels (Sequence[str]): Label for the ROC curve
|
|
183
|
+
counts (np.ndarray): 2D array with columns [fpr, tpr]
|
|
184
|
+
kwargs (dict): Additional keyword arguments
|
|
185
|
+
"""
|
|
186
|
+
fpr, tpr = counts[:, 0], counts[:, 1]
|
|
187
|
+
|
|
188
|
+
from sklearn.metrics import auc
|
|
189
|
+
|
|
190
|
+
roc_auc = auc(fpr, tpr)
|
|
191
|
+
|
|
192
|
+
plt.plot(fpr, tpr, lw=2, label=f"{labels[0]} (AUC = {roc_auc:.2f})", **kwargs)
|
|
193
|
+
|
|
194
|
+
plt.plot([0, 1], [0, 1], "--", color="gray", lw=1)
|
|
195
|
+
|
|
196
|
+
plt.xlim([0.0, 1.0])
|
|
197
|
+
plt.ylim([0.0, 1.05])
|
|
198
|
+
plt.xlabel("False Positive Rate")
|
|
199
|
+
plt.ylabel("True Positive Rate")
|
|
200
|
+
plt.legend(loc="lower right")
|
|
201
|
+
plt.grid(True, alpha=0.3)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def plot_precision_recall_curve(labels: Sequence[str], counts: np.ndarray, kwargs: dict) -> None:
|
|
205
|
+
"""
|
|
206
|
+
Plots a precision-recall curve for model evaluation.
|
|
207
|
+
|
|
208
|
+
Args:
|
|
209
|
+
labels (Sequence[str]): Label for the PR curve
|
|
210
|
+
counts (np.ndarray): 2D array with columns [precision, recall]
|
|
211
|
+
kwargs (dict): Additional keyword arguments
|
|
212
|
+
"""
|
|
213
|
+
precision, recall = counts[:, 0], counts[:, 1]
|
|
214
|
+
|
|
215
|
+
plt.plot(recall, precision, lw=2, label=f"{labels[0]}", **kwargs)
|
|
216
|
+
|
|
217
|
+
plt.xlim([0.0, 1.0])
|
|
218
|
+
plt.ylim([0.0, 1.05])
|
|
219
|
+
plt.xlabel("Recall")
|
|
220
|
+
plt.ylabel("Precision")
|
|
221
|
+
plt.legend(loc="lower left")
|
|
222
|
+
plt.grid(True, alpha=0.3)
|
|
223
|
+
|
|
224
|
+
|
|
125
225
|
plot_map: dict = {
|
|
126
226
|
"histogram": plot_histogram,
|
|
127
227
|
"pie_chart": plot_piechart,
|
|
128
228
|
"box_plot": plot_boxplot,
|
|
129
229
|
"k_means_clustering": plot_cluster,
|
|
230
|
+
"correlation": plot_correlation,
|
|
231
|
+
"categorical_dist": plot_categorical_dist,
|
|
232
|
+
"confusion_matrix": plot_confusion_matrix,
|
|
233
|
+
"roc_curve": plot_roc_curve,
|
|
234
|
+
"precision_recall_curve": plot_precision_recall_curve,
|
|
130
235
|
}
|
|
@@ -35,7 +35,7 @@ def pre_process_images(images: list[ImagePacket], size: int = 28) -> np.ndarray
|
|
|
35
35
|
Args:
|
|
36
36
|
images (list[ImagePacket]): list of ImagePackets to resize
|
|
37
37
|
size (int): new size for the images.
|
|
38
|
-
|
|
38
|
+
|
|
39
39
|
Returns:
|
|
40
40
|
np.ndarray : The array with flattened and resized image"""
|
|
41
41
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from enum import Enum
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Tags(Enum):
|
|
6
|
+
ANNOTATIONS = "annotations"
|
|
7
|
+
BBOX = "bbox"
|
|
8
|
+
CHARTS = "charts"
|
|
9
|
+
CLASSIFICATION = "classification"
|
|
10
|
+
CONFUSION = "confusion_matrix"
|
|
11
|
+
CORRELATION = "correlation_matrix"
|
|
12
|
+
CURVES = "curves"
|
|
13
|
+
CLUSTERING = "clustering"
|
|
14
|
+
DISTRIBUTION = "distribution"
|
|
15
|
+
DRAWER = "drawer"
|
|
16
|
+
DYNAMIC = "dynamic"
|
|
17
|
+
HISTOGRAM = "histogram"
|
|
18
|
+
IMAGE = "image"
|
|
19
|
+
KEYPOINTS = "keypoints"
|
|
20
|
+
LABELS = "labels"
|
|
21
|
+
MATPLOTLIB = "matplotlib"
|
|
22
|
+
MASKS = "masks"
|
|
23
|
+
METRICS = "metrics"
|
|
24
|
+
ORIENTED_BBOX = "oriented_bbox"
|
|
25
|
+
PANDAS = "pandas"
|
|
26
|
+
PLOTS = "plots"
|
|
27
|
+
SEGMENTATION = "segmentation"
|
|
28
|
+
VISUALIZATION = "visualization"
|
|
@@ -5,12 +5,14 @@ from typing import Callable
|
|
|
5
5
|
_root_lib_path = "sinapsis_data_visualization.templates"
|
|
6
6
|
|
|
7
7
|
_template_lookup = {
|
|
8
|
-
"DataDistributionVisualization": f"{_root_lib_path}.
|
|
8
|
+
"DataDistributionVisualization": f"{_root_lib_path}.data_distribution_visualization",
|
|
9
9
|
"LabelDrawer": f"{_root_lib_path}.label_drawer",
|
|
10
10
|
"BBoxDrawer": f"{_root_lib_path}.bbox_drawer",
|
|
11
11
|
"KeyPointsDrawer": f"{_root_lib_path}.key_points_drawer",
|
|
12
12
|
"OrientedBBoxDrawer": f"{_root_lib_path}.oriented_bbox_drawer",
|
|
13
13
|
"SegmentationMaskDrawer": f"{_root_lib_path}.segmentation_mask_drawer",
|
|
14
|
+
"TabularDataVisualization": f"{_root_lib_path}.tabular_data_visualization",
|
|
15
|
+
"PredictionVisualization": f"{_root_lib_path}.prediction_visualization",
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
|
|
@@ -18,6 +18,7 @@ from sinapsis_data_visualization.helpers.color_utils import (
|
|
|
18
18
|
build_color_map,
|
|
19
19
|
get_color_rgb_tuple,
|
|
20
20
|
)
|
|
21
|
+
from sinapsis_data_visualization.helpers.tags import Tags
|
|
21
22
|
|
|
22
23
|
random.seed(0)
|
|
23
24
|
|
|
@@ -33,7 +34,10 @@ class BaseAnnotationDrawer(Template, abc.ABC):
|
|
|
33
34
|
"""
|
|
34
35
|
|
|
35
36
|
COLOR_MAP = build_color_map()
|
|
36
|
-
UIProperties = UIPropertiesMetadata(
|
|
37
|
+
UIProperties = UIPropertiesMetadata(
|
|
38
|
+
output_type=OutputTypes.IMAGE,
|
|
39
|
+
tags=[Tags.ANNOTATIONS, Tags.DRAWER, Tags.IMAGE, Tags.VISUALIZATION],
|
|
40
|
+
)
|
|
37
41
|
|
|
38
42
|
class AttributesBaseModel(TemplateAttributes):
|
|
39
43
|
"""
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import io
|
|
3
|
+
import os
|
|
4
|
+
from abc import ABC, abstractmethod
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import matplotlib
|
|
9
|
+
import matplotlib.pyplot as plt
|
|
10
|
+
import numpy as np
|
|
11
|
+
import PIL.Image
|
|
12
|
+
from pydantic import Field
|
|
13
|
+
from sinapsis_core.data_containers.data_packet import DataContainer, ImagePacket
|
|
14
|
+
from sinapsis_core.template_base import Template
|
|
15
|
+
from sinapsis_core.template_base.base_models import (
|
|
16
|
+
OutputTypes,
|
|
17
|
+
TemplateAttributes,
|
|
18
|
+
TemplateAttributeType,
|
|
19
|
+
UIPropertiesMetadata,
|
|
20
|
+
)
|
|
21
|
+
from sinapsis_core.utils.env_var_keys import SINAPSIS_CACHE_DIR
|
|
22
|
+
from sinapsis_data_visualization.helpers.plot_distributions import plot_distribution
|
|
23
|
+
from sinapsis_data_visualization.helpers.tags import Tags
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class PlotTypes:
|
|
28
|
+
"""Dataclass with different types of plots."""
|
|
29
|
+
|
|
30
|
+
HISTOGRAM: str = "histogram"
|
|
31
|
+
BOX_PLOT: str = "box_plot"
|
|
32
|
+
PIE_CHART: str = "pie_chart"
|
|
33
|
+
CLUSTERING: str = "k_means_clustering"
|
|
34
|
+
CORRELATION: str = "correlation"
|
|
35
|
+
CATEGORICAL_DIST: str = "categorical_dist"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class BasePlotAttributes(TemplateAttributes):
|
|
39
|
+
"""Base attributes for all plot types.
|
|
40
|
+
|
|
41
|
+
Attributes:
|
|
42
|
+
generic_field_key (str): Key to extract data from the container.
|
|
43
|
+
fig_title (str|None): Title for the figure.
|
|
44
|
+
y_label (str|None): Label of the y-axis.
|
|
45
|
+
x_label (str|None): Label of the x-axis.
|
|
46
|
+
fig_width (int): Width of the figure.
|
|
47
|
+
fig_height (int): Height of the figure.
|
|
48
|
+
x_position (float): X-position for the legend and/or texts.
|
|
49
|
+
y_position (float): Y-position for the legend and/or texts.
|
|
50
|
+
save_image_dir (str): Directory to save the image.
|
|
51
|
+
fig_name (str): Name of the figure.
|
|
52
|
+
add_to_image_packets (bool): Whether to add generated plots to image packets.
|
|
53
|
+
image_format (str): File format for saved images.
|
|
54
|
+
histogram (bool): Whether to create a histogram visualization.
|
|
55
|
+
box_plot (bool): Whether to create a box plot visualization.
|
|
56
|
+
pie_chart (bool): Whether to create a pie chart visualization.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
generic_field_key: str | None = None
|
|
60
|
+
fig_title: str | None = None
|
|
61
|
+
y_label: str | None = None
|
|
62
|
+
x_label: str | None = None
|
|
63
|
+
fig_width: int = 10
|
|
64
|
+
fig_height: int = 10
|
|
65
|
+
x_position: float = 0.5
|
|
66
|
+
y_position: float = 0.5
|
|
67
|
+
text: str | None = None
|
|
68
|
+
save_image_dir: str | None = SINAPSIS_CACHE_DIR
|
|
69
|
+
fig_name: str = "visualization"
|
|
70
|
+
kwargs: dict[str, Any] = Field(default_factory=dict)
|
|
71
|
+
add_to_image_packets: bool = True
|
|
72
|
+
image_format: str = "png"
|
|
73
|
+
histogram: bool = False
|
|
74
|
+
box_plot: bool = False
|
|
75
|
+
pie_chart: bool = False
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class BaseVisualizationTemplate(Template, ABC):
|
|
79
|
+
"""
|
|
80
|
+
Base class for all visualization templates.
|
|
81
|
+
|
|
82
|
+
This class provides common functionality for creating, saving, and adding
|
|
83
|
+
figures to data containers. Child classes should implement the required
|
|
84
|
+
abstract methods.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
UIProperties = UIPropertiesMetadata(
|
|
88
|
+
category="Visualization",
|
|
89
|
+
output_type=OutputTypes.IMAGE,
|
|
90
|
+
tags=[Tags.IMAGE, Tags.MATPLOTLIB, Tags.VISUALIZATION],
|
|
91
|
+
)
|
|
92
|
+
AttributesBaseModel = BasePlotAttributes
|
|
93
|
+
|
|
94
|
+
def __init__(self, attributes: TemplateAttributeType) -> None:
|
|
95
|
+
super().__init__(attributes)
|
|
96
|
+
self.saved_image_paths: list[str] = []
|
|
97
|
+
if self.attributes.save_image_dir and not os.path.exists(self.attributes.save_image_dir):
|
|
98
|
+
os.makedirs(self.attributes.save_image_dir)
|
|
99
|
+
|
|
100
|
+
def create_figure(self, text: str | None = None, rotate_x_ticks: bool = False) -> matplotlib.figure.Figure:
|
|
101
|
+
"""
|
|
102
|
+
Creates a matplotlib figure with basic configuration.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
text (str | None): Optional text to add to the figure.
|
|
106
|
+
rotate_x_ticks (bool): Whether to rotate x-axis tick labels.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
matplotlib.figure.Figure: The configured figure.
|
|
110
|
+
"""
|
|
111
|
+
figure = plt.figure(figsize=(self.attributes.fig_width, self.attributes.fig_height))
|
|
112
|
+
plt.xlabel(self.attributes.x_label or "X-Axis")
|
|
113
|
+
plt.ylabel(self.attributes.y_label or "Y-Axis")
|
|
114
|
+
plt.title(self.attributes.fig_title or "Plot Title")
|
|
115
|
+
|
|
116
|
+
if text:
|
|
117
|
+
plt.text(self.attributes.x_position, self.attributes.y_position, text)
|
|
118
|
+
|
|
119
|
+
if rotate_x_ticks:
|
|
120
|
+
plt.xticks(rotation=90)
|
|
121
|
+
|
|
122
|
+
return figure
|
|
123
|
+
|
|
124
|
+
def save_figure(
|
|
125
|
+
self,
|
|
126
|
+
figure: matplotlib.figure.Figure,
|
|
127
|
+
name: str,
|
|
128
|
+
container_id: str | None = None,
|
|
129
|
+
) -> str:
|
|
130
|
+
"""
|
|
131
|
+
Saves the figure to disk and records the path.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
figure (matplotlib.figure.Figure): The figure to save.
|
|
135
|
+
name (str): Base name for the figure file.
|
|
136
|
+
container_id (str | None): Optional container ID to append to filename.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
str: The saved file path with extension.
|
|
140
|
+
"""
|
|
141
|
+
if not self.attributes.save_image_dir:
|
|
142
|
+
self.logger.warning("No save directory specified, figure will not be saved to disk")
|
|
143
|
+
return ""
|
|
144
|
+
|
|
145
|
+
file_name = f"{name}_{self.attributes.fig_name}"
|
|
146
|
+
if container_id:
|
|
147
|
+
file_name = f"{file_name}_{container_id}"
|
|
148
|
+
|
|
149
|
+
file_name_with_ext = f"{file_name}.{self.attributes.image_format}"
|
|
150
|
+
fig_path = os.path.join(self.attributes.save_image_dir, file_name_with_ext)
|
|
151
|
+
|
|
152
|
+
plt.savefig(fig_path)
|
|
153
|
+
plt.close(figure)
|
|
154
|
+
|
|
155
|
+
self.saved_image_paths.append(fig_path)
|
|
156
|
+
|
|
157
|
+
return fig_path
|
|
158
|
+
|
|
159
|
+
def add_figure_to_container(self, container: DataContainer, figure: matplotlib.figure.Figure, name: str) -> None:
|
|
160
|
+
"""
|
|
161
|
+
Adds the figure to the container as an image packet if requested.
|
|
162
|
+
|
|
163
|
+
Args:
|
|
164
|
+
container (DataContainer): The data container to update.
|
|
165
|
+
figure (matplotlib.figure.Figure): The figure to add.
|
|
166
|
+
name (str): Name key for the figure.
|
|
167
|
+
"""
|
|
168
|
+
if self.attributes.add_to_image_packets:
|
|
169
|
+
buf = io.BytesIO()
|
|
170
|
+
figure.savefig(buf, format=self.attributes.image_format)
|
|
171
|
+
buf.seek(0)
|
|
172
|
+
image = PIL.Image.open(buf)
|
|
173
|
+
image_array = np.array(image)
|
|
174
|
+
image_packet = ImagePacket(
|
|
175
|
+
content=image_array,
|
|
176
|
+
source=f"visualization_{name}",
|
|
177
|
+
)
|
|
178
|
+
container.images.append(image_packet)
|
|
179
|
+
|
|
180
|
+
def plot_and_save(
|
|
181
|
+
self,
|
|
182
|
+
container: DataContainer,
|
|
183
|
+
plot_type: str,
|
|
184
|
+
labels: list[str] | np.ndarray,
|
|
185
|
+
counts: list[float] | np.ndarray,
|
|
186
|
+
text: str | None = None,
|
|
187
|
+
rotate_x_ticks: bool = False,
|
|
188
|
+
) -> matplotlib.figure.Figure:
|
|
189
|
+
"""
|
|
190
|
+
Creates a figure, plots data, saves it, and adds it to the container.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
container (DataContainer): The data container.
|
|
194
|
+
plot_type (str): Type of plot to create.
|
|
195
|
+
labels (list[str] | np.ndarray): Labels for the plot.
|
|
196
|
+
counts (list[float] | np.ndarray): Data values for the plot.
|
|
197
|
+
text (str | None): Optional title text.
|
|
198
|
+
rotate_x_ticks (bool): Whether to rotate x-axis tick labels.
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
matplotlib.figure.Figure: The created figure.
|
|
202
|
+
"""
|
|
203
|
+
figure = self.create_figure(text=text, rotate_x_ticks=rotate_x_ticks)
|
|
204
|
+
|
|
205
|
+
figure = plot_distribution(
|
|
206
|
+
figure=figure,
|
|
207
|
+
labels=labels,
|
|
208
|
+
counts=counts,
|
|
209
|
+
plot_type=plot_type,
|
|
210
|
+
kwargs=self.attributes.kwargs,
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
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.add_figure_to_container(container, figure, plot_name)
|
|
216
|
+
return figure
|
|
217
|
+
|
|
218
|
+
@abstractmethod
|
|
219
|
+
def get_data_for_visualization(self, container: DataContainer) -> Any:
|
|
220
|
+
"""
|
|
221
|
+
Extracts data from the container for visualization.
|
|
222
|
+
|
|
223
|
+
Args:
|
|
224
|
+
container (DataContainer): The data container.
|
|
225
|
+
|
|
226
|
+
Returns:
|
|
227
|
+
Any: The data to visualize.
|
|
228
|
+
"""
|
|
229
|
+
|
|
230
|
+
@abstractmethod
|
|
231
|
+
def generate_visualizations(self, container: DataContainer, data: Any) -> None:
|
|
232
|
+
"""
|
|
233
|
+
Generates all visualizations for the given data.
|
|
234
|
+
|
|
235
|
+
Args:
|
|
236
|
+
container (DataContainer): The data container.
|
|
237
|
+
data (Any): The data to visualize.
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
def execute(self, container: DataContainer) -> DataContainer:
|
|
241
|
+
"""
|
|
242
|
+
Executes the visualization template.
|
|
243
|
+
|
|
244
|
+
Args:
|
|
245
|
+
container (DataContainer): The input data container.
|
|
246
|
+
|
|
247
|
+
Returns:
|
|
248
|
+
DataContainer: The updated data container with visualizations.
|
|
249
|
+
"""
|
|
250
|
+
data = self.get_data_for_visualization(container)
|
|
251
|
+
if data is not None:
|
|
252
|
+
self.generate_visualizations(container, data)
|
|
253
|
+
|
|
254
|
+
self._set_generic_data(container, self.saved_image_paths)
|
|
255
|
+
|
|
256
|
+
return container
|
|
@@ -4,8 +4,12 @@ import cv2
|
|
|
4
4
|
import numpy as np
|
|
5
5
|
from sinapsis_core.data_containers.annotations import BoundingBox, ImageAnnotations
|
|
6
6
|
from sinapsis_data_visualization.helpers.color_utils import RGB_TYPE
|
|
7
|
+
from sinapsis_data_visualization.helpers.tags import Tags
|
|
7
8
|
from sinapsis_data_visualization.templates.label_drawer import LabelDrawer
|
|
8
9
|
|
|
10
|
+
BBoxDrawerUIProperties = LabelDrawer.UIProperties
|
|
11
|
+
BBoxDrawerUIProperties.tags.extend([Tags.BBOX])
|
|
12
|
+
|
|
9
13
|
|
|
10
14
|
class BBoxDrawer(LabelDrawer):
|
|
11
15
|
"""
|
|
@@ -42,6 +46,8 @@ class BBoxDrawer(LabelDrawer):
|
|
|
42
46
|
|
|
43
47
|
"""
|
|
44
48
|
|
|
49
|
+
UIProperties = BBoxDrawerUIProperties
|
|
50
|
+
|
|
45
51
|
def set_drawing_strategy(self) -> None:
|
|
46
52
|
"""
|
|
47
53
|
Appends the method to draw the bbox on the image, to
|
|
@@ -0,0 +1,147 @@
|
|
|
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.scikit_pca_analysis import (
|
|
5
|
+
perform_k_means_analysis,
|
|
6
|
+
pre_process_images,
|
|
7
|
+
)
|
|
8
|
+
from sinapsis_data_visualization.helpers.tags import Tags
|
|
9
|
+
from sinapsis_data_visualization.templates.base_visualization_template import (
|
|
10
|
+
BasePlotAttributes,
|
|
11
|
+
BaseVisualizationTemplate,
|
|
12
|
+
PlotTypes,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DataDistributionAttributes(BasePlotAttributes):
|
|
17
|
+
"""
|
|
18
|
+
Attributes for data distribution visualization.
|
|
19
|
+
|
|
20
|
+
Attributes:
|
|
21
|
+
histogram (bool): Whether to plot a histogram.
|
|
22
|
+
box_plot (bool): Whether to plot a box plot.
|
|
23
|
+
pie_chart (bool): Whether to plot a pie chart.
|
|
24
|
+
k_means_clustering (bool): Whether to plot k-means clustering.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
histogram: bool = False
|
|
28
|
+
box_plot: bool = False
|
|
29
|
+
pie_chart: bool = False
|
|
30
|
+
k_means_clustering: bool = False
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
DataDistributionUIProperties = BaseVisualizationTemplate.UIProperties
|
|
34
|
+
DataDistributionUIProperties.tags.extend([Tags.CHARTS, Tags.CLUSTERING, Tags.DISTRIBUTION, Tags.PLOTS])
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class DataDistributionVisualization(BaseVisualizationTemplate):
|
|
38
|
+
"""
|
|
39
|
+
This template plots the distribution for image data and can be
|
|
40
|
+
extended to plot the distribution of different Packets, provided
|
|
41
|
+
they have labels.
|
|
42
|
+
|
|
43
|
+
The template allows for drawing histograms, box plots, pie charts,
|
|
44
|
+
and PCA from k-means clusters.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
AttributesBaseModel = DataDistributionAttributes
|
|
48
|
+
UIProperties = DataDistributionUIProperties
|
|
49
|
+
|
|
50
|
+
def retrieve_labels_from_images(self, container: DataContainer) -> tuple[list[str | int], list[int]]:
|
|
51
|
+
"""
|
|
52
|
+
Iterates through the ImagePackets, extracting each of the
|
|
53
|
+
annotations for all of them.
|
|
54
|
+
Generates a dictionary with the label and number of points associated
|
|
55
|
+
with that label.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
tuple[list[str | int], list[int]]: list of labels and number of coincidences for each label
|
|
59
|
+
"""
|
|
60
|
+
labels: list[str | int] = [ann.label_str for image in container.images for ann in image.annotations]
|
|
61
|
+
|
|
62
|
+
label_count: dict[str | int, int] = {}
|
|
63
|
+
for label in labels:
|
|
64
|
+
label_count[label] = label_count.get(label, 0) + 1
|
|
65
|
+
|
|
66
|
+
labels, counts = list(label_count.keys()), list(label_count.values())
|
|
67
|
+
return labels, counts
|
|
68
|
+
|
|
69
|
+
def process_cluster(self, container: DataContainer) -> tuple[list[int | str], np.ndarray]:
|
|
70
|
+
"""
|
|
71
|
+
For the k-means clustering, the images need to be flattened first
|
|
72
|
+
and then returned as a reduced vector. This method calls the pre_process_images
|
|
73
|
+
method from scikit-pca analysis to perform such preprocessing.
|
|
74
|
+
Then, perform_k_means_analysis instantiates a KMeans class from scikit-learn
|
|
75
|
+
and perform Principal Component Analysis (PCA) on the reduced vector.
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
container (DataContainer): Container to extract the images from
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
list[str | int]: list of labels
|
|
83
|
+
np.ndarray : transformed values from the pca analysis
|
|
84
|
+
"""
|
|
85
|
+
images = container.images
|
|
86
|
+
feature_arr = pre_process_images(images)
|
|
87
|
+
label, counts = perform_k_means_analysis(feature_arr)
|
|
88
|
+
return label, counts
|
|
89
|
+
|
|
90
|
+
def get_data_for_visualization(self, container: DataContainer) -> tuple[list[str | int], list[int]]:
|
|
91
|
+
"""
|
|
92
|
+
Gets labels and counts from images in the container.
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
container (DataContainer): Data container.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
tuple[list[str | int], list[int]]: Labels and counts.
|
|
99
|
+
"""
|
|
100
|
+
return self.retrieve_labels_from_images(container)
|
|
101
|
+
|
|
102
|
+
def generate_visualizations(self, container: DataContainer, data: tuple[list[str | int], list[int]]) -> None:
|
|
103
|
+
"""
|
|
104
|
+
Generates all requested visualizations.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
container (DataContainer): Data container.
|
|
108
|
+
data (tuple[list[str | int], list[int]]): Labels and counts.
|
|
109
|
+
"""
|
|
110
|
+
labels_to_plot, counts_to_plot = data
|
|
111
|
+
|
|
112
|
+
if self.attributes.k_means_clustering:
|
|
113
|
+
labels, feature_vec = self.process_cluster(container)
|
|
114
|
+
self.plot_and_save(
|
|
115
|
+
container=container,
|
|
116
|
+
plot_type=PlotTypes.CLUSTERING,
|
|
117
|
+
labels=labels,
|
|
118
|
+
counts=feature_vec,
|
|
119
|
+
text="K-means Clustering",
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
if self.attributes.histogram:
|
|
123
|
+
self.plot_and_save(
|
|
124
|
+
container=container,
|
|
125
|
+
plot_type=PlotTypes.HISTOGRAM,
|
|
126
|
+
labels=labels_to_plot,
|
|
127
|
+
counts=counts_to_plot,
|
|
128
|
+
text="Histogram",
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
if self.attributes.box_plot:
|
|
132
|
+
self.plot_and_save(
|
|
133
|
+
container=container,
|
|
134
|
+
plot_type=PlotTypes.BOX_PLOT,
|
|
135
|
+
labels=labels_to_plot,
|
|
136
|
+
counts=counts_to_plot,
|
|
137
|
+
text="Box Plot",
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
if self.attributes.pie_chart:
|
|
141
|
+
self.plot_and_save(
|
|
142
|
+
container=container,
|
|
143
|
+
plot_type=PlotTypes.PIE_CHART,
|
|
144
|
+
labels=labels_to_plot,
|
|
145
|
+
counts=counts_to_plot,
|
|
146
|
+
text="Pie Chart",
|
|
147
|
+
)
|
|
@@ -7,8 +7,12 @@ from sinapsis_data_visualization.helpers.annotation_drawer_types import (
|
|
|
7
7
|
KeyPointAppearance,
|
|
8
8
|
)
|
|
9
9
|
from sinapsis_data_visualization.helpers.color_utils import RGB_TYPE
|
|
10
|
+
from sinapsis_data_visualization.helpers.tags import Tags
|
|
10
11
|
from sinapsis_data_visualization.templates.bbox_drawer import BBoxDrawer
|
|
11
12
|
|
|
13
|
+
KeyPointsDrawerUIProperties = BBoxDrawer.UIProperties
|
|
14
|
+
KeyPointsDrawerUIProperties.tags.extend([Tags.KEYPOINTS])
|
|
15
|
+
|
|
12
16
|
|
|
13
17
|
class KeyPointsDrawer(BBoxDrawer):
|
|
14
18
|
"""
|
|
@@ -48,6 +52,8 @@ class KeyPointsDrawer(BBoxDrawer):
|
|
|
48
52
|
|
|
49
53
|
"""
|
|
50
54
|
|
|
55
|
+
UIProperties = KeyPointsDrawerUIProperties
|
|
56
|
+
|
|
51
57
|
def set_drawing_strategy(self) -> None:
|
|
52
58
|
"""
|
|
53
59
|
Appends the method to draw the keypoints on the image, to
|