Esempio n. 1
0
def load_stim_file(subject, args):
    stim_fname = op.join(
        MMVT_DIR, subject, 'electrodes',
        '{}{}.npz'.format(args.file_frefix, args.stim_channel))
    stim = np.load(stim_fname)
    labels, psd, time, freqs = (stim[k]
                                for k in ['labels', 'psd', 'time', 'freqs'])
    bipolar = '-' in labels[0]
    data = None
    freqs_dim = psd.shape.index(len(freqs))
    labels_dim = psd.shape.index(len(labels))
    if time.ndim > 0:
        time_dim = psd.shape.index(len(time))
    else:
        time_dim = next(iter(set(range(3)) - set([freqs_dim, labels_dim])))
    T, L, F = psd.shape[time_dim], psd.shape[labels_dim], psd.shape[freqs_dim]
    if args.downsample > 1:
        time = utils.downsample(time, args.downsample)
    colors = None
    conditions = []
    for freq_ind, (freq_from, freq_to) in enumerate(freqs):
        if data is None:
            # initialize the data matrix (electrodes_num x T x freqs_num)
            data = np.zeros((L, T, F))
        psd_slice = get_psd_freq_slice(psd, freq_ind, freqs_dim, time_dim)
        if args.downsample > 1:
            psd_slice = utils.downsample(psd_slice, args.downsample)
        data[:, :, freq_ind] = psd_slice
        data_min, data_max = utils.calc_min_max(psd_slice,
                                                norm_percs=args.norm_percs)
        if colors is None:
            colors = np.zeros((*data.shape, 3))
        for elec_ind, elec_name in enumerate(labels):
            elec_group = utils.elec_group(elec_name, bipolar)
            # if elec_group in ['LVF', 'RMT']:
            #     colors[elec_ind, :, freq_ind] = utils.mat_to_colors(psd_slice[elec_ind], data_min, data_max, colorsMap='BuGn')
            # else:
            colors[elec_ind, :,
                   freq_ind] = utils.mat_to_colors(psd_slice[elec_ind],
                                                   data_min,
                                                   data_max,
                                                   colorsMap=args.colors_map)
        conditions.append('{}-{}Hz'.format(freq_from, freq_to))
    output_fname = op.join(
        MMVT_DIR, subject, 'electrodes',
        'stim_electrodes_{}{}_{}.npz'.format(args.file_frefix,
                                             'bipolar' if bipolar else '',
                                             args.stim_channel))
    print('Saving {}'.format(output_fname))
    np.savez(output_fname,
             data=data,
             names=labels,
             conditions=conditions,
             colors=colors)
    return dict(data=data, labels=labels, conditions=conditions, colors=colors)
Esempio n. 2
0
def load_stim_file(subject, args):
    stim_fname = op.join(MMVT_DIR, subject, 'electrodes', '{}{}.npz'.format(
        args.file_frefix, args.stim_channel))
    stim = np.load(stim_fname)
    labels, psd, time, freqs = (stim[k] for k in ['labels', 'psd', 'time', 'freqs'])
    bipolar = '-' in labels[0]
    data = None
    freqs_dim = psd.shape.index(len(freqs))
    labels_dim = psd.shape.index(len(labels))
    if time.ndim > 0:
        time_dim = psd.shape.index(len(time))
    else:
        time_dim = next(iter(set(range(3)) - set([freqs_dim, labels_dim])))
    T, L, F = psd.shape[time_dim], psd.shape[labels_dim], psd.shape[freqs_dim]
    if args.downsample > 1:
        time = utils.downsample(time, args.downsample)
    colors = None
    conditions = []
    for freq_ind, (freq_from, freq_to) in enumerate(freqs):
        if data is None:
            # initialize the data matrix (electrodes_num x T x freqs_num)
            data = np.zeros((L, T, F))
        psd_slice = get_psd_freq_slice(psd, freq_ind, freqs_dim, time_dim)
        if args.downsample > 1:
            psd_slice = utils.downsample(psd_slice, args.downsample)
        data[:, :, freq_ind] = psd_slice
        data_min, data_max = utils.calc_min_max(psd_slice, norm_percs=args.norm_percs)
        if colors is None:
            colors = np.zeros((*data.shape, 3))
        for elec_ind, elec_name in enumerate(labels):
            elec_group = utils.elec_group(elec_name, bipolar)
            # if elec_group in ['LVF', 'RMT']:
            #     colors[elec_ind, :, freq_ind] = utils.mat_to_colors(psd_slice[elec_ind], data_min, data_max, colorsMap='BuGn')
            # else:
            colors[elec_ind, :, freq_ind] = utils.mat_to_colors(psd_slice[elec_ind], data_min, data_max, colorsMap=args.colors_map)
        conditions.append('{}-{}Hz'.format(freq_from, freq_to))
    output_fname = op.join(MMVT_DIR, subject, 'electrodes', 'stim_electrodes_{}{}_{}.npz'.format(
            args.file_frefix, 'bipolar' if bipolar else '', args.stim_channel))
    print('Saving {}'.format(output_fname))
    np.savez(output_fname, data=data, names=labels, conditions=conditions, colors=colors)
    return dict(data=data, labels=labels, conditions=conditions, colors=colors)
Esempio n. 3
0
def calc_labels_data(elecs_lookup, stim_data, stim_labels, hemi=None):
    labels_names = list(elecs_lookup.keys())
    labels_data = np.zeros((len(labels_names), stim_data.shape[1], stim_data.shape[2]))
    colors = np.zeros((*labels_data.shape, 3))
    labels_data_names = []
    label_ind = 0
    for label_name, electordes_data in elecs_lookup.items():
        if not hemi is None:
            if lu.get_hemi_from_name(label_name) != hemi:
                continue
        labels_data_names.append(label_name)
        for elec_name, elec_prob in electordes_data:
            elec_inds = np.where(stim_labels == elec_name)[0]
            if len(elec_inds) > 0:
                elec_data = stim_data[elec_inds[0], :, :] * elec_prob
                labels_data[label_ind, :, :] += elec_data
        label_ind += 1
    # Calc colors for each freq
    for freq_id in range(labels_data.shape[2]):
        data_min, data_max = utils.calc_min_max(labels_data[:, :, freq_id], norm_percs=args.norm_percs)
        colors[:, :, freq_id] = utils.mat_to_colors(
            labels_data[:, :, freq_id], data_min, data_max, colorsMap=args.colors_map)
    return labels_data, colors, labels_data_names