Validation with PySPEDAS

Performance Notes
  • Calling Python from Julia (via PythonCall.jl) introduces only a negligible overhead, typically within nanoseconds.
  • Memory allocations shown in Julia benchmarks do not include allocations that occur within Python. To measure Python-side allocations, profiling should be done directly in Python.
  • The documentation and benchmarks are generated using GitHub Actions. Running the code locally with multiple threads (e.g., by setting JULIA_NUM_THREADS) can yield even greater performance improvements for Julia.
using PlasmaWaves
using PySPEDAS, PythonCall
using DimensionalData
using PySPEDAS: get_data
using SpaceDataModel
using CairoMakie, SpacePhysicsMakie
    CondaPkg Found dependencies: /home/runner/.julia/packages/CondaPkg/Pc7ZA/CondaPkg.toml
    CondaPkg Found dependencies: /home/runner/.julia/packages/PySPEDAS/638Cp/CondaPkg.toml
    CondaPkg Found dependencies: /home/runner/.julia/packages/PythonCall/trYos/CondaPkg.toml
    CondaPkg Resolving changes
             + certifi
             + libpython
             + netcdf4
             + openssl
             + pyspedas (pip)
             + python
             + python-gil
             + uv
    CondaPkg Initialising pixi
             │ /home/runner/.julia/artifacts/8429d049f6c01106c62971e9540e146dc068df25/bin/pixi
             │ init
             │ --format pixi
             └ /home/runner/work/PlasmaWaves.jl/PlasmaWaves.jl/docs/.CondaPkg
✔ Created /home/runner/work/PlasmaWaves.jl/PlasmaWaves.jl/docs/.CondaPkg/pixi.toml
    CondaPkg Wrote /home/runner/work/PlasmaWaves.jl/PlasmaWaves.jl/docs/.CondaPkg/pixi.toml
             │ [dependencies]
             │ python-gil = "*"
             │ openssl = ">=3, <3.6"
             │ uv = ">=0.4"
             │ certifi = "*"
             │ netcdf4 = "*"
             │
             │     [dependencies.python]
             │     version = ">=3.10,<3.15, >=3.10,!=3.14.0,!=3.14.1,<4"
             │     build = "*cp*"
             │     channel = "conda-forge"
             │
             │     [dependencies.libpython]
             │     version = "*"
             │     channel = "conda-forge"
             │
             │ [pypi-dependencies.pyspedas]
             │ git = "https://github.com/spedas/pyspedas"
             │
             │ [workspace]
             │ name = ".CondaPkg"
             │ description = "automatically generated by CondaPkg.jl"
             │ platforms = ["linux-64"]
             │ channel-priority = "strict"
             └ channels = ["conda-forge"]
    CondaPkg Installing packages
             │ /home/runner/.julia/artifacts/8429d049f6c01106c62971e9540e146dc068df25/bin/pixi
             │ install
             └ --manifest-path /home/runner/work/PlasmaWaves.jl/PlasmaWaves.jl/docs/.CondaPkg/pixi.toml
✔ The default environment has been installed.

Wave polarization

@py import pyspedas.analysis.tests.test_twavpol: TwavpolDataValidation
TwavpolDataValidation.setUpClass()

thc_scf_fac = get_data("thc_scf_fac") |> DimArray
py_tvars = [
    "thc_scf_fac_powspec",
    "thc_scf_fac_degpol",
    "thc_scf_fac_waveangle",
    "thc_scf_fac_elliptict",
    "thc_scf_fac_helict",
]
# PySPEDAS returns non valid values at the first and last frequency bin, the last time bin is also not valid
_subset_py(x) = x[1:end-1,2:end-1]
# The PySPEDAS metadata is a Python-backed dict that cannot hold Julia functions, so copy it
function _colorscale(x, f)
    meta = Dict{Any,Any}(:colorscale => f, pairs(metadata(x))...)
    rebuild(x; metadata = SpaceDataModel.SchemaDict(SpaceDataModel.get_schema(x), meta))
end
py_arrays = _subset_py.(DimArray.(get_data.(py_tvars)))
py_result = DimStack([
    _colorscale(py_arrays[1], log10),
    py_arrays[2:end-1]...,
    _colorscale(py_arrays[end], identity),
])
res = twavpol(thc_scf_fac)

f = Figure(; size=(1200, 800))
tplot(f[1,1], py_result)
tplot(f[1,2], res[(:power, :degpol, :waveangle, :ellipticity_perp, :ellipticity)])
f
Example block output

Intended differences from PySPEDAS:

  • power at the first and last bin is not doubled (one-sided PSD), and the Nyquist bin is kept.
  • Polarization parameters are NaN at the edge bins instead of undefined values.
  • ellipticity_perp corresponds to PySPEDAS elliptict and abs(ellipticity) to helict. Rows of the spectral matrix are weighted by power, and ellipticity_perp differs for tilted ellipses; see Means.

Other methods: singular value decomposition (Santolik) and the principal eigenvector (Samson).

res = twavpol(thc_scf_fac; method = Santolik())
tplot(res)
Example block output

Benchmark

using Chairmarks

@b twavpol(thc_scf_fac), twavpol(thc_scf_fac; method = Samson()), twavpol(thc_scf_fac; method = Santolik()), pyspedas.twavpol("thc_scf_fac")
(5.438 ms (185 allocs: 1.523 MiB), 8.833 ms (185 allocs: 1.523 MiB), 8.596 ms (191 allocs: 1.586 MiB), 4.341 s (6 allocs: 112 bytes))

Julia is about 100 times faster than Python.

References