-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrd_distribution_measures.py
202 lines (165 loc) · 8.32 KB
/
rd_distribution_measures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script to plot distribution of data included in the input CSV.
CSV file must be the output of rdt_prepare_csv*.py
with --split_by_method option if you're not filter your dataframe.
By default, MRI measurement ranges are defined in the parameter file.
Use the --apply_factor option if you have applied a factor to individual
measurements, to adapt the ranges from the parameter file.
rdt_generate_mean_measures_across_bundles_plot.py dti.csv
"""
import argparse
from scilpy.io.utils import add_overwrite_arg, assert_inputs_exist
from dataframe.parameters import scaling_metrics
from dataframe.func import split_df_by
from dataframe.utils import load_df
from plots.parameters import (average_parameters_dict, order_plot_dict,
bundle_dict_color_v1, bundle_dict_color_v10)
from plots.utils import (check_df_for_columns, check_agreement_with_dict,
save_figures_as)
from plots.scatter import interactive_distribution_scatter
def _build_arg_parser():
p = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
description=__doc__)
p.add_argument('in_csv',
help='CSV MRI data.')
p.add_argument('--out_name', default='_measurement_distribution',
help='Output filename to save plot.')
p.add_argument('--out_dir',
help='Output directory for the labeled mask.')
p.add_argument('--rbx_version', choices={'v1', 'v10'},
help='Rbx flow version to segment bundles.'
'[%(default)s]')
p.add_argument('--use_stats',
help='Use to select a specific statistic. '
'[%(default)s]')
p.add_argument('--specific_method',
help='String. Use to select a specific method. '
'Could be DTI, FW, NODDI, etc.')
p.add_argument('--split_by',
help='Column name. Use to plot distribution on each unique'
' argument in slected column. ')
p.add_argument('--filter_missing', action='store_true',
help='Use to filter missing metrics when you reorder.')
scatter = p.add_argument_group(title='Scatter plot options')
scatter.add_argument('--plot_size', nargs=2, type=int,
metavar=('p_width', 'p_height'),
default=(950, 700),
help='Width and Height of Scatter Plot. ')
scatter.add_argument('--custom_order',
help='Use dictionary provided to set order of '
'metrics plot.')
scatter.add_argument('--custom_y',
help='Use dictionary provided to set x and y axis '
'range by measures.')
scatter.add_argument('--use_data', action='store_true',
help='Use data to set x and y axis range.')
scatter.add_argument('--custom_colors',
help='Dictionary containing the bundle names and '
'colors associated in HEX format.')
scatter.add_argument('--apply_factor', type=int,
help='Factor applied on MRI measure for plot. '
' [%(default)s].')
scatter.add_argument('--print_yaxis_range', action='store_true',
help='Use to check/update the y axis range. ')
scatter.add_argument('--save_as_png', action='store_true',
help='Save plot as png. Require kaleido.')
scatter.add_argument('--dpi_scale', type=int, default=6,
help='Use to increase (>1) or decrease (<1) the '
' image resolution. [%(default)s]')
add_overwrite_arg(p)
return p
def main():
parser = _build_arg_parser()
args = parser.parse_args()
assert_inputs_exist(parser, args.in_csv)
if args.out_dir is None:
args.out_dir = './'
# Load and filter Dataframe
df = load_df(args.in_csv)
if args.use_stats:
df = df.loc[df.Statistics == args.use_stats].reset_index(drop=True)
if args.rbx_version:
df = df[(df.rbx_version == args.rbx_version)].reset_index(drop=True)
if args.specific_method:
df = df[df['Method'] == args.specific_method].reset_index(drop=True)
if args.custom_colors is not None:
bundle_colors = args.custom_colors
elif args.rbx_version == 'v10':
bundle_colors = bundle_dict_color_v10
else:
bundle_colors = bundle_dict_color_v1
# check Dataframe shape before plot
check_df_for_columns(df, split_filter=args.split_by)
df = check_agreement_with_dict(df, 'Bundles', bundle_colors,
ignore_lenght=True,
rm_missing=args.filter_missing)
df = check_agreement_with_dict(df, 'Method', order_plot_dict,
rm_missing=args.filter_missing,
ignore_lenght=True)
if args.split_by:
multi_df, df_names = split_df_by(df, args.split_by)
for frame, curr_method in zip(multi_df, df_names):
curr_title = "Distribution of " + curr_method + " measurements"
if args.custom_order and args.custom_y is not None:
custom_order = args.custom_order[curr_method]
custom_yaxis = args.custom_y
elif args.use_data:
custom_order = frame['Measures'].unique().tolist()
custom_yaxis = False
else:
custom_order = order_plot_dict[curr_method]
custom_yaxis = average_parameters_dict
if args.apply_factor:
for metric in custom_order:
if metric in scaling_metrics:
custom_yaxis[metric][1] *= args.apply_factor
col_wrap = 0
if len(frame['Measures'].unique()) > 2:
col_wrap = len(frame['Measures'].unique().tolist()) / 2
fig = interactive_distribution_scatter(
frame, "Bundles", "Value", "Bundles", colormap=bundle_colors,
f_column="Measures", column_wrap=int(col_wrap),
custom_order={"Measures": custom_order}, figtitle=curr_title,
fig_width=args.plot_size[0], fig_height=args.plot_size[1],
print_yaxis_range=args.print_yaxis_range,
custom_y_range=custom_yaxis)
outname = curr_method + args.out_name
save_figures_as(fig, args.out_dir, outname,
save_as_png=args.save_as_png,
dpi_scale=args.dpi_scale,
heigth_value=args.plot_size[1],
width_value=args.plot_size[0])
else:
single_method = df['Method'].unique().tolist()[0]
curr_title = "Distribution of " + single_method + " measurements"
if args.custom_order and args.custom_y is not None:
custom_order = args.custom_order
custom_yaxis = args.custom_y
elif args.use_data:
custom_order = df['Measures'].unique().tolist()
custom_yaxis = False
else:
custom_order = order_plot_dict[single_method]
custom_yaxis = average_parameters_dict
if args.apply_factor:
for metric in custom_order:
if metric in scaling_metrics:
custom_yaxis[metric][1] *= args.apply_factor
col_wrap = 0
if len(df['Measures'].unique()) > 2:
col_wrap = len(df['Measures'].unique().tolist()) / 2
fig = interactive_distribution_scatter(
df, "Bundles", "Value", "Bundles", colormap=bundle_colors,
f_column="Measures", column_wrap=int(col_wrap),
custom_order={"Measures": custom_order}, figtitle=curr_title,
fig_width=args.plot_size[0], fig_height=args.plot_size[1],
print_yaxis_range=args.print_yaxis_range, custom_y_range=custom_yaxis)
outname = single_method + args.out_name
save_figures_as(fig, args.out_dir, outname,
save_as_png=args.save_as_png, dpi_scale=args.dpi_scale,
heigth_value=args.plot_size[1],
width_value=args.plot_size[0], play=args.autoplay)
if __name__ == '__main__':
main()