Skip to content

Commit

Permalink
refactor multisensitivity batchPars so only a single batchpars is gen…
Browse files Browse the repository at this point in the history
…erated
  • Loading branch information
EmmaLRussell committed Sep 1, 2023
1 parent 69b512d commit e3c3684
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export default defineComponent({
if (settingsInternal.variationType === SensitivityVariationType.Custom) {
return null;
}
return generateBatchPars(store.state, settingsInternal, paramValues.value);
return generateBatchPars(store.state, [settingsInternal], paramValues.value);
});
const batchPars = computed(() => batchParsResult.value?.batchPars);
const batchParsError = computed(() => {
Expand Down
10 changes: 5 additions & 5 deletions app/static/src/app/components/options/SensitivityOptions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</li>
</ul>
<sensitivity-param-values v-if="settings.variationType !== 'Custom'"
:batch-pars="allBatchPars[idx]"
:batch-pars="batchPars"
:param-name="settings.parameterToVary">
</sensitivity-param-values>
<hr v-if="idx < allSettings.length-1" />
Expand Down Expand Up @@ -166,9 +166,9 @@ export default defineComponent({
updateMultiSensitivitySettings(newSettings);
};
const allBatchPars = computed(() => (props.multiSensitivity
? store.getters[`multiSensitivity/${MultiSensitivityGetter.multiBatchPars}`]
: [store.getters[`sensitivity/${SensitivityGetter.batchPars}`]]));
const batchPars = computed(() => (props.multiSensitivity
? store.getters[`multiSensitivity/${MultiSensitivityGetter.batchPars}`]
: store.getters[`sensitivity/${SensitivityGetter.batchPars}`]));
return {
title,
Expand All @@ -185,7 +185,7 @@ export default defineComponent({
addSettings,
deleteSettings,
editOpen,
allBatchPars
batchPars
};
}
});
Expand Down
23 changes: 23 additions & 0 deletions app/static/src/app/store/multiSensitivity/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
import {BatchPars} from "../../types/responseTypes";
import {ActionContext, ActionTree} from "vuex";
import {SensitivityState} from "../sensitivity/state";
import {AppState, AppType} from "../appState/state";
import {ModelGetter} from "../model/getters";
import {OdinSensitivityResult} from "../../types/wrapperTypes";
import userMessages from "../../userMessages";
import {SensitivityMutation} from "../sensitivity/mutations";
import {SensitivityGetter} from "../sensitivity/getters";
import {Dict} from "../../types/utilTypes";
import {SensitivityAction} from "../sensitivity/actions";
import {MultiSensitivityState} from "./state";

export enum MultiSensitivityAction {
RunMultiSensitivity = "RunMultiSensitivity",
ComputeNext = "ComputeNext"
}

export const actions: ActionTree<MultiSensitivityState, AppState> = {
[MultiSensitivityAction.RunMultiSensitivity](context) {
const {rootState, getters} = context;
const {endTime} = rootState.run;
const batchPars = getters[SensitivityGetter.batchPars];

//runSensitivity(batchPars, endTime, context);
}
}
12 changes: 5 additions & 7 deletions app/static/src/app/store/multiSensitivity/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import { AppState } from "../appState/state";
import { generateBatchPars } from "../../utils";

export enum MultiSensitivityGetter {
multiBatchPars = "multiBatchPars"
batchPars = "batchPars"
}

export interface MultiSensitivityGetters {
[MultiSensitivityGetter.multiBatchPars]: Getter<MultiSensitivityState, AppState>
[MultiSensitivityGetter.batchPars]: Getter<MultiSensitivityState, AppState>
}

export const getters: MultiSensitivityGetters & GetterTree<MultiSensitivityState, AppState> = {
[MultiSensitivityGetter.multiBatchPars]: (state: MultiSensitivityState, _: MultiSensitivityGetters,
rootState: AppState) => {
return state.paramSettings.map((settings) => {
return generateBatchPars(rootState, settings, rootState.run.parameterValues).batchPars;
});
[MultiSensitivityGetter.batchPars]: (state: MultiSensitivityState, _: MultiSensitivityGetters,
rootState: AppState) => {
return generateBatchPars(rootState, state.paramSettings, rootState.run.parameterValues).batchPars;
}
};
4 changes: 2 additions & 2 deletions app/static/src/app/store/sensitivity/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export interface SensitivityGetters {
export const getters: SensitivityGetters & GetterTree<SensitivityState, AppState> = {
[SensitivityGetter.batchPars]: (state: SensitivityState, sensitivityGetters: SensitivityGetters,
rootState: AppState) => {
return generateBatchPars(rootState, state.paramSettings, rootState.run.parameterValues).batchPars;
return generateBatchPars(rootState, [state.paramSettings], rootState.run.parameterValues).batchPars;
},

[SensitivityGetter.parameterSetBatchPars]: (state: SensitivityState, sensitivityGetters: SensitivityGetters,
rootState: AppState) => {
const result = {} as Dict<BatchPars | null>;
const { paramSettings } = state;
rootState.run.parameterSets.forEach((paramSet: ParameterSet) => {
result[paramSet.name] = generateBatchPars(rootState, paramSettings, paramSet.parameterValues).batchPars;
result[paramSet.name] = generateBatchPars(rootState, [paramSettings], paramSet.parameterValues).batchPars;
});
return result;
},
Expand Down
82 changes: 36 additions & 46 deletions app/static/src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AdvancedSettingsOdin,
BatchPars,
OdinModelResponseError,
OdinUserType,
OdinUserType, VaryingPar,
WodinError
} from "./types/responseTypes";
import userMessages from "./userMessages";
Expand Down Expand Up @@ -131,13 +131,11 @@ export interface GenerateBatchParsResult {
error: WodinError | null
}

function generateBatchParsFromOdin(
function generateVaryingParFromOdin(
rootState: AppState,
paramSettings: SensitivityParameterSettings,
paramValues: OdinUserType
): GenerateBatchParsResult {
let varyingParam = null;
let errorDetail = null;
): VaryingPar {
// TODO: NB For now we use ode runner to generate batch pars for all app types, but expect this to change
const runner = rootState.model.odinRunnerOde;
const {
Expand All @@ -146,45 +144,29 @@ function generateBatchParsFromOdin(
const logarithmic = scaleType === SensitivityScaleType.Logarithmic;

if (variationType === SensitivityVariationType.Percentage) {
try {
varyingParam = runner!.batchParsDisplace(
paramValues, parameterToVary!,
numberOfRuns, logarithmic,
variationPercentage
);
} catch (e) {
errorDetail = (e as Error).message;
}
} else {
try {
varyingParam = runner!.batchParsRange(
paramValues,
return runner!.batchParsDisplace(
paramValues, parameterToVary!,
numberOfRuns, logarithmic,
variationPercentage
);
}
return runner!.batchParsRange(
paramValues,
parameterToVary!,
numberOfRuns,
logarithmic,
rangeFrom,
rangeTo
);
} catch (e) {
errorDetail = (e as Error).message;
}
}

const error = errorDetail ? { error: userMessages.sensitivity.invalidSettings, detail: errorDetail } : null;
const batchPars = errorDetail ? null : { base: paramValues!, varying: [varyingParam!] };
return {
batchPars,
error
};
);
}

export function generateBatchPars(
rootState: AppState,
paramSettings: SensitivityParameterSettings,
paramSettings: SensitivityParameterSettings[],
paramValues: OdinUserType | null
): GenerateBatchParsResult {
let errorDetail = null;
if (!paramSettings.parameterToVary) {
if (paramSettings.some((s) => !s.parameterToVary)) {
errorDetail = "Parameter to vary is not set";
} else if (!rootState.model.odinRunnerOde || !paramValues) {
errorDetail = "Model is not initialised";
Expand All @@ -196,20 +178,28 @@ export function generateBatchPars(
};
}

if (paramSettings.variationType === SensitivityVariationType.Custom) {
const batchPars: BatchPars = {
base: paramValues!,
varying: [{
name: paramSettings.parameterToVary!,
values: paramSettings.customValues
}]
};
return {
batchPars,
error: null
};
}
return generateBatchParsFromOdin(rootState, paramSettings, paramValues!);
const varying: VaryingPar[] = [];
paramSettings.forEach((s) => {
if (s.variationType === SensitivityVariationType.Custom) {
varying.push({
name: s.parameterToVary!,
values: s.customValues
});
} else {
try {
varying.push(generateVaryingParFromOdin(rootState, s, paramValues!));
} catch (e) {
errorDetail = (e as Error).message;
}
}
});

const error = errorDetail ? { error: userMessages.sensitivity.invalidSettings, detail: errorDetail } : null;
const batchPars = errorDetail ? null : { base: paramValues!, varying };
return {
batchPars,
error
};
}

export const newSessionId = (): string => uid(32);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("SensitivityOptions", () => {
paramSettings
},
getters: {
[MultiSensitivityGetter.multiBatchPars]: () => mockMultiBatchPars
[MultiSensitivityGetter.batchPars]: () => mockMultiBatchPars
},
mutations: {
[MultiSensitivityMutation.SetParamSettings]: mockMultiSensitivitySetParamSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("MultiSensitivity getters", () => {
const mockSpyDisplace = jest.spyOn(odinRunnerOde, "batchParsDisplace");
const mockSpyRange = jest.spyOn(odinRunnerOde, "batchParsRange");

const result = getters[MultiSensitivityGetter.multiBatchPars](state, getters, rootState, {} as any);
const result = getters[MultiSensitivityGetter.batchPars](state, getters, rootState, {} as any);
expect(result).toStrictEqual([
{
varying: [
Expand Down

0 comments on commit e3c3684

Please sign in to comment.