Skip to content

Commit

Permalink
WID-223: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbacter01 committed Aug 25, 2023
1 parent 706fb0f commit bd73016
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
18 changes: 9 additions & 9 deletions notebooks/Connectivity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
],
Expand All @@ -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": {},
Expand Down
55 changes: 36 additions & 19 deletions tvbwidgets/ui/connectivity_ipy/connectivity_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

pyvista.set_jupyter_backend('pythreejs')

DROPDOWN_KEY = 'dropdown'


@dataclasses.dataclass
class ConnectivityConfig:
Expand All @@ -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
Expand All @@ -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

0 comments on commit bd73016

Please sign in to comment.