Skip to content

Commit cc3293a

Browse files
authored
Merge pull request #1120 from effigies/fix/load_collections_dict_mutation
fix: Copy entities dict before modifying run variable type
2 parents 7af6a63 + 8dcc735 commit cc3293a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/bids/modeling/tests/test_statsmodels.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from bids.modeling import BIDSStatsModelsGraph
1313
from bids.modeling.statsmodels import ContrastInfo, expand_wildcards
1414
from bids.layout import BIDSLayout
15+
from bids.layout.utils import PaddedInt
1516
from bids.tests import get_test_data_path
1617
from bids.variables import BIDSVariableCollection
1718

@@ -43,6 +44,13 @@ def graph_nodummy():
4344
graph.load_collections(scan_length=480, subject=["01", "02"])
4445
return graph
4546

47+
def test_load_collections_types(graph):
48+
# Very narrow regression test that ensures that load_collections does not
49+
# modify the types of entities in-place
50+
layout = graph.layout
51+
bold = layout.get(suffix="bold", extension="nii.gz")[0]
52+
assert isinstance(bold.entities["run"], PaddedInt)
53+
4654
@pytest.mark.skipif(not has_graphviz, reason="Test requires graphviz")
4755
def test_write_graph(graph, tmp_path):
4856
from graphviz import Digraph
@@ -306,4 +314,4 @@ def test_missing_value_fill():
306314
graph.run_graph(missing_values='ignore')
307315

308316
outputs = graph.nodes['run'].outputs_
309-
assert np.isnan(outputs[0].model_spec.X).any().any()
317+
assert np.isnan(outputs[0].model_spec.X).any().any()

src/bids/variables/io.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,13 @@ def _load_time_variables(layout, dataset=None, columns=None, scan_length=None,
188188
# Main loop over images
189189
for img_obj in images:
190190

191-
entities = img_obj.entities
192191
img_f = img_obj.path
193192

194-
# Run is not mandatory, but we need a default for proper indexing
193+
# Run is not mandatory, but we need it to behave like an int and not
194+
# a PaddedInt later when we call `dataset.get_nodes('run', select_on)`
195+
# Without this, we would see "run ==02" in the query, which breaks
196+
# pandas.
197+
entities = img_obj.entities.copy()
195198
if 'run' in entities:
196199
entities['run'] = int(entities['run'])
197200

0 commit comments

Comments
 (0)