Skip to content

Commit

Permalink
Minor refactoring and fix bugs/issues with atoms
Browse files Browse the repository at this point in the history
Issues/bugs with atoms due to incorrect import/initialization in loadModule and registerModule
  • Loading branch information
jorgenherje committed Dec 19, 2024
1 parent 9b94e94 commit 70fb156
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 122 deletions.
6 changes: 6 additions & 0 deletions backend_py/primary/primary/routers/timeseries/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ async def get_statistical_vector_data_per_sensitivity(
sens_case_realization_mask = pc.and_(requested_realizations_mask, sens_case_realization_mask)
table = vector_table.filter(sens_case_realization_mask)

if table.num_rows == 0 and requested_realizations_mask is not None:
raise HTTPException(
status_code=404,
detail="The combination of realizations to include and sensitivity case realizations results in no valid realizations",
)

statistics = compute_vector_statistics(table, vector_name, service_stat_funcs_to_compute)
if not statistics:
raise HTTPException(status_code=404, detail="Could not compute statistics")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { VectorSpec } from "./typesAndEnums";
export type SettingsToViewInterface = {
vectorSpecification: VectorSpec | null;
resamplingFrequency: Frequency_api | null;
selectedSensitivityNames: string[] | null;
selectedSensitivityNames: string[];
showStatistics: boolean;
showRealizations: boolean;
showHistorical: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { ModuleRegistry } from "@framework/ModuleRegistry";

import { Interfaces, settingsToViewInterfaceInitialization } from "./interfaces";
import { MODULE_NAME } from "./registerModule";
import { Settings } from "./settings/settings";
import { settingsToViewInterfaceEffects } from "./view/atoms/interfaceEffects";
import { View } from "./view/view";

const module = ModuleRegistry.initModule<Interfaces>("SimulationTimeSeriesSensitivity", {
const module = ModuleRegistry.initModule<Interfaces>(MODULE_NAME, {
settingsToViewInterfaceInitialization,
settingsToViewInterfaceEffects,
});

module.viewFC = View;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { ModuleCategory, ModuleDevState } from "@framework/Module";
import { ModuleDataTagId } from "@framework/ModuleDataTags";
import { ModuleRegistry } from "@framework/ModuleRegistry";
import { SyncSettingKey } from "@framework/SyncSettings";
import { Interfaces } from "@modules/3DViewer/interfaces";

import { channelDefs } from "./channelDefs";
import { Interfaces } from "./interfaces";

export const MODULE_NAME = "SimulationTimeSeriesSensitivity";

const description = "Plotting of simulation time series data for ensembles with design matrices.";

ModuleRegistry.registerModule<Interfaces>({
moduleName: "SimulationTimeSeriesSensitivity",
moduleName: MODULE_NAME,
defaultTitle: "Simulation time series per sensitivity",
category: ModuleCategory.MAIN,
devState: ModuleDevState.PROD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { atomWithCompare } from "@framework/utils/atomUtils";
import { atom } from "jotai";
import { isEqual } from "lodash";

function areEnsembleIdentsEqual(a: EnsembleIdent | null, b: EnsembleIdent | null) {
if (a === null) {
return b === null;
}
return a.equals(b);
}

export const syncedEnsembleIdentsAtom = atom<EnsembleIdent[] | null>(null);
export const syncedVectorNameAtom = atom<string | null>(null);

Expand All @@ -13,7 +20,14 @@ export const showStatisticsAtom = atom<boolean>(true);
export const showRealizationsAtom = atom<boolean>(false);
export const showHistoricalAtom = atom<boolean>(true);

export const userSelectedEnsembleIdentAtom = atom<EnsembleIdent | null>(null);
export const userSelectedVectorNameAtom = atom<string | null>(null);
export const userSelectedVectorTagAtom = atom<string | null>(null);
export const userSelectedEnsembleIdentAtom = atomWithCompare<EnsembleIdent | null>(null, areEnsembleIdentsEqual);
export const userSelectedVectorNameAndTagAtom = atomWithCompare<{ name: string | null; tag: string | null }>(
{
name: null,
tag: null,
},
isEqual
);

// Note: Default value of null, to detect uninitialized state, and select all sensitivities on first render
export const userSelectedSensitivityNamesAtom = atomWithCompare<string[] | null>(null, isEqual);
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
syncedVectorNameAtom,
userSelectedEnsembleIdentAtom,
userSelectedSensitivityNamesAtom,
userSelectedVectorNameAtom,
userSelectedVectorTagAtom,
userSelectedVectorNameAndTagAtom,
} from "./baseAtoms";
import { vectorListQueryAtom } from "./queryAtoms";

Expand All @@ -35,27 +34,29 @@ export const availableSensitivityNamesAtom = atom<string[]>((get) => {
const ensemble = selectedEnsembleIdent ? ensembleSet.findEnsemble(selectedEnsembleIdent) : null;
const ensembleSensitivityNames = ensemble?.getSensitivities()?.getSensitivityNames() ?? [];

return ensembleSensitivityNames;
return [...ensembleSensitivityNames];
});

export const selectedSensitivityNamesAtom = atom<string[]>((get) => {
const userSelectedSensitivityNames = get(userSelectedSensitivityNamesAtom);
const availableSensitivityNames = get(availableSensitivityNamesAtom);

// If userSelectedSensitivityNames is empty, do not override it
if (!userSelectedSensitivityNames || userSelectedSensitivityNames.length === 0) {
// If userSelectedSensitivityNames is empty, do not override it (i.e. deselect all)
if (userSelectedSensitivityNames && userSelectedSensitivityNames.length === 0) {
return [];
}

// Fixup invalid sensitivity names
// - If no valid sensitivity names are selected, the change can be due to new available sensitivity names
// - If user selected sensitivity names is null, the module is considered uninitialized
// - If user selected sensitivity names fixup results in empty array, it is due to new available sensitivity
// names, and all are selected.
const fixedUpSensitivityNames =
userSelectedSensitivityNames?.filter((sens) => availableSensitivityNames.includes(sens)) ?? [];
if (fixedUpSensitivityNames.length === 0) {
return availableSensitivityNames;
userSelectedSensitivityNames?.filter((sens) => availableSensitivityNames.includes(sens)) ?? null;
if (!fixedUpSensitivityNames || fixedUpSensitivityNames.length === 0) {
return [...availableSensitivityNames];
}

return fixedUpSensitivityNames;
return [...fixedUpSensitivityNames];
});

export const availableVectorNamesAtom = atom<string[]>((get) => {
Expand All @@ -77,10 +78,9 @@ export const vectorSelectorDataAtom = atom((get) => {
/**
* Atom that handles vector name and tag in synch with fixup
*/
const fixedUpVectorNameAndTagAtom = atom<{ name: string | null; tag: string | null }>((get) => {
const selectedVectorNameAndTagAtom = atom<{ name: string | null; tag: string | null }>((get) => {
const syncedVectorName = get(syncedVectorNameAtom);
const userSelectedVectorName = get(userSelectedVectorNameAtom);
const userSelectedVectorTag = get(userSelectedVectorTagAtom);
const userSelectedVectorNameAndTag = get(userSelectedVectorNameAndTagAtom);
const availableVectorNames = get(availableVectorNamesAtom);

// Override with synced vector name if available
Expand All @@ -89,22 +89,23 @@ const fixedUpVectorNameAndTagAtom = atom<{ name: string | null; tag: string | nu
}

// If vector name is fixed up, adjust tag as well
const userSelectedVectorName = userSelectedVectorNameAndTag.name;
const fixedUpVectorName = fixupVectorName(userSelectedVectorName, availableVectorNames);
if (fixedUpVectorName !== userSelectedVectorName) {
return { name: fixedUpVectorName, tag: fixedUpVectorName };
}

return { name: userSelectedVectorName, tag: userSelectedVectorTag };
return { name: userSelectedVectorName, tag: userSelectedVectorNameAndTag.tag };
});

export const selectedVectorNameAtom = atom<string | null>((get) => {
const fixedUpVectorName = get(fixedUpVectorNameAndTagAtom).name;
return fixedUpVectorName;
const selectedVectorNameAtom = atom<string | null>((get) => {
const userSelectedVectorNameAndTag = get(selectedVectorNameAndTagAtom);
return userSelectedVectorNameAndTag.name;
});

export const selectedVectorTagAtom = atom<string | null>((get) => {
const fixedUpVectorTag = get(fixedUpVectorNameAndTagAtom).tag;
return fixedUpVectorTag;
const userSelectedVectorNameAndTag = get(selectedVectorNameAndTagAtom);
return userSelectedVectorNameAndTag.tag;
});

export const selectedVectorNameHasHistoricalAtom = atom<boolean>((get) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import {
syncedVectorNameAtom,
userSelectedEnsembleIdentAtom,
userSelectedSensitivityNamesAtom,
userSelectedVectorNameAtom,
userSelectedVectorTagAtom,
userSelectedVectorNameAndTagAtom,
} from "./atoms/baseAtoms";
import {
availableSensitivityNamesAtom,
Expand All @@ -51,8 +50,7 @@ export function Settings({ settingsContext, workbenchSession, workbenchServices
const setSyncedEnsembleIdents = useSetAtom(syncedEnsembleIdentsAtom);
const setSyncedVectorName = useSetAtom(syncedVectorNameAtom);
const setUserSelectedEnsembleIdent = useSetAtom(userSelectedEnsembleIdentAtom);
const setUserSelectedVectorName = useSetAtom(userSelectedVectorNameAtom);
const setUserSelectedVectorTag = useSetAtom(userSelectedVectorTagAtom);
const setUserSelectedVectorNameAndTag = useSetAtom(userSelectedVectorNameAndTagAtom);
const setUserSelectedSensitivityNamesAtom = useSetAtom(userSelectedSensitivityNamesAtom);
const selectedEnsembleIdent = useAtomValue(selectedEnsembleIdentAtom);
const vectorsListQuery = useAtomValue(vectorListQueryAtom);
Expand Down Expand Up @@ -101,8 +99,9 @@ export function Settings({ settingsContext, workbenchSession, workbenchServices
setResamplingFrequency(newFreq);
}
function handleVectorSelectChange(selection: SmartNodeSelectorSelection) {
setUserSelectedVectorName(selection.selectedNodes[0] ?? null);
setUserSelectedVectorTag(selection.selectedTags[0]?.text ?? null);
const userSelectedVectorName = selection.selectedNodes[0] ?? null;
const userSelectedVectorTag = selection.selectedTags[0]?.text ?? null;
setUserSelectedVectorNameAndTag({ name: userSelectedVectorName, tag: userSelectedVectorTag });
}
function handleShowHistorical(event: React.ChangeEvent<HTMLInputElement>) {
setShowHistorical(event.target.checked);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { atom } from "jotai";

export const vectorSpecificationAtom = atom<VectorSpec | null>(null);
export const resamplingFrequencyAtom = atom<Frequency_api | null>(Frequency_api.MONTHLY);
export const selectedSensitivityNamesAtom = atom<string[] | null>(null);
export const selectedSensitivityNamesAtom = atom<string[]>([]);
export const showStatisticsAtom = atom<boolean>(true);
export const showRealizationsAtom = atom<boolean>(false);
export const showHistoricalAtom = atom<boolean>(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { atom } from "jotai";

import { userSelectedActiveTimestampUtcMsAtom } from "./baseAtoms";
import { statisticalVectorSensitivityDataQueryAtom } from "./queryAtoms";
import {
historicalVectorDataQueryAtom,
statisticalVectorSensitivityDataQueryAtom,
vectorDataQueryAtom,
} from "./queryAtoms";

export const activeTimestampUtcMsAtom = atom<number | null>((get) => {
const userSelectedActiveTimestampUtcMs = get(userSelectedActiveTimestampUtcMsAtom);
Expand All @@ -15,3 +19,15 @@ export const activeTimestampUtcMsAtom = atom<number | null>((get) => {
}
return userSelectedActiveTimestampUtcMs;
});

export const queryIsFetchingAtom = atom((get) => {
const vectorDataQuery = get(vectorDataQueryAtom);
const statisticalVectorSensitivityDataQuery = get(statisticalVectorSensitivityDataQueryAtom);
const historicalVectorDataQuery = get(historicalVectorDataQueryAtom);

return (
vectorDataQuery.isFetching ||
statisticalVectorSensitivityDataQuery.isFetching ||
historicalVectorDataQuery.isFetching
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ValidEnsembleRealizationsFunctionAtom } from "@framework/GlobalAtoms";
import { encodeAsUintListStr } from "@lib/utils/queryStringUtils";
import {
resamplingFrequencyAtom,
showHistoricalAtom,
showStatisticsAtom,
vectorSpecificationAtom,
} from "@modules/SimulationTimeSeriesSensitivity/view/atoms/baseAtoms";
Expand Down Expand Up @@ -90,6 +91,7 @@ export const statisticalVectorSensitivityDataQueryAtom = atomWithQuery((get) =>

export const historicalVectorDataQueryAtom = atomWithQuery((get) => {
const vectorSpecification = get(vectorSpecificationAtom);
const showHistorical = get(showHistoricalAtom);
const resampleFrequency = get(resamplingFrequencyAtom);

const query = {
Expand All @@ -109,7 +111,7 @@ export const historicalVectorDataQueryAtom = atomWithQuery((get) => {
),
staleTime: STALE_TIME,
gcTime: CACHE_TIME,
enabled: !!vectorSpecification,
enabled: !!(vectorSpecification && showHistorical),
};

return query;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ViewStatusWriter } from "@framework/StatusWriter";

import { useAtomValue } from "jotai";

import { showHistoricalAtom, showStatisticsAtom } from "../atoms/baseAtoms";
import {
historicalVectorDataQueryAtom,
statisticalVectorSensitivityDataQueryAtom,
vectorDataQueryAtom,
} from "../atoms/queryAtoms";

export function useMakeViewStatusWriterMessages(statusWriter: ViewStatusWriter) {
const vectorDataQuery = useAtomValue(vectorDataQueryAtom);
const statisticalVectorSensitivityDataQuery = useAtomValue(statisticalVectorSensitivityDataQueryAtom);
const historicalVectorDataQuery = useAtomValue(historicalVectorDataQueryAtom);
const showStatistics = useAtomValue(showStatisticsAtom);
const showHistorical = useAtomValue(showHistoricalAtom);

const isAnyQueryFetching =
vectorDataQuery.isFetching ||
statisticalVectorSensitivityDataQuery.isFetching ||
historicalVectorDataQuery.isFetching;
statusWriter.setLoading(isAnyQueryFetching);

if (vectorDataQuery.isError) {
statusWriter.addError("Realization data query has error state.");
}
if (showStatistics && statisticalVectorSensitivityDataQuery.isError) {
statusWriter.addError("Statistics data per sensitivity query has error state.");
}
if (showHistorical && historicalVectorDataQuery.isError) {
statusWriter.addWarning("Historical data query has error state.");
}
}
Loading

0 comments on commit 70fb156

Please sign in to comment.