jayfmil / miller_ecog_tools

7 stars 3 forks source link

multiple TH patients run into issues with loading EEG data #1

Closed seqasim closed 6 years ago

seqasim commented 6 years ago

Hey, I keep getting the following error with patients when trying to load and save their monopolar EEG data. These include R1182C, and R1189M. Have you had this problem? It's a KeyError that seems to imply that these patients don't have "contact" information.

KeyError                                  Traceback (most recent call last)
<ipython-input-7-4fac8ab55b38> in <module>()
      2 for x in TH1_subjs:
      3     subj = SubjectEEGData(task='RAM_TH1', subject=x)
----> 4     subj.load_data()
      5     subj.save_data()

/home1/salman.qasim/Salman_Python_Scripts/RAM_ECoG/miller_ecog_tools/SubjectLevel/subject_data.py in load_data(self)
    171         Call super's load data, and then additionally cast data to float32 to take up less space.
    172         """
--> 173         super(SubjectEEGData, self).load_data()
    174         if self.subject is not None:
    175             self.subject_data.data = self.subject_data.data.astype('float32')

/home1/salman.qasim/Salman_Python_Scripts/RAM_ECoG/miller_ecog_tools/SubjectLevel/subject_data.py in load_data(self)
     61         # otherwise compute
     62         if self.subject_data is None:
---> 63             self.subject_data = self.compute_data()
     64 
     65     def unload_data(self):

/home1/salman.qasim/Salman_Python_Scripts/RAM_ECoG/miller_ecog_tools/SubjectLevel/subject_data.py in compute_data(self)
    210                                                  mean_over_time=self.mean_over_time,
    211                                                  use_mirror_buf=self.use_mirror_buf,
--> 212                                                  loop_over_chans=True)
    213         return subject_data
    214 

/home1/salman.qasim/Salman_Python_Scripts/RAM_ECoG/miller_ecog_tools/RAM_helpers.py in compute_power(events, freqs, wave_num, rel_start_ms, rel_stop_ms, buf_ms, elec_scheme, noise_freq, resample_freq, mean_over_time, log_power, loop_over_chans, cluster_pool, use_mirror_buf, time_bins)
    427             pow_list = cluster_pool.map(_parallel_compute_power, arg_list)
    428         else:
--> 429             pow_list = list(map(_parallel_compute_power, tqdm(arg_list, disable=True if len(arg_list) == 1 else False)))
    430 
    431         # This is the stupidest thing in the world. I should just be able to do concat(pow_list, dim='channels') or

/home1/salman.qasim/Salman_Python_Scripts/RAM_ECoG/miller_ecog_tools/RAM_helpers.py in _parallel_compute_power(arg_list)
    465     # first load eeg
    466     eeg = load_eeg(events, rel_start_ms, rel_stop_ms, buf_ms=buf_ms, elec_scheme=elec_scheme,
--> 467                    noise_freq=noise_freq, resample_freq=resample_freq, use_mirror_buf=use_mirror_buf)
    468 
    469     # then compute power

/home1/salman.qasim/Salman_Python_Scripts/RAM_ECoG/miller_ecog_tools/RAM_helpers.py in load_eeg(events, rel_start_ms, rel_stop_ms, buf_ms, elec_scheme, noise_freq, resample_freq, pass_band, use_mirror_buf, demean)
    241     # Should auto convert to PTSA? Any reason not to?
    242     eeg = CMLReader(subject=events.iloc[0].subject).load_eeg(events, rel_start=actual_start, rel_stop=actual_stop,
--> 243                                                         scheme=elec_scheme).to_ptsa()
    244     if demean:
    245         eeg = eeg.baseline_corrected([rel_start_ms, rel_stop_ms])

/home1/salman.qasim/.conda/envs/py3_env/lib/python3.6/site-packages/cmlreaders/cmlreader.py in load_eeg(self, events, rel_start, rel_stop, scheme)
    266             })
    267 
--> 268         return self.load('eeg', **kwargs)
    269 
    270     @classmethod

/home1/salman.qasim/.conda/envs/py3_env/lib/python3.6/site-packages/cmlreaders/cmlreader.py in load(self, data_type, **kwargs)
    213             )
    214 
--> 215         return cls.load(**kwargs)
    216 
    217     def load_eeg(self, events: Optional[pd.DataFrame] = None,

/home1/salman.qasim/.conda/envs/py3_env/lib/python3.6/site-packages/cmlreaders/readers/eeg.py in load(self, **kwargs)
    475 
    476         events = self._eegfile_absolute(events.copy())
--> 477         return self.as_timeseries(events, kwargs["rel_start"], kwargs["rel_stop"])
    478 
    479     def as_dataframe(self):

/home1/salman.qasim/.conda/envs/py3_env/lib/python3.6/site-packages/cmlreaders/readers/eeg.py in as_timeseries(self, events, rel_start, rel_stop)
    563 
    564             if self.scheme is not None:
--> 565                 data, labels = reader.rereference(data, contacts)
    566                 channels = labels
    567             else:

/home1/salman.qasim/.conda/envs/py3_env/lib/python3.6/site-packages/cmlreaders/readers/eeg.py in rereference(self, data, contacts)
    201             return reref, self.scheme["label"].tolist()
    202         else:
--> 203             channels = [contact_to_index[c] for c in self.scheme["contact"]]
    204             subset = np.array(
    205                 [data[i, channels, :] for i in range(data.shape[0])]

/home1/salman.qasim/.conda/envs/py3_env/lib/python3.6/site-packages/cmlreaders/readers/eeg.py in <listcomp>(.0)
    201             return reref, self.scheme["label"].tolist()
    202         else:
--> 203             channels = [contact_to_index[c] for c in self.scheme["contact"]]
    204             subset = np.array(
    205                 [data[i, channels, :] for i in range(data.shape[0])]

KeyError: 1
seqasim commented 6 years ago

Most other patients so far have worked fine.

seqasim commented 6 years ago

If you want code to exactly test this case:

sessions_completed = r1_data[(r1_data['subject'] == 'R1182C') &
                                 (r1_data['experiment'] == 'TH1')]
ses_reader = cml.CMLReader(subject='R1182C', experiment="TH1", session=0,
                           localization=0, montage=0)
elec_info = ses_reader.load('contacts')
mono_chans = ses_reader.load('leads')
events_df = ses_reader.load('task_events')

freqs=np.logspace(np.log10(1), np.log10(200), 50)                
            # filter to just item presentation events
events = events_df[(events_df.type == 'CHEST') & (events_df.confidence>=0)] 
            #bipol_chans=RAM_helpers.get_channel_numbers(subj[0], montage=subj[1], bipol=True, use_json=True)
subj_pow = RAM_helpers.compute_power(events,
                                                 freqs,
                                                 wave_num = 5,
                                                 rel_start_ms= -500,
                                                 rel_stop_ms= 1000,
                                                 buf_ms= 2000,
                                                 log_power= True,
                                                 noise_freq= [58.0, 62.0],
                                                 elec_scheme= elec_info,
                                                 cluster_pool= None,
                                                 resample_freq= None,
                                                 mean_over_time= False,
                                                 use_mirror_buf= False,
                                                 loop_over_chans=True)
jayfmil commented 6 years ago

Yeah there is definitely something weird with that subject. Thee eeg files that the events point to don't exist, at least not in that location.

/protocols/r1/subjects/R1189M/experiments/TH1/sessions/0/ephys/current_processed/noreref/R1189M_TH1_0_28Jul16_1918

jayfmil commented 6 years ago

closing this as it's a data storage or cmlreaders issue.