Skip to content

Commit

Permalink
Refactor parameter matrix module (equinor#609)
Browse files Browse the repository at this point in the history
Co-authored-by: Hans Kallekleiv <[email protected]>
  • Loading branch information
jorgenherje and HansKallekleiv authored Apr 16, 2024
1 parent de630c7 commit d7ac146
Show file tree
Hide file tree
Showing 14 changed files with 392 additions and 282 deletions.

This file was deleted.

10 changes: 6 additions & 4 deletions frontend/src/modules/ParameterDistributionMatrix/loadModule.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ModuleRegistry } from "@framework/ModuleRegistry";

import { Settings } from "./settings";
import { MODULE_NAME } from "./registerModule";
import { Settings } from "./settings/settings";
import { Interface, interfaceInitialization } from "./settingsToViewInterface";
import { State } from "./state";
import { View } from "./view";
import { View } from "./view/view";

const defaultState: State = { ensembleSetParameterIdents: [] };
const defaultState: State = {};

const module = ModuleRegistry.initModule<State>("ParameterDistributionMatrix", defaultState);
const module = ModuleRegistry.initModule<State, Interface>(MODULE_NAME, defaultState, {}, interfaceInitialization);

module.viewFC = View;
module.settingsFC = Settings;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ModuleRegistry } from "@framework/ModuleRegistry";

import { Interface } from "./settingsToViewInterface";
import { State } from "./state";

export const MODULE_NAME = "ParameterDistributionMatrix";
ModuleRegistry.registerModule<State, Interface>({
moduleName: MODULE_NAME,
defaultTitle: "Parameter Distribution Matrix",
});
185 changes: 0 additions & 185 deletions frontend/src/modules/ParameterDistributionMatrix/settings.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EnsembleIdent } from "@framework/EnsembleIdent";
import { ParameterIdent } from "@framework/EnsembleParameters";
import { atomWithCompare } from "@framework/utils/atomUtils";

import { atom } from "jotai";

function areEnsembleIdentListsEqual(a: EnsembleIdent[], b: EnsembleIdent[]) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!a[i].equals(b[i])) {
return false;
}
}
return true;
}

export const userSelectedEnsembleIdentsAtom = atomWithCompare<EnsembleIdent[]>([], areEnsembleIdentListsEqual);
export const userSelectedParameterIdentsAtom = atom<ParameterIdent[]>([]);
export const showConstantParametersAtom = atom<boolean>(false);
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { ParameterIdent, ParameterType } from "@framework/EnsembleParameters";
import { EnsembleSetAtom } from "@framework/GlobalAtoms";

import { atom } from "jotai";

import {
showConstantParametersAtom,
userSelectedEnsembleIdentsAtom,
userSelectedParameterIdentsAtom,
} from "./baseAtoms";

export const selectedEnsembleIdentsAtom = atom((get) => {
const ensembleSet = get(EnsembleSetAtom);
const userSelectedEnsembleIdents = get(userSelectedEnsembleIdentsAtom);

let computedEnsembleIdents = userSelectedEnsembleIdents.filter((el) => ensembleSet.hasEnsemble(el));
if (computedEnsembleIdents.length === 0 && ensembleSet.getEnsembleArr().length > 0) {
computedEnsembleIdents = [ensembleSet.getEnsembleArr()[0].getIdent()];
}

return computedEnsembleIdents;
});

export const intersectedParameterIdentsAtom = atom((get) => {
const ensembleSet = get(EnsembleSetAtom);
const selectedEnsembleIdents = get(selectedEnsembleIdentsAtom);
const showConstantParameters = get(showConstantParametersAtom);

if (selectedEnsembleIdents.length === 0) return [];

// Find set of parameter idents per ensemble
const ensembleParameterSets: Set<ParameterIdent>[] = [];

for (const ensembleIdent of selectedEnsembleIdents) {
const ensemble = ensembleSet.findEnsemble(ensembleIdent);
if (!ensemble) continue;

const parameters = ensemble
.getParameters()
.getParameterArr()
.filter(
(parameter) =>
(showConstantParameters || !parameter.isConstant) && parameter.type === ParameterType.CONTINUOUS
);
const identArr: ParameterIdent[] = [];
for (const parameter of parameters) {
identArr.push(new ParameterIdent(parameter.name, parameter.groupName));
}
const parameterIdents = new Set(identArr);
if (parameterIdents.size > 0) {
ensembleParameterSets.push(parameterIdents);
}
}

if (ensembleParameterSets.length === 0) return [];

// Intersection of parameters across ensembles
const intersectedParameterIdents = ensembleParameterSets.reduce((acc, set) => {
if (acc === null) return set;
return new Set([...acc].filter((ident1) => [...set].some((ident2) => ident1.equals(ident2))));
}, ensembleParameterSets[0] || new Set());

return Array.from(intersectedParameterIdents).sort((a, b) => a.toString().localeCompare(b.toString()));
});

export const selectedParameterIdentsAtom = atom((get) => {
const intersectedParameterIdents = get(intersectedParameterIdentsAtom);
const userSelectedParameterIdents = get(userSelectedParameterIdentsAtom);

return userSelectedParameterIdents.filter((ident) =>
intersectedParameterIdents.some((intersectIdent) => intersectIdent.equals(ident))
);
});
Loading

0 comments on commit d7ac146

Please sign in to comment.