Skip to content

Commit

Permalink
Merge pull request #973 from hrntsm/Show-hyperparams-in--history
Browse files Browse the repository at this point in the history
Add Hovertext to history plot
  • Loading branch information
porink0424 authored Sep 27, 2024
2 parents 1d6cac2 + 4e72c51 commit bb90592
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tslib/react/src/components/PlotHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as plotly from "plotly.js-dist-min"
import { ChangeEvent, FC, useEffect, useState } from "react"

import { useGraphComponentState } from "../hooks/useGraphComponentState"
import { makeHovertext } from "../utils/graph"
import {
Target,
useFilteredTrialsFromStudies,
Expand Down Expand Up @@ -332,6 +333,10 @@ const plotHistory = (
infeasibleTrials.push(t)
}
}
const hovertemplate =
infeasibleTrials.length === 0
? "%{text}<extra>Trial</extra>"
: "%{text}<extra>Feasible Trial</extra>"
plotData.push({
x: feasibleTrials.map(getAxisX),
y: feasibleTrials.map(
Expand All @@ -343,6 +348,8 @@ const plotHistory = (
},
mode: "markers",
type: "scatter",
text: feasibleTrials.map((t) => makeHovertext(t)),
hovertemplate: hovertemplate,
})

const objectiveId = target.getObjectiveId()
Expand Down Expand Up @@ -411,6 +418,8 @@ const plotHistory = (
},
mode: "markers",
type: "scatter",
text: infeasibleTrials.map((t) => makeHovertext(t)),
hovertemplate: "%{text}<extra>Infeasible Trial</extra>",
showlegend: false,
})
}
Expand Down
21 changes: 21 additions & 0 deletions tslib/react/src/utils/graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as Optuna from "@optuna/types"

export const makeHovertext = (trial: Optuna.Trial): string => {
return JSON.stringify(
{
number: trial.number,
values: trial.values,
params: trial.params
.map((p) => [p.name, p.param_external_value])
.reduce(
(obj, [key, value]) => {
obj[key as string] = value
return obj
},
{} as Record<string, Optuna.CategoricalChoiceType>
),
},
undefined,
" "
).replace(/\n/g, "<br>")
}

0 comments on commit bb90592

Please sign in to comment.