From 8cfba0610b03d0b8ca20b30037baef5f86b7bd23 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Sat, 26 Jan 2019 21:32:50 -0500 Subject: [PATCH 1/2] BUG: Demonstrate resampling bug --- bids/variables/tests/test_variables.py | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/bids/variables/tests/test_variables.py b/bids/variables/tests/test_variables.py index 339ad8553..74876489c 100644 --- a/bids/variables/tests/test_variables.py +++ b/bids/variables/tests/test_variables.py @@ -1,5 +1,6 @@ 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, @@ -7,7 +8,9 @@ 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): @@ -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 + assert regressor.shape == (nvols, 1) From f936aedcf36f715914816597d692ca37f9297f55 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Tue, 29 Jan 2019 09:50:26 -0500 Subject: [PATCH 2/2] FIX: Round duration estimation --- bids/variables/variables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bids/variables/variables.py b/bids/variables/variables.py index 0f98548bb..53eebe1b2 100644 --- a/bids/variables/variables.py +++ b/bids/variables/variables.py @@ -323,7 +323,7 @@ def to_dense(self, sampling_rate): A DenseRunVariable. ''' - duration = int(math.ceil(sampling_rate * self.get_duration())) + duration = int(np.round(sampling_rate * self.get_duration())) ts = np.zeros(duration, dtype=self.values.dtype) onsets = np.round(self.onset * sampling_rate).astype(int)