Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import numpy as np
  5. import mne
  6. from mne.preprocessing import ICA
  7.  
  8. argc = len(sys.argv)
  9.  
  10. # lists of data sources
  11. subjects = ('CC110033', 'CC110037', 'CC110045')
  12. # different origins
  13. archs = ('passive', 'rest', 'task')
  14.  
  15. id_subject = 0
  16. id_arch = 0
  17. subject = subjects[id_subject]
  18. arch = archs[id_arch]
  19.  
  20. subjects_dir = 'subjects'
  21.  
  22. # load raw data from the first subject
  23. raw_data = mne.io.Raw(subject + '/' + arch + '/' + arch + '_raw.fif')
  24.  
  25. # display the channels. expert can select a channel to 'blacklist' it
  26. if argc > 1 and sys.argv[1] == '-p':
  27. #raw_data.plot(block=True, lowpass=40)
  28. raw_data.plot_psd()
  29.  
  30. # filter the bad channels
  31. calibration_file = 'sss_cal.dat'
  32. cross_talk_file = 'ct_sparse.fif'
  33. preprocessed_data = mne.preprocessing.maxwell_filter(raw_data,
  34. calibration=calibration_file,
  35. cross_talk=cross_talk_file)
  36.  
  37. # display the channels after filtering
  38. if argc > 1 and sys.argv[1] == '-p':
  39. preprocessed_data.plot(block=True, lowpass=40)
  40. preprocessed_data.plot_psd()
  41.  
  42. # keep frequencies from 1Hz to 45Hz (remove noise and signal of non-interest)
  43. low_freq = 1
  44. high_freq = 45
  45. bandpass_data = preprocessed_data.filter(low_freq, high_freq)
  46.  
  47. # checked that the filtered frequencies are dumped
  48. if argc > 1 and sys.argv[1] == '-p':
  49. #bandpass_data.plot(block=True, lowpass=40)
  50. bandpass_data.plot_psd()
  51.  
  52. # compute ICA
  53. picks_meg = mne.pick_types(bandpass_data.info, meg=True, eeg=False, eog=False,
  54. stim=False, exclude='bads')
  55.  
  56. method = 'fastica'
  57. # a good estimate of the number of components is the rank of the Raw object
  58. n_components = bandpass_data.estimate_rank()
  59. decim = 3
  60. random_state = 23
  61. ica = ICA(n_components=n_components, method=method, random_state=random_state)
  62.  
  63. reject = dict(mag=5e-12, grad=4000e-13)
  64. # goal: remove artifacts, so they should be easy to notice
  65. ica.fit(bandpass_data, picks=picks_meg, decim=decim, reject=reject)
  66.  
  67. if argc > 1 and sys.argv[1] == '-p':
  68. ica.plot_components()
  69.  
  70.  
  71. # pass on Epoch computing
  72. # 1) extract events from stim channels
  73. events = mne.find_events(bandpass_data, stim_channel='STI101')
  74. # 2) create epochs from Raw + events
  75. epochs = mne.Epochs(bandpass_data, events)
  76. # 3) average the epochs to reduce noise
  77. evoked = epochs.average()
  78.  
  79. evoked.shift_time(-0.05)
  80. if argc > 1 and sys.argv[1] == '-p':
  81. evoked.plot()
  82.  
  83.  
  84. bandpass_data.save('out.fif')
  85. mne.gui.coregistration(subjects_dir=subjects_dir)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement