Skip to content

Commit

Permalink
Ts/3398 interventions (#3461)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom-Szendrey <[email protected]>
  • Loading branch information
Tom-Szendrey and Tom-Szendrey authored May 1, 2024
1 parent d447dd0 commit f79bfae
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,20 @@
tabindex="0"
@click="
() => {
if (!configuration?.metadata?.timeseries?.[parameter.id]) {
emit('enter-value-cell', 'parameters', 'value', i, j);
}
emit('enter-value-cell', 'parameters', 'value', i, j);
}
"
@keyup.enter="
() => {
if (!configuration?.metadata?.timeseries?.[parameter.id]) {
emit('enter-value-cell', 'parameters', 'value', i, j);
}
emit('enter-value-cell', 'parameters', 'value', i, j);
}
"
>
<section
:class="!cellEditStates[i].parameters[j] ? 'editable-cell' : 'editable-cell-hidden'"
>
<div class="distribution-cell">
<!-- To represent a time series variable -->
<span v-if="configuration?.metadata?.timeseries?.[parameter.id]">TS</span>
<span v-else>{{ parameter.value }}</span>
<span>{{ parameter.value }}</span>
<span class="distribution-range" v-if="parameter.distribution"
>Min: {{ parameter.distribution.parameters.minimum }} Max:
{{ parameter.distribution.parameters.maximum }}</span
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<template>
<div class="form">
<div class="label-col">
<label>Parameter name </label>
<Dropdown :options="parameterOptions" v-model.lazy="name" @blur="updateIntervention" />
</div>
<div class="label-col">
<label>Timestep</label>
<tera-input-number v-model.lazy="timestep" @blur="updateIntervention" />
</div>
<div class="label-col">
<label>Value</label>
<tera-input-number v-model.lazy="value" @blur="updateIntervention" />
</div>
<Button label="Delete" icon="pi pi-trash" @click="$emit('delete')" rounded text />
</div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Intervention } from '@/types/Types';
import teraInputNumber from '@/components/widgets/tera-input-number.vue';
import Button from 'primevue/button';
import Dropdown from 'primevue/dropdown';
const props = defineProps<{
intervention: Intervention;
parameterOptions: string[];
}>();
const emit = defineEmits(['update-value', 'delete']);
const name = ref(props.intervention.name);
const timestep = ref(props.intervention.timestep);
const value = ref(props.intervention.value);
function updateIntervention() {
const intervention: Intervention = {
name: name.value,
timestep: timestep.value,
value: value.value
};
emit('update-value', intervention);
}
</script>

<style scoped>
.form {
display: flex;
}
.label-col {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
</template>
</Column>
<!-- Value type: Matrix or a Dropdown with: Time varying, Constant, Distribution (with icons) -->
<!-- Value type: Matrix or a Dropdown with: Constant, Distribution (with icons) -->
<Column field="type" header="Value Type" class="w-2">
<template #body="slotProps">
<Button
Expand Down Expand Up @@ -212,20 +212,6 @@
/>
-->
</span>
<!-- Time series -->
<span
class="timeseries-container mr-2"
v-else-if="slotProps.data.type === ParamType.TIME_SERIES"
>
<InputText
:placeholder="'step:value, step:value, (e.g., 0:25, 1:26, 2:27 etc.)'"
v-model.lazy="slotProps.data.timeseries"
:disabled="readonly"
@update:model-value="(val) => updateTimeseries(slotProps.data.value.id, val)"
/>
<small v-if="errorMessage" class="invalid-message">{{ errorMessage }}</small>
</span>
</template>
</Column>
Expand Down Expand Up @@ -323,37 +309,21 @@
</Column>
<Column header="Value type">
<template #body="{ data }">
{{ typeOptions[getParamType(data.parameter, data.configuration.configuration)].label }}
{{ typeOptions[getParamType(data.parameter)].label }}
</template>
</Column>
<Column header="Value">
<template #body="{ data }">
<span
v-if="
getParamType(data.parameter, data.configuration.configuration) ===
ParamType.CONSTANT
"
>
<span v-if="getParamType(data.parameter) === ParamType.CONSTANT">
{{ data.parameter.value }}
</span>
<div
class="distribution-container"
v-else-if="
getParamType(data.parameter, data.configuration.configuration) ===
ParamType.DISTRIBUTION
"
v-else-if="getParamType(data.parameter) === ParamType.DISTRIBUTION"
>
<span>Min: {{ data.parameter.distribution.parameters.minimum }}</span>
<span>Max: {{ data.parameter.distribution.parameters.maximum }}</span>
</div>
<span
v-else-if="
getParamType(data.parameter, data.configuration.configuration) ===
ParamType.TIME_SERIES
"
>
{{ data.configuration?.configuration?.metadata?.timeseries?.[data.parameter.id] }}
</span>
</template>
</Column>
<Column header="Source">
Expand Down Expand Up @@ -404,8 +374,6 @@ import { MiraModel, MiraTemplateParams } from '@/model-representation/mira/mira-
import { isStratifiedModel, collapseParameters } from '@/model-representation/mira/mira';
import {
updateVariable,
validateTimeSeries,
getTimeseries,
getParameterMetadata,
getParameters,
updateParameterMetadata
Expand All @@ -426,8 +394,7 @@ const props = defineProps<{
const typeOptions = [
{ label: 'Constant', value: ParamType.CONSTANT, icon: 'pi pi-hashtag' },
{ label: 'Distribution', value: ParamType.DISTRIBUTION, icon: 'custom-icon-distribution' },
{ label: 'Time varying', value: ParamType.TIME_SERIES, icon: 'pi pi-clock' }
{ label: 'Distribution', value: ParamType.DISTRIBUTION, icon: 'custom-icon-distribution' }
];
const emit = defineEmits(['update-value', 'update-model']);
Expand Down Expand Up @@ -486,13 +453,11 @@ const tableFormattedParams = ref<ModelConfigTableData[]>([]);
// FIXME: This method doee not really work in this context, the different types
// of CONSTANT/TIME_SERIES/DISTRIBUTION are not mutually exclusive, a parameter
// can have one or more types
const getParamType = (param: ModelParameter | undefined, model: Model = props.model) => {
const getParamType = (param: ModelParameter | undefined) => {
let type = ParamType.CONSTANT;
if (!param) return type;
if (model.metadata?.timeseries?.[param.id] && model.metadata?.timeseries?.[param.id] !== null) {
type = ParamType.TIME_SERIES;
} else if (param?.distribution) {
if (param?.distribution) {
type = ParamType.DISTRIBUTION;
}
return type;
Expand All @@ -507,7 +472,6 @@ const buildParameterTable = () => {
const tableFormattedMatrix: ModelConfigTableData[] = vals.map((v) => {
const param = getParameters(model).find((i) => i.id === v);
const paramType = getParamType(param);
const timeseriesValue = getTimeseries(props.model, param!.id);
const parametersMetadata = getParameterMetadata(props.model, param!.id);
const sourceValue = parametersMetadata?.source;
return {
Expand All @@ -519,8 +483,7 @@ const buildParameterTable = () => {
units: param?.units?.expression ?? '',
value: param,
source: sourceValue,
visibility: false,
timeseries: timeseriesValue
visibility: false
};
});
formattedParams.push({
Expand All @@ -541,7 +504,6 @@ const buildParameterTable = () => {
if (!param) return;
const paramType = getParamType(param);
const timeseriesValue = getTimeseries(props.model, param.id);
const parametersMetadata = getParameterMetadata(props.model, param.id);
const sourceValue = parametersMetadata?.source;
formattedParams.push({
Expand All @@ -553,16 +515,14 @@ const buildParameterTable = () => {
unit: param.units?.expression,
value: param,
source: sourceValue,
visibility: false,
timeseries: timeseriesValue
visibility: false
});
});
// Stockflow has auxiliaries
const auxiliaries = model.model?.auxiliaries ?? [];
auxiliaries.forEach((aux) => {
const paramType = getParamType(aux);
const timeseriesValue = model.metadata?.timeseries?.[aux.id];
const parametersMetadata = model.metadata?.parameters?.[aux.id];
const sourceValue = parametersMetadata?.source;
formattedParams.push({
Expand All @@ -574,8 +534,7 @@ const buildParameterTable = () => {
unit: aux.units?.expression,
value: aux,
source: sourceValue,
visibility: false,
timeseries: timeseriesValue
visibility: false
});
});
}
Expand All @@ -594,8 +553,6 @@ const modelType = computed(() => getModelType(props.model));
// const addPlusMinus = ref(10);
const errorMessage = ref('');
const expandedRows = ref([]);
const openMatrixModal = (datum: ModelConfigTableData) => {
Expand Down Expand Up @@ -624,25 +581,6 @@ const updateParamValue = (param: ModelParameter, key: string, value: any) => {
emit('update-value', [clonedParam]);
};
const updateTimeseries = (id: string, value: string) => {
// Empty string => removal
if (value === '') {
const clonedModel = cloneDeep(props.model);
if (clonedModel.metadata && clonedModel.metadata.timeseries) {
clonedModel.metadata.timeseries[id] = null;
}
emit('update-model', clonedModel);
return;
}
if (!validateTimeSeries(value)) return;
const clonedModel = cloneDeep(props.model);
clonedModel.metadata ??= {};
clonedModel.metadata.timeseries ??= {};
clonedModel.metadata.timeseries[id] = value;
emit('update-model', clonedModel);
};
const updateMetadataFromInput = (id: string, key: string, value: any) => {
const clonedModel = cloneDeep(props.model);
updateParameterMetadata(clonedModel, id, key, value);
Expand Down Expand Up @@ -672,20 +610,14 @@ const applySelectedValue = () => {
if (!selectedValue.value) return;
const clonedModel = cloneDeep(props.model);
const timeseries =
selectedValue.value.configuration.configuration.metadata?.timeseries?.[
selectedValue.value.parameter.id
];
const metadata =
selectedValue.value.configuration.configuration.metadata?.parameters?.[
selectedValue.value.parameter.id
] ?? {};
clonedModel.metadata ??= {};
clonedModel.metadata.parameters ??= {};
clonedModel.metadata.timeseries ??= {};
clonedModel.metadata.parameters[selectedValue.value.parameter.id] = metadata;
clonedModel.metadata.timeseries[selectedValue.value.parameter.id] = timeseries;
// default source to use confirguration's source if there is no source
clonedModel.metadata.parameters[selectedValue.value.parameter.id].source =
Expand Down Expand Up @@ -770,10 +702,6 @@ watch(
text-align: right;
}
.timeseries-container {
font-size: var(--font-caption);
}
.add-plus-minus {
width: 3rem;
margin-left: var(--gap-xsmall);
Expand Down Expand Up @@ -828,11 +756,6 @@ watch(
color: var(--text-color-danger);
}
.timeseries-container {
display: flex;
flex-direction: column;
}
.secondary-text {
color: var(--text-color-subdued);
}
Expand Down
Loading

0 comments on commit f79bfae

Please sign in to comment.