Files
optuna-dashboard/optuna_dashboard/ts/components/GraphIntermediateValues.tsx
T
Masashi Shibata 5f333f6bd0 Merge pull request #191 from optuna/plotly-dist-min
Use plotly.js-dist-min to reduce bundle.js.
2022-03-22 23:10:46 +09:00

71 lines
1.8 KiB
TypeScript

import * as plotly from "plotly.js-dist-min"
import React, { FC, useEffect } from "react"
import { Box, Grid, Typography, useTheme } from "@mui/material"
import { plotlyDarkTemplate } from "./PlotlyDarkMode"
const plotDomId = "graph-intermediate-values"
export const GraphIntermediateValues: FC<{
trials: Trial[]
}> = ({ trials = [] }) => {
const theme = useTheme()
useEffect(() => {
plotIntermediateValue(trials, theme.palette.mode)
}, [trials, theme.palette.mode])
return (
<Grid container direction="row">
<Grid
item
xs={3}
container
direction="column"
sx={{ paddingRight: theme.spacing(2) }}
>
<Typography variant="h6" sx={{ margin: "1em 0", fontWeight: 600 }}>
Intermediate values
</Typography>
</Grid>
<Grid item xs={9}>
<Box id={plotDomId} sx={{ height: "450px" }} />
</Grid>
</Grid>
)
}
const plotIntermediateValue = (trials: Trial[], mode: string) => {
if (document.getElementById(plotDomId) === null) {
return
}
const layout: Partial<plotly.Layout> = {
margin: {
l: 50,
t: 0,
r: 50,
b: 0,
},
template: mode === "dark" ? plotlyDarkTemplate : {},
}
if (trials.length === 0) {
plotly.react(plotDomId, [], layout)
return
}
const filteredTrials = trials.filter(
(t) =>
t.state === "Complete" ||
(t.state === "Pruned" && t.values && t.values.length > 0)
)
const plotData: Partial<plotly.PlotData>[] = filteredTrials.map((trial) => {
const values = trial.intermediate_values.filter((iv) => iv.value !== "inf")
return {
x: values.map((iv) => iv.step),
y: values.map((iv) => iv.value),
mode: "lines+markers",
type: "scatter",
name: `trial #${trial.number}`,
}
})
plotly.react(plotDomId, plotData, layout)
}