Example #1
0
def create_mmvt_file(subject,
                     stim_channel,
                     channel_list,
                     fs,
                     time,
                     stim,
                     theta,
                     downsample_ratio=10):
    theta -= np.min(theta)
    theta /= np.max(theta)
    theta *= -1
    stim = utils.downsample(stim.squeeze(), downsample_ratio)
    stim[stim >= 0.5] = 1
    stim[stim < 0.5] = 0
    theta = utils.downsample_2d(theta.T, downsample_ratio)
    data = np.vstack((stim, theta))
    data = data[:, :, np.newaxis]
    channel_list = [stim_channel] + channel_list
    meta_data_fname = op.join(MMVT_DIR, subject, 'electrodes',
                              'electrodes_bipolar_data.npz')
    np.savez(meta_data_fname,
             data=data,
             names=channel_list,
             conditions=['on'],
             dt=1 / fs)
Example #2
0
def create_mmvt_file(subject,
                     stim_channel,
                     channel_list,
                     fs,
                     time,
                     stim,
                     theta,
                     downsample_ratio=10,
                     downsample_using_mean=True,
                     smooth_win_len=101):
    theta = utils.downsample_2d(theta.T, downsample_ratio,
                                downsample_using_mean)
    theta_smooth = np.zeros((theta.shape[0], theta.shape[1]))
    for k in range(theta.shape[0]):
        theta_smooth[k] = scipy.signal.savgol_filter(theta[k], smooth_win_len,
                                                     5)
        theta_smooth[k] -= np.min(theta_smooth[k])
        theta_smooth[k] /= np.max(theta_smooth[k])
    plt.plot(theta_smooth.T)
    plt.show()
    theta_smooth *= -1

    stim = utils.downsample(stim.squeeze(), downsample_ratio)
    stim[stim > 0] = 1
    stim_indices = np.where(np.diff(stim) == 1)[0] + 1
    print('{} stim!'.format(len(stim_indices)))
    plt.plot(stim)
    plt.show()

    data = np.vstack((stim, theta_smooth))
    data = data[:, :, np.newaxis]
    channel_list = [stim_channel] + channel_list
    meta_data_fname = op.join(MMVT_DIR, subject, 'electrodes',
                              'electrodes_bipolar_data.npz')
    np.savez(meta_data_fname,
             data=data,
             names=channel_list,
             conditions=['on'],
             dt=1 / fs)
def downsample_data(data):
    C, E, T = data.shape
    new_data = np.zeros((C, E, int(T/2)))
    for epoch in range(C):
        new_data[epoch, :, :] = utils.downsample_2d(data[epoch, :, :], 2)
    return new_data
Example #4
0
def downsample_data(data):
    C, E, T = data.shape
    new_data = np.zeros((C, E, int(T/2)))
    for epoch in range(C):
        new_data[epoch, :, :] = utils.downsample_2d(data[epoch, :, :], 2)
    return new_data
Example #5
0
def read_electrodes_data_one_mat(subject, mat_file, conditions, stat, output_file_name, bipolar, electrodes_names_field,
        field_cond_template, from_t=0, to_t=None, norm_by_percentile=True, norm_percs=(3, 97), threshold=0,
        color_map='jet', cm_big='YlOrRd', cm_small='PuBu', flip_cm_big=False, flip_cm_small=True,
        moving_average_win_size=0, downsample=2):
    # load the matlab file
    d = sio.loadmat(mat_file)
    # get the labels names
    if electrodes_names_field in d:
        labels = d[electrodes_names_field]
        labels = mu.matlab_cell_str_to_list(labels)
        if bipolar and '-' in labels[0]:
            labels = fix_bipolar_labels(labels)
        # else:
        #     #todo: change that!!!
        #     if len(labels) == 1:
        #         labels = [str(l[0]) for l in labels[0]]
        #     else:
        #         labels = [str(l[0][0]) for l in labels]
    else:
        raise Exception('electrodes_names_field not in the matlab file!')
    # Loop for each condition
    for cond_id, cond_name in enumerate(conditions):
        field = field_cond_template.format(cond_name)
        if field not in d:
            field = field_cond_template.format(cond_name.title())
        if field not in d:
            print('{} not in the mat file!'.format(cond_name))
            continue
        # todo: downsample suppose to be a cmdline paramter
        # dims: samples * electrodes * time (single triale) or electrodes * time (evoked)
        if to_t == 0 or to_t == -1:
            to_t = int(d[field].shape[-1] / downsample)
        # initialize the data matrix (electrodes_num x T x 2)
        # times = np.arange(0, to_t*2, 2)
        cond_data = d[field] # [:, times]
        if cond_id == 0:
            single_trials = cond_data.ndim == 3
            data = {} if single_trials else np.zeros((cond_data.shape[0], to_t - from_t, 2))
        if single_trials:
            # Different number of trials per condition, can't save it in a matrix
            # data[cond_name] = np.zeros((cond_data.shape[0], cond_data.shape[1], to_t - from_t))
            cond_data_downsample = utils.downsample_3d(cond_data, downsample)
            data[cond_name] = cond_data_downsample[:, :, from_t:to_t]
        else:
            cond_data_downsample = utils.downsample_2d(cond_data, downsample)
            data[:, :, cond_id] = cond_data_downsample[:, from_t:to_t]

    if bipolar:
        data, labels = bipolarize_data(data, labels)

    if single_trials:
        output_fname = op.join(MMVT_DIR, subject, 'electrodes', 'electrodes{}_data_st.npz'.format(
            '_bipolar' if bipolar else ''))
        data_conds = [(data[key], key) for key in data.keys()]
        data = [d[0] for d in data_conds]
        conditions = [d[1] for d in data_conds]
        np.savez(output_fname, data=data, names=labels, conditions=conditions)
    else:
        data = utils.normalize_data(data, norm_by_percentile, norm_percs)
        stat_data = calc_stat_data(data, stat)
        calc_colors = partial(
            utils.mat_to_colors_two_colors_maps, threshold=threshold, cm_big=cm_big, cm_small=cm_small, default_val=1,
            flip_cm_big=flip_cm_big, flip_cm_small=flip_cm_small, min_is_abs_max=True, norm_percs=norm_percs)
        if moving_average_win_size > 0:
            # data_mv[:, :, cond_id] = utils.downsample_2d(data[:, :, cond_id], moving_average_win_size)
            stat_data_mv = utils.moving_avg(stat_data, moving_average_win_size)
            colors_mv = calc_colors(stat_data_mv)
            np.savez(output_file_name, data=data, stat=stat_data_mv, names=labels, conditions=conditions, colors=colors_mv)
        else:
            colors = calc_colors(stat_data)
            np.savez(output_file_name, data=data, names=labels, conditions=conditions, colors=colors)