Jeremiah_

d

Mar 17th, 2020
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.98 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Mar 16 11:06:18 2020
  4.  
  5. @author: J. Abreu
  6.  
  7. """
  8.  
  9. #%% importings
  10. import numpy as np
  11. import mne
  12. import os
  13. from argparse import ArgumentParser
  14.  
  15. mne.set_log_level('WARNING')
  16. mne.utils.set_config('MNE_USE_CUDA', 'true')
  17. #mne.io.Raw.set_montage(montage='standard_1020')
  18. #%%
  19. parser = ArgumentParser()
  20. parser.add_argument("--dev-sys", "-devsys", type=str, required=False, default='NEUROMAP',
  21.                     help="Indica qual o sistema de captura. Opções 'DSI', 'NEUROMAP'.")
  22.  
  23. #%%
  24. def read_edf(path='D:/Jeremias/Datasets/tuh_mixed', num_files=1):
  25.     raw_list = []
  26.     for file in os.listdir(path):
  27.         if os.path.isfile(os.path.join(path, file)) and os.path.join(path, file)[-3:] == 'edf':
  28.             raw_list.append(mne.io.read_raw_edf(os.path.join(path, file), preload=True))
  29.         if(len(raw_list) == num_files):
  30.             print(len(raw_list), ' raw files were loaded')
  31.             return raw_list
  32.     else:
  33.         print(len(raw_list), ' raw files were loaded')
  34.         return raw_list
  35. #%%
  36. def sum_events(raw_list):
  37.     if type(raw_list) == list:
  38.         sum = 0
  39.         for raw in raw_list:
  40.             sum += len(raw.info['events'])
  41.         print(sum)
  42.     elif type(raw_list) == mne.io.edf.edf.RawEDF:
  43.         print(len(raw_list.info['events']))
  44.     else:
  45.         print('type of file not accepted! only list of io.edf.edf.RawEDF ' \
  46.               + 'or a single io.edf.edf.RawEDF file')
  47.  
  48. #%%
  49. def get_annotation_file(raw=None):
  50.     if raw is None:
  51.         print('no file name')
  52.         return
  53.    
  54.     try:
  55.         path = raw._filenames[0][:-9] + '.txt'
  56.         with open(path, 'r') as f:
  57.             txt = f.read()
  58.            
  59.         print(txt)
  60.     except:
  61.         raise FileNotFoundError('verify if the file name is correct')
  62. #%% set standard montage
  63. #montage = mne.channels.make_standard_montage('stardad_1020')
  64. #raw.set_montage(montage, raise_if_subset=False, verbose=False)
  65.  
  66. #%%
  67. def viz(raw):
  68.     montage = mne.channels.make_standard_montage('standard_1020')
  69.     #raw.set_montage(montage, set_dig = True, raise_if_subset=False, verbose=False)
  70.    
  71.     print('number of events: ', len(raw.info['events']))
  72.    
  73.     events = mne.make_fixed_length_events(raw, first_samp=True)
  74.     picks = mne.pick_types(raw.info, meg=False, eeg=True)
  75.    
  76.     baseline = (None, 0)
  77.     print(picks)
  78.    
  79.     epochs = mne.Epochs(raw, events, event_id=1, picks=picks, preload=True)
  80.     epochs.plot_psd(fmin=1., fmax=40.)
  81.     epochs.plot_psd_topomap()
  82.     evoked = epochs.average()
  83.     #evoked.set_montage(montage, set_dig=True, raise_if_subset=True)
  84.     evoked.plot()
  85. #%%
  86. def ch_order(sys_map):
  87.    
  88.     if sys_map == 'DSI':
  89.         map_ch = {'EEG FP1-LE':'Fp1',
  90.                  'EEG FP2-LE':'Fp2',
  91.                  'EEG F3-LE':'F3',
  92.                  'EEG F4-LE':'F4',
  93.                  'EEG C3-LE':'C3',
  94.                  'EEG C4-LE':'C4',
  95.                  'EEG A1-LE':'A1',
  96.                  'EEG A2-LE':'A2',
  97.                  'EEG P3-LE':'P3',
  98.                  'EEG P4-LE':'P4',
  99.                  'EEG O1-LE':'O1',
  100.                  'EEG O2-LE':'O2',
  101.                  'EEG F7-LE':'F7',
  102.                  'EEG F8-LE':'F8',
  103.                  'EEG T3-LE':'T3',
  104.                  'EEG T4-LE':'T4',
  105.                  'EEG T5-LE':'T5',
  106.                  'EEG T6-LE':'T6',
  107.                  'EEG FZ-LE':'Fz',
  108.                  'EEG CZ-LE':'Cz',
  109.                  'EEG PZ-LE':'Pz',
  110.                  'EEG OZ-LE':'Oz',
  111.                  'EEG PG1-LE':'Pg1',
  112.                  'EEG PG2-LE':'Pg2',
  113.                  'EEG EKG-LE':'Ekg',
  114.                  'EEG SP2-LE':'Sp2',
  115.                  'EEG SP1-LE':'Sp1',
  116.                  'EEG T1-LE':'T1',
  117.                  'EEG T2-LE':'T2',
  118.                  'EEG C4-LE':'C4', 'EEG CZ-LE':'Cz', 'EEG EKG-LE':'Ekg',
  119.                  'EEG F3-LE':'F3', 'EEG F4-LE':'F4', 'EEG F7-LE':'F7',
  120.                  'EEG F8-LE':'F8', 'EEG FP1-LE':'Fp1', 'EEG FP2-LE':'Fp2',
  121.                  'EEG FZ-LE':'Fz', 'EEG O1-LE':'O1', 'EEG O2-LE':'O2',
  122.                  'EEG OZ-LE':'Oz', 'EEG P3-LE':'P3', 'EEG P4-LE':'P4',
  123.                  'EEG PG1-LE':'Pg1', 'EEG PG2-LE':'Pg2', 'EEG PZ-LE':'Pz',
  124.                  'EEG SP1-LE':'Sp1', 'EEG SP2-LE':'Sp2','EEG T1-LE':'T1',
  125.                  'EEG T2-LE':'T2', 'EEG T3-LE':'T3', 'EEG T4-LE':'T4','EEG T5-LE':'T5',
  126.                  'EEG T6-LE':'T6'}
  127.                        
  128.         ch_order = ['Fp1','Fp2','AF9','AF7','AF5','AF3','AF1','AFz','AF2','AF4',
  129.                     'AF6','AF8','AF10','F9','F7','F5','F3','F1','Fz','F2','F4','F6',
  130.                     'F8','F10','FT9','FT7','FC5','FC3','FC1','FCz','FC2','FC4','FC6',
  131.                     'FT8','FT10','T9','T7','C5','C3','C1','Cz','C2','C4','C6','T8',
  132.                     'T10','TP9','TP7','CP5','CP3','CP1','CPz','CP2','CP4','CP6','TP8',
  133.                     'TP10','P9','P7','P5','P3','P1','Pz','P2','P4','P6','P8','P10',
  134.                     'PO9','PO7','PO5','PO3','PO1','POz','PO2','PO4','PO6','PO8','PO10',
  135.                     'O1','Oz','O2','O9','Iz','O10','T3','T5','T4','T6','M1','M2','A1',
  136.                     'A2']
  137.         return (map_ch, ch_order)
  138.     else:
  139.         print('sys_map is not DSI')
  140.         return None
  141. #%%
  142. raw_list = read_edf(num_files=1)
  143. #%%
  144. raw = raw_list[0]
  145. raw.info['bads'] = ['PHOTIC PH']
  146.  
  147. picks = mne.pick_types(raw.info, meg=False, eeg=True)
  148. raw_1020 = raw.copy()
  149. raw_1020.drop_channels(['PHOTIC PH','EEG 28-LE', 'EEG 29-LE', 'EEG 30-LE'])
  150.  
  151. (map_ch, ch_order) = ch_order('DSI')
  152.  
  153. raw_1020.rename_channels(map_ch)
  154. #raw = raw.reorder_channels(ch_order)
  155. #%%
  156. raw_1020 = raw_1020.set_montage('standard_1020', raise_if_subset=False)
  157.  
  158. raw_1020.plot_psd_topo()
  159. #%%
  160.  
  161. montage = mne.channels.make_standard_montage('standard_1020')
  162. raw.set_montage(montage=montage)
  163. raw.set_montage(montage, set_dig=True)
  164. raw.plot_psd_topo()
  165. #viz(raw)
  166. #%%
  167.  
  168. montage = mne.channels.read_montage('standard_1020')
  169.  
  170.  
  171.  
  172. #raw.set_montage(montage, set_dig=True)
Advertisement
Add Comment
Please, Sign In to add comment