def test_sem(self): # This is not in R, so used: sqrt(var(testcase)*3/4) / sqrt(3) y = mstats.sem(self.testcase) assert_almost_equal(y, 0.6454972244) n = self.testcase.count() assert_allclose(mstats.sem(self.testcase, ddof=0) * np.sqrt(n/(n-2)), mstats.sem(self.testcase, ddof=2))
def test_nd_input(self): x = np.array((-2,-1,0,1,2,3)*4)**2 x_2d = np.vstack([x] * 2).T for func in [mstats.normaltest, mstats.skewtest, mstats.kurtosistest]: res_1d = func(x) res_2d = func(x_2d) assert_allclose(res_2d[0], [res_1d[0]] * 2) assert_allclose(res_2d[1], [res_1d[1]] * 2)
def test_skewtest(self): # this test is for 1D data for n in self.get_n(): if n > 8: x, y, xm, ym = self.generate_xy_sample(n) r = stats.skewtest(x) rm = stats.mstats.skewtest(xm) assert_allclose(r[0], rm[0], rtol=1e-15)
def test_sem(self): # This is not in R, so used: sqrt(var(testcase)*3/4) / sqrt(3) # Note, differs from stats.sem return due to different ddof (backwards # compat reasons). y = mstats.sem(self.testcase) assert_almost_equal(y, 0.55901699437494745) n = self.testcase.count() assert_allclose(mstats.sem(self.testcase, ddof=0) * np.sqrt(n/(n-2)), mstats.sem(self.testcase, ddof=2))
def test_gmean(self): for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) r = stats.gmean(abs(x)) rm = stats.mstats.gmean(abs(xm)) assert_allclose(r, rm, rtol=1e-13) r = stats.gmean(abs(y)) rm = stats.mstats.gmean(abs(ym)) assert_allclose(r, rm, rtol=1e-13)
def test_normaltest(self): np.seterr(over='raise') for n in self.get_n(): if n > 8: with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=UserWarning) x, y, xm, ym = self.generate_xy_sample(n) r = stats.normaltest(x) rm = stats.mstats.normaltest(xm) assert_allclose(np.asarray(r), np.asarray(rm))
def test_sem(self): # example from stats.sem doc a = np.arange(20).reshape(5,4) am = np.ma.array(a) r = stats.sem(a,ddof=1) rm = stats.mstats.sem(am, ddof=1) assert_allclose(r, 2.82842712, atol=1e-5) assert_allclose(rm, 2.82842712, atol=1e-5) for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) assert_almost_equal(stats.mstats.sem(xm, axis=None, ddof=0), stats.sem(x, axis=None, ddof=0), decimal=13) assert_almost_equal(stats.mstats.sem(ym, axis=None, ddof=0), stats.sem(y, axis=None, ddof=0), decimal=13) assert_almost_equal(stats.mstats.sem(xm, axis=None, ddof=1), stats.sem(x, axis=None, ddof=1), decimal=13) assert_almost_equal(stats.mstats.sem(ym, axis=None, ddof=1), stats.sem(y, axis=None, ddof=1), decimal=13)
def test_maskedarray_input(self): # Add some masked values, test result doesn't change x = np.array((-2, -1, 0, 1, 2, 3) * 4) ** 2 xm = np.ma.array(np.r_[np.inf, x, 10], mask=np.r_[True, [False] * x.size, True]) assert_allclose(mstats.normaltest(xm), stats.normaltest(x)) assert_allclose(mstats.skewtest(xm), stats.skewtest(x)) assert_allclose(mstats.kurtosistest(xm), stats.kurtosistest(x))
def test_axis_None(self): # Test axis=None (equal to axis=0 for 1-D input) x = np.array((-2,-1,0,1,2,3)*4)**2 assert_allclose(mstats.normaltest(x, axis=None), mstats.normaltest(x)) assert_allclose(mstats.skewtest(x, axis=None), mstats.skewtest(x)) assert_allclose(mstats.kurtosistest(x, axis=None), mstats.kurtosistest(x))
def test_maskedarray_input(self): # Add some masked values, test result doesn't change x = np.array((-2,-1,0,1,2,3)*4)**2 xm = np.ma.array(np.r_[np.inf, x, 10], mask=np.r_[True, [False] * x.size, True]) assert_allclose(mstats.normaltest(xm), stats.normaltest(x)) assert_allclose(mstats.skewtest(xm), stats.skewtest(x)) assert_allclose(mstats.kurtosistest(xm), stats.kurtosistest(x))
def test_hilbert_theoretical_long_signals(self): """ Tests the output of the hilbert function with regard to amplitude and phase of long test signals """ # Performing test using all pad types for padding in ['nextpow', 'none', 16384]: h = elephant.signal_processing.hilbert( self.long_signals, N=padding) phase = np.angle(h.magnitude) amplitude = np.abs(h.magnitude) real_value = np.real(h.magnitude) # The real part should be equal to the original long_signals assert_array_almost_equal( real_value, self.long_signals.magnitude, decimal=14) # Test only in the middle half of the array (border effects) ind1 = int(len(h.times) / 4) ind2 = int(3 * len(h.times) / 4) # Calculate difference in phase between signal and original phase # and use smaller of any two phase differences phasediff = np.abs(phase[ind1:ind2, :] - self.phase[ind1:ind2, :]) phasediff[phasediff >= np.pi] = \ 2 * np.pi - phasediff[phasediff >= np.pi] # Calculate difference in amplitude between signal and original # amplitude amplitudediff = \ amplitude[ind1:ind2, :] - self.amplitude[ind1:ind2, :] # assert_allclose(phasediff, 0, atol=0.1) assert_allclose(amplitudediff, 0, atol=0.5)
def test_zscore(self): for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) #reference solution zx = (x - x.mean()) / x.std() zy = (y - y.mean()) / y.std() #validate stats assert_allclose(stats.zscore(x), zx, rtol=1e-10) assert_allclose(stats.zscore(y), zy, rtol=1e-10) #compare stats and mstats assert_allclose(stats.zscore(x), stats.mstats.zscore(xm[0:len(x)]), rtol=1e-10) assert_allclose(stats.zscore(y), stats.mstats.zscore(ym[0:len(y)]), rtol=1e-10)
def test_sem(self): # example from stats.sem doc a = np.arange(20).reshape(5, 4) am = np.ma.array(a) r = stats.sem(a, ddof=1) rm = stats.mstats.sem(am, ddof=1) assert_allclose(r, 2.82842712, atol=1e-5) assert_allclose(rm, 2.82842712, atol=1e-5) for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) assert_almost_equal(stats.mstats.sem(xm, axis=None, ddof=0), stats.sem(x, axis=None, ddof=0), decimal=13) assert_almost_equal(stats.mstats.sem(ym, axis=None, ddof=0), stats.sem(y, axis=None, ddof=0), decimal=13) assert_almost_equal(stats.mstats.sem(xm, axis=None, ddof=1), stats.sem(x, axis=None, ddof=1), decimal=13) assert_almost_equal(stats.mstats.sem(ym, axis=None, ddof=1), stats.sem(y, axis=None, ddof=1), decimal=13)
def test_perfect_locking_many_spiketrains_one_signal(self): phases, amps, times = elephant.phase_analysis.spike_triggered_phase( elephant.signal_processing.hilbert(self.anasig0), [self.st0, self.st0], interpolate=True) assert_allclose(phases[0], -np.pi / 2.) assert_allclose(amps[0], 1, atol=0.1) assert_allclose(times[0].magnitude, self.st0.magnitude) self.assertEqual(len(phases[0]), len(self.st0)) self.assertEqual(len(amps[0]), len(self.st0)) self.assertEqual(len(times[0]), len(self.st0))
def test_vs_nonmasked(self): np.random.seed(1234567) outcome = np.random.randn(20, 4) + [0, 0, 1, 2] # 1-D inputs res1 = stats.ttest_1samp(outcome[:, 0], 1) res2 = mstats.ttest_1samp(outcome[:, 0], 1) assert_allclose(res1, res2) # 2-D inputs res1 = stats.ttest_1samp(outcome[:, 0], outcome[:, 1], axis=None) res2 = mstats.ttest_1samp(outcome[:, 0], outcome[:, 1], axis=None) assert_allclose(res1, res2) res1 = stats.ttest_1samp(outcome[:, :2], outcome[:, 2:], axis=0) res2 = mstats.ttest_1samp(outcome[:, :2], outcome[:, 2:], axis=0) assert_allclose(res1, res2) # Check default is axis=0 res3 = mstats.ttest_1samp(outcome[:, :2], outcome[:, 2:]) assert_allclose(res2, res3)
def test_rankdata(self): for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) r = stats.rankdata(x) rm = stats.mstats.rankdata(x) assert_allclose(r, rm)
def test_vs_nonmasked(self): np.random.seed(1234567) outcome = np.random.randn(20, 4) + [0, 0, 1, 2] # 1-D inputs res1 = stats.ttest_ind(outcome[:, 0], outcome[:, 1]) res2 = mstats.ttest_ind(outcome[:, 0], outcome[:, 1]) assert_allclose(res1, res2) # 2-D inputs res1 = stats.ttest_ind(outcome[:, 0], outcome[:, 1], axis=None) res2 = mstats.ttest_ind(outcome[:, 0], outcome[:, 1], axis=None) assert_allclose(res1, res2) res1 = stats.ttest_ind(outcome[:, :2], outcome[:, 2:], axis=0) res2 = mstats.ttest_ind(outcome[:, :2], outcome[:, 2:], axis=0) assert_allclose(res1, res2) # Check default is axis=0 res3 = mstats.ttest_ind(outcome[:, :2], outcome[:, 2:]) assert_allclose(res2, res3) # Check equal_var res4 = stats.ttest_ind(outcome[:, 0], outcome[:, 1], equal_var=True) res5 = mstats.ttest_ind(outcome[:, 0], outcome[:, 1], equal_var=True) assert_allclose(res4, res5) res4 = stats.ttest_ind(outcome[:, 0], outcome[:, 1], equal_var=False) res5 = mstats.ttest_ind(outcome[:, 0], outcome[:, 1], equal_var=False) assert_allclose(res4, res5)
def test_linregress(self): for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) res1 = stats.linregress(x, y) res2 = stats.mstats.linregress(xm, ym) assert_allclose(np.asarray(res1), np.asarray(res2))
def test_zmap(self): for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) z = stats.zmap(x, y) zm = stats.mstats.zmap(xm, ym) assert_allclose(z, zm[0:len(z)], atol=1e-10)
def test_skewtest_2D_notmasked(self): # a normal ndarray is passed to the masked function x = np.random.random((20, 2)) * 20. r = stats.skewtest(x) rm = stats.mstats.skewtest(x) assert_allclose(np.asarray(r), np.asarray(rm))
def test_zmap(self): for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) z = stats.zmap(x,y) zm = stats.mstats.zmap(xm,ym) assert_allclose(z, zm[0:len(z)], atol=1e-10)
def test_trimboth(self): a = np.arange(20) b = stats.trimboth(a, 0.1) bm = stats.mstats.trimboth(a, 0.1) assert_allclose(b, bm.data[~bm.mask])
def test_trimboth(self): a = np.arange(20) b = stats.trimboth(a, 0.1) bm = stats.mstats.trimboth(a, 0.1) assert_allclose(np.sort(b), bm.data[~bm.mask])