Skip to content

Commit

Permalink
WID-223: add connectivity to global context and add operations buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbacter01 committed Sep 14, 2023
1 parent 2eba9ff commit dc2cdd1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 14 deletions.
41 changes: 33 additions & 8 deletions tvbwidgets/ui/connectivity_ipy/global_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# (c) 2022-2023, TVB Widgets Team
#
from typing import Callable
from tvb.datatypes.connectivity import Connectivity


class SingletonMeta(type):
Expand All @@ -22,21 +23,45 @@ class GlobalContext(metaclass=SingletonMeta):

def __init__(self):
self._matrix = 'weights'
self._connectivity = None
self.connectivities_history = [] # list of connectivities previously used

@property
def matrix(self):
return self._matrix

@matrix.setter
def matrix(self, value):
old_value = self._matrix
self._matrix = value
if old_value == value:
return
def matrix(self, next_value):
prev_value = self._matrix
self._matrix = next_value
if prev_value != next_value:
self.__notify_observers('matrix', next_value)

@property
def connectivity(self):
# type: () -> Connectivity
return self._connectivity

@connectivity.setter
def connectivity(self, next_value):
# type: (Connectivity) -> None
previous = self._connectivity
self._connectivity = next_value
if previous != next_value:
self.__notify_observers('connectivity', next_value)
if not any([conn.gid == next_value.gid for conn in self.connectivities_history]):
self.connectivities_history.append(next_value)

def __notify_observers(self, observed_attribute, next_value):
# type: (str, any) -> None
"""
Calls all the observer functions of the provided observed attribute
passing as argument the next value for the attribute
"""
try:
observers = self._observed_state['matrix']
for observer in observers:
observer(value)
observers = self._observed_state[observed_attribute]
for obs in observers:
obs(next_value)
except KeyError:
pass

Expand Down
52 changes: 46 additions & 6 deletions tvbwidgets/ui/connectivity_ipy/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ def add_datatype(self, datatype):
pass

def __init__(self, connectivity, **kwargs):
super().__init__(layout={**self.DEFAULT_BORDER, 'width': '50%'}, **kwargs)
super().__init__(layout={**self.DEFAULT_BORDER, 'width': '50%', 'justify-content': 'start'}, **kwargs)
self.connectivity = connectivity
self.regions_checkboxes = []
selector = self.__get_node_selector()
buttons = self.__get_operations_buttons()
children = [
ipywidgets.HTML(
value=f'<h3>Operations for Connectivity-{connectivity.number_of_regions}</h3>'
), selector]
), selector, buttons]
self.children = children
self.regions_checkboxes = []

def __get_node_selector(self):
left_children = []
right_children = []
region_labels = self.connectivity.region_labels
# print('region labels: ', region_labels)

for region in region_labels:
label = str(region)
selector = ipywidgets.Checkbox(value=False, description=label, layout={'width': 'max-content'},
Expand Down Expand Up @@ -72,10 +73,49 @@ def on_ctx_change(value):
container = ipywidgets.VBox(children=(matrix_dropdown,
ipywidgets.HBox(children=(left, right))))
accordion = ipywidgets.Accordion(children=[container], selected_index=None,
layout={'height': '50vh'})
layout={'max-height': '50vh'})
accordion.set_title(0, 'Regions selector')
return accordion

@property
def selected_regions(self):
return map(lambda x: x.description, filter(lambda x: x.value, self.regions_checkboxes))
print(self.regions_checkboxes)
return list(map(lambda x: x.description, filter(lambda x: x.value, self.regions_checkboxes)))

def __get_operations_buttons(self):
cut_selected_tooltip = """
Create a new connectivity removing the selected nodes.
Check the selected nodes in the above dropdown to see what it is included
"""
cut_edges_tooltip = """
Create a new connectivity cutting the edges of selected nodes.
Check the selected nodes in the above dropdown to see what it is included
"""
cut_selected = ipywidgets.Button(description='Cut selected regions',
disabled=False,
button_style='success',
tooltip=cut_selected_tooltip,
icon='scissors')

cut_edges_of_selected = ipywidgets.Button(description='Cut edges of selected',
disabled=False,
button_style='warning',
tooltip=cut_edges_tooltip,
icon='scissors')

cut_selected.on_click(lambda *args: self.__cut_selected_nodes())
cut_edges_of_selected.on_click(lambda *args: self.__cut_selected_edges())

return ipywidgets.HBox(children=[cut_selected, cut_edges_of_selected])

def __cut_selected_nodes(self):
print('cutting selected: ', self.selected_regions)

def __cut_selected_edges(self):
print('cutting edges: ', self.selected_regions)

def __cut_incoming_edges(self):
pass

def _cut_outgoing_edges(self):
pass

0 comments on commit dc2cdd1

Please sign in to comment.