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
- from nidm .experiment .Utils import map_variables_to_terms
7
+ from nidm .experiment .Utils import map_variables_to_terms , write_json_mapping_file
7
8
8
9
9
10
@dataclass
10
11
class Setup :
11
12
data : pd .DataFrame
12
13
reproschema_json_map : dict
13
14
bids_sidecar : dict
15
+ bids_sidecar_simple : dict
14
16
15
17
16
18
@pytest .fixture (scope = "module" )
@@ -135,11 +137,24 @@ def setup() -> Setup:
135
137
}
136
138
"""
137
139
)
140
+ bids_sidecar_simple = json .loads (
141
+ """
142
+ {
143
+ "age": {
144
+ "description": "age of participant"
145
+ },
146
+ "sex": {
147
+ "description": "biological sex of participant"
148
+ }
149
+ }
150
+ """
151
+ )
138
152
139
153
return Setup (
140
154
data = data ,
141
155
reproschema_json_map = reproschema_json_map ,
142
156
bids_sidecar = bids_sidecar ,
157
+ bids_sidecar_simple = bids_sidecar_simple ,
143
158
)
144
159
145
160
@@ -149,6 +164,7 @@ def test_map_vars_to_terms_BIDS(setup: Setup, tmp_path: Path) -> None:
149
164
JSON sidecar file
150
165
"""
151
166
167
+ # test BIDS sidecar json file with all pynidm annotations
152
168
column_to_terms , cde = map_variables_to_terms (
153
169
df = setup .data ,
154
170
json_source = setup .bids_sidecar ,
@@ -204,6 +220,17 @@ def test_map_vars_to_terms_BIDS(setup: Setup, tmp_path: Path) -> None:
204
220
]["Male" ]
205
221
)
206
222
223
+ # force writing of column_to_terms structure because here we're not doing annotations and so
224
+ # map_variables_to_terms won't write it out since we supplied one for it to open...thus it already exists
225
+ # and no annotations were made so it should exist in its original form.
226
+ # By explicitly writing it out here, after running map_variables_to_terms, we can assure it's the same as the
227
+ # original.
228
+
229
+ # write annotations to json file since data element annotations are complete
230
+ write_json_mapping_file (
231
+ column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), True
232
+ )
233
+
207
234
# now check the JSON sidecar file created by map_variables_to_terms which should match BIDS format
208
235
with open (tmp_path / "nidm_annotations.json" , encoding = "utf-8" ) as fp :
209
236
bids_sidecar = json .load (fp )
@@ -245,6 +272,69 @@ def test_map_vars_to_terms_BIDS(setup: Setup, tmp_path: Path) -> None:
245
272
assert len (results ) == 20
246
273
247
274
275
+ def test_map_vars_to_terms_BIDS_simple (setup : Setup , tmp_path : Path ) -> None :
276
+ """
277
+ This function will test the Utils.py "map_vars_to_terms" function with a BIDS-formatted
278
+ JSON sidecar file
279
+ """
280
+
281
+ # test BIDS sidecar json file with all pynidm annotations
282
+ column_to_terms , cde = map_variables_to_terms (
283
+ df = setup .data ,
284
+ json_source = setup .bids_sidecar_simple ,
285
+ directory = str (tmp_path ),
286
+ assessment_name = "test" ,
287
+ associate_concepts = False ,
288
+ bids = True ,
289
+ )
290
+
291
+ # check whether JSON mapping structure returned from map_variables_to_terms matches the
292
+ # reproshema structure
293
+ assert "DD(source='test', variable='age')" in column_to_terms
294
+ assert "DD(source='test', variable='sex')" in column_to_terms
295
+ assert "description" in column_to_terms ["DD(source='test', variable='age')" ]
296
+ assert "description" in column_to_terms ["DD(source='test', variable='sex')" ]
297
+
298
+ # force writing of column_to_terms structure because here we're not doing annotations and so
299
+ # map_variables_to_terms won't write it out since we supplied one for it to open...thus it already exists
300
+ # and no annotations were made so it should exist in its original form.
301
+ # By explicitly writing it out here, after running map_variables_to_terms, we can assure it's the same as the
302
+ # original.
303
+
304
+ # write annotations to json file since data element annotations are complete
305
+ write_json_mapping_file (
306
+ column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), True
307
+ )
308
+
309
+ # now check the JSON sidecar file created by map_variables_to_terms which should match BIDS format
310
+ with open (tmp_path / "nidm_annotations.json" , encoding = "utf-8" ) as fp :
311
+ bids_sidecar = json .load (fp )
312
+
313
+ assert "age" in bids_sidecar .keys ()
314
+ assert "sex" in bids_sidecar .keys ()
315
+ assert "description" in bids_sidecar ["age" ]
316
+ assert "description" in bids_sidecar ["sex" ]
317
+
318
+ # check the CDE dataelement graph for correct information
319
+ query = """
320
+ prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
321
+
322
+ select distinct ?uuid ?DataElements ?property ?value
323
+ where {
324
+
325
+ ?uuid a/rdfs:subClassOf* nidm:DataElement ;
326
+ ?property ?value .
327
+
328
+ }"""
329
+ qres = cde .query (query )
330
+
331
+ results = []
332
+ for row in qres :
333
+ results .append (list (row ))
334
+
335
+ assert len (results ) == 16
336
+
337
+
248
338
def test_map_vars_to_terms_reproschema (setup : Setup , tmp_path : Path ) -> None :
249
339
"""
250
340
This function will test the Utils.py "map_vars_to_terms" function with a reproschema-formatted
@@ -305,6 +395,17 @@ def test_map_vars_to_terms_reproschema(setup: Setup, tmp_path: Path) -> None:
305
395
]["Male" ]
306
396
)
307
397
398
+ # force writing of column_to_terms structure because here we're not doing annotations and so
399
+ # map_variables_to_terms won't write it out since we supplied one for it to open...thus it already exists
400
+ # and no annotations were made so it should exist in its original form.
401
+ # By explicitly writing it out here, after running map_variables_to_terms, we can assure it's the same as the
402
+ # original.
403
+
404
+ # write annotations to json file since data element annotations are complete
405
+ write_json_mapping_file (
406
+ column_to_terms , join (str (tmp_path ), "nidm_annotations.json" ), False
407
+ )
408
+
308
409
# now check the JSON mapping file created by map_variables_to_terms which should match Reproschema format
309
410
with open (tmp_path / "nidm_annotations_annotations.json" , encoding = "utf-8" ) as fp :
310
411
json .load (fp )
0 commit comments