Skip to content

blackboxopt.visualizations.utils

get_incumbent_objective_over_time_single_fidelity(objective, objective_values, times, fidelities, target_fidelity)

Filter for results with given target fidelity and generate incumbent trace.

Source code in blackboxopt/visualizations/utils.py
def get_incumbent_objective_over_time_single_fidelity(
    objective: Objective,
    objective_values: np.ndarray,
    times: np.ndarray,
    fidelities: np.ndarray,
    target_fidelity: float,
):
    """Filter for results with given target fidelity and generate incumbent trace."""
    # filter out fidelity and take min/max of objective_values
    idx = np.logical_and(fidelities == target_fidelity, np.isfinite(objective_values))
    _times = times[idx]
    if objective.greater_is_better:
        _objective_values = np.maximum.accumulate(objective_values[idx])
    else:
        _objective_values = np.minimum.accumulate(objective_values[idx])
    # get unique objective values and sort their indices (to be in chronological order)
    _, idx = np.unique(_objective_values, return_index=True)
    idx.sort()
    # find objective_values
    _objective_values = _objective_values[idx]
    _times = _times[idx]
    # add steps where a new incumbent was found
    _times = np.repeat(_times, 2)[1:]
    _objective_values = np.repeat(_objective_values, 2)[:-1]
    # append best value for largest time to extend the lines
    _times = np.concatenate([_times, np.nanmax(times, keepdims=True)])
    _objective_values = np.concatenate([_objective_values, _objective_values[-1:]])
    return _times, _objective_values

patch_plotly_io_to_html(method)

Patch plotly.io.to_html with additional javascript to improve usability.

Might become obsolete, when https://github.com/plotly/plotly.js/issues/998 gets fixed.

Injects <script>-tag with content from to_html_patch.js at the end of the HTML output. But only, if the chart title starts with "[BBO]" (to minimize side effects, if the user uses plotly.io for something else).

plotly.io.to_html is also internally used for figure.show() and figure.to_html(), so this is covered, too.

Parameters:

Name Type Description Default
method Callable

Original plotly.io.to_html method.

required

Returns:

Type Description
Callable

Patched method.

Source code in blackboxopt/visualizations/utils.py
def patch_plotly_io_to_html(method: Callable) -> Callable:
    """Patch `plotly.io.to_html` with additional javascript to improve usability.

    Might become obsolete, when https://github.com/plotly/plotly.js/issues/998 gets
    fixed.

    Injects `<script>`-tag with content from `to_html_patch.js` at the end of the HTML
    output. But only, if the chart title starts with "[BBO]" (to minimize side
    effects, if the user uses `plotly.io` for something else).

    `plotly.io.to_html` is also internally used for `figure.show()` and
    `figure.to_html()`, so this is covered, too.

    Args:
        method: Original `plotly.io.to_html` method.

    Returns:
        Patched method.
    """

    @wraps(method)
    def wrapped(*args, **kwargs):
        html = method(*args, **kwargs)

        # Test if title text contains "[BBO]"
        if html.find('"title":{"text":"[BBO]') < 0:
            return html

        js = importlib.resources.read_text(
            blackboxopt.visualizations, "to_html_patch.js"
        )
        html_to_inject = f"<script>{js}</script>"
        insert_idx = html.rfind("</body>")
        if insert_idx >= 0:
            # Full html page got rendered, inject <script> before <\body>
            html = html[:insert_idx] + html_to_inject + html[insert_idx:]
        else:
            # Only chart part got rendered: append <script> at the end
            html = html + html_to_inject

        return html

    return wrapped