Skip to content

Commit

Permalink
Implement down-selection controls for C vs. cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Jul 19, 2023
1 parent 9bd678f commit 5f0a9d4
Showing 1 changed file with 98 additions and 1 deletion.
99 changes: 98 additions & 1 deletion aurora/results/plot/presenters/capacity_cycle.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Tuple
from contextlib import suppress
from typing import List, Tuple

import ipywidgets as ipw
import numpy as np
Expand Down Expand Up @@ -28,6 +29,22 @@ def __init__(

super().__init__(model, view)

_range = ipw.Text(
layout={
"width": "90%",
},
description="range",
placeholder="start:end:step",
)

points = ipw.Text(
layout={
"width": "90%",
},
description="points",
placeholder="e.g 0 5 -1",
)

electrode = ipw.RadioButtons(
layout={},
options=[
Expand All @@ -40,6 +57,8 @@ def __init__(
)

controls = {
'range': _range,
'points': points,
'electrode': electrode,
}

Expand All @@ -55,6 +74,7 @@ def plot_series(self, eid: int, dataset: dict) -> None:
"""docstring"""
x, y = (np.array(a) for a in self.extract_data(dataset))
y *= self.weights[eid].get(self.view.electrode.value, 1)
x, y = self._down_select(x, y)
color = self.model.colors.get(eid)
self.model.ax.plot(x, y, '.', label=eid, color=color)

Expand All @@ -67,11 +87,88 @@ def _set_event_handlers(self) -> None:

super()._set_event_handlers()

self.view.range.observe(
names='value',
handler=self._on_range_change,
)

self.view.points.observe(
names='value',
handler=self._on_points_change,
)

self.view.electrode.observe(
names='value',
handler=self._on_electrode_change,
)

def _on_range_change(self, _=None) -> None:
"""docstring"""

self.view.points.unobserve(
names='value',
handler=self._on_points_change,
)

self.view.points.value = ''

self.view.points.observe(
names='value',
handler=self._on_points_change,
)

self.refresh()

def _on_points_change(self, _=None) -> None:
"""docstring"""

self.view.range.unobserve(
names='value',
handler=self._on_range_change,
)

self.view.range.value = ''

self.view.range.observe(
names='value',
handler=self._on_range_change,
)

self.refresh()

def _on_electrode_change(self, _=None) -> None:
"""docstring"""
self.refresh(skip_x=True)

def _down_select(self, x: np.ndarray, y: np.ndarray) -> Tuple:
"""docstring"""
x, y = self._filter_range(x, y)
x, y = self._filter_points(x, y)
return (x, y)

def _filter_range(self, x: np.ndarray, y: np.ndarray) -> Tuple:
"""docstring"""

start, end, step = None, None, None
args: List[str] = self.view.range.value.split(':')

if args:
with suppress(Exception):
start = int(args[0])

if len(args) > 1:
with suppress(Exception):
end = int(args[1]) + 1 or None

if len(args) > 2:
with suppress(Exception):
step = int(args[2])

return (x[start:end:step], y[start:end:step])

def _filter_points(self, x: np.ndarray, y: np.ndarray) -> Tuple:
"""docstring"""
raw_list: List[str] = self.view.points.value.strip().split()
points = [int(p) for p in raw_list if p.strip('-').isnumeric()]
valid = [p for p in set(points) if -len(x) - 1 <= p < len(x)]
return (x[valid], y[valid]) if valid else (x, y)

0 comments on commit 5f0a9d4

Please sign in to comment.