示例#1
0
def test_add_epoch(epoch_df):
    result = add_epoch(epoch_df, 'parent', 'child_a')
    assert len(result) == (len(epoch_df) + 2)

    m = result['name'] == 'parent_child_a'
    assert (m.sum() == 2)

    expected = [[1, 2], [30, 31]]
    values = result.loc[m, ['start', 'end']].values
    assert np.array_equal(expected, values)
示例#2
0
文件: io.py 项目: nadoss/nems_db
def reformat_epochs(epochs, target_id):
    new_epochs = []
    new_epochs.append(epochs)
    fg_samples = extract_sample_ids(epochs, 'fg')
    bg_samples = extract_sample_ids(epochs, 'bg')
    new_epochs.append(fg_samples.copy())
    new_epochs.append(bg_samples.copy())
    fg_samples['name'] = fg_samples['name'].apply(lambda x: f'fg_{x}')
    bg_samples['name'] = bg_samples['name'].apply(lambda x: f'bg_{x}')
    new_epochs.append(fg_samples.copy())
    new_epochs.append(bg_samples.copy())
    samples = pd.concat(new_epochs, ignore_index=True)
    samples.sort_values('start', inplace=True)

    target_id.epochs = samples
    x = target_id.as_continuous()[0]
    m = np.isfinite(x) & (x != 0)
    targets = np.unique(x[m])

    # Tag the epochs as target and whether it's current or not.
    for i, target in enumerate(targets):
        target_name = '{:02d}'.format(int(target))
        m = samples['name'].str.match(target_name)
        e = samples.loc[m].copy()
        e['name'] = 'target_{}'.format(i)
        new_epochs.append(e)

        # extract all occurances of the target across all trials. find out what
        # the actual (intended) target was for each particular trial.
        x = target_id.extract_epoch(target_name)[:, 0, 0]
        m = x == target
        c = e.loc[m].copy()
        c['name'] = 'current_target'
        new_epochs.append(c)

        c = e.loc[~m].copy()
        c['name'] = 'other_target'
        new_epochs.append(c)

    repeating = extract_repeating(epochs)
    new_epochs.append(repeating)

    random = extract_random(epochs)
    new_epochs.append(random)

    stream_type = extract_stream_type(epochs)
    new_epochs.append(stream_type)

    epochs = pd.concat(new_epochs, ignore_index=True)
    epochs.drop_duplicates(inplace=True)
    epochs.sort_values('start', inplace=True)

    # This fixes a bug in BAPHY->NEMS conversion of timestamps (not all trials
    # from the same sequence are identical length and may be off by 0.001).
    # This should be fine for RDT data because all relevant epochs are
    # in units of 0.5, 0.15, 0.3, 0.25 sec.
    epochs['start'] = epochs['start'].round(2)
    epochs['end'] = epochs['end'].round(2)

    epochs = add_epoch(epochs, 'random', 'single')
    epochs = add_epoch(epochs, 'repeating', 'single')
    epochs = add_epoch(epochs, 'random', 'dual')
    epochs = add_epoch(epochs, 'repeating', 'dual')

    for i in range(len(targets)):
        target_name = f'target_{i}'
        epochs = add_epoch(epochs, target_name, 'random_single')
        epochs = add_epoch(epochs, target_name, 'repeating_single')
        epochs = add_epoch(epochs, target_name, 'random_dual')
        epochs = add_epoch(epochs, target_name, 'repeating_dual')

    return epochs