-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
Use tslib's PlotHistory in optuna-dashboard #928
Merged
porink0424
merged 8 commits into
optuna:main
from
keisuke-umezawa:feature/tslib-plothistory
Sep 11, 2024
+299
−536
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2d74da3
Save
keisuke-umezawa 270c406
Add plotly color theme to PlotImportance
keisuke-umezawa c1802ae
Apply PlotHistory to standalone
keisuke-umezawa bcf5702
Apply PlotHistory to optuna-dashboard
keisuke-umezawa 947c5bb
Add logScale as optional
keisuke-umezawa 6498a55
Add includePruned as optional
keisuke-umezawa 8bba0d2
Fix typo
keisuke-umezawa de2efee
Implement a link for each trial
keisuke-umezawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,373 +1,32 @@ | ||
import { | ||
Box, | ||
FormControl, | ||
FormControlLabel, | ||
FormLabel, | ||
Grid, | ||
MenuItem, | ||
Radio, | ||
RadioGroup, | ||
Select, | ||
SelectChangeEvent, | ||
Slider, | ||
Typography, | ||
useTheme, | ||
} from "@mui/material" | ||
import { | ||
Target, | ||
useFilteredTrialsFromStudies, | ||
useObjectiveAndUserAttrTargetsFromStudies, | ||
} from "@optuna/react" | ||
import * as Optuna from "@optuna/types" | ||
import * as plotly from "plotly.js-dist-min" | ||
import React, { ChangeEvent, FC, useEffect, useState } from "react" | ||
import { useTheme } from "@mui/material" | ||
import { PlotHistory } from "@optuna/react" | ||
import React, { FC } from "react" | ||
import { useNavigate } from "react-router-dom" | ||
import { StudyDetail } from "ts/types/optuna" | ||
import { useConstants } from "../constantsProvider" | ||
import { usePlotlyColorTheme } from "../state" | ||
|
||
const plotDomId = "graph-history" | ||
|
||
interface HistoryPlotInfo { | ||
study_name: string | ||
trials: Optuna.Trial[] | ||
directions: Optuna.StudyDirection[] | ||
metric_names?: string[] | ||
} | ||
|
||
export const GraphHistory: FC<{ | ||
studies: StudyDetail[] | ||
logScale: boolean | ||
includePruned: boolean | ||
}> = ({ studies, logScale, includePruned }) => { | ||
const { url_prefix } = useConstants() | ||
|
||
const theme = useTheme() | ||
const colorTheme = usePlotlyColorTheme(theme.palette.mode) | ||
const navigate = useNavigate() | ||
const [xAxis, setXAxis] = useState< | ||
"number" | "datetime_start" | "datetime_complete" | ||
>("number") | ||
const [markerSize, setMarkerSize] = useState<number>(5) | ||
|
||
const [targets, selected, setTarget] = | ||
useObjectiveAndUserAttrTargetsFromStudies(studies) | ||
|
||
const trials = useFilteredTrialsFromStudies( | ||
studies, | ||
[selected], | ||
!includePruned | ||
) | ||
const historyPlotInfos = studies.map((study, index) => { | ||
const h: HistoryPlotInfo = { | ||
study_name: study?.name, | ||
trials: trials[index], | ||
directions: study?.directions, | ||
metric_names: study?.metric_names, | ||
} | ||
return h | ||
}) | ||
|
||
useEffect(() => { | ||
plotHistory( | ||
historyPlotInfos, | ||
selected, | ||
xAxis, | ||
logScale, | ||
theme.palette.mode, | ||
colorTheme, | ||
markerSize | ||
) | ||
const element = document.getElementById(plotDomId) | ||
if (element !== null && studies.length >= 1) { | ||
// @ts-ignore | ||
element.on("plotly_click", (data) => { | ||
if (data.points[0].data.mode !== "lines") { | ||
let studyId = 1 | ||
if (data.points[0].data.name.includes("Infeasible Trial of")) { | ||
const studyInfo: { id: number; name: string }[] = [] | ||
studies.forEach((study) => { | ||
studyInfo.push({ id: study.id, name: study.name }) | ||
}) | ||
const dataPointStudyName = data.points[0].data.name.replace( | ||
"Infeasible Trial of ", | ||
"" | ||
) | ||
const targetId = studyInfo.find( | ||
(s) => s.name === dataPointStudyName | ||
)?.id | ||
if (targetId !== undefined) { | ||
studyId = targetId | ||
} | ||
} else { | ||
studyId = studies[Math.floor(data.points[0].curveNumber / 2)].id | ||
} | ||
navigate( | ||
url_prefix + | ||
`/studies/${studyId}/trials?numbers=${data.points[0].x}` | ||
) | ||
} | ||
}) | ||
return () => { | ||
// @ts-ignore | ||
element.removeAllListeners("plotly_click") | ||
} | ||
} | ||
}, [ | ||
studies, | ||
selected, | ||
logScale, | ||
xAxis, | ||
theme.palette.mode, | ||
colorTheme, | ||
markerSize, | ||
]) | ||
|
||
const handleObjectiveChange = (event: SelectChangeEvent<string>) => { | ||
setTarget(event.target.value) | ||
} | ||
|
||
const handleXAxisChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
if (e.target.value === "number") { | ||
setXAxis("number") | ||
} else if (e.target.value === "datetime_start") { | ||
setXAxis("datetime_start") | ||
} else if (e.target.value === "datetime_complete") { | ||
setXAxis("datetime_complete") | ||
} | ||
const linkURL = (studyId: number, trialNumber: number) => { | ||
return url_prefix + `/studies/${studyId}/trials?numbers=${trialNumber}` | ||
} | ||
const navigate = useNavigate() | ||
|
||
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: theme.typography.fontWeightBold }} | ||
> | ||
History | ||
</Typography> | ||
{studies[0] !== null && targets.length >= 2 ? ( | ||
<FormControl | ||
component="fieldset" | ||
sx={{ marginBottom: theme.spacing(2) }} | ||
> | ||
<FormLabel component="legend">y Axis</FormLabel> | ||
<Select | ||
value={selected.identifier()} | ||
onChange={handleObjectiveChange} | ||
> | ||
{targets.map((t, i) => ( | ||
<MenuItem value={t.identifier()} key={i}> | ||
{t.toLabel(studies[0].metric_names)} | ||
</MenuItem> | ||
))} | ||
</Select> | ||
</FormControl> | ||
) : null} | ||
<FormControl | ||
component="fieldset" | ||
sx={{ marginBottom: theme.spacing(2) }} | ||
> | ||
<FormLabel component="legend">X-axis:</FormLabel> | ||
<RadioGroup | ||
aria-label="gender" | ||
name="gender1" | ||
value={xAxis} | ||
onChange={handleXAxisChange} | ||
> | ||
<FormControlLabel | ||
value="number" | ||
control={<Radio />} | ||
label="Number" | ||
/> | ||
<FormControlLabel | ||
value="datetime_start" | ||
control={<Radio />} | ||
label="Datetime start" | ||
/> | ||
<FormControlLabel | ||
value="datetime_complete" | ||
control={<Radio />} | ||
label="Datetime complete" | ||
/> | ||
</RadioGroup> | ||
</FormControl> | ||
<FormControl> | ||
<FormLabel component="legend">Marker size:</FormLabel> | ||
<Slider | ||
defaultValue={5} | ||
marks={true} | ||
min={1} | ||
max={10} | ||
step={1} | ||
onChange={(e) => { | ||
// @ts-ignore | ||
setMarkerSize(e.target.value as number) | ||
}} | ||
/> | ||
</FormControl> | ||
</Grid> | ||
<Grid item xs={9}> | ||
<Box | ||
component="div" | ||
id={plotDomId} | ||
sx={{ | ||
height: "450px", | ||
}} | ||
/> | ||
</Grid> | ||
</Grid> | ||
<PlotHistory | ||
studies={studies} | ||
logScale={logScale} | ||
includePruned={includePruned} | ||
colorTheme={colorTheme} | ||
linkURL={linkURL} | ||
router={navigate} | ||
/> | ||
) | ||
} | ||
|
||
const plotHistory = ( | ||
historyPlotInfos: HistoryPlotInfo[], | ||
target: Target, | ||
xAxis: "number" | "datetime_start" | "datetime_complete", | ||
logScale: boolean, | ||
mode: string, | ||
colorTheme: Partial<Plotly.Template>, | ||
markerSize: number | ||
) => { | ||
if (document.getElementById(plotDomId) === null) { | ||
return | ||
} | ||
if (historyPlotInfos.length === 0) { | ||
plotly.react(plotDomId, [], { | ||
template: colorTheme, | ||
}) | ||
return | ||
} | ||
|
||
const layout: Partial<plotly.Layout> = { | ||
margin: { | ||
l: 50, | ||
t: 0, | ||
r: 50, | ||
b: 0, | ||
}, | ||
yaxis: { | ||
title: target.toLabel(historyPlotInfos[0].metric_names), | ||
type: logScale ? "log" : "linear", | ||
}, | ||
xaxis: { | ||
title: xAxis === "number" ? "Trial" : "Time", | ||
type: xAxis === "number" ? "linear" : "date", | ||
}, | ||
showlegend: historyPlotInfos.length === 1 ? false : true, | ||
template: colorTheme, | ||
legend: { | ||
x: 1.0, | ||
y: 0.95, | ||
}, | ||
} | ||
|
||
const getAxisX = (trial: Optuna.Trial): number | Date => { | ||
return xAxis === "number" | ||
? trial.number | ||
: xAxis === "datetime_start" | ||
? trial.datetime_start ?? new Date() | ||
: trial.datetime_complete ?? new Date() | ||
} | ||
|
||
const plotData: Partial<plotly.PlotData>[] = [] | ||
const infeasiblePlotData: Partial<plotly.PlotData>[] = [] | ||
historyPlotInfos.forEach((h) => { | ||
const feasibleTrials: Optuna.Trial[] = [] | ||
const infeasibleTrials: Optuna.Trial[] = [] | ||
h.trials.forEach((t) => { | ||
if (t.constraints.every((c) => c <= 0)) { | ||
feasibleTrials.push(t) | ||
} else { | ||
infeasibleTrials.push(t) | ||
} | ||
}) | ||
plotData.push({ | ||
x: feasibleTrials.map(getAxisX), | ||
y: feasibleTrials.map( | ||
(t: Optuna.Trial): number => target.getTargetValue(t) as number | ||
), | ||
name: `${target.toLabel(h.metric_names)} of ${h.study_name}`, | ||
marker: { | ||
size: markerSize, | ||
}, | ||
mode: "markers", | ||
type: "scatter", | ||
}) | ||
|
||
const objectiveId = target.getObjectiveId() | ||
if (objectiveId !== null) { | ||
const xForLinePlot: (number | Date)[] = [] | ||
const yForLinePlot: number[] = [] | ||
let currentBest: number | null = null | ||
for (let i = 0; i < feasibleTrials.length; i++) { | ||
const t = feasibleTrials[i] | ||
const value = target.getTargetValue(t) as number | ||
if (t.state !== "Complete") { | ||
continue | ||
} else if (currentBest === null) { | ||
currentBest = value | ||
xForLinePlot.push(getAxisX(t)) | ||
yForLinePlot.push(value) | ||
} else if ( | ||
h.directions[objectiveId] === "maximize" && | ||
value > currentBest | ||
) { | ||
const p = feasibleTrials[i - 1] | ||
if (!xForLinePlot.includes(getAxisX(p))) { | ||
xForLinePlot.push(getAxisX(p)) | ||
yForLinePlot.push(currentBest) | ||
} | ||
currentBest = value | ||
xForLinePlot.push(getAxisX(t)) | ||
yForLinePlot.push(value) | ||
} else if ( | ||
h.directions[objectiveId] === "minimize" && | ||
value < currentBest | ||
) { | ||
const p = feasibleTrials[i - 1] | ||
if (!xForLinePlot.includes(getAxisX(p))) { | ||
xForLinePlot.push(getAxisX(p)) | ||
yForLinePlot.push(currentBest) | ||
} | ||
currentBest = value | ||
xForLinePlot.push(getAxisX(t)) | ||
yForLinePlot.push(value) | ||
} | ||
} | ||
if (h.trials.length !== 0) { | ||
xForLinePlot.push(getAxisX(h.trials[h.trials.length - 1])) | ||
yForLinePlot.push(yForLinePlot[yForLinePlot.length - 1]) | ||
} | ||
plotData.push({ | ||
x: xForLinePlot, | ||
y: yForLinePlot, | ||
name: `Best Value of ${h.study_name}`, | ||
mode: "lines", | ||
type: "scatter", | ||
}) | ||
} | ||
infeasiblePlotData.push({ | ||
x: infeasibleTrials.map(getAxisX), | ||
y: infeasibleTrials.map( | ||
(t: Optuna.Trial): number => target.getTargetValue(t) as number | ||
), | ||
name: `Infeasible Trial of ${h.study_name}`, | ||
marker: { | ||
size: markerSize, | ||
color: mode === "dark" ? "#666666" : "#cccccc", | ||
}, | ||
mode: "markers", | ||
type: "scatter", | ||
showlegend: false, | ||
}) | ||
}) | ||
plotData.push(...infeasiblePlotData) | ||
plotly.react(plotDomId, plotData, layout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this part is missing in
tslib/react/src/components/PlotHistory.tsx
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this part. Thank you.