Skip to content

Commit

Permalink
Use plot_param_importances in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
knshnb committed Nov 7, 2023
1 parent e0c89e2 commit e135860
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions optuna_dashboard/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ def get_param_importances(study_id: int) -> dict[str, Any]:
response.status = 400 # Bad request
return {"reason": str(e)}

@app.get("/api/studies/<study_id:int>/param_importances_plot")
@json_api_view
def get_param_importances_plot(study_id: int) -> dict[str, Any]:
study = optuna.load_study(

Check warning on line 262 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L262

Added line #L262 was not covered by tests
study_name=storage.get_study_name_from_id(study_id), storage=storage
)
fig = optuna.visualization.plot_param_importances(study)
return fig.to_json()

Check warning on line 266 in optuna_dashboard/_app.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/_app.py#L265-L266

Added lines #L265 - L266 were not covered by tests

@app.put("/api/studies/<study_id:int>/note")
@json_api_view
def save_study_note(study_id: int) -> dict[str, Any]:
Expand Down
12 changes: 12 additions & 0 deletions optuna_dashboard/ts/components/GraphHyperparameterImportances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { plotlyDarkTemplate } from "./PlotlyDarkMode"
import { actionCreator } from "../action"
import { useParamImportanceValue, useStudyDirections } from "../state"
const plotDomId = "graph-hyperparameter-importances"
const plotBackendDomId = "graph-hyperparameter-importances-backend"

export const GraphHyperparameterImportance: FC<{
studyId: number
Expand All @@ -27,6 +28,16 @@ export const GraphHyperparameterImportance: FC<{
action.updateParamImportance(studyId)
}, [numCompletedTrials])

useEffect(() => {
fetch(`/api/studies/${studyId}/param_importances_plot`, {mode: 'cors'})
.then((response) => response.json())
.then((figure) => {
plotly.react(plotBackendDomId, figure.data, figure.layout)
}).catch((err) => {
console.error(err);
})
}, [numCompletedTrials])

useEffect(() => {
if (importances !== null && nObjectives === importances.length) {
plotParamImportance(importances, objectiveNames, theme.palette.mode)
Expand All @@ -43,6 +54,7 @@ export const GraphHyperparameterImportance: FC<{
Hyperparameter Importance
</Typography>
<Box id={plotDomId} sx={{ height: graphHeight }} />
<Box id={plotBackendDomId} sx={{ height: graphHeight }} />
</CardContent>
</Card>
)
Expand Down

0 comments on commit e135860

Please sign in to comment.