Advertisement
El_Chaderino

Qeeg Topomap Script

Oct 19th, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. import mne
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from mne.time_frequency import psd_welch
  5.  
  6. # Load the EDF file
  7. edf_file_path = '*.edf'  # Replace with your file path
  8. raw = mne.io.read_raw_edf(edf_file_path, preload=True)
  9.  
  10. # Select channels for analysis
  11. selected_channels = ['Cz-LE', 'O1-LE', 'O2-LE', 'F3-LE', 'F4-LE', 'Fz-LE', 'Pz-LE']
  12. raw_selected = raw.copy().pick_channels(selected_channels)
  13.  
  14. # Rename the channels to match the standard 10-20 montage naming convention
  15. rename_mapping = {
  16.     'F3-LE': 'F3', 'Fz-LE': 'Fz', 'F4-LE': 'F4',
  17.     'Cz-LE': 'Cz', 'Pz-LE': 'Pz', 'O1-LE': 'O1', 'O2-LE': 'O2'
  18. }
  19. raw_selected.rename_channels(rename_mapping)
  20.  
  21. # Apply a standard 10-20 EEG montage
  22. montage = mne.channels.make_standard_montage('standard_1020')
  23. raw_selected.set_montage(montage)
  24.  
  25. # Apply a bandpass filter to retain frequencies between 1 and 45 Hz
  26. raw_selected.filter(1., 45., fir_design='firwin')
  27.  
  28. # Apply Notch filter to remove power line noise at 50 Hz (or 60 Hz depending on your region)
  29. raw_selected.notch_filter(freqs=[50])
  30.  
  31. # Re-reference to the average (common re-referencing step in qEEG analysis)
  32. raw_selected.set_eeg_reference('average', projection=True)
  33.  
  34. # Detect and annotate bad segments (e.g., muscle artifacts, eye blinks)
  35. raw_selected = mne.preprocessing.annotate_muscle_zscore(raw_selected, ch_type='eeg', threshold=4.0)
  36. raw_selected = mne.preprocessing.annotate_movement(raw_selected, threshold=5.0)
  37. raw_selected = mne.preprocessing.annotate_flat(raw_selected, bad_percent=5.0)
  38.  
  39. # Interpolate bad channels (if any were marked during artifact detection)
  40. raw_selected.interpolate_bads()
  41.  
  42. # Apply the average reference projection
  43. raw_selected.apply_proj()
  44.  
  45. # Apply ICA for advanced artifact removal
  46. ica = mne.preprocessing.ICA(n_components=15, random_state=97, max_iter='auto')
  47. ica.fit(raw_selected)
  48.  
  49. # Detect and exclude components related to eye blinks or cardiac artifacts
  50. ica.detect_artifacts(raw_selected)
  51. raw_selected = ica.apply(raw_selected)
  52.  
  53. # Calculate Power Spectral Density (PSD) for each channel
  54. psds, freqs = psd_welch(raw_selected, fmin=1, fmax=45, n_fft=2048)
  55.  
  56. # Average the PSD values across each frequency band
  57. band_powers = {
  58.     'Delta': (0.5, 4),
  59.     'Theta': (4, 8),
  60.     'Alpha': (8, 12),
  61.     'Beta': (12, 30),
  62.     'Gamma': (30, 45)
  63. }
  64.  
  65. # Create a dictionary to store averaged band power for each channel
  66. avg_band_powers = {band: [] for band in band_powers}
  67.  
  68. for band, (fmin, fmax) in band_powers.items():
  69.     # Find the indices of frequencies within the desired band
  70.     band_idx = np.where((freqs >= fmin) & (freqs <= fmax))[0]
  71.     # Average the power spectral density across the band for each channel
  72.     avg_band_powers[band] = np.mean(psds[:, band_idx], axis=1)
  73.  
  74. # Plot topographic maps for each frequency band
  75. fig, axes = plt.subplots(1, 5, figsize=(20, 4))
  76. fig.suptitle('Topographic Maps of EEG Frequency Bands', fontsize=16)
  77.  
  78. for i, (band, power) in enumerate(avg_band_powers.items()):
  79.     mne.viz.plot_topomap(power, raw_selected.info, axes=axes[i], show=False)
  80.     axes[i].set_title(f'{band} Band')
  81.  
  82. plt.show()
Tags: eeg qeeg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement