From 2c33cad3f19ffbbfcc8f6fa25344110343fc4a7e Mon Sep 17 00:00:00 2001 From: Walter Baccinelli Date: Thu, 4 Apr 2024 14:11:37 +0200 Subject: [PATCH] common function for slidebar --- eit_dash/callbacks/load_callbacks.py | 18 +++++------ eit_dash/callbacks/preprocessing_callbacks.py | 13 +++----- eit_dash/utils/common.py | 30 +++++++++++++++---- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/eit_dash/callbacks/load_callbacks.py b/eit_dash/callbacks/load_callbacks.py index 9ac98f3..015dbcc 100644 --- a/eit_dash/callbacks/load_callbacks.py +++ b/eit_dash/callbacks/load_callbacks.py @@ -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 @@ -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: diff --git a/eit_dash/callbacks/preprocessing_callbacks.py b/eit_dash/callbacks/preprocessing_callbacks.py index 5516eca..448e1f3 100644 --- a/eit_dash/callbacks/preprocessing_callbacks.py +++ b/eit_dash/callbacks/preprocessing_callbacks.py @@ -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 @@ -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: diff --git a/eit_dash/utils/common.py b/eit_dash/utils/common.py index 62fac5d..67768b0 100644 --- a/eit_dash/utils/common.py +++ b/eit_dash/utils/common.py @@ -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( @@ -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. @@ -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: @@ -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