Skip to content

Commit

Permalink
common function for slidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
wbaccinelli committed Apr 4, 2024
1 parent 0bc4298 commit 2c33cad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
18 changes: 8 additions & 10 deletions eit_dash/callbacks/load_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
import eit_dash.definitions.element_ids as ids
from eit_dash.app import data_object
from eit_dash.definitions.option_lists import InputFiletypes
from eit_dash.utils.common import create_slider_figure, get_signal_options
from eit_dash.utils.common import (
create_slider_figure,
get_signal_options,
get_selections_slidebar,
)

file_data: Sequence | None = None

Expand Down Expand Up @@ -196,15 +200,9 @@ def show_info(
if file_data:
if slidebar_stat is not None:
# TODO: the following is used also in preprocessing. Refactor to avoid duplications
if "xaxis.range" in slidebar_stat:
start_sample = slidebar_stat["xaxis.range"][0]
stop_sample = slidebar_stat["xaxis.range"][1]
elif ("xaxis.range[0]" in slidebar_stat) and (
"xaxis.range[1]" in slidebar_stat
):
start_sample = slidebar_stat["xaxis.range[0]"]
stop_sample = slidebar_stat["xaxis.range[1]"]
else:
start_sample, stop_sample = get_selections_slidebar(slidebar_stat)

if not start_sample or not stop_sample:
start_sample = file_data.eit_data["raw"].time[0]
stop_sample = file_data.eit_data["raw"].time[-1]
else:
Expand Down
13 changes: 4 additions & 9 deletions eit_dash/callbacks/preprocessing_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
create_slider_figure,
get_signal_options,
mark_selected_periods,
get_selections_slidebar,
)

# ruff: noqa: D103 #TODO remove this line when finalizing this module
Expand Down Expand Up @@ -359,15 +360,9 @@ def select_period(
data = data_object.get_sequence_at(int(dataset))

if slidebar_stat is not None:
if "xaxis.range" in slidebar_stat:
start_sample = slidebar_stat["xaxis.range"][0]
stop_sample = slidebar_stat["xaxis.range"][1]
elif ("xaxis.range[0]" in slidebar_stat) and (
"xaxis.range[1]" in slidebar_stat
):
start_sample = slidebar_stat["xaxis.range[0]"]
stop_sample = slidebar_stat["xaxis.range[1]"]
else:
start_sample, stop_sample = get_selections_slidebar(slidebar_stat)

if not start_sample or not stop_sample:
start_sample = data.time[0]
stop_sample = data.time[-1]
else:
Expand Down
30 changes: 25 additions & 5 deletions eit_dash/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ def create_slider_figure(
eit_variants = ["raw"]

for eit_variant in eit_variants:
figure.add_trace(go.Scatter(x=dataset.eit_data[eit_variant].time,
y=dataset.eit_data[eit_variant].global_impedance,
name=eit_variant))
figure.add_trace(
go.Scatter(
x=dataset.eit_data[eit_variant].time,
y=dataset.eit_data[eit_variant].global_impedance,
name=eit_variant,
)
)

for n, cont_signal in enumerate(continuous_data):
figure.add_trace(
Expand Down Expand Up @@ -93,7 +97,8 @@ def create_slider_figure(


def mark_selected_periods(
original_figure: go.Figure | dict, periods: List[Sequence],
original_figure: go.Figure | dict,
periods: List[Sequence],
) -> go.Figure:
"""
Create the figure for the selection of range.
Expand Down Expand Up @@ -137,7 +142,9 @@ def mark_selected_periods(
return original_figure


def get_signal_options(dataset: Sequence, show_eit: bool = False) -> list[dict[str, int | str]]:
def get_signal_options(
dataset: Sequence, show_eit: bool = False
) -> list[dict[str, int | str]]:
"""Get the options for signal selection to be shown in the signal selection section.
Args:
Expand All @@ -158,3 +165,16 @@ def get_signal_options(dataset: Sequence, show_eit: bool = False) -> list[dict[s
options.append({"label": cont, "value": len(options)})

return options


def get_selections_slidebar(slidebar_stat: dict) -> tuple:
if "xaxis.range" in slidebar_stat:
start_sample = slidebar_stat["xaxis.range"][0]
stop_sample = slidebar_stat["xaxis.range"][1]
elif ("xaxis.range[0]" in slidebar_stat) and ("xaxis.range[1]" in slidebar_stat):
start_sample = slidebar_stat["xaxis.range[0]"]
stop_sample = slidebar_stat["xaxis.range[1]"]
else:
start_sample = stop_sample = None

return start_sample, stop_sample

0 comments on commit 2c33cad

Please sign in to comment.