Created
July 23, 2019 16:37
-
-
Save oesteban/d0cdc580b69db690a262b8038d1e6a9a to your computer and use it in GitHub Desktop.
Pull the ratings associated to a given MD5 checksum.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import getenv | |
from pathlib import Path | |
from json import load | |
import urllib.request, json | |
import pandas as pd | |
def get_rating(checksum): | |
""" | |
Grab the latest rating found for a given MD5 checksum. | |
Example | |
------- | |
>>> get_rating('7e7e085a93dac55632cf9289650f6dd3') | |
{'rating': '1', 'comment': ['head-motion', 'inu']} | |
""" | |
url = 'https://mriqc.nimh.nih.gov/api/v1/rating?where={"md5sum":"%s"}' | |
with urllib.request.urlopen(url % checksum) as conn: | |
data = json.loads(conn.read().decode()) | |
try: | |
element = data['_items'][-1] | |
except IndexError: | |
return {'rating': 'n/a', 'comment': 'n/a'} | |
# Compose a pandas dataframe | |
return { | |
'rating': data['_items'][-1]['rating'], | |
'comment': data['_items'][-1]['comment'].replace('"', '')[1:-1].split(',') | |
} | |
def compose_table(path, modality, suffix): | |
"""Colate a dataframe of subject IDs, ratings and comments.""" | |
mriqc_files = path.glob('sub-*/%s/sub-*_%s.json' % (modality, suffix)) | |
qc_data = {'participant_id': [], 'rating': [], 'comment': []} | |
for im in mriqc_files: | |
sub = json.loads(im.read_text()) | |
rating_data = get_rating(sub['provenance']['md5sum']) | |
qc_data['participant_id'].append(sub['bids_meta']['subject_id']) | |
qc_data['rating'].append(rating_data['rating']) | |
qc_data['comment'].append('+'.join(rating_data['comment'])) | |
return pd.DataFrame(qc_data) | |
if __name__ == '__main__': | |
mriqc_dir = Path(getenv('OAK', '/oak/stanford/groups/russpold/data/P1000/derivatives/mriqc-0.15.0')) | |
t1w_df = compose_table(mriqc_dir, 'anat', 'T1w') | |
t1w_df.to_csv('qc_T1w.tsv', sep='\t', index=None) | |
t2w_df = compose_table(mriqc_dir, 'anat', 'T2w') | |
t2w_df.to_csv('qc_T2w.tsv', sep='\t', index=None) | |
bold_df = compose_table(mriqc_dir, 'func', 'bold') | |
bold_df.to_csv('qc_bold.tsv', sep='\t', index=None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment