A better notebook experience

Contents

Go to notebook file


[1]:
%reload_ext eradiate.notebook.tutorials

Last updated: 2024-03-07 15:50 (eradiate v0.25.1.dev31+g2a83b0ba.d20240229)

A better notebook experience#

Overview

This tutorial demonstrates features which make the experience of using Eradiate in a Jupyter notebook more enjoyable.

What you will learn

  • How to load the Eradiate IPython extension.

  • How it improves the notebook experience.


First, we load Eradiate and select the CKD mode.

[2]:
import eradiate
from eradiate import unit_registry as ureg
eradiate.set_mode("ckd")

Let’s define a very simulation with a single spectral bin, then visualise it:

[3]:
# Basic behaviour: looks bad
exp = eradiate.experiments.AtmosphereExperiment(
    measures={
        "type": "mdistant",
        "construct": "hplane",
        "zeniths": [-30, 0, 30],
        "azimuth": 0,
        "srf": {
            "type": "multi_delta",
            "wavelengths": 550.0 * ureg.nm,
        },
    }
)
exp
[3]:
AtmosphereExperiment(measures=[MultiDistantMeasure(id='measure', mi_results={}, srf=MultiDeltaSpectrum(id=None, quantity=DIMENSIONLESS, wavelengths=[550.0] nm), sampler='independent', rfilter='box', spp=1000, target=TargetPoint(xyz=[0.0 0.0 0.0] m), ray_offset=None, direction_layout=HemispherePlaneLayout(azimuth_convention=<AzimuthConvention.EAST_RIGHT: (0.0, 1)>, zeniths=[-30   0  30] deg, azimuth=0 deg))], quad_spec=QuadSpecFixed(n=16, quad_type=<QuadType.GAUSS_LEGENDRE: 'gauss_legendre'>), extra_objects={}, illumination=DirectionalIllumination(id='illumination', zenith=0.0 deg, azimuth=0.0 deg, azimuth_convention=<AzimuthConvention.EAST_RIGHT: (0.0, 1)>, irradiance=SolarIrradianceSpectrum(id=None, dataset=<xarray.Dataset | source='/home/leroyv/Documents/src/rayference/rtm/eradiate/resources/data/spectra/solar_irradiance/coddington_2021-1_nm.nc'>, scale=1.0, datetime=None)), geometry=PlaneParallelGeometry(toa_altitude=120.0 km, ground_altitude=0.0 km, zgrid=ZGrid(levels=[0.0 100.0 200.0 ... 119800.0 119900.0 120000.0] m, _layers=[50.0 150.0 250.0 ... 119750.0 119850.0 119950.0] m, _layer_height=100.0 m, _total_height=120000.0 m), width=1000000.0 km), atmosphere=HomogeneousAtmosphere(id='atmosphere', geometry=PlaneParallelGeometry(toa_altitude=120.0 km, ground_altitude=0.0 km, zgrid=ZGrid(levels=[0.0 100.0 200.0 ... 119800.0 119900.0 120000.0] m, _layers=[50.0 150.0 250.0 ... 119750.0 119850.0 119950.0] m, _layer_height=100.0 m, _total_height=120000.0 m), width=1000000.0 km), sigma_s=AirScatteringCoefficientSpectrum(id=None), sigma_a=UniformSpectrum(id=None, quantity=COLLISION_COEFFICIENT, value=0.0 1/m), _phase=RayleighPhaseFunction(id='phase_atmosphere')), surface=BasicSurface(id='surface', shape=None, bsdf=LambertianBSDF(id='surface_bsdf', reflectance=UniformSpectrum(id=None, quantity=REFLECTANCE, value=0.5))), _integrator=VolPathIntegrator(id='integrator', timeout=None, max_depth=None, rr_depth=None, hide_emitters=None))

Arguably, this ouput is impractical and makes the process of inspecting the contents of the OneDimExperiment instance quite challenging. Anyway, let’s run the corresponding simulation:

[4]:
eradiate.run(exp);

The produced output is uncontrolled and mixes progress information coming from Eradiate (the “Spectral loop” progress bar), but also from the Mitsuba radiometric kernel (the “Rendering” progress bar). A notable consequence is that changing the progress level using config.progress to suppress Mitsuba’s progress will not have the expected effect.

This can be improved by loading Eradiate’s IPython extension as follows:

[5]:
%load_ext eradiate

This automatically loads Rich’s “pretty” and “traceback” extensions, and it redirects Mitsuba’s logs and progress to, respectively, the standard Python logging framework and the tqdm library.

A first noticeable effect is that all objects created using the attrs framework are pretty-printed and are consequently much more readable:

[6]:
eradiate.experiments.AtmosphereExperiment()
[6]:

AtmosphereExperiment(
    measures=[
        MultiDistantMeasure(
            id='measure',
            mi_results={},
            srf=MultiDeltaSpectrum(id=None, quantity=DIMENSIONLESS, wavelengths=[550.0] nm),
            sampler='independent',
            rfilter='box',
            spp=1000,
            target=TargetPoint(xyz=[0.0 0.0 0.0] m),
            ray_offset=None,
            direction_layout=DirectionLayout(
                azimuth_convention=<AzimuthConvention.EAST_RIGHT: (0.0, 1)>,
                _directions=array([[0, 0, 1]])
            )
        )
    ],
    quad_spec=QuadSpecFixed(n=16, quad_type=<QuadType.GAUSS_LEGENDRE: 'gauss_legendre'>),
    extra_objects={},
    illumination=DirectionalIllumination(
        id='illumination',
        zenith=0.0 deg,
        azimuth=0.0 deg,
        azimuth_convention=<AzimuthConvention.EAST_RIGHT: (0.0, 1)>,
        irradiance=SolarIrradianceSpectrum(
            id=None,
            dataset=<xarray.Dataset | source='/home/leroyv/Documents/src/rayference/rtm/eradiate/resources/data/spectra/solar_irradiance/coddington_2021-1_nm.nc'>,
            scale=1.0,
            datetime=None
        )
    ),
    geometry=PlaneParallelGeometry(
        toa_altitude=120.0 km,
        ground_altitude=0.0 km,
        zgrid=ZGrid(
            levels=[0.0 100.0 200.0 ... 119800.0 119900.0 120000.0] m,
            _layers=[50.0 150.0 250.0 ... 119750.0 119850.0 119950.0] m,
            _layer_height=100.0 m,
            _total_height=120000.0 m
        ),
        width=1000000.0 km
    ),
    atmosphere=HomogeneousAtmosphere(
        id='atmosphere',
        geometry=PlaneParallelGeometry(
            toa_altitude=120.0 km,
            ground_altitude=0.0 km,
            zgrid=ZGrid(
                levels=[0.0 100.0 200.0 ... 119800.0 119900.0 120000.0] m,
                _layers=[50.0 150.0 250.0 ... 119750.0 119850.0 119950.0] m,
                _layer_height=100.0 m,
                _total_height=120000.0 m
            ),
            width=1000000.0 km
        ),
        sigma_s=AirScatteringCoefficientSpectrum(id=None),
        sigma_a=UniformSpectrum(id=None, quantity=COLLISION_COEFFICIENT, value=0.0 1/m),
        _phase=RayleighPhaseFunction(id='phase_atmosphere')
    ),
    surface=BasicSurface(
        id='surface',
        shape=None,
        bsdf=LambertianBSDF(
            id='surface_bsdf',
            reflectance=UniformSpectrum(id=None, quantity=REFLECTANCE, value=0.5)
        )
    ),
    _integrator=VolPathIntegrator(
        id='integrator',
        timeout=None,
        max_depth=None,
        rr_depth=None,
        hide_emitters=None
    )
)

A second consequence is that progress display control now works as expected:

[7]:
eradiate.config.progress = "KERNEL"
eradiate.run(exp);
[8]:
eradiate.config.progress = "SPECTRAL_LOOP"
eradiate.run(exp);

Further reading#

The extension can also be activated by an explicit call to eradiate.notebook.install(), which allows for further customisation.