From 0a22792ef1b720c9b0f0aa6f02af3f9da494329c Mon Sep 17 00:00:00 2001 From: Ajinkya Hire Date: Tue, 26 Sep 2023 13:34:25 -0400 Subject: [PATCH] Added one more unit test which fails with current get_columns_to_drop_3b algorithm --- tests/test_optimize.py | 148 ++++++++++++++++++++++++++++++++--------- 1 file changed, 118 insertions(+), 30 deletions(-) diff --git a/tests/test_optimize.py b/tests/test_optimize.py index 8505a226..1589e5ca 100644 --- a/tests/test_optimize.py +++ b/tests/test_optimize.py @@ -12,24 +12,59 @@ def Nb_Sn_chemistry(): yield chemistry_config @pytest.fixture() -def bspline_config_larger_cutoff(Nb_Sn_chemistry): +def config_1(): + min_max_ks = {} + min_max_ks["rmin_2b"] = 0.01 + min_max_ks["rmax_2b"] = 6.01 + min_max_ks["rmin_3b"] = 0.8 + min_max_ks["rmax_3b"] = 4 + min_max_ks["knot_spacing_2b"] = 0.4 + min_max_ks["knot_spacing_3b"] = 0.8 + yield min_max_ks + + +@pytest.fixture() +def bspline_config_larger_cutoff(Nb_Sn_chemistry,config_1): element_list = ['Nb', 'Sn'] chemistry_config = composition.ChemicalSystem(element_list,degree=3) - bspline_config = optimize.get_bspline_config(Nb_Sn_chemistry, rmin=0, - rmax_2b=6, rmax_3b=4, - knot_spacing=0.4) + bspline_config = optimize.get_bspline_config(Nb_Sn_chemistry, + rmin_2b=config_1["rmin_2b"], + rmin_3b=config_1["rmin_3b"], + rmax_2b=config_1["rmax_2b"], + rmax_3b=config_1["rmax_3b"], + knot_spacing_2b=config_1["knot_spacing_2b"], + knot_spacing_3b=config_1["knot_spacing_3b"], + leading_trim=0, + trailing_trim=3) yield bspline_config +@pytest.fixture() +def config_2(): + min_max_ks = {} + min_max_ks["rmin_2b"] = 0.1 + min_max_ks["rmax_2b"] = 9.1 + min_max_ks["rmin_3b"] = 0.9 + min_max_ks["rmax_3b"] = 7.2 + min_max_ks["knot_spacing_2b"] = 0.3 + min_max_ks["knot_spacing_3b"] = 0.9 + yield min_max_ks @pytest.fixture() -def bspline_config_smaller_cutoff(Nb_Sn_chemistry): +def bspline_config_larger_cutoff_2(Nb_Sn_chemistry,config_2): element_list = ['Nb', 'Sn'] chemistry_config = composition.ChemicalSystem(element_list,degree=3) - bspline_config = optimize.get_bspline_config(Nb_Sn_chemistry, rmin=0, - rmax_2b=4.4, rmax_3b=3.2, - knot_spacing=0.4) + bspline_config = optimize.get_bspline_config(Nb_Sn_chemistry, + rmin_2b=config_2["rmin_2b"], + rmin_3b=config_2["rmin_3b"], + rmax_2b=config_2["rmax_2b"], + rmax_3b=config_2["rmax_3b"], + knot_spacing_2b=config_2["knot_spacing_2b"], + knot_spacing_3b=config_2["knot_spacing_3b"], + leading_trim=0, + trailing_trim=3) yield bspline_config + @pytest.fixture() def Nb3Sn_geom(): geom = ase.Atoms('Nb6Sn2', @@ -50,7 +85,7 @@ def Nb3Sn_geom(): class TestOptimize: - + """ def test_get_bspline_config(self,bspline_config_larger_cutoff): for i in bspline_config_larger_cutoff.interactions_map[2]: @@ -63,41 +98,94 @@ def test_get_bspline_config(self,bspline_config_larger_cutoff): assert bspline_config_larger_cutoff.r_max_map[i] == [4, 4, 8] assert bspline_config_larger_cutoff.resolution_map[i] == [10,10,20] - def test_get_possible_lower_cutoffs(self,bspline_config_larger_cutoff): cutoff_dict = optimize.get_possible_lower_cutoffs(bspline_config_larger_cutoff) - + print(cutoff_dict) assert np.allclose(cutoff_dict['rmax_2b_poss'],np.array([0.4, 0.8, 1.2,\ 1.6, 2. , 2.4, 2.8, 3.2, 3.6, 4. , 4.4, 4.8, 5.2, 5.6, 6. ])) assert np.allclose(cutoff_dict['rmax_3b_poss'],np.array([0.8, 1.6, 2.4, 3.2, 4. ])) + """ - def test_drop_columns(self,bspline_config_larger_cutoff,Nb_Sn_chemistry, Nb3Sn_geom): + def test_drop_columns(self,bspline_config_larger_cutoff,Nb_Sn_chemistry, Nb3Sn_geom, config_1): + + cutoff_dict = optimize.get_possible_lower_cutoffs(bspline_config_larger_cutoff) - bspline_handler_larger_cutoff = BasisFeaturizer(bspline_config_larger_cutoff) + for i in range(len(cutoff_dict['rmax_2b_poss'])): + for j in range(len(cutoff_dict['rmax_3b_poss'])): + bspline_config_smaller_cutoff = optimize.get_bspline_config(Nb_Sn_chemistry, + rmin_2b=config_1["rmin_2b"], + rmin_3b=config_1["rmin_3b"], + rmax_2b=cutoff_dict['rmax_2b_poss'][i], + rmax_3b=cutoff_dict['rmax_3b_poss'][j], + knot_spacing_2b=config_1["knot_spacing_2b"], + knot_spacing_3b=config_1["knot_spacing_3b"], + leading_trim=0, + trailing_trim=3) + + bspline_handler_larger_cutoff = BasisFeaturizer(bspline_config_larger_cutoff) + bspline_handler_smaller_cutoff = BasisFeaturizer(bspline_config_smaller_cutoff) - columns_to_drop_2b = optimize.get_columns_to_drop_2b(original_bspline_config=bspline_config_larger_cutoff, - modify_2b_cutoff=4.4, - knot_spacing=0.4) + columns_to_drop_2b = optimize.get_columns_to_drop_2b(original_bspline_config=bspline_config_larger_cutoff, + modify_2b_cutoff=cutoff_dict['rmax_2b_poss'][i], + knot_spacing=config_1["knot_spacing_2b"]) - columns_to_drop_3b = optimize.get_columns_to_drop_3b(original_bspline_config=bspline_config_larger_cutoff, - modify_3b_cutoff=3.2, - knot_spacing=0.4) + columns_to_drop_3b = optimize.get_columns_to_drop_3b(original_bspline_config=bspline_config_larger_cutoff, + modify_3b_cutoff=cutoff_dict['rmax_3b_poss'][j], + knot_spacing=config_1["knot_spacing_3b"]) + + columns_to_drop = columns_to_drop_2b + columns_to_drop_3b + + assert len(bspline_handler_larger_cutoff.columns) - len(columns_to_drop) == len(bspline_handler_smaller_cutoff.columns) + + energy_feature_larger_cutoff = bspline_handler_larger_cutoff.evaluate_configuration(geom=Nb3Sn_geom,energy=-69.44185169) + energy_feature_smaller_cutoff = bspline_handler_smaller_cutoff.evaluate_configuration(geom=Nb3Sn_geom,energy=-69.44185169) + + index_to_drop = np.where(np.isin(bspline_handler_larger_cutoff.columns,columns_to_drop))[0] + energy_feature_from_larger_cutoff = np.delete(energy_feature_larger_cutoff['energy'],index_to_drop) + + assert np.allclose(energy_feature_smaller_cutoff['energy'],energy_feature_from_larger_cutoff) + + + def test_drop_columns_2(self,bspline_config_larger_cutoff_2,Nb_Sn_chemistry, Nb3Sn_geom, config_2): + + cutoff_dict = optimize.get_possible_lower_cutoffs(bspline_config_larger_cutoff_2) + print(bspline_config_larger_cutoff_2.knots_map[('Nb','Nb','Nb')]) + for i in range(len(cutoff_dict['rmax_2b_poss'])): + for j in range(len(cutoff_dict['rmax_3b_poss'])): + print(cutoff_dict['rmax_2b_poss'][i],cutoff_dict['rmax_3b_poss'][j]) + bspline_config_smaller_cutoff = optimize.get_bspline_config(Nb_Sn_chemistry, + rmin_2b=config_2["rmin_2b"], + rmin_3b=config_2["rmin_3b"], + rmax_2b=cutoff_dict['rmax_2b_poss'][i], + rmax_3b=cutoff_dict['rmax_3b_poss'][j], + knot_spacing_2b=config_2["knot_spacing_2b"], + knot_spacing_3b=config_2["knot_spacing_3b"], + leading_trim=0, + trailing_trim=3) + + print(bspline_config_smaller_cutoff.knots_map[('Nb','Nb','Nb')]) + + bspline_handler_larger_cutoff = BasisFeaturizer(bspline_config_larger_cutoff_2) + bspline_handler_smaller_cutoff = BasisFeaturizer(bspline_config_smaller_cutoff) + + columns_to_drop_2b = optimize.get_columns_to_drop_2b(original_bspline_config=bspline_config_larger_cutoff_2, + modify_2b_cutoff=cutoff_dict['rmax_2b_poss'][i], + knot_spacing=config_2["knot_spacing_2b"]) - columns_to_drop = columns_to_drop_2b + columns_to_drop_3b + columns_to_drop_3b = optimize.get_columns_to_drop_3b(original_bspline_config=bspline_config_larger_cutoff_2, + modify_3b_cutoff=cutoff_dict['rmax_3b_poss'][j], + knot_spacing=config_2["knot_spacing_3b"]) - bspline_config_smaller_cutoff = optimize.get_bspline_config(Nb_Sn_chemistry, rmin=0, - rmax_2b=4.4, rmax_3b=3.2, - knot_spacing=0.4) - bspline_handler_smaller_cutoff = BasisFeaturizer(bspline_config_smaller_cutoff) + columns_to_drop = columns_to_drop_2b + columns_to_drop_3b - assert len(bspline_handler_larger_cutoff.columns) - len(columns_to_drop) == len(bspline_handler_smaller_cutoff.columns) + assert len(bspline_handler_larger_cutoff.columns) - len(columns_to_drop) == len(bspline_handler_smaller_cutoff.columns) - energy_feature_larger_cutoff = bspline_handler_larger_cutoff.evaluate_configuration(geom=Nb3Sn_geom,energy=-69.44185169) - energy_feature_smaller_cutoff = bspline_handler_smaller_cutoff.evaluate_configuration(geom=Nb3Sn_geom,energy=-69.44185169) + energy_feature_larger_cutoff = bspline_handler_larger_cutoff.evaluate_configuration(geom=Nb3Sn_geom,energy=-69.44185169) + energy_feature_smaller_cutoff = bspline_handler_smaller_cutoff.evaluate_configuration(geom=Nb3Sn_geom,energy=-69.44185169) - index_to_drop = np.where(np.isin(bspline_handler_larger_cutoff.columns,columns_to_drop))[0] - energy_feature_from_larger_cutoff = np.delete(energy_feature_larger_cutoff['energy'],index_to_drop) + index_to_drop = np.where(np.isin(bspline_handler_larger_cutoff.columns,columns_to_drop))[0] + energy_feature_from_larger_cutoff = np.delete(energy_feature_larger_cutoff['energy'],index_to_drop) - assert np.allclose(energy_feature_smaller_cutoff['energy'],energy_feature_from_larger_cutoff) + assert np.allclose(energy_feature_smaller_cutoff['energy'],energy_feature_from_larger_cutoff)