Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ekcole committed Aug 25, 2020
1 parent e06c259 commit c0033b5
Show file tree
Hide file tree
Showing 27 changed files with 7,379 additions and 1 deletion.
113 changes: 113 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

#checkpoints and tensorboards
events*
checkpoint*
graph*

#data
raw*
tmp*
.zip
*.zip
data
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
# unsupGAN-release
# unsupGAN-release

# Unsupervised MRI Reconstruction

Unsupervised MRI Reconstruction with Generative Adversarial Networks

- 2020 Elizabeth Cole, Stanford University ([email protected])

## Setup

Make sure the python requirements are installed

pip3 install -r requirements.txt

The setup assumes that the latest Berkeley Advanced Reconstruction Toolbox is installed [1]. The scripts have all been tested with v0.4.01.

## Data preparation

We will first download data, generate sampling masks, and generate TFRecords for training. The datasets downloaded are fully sampled volumetric knee scans from mridata [2]. The setup script uses the BART binary. In a new folder, run the follwing script:

python3 mri_util/setup_mri.py -v

## Training/Testing Unsupervised GAN

The training of the unsupervised GAN can be ran using the following script:
python3 train_unsupervised.py dataset_dir model_dir
where dataset_dir is the folder where the knee datasets were saved to,
and model_dir will be the top directory where the models will be saved to.

Testing can be ran using:
python3 test_unsupervised.py dataset_dir model_dir

## Training/Testing Supervised GAN

The training of the supervised GAN can be ran using the following script:
python3 train_supervised.py dataset_dir model_dir
where dataset_dir is the folder where the knee datasets were saved to,
and model_dir will be the top directory where the models will be saved to.

Testing can be ran using:
python3 test_supervised.py dataset_dir model_dir

## Questions/Issues

For any issues or questions, please open an issue on the github repo or contact
Elizabeth at [email protected].
14 changes: 14 additions & 0 deletions create_masks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from mri_util import mri_prep

dir_out = "/home_local/ekcole/knee_masks"
mri_prep.create_masks(
dir_out,
shape_y=320,
shape_z=256,
verbose=True,
acc_y=(1, 2, 3, 4),
acc_z=(1, 2, 3, 4),
shape_calib=1,
variable_density=True,
num_repeat=4,
)
34 changes: 34 additions & 0 deletions masks_train_acc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from mri_util import mri_prep
acc_y = 2
acc_z = 7
total_acc = acc_y*acc_z
dir_out = "/home_local/ekcole/knee_masks_%d" % total_acc
print(dir_out)
mri_prep.create_masks(
dir_out,
shape_y=320,
shape_z=256,
verbose=True,
acc_y=[acc_y],
acc_z=[acc_z],
shape_calib=1,
variable_density=True,
num_repeat=3,
)

acc_y = 7
acc_z = 2
total_acc = acc_y*acc_z
dir_out = "/home_local/ekcole/knee_masks_%d" % total_acc
print(dir_out)
mri_prep.create_masks(
dir_out,
shape_y=320,
shape_z=256,
verbose=True,
acc_y=[acc_y],
acc_z=[acc_z],
shape_calib=1,
variable_density=True,
num_repeat=3,
)
Empty file added mri_util/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions mri_util/cfl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2013-2015. The Regents of the University of California.
# All rights reserved. Use of this source code is governed by
# a BSD-style license which can be found in the LICENSE file.
#
# Authors:
# 2013 Martin Uecker <[email protected]>
# 2015 Jonathan Tamir <[email protected]>

import numpy as np


def read_hdr(name, order="C"):
# get dims from .hdr
h = open(name + ".hdr", "r")
h.readline() # skip
l = h.readline()
h.close()
dims = [int(i) for i in l.split()]
if order == "C":
dims.reverse()
return dims


def read(name, order="C"):

dims = read_hdr(name, order)

# remove singleton dimensions from the end
n = np.prod(dims)
dims_prod = np.cumprod(dims)
dims = dims[: np.searchsorted(dims_prod, n) + 1]

# load data and reshape into dims
d = open(name + ".cfl", "r")
a = np.fromfile(d, dtype=np.complex64, count=n)
d.close()
return a.reshape(dims, order=order) # column-major


def readcfl(name):
return read(name, order="F")


def write(name, array, order="C"):
h = open(name + ".hdr", "w")
h.write("# Dimensions\n")
if order == "C":
for i in array.shape[::-1]:
h.write("%d " % i)
else:
for i in array.shape:
h.write("%d " % i)
h.write("\n")
h.close()

d = open(name + ".cfl", "w")
if order == "C":
array.astype(np.complex64).tofile(d)
else:
# tranpose for column-major order
array.T.astype(np.complex64).tofile(d)
d.close()


def writecfl(name, array):
write(name, array, order="F")
Loading

0 comments on commit c0033b5

Please sign in to comment.