Skip to content

Commit

Permalink
from review
Browse files Browse the repository at this point in the history
  • Loading branch information
HansKallekleiv committed Sep 21, 2023
1 parent a808ce5 commit e025de4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export function settings({ moduleContext, workbenchSession, workbenchServices }:
if (computedVectorName && computedVectorName !== selectedVectorName) {
setSelectedVectorName(computedVectorName);
}
const hasHistorical =
vectorsListQuery.data?.some((vec) => vec.name === computedVectorName && vec.has_historical) ?? false;

if (computedEnsembleIdent && !computedEnsembleIdent.equals(selectedEnsembleIdent)) {
setSelectedEnsembleIdent(computedEnsembleIdent);
Expand All @@ -78,25 +76,26 @@ export function settings({ moduleContext, workbenchSession, workbenchServices }:
},
[selectedEnsembleIdent]
);
const sensitivityOptions: SelectOption[] =
sensitivityNames.map((name) => ({
value: name,
label: name,
})) ?? [];
const sensitivityOptions: SelectOption[] = sensitivityNames.map((name) => ({
value: name,
label: name,
}));

React.useEffect(
function propagateVectorSpecToView() {
if (computedEnsembleIdent && computedVectorName) {
moduleContext.getStateStore().setValue("vectorSpec", {
ensembleIdent: computedEnsembleIdent,
vectorName: computedVectorName,
hasHistorical,
hasHistorical:
vectorsListQuery.data?.some((vec) => vec.name === computedVectorName && vec.has_historical) ??
false,
});
} else {
moduleContext.getStateStore().setValue("vectorSpec", null);
}
},
[computedEnsembleIdent, computedVectorName, hasHistorical]
[computedEnsembleIdent, computedVectorName]
);

function handleEnsembleSelectionChange(newEnsembleIdent: EnsembleIdent | null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ export interface TimeSeriesPlotlyTrace extends Partial<PlotData> {

export function createStatisticalLineTraces(
sensitivityData: VectorStatisticSensitivityData_api[],
statisticsFunction: StatisticFunction_api,
color: string
): TimeSeriesPlotlyTrace[] {
const traces: TimeSeriesPlotlyTrace[] = [];
sensitivityData.forEach((aCase, index) => {
const meanObj = aCase.value_objects.find((obj) => obj.statistic_function === StatisticFunction_api.MEAN);
if (meanObj) {
const statisticObj = aCase.value_objects.find((obj) => obj.statistic_function === statisticsFunction);
if (statisticObj) {
traces.push(
createLineTrace({
timestampsMsUtc: aCase.timestamps_utc_ms,
values: meanObj.values,
values: statisticObj.values,
name: `${aCase.sensitivity_name}`,
legendGroup: `${aCase.sensitivity_name}`,
lineShape: "linear",
Expand Down
49 changes: 24 additions & 25 deletions frontend/src/modules/SimulationTimeSeriesSensitivity/view.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from "react";

import { VectorRealizationData_api, VectorStatisticSensitivityData_api } from "@api";
import { StatisticFunction_api, VectorRealizationData_api, VectorStatisticSensitivityData_api } from "@api";
import { BroadcastChannelData, BroadcastChannelMeta } from "@framework/Broadcaster";
import { ModuleFCProps } from "@framework/Module";
import { useSubscribedValue } from "@framework/WorkbenchServices";
import { timestampUtcMsToCompactIsoString } from "@framework/utils/timestampUtils";
import { useElementSize } from "@lib/hooks/useElementSize";
import { createSensitivityColorMap } from "@modules/_shared/sensitivityColors";

import { indexOf } from "lodash";

Expand Down Expand Up @@ -107,35 +108,33 @@ export const view = ({
const colorSet = workbenchSettings.useColorSet();

const allSensitivityNamesInEnsemble = ensemble?.getSensitivities()?.getSensitivityNames().sort() ?? [];

const traceDataArr: TimeSeriesPlotlyTrace[] = [];
if (ensemble && selectedSensitivities && selectedSensitivities.length > 0) {
// Loop through all available sensitivities to get correct color
allSensitivityNamesInEnsemble.forEach((sensitivityName, index) => {
const color = index === 0 ? colorSet.getFirstColor() : colorSet.getNextColor();

// Work on selected sensitivities only
if (selectedSensitivities.includes(sensitivityName)) {
// Add statistics traces
if (showStatistics && statisticsQuery.data) {
const matchingCases: VectorStatisticSensitivityData_api[] = statisticsQuery.data.filter(
(stat) => stat.sensitivity_name === sensitivityName
const sensitivitiesColorMap = createSensitivityColorMap(allSensitivityNamesInEnsemble, colorSet);
selectedSensitivities.forEach((sensitivityName, index) => {
const color = sensitivitiesColorMap[sensitivityName];

// Add statistics traces
if (showStatistics && statisticsQuery.data) {
const matchingCases: VectorStatisticSensitivityData_api[] = statisticsQuery.data.filter(
(stat) => stat.sensitivity_name === sensitivityName
);
const traces = createStatisticalLineTraces(matchingCases, StatisticFunction_api.MEAN, color);
traceDataArr.push(...traces);
}

// Add realization traces
const sensitivity = ensemble.getSensitivities()?.getSensitivityByName(sensitivityName);
if (showRealizations && realizationsQuery.data && sensitivity) {
for (const sensCase of sensitivity.cases) {
const realsToInclude = sensCase.realizations;
const realizationData: VectorRealizationData_api[] = realizationsQuery.data.filter((vec) =>
realsToInclude.includes(vec.realization)
);
const traces = createStatisticalLineTraces(matchingCases, color);
const traces = createRealizationLineTraces(realizationData, sensitivity.name, color);
traceDataArr.push(...traces);
}

// Add realization traces
const sensitivity = ensemble.getSensitivities()?.getSensitivityByName(sensitivityName);
if (showRealizations && realizationsQuery.data && sensitivity) {
for (const sensCase of sensitivity.cases) {
const realsToInclude = sensCase.realizations;
const realizationData: VectorRealizationData_api[] = realizationsQuery.data.filter((vec) =>
realsToInclude.includes(vec.realization)
);
const traces = createRealizationLineTraces(realizationData, sensitivity.name, color);
traceDataArr.push(...traces);
}
}
}
});
// Add history
Expand Down
20 changes: 10 additions & 10 deletions frontend/src/modules/TornadoChart/sensitivityChart.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useEffect, useState } from "react";
import Plot from "react-plotly.js";

import { SensitivityColorMap } from "@modules/_shared/sensitivityColors";

import { Layout, PlotData, PlotMouseEvent } from "plotly.js";

import { SensitivityResponse, SensitivityResponseDataset } from "./sensitivityResponseCalculator";
Expand All @@ -12,7 +14,7 @@ export type SensitivityColors = {
};
export type sensitivityChartProps = {
sensitivityResponseDataset: SensitivityResponseDataset;
sensitivityColors: SensitivityColors[];
sensitivityColorMap: SensitivityColorMap;
showLabels: boolean;
hideZeroY: boolean;
showRealizationPoints: boolean;
Expand Down Expand Up @@ -107,7 +109,7 @@ const lowTrace = (
sensitivityResponses: SensitivityResponse[],
showLabels: boolean,
selectedBar: SelectedBar | null,
colors: string[]
sensitivitiesColorMap: SensitivityColorMap
): Partial<SensitivityChartTraceData> => {
const label = showLabels ? sensitivityResponses.map((s) => computeLowLabel(s)) : [];

Expand All @@ -126,7 +128,7 @@ const lowTrace = (
showlegend: false,
orientation: "h",
marker: {
color: colors,
color: sensitivityResponses.map((s) => sensitivitiesColorMap[s.sensitivityName]),
line: {
width: 3,
color: sensitivityResponses.map((s, idx) =>
Expand All @@ -144,7 +146,7 @@ const highTrace = (
sensitivityResponses: SensitivityResponse[],
showLabels: boolean,
selectedBar: SelectedBar | null,
colors: string[]
sensitivitiesColorMap: SensitivityColorMap
): Partial<SensitivityChartTraceData> => {
// const label = showLabels
// ? sensitivityResponses.map(
Expand All @@ -171,7 +173,7 @@ const highTrace = (
showlegend: false,
orientation: "h",
marker: {
color: colors,
color: sensitivityResponses.map((s) => sensitivitiesColorMap[s.sensitivityName]),
line: {
width: 3,
color: sensitivityResponses.map((s, idx) =>
Expand Down Expand Up @@ -200,11 +202,9 @@ const sensitivityChart: React.FC<sensitivityChartProps> = (props) => {
(s) => s.lowCaseReferenceDifference !== 0 || s.highCaseReferenceDifference !== 0
);
}
const colors: string[] = filteredSensitivityResponses.map(
(s) => props.sensitivityColors.find((sc) => sc.sensitivityName === s.sensitivityName)?.color ?? "black"
);
traces.push(lowTrace(filteredSensitivityResponses, showLabels, selectedBar, colors));
traces.push(highTrace(filteredSensitivityResponses, showLabels, selectedBar, colors));

traces.push(lowTrace(filteredSensitivityResponses, showLabels, selectedBar, props.sensitivityColorMap));
traces.push(highTrace(filteredSensitivityResponses, showLabels, selectedBar, props.sensitivityColorMap));
// if (showRealizationPoints) {
// TODO: Add realization points

Expand Down
17 changes: 8 additions & 9 deletions frontend/src/modules/TornadoChart/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
import SensitivityTable from "./sensitivityTable";
import { PlotType, State } from "./state";

import { createSensitivityColorMap } from "../_shared/sensitivityColors";

export const view = ({
moduleContext,
workbenchSession,
Expand Down Expand Up @@ -89,14 +91,11 @@ export const view = ({

const sensitivities = channelEnsemble?.getSensitivities();
const colorSet = workbenchSettings.useColorSet();
const sensitivityColors: SensitivityColors[] =
sensitivities
?.getSensitivityNames()
.map((sensitivityName, index) =>
index === 0
? { sensitivityName, color: colorSet.getFirstColor() }
: { sensitivityName, color: colorSet.getNextColor() }
) ?? [];
const sensitivitiesColorMap = createSensitivityColorMap(
sensitivities?.getSensitivityNames().sort() ?? [],
colorSet
);

let computedSensitivityResponseDataset: SensitivityResponseDataset | null = null;
if (sensitivities && channelResponseData) {
// How to handle errors?
Expand Down Expand Up @@ -187,7 +186,7 @@ export const view = ({
{computedSensitivityResponseDataset && plotType === PlotType.TORNADO && (
<SensitivityChart
sensitivityResponseDataset={computedSensitivityResponseDataset}
sensitivityColors={sensitivityColors}
sensitivityColorMap={sensitivitiesColorMap}
width={wrapperDivSize.width}
height={wrapperDivSize.height}
showLabels={showLabels}
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/modules/_shared/sensitivityColors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ColorSet } from "@lib/utils/ColorSet";

export type SensitivityColorMap = { [name: string]: string };

export function createSensitivityColorMap(sensitivityNames: string[], colorSet: ColorSet): SensitivityColorMap {
const colorMap: SensitivityColorMap = {};
sensitivityNames.forEach((sensitivityName, index) => {
colorMap[sensitivityName] = index === 0 ? colorSet.getFirstColor() : colorSet.getNextColor();
});
return colorMap;
}

0 comments on commit e025de4

Please sign in to comment.