def visualize_optim_plot_slice()

in src/backend/domain/services/charts.py [0:0]


def visualize_optim_plot_slice(study, params: list[str] | None, mode: int = 0):
    # In-Sample
    if mode == 0:
        return optuna.visualization.plot_slice(study, params=params, target_name="In-Sample Objective Value")
    # Out-of-Sample
    elif mode == 1:
        return optuna.visualization.plot_slice(
            study,
            params=params,
            target=lambda x: x.user_attrs["test_value"],
            target_name="Out-of-Sample Objective Value",
        )
    # Both
    slice_in_sample = optuna.visualization.plot_slice(study, params=params, target_name="Objective Value")
    slice_out_of_sample = optuna.visualization.plot_slice(
        study,
        params=params,
        target=lambda x: x.user_attrs["test_value"],
        target_name="Out-of-Sample Objective Value",
    )
    slice_in_sample.data[0].marker["colorbar"]["title"]["text"] = "In Trial"
    slice_in_sample.data[0].marker["colorbar"]["xpad"] = 20
    slice_out_of_sample.data[0].marker["colorbar"]["title"]["text"] = "Out Trial"
    slice_out_of_sample.data[0].marker["colorbar"]["xpad"] = 100

    for i in range(len(slice_in_sample.data)):
        slice_in_sample.data[i].name = "in sample"

    for i in range(len(slice_out_of_sample.data)):
        slice_out_of_sample.data[i].name = "out of sample"
        slice_out_of_sample.data[i].marker["colorscale"] = (
            (0.0, "rgb(255,247,251)"),
            (0.125, "rgb(247,222,235)"),
            (0.25, "rgb(239,198,219)"),
            (0.375, "rgb(225,158,202)"),
            (0.5, "rgb(214,107,174)"),
            (0.625, "rgb(198,66,146)"),
            (0.75, "rgb(181,33,113)"),
            (0.875, "rgb(156,8,81)"),
            (1.0, "rgb(107,8,48)"),
        )
    return go.Figure(data=slice_in_sample.data + slice_out_of_sample.data, layout=slice_in_sample.layout)