Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addressed issues in map_variables_to_terms to support dictionary of isAbout. Fixed up test: test_map_vars_to_terms.py to test for the two supported JSON sidecar formats #385

Merged
merged 11 commits into from
Sep 16, 2023
Merged
4 changes: 2 additions & 2 deletions src/nidm/experiment/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,13 @@ def prefix_to_context(self):
# context[key]['@id']= value.uri

# context[key]['@type']='@id'
if type(value.uri) == str:
if type(value.uri) is str:
context[key] = value.uri
# added for some weird namespaces where key is URIRef and value is Namespace
# seems to only apply to PROV and NIDM qualified names.
# has something to do with read_nidm function in Utils and add_metadata_for_subject
# when it comes across a NIDM or PROV term.
elif type(key) == URIRef:
elif type(key) is URIRef:
continue
else:
context[key] = str(value.uri)
Expand Down
2 changes: 1 addition & 1 deletion src/nidm/experiment/Navigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def expandID(id, namespace): # noqa: A002
if id.find("http") < 0:
return namespace[id]
# it has a http, but isn't a URIRef so convert it
if type(id) == str:
if type(id) is str:
return URIRef(id)

return id
Expand Down
2 changes: 1 addition & 1 deletion src/nidm/experiment/Query.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def GetProjectsUUID(nidm_file_list, output_file=None):
"""
df = sparql_query_nidm(nidm_file_list, query, output_file=output_file)

return df["uuid"] if type(df["uuid"]) == list else df["uuid"].tolist()
return df["uuid"] if type(df["uuid"]) is list else df["uuid"].tolist()


def GetProjectLocation(nidm_file_list, project_uuid, output_file=None): # noqa: U100
Expand Down
210 changes: 129 additions & 81 deletions src/nidm/experiment/Utils.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/nidm/experiment/tools/nidm2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def GetDataElementMetadata(nidm_graph, de_uuid):
de[current_tuple]["isAbout"] = []

# check whether there are multiple 'isAbout' entries
if type(value) == "list":
if isinstance(value, "list"):
# if this is a list we have to loop through the entries and store the url and labels
for entry in value:
# query for label for this isAbout URL
Expand Down
2 changes: 1 addition & 1 deletion src/nidm/experiment/tools/nidm_linreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from nidm.experiment.Query import GetProjectsUUID
from nidm.experiment.tools.click_base import cli
from nidm.experiment.tools.rest import RestParser
from .utils import Reporter
from nidm.experiment.tools.utils import Reporter

MAX_ALPHA = 700

Expand Down
30 changes: 15 additions & 15 deletions src/nidm/experiment/tools/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def arrayFormat(self, result, headers):
def allUUIDs(arr):
uuid_only = True
for s in arr:
if type(s) != str or not re.match(
if type(s) is not str or not re.match(
"^[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+$", s
):
uuid_only = False
Expand All @@ -72,7 +72,7 @@ def dictFormat(self, result, headers=None):
appendicies = []
for key in result:
# format a list
if type(result[key]) == list:
if type(result[key]) is list:
appendix = []
for line in result[key]:
appendix.append([json.dumps(line)])
Expand All @@ -83,11 +83,11 @@ def dictFormat(self, result, headers=None):
table.append([json.dumps(key), ",".join(result[key])])

# format a string
elif type(result[key]) == str:
elif type(result[key]) is str:
table.append([json.dumps(key), result[key]])

# format a dictionary
elif type(result[key]) == dict:
elif type(result[key]) is dict:
# put any dict into it's own table at the end (sort of like an appendix)
appendix = []
for inner_key in result[key]:
Expand All @@ -97,7 +97,7 @@ def dictFormat(self, result, headers=None):
# format anything else
else:
col1 = json.dumps(key)
if type(result[key]) == set:
if type(result[key]) is set:
col2 = json.dumps(list(result[key]))
else:
col2 = json.dumps(result[key])
Expand All @@ -115,10 +115,10 @@ def flatten(obj, maxDepth=10, table=None, rowInProgress=None, depth=0):
rowInProgress = []
for key in obj:
newrow = deepcopy(rowInProgress)
if depth < maxDepth and type(obj[key]) == dict:
if depth < maxDepth and type(obj[key]) is dict:
newrow.append(key)
flatten(obj[key], maxDepth, table, newrow, depth + 1)
elif type(obj[key]) == str:
elif type(obj[key]) is str:
newrow.append(key)
newrow.append(obj[key])
table.append(newrow)
Expand Down Expand Up @@ -319,9 +319,9 @@ def subjectSummaryFormat(self, result):
toptable.append([key, result[key]])

for key in special_keys:
if type(result[key]) == dict:
if type(result[key]) is dict:
toptable.append([key, ",".join(result[key].keys())])
elif type(result[key]) == list:
elif type(result[key]) is list:
toptable.append([key, ",".join(result[key])])
else:
toptable.append([key, json.dumps(result[key])])
Expand All @@ -345,15 +345,15 @@ def subjectSummaryFormat_v2(self, result):

for key in special_keys:
if key in result:
if type(result[key]) == dict:
if type(result[key]) is dict:
toptable.append([key, ",".join(result[key].keys())])
if (
type(result[key]) == list
type(result[key]) is list
and len(result[key]) > 0
and type(result[key][0]) == Navigate.ActivityData
and type(result[key][0]) is Navigate.ActivityData
):
toptable.append([key, ",".join([x.uuid for x in result[key]])])
elif type(result[key]) == list:
elif type(result[key]) is list:
toptable.append([key, ",".join])
else:
toptable.append([key, json.dumps(result[key])])
Expand Down Expand Up @@ -950,9 +950,9 @@ def format(self, result, headers=None):
return json_str

elif self.output_format == RestParser.CLI_FORMAT:
if type(result) == dict:
if type(result) is dict:
return self.dictFormat(result, headers)
if type(result) == list:
if type(result) is list:
return self.arrayFormat(result, headers)
else:
return str(result)
Expand Down
2 changes: 1 addition & 1 deletion src/nidm/experiment/tools/rest_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def GetProjectsComputedMetadata(nidm_file_list):

for a in activities:
data = Navigate.getActivityData(tuple(nidm_file_list), a)
if type(data) == Navigate.ActivityData:
if type(data) is Navigate.ActivityData:
for x in data.data:
if x.isAbout == Constants.NIDM_IS_ABOUT_AGE:
if (
Expand Down
8 changes: 6 additions & 2 deletions tests/experiment/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
def brain_vol_files(tmp_path_factory: pytest.TempPathFactory) -> list[str]:
tmp_path = tmp_path_factory.mktemp("brain_vol_files")
urlretrieve(
"https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/master/datasets.datalad.org/abide/RawDataBIDS/CMU_a/nidm.ttl",
# commented out until latest simple2 NIDM files contain brain volumes again...
# "https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/master/datasets.datalad.org/abide/RawDataBIDS/CMU_a/nidm.ttl",
"https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/a7e3fa291975c08932a412d937d12199da62dd28/datasets.datalad.org/abide/RawDataBIDS/CMU_a/nidm.ttl",
tmp_path / "cmu_a.nidm.ttl",
)
urlretrieve(
"https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/master/datasets.datalad.org/abide/RawDataBIDS/Caltech/nidm.ttl",
# commented out until latest simple2 NIDM files contain brain volumes again...
# "https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/master/datasets.datalad.org/abide/RawDataBIDS/Caltech/nidm.ttl",
"https://raw.githubusercontent.com/dbkeator/simple2_NIDM_examples/a7e3fa291975c08932a412d937d12199da62dd28/datasets.datalad.org/abide/RawDataBIDS/Caltech/nidm.ttl",
tmp_path / "caltech.nidm.ttl",
)
return [
Expand Down
4 changes: 3 additions & 1 deletion tests/experiment/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ def test_GetProjectAttributes(abide: ProjectData) -> None:
project_attributes = nidm.experiment.Navigate.GetProjectAttributes(
files, project_uuid
)
assert ("prov:Location" in project_attributes) or ("Location" in project_attributes)
# 8/31/23 - commented out test because we remove local prov:Location if datalad remotes aren't available
# so this test will fail under those circumstances
# assert ("prov:Location" in project_attributes) or ("Location" in project_attributes)
assert ("dctypes:title" in project_attributes) or ("title" in project_attributes)
assert (
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type" in project_attributes
Expand Down
28 changes: 14 additions & 14 deletions tests/experiment/tools/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ def test_uri_subject_list(brain_vol: BrainVol, openneuro: OpenNeuro) -> None:
restParser = RestParser(output_format=RestParser.OBJECT_FORMAT)
result = restParser.run(brain_vol.files + openneuro.files, "/subjects")

assert type(result) == dict
assert type(result["subject"]) == list
assert type(result) is dict
assert type(result["subject"]) is list
assert len(result["subject"]) > 10


Expand All @@ -199,15 +199,15 @@ def test_uri_subject_list_with_fields(
brain_vol.files + openneuro.files,
"/subjects?fields=ilx_0100400,MagneticFieldStrength",
) # ilx_0100400 "is about" age
assert type(result) == dict
assert type(result) is dict

assert type(result["subject"]) == list
assert type(result["subject"]) is list
assert len(result["subject"]) > 10

assert type(result["fields"]) == dict
assert type(result["fields"]) is dict
all_fields = []
for sub in result["fields"]:
assert type(result["fields"][sub]) == dict
assert type(result["fields"][sub]) is dict
for activity in result["fields"][sub]:
all_fields.append(result["fields"][sub][activity].label)
if result["fields"][sub][activity].value != "n/a":
Expand Down Expand Up @@ -250,7 +250,7 @@ def test_uri_project_list(tmp_path: Path) -> None:
for uuid_ in result:
project_uuids.append(uuid_)

assert type(result) == list
assert type(result) is list
assert len(project_uuids) >= 2
assert proj1_uuid in project_uuids
assert proj2_uuid in project_uuids
Expand All @@ -276,7 +276,7 @@ def test_uri_projects_subjects_1(rest_test: RestTest) -> None:
restParser = RestParser()
result = restParser.run([rest_test.path], f"/projects/{proj_uuid}/subjects")

assert type(result) == dict
assert type(result) is dict
assert len(result["uuid"]) == 2

assert rest_test.p2_subject_uuids[0] in result["uuid"]
Expand All @@ -290,7 +290,7 @@ def test_uri_subjects(brain_vol: BrainVol) -> None:
brain_vol.files, f"/subjects/{brain_vol.cmu_test_subject_uuid}"
)

assert type(result) == dict
assert type(result) is dict
assert "uuid" in result
assert "instruments" in result
assert "derivatives" in result
Expand All @@ -308,7 +308,7 @@ def test_uri_projects_subjects_id(openneuro: OpenNeuro) -> None:
uri = f"/projects/{project}/subjects/{subject}"
result = restParser.run(openneuro.files, uri)

assert type(result) == dict
assert type(result) is dict
assert result["uuid"] == subject
assert len(result["instruments"]) > 2

Expand Down Expand Up @@ -663,7 +663,7 @@ def test_multiple_project_fields(brain_vol: BrainVol) -> None:
# fv = project['field_values']
print(fields)
fv = fields
assert type(fv) == list
assert type(fv) is list
fields_used = {i.label for i in fv}
assert ("brain" in fields_used) or (
"Brain Segmentation Volume (mm^3)" in fields_used
Expand All @@ -685,7 +685,7 @@ def test_odd_isabout_uris(brain_vol: BrainVol) -> None:
# fv = project['field_values']
print(fields)
fv = fields
assert type(fv) == list
assert type(fv) is list
fields_used = {i.label for i in fv}
assert "ADOS_TOTAL" in fields_used

Expand All @@ -704,7 +704,7 @@ def test_project_fields_deriv(brain_vol: BrainVol) -> None:
assert len(project) > 0
# fv = project['field_values']
fv = project
assert type(fv) == list
assert type(fv) is list
fields_used = {i.label for i in fv}
assert ("brain" in fields_used) or (
"Brain Segmentation Volume (mm^3)" in fields_used
Expand All @@ -727,7 +727,7 @@ def test_project_fields_instruments(brain_vol: BrainVol) -> None:
assert len(project) > 0
# fv = project['field_values']
fv = project
assert type(fv) == list
assert type(fv) is list
fields_used = {i.label for i in fv}
assert field in fields_used

Expand Down
4 changes: 2 additions & 2 deletions tests/experiment/tools/test_rest_dataelements.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_dataelement_list(
rest_parser = RestParser(output_format=RestParser.OBJECT_FORMAT)
result = rest_parser.run(openneuro_files, "/dataelements")

assert type(result) == dict
assert type(result) is dict
assert "data_elements" in result
assert "uuid" in result["data_elements"]
assert "label" in result["data_elements"]
Expand All @@ -75,7 +75,7 @@ def test_dataelement_list(

# now check for derivatives
result = rest_parser.run(brain_vol_files, "/dataelements")
assert type(result) == dict
assert type(result) is dict
assert (
"Left-WM-hypointensities Volume_mm3 (mm^3)" in result["data_elements"]["label"]
)
Expand Down