Skip to content

Commit

Permalink
Merge pull request #268 from dbkeator/master
Browse files Browse the repository at this point in the history
fixes version of ontquery to 0.2.3.
  • Loading branch information
dbkeator authored Mar 24, 2021
2 parents 0b27cd8 + 879ab48 commit 42ec135
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 20 deletions.
35 changes: 29 additions & 6 deletions nidm/core/BIDS_Constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,42 @@
"T2w" : Constants.NIDM_MRI_T2,
"inplaneT2" : Constants.NIDM_MRI_T2,
"bold" : Constants.NIDM_MRI_FLOW,
"dti" : Constants.NIDM_MRI_DIFFUSION_TENSOR
"dti" : Constants.NIDM_MRI_DIFFUSION_TENSOR,
"asl" : Constants.NIDM_MRI_ASL
}
#JSON file keys
json_keys = {
##Image terms
"run" : Constants.NIDM_ACQUISITION_ENTITY,
"ImageType" : Constants.DICOM["ImageType"],
"ManufacturerModelName" : Constants.DICOM["ManufacturerModelName"],
"Manufacturer" : Constants.DICOM["Manufacturer"],
"ScanningSequence" : Constants.DICOM["ScanningSequence"],
"SequenceVariant" : Constants.DICOM["SequenceVariant"],
"ScanOptions" : Constants.DICOM["ScanOptions"],
"MRAcquisitionType" : Constants.DICOM["MRAcquisitionType"],
"SequenceName" : Constants.DICOM["SequenceName"],
"RepetitionTime" : Constants.DICOM["RepetitionTime"],
"EchoTime" : Constants.DICOM["EchoTime"],
"RepetitionTimePreparation" : Constants.BIDS["RepetitionTimePreparation"],
"ArterialSpinLabelingType" : Constants.BIDS["ArterialSpinLabelingType"],
"PostLabelingDelay" : Constants.BIDS["PostLabelingDelay"],
"BackgroundSuppression" : Constants.BIDS["BackgroundSuppression"],
"BackgroundSuppressionPulseTime" : Constants.BIDS["BackgroundSuppressionPulseTime"],
"BackgroundSuppressionNumberPulses" : Constants.BIDS["BackgroundSuppressionNumberPulses"],
"LabelingLocationDescription" : Constants.BIDS["LabelingLocationDescription"],
"LookLocker" : Constants.BIDS["LookLocker"],
"LabelingEfficiency" : Constants.BIDS["LabelingEfficiency"],
"LabelingDuration" : Constants.BIDS["LabelingDuration"],
"LabelingPulseAverageGradient" : Constants.BIDS["LabelingPulseAverageGradient"],
"LabelingPulseMaximumGradient" : Constants.BIDS["LabelingPulseMaximumGradient"],
"LabelingPulseDuration" : Constants.BIDS["LabelingPulseDuration"],
"LabelingPulseFlipAngle" : Constants.BIDS["LabelingPulseFlipAngle"],
"LabelingPulseInterval" : Constants.BIDS["LabelingPulseInterval"],
"PCASLType" : Constants.BIDS["PCASLType"],
"M0Type" : Constants.BIDS["M0Type"],
"TotalAcquiredPairs" : Constants.BIDS["TotalAcquiredPairs"],
"VascularCrushing" : Constants.BIDS["VascularCrushing"],
"EchoTime" : Constants.BIDS["EchoTime"],
"InversionTime" : Constants.DICOM["InversionTime"],
"NumberOfAverages" : Constants.DICOM["NumberOfAverages"],
"ImagingFrequency" : Constants.DICOM["ImagingFrequency"],
Expand All @@ -66,17 +87,19 @@
"ProtocolName": Constants.DICOM["ProtocolName"],
"TransmitCoilName": Constants.DICOM["TransmitCoilName"],
"AcquisitionMatrix": Constants.DICOM["AcquisitionMatrix"],
"AcquisitionVoxelSize" : Constants.BIDS["AcquisitionVoxelSize"],
"InPlanePhaseEncodingDirection": Constants.DICOM["InPlanePhaseEncodingDirection"],
"FlipAngle": Constants.DICOM["FlipAngle"],
"FlipAngle": Constants.BIDS["FlipAngle"],
"VariableFlipAngleFlag": Constants.DICOM["VariableFlipAngleFlag"],
"PatientPosition": Constants.DICOM["PatientPosition"],
"PhaseEncodingDirection": Constants.NIDM["PhaseEncodingDirection"],
"SliceTiming": Constants.NIDM["SliceTiming"],
"TotalReadoutTime" : Constants.DICOM["TotalReadoutTime"],
"PhaseEncodingDirection": Constants.BIDS["PhaseEncodingDirection"],
"SliceTiming": Constants.BIDS["SliceTiming"],
"TotalReadoutTime" : Constants.BIDS["TotalReadoutTime"],
"EffectiveEchoSpacing" : Constants.NIDM["EffectiveEchoSpacing"],
"NumberDiscardedVolumesByScanner" : Constants.NIDM["NumberDiscardedVolumesByScanner"],
"NumberDiscardedVolumesByUser" : Constants.NIDM["NumberDiscardedVolumesByUser"],
"DelayTime" : Constants.NIDM["DelayTime"],
"PulseSequenceType" : Constants.DICOM["PulseSequenceName"],
###Task Stuff
"TaskName" : Constants.NIDM_MRI_FUNCTION_TASK
# "CogAtlasID" :
Expand Down
1 change: 1 addition & 0 deletions nidm/core/Constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ def __init__(self, namespaces=None):
NIDM_MRI_DIFFUSION_TENSOR = QualifiedName(provNamespace("nidm", NIDM),"DiffusionTensor")
NIDM_MRI_FLOW = QualifiedName(provNamespace("nidm", NIDM),"FlowWeighted")
NIDM_MRI_BOLD_EVENTS = QualifiedName(provNamespace("nidm", NIDM),"StimulusResponseFile")
NIDM_MRI_ASL = QualifiedName(provNamespace("nidm",NIDM),"ArterialSpinLabeling")
CRYPTO_SHA512 =QualifiedName(provNamespace("crypto", CRYPTO),"sha512")
DATALAD_LOCATION = QualifiedName(provNamespace("datalad", DATALAD),"Location")
##############################################################################
Expand Down
19 changes: 14 additions & 5 deletions nidm/experiment/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,18 +361,21 @@ def serializeJSONLD(self):



#WIP: currently this creates a default JSON-LD context from Constants.py and not in the correct way from the
#NIDM-E OWL files that that will be the next iteration
# WIP: currently this creates a default JSON-LD context from Constants.py and not in the correct way from the
# NIDM-E OWL files that that will be the next iteration
context1 = self.createDefaultJSONLDcontext()
#This part adds to the context any prefixes in an existing NIDM-E file that might have been added by a user
#and isn't covered by the default namespaces / constants in Constants.py
# This part adds to the context any prefixes in an existing NIDM-E file that might have been added by a user
# and isn't covered by the default namespaces / constants in Constants.py
context2 = self.prefix_to_context()
context = dict(context1, **context2)

#WIP: LOOK AT https://github.com/satra/nidm-jsonld
# WIP: LOOK AT https://github.com/satra/nidm-jsonld
#return rdf_graph_parse.serialize(format='json-ld', context=context, indent=4).decode('ASCII')
g=rdf_graph_parse.serialize(format='json-ld', indent=4).decode('ASCII')


import pyld as ld

return json.dumps(ld.jsonld.compact(json.loads(g), context),indent=4)

def createDefaultJSONLDcontext(self):
Expand Down Expand Up @@ -548,6 +551,12 @@ def prefix_to_context(self):
#context[key]['@type']='@id'
if type(value.uri) == 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:
continue
else:
context[key]= str(value.uri)

Expand Down
9 changes: 6 additions & 3 deletions nidm/experiment/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def read_nidm(nidmDoc):
rdf_graph = Graph()
rdf_graph_parse = rdf_graph.parse(nidmDoc,format=util.guess_format(nidmDoc))



# add known CDE graphs
#rdf_graph_parse = rdf_graph.parse

Expand Down Expand Up @@ -137,6 +139,7 @@ def read_nidm(nidmDoc):
add_metadata_for_subject (rdf_graph_parse,proj_id,project.graph.namespaces,project)



#Query graph for sessions, instantiate session objects, and add to project._session list
#Get subject URI for sessions
for s in rdf_graph_parse.subjects(predicate=RDF.type,object=URIRef(Constants.NIDM_SESSION.uri)):
Expand All @@ -153,7 +156,6 @@ def read_nidm(nidmDoc):
#add session to project
project.add_sessions(session)


#now get remaining metadata in session object and add to session
#Cycle through Session metadata adding to prov graph
add_metadata_for_subject (rdf_graph_parse,s,project.graph.namespaces,session)
Expand Down Expand Up @@ -325,6 +327,7 @@ def read_nidm(nidmDoc):
add_metadata_for_subject(rdf_graph_parse, row['uuid'], project.graph.namespaces, derivobj)



return(project)


Expand Down Expand Up @@ -390,9 +393,9 @@ def add_metadata_for_subject (rdf_graph,subject_uri,namespaces,nidm_obj):
# automatically by provDocument so they aren't accessible via the namespaces list
# so we check explicitly here
if ((obj_nm == str(Constants.PROV))):
nidm_obj.add_attributes({predicate: pm.QualifiedName(Constants.PROV,obj_term)})
nidm_obj.add_attributes({predicate: QualifiedName(Constants.PROV[obj_term])})
elif ((obj_nm == str(Constants.NIDM))):
nidm_obj.add_attributes({predicate: pm.QualifiedName(Constants.NIDM,obj_term)})
nidm_obj.add_attributes({predicate: QualifiedName(Constants.NIDM[obj_term])})
else:
found_uri = find_in_namespaces(search_uri=URIRef(obj_nm),namespaces=namespaces)
# if obj_nm is not in namespaces then it must just be part of some URI in the triple
Expand Down
70 changes: 67 additions & 3 deletions nidm/experiment/tools/bidsmri2nidm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# **************************************************************************************

import sys, getopt, os

import bids
from nidm.experiment import Project,Session,MRAcquisition,AcquisitionObject,DemographicsObject, AssessmentAcquisition, \
AssessmentObject,MRObject,Acquisition
from nidm.core import BIDS_Constants,Constants
Expand All @@ -44,7 +44,7 @@
import csv
import glob
from argparse import ArgumentParser
from bids import BIDSLayout

# Python program to find SHA256 hash string of a file
import hashlib
from io import StringIO
Expand Down Expand Up @@ -395,6 +395,69 @@ def addimagingsessions(bids_layout,subject_id,session,participant, directory,img
else:
acq_obj.add_attributes({BIDS_Constants.json_keys[key]:dataset[key]})

# DBK added for ASL support 3/16/21
# WIP: Waiting for pybids > 0.12.4 to support perfusion scans
elif file_tpl.entities['datatype'] == 'perf':
acq_obj = MRObject(acq)
# add image contrast type
if file_tpl.entities['suffix'] in BIDS_Constants.scans:
acq_obj.add_attributes(
{Constants.NIDM_IMAGE_CONTRAST_TYPE: BIDS_Constants.scans[file_tpl.entities['suffix']]})
else:
logging.info(
"WARNING: No matching image contrast type found in BIDS_Constants.py for %s" % file_tpl.entities[
'suffix'])
# add image usage type
if file_tpl.entities['datatype'] in BIDS_Constants.scans:
acq_obj.add_attributes({Constants.NIDM_IMAGE_USAGE_TYPE: BIDS_Constants.scans["asl"]})
else:
logging.info(
"WARNING: No matching image usage type found in BIDS_Constants.py for %s" % file_tpl.entities[
'datatype'])
# make relative link to
acq_obj.add_attributes(
{Constants.NIDM_FILENAME: getRelPathToBIDS(join(file_tpl.dirname, file_tpl.filename), directory)})
# add sha512 sum
if isfile(join(directory, file_tpl.dirname, file_tpl.filename)):
acq_obj.add_attributes(
{Constants.CRYPTO_SHA512: getsha512(join(directory, file_tpl.dirname, file_tpl.filename))})
else:
logging.info(
"WARNING file %s doesn't exist! No SHA512 sum stored in NIDM files..." % join(directory,
file_tpl.dirname,
file_tpl.filename))

# add git-annex/datalad info if exists
num_sources = addGitAnnexSources(obj=acq_obj, filepath=join(file_tpl.dirname, file_tpl.filename),
bids_root=directory)

if num_sources == 0:
acq_obj.add_attributes(
{Constants.PROV['Location']: "file:/" + join(file_tpl.dirname, file_tpl.filename)})

if 'run' in file_tpl.entities:
acq_obj.add_attributes({BIDS_Constants.json_keys["run"]: file_tpl.run})

# get associated JSON file if exists
json_data = (bids_layout.get(suffix=file_tpl.entities['suffix'], subject=subject_id))[0].metadata

if len(json_data.info) > 0:
for key in json_data.info.items():
if key in BIDS_Constants.json_keys:
if type(json_data.info[key]) is list:
acq_obj.add_attributes({BIDS_Constants.json_keys[key.replace(" ", "_")]: ''.join(
str(e) for e in json_data.info[key])})
else:
acq_obj.add_attributes(
{BIDS_Constants.json_keys[key.replace(" ", "_")]: json_data.info[key]})

# check if separate M0 scan exists, if so add location and filename
# WIP, waiting for pybids > 0.12.4 to support...

# WIP support B0 maps...waiting for pybids > 0.12.4
# elif file_tpl.entities['datatype'] == 'fmap':


elif file_tpl.entities['datatype'] == 'dwi':
#do stuff with with dwi scans...
acq_obj = MRObject(acq)
Expand Down Expand Up @@ -516,7 +579,8 @@ def bidsmri2project(directory, args):


# get BIDS layout
bids_layout = BIDSLayout(directory)
bids.config.set_option('extension_initial_dot', True)
bids_layout = bids.BIDSLayout(directory)


# create empty dictinary for sessions where key is subject id and used later to link scans to same session as demographics
Expand Down
6 changes: 3 additions & 3 deletions nidm/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Format expected by setup.py and doc/source/conf.py: string of form "X.Y.Z"
_version_major = 3
_version_minor = 7
_version_micro = '4' # use '' for first of series, number for 1 and above
_version_micro = '6' # use '' for first of series, number for 1 and above
_version_extra = ''
# _version_extra = '' # Uncomment this for full releases

Expand Down Expand Up @@ -56,6 +56,6 @@
MICRO = _version_micro
VERSION = __version__
INSTALL_REQUIRES = ["prov", "graphviz", "pydotplus", "pydot", "validators", "requests", "rapidfuzz", "pygithub",
"pandas", "pybids>=0.12.0", "duecredit", "pytest", "graphviz", "click", "rdflib-jsonld",
"pyld", "rdflib", "datalad", "ontquery>=0.2.3", "orthauth>=0.0.12","tabulate", "joblib", "cognitiveatlas", "numpy>=1.16.5", "etelemetry","click-option-group"]
"pandas", "pybids>=0.12.4", "duecredit", "pytest", "graphviz", "click", "rdflib-jsonld",
"pyld", "rdflib", "datalad", "ontquery==0.2.3", "orthauth>=0.0.12","tabulate", "joblib", "cognitiveatlas", "numpy>=1.16.5", "etelemetry","click-option-group"]
SCRIPTS = ["bin/nidm_query", "bin/bidsmri2nidm", "bin/csv2nidm","bin/nidm_utils"]

0 comments on commit 42ec135

Please sign in to comment.