Skip to content
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

Move getAxis to tslib #983

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion optuna_dashboard/ts/components/GraphContour.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import {
import blue from "@mui/material/colors/blue"
import {
GraphContainer,
getAxisInfo,
useGraphComponentState,
useMergedUnionSearchSpace,
} from "@optuna/react"
import * as plotly from "plotly.js-dist-min"
import React, { FC, useEffect, useMemo, useState } from "react"
import { SearchSpaceItem, StudyDetail, Trial } from "ts/types/optuna"
import { PlotType } from "../apiClient"
import { getAxisInfo } from "../graphUtil"
import { usePlot } from "../hooks/usePlot"
import { usePlotlyColorTheme } from "../state"
import { useBackendRender } from "../state"
Expand Down
2 changes: 1 addition & 1 deletion optuna_dashboard/ts/components/GraphParetoFront.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
Typography,
useTheme,
} from "@mui/material"
import { makeHovertext } from "@optuna/react"
import * as Optuna from "@optuna/types"
import * as plotly from "plotly.js-dist-min"
import React, { FC, useEffect, useState } from "react"
import { useNavigate } from "react-router-dom"
import { StudyDetail, Trial } from "ts/types/optuna"
import { PlotType } from "../apiClient"
import { useConstants } from "../constantsProvider"
import { makeHovertext } from "../graphUtil"
import { usePlot } from "../hooks/usePlot"
import { usePlotlyColorTheme } from "../state"
import { useBackendRender } from "../state"
Expand Down
3 changes: 2 additions & 1 deletion optuna_dashboard/ts/components/GraphRank.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {
} from "@mui/material"
import {
GraphContainer,
getAxisInfo,
useGraphComponentState,
useMergedUnionSearchSpace,
} from "@optuna/react"
import { makeHovertext } from "@optuna/react"
import * as plotly from "plotly.js-dist-min"
import React, { FC, useEffect, useState } from "react"
import { SearchSpaceItem, StudyDetail, Trial } from "ts/types/optuna"
import { PlotType } from "../apiClient"
import { getAxisInfo, makeHovertext } from "../graphUtil"
import { usePlot } from "../hooks/usePlot"
import { useBackendRender, usePlotlyColorTheme } from "../state"

Expand Down
117 changes: 1 addition & 116 deletions optuna_dashboard/ts/graphUtil.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,5 @@
import * as Optuna from "@optuna/types"
import { SearchSpaceItem, StudyDetail, Trial } from "./types/optuna"

const PADDING_RATIO = 0.05

export type AxisInfo = {
name: string
isLog: boolean
isCat: boolean
indices: (string | number)[]
values: (string | number | null)[]
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const unique = (array: any[]) => {
const knownElements = new Map()
array.forEach((elem) => knownElements.set(elem, true))
return Array.from(knownElements.keys())
}

export const getAxisInfo = (
trials: Trial[],
param: SearchSpaceItem
): AxisInfo => {
if (param.distribution.type === "CategoricalDistribution") {
return getAxisInfoForCategoricalParams(
trials,
param.name,
param.distribution
)
} else {
return getAxisInfoForNumericalParams(trials, param.name, param.distribution)
}
}

const getAxisInfoForCategoricalParams = (
trials: Trial[],
paramName: string,
distribution: Optuna.CategoricalDistribution
): AxisInfo => {
const values = trials.map(
(trial) =>
trial.params.find((p) => p.name === paramName)?.param_external_value ||
null
)

const indices = distribution.choices
.map((c) => c?.value ?? "null")
.sort((a, b) =>
a.toLowerCase() < b.toLowerCase()
? -1
: a.toLowerCase() > b.toLowerCase()
? 1
: 0
)
return {
name: paramName,
isLog: false,
isCat: true,
indices,
values,
}
}

const getAxisInfoForNumericalParams = (
trials: Trial[],
paramName: string,
distribution: Optuna.FloatDistribution | Optuna.IntDistribution
): AxisInfo => {
let min = 0
let max = 0
if (distribution.log) {
const padding =
(Math.log10(distribution.high) - Math.log10(distribution.low)) *
PADDING_RATIO
min = Math.pow(10, Math.log10(distribution.low) - padding)
max = Math.pow(10, Math.log10(distribution.high) + padding)
} else {
const padding = (distribution.high - distribution.low) * PADDING_RATIO
min = distribution.low - padding
max = distribution.high + padding
}

const values = trials.map(
(trial) =>
trial.params.find((p) => p.name === paramName)?.param_internal_value ||
null
)
const indices = unique(values)
.filter((v) => v !== null)
.sort((a, b) => a - b)
if (indices.length >= 2) {
indices.unshift(min)
indices.push(max)
}
return {
name: paramName,
isLog: distribution.log,
isCat: false,
indices,
values,
}
}

export const makeHovertext = (trial: 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]: value }), {}),
},
undefined,
" "
).replace(/\n/g, "<br>")
}
import { StudyDetail } from "./types/optuna"

export const studyDetailToStudy = (
studyDetail: StudyDetail | null
Expand Down
2 changes: 2 additions & 0 deletions tslib/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ export {
useObjectiveAndUserAttrTargetsFromStudies,
} from "./utils/trialFilter"
export { useMergedUnionSearchSpace } from "./utils/searchSpace"
export { makeHovertext, getAxisInfo } from "./utils/graph"
export type { AxisInfo } from "./utils/graph"
export type { GraphComponentState } from "./types"
103 changes: 103 additions & 0 deletions tslib/react/src/utils/graph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import * as Optuna from "@optuna/types"

const PADDING_RATIO = 0.05

export type AxisInfo = {
name: string
isLog: boolean
isCat: boolean
indices: (string | number)[]
values: (string | number | null)[]
}

// biome-ignore lint/suspicious/noExplicitAny: Accept any array.
const unique = (array: any[]) => {
const knownElements = new Map()
for (const e of array) {
knownElements.set(e, true)
}
return Array.from(knownElements.keys())
}

export const makeHovertext = (trial: Optuna.Trial): string => {
return JSON.stringify(
{
Expand All @@ -16,3 +35,87 @@ export const makeHovertext = (trial: Optuna.Trial): string => {
" "
).replace(/\n/g, "<br>")
}

export const getAxisInfo = (
trials: Optuna.Trial[],
param: Optuna.SearchSpaceItem
): AxisInfo => {
if (param.distribution.type === "CategoricalDistribution") {
return getAxisInfoForCategoricalParams(
trials,
param.name,
param.distribution
)
}

return getAxisInfoForNumericalParams(trials, param.name, param.distribution)
}

const getAxisInfoForCategoricalParams = (
trials: Optuna.Trial[],
paramName: string,
distribution: Optuna.CategoricalDistribution
): AxisInfo => {
const values = trials.map(
(trial) =>
trial.params.find((p) => p.name === paramName)?.param_external_value ||
null
)

const indices = distribution.choices
.map((c) => c?.toString() ?? "null")
.sort((a, b) =>
a.toLowerCase() < b.toLowerCase()
? -1
: a.toLowerCase() > b.toLowerCase()
? 1
: 0
)
return {
name: paramName,
isLog: false,
isCat: true,
indices,
values,
}
}

const getAxisInfoForNumericalParams = (
trials: Optuna.Trial[],
paramName: string,
distribution: Optuna.FloatDistribution | Optuna.IntDistribution
): AxisInfo => {
let min = 0
let max = 0
if (distribution.log) {
const padding =
(Math.log10(distribution.high) - Math.log10(distribution.low)) *
PADDING_RATIO
min = 10 ** (Math.log10(distribution.low) - padding)
max = 10 ** (Math.log10(distribution.high) + padding)
} else {
const padding = (distribution.high - distribution.low) * PADDING_RATIO
min = distribution.low - padding
max = distribution.high + padding
}

const values = trials.map(
(trial) =>
trial.params.find((p) => p.name === paramName)?.param_internal_value ||
null
)
const indices = unique(values)
.filter((v) => v !== null)
.sort((a, b) => a - b)
if (indices.length >= 2) {
indices.unshift(min)
indices.push(max)
}
return {
name: paramName,
isLog: distribution.log,
isCat: false,
indices,
values,
}
}
Loading