Files
rustytorch/crates/specialized/rtx-neuro-python/examples/quickstart.py
T
2026-03-04 00:08:42 +00:00

120 lines
3.8 KiB
Python

"""
RTX-Neuro Quickstart Guide
RTX-Neuro is a high-performance MEG/EEG analysis library implemented in Rust
with Python bindings via PyO3.
Installation:
pip install rtx-neuro
Or build from source:
cd crates/specialized/rtx-neuro-python
maturin develop # For development
maturin build # For release wheel
"""
import rtx_neuro as rtx
import numpy as np
# Check version
print(f"RTX-Neuro version: {rtx.__version__}")
# ============================================================
# Available Modules
# ============================================================
print("\nAvailable modules:")
print("- rtx.io: File format readers")
print("- rtx.stats: Statistical analysis")
# ============================================================
# Quick Example: Complete Analysis Pipeline
# ============================================================
print("\n" + "=" * 50)
print("Quick Example: Statistical Analysis")
print("=" * 50)
# Generate synthetic data (simulating ERP amplitudes)
np.random.seed(42)
# Control group: N=30 subjects
control = np.random.normal(loc=0.0, scale=1.0, size=30)
# Treatment group: N=30 subjects with a 0.8 effect
treatment = np.random.normal(loc=0.8, scale=1.0, size=30)
# Perform statistical test
t_stat, p_value = rtx.stats.ttest_ind(control, treatment)
print(f"\nIndependent t-test:")
print(f" Control mean: {np.mean(control):.3f}")
print(f" Treatment mean: {np.mean(treatment):.3f}")
print(f" t-statistic: {t_stat:.3f}")
print(f" p-value: {p_value:.6f}")
print(f" Result: {'Significant' if p_value < 0.05 else 'Not significant'} at alpha=0.05")
# ============================================================
# Quick Example: Multiple Comparison Correction
# ============================================================
print("\n" + "=" * 50)
print("Quick Example: FDR Correction")
print("=" * 50)
# Simulate p-values from 50 statistical tests
p_values = np.concatenate([
np.random.uniform(0.001, 0.01, 5), # 5 true effects
np.random.uniform(0.1, 1.0, 45) # 45 null effects
])
np.random.shuffle(p_values)
# Apply FDR correction
rejected, corrected_p = rtx.stats.fdr_correction(p_values, alpha=0.05)
print(f"\n50 statistical tests:")
print(f" Uncorrected significant: {np.sum(p_values < 0.05)}")
print(f" FDR-corrected significant: {np.sum(rejected)}")
print(f" Smallest corrected p-value: {np.min(corrected_p):.6f}")
# ============================================================
# API Reference
# ============================================================
print("\n" + "=" * 50)
print("API Reference")
print("=" * 50)
print("""
IO Module (rtx.io):
read_raw_edf(path) - Read EDF/EDF+ files
read_raw_fif(path) - Read Elekta/Neuromag FIF files
read_raw_ctf(path) - Read CTF MEG datasets (.ds)
read_raw_bti(path) - Read 4D-Neuroimaging/BTi data
read_raw_kit(path) - Read Yokogawa/KIT data (.con, .sqd)
read_raw_egi(path) - Read EGI data (.raw, .mff)
RawData Properties:
.sfreq - Sampling frequency (Hz)
.n_channels - Number of channels
.n_samples - Number of samples
.duration - Recording duration (seconds)
.ch_names - List of channel names
.path - File path
RawData Methods:
.get_data(tmin, tmax) - Get numpy array [n_channels x n_samples]
.pick_channels(ch_names) - Get indices for specific channels
Stats Module (rtx.stats):
ttest_1samp(data, popmean=0.0) - One-sample t-test
ttest_ind(a, b) - Independent samples t-test
ttest_rel(a, b) - Paired samples t-test
permutation_t_test(a, b, n_permutations, seed) - Permutation test
fdr_correction(p_values, alpha=0.05) - FDR (BH) correction
bonferroni_correction(p_values, alpha=0.05) - Bonferroni correction
""")
print("=" * 50)
print("For more examples, see the examples/ directory.")