Skip to content

Commit

Permalink
Refactor ParameterColorScaleHelper
Browse files Browse the repository at this point in the history
- Rename to continuous
- improve constructor to remove cast (usage of `as`)
  • Loading branch information
jorgenherje committed Sep 15, 2023
1 parent 2cd43ce commit b847edc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Ensemble } from "@framework/Ensemble";
import { ContinuousParameter, Parameter, ParameterIdent, ParameterType } from "@framework/EnsembleParameters";
import { ContinuousParameter, ParameterIdent, ParameterType } from "@framework/EnsembleParameters";
import { ColorScale } from "@lib/utils/ColorScale";
import { MinMax } from "@lib/utils/MinMax";

export class ParameterColorScaleHelper {
export class ContinuousParameterColorScaleHelper {
private _parameterIdent: ParameterIdent;
private _ensembleContinuousParameterSet: { [ensembleName: string]: Parameter };
private _ensembleContinuousParameterSet: { [ensembleName: string]: ContinuousParameter };
private _minMax: MinMax;
private _colorScale: ColorScale;

Expand All @@ -15,13 +15,13 @@ export class ParameterColorScaleHelper {
this._minMax = MinMax.createInvalid();
for (const ensemble of selectedEnsembles) {
const parameters = ensemble.getParameters();
const continuousParameters = parameters.getParameterIdents(ParameterType.CONTINUOUS);
const hasParameter = continuousParameters.some((elm) => elm.equals(this._parameterIdent));
if (!hasParameter) continue;
if (!parameters.hasParameter(parameterIdent)) continue;

const parameter = parameters.getParameter(parameterIdent);
this._ensembleContinuousParameterSet[ensemble.getEnsembleName()] = parameter;
this._minMax = this._minMax.extendedBy(parameters.getContinuousParameterMinMax(parameterIdent));
if (parameter.type === ParameterType.CONTINUOUS) {
this._ensembleContinuousParameterSet[ensemble.getEnsembleName()] = parameter;
this._minMax = this._minMax.extendedBy(parameters.getContinuousParameterMinMax(parameterIdent));
}
}

// TODO: Set Range [0,0] if parameterMinMax is invalid?
Expand All @@ -43,27 +43,25 @@ export class ParameterColorScaleHelper {
}

hasEnsembleName(ensembleName: string): boolean {
return this._ensembleContinuousParameterSet[ensembleName] !== null;
return ensembleName in this._ensembleContinuousParameterSet;
}

hasParameterRealizationValue(ensembleName: string, realization: number): boolean {
hasParameterRealizationNumericalValue(ensembleName: string, realization: number): boolean {
if (!this.hasEnsembleName(ensembleName)) return false;

const parameter = this._ensembleContinuousParameterSet[ensembleName];
const realizationIndex = parameter.realizations.indexOf(realization);
if (realizationIndex === -1) return false;

return parameter.values[realizationIndex] !== null;
return parameter.realizations.indexOf(realization) !== -1;
}

getParameterRealizationValue(ensembleName: string, realization: number): number {
if (!this.hasParameterRealizationValue(ensembleName, realization)) {
if (!this.hasParameterRealizationNumericalValue(ensembleName, realization)) {
throw new Error(
`Parameter ${this._parameterIdent.toString()} has no value for realization ${realization} in ensemble ${ensembleName}`
`Parameter ${this._parameterIdent.toString()} has no numerical value for realization ${realization} in ensemble ${ensembleName}`
);
}

// This cast `as` relies on the verification of continuous parameter values in the constructor
return (this._ensembleContinuousParameterSet[ensembleName] as ContinuousParameter).values[realization];
const parameter = this._ensembleContinuousParameterSet[ensembleName];
const realizationIndex = parameter.realizations.indexOf(realization);
return parameter.values[realizationIndex];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
createVectorStatisticsTraces,
} from "./PlotlyTraceUtils/createVectorTracesUtils";
import { scaleHexColorLightness } from "./colorUtils";
import { ParameterColorScaleHelper } from "./parameterColoringUtils";
import { ContinuousParameterColorScaleHelper } from "./parameterColoringUtils";
import { TimeSeriesPlotData } from "./timeSeriesPlotData";

import { VectorSpec } from "../state";
Expand Down Expand Up @@ -47,8 +47,11 @@ export class SubplotBuilder {
private _vectorHexColors: HexColorMap = {};
// private _brighten = filterBrightness(1.3, "rgb");

private _hasRealizationsTraces = false;
private _hasRealizationsTracesColoredByParameter = false;
private _hasHistoryTraces = false;
private _hasObservationTraces = false;

private _historyVectorColor = "black";
private _observationColor = "black";

Expand All @@ -57,7 +60,7 @@ export class SubplotBuilder {

private _defaultScatterType = "scatter";

private _parameterColorScaleHelper: ParameterColorScaleHelper | null = null;
private _parameterColorScaleHelper: ContinuousParameterColorScaleHelper | null = null;
private _parameterFallbackColor = "#808080";

constructor(
Expand All @@ -66,7 +69,7 @@ export class SubplotBuilder {
colorSet: ColorSet,
width: number,
height: number,
parameterColorScaleHelper?: ParameterColorScaleHelper
parameterColorScaleHelper?: ContinuousParameterColorScaleHelper
) {
this._selectedVectorSpecifications = selectedVectorSpecifications;
this._width = width;
Expand Down Expand Up @@ -158,7 +161,7 @@ export class SubplotBuilder {
let currentLegendRank = 1;

// Add legend for each vector/ensemble when not coloring by parameter
if (this._parameterColorScaleHelper === null) {
if (this._hasRealizationsTraces) {
// Helper function to create legend trace
const subplotDataLegendTrace = (name: string, hexColor: string): Partial<TimeSeriesPlotData> => {
return {
Expand All @@ -185,24 +188,6 @@ export class SubplotBuilder {
this._plotData.push(subplotDataLegendTrace(ensembleName, this._ensembleHexColors[ensembleName]));
});
}
} else {
// Add color scale for color by parameter
// const colorScaleMarker: Partial<PlotMarker> = {
// ...this._parameterColorScaleHelper.getColorScale().getAsPlotlyColorScaleMarkerObject(),
// colorbar: {
// title: "Parameter range",
// titleside: "right",
// ticks: "outside",
// len: 0.75,
// },
// };
// const parameterColorLegendTrace: Partial<TimeSeriesPlotData> = {
// x: [null],
// y: [null],
// marker: colorScaleMarker,
// showlegend: false,
// };
// this._plotData.push(parameterColorLegendTrace);
}

// Add legend for history trace with legendrank after vectors/ensembles
Expand Down Expand Up @@ -241,6 +226,26 @@ export class SubplotBuilder {

this._plotData.push(observationLegendTrace);
}

// Add color scale for color by parameter
if (this._hasRealizationsTracesColoredByParameter && this._parameterColorScaleHelper !== null) {
const colorScaleMarker: Partial<PlotMarker> = {
...this._parameterColorScaleHelper.getColorScale().getAsPlotlyColorScaleMarkerObject(),
colorbar: {
title: "Parameter range",
titleside: "right",
ticks: "outside",
len: 0.75,
},
};
const parameterColorLegendTrace: Partial<TimeSeriesPlotData> = {
x: [null],
y: [null],
marker: colorScaleMarker,
showlegend: false,
};
this._plotData.push(parameterColorLegendTrace);
}
}

addRealizationTracesColoredByParameter(
Expand Down Expand Up @@ -273,7 +278,7 @@ export class SubplotBuilder {
let parameterColor = this._parameterFallbackColor;
const ensembleName = elm.vectorSpecification.ensembleIdent.getEnsembleName();
if (
this._parameterColorScaleHelper.hasParameterRealizationValue(
this._parameterColorScaleHelper.hasParameterRealizationNumericalValue(
ensembleName,
realizationData.realization
)
Expand All @@ -295,6 +300,7 @@ export class SubplotBuilder {
yaxis: `y${subplotIndex + 1}`,
});
this._plotData.push(vectorRealizationTrace);
this._hasRealizationsTracesColoredByParameter = true;
}
}
}
Expand Down Expand Up @@ -346,6 +352,7 @@ export class SubplotBuilder {
});

this._plotData.push(...vectorRealizationTraces);
this._hasRealizationsTraces = true;
});
}

Expand Down
5 changes: 2 additions & 3 deletions frontend/src/modules/SimulationTimeSeriesMatrix/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import { ModuleFCProps } from "@framework/Module";
import { useEnsembleSet } from "@framework/WorkbenchSession";
import { useElementSize } from "@lib/hooks/useElementSize";
import { ColorScaleGradientType } from "@lib/utils/ColorScale";
import { ColorScale } from "@lib/utils/ColorScale";
// Note: Have for debug render count info
import { isDevMode } from "@lib/utils/devMode";

import { useHistoricalVectorDataQueries, useStatisticalVectorDataQueries, useVectorDataQueries } from "./queryHooks";
import { GroupBy, State, VisualizationMode } from "./state";
import { ParameterColorScaleHelper } from "./utils/parameterColoringUtils";
import { ContinuousParameterColorScaleHelper } from "./utils/parameterColoringUtils";
import { SubplotBuilder, SubplotOwner } from "./utils/subplotBuilder";
import {
createLoadedVectorSpecificationAndDataArray,
Expand Down Expand Up @@ -70,7 +69,7 @@ export const view = ({ moduleContext, workbenchSession, workbenchSettings }: Mod
// Create parameter color scale helper
const doColorByParameter = colorRealizationsByParameter && parameterIdent !== null && selectedEnsembles.length > 0;
const parameterColorScaleHelper = doColorByParameter
? new ParameterColorScaleHelper(parameterIdent, selectedEnsembles, parameterColorScale)
? new ContinuousParameterColorScaleHelper(parameterIdent, selectedEnsembles, parameterColorScale)
: null;

// Queries
Expand Down

0 comments on commit b847edc

Please sign in to comment.