diff --git a/notebooks/Connectivity.ipynb b/notebooks/Connectivity.ipynb index 6905653..f515bf2 100644 --- a/notebooks/Connectivity.ipynb +++ b/notebooks/Connectivity.ipynb @@ -18,12 +18,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "25-08-2023 12:20:46 - DEBUG - tvbwidgets - Package is not fully installed\n", - "25-08-2023 12:20:46 - DEBUG - tvbwidgets - Version read from the internal package.json file\n", - "25-08-2023 12:20:46 - INFO - tvbwidgets - Version: 1.5.0\n", - "2023-08-25 12:20:52,653 - INFO - tvb.storage.h5.encryption.data_encryption_handler - Cannot import syncrypto library.\n", - "25-08-2023 12:20:52 - INFO - tvbwidgets.core.pse.parameters - ImportError: Dask dependency is not included, so this functionality won't be available\n", - "2023-08-25 12:20:52,857 - WARNING - tvb.basic.readers - File 'hemispheres' not found in ZIP.\n" + "25-08-2023 02:41:22 - DEBUG - tvbwidgets - Package is not fully installed\n", + "25-08-2023 02:41:22 - DEBUG - tvbwidgets - Version read from the internal package.json file\n", + "25-08-2023 02:41:22 - INFO - tvbwidgets - Version: 1.5.0\n", + "2023-08-25 14:41:28,344 - INFO - tvb.storage.h5.encryption.data_encryption_handler - Cannot import syncrypto library.\n", + "25-08-2023 02:41:28 - INFO - tvbwidgets.core.pse.parameters - ImportError: Dask dependency is not included, so this functionality won't be available\n", + "2023-08-25 14:41:28,543 - WARNING - tvb.basic.readers - File 'hemispheres' not found in ZIP.\n" ] } ], @@ -46,18 +46,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "2023-08-25 12:20:52,875 - WARNING - tvb.basic.readers - File 'hemispheres' not found in ZIP.\n" + "2023-08-25 14:41:28,570 - WARNING - tvb.basic.readers - File 'hemispheres' not found in ZIP.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "86eda657f9fa4a81a24d9e267faf335b", + "model_id": "b1d4cf7c16d244c1aae087b72e8c3218", "version_major": 2, "version_minor": 0 }, "text/plain": [ - "ConnectivityWidget(children=(Dropdown(description='Matrix:', index=1, options=(('Tracts', 'tracts'), ('Weights…" + "ConnectivityWidget(children=(Connectivity2DViewer(children=(Dropdown(description='Matrix:', index=1, options=(…" ] }, "metadata": {}, diff --git a/tvbwidgets/ui/connectivity_ipy/connectivity_widget.py b/tvbwidgets/ui/connectivity_ipy/connectivity_widget.py index 4a18d64..ca1391e 100644 --- a/tvbwidgets/ui/connectivity_ipy/connectivity_widget.py +++ b/tvbwidgets/ui/connectivity_ipy/connectivity_widget.py @@ -17,6 +17,8 @@ pyvista.set_jupyter_backend('pythreejs') +DROPDOWN_KEY = 'dropdown' + @dataclasses.dataclass class ConnectivityConfig: @@ -32,55 +34,62 @@ class ConnectivityConfig: class CustomOutput(ipywidgets.Output): CONFIG = ConnectivityConfig() - MAX_ACTORS = 10 def __init__(self, **kwargs): super().__init__(**kwargs) self.plotter = matplotlib.pyplot -class ConnectivityWidget(ipywidgets.HBox, TVBWidget): +class Connectivity2DViewer(ipywidgets.VBox, TVBWidget): DROPDOWN_DESCRIPTION = 'Matrix:' - def __init__(self, connectivity): - # type: (Connectivity) -> None + def __init__(self, connectivity, **kwargs): + # type: (Connectivity, dict) -> None """ :param connectivity: Connectivity to view or operate on """ self.connectivity = connectivity - self.output_plot = CustomOutput() + self.output = CustomOutput() + self.widgets_map = dict() - super().__init__([self.output_plot], layout=self.DEFAULT_BORDER) + super().__init__([self.output], layout=self.DEFAULT_BORDER, **kwargs) self.__draw_connectivity() + self.__show_plot() def add_datatype(self, datatype): # type: (HasTraits) -> None pass def __show_plot(self, matrix=None): + # type: (ndarray) -> None + """ + Clears the custom output and draws the connectivity matrix + based on the current selection + """ dropdown = self.__find_dropdown() if not dropdown and matrix is None: - self.logger.error('Non matrix found for plot!') + self.logger.error('No matrix found for plot!') return None dropdown_matrix = self.connectivity.weights if dropdown.value == 'weights' else self.connectivity.tract_lengths matrix = matrix if matrix is not None else dropdown_matrix - with self.output_plot: - self.output_plot.clear_output(wait=True) - self.output_plot.plotter.matshow(matrix) - self.output_plot.plotter.show() + with self.output: + self.output.clear_output(wait=True) + self.output.plotter.matshow(matrix) + self.output.plotter.show() def __find_dropdown(self): # type: () -> ipywidgets.Dropdown | None - for wid in self.children: - try: - if wid.description and wid.description == self.DROPDOWN_DESCRIPTION: - return wid - except AttributeError: - pass - return None + try: + return self.widgets_map[DROPDOWN_KEY] + except KeyError: + return None def __draw_connectivity(self): # type: () -> None + """ + Creates the connectivity matrix dropdown as a child of this widget + """ + def on_change(change): if change['type'] == 'change' and change['name'] == 'value': matrix = self.connectivity.weights if change['new'] == 'weights' else self.connectivity.tract_lengths @@ -92,5 +101,13 @@ def on_change(change): description='Matrix:' ) dropdown.observe(on_change) + self.widgets_map[DROPDOWN_KEY] = dropdown self.children = (dropdown, *self.children) - self.__show_plot() + + +class ConnectivityWidget(ipywidgets.Tab): + def __init__(self, connectivity, **kwargs): + super().__init__(**kwargs) + children = [Connectivity2DViewer(connectivity)] + self.set_title(0, 'Connectivity 2D Viewer') + self.children = children