Skip to content

Commit a9ae623

Browse files
committed
add additional support for transformation parsing
Add tests and add support for: Model jsons with transformations Transformation jsons Python in memory representations of the above
1 parent 02cd6fc commit a9ae623

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

bids/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"reports",
1515
"utils",
1616
"variables",
17-
"statsmodels_design_synthesizer",
1817
]
1918

2019
due.cite(Doi("10.1038/sdata.2016.44"),

bids/variables/io.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,13 +584,13 @@ def parse_transforms(transforms_in, validate=True,level="run"):
584584
# transformations has been obtained. This will most likely be the case since
585585
# transformations at higher levels will no longer be required when the new
586586
# "flow" approach is used.
587-
if "nodes" in transforms_raw:
588-
nodes_key = "nodes"
589-
elif "steps" in transforms_raw:
590-
nodes_key = "steps"
587+
if "transformations" in transforms_raw:
588+
transforms = transforms_raw["transformations"]
589+
elif any(k in transforms_raw for k in ["nodes","steps"]):
590+
nodes_key = "nodes" if "nodes" in transforms_raw else "steps"
591+
transforms = transforms_raw[nodes_key][0]["transformations"]
591592
else:
592593
raise ValueError("Cannot find a key for nodes in the json input representing the model")
593-
transforms = transforms_raw[nodes_key][0]["transformations"]
594594
return transforms
595595

596596

bids/variables/tests/test_io.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
from bids.variables import (SparseRunVariable, SimpleVariable,
33
DenseRunVariable, load_variables)
44
from bids.variables.entities import Node, RunNode, NodeIndex
5+
from bids.variables.io import parse_transforms
56
from unittest.mock import patch
67
import pytest
78
from os.path import join
9+
from pathlib import Path
10+
import tempfile
11+
import json
812
from bids.tests import get_test_data_path
913
from bids.config import set_option, get_option
1014

15+
EXAMPLE_TRANSFORM = {
16+
"Transformations":[{"Name":"example_trans","Inputs":["col_a","col_b"]}]
17+
}
18+
TRANSFORMS_JSON = join(tempfile.tempdir,"tranformations.json")
19+
Path(TRANSFORMS_JSON).write_text(json.dumps(EXAMPLE_TRANSFORM))
1120

1221
@pytest.fixture
1322
def layout1():
@@ -103,3 +112,29 @@ def test_load_synthetic_dataset(synthetic):
103112
subs = index.get_nodes('subject')
104113
assert len(subs) == 5
105114
assert set(subs[0].variables.keys()) == {'systolic_blood_pressure'}
115+
116+
@pytest.mark.parametrize(
117+
"test_case,transform_input,expected_names",
118+
[
119+
("raw transform json",
120+
EXAMPLE_TRANSFORM,
121+
["example_trans"]
122+
),
123+
("transform json file",
124+
TRANSFORMS_JSON,
125+
["example_trans"]
126+
),
127+
("raw model json",
128+
{"Nodes": [EXAMPLE_TRANSFORM]},
129+
["example_trans"]
130+
),
131+
("model json file",
132+
str(Path(get_test_data_path()) / "ds005/models/ds-005_type-mfx_model.json"),
133+
["Scale"]
134+
),
135+
]
136+
)
137+
def test_parse_transforms(test_case,transform_input,expected_names):
138+
result = parse_transforms(transform_input)
139+
transformation_names = [x['name'] for x in result]
140+
assert expected_names == transformation_names

0 commit comments

Comments
 (0)