Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Øyvind Lind-Johansen committed May 15, 2023
1 parent 3635002 commit fdf8018
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


class LineType(StrEnum):
REALIZATION = "realization"
MEAN = "mean"
REALIZATION = "realizations"
STATISTICS = "statistics"


class ScaleType(StrEnum):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def layout(self) -> List[Component]:
id=self.register_component_unique_id(self.Ids.REALIZATION_OR_MEAN),
options=[
{"label": "Individual realizations", "value": LineType.REALIZATION},
{"label": "Mean over Sensitivities", "value": LineType.MEAN},
{"label": "Mean over Sensitivities", "value": LineType.STATISTICS},
],
value=LineType.REALIZATION,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict, List, Optional, Tuple, Union
import datetime
from typing import Any, Dict, List, Optional, Tuple, Union

import plotly.graph_objects as go
from dash import ALL, Input, Output, State, callback, ctx
Expand Down Expand Up @@ -284,6 +285,18 @@ def _update_vector_store(vector: list) -> str:
),
"value",
),
Output(
self.settings_group_unique_id(
self.Ids.SELECTIONS, Selections.Ids.DATE_SLIDER
),
"max",
),
Output(
self.settings_group_unique_id(
self.Ids.SELECTIONS, Selections.Ids.DATE_SLIDER
),
"marks",
),
Input(
self.settings_group_unique_id(
self.Ids.SELECTIONS, Selections.Ids.ENSEMBLE
Expand All @@ -307,7 +320,7 @@ def _update_date(
ensemble: str,
timeseries_clickdata: Union[None, dict],
dateidx: int,
) -> Tuple[str, str, int]:
) -> Tuple[str, str, int, int, Dict[int, Dict[str, Any]]]:
"""Store selected date and tornado input. Write statistics
to table"""

Expand All @@ -332,6 +345,9 @@ def _update_date(
date_selected = date_from_str(
timeseries_clickdata.get("points", [{}])[0]["x"]
)
if date_selected not in dates:
date_selected = get_closest_date(dates, date_selected)

elif dateslider_drag:
date_selected = dates[dateidx]
else:
Expand All @@ -341,6 +357,14 @@ def _update_date(
date_to_str(date_selected),
date_to_str(date_selected),
dates.index(date_selected),
len(dates) - 1,
{
idx: {
"label": date_to_str(dates[idx]),
"style": {"white-space": "nowrap"},
}
for idx in [0, len(dates) - 1]
},
)

@callback(
Expand Down Expand Up @@ -399,7 +423,7 @@ def _update_timeseries_figure(
ensemble
),
)
if linetype == LineType.MEAN:
if linetype == LineType.STATISTICS:
data = self._data_model.create_vectors_statistics_df(data)

return self._data_model.create_timeseries_figure(
Expand Down Expand Up @@ -577,3 +601,10 @@ def _display_table_or_realplot(selected_vizualisation: str) -> tuple:
return {
"display": "none" if selected_vizualisation == "table" else "block"
}, {"display": "block" if selected_vizualisation == "table" else "none"}


def get_closest_date(
dates: List[datetime.datetime], date: datetime.datetime
) -> datetime.datetime:
# Returns the closest date to the input date in the dates list.
return min(dates, key=lambda dte: abs(dte - date))

0 comments on commit fdf8018

Please sign in to comment.