Skip to content

Commit

Permalink
Better sanitize inputs for plot cycle selector
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Aug 18, 2023
1 parent 090ced9 commit fca8807
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
13 changes: 12 additions & 1 deletion pydatalab/pydatalab/apps/echem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,26 @@ def filter_df_by_cycle_index(
if cycle_list is None:
return df

cycle_list = sorted(i for i in cycle_list if i > 0)

if "half cycle" not in df.columns:
if "cycle index" not in df.columns:
raise ValueError(
"Input dataframe must have either 'half cycle' or 'cycle index' column"
)

if len(cycle_list) == 1 and max(cycle_list) > df["cycle index"].max():
cycle_list[0] = df["cycle index"].max()
return df[df["cycle index"].isin(i for i in cycle_list)]

try:
half_cycles = [i for item in cycle_list for i in [(2 * int(item)) - 1, 2 * int(item)]]
if len(cycle_list) == 1 and 2 * max(cycle_list) > df["half cycle"].max():
cycle_list[0] = df["half cycle"].max() // 2
half_cycles = [
i
for item in cycle_list
for i in [max((2 * int(item)) - 1, df["half cycle"].min()), 2 * int(item)]
]
except ValueError as exc:
raise ValueError(
f"Unable to parse `cycle_list` as integers: {cycle_list}. Error: {exc}"
Expand Down
6 changes: 5 additions & 1 deletion pydatalab/pydatalab/bokeh_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,12 @@ def double_axes_echem_plot(
else:
y = "dV/dQ (V/mA)"

# if filtering has removed all cycles, skip making the plot
if len(df) < 1:
raise RuntimeError("No data remaining to plot after filtering.")

# trim the end of the colour cycle for visibility on a white background
color_space = np.linspace(0.3, 0.7, int(df["half cycle"].max())) # type: ignore
color_space = np.linspace(0.3, 0.7, max(int(df["half cycle"].max()), 1)) # type: ignore

for _, group in grouped_by_half_cycle:
line = plot.line(
Expand Down
10 changes: 10 additions & 0 deletions webapp/src/components/datablocks/CycleBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<input
type="text"
class="form-control"
placeholder="e.g., 1-5, 7, 9-10. Starts at 1."
:class="{ 'is-invalid': cycle_num_error }"
v-model="cyclesString"
@keydown.enter="
Expand Down Expand Up @@ -209,10 +210,19 @@ export default {
for (const section of cycle_string_sections) {
let split_section = section.split("-");
if (split_section.length == 1) {
let value = parseInt(split_section[0]);
if (value < 1) {
this.cycle_num_error = `Invalid input '${cyclesString}', cycle numbers start at 1.`;
return;
}
all_cycles.push(parseInt(split_section[0]));
} else {
let upper_range = parseInt(split_section[1]);
let lower_range = parseInt(split_section[0]);
if (lower_range < 1) {
this.cycle_num_error = `Invalid input '${cyclesString}', cycle numbers start at 1.`;
return;
}
for (
let j = Math.min(lower_range, upper_range);
j <= Math.max(lower_range, upper_range);
Expand Down

0 comments on commit fca8807

Please sign in to comment.