Skip to content

Commit bb90592

Browse files
authored
Merge pull request #973 from hrntsm/Show-hyperparams-in--history
Add Hovertext to history plot
2 parents 1d6cac2 + 4e72c51 commit bb90592

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

tslib/react/src/components/PlotHistory.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as plotly from "plotly.js-dist-min"
1818
import { ChangeEvent, FC, useEffect, useState } from "react"
1919

2020
import { useGraphComponentState } from "../hooks/useGraphComponentState"
21+
import { makeHovertext } from "../utils/graph"
2122
import {
2223
Target,
2324
useFilteredTrialsFromStudies,
@@ -332,6 +333,10 @@ const plotHistory = (
332333
infeasibleTrials.push(t)
333334
}
334335
}
336+
const hovertemplate =
337+
infeasibleTrials.length === 0
338+
? "%{text}<extra>Trial</extra>"
339+
: "%{text}<extra>Feasible Trial</extra>"
335340
plotData.push({
336341
x: feasibleTrials.map(getAxisX),
337342
y: feasibleTrials.map(
@@ -343,6 +348,8 @@ const plotHistory = (
343348
},
344349
mode: "markers",
345350
type: "scatter",
351+
text: feasibleTrials.map((t) => makeHovertext(t)),
352+
hovertemplate: hovertemplate,
346353
})
347354

348355
const objectiveId = target.getObjectiveId()
@@ -411,6 +418,8 @@ const plotHistory = (
411418
},
412419
mode: "markers",
413420
type: "scatter",
421+
text: infeasibleTrials.map((t) => makeHovertext(t)),
422+
hovertemplate: "%{text}<extra>Infeasible Trial</extra>",
414423
showlegend: false,
415424
})
416425
}

tslib/react/src/utils/graph.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as Optuna from "@optuna/types"
2+
3+
export const makeHovertext = (trial: Optuna.Trial): string => {
4+
return JSON.stringify(
5+
{
6+
number: trial.number,
7+
values: trial.values,
8+
params: trial.params
9+
.map((p) => [p.name, p.param_external_value])
10+
.reduce(
11+
(obj, [key, value]) => {
12+
obj[key as string] = value
13+
return obj
14+
},
15+
{} as Record<string, Optuna.CategoricalChoiceType>
16+
),
17+
},
18+
undefined,
19+
" "
20+
).replace(/\n/g, "<br>")
21+
}

0 commit comments

Comments
 (0)