def draw_pedigree(pedigree, out_file, plot_params=fp.PlotParams(), colors=fp.DEFAULT_COLORS, add_dummy=False, identifier='id', labels=None): '''Draw pedigree to file (EPS/PNG/...).''' extension = util.file_ext(out_file) if not extension in ['eps', 'png']: raise ValueError('Unsupported file extension ''%s''' % (extension,)) coord_params = fp.CoordParams() coord_params.algorithm = 'default' # Create famplot input file # TODO: replace writing to a temp file by a copy constructor fp.Pedigree(our Pedigree) and # pass it instead of prefix tempdir = tempfile.mkdtemp() famplot_file = tempdir + '/file.fpl' eps_file = out_file if extension == 'eps' else tempdir + '/file.eps' to_pedfiddler(pedigree, famplot_file, add_dummy=add_dummy, identifier=identifier, labels=labels) p = fp.draw_pedigree(famplot_file, coord_params, plot_params, colors, eps_file) # Convert EPS to PNG/other supported formats by the Image library if extension != 'eps': with open(eps_file, 'rb') as eps: img = Image.open(eps) img.save(out_file) # Clean up shutil.rmtree(tempdir) return p
def test_draw_pedigree_hardcoded(self): '''Test drawing a pedigree as an EPS file using hard-coded positions.''' coord_params = fp.CoordParams() coord_params.algorithm = 'hardcoded' coord_params.coord_file = TestFamPlot.__PEDIGREE_POSITIONS coord_params.balance_marriages = False out = tempfile.NamedTemporaryFile(suffix='.eps', delete=False) ped_info = fp.draw_pedigree(TestFamPlot.__PEDIGREE, coord_params, fp.PlotParams(), fp.DEFAULT_COLORS, out) expected_coord = TestFamPlot.__read_coord_dict(coord_params.coord_file) test_util.assert_positions_almost_equal(ped_info.coord_dict, expected_coord, decimal=10, err_msg='Wrong coordinates')
def test_generation_number(self): '''Test calculating generation numbers..''' ped = fp.read_pedigree(TestFamPlot.__PEDIGREE_UNBALANCED) ped_info = fp.PedigreeInfo(ped) cp = fp.CoordParams() cp.algorithm = 'default' fp.compute_coords(ped_info, cp) assert_equal(ped_info.gen_dict, { 1: 0, 2: 0, 3: 1, 4: 1, 5: 1, 6: 2, 7: 2, 8: 3 }, 'Wrong generation numbers')
def __test_draw_pedigree_default(self, infile): '''Test drawing a pedigree as an EPS file using the default node placement.''' coord_params = fp.CoordParams() coord_params.algorithm = 'default' out = tempfile.NamedTemporaryFile(suffix='.eps', delete=True) ped_info = fp.draw_pedigree(infile, coord_params, fp.PlotParams(), fp.DEFAULT_COLORS, out) #expected_coord = TestFamPlot.__read_coord_dict(TestFamPlot.__PEDIGREE_POSITIONS) # Positions are not unique; they depend on ordering person lists within the default algorithm # test_util.assert_positions_almost_equal(ped_info.coord_dict, expected_coord, # decimal=10, err_msg='Wrong coordinates') # print '===============================================' # print ped_info.pprint() # print '===============================================' for info in ped_info.data.itervalues(): self.assertTrue(info.coord[0] >= 0 and info.coord[1] >= 0, 'Unset coordinate at node %s' % (info.name, )) return ped_info
#!/usr/bin/env python ''' ============================================================ Plot the nbhrs1298 pedigree. Created on August 16, 2012 @author: Oren Livne <*****@*****.**> ============================================================ ''' import impute as im, famplot as fp p = im.io.read_npz(im.itu.NBHRS1298_STAGE4) in_file = 'NBHRS1298_STAGE4.txt' im.pt.to_pedfiddler(p.pedigree, in_file) coord_params = fp.CoordParams() coord_params.algorithm = 'default' out = 'NBHRS1298_STAGE4.eps' # out = tempfile.NamedTemporaryFile(suffix='.eps', delete=False) ped_info = fp.draw_pedigree(in_file, coord_params, fp.PlotParams(), fp.DEFAULT_COLORS, out)