import * as plotly from "plotly.js-dist-min" import React, { FC, useEffect } from "react" import { Typography, useTheme, Box, Card, CardContent } from "@mui/material" import { plotlyDarkTemplate } from "./PlotlyDarkMode" import { actionCreator } from "../action" import { useParamImportanceValue, useStudyDirections } from "../state" const plotDomId = "graph-hyperparameter-importances" export const GraphHyperparameterImportance: FC<{ studyId: number study: StudyDetail | null graphHeight: string }> = ({ studyId, study = null, graphHeight }) => { const theme = useTheme() const action = actionCreator() const importances = useParamImportanceValue(studyId) const numCompletedTrials = study?.trials.filter((t) => t.state === "Complete").length || 0 const nObjectives = useStudyDirections(studyId)?.length const objectiveNames: string[] = study?.objective_names || study?.directions.map((d, i) => `Objective ${i}`) || [] useEffect(() => { action.updateParamImportance(studyId) }, [numCompletedTrials]) useEffect(() => { if (importances !== null && nObjectives === importances.length) { plotParamImportance(importances, objectiveNames, theme.palette.mode) } }, [nObjectives, importances, theme.palette.mode]) return ( Hyperparameter Importance ) } const plotParamImportance = ( importances: ParamImportance[][], objectiveNames: string[], mode: string ) => { const layout: Partial = { xaxis: { title: "Hyperparameter Importance", }, yaxis: { title: "Hyperparameter", automargin: true, }, margin: { l: 50, t: 0, r: 50, b: 50, }, barmode: "group", bargap: 0.15, bargroupgap: 0.1, uirevision: "true", template: mode === "dark" ? plotlyDarkTemplate : {}, } if (document.getElementById(plotDomId) === null) { return } const traces: Partial[] = importances.map( (importance, i) => { const reversed = [...importance].reverse() const importance_values = reversed.map((p) => p.importance) const param_names = reversed.map((p) => p.name) const param_hover_templates = reversed.map( (p) => `${p.name} (${p.distribution}): ${p.importance} ` ) return { type: "bar", orientation: "h", name: objectiveNames[i], x: importance_values, y: param_names, text: importance_values.map((v) => String(v.toFixed(2))), textposition: "outside", hovertemplate: param_hover_templates, } } ) plotly.react(plotDomId, traces, layout) }