From 702865ed0999daa58e93fa393a29cabc9f51bef1 Mon Sep 17 00:00:00 2001 From: jorgenherje Date: Fri, 15 Sep 2023 14:27:00 +0200 Subject: [PATCH] Refactor ParameterColorScaleHelper - Rename to continuous - improve constructor to remove cast (usage of `as`) --- .../utils/parameterColoringUtils.ts | 34 ++++++------ .../utils/subplotBuilder.ts | 53 +++++++++++-------- .../SimulationTimeSeriesMatrix/view.tsx | 5 +- 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/frontend/src/modules/SimulationTimeSeriesMatrix/utils/parameterColoringUtils.ts b/frontend/src/modules/SimulationTimeSeriesMatrix/utils/parameterColoringUtils.ts index 19625a362..b5d7457d4 100644 --- a/frontend/src/modules/SimulationTimeSeriesMatrix/utils/parameterColoringUtils.ts +++ b/frontend/src/modules/SimulationTimeSeriesMatrix/utils/parameterColoringUtils.ts @@ -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; @@ -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? @@ -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]; } } diff --git a/frontend/src/modules/SimulationTimeSeriesMatrix/utils/subplotBuilder.ts b/frontend/src/modules/SimulationTimeSeriesMatrix/utils/subplotBuilder.ts index 9c2275a27..f09e22e2a 100644 --- a/frontend/src/modules/SimulationTimeSeriesMatrix/utils/subplotBuilder.ts +++ b/frontend/src/modules/SimulationTimeSeriesMatrix/utils/subplotBuilder.ts @@ -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"; @@ -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"; @@ -57,7 +60,7 @@ export class SubplotBuilder { private _defaultScatterType = "scatter"; - private _parameterColorScaleHelper: ParameterColorScaleHelper | null = null; + private _parameterColorScaleHelper: ContinuousParameterColorScaleHelper | null = null; private _parameterFallbackColor = "#808080"; constructor( @@ -66,7 +69,7 @@ export class SubplotBuilder { colorSet: ColorSet, width: number, height: number, - parameterColorScaleHelper?: ParameterColorScaleHelper + parameterColorScaleHelper?: ContinuousParameterColorScaleHelper ) { this._selectedVectorSpecifications = selectedVectorSpecifications; this._width = width; @@ -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 => { return { @@ -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 = { - // ...this._parameterColorScaleHelper.getColorScale().getAsPlotlyColorScaleMarkerObject(), - // colorbar: { - // title: "Parameter range", - // titleside: "right", - // ticks: "outside", - // len: 0.75, - // }, - // }; - // const parameterColorLegendTrace: Partial = { - // x: [null], - // y: [null], - // marker: colorScaleMarker, - // showlegend: false, - // }; - // this._plotData.push(parameterColorLegendTrace); } // Add legend for history trace with legendrank after vectors/ensembles @@ -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 = { + ...this._parameterColorScaleHelper.getColorScale().getAsPlotlyColorScaleMarkerObject(), + colorbar: { + title: "Parameter range", + titleside: "right", + ticks: "outside", + len: 0.75, + }, + }; + const parameterColorLegendTrace: Partial = { + x: [null], + y: [null], + marker: colorScaleMarker, + showlegend: false, + }; + this._plotData.push(parameterColorLegendTrace); + } } addRealizationTracesColoredByParameter( @@ -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 ) @@ -295,6 +300,7 @@ export class SubplotBuilder { yaxis: `y${subplotIndex + 1}`, }); this._plotData.push(vectorRealizationTrace); + this._hasRealizationsTracesColoredByParameter = true; } } } @@ -346,6 +352,7 @@ export class SubplotBuilder { }); this._plotData.push(...vectorRealizationTraces); + this._hasRealizationsTraces = true; }); } diff --git a/frontend/src/modules/SimulationTimeSeriesMatrix/view.tsx b/frontend/src/modules/SimulationTimeSeriesMatrix/view.tsx index 28e9f74c0..873d0fb27 100644 --- a/frontend/src/modules/SimulationTimeSeriesMatrix/view.tsx +++ b/frontend/src/modules/SimulationTimeSeriesMatrix/view.tsx @@ -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, @@ -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