jupyter / jupyter-sphinx

Sphinx extension for rendering of Jupyter interactive widgets.
https://jupyter-sphinx.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
188 stars 65 forks source link

Static and interactive figures are not rendered #277

Open eurunuela opened 4 months ago

eurunuela commented 4 months ago

I am trying to add a page to my docs with some examples that contain static and interactive (plotly) figures. However, after installing jupyter_sphinx and adding it to my config.py file, the code does not seem to be running and generating the figures.

I don't see any error messages related to this file when building the documentation, so I don't quite understand why it doesn't work. Could you please help me find a solution?

Here's my examples.md file.

# Examples

.. jupyter-kernel:: python3

.. jupyter-execute::
    :hide-code:

    import numpy as np
    import plotly.graph_objects as go
    import pandas as pd

    from pySPFM.deconvolution.hrf_generator import HRFMatrix
    from pySPFM.deconvolution.lars import solve_regularization_path
    import matplotlib.pyplot as plt

    n_scans = 760
    tr = 1

    noise_level = 1.5

    onsets = np.zeros(n_scans)

    hrf_generator = HRFMatrix(te=[0], block=False)
    hrf = hrf_generator.generate_hrf(tr=tr, n_scans=n_scans).hrf_

    onsets = np.zeros(n_scans)
    onsets[20:24] = 1
    onsets[50:64] = 1
    onsets[67:72] = 1
    onsets[101:124] = 1
    onsets[133:140] = 1
    onsets[311:324] = 1
    onsets[372:374] = 1
    onsets[420:424] = 1
    onsets[450:464] = 1
    onsets[467:472] = 1
    onsets[501:524] = 1
    onsets[550:564] = 1
    onsets[567:572] = 1
    onsets[601:624] = 1
    onsets[660:664] = 1
    onsets[701:714] = 1
    onsets[730:744] = 1

    data_clean = np.dot(hrf, onsets)
    data = data_clean + np.random.normal(0, noise_level, data_clean.shape)
    data_spc = (data - np.mean(data))/np.mean(data)
    data_spc = data_spc/np.sqrt(np.sum(data_spc**2, axis=0))

    plt.figure(figsize=(15,5))
    plt.plot(data_spc, label="Simulated")
    plt.plot(onsets*0.1, label="Onsets")
    plt.tight_layout()
    plt.xlabel("Time (s)")
    plt.ylabel("Amplitude (psc)")

.. jupyter-execute::
    :hide-code:

    _, lambda_opt, coef_path, lambdas = solve_regularization_path(hrf, data_spc, n_scans)
    df = pd.DataFrame(coef_path)
    df["Time"] = np.arange(0, n_scans, 1)
    df["Lambdas"] = lambdas

    lambda_opt_id = np.where(lambdas == lambda_opt)[0][0]
    df_clean = pd.DataFrame()
    df_clean["Data"] = data_clean*0.01
    df_clean["Time"] = np.arange(0, n_scans, 1)

    # Estimates figure
    fig = go.Figure()

    # Add traces, one for each slider step
    for step in np.arange(0, n_scans, 1):
        fig.add_trace(
            go.Scatter(visible=False,
                    line=dict(color="blue", width=1),
                    name=f"Lambda = {lambdas[step]}",
                    x=np.arange(0, n_scans, 1),
                    y=coef_path[:, step]
                    )
        )

    fig.data[lambda_opt_id].visible = True

    # Create and add slider
    steps = []
    for i in range(len(fig.data)):
        step = dict(
            method="update",
            args=[{"visible": [False] * len(fig.data)},
                {"title": f"Estimates with lambda = {lambdas[i]}"}],
        )
        step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible"
        steps.append(step)

    sliders = [dict(
        active = lambda_opt_id,
        currentvalue={"prefix": "Lambda: "},
        pad={"t": 50},
        steps=steps
    )]

    bold = go.Scatter(visible=True,
            line=dict(color="green", width=0.5),
            name="Simulated BOLD data",
            x=np.arange(0, n_scans, 1),
            y=data_clean*0.01
            )
    fig.add_trace(bold)

    fig.update_layout(
        sliders=sliders,
        xaxis=dict(title='Time (s)'),
        yaxis=dict(title='Amplitude'),
    )

    fig.show()