spectropath from Python

spectropath is an R package. This notebook shows the simplest Python-facing workflow: call the package with Rscript, and then an optional direct bridge with rpy2.

import io
import subprocess
import pandas as pd

Path-signature features via Rscript

R_CODE = r'''
library(spectropath)

u <- seq(-5, 5, length.out = 500)
f <- dnorm(u, 0, 1) + 0.25 * dnorm(u, 1.8, 0.45)
path <- cbind(u, f)

write.csv(path_features(path), stdout(), row.names = FALSE)
'''

result = subprocess.run(
    ['Rscript', '-e', R_CODE],
    check=True,
    capture_output=True,
    text=True,
)

features = pd.read_csv(io.StringIO(result.stdout))
features

Classical summaries via Rscript

R_CODE = r'''
library(spectropath)

u <- seq(-5, 5, length.out = 500)
f <- dnorm(u, 0, 1) + 0.25 * dnorm(u, 1.8, 0.45)
path <- cbind(u, f)

write.csv(classical_features(path), stdout(), row.names = FALSE)
'''

result = subprocess.run(
    ['Rscript', '-e', R_CODE],
    check=True,
    capture_output=True,
    text=True,
)

classical = pd.read_csv(io.StringIO(result.stdout))
classical

Optional direct bridge with rpy2

Use this only if rpy2 is already installed in your Python environment.

import numpy as np
from rpy2.robjects import r
from rpy2.robjects import numpy2ri

numpy2ri.activate()
r('library(spectropath)')

u = np.linspace(-5, 5, 500)
f = np.exp(-0.5 * u**2) + 0.25 * np.exp(-0.5 * ((u - 1.8) / 0.45)**2)
path = np.column_stack([u, f])

r['path_features'](path)