77import os
88from collections import OrderedDict
99
10+ import matplotlib
1011import matplotlib .pyplot as plt
1112import numpy as np
13+ import seaborn as sns
14+ matplotlib .use ('Agg' )
1215
1316np .seterr (divide = 'ignore' , invalid = 'ignore' )
1417
1518
16- # plt.style.use('ggplot')
17-
1819# %%
1920
2021
21- def plot_histogram_surface_distances (pat_name , lesion_id , rootdir , distanceMap , num_voxels , title , ablation_date ,
22+ def plot_histogram_surface_distances (pat_name , lesion_id , rootdir , distance_map , num_voxels , title , ablation_date ,
2223 flag_to_plot = True ):
23- fontsize = 18
24- lesion_id_str = str (lesion_id )
25- lesion_id = lesion_id_str .split ('.' )[0 ]
26- figName_hist = 'Pat_' + str (pat_name ) + '_Lesion' + str (lesion_id ) + '_AblationDate_' + ablation_date + '_histogram'
27- min_val = int (np .floor (min (distanceMap )))
28- max_val = int (np .ceil (max (distanceMap )))
24+
25+ cmap = sns .color_palette ("colorblind" ) # colorblind friendly palette
26+ fontsize = 20
27+ try :
28+ lesion_id_str = str (lesion_id )
29+ lesion_id = lesion_id_str .split ('.' )[0 ]
30+ except Exception :
31+ pass
32+ # lesion id is either not a string, nor it contains numerical characters. do nothing
33+ min_val = int (np .floor (min (distance_map )))
34+ max_val = int (np .ceil (max (distance_map )))
2935 fig , ax = plt .subplots (figsize = (12 , 10 ))
3036
31- col_height , bins , patches = ax .hist (distanceMap , ec = 'darkgrey ' , bins = range (min_val - 1 , max_val + 1 ))
37+ col_height , bins , patches = ax .hist (distance_map , ec = 'black ' , bins = range (min_val - 1 , max_val + 1 ))
3238
3339 voxels_nonablated = []
3440 voxels_insuffablated = []
@@ -52,21 +58,23 @@ def plot_histogram_surface_distances(pat_name, lesion_id, rootdir, distanceMap,
5258 sum_perc_insuffablated = ((voxels_insuffablated / num_voxels ) * 100 ).sum ()
5359 sum_perc_ablated = ((voxels_ablated / num_voxels ) * 100 ).sum ()
5460 # %%
61+
5562 if flag_to_plot is True :
5663 '''iterate through the bins to change the colors of the patches bases on the range [mm]'''
5764 for b , p , col_val in zip (bins , patches , col_height ):
5865 if b < 0 :
59- plt .setp (p , 'facecolor' , 'tab:red' ,
66+ plt .setp (p , 'facecolor' , cmap [ 3 ] ,
6067 label = 'Ablation Margin ' + r'$x < 0$' + 'mm :' + " %.2f" % sum_perc_nonablated + '%' )
6168 elif 0 <= b < 5 :
62- plt .setp (p , 'facecolor' , 'tab:orange' , label = 'Ablation Margin ' + r'$0 \leq x < 5$' + 'mm: ' + "%.2f" % sum_perc_insuffablated + '%' )
69+ plt .setp (p , 'facecolor' , cmap [8 ],
70+ label = 'Ablation Margin ' + r'$0 \leq x < 5$' + 'mm: ' + "%.2f" % sum_perc_insuffablated + '%' )
6371 elif b >= 5 : # fixed color in histogram
64- plt .setp (p , 'facecolor' , 'darkgreen' ,
65- label = 'Ablation Margin ' + r'$x \geq 5$' + 'mm: ' + " %.2f" % sum_perc_ablated + '%' )
72+ plt .setp (p , 'facecolor' , cmap [ 2 ] ,
73+ label = 'Ablation Margin ' + r'$x \geq 5$' + 'mm: ' + " %.2f" % sum_perc_ablated + '%' )
6674 # %%
6775 '''edit the axes limits and labels'''
6876 # csfont = {'fontname': 'Times New Roman'}
69- plt .xlabel ('Euclidean Distances (mm)' , fontsize = fontsize , color = 'black' )
77+ plt .xlabel ('Surface-to-Surface Euclidean Distances (mm)' , fontsize = fontsize , color = 'black' )
7078 plt .tick_params (labelsize = fontsize , color = 'black' )
7179 ax .tick_params (colors = 'black' , labelsize = fontsize )
7280 ax .set_xlim ([- 15 , 15 ])
@@ -92,10 +100,18 @@ def plot_histogram_surface_distances(pat_name, lesion_id, rootdir, distanceMap,
92100 plt .xticks (fontsize = fontsize )
93101 ax .tick_params (axis = 'both' , labelsize = fontsize )
94102 ax .grid (False )
95-
103+ #%% save the fig to disk as png and eps
104+ figName_hist = 'Pat_' + str (pat_name ) + '_Lesion' + str (
105+ lesion_id ) + '_AblationDate_' + ablation_date + '_histogram'
96106 plt .title (title + '. Patient ' + str (pat_name ) + '. Lesion ' + str (lesion_id ), fontsize = fontsize )
97107 figpathHist = os .path .join (rootdir , figName_hist )
98- plt .savefig (figpathHist , dpi = 300 , bbox_inches = 'tight' )
108+ ax .set_rasterized (True )
109+
110+ plt .savefig (figpathHist , dpi = 600 , bbox_inches = 'tight' )
111+ plt .savefig (figpathHist + '.eps' , dpi = 600 )
112+ plt .savefig (figpathHist + '.svg' , dpi = 600 )
113+ plt .savefig (figpathHist + '.pdf' , dpi = 600 )
114+
99115 plt .close ()
100116 return sum_perc_nonablated , sum_perc_insuffablated , sum_perc_ablated
101117 else :
0 commit comments