|
a |
|
b/dosma/msk/knee.py |
|
|
1 |
""" |
|
|
2 |
Command line interface declaration for knee-related analyses |
|
|
3 |
|
|
|
4 |
@author: Arjun Desai |
|
|
5 |
(C) Stanford University, 2019 |
|
|
6 |
""" |
|
|
7 |
|
|
|
8 |
import logging |
|
|
9 |
import uuid |
|
|
10 |
|
|
|
11 |
from dosma.core.quant_vals import QuantitativeValueType as QV |
|
|
12 |
from dosma.defaults import preferences |
|
|
13 |
from dosma.tissues import FemoralCartilage, Meniscus, PatellarCartilage, TibialCartilage |
|
|
14 |
|
|
|
15 |
KNEE_KEY = "knee" |
|
|
16 |
MEDIAL_TO_LATERAL_KEY = "ml" |
|
|
17 |
TISSUES_KEY = "tissues" |
|
|
18 |
LOAD_KEY = "load" |
|
|
19 |
SAVE_KEY = "save" |
|
|
20 |
PID_KEY = "pid" |
|
|
21 |
|
|
|
22 |
SUPPORTED_TISSUES = [FemoralCartilage, Meniscus, TibialCartilage, PatellarCartilage] |
|
|
23 |
SUPPORTED_QUANTITATIVE_VALUES = [QV.T2, QV.T1_RHO, QV.T2_STAR] |
|
|
24 |
|
|
|
25 |
_logger = logging.getLogger(__name__) |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
def knee_parser(base_parser): |
|
|
29 |
"""Parse command line input related to knee |
|
|
30 |
|
|
|
31 |
:param base_parser: the base parser to add knee subcommand to |
|
|
32 |
""" |
|
|
33 |
parser_tissue = base_parser.add_parser( |
|
|
34 |
KNEE_KEY, help="calculate/analyze quantitative data for knee" |
|
|
35 |
) |
|
|
36 |
|
|
|
37 |
parser_tissue.add_argument( |
|
|
38 |
"--%s" % MEDIAL_TO_LATERAL_KEY, |
|
|
39 |
action="store_const", |
|
|
40 |
const=True, |
|
|
41 |
default=False, |
|
|
42 |
help="defines slices in sagittal direction going from medial -> lateral", |
|
|
43 |
) |
|
|
44 |
|
|
|
45 |
parser_tissue.add_argument( |
|
|
46 |
"--%s" % PID_KEY, nargs="?", default=str(uuid.uuid4()), help="specify pid" |
|
|
47 |
) |
|
|
48 |
|
|
|
49 |
for tissue in SUPPORTED_TISSUES: |
|
|
50 |
parser_tissue.add_argument( |
|
|
51 |
"--%s" % tissue.STR_ID, |
|
|
52 |
action="store_const", |
|
|
53 |
default=False, |
|
|
54 |
const=True, |
|
|
55 |
help="analyze %s" % tissue.FULL_NAME, |
|
|
56 |
) |
|
|
57 |
|
|
|
58 |
qvs_dict = {} |
|
|
59 |
for qv in SUPPORTED_QUANTITATIVE_VALUES: |
|
|
60 |
qv_name = qv.name.lower() |
|
|
61 |
qvs_dict[qv_name] = qv |
|
|
62 |
parser_tissue.add_argument( |
|
|
63 |
"--%s" % qv_name, |
|
|
64 |
action="store_const", |
|
|
65 |
const=True, |
|
|
66 |
default=False, |
|
|
67 |
help="quantify %s" % qv_name, |
|
|
68 |
) |
|
|
69 |
|
|
|
70 |
parser_tissue.set_defaults(func=handle_knee) |
|
|
71 |
|
|
|
72 |
|
|
|
73 |
def handle_knee(vargin): |
|
|
74 |
"""Handle parsing command-line input for knee subcommand |
|
|
75 |
:param vargin: |
|
|
76 |
:return: |
|
|
77 |
""" |
|
|
78 |
tissues = vargin[TISSUES_KEY] |
|
|
79 |
load_path = vargin[LOAD_KEY] |
|
|
80 |
medial_to_lateral = vargin[MEDIAL_TO_LATERAL_KEY] |
|
|
81 |
pid = vargin[PID_KEY] |
|
|
82 |
|
|
|
83 |
if tissues is None or len(tissues) == 0: |
|
|
84 |
_logger.info("Computing for all supported knee tissues...") |
|
|
85 |
tissues = [] |
|
|
86 |
for t in SUPPORTED_TISSUES: |
|
|
87 |
tissues.append(t()) |
|
|
88 |
|
|
|
89 |
# Get all supported quantitative values |
|
|
90 |
qvs = [] |
|
|
91 |
for qv in SUPPORTED_QUANTITATIVE_VALUES: |
|
|
92 |
if vargin[qv.name.lower()]: |
|
|
93 |
qvs.append(qv) |
|
|
94 |
|
|
|
95 |
if len(qvs) == 0: |
|
|
96 |
_logger.info("Computing for all supported quantitative values...") |
|
|
97 |
qvs = SUPPORTED_QUANTITATIVE_VALUES |
|
|
98 |
|
|
|
99 |
for tissue in tissues: |
|
|
100 |
tissue.pid = pid |
|
|
101 |
tissue.medial_to_lateral = medial_to_lateral |
|
|
102 |
tissue.load_data(load_path) |
|
|
103 |
|
|
|
104 |
_logger.info("") |
|
|
105 |
_logger.info("==" * 40) |
|
|
106 |
_logger.info(tissue.FULL_NAME) |
|
|
107 |
_logger.info("==" * 40) |
|
|
108 |
|
|
|
109 |
for qv in qvs: |
|
|
110 |
# load file |
|
|
111 |
_logger.info("Analyzing %s" % qv.name.lower()) |
|
|
112 |
tissue.calc_quant_vals() |
|
|
113 |
|
|
|
114 |
for tissue in tissues: |
|
|
115 |
tissue.save_data(vargin[SAVE_KEY], data_format=preferences.image_data_format) |
|
|
116 |
|
|
|
117 |
return tissues |