From 270c406425c983468218f7fb790c599dc8b43a87 Mon Sep 17 00:00:00 2001 From: keisuke-umezawa Date: Thu, 15 Aug 2024 14:07:10 +0900 Subject: [PATCH] Add plotly color theme to PlotImportance --- tslib/react/src/components/PlotEdf.tsx | 2 +- tslib/react/src/components/PlotHistory.tsx | 152 +++++++++------------ 2 files changed, 65 insertions(+), 89 deletions(-) diff --git a/tslib/react/src/components/PlotEdf.tsx b/tslib/react/src/components/PlotEdf.tsx index f1cc5aa7..65281dcf 100644 --- a/tslib/react/src/components/PlotEdf.tsx +++ b/tslib/react/src/components/PlotEdf.tsx @@ -4,7 +4,7 @@ import * as plotly from "plotly.js-dist-min" import { FC, useEffect, useMemo } from "react" import { useGraphComponentState } from "../hooks/useGraphComponentState" import { Target, useFilteredTrialsFromStudies } from "../utils/trialFilter" -import { GraphContainer } from"./GraphContainer" +import { GraphContainer } from "./GraphContainer" import { plotlyDarkTemplate } from "./PlotlyDarkMode" export type EdfPlotInfo = { diff --git a/tslib/react/src/components/PlotHistory.tsx b/tslib/react/src/components/PlotHistory.tsx index ed0728a8..6e235997 100644 --- a/tslib/react/src/components/PlotHistory.tsx +++ b/tslib/react/src/components/PlotHistory.tsx @@ -1,5 +1,4 @@ import { - Checkbox, FormControl, FormControlLabel, FormLabel, @@ -9,16 +8,21 @@ import { RadioGroup, Select, SelectChangeEvent, - Switch, + Slider, Typography, useTheme, } from "@mui/material" import * as Optuna from "@optuna/types" import * as plotly from "plotly.js-dist-min" -import { ChangeEvent, FC, useEffect, useState, useMemo } from "react" +import { ChangeEvent, FC, useEffect, useState } from "react" import { useGraphComponentState } from "../hooks/useGraphComponentState" -import { Target, useFilteredTrialsFromStudies } from "../utils/trialFilter" +import { + Target, + useFilteredTrialsFromStudies, + useObjectiveAndUserAttrTargetsFromStudies, +} from "../utils/trialFilter" +import { plotlyDarkTemplate } from "./PlotlyDarkMode" const plotDomId = "plot-history" @@ -31,29 +35,31 @@ interface HistoryPlotInfo { export const PlotHistory: FC<{ studies: Optuna.Study[] -}> = ({ studies }) => { + colorTheme?: Partial +}> = ({ studies, colorTheme }) => { + const logScale = false + const filterPrunedTrial = false + const { graphComponentState, notifyGraphDidRender } = useGraphComponentState() const theme = useTheme() + const colorThemeUsed = + colorTheme ?? (theme.palette.mode === "dark" ? plotlyDarkTemplate : {}) const [xAxis, setXAxis] = useState< "number" | "datetime_start" | "datetime_complete" >("number") - - const [objectiveId, setObjectiveId] = useState(0) - const [logScale, setLogScale] = useState(false) - const [filterPrunedTrial, setFilterPrunedTrial] = useState(false) const [markerSize, setMarkerSize] = useState(5) - const target = useMemo( - () => new Target("objective", objectiveId), - [objectiveId] - ) + const [targets, selected, setTarget] = + useObjectiveAndUserAttrTargetsFromStudies(studies) + const trials = useFilteredTrialsFromStudies( studies, - [target], - filterPrunedTrial, + [selected], + filterPrunedTrial ) + const historyPlotInfos = studies.map((study, index) => { const h: HistoryPlotInfo = { study_name: study.name, @@ -64,8 +70,8 @@ export const PlotHistory: FC<{ return h }) - const handleObjectiveChange = (event: SelectChangeEvent) => { - setObjectiveId(event.target.value as number) + const handleObjectiveChange = (event: SelectChangeEvent) => { + setTarget(event.target.value) } const handleXAxisChange = (e: ChangeEvent) => { @@ -78,33 +84,18 @@ export const PlotHistory: FC<{ } } - const handleLogScaleChange = () => { - setLogScale(!logScale) - } - - const handleFilterPrunedChange = () => { - setFilterPrunedTrial(!filterPrunedTrial) - } - useEffect(() => { if (graphComponentState !== "componentWillMount") { plotHistory( historyPlotInfos, - target, + selected, xAxis, logScale, - theme.palette.mode, - colorTheme, - + colorThemeUsed, + markerSize ) } - }, [ - studies, - target, - logScale, - xAxis, - theme.palette.mode, - ]) + }, [graphComponentState, studies, selected, logScale, xAxis, colorThemeUsed, markerSize]) return ( @@ -115,60 +106,30 @@ export const PlotHistory: FC<{ direction="column" sx={{ paddingRight: theme.spacing(2) }} > - + History - {study !== null && study.directions.length !== 1 ? ( + {studies[0] !== null && targets.length >= 2 ? ( - Objective ID: - + {targets.map((t, i) => ( + + {t.toLabel(studies[0].metric_names)} ))} ) : null} - - Log y scale: - - - - Filter state: - - } - label="Complete" - /> - - } - label="Pruned" - /> - + + Marker size: + { + // @ts-ignore + setMarkerSize(e.target.value as number) + }} + /> +
@@ -210,7 +185,6 @@ const plotHistory = ( target: Target, xAxis: "number" | "datetime_start" | "datetime_complete", logScale: boolean, - mode: string, colorTheme: Partial, markerSize: number ) => { @@ -232,7 +206,7 @@ const plotHistory = ( b: 0, }, yaxis: { - title: target.toLabel(historyPlotInfos[0].objective_names), + title: target.toLabel(historyPlotInfos[0].metric_names), type: logScale ? "log" : "linear", }, xaxis: { @@ -257,22 +231,22 @@ const plotHistory = ( const plotData: Partial[] = [] const infeasiblePlotData: Partial[] = [] - historyPlotInfos.forEach((h) => { + for (const h of historyPlotInfos) { const feasibleTrials: Optuna.Trial[] = [] const infeasibleTrials: Optuna.Trial[] = [] - h.trials.forEach((t) => { + for (const t of h.trials) { 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.objective_names)} of ${h.study_name}`, + name: `${target.toLabel(h.metric_names)} of ${h.study_name}`, marker: { size: markerSize, }, @@ -290,7 +264,9 @@ const plotHistory = ( const value = target.getTargetValue(t) as number if (t.state !== "Complete") { continue - } else if (currentBest === null) { + } + + if (currentBest === null) { currentBest = value xForLinePlot.push(getAxisX(t)) yForLinePlot.push(value) @@ -340,13 +316,13 @@ const plotHistory = ( name: `Infeasible Trial of ${h.study_name}`, marker: { size: markerSize, - color: mode === "dark" ? "#666666" : "#cccccc", + color: colorTheme === plotlyDarkTemplate ? "#666666" : "#cccccc", }, mode: "markers", type: "scatter", showlegend: false, }) - }) + } plotData.push(...infeasiblePlotData) plotly.react(plotDomId, plotData, layout) }