Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ismrmrd dependency #90

Merged
merged 3 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
- run:
name: Preinstallation Packages
command: |
pip install pyxb
pip install wheel
pip install pytest
- python/install-packages:
Expand Down
26 changes: 16 additions & 10 deletions experimental/zero_filled/run_zero_filled.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
)
Expand Down
53 changes: 40 additions & 13 deletions fastmri/data/mri_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"pytorch_lightning",
"h5py",
"PyYAML",
"pytest",
"ismrmrd @ git+https://github.com/ismrmrd/ismrmrd-python.git",
]

setup(
Expand Down