forked from wintermind/bestpred
/
bestpred_curves.py
306 lines (286 loc) · 11.7 KB
/
bestpred_curves.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
###############################################################################
# NAME: aiplcurves.py
# VERSION: 2.0 beta 3
# RELEASED: 01 AUGUST 2007
# MODIFIED: 10 September 2007
# AUTHORS: John B. Cole (john.cole@ars.usda.gov)
# DESCRIPTION: Reads output files from bestpred and plots the resulting
# lactation curves. This program is part of the bestpred package
# from AIPL.
###############################################################################
import glob, string, sys
#-- Keep this block as-is or you'll break
#-- your graphs.
import matplotlib.numerix.ma as M
import matplotlib
matplotlib.use('Agg')
import pylab
#--
# Change this to 1 if you want graphs plotted
# with kilograms instead of pounds.
lbtokg = 0
simmast = 1
# These parameters affect the appearance of your
# graphs. If you want larger typefaces on the
# graphs increase the *.*size items.
params = {'backend': 'Agg',
'axes.labelsize': 18,
'text.fontsize': 18,
'xtick.labelsize': 14,
'ytick.labelsize': 14
}
number2name = {'1':'Milk',
'2':'Fat',
'3':'Protein',
'4':'SCS'
}
number2units = {1:'kgs',
0:'lbs'
}
#
# Simple command line processing to get files.
# !!! If you change the CURVEfile parameter in
# bestpred.par you'll also need to change
# "cowcurve" below if you don't pass in actual
# . filenames and let aiplcurves glob the input.
#
if len(sys.argv) == 1:
#print '[ERROR]: You must provide a plot style: [C]omplete or [P]artial'
print '[ERROR]: You must provide a trait: 1 = milk, 2 = fat, 3 = protein, 4 = SCS, 5 = all'
sys.exit(0)
elif len(sys.argv) == 2:
curvefiles = glob.glob("cowcurve.*")
else:
curvefiles = sys.argv[2:]
# matplotlib's line plots, created using the plot command,
# do not know what to do with masked arrays. So the plotstyle
# is used to select either line or scatter plots. This may be
# fixed in newer versions of matplotlib.
#if sys.argv[1] not in ['C','P']:
# plotstyle = 'P'
#else:
# plotstyle = sys.argv[1]
if sys.argv[1] not in ['1','2','3','4','5']:
plottrait = 1
else:
plottrait = sys.argv[1]
#
# Loop over the filenames provided and create a graph from each file.
#
threshold = -999.0
lastcowid = 0
lastlacnum = 0
lasttrait = 0
nround = 0
dim, dbp, tdy, std, lac, dev = [], [], [], [], [], []
stdsum, dbpsum = 0., 0.
for cf in curvefiles:
print 'Processing %s' % ( cf )
infile = open(cf,'r')
while 1:
line = infile.readline()
if not line:
break
lpre = " ".join(line.split())
lp = string.split(lpre)
if len(lp) > 0:
cowid = lp[0]
lacnum = lp[1]
trait = lp[2]
if nround == 0:
lasttrait = trait
lastcowid = cowid
lastlacnum = lacnum
nround = nround + 1
if cowid != lastcowid or trait != lasttrait:
if int(lasttrait) == int(plottrait) or int(plottrait) == 5:
if cowid != lastcowid:
#print 'Cowid changed from %s to %s' % ( lastcowid, cowid )
_cowid = lastcowid
_lacnum = lastlacnum
else:
_cowid = cowid
_lacnum = lacnum
if trait != lasttrait:
#print 'Trait changed from %s to %s' % ( lasttrait, trait )
_trait = lasttrait
else:
_trait = trait
# We're processing a new animal
plotdim = M.array(dim)
plottd_ = M.array(tdy)
plottdy = M.masked_where(plottd_ == threshold, plottd_)
plotla_ = M.array(lac)
plotlac = M.masked_where(plotla_ == -2.0, plotla_)
plotst_ = M.array(std)
plotstd = M.masked_where(plotst_ == threshold, plotst_)
plotdb_ = M.array(dbp)
plotdbp = M.masked_where(plotdb_ == threshold, plotdb_)
plotde_ = M.array(dev)
plotdev = M.masked_where(plotde_ == threshold, plotde_)
maxdim = plotdim.max()
if lbtokg == 1 and int(_trait) < 4:
plottdy = plottdy / 2.2
plotlac = plotlac / 2.2
plotstd = plotstd / 2.2
stdsum = stdsum / 2.2
dbpsum = dbpsum / 2.2
# Plot lactation curves for each cow
# This will only work for lactation curve files with the
# form, e.g., 'cowcurve.S.HOUSA.EX.COW.0041.MT'.
#fig = pylab.figure(frameon=False)
pylab.rcParams.update(params)
fig = pylab.figure()
plot = fig.add_subplot(111)
plot_title = 'BP of %s for Cow %s (lactation %s)' % ( number2name[_trait], _cowid, _lacnum )
pylab.title(plot_title)
pylab.xlabel('DIM')
if int(_trait) < 4:
pylab.ylabel('%s Yield (%s)' % ( number2name[_trait], number2units[lbtokg] ) )
else:
pylab.ylabel('%s Yield' % ( number2name[_trait] ) )
plot.plot(plotdim, plottdy, 'go')
#if plotstyle == 'C':
plot.plot(plotdim, plotlac, '-g', linewidth=2)
plot.plot(plotdim, plotstd, '-b', linewidth=2)
##plot.plot(plotdim, plotdev, '-r', linewidth=1)
##plot.plot(plotdim, plotdbp, '-y', linewidth=1)
#else:
# plot.plot(plotdim, plotlac, 'o-g')
# plot.plot(plotdim, plotstd, 'o-b')
# Automatically scale axes.
pylab.axis([1,maxdim,0.,max(plotlac.max(),plotstd.max(),plottdy.max())])
figfile = 'curve_%s_%s_%s.png' % ( _cowid,number2name[_trait],_lacnum )
if int(_trait) < 4:
plot.annotate(str(round(dbpsum)), xy=(0.85, 0.95), xycoords='axes fraction', color='green', size=16)
plot.annotate(str(round(stdsum)), xy=(0.85, 0.90), xycoords='axes fraction', color='blue', size=16)
else:
plot.annotate(str(round(dbpsum/365.,2)), xy=(0.85, 0.85), xycoords='axes fraction', color='green', size=16)
plot.annotate(str(round(stdsum/365.,2)), xy=(0.85, 0.80), xycoords='axes fraction', color='blue', size=16)
print '\rWriting file %s' % ( figfile )
pylab.savefig(figfile)
pylab.show()
# We're not going to plot this trait
else:
pass
if trait != lasttrait:
lasttrait = trait
if cowid != lastcowid:
lastcowid = cowid
lastlacnum = lacnum
# We still need to clean up
if nround > 1:
# Cleanup to avoid multiple-plots-on-the-same-canvas
# problem.
try: del plotdim
except: pass
try: del plottd_
except: pass
try: del plottdy
except: pass
try: del plotlac
except: pass
try: del plotstd
except: pass
try: del plotdbp
except: pass
try: del plotde_
except: pass
try: del plotdev
except: pass
try: del plotdb_
except: pass
try: del plotla_
except: pass
try: del plotst_
except: pass
try: del dim
except: pass
try: del tdy
except: pass
try: del lac
except: pass
try: del std
except: pass
try: del dbp
except: pass
try: del dev
except: pass
try: del dev
except: pass
try: del fig
except: pass
try: del stdsum
except: pass
try: del dbpsum
except: pass
dim, dbp, tdy, std, lac, dev = [], [], [], [], [], []
stdsum, dbpsum = 0., 0.
else:
# We're adding another line of data to an existing
# cow-trait combination.
dim.append(int(lp[3]))
dbp.append(float(lp[4]))
tdy.append(float(lp[5]))
std.append(float(lp[6]))
lac.append(float(lp[4])+float(lp[6]))
dev.append(float(lp[7]))
# Increment sums
if int(lp[3]) <= 305:
stdsum = stdsum + float(lp[6])
dbpsum = dbpsum + ( float(lp[4]) + float(lp[6]) )
nround = nround + 1
else:
pass
infile.close()
# We ned to catch the data from the last animal in the input file
# and plot it. Wow, this is ugly.
plotdim = M.array(dim)
plottd_ = M.array(tdy)
plottdy = M.masked_where(plottd_ == threshold, plottd_)
plotla_ = M.array(lac)
plotlac = M.masked_where(plotla_ == -2.0, plotla_)
plotst_ = M.array(std)
plotstd = M.masked_where(plotst_ == threshold, plotst_)
plotdb_ = M.array(dbp)
plotdbp = M.masked_where(plotdb_ == threshold, plotdb_)
plotde_ = M.array(dev)
plotdev = M.masked_where(plotde_ == threshold, plotde_)
maxdim = plotdim.max()
if lbtokg == 1 and int(_trait) < 4:
plottdy = plottdy / 2.2
plotlac = plotlac / 2.2
plotstd = plotstd / 2.2
stdsum = stdsum / 2.2
dbpsum = dbpsum / 2.2
pylab.rcParams.update(params)
fig = pylab.figure()
plot = fig.add_subplot(111)
plot_title = 'BP of %s for Cow %s (lactation %s)' % ( number2name[trait], cowid, lacnum )
pylab.title(plot_title)
pylab.xlabel('DIM')
if int(trait) < 4:
pylab.ylabel('%s Yield (%s)' % ( number2name[trait], number2units[lbtokg] ) )
else:
pylab.ylabel('%s' % ( number2name[trait] ) )
plot.plot(plotdim, plottdy, 'go')
#if plotstyle == 'C':
plot.plot(plotdim, plotlac, '-g', linewidth=2)
plot.plot(plotdim, plotstd, '-b', linewidth=2)
#plot.plot(plotdim, plotdev, '-r', linewidth=1)
#plot.plot(plotdim, plotdbp, '-y', linewidth=1)
#else:
# plot.plot(plotdim, plotlac, 'o-g')
# plot.plot(plotdim, plotstd, 'o-b')
# Automatically scale axes.
pylab.axis([1,maxdim,0.,max(plotlac.max(),plotstd.max(),plottdy.max())])
figfile = 'curve_%s_%s_%s.png' % ( cowid,number2name[trait],lacnum )
if int(_trait) == 4:
plot.annotate(str(round(dbpsum/365.,2)), xy=(0.85, 0.85), xycoords='axes fraction', color='green', size=16)
plot.annotate(str(round(stdsum/365.,2)), xy=(0.85, 0.80), xycoords='axes fraction', color='blue', size=16)
else:
plot.annotate(str(round(dbpsum)), xy=(0.85, 0.95), xycoords='axes fraction', color='green', size=16)
plot.annotate(str(round(stdsum)), xy=(0.85, 0.90), xycoords='axes fraction', color='blue', size=16)
print '\rWriting file %s' % ( figfile )
pylab.savefig(figfile)
pylab.show()