def save_all_channels(experiment, selected_name): """ """ column_names = [] row_descs = [] csv_data = [] # accumulate csv contents for subject in experiment.subjects.values(): evoked = subject.evoked.get(selected_name) if not evoked: continue for key, mne_evoked in evoked.content.items(): column_names = format_floats(mne_evoked.times) for ch_idx, ch_name in enumerate(mne_evoked.info['ch_names']): if ch_name in mne_evoked.info['bads']: continue csv_data.append(mne_evoked.data[ch_idx].tolist()) row_desc = (subject.name, key, ch_name) row_descs.append(row_desc) folder = filemanager.create_timestamped_folder(experiment) fname = selected_name + '_all_subjects_all_channels_evoked.csv' path = os.path.join(folder, fname) filemanager.save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)
def save_channel_averages(experiment, selected_name): """ """ column_names = [] row_descs = [] csv_data = [] # accumulate csv contents for subject in experiment.subjects.values(): evoked = subject.evoked.get(selected_name) if not evoked: continue for key, mne_evoked in evoked.content.items(): data_labels, averaged_data = create_averages( experiment, mne_evoked) if not data_labels: raise Exception('No channel groups matching the data found') csv_data.extend(averaged_data.tolist()) column_names = format_floats(mne_evoked.times) for ch_type, area in data_labels: row_desc = (subject.name, key, ch_type, area) row_descs.append(row_desc) folder = filemanager.create_timestamped_folder(experiment) fname = selected_name + '_all_subjects_channel_averages_evoked.csv' path = os.path.join(folder, fname) filemanager.save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)
def save_tse_all_channels(experiment, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax): """ Saves all channels of a tse to a csv file. """ column_names = [] row_descs = [] csv_data = [] # accumulate csv contents for subject in experiment.subjects.values(): tfr = subject.tfr.get(tfr_name) if not tfr: continue tses = _compute_tse(tfr, fmin, fmax) for key, tse in tses.items(): times, tse = _crop_and_correct_to_baseline( tse, blmode, blstart, blend, tmin, tmax, tfr.times) csv_data.extend(tse.tolist()) for ch_name in tfr.ch_names: row_desc = (subject.name, key, ch_name) row_descs.append(row_desc) column_names = format_floats(times) folder = filemanager.create_timestamped_folder(experiment) fname = tfr_name + '_all_subjects_all_channels_tse.csv' path = os.path.join(folder, fname) filemanager.save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)
def save_tfr_channel_averages(experiment, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax): """ """ column_names = [] row_descs = [] csv_data = [] channel_groups = experiment.channel_groups # accumulate csv contents for subject in experiment.subjects.values(): tfr = subject.tfr.get(tfr_name) if not tfr: continue ch_names = tfr.ch_names info = tfr.info for key, mne_tfr in tfr.content.items(): # crop and correct to baseline mne_tfr = mne_tfr.copy().crop(tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax) times = mne_tfr.times column_names = format_floats(times) freqs = format_floats(mne_tfr.freqs) data = mne.baseline.rescale(mne_tfr.data, times, baseline=(blstart, blend), mode=blmode) data_labels, averaged_data = average_to_channel_groups( data, info, ch_names, channel_groups) if not data_labels: raise Exception('No channel groups matching the data found') for ix in range(averaged_data.shape[0]): for iy in range(averaged_data.shape[1]): ch_type, area = data_labels[ix] csv_data.append(averaged_data[ix, iy].tolist()) row_desc = (subject.name, key, ch_type, area, freqs[iy]) row_descs.append(row_desc) folder = filemanager.create_timestamped_folder(experiment) fname = tfr_name + '_all_subjects_channel_averages_tfr.csv' path = os.path.join(folder, fname) filemanager.save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)
def save_content(self): # if exists, delete first self.delete_content() for key, psd in self._content.items(): row_descs = [(ch_name,) for ch_name in self._ch_names] column_names = self._freqs.tolist() data = psd.tolist() path = os.path.join(self._spectrum_directory, self._name + '_' + str(key) + '.csv') filemanager.save_csv(path, data, column_names, row_descs)
def test_save_and_load_csv(): with tempfile.TemporaryDirectory() as dirpath: filepath = os.path.join(dirpath, 'data.csv') data = np.array([[1,2,3], [4,5,6]]) column_names = ['A', 'B', 'C'] row_descs = [('X', 'Kissa'), ('Y', 'Koira')] save_csv(filepath, data, column_names, row_descs) loaded_column_names, loaded_row_descs, loaded_data = load_csv(filepath) assert(loaded_column_names == column_names) assert(loaded_row_descs[0] == row_descs[0]) assert(loaded_row_descs[1] == row_descs[1]) assert(np.array_equal(loaded_data, data))
def save_tfr_all_channels(experiment, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax): """ Saves all channels of tfr item to a csv file. """ column_names = [] row_descs = [] csv_data = [] # accumulate csv contents for subject in experiment.subjects.values(): tfr = subject.tfr.get(tfr_name) if not tfr: continue ch_names = tfr.ch_names for key, mne_tfr in tfr.content.items(): # crop and correct to baseline mne_tfr = mne_tfr.copy().crop(tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax) times = mne_tfr.times column_names = format_floats(times) freqs = format_floats(mne_tfr.freqs) data = mne.baseline.rescale(mne_tfr.data, times, baseline=(blstart, blend), mode=blmode) for ix in range(data.shape[0]): for iy in range(data.shape[1]): csv_data.append(data[ix, iy].tolist()) row_desc = (subject.name, key, ch_names[ix], freqs[iy]) row_descs.append(row_desc) folder = filemanager.create_timestamped_folder(experiment) fname = tfr_name + '_all_subjects_all_channels_tfr.csv' path = os.path.join(folder, fname) filemanager.save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)
def save_channel_averages(experiment, selected_name, log_transformed=False): column_names = [] row_descs = [] csv_data = [] channel_groups = experiment.channel_groups # accumulate csv contents for subject in experiment.subjects.values(): spectrum = subject.spectrum.get(selected_name) if not spectrum: continue ch_names = spectrum.ch_names freqs = spectrum.freqs info = subject.get_raw().info for key, psd in spectrum.content.items(): data_labels, averaged_data = average_to_channel_groups( psd, info, ch_names, channel_groups) if not data_labels: raise Exception('No channel groups matching the data found.') if log_transformed: csv_data.extend(10 * np.log10(averaged_data.tolist())) else: csv_data.extend(averaged_data.tolist()) column_names = format_floats(freqs) for ch_type, area in data_labels: row_desc = (subject.name, key, ch_type, area) row_descs.append(row_desc) folder = filemanager.create_timestamped_folder(experiment) fname = selected_name + '_all_subjects_channel_averages_spectrum.csv' path = os.path.join(folder, fname) filemanager.save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)
def save_tse_channel_averages(experiment, tfr_name, blmode, blstart, blend, tmin, tmax, fmin, fmax): """ """ column_names = [] row_descs = [] csv_data = [] channel_groups = experiment.channel_groups # accumulate csv contents for subject in experiment.subjects.values(): tfr = subject.tfr.get(tfr_name) if not tfr: continue times, tses = _compute_tse(tfr, fmin, fmax, tmin, tmax, blmode, blstart, blend) column_names = format_floats(times) for key, tse in tses.items(): data_labels, averaged_data = average_to_channel_groups( tse, tfr.info, tfr.ch_names, channel_groups) if not data_labels: raise Exception('No channel groups matching the data found.') csv_data.extend(averaged_data.tolist()) for ch_type, area in data_labels: row_desc = (subject.name, key, ch_type, area) row_descs.append(row_desc) folder = filemanager.create_timestamped_folder(experiment) fname = tfr_name + '_all_subjects_channel_averages_tfr.csv' path = os.path.join(folder, fname) filemanager.save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)
def save_content(self): """Saves spectral data and info structure to the spectrum directory. """ try: # save info info_path = os.path.join(self._directory, self._name + '-info.fif') mne.io.meas_info.write_info(info_path, self._info) self._params['info_set'] = True # save data for key, psd in self._content.items(): row_descs = [(ch_name, ) for ch_name in self.ch_names] column_names = self._freqs.tolist() data = psd.tolist() path = os.path.join(self._directory, self._name + '_' + str(key) + '.csv') filemanager.save_csv(path, data, column_names, row_descs) except Exception as exc: raise Exception("Writing spectrums failed. Please check that the " "entire experiment folder has write permissions.")
def save_all_channels(experiment, selected_name): column_names = [] row_descs = [] csv_data = [] for subject in experiment.subjects.values(): spectrum = subject.spectrum.get(selected_name) if not spectrum: continue for key, psd in spectrum.content.items(): csv_data.extend(psd.tolist()) column_names = format_floats(spectrum.freqs) for ch_name in spectrum.ch_names: row_desc = (subject.name, key, ch_name) row_descs.append(row_desc) folder = filemanager.create_timestamped_folder(experiment) fname = selected_name + '_all_subjects_all_channels_spectrum.csv' path = os.path.join(folder, fname) filemanager.save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)
def save_all_channels(experiment, selected_name): """ Saves peak params and aperiodic params to a csv file for every subject and channel and condition """ row_descs = [] csv_data = [] column_names = ['CF', 'Amp', 'BW', 'Aperiodic offset', 'Aperiodic exponent'] for subject in experiment.subjects.values(): fooof_item = subject.fooof_report.get(selected_name) if not fooof_item: continue for key, report in fooof_item.content.items(): for ch_idx, ch_name in enumerate(fooof_item.params['ch_names']): ch_report = report.get_fooof(ch_idx) for peak in ch_report.peak_params_: csv_data.append([format_float(peak[0]), format_float(peak[1]), format_float(peak[2]), '', '']) row_descs.append((subject.name, key, ch_name)) aparams = ch_report.aperiodic_params_ csv_data.append(['', '', '', format_float(aparams[0]), format_float(aparams[1])]) row_descs.append((subject.name, key, ch_name)) # Save the resulting csv into a output folder 'meggie way' folder = create_timestamped_folder(experiment) fname = selected_name + '_all_subject_all_channels_fooof.csv' path = os.path.join(folder, fname) save_csv(path, csv_data, column_names, row_descs) logging.getLogger('ui_logger').info('Saved the csv file to ' + path)