def test_validate_fp16_transcendentals(self, ufunc): with np.errstate(all='ignore'): arr = np.arange(65536, dtype=np.int16) datafp16 = np.frombuffer(arr.tobytes(), dtype=np.float16) datafp32 = datafp16.astype(np.float32) assert_array_max_ulp(ufunc(datafp16), ufunc(datafp32), maxulp=1, dtype=np.float16)
def test_validate_transcendentals(self): with np.errstate(all="ignore"): for filename in files: data_dir = path.join(path.dirname(__file__), "data") filepath = path.join(data_dir, filename) file_without_comments = (r for r in open(filepath) if not r[0] in ("$", "#")) data = np.genfromtxt( file_without_comments, dtype=("|S39", "|S39", "|S39", np.int), names=("type", "input", "output", "ulperr"), delimiter=",", skip_header=1, ) npfunc = getattr(np, filename.split("-")[3]) for datatype in np.unique(data["type"]): data_subset = data[data["type"] == datatype] inval = np.array( str_to_float(data_subset["input"].astype(str)), dtype=eval(datatype), ) outval = np.array( str_to_float(data_subset["output"].astype(str)), dtype=eval(datatype), ) perm = np.random.permutation(len(inval)) inval = inval[perm] outval = outval[perm] maxulperr = data_subset["ulperr"].max() assert_array_max_ulp(npfunc(inval), outval, maxulperr)
def test_consistency(self): for win_size, img_size in [ ((2, 2), (4, 4)), ((3, 3), (4, 4)), (4, (4, 4)), ((1, 3, 3), (3, 3, 3)), ((2, 3, 3), (3, 3, 3)), (3, (4, 4, 4)), ]: for dtype in [ np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64, ]: np.random.seed(0) info = np.iinfo(dtype) img = np.random.randint(info.min, info.max, img_size, dtype=dtype) clip_limit = .5 fast = clahe.clahe(img, win_size, clip_limit) slow = clahe.clahe(img, win_size, clip_limit, _fast=False) assert fast.shape == slow.shape == img.shape # Not clear why it's not exactly equal. assert_array_max_ulp(fast, slow, 14)
def test_fixed_array_to_world(self): f = [[0, 1, 100], [20, 40, 60], [210, 310, 410]] assert_array_max_ulp( self.coords.fixed_to_world(f), [[-1.0, -0.9, 9.0], [1.0, 3.0, 5.0], [20.0, 30.0, 40.0]], dtype=np.float32)
def test_dimensionReduction(self): testRequestetDim = 1 testDataSet = np.array( [ [0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [0, 1], [0, -1] ] ) p = pca.PCA(testDataSet, testRequestetDim) p.substractMean() transformedTestDataSet = np.zeros( ( 1, len(testDataSet) ) ) for i in range ( len(testDataSet) ): transformedTestDataSet[0][i] = p.meanData[i][0] # Test for PCA with covariance matrix pCov = pca.PCACOV(testDataSet, testRequestetDim) pCov.substractMean() pCov.fit() pCov.project() pCov.dimensionReduction() print transformedTestDataSet print pCov.reducedTransData npt.assert_array_max_ulp(transformedTestDataSet, pCov.reducedTransData, maxulp = 0) # Test for PCA with SVD pSvd = pca.PCASVD(testDataSet, testRequestetDim) pSvd.substractMean() pSvd.fit() pSvd.project() pSvd.dimensionReduction() npt.assert_array_max_ulp(transformedTestDataSet, pSvd.reducedTransData, maxulp = 0)
def test_array(self, stride, astype, func): dtype = [('template_id', '<i8'), ('bank_chisq', '<f4'), ('bank_chisq_dof', '<i8'), ('chisq', '<f4'), ('chisq_dof', '<i8'), ('cont_chisq', '<f4'), ('psd_var_val', '<f4'), ('sg_chisq', '<f4'), ('mycomplex', astype), ('time_index', '<i8')] vec = np.array([ (0, 0., 0, -31.666483, 200, 0., 0., 1., 3.0 + 4.0j, 613090), (1, 0., 0, 260.91525, 42, 0., 0., 1., 5.0 + 12.0j, 787315), (1, 0., 0, 52.15155, 42, 0., 0., 1., 8.0 + 15.0j, 806641), (1, 0., 0, 52.430195, 42, 0., 0., 1., 7.0 + 24.0j, 1363540), (2, 0., 0, 304.43646, 58, 0., 0., 1., 20.0 + 21.0j, 787323), (3, 0., 0, 299.42108, 52, 0., 0., 1., 12.0 + 35.0j, 787332), (4, 0., 0, 39.4836, 28, 0., 0., 9.182192, 9.0 + 40.0j, 787304), (4, 0., 0, 76.83787, 28, 0., 0., 1., 28.0 + 45.0j, 1321869), (5, 0., 0, 143.26366, 24, 0., 0., 10.996129, 11.0 + 60.0j, 787299) ], dtype=dtype) myfunc = getattr(np, func) a = vec['mycomplex'] g = myfunc(a[::stride]) b = vec['mycomplex'].copy() h = myfunc(b[::stride]) assert_array_max_ulp(h.real, g.real, 1) assert_array_max_ulp(h.imag, g.imag, 1)
def test_validate_transcendentals(self): with np.errstate(all='ignore'): for filename in files: data_dir = path.join(path.dirname(__file__), 'data') filepath = path.join(data_dir, filename) file_without_comments = (r for r in open(filepath) if not r[0] in ('$', '#')) data = np.genfromtxt(file_without_comments, dtype=('|S39', '|S39', '|S39', np.int), names=('type', 'input', 'output', 'ulperr'), delimiter=',', skip_header=1) npfunc = getattr(np, filename.split('-')[3]) for datatype in np.unique(data['type']): data_subset = data[data['type'] == datatype] inval = np.array(str_to_float( data_subset['input'].astype(str)), dtype=eval(datatype)) outval = np.array(str_to_float( data_subset['output'].astype(str)), dtype=eval(datatype)) perm = np.random.permutation(len(inval)) inval = inval[perm] outval = outval[perm] maxulperr = data_subset['ulperr'].max() assert_array_max_ulp(npfunc(inval), outval, maxulperr)
def test_nan(self): # Test that nan is 'far' from small, tiny, inf, max and min for dt in [np.float32, np.float64]: if dt == np.float32: maxulp = 1e6 else: maxulp = 1e12 inf = np.array([np.inf]).astype(dt) nan = np.array([np.nan]).astype(dt) big = np.array([np.finfo(dt).max]) tiny = np.array([np.finfo(dt).tiny]) zero = np.array([np.PZERO]).astype(dt) nzero = np.array([np.NZERO]).astype(dt) self.assertRaises(AssertionError, lambda: assert_array_max_ulp(nan, inf, maxulp=maxulp)) self.assertRaises(AssertionError, lambda: assert_array_max_ulp(nan, big, maxulp=maxulp)) self.assertRaises(AssertionError, lambda: assert_array_max_ulp(nan, tiny, maxulp=maxulp)) self.assertRaises(AssertionError, lambda: assert_array_max_ulp(nan, zero, maxulp=maxulp)) self.assertRaises(AssertionError, lambda: assert_array_max_ulp(nan, nzero, maxulp=maxulp))
def test_nan(self): # Test that nan is 'far' from small, tiny, inf, max and min for dt in [np.float32, np.float64]: if dt == np.float32: maxulp = 1e6 else: maxulp = 1e12 inf = np.array([np.inf]).astype(dt) nan = np.array([np.nan]).astype(dt) big = np.array([np.finfo(dt).max]) tiny = np.array([np.finfo(dt).tiny]) zero = np.array([np.PZERO]).astype(dt) nzero = np.array([np.NZERO]).astype(dt) self.assertRaises( AssertionError, lambda: assert_array_max_ulp(nan, inf, maxulp=maxulp)) self.assertRaises( AssertionError, lambda: assert_array_max_ulp(nan, big, maxulp=maxulp)) self.assertRaises( AssertionError, lambda: assert_array_max_ulp(nan, tiny, maxulp=maxulp)) self.assertRaises( AssertionError, lambda: assert_array_max_ulp(nan, zero, maxulp=maxulp)) self.assertRaises( AssertionError, lambda: assert_array_max_ulp(nan, nzero, maxulp=maxulp))
def spectrum_answer_testing(spec, filename, answer_store, answer_dir): testfile = os.path.join(answer_dir, filename) if answer_store: spec.write_h5_file(testfile, overwrite=True) else: answer_spec = type(spec).from_file(testfile) assert_array_max_ulp(answer_spec.emid.value, spec.emid.value, maxulp=4) assert_array_max_ulp(answer_spec.flux.value, spec.flux.value, maxulp=4) assert answer_spec.flux.unit == spec.flux.unit
def test_consistency(win_size, img_size): np.random.seed(0) img = np.random.randint(0, 256, img_size, np.uint8) clip_limit = .5 fast = clahe.clahe(img, win_size, clip_limit) slow = clahe.clahe(img, win_size, clip_limit, _fast=False) assert fast.shape == slow.shape == img.shape # Not clear why it's not exactly equal. assert_array_max_ulp(fast, slow)
def test_single_positionRms(): ra = np.deg2rad(np.array([10.0010])) dec = np.deg2rad(np.array([20.001])) ra_avg = np.mean(ra) dec_avg = np.mean(dec) exp = 0 obs = util.positionRms(ra_avg, dec_avg, ra, dec) assert_array_max_ulp(obs, exp)
def test_fixed_array_to_world(self): f = [[0, 1, 100], [20, 40, 60], [210, 310, 410]] assert_array_max_ulp(self.coords.fixed_to_world(f), [[-1.0, -0.9, 9.0], [1.0, 3.0, 5.0], [20.0, 30.0, 40.0]], dtype=np.float32)
def test_init(self): testDataArray = np.array( [ [1, 2, 3], [4.5, 7.3, 1.2], [4, 5, 9], [7, 8, 9] ] ) testK = 2 testP = pca.PCA(testDataArray, testK) # Test Array Equality npt.assert_array_max_ulp(testP.data, testDataArray, maxulp = 0) # Test requested dimension equality self.assertTrue(testP.request == testK)
def test_correct_images(): x = np.ones((3, 10, 10), dtype=np.uint16) x[0] = x[0] * 0x0010 x[1] = x[1] * 0x8020 x[2] = x[2] * 0xC030 y = np.ones((3, 10, 10), dtype=np.float32) y[0] = y[0] * 0x0010 y[1] = y[1] * 0x0020 y[2] = y[2] * 0x0030 z = correct_images(x, y) assert_array_max_ulp(z, np.zeros_like(x))
def test_rescale_squared_micro_prefix(self): y_um = self.f['y'][:] / 1000**2 self.plotter.rescale('y', 'y_um', u.um**2) assert_array_max_ulp(self.plotter.groups[0]['y_um'][:], y_um, 10) y_um_attrs = self.f['y_um'].attrs exp_y_um_attrs = OrderedDict( ((u'name', "Displacement Squared"), (u'unit', u'um^2'), (u'label', u"z [\xb5m\xb2]"), (u'label_latex', u'$z \\: [\\mu\\mathrm{m}^{2}]$'), (u'help', u'Displacement example'), (u'n_avg', 1.0))) for key, val in exp_y_um_attrs.items(): eq_(val, y_um_attrs[key])
def test_rescale_normal(self): x_sec = self.f['x'][:] / 1000 self.plotter.rescale('x', 'x_s', 's') assert_array_max_ulp(self.plotter.groups[0]['x_s'][:], x_sec, 5) x_s_attrs = self.f['x_s'].attrs exp_x_s_attrs = OrderedDict( ((u'name', "Time"), (u'unit', u's'), (u'label', u"Time [s]"), (u'label_latex', u'Time $t \\: [\\mathrm{s}]$'), (u'help', u'Time example'))) for key, val in exp_x_s_attrs.items(): eq_(val, x_s_attrs[key])
def test_init(self): testDataArray = np.array([[1, 4.5, 4, 7], [2, 7.3, 5, 8], [3, 1.2, 9, 9]]) testK = 2 testP = pca.PCA(testDataArray, testK) #testPB = pca.PCABig("testDataFile.csv", "transformed.bin", testK) # Test Array Equality npt.assert_array_max_ulp(testP.data, testDataArray, maxulp = 0.) #npt.assert_array_max_ulp(testPB.data, "testDataFile.csv", maxulp = 0.) # Test requested dimension equality self.assertTrue(testP.k == testK)
def test_pcaDataTransformation(self): testRequestetDim = 1 testDataSet = np.array( [ [0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [0, 1], [0, -1] ] ) pCov = pca.PCACOV(testDataSet, testRequestetDim) pCov.substractMean() pCov.fit() pCov.project() pSvd = pca.PCASVD(testDataSet, testRequestetDim) pSvd.substractMean() pSvd.fit() pSvd.project() npt.assert_array_max_ulp(pCov.transData, pSvd.transData, maxulp = 0)
def test_rescale_different_units(self): x_s = self.f['x'][:] * 1000000 self.plotter.rescale('x', 'x_s', 's') # Rescale twice; make sure the rescale function can handle this self.plotter.rescale('x', 'x_s', 'ns') assert_array_max_ulp(self.plotter.groups[0]['x_s'][:], x_s, 5) x_s_attrs = self.f['x_s'].attrs exp_x_s_attrs = OrderedDict( ((u'name', "Time"), (u'unit', u'ns'), (u'label', u"Time [ns]"), (u'label_latex', u'Time $t \\: [\\mathrm{ns}]$'), (u'help', u'Time example'))) for key, val in exp_x_s_attrs.items(): eq_(val, x_s_attrs[key])
def test_square_root_y(self): new_attrs = OrderedDict( ((u'name', "Displacement"), (u'unit', u'nm'), (u'label', u"z [nm]"), (u'label_latex', u'$z \\: [\\mathrm{nm}]$'), (u'help', u'Displacement example'), (u'n_avg', 1.0))) exp_y_sqrt = 10 * np.sqrt(np.abs(np.sin(self.f['x'][:]))) def transform(group): return np.abs(group['y'][:])**0.5 self.plotter.map(transform, 'y_sqrt', new_attrs) y_sqrt = self.plotter.groups[0]['y_sqrt'] assert_array_max_ulp(exp_y_sqrt, y_sqrt[:], 5) for key, val in new_attrs.items(): eq_(val, y_sqrt.attrs[key])
def test_substractMean(self): testDataArray = np.array( [ [1, 2, 3], [4.5, 7.3, 1.2], [4, 5, 9], [7, 8, 9] ] ) p = pca.PCA(testDataArray, 2) p.substractMean() maximumDeviationInLastDigit = 1 # Test mean calculation meanControl1 = (1 + 4.5 + 4 + 7) / 4. meanControl2 = (2 + 7.3 + 5 + 8) / 4. meanControl3 = (3 + 1.2 + 9 + 9) / 4. meanControl = np.array( [meanControl1, meanControl2, meanControl3] ) npt.assert_array_max_ulp(meanControl, p.mean, maxulp = maximumDeviationInLastDigit) # Test mean free data meanFreeTestDataArray = testDataArray - meanControl npt.assert_array_max_ulp(meanFreeTestDataArray, p.meanData, maxulp = maximumDeviationInLastDigit)
def test_fit(self): testRequestetDim = 1 testDataSet = np.array( [ [0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [0, 1], [0, -1] ] ) meanFreeTestDataSet = testDataSet - np.mean(testDataSet, axis=0) # Test PCA with covariance matrix p = pca.PCACOV(testDataSet, testRequestetDim) p.substractMean() p.fit() ## Test covariance matrix controlCovMat = 1. / 6 * np.dot ( np.transpose (meanFreeTestDataSet), meanFreeTestDataSet ) npt.assert_array_max_ulp(controlCovMat, p.covMat, maxulp = 0) ## Test eigenvalue and eigenvector calculation eigVal, eigVec = np.linalg.eig(p.covMat) for i in range( len(eigVal) ): eigVal[i] = math.fabs(eigVal[i]) controlEigPairs = [ [ eigVal[0], eigVec[0] ], [ eigVal[1], eigVec[1] ] ] for i in range ( len(controlEigPairs) ): self.assertTrue(controlEigPairs[i][0] == p.eig_pairs[i][0]) npt.assert_array_max_ulp(controlEigPairs[i][1], p.eig_pairs[i][1], maxulp = 0) ## Test transformation Matrix controlTransformationMatrix = np.array( [ [1,0], [0,1] ] ) npt.assert_array_max_ulp(controlTransformationMatrix, p.transMat, maxulp = 0) # Test PCA with SVD p = pca.PCASVD(testDataSet, testRequestetDim) p.substractMean() p.fit() ## Test transformation matrix controlTransformationMatrix = np.array( [ [1,0], [0,1] ] ) npt.assert_array_max_ulp(controlTransformationMatrix, p.transMat, maxulp = 0)
def test_inf(self): for dt in [np.float32, np.float64]: inf = np.array([np.inf]).astype(dt) big = np.array([np.finfo(dt).max]) assert_array_max_ulp(inf, big, maxulp=200)
def test_empty(self): a, b = histogramdd([[], []], bins=([0, 1], [0, 1])) assert_array_max_ulp(a, np.array([[0.]])) a, b = np.histogramdd([[], [], []], bins=2) assert_array_max_ulp(a, np.zeros((2, 2, 2)))
def assert_array_max_ulp(self, *args, **kwargs): """ Check that all items of arrays differ in at most N Units in the Last Place. """ return assert_array_max_ulp(*args, **kwargs)
for digit in xrange(20,0,-1): try: assert_approx_equal(a[1],b[1],digit) print 'digit= %d'%digit break except Exception, err: continue print '\nassert_array_almost_nulp' for nulp in xrange(1,20): try: assert_array_almost_equal_nulp(a,b,nulp) print 'nulp= %d'%nulp break except Exception, err: print err continue print '\nassert_array_max_ulp' for maxulp in xrange(20,0,-1): try: res = assert_array_max_ulp(a,b,maxulp) print res print 'maxulp= %d'%maxulp break except Exception, err: print err continue
def test_ignore_nan_ulperror(self): # Ignore ULP differences between various NAN's nan1_f32 = np.array(str_to_float('0xffffffff'), dtype=np.float32) nan2_f32 = np.array(str_to_float('0x7fddbfbf'), dtype=np.float32) assert_array_max_ulp(nan1_f32, nan2_f32, 0)
def test_equal(self): x = np.random.randn(10) assert_array_max_ulp(x, x, maxulp=0)
def test_double(self): # Generate 1 + small deviation, check that adding eps gives a few UNL x = np.ones(10).astype(np.float64) x += 0.01 * np.random.randn(10).astype(np.float64) eps = np.finfo(np.float64).eps assert_array_max_ulp(x, x+eps, maxulp=200)
def assert_array_max_ulp(self, a, b, maxulp=1, dtype=None): a = a.tondarray() if hasattr(a, 'tondarray') else a b = b.tondarray() if hasattr(b, 'tondarray') else b return npt.assert_array_max_ulp(a, b, maxulp, dtype)
def test_empty(self): a, edge1, edge2 = histogram2d([], [], bins=([0, 1], [0, 1])) assert_array_max_ulp(a, array([[0.]])) a, edge1, edge2 = histogram2d([], [], bins=4) assert_array_max_ulp(a, np.zeros((4, 4)))
def test_to_dataframe(self): df = self.plotter.to_DataFrame(['x', 'y'], index=0) group = self.plotter.groups[0] assert_array_max_ulp(group['x'][:], df['Time'].values) assert_array_max_ulp(group['y'][:], df['Displacement'].values)
def test_fixed_to_world(self): f = [0, 1, 100] assert_array_max_ulp(self.coords.fixed_to_world(f), [-1.0, -0.9, 9.0], dtype=np.float32)
def test_double(self): # Generate 1 + small deviation, check that adding eps gives a few UNL x = np.ones(10).astype(np.float64) x += 0.01 * np.random.randn(10).astype(np.float64) eps = np.finfo(np.float64).eps assert_array_max_ulp(x, x + eps, maxulp=200)