Files
optuna-dashboard/optuna_dashboard/static/components/GraphEdf.tsx
T
2022-03-05 13:23:40 +09:00

126 lines
3.0 KiB
TypeScript

import * as plotly from "plotly.js-dist"
import React, { FC, useEffect, useState } from "react"
import {
Grid,
FormControl,
FormLabel,
MenuItem,
Select,
Typography,
SelectChangeEvent,
useTheme,
} from "@mui/material"
import {plotlyDarkTemplate} from "./PlotlyDarkMode";
const plotDomId = "graph-edf"
export const Edf: FC<{
study: StudyDetail | null
}> = ({ study = null }) => {
const theme = useTheme()
const [objectiveId, setObjectiveId] = useState<number>(0)
const handleObjectiveChange = (event: SelectChangeEvent<number>) => {
setObjectiveId(event.target.value as number)
}
useEffect(() => {
if (study != null) {
plotEdf(study, objectiveId)
}
}, [study, objectiveId])
return (
<Grid container direction="row">
<Grid item xs={3}>
<Grid container direction="column">
<Typography variant="h6" sx={{ margin: "1em 0" }}>
EDF
</Typography>
{study !== null && study.directions.length !== 1 ? (
<FormControl
component="fieldset"
sx={{
marginBottom: theme.spacing(2),
marginRight: theme.spacing(5),
}}
>
<FormLabel component="legend">Objective ID:</FormLabel>
<Select value={objectiveId} onChange={handleObjectiveChange}>
{study.directions.map((d, i) => (
<MenuItem value={i} key={i}>
{i}
</MenuItem>
))}
</Select>
</FormControl>
) : null}
</Grid>
</Grid>
<Grid item xs={9}>
<div id={plotDomId} />
</Grid>
</Grid>
)
}
const plotEdf = (study: StudyDetail, objectiveId: number) => {
if (document.getElementById(plotDomId) === null) {
return
}
const trials: Trial[] = study ? study.trials : []
const completedTrials = trials.filter((t) => t.state === "Complete")
if (completedTrials.length === 0) {
plotly.react(plotDomId, [])
return
}
const target_name = "Objective Value"
const target = (t: Trial): number => {
return t.values![objectiveId]
}
const layout: Partial<plotly.Layout> = {
xaxis: {
title: target_name,
},
yaxis: {
title: "Cumulative Probability",
},
margin: {
l: 50,
t: 0,
r: 50,
b: 50,
},
template: plotlyDarkTemplate,
}
const values = completedTrials.map((t) => target(t))
const numValues = values.length
const minX = Math.min(...values)
const maxX = Math.max(...values)
const numStep = 100
const _step = (maxX - minX) / (numStep - 1)
const xValues = []
const yValues = []
for (let i = 0; i < numStep; i++) {
const boundary_right = minX + _step * i
xValues.push(boundary_right)
yValues.push(values.filter((v) => v <= boundary_right).length / numValues)
}
const plotData: Partial<plotly.PlotData>[] = [
{
type: "scatter",
x: xValues,
y: yValues,
},
]
plotly.react(plotDomId, plotData, layout)
}