mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-24 13:41:07 +08:00
131 lines
3.1 KiB
TypeScript
131 lines
3.1 KiB
TypeScript
import * as plotly from "plotly.js-dist"
|
|
import React, { FC, useEffect, useState } from "react"
|
|
import {
|
|
Grid,
|
|
FormControl,
|
|
FormLabel,
|
|
MenuItem,
|
|
Select,
|
|
Typography,
|
|
} from "@material-ui/core"
|
|
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
|
|
|
|
const useStyles = makeStyles((theme: Theme) =>
|
|
createStyles({
|
|
title: {
|
|
margin: "1em 0",
|
|
},
|
|
formControl: {
|
|
marginBottom: theme.spacing(2),
|
|
marginRight: theme.spacing(5),
|
|
},
|
|
})
|
|
)
|
|
|
|
const plotDomId = "graph-edf"
|
|
|
|
export const Edf: FC<{
|
|
study: StudyDetail | null
|
|
}> = ({ study = null }) => {
|
|
const classes = useStyles()
|
|
const [objectiveId, setObjectiveId] = useState<number>(0)
|
|
|
|
const handleObjectiveChange = (
|
|
event: React.ChangeEvent<{ value: unknown }>
|
|
) => {
|
|
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" className={classes.title}>
|
|
EDF
|
|
</Typography>
|
|
{study !== null && study.directions.length !== 1 ? (
|
|
<FormControl component="fieldset" className={classes.formControl}>
|
|
<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,
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|