-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForestplot.py
More file actions
165 lines (125 loc) · 9.77 KB
/
Forestplot.py
File metadata and controls
165 lines (125 loc) · 9.77 KB
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
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
''''''''' Forest Plot '''''''''
# Parameters
# Only model_estimate and text are mandatory. Other parameters are optional.
# 1. model_estimate - Model whose forest plot is to be made.
# Should be a dataframe with column names as Study,value1,value2,mean and weight (weight column is optional).
# Study - name of the Studies done.
# value1 - lower limit of forest plot.
# value2 - upper limit of forest plot.
# mean - mean value or position of model estimates.
# weight - weight of the Studies done (optional).
# 2. text - Data to be represented in tabular form - Should be a dataframe with column names as text,info,O1 and O2 (O1 and O2 are Optional).
# text - column1 header of the table and name of Studies.
# info - data about the mean,lower and upper limits of forest plot.
# O1 and O2 are treatment and control group ratios.
# 3. save_location - location and name of the forestplot to be saved (default - saves as png file with the name 'forestplot' in the working directory).
# 4. zero - Location of the null line (default - 1).
# 5. clip - Limit after which the line is to be clipped into arrow. Should be a list - [lower_clip_limit, upper_clip_limit] (default - [0,10]).
# 6. xlab - Label for the x-axis (default - Lab)
# 7. colour - Set the colors for all the elements. Should be a list - [model_estimate_colour,reported_estimate_colour] (default - ['b','r']).
# 8. x_scale - Set the scaling of the x-axis (default - 'linear').
# 9. title - Title of the forest plot (default - '').
# 10. grid - To add grids to the forest plot if required (default - False).
# 11. is_summary - To select whether the table is to be viewed along with the plot (default - False).
# 12. fontsize - Set the font size of the data displayed in the table (default - 14).
# 13. dpi - Set the resolution of the forest plot being saved (default - 500)
# 14. reported_estimate - Reported estimate for model whose forest plot is to be made.
# Should be a dataframe with column names as Study,value1,value2,mean and weight (weight column is optional). (default - pd.DataFrame()).
def forestplot(model_estimate,text,save_location='forestplot.png',zero=1,clip=[0,10],xlab='Lab',colour=['b','r'],x_scale='linear',title='',grid=False,is_summary=False,fontsize=14,dpi=500,reported_estimate=pd.DataFrame()):
if(not('weight' in model_estimate)):
model_estimate['weight'] = 50/((model_estimate['value2']-model_estimate['value1']))
Overall = model_estimate[model_estimate['Study'] == 'Overall'].reset_index()
if len(colour) == 1 :
colour.append('r')
for i in range(len(model_estimate)-1):
if model_estimate['value1'][len(model_estimate)-i-2] < clip[0]:
line_range_model_estimate = [clip[0],model_estimate['value2'][len(model_estimate)-i-2]]
label = [i+1,i+1]
study = [model_estimate['mean'][len(model_estimate)-i-2],model_estimate['mean'][len(reported_estimate)-i-2]]
plt.plot(line_range_model_estimate,label, color=colour[0])
plt.scatter(study,label,marker='s',s=model_estimate['weight'][len(model_estimate)-i-2], color=colour[0])
plt.plot([clip[0]+0.1,clip[0],clip[0]+0.1],[i+1.1,i+1,i+0.9], color=colour[0])
if model_estimate['value2'][len(model_estimate)-i-2] > clip[1]:
line_range_model_estimate = [model_estimate['value1'][len(model_estimate)-i-2],clip[1]]
label = [i+1,i+1]
study = [model_estimate['mean'][len(model_estimate)-i-2],model_estimate['mean'][len(model_estimate)-i-2]]
plt.plot(line_range_model_estimate,label, color=colour[0])
plt.scatter(study,label,marker='s',s=model_estimate['weight'][len(model_estimate)-i-2], color=colour[0])
plt.plot([clip[1]-0.1,clip[1],clip[1]-0.1],[i+1.1,i+1,i+0.9], color=colour[0])
else:
line_range_model_estimate = [model_estimate['value1'][len(model_estimate)-i-2],model_estimate['value2'][len(model_estimate)-i-2]]
label = [i+1,i+1]
study = [model_estimate['mean'][len(model_estimate)-i-2],model_estimate['mean'][len(model_estimate)-i-2]]
plt.plot(line_range_model_estimate,label, color=colour[0])
plt.scatter(study,label,marker='s',s=model_estimate['weight'][len(model_estimate)-i-2], color=colour[0])
if not(reported_estimate.empty):
if(not('weight' in reported_estimate)):
reported_estimate['weight'] = 50/((reported_estimate['value2']-reported_estimate['value1']))
for i in range(len(reported_estimate)):
if reported_estimate['value1'][len(reported_estimate)-i-1] < clip[0]:
line_range_reported_estimate = [clip[0],reported_estimate['value2'][len(reported_estimate)-i-1]]
label1 = [i+0.6,i+0.6]
study1 = [reported_estimate['mean'][len(reported_estimate)-i-1],reported_estimate['mean'][len(reported_estimate)-i-1]]
plt.plot(line_range_reported_estimate,label1, color=colour[1])
plt.scatter(study1,label1,s=reported_estimate['weight'][len(reported_estimate)-i-1], color=colour[1])
plt.plot([clip[0]+0.1,clip[0],clip[0]+0.1],[i+0.7,i+0.6,i+0.5], color=colour[1])
if reported_estimate['value2'][len(reported_estimate)-i-1] > clip[1]:
line_range_reported_estimate = [reported_estimate['value1'][len(reported_estimate)-i-1],clip[1]]
label1 = [i+0.6,i+0.6]
study1 = [reported_estimate['mean'][len(reported_estimate)-i-1],reported_estimate['mean'][len(reported_estimate)-i-1]]
plt.plot(line_range_reported_estimate,label1, color=colour[1])
plt.scatter(study1,label1,s=reported_estimate['weight'][len(reported_estimate)-i-1], color=colour[1])
plt.plot([clip[1]-0.1,clip[1],clip[1]-0.1],[i+0.7,i+0.6,i+0.5], color=colour[1])
else:
line_range_reported_estimate = [reported_estimate['value1'][len(reported_estimate)-i-1],reported_estimate['value2'][len(reported_estimate)-i-1]]
label1 = [i+0.6,i+0.6]
study1 = [reported_estimate['mean'][len(reported_estimate)-i-1],reported_estimate['mean'][len(reported_estimate)-i-1]]
plt.plot(line_range_reported_estimate,label1, color=colour[1])
plt.scatter(study1,label1,s=reported_estimate['weight'][len(reported_estimate)-i-1], color=colour[1])
sns.set_style('darkgrid')
plt.fill([Overall['value1'][0],Overall['mean'][0],Overall['value2'][0],Overall['mean'][0]],[0,-0.25,0,0.25], colour[0])
plt.xscale(x_scale)
plt.axvline(x=zero)
plt.xlim(xmin=clip[0],xmax=clip[1])
plt.xlabel(xlab)
plt.title(title,y=1.1)
plt.tick_params(axis='y', which='both',labelsize =0)
if grid == False:
plt.grid()
#Legends
if not(reported_estimate.empty):
patch1 = mlines.Line2D([], [], color=colour[0], marker='s', linestyle='None',
markersize=10, label='Model Estimates')
patch2 = mlines.Line2D([], [], color=colour[1], marker='o', linestyle='None',
markersize=10, label='Reported Estimates')
plt.legend(handles=[patch1,patch2],loc='upper center',bbox_to_anchor=(0.5, 1.1),ncol=2)
# Tables
if is_summary :
left_table_data = text.loc[:,['text','info']]
left_cell_value = []
for i in range(len(left_table_data)):
left_cell_value.append([left_table_data['text'].tolist()[i],left_table_data['info'].tolist()[i]])
left_table = plt.table(cellText=left_cell_value,
cellLoc='left', colWidths=[0.5,0.5],
rowLoc='left', colLoc='left',
loc='left', bbox=[-0.65, 0, 0.55, 1.1],
edges='open')
left_table.auto_set_font_size(False)
left_table.set_fontsize(fontsize)
if('O1' in text and 'O2' in text):
right_table_data = text.loc[:,['O1','O2']]
right_cell_value = []
for i in range(len(right_table_data)):
right_cell_value.append([right_table_data['O1'].tolist()[i],right_table_data['O2'].tolist()[i]])
right_table = plt.table(cellText=right_cell_value,
cellLoc='left', colWidths=[0.5,0.5],
rowLoc='left', colLoc='left',
loc='right', bbox=[1.05, 0, 0.55, 1.1],
edges='open')
right_table.auto_set_font_size(False)
right_table.set_fontsize(fontsize)
plt.savefig(save_location, dpi=dpi, bbox_inches='tight')