Skip to content

Commit

Permalink
updating code to allow more general specification of default visualiz…
Browse files Browse the repository at this point in the history
…ation params
  • Loading branch information
dylansdaniels committed Nov 20, 2024
1 parent 0fde5c0 commit 4edf730
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 20 deletions.
31 changes: 19 additions & 12 deletions hnn_core/gui/_viz_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,11 +549,12 @@ def _clear_axis(b, widgets, data, fig_idx, fig, ax, widgets_plot_type,
_dynamic_rerender(fig)


def _get_ax_control(widgets, data, default_smoothing, fig_idx, fig, ax):
def _get_ax_control(widgets, data, fig_default_params, fig_idx, fig, ax):
analysis_style = {'description_width': '200px'}
layout = Layout(width="98%")
simulation_names = tuple(data['simulations'].keys())
sim_index = 0
default_smoothing = fig_default_params['default_smoothing']
if not simulation_names:
simulation_names = ("None",)
else:
Expand Down Expand Up @@ -761,12 +762,12 @@ def _close_figure(b, widgets, data, fig_idx):
display(Label(_fig_placeholder))


def _add_axes_controls(widgets, data, default_smoothing, fig, axd):
def _add_axes_controls(widgets, data, fig_default_smoothing, fig, axd):
fig_idx = data['fig_idx']['idx']

controls = Tab()
children = [
_get_ax_control(widgets, data, default_smoothing, fig_idx=fig_idx,
_get_ax_control(widgets, data, fig_default_smoothing, fig_idx=fig_idx,
fig=fig, ax=ax)
for ax_key, ax in axd.items()
]
Expand All @@ -787,7 +788,7 @@ def _add_axes_controls(widgets, data, default_smoothing, fig, axd):
widgets['axes_config_tabs'].set_title(n_tabs, _idx2figname(fig_idx))


def _add_figure(b, widgets, data, default_smoothing,
def _add_figure(b, widgets, data, fig_default_smoothing,
template_type, scale=0.95, dpi=96):
fig_idx = data['fig_idx']['idx']
viz_output_layout = data['visualization_output']
Expand Down Expand Up @@ -820,7 +821,7 @@ def _add_figure(b, widgets, data, default_smoothing,
else:
display(fig.canvas)

_add_axes_controls(widgets, data, default_smoothing, fig=fig, axd=axd)
_add_axes_controls(widgets, data, fig_default_smoothing, fig=fig, axd=axd)

data['figs'][fig_idx] = fig
widgets['figs_tabs'].selected_index = n_tabs
Expand Down Expand Up @@ -871,10 +872,10 @@ class _VizManager:
A dict of external simulation data object
"""

def __init__(self, gui_data, viz_layout, default_smoothing):
def __init__(self, gui_data, viz_layout, fig_defaults):
plt.close("all")
self.viz_layout = viz_layout
self.default_smoothing = default_smoothing
self.fig_defaults = fig_defaults
self.use_ipympl = 'ipympl' in matplotlib.get_backend()

self.axes_config_output = Output()
Expand Down Expand Up @@ -905,9 +906,10 @@ def __init__(self, gui_data, viz_layout, default_smoothing):
button_style="primary",
style={'button_color': self.viz_layout['theme_color']},
layout=self.viz_layout['btn'])
self.make_fig_button.on_click(
lambda b: self.add_figure(self.default_smoothing)
)
# self.make_fig_button.on_click(
# lambda b: self.add_figure(self.default_smoothing)
# )
self.make_fig_button.on_click(self.add_figure)

self.datasets_dropdown = Dropdown(
description='Dataset:',
Expand Down Expand Up @@ -942,6 +944,11 @@ def data(self):
"figs": self.figs
}

@property
def fig_default_params(self):
"""Expose default visualization parameters for figures"""
return self.fig_defaults

def reset_fig_config_tabs(self, template_name=None):
"""Reset the figure config tabs with most recent simulation data."""
simulation_names = tuple(self.data['simulations'].keys())
Expand Down Expand Up @@ -1009,7 +1016,7 @@ def _layout_template_change(self, template_type):
self.datasets_dropdown.layout.visibility = "hidden"

@unlink_relink(attribute='figs_config_tab_link')
def add_figure(self, default_smoothing, b=None):
def add_figure(self, b=None):
"""Add a figure and corresponding config tabs to the dashboard.
"""
if len(self.data["simulations"]) == 0:
Expand All @@ -1034,7 +1041,7 @@ def add_figure(self, default_smoothing, b=None):
_add_figure(None,
self.widgets,
self.data,
default_smoothing,
self.fig_default_params,
template_type,
scale=0.97,
dpi=self.viz_layout['dpi'])
Expand Down
32 changes: 24 additions & 8 deletions hnn_core/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,16 @@ def __init__(self, theme_color="#802989",
# In-memory storage of all simulation and visualization related data
self.simulation_data = defaultdict(lambda: dict(net=None, dpls=list()))

# Simulation parameters
# Default vizualization params for figures
self.widget_default_smoothing = BoundedFloatText(
value=30.0, description='Smoothing:',
min=0.0, max=100.0, step=1.0, disabled=False)

self.fig_default_params = {
'default_smoothing': self.widget_default_smoothing.value
}

# Simulation parameters
self.widget_tstop = BoundedFloatText(
value=170, description='tstop (ms):', min=0, max=1e6, step=1,
disabled=False)
Expand Down Expand Up @@ -480,7 +486,7 @@ def _init_ui_components(self):
self._log_out = Output()

self.viz_manager = _VizManager(self.data, self.layout,
self.widget_default_smoothing.value)
self.fig_default_params)

# detailed configuration of backends
self._backend_config_out = Output()
Expand Down Expand Up @@ -520,6 +526,11 @@ def data(self):
"""Provides easy access to simulation-related data."""
return {"simulation_data": self.simulation_data}

# @property
# def fig_default_params(self):
# """Expose default visualization parameters for figures"""
# return {"fig_default_params": self.fig_default_params}

@staticmethod
def load_parameters(params_fname):
"""Read parameters from file."""
Expand Down Expand Up @@ -569,7 +580,7 @@ def _run_button_clicked(b):
return run_button_clicked(
self.widget_simulation_name, self._log_out, self.drive_widgets,
self.data, self.widget_dt, self.widget_tstop,
self.widget_default_smoothing,
self.fig_default_params, self.widget_default_smoothing,
self.widget_ntrials, self.widget_backend_selection,
self.widget_mpi_cmd, self.widget_n_jobs, self.params,
self._simulation_status_bar, self._simulation_status_contents,
Expand Down Expand Up @@ -1759,7 +1770,7 @@ def on_upload_data_change(change, data, viz_manager, log_out):
# Create a dipole plot
_template_name = "[Blank] single figure"
viz_manager.reset_fig_config_tabs(template_name=_template_name)
viz_manager.add_figure(default_smoothing=0)
viz_manager.add_figure()
fig_name = _idx2figname(viz_manager.data['fig_idx']['idx'] - 1)
process_configs = {'dipole_smooth': 0, 'dipole_scaling': 1}
viz_manager._simulate_edit_figure(fig_name,
Expand Down Expand Up @@ -1915,7 +1926,8 @@ def _init_network_from_widgets(params, dt, tstop, single_simulation_data,


def run_button_clicked(widget_simulation_name, log_out, drive_widgets,
all_data, dt, tstop, widget_default_smoothing,
all_data, dt, tstop,
fig_default_params, widget_default_smoothing,
ntrials, backend_selection,
mpi_cmd, n_jobs, params, simulation_status_bar,
simulation_status_contents, connectivity_textfields,
Expand Down Expand Up @@ -1966,9 +1978,13 @@ def run_button_clicked(widget_simulation_name, log_out, drive_widgets,
simulations_list_widget.value = sim_names[0]

viz_manager.reset_fig_config_tabs()
default_smoothing = widget_default_smoothing.value
viz_manager.default_smoothing = default_smoothing
viz_manager.add_figure(default_smoothing=default_smoothing)
# set default_smoothing in fig_default_params
display(widget_default_smoothing.value)
fig_default_params['default_smoothing'] = widget_default_smoothing.value
# change default smoothing in viz_manager
new_default_smoothing = fig_default_params['default_smoothing']
viz_manager.fig_default_params['default_smoothing'] = new_default_smoothing
viz_manager.add_figure()
fig_name = _idx2figname(viz_manager.data['fig_idx']['idx'] - 1)
ax_plots = [("ax0", "input histogram"), ("ax1", "current dipole")]
for ax_name, plot_type in ax_plots:
Expand Down
43 changes: 43 additions & 0 deletions hnn_core/tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,3 +1116,46 @@ def test_delete_single_drive(setup_gui):
'alpha_prox (proximal)',
'poisson (proximal)',
'tonic')


def test_default_smoothing(setup_gui):
"""Tests default smoothing is inherited correctly"""
import pdb # pdb

gui = setup_gui
gui.run_button.click()

gui.widget_simulation_name.value = 'no_smoothing'
gui.widget_default_smoothing.value = 0

gui.run_button.click()

pdb.set_trace() # pdb ##################################################

# gui._simulate_viz_action("switch_fig_template", "[Blank] single figure")
# gui._simulate_viz_action("add_fig")
# figid = 2
# figname = f'Figure {figid}'
# axname = 'ax0'

# _dipole_plot_types = [
# 'current dipole',
# 'layer2 dipole',
# 'layer5 dipole',
# ]

# for viz_type in _dipole_plot_types:
# gui._simulate_viz_action("edit_figure", figname,
# axname, 'default', viz_type, {}, 'clear')

# # Check that extra axes have been successfully removed
# assert len(gui.viz_manager.figs[figid].axes) == 1
# # Check if data on the axes has been successfully cleared
# assert not gui.viz_manager.figs[figid].axes[0].has_data()

# gui._simulate_viz_action("edit_figure", figname,
# axname, 'default', viz_type, {}, 'plot')
# # Check if data is plotted on the axes
# assert gui.viz_manager.figs[figid].axes[0].has_data()

plt.close('all')

0 comments on commit 4edf730

Please sign in to comment.