def test_toString(self): """ tests toString() """ self.lrp = pb.LeafRandomParameter(pb.Organism()) self.add_successors() lrp = self.lrp # rename lrp.name = "the leaf" self.assertEqual(lrp.__str__(False), "name: the leaf, organType: 4, subType: -1.", "toString: value unexpected")
def test_toString(self): self.srp = pb.StemRandomParameter(pb.Organism()) srp = self.srp # rename srp.name = "the stem" self.assertEqual(srp.__str__(False), "name: the stem, organType: 3, subType: -1.", "toString: value unexpected")
def test_constructors(self): """ tests two kinds of constructors and copy""" self.root_example_rrp() # 1. constructor from scratch param = self.p0.realize() root = pb.Root(1, param, True, True, 0., 0., pb.Vector3d(0, 0, -1), 0, 0, False, 0) root.setOrganism(self.plant) root.addNode(pb.Vector3d(0, 0, -3), 0) # parent must have at least one nodes # 2. used in simulation (must have parent, since there is no nullptr in Pyhton) root2 = pb.Root(self.plant, self.p1.subType, pb.Vector3d(0, 0, -1), 0, root, 0, 0) root.addChild(root2) # 3. deep copy (with a factory function) plant2 = pb.Organism() root3 = root.copy(plant2) self.assertEqual( str(root), str(root3), "deep copy: the root string representations shold be equal") self.assertIsNot(root.getParam(), root3.getParam(), "deep copy: roots have same specific parameter set" ) # type OrganSpecificParameter self.assertEqual(str(root.param()), str(root3.param()), "deep copy: roots have different parameter values" ) # type RootSpecificParameter
def stem_example_rtp(self): """ an example used in the tests below, a main stem with laterals """ self.plant = pb.Organism( ) # store organism (not owned by Organ, or OrganRandomParameter) p0 = pb.StemRandomParameter(self.plant) p0.name, p0.subType, p0.la, p0.lb, p0.lmax, p0.ln, p0.r, p0.dx = "main", 1, 1, 10, 100, ( 89. / 19.), 1, 0.5 p0.successor = [3] p0.successorP = [1.] p1 = pb.StemRandomParameter(self.plant) p1.name, p1.subType, p1.la, p1.ln, p1.r, p1.dx = "lateral", 3, 25, 0, 2, 0.1 self.p0, self.p1 = p0, p1 # needed at later point self.plant.setOrganRandomParameter( p0) # the organism manages the type parameters and takes ownership self.plant.setOrganRandomParameter(p1) # TODO (first node is not set, if seed is used) self.plant.setOrganRandomParameter(pb.SeedRandomParameter(self.plant)) self.seed = pb.Seed( self.plant) # store parent (not owned by child Organ) # param0 = p0.realize() # set up stem by hand (without a stem system) param0.la, param0.lb = 0, 0 # its important parent has zero length, otherwise creation times are messed up parentstem = pb.Stem(1, param0, True, True, 0., 0., pb.Vector3d(0, 0, -1), 0, 0, False, 0) # takes ownership of param0 parentstem.setOrganism(self.plant) parentstem.addNode(pb.Vector3d(0, 0, -3), 0) # there is no nullptr in Python self.parentstem = parentstem # store parent (not owned by child Organ) # self.stem = pb.Stem(self.plant, p0.subType, pb.Vector3d(0, 0, -1), 0, self.parentstem, 0, 0) self.stem.setOrganism(self.plant)
def test_parameter(self): """ tests getParameter() """ plant = pb.Organism() srp = pb.SeedRandomParameter(plant) srp.firstB = 0.123 srp.delaySBs = 12 ot = srp.getParameter("organType") # test defaults st = srp.getParameter("subType") nz = srp.getParameter("nz") firstB = srp.getParameter("firstB") delaySBs = srp.getParameter("delaySB_dev") firstB2 = srp.getParameter( "firstB_mean" ) # we can always add "_mean" to avoid naming conflicts self.assertEqual(ot, 1., "getParameter: value unexpected") self.assertEqual(st, 0, "getParameter: value unexpected") self.assertEqual(nz, 1., "getParameter: value unexpected") self.assertEqual(firstB, 0.123, "getParameter: value unexpected") self.assertEqual(firstB, firstB2, "getParameter: value unexpected") self.assertEqual(delaySBs, 12, "getParameter: value unexpected") srp.firstB = 123 # change values srp.delaySBs = 456 firstB = srp.getParameter("firstB") delaySBs = srp.getParameter("delaySB_dev") self.assertEqual(firstB, 123, "getParameter: value unexpected") self.assertEqual(delaySBs, 456, "getParameter: value unexpected")
def test_parameter(self): """ tests getParameter() """ self.plant = pb.Organism() rrp = pb.RootRandomParameter(self.plant) rrp.lns = 0.123 rrp.la = 12 ot = rrp.getParameter("organType") # test defaults st = rrp.getParameter("subType") gf = rrp.getParameter("gf") ln = rrp.getParameter("ln") lns = rrp.getParameter("ln_dev") la = rrp.getParameter( "la_mean") # we can always add "_mean" to avoid naming conflicts self.assertEqual(ot, 2., "getParameter: value unexpected") self.assertEqual(st, -1., "getParameter: value unexpected") self.assertEqual(gf, 1., "getParameter: value unexpected") self.assertEqual(ln, 1., "getParameter: value unexpected") self.assertEqual(lns, 0.123, "getParameter: value unexpected") self.assertEqual(la, 12, "getParameter: value unexpected") rrp.theta = 123 # change values rrp.thetas = 456 theta = rrp.getParameter("theta") thetas = rrp.getParameter("theta_dev") self.assertEqual(theta, 123, "getParameter: value unexpected") self.assertEqual(thetas, 456, "getParameter: value unexpected")
def test_leaf_geometry(self): """ tests if a leaf geometry can be set""" self.plant = pb.Organism() lrp = pb.LeafRandomParameter(self.plant) lrp.la = 3.5 lrp.lb = 1. lrp.ln = 3. lrp.lmax = lrp.la + lrp.lb + lrp.ln lrp.areaMax = 10 """ radial geometry """ y = np.array([-3, -3 * 0.7, 0., 3.5 * 0.7, 3.5]) l = np.array([0., 2.2 * 0.7, 1.7, 1.8 * 0.7, 0.]) N = 105 # N is rather high for testing lrp.createLeafGeometry(y, l, N) self.assertEqual(lrp.leafMid(), 3., "unexpected leaf mid") self.assertEqual(lrp.leafLength(), 6.5, "unexpected leaf length") y_ = np.linspace(0, lrp.leafLength(), N) geom = lrp.leafGeometry x_ = np.array([ x[-1] for x in geom]) self.assertEqual(x_.shape[0], y_.shape[0], "leaf geometry has wrong size"); a = lrp.areaMax / lrp.leafLength() self.assertAlmostEqual(2 * np.sum(x_) / N * a * lrp.leafLength(), lrp.areaMax , 2, "xml: value unexpected") plt.plot(x_ * a, y_, "g-*") plt.plot(-x_ * a, y_, "g-*") plt.ylim([0, 7]) plt.xlim([-2, 2]) plt.show()
def root_example_rrp(self): """ an example used in the tests below, a main root with laterals """ self.plant = pb.Organism( ) # store organism (not owned by Organ, or OrganRandomParameter) p0 = pb.RootRandomParameter(self.plant) p0.name, p0.subType, p0.la, p0.lb, p0.lmax, p0.ln, p0.r, p0.dx = "taproot", 1, 10., 1., 100., 1., 1.5, 0.5 p0.successor = [2] p0.successorP = [1.] p1 = pb.RootRandomParameter(self.plant) p1.name, p1.subType, p1.lmax, p1.r, p1.dx = "lateral", 2, 25., 2., 0.1 self.p0, self.p1 = p0, p1 # needed at later point self.plant.setOrganRandomParameter( p0) # the organism manages the type parameters and takes ownership self.plant.setOrganRandomParameter(p1) srp = pb.SeedRandomParameter(self.plant) self.plant.setOrganRandomParameter(srp) param0 = p0.realize() # set up root by hand (without a root system) param0.la, param0.lb = 0, 0 # its important parent has zero length, otherwise creation times are messed up parentroot = pb.Root(1, param0, True, True, 0., 0., pb.Vector3d(0, 0, -1), 0, 0, False, 0) # takes ownership of param0 parentroot.setOrganism(self.plant) parentroot.addNode(pb.Vector3d(0, 0, -3), 0) # there is no nullptr in Python self.parentroot = parentroot # store parent (not owned by child Organ) self.root = pb.Root(self.plant, p0.subType, pb.Vector3d(0, 0, -1), 0, self.parentroot, 0, 0) self.root.setOrganism(self.plant)
def test_toString(self): """ tests __str__ output """ self.rrp = pb.RootRandomParameter(pb.Organism()) rrp = self.rrp # rename rrp.name = "the root" self.assertEqual(rrp.__str__(False), "name: the root, organType: 2, subType: -1.", "toString: value unexpected")
def stem_example(self): self.plant = pb.Organism() self.srp = pb.StemRandomParameter(self.plant) self.srp.la = 1.5 self.srp.lb = 5.5 self.srp.ln = 1.25 self.srp.lns = 0.12 self.srp.lmax = 7 * self.srp.ln + self.srp.la + self.srp.lb
def leaf_example(self): self.plant = pb.Organism() self.lrp = pb.LeafRandomParameter(self.plant) self.lrp.la = 1.5 self.lrp.lb = 5.5 self.lrp.ln = 1.25 self.lrp.lns = 0.12 self.lrp.lmax = 7 * self.lrp.ln + self.lrp.la + self.lrp.lb self.lrp.subType = 1
def test_toString(self): """ tests __str__ output """ orp = pb.OrganRandomParameter(pb.Organism()) orp.name = "the great organ" orp.organType = 1 orp.subType = 2 self.assertEqual(orp.__str__(False), "name: the great organ, organType: 1, subType: 2.", "toString: value unexpected")
def test_realize(self): """ calls realize """ o = pb.Organism() orp = pb.OrganRandomParameter(o) orp.subType = 2 p = orp.realize() self.assertEqual(p.__class__.__name__, "OrganSpecificParameter", "realize: unexpected class type") self.assertEqual(p.subType, 2, "realize: unexpected sub type")
def test_parameter(self): """ tests getParameter() """ orp = pb.OrganRandomParameter(pb.Organism()) orp.organType = 1 orp.subType = 2 ot = orp.getParameter("organType") st = orp.getParameter("subType") self.assertEqual(ot, 1., "getParameter: value unexpected") self.assertEqual(st, 2., "getParameter: value unexpected")
def test_toString(self): """ tests __str__ output """ self.srp = pb.SeedRandomParameter(pb.Organism()) srp = self.srp # rename srp.name = "the seed" # print(srp.__str__(False)) # print(srp) self.assertEqual(srp.__str__(False), "name: the seed, organType: 1, subType: 0.", "toString: value unexpected")
def test_xml(self): """ write the organ type parameter as xml, and rereads it """ plant = pb.Organism() orp = pb.OrganRandomParameter(plant) orp.name = "the great organ" orp.subType = 2 orp.writeXML("organ.xml") otp2 = pb.OrganRandomParameter(plant) otp2.readXML("organ.xml") self.assertEqual(otp2.name, orp.name, "xml: value unexpected") self.assertEqual(otp2.subType, orp.subType, "xml: value unexpected")
def shoot_example(self): """ example seed parameters used below """ self.plant = pb.Organism() self.srp = pb.SeedRandomParameter(self.plant) self.srp.firstB = 10 self.srp.delayB = 7 self.srp.nC = 5 self.srp.firstSB = 21 self.srp.delaySB = 4 self.srp.delayRC = 15 self.srp.nz = 1. self.srp.subType = 0
def hand_example(self): """ an example used in the tests below, a hand with two fingers """ self.human1 = pb.Organism() # same example as in test_constructor ... otp = pb.OrganRandomParameter(self.human1) self.human1.setOrganRandomParameter(otp) op = otp.realize() self.hand = pb.Organ(self.human1.getOrganIndex(), op, True, True, 0, 15., pb.Vector3d(0., 0., 1.), 0., 0, False, 0) self.hand.setOrganism(self.human1) self.thumb = pb.Organ(self.human1, self.hand, 0, 0, 4, pb.Vector3d(0., 0., 1.), 0., 0) # delayedfor 4 days self.little_finger = pb.Organ(self.human1, self.hand, 0, 0, 3, pb.Vector3d(0., 0., 1.), 0., 0) # delayed for 3 days self.hand.addChild(self.thumb)
def stem_example(self): self.plant = pb.Organism() self.srp = pb.StemRandomParameter(self.plant) self.srp.la = 1.5 self.srp.lb = 5.5 self.srp.ln = 1.25 self.srp.lns = 0.12 self.srp.lmax = 7 * self.srp.ln + self.srp.la + self.srp.lb # print(self.srp.las) self.srp.successor = [4, 5, 6] self.srp.successorP = [0.4, 0.1, 0.5]
def test_organ_random_parameters(self): """ test ability to set, get, read, and write type parameters """ human1 = pb.Organism() # same example as in test_constructor ... otp1 = pb.OrganRandomParameter(human1) otp1.name = "nose" otp1.subType = 1 otp2 = pb.OrganRandomParameter(human1) otp2.subType = 2 otp2.name = "eye" human1.setOrganRandomParameter(otp1) # set human1.setOrganRandomParameter(otp2) otps = human1.getOrganRandomParameter(pb.OrganTypes.organ) self.assertEqual(otps[0].name, "nose", "otp: name not expected ") self.assertEqual(otps[1].name, "eye", "otp: name not expected ") otp3 = pb.OrganRandomParameter(human1) otp3.organType = pb.OrganTypes.root otp3.subType = 1 otp3.name = "rootyhand" human1.setOrganRandomParameter(otp3) human1.writeParameters("human.xml") human2 = pb.Organism() # read again prototype1 = pb.OrganRandomParameter(human2) prototype1.organType = pb.OrganTypes.organ prototype2 = pb.OrganRandomParameter(human2) prototype2.organType = pb.OrganTypes.root human2.setOrganRandomParameter( prototype1 ) # set prototypes for reading, subTypes are overwritten if equal human2.setOrganRandomParameter(prototype2) human2.readParameters("human.xml") otp1 = human2.getOrganRandomParameter(pb.OrganTypes.organ, 1) otp2 = human2.getOrganRandomParameter(pb.OrganTypes.organ, 2) self.assertEqual(otp1.name, "nose", "otp: name not expected ") self.assertEqual(otp2.name, "eye", "otp: name not expected ") self.assertEqual(otp1.subType, 1, "otp: subType not expected ") self.assertEqual(otp2.subType, 2, "otp: subType not expected ") rtp = human2.getOrganRandomParameter(pb.OrganTypes.root, 1) self.assertEqual(rtp.name, "rootyhand", "otp: name not expected ") self.assertEqual(rtp.subType, 1, "otp: subType not expected ")
def test_constructors(self): """ tests constructor and copy """ plant = pb.Organism() orp = pb.OrganRandomParameter(plant) self.assertEqual(orp.name, "organ", "copy: default value unexpected") self.assertEqual(orp.organType, 0, "copy: default value unexpected") self.assertEqual(orp.subType, 0, "copy: default value unexpected") otp2 = orp.copy(plant) self.assertIsNot(orp, otp2, "copy: organ type parameter set is not copied") self.assertEqual(otp2.name, orp.name, "copy: value unexpected") self.assertEqual(otp2.organType, orp.organType, "copy: value unexpected") self.assertEqual(otp2.subType, orp.subType, "copy: value unexpected")
def test_constructors(self): """ tests three different kinds of constructors """ human1 = pb.Organism() otp = pb.OrganRandomParameter(human1) human1.setOrganRandomParameter(otp) op = otp.realize() # 1. constructor from scratch hand = pb.Organ(human1.getOrganIndex(), op, True, True, 0., 15., pb.Vector3d(0., 0., 1.), 0., 0, False, 0) hand.setOrganism(human1) # 2. used in simulation (must have parent, since there is no nullptr in Pyhton) thumb = pb.Organ(human1, hand, 0, 0, 4, pb.Vector3d(0., 0., 1.), 0., 0) little_finger = pb.Organ(human1, hand, 0, 0, 3, pb.Vector3d(0., 0., 1.), 0., 0) hand.addChild(thumb) hand.addChild(little_finger) # 3. deep copy (with a factory function) human2 = pb.Organism() human2.setOrganRandomParameter(otp.copy(human2)) hand2 = hand.copy(human2) self.assertEqual(str(hand), str(hand2), "deep copy: the organs should be equal") self.assertIsNot(hand.getParam(), hand2.getParam(), "deep copy: organs have same parameter set") self.assertEqual(str(hand.getParam()), str(hand2.getParam()), "deep copy: the different parameter set values") self.assertEqual(str(hand.getOrganRandomParameter()), str(hand2.getOrganRandomParameter()), "deep copy: the different random parameter set values")
def root_example(self): """ example root parameters used below """ self.plant = pb.Organism() self.rrp = pb.RootRandomParameter(self.plant) self.rrp.la = 1.5 self.rrp.lb = 5.5 self.rrp.ln = 1.25 self.rrp.lns = 0.12 self.rrp.lmax = 7 * self.rrp.ln + self.rrp.la + self.rrp.lb self.rrp.subType = 1 self.rrp.successor = [4, 5, 6] self.rrp.successorP = [0.4, 0.1, 0.5] self.rrp.ldelay = 5 self.rrp.ldelays = 2
def test_constructors(self): """ tests constructor and copy """ plant = pb.Organism() otp = pb.LeafRandomParameter(plant) otp.theta = 123 otp.thetas = 456 otp.gf = 789 otp2 = otp.copy(plant) self.assertIsNot(otp, otp2, "copy: organ type parameter set is not copied") self.assertEqual(otp2.name, otp.name, "copy: value unexpected") self.assertEqual(otp2.organType, otp.organType, "copy: value unexpected") self.assertEqual(otp2.subType, otp.subType, "copy: value unexpected") self.assertEqual(otp2.a, otp.a, "copy: value unexpected") self.assertEqual(otp2.theta, otp.theta, "copy: value unexpected") self.assertEqual(otp2.thetas, otp.thetas, "copy: value unexpected") self.assertEqual(otp2.gf, otp.gf, "copy: value unexpected")
def test_constructors(self): """ tests two kinds of constructors and copy """ srp = self.get_srp() # print(srp) # 1. constructor from scratch param = srp.realize() seed = pb.Seed(self.plant.getOrganIndex(), param, True, True, 0, 0, False, 0) seed.setOrganism(self.plant) # 2. used in simulation (must have parent, since there is no nullptr in Pyhton) seed2 = pb.Seed(self.plant) # 3. deep copy (with a factory function) plant2 = pb.Organism() seed3 = seed.copy(plant2) self.assertEqual(str(seed), str(seed3), "deep copy: the seed string representations shold be equal") self.assertIsNot(seed.getParam(), seed3.getParam(), "deep copy: organs have same parameter set") self.assertEqual(str(seed.param()), str(seed3.param()), "deep copy: organs have different parameter values") # type RootSpecificParameter
def test_constructors(self): ''' tests two kinds of constructors and copy''' self.stem_example_rtp() # 1. constructor from scratch param = self.p0.realize() stem = pb.Stem(1, param, True, True, 0., 0., self.ons , 0, False, 0) stem.setOrganism(self.plant) stem.addNode(pb.Vector3d(0, 0, -3), 0) # parent must have at least one nodes # 2. used in simulation (must have parent, since there is no nullptr in Pyhton) stem2 = pb.Stem(self.plant, self.p1.subType, self.ons , 0, stem, 0) stem.addChild(stem2) # 3. deep copy (with a factory function) plant2 = pb.Organism() stem3 = stem.copy(plant2) self.assertEqual(str(stem), str(stem3), "deep copy: the organs shold be equal") self.assertIsNot(stem.getParam(), stem3.getParam(), "deep copy: organs have same parameter set")
def get_srp(self): self.plant = pb.Organism() # need to store this srp = pb.SeedRandomParameter(self.plant) srp.seedPos = pb.Vector3d(0, 0, -2) srp.firstB = 3 # basal srp.delayB = 5 srp.maxB = 7 srp.nC = 7 srp.firstSB = 30 # shoot borne srp.delaySB = 10 srp.delayRC = 70 srp.nz = 0.7 srp.maxTil = 3 # stem srp.simtime = 10 self.plant.setOrganRandomParameter(srp) return srp
def test_radial_leaf_geometry(self): """ tests if a radial leaf geometry can be set""" self.plant = pb.Organism() lrp = pb.LeafRandomParameter(self.plant) lrp.la = 3.5 lrp.lb = 1. lrp.ln = 3. lrp.lmax = lrp.la + lrp.lb + lrp.ln lrp.areaMax = 50 # """ radial geometry """ # phi = np.array([-90, -45, 0., 45, 90]) / 180. * np.pi # l = np.array([3, 2.2, 1.7, 2, 3.5]) # N = 105 # N is rather high for testing # lrp.createLeafRadialGeometry(phi, l, N) lrp.la, lrp.lb, lrp.lmax, lrp.ln, lrp.r, lrp.dx = 5, 1, 11, 5, 1, 0.5 phi = np.array([-90., -67.5, -45, -22.5, 0, 22.5, 45, 67.5, 90]) / 180. * np.pi l = np.array([5., 1, 5, 1, 5, 1, 5, 1, 5]) assert(l.shape == phi.shape) N = 500 # N is rather high for testing lrp.createLeafRadialGeometry(phi, l, N) # self.assertEqual(lrp.leafMid(), 3., "unexpected leaf mid") # self.assertEqual(lrp.leafLength(), 6.5, "unexpected leaf length") yy = np.linspace(0, lrp.leafLength(), N) geom = lrp.leafGeometry x_, y_ = [], [] for i, x in enumerate(geom): # if len(x) > 1: # print(len(x), x, [yy[i]] * len(x)) # if len(x) == 1: # print(len(x), x, [yy[i]] * len(x)) x_.extend(x) y_.extend([yy[i]] * len(x)) x_ = np.array(x_) y_ = np.array(y_) # self.assertEqual(x_.shape[0], y_.shape[0], "leaf geometry has wrong size"); a = lrp.areaMax / lrp.leafLength() # self.assertAlmostEqual(2 * np.sum(x_) / N * a * lrp.leafLength(), lrp.areaMax , 2, "xml: value unexpected") plt.plot(x_ * a, y_, "g*") plt.plot(-x_ * a, y_, "g*") plt.ylim([0, 10]) plt.xlim([-7.5, 7.5]) plt.show()
def test_constructors(self): """ tests constructor and copy """ plant = pb.Organism() srp = pb.SeedRandomParameter(plant) srp.firstB = 123 srp.firstBs = 456 srp.nz = 789 srp2 = srp.copy(plant) self.assertIsNot(srp, srp2, "copy: organ type parameter set is not copied") self.assertEqual(srp2.name, srp.name, "copy: value unexpected") self.assertEqual(srp2.organType, srp.organType, "copy: value unexpected") self.assertEqual(srp2.subType, srp.subType, "copy: value unexpected") self.assertEqual(srp2.firstB, srp.firstB, "copy: value unexpected") self.assertEqual(srp2.firstBs, srp.firstBs, "copy: value unexpected") self.assertEqual(srp2.nz, srp.nz, "copy: value unexpected")