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 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 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 Image Processing Module', 'magenta')) # generate a random noise imageSize = 1024 noiseStd = np.random.rand(1) * 4 noiseMean = np.random.randint(75, 100, [ 1, ]).item() noise = np.round( np.random.randn(imageSize, imageSize) * noiseStd + noiseMean) # scatter some point sources on it pointSize = 5 pointHalfSize = pointSize // 2 pointSource = np.asarray([[1, 1, 1, 1, 1], [1, 5, 30, 5, 1], [1, 30, 100, 30, 1], [1, 5, 30, 5, 1], [1, 1, 1, 1, 1]]) scene = noise.copy() numPointSources = 3000 for point in range(numPointSources): row = np.random.randint(pointHalfSize, imageSize - pointHalfSize, [ 1, ]).item() col = np.random.randint(pointHalfSize, imageSize - pointHalfSize, [ 1, ]).item() cutout = scene[row - pointHalfSize:row + pointHalfSize + 1, col - pointHalfSize:col + pointHalfSize + 1] cutout = cutout + pointSource scene[row - pointHalfSize:row + pointHalfSize + 1, col - pointHalfSize:col + pointHalfSize + 1] = cutout # generate centroids from the image thresholdRate = 0.014 borderWidth = np.random.randint(0, 4, [ 1, ]).item() cScene = NumCpp.NdArray(imageSize) cScene.setArray(scene) threshold = NumCpp.generateThreshold(cScene, thresholdRate) print(f'Scene Min = {scene.min()}') print(f'Scene Max = {scene.max()}') print(f'Threshold = {threshold}') print(f'Desired Rate = {thresholdRate}') print( f'Actual Rate(Threshold) = {np.count_nonzero(scene > threshold) / scene.size}' ) print( f'Actual Rate(Threshold - 1) = {np.count_nonzero(scene > threshold - 1) / scene.size}' ) centroids = list( NumCpp.generateCentroids(cScene, thresholdRate, 'pre', borderWidth)) print( f'Window Pre Number of Centroids (Border = {borderWidth}) = {len(centroids)}' ) # plt the results plt.figure() plt.imshow(scene) plt.colorbar() plt.clim([threshold, threshold + 1]) plt.xlabel('Rows') plt.ylabel('Cols') plt.title(f'Window Pre Centroids\nNumber of Centroids = {len(centroids)}') for centroid in centroids: plt.plot(centroid.col(), centroid.row(), 'og', fillstyle='none') plt.show() centroidInfo = np.asarray([[centroid.intensity(), centroid.eod()] for centroid in centroids]) plt.figure() plt.plot(np.sort(centroidInfo[:, 0].flatten())) plt.title('Window Pre Centroid Intensities') plt.xlabel('Centroid #') plt.ylabel('Counts') plt.show() plt.figure() plt.plot(np.sort(centroidInfo[:, 1].flatten() * 100)) plt.title('Window Pre Centroid EOD') plt.xlabel('Centroid #') plt.ylabel('EOD (%)') plt.show() centroids = list( NumCpp.generateCentroids(cScene, thresholdRate, 'post', borderWidth)) print( f'Window Post Number of Centroids (Border = {borderWidth}) = {len(centroids)}' ) # plt the results plt.figure() plt.imshow(scene) plt.colorbar() plt.clim([threshold, threshold + 1]) plt.xlabel('Rows') plt.ylabel('Cols') plt.title(f'Window Post Centroids\nNumber of Centroids = {len(centroids)}') for centroid in centroids: plt.plot(centroid.col(), centroid.row(), 'og', fillstyle='none') plt.show() centroidInfo = np.asarray([[centroid.intensity(), centroid.eod()] for centroid in centroids]) plt.figure() plt.plot(np.sort(centroidInfo[:, 0].flatten())) plt.title('Window Post Centroid Intensities') plt.xlabel('Centroid #') plt.ylabel('Counts') plt.show() plt.figure() plt.plot(np.sort(centroidInfo[:, 1].flatten() * 100)) plt.title('Window Post Centroid EOD') plt.xlabel('Centroid #') plt.ylabel('EOD (%)') plt.show() print(colored('\tPASS', 'green'))
def doTest(): print(colored('Testing Polynomial Module', 'magenta')) print(colored('Testing Constructor', 'cyan')) numCoefficients = np.random.randint(3, 10, [ 1, ]).item() coefficients = np.random.randint(-20, 20, [ numCoefficients, ]) coefficientsC = NumCpp.NdArray(1, numCoefficients) coefficientsC.setArray(coefficients) polyC = NumCpp.Poly1d(coefficientsC, False) if np.array_equal(polyC.coefficients().getNumpyArray().flatten(), coefficients): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing Constructor Roots', 'cyan')) numRoots = np.random.randint(3, 10, [ 1, ]).item() roots = np.random.randint(-20, 20, [ numRoots, ]) rootsC = NumCpp.NdArray(1, numRoots) rootsC.setArray(roots) poly = np.poly1d(roots, True) polyC = NumCpp.Poly1d(rootsC, True) if np.array_equal( np.fliplr(polyC.coefficients().getNumpyArray()).flatten().astype( np.int), poly.coefficients): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing order', 'cyan')) if polyC.order() == roots.size: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing operator()', 'cyan')) value = np.random.randint(-20, 20, [ 1, ]).item() if polyC[value] == poly(value): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing addition', 'cyan')) numCoefficients = np.random.randint(3, 10, [ 1, ]).item() coefficients = np.random.randint(-20, 20, [ numCoefficients, ]) coefficientsC = NumCpp.NdArray(1, numCoefficients) coefficientsC.setArray(coefficients) polyC2 = NumCpp.Poly1d(coefficientsC, False) poly2 = np.poly1d(np.flip(coefficients)) if np.array_equal( np.fliplr( (polyC + polyC2).coefficients().getNumpyArray()).flatten(), (poly + poly2).coefficients): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing subtraction', 'cyan')) if np.array_equal( np.fliplr( (polyC - polyC2).coefficients().getNumpyArray()).flatten(), (poly - poly2).coefficients): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing multiplication', 'cyan')) if np.array_equal( np.fliplr( (polyC * polyC2).coefficients().getNumpyArray()).flatten(), (poly * poly2).coefficients): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing power', 'cyan')) exponent = np.random.randint(0, 5, [ 1, ]).item() if np.array_equal( np.fliplr( (polyC2**exponent).coefficients().getNumpyArray()).flatten(), (poly2**exponent).coefficients): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing print', 'cyan')) polyC.print()
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 Coordinates Module', 'magenta')) print(colored('Testing Ra', 'magenta')) print(colored('Testing Default Constructor', 'cyan')) ra = NumCpp.RaDouble() print(colored('\tPASS', 'green')) print(colored('Testing Degree Constructor', 'cyan')) randDegrees = np.random.rand(1).item() * 360 ra = NumCpp.RaDouble(randDegrees) raPy = Longitude(randDegrees, unit=u.deg) if (round(ra.degrees(), 9) == round(randDegrees, 9) and ra.hours() == raPy.hms.h and ra.minutes() == raPy.hms.m and round(ra.seconds(), 9) == round(raPy.hms.s, 9) and round(ra.radians(), 9) == round(np.deg2rad(randDegrees), 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing hms Constructor', 'cyan')) hours = np.random.randint(0, 24, [1,], dtype=np.uint8).item() minutes = np.random.randint(0, 60, [1, ], dtype=np.uint8).item() seconds = np.random.rand(1).astype(np.float32).item() * 60 ra = NumCpp.RaDouble(hours, minutes, seconds) degreesPy = (hours + minutes / 60 + seconds / 3600) * 15 if (round(ra.degrees(), 9) == round(degreesPy, 9) and ra.hours() == hours and ra.minutes() == minutes and round(ra.seconds(), 9) == round(seconds, 9) and round(ra.radians(), 9) == round(np.deg2rad(degreesPy), 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing astype', 'cyan')) raF = ra.asFloat() if (round(ra.degrees(), 4) == round(raF.degrees(), 4) and ra.hours() == raF.hours() and ra.minutes() == raF.minutes() and round(ra.seconds(), 4) == round(raF.seconds(), 4) and round(ra.radians(), 4) == round(raF.radians(), 4)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing equality operator', 'cyan')) ra2 = NumCpp.RaDouble(ra) if ra == ra2: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing not equality operator', 'cyan')) randDegrees = np.random.rand(1).item() * 360 ra2 = NumCpp.RaDouble(randDegrees) if ra != ra2: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing print', 'cyan')) ra.print() print(colored('\tPASS', 'green')) print(colored('Testing Dec', 'magenta')) print(colored('Testing Default Constructor', 'cyan')) dec = NumCpp.DecDouble() print(colored('\tPASS', 'green')) print(colored('Testing Degree Constructor', 'cyan')) randDegrees = np.random.rand(1).item() * 180 - 90 dec = NumCpp.DecDouble(randDegrees) decPy = Latitude(randDegrees, unit=u.deg) sign = NumCpp.Sign.NEGATIVE if randDegrees < 0 else NumCpp.Sign.POSITIVE if (round(dec.degrees(), 8) == round(randDegrees, 8) and dec.sign() == sign and dec.degreesWhole() == abs(decPy.dms.d) and dec.minutes() == abs(decPy.dms.m) and round(dec.seconds(), 8) == round(abs(decPy.dms.s), 8) and round(dec.radians(), 8) == round(np.deg2rad(randDegrees), 8)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing dms Constructor', 'cyan')) sign = NumCpp.Sign.POSITIVE if np.random.randint(-1, 1) == 0 else NumCpp.Sign.NEGATIVE degrees = np.random.randint(0, 91, [1, ], dtype=np.uint8).item() minutes = np.random.randint(0, 60, [1, ], dtype=np.uint8).item() seconds = np.random.rand(1).astype(np.float32).item() * 60 dec = NumCpp.DecDouble(sign, degrees, minutes, seconds) degreesPy = degrees + minutes / 60 + seconds / 3600 if sign == NumCpp.Sign.NEGATIVE: degreesPy *= -1 if (dec.sign() == sign and round(dec.degrees(), 9) == round(degreesPy, 9) and dec.degreesWhole() == degrees and dec.minutes() == minutes and round(dec.seconds(), 9) == round(seconds, 9) and round(dec.radians(), 8) == round(np.deg2rad(degreesPy), 8)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing astype', 'cyan')) decF = dec.asFloat() if (round(dec.degrees(), 4) == round(decF.degrees(), 4) and dec.sign() == decF.sign() and dec.degreesWhole() == decF.degreesWhole() and dec.minutes() == decF.minutes() and round(dec.seconds(), 4) == round(decF.seconds(), 4) and round(dec.radians(), 4) == round(decF.radians(), 4)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing equality operator', 'cyan')) dec2 = NumCpp.DecDouble(dec) if dec == dec2: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing not equality operator', 'cyan')) randDegrees = np.random.rand(1).item() * 180 - 90 dec2 = NumCpp.DecDouble(randDegrees) if dec != dec2: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing print', 'cyan')) dec.print() print(colored('\tPASS', 'green')) print(colored('Testing Coordinate', 'magenta')) print(colored('Testing Default Constructor', 'cyan')) coord = NumCpp.CoordinateDouble() print(colored('\tPASS', 'green')) print(colored('Testing Degree Constructor', 'cyan')) raDegrees = np.random.rand(1).item() * 360 ra = NumCpp.RaDouble(raDegrees) decDegrees = np.random.rand(1).item() * 180 - 90 dec = NumCpp.DecDouble(decDegrees) pyCoord = SkyCoord(raDegrees, decDegrees, unit=u.deg) cCoord = NumCpp.CoordinateDouble(raDegrees, decDegrees) if (cCoord.ra() == ra and cCoord.dec() == dec and round(cCoord.x(), 10) == round(pyCoord.cartesian.x.value, 10) and round(cCoord.y(), 10) == round(pyCoord.cartesian.y.value, 10) and round(cCoord.z(), 10) == round(pyCoord.cartesian.z.value, 10)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing Ra/Dec Constructor', 'cyan')) raDegrees = np.random.rand(1).item() * 360 ra = NumCpp.RaDouble(raDegrees) decDegrees = np.random.rand(1).item() * 180 - 90 dec = NumCpp.DecDouble(decDegrees) pyCoord = SkyCoord(raDegrees, decDegrees, unit=u.deg) cCoord = NumCpp.CoordinateDouble(ra, dec) if (cCoord.ra() == ra and cCoord.dec() == dec and round(cCoord.x(), 10) == round(pyCoord.cartesian.x.value, 10) and round(cCoord.y(), 10) == round(pyCoord.cartesian.y.value, 10) and round(cCoord.z(), 10) == round(pyCoord.cartesian.z.value, 10)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing x/y/z Constructor', 'cyan')) raDegrees = np.random.rand(1).item() * 360 ra = NumCpp.RaDouble(raDegrees) decDegrees = np.random.rand(1).item() * 180 - 90 dec = NumCpp.DecDouble(decDegrees) pyCoord = SkyCoord(raDegrees, decDegrees, unit=u.deg) cCoord = NumCpp.CoordinateDouble(pyCoord.cartesian.x.value, pyCoord.cartesian.y.value, pyCoord.cartesian.z.value) if (round(cCoord.ra().degrees(), 9) == round(ra.degrees(), 9) and round(cCoord.dec().degrees(), 9) == round(dec.degrees(), 9) and round(cCoord.x(), 9) == round(pyCoord.cartesian.x.value, 9) and round(cCoord.y(), 9) == round(pyCoord.cartesian.y.value, 9) and round(cCoord.z(), 9) == round(pyCoord.cartesian.z.value, 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing NdArray Constructor', 'cyan')) raDegrees = np.random.rand(1).item() * 360 ra = NumCpp.RaDouble(raDegrees) decDegrees = np.random.rand(1).item() * 180 - 90 dec = NumCpp.DecDouble(decDegrees) pyCoord = SkyCoord(raDegrees, decDegrees, unit=u.deg) vec = np.asarray([pyCoord.cartesian.x.value, pyCoord.cartesian.y.value, pyCoord.cartesian.z.value]) cVec = NumCpp.NdArray(1, 3) cVec.setArray(vec) cCoord = NumCpp.CoordinateDouble(cVec) if (round(cCoord.ra().degrees(), 9) == round(ra.degrees(), 9) and round(cCoord.dec().degrees(), 9) == round(dec.degrees(), 9) and round(cCoord.x(), 9) == round(pyCoord.cartesian.x.value, 9) and round(cCoord.y(), 9) == round(pyCoord.cartesian.y.value, 9) and round(cCoord.z(), 9) == round(pyCoord.cartesian.z.value, 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing hms/dms Constructor', 'cyan')) raHours = np.random.randint(0, 24, [1,], dtype=np.uint8).item() raMinutes = np.random.randint(0, 60, [1, ], dtype=np.uint8).item() raSeconds = np.random.rand(1).astype(np.float32).item() * 60 raDegreesPy = (raHours + raMinutes / 60 + raSeconds / 3600) * 15 decSign = NumCpp.Sign.POSITIVE if np.random.randint(-1, 1) == 0 else NumCpp.Sign.NEGATIVE decDegrees = np.random.randint(0, 91, [1, ], dtype=np.uint8).item() decMinutes = np.random.randint(0, 60, [1, ], dtype=np.uint8).item() decSeconds = np.random.rand(1).astype(np.float32).item() * 60 decDegreesPy = decDegrees + decMinutes / 60 + decSeconds / 3600 if decSign == NumCpp.Sign.NEGATIVE: decDegreesPy *= -1 cCoord = NumCpp.CoordinateDouble(raHours, raMinutes, raSeconds, decSign, decDegrees, decMinutes, decSeconds) cRa = cCoord.ra() cDec = cCoord.dec() pyCoord = SkyCoord(raDegreesPy, decDegreesPy, unit=u.deg) if (round(cRa.degrees(), 9) == round(raDegreesPy, 9) and cRa.hours() == raHours and cRa.minutes() == raMinutes and round(cRa.seconds(), 9) == round(raSeconds, 9) and cDec.sign() == decSign and round(cDec.degrees(), 9) == round(decDegreesPy, 9) and cDec.degreesWhole() == decDegrees and cDec.minutes() == decMinutes and round(cDec.seconds(), 9) == round(decSeconds, 9) and round(cCoord.x(), 9) == round(pyCoord.cartesian.x.value, 9) and round(cCoord.y(), 9) == round(pyCoord.cartesian.y.value, 9) and round(cCoord.z(), 9) == round(pyCoord.cartesian.z.value, 9)): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing equality operator', 'cyan')) cCoord2 = NumCpp.CoordinateDouble(cCoord) if cCoord2 == cCoord: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing not equality operator', 'cyan')) raDegrees = np.random.rand(1).item() * 360 decDegrees = np.random.rand(1).item() * 180 - 90 cCoord2 = NumCpp.CoordinateDouble(raDegrees, decDegrees) if cCoord2 != cCoord: print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing xyz', 'cyan')) xyz = [cCoord.x(), cCoord.y(), cCoord.z()] if np.array_equal(cCoord.xyz().getNumpyArray().flatten(), xyz): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing degreeSeperation Coordinate', 'cyan')) pyCoord2 = SkyCoord(cCoord2.ra().degrees(), cCoord2.dec().degrees(), unit=u.deg) cDegSep = cCoord.degreeSeperation(cCoord2) pyDegSep = pyCoord.separation(pyCoord2).value if round(cDegSep, 9) == round(pyDegSep, 9): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing radianSeperation Coordinate', 'cyan')) cRadSep = cCoord.radianSeperation(cCoord2) pyRadSep = np.deg2rad(pyDegSep) if round(cRadSep, 9) == round(pyRadSep, 9): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing degreeSeperation Vector', 'cyan')) vec2 = np.asarray([pyCoord2.cartesian.x, pyCoord2.cartesian.y, pyCoord2.cartesian.z]) cArray = NumCpp.NdArray(1, 3) cArray.setArray(vec2) cDegSep = cCoord.degreeSeperation(cArray) if round(cDegSep, 9) == round(pyDegSep, 9): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing radianSeperation Vector', 'cyan')) cArray = NumCpp.NdArray(1, 3) cArray.setArray(vec2) cDegSep = cCoord.radianSeperation(cArray) if round(cDegSep, 9) == round(pyRadSep, 9): print(colored('\tPASS', 'green')) else: print(colored('\tFAIL', 'red')) print(colored('Testing print', 'cyan')) cCoord.print() print(colored('\tPASS', 'green'))
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'))