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

feat: dont simulate if there are no plots or secondary params #574

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions frontend-v2/src/features/main/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,20 +403,19 @@ export default function Sidebar() {
{drawer}
</Drawer>
</Box>
{selectedPage === PageName.SIMULATIONS && (
<Box
component="nav"
sx={{
width: { sm: drawerWidth },
flexShrink: { sm: 0 },
height: "100vh",
backgroundColor: '#FBFBFA',
borderRight: '1px solid #DBD6D1'
}}
aria-label="mailbox folders"
id='simulations-portal'
/>
)}
<Box
component="nav"
sx={{
width: { sm: selectedPage === PageName.SIMULATIONS ? drawerWidth : 0 },
flexShrink: { sm: 0 },
height: "100vh",
backgroundColor: '#FBFBFA',
borderRight: '1px solid #DBD6D1'
}}
aria-label="mailbox folders"
id='simulations-portal'
zIndex={0}
/>
<Box
component="main"
sx={{
Expand Down
4 changes: 1 addition & 3 deletions frontend-v2/src/features/simulation/SimulationSliderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ const SimulationSliderView: FC<SimulationSliderProps> = ({
// update the slider value if the variable default value changes
const defaultValue = variable?.default_value || 1.0;
const [value, setValue] = useState<number>(defaultValue);

useEffect(() => {
// don't set the value of the slider until the variable is loaded
if (variable) {
const defaultValue = variable.default_value || 1.0;
setValue(defaultValue);
onChange(slider.variable, defaultValue);
}
}, [onChange, variable]);
}, [onChange, defaultValue, variable]);

const handleSliderChange = (
event: Event | SyntheticEvent<Element, Event>,
Expand Down
7 changes: 6 additions & 1 deletion frontend-v2/src/features/simulation/Simulations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,16 @@ const Simulations: FC = () => {
variables,
timeMax,
);
const hasPlots = simulation ? simulation.plots.length > 0 : false;
const hasSecondaryParameters = model ?
model.derived_variables.reduce((acc, dv) => { return acc || dv.type === "AUC"; }, false) :
false;

const {
loadingSimulate,
data,
error: simulateError,
} = useSimulation(simInputs, model);
} = useSimulation(simInputs, model, hasPlots || hasSecondaryParameters);

const refSimInputs = useSimulationInputs(
model,
Expand Down
10 changes: 8 additions & 2 deletions frontend-v2/src/features/simulation/SimulationsSidePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import UnitField from "../../components/UnitField";
import SimulationSliderView from "./SimulationSliderView";
import HelpButton from "../../components/HelpButton";
import { ExpandLess, ExpandMore } from "@mui/icons-material";
import { ChangeEvent, useState } from "react";
import { ChangeEvent, useEffect, useState } from "react";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this component uses any effect hooks.

import { getTableHeight } from "../../shared/calculateTableHeights";
import { useSelector } from "react-redux";
import { RootState } from "../../app/store";
import { PageName } from "../main/mainSlice";
import {
Simulation,
SimulationPlot,
Expand Down Expand Up @@ -136,6 +139,9 @@ export const SimulationsSidePanel = ({
shouldShowLegend,
setShouldShowLegend,
}: SimulationsSidePanelType) => {
const selectedPage = useSelector(
(state: RootState) => state.main.selectedPage,
);
const portalRoot = document.getElementById(portalId);
const [collapseLayout, setCollapseLayout] = useState(true);
const [collapseOptions, setCollapseOptions] = useState(true);
Expand All @@ -144,7 +150,7 @@ export const SimulationsSidePanel = ({
const [collapseReference, setCollapseReference] = useState(true);
const [collapseLegend, setCollapseLegend] = useState(true);

if (!portalRoot) return null;
if (!portalRoot || selectedPage !== PageName.SIMULATIONS) return null;

return ReactDOM.createPortal(
<Box
Expand Down
9 changes: 8 additions & 1 deletion frontend-v2/src/features/simulation/useSimulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ const SIMULATION_PAGES = [PageName.SIMULATIONS, PageName.RESULTS];
export default function useSimulation(
simInputs: Simulate,
model: CombinedModelRead | undefined,
runSimulation: boolean = true
) {
const { compound, protocols } = useProtocols();
const { setSimulations } = useContext(SimulationContext);
const [loadingSimulate, setLoadingSimulate] = useState<boolean>(false);
const [data, setData] = useState<SimulateResponse[]>([]);
const [lastSimulate, setLastSimulate] = useState<Simulate | undefined>();
const [simulate, { error: simulateErrorBase }] =
useCombinedModelSimulateCreateMutation();
const simulateError: ErrorObject | undefined = simulateErrorBase
Expand All @@ -36,13 +38,16 @@ export default function useSimulation(

useEffect(() => {
let ignore = false;
const simulateInputsDifferent = JSON.stringify(simInputs) !== JSON.stringify(lastSimulate);
if (
runSimulation &&
simInputs.outputs?.length > 1 &&
simInputs.time_max &&
model &&
protocols &&
compound &&
SIMULATION_PAGES.includes(page)
SIMULATION_PAGES.includes(page) &&
simulateInputsDifferent
) {
setLoadingSimulate(true);
console.log("Simulating with params", simInputs.variables);
Expand All @@ -56,6 +61,7 @@ export default function useSimulation(
const responseData = response.data as SimulateResponse[];
setData(responseData);
setSimulations(responseData);
setLastSimulate(simInputs);
}
}
});
Expand All @@ -70,6 +76,7 @@ export default function useSimulation(
simulate,
JSON.stringify(simInputs),
page,
runSimulation,
]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point, I think I should rewrite this hook, to see if I can reduce the number of dependencies for this effect.


return {
Expand Down
Loading