diff --git a/.circleci/config.yml b/.circleci/config.yml index 6f04e80d..09717283 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,7 +12,6 @@ jobs: - run: name: Preinstallation Packages command: | - pip install pyxb pip install wheel pip install pytest - python/install-packages: diff --git a/experimental/zero_filled/run_zero_filled.py b/experimental/zero_filled/run_zero_filled.py index a506f7ff..f9fc3b87 100644 --- a/experimental/zero_filled/run_zero_filled.py +++ b/experimental/zero_filled/run_zero_filled.py @@ -5,25 +5,31 @@ LICENSE file in the root directory of this source tree. """ -import pathlib +import xml.etree.ElementTree as etree from argparse import ArgumentParser +from pathlib import Path import fastmri import h5py -import ismrmrd from fastmri.data import transforms +from fastmri.data.mri_data import et_query +from tqdm import tqdm def save_zero_filled(data_dir, out_dir, which_challenge): reconstructions = {} - for f in data_dir.iterdir(): - with h5py.File(f, "r") as hf: - enc = ismrmrd.xsd.CreateFromDocument(hf["ismrmrd_header"][()]).encoding[0] + for fname in tqdm(list(data_dir.glob("*.h5"))): + with h5py.File(fname, "r") as hf: + et_root = etree.fromstring(hf["ismrmrd_header"][()]) masked_kspace = transforms.to_tensor(hf["kspace"][()]) # extract target image width, height from ismrmrd header - crop_size = (enc.reconSpace.matrixSize.x, enc.reconSpace.matrixSize.y) + enc = ["encoding", "encodedSpace", "matrixSize"] + crop_size = ( + int(et_query(et_root, enc + ["x"])), + int(et_query(et_root, enc + ["y"])), + ) # inverse Fourier Transform to get zero filled solution image = fastmri.ifft2c(masked_kspace) @@ -42,20 +48,20 @@ def save_zero_filled(data_dir, out_dir, which_challenge): if which_challenge == "multicoil": image = fastmri.rss(image, dim=1) - reconstructions[f.name] = image + reconstructions[fname.name] = image fastmri.save_reconstructions(reconstructions, out_dir) def create_arg_parser(): - parser = ArgumentParser(add_help=False) + parser = ArgumentParser() parser.add_argument( - "--data_path", type=pathlib.Path, required=True, help="Path to the data", + "--data_path", type=Path, required=True, help="Path to the data", ) parser.add_argument( "--out_path", - type=pathlib.Path, + type=Path, required=True, help="Path to save the reconstructions to", ) diff --git a/fastmri/data/mri_data.py b/fastmri/data/mri_data.py index be45f7bd..ec3415ff 100644 --- a/fastmri/data/mri_data.py +++ b/fastmri/data/mri_data.py @@ -9,15 +9,41 @@ import pathlib import pickle import random +import xml.etree.ElementTree as etree from warnings import warn import h5py -import ismrmrd import numpy as np import torch import yaml +def et_query(root, qlist, namespace="http://www.ismrm.org/ISMRMRD"): + """ + ElementTree query function. + + This can be used to query an xml document via ElementTree. It uses qlist + for nexted queries. + + Args: + root (xml.etree.ElementTree.Element): Root of the xml. + qlist (Sequence): A list of strings for nested searches. + namespace (str): xml namespace. + + Returns: + str: The retrieved data. + """ + s = "." + prefix = "ismrmrd_namespace" + + ns = {prefix: namespace} + + for el in qlist: + s = s + f"//{prefix}:{el}" + + return root.find(s, ns).text + + def fetch_dir(key, data_config_file=pathlib.Path("fastmri_dirs.yaml")): """ Data directory fetcher. @@ -162,24 +188,25 @@ def __init__( files = list(pathlib.Path(root).iterdir()) for fname in sorted(files): with h5py.File(fname, "r") as hf: - hdr = ismrmrd.xsd.CreateFromDocument(hf["ismrmrd_header"][()]) - enc = hdr.encoding[0] + et_root = etree.fromstring(hf["ismrmrd_header"][()]) + enc = ["encoding", "encodedSpace", "matrixSize"] enc_size = ( - enc.encodedSpace.matrixSize.x, - enc.encodedSpace.matrixSize.y, - enc.encodedSpace.matrixSize.z, + int(et_query(et_root, enc + ["x"])), + int(et_query(et_root, enc + ["y"])), + int(et_query(et_root, enc + ["z"])), ) + rec = ["encoding", "reconSpace", "matrixSize"] recon_size = ( - enc.reconSpace.matrixSize.x, - enc.reconSpace.matrixSize.y, - enc.reconSpace.matrixSize.z, + int(et_query(et_root, rec + ["x"])), + int(et_query(et_root, rec + ["y"])), + int(et_query(et_root, rec + ["z"])), ) - enc_limits_center = enc.encodingLimits.kspace_encoding_step_1.center - enc_limits_max = ( - enc.encodingLimits.kspace_encoding_step_1.maximum + 1 - ) + lims = ["encoding", "encodingLimits", "kspace_encoding_step_1"] + enc_limits_center = int(et_query(et_root, lims + ["center"])) + enc_limits_max = int(et_query(et_root, lims + ["maximum"])) + 1 + padding_left = enc_size[1] // 2 - enc_limits_center padding_right = padding_left + enc_limits_max diff --git a/requirements.txt b/requirements.txt index 58c5ea88..3048d309 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,3 @@ runstats>=1.8.0 pytorch_lightning>=1.0.0 h5py>=2.10.0 PyYAML>=5.3.1 -git+https://github.com/ismrmrd/ismrmrd-python.git diff --git a/setup.py b/setup.py index 9b55098a..e4b49696 100644 --- a/setup.py +++ b/setup.py @@ -16,8 +16,6 @@ "pytorch_lightning", "h5py", "PyYAML", - "pytest", - "ismrmrd @ git+https://github.com/ismrmrd/ismrmrd-python.git", ] setup(