def test_first_hill(self): # typical bimodal distribution data = np.concatenate([self.dist_norm1, self.dist_norm2])[:, np.newaxis] estimator = KernelDensity(kernel='gaussian', bandwidth=0.5) kde = estimator.fit(data) x, y = Analyze.density_func(data, kde, 100) obs_x, obs_y = Analyze.first_hill(x, y) exp_x, exp_y = 1.0971012583068704, 2.5302323352207674 self.assertAlmostEqual(obs_x, exp_x) self.assertAlmostEqual(obs_y, exp_y) # peak larger than valley data = np.negative(data) kde = estimator.fit(data) x, y = Analyze.density_func(data, kde, 100) with self.assertRaises(ValueError) as ctx: Analyze.first_hill(x, y) msg = 'Peak is larger than valley.' self.assertEqual(str(ctx.exception), msg) # unimodal distribution data = self.dist_norm1[:, np.newaxis] kde = estimator.fit(data) x, y = Analyze.density_func(data, kde, 100) with self.assertRaises(ValueError) as ctx: Analyze.first_hill(x, y) msg = 'Cannot identify at least two peaks.' self.assertEqual(str(ctx.exception), msg)
def test_plot_density(self): data = np.concatenate([self.dist_norm1, self.dist_norm2])[:, np.newaxis] estimator = KernelDensity(kernel='gaussian', bandwidth=0.5) kde = estimator.fit(data) x, y = Analyze.density_func(data, kde, 100) peak, valley = Analyze.first_hill(x, y) th = valley - (valley - peak) * 0.5 / 100 fp = join(self.tmpdir, 'tmp.png') Analyze.plot_density(x, y, peak, valley, th, fp) self.assertTrue(isfile(fp)) remove(fp)
def test_density_func(self): data = self.dist_norm1[:, np.newaxis] estimator = KernelDensity(kernel='gaussian', bandwidth=0.5) kde = estimator.fit(data) obs = Analyze.density_func(data, kde, 10) exp = (np.array([ -1.48253468, 0.0939095, 1.67035369, 3.24679787, 4.82324206, 6.39968624, 7.97613043, 9.55257461, 11.1290188, 12.70546298 ]), np.array([ 0.00104342, 0.00788705, 0.0496806, 0.13173376, 0.19176352, 0.15754466, 0.06992292, 0.02140856, 0.00150463, 0.00053637 ])) np.testing.assert_array_almost_equal(obs, exp)