def doTest(): print(colored('Testing Shape Class', 'magenta')) print(colored('Testing Default Constructor', 'cyan')) shape = NumCpp.Shape() if shape.rows == 0 and shape.cols == 0: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing Square Constructor', 'cyan')) shapeInput = np.random.randint(0, 100, [ 1, ]).item() shape = NumCpp.Shape(shapeInput) if shape.rows == shapeInput and shape.cols == shapeInput: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing Rectangle Constructor', 'cyan')) shapeInput = np.random.randint(0, 100, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) if shape.rows == shapeInput[0] and shape.cols == shapeInput[1]: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing Copy Constructor', 'cyan')) shape2 = NumCpp.Shape(shape) if shape2.rows == shape.rows and shape2.cols == shape.cols: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing Member Setting', 'cyan')) shape = NumCpp.Shape() shapeInput = np.random.randint(0, 100, [ 2, ]) shape.rows = shapeInput[0].item() shape.cols = shapeInput[1].item() if shape.rows == shapeInput[0] and shape.cols == shapeInput[1]: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing Print', 'cyan')) shape.print() print(colored('\tPASS', 'green'))
def test2D(): modes = { 'reflect': NumCpp.Mode.REFLECT, 'constant': NumCpp.Mode.CONSTANT, 'nearest': NumCpp.Mode.NEAREST, 'mirror': NumCpp.Mode.MIRROR, 'wrap': NumCpp.Mode.WRAP } for mode in modes.keys(): print( colored(f'Testing complementaryMedianFilter: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.complementaryMedianFilter( cArray, kernalSize, modes[mode], constantValue).getNumpyArray() dataOutPy = data - filters.median_filter( data, size=kernalSize, mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing convolve: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(10, 20, shape).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition weights = np.random.randint(-2, 3, [kernalSize, kernalSize]).astype(np.double) cWeights = NumCpp.NdArray(kernalSize) cWeights.setArray(weights) dataOutC = NumCpp.convolve(cArray, kernalSize, cWeights, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.convolve(data, weights, mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing gaussianFilter: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape).astype(np.double) cArray.setArray(data) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition sigma = np.random.rand(1).item() * 2 dataOutC = NumCpp.gaussianFilter(cArray, sigma, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.gaussian_filter(data, sigma, mode=mode, cval=constantValue) if np.array_equal(np.round(dataOutC, 2), np.round(dataOutPy, 2)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing maximumFilter: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.maximumFilter(cArray, kernalSize, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.maximum_filter(data, size=kernalSize, mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing medianFilter: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.medianFilter(cArray, kernalSize, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.median_filter(data, size=kernalSize, mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing minimumFilter: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.minimumFilter(cArray, kernalSize, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.minimum_filter(data, size=kernalSize, mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing percentileFilter: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) percentile = np.random.randint(0, 101, [ 1, ]).item() constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.percentileFilter(cArray, kernalSize, percentile, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.percentile_filter(data, percentile, size=kernalSize, mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing rankFilter: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) rank = np.random.randint(0, kernalSize**2 - 1, [ 1, ]).item() constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.rankFilter(cArray, kernalSize, rank, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.rank_filter(data, rank, size=kernalSize, mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing uniformFilter: mode = {mode}', 'cyan')) shape = np.random.randint(1000, 2000, [ 2, ]).tolist() cShape = NumCpp.Shape(shape[0], shape[1]) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, shape).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.uniformFilter(cArray, kernalSize, modes[mode], constantValue).getNumpyArray() dataOutPy = filters.uniform_filter(data, size=kernalSize, mode=mode, cval=constantValue) if np.array_equal(np.round(dataOutC, 8), np.round(dataOutPy, 8)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red'))
def test1D(): modes = { 'reflect': NumCpp.Mode.REFLECT, 'constant': NumCpp.Mode.CONSTANT, 'nearest': NumCpp.Mode.NEAREST, 'mirror': NumCpp.Mode.MIRROR, 'wrap': NumCpp.Mode.WRAP } for mode in modes.keys(): print( colored(f'Testing complementaryMedianFilter1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.complementaryMedianFilter1d( cArray, kernalSize, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = data - filters.generic_filter(data, np.median, footprint=np.ones([ kernalSize, ]), mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing convolve1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) weights = np.random.randint(1, 5, [ kernalSize, ]) cWeights = NumCpp.NdArray(1, kernalSize) cWeights.setArray(weights) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.convolve1d(cArray, cWeights, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.convolve(data, weights, mode=mode, cval=constantValue) if np.array_equal(np.round(dataOutC, 8), np.round(dataOutPy, 8)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing gaussianFilter1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) sigma = np.random.rand(1).item() * 2 constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.gaussianFilter1d( cArray, sigma, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.gaussian_filter(data, sigma, mode=mode, cval=constantValue) if np.array_equal(np.round(dataOutC, 7), np.round(dataOutPy, 7)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing maximumFilter1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.maximumFilter1d( cArray, kernalSize, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.generic_filter(data, np.max, footprint=np.ones([ kernalSize, ]), mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing medianFilter1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.medianFilter1d( cArray, kernalSize, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.generic_filter(data, np.median, footprint=np.ones([ kernalSize, ]), mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing minumumFilter1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.minumumFilter1d( cArray, kernalSize, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.generic_filter(data, np.min, footprint=np.ones([ kernalSize, ]), mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing percentileFilter1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) percentile = np.random.randint(0, 101, [ 1, ]).item() constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.percentileFilter1d( cArray, kernalSize, percentile, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.generic_filter(data, np.percentile, footprint=np.ones([ kernalSize, ]), mode=mode, cval=constantValue, extra_arguments=(percentile, )) if np.array_equal(np.round(dataOutC, 7), np.round(dataOutPy, 7)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing rankFilter1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) rank = np.random.randint(0, kernalSize - 1, [ 1, ]).item() constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.rankFilter1d( cArray, kernalSize, rank, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.rank_filter(data, rank, footprint=np.ones([ kernalSize, ]), mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored(f'Testing uniformFilter1d: mode = {mode}', 'cyan')) size = np.random.randint(1000, 2000, [ 1, ]).item() cShape = NumCpp.Shape(1, size) cArray = NumCpp.NdArray(cShape) data = np.random.randint(100, 1000, [ size, ]).astype(np.double) cArray.setArray(data) kernalSize = 0 while kernalSize % 2 == 0: kernalSize = np.random.randint(5, 15) constantValue = np.random.randint(0, 5, [ 1, ]).item() # only actaully needed for constant boundary condition dataOutC = NumCpp.uniformFilter1d( cArray, kernalSize, modes[mode], constantValue).getNumpyArray().flatten() dataOutPy = filters.generic_filter(data, np.mean, footprint=np.ones([ kernalSize, ]), mode=mode, cval=constantValue) if np.array_equal(dataOutC, dataOutPy): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red'))
def doTest(): print(colored('Testing Random Module', 'magenta')) # it is kind of hard to test randomness so my criteria for passing will # simply be whether or not it crashes print(colored('Testing bernoulli', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) p = np.random.rand() r = NumCpp.Random.bernoulli(inShape, p) print(colored('\tPASS', 'green')) print(colored('Testing beta', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) alpha = np.random.rand() beta = np.random.rand() r = NumCpp.Random.beta(inShape, alpha, beta) print(colored('\tPASS', 'green')) print(colored('Testing binomial', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) n = np.random.randint(1, 100, [1,]).item() p = np.random.rand() r = NumCpp.Random.binomial(inShape, n, p) print(colored('\tPASS', 'green')) print(colored('Testing chiSquare', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) dof = np.random.randint(1, 100, [1,]).item() r = NumCpp.Random.chiSquare(inShape, dof) print(colored('\tPASS', 'green')) print(colored('Testing choice: Single', 'cyan')) shapeInput = np.random.randint(1, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) r = NumCpp.Random.choiceSingle(cArray) print(colored('\tPASS', 'green')) print(colored('Testing choice: Multiple', 'cyan')) shapeInput = np.random.randint(1, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.rand(shape.rows, shape.cols) cArray.setArray(data) num = np.random.randint(1, data.size, [1,]).item() r = NumCpp.Random.choiceMultiple(cArray, num) if r.size == num: print(colored('\tPASS', 'green')) print(colored('Testing cauchy', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) mean = np.random.randn() * 10 sigma = np.random.rand() * 10 r = NumCpp.Random.cauchy(inShape, mean, sigma) print(colored('\tPASS', 'green')) print(colored('Testing discrete', 'cyan')) shapeInput = np.random.randint(1, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) weights = np.random.randint(1, 10, [shape.rows, shape.cols]) cArray.setArray(weights) r = NumCpp.Random.discrete(inShape, cArray) print(colored('\tPASS', 'green')) print(colored('Testing exponential', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) scale = np.random.rand() * 10 r = NumCpp.Random.exponential(inShape, scale) print(colored('\tPASS', 'green')) print(colored('Testing extremeValue', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) a = np.random.rand() * 10 b = np.random.rand() * 100 r = NumCpp.Random.extremeValue(inShape, a, b) print(colored('\tPASS', 'green')) print(colored('Testing f', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) dofN = np.random.rand() * 10 dofD = np.random.rand() * 100 r = NumCpp.Random.f(inShape, dofN, dofD) print(colored('\tPASS', 'green')) print(colored('Testing gamma', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) shape = np.random.rand() * 10 scale = np.random.rand() * 100 r = NumCpp.Random.gamma(inShape, shape, scale) print(colored('\tPASS', 'green')) print(colored('Testing geometric', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) p = np.random.rand() r = NumCpp.Random.geometric(inShape, p) print(colored('\tPASS', 'green')) print(colored('Testing laplace', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) loc = np.random.rand() * 10 scale = np.random.rand() * 100 r = NumCpp.Random.laplace(inShape, loc, scale) print(colored('\tPASS', 'green')) print(colored('Testing lognormal', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) mean = np.random.randn() * 10 sigma = np.random.rand() * 10 r = NumCpp.Random.lognormal(inShape, mean, sigma) print(colored('\tPASS', 'green')) print(colored('Testing negativeBinomial', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) n = np.random.randint(1, 100, [1,]).item() p = np.random.rand() r = NumCpp.Random.negativeBinomial(inShape, n, p) print(colored('\tPASS', 'green')) print(colored('Testing nonCentralChiSquared', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) k = np.random.rand() * 10 l = np.random.rand() * 100 r = NumCpp.Random.nonCentralChiSquared(inShape, k, l) print(colored('\tPASS', 'green')) print(colored('Testing normal', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) mean = np.random.randn() * 10 sigma = np.random.rand() * 10 r = NumCpp.Random.normal(inShape, mean, sigma) print(colored('\tPASS', 'green')) print(colored('Testing permutation scalar', 'cyan')) r = NumCpp.Random.permutationScaler(np.random.randint(1,100, [1,]).item()) print(colored('\tPASS', 'green')) print(colored('Testing permutation array', 'cyan')) shapeInput = np.random.randint(1, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 10, [shape.rows, shape.cols]) cArray.setArray(data) r = NumCpp.Random.permutationArray(cArray) print(colored('\tPASS', 'green')) print(colored('Testing poisson', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) mean = np.random.rand() * 10 r = NumCpp.Random.poisson(inShape, mean) print(colored('\tPASS', 'green')) print(colored('Testing rand', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) r = NumCpp.Random.rand(inShape) print(colored('\tPASS', 'green')) print(colored('Testing randFloat', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) values = np.random.randint(1, 100, [2, ]) r = NumCpp.Random.randFloat(inShape, values[0].item(), values[1].item()) print(colored('\tPASS', 'green')) print(colored('Testing randInt', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) values = np.random.randint(1, 100, [2, ]) r = NumCpp.Random.randInt(inShape, values[0].item(), values[1].item()) print(colored('\tPASS', 'green')) print(colored('Testing randN', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) r = NumCpp.Random.randN(inShape) print(colored('\tPASS', 'green')) print(colored('Testing seed', 'cyan')) NumCpp.Random.seed(np.random.randint(0, 100000, [1,]).item()) print(colored('\tPASS', 'green')) print(colored('Testing shuffle array', 'cyan')) shapeInput = np.random.randint(1, 100, [2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 10, [shape.rows, shape.cols]) cArray.setArray(data) NumCpp.Random.shuffle(cArray) print(colored('\tPASS', 'green')) print(colored('Testing standardNormal', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) r = NumCpp.Random.standardNormal(inShape) print(colored('\tPASS', 'green')) print(colored('Testing studentT', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) dof = np.random.randint(1, 100, [1, ]).item() r = NumCpp.Random.studentT(inShape, dof) print(colored('\tPASS', 'green')) print(colored('Testing triangle', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) values = np.random.rand(3) values.sort() r = NumCpp.Random.triangle(inShape, values[0].item(), values[1].item(), values[2].item()) print(colored('\tPASS', 'green')) print(colored('Testing uniform', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) values = np.random.randint(1, 100, [2, ]) r = NumCpp.Random.uniform(inShape, values[0].item(), values[1].item()) print(colored('\tPASS', 'green')) print(colored('Testing uniformOnSphere', 'cyan')) inputs = np.random.randint(1, 100, [2,]) r = NumCpp.Random.uniformOnSphere(inputs[0].item(), inputs[1].item()) print(colored('\tPASS', 'green')) print(colored('Testing weibull', 'cyan')) shapeInput = np.random.randint(1, 100, [2,]) inShape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) inputs = np.random.rand(2) r = NumCpp.Random.weibull(inShape, inputs[0].item(), inputs[1].item()) print(colored('\tPASS', 'green'))
def doTest(): print(colored('Testing DataCube Module', 'magenta')) print(colored('Testing Default Constructor', 'cyan')) dataCube = NumCpp.DataCube() if dataCube.isempty(): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing Size Constructor', 'cyan')) size = np.random.randint(10, 20, [ 1, ]).item() dataCube = NumCpp.DataCube(size) if dataCube.size() == size: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing push_front/push_back', 'cyan')) shape = np.random.randint(10, 100, [ 3, ]) cShape = NumCpp.Shape(shape[0].item(), shape[1].item()) data = np.random.randint(0, 100, shape) dataCube = NumCpp.DataCube() frameOrder = list() for frame in range(shape[-1]): cArray = NumCpp.NdArray(cShape) cArray.setArray(data[:, :, frame]) if frame % 2 == 0: dataCube.push_back(cArray) frameOrder.append(frame) else: dataCube.push_front(cArray) frameOrder = [frame] + frameOrder if not dataCube.isempty() and dataCube.size() == shape[-1]: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing shape', 'cyan')) if dataCube.shape() == cShape: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing back', 'cyan')) if np.array_equal(dataCube.back().getNumpyArray(), data[:, :, frameOrder[-1]]): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing front', 'cyan')) if np.array_equal(dataCube.front().getNumpyArray(), data[:, :, frameOrder[0]]): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing [] operator', 'cyan')) allPass = True for idx, frame in enumerate(frameOrder): if not np.array_equal(dataCube[idx].getNumpyArray(), data[:, :, frame]): allPass = False break if allPass: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing at', 'cyan')) for idx, frame in enumerate(frameOrder): if not np.array_equal( dataCube.at(idx).getNumpyArray(), data[:, :, frame]): allPass = False break if allPass: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing dump', 'cyan')) tempDir = r'C:\Temp' if not os.path.exists(tempDir): os.mkdir(tempDir) tempFile = os.path.join(tempDir, 'DataCube.bin') dataCube.dump(tempFile) if os.path.exists(tempFile): filesize = os.path.getsize(tempFile) if filesize == data.size * 8: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) else: print(colored('\tFAIL', 'red')) os.remove(tempFile) print(colored('Testing pop_front/pop_back', 'cyan')) sizeInitial = dataCube.size() sizeNow = sizeInitial allPass = True for idx in range(sizeInitial): if idx % 2 == 0: dataCube.pop_front() else: dataCube.pop_back() sizeNow -= 1 if dataCube.size() != sizeNow: allPass = False if dataCube.isempty() and allPass: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red'))
def doTest(): print(colored('Testing Linalg Module', 'magenta')) print(colored('Testing det: 2x2', 'cyan')) order = 2 shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]).astype(np.double) cArray.setArray(data) if round(NumCpp.det(cArray)) == round(np.linalg.det(data).item()): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing det: 3x3', 'cyan')) order = 3 shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]).astype(np.double) cArray.setArray(data) if round(NumCpp.det(cArray)) == round(np.linalg.det(data).item()): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing det: NxN', 'cyan')) order = np.random.randint(4, 8, [ 1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]).astype(np.double) cArray.setArray(data) if round(NumCpp.det(cArray)) == round(np.linalg.det(data).item()): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing hat', 'cyan')) shape = NumCpp.Shape(1, 3) cArray = NumCpp.NdArray(shape) data = np.random.randint(0, 100, [shape.rows, shape.cols]).flatten() cArray.setArray(data) if np.array_equal(NumCpp.hat(cArray), hat(data)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing inv', 'cyan')) order = np.random.randint(5, 50, [ 1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) if np.array_equal(np.round(NumCpp.inv(cArray).getNumpyArray(), 9), np.round(np.linalg.inv(data), 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing matrix_power: power = 0', 'cyan')) order = np.random.randint(5, 50, [ 1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) if np.array_equal( NumCpp.matrix_power(cArray, 0).getNumpyArray(), np.linalg.matrix_power(data, 0)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing matrix_power: power = 1', 'cyan')) order = np.random.randint(5, 50, [ 1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) if np.array_equal( NumCpp.matrix_power(cArray, 1).getNumpyArray(), np.linalg.matrix_power(data, 1)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing matrix_power: power = -1', 'cyan')) order = np.random.randint(5, 50, [ 1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) if np.array_equal( np.round(NumCpp.matrix_power(cArray, -1).getNumpyArray(), 8), np.round(np.linalg.matrix_power(data, -1), 8)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing matrix_power: power > 1', 'cyan')) order = np.random.randint(5, 50, [ 1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 5, [shape.rows, shape.cols]).astype(np.double) cArray.setArray(data) power = np.random.randint(2, 9, [ 1, ]).item() if np.array_equal( NumCpp.matrix_power(cArray, power).getNumpyArray(), np.linalg.matrix_power(data, power)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing matrix_power: power < -1', 'cyan')) order = np.random.randint(5, 50, [ 1, ]).item() shape = NumCpp.Shape(order) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) power = np.random.randint(2, 9, [ 1, ]).item() * -1 if np.array_equal( np.round(NumCpp.matrix_power(cArray, power).getNumpyArray(), 9), np.round(np.linalg.matrix_power(data, power), 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing multi_dot', 'cyan')) shapeInput = np.random.randint(5, 50, [ 2, ]) shape1 = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) shape2 = NumCpp.Shape(shape1.cols, np.random.randint(5, 50, [ 1, ]).item()) shape3 = NumCpp.Shape(shape2.cols, np.random.randint(5, 50, [ 1, ]).item()) shape4 = NumCpp.Shape(shape3.cols, np.random.randint(5, 50, [ 1, ]).item()) cArray1 = NumCpp.NdArray(shape1) cArray2 = NumCpp.NdArray(shape2) cArray3 = NumCpp.NdArray(shape3) cArray4 = NumCpp.NdArray(shape4) data1 = np.random.randint(1, 10, [shape1.rows, shape1.cols]) data2 = np.random.randint(1, 10, [shape2.rows, shape2.cols]) data3 = np.random.randint(1, 10, [shape3.rows, shape3.cols]) data4 = np.random.randint(1, 10, [shape4.rows, shape4.cols]) cArray1.setArray(data1) cArray2.setArray(data2) cArray3.setArray(data3) cArray4.setArray(data4) if np.array_equal( np.round(NumCpp.multi_dot(cArray1, cArray2, cArray3, cArray4), 9), np.round(np.linalg.multi_dot([data1, data2, data3, data4]), 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing lstsq', 'cyan')) shapeInput = np.random.randint(5, 50, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) aArray = NumCpp.NdArray(shape) bArray = NumCpp.NdArray(1, shape.rows) aData = np.random.randint(1, 100, [shape.rows, shape.cols]) bData = np.random.randint(1, 100, [ shape.rows, ]) aArray.setArray(aData) bArray.setArray(bData) x = NumCpp.lstsq(aArray, bArray, 1e-12).getNumpyArray().flatten() if np.array_equal( np.round(x, 9), np.round(np.linalg.lstsq(aData, bData, rcond=None)[0], 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing svd', 'cyan')) shapeInput = np.random.randint(5, 50, [ 2, ]) shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item()) cArray = NumCpp.NdArray(shape) data = np.random.randint(1, 100, [shape.rows, shape.cols]) cArray.setArray(data) uArray = NumCpp.NdArray() sArray = NumCpp.NdArray() vArray = NumCpp.NdArray() NumCpp.svd(cArray, uArray, sArray, vArray) data2 = np.dot(uArray.getNumpyArray(), np.dot(sArray.getNumpyArray(), vArray.getNumpyArray())) if np.array_equal(np.round(data, 9), np.round(data2, 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red'))