00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 import logging
00058 import math, string
00059 from PyPedal import pyp_demog
00060
00061 try:
00062 import psyco
00063 psyco.full()
00064 except ImportError:
00065 print '[WARNING]: The psyco module could not be imported in pyp_graphics. Psyco speed optiimizations are not available.'
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 def rmuller_spy_matrix_pil(A,fname='tmp.png',cutoff=0.1,do_outline=0,
00083 height=300,width=300):
00084 """
00085 rmuller_spy_matrix_pil() implements a matlab-like 'spy' function to display the
00086 sparsity of a matrix using the Python Imaging Library.
00087 """
00088 try:
00089 import Image, ImageDraw
00090 except:
00091 return 0
00092 img = Image.new("RGB",(width,height),(255,255,255))
00093 draw = ImageDraw.Draw(img)
00094 n,m = A.shape
00095 if n > width or m > height:
00096 raise "Rectangle too big %d %d %d %d" % (n,m,width,height)
00097 for i in range(n):
00098 xmin = width*i/float(n)
00099 xmax = width*(i+1)/float(n)
00100
00101
00102
00103 if n == m:
00104 for j in range(i,n-1):
00105
00106 ymin = height*j/float(m)
00107 ymax = height*(j+1)/float(m)
00108 if abs(A[i,j]) > cutoff:
00109 if do_outline:
00110 draw.rectangle((xmin,ymin,xmax,ymax),fill=(0,0,255),
00111 outline=(0,0,0))
00112 draw.rectangle((ymin,xmin,ymax,xmax),fill=(0,0,255),
00113 outline=(0,0,0))
00114 else:
00115 draw.rectangle((xmin,ymin,xmax,ymax),fill=(0,0,255))
00116 draw.rectangle((ymin,xmin,ymax,xmax),fill=(0,0,255))
00117
00118 else:
00119 for j in range(m):
00120 ymin = height*j/float(m)
00121 ymax = height*(j+1)/float(m)
00122 if abs(A[i,j]) > cutoff:
00123 if do_outline:
00124 draw.rectangle((xmin,ymin,xmax,ymax),fill=(0,0,255),
00125 outline=(0,0,0))
00126 else:
00127 draw.rectangle((xmin,ymin,xmax,ymax),fill=(0,0,255))
00128 img.save(fname)
00129 return
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 def rmuller_pcolor_matrix_pil(A,fname='tmp.png',do_outline=0,height=300,width=300):
00143 """
00144 rmuller_pcolor_matrix_pil() implements a matlab-like 'pcolor' function to
00145 display the large elements of a matrix in pseudocolor using the Python Imaging
00146 Library.
00147 """
00148 try:
00149 import Image, ImageDraw
00150 except:
00151 return 0
00152 key_dict = {}
00153 color_cache = {}
00154
00155 img = Image.new("RGB",(width,height),(255,255,255))
00156 draw = ImageDraw.Draw(img)
00157
00158
00159
00160
00161
00162
00163 mina = A.min()
00164 maxa = A.max()
00165
00166 n,m = A.shape
00167 if n > width or m > height:
00168 raise "Rectangle too big %d %d %d %d" % (n,m,width,height)
00169 for i in range(n):
00170 xmin = width*i/float(n)
00171 xmax = width*(i+1)/float(n)
00172 for j in range(m):
00173 ymin = height*j/float(m)
00174 ymax = height*(j+1)/float(m)
00175
00176
00177
00178
00179
00180
00181 _cache_key = '%s_%s_%s' % (A[i,j],mina,maxa)
00182 try:
00183 color = color_cache[_cache_key]
00184 except KeyError:
00185 color = rmuller_get_color(A[i,j],mina,maxa)
00186 color_cache[_cache_key] = color
00187
00188 try:
00189 _v = key_dict[color]
00190 except KeyError:
00191
00192 key_dict[A[i,j]] = color
00193 if do_outline:
00194 draw.rectangle((xmin,ymin,xmax,ymax),fill=color,outline=(0,0,0))
00195 else:
00196 draw.rectangle((xmin,ymin,xmax,ymax),fill=color)
00197
00198
00199 img.save(fname)
00200 return
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 def rmuller_get_color(a,cmin,cmax):
00211 """
00212 Convert a float value to one of a continuous range of colors.
00213 Rewritten to use recipe 9.10 from the O'Reilly Python Cookbook.
00214 """
00215 try:
00216 a = float(a-cmin)/(cmax-cmin)
00217 except ZeroDivisionError:
00218 a = 0.5
00219 blue = min((max((4*(0.75-a),0.)),1.))
00220 red = min((max((4*(a-0.25),0.)),1.))
00221 green = min((max((4*math.fabs(a-0.5)-1.,0)),1.))
00222 return '#%1x%1x%1x' % (int(15*red),int(15*green),int(15*blue))
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 def draw_pedigree(pedobj, gfilename='pedigree', gtitle='', gformat='jpg', gsize='f', gdot='1', gorient='p', gdirec='', gname=0, gfontsize=10, garrow=1, gtitloc='b', gtitjust='c', gshowall=1, gclusters=0):
00248 """
00249 draw_pedigree() uses the pydot bindings to the graphviz library -- if they
00250 are available on your system -- to produce a directed graph of your pedigree
00251 with paths of inheritance as edges and animals as nodes. If there is more than
00252 one generation in the pedigree as determind by the "gen" attributes of the animals
00253 in the pedigree, draw_pedigree() will use subgraphs to try and group animals in the
00254 same generation together in the drawing.
00255 """
00256 from pyp_utils import string_to_table_name
00257 _gtitle = string_to_table_name(gtitle)
00258
00259
00260
00261
00262 if gtitloc not in ['t','b']:
00263 gtitloc = 'b'
00264 if gtitjust not in ['c','l','r']:
00265 gtitjust = 'c'
00266
00267
00268
00269 if not pedobj.kw['pedigree_is_renumbered']:
00270 if pedobj.kw['messages'] != 'quiet':
00271 print '[GRAPH]: The pedigree that you passed to pyp_graphics/draw_pedigree() is not renumbered. Because of this, there may be errors in the rendered pedigree. In order to insure that the pedigree drawing is accurate, you should renumber the pedigree before calling draw_pedigree().'
00272 logging.error('The pedigree that you passed to pyp_graphics/draw_pedigree() is not renumbered. Because of this, there may be errors in the rendered pedigree. In order to insure that the pedigree drawing is accurate, you should renumber the pedigree before calling draw_pedigree().')
00273
00274
00275 import pydot
00276
00277
00278
00279 if gtitle == '':
00280 if gshowall:
00281 g = pydot.Dot(graph_name=_gtitle, type='graph', strict=False, suppress_disconnected=False, simplify=True)
00282 else:
00283 g = pydot.Dot(graph_name=_gtitle, type='graph', strict=False, suppress_disconnected=True, simplify=True)
00284 else:
00285 if gshowall:
00286 g = pydot.Dot(label=gtitle, labelloc=gtitloc, labeljust=gtitjust, graph_name=_gtitle, type='graph', strict=False, suppress_disconnected=False, simplify=True)
00287 else:
00288 g = pydot.Dot(label=gtitle, labelloc=gtitloc, labeljust=gtitjust, graph_name=_gtitle, type='graph', strict=False, suppress_disconnected=True, simplify=True)
00289
00290 try:
00291 gfontsize = int(gfontsize)
00292 except:
00293 gfontsize = 10
00294 if gfontsize < 10:
00295 gfontsize = 10
00296 gfontsize = str(gfontsize)
00297
00298 g.set_page("8.5,11")
00299 g.set_size("7.5,10")
00300 if gorient == 'l':
00301 g.set_orientation("landscape")
00302 else:
00303 g.set_orientation("portrait")
00304 if gsize != 'l':
00305 g.set_ratio("auto")
00306 if gdirec == 'RL':
00307 g.set_rankdir('RL')
00308 elif gdirec == 'LR':
00309 g.set_rankdir('LR')
00310 else:
00311 pass
00312 g.set_center('true')
00313 g.set_concentrate('true')
00314 g.set_ordering('out')
00315 if gformat not in g.formats:
00316 gformat = 'jpg'
00317
00318 if len(pedobj.metadata.unique_gen_list) <= 1:
00319 for _m in pedobj.pedigree:
00320
00321 if gname:
00322 _node_name = _m.name
00323 else:
00324 _node_name = _m.animalID
00325 _an_node = pydot.Node(_node_name)
00326 _an_node.set_fontname('Helvetica')
00327
00328 _an_node.set_fontsize(gfontsize)
00329 _an_node.set_height('0.35')
00330 if _m.sex == 'M' or _m.sex == 'm':
00331 _an_node.set_shape('box')
00332 elif _m.sex == 'F' or _m.sex == 'f':
00333 _an_node.set_shape('ellipse')
00334 else:
00335 pass
00336 g.add_node(_an_node)
00337
00338 if int(_m.sireID) != pedobj.kw['missing_parent']:
00339 if gname:
00340 if garrow:
00341 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].name, _m.name))
00342 else:
00343 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].name, _m.name, dir='none'))
00344 else:
00345 if garrow:
00346 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].originalID,_m.originalID))
00347 else:
00348 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].originalID,_m.originalID, dir='none'))
00349 if int(_m.damID) != pedobj.kw['missing_parent']:
00350 if gname:
00351 if garrow:
00352 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].name, _m.name))
00353 else:
00354 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].name, _m.name, dir='none'))
00355 else:
00356 if garrow:
00357 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].originalID,_m.originalID))
00358 else:
00359 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].originalID,_m.originalID, dir='none'))
00360
00361 elif gclusters:
00362 for _g in pedobj.metadata.unique_gen_list:
00363 _sg_anims = []
00364 _sg_name = 'sg%s' % (_g)
00365 if gshowall:
00366 sg = pydot.Cluster(graph_name=_sg_name, suppress_disconnected=False, simplify=True)
00367 else:
00368 sg = pydot.Cluster(graph_name=_sg_name, suppress_disconnected=True, simplify=True)
00369 for _m in pedobj.pedigree:
00370 if int(_m.gen) == int(_g):
00371 _sg_anims.append(_m.animalID)
00372
00373 if gname:
00374 _node_name = _m.name
00375 else:
00376 _node_name = _m.animalID
00377 _an_node = pydot.Node(_node_name)
00378 _an_node.set_fontname('Helvetica')
00379 _an_node.set_fontsize(gfontsize)
00380 _an_node.set_height('0.35')
00381 if _m.sex == 'M' or _m.sex == 'm':
00382 _an_node.set_shape('box')
00383 if _m.sex == 'F' or _m.sex == 'f':
00384 _an_node.set_shape('ellipse')
00385 sg.add_node(_an_node)
00386 g.add_subgraph(sg)
00387
00388
00389 for _m in pedobj.pedigree:
00390 if _m.sireID != pedobj.kw['missing_parent']:
00391 if gname:
00392 if garrow:
00393 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].name,_m.name))
00394 else:
00395 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].name,_m.name, dir='none'))
00396 else:
00397 if garrow:
00398 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].animalID,_m.animalID))
00399 else:
00400 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].animalID,_m.animalID, dir='none'))
00401 if _m.damID != pedobj.kw['missing_parent']:
00402 if gname:
00403 if garrow:
00404 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].name,_m.name))
00405 else:
00406 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].name,_m.name, dir='none'))
00407 else:
00408 if garrow:
00409 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].animalID,_m.animalID))
00410 else:
00411 g.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].animalID,_m.animalID, dir='none'))
00412
00413 else:
00414 for _g in pedobj.metadata.unique_gen_list:
00415 _sg_anims = []
00416 _sg_name = 'sg%s' % (_g)
00417 if gshowall:
00418 sg = pydot.Subgraph(graph_name=_sg_name, suppress_disconnected=False, simplify=True)
00419 else:
00420 sg = pydot.Subgraph(graph_name=_sg_name, suppress_disconnected=True, simplify=True)
00421 for _m in pedobj.pedigree:
00422 if int(_m.gen) == int(_g):
00423 _sg_anims.append(_m.animalID)
00424
00425
00426 if gname:
00427 _node_name = _m.name
00428 else:
00429 _node_name = _m.animalID
00430 _an_node = pydot.Node(_node_name)
00431 _an_node.set_fontname('Helvetica')
00432 _an_node.set_fontsize(gfontsize)
00433 _an_node.set_height('0.35')
00434 if _m.sex == 'M' or _m.sex == 'm':
00435 _an_node.set_shape('box')
00436 if _m.sex == 'F' or _m.sex == 'f':
00437 _an_node.set_shape('ellipse')
00438 sg.add_node(_an_node)
00439
00440
00441 if _m.sireID != pedobj.kw['missing_parent']:
00442 if gname:
00443 if garrow:
00444 sg.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].name,_m.name))
00445 else:
00446 sg.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].name,_m.name, dir='none'))
00447 else:
00448 if garrow:
00449 sg.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].animalID,_m.animalID))
00450 else:
00451 sg.add_edge(pydot.Edge(pedobj.pedigree[int(_m.sireID)-1].animalID,_m.animalID, dir='none'))
00452 if _m.damID != pedobj.kw['missing_parent']:
00453 if gname:
00454 if garrow:
00455 sg.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].name,_m.name))
00456 else:
00457 sg.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].name,_m.name, dir='none'))
00458 else:
00459 if garrow:
00460 sg.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].animalID,_m.animalID))
00461 else:
00462 sg.add_edge(pydot.Edge(pedobj.pedigree[int(_m.damID)-1].animalID,_m.animalID, dir='none'))
00463
00464 if len(_sg_anims) > 0:
00465 _sg_list = ''
00466 for _a in _sg_anims:
00467 if len(_sg_list) == 0:
00468 _sg_list = 'same,%s' % (_a)
00469 else:
00470 _sg_list = '%s,%s' % (_sg_list,_a)
00471 sg.set_rank(_sg_list)
00472 g.add_subgraph(sg)
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 if gdot:
00484 dfn = '%s.dot' % (gfilename)
00485
00486 g.write(dfn)
00487
00488
00489
00490 outfile = '%s.%s' % (gfilename,gformat)
00491
00492 g.write(outfile,prog='dot',format=gformat)
00493 return 1
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509 def plot_founders_by_year(pedobj,gfilename='founders_by_year',gtitle='Founders by Birthyear'):
00510 """
00511 founders_by_year() uses matplotlib -- if available on your system -- to produce a
00512 bar graph of the number (count) of founders in each birthyear.
00513 """
00514 try:
00515 import matplotlib
00516 matplotlib.use('Agg')
00517 import pylab
00518 except ImportError:
00519 if pedobj.kw['messages'] == 'verbose':
00520 print '[ERROR]: pyp_graphics/plot_founders_by_year() was unable to import the matplotlib module!'
00521 logging.error('pyp_graphics/plot_founders_by_year() was unable to import the matplotlib module!')
00522 return 0
00523
00524 fby = pyp_demog.founders_by_year(pedobj)
00525
00526 try:
00527 pylab.clf()
00528 pylab.bar(fby.keys(),fby.values())
00529 pylab.title(gtitle)
00530 pylab.xlabel('Year')
00531 pylab.ylabel('Number of founders')
00532 plotfile = '%s.png' % (gfilename)
00533
00534 myplotfile = open(plotfile,'w')
00535
00536 pylab.savefig(myplotfile)
00537 myplotfile.close()
00538 return 1
00539 except:
00540 if pedobj.kw['messages'] == 'verbose':
00541 print '[ERROR]: pyp_graphics/plot_founders_by_year() was unable to create the plot \'%s\' (%s.png).' % ( gtitle, gfilename )
00542
00543 logging.error('pyp_graphics/plot_founders_by_year() was unable to create the plot \'%s\' (%s.png).', gtitle, gfilename)
00544 return 0
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 def plot_founders_pct_by_year(pedobj,gfilename='founders_pct_by_year',gtitle='Founders by Birthyear'):
00555 """
00556 founders_pct_by_year() uses matplotlib -- if available on your system -- to produce a
00557 line graph of the frequency (percentage) of founders in each birthyear.
00558 """
00559 try:
00560 import matplotlib
00561 matplotlib.use('Agg')
00562 import pylab
00563 except ImportError:
00564 if pedobj.kw['messages'] == 'verbose':
00565 print '[ERROR]: pyp_graphics/plot_founders_pct_by_year() was unable to import the matplotlib module!'
00566 logging.error('pyp_graphics/plot_founders_pct_by_year() was unable to import the matplotlib module!')
00567 return 0
00568
00569 fby = pyp_demog.founders_by_year(pedobj)
00570 _freqdict = {}
00571 for _k in fby.keys():
00572 _freqdict[_k] = float(fby[_k]) / float(pedobj.metadata.num_unique_founders)
00573 try:
00574 import matplotlib
00575 matplotlib.use('Agg')
00576 import pylab
00577 pylab.clf()
00578 pylab.plot(fby.keys(),_freqdict.values())
00579 pylab.title(gtitle)
00580 pylab.xlabel('Year')
00581 pylab.ylabel('% founders')
00582 plotfile = '%s.png' % (gfilename)
00583 myplotfile = open(plotfile,'w')
00584 pylab.savefig(myplotfile)
00585 myplotfile.close()
00586 return 1
00587 except:
00588 if pedobj.kw['messages'] == 'verbose':
00589 print '[ERROR]: pyp_graphics/plot_pct_founders_by_year() was unable to create the plot \'%s\' (%s.png).' % (gtitle,gfilename)
00590 logging.error('pyp_graphics/plot_pct_founders_by_year() was unable to create the plot \'%s\' (%s.png).' % (gtitle,gfilename))
00591 return 0
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604 def pcolor_matrix_pylab(A,fname='pcolor_matrix_matplotlib'):
00605 """
00606 pcolor_matrix_pylab() implements a matlab-like 'pcolor' function to
00607 display the large elements of a matrix in pseudocolor using the Python Imaging
00608 Library.
00609 """
00610 try:
00611 import matplotlib
00612 matplotlib.use('Agg')
00613 import pylab
00614 except ImportError:
00615 if pedobj.kw['messages'] == 'verbose':
00616 print '[ERROR]: pyp_graphics/pcolor_matrix_pylab() was unable to import the matplotlib module!'
00617 logging.error('pyp_graphics/pcolor_matrix_pylab() was unable to import the matplotlib module!')
00618 return 0
00619
00620 try:
00621 import numpy
00622 pylab.clf()
00623 x = pylab.arange(A.shape[0])
00624 X, Y = pylab.meshgrid(x,x)
00625
00626 xmin = min(pylab.ravel(X))
00627 xmax = max(pylab.ravel(X))
00628 pylab.xlim(xmin, xmax)
00629 ymin = min(pylab.ravel(Y))
00630 ymax = max(pylab.ravel(Y))
00631 pylab.ylim(ymin, ymax)
00632 pylab.axis('off')
00633
00634 pylab.pcolor(X, Y, pylab.transpose(A))
00635 pylab.clim(0.0, 1.0)
00636 plotfile = '%s.png' % (fname)
00637 myplotfile = open(plotfile,'w')
00638 pylab.savefig(myplotfile)
00639 myplotfile.close()
00640 return 1
00641 except:
00642 if pedobj.kw['messages'] == 'verbose':
00643 print '[ERROR]: pyp_graphics/pcolor_matrix_pylab() was unable to create the plot %s.' % (plotfile)
00644 logging.error('pyp_graphics/pcolor_matrix_pylab() was unable to create the plot %s.', (plotfile))
00645 return 0
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 def spy_matrix_pylab(A,fname='spy_matrix_matplotlib'):
00659 """
00660 spy_matrix_pylab() implements a matlab-like 'pcolor' function to
00661 display the large elements of a matrix in pseudocolor using the Python Imaging
00662 Library.
00663 """
00664 try:
00665 import matplotlib
00666 matplotlib.use('Agg')
00667 import pylab
00668 except ImportError:
00669 if pedobj.kw['messages'] == 'verbose':
00670 print '[ERROR]: pyp_graphics/spy_matrix_pylab() was unable to import the matplotlib module!'
00671 logging.error('pyp_graphics/spy_matrix_pylab() was unable to import the matplotlib module!')
00672 return 0
00673
00674 try:
00675 import numpy
00676 pylab.clf()
00677 pylab.spy2(A)
00678 plotfile = '%s.png' % (fname)
00679 myplotfile = open(plotfile,'w')
00680 pylab.savefig(myplotfile)
00681 myplotfile.close()
00682 return 1
00683 except:
00684 if pedobj.kw['messages'] == 'verbose':
00685 print '[ERROR]: pyp_graphics/spy_matrix_pylab() was unable to create the plot %s.' % (plotfile)
00686 logging.error('pyp_graphics/spy_matrix_pylab() was unable to create the plot %s.', (plotfile))
00687 return 0
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699 def plot_line_xy(xydict, gfilename='plot_line_xy', gtitle='Value by key', gxlabel='X', gylabel='Y', gformat='png'):
00700 """
00701 plot_line_xy() uses matplotlib -- if available on your system -- to produce a
00702 line graph of the values in a dictionary for each level of key.
00703 """
00704 try:
00705 import matplotlib
00706 matplotlib.use('Agg')
00707 import pylab
00708 except ImportError:
00709 if pedobj.kw['messages'] == 'verbose':
00710 print '[ERROR]: pyp_graphics/plot_line_xy() was unable to import the matplotlib module!'
00711 logging.error('pyp_graphics/plot_line_xy() was unable to import the matplotlib module!')
00712 return 0
00713
00714 if gformat not in ['png']:
00715 gformat = 'png'
00716
00717 try:
00718 pylab.clf()
00719 pylab.plot(xydict.keys(),xydict.values())
00720 pylab.title(gtitle)
00721 pylab.xlabel(gxlabel)
00722 pylab.ylabel(gylabel)
00723 plotfile = '%s.%s' % (gfilename, gformat)
00724 myplotfile = open(plotfile,'w')
00725 pylab.savefig(myplotfile)
00726 myplotfile.close()
00727 _status = 1
00728 except:
00729
00730
00731 logging.error('pyp_graphics/plot_line_xy() was unable to create the plot \'%s\' (%s.%s).' % (gtitle,gfilename,gformat))
00732 _status = 0
00733 return _status
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757 def new_draw_pedigree(pedobj, gfilename='pedigree', gtitle='', gformat='jpg', \
00758 gsize='f', gdot=1, gorient='p', gdirec='', gname=0, garrow=1, \
00759 gtitloc='b', gtitjust='c', gshowall=1, gprog='dot'):
00760 """
00761 draw_pedigree() uses the pydot bindings to the graphviz library -- if they
00762 are available on your system -- to produce a directed graph of your pedigree
00763 with paths of inheritance as edges and animals as nodes. If there is more than
00764 one generation in the pedigree as determind by the "gen" attributes of the animals
00765 in the pedigree, draw_pedigree() will use subgraphs to try and group animals in the
00766 same generation together in the drawing.
00767 """
00768
00769 try:
00770 import pygraphviz
00771 except ImportError:
00772 if pedobj.kw['messages'] == 'verbose':
00773 print '[ERROR]: pyp_graphics/new_draw_pedigree() was unable to import the pygraphviz module!'
00774 logging.error('pyp_graphics/new_draw_pedigree() was unable to import the pygraphviz module!')
00775 return 0
00776
00777
00778
00779 _tf = {0:False, 1:True}
00780
00781 from pyp_utils import string_to_table_name
00782 _gtitle = string_to_table_name(gtitle)
00783
00784 if gtitloc not in ['t','b']:
00785 gtitloc = 'b'
00786 if gtitjust not in ['c','l','r']:
00787 gtitjust = 'c'
00788
00789 if not pedobj.kw['pedigree_is_renumbered']:
00790 if pedobj.kw['messages'] != 'quiet':
00791 print '[GRAPH]: The pedigree that you passed to pyp_graphics/draw_pedigree() is not renumbered. Because of this, there may be errors in the rendered pedigree. In order to insure that the pedigree drawing is accurate, you should renumber the pedigree before calling draw_pedigree().'
00792 logging.error('The pedigree that you passed to pyp_graphics/draw_pedigree() is not renumbered. Because of this, there may be errors in the rendered pedigree. In order to insure that the pedigree drawing is accurate, you should renumber the pedigree before calling draw_pedigree().')
00793
00794
00795 g = pygraphviz.AGraph(directed=True,strict=False)
00796
00797
00798 g.graph_attr['type'] = 'graph'
00799
00800
00801
00802 g.graph_attr['name'] = _gtitle
00803
00804
00805
00806
00807
00808 if gtitle != '':
00809 g.graph_attr['label'] = gtitle
00810 g.graph_attr['labelloc'] = gtitloc
00811 g.graph_attr['labeljust'] = gtitjust
00812
00813
00814 g.graph_attr['page'] = '8.5,11'
00815 g.graph_attr['size'] = '7.5,10'
00816
00817
00818 if gorient == 'l':
00819 g.graph_attr['orientation'] = 'landscape'
00820 else:
00821 g.graph_attr['orientation'] = 'portrait'
00822
00823 if gsize != 'l':
00824 g.graph_attr['ratio'] = 'auto'
00825 if gdirec == 'RL':
00826 g.graph_attr['rankdir'] = 'RL'
00827 elif gdirec == 'LR':
00828 g.graph_attr['rankdir'] = 'LR'
00829 else:
00830 pass
00831
00832
00833 g.graph_attr['center'] = 'True'
00834 g.graph_attr['concentrate'] = 'True'
00835 g.graph_attr['fontsize'] = str(pedobj.kw['default_fontsize'])
00836 g.graph_attr['ordering'] = 'out'
00837
00838 for _m in pedobj.pedigree:
00839
00840 if gname:
00841 _node_name = _m.name
00842 else:
00843 _node_name = _m.animalID
00844 g.add_node(_node_name)
00845 n = g.get_node(_node_name)
00846 n.attr['shape'] = 'box'
00847 n.attr['fontname'] = 'Helvetica'
00848 n.attr['fontsize'] = str(pedobj.kw['default_fontsize'])
00849 n.attr['height'] = '0.35'
00850
00851 if _m.sex == 'M' or _m.sex == 'm':
00852 n.attr['shape'] = 'box'
00853 elif _m.sex == 'F' or _m.sex == 'f':
00854 n.attr['shape'] = 'ellipse'
00855 else:
00856 n.attr['shape'] = 'octagon'
00857
00858
00859
00860 if int(_m.sireID) != pedobj.kw['missing_parent']:
00861 if gname:
00862 _sire_edge = pedobj.pedigree[int(_m.sireID)-1].name
00863 else:
00864
00865
00866
00867
00868 _sire_edge = pedobj.pedigree[int(_m.sireID)-1].animalID
00869 g.add_edge(_sire_edge,_node_name)
00870 if not _tf[garrow]:
00871
00872 e = g.get_edge(_sire_edge,n)
00873 e.attr['dir'] = 'none'
00874 if int(_m.damID) != pedobj.kw['missing_parent']:
00875 if gname:
00876 _dam_edge = pedobj.pedigree[int(_m.damID)-1].name
00877 else:
00878 _dam_edge = pedobj.pedigree[int(_m.damID)-1].animalID
00879 g.add_edge(_dam_edge,_node_name)
00880 if not _tf[garrow]:
00881
00882 e = g.get_edge(_dam_edge,n)
00883 e.attr['dir'] = 'none'
00884
00885
00886
00887
00888 if gdot:
00889 dfn = '%s.dot' % (gfilename)
00890 try:
00891 g.write(dfn)
00892 except:
00893 if pedobj.kw['messages'] == 'verbose':
00894 print '[ERROR]: pyp_graphics/new_draw_pedigree() was unable to write the dotfile %s.' % (dfn)
00895 logging.error('pyp_graphics/new_draw_pedigree() was unable to draw the dotfile %s.', (dfn))
00896
00897
00898
00899 outfile = '%s.%s' % (gfilename,gformat)
00900 g.draw(outfile,prog=gprog)
00901 return 1
00902
00903
00904
00905
00906
00907