from deepdiff import DeepDiff

from CAF.features import ternary


def test_generate_features(oliynyk_db):
    formula = "NdSi2Th2"
    actual_features = ternary.generate_features(formula, oliynyk_db)
    expected_features = {
        "formula": "NdSi2Th2",
        # index - 9 features
        "index_R": 1.0,
        "index_M": 2.0,
        "index_X": 2.0,
        "index_R_norm": 0.2,
        "index_M_norm": 0.4,
        "index_X_norm": 0.4,
        "index_max": 2.0,
        "index_min": 1.0,
        "index_avg": 1.6666666666666667,
        # atomic_weight - 4 features
        "atomic_weight_RMX_sum_weighted": 664.4892,
        "atomic_weight_R/M": 5.135817414680173,
        "atomic_weight_M/X": 0.12103831224268774,
        "atomic_weight_R/X": 0.6216306718594921,
        # atomic number - 8 features
        "atomic_number_R-M": 46,
        "atomic_number_M-X": -76,
        "atomic_number_R-X": -30,
        "atomic_number_RMX_avg": 54.666666666666664,
        "atomic_number_RMX_avg_weighted": 53.6,
        "atomic_number_RM_avg": 37.0,
        "atomic_number_MX_avg": 52.0,
        "atomic_number_RX_avg": 75.0,
        # period - 7 features
        "period_R": 6,
        "period_M": 3,
        "period_X": 7,
        "period_RMX_sum_weighted_norm": 5.200000000000001,
        "period_RM_sum_weighted_norm": 4.0,
        "period_MX_sum_weighted_norm": 5.0,
        "period_RX_sum_weighted_norm": 6.666666666666666,
        # group - 10 features
        "group_R": 3,
        "group_M": 14,
        "group_X": 3,
        "group_R-M": -11,
        "group_M-X": 11,
        "group_R-X": 0,
        "group_RMX_sum_weighted_norm": 7.400000000000001,
        "group_RM_sum_weighted_norm": 10.333333333333334,
        "group_MX_sum_weighted_norm": 8.5,
        "group_RX_sum_weighted_norm": 3.0,
        # Mendeleev number - 14 features
        "Mendeleev_number_R": 19,
        "Mendeleev_number_M": 78,
        "Mendeleev_number_X": 16,
        "Mendeleev_number_R-M": -59,
        "Mendeleev_number_M-X": 62,
        "Mendeleev_number_R-X": 3,
        "Mendeleev_number_RMX_avg": 37.666666666666664,
        "Mendeleev_number_RMX_avg_weighted": 41.4,
        "Mendeleev_number_RM_avg": 48.5,
        "Mendeleev_number_MX_avg": 47.0,
        "Mendeleev_number_RX_avg": 17.5,
        "Mendeleev_number_RM_sum_weighted_norm": 58.33333333333332,
        "Mendeleev_number_MX_sum_weighted_norm": 47.0,
        "Mendeleev_number_RX_sum_weighted_norm": 17.0,
        # valence total - 9 features
        "valencee_total_R": 6,
        "valencee_total_M": 4,
        "valencee_total_X": 4,
        "valencee_total_RMX_sum": 14,
        "valencee_total_RMX_sum_weighted": 22.0,
        "valencee_total_RMX_sum_weighted_norm": 4.4,
        "valencee_total_RM_sum_weighted_norm": 4.666666666666666,
        "valencee_total_MX_sum_weighted_norm": 4.0,
        "valencee_total_RX_sum_weighted_norm": 4.666666666666666,
        # unpaired_electrons - 9 features
        "unpaired_electrons_R": 4,
        "unpaired_electrons_M": 2,
        "unpaired_electrons_X": 2,
        "unpaired_electrons_RMX_sum": 8,
        "unpaired_electrons_RMX_sum_weighted": 12.0,
        "unpaired_electrons_RMX_sum_weighted_norm": 2.4000000000000004,
        "unpaired_electrons_RM_sum_weighted_norm": 2.6666666666666665,
        "unpaired_electrons_MX_sum_weighted_norm": 2.0,
        "unpaired_electrons_RX_sum_weighted_norm": 2.6666666666666665,
        # Gilman - 9 features
        "Gilman_R": 3,
        "Gilman_M": 4,
        "Gilman_X": 2,
        "Gilman_RMX_sum": 9,
        "Gilman_RMX_sum_weighted": 15.0,
        "Gilman_RMX_sum_weighted_norm": 3.0,
        "Gilman_RM_sum_weighted_norm": 3.6666666666666665,
        "Gilman_MX_sum_weighted_norm": 3.0000000000000004,
        "Gilman_RX_sum_weighted_norm": 2.333333333333333,
        # Z_eff - 9 features
        "Z_eff_R": 3.822,
        "Z_eff_M": 2.321,
        "Z_eff_X": 4.679,
        "Z_eff_R/M": 1.6467040068935803,
        "Z_eff_M/X": 0.4960461637101945,
        "Z_eff_R/X": 0.8168412053857661,
        "Z_eff_max": 4.679,
        "Z_eff_min": 2.321,
        "Z_eff_avg": 3.6073333333333335,
        # ionization_energy - 9 features
        "ionization_energy_R": 5.525,
        "ionization_energy_M": 8.1517,
        "ionization_energy_X": 6.3067,
        "ionization_energy_R/M": 0.6777727345216336,
        "ionization_energy_M/X": 1.2925460224840248,
        "ionization_energy_R/X": 0.8760524521540584,
        "ionization_energy_max": 8.1517,
        "ionization_energy_min": 5.525,
        "ionization_energy_avg": 6.661133333333333,
        # coordination_number - 9 features
        "coordination_number_R": 12,
        "coordination_number_M": 4,
        "coordination_number_X": 12,
        "coordination_number_R/M": 3.0,
        "coordination_number_M/X": 0.3333333333333333,
        "coordination_number_R/X": 1.0,
        "coordination_number_max": 12,
        "coordination_number_min": 4,
        "coordination_number_avg": 9.333333333333334,
        # ratio_closest - 9 features
        "ratio_closest_R": 0.5,
        "ratio_closest_M": 1.0,
        "ratio_closest_X": 1.0,
        "ratio_closest_R/M": 0.5,
        "ratio_closest_M/X": 1.0,
        "ratio_closest_R/X": 0.5,
        "ratio_closest_max": 1.0,
        "ratio_closest_min": 0.5,
        "ratio_closest_avg": 0.8333333333333334,
        # polyhedron_distortion - 9 features
        "polyhedron_distortion_R": 0.992,
        "polyhedron_distortion_M": 1.0,
        "polyhedron_distortion_X": 1.0,
        "polyhedron_distortion_R/M": 0.992,
        "polyhedron_distortion_M/X": 1.0,
        "polyhedron_distortion_R/X": 0.992,
        "polyhedron_distortion_max": 1.0,
        "polyhedron_distortion_min": 0.992,
        "polyhedron_distortion_avg": 0.9973333333333333,
        # CIF_radius - 9 features
        "CIF_radius_R": 1.813,
        "CIF_radius_M": 1.176,
        "CIF_radius_X": 1.798,
        "CIF_radius_R/M": 1.5416666666666667,
        "CIF_radius_M/X": 0.6540600667408231,
        "CIF_radius_R/X": 1.0083426028921023,
        "CIF_radius_max": 1.813,
        "CIF_radius_min": 1.176,
        "CIF_radius_avg": 1.5956666666666666,
        # Pauling_radius_CN12 - 9 features
        "Pauling_radius_CN12_R": 1.818,
        "Pauling_radius_CN12_M": 1.316,
        "Pauling_radius_CN12_X": 1.795,
        "Pauling_radius_CN12_R/M": 1.3814589665653496,
        "Pauling_radius_CN12_M/X": 0.7331476323119778,
        "Pauling_radius_CN12_R/X": 1.0128133704735376,
        "Pauling_radius_CN12_max": 1.818,
        "Pauling_radius_CN12_min": 1.316,
        "Pauling_radius_CN12_avg": 1.643,
        # Pauling_EN - 9 features
        "Pauling_EN_R": 1.14,
        "Pauling_EN_M": 1.9,
        "Pauling_EN_X": 1.3,
        "Pauling_EN_R/M": 0.6,
        "Pauling_EN_M/X": 1.4615384615384615,
        "Pauling_EN_R/X": 0.8769230769230768,
        "Pauling_EN_max": 1.9,
        "Pauling_EN_min": 1.14,
        "Pauling_EN_avg": 1.4466666666666665,
        # Martynov_Batsanov_EN - 9 features
        "Martynov_Batsanov_EN_R": 1.2,
        "Martynov_Batsanov_EN_M": 1.98,
        "Martynov_Batsanov_EN_X": 1.3,
        "Martynov_Batsanov_EN_R/M": 0.6060606060606061,
        "Martynov_Batsanov_EN_M/X": 1.523076923076923,
        "Martynov_Batsanov_EN_R/X": 0.923076923076923,
        "Martynov_Batsanov_EN_max": 1.98,
        "Martynov_Batsanov_EN_min": 1.2,
        "Martynov_Batsanov_EN_avg": 1.4933333333333332,
        # melting_point_K - 9 features
        "melting_point_K_R": 1289.15,
        "melting_point_K_M": 1683.15,
        "melting_point_K_X": 2023.15,
        "melting_point_K_R/M": 0.7659150996643199,
        "melting_point_K_M/X": 0.831945233917406,
        "melting_point_K_R/X": 0.6371994167511059,
        "melting_point_K_max": 2023.15,
        "melting_point_K_min": 1289.15,
        "melting_point_K_avg": 1665.1500000000003,
        # density - 9 features
        "density_R": 7.0,
        "density_M": 2.33,
        "density_X": 11.7,
        "density_R/M": 3.004291845493562,
        "density_M/X": 0.19914529914529916,
        "density_R/X": 0.5982905982905983,
        "density_max": 11.7,
        "density_min": 2.33,
        "density_avg": 7.010000000000001,
        # specific_heat - 9 features
        "specific_heat_R": 0.19,
        "specific_heat_M": 0.71,
        "specific_heat_X": 0.12,
        "specific_heat_R/M": 0.26760563380281693,
        "specific_heat_M/X": 5.916666666666667,
        "specific_heat_R/X": 1.5833333333333335,
        "specific_heat_max": 0.71,
        "specific_heat_min": 0.12,
        "specific_heat_avg": 0.34,
        # cohesive_energy - 9 features
        "cohesive_energy_R": 3.4,
        "cohesive_energy_M": 4.63,
        "cohesive_energy_X": 6.2,
        "cohesive_energy_R/M": 0.734341252699784,
        "cohesive_energy_M/X": 0.746774193548387,
        "cohesive_energy_R/X": 0.5483870967741935,
        "cohesive_energy_max": 6.2,
        "cohesive_energy_min": 3.4,
        "cohesive_energy_avg": 4.743333333333333,
        # bulk_modulus - 9 features
        "bulk_modulus_R": 31.8,
        "bulk_modulus_M": 98.0,
        "bulk_modulus_X": 54.0,
        "bulk_modulus_R/M": 0.32448979591836735,
        "bulk_modulus_M/X": 1.8148148148148149,
        "bulk_modulus_R/X": 0.5888888888888889,
        "bulk_modulus_max": 98.0,
        "bulk_modulus_min": 31.8,
        "bulk_modulus_avg": 61.26666666666667,
    }

    diff = DeepDiff(expected_features, actual_features, significant_digits=3)
    assert diff == {}
