def test_read_sg_005_edges(self): """Just a edge""" g = SizeGraph.readsg(sio("""4 MS 0.1 0.2 0.3 0.4 1 0 1 """)) self.assertEqual(4, len(g.nodes)) C = g.get_connections() self.assertEqual(1, len(C)) c = C.pop() self._assert_connection(c, 0.1, 0.2) g = SizeGraph.readsg(sio("""4 1 0 1 """),ms=[.1,.2,.3,.4]) self.assertEqual(4, len(g.nodes)) C = g.get_connections() self.assertEqual(1, len(C)) c = C.pop() self._assert_connection(c, 0.1, 0.2) """More edges""" g = SizeGraph.readsg(sio("""4 MS 0.1 0.2 0.3 0.4 4 0 1 1 2 2 3 3 0 """)) self.assertEqual(4, len(g.nodes)) C = g.get_connections() self.assertEqual(4, len(C)) ii=[.1,.2,.3,.4,.1] for i in xrange(4): self._assert_in_connection(C, ii[i],ii[i+1]) g = SizeGraph.readsg(sio("""4 4 0 1 1 2 2 3 3 0 """),ms=[.1,.2,.3,.4]) self.assertEqual(4, len(g.nodes)) C = g.get_connections() self.assertEqual(4, len(C)) ii=[.1,.2,.3,.4,.1] for i in xrange(4): self._assert_in_connection(C, ii[i],ii[i+1])
def _get_graph(fg, fms): if fms is None: ms = None else: ms = [] l = fms.readline() while l: ms.append(float(l)) l = fms.readline() return SizeGraph.readsg(fg, ms)
def _get_graph(fg,fms): if fms is None: ms = None else: ms = [] l=fms.readline() while l: ms.append(float(l)) l=fms.readline() return SizeGraph.readsg(fg, ms)
def test_xxx_dump_read_sg_dual(self): g = SG() N = [g.add_node(.1), g.add_node(.2), g.add_node(.3), g.add_node(.4) ] N[0].connect(N[1]) N[0].connect(N[2]) N[1].connect(N[3]) f = sio() g.dump(f) f.seek(0) self.assertSameGraph(g, SizeGraph.readsg(f)) print f.getvalue()
def _read_and_compute(fg, fms, fout): if fms is None: ms = None else: ms = [] l = fms.readline() while l: ms.append(float(l)) l = fms.readline() g = SizeGraph.readsg(fg, ms) h = H0Tree.compute_H0Tree(g) sf = h.get_sf() sf.dump(fout)
def test_xxx_dump_options(self): g = SG() N = [g.add_node(.1), g.add_node(.2), g.add_node(.3), g.add_node(.4) ] N[0].connect(N[1]) N[0].connect(N[2]) N[1].connect(N[3]) f = sio() g.dump(f,legacy=True) f.seek(0) print """LEGACY ######################## """ + f.getvalue() self.assertSameGraph(g, SizeGraph.readsg(f,ms=[.1,.2,.3,.4])) self.assertNotIn("#", f.getvalue()) self.assertNotIn("MS", f.getvalue()) f = sio() g.dump(f,comments=False) f.seek(0) print """COMMENTS ######################## """ + f.getvalue() self.assertSameGraph(g, SizeGraph.readsg(f)) self.assertNotIn("#", f.getvalue()) self.assertIn("MS", f.getvalue())
def test_read_sg_x00_from_file_real_file(self): p = tempfile.mktemp() f = file(p, "w") try: f.write("""1 MS 1.1 """) f.close() g = SG() g.add_node(1.1) self.assertSameGraph(g, SizeGraph.readsg(p)) finally: os.unlink(p)
def test_read_sg_000_nodes(self): g = SizeGraph.readsg(sio()) self.assertIsInstance(g, SG) """Just a Node""" g = SizeGraph.readsg(sio("1")) self.assertEqual(1, len(g.nodes)) g = SizeGraph.readsg(sio("""1"""),[0.2]) self.assertEqual(1, len(g.nodes)) self.assertEqual(0.2,g.nodes.pop().phy) g = SizeGraph.readsg(sio("""1 MS 0.5""")) self.assertEqual(1, len(g.nodes)) self.assertEqual(0.5,g.nodes.pop().phy) """Just a Nodes""" g = SizeGraph.readsg(sio("4")) self.assertEqual(4, len(g.nodes)) g = SizeGraph.readsg(sio("""4"""),[1,2,3,4]) self.assertEqual(4, len(g.nodes)) for i in xrange(1,5): self.assertIn(i,[n.phy for n in g.nodes]) g = SizeGraph.readsg(sio("""4 MS 0.1 0.2 0.3 0.4""")) self.assertEqual(4, len(g.nodes)) for i in range(1,5): self.assertIn(i/10.0,[n.phy for n in g.nodes]) """ms instead file value""" g = SizeGraph.readsg(sio("""4 MS 0.1 0.2 0.3 0.4"""),[1,2,3,4]) self.assertEqual(4, len(g.nodes)) for i in range(1,5): self.assertIn(i,[n.phy for n in g.nodes])
def test_read_sg_050_asserts(self): """Pass wrong ms""" self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""2"""),ms=[1]) """Sanity check""" SizeGraph.readsg(f=sio("""2"""),ms=[1,2,3]) """Invalid formats""" self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""1.2""")) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""1.2""")) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""1 paperino""")) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""1 1.3""")) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""1 MS qqq""")) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""1 MS 1.2 2.2 """)) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""1 MS 1.2 pippo """)) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""2 MS 1.2 2.3 1 a 2 """)) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""2 MS 1.2 2.3 1 1 a """)) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""2 MS 1.2 2.3 1 paperino """)) self.assertRaises(ValueError, SizeGraph.readsg, f=sio("""2 MS 1.2 2.3 1 0.2 1.1 """))
def test_read_sg_010_comments_and_empty_lines(self): """Empty graph""" g = SizeGraph.readsg(sio(""" """)) self.assertEqual(0, len(g.nodes)) g = SizeGraph.readsg(sio("""# comment #c # o #mme # nt """)) self.assertEqual(0, len(g.nodes)) """Just a node""" g = SizeGraph.readsg(sio(""" 1 """)) self.assertEqual(1, len(g.nodes)) g = SizeGraph.readsg(sio("""# comment #c # o 1 #mme # nt """)) self.assertEqual(1, len(g.nodes)) """Some Nodes""" g = SizeGraph.readsg(sio(""" 4 """)) self.assertEqual(4, len(g.nodes)) g = SizeGraph.readsg(sio("""# comment #c # o 5 #mme # nt """)) self.assertEqual(5, len(g.nodes)) g = SizeGraph.readsg(sio(""" 4 MS 1 # www 2 3 # ha # ha 4 """)) self.assertEqual(4, len(g.nodes)) for i in xrange(1,5): self.assertIn(i, [n.phy for n in g.nodes]) """Edges""" g = SizeGraph.readsg(sio("""# dddd 4 #co MS #mments 0.1 # 0.2 0.3 0.4 # 4 #more com 0 1 1 2 # ments 2 3 3 0 """)) self.assertEqual(4, len(g.nodes)) C = g.get_connections() self.assertEqual(4, len(C)) ii=[.1,.2,.3,.4,.1] for i in xrange(4): self._assert_in_connection(C, ii[i],ii[i+1])