def test_member_value(self): """Test that module Members of Value work correctly. As Variable?""" M = Module() x = T.dscalar() M.y = T.value(40) M.f = Method([x], x + 2 * M.y) m = M.make() m.y = 80 assert m.f(20) == 180
def test_tuple_assign(self): """Test that list members can be assigned tuple-wise""" def local_test(x,y): m1=Module() m1.l=(x(), y()) # create a Method that makes the second list element a shared Member m1.g=Method([], m1.l[0]) m1.f=Method([], m1.l[1]) m = m1.make() #assign 4 and 5 to the two variables' containers in m m.l = (4, 5) assert 5 == m.f() assert 4 == m.g() local_test(lambda:T.dscalar(),lambda:T.dscalar()) local_test(lambda:T.value(1),lambda:T.value(2))
def test_tuple_assign(self): """Test that list members can be assigned tuple-wise""" def local_test(x, y): m1 = Module() m1.l = (x(), y()) # create a Method that makes the second list element a shared Member m1.g = Method([], m1.l[0]) m1.f = Method([], m1.l[1]) m = m1.make() #assign 4 and 5 to the two variables' containers in m m.l = (4, 5) assert 5 == m.f() assert 4 == m.g() local_test(lambda: T.dscalar(), lambda: T.dscalar()) local_test(lambda: T.value(1), lambda: T.value(2))
def test_dict_assign(self): """Test that list members can be assigned dict-wise""" def local_test(x,y): m1=Module() ##DICT m1.l={'x':x(), 'y':y()} # create a Method that makes the second list element a shared Member m1.f=Method([], m1.l['y']) m1.g=Method([], m1.l['x']) m = m1.make() #assign 4 and 5 to the two variables' containers in m m.l = dict(x=4, y=5) assert 5 == m.f() assert 4 == m.g() #print 'dscalar test' local_test(lambda:T.dscalar(),lambda:T.dscalar()) #print 'value test' local_test(lambda:T.value(1),lambda:T.value(2))
def test_list_assign(self): """Test that list members can be assigned list-wise""" def local_test(x,y): m1=Module() #create a list with some variables in it m1.l=[x(), y()] # create a Method that makes the second list element a shared Member m1.f=Method([], m1.l[1]) m1.g=Method([], m1.l[0]) m = m1.make() #assign 4 and 5 to the two variables' containers in m m.l = [4, 5] m.f() assert numpy.all(5 == m.f()) assert numpy.all(4 == m.g()) local_test(lambda:T.dscalar(),lambda:T.dscalar()) local_test(lambda:T.value(1),lambda:T.value(2))
def test_dict_assign(self): """Test that list members can be assigned dict-wise""" def local_test(x, y): m1 = Module() ##DICT m1.l = {'x': x(), 'y': y()} # create a Method that makes the second list element a shared Member m1.f = Method([], m1.l['y']) m1.g = Method([], m1.l['x']) m = m1.make() #assign 4 and 5 to the two variables' containers in m m.l = dict(x=4, y=5) assert 5 == m.f() assert 4 == m.g() #print 'dscalar test' local_test(lambda: T.dscalar(), lambda: T.dscalar()) #print 'value test' local_test(lambda: T.value(1), lambda: T.value(2))
def test_list_assign(self): """Test that list members can be assigned list-wise""" def local_test(x, y): m1 = Module() #create a list with some variables in it m1.l = [x(), y()] # create a Method that makes the second list element a shared Member m1.f = Method([], m1.l[1]) m1.g = Method([], m1.l[0]) m = m1.make() #assign 4 and 5 to the two variables' containers in m m.l = [4, 5] m.f() assert numpy.all(5 == m.f()) assert numpy.all(4 == m.g()) local_test(lambda: T.dscalar(), lambda: T.dscalar()) local_test(lambda: T.value(1), lambda: T.value(2))
def test_members_in_list_tuple_or_dict(self): """Test that a Member which is only included via a list, tuple or dictionary is still treated as if it were a toplevel attribute and not shared """ def local_test(x,y): m1=Module() m1.x=x() m1.y=y() m1.emtpylist = [] m1.lx=[x()]#cast Variable] m1.ly=[y()] m1.llx=[[x()]]#cast Variable] m1.lly=[[y()]] m1.ltx=[(x(),)] m1.lty=[(y(),)] m1.ldx=[{"x":x()}] m1.ldy=[{"y":y()}] m1.tx=(x(),) m1.ty=(y(),) m1.tlx=[(x(),)] m1.tly=[(y(),)] m1.ttx=((x(),),) m1.tty=((y(),),) m1.tdx=({"x":x()},) m1.tdy=({"y":y()},) m1.dx={"x":x()} m1.dy={"y":y()} m1.dlx={"x":[x()]} m1.dly={"y":[y()]} m1.dtx={"x":(x(),)} m1.dty={"y":(y(),)} m1.ddx={"x":{"x":x()}} m1.ddy={"y":{"y":y()}} assert isinstance(m1.x,(gof.Variable)) assert isinstance(m1.y,(gof.Variable)) for i, obj in enumerate([ m1.lx[0], #0 m1.llx[0][0], m1.ltx[0][0], m1.ldx[0]['x'], m1.lty[0][0],#5 m1.ldy[0]['y'], m1.ly[0], m1.lly[0][0], m1.tx[0], #8 m1.ty[0], m1.tlx[0][0], m1.tly[0][0], m1.ttx[0][0], m1.tty[0][0], m1.tdx[0]['x'], m1.tdy[0]['y'], m1.dx['x'], m1.dy['y'], m1.dlx['x'][0], m1.dly['y'][0], m1.dtx['x'][0], m1.dty['y'][0], m1.ddx['x']['x'], m1.ddy['y']['y']]): assert isinstance(obj,(gof.Variable)) inst=m1.make() def get_l(): return [inst.lx, inst.ly, inst.tx, inst.ty, inst.dx, inst.dy, inst.llx, inst.lly, inst.ltx, inst.lty, inst.ldx, inst.ldy, inst.tlx, inst.tly, inst.ttx, inst.tty, inst.tdx, inst.tdy, inst.dly, inst.dlx, inst.dty, inst.dtx, inst.ddy, inst.ddx] def get_l2(): # return [inst.lx[0], inst.ly[0], inst.tx[0], inst.ty[0], inst.dx['x'], inst.dy['y'], inst.llx[0][0], inst.lly[0][0], inst.ltx[0][0], inst.lty[0][0], inst.ldx[0]['x'], inst.ldy[0]['y'], inst.tlx[0][0], inst.tly[0][0], inst.ttx[0][0], inst.tty[0][0], inst.tdx, inst.tdy, inst.dly, inst.dlx, inst.dty, inst.dtx, inst.ddy, inst.ddx] return [inst.lx, inst.ly, inst.tx, inst.ty, inst.llx[0], inst.lly[0], inst.ltx[0], inst.lty[0], inst.ldx[0], inst.ldy[0], inst.tlx[0], inst.tly[0], inst.ttx[0], inst.tty[0], inst.tdx[0], inst.tdy[0], inst.dly['y'], inst.dlx['x'], inst.dty['y'], inst.dtx['x']]#, inst.ddy['y'], inst.ddx['x']] #test that we can access the data inst.x inst.y for i in get_l(): assert i #test that we can set a value to the data the get this value if not isinstance(m1.x, gof.Constant): inst.x=-1 inst.y=-2 inst.ldx[0]['x']=-3 inst.ldy[0]['y']=-4 inst.tdx[0]['x']=-5 inst.tdy[0]['y']=-6 inst.ddx['x']['x']=-7 inst.ddy['y']['y']=-8 for i,j in zip(get_l2(),range(len(get_l2()))): i[0]=j assert inst.x==-1 assert inst.y==-2 assert inst.ldx[0]['x']==-3 assert inst.ldy[0]['y']==-4 assert inst.tdx[0]['x']==-5 assert inst.tdy[0]['y']==-6 assert inst.ddx['x']['x']==-7 assert inst.ddy['y']['y']==-8 for i,j in zip(get_l2(),range(len(get_l2()))): assert i[0]==j local_test(lambda:T.dscalar(),lambda:T.dscalar()) local_test(lambda:T.value(1),lambda:T.value(2)) local_test(lambda:T.constant(1),lambda:T.constant(2))
def __init__(self, stepsize): super(Blah, self).__init__() self.stepsize = T.value(stepsize) x = T.dscalar() self.step = Method([x], x - self.stepsize)
def test_members_in_list_tuple_or_dict(self): """Test that a Member which is only included via a list, tuple or dictionary is still treated as if it were a toplevel attribute and not shared """ def local_test(x, y): m1 = Module() m1.x = x() m1.y = y() m1.emtpylist = [] m1.lx = [x()] #cast Variable] m1.ly = [y()] m1.llx = [[x()]] #cast Variable] m1.lly = [[y()]] m1.ltx = [(x(), )] m1.lty = [(y(), )] m1.ldx = [{"x": x()}] m1.ldy = [{"y": y()}] m1.tx = (x(), ) m1.ty = (y(), ) m1.tlx = [(x(), )] m1.tly = [(y(), )] m1.ttx = ((x(), ), ) m1.tty = ((y(), ), ) m1.tdx = ({"x": x()}, ) m1.tdy = ({"y": y()}, ) m1.dx = {"x": x()} m1.dy = {"y": y()} m1.dlx = {"x": [x()]} m1.dly = {"y": [y()]} m1.dtx = {"x": (x(), )} m1.dty = {"y": (y(), )} m1.ddx = {"x": {"x": x()}} m1.ddy = {"y": {"y": y()}} assert isinstance(m1.x, (gof.Variable)) assert isinstance(m1.y, (gof.Variable)) for i, obj in enumerate([ m1.lx[0], #0 m1.llx[0][0], m1.ltx[0][0], m1.ldx[0]['x'], m1.lty[0][0], #5 m1.ldy[0]['y'], m1.ly[0], m1.lly[0][0], m1.tx[0], #8 m1.ty[0], m1.tlx[0][0], m1.tly[0][0], m1.ttx[0][0], m1.tty[0][0], m1.tdx[0]['x'], m1.tdy[0]['y'], m1.dx['x'], m1.dy['y'], m1.dlx['x'][0], m1.dly['y'][0], m1.dtx['x'][0], m1.dty['y'][0], m1.ddx['x']['x'], m1.ddy['y']['y'] ]): assert isinstance(obj, (gof.Variable)) inst = m1.make() def get_l(): return [ inst.lx, inst.ly, inst.tx, inst.ty, inst.dx, inst.dy, inst.llx, inst.lly, inst.ltx, inst.lty, inst.ldx, inst.ldy, inst.tlx, inst.tly, inst.ttx, inst.tty, inst.tdx, inst.tdy, inst.dly, inst.dlx, inst.dty, inst.dtx, inst.ddy, inst.ddx ] def get_l2(): # return [inst.lx[0], inst.ly[0], inst.tx[0], inst.ty[0], inst.dx['x'], inst.dy['y'], inst.llx[0][0], inst.lly[0][0], inst.ltx[0][0], inst.lty[0][0], inst.ldx[0]['x'], inst.ldy[0]['y'], inst.tlx[0][0], inst.tly[0][0], inst.ttx[0][0], inst.tty[0][0], inst.tdx, inst.tdy, inst.dly, inst.dlx, inst.dty, inst.dtx, inst.ddy, inst.ddx] return [ inst.lx, inst.ly, inst.tx, inst.ty, inst.llx[0], inst.lly[0], inst.ltx[0], inst.lty[0], inst.ldx[0], inst.ldy[0], inst.tlx[0], inst.tly[0], inst.ttx[0], inst.tty[0], inst.tdx[0], inst.tdy[0], inst.dly['y'], inst.dlx['x'], inst.dty['y'], inst.dtx['x'] ] #, inst.ddy['y'], inst.ddx['x']] #test that we can access the data inst.x inst.y for i in get_l(): assert i #test that we can set a value to the data the get this value if not isinstance(m1.x, gof.Constant): inst.x = -1 inst.y = -2 inst.ldx[0]['x'] = -3 inst.ldy[0]['y'] = -4 inst.tdx[0]['x'] = -5 inst.tdy[0]['y'] = -6 inst.ddx['x']['x'] = -7 inst.ddy['y']['y'] = -8 for i, j in zip(get_l2(), range(len(get_l2()))): i[0] = j assert inst.x == -1 assert inst.y == -2 assert inst.ldx[0]['x'] == -3 assert inst.ldy[0]['y'] == -4 assert inst.tdx[0]['x'] == -5 assert inst.tdy[0]['y'] == -6 assert inst.ddx['x']['x'] == -7 assert inst.ddy['y']['y'] == -8 for i, j in zip(get_l2(), range(len(get_l2()))): assert i[0] == j local_test(lambda: T.dscalar(), lambda: T.dscalar()) local_test(lambda: T.value(1), lambda: T.value(2)) local_test(lambda: T.constant(1), lambda: T.constant(2))