diff --git a/Dashboard.ipynb b/Dashboard.ipynb index 20cdfb5..d2ac6ae 100644 --- a/Dashboard.ipynb +++ b/Dashboard.ipynb @@ -199,7 +199,6 @@ "plt.show()\n", "\n", "dashboard = ExperimentDashboard(\n", - " selected_experiment_path, \n", " confusion_matrices, \n", " experiment.partitions,\n", " experiment.training_engine.graph,\n", diff --git a/dnotebooks/plots/confusion_matrix_utils.py b/dnotebooks/plots/confusion_matrix_utils.py index dee4c17..f0e49ca 100644 --- a/dnotebooks/plots/confusion_matrix_utils.py +++ b/dnotebooks/plots/confusion_matrix_utils.py @@ -6,6 +6,7 @@ from networkx.algorithms import descendants_at_distance from dengine.analysis import ExperimentConfusionMatrix +from dnotebooks.widgets import ExperimentGroup def reduce_n_hop_normalized_confusion_matrices( @@ -26,7 +27,15 @@ def reduce_n_hop_normalized_confusion_matrices( try: cfs = [confusion_matrices.device_view(X).confusion_matrix(normalize=True) for X in k_hop_neighbors] except Exception: - logging.warning(f"No confusion found for {device}") + logging.warning( + f"No metric found at index {device}. " + f"This warning may be caused by aggregated experiment seeds. When loading multiple experiments via " + f"{ExperimentGroup.__name__}, node metrics for each experiment are initially aggregated by mean. " + "Different seeds are then stored internally as separate nodes in a fictitious graph. " + "If multiple experiment seeds are loaded, this warning can safely be ignored. " + "Future versions will handle this use case automatically." + ) + continue mean_cfs = reduction_fun(np.stack(cfs)) devices_n_hops_neighbors_mean_cf.append(mean_cfs) return ExperimentConfusionMatrix( diff --git a/dnotebooks/widgets/confusion_matrix.py b/dnotebooks/widgets/confusion_matrix.py index a136ea8..6a0e15f 100644 --- a/dnotebooks/widgets/confusion_matrix.py +++ b/dnotebooks/widgets/confusion_matrix.py @@ -31,7 +31,6 @@ class ExperimentDashboard: def __init__( self, - experiment_root_path: Path, confusion_matrices: Optional[ExperimentConfusionMatrix], partitions: ExperimentPartitions, graph: Graph, diff --git a/dnotebooks/widgets/latent_space.py b/dnotebooks/widgets/latent_space.py index f317469..01ce2bc 100644 --- a/dnotebooks/widgets/latent_space.py +++ b/dnotebooks/widgets/latent_space.py @@ -140,6 +140,7 @@ def __init__( D: SupervisedDataset, activations, labels: dict[int, str] | None = None, + labels_color: dict[int, str] | None = None, title: str = "", highlight_labels: set[int] | None = None, output_directory: Path = Path('res/'), @@ -160,16 +161,25 @@ def __init__( self._exception_output = Output() self._label_names = labels or {} + self._label_colors = labels_color or {} self._data = _get_data(D) self._labels = _get_labels(D) self._unique_labels = np.unique(self._labels) self._fig.canvas.mpl_connect('button_press_event', self._onclick_print_execpt) - def _label_text(self, label): + def _label_text(self, label) -> str: if label in self._label_names: - return f"{self._label_names[label]} ({label})" + return f"{label} ({self._label_names[label]})" return f"{label}" + def _label_color(self, label) -> Optional[str]: + is_highlighted = not self._highlight_labels or label in self._highlight_labels + if not is_highlighted: + return "gray" + if label in self._label_colors: + return self._label_colors[label] + return None + def draw(self, highlight_idx=None, img=None): self._ax.cla() @@ -183,11 +193,11 @@ def draw(self, highlight_idx=None, img=None): label=self._label_text(label), alpha=0.3 if is_highlighted else 0.08, edgecolors="red" if (label < 0) else "none", - color="gray" if not is_highlighted else None, + color=self._label_color(label), s=32 ) - if highlight_idx is not None: + if highlight_idx is not None: # Handles onclick over a sample self._ax.scatter( self._embedding[highlight_idx, 0], self._embedding[highlight_idx, 1], @@ -265,7 +275,7 @@ def dump(self, *args, **kwargs): filename = f"{self._title}.png" self._ax.set_title(self._title) - self._fig.savefig(filename, bbox_inches="tight", pad_inches=0, transparent=True, dpi=300) + self._fig.savefig(self._output_dir / filename, bbox_inches="tight", pad_inches=0, transparent=True, dpi=300) class UmapWidget: @@ -274,6 +284,7 @@ def __init__( D: SupervisedDataset, activations, labels: dict[int, str] | None = None, + labels_colors: dict[int, str] | None = None, title: str = "", highlight_labels: set[int] | None = None, ): @@ -284,7 +295,8 @@ def __init__( labels=labels, title=title, highlight_labels=highlight_labels, - output_directory=self._output_dir + output_directory=self._output_dir, + labels_color=labels_colors ) self._dump_button = Button(description='Save all figures') self._dump_button.on_click(self.dump) diff --git a/dnotebooks/widgets/selection.py b/dnotebooks/widgets/selection.py index 480b691..065415d 100644 --- a/dnotebooks/widgets/selection.py +++ b/dnotebooks/widgets/selection.py @@ -37,7 +37,11 @@ def aggregate_mean(self) -> ExperimentMetric: devices_confusion_matrices=m0.data, description=m0.description ) - stacked_means = np.stack([m.mean() for m in self.metrics]) + + metrics_mean = [m.mean() for m in self.metrics] + metrix_epoch = min([x.shape[0] for x in metrics_mean]) + metrics_truncated_mean = [x[:metrix_epoch] for x in metrics_mean] + stacked_means = np.stack(metrics_truncated_mean) return ExperimentConfusionMatrix( devices_confusion_matrices=stacked_means, description=m0.description,