Skip to content

Commit

Permalink
Use a helper function for min/max of arrays
Browse files Browse the repository at this point in the history
The spread operator has the potential to be expensive for large arrays. Use a
for-loop helper function to find the min and max instead.

Signed-off-by: Brianna Major <[email protected]>
  • Loading branch information
bnmajor committed Sep 1, 2023
1 parent 31ebe25 commit 2205035
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
13 changes: 7 additions & 6 deletions client/src/components/widgets/DownloadOptions/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from "lodash";
import { mapGetters, mapMutations } from "vuex";
import { extractRange } from "../../../utils/helpers";

export default {
inject: ["girderRest"],
Expand Down Expand Up @@ -61,17 +62,17 @@ export default {
},
minStep() {
if (this.id) {
return Math.min(
...this.$store.getters[`${this.id}/PLOT_AVAILABLE_TIME_STEPS`],
);
let ats = this.$store.getters[`${this.id}/PLOT_AVAILABLE_TIME_STEPS`];
let [min] = extractRange(ats);
return min;
}
return 0;
},
maxStep() {
if (this.id) {
return Math.max(
...this.$store.getters[`${this.id}/PLOT_AVAILABLE_TIME_STEPS`],
);
let ats = this.$store.getters[`${this.id}/PLOT_AVAILABLE_TIME_STEPS`];
let [, max] = extractRange(ats);
return max;
}
return 0;
},
Expand Down
11 changes: 5 additions & 6 deletions client/src/components/widgets/PlotlyPlot/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isEmpty, isEqual, isNil } from "lodash";
import { mapActions, mapGetters, mapMutations } from "vuex";
import { PlotType } from "../../../utils/constants";
import Annotations from "../Annotations";
import { extractRange } from "../../../utils/helpers";

//-----------------------------------------------------------------------------
// Utility Functions
Expand Down Expand Up @@ -313,9 +314,9 @@ export default {
if (this.runGlobals) this.useRunGlobals(image);
if (this.zoom) this.applyZoom(image);
const data = image.data[0];
const xRange = [Math.min(...data.x), Math.max(...data.y)];
const xRange = extractRange(data.x);
let yRange = this.range;
if (!yRange) yRange = [Math.min(...data.y), Math.max(...data.y)];
if (!yRange) yRange = extractRange(data.y);
this.currentRange = [...xRange, ...yRange];
this.setAnnotations(image.data[0]);
this.updatePlotDetails(image);
Expand All @@ -330,10 +331,8 @@ export default {
this.averagingValues = [];
}
} else {
let end = Math.min(
this.currentTimeStep + this.timeAverage,
Math.max(...this.availableTimeSteps),
);
let [, max] = extractRange(this.availableTimeSteps);
let end = Math.min(this.currentTimeStep + this.timeAverage, max);
this.avgAnnotation = `Averaging Over Time Steps ${this.currentTimeStep} - ${end}`;
this.averagingValues = [];
for (
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/widgets/Plots/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PlotType } from "../../../utils/constants";
import { PlotFetcher } from "../../../utils/plotFetcher";

import plot from "../../../store/plot";
import { extractRange } from "../../../utils/helpers";

// // Number of timesteps to prefetch data for.
// const TIMESTEPS_TO_PREFETCH = 3;
Expand Down Expand Up @@ -276,7 +277,8 @@ export default {
);
await this.plotFetcher.fetchTimeStepFn(response, firstAvailableStep);

this.setMaxTimeStep(Math.max(this.maxTimeStep, Math.max(...ats)));
let [, max] = extractRange(ats);
this.setMaxTimeStep(Math.max(this.maxTimeStep, max));
this.setItemId(this.itemId);
this.setInitialLoad(false);
this.$refs[`${this.row}-${this.col}`].react();
Expand Down
9 changes: 3 additions & 6 deletions client/src/components/widgets/VTKPlot/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import vtkPolyData from "@kitware/vtk.js/Common/DataModel/PolyData";
import vtkRenderer from "@kitware/vtk.js/Rendering/Core/Renderer";
import vtkScalarBarActor from "@kitware/vtk.js/Rendering/Core/ScalarBarActor";
import vtkCustomCubeAxesActor from "../../../utils/vtkCustomCubeAxesActor";
import { extractRange } from "../../../utils/helpers";

const Y_AXES_LABEL_BOUNDS_ADJUSTMENT = 0.001;
const MESH_XAXIS_SCALE_OFFSET = 0.1;
Expand Down Expand Up @@ -698,12 +699,8 @@ export default {
this.renderer.resetCamera(bounds);
},
actorScale(xVals, yVals) {
const [x0, x1] = this.runGlobals
? this.xRange
: [Math.min(...xVals), Math.max(...xVals)];
const [y0, y1] = this.runGlobals
? this.yRange
: [Math.min(...yVals), Math.max(...yVals)];
const [x0, x1] = this.runGlobals ? this.xRange : extractRange(xVals);
const [y0, y1] = this.runGlobals ? this.yRange : extractRange(yVals);
this.currentRange = [x0, x1, y0, y1];
return (y1 - y0) / (x1 - x0);
},
Expand Down
6 changes: 3 additions & 3 deletions client/src/store/plot.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { extractRange } from "../utils/helpers";

export default {
namespaced: true,
state: () => ({
Expand Down Expand Up @@ -111,9 +113,7 @@ export default {
let newMax = -Infinity;
rootGetters.VIEW_SELECTIONS.forEach((id) => {
let steps = rootGetters[`${id}/PLOT_AVAILABLE_TIME_STEPS`] || [];
let tsMin = Math.min(...steps);
let tsMax = Math.max(...steps);

let [tsMin, tsMax] = extractRange(steps);
newMin = Math.min(newMin, tsMin);
newMax = Math.max(newMax, tsMax);
});
Expand Down
13 changes: 13 additions & 0 deletions client/src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function extractRange(array) {
let minValue = array[0];
let maxValue = array[0];
for (let i = 0; i < array.length; i++) {
const v = array[i];
if (v < minValue) {
minValue = v;
} else if (v > maxValue) {
maxValue = v;
}
}
return [minValue, maxValue];
}

0 comments on commit 2205035

Please sign in to comment.