1
1
from dataclasses import dataclass
2
2
import json
3
+ from os .path import join
3
4
from pathlib import Path
4
5
import pandas as pd
5
6
import pytest
6
7
from nidm .experiment .Utils import map_variables_to_terms , write_json_mapping_file
7
- from os . path import join
8
+
8
9
9
10
@dataclass
10
11
class Setup :
@@ -153,7 +154,7 @@ def setup() -> Setup:
153
154
data = data ,
154
155
reproschema_json_map = reproschema_json_map ,
155
156
bids_sidecar = bids_sidecar ,
156
- bids_sidecar_simple = bids_sidecar_simple ,
157
+ bids_sidecar_simple = bids_sidecar_simple ,
157
158
)
158
159
159
160
@@ -172,8 +173,6 @@ def test_map_vars_to_terms_BIDS(setup: Setup, tmp_path: Path) -> None:
172
173
bids = True ,
173
174
)
174
175
175
-
176
-
177
176
# check whether JSON mapping structure returned from map_variables_to_terms matches the
178
177
# reproshema structure
179
178
assert "DD(source='test', variable='age')" in column_to_terms
@@ -228,7 +227,9 @@ def test_map_vars_to_terms_BIDS(setup: Setup, tmp_path: Path) -> None:
228
227
# original.
229
228
230
229
# write annotations to json file since data element annotations are complete
231
- write_json_mapping_file (column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), True )
230
+ write_json_mapping_file (
231
+ column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), True
232
+ )
232
233
233
234
# now check the JSON sidecar file created by map_variables_to_terms which should match BIDS format
234
235
with open (tmp_path / "nidm_annotations.json" , encoding = "utf-8" ) as fp :
@@ -270,6 +271,7 @@ def test_map_vars_to_terms_BIDS(setup: Setup, tmp_path: Path) -> None:
270
271
271
272
assert len (results ) == 20
272
273
274
+
273
275
def test_map_vars_to_terms_BIDS_simple (setup : Setup , tmp_path : Path ) -> None :
274
276
"""
275
277
This function will test the Utils.py "map_vars_to_terms" function with a BIDS-formatted
@@ -286,33 +288,32 @@ def test_map_vars_to_terms_BIDS_simple(setup: Setup, tmp_path: Path) -> None:
286
288
bids = True ,
287
289
)
288
290
289
-
290
-
291
291
# check whether JSON mapping structure returned from map_variables_to_terms matches the
292
292
# reproshema structure
293
293
assert "DD(source='test', variable='age')" in column_to_terms
294
294
assert "DD(source='test', variable='sex')" in column_to_terms
295
295
assert "description" in column_to_terms ["DD(source='test', variable='age')" ]
296
296
assert "description" in column_to_terms ["DD(source='test', variable='sex')" ]
297
297
298
-
299
298
# force writing of column_to_terms structure because here we're not doing annotations and so
300
299
# map_variables_to_terms won't write it out since we supplied one for it to open...thus it already exists
301
300
# and no annotations were made so it should exist in its original form.
302
301
# By explicitly writing it out here, after running map_variables_to_terms, we can assure it's the same as the
303
302
# original.
304
303
305
304
# write annotations to json file since data element annotations are complete
306
- write_json_mapping_file (column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), True )
305
+ write_json_mapping_file (
306
+ column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), True
307
+ )
307
308
308
309
# now check the JSON sidecar file created by map_variables_to_terms which should match BIDS format
309
310
with open (tmp_path / "nidm_annotations.json" , encoding = "utf-8" ) as fp :
310
311
bids_sidecar = json .load (fp )
311
312
312
313
assert "age" in bids_sidecar .keys ()
313
314
assert "sex" in bids_sidecar .keys ()
314
- assert "description" in bids_sidecar [' age' ]
315
- assert "description" in bids_sidecar [' sex' ]
315
+ assert "description" in bids_sidecar [" age" ]
316
+ assert "description" in bids_sidecar [" sex" ]
316
317
317
318
# check the CDE dataelement graph for correct information
318
319
query = """
@@ -333,6 +334,7 @@ def test_map_vars_to_terms_BIDS_simple(setup: Setup, tmp_path: Path) -> None:
333
334
334
335
assert len (results ) == 16
335
336
337
+
336
338
def test_map_vars_to_terms_reproschema (setup : Setup , tmp_path : Path ) -> None :
337
339
"""
338
340
This function will test the Utils.py "map_vars_to_terms" function with a reproschema-formatted
@@ -400,7 +402,9 @@ def test_map_vars_to_terms_reproschema(setup: Setup, tmp_path: Path) -> None:
400
402
# original.
401
403
402
404
# write annotations to json file since data element annotations are complete
403
- write_json_mapping_file (column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), False )
405
+ write_json_mapping_file (
406
+ column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), False
407
+ )
404
408
405
409
# now check the JSON mapping file created by map_variables_to_terms which should match Reproschema format
406
410
with open (tmp_path / "nidm_annotations_annotations.json" , encoding = "utf-8" ) as fp :
0 commit comments