HTML Param Groups
[1]:
import sys
sys.path.append("..")
[10]:
# USE THIS BEFORE TESTING!
from pathlib import Path
import shutil
import os
#import cubids
from bids.layout import parse_file_entities
from cubids import CuBIDS
from pkg_resources import resource_filename as pkgrf
# returns string path to testdata
TEST_DATA = pkgrf("cubids", "testdata")
# should give you the full path
tmp_path = Path().resolve()
#print(tmp_path)
# dest path
data_root = tmp_path / "testdata"
# ensure path does not already exist in cwd
if data_root.exists():
shutil.rmtree(str(data_root))
# equivalent of command line "cp"
cwd = shutil.copytree(TEST_DATA, str(data_root))
cwd
[10]:
'/Users/Covitz/CuBIDS/notebooks/testdata'
[3]:
! find testdata
testdata
testdata/complete
testdata/complete/sub-01
testdata/complete/sub-01/ses-phdiff
testdata/complete/sub-01/ses-phdiff/dwi
testdata/complete/sub-01/ses-phdiff/dwi/sub-01_ses-phdiff_acq-HASC55AP_dwi.bval
testdata/complete/sub-01/ses-phdiff/dwi/sub-01_ses-phdiff_acq-HASC55AP_dwi.bvec
testdata/complete/sub-01/ses-phdiff/dwi/sub-01_ses-phdiff_acq-HASC55AP_dwi.nii.gz
testdata/complete/sub-01/ses-phdiff/dwi/sub-01_ses-phdiff_acq-HASC55AP_dwi.json
testdata/complete/sub-01/ses-phdiff/fmap
testdata/complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_phasediff.json
testdata/complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude2.json
testdata/complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude1.nii.gz
testdata/complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_phasediff.nii.gz
testdata/complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_dir-PA_epi.json
testdata/complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude2.nii.gz
testdata/complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_dir-PA_epi.nii.gz
testdata/complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude1.json
testdata/complete/sub-01/ses-phdiff/anat
testdata/complete/sub-01/ses-phdiff/anat/sub-01_ses-phdiff_T1w.json
testdata/complete/sub-01/ses-phdiff/anat/sub-01_ses-phdiff_T1w.nii.gz
testdata/complete/sub-01/ses-phdiff/func
testdata/complete/sub-01/ses-phdiff/func/sub-01_ses-phdiff_task-rest_bold.nii.gz
testdata/complete/sub-01/ses-phdiff/func/sub-01_ses-phdiff_task-rest_bold.json
testdata/complete/README
testdata/complete/dataset_description.json
testdata/complete/sub-02
testdata/complete/sub-02/ses-phdiff
testdata/complete/sub-02/ses-phdiff/dwi
testdata/complete/sub-02/ses-phdiff/dwi/sub-02_ses-phdiff_acq-HASC55AP_dwi.bvec
testdata/complete/sub-02/ses-phdiff/dwi/sub-02_ses-phdiff_acq-HASC55AP_dwi.nii.gz
testdata/complete/sub-02/ses-phdiff/dwi/sub-02_ses-phdiff_acq-HASC55AP_dwi.bval
testdata/complete/sub-02/ses-phdiff/dwi/sub-02_ses-phdiff_acq-HASC55AP_dwi.json
testdata/complete/sub-02/ses-phdiff/fmap
testdata/complete/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude1.nii.gz
testdata/complete/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_phasediff.json
testdata/complete/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude2.json
testdata/complete/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_dir-PA_epi.nii.gz
testdata/complete/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude2.nii.gz
testdata/complete/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude1.json
testdata/complete/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_phasediff.nii.gz
testdata/complete/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_dir-PA_epi.json
testdata/complete/sub-02/ses-phdiff/anat
testdata/complete/sub-02/ses-phdiff/anat/sub-02_ses-phdiff_T1w.nii.gz
testdata/complete/sub-02/ses-phdiff/anat/sub-02_ses-phdiff_T1w.json
testdata/complete/sub-02/ses-phdiff/func
testdata/complete/sub-02/ses-phdiff/func/sub-02_ses-phdiff_task-rest_bold.nii.gz
testdata/complete/sub-02/ses-phdiff/func/sub-02_ses-phdiff_task-rest_bold.json
testdata/complete/sub-03
testdata/complete/sub-03/ses-phdiff
testdata/complete/sub-03/ses-phdiff/dwi
testdata/complete/sub-03/ses-phdiff/dwi/sub-03_ses-phdiff_acq-HASC55AP_dwi.nii.gz
testdata/complete/sub-03/ses-phdiff/dwi/sub-03_ses-phdiff_acq-HASC55AP_dwi.json
testdata/complete/sub-03/ses-phdiff/dwi/sub-03_ses-phdiff_acq-HASC55AP_dwi.bvec
testdata/complete/sub-03/ses-phdiff/dwi/sub-03_ses-phdiff_acq-HASC55AP_dwi.bval
testdata/complete/sub-03/ses-phdiff/fmap
testdata/complete/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_magnitude2.nii.gz
testdata/complete/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_magnitude2.json
testdata/complete/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_dir-PA_epi.nii.gz
testdata/complete/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_magnitude1.nii.gz
testdata/complete/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_dir-PA_epi.json
testdata/complete/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_magnitude1.json
testdata/complete/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_phasediff.json
testdata/complete/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_phasediff.nii.gz
testdata/complete/sub-03/ses-phdiff/anat
testdata/complete/sub-03/ses-phdiff/anat/sub-03_ses-phdiff_T1w.json
testdata/complete/sub-03/ses-phdiff/anat/sub-03_ses-phdiff_T1w.nii.gz
testdata/complete/sub-03/ses-phdiff/func
testdata/complete/sub-03/ses-phdiff/func/sub-03_ses-phdiff_task-rest_bold.json
testdata/complete/sub-03/ses-phdiff/func/sub-03_ses-phdiff_task-rest_bold.nii.gz
testdata/inconsistent
testdata/inconsistent/sub-01
testdata/inconsistent/sub-01/ses-phdiff
testdata/inconsistent/sub-01/ses-phdiff/dwi
testdata/inconsistent/sub-01/ses-phdiff/dwi/sub-01_ses-phdiff_acq-HASC55AP_dwi.bval
testdata/inconsistent/sub-01/ses-phdiff/dwi/sub-01_ses-phdiff_acq-HASC55AP_dwi.bvec
testdata/inconsistent/sub-01/ses-phdiff/dwi/sub-01_ses-phdiff_acq-HASC55AP_dwi.nii.gz
testdata/inconsistent/sub-01/ses-phdiff/dwi/sub-01_ses-phdiff_acq-HASC55AP_dwi.json
testdata/inconsistent/sub-01/ses-phdiff/fmap
testdata/inconsistent/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_phasediff.json
testdata/inconsistent/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude2.json
testdata/inconsistent/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude1.nii.gz
testdata/inconsistent/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_phasediff.nii.gz
testdata/inconsistent/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_dir-PA_epi.json
testdata/inconsistent/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude2.nii.gz
testdata/inconsistent/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_dir-PA_epi.nii.gz
testdata/inconsistent/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude1.json
testdata/inconsistent/sub-01/ses-phdiff/anat
testdata/inconsistent/sub-01/ses-phdiff/anat/sub-01_ses-phdiff_T1w.json
testdata/inconsistent/sub-01/ses-phdiff/anat/sub-01_ses-phdiff_T1w.nii.gz
testdata/inconsistent/sub-01/ses-phdiff/func
testdata/inconsistent/sub-01/ses-phdiff/func/sub-01_ses-phdiff_task-rest_bold.nii.gz
testdata/inconsistent/sub-01/ses-phdiff/func/sub-01_ses-phdiff_task-rest_bold.json
testdata/inconsistent/README
testdata/inconsistent/dataset_description.json
testdata/inconsistent/sub-02
testdata/inconsistent/sub-02/ses-phdiff
testdata/inconsistent/sub-02/ses-phdiff/dwi
testdata/inconsistent/sub-02/ses-phdiff/dwi/sub-02_ses-phdiff_acq-HASC55AP_dwi.bvec
testdata/inconsistent/sub-02/ses-phdiff/dwi/sub-02_ses-phdiff_acq-HASC55AP_dwi.nii.gz
testdata/inconsistent/sub-02/ses-phdiff/dwi/sub-02_ses-phdiff_acq-HASC55AP_dwi.bval
testdata/inconsistent/sub-02/ses-phdiff/dwi/sub-02_ses-phdiff_acq-HASC55AP_dwi.json
testdata/inconsistent/sub-02/ses-phdiff/fmap
testdata/inconsistent/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude1.nii.gz
testdata/inconsistent/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_phasediff.json
testdata/inconsistent/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude2.json
testdata/inconsistent/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_dir-PA_epi.nii.gz
testdata/inconsistent/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude2.nii.gz
testdata/inconsistent/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude1.json
testdata/inconsistent/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_phasediff.nii.gz
testdata/inconsistent/sub-02/ses-phdiff/fmap/sub-02_ses-phdiff_dir-PA_epi.json
testdata/inconsistent/sub-02/ses-phdiff/anat
testdata/inconsistent/sub-02/ses-phdiff/anat/sub-02_ses-phdiff_T1w.nii.gz
testdata/inconsistent/sub-02/ses-phdiff/anat/sub-02_ses-phdiff_T1w.json
testdata/inconsistent/sub-02/ses-phdiff/func
testdata/inconsistent/sub-02/ses-phdiff/func/sub-02_ses-phdiff_task-rest_bold.nii.gz
testdata/inconsistent/sub-02/ses-phdiff/func/sub-02_ses-phdiff_task-rest_bold.json
testdata/inconsistent/sub-03
testdata/inconsistent/sub-03/ses-phdiff
testdata/inconsistent/sub-03/ses-phdiff/dwi
testdata/inconsistent/sub-03/ses-phdiff/dwi/sub-03_ses-phdiff_acq-HASC55AP_dwi.nii.gz
testdata/inconsistent/sub-03/ses-phdiff/dwi/sub-03_ses-phdiff_acq-HASC55AP_dwi.json
testdata/inconsistent/sub-03/ses-phdiff/dwi/sub-03_ses-phdiff_acq-HASC55AP_dwi.bvec
testdata/inconsistent/sub-03/ses-phdiff/dwi/sub-03_ses-phdiff_acq-HASC55AP_dwi.bval
testdata/inconsistent/sub-03/ses-phdiff/fmap
testdata/inconsistent/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_magnitude2.nii.gz
testdata/inconsistent/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_magnitude2.json
testdata/inconsistent/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_dir-PA_epi.nii.gz
testdata/inconsistent/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_magnitude1.nii.gz
testdata/inconsistent/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_dir-PA_epi.json
testdata/inconsistent/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_magnitude1.json
testdata/inconsistent/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_phasediff.json
testdata/inconsistent/sub-03/ses-phdiff/fmap/sub-03_ses-phdiff_acq-v4_phasediff.nii.gz
testdata/inconsistent/sub-03/ses-phdiff/anat
testdata/inconsistent/sub-03/ses-phdiff/anat/sub-03_ses-phdiff_T1w.json
testdata/inconsistent/sub-03/ses-phdiff/anat/sub-03_ses-phdiff_T1w.nii.gz
testdata/inconsistent/sub-03/ses-phdiff/func
testdata/inconsistent/sub-03/ses-phdiff/func/sub-03_ses-phdiff_task-rest_bold.json
testdata/inconsistent/sub-03/ses-phdiff/func/sub-03_ses-phdiff_task-rest_bold.nii.gz
[29]:
# TEST BED FOR HTML THING
# ISSUE! Grouping by char!
#param_group = cubids_obj.get_param_groups(key_group)
# print(key_group)
# #print(param_group)
NON_KEY_ENTITIES = set(["subject", "session", "extension"])
def key_group_to_entities(key_group):
return dict([group.split("-") for group in key_group.split("_")])
def entities_to_key_group(entities):
group_keys = sorted(entities.keys())
return "_".join(
["{}-{}".format(key, entities[key]) for key in group_keys])
def file_to_entities(filename):
entities = parse_file_entities(str(filename))
return entities
def file_to_key_group(filename):
entities = parse_file_entities(str(filename))
keys = entities_to_key_group(entities)
return keys
def key_group_to_entities(key_group):
return dict([group.split("-") for group in key_group.split("_")])
def get_file_params(files):
"""Finds a list of *parameter groups* from a list of files.
Parameters:
-----------
files : list
List of file names
Returns:
--------
files_params : dictionary
A dictionary of filename, param_dict pairs
For each file in `files`, find critical parameters for metadata. Then find
unique sets of these critical parameters.
"""
# # DICTIONARY OF FILENAME, DICT_PARAMS
files_params = {}
for path in files:
metadata = self.layout.get_metadata(path)
wanted_keys = metadata.keys() & IMAGING_PARAMS
example_data = {key: metadata[key] for key in wanted_keys}
# Expand slice timing to multiple columns
SliceTime = example_data.get('SliceTiming')
if SliceTime:
# round each slice time to one place after the decimal
for i in range(len(SliceTime)):
SliceTime[i] = round(SliceTime[i], 1)
example_data.update(
{"SliceTime%03d" % SliceNum: time for
SliceNum, time in enumerate(SliceTime)})
del example_data['SliceTiming']
# ADD TO THE DICTIONARY
files_params[path] = example_data
return files_params
#def get_param_groups(key_group, path):
# key_entities = key_group_to_entities(key_group)
# path = Path(path)
# matching_files = path.layout.get(return_type="file", scope="self",
# **key_entities)
# return _get_param_groups(matching_files)
filename = "/Users/Covitz/CuBIDS/cubids/testdata/complete/sub-01/ses-phdiff/fmap/sub-02_ses-phdiff_acq-v4_magnitude1.json"
path = "/Users/Covitz/CuBIDS/cubids/testdata/complete/"
ret_entities = file_to_entities(filename)
print(ret_entities)
key_group = entities_to_key_group(ret_entities)
print(key_group)
entities = key_group_to_entities(key_group)
print("Entities: ", entities)
key_groups = file_to_key_group(filename)
print("F_2_K: ", key_groups)
{'a': [0, 2], 'b': [1, 3], 'c': [4], 'd': [5]}
{'subject': '01', 'session': 'phdiff', 'acquisition': 'v4', 'suffix': 'magnitude1', 'datatype': 'fmap', 'extension': '.json'}
acquisition-v4_datatype-fmap_extension-.json_session-phdiff_subject-01_suffix-magnitude1
Entities: {'acquisition': 'v4', 'datatype': 'fmap', 'extension': '.json', 'session': 'phdiff', 'subject': '01', 'suffix': 'magnitude1'}
F_2_K: acquisition-v4_datatype-fmap_extension-.json_session-phdiff_subject-01_suffix-magnitude1
[16]:
# IMPORT SET TRACE
# assert 0, debug
cubids_obj = CuBIDS("/Users/Covitz/CuBIDS/cubids/testdata/complete/")
key_group = cubids_obj.get_key_groups()
key_group
[16]:
['acquisition-HASC55AP_datatype-dwi_suffix-dwi',
'acquisition-v4_datatype-fmap_fmap-magnitude1_suffix-magnitude1',
'acquisition-v4_datatype-fmap_fmap-magnitude2_suffix-magnitude2',
'acquisition-v4_datatype-fmap_fmap-phasediff_suffix-phasediff',
'datatype-anat_suffix-T1w',
'datatype-fmap_direction-PA_fmap-epi_suffix-epi',
'datatype-func_suffix-bold_task-rest']
[24]:
Requirement already satisfied: pybids in /Applications/miniconda3/envs/cubids/lib/python3.8/site-packages (0.12.1)
Requirement already satisfied: pandas>=0.23 in /Applications/miniconda3/envs/cubids/lib/python3.8/site-packages (from pybids) (1.1.3)
Requirement already satisfied: scipy in /Users/Covitz/.local/lib/python3.8/site-packages (from pybids) (1.5.2)
Requirement already satisfied: sqlalchemy in /Applications/miniconda3/envs/cubids/lib/python3.8/site-packages (from pybids) (1.3.19)
Requirement already satisfied: bids-validator in /Applications/miniconda3/envs/cubids/lib/python3.8/site-packages (from pybids) (1.5.6)
Requirement already satisfied: patsy in /Applications/miniconda3/envs/cubids/lib/python3.8/site-packages (from pybids) (0.5.1)
Requirement already satisfied: numpy in /Users/Covitz/.local/lib/python3.8/site-packages (from pybids) (1.19.2)
Requirement already satisfied: num2words in /Applications/miniconda3/envs/cubids/lib/python3.8/site-packages (from pybids) (0.5.10)
Requirement already satisfied: nibabel>=2.1 in /Users/Covitz/.local/lib/python3.8/site-packages (from pybids) (3.1.1)
Requirement already satisfied: click in /Users/Covitz/.local/lib/python3.8/site-packages (from pybids) (7.1.2)
Requirement already satisfied: pytz>=2017.2 in /Applications/miniconda3/envs/cubids/lib/python3.8/site-packages (from pandas>=0.23->pybids) (2020.1)
Requirement already satisfied: python-dateutil>=2.7.3 in /Users/Covitz/.local/lib/python3.8/site-packages (from pandas>=0.23->pybids) (2.8.1)
Requirement already satisfied: six in /Users/Covitz/.local/lib/python3.8/site-packages (from patsy->pybids) (1.15.0)
Requirement already satisfied: docopt>=0.6.2 in /Applications/miniconda3/envs/cubids/lib/python3.8/site-packages (from num2words->pybids) (0.6.2)
Requirement already satisfied: packaging>=14.3 in /Users/Covitz/.local/lib/python3.8/site-packages (from nibabel>=2.1->pybids) (20.4)
Requirement already satisfied: pyparsing>=2.0.2 in /Users/Covitz/.local/lib/python3.8/site-packages (from packaging>=14.3->nibabel>=2.1->pybids) (2.4.7)
Note: you may need to restart the kernel to use updated packages.
[66]:
import pathlib
# @Params
# - path: a string containing the path to the bids directory inside which we want to change files
# @Returns
# - HTML report of acquisitions and their parameter groups
# WHERE DO WE FIND THE ACQUISITION TYPE?
def _entities_to_key_group(entities):
group_keys = sorted(entities.keys() - NON_KEY_ENTITIES)
return "_".join(
["{}-{}".format(key, entities[key]) for key in group_keys])
def _file_to_key_group(filename):
entities = parse_file_entities(str(filename))
return _entities_to_key_group(entities)
def html_groups(bids_dir):
# get key groups using cubids.get_key_groups
# use key_group_to_entities to get entities
# get param groups for each entity
# initialize dictionary of acquisition types
d_acts = {}
for path in pathlib.Path(path_to_dir).iterdir():
if path.is_file():
ext = path.suffix
# check if the file is a .json file
if ext == ".json":
# parse keys
d_keys = parse_file_entities(path)
# create html file
[ ]: