def test_new_ssf(self): sf = SF.SizeFunction() ssf = sf.new_ssf(cl=-2, m=5, points=((0, 1), (1, 2))) self.assertIsInstance(ssf, SimpleSizeFunction) self.assertEqual(ssf, SimpleSizeFunction(-2, 5, ((0, 1), (1, 2)))) ssf = sf.new_ssf(cl=-2, points=((0, 1), (1, 2))) self.assertEqual(ssf, SimpleSizeFunction(-2, None, ((0, 1), (1, 2))))
def test_copy(self): sf1 = SF.SizeFunction() sf1.new_ssf(1).add_point(2, 3) sf2 = sf1.copy() self.assertIsInstance(sf2, SF.SizeFunction) self.assertEqual(sf1, sf2) self.assertFalse(sf1 is sf2)
def test_equal(self): sf1 = SF.SizeFunction() sf2 = SF.SizeFunction() self.assertEqual(sf1, sf2) ssf1 = sf1.new_ssf(1, 5) self.assertNotEqual(sf1, sf2) ssf2 = sf2.new_ssf(1, 5) self.assertEqual(sf1, sf2) ssf1.add_point(2, 3) self.assertNotEqual(sf1, sf2) ssf2.add_point(2, 3) self.assertEqual(sf1, sf2) sf1.add(ssf1) self.assertNotEqual(sf1, sf2) sf2.add(ssf1) self.assertEqual(sf1, sf2)
def test_read_sf_from_file_dump_dual(self): sf = SF.SizeFunction() sf.new_ssf(-10, 10, [(-3, 2), (-5, -2), (2, 9), (-1, 1)]) sf.new_ssf(-3, 12, [(-1, 2), (2, 10), (2, 11), (-1, 3)]) f = sio() sf.dump(f) f.seek(0) self.assertEqual(sf, readsf(f))
def test_add(self): sf = SF.SizeFunction() ssf = SimpleSizeFunction(cl=-2, m=5, points=((0, 1), (1, 2))) sf.add(ssf) self.assertEqual(ssf, sf._ssfs[0]) self.assertFalse(ssf is sf._ssfs[0]) self.assertRaises(ValueError, sf.add, SimpleSizeFunctionOld(-1, ((1, 2), (1.5, 2.5))))
def test_add(self): sf = SF.SizeFunctionOld() ssf = SimpleSizeFunctionOld(cl=-2, points=((0, 1), (1, 2))) ssf2 = sf.add(ssf) self.assertEqual(ssf, sf._ssfs[0]) self.assertFalse(ssf is sf._ssfs[0]) self.assertIsInstance(ssf2, SimpleSizeFunctionOld) self.assertEqual(ssf, ssf2) self.assertIs(ssf2, sf._ssfs[0]) self.assertRaises(ValueError, sf.add, SimpleSizeFunction(-1, 5, ((1, 2), (1.5, 2.5))))
def test_read_sf_from_file_real_file(self): p = tempfile.mktemp() f = file(p, "w") try: f.write("""l 0 0 1 p 1 .1 .5""") f.close() sf = SF.SizeFunction() sf.new_ssf(0, 1, [(.1, .5)]) self.assertEqual(sf, readsf(p)) finally: os.unlink(p)
def test_ssfs(self): sf = SF.SizeFunction() ssf = sf.new_ssf(1, 5) ssf.add_point(2, 3) ssfs = sf.ssfs self.assertEqual(1, len(ssfs)) self.assertEqual(ssf, ssfs[0]) sf.add(ssf) ssfs = sf.ssfs self.assertEqual(2, len(ssfs)) self.assertEqual(ssf, ssfs[0]) self.assertEqual(ssf, ssfs[1])
def test_read_sf_from_file_0000(self): empty = SF.SizeFunctionOld() """Empty""" self.assertEqual(empty, readsf(f=sio())) """Just Empty lines""" self.assertEqual(empty, readsf(f=sio(""" """))) """Just Comments""" self.assertEqual( empty, readsf(f=sio("""#Just #S #o #m #e # COMMENTS######## ################"""))) """Comments and empty""" self.assertEqual(empty, readsf(f=sio("""# Comment ################"""))) """A Line """ sf = SF.SizeFunctionOld() sf.new_ssf(0.0) self.assertEqual(sf, readsf(sio("""l 0 0.0"""))) """A Line min max""" sf = SF.SizeFunction() sf.new_ssf(0.0, 1.0) self.assertEqual(sf, readsf(sio("""l 0 0.0 1.0"""))) """Two lines min max""" sf = SF.SizeFunction() sf.new_ssf(0.0, 1.0) sf.new_ssf(-30.0, 20.0) self.assertEqual(sf, readsf(sio("""l 0 0.0 1.0 l 1 -30 20"""))) """Two lines mix""" sf = SF.SizeFunction() sf.new_ssf(0.0, 1.0) sf.new_ssf(-30.0) self.assertEqual(sf, readsf(sio("""l 0 0.0 1.0 l 1 -30"""))) """Line and points""" sf = SF.SizeFunction() sf.new_ssf(-2.0, 1.0, [(0.1, .5), (-1, 0.4)]) self.assertEqual( sf, readsf( sio("""l 0 -2.0 1.0 p 1 0.1 0.5 p 2 -1.0 0.4"""))) """Lines and points""" sf = SF.SizeFunction() sf.new_ssf(-2.0, 1.0, [(0.1, .5), (-1, 0.4)]) sf.new_ssf(-23.0, 31.0, [(-0.1, 5), (-12, 0.44)]) self.assertEqual( sf, readsf( sio("""l 0 -2.0 1.0 p 1 0.1 0.5 p 2 -1.0 0.4 l 3 -23 31 p 4 -0.1 5 p 5 -12 0.44"""))) """Lines and points empty and comments""" sf = SF.SizeFunction() sf.new_ssf(-2.0, 1.0, [(0.1, .5), (-1, 0.4)]) sf.new_ssf(-23.0, 31.0, [(-0.1, 5), (-12, 0.44)]) self.assertEqual( sf, readsf( sio(""" # first comment l 0 -2.0 1.0 # second comment p 1 0.1 0.5 p 2 -1.0 0.4 #l 3 -23 31 l 3 -23 31 #more comment p 4 -0.1 5 p 5 -12 0.44 """)))
def test_Create(self): SF.SizeFunction()
N = len(elements) for f in elements: i += 1 logging.info("========= [%d/%d] =========" % (i, N)) b = os.path.basename(f).rsplit('.')[0] d = traindir f = os.path.join(d, f) ms = os.path.join(d, b + ms_ext) logging.info("Read graph from %s [ms = %s] " % (f, ms)) g = _get_graph(f, file(ms)) logging.info("..... Computing H0Tree") h = H0Tree.compute_H0Tree(g) logging.info("..... Computing SF") sf = h.get_sf() logging.info("Done") if outdir is not None: out = os.path.join(outdir, b + sf_ext) logging.info("Save the sf to %s" % out) sf.dump(file(out, 'w')) chk_f = os.path.join(outdir, b + sf_ext) sf_chk = None try: sf_chk = SizeFunction.readsf(chk_f) except IOError: logging.warning("Cannot find %s for checking sf" % chk_f) else: if sf_chk == sf: logging.info("OK: The function are the SAME!!!!!!") else: logging.warning("KO: Something was going wrong !!!!!!")
for f in elements: i += 1 logging.info("========= [%d/%d] ========="%(i,N)) b = os.path.basename(f).rsplit('.')[0] d = traindir f = os.path.join(d,f) ms = os.path.join(d,b + ms_ext) logging.info("Read graph from %s [ms = %s] "%(f,ms)) g = _get_graph(f, file(ms)) logging.info("..... Computing H0Tree") h = H0Tree.compute_H0Tree(g) logging.info("..... Computing SF") sf = h.get_sf() logging.info("Done") if outdir is not None: out = os.path.join(outdir , b + sf_ext) logging.info("Save the sf to %s"%out) sf.dump(file(out,'w')) chk_f = os.path.join(outdir,b + sf_ext) sf_chk= None try: sf_chk = SizeFunction.readsf(chk_f) except IOError: logging.warning("Cannot find %s for checking sf"%chk_f) else: if sf_chk == sf: logging.info("OK: The function are the SAME!!!!!!") else: logging.warning("KO: Something was going wrong !!!!!!")