from deepdiff import DeepDiff

from SAF.features.generator import compute_ternary_features


def test_generate_features():
    file_path = "tests/cif/ternary/URhIn.cif"
    actual_features, actual_uni_features = compute_ternary_features(file_path)
    expected_features = {
        "Entry": "URhIn",
        "Formula": "URhIn",
        "Structure": "ZrNiAl",
        "R": "U",
        "M": "Rh",
        "X": "In",
        "INT_RR_dist": 3.881,
        "INT_MM_dist": 3.881,
        "INT_XX_dist": 3.244,
        "INT_RM_dist": 2.983,
        "INT_MX_dist": 2.697,
        "INT_RX_dist": 3.21,
        "INT_Rsize": 1.377,
        "INT_Msize": 1.345,
        "INT_Xsize": 1.624,
        "INT_Rsize_by_Msize": 1.0237918215613384,
        "INT_Msize_by_Xsize": 0.8282019704433496,
        "INT_Rsize_by_Xsize": 0.8479064039408867,
        "INT_RR_dist_by_2_by_Rsize": 1.4092229484386347,
        "INT_MM_dist_by_2_by_Msize": 1.4427509293680296,
        "INT_XX_dist_by_2_by_Xsize": 0.9987684729064039,
        "INT_RM_dist_by_RMsizes": 1.0958853783982367,
        "INT_MX_dist_by_MXsizes": 0.9083866621758167,
        "INT_RX_dist_by_RXsizes": 1.0696434521826057,
        "INT_Rsize_ref": 1.4874430151209064,
        "INT_Msize_ref": 1.3560056919905379,
        "INT_Xsize_ref": 1.4864270730120939,
        "INT_percent_diff_R_by_100": 0.08020553022578532,
        "INT_percent_diff_M_by_100": 0.008182670624935252,
        "INT_percent_diff_X_by_100": -0.0847123934654595,
        "INT_RR_dist_minus_ref_diff": 0.2334743544854901,
        "INT_MM_dist_minus_ref_diff": 0.3012080948257985,
        "INT_XX_dist_minus_ref_diff": 0.08358380208872147,
        "INT_RM_dist_minus_ref_diff": 0.04678219674440349,
        "INT_MX_dist_minus_ref_diff": -0.05392390248521745,
        "INT_RX_dist_minus_ref_diff": 0.07356072020778807,
        "INT_R_factor": 0.013676072784002367,
        "INT_UNI_shortest_homoatomic_dist": 3.244,
        "INT_UNI_shortest_heteroatomic_dist": 2.697,
        "INT_UNI_shortest_homoatomic_dist_by_2_by_atom_size": 0.9987684729064039,
        "INT_UNI_shortest_heteroatomic_dist_by_sum_of_atom_sizes": 0.9083866621758167,
        "INT_UNI_shortest_homoatomic_dist_by_2_by_refined_atom_size": 1.09120725089673,
        "INT_UNI_shortest_heteroatomic_dist_by_sum_of_refined_atom_sizes": 0.948835108153386,
        "INT_UNI_highest_refined_percent_diff_abs": 0.0847123934654595,
        "INT_UNI_lowest_refined_percent_diff_abs": 0.008182670624935252,
        "INT_UNI_refined_packing_efficiency": 0.606644672780686,
        "WYK_R_lowest_wyckoff": 3,
        "WYK_M_lowest_wyckoff": 1,
        "WYK_X_lowest_wyckoff": 3,
        "WYK_identical_lowest_wyckoff_count": 1,
        "WYK_R_sites_total": 1,
        "WYK_M_sites_total": 2,
        "WYK_X_sites_total": 1,
        "WYK_R_multiplicity_total": 3,
        "WYK_M_multiplicity_total": 3,
        "WYK_X_multiplicity_total": 3,
        "ENV_R_shortest_dist_count": 3,
        "ENV_M_shortest_dist_count": 6,
        "ENV_X_shortest_dist_count": 2,
        "ENV_R_avg_shortest_dist_count": 3.0,
        "ENV_M_avg_shortest_dist_count": 4.0,
        "ENV_X_avg_shortest_dist_count": 2.0,
        "ENV_R_shortest_tol_dist_count": 5,
        "ENV_M_shortest_tol_dist_count": 6,
        "ENV_X_shortest_tol_dist_count": 2,
        "ENV_R_avg_shortest_dist_within_tol_count": 5.0,
        "ENV_M_avg_shortest_dist_within_tol_count": 7.5,
        "ENV_X_avg_shortest_dist_within_tol_count": 2.0,
        "ENV_R_second_by_first_shortest_dist": 1.000335232986926,
        "ENV_M_second_by_first_shortest_dist": 1.1294030404152762,
        "ENV_X_second_by_first_shortest_dist": 1.057471264367816,
        "ENV_R_avg_second_by_first_shortest_dist": 1.000335232986926,
        "ENV_M_avg_second_by_first_shortest_dist": 1.0648768357756606,
        "ENV_X_avg_second_by_first_shortest_dist": 1.057471264367816,
        "ENV_R_second_shortest_dist_count": 1,
        "ENV_M_second_shortest_dist_count": 3,
        "ENV_X_second_shortest_dist_count": 2,
        "ENV_R_avg_second_shortest_dist_count": 1.0,
        "ENV_M_avg_second_shortest_dist_count": 2.0,
        "ENV_X_avg_second_shortest_dist_count": 2.0,
        "ENV_R_homoatomic_dist_by_shortest_dist": 1.3010392222594702,
        "ENV_M_homoatomic_dist_by_shortest_dist": 1.4390063032999627,
        "ENV_X_homoatomic_dist_by_shortest_dist": 1.2028179458657768,
        "ENV_R_avg_homoatomic_dist_by_shortest_dist": 1.3010392222594702,
        "ENV_M_avg_homoatomic_dist_by_shortest_dist": 1.3999028711450725,
        "ENV_X_avg_homoatomic_dist_by_shortest_dist": 1.2028179458657768,
        "ENV_R_count_at_R_shortest_dist": 0,
        "ENV_M_count_at_R_shortest_dist": 3,
        "ENV_X_count_at_R_shortest_dist": 0,
        "ENV_R_avg_count_at_R_shortest_dist": 0.0,
        "ENV_M_avg_count_at_R_shortest_dist": 0.0,
        "ENV_X_avg_count_at_R_shortest_dist": 0.0,
        "ENV_R_count_at_M_shortest_dist": 0,
        "ENV_M_count_at_M_shortest_dist": 0,
        "ENV_X_count_at_M_shortest_dist": 6,
        "ENV_R_avg_count_at_M_shortest_dist": 1.5,
        "ENV_M_avg_count_at_M_shortest_dist": 0.0,
        "ENV_X_avg_count_at_M_shortest_dist": 1.0,
        "ENV_R_count_at_X_shortest_dist": 0,
        "ENV_M_count_at_X_shortest_dist": 2,
        "ENV_X_count_at_X_shortest_dist": 0,
        "ENV_R_avg_count_at_X_shortest_dist": 0.0,
        "ENV_M_avg_count_at_X_shortest_dist": 8.0,
        "ENV_X_avg_count_at_X_shortest_dist": 0.0,
        "CN_AVG_coordination_number": 11.5,
        "CN_AVG_R_atom_count": 4.5,
        "CN_AVG_M_atom_count": 2.25,
        "CN_AVG_X_atom_count": 4.75,
        "CN_AVG_polyhedron_volume": 67.79050000000001,
        "CN_AVG_central_atom_to_center_of_mass_dist": 0.07925000000000001,
        "CN_AVG_number_of_edges": 28.5,
        "CN_AVG_number_of_faces": 19.0,
        "CN_AVG_shortest_distance_to_face": 2.16375,
        "CN_AVG_shortest_distance_to_edge": 2.238625,
        "CN_AVG_volume_of_inscribed_sphere": 44.545875,
        "CN_AVG_packing_efficiency": 0.650375,
        "CN_MIN_coordination_number": 10.75,
        "CN_MIN_R_atom_count": 3.75,
        "CN_MIN_M_atom_count": 2.25,
        "CN_MIN_X_atom_count": 4.75,
        "CN_MIN_polyhedron_volume": 60.91375,
        "CN_MIN_central_atom_to_center_of_mass_dist": 0.04225,
        "CN_MIN_number_of_edges": 26.25,
        "CN_MIN_number_of_faces": 17.5,
        "CN_MIN_shortest_distance_to_face": 2.08575,
        "CN_MIN_shortest_distance_to_edge": 2.14975,
        "CN_MIN_volume_of_inscribed_sphere": 39.1475,
        "CN_MIN_packing_efficiency": 0.638,
        "CN_MAX_coordination_number": 12.25,
        "CN_MAX_R_atom_count": 5.25,
        "CN_MAX_M_atom_count": 2.25,
        "CN_MAX_X_atom_count": 4.75,
        "CN_MAX_polyhedron_volume": 74.66725,
        "CN_MAX_central_atom_to_center_of_mass_dist": 0.11625,
        "CN_MAX_number_of_edges": 30.75,
        "CN_MAX_number_of_faces": 20.5,
        "CN_MAX_shortest_distance_to_face": 2.2417499999999997,
        "CN_MAX_shortest_distance_to_edge": 2.3274999999999997,
        "CN_MAX_volume_of_inscribed_sphere": 49.94425,
        "CN_MAX_packing_efficiency": 0.66275,
    }
    diff = DeepDiff(expected_features, actual_features, significant_digits=2)
    assert diff == {}
    expected_uni_features = {
        "Entry": "URhIn",
        "Formula": "URhIn",
        "INT_UNI_shortest_homoatomic_dist": 3.244,
        "INT_UNI_shortest_heteroatomic_dist": 2.697,
        "INT_UNI_shortest_homoatomic_dist_by_2_by_atom_size": 0.9987684729064039,
        "INT_UNI_shortest_heteroatomic_dist_by_sum_of_atom_sizes": 0.9083866621758167,
        "INT_UNI_shortest_homoatomic_dist_by_2_by_refined_atom_size": 1.09120725089673,
        "INT_UNI_shortest_heteroatomic_dist_by_sum_of_refined_atom_sizes": 0.948835108153386,
        "INT_UNI_highest_refined_percent_diff_abs": 0.0847123934654595,
        "INT_UNI_lowest_refined_percent_diff_abs": 0.008182670624935252,
        "INT_UNI_refined_packing_efficiency": 0.606644672780686,
        "UNI_WYK_lowest_wyckoff": 1,
    }
    diff = DeepDiff(expected_uni_features, actual_uni_features, significant_digits=2)
    assert diff == {}
