def test_PointAdaptive_kNN(data_Fig1, output_Fig1_labels, output_Fig1_labelsHalos, output_Fig1_borders): est = DensityPeakAdvanced(Z=1.5, n_jobs=-1) assert est.dim == None assert est.k_max == 1000 assert est.D_thr == 23.92812698 assert est.metric == "euclidean" assert est.dim_algo == "twoNN" est.fit(data_Fig1) assert hasattr(est, 'is_fitted_') assert est.k_max_ == max(est.k_hat_) print(len(data_Fig1), len(est.densities_)) assert len(data_Fig1) == len(est.densities_) assert_array_equal(est.labels_, [c - 1 for c in output_Fig1_labels["clu"]]) is_almost_equal(est.halos_, [c - 1 for c in output_Fig1_labelsHalos["clu"]], 0.0, 0) #assert_array_equal(est.halos_, output_Fig1_labelsHalos["clu"]) assert_array_equal( [est.topography_[i][0] + 1 for i in range(len(est.topography_))], output_Fig1_borders["i"]) assert_array_equal( [est.topography_[i][1] + 1 for i in range(len(est.topography_))], output_Fig1_borders["j"]) npt.assert_almost_equal( [est.topography_[i][2] for i in range(len(est.topography_))], output_Fig1_borders["rho_b"], decimal=3) npt.assert_almost_equal( [est.topography_[i][3] for i in range(len(est.topography_))], output_Fig1_borders["err_rho_b"], decimal=3)
def calculate_kl(p, files, fine_grid, n, size): kls = [] for file in files: x = np.load(file) np.random.shuffle(x) x = x[:size, :n] print(x.shape) nn = NearestNeighbors(n_neighbors=3).fit(x) dens = DensityPeakAdvanced(k_max=500).fit(x) q = predict(dens, nn, fine_grid) kl = JS(p, q) kls.append(kl) print(file, kl) return kls
def ref_prob(filename, grid, n, size, D_thr=15): x = np.load(filename)[:size, :n] nn = NearestNeighbors(n_neighbors=3).fit(x) dens = DensityPeakAdvanced(D_thr=D_thr, k_max=500).fit(x) return predict(dens, nn, grid), x
def test_metric_fail(): with pytest.raises(ValueError): _ = DensityPeakAdvanced(metric='a_metric')
def test_metric_callable(): _ = DensityPeakAdvanced(metric=lambda x, y: 1)
import pytest from sklearn.utils.estimator_checks import check_estimator from sklearn.utils.estimator_checks import ( _construct_instance, _set_checking_parameters, _get_check_estimator_ids, check_parameters_default_constructible, check_class_weight_balanced_linear_classifier, parametrize_with_checks) from Pipeline.DPA import PointAdaptive_kNN from Pipeline.DPA import twoNearestNeighbors from Pipeline.DPA import DensityPeakAdvanced @parametrize_with_checks( [DensityPeakAdvanced(), PointAdaptive_kNN(), twoNearestNeighbors()]) def test_scikitlearn_compatible_estimator(estimator, check): check(estimator)