Skip to content

Commit

Permalink
add cones support
Browse files Browse the repository at this point in the history
  • Loading branch information
ad12 committed Sep 30, 2018
1 parent 48d7d4e commit 167b2b8
Show file tree
Hide file tree
Showing 11 changed files with 500 additions and 122 deletions.
8 changes: 7 additions & 1 deletion file_constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

DEBUG = 1
DEBUG = 0

# Elastix files

Expand All @@ -16,3 +16,9 @@
TEMP_FOLDER_PATH = os.path.join(__DIR__, 'temp')

# TODO: nipype logging
NIPYPE_LOGGING = 'none'

def set_debug():
global DEBUG, NIPYPE_LOGGING
DEBUG=1
NIPYPE_LOGGING = 'stream'
9 changes: 7 additions & 2 deletions msk/knee.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ def handle_knee(vargin):
tissue.ORIENTATION = orientation
tissue.load_data(load_path)

print('')
print('=='*40)
print(tissue.FULL_NAME)
print('==' * 40)

for qv in qvs:
# load file
print('Analyzing %s' % qv.name.lower())
filepath = find_filepath_with_qv(load_path, qv)
tmp = io_utils.load_h5(filepath)
qv_map = tmp['data']
print(qv.name)
tissue.calc_quant_vals(qv_map, qv)

for tissue in tissues:
Expand All @@ -69,7 +74,7 @@ def find_filepath_with_qv(load_path, qv):
if len(dirlist) == 0:
raise ValueError('No map for %s found. Must have name %s.h5' % (name, name))

if (len(dirlist) > 1):
if len(dirlist) > 1:
raise ValueError('Multiple %s maps found. Delete extra %s maps' % (name, name))

return dirlist[0]
82 changes: 56 additions & 26 deletions pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from models.get_model import SUPPORTED_MODELS
from scan_sequences.dess import Dess
from scan_sequences.cube_quant import CubeQuant
from scan_sequences.cones import Cones
from models.get_model import get_model
from tissues.femoral_cartilage import FemoralCartilage

from utils.quant_vals import QuantitativeValue as QV, get_qv
from file_constants import DEBUG
import file_constants as fc

from msk import knee

Expand All @@ -20,6 +21,7 @@
SUPPORTED_TISSUES = [FemoralCartilage()]
SUPPORTED_QUANTITATIVE_VALUES = [QV.T2, QV.T1_RHO, QV.T2_STAR]

DEBUG_KEY = 'debug'

DICOM_KEY = 'dicom'
MASK_KEY = 'mask'
Expand Down Expand Up @@ -115,27 +117,16 @@ def handle_segmentation(vargin, scan):
scan.segment(model, tissue)


def handle_t2_analysis(scan):
scan.generate_t2_map()


def handle_t1_rho_analysis(scan, load_dir):
if not load_dir:
raise ValueError('Must provide %s for directory to masks' % LOAD_KEY)

print('\nCalculating T1_rho')

scan.generate_t1_rho_map()


def handle_dess(vargin):
print('\nAnalyze DESS')
scan = Dess(dicom_path=vargin[DICOM_KEY], dicom_ext=vargin[EXT_KEY], load_path=vargin[LOAD_KEY])
scan.use_rms = vargin[USE_RMS_KEY]
if vargin[ACTION_KEY] is not None and vargin[ACTION_KEY] == 'segment':
handle_segmentation(vargin, scan)

if vargin[T2_KEY]:
handle_t2_analysis(scan)
print('\nCalculating T2')
scan.generate_t2_map()

scan.save_data(vargin[SAVE_KEY])

Expand All @@ -146,6 +137,7 @@ def handle_dess(vargin):


def handle_cubequant(vargin):
print('\nAnalyze cubequant')
scan = CubeQuant(dicom_path=vargin[DICOM_KEY],
dicom_ext=vargin[EXT_KEY],
load_path=vargin[LOAD_KEY])
Expand All @@ -156,22 +148,49 @@ def handle_cubequant(vargin):
scan.tissues = vargin['tissues']

if vargin[ACTION_KEY] is not None and vargin[ACTION_KEY] == 'interregister':
target_scan = vargin[TARGET_SCAN_KEY]
scan.interregister(target_scan[0], vargin[TARGET_MASK_KEY])
target_scan = vargin[TARGET_SCAN_KEY][0]
if not os.path.isfile(target_scan):
raise FileNotFoundError('%s is not a file' % target_scan)

scan.interregister(target_scan, vargin[TARGET_MASK_KEY])

scan.save_data(vargin[SAVE_KEY])

load_filepath = vargin[LOAD_KEY] if vargin[LOAD_KEY] else vargin[SAVE_KEY]
if vargin[T1_RHO_Key]:
handle_t1_rho_analysis(scan, load_filepath)
print('\nCalculating T1_rho')
scan.generate_t1_rho_map()

scan.save_data(vargin[SAVE_KEY])

return scan


def handle_cones(vargin):
pass
print('\nAnalyze cones')
scan = Cones(dicom_path=vargin[DICOM_KEY],
dicom_ext=vargin[EXT_KEY],
load_path=vargin[LOAD_KEY])

if vargin[FOCUSED_MASK_KEY]:
scan.focused_mask_filepath = vargin[FOCUSED_MASK_KEY]

scan.tissues = vargin['tissues']

if vargin[ACTION_KEY] is not None and vargin[ACTION_KEY] == 'interregister':
target_scan = vargin[TARGET_SCAN_KEY]
scan.interregister(target_scan[0], vargin[TARGET_MASK_KEY])

scan.save_data(vargin[SAVE_KEY])

load_filepath = vargin[LOAD_KEY] if vargin[LOAD_KEY] else vargin[SAVE_KEY]
if vargin[T2_STAR_KEY]:
print('Calculating T2_star')
scan.generate_t2_star_map()

scan.save_data(vargin[SAVE_KEY])

return scan


def parse_args():
Expand All @@ -182,6 +201,7 @@ def parse_args():
"""
parser = argparse.ArgumentParser(prog='pipeline',
description='Pipeline for segmenting MRI knee volumes')
parser.add_argument('--%s' % DEBUG_KEY, action='store_const', const=True, default=False, help='debug')

# Dicom and results paths
parser.add_argument('-d', '--%s' % DICOM_KEY, metavar='D', type=str, default=None, nargs='?',
Expand Down Expand Up @@ -226,16 +246,32 @@ def parse_args():
add_interregister_subparser(subparsers_cubequant)
parser_cubequant.set_defaults(func=handle_cubequant)

# Cones parser
parser_cones = subparsers.add_parser(CONES_KEY, help='analyze cones sequence')
parser_cones.add_argument('-%s' % T2_STAR_KEY, action='store_const', default=False, const=True,
help='do t2* analysis')
parser_cones.add_argument('-%s' % FOCUSED_MASK_KEY,
nargs='?',
default=None,
help='focused mask to speed up t1rho calculation')

subparsers_cones = parser_cones.add_subparsers(help='sub-command help', dest=ACTION_KEY)
add_interregister_subparser(subparsers_cones)
parser_cones.set_defaults(func=handle_cones)

# MSK knee parser
knee.knee_parser(subparsers)

start_time = time.time()
args = parser.parse_args()
vargin = vars(args)

if vargin[DEBUG_KEY]:
fc.set_debug()

gpu = vargin[GPU_KEY]

if DEBUG:
if fc.DEBUG:
print(vargin)
# Only supporting femoral cartilage for now

Expand Down Expand Up @@ -264,12 +300,6 @@ def parse_args():

print('Time Elapsed: %0.2f seconds' % (time.time() - start_time))

#
# # Cones parser
# parser_cubequant = subparsers.add_parser(CONES_KEY, help='analyze cones sequence')
# parser_cubequant.add_argument('-%s' % T2_STAR_KEY, action='store_const', default=False, const=True,
# help='do t2* analysis')


if __name__ == '__main__':
parse_args()
Loading

0 comments on commit 167b2b8

Please sign in to comment.