def add(*matrices): if not equal(matrices, len): raise ValueError("Given matrices are not the same size.") s = [] for rows in zip(*matrices): if not equal(rows, len): raise ValueError("Given matrices are not the same size.") t = [] for elems in zip(*rows): t.append(sum(elems)) s.append(t) return s
def clean(): args = get_args_clean() import os full_path = os.path.abspath(args["json_file"]) folder = os.path.dirname(full_path) all_imgs = utils.open_json(args["json_file"]) filtered = utils.filter_by_type(os.listdir(folder), "_c.jpg") clean_json = [] for img in all_imgs: for one in filtered: if utils.equal(img["file_name"], one): clean_json.append(img) break utils.save_json(clean_json, args["outfile"])
def inplace_rotate(image): assert(utils.equal(*image.shape)) #NxN N = image.shape[0] #rotate 90degrees = change coordinatesystem (x,y)->(y,N-x) def rotate_pixel_quartet(image, x, y): """rotates a pixel quartet, i that is all 4pixels that needs to be moved ( x, y),( y,N-x),(N-x,N-y),(N-y, x) //coordinate change 3 times """ first_pixel = image[x,y] #copy backwards to save a few temporary variables image[ x, y] = image[N-y-1, x] image[N-y-1, x] = image[N-x-1,N-y-1] image[N-x-1,N-y-1] = image[ y,N-x-1] image[ y,N-x-1] = first_pixel rotate = ft.partial(rotate_pixel_quartet, image) # now only do the quartet rotate on a chunk like this (else you will rotate the same pixel multiple time) # # XXXX000 # XXXX000 # XXXX000 # 0000000 # 0000000 # 0000000 # 0000000 # # the box((0,0),(ceil(N/2.0),N//2)) show with 'X' works for both odd and even N # # added bonus: the center will not be copied at all if N is odd, and it is stationary list(it.starmap( rotate, it.product( range(ceil(N/2.0)), range(N//2) ) ))
def test_norm(self): p = viennagrid.wrapper.PointCartesian3D(1, 2, 3) self.assertTrue(equal(p.norm_1(), 6)) self.assertTrue(equal(p.norm_2(), 3.74)) self.assertTrue(equal(p.norm_inf(), 3))
def patternmatch(pattern, expr, state): """ `expr` should've been evaluated, `pattern` should not yet have been evaluated. Keep in mind that syntactially, 'pattern' can be any expression, like a function definition or whatever, even though patternmatching only works on simpler things like numbers and tuples. """ make_exception = lambda s: utils.InvalidMatch("Invalid match ({})".format(s), state.newinfonode(pattern)) if pattern._type == 'uminus' and pattern.content._type == 'number': # Special rule for negative numbers, otherwise I wouldn't be able to match # them. pattern = pattern.content pattern.content = -pattern.content if pattern._type == 'list-literal': return patternmatch(utils.transform_list_literal(pattern.content, infonode=pattern), expr, state) elif state.env.is_unbound_identifier(pattern): if state.readonly: raise MalangError("Cannot bind identifier {!r} in pattern in readonly mode".format(pattern.content), state.newinfonode(pattern)) else: state.env.bind(pattern.content, expr) return elif pattern._type == 'id' and state.env.is_bound(pattern.content): val = state.env.get(pattern.content, state.newinfonode(pattern)) if not utils.equal(val, expr): raise make_exception("identifier {!r} is already bound".format(pattern.content)) return elif pattern._type == 'cons': if expr._type != 'list': raise make_exception("tried to match something of type {} agains a cons pattern".format(expr._type)) elif utils.is_nil(expr): raise make_exception("tried to match the empty list against a cons pattern") patternmatch(pattern.content[0], expr.content[0], state) patternmatch(pattern.content[1], expr.content[1], state) return elif pattern._type == 'module_access': module, access = pattern.content if not (module._type == 'id' and state.env.is_bound(module.content) and state.env.get(module.content, state.newinfonode(pattern))._type == 'module'): raise MalangError("Invalid module in pattern", state.newinfonode(pattern)) else: return patternmatch(access, expr, state.newenv( state.env.get(module.content, state.newinfonode(pattern)).content).newreadonly(True)) elif pattern._type != expr._type: raise make_exception( "tried to match something of type {} agains a pattern of type {}".format(expr._type, pattern._type)) elif pattern._type == 'list': pattern_nil, expr_nil = utils.is_nil(pattern), utils.is_nil(expr) if pattern_nil and expr_nil: return elif pattern_nil or expr_nil: if pattern_nil: raise make_exception("tried to match a list of 1 or more elements agains the empty list pattern") elif expr_nil: raise make_exception("tried to match the empty list agains a pattern-list of 1 or more elements") patternmatch(pattern.content[0], expr.content[0], state) patternmatch(pattern.content[1], expr.content[1], state) return elif pattern._type == 'tuple': pattern_len = len(pattern.content) expr_len = len(expr.content) if not pattern_len == expr_len: raise make_exception( "tried to match a tuple of length {} agains a pattern-tuple of length {}".format(expr_len, pattern_len)) for pat, e in zip(pattern.content, expr.content): patternmatch(pat, e, state) return elif pattern._type in ('number', 'str', 'atom'): if not pattern.content == expr.content: raise make_exception( "tried to match the {} {!r} agains the pattern-{} {!r}".format(expr._type, expr.content, pattern._type, pattern.content)) return raise MalangError("Don't know how to pattern-match that. ", state.newinfonode(pattern))
arithmetic_funcs = { 'plus': operator.add, 'minus': operator.sub, 'divide': operator.floordiv, 'times': operator.mul, 'modulo': operator.mod, 'pow': operator.pow } cmp_funcs = { 'gt': utils.greater_than, 'lt': utils.less_than, 'ge': utils.greater_than_or_eq, 'le': utils.less_than_or_eq, 'eq': utils.equal, 'ne': lambda op1, op2: not utils.equal(op1, op2) } def thunk(func, *args, **kwargs): def _f(): return func(*args, **kwargs) return _f def trampoline(expr, state): """ This trampoline function is necessary for the purpose of implementing tail call elimination. When `maval' is about to evaluate an expression in tail position, it returns a thunk instead of recursing deeper into itself, thus making sure that the python call stack doesn't grow huge. So it's like `maval' is 'jumping' on this trampoline. I guess. """ result = maval(expr, state)
def test_init(self): """ Test constructor with all possible argument combinations. Valid signatures are: Point(dim) Point(coord_system, dim) Point(x) Point(x, coord_system) Point(x, dim) Point(x, coord_system, dim) Point(x, y) Point(x, y, coord_system) Point(x, y, dim) Point(x, y, coord_system, dim) Point(x, y, z) Point(x, y, z, coord_system) Point(x, y, z, dim) Point(x, y, z, coord_system, dim) """ ########################################################################################### """ The following signatures must fail: Point() """ x = 0 y = 0 z = 0 self.assertRaises(TypeError, viennagrid.Point) ########################################################################################### """ Test signature: Point(dim) """ p = viennagrid.Point(dim=1) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 1) self.assertTrue(*equal(p.coords[0], x)) p = viennagrid.Point(dim=2) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) p = viennagrid.Point(dim=3) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) self.assertRaises(ValueError, viennagrid.Point, dim=0) self.assertRaises(ValueError, viennagrid.Point, dim=4) self.assertRaises(TypeError, viennagrid.Point, dim='') self.assertRaises(TypeError, viennagrid.Point, dim=[]) self.assertRaises(TypeError, viennagrid.Point, dim=long(1)) self.assertRaises(TypeError, viennagrid.Point, dim=float(1)) self.assertRaises(TypeError, viennagrid.Point, dim=complex(1)) ########################################################################################### """ Test signature: Point(coord_system) This must fail for coord_system=CARTESIAN, because 'dim' is unknown. However, for coord_system=POLAR, coord_system=SPHERICAL and coord_system=CYLINDRICAL it must work, because 'dim' is unambiguous. """ self.assertRaises(TypeError, viennagrid.Point, coord_system=viennagrid.config.CARTESIAN) p = viennagrid.Point(coord_system=viennagrid.config.POLAR) self.assertEqual(p.coord_system, viennagrid.config.POLAR) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) p = viennagrid.Point(coord_system=viennagrid.config.SPHERICAL) self.assertEqual(p.coord_system, viennagrid.config.SPHERICAL) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) p = viennagrid.Point(coord_system=viennagrid.config.CYLINDRICAL) self.assertEqual(p.coord_system, viennagrid.config.CYLINDRICAL) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) self.assertRaises(ValueError, viennagrid.Point, coord_system='') self.assertRaises(TypeError, viennagrid.Point, coord_system=[]) self.assertRaises(TypeError, viennagrid.Point, coord_system=int(1)) ########################################################################################### """ Test signature: Point(coord_system, dim) """ p = viennagrid.Point(coord_system=viennagrid.config.CARTESIAN, dim=1) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 1) self.assertTrue(*equal(p.coords[0], x)) p = viennagrid.Point(coord_system=viennagrid.config.CARTESIAN, dim=2) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) p = viennagrid.Point(coord_system=viennagrid.config.CARTESIAN, dim=3) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) p = viennagrid.Point(coord_system=viennagrid.config.POLAR, dim=2) self.assertEqual(p.coord_system, viennagrid.config.POLAR) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) p = viennagrid.Point(coord_system=viennagrid.config.SPHERICAL, dim=3) self.assertEqual(p.coord_system, viennagrid.config.SPHERICAL) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) p = viennagrid.Point(coord_system=viennagrid.config.CYLINDRICAL, dim=3) self.assertEqual(p.coord_system, viennagrid.config.CYLINDRICAL) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.CARTESIAN, dim=0) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.CARTESIAN, dim=4) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.POLAR, dim=1) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.POLAR, dim=3) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.SPHERICAL, dim=1) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.SPHERICAL, dim=2) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.SPHERICAL, dim=4) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.CYLINDRICAL, dim=1) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.CYLINDRICAL, dim=2) self.assertRaises(ValueError, viennagrid.Point, coord_system=viennagrid.config.CYLINDRICAL, dim=4) ########################################################################################### """ Test signature: Point(x) """ x = 15 y = 83 z = 49 p = viennagrid.Point(x) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 1) self.assertTrue(*equal(p.coords[0], x)) ########################################################################################### """ Test signature: Point(x, dim) """ p = viennagrid.Point(x, dim=1) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 1) self.assertTrue(*equal(p.coords[0], x)) self.assertRaises(ValueError, viennagrid.Point, x, dim=0) self.assertRaises(ValueError, viennagrid.Point, x, dim=2) self.assertRaises(ValueError, viennagrid.Point, x, dim=3) self.assertRaises(ValueError, viennagrid.Point, x, dim=0) self.assertRaises(ValueError, viennagrid.Point, x, dim=4) self.assertRaises(TypeError, viennagrid.Point, x, dim='') self.assertRaises(TypeError, viennagrid.Point, x, dim=[]) self.assertRaises(TypeError, viennagrid.Point, x, dim=long(1)) self.assertRaises(TypeError, viennagrid.Point, x, dim=float(1)) self.assertRaises(TypeError, viennagrid.Point, x, dim=complex(1)) ########################################################################################### """ Test signature: Point(x, coord_system) """ p = viennagrid.Point(x, coord_system=viennagrid.config.CARTESIAN) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 1) self.assertTrue(*equal(p.coords[0], x)) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.SPHERICAL) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.POLAR) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.CYLINDRICAL) self.assertRaises(ValueError, viennagrid.Point, x, coord_system='') self.assertRaises(TypeError, viennagrid.Point, x, coord_system=[]) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=int(1)) ########################################################################################### """ Test signature: Point(x, coord_system, dim) """ p = viennagrid.Point(x, coord_system=viennagrid.config.CARTESIAN, dim=1) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 1) self.assertTrue(*equal(p.coords[0], x)) self.assertRaises(ValueError, viennagrid.Point, x, coord_system=viennagrid.config.CARTESIAN, dim=0) self.assertRaises(ValueError, viennagrid.Point, x, coord_system=viennagrid.config.CARTESIAN, dim=2) self.assertRaises(ValueError, viennagrid.Point, x, coord_system=viennagrid.config.CARTESIAN, dim=3) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.SPHERICAL, dim=1) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.SPHERICAL, dim=2) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.SPHERICAL, dim=3) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.CYLINDRICAL, dim=1) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.CYLINDRICAL, dim=2) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.CYLINDRICAL, dim=3) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.POLAR, dim=1) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.POLAR, dim=2) self.assertRaises(TypeError, viennagrid.Point, x, coord_system=viennagrid.config.POLAR, dim=3) ########################################################################################### """ Test signature: Point(x, y) """ x = 15 y = 83 z = 49 p = viennagrid.Point(x, y) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) ########################################################################################### """ Test signature: Point(x, y, dim) """ p = viennagrid.Point(x, y, dim=2) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertRaises(ValueError, viennagrid.Point, x, y, dim=3) self.assertRaises(ValueError, viennagrid.Point, x, y, dim=0) self.assertRaises(ValueError, viennagrid.Point, x, y, dim=1) self.assertRaises(ValueError, viennagrid.Point, x, y, dim=4) self.assertRaises(TypeError, viennagrid.Point, x, y, dim='') self.assertRaises(TypeError, viennagrid.Point, x, y, dim=[]) self.assertRaises(TypeError, viennagrid.Point, x, y, dim=long(1)) self.assertRaises(TypeError, viennagrid.Point, x, y, dim=float(1)) self.assertRaises(TypeError, viennagrid.Point, x, y, dim=complex(1)) ########################################################################################### """ Test signature: Point(x, y, coord_system) """ p = viennagrid.Point(x, y, coord_system=viennagrid.config.CARTESIAN) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) p = viennagrid.Point(x, y, coord_system=viennagrid.config.POLAR) self.assertEqual(p.coord_system, viennagrid.config.POLAR) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.SPHERICAL) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.CYLINDRICAL) self.assertRaises(ValueError, viennagrid.Point, x, y, coord_system='') self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=[]) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=int(1)) ########################################################################################### """ Test signature: Point(x, y, coord_system, dim) """ p = viennagrid.Point(x, y, coord_system=viennagrid.config.CARTESIAN, dim=2) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertRaises(ValueError, viennagrid.Point, x, y, coord_system=viennagrid.config.CARTESIAN, dim=3) p = viennagrid.Point(x, y, coord_system=viennagrid.config.POLAR, dim=2) self.assertEqual(p.coord_system, viennagrid.config.POLAR) self.assertEqual(p.dim, 2) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.SPHERICAL, dim=3) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.CYLINDRICAL, dim=3) self.assertRaises(ValueError, viennagrid.Point, x, y, coord_system=viennagrid.config.CARTESIAN, dim=1) self.assertRaises(ValueError, viennagrid.Point, x, y, coord_system=viennagrid.config.CARTESIAN, dim=4) self.assertRaises(ValueError, viennagrid.Point, x, y, coord_system=viennagrid.config.POLAR, dim=1) self.assertRaises(ValueError, viennagrid.Point, x, y, coord_system=viennagrid.config.POLAR, dim=3) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.SPHERICAL, dim=1) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.SPHERICAL, dim=2) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.SPHERICAL, dim=4) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.CYLINDRICAL, dim=1) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.CYLINDRICAL, dim=2) self.assertRaises(TypeError, viennagrid.Point, x, y, coord_system=viennagrid.config.CYLINDRICAL, dim=4) ########################################################################################### """ Test signature: Point(x, y, z) """ p = viennagrid.Point(x, y, z) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) ########################################################################################### """ Test signature: Point(x, y, z, dim) """ self.assertRaises(ValueError, viennagrid.Point, x, y, z, dim=2) p = viennagrid.Point(x, y, z, dim=3) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) self.assertRaises(ValueError, viennagrid.Point, x, y, z, dim=0) self.assertRaises(ValueError, viennagrid.Point, x, y, z, dim=1) self.assertRaises(ValueError, viennagrid.Point, x, y, z, dim=4) self.assertRaises(TypeError, viennagrid.Point, x, y, z, dim='') self.assertRaises(TypeError, viennagrid.Point, x, y, z, dim=[]) self.assertRaises(TypeError, viennagrid.Point, x, y, z, dim=long(1)) self.assertRaises(TypeError, viennagrid.Point, x, y, z, dim=float(1)) self.assertRaises(TypeError, viennagrid.Point, x, y, z, dim=complex(1)) ########################################################################################### """ Test signature: Point(x, y, z, coord_system) """ p = viennagrid.Point(x, y, z, coord_system=viennagrid.config.CARTESIAN) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) self.assertRaises(TypeError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.POLAR) p = viennagrid.Point(x, y, z, coord_system=viennagrid.config.SPHERICAL) self.assertEqual(p.coord_system, viennagrid.config.SPHERICAL) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) p = viennagrid.Point(x, y, z, coord_system=viennagrid.config.CYLINDRICAL) self.assertEqual(p.coord_system, viennagrid.config.CYLINDRICAL) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system='') self.assertRaises(TypeError, viennagrid.Point, x, y, z, coord_system=[]) self.assertRaises(TypeError, viennagrid.Point, x, y, z, coord_system=int(1)) ########################################################################################### """ Test signature: Point(x, y, z, coord_system, dim) """ self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.CARTESIAN, dim=2) p = viennagrid.Point(x, y, z, coord_system=viennagrid.config.CARTESIAN, dim=3) self.assertEqual(p.coord_system, viennagrid.config.CARTESIAN) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) self.assertRaises(TypeError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.POLAR, dim=2) p = viennagrid.Point(x, y, z, coord_system=viennagrid.config.SPHERICAL, dim=3) self.assertEqual(p.coord_system, viennagrid.config.SPHERICAL) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) p = viennagrid.Point(x, y, z, coord_system=viennagrid.config.CYLINDRICAL, dim=3) self.assertEqual(p.coord_system, viennagrid.config.CYLINDRICAL) self.assertEqual(p.dim, 3) self.assertTrue(*equal(p.coords[0], x)) self.assertTrue(*equal(p.coords[1], y)) self.assertTrue(*equal(p.coords[2], z)) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.CARTESIAN, dim=1) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.CARTESIAN, dim=4) self.assertRaises(TypeError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.POLAR, dim=1) self.assertRaises(TypeError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.POLAR, dim=3) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.SPHERICAL, dim=1) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.SPHERICAL, dim=2) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.SPHERICAL, dim=4) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.CYLINDRICAL, dim=1) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.CYLINDRICAL, dim=2) self.assertRaises(ValueError, viennagrid.Point, x, y, z, coord_system=viennagrid.config.CYLINDRICAL, dim=4)
def test_mean_extractor_data(): # Liverpool - Manchester Utd 1:0 (1:0, 0:0) # Swansea - Liverpool 2:2 (1:2, 1:0) # Liverpool - Southampton 0:1 (0:0, 0:1) # Sunderland - Liverpool 1:3 (0:2, 1:1) # Liverpool - Crystal Palace 3:1 (3:0, 0:1) # Newcastle - Liverpool 2:2 (1:1, 1:1) # Liverpool - West Brom 4:1 (2:0, 2:1) # Arsenal - Liverpool 2:0 (1:0, 1:0) # Liverpool - Fulham 4:0 (3:0, 1:0) all_data = select_matches([ 'W8tiZmre', 'b5ZpFZlH', 'Y7HP6S3Q', 'KSHwEqBg', 'l89fA1AI', 'plSc5COj', 'tzC0PXFM', '8MZNtZpc', '0b78hWq9' ]) all_data['real_outcome_o1x2'] = all_data.apply(get_outcome_o1x2, axis=1) extractor = MeanExtractor() _, extracted_data = extractor.extract(all_data) expected = [ (np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan), (np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan), (1, np.nan, 1, 0, 0, np.nan, np.nan, np.nan), (np.nan, 2, np.nan, np.nan, np.nan, 0, 1, 0), (1 / 2, np.nan, 1 / 2, 0, 1 / 2, np.nan, np.nan, np.nan), (np.nan, 5 / 2, np.nan, np.nan, np.nan, 1 / 2, 1 / 2, 0), (4 / 3, np.nan, 2 / 3, 0, 1 / 3, np.nan, np.nan, np.nan), (np.nan, 7 / 3, np.nan, np.nan, np.nan, 1 / 3, 2 / 3, 0), (2, np.nan, 3 / 4, 0, 1 / 4, np.nan, np.nan, np.nan), ] for i, (total_score_home, total_score_away, w_prob_home, d_prob_home, l_prob_home, w_prob_away, d_prob_away, l_prob_away) in enumerate(expected): equal( extracted_data.loc[i, 'cummean_total_score_home_match_team_home'], total_score_home) equal( extracted_data.loc[i, 'cummean_total_score_away_match_team_away'], total_score_away) equal(extracted_data.loc[i, 'cummean_w_prob_home_match_team_home'], w_prob_home) equal(extracted_data.loc[i, 'cummean_d_prob_home_match_team_home'], d_prob_home) equal(extracted_data.loc[i, 'cummean_l_prob_home_match_team_home'], l_prob_home) equal(extracted_data.loc[i, 'cummean_w_prob_away_match_team_away'], w_prob_away) equal(extracted_data.loc[i, 'cummean_d_prob_away_match_team_away'], d_prob_away) equal(extracted_data.loc[i, 'cummean_l_prob_away_match_team_away'], l_prob_away)