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

147 lines
3.9 KiB
Python

"""
RTX-Neuro Example: Reading MEG/EEG Data
This example demonstrates how to read neuroimaging data from various file formats
using the rtx_neuro library.
Supported formats:
- EDF/EDF+ (European Data Format)
- FIF (Elekta/Neuromag)
- CTF (.ds directories)
- BTi/4D-Neuroimaging
- KIT/Yokogawa/Ricoh (.con, .sqd)
- EGI (.raw, .mff)
"""
import rtx_neuro as rtx
import numpy as np
# Example 1: Read EDF file
def read_edf_example():
"""Read an EDF/EDF+ file."""
# Replace with your actual file path
raw = rtx.io.read_raw_edf("path/to/recording.edf")
# Access metadata
print(f"Sampling frequency: {raw.sfreq} Hz")
print(f"Number of channels: {raw.n_channels}")
print(f"Number of samples: {raw.n_samples}")
print(f"Duration: {raw.duration:.2f} seconds")
print(f"Channel names: {raw.ch_names[:5]}...") # First 5 channels
# Get data as numpy array
# Full data
data = raw.get_data()
print(f"Data shape: {data.shape}") # (n_channels, n_samples)
# Specific time window
data_segment = raw.get_data(tmin=0.0, tmax=10.0) # First 10 seconds
print(f"Segment shape: {data_segment.shape}")
return raw, data
# Example 2: Read FIF file (Elekta/Neuromag MEG)
def read_fif_example():
"""Read an Elekta/Neuromag FIF file."""
raw = rtx.io.read_raw_fif("path/to/recording.fif")
print(f"FIF file: {raw.path}")
print(f"MEG channels: {raw.n_channels}")
# Get specific channels by name
channel_indices = raw.pick_channels(["MEG0111", "MEG0121", "MEG0131"])
print(f"Picked channel indices: {channel_indices}")
return raw
# Example 3: Read CTF MEG data
def read_ctf_example():
"""Read a CTF MEG dataset (.ds directory)."""
raw = rtx.io.read_raw_ctf("path/to/dataset.ds")
print(f"CTF dataset loaded")
print(f"Channels: {raw.n_channels}")
print(f"Sample rate: {raw.sfreq} Hz")
return raw
# Example 4: Read BTi/4D-Neuroimaging data
def read_bti_example():
"""Read 4D-Neuroimaging/BTi MEG data."""
raw = rtx.io.read_raw_bti("path/to/bti_directory")
print(f"BTi data loaded")
print(f"Channels: {raw.n_channels}")
return raw
# Example 5: Read KIT/Yokogawa data
def read_kit_example():
"""Read Yokogawa/KIT/Ricoh MEG data."""
raw = rtx.io.read_raw_kit("path/to/recording.con")
# Or for .sqd files:
# raw = rtx.io.read_raw_kit("path/to/recording.sqd")
print(f"KIT data loaded")
print(f"Channels: {raw.n_channels}")
return raw
# Example 6: Read EGI data
def read_egi_example():
"""Read EGI data (.raw or .mff format)."""
# For .raw files:
raw = rtx.io.read_raw_egi("path/to/recording.raw")
# For MFF directories:
# raw = rtx.io.read_raw_egi("path/to/recording.mff")
print(f"EGI data loaded")
print(f"Channels: {raw.n_channels}")
return raw
# Example 7: Working with data
def data_manipulation_example():
"""Demonstrate data manipulation workflows."""
raw = rtx.io.read_raw_edf("path/to/recording.edf")
# Get full data
data = raw.get_data()
# Basic numpy operations
mean_signal = np.mean(data, axis=1) # Mean per channel
max_amplitude = np.max(np.abs(data))
# Time vector
times = np.arange(raw.n_samples) / raw.sfreq
# Select specific channels
eeg_indices = raw.pick_channels(["Fp1", "Fp2", "Fz"])
selected_data = data[eeg_indices, :]
print(f"Selected {len(eeg_indices)} channels")
print(f"Max amplitude: {max_amplitude:.2f} uV")
return data, times
if __name__ == "__main__":
print("RTX-Neuro Data Reading Examples")
print("=" * 40)
print("\nNote: Replace file paths with your actual data files.")
print("\nAvailable functions:")
print("- read_edf_example()")
print("- read_fif_example()")
print("- read_ctf_example()")
print("- read_bti_example()")
print("- read_kit_example()")
print("- read_egi_example()")
print("- data_manipulation_example()")