Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Resampled column has one too many rows #358

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions bids/variables/tests/test_variables.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from bids.layout import BIDSLayout
import pytest
import os
from os.path import join
from bids.tests import get_test_data_path
from bids.variables import (merge_variables, DenseRunVariable, SimpleVariable,
load_variables)
from bids.variables.entities import RunInfo
import numpy as np
import pandas as pd
import nibabel as nb
import uuid
import json


def generate_DEV(name='test', sr=20, duration=480):
Expand Down Expand Up @@ -174,3 +177,28 @@ def test_filter_simple_variable(layout2):
assert merged.filter({'nonexistent': 2}, strict=True) is None
merged.filter({'acquisition': 'fullbrain'}, inplace=True)
assert merged.to_df().shape == (40, 9)


@pytest.mark.parametrize(
"TR, nvols",
[(2.00000, 251),
(2.000001, 251)])
def test_resampling_edge_case(tmpdir, TR, nvols):
tmpdir.chdir()
os.makedirs('sub-01/func')
with open('sub-01/func/sub-01_task-task_events.tsv', 'w') as fobj:
fobj.write('onset\tduration\tval\n1\t0.1\t1\n')
with open('sub-01/func/sub-01_task-task_bold.json', 'w') as fobj:
json.dump({'RepetitionTime': TR}, fobj)

dataobj = np.zeros((5, 5, 5, nvols), dtype=np.int16)
affine = np.diag((2.5, 2.5, 2.5, 1))
img = nb.Nifti1Image(dataobj, affine)
img.header.set_zooms((2.5, 2.5, 2.5, TR))
img.to_filename('sub-01/func/sub-01_task-task_bold.nii.gz')

layout = BIDSLayout('.', validate=False)
coll = load_variables(layout).get_collections('run')[0]
dense_var = coll.variables['val'].to_dense(coll.sampling_rate)
regressor = dense_var.resample(1.0 / TR).values
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably note that you do get the right shape if you go directly to 1/TR:

regressor = coll.variables['val'].to_dense(1.0 / TR).values

assert regressor.shape == (nvols, 1)