Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare scenario chart share intervention colouring with ranking charts #6333

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WorkflowNode, WorkflowPortStatus } from '@/types/workflow';
import { renameFnGenerator } from '@/components/workflow/ops/calibrate-ciemss/calibrate-utils';
import { Ref } from 'vue';

import { createRankingInterventionsChart, CATEGORICAL_SCHEME } from '@/services/charts';
import { createRankingInterventionsChart } from '@/services/charts';
import { DATASET_VAR_NAME_PREFIX, getDatasetResultCSV, mergeResults, getDataset } from '@/services/dataset';
import {
DataArray,
Expand All @@ -16,7 +16,7 @@ import {
import { getInterventionPolicyById } from '@/services/intervention-policy';
import { getModelConfigurationById } from '@/services/model-configurations';

import { ChartData } from '@/composables/useCharts';
import { ChartData, setInterventionColorAndScoreMaps } from '@/composables/useCharts';

import { PlotValue, TimepointOption, RankOption, CompareDatasetsState } from './compare-datasets-operation';

Expand Down Expand Up @@ -227,7 +227,13 @@ export function generateRankingCharts(

const rankingCriteriaValues: { score: number; policyName: string; configName: string }[] = [];

let colorIndex = 0;
setInterventionColorAndScoreMaps(
datasets,
modelConfigurations,
interventionPolicies,
interventionNameColorMap,
interventionNameScoresMap
);
datasets.value.forEach((dataset, index: number) => {
const { metadata } = dataset;
const modelConfiguration: ModelConfiguration = modelConfigurations.value.find(
Expand All @@ -243,16 +249,6 @@ export function generateRankingCharts(
return;
}

if (!interventionNameColorMap[policyName]) {
interventionNameScoresMap[policyName] = [];
if (!policy?.name) {
interventionNameColorMap[policyName] = 'black';
} else {
interventionNameColorMap[policyName] = CATEGORICAL_SCHEME[colorIndex];
colorIndex++;
}
}

rankingCriteriaValues.push({
score: pointOfComparison[`${variableKey}:${index}`] ?? 0,
policyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,13 @@ const selectedPlotType = computed(() => knobs.value.selectedPlotType);
const baselineDatasetIndex = computed(() =>
datasets.value.findIndex((dataset) => dataset.id === knobs.value.selectedBaselineDatasetId)
);
const variableCharts = useCompareDatasetCharts(selectedVariableSettings, selectedPlotType, baselineDatasetIndex);
const variableCharts = useCompareDatasetCharts(
selectedVariableSettings,
selectedPlotType,
datasets,
modelConfigurations,
interventionPolicies
);

function outputPanelBehavior() {
if (knobs.value.selectedCompareOption === CompareValue.RANK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ onMounted(() => {
rankingResultsChart
);
});
const comparisonCharts = useCompareDatasetCharts(selectedVariableSettings, selectedPlotType, baselineDatasetIndex);
const comparisonCharts = useCompareDatasetCharts(
selectedVariableSettings,
selectedPlotType,
datasets,
modelConfigurations,
interventionPolicies
);

watch(
() => props.node.inputs,
Expand Down
69 changes: 62 additions & 7 deletions packages/client/hmi-client/src/composables/useCharts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _, { capitalize, cloneDeep } from 'lodash';
import _, { capitalize } from 'lodash';
import { mean, variance } from 'd3';
import { computed, ComputedRef, Ref } from 'vue';
import { VisualizationSpec } from 'vega-embed';
Expand Down Expand Up @@ -27,7 +27,7 @@ import {
ChartSettingSensitivity,
ChartSettingType
} from '@/types/common';
import { ChartAnnotation, Intervention, Model, ModelConfiguration } from '@/types/Types';
import { ChartAnnotation, Dataset, Intervention, InterventionPolicy, Model, ModelConfiguration } from '@/types/Types';
import { displayNumber } from '@/utils/number';
import { getStateVariableStrataEntries, getUnitsFromModelParts, getVegaDateOptions } from '@/services/model';
import { CalibrateMap, isCalibrateMap } from '@/services/calibrate-workflow';
Expand Down Expand Up @@ -540,22 +540,42 @@ export function useCharts(
const useCompareDatasetCharts = (
chartSettings: ComputedRef<ChartSetting[]>,
selectedPlotType: ComputedRef<PlotValue>,
baselineIndex: ComputedRef<number>
// baselineIndex: ComputedRef<number>,
mloppie marked this conversation as resolved.
Show resolved Hide resolved
datasets: Ref<Dataset[]>,
modelConfigurations,
mloppie marked this conversation as resolved.
Show resolved Hide resolved
interventionPolicies
mloppie marked this conversation as resolved.
Show resolved Hide resolved
) => {
const compareDatasetCharts = computed(() => {
const charts: Record<string, VisualizationSpec> = {};
if (!isChartReadyToBuild.value) return charts;

// Make baseline black
const colorScheme = cloneDeep(CATEGORICAL_SCHEME);
colorScheme.splice(baselineIndex.value, 0, 'black');
const interventionNameColorMap: Record<string, string> = {};
const interventionNameScoreMap: Record<string, string> = {};
mloppie marked this conversation as resolved.
Show resolved Hide resolved
setInterventionColorAndScoreMaps(
datasets,
modelConfigurations,
interventionPolicies,
interventionNameColorMap,
interventionNameScoreMap
);

const variableColorMap = datasets.value.map(({ name }) => {
const interventionName = name?.match(/\(([^)]+)\)/);
mloppie marked this conversation as resolved.
Show resolved Hide resolved
if (interventionName?.length) {
if (interventionNameColorMap[interventionName[1]]) {
return interventionNameColorMap[interventionName[1]];
}
}
return 'black';
});

chartSettings.value.forEach((settings) => {
const varName = settings.selectedVariables[0];
const { statLayerVariables, sampleLayerVariables, options } = createForecastChartOptions(settings);
options.title = varName;
options.yAxisTitle = capitalize(selectedPlotType.value);
options.colorscheme = colorScheme;

options.colorscheme = variableColorMap;

const annotations = getChartAnnotationsByChartId(settings.id);
const chart = !settings.showQuantiles
Expand Down Expand Up @@ -1230,3 +1250,38 @@ export function useCharts(
useEnsembleErrorCharts
};
}

export function setInterventionColorAndScoreMaps(
datasets,
modelConfigurations,
interventionPolicies,
interventionNameColorMap,
interventionNameScoresMap
mloppie marked this conversation as resolved.
Show resolved Hide resolved
) {
let colorIndex = 0;
datasets.value.forEach((dataset) => {
const { metadata } = dataset;
const modelConfiguration: ModelConfiguration = modelConfigurations.value.find(
({ id }) => id === metadata.simulationAttributes?.modelConfigurationId
);
const policy: InterventionPolicy = interventionPolicies.value.find(
({ id }) => id === metadata.simulationAttributes?.interventionPolicyId
);

const policyName = policy?.name ?? 'no policy';

if (!modelConfiguration?.name) {
return;
}

if (!interventionNameColorMap[policyName]) {
interventionNameScoresMap[policyName] = [];
mloppie marked this conversation as resolved.
Show resolved Hide resolved
if (!policy?.name) {
interventionNameColorMap[policyName] = 'black';
} else {
interventionNameColorMap[policyName] = CATEGORICAL_SCHEME[colorIndex];
colorIndex++;
}
}
});
}