Skip to content

Commit 81acce2

Browse files
committed
working on migration script for old nidm files
1 parent 613dadd commit 81acce2

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

src/nidm/core/Constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
SKOS = Namespace("http://www.w3.org/2004/02/skos/core#")
5151
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
5252
VC = Namespace("http://www.w3.org/2006/vcard/ns#")
53-
DICOM = Namespace("http://neurolex.org/wiki/Category:DICOM_term/")
53+
DICOM = Namespace("http://neurolex.org/wiki/Category/DICOM_term/")
5454
DCTYPES = Namespace("http://purl.org/dc/dcmitype/")
5555
NCIT = Namespace("http://ncitt.ncit.nih.gov/")
5656
DCAT = Namespace("http://www.w3.org/ns/dcat#")

src/nidm/experiment/Query.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def getProjectAcquisitionObjects(nidm_file_list, project_id):
660660
# find all the projects
661661
for project, _, _ in rdf_graph.triples((None, None, Constants.NIDM["Project"])):
662662
# check if it is our project
663-
if str(project) == project_uuid:
663+
if (str(project) == project_uuid) or (str(project) == str(project_uuid)):
664664
for session, _, _ in rdf_graph.triples(
665665
(None, isa, Constants.NIDM["Session"])
666666
):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""Tools for working with NIDM-Experiment files"""
2+
3+
import click
4+
from rdflib import Graph, Literal, Namespace, URIRef, util
5+
from rdflib.namespace import RDF, split_uri
6+
from nidm.core import Constants
7+
from nidm.experiment.Core import getUUID
8+
from nidm.experiment.Query import GetProjectsMetadata, getProjectAcquisitionObjects
9+
from nidm.experiment.tools.click_base import cli
10+
11+
12+
# adding click argument parsing
13+
@cli.command()
14+
@click.option(
15+
"--nidm_file_list",
16+
"-nl",
17+
required=True,
18+
help="A comma separated list of NIDM files with full path",
19+
)
20+
def update(nidm_file_list):
21+
"""
22+
This function will update simple 2 NIDM files. It will essentially move some of the metadata in the project activity
23+
to a prov:Collection entity and then include all nidm:AcquisitionObject entities as prov:hadMember entries in this
24+
new prov:Collection
25+
"""
26+
BIDS = Namespace(Constants.BIDS)
27+
PROV = Namespace(Constants.PROV)
28+
NIIRI = Namespace(Constants.NIIRI)
29+
30+
# for each nidm file provided
31+
for nidm_file in nidm_file_list.split(","):
32+
# create empty graph
33+
graph_orig = Graph()
34+
35+
# bind namespaces
36+
graph_orig.bind("bids", BIDS)
37+
graph_orig.bind("prov", PROV)
38+
graph_orig.bind("niiri", NIIRI)
39+
40+
# load graph
41+
graph_orig.parse(nidm_file, format=util.guess_format(nidm_file))
42+
43+
# create a new prov:Collection, prov:Entity to store metadata about project
44+
collection_uuid = getUUID()
45+
graph_orig.add((NIIRI.term(collection_uuid), RDF.type, BIDS.Dataset))
46+
graph_orig.add((NIIRI.term(collection_uuid), RDF.type, PROV.Collection))
47+
graph_orig.add((NIIRI.term(collection_uuid), RDF.type, PROV.Entity))
48+
49+
# query for all metadata in nidm:Project activity
50+
proj_metadata = GetProjectsMetadata([nidm_file])
51+
52+
# loop through projects and get associated acquisition objects
53+
for proj_uuid in proj_metadata["projects"].keys():
54+
# for all project metadata, only leave dcmitype:title, otherwise move to collection_uuid
55+
for metadata_key in proj_metadata["projects"][proj_uuid].keys():
56+
if (metadata_key != str(RDF.type)) and (
57+
metadata_key != "dctypes:title"
58+
):
59+
# get namespace and term from metadata_key
60+
nm, term = split_uri(metadata_key)
61+
62+
# add RDF namespace
63+
temp_nm = Namespace(str(Constants.namespaces[nm.split(":", 1)[0]]))
64+
graph_orig.bind(nm.split(":", 1)[0], temp_nm)
65+
66+
if (metadata_key == "dcat:creator") or (
67+
metadata_key == "prov:Location"
68+
):
69+
# now add this metadata to the collection_uuid
70+
graph_orig.add(
71+
(
72+
NIIRI.collection_uuid,
73+
temp_nm.term(term),
74+
URIRef(
75+
proj_metadata["projects"][proj_uuid][metadata_key]
76+
),
77+
)
78+
)
79+
else:
80+
# now add this metadata to the collection_uuid
81+
graph_orig.add(
82+
(
83+
NIIRI.collection_uuid,
84+
temp_nm.term(term),
85+
Literal(
86+
proj_metadata["projects"][proj_uuid][metadata_key]
87+
),
88+
)
89+
)
90+
91+
# get acquisition objects
92+
acq_objects = getProjectAcquisitionObjects(
93+
[nidm_file], project_id=str(proj_uuid.split(":", 1)[1])
94+
)
95+
96+
for acq_obj in acq_objects.keys():
97+
print(acq_obj)
98+
99+
# graph.serialize(out_file, format="turtle")
100+
101+
102+
if __name__ == "__main__":
103+
update()

0 commit comments

Comments
 (0)