from deepdiff import DeepDiff

from CAF.features import quaternary


def test_generate_features(oliynyk_db):
    formula = "YNdThSi2"
    actual_features = quaternary.generate_features(formula, oliynyk_db)
    expected_features = {
        "formula": "YNdThSi2",
        "index_A": 1.0,
        "index_B": 1.0,
        "index_C": 1.0,
        "index_D": 2.0,
        "index_A_norm": 0.2,
        "index_B_norm": 0.2,
        "index_C_norm": 0.2,
        "index_D_norm": 0.4,
        "index_max": 2.0,
        "index_min": 1.0,
        "index_avg": 1.25,
        "atomic_weight_ABCD_sum_weighted": 521.35694,
        "atomic_weight_A/B": 0.6163658296473982,
        "atomic_weight_A/C": 0.3831519047949453,
        "atomic_weight_A/D": 3.1655423617169003,
        "atomic_weight_B/C": 0.6216306718594921,
        "atomic_weight_B/D": 5.135817414680173,
        "atomic_weight_C/D": 8.261846860479606,
        "atomic_number_A-B": -21,
        "atomic_number_A-C": -51,
        "atomic_number_A-D": 25,
        "atomic_number_B-C": -30,
        "atomic_number_B-D": 46,
        "atomic_number_C-D": 76,
        "atomic_number_ABCD_avg": 50.75,
        "atomic_number_ABCD_avg_weighted": 43.4,
        "atomic_number_AB_avg": 49.5,
        "atomic_number_AC_avg": 64.5,
        "atomic_number_AD_avg": 26.5,
        "atomic_number_BC_avg": 75.0,
        "atomic_number_BD_avg": 37.0,
        "atomic_number_CD_avg": 52.0,
        "period_A": 5,
        "period_B": 6,
        "period_C": 7,
        "period_D": 3,
        "period_ABCD_sum_weighted_norm": 4.800000000000001,
        "period_AB_sum_weighted_norm": 5.5,
        "period_AC_sum_weighted_norm": 6.000000000000001,
        "period_AD_sum_weighted_norm": 3.6666666666666665,
        "period_BC_sum_weighted_norm": 6.500000000000001,
        "period_BD_sum_weighted_norm": 4.0,
        "period_CD_sum_weighted_norm": 4.333333333333334,
        "group_A": 3,
        "group_B": 3,
        "group_C": 3,
        "group_D": 14,
        "group_A-B": 0,
        "group_A-C": 0,
        "group_A-D": -11,
        "group_B-C": 0,
        "group_B-D": -11,
        "group_C-D": -11,
        "group_ABCD_sum_weighted_norm": 7.4,
        "group_AB_sum_weighted_norm": 3.0000000000000004,
        "group_AC_sum_weighted_norm": 3.0000000000000004,
        "group_AD_sum_weighted_norm": 10.333333333333334,
        "group_BC_sum_weighted_norm": 3.0000000000000004,
        "group_BD_sum_weighted_norm": 10.333333333333334,
        "group_CD_sum_weighted_norm": 10.333333333333334,
        "Mendeleev_number_A": 12,
        "Mendeleev_number_B": 19,
        "Mendeleev_number_C": 16,
        "Mendeleev_number_D": 78,
        "Mendeleev_number_A-B": -7,
        "Mendeleev_number_A-C": -4,
        "Mendeleev_number_A-D": -66,
        "Mendeleev_number_B-C": 3,
        "Mendeleev_number_B-D": -59,
        "Mendeleev_number_C-D": -62,
        "Mendeleev_number_ABCD_avg": 31.25,
        "Mendeleev_number_ABCD_avg_weighted": 40.6,
        "Mendeleev_number_AB_avg": 15.5,
        "Mendeleev_number_AC_avg": 14.0,
        "Mendeleev_number_AD_avg": 45.0,
        "Mendeleev_number_BC_avg": 17.5,
        "Mendeleev_number_BD_avg": 48.5,
        "Mendeleev_number_CD_avg": 47.0,
        "Mendeleev_number_AB_weighted_norm": 15.500000000000002,
        "Mendeleev_number_AC_weighted_norm": 14.0,
        "Mendeleev_number_AD_weighted_norm": 55.99999999999999,
        "Mendeleev_number_BC_weighted_norm": 17.5,
        "Mendeleev_number_BD_weighted_norm": 58.33333333333332,
        "Mendeleev_number_CD_weighted_norm": 57.333333333333336,
        "valencee_total_A": 3,
        "valencee_total_B": 6,
        "valencee_total_C": 4,
        "valencee_total_D": 4,
        "valencee_total_ABCD_sum": 17,
        "valencee_total_ABCD_sum_weighted": 21.0,
        "valencee_total_ABCD_sum_weighted_norm": 4.200000000000001,
        "valencee_total_AB_sum_weighted_norm": 4.5,
        "valencee_total_AC_sum_weighted_norm": 3.5,
        "valencee_total_AD_sum_weighted_norm": 3.6666666666666665,
        "valencee_total_BC_sum_weighted_norm": 5.0,
        "valencee_total_BD_sum_weighted_norm": 4.666666666666666,
        "valencee_total_CD_sum_weighted_norm": 4.0,
        "unpaired_electrons_A": 1,
        "unpaired_electrons_B": 4,
        "unpaired_electrons_C": 2,
        "unpaired_electrons_D": 2,
        "unpaired_electrons_ABCD_sum": 9,
        "unpaired_electrons_ABCD_sum_weighted": 11.0,
        "unpaired_electrons_ABCD_sum_weighted_norm": 2.2,
        "unpaired_electrons_AB_sum_weighted_norm": 2.5,
        "unpaired_electrons_AC_sum_weighted_norm": 1.5000000000000002,
        "unpaired_electrons_AD_sum_weighted_norm": 1.6666666666666665,
        "unpaired_electrons_BC_sum_weighted_norm": 3.0000000000000004,
        "unpaired_electrons_BD_sum_weighted_norm": 2.6666666666666665,
        "unpaired_electrons_CD_sum_weighted_norm": 2.0,
        "Gilman_A": 1,
        "Gilman_B": 3,
        "Gilman_C": 2,
        "Gilman_D": 4,
        "Gilman_ABCD_sum": 10,
        "Gilman_ABCD_sum_weighted": 14.0,
        "Gilman_ABCD_sum_weighted_norm": 2.8000000000000003,
        "Gilman_AB_sum_weighted_norm": 2.0,
        "Gilman_AC_sum_weighted_norm": 1.5000000000000002,
        "Gilman_AD_sum_weighted_norm": 2.9999999999999996,
        "Gilman_BC_sum_weighted_norm": 2.5,
        "Gilman_BD_sum_weighted_norm": 3.6666666666666665,
        "Gilman_CD_sum_weighted_norm": 3.333333333333333,
        "Z_eff_A": 3.379,
        "Z_eff_B": 3.822,
        "Z_eff_C": 4.679,
        "Z_eff_D": 2.321,
        "Z_eff_A/B": 0.8840920983778127,
        "Z_eff_A/C": 0.7221628553109638,
        "Z_eff_A/D": 1.4558380008616973,
        "Z_eff_B/C": 0.8168412053857661,
        "Z_eff_B/D": 1.6467040068935803,
        "Z_eff_C/D": 2.015941404566997,
        "Z_eff_ABCD_max": 4.679,
        "Z_eff_ABCD_min": 2.321,
        "Z_eff_ABCD_avg": 3.55025,
        "ionization_energy_A": 6.2173,
        "ionization_energy_B": 5.525,
        "ionization_energy_C": 6.3067,
        "ionization_energy_D": 8.1517,
        "ionization_energy_A/B": 1.1253031674208143,
        "ionization_energy_A/C": 0.9858245992357334,
        "ionization_energy_A/D": 0.762699804948661,
        "ionization_energy_B/C": 0.8760524521540584,
        "ionization_energy_B/D": 0.6777727345216336,
        "ionization_energy_C/D": 0.7736668424991107,
        "ionization_energy_ABCD_max": 8.1517,
        "ionization_energy_ABCD_min": 5.525,
        "ionization_energy_ABCD_avg": 6.550174999999999,
        "coordination_number_A": 12,
        "coordination_number_B": 12,
        "coordination_number_C": 12,
        "coordination_number_D": 4,
        "coordination_number_A/B": 1.0,
        "coordination_number_A/C": 1.0,
        "coordination_number_A/D": 3.0,
        "coordination_number_B/C": 1.0,
        "coordination_number_B/D": 3.0,
        "coordination_number_C/D": 3.0,
        "coordination_number_ABCD_max": 12,
        "coordination_number_ABCD_min": 4,
        "coordination_number_ABCD_avg": 10.0,
        "ratio_closest_A": 0.5,
        "ratio_closest_B": 0.5,
        "ratio_closest_C": 1.0,
        "ratio_closest_D": 1.0,
        "ratio_closest_A/B": 1.0,
        "ratio_closest_A/C": 0.5,
        "ratio_closest_A/D": 0.5,
        "ratio_closest_B/C": 0.5,
        "ratio_closest_B/D": 0.5,
        "ratio_closest_C/D": 1.0,
        "ratio_closest_ABCD_max": 1.0,
        "ratio_closest_ABCD_min": 0.5,
        "ratio_closest_ABCD_avg": 0.75,
        "polyhedron_distortion_A": 0.975,
        "polyhedron_distortion_B": 0.992,
        "polyhedron_distortion_C": 1.0,
        "polyhedron_distortion_D": 1.0,
        "polyhedron_distortion_A/B": 0.9828629032258064,
        "polyhedron_distortion_A/C": 0.975,
        "polyhedron_distortion_A/D": 0.975,
        "polyhedron_distortion_B/C": 0.992,
        "polyhedron_distortion_B/D": 0.992,
        "polyhedron_distortion_C/D": 1.0,
        "polyhedron_distortion_ABCD_max": 1.0,
        "polyhedron_distortion_ABCD_min": 0.975,
        "polyhedron_distortion_ABCD_avg": 0.99175,
        "CIF_radius_A": 1.783,
        "CIF_radius_B": 1.813,
        "CIF_radius_C": 1.798,
        "CIF_radius_D": 1.176,
        "CIF_radius_A/B": 0.9834528405956977,
        "CIF_radius_A/C": 0.9916573971078976,
        "CIF_radius_A/D": 1.5161564625850341,
        "CIF_radius_B/C": 1.0083426028921023,
        "CIF_radius_B/D": 1.5416666666666667,
        "CIF_radius_C/D": 1.5289115646258504,
        "CIF_radius_ABCD_max": 1.813,
        "CIF_radius_ABCD_min": 1.176,
        "CIF_radius_ABCD_avg": 1.6425,
        "Pauling_radius_CN12_A": 1.797,
        "Pauling_radius_CN12_B": 1.818,
        "Pauling_radius_CN12_C": 1.795,
        "Pauling_radius_CN12_D": 1.316,
        "Pauling_radius_CN12_A/B": 0.9884488448844884,
        "Pauling_radius_CN12_A/C": 1.0011142061281337,
        "Pauling_radius_CN12_A/D": 1.3655015197568388,
        "Pauling_radius_CN12_B/C": 1.0128133704735376,
        "Pauling_radius_CN12_B/D": 1.3814589665653496,
        "Pauling_radius_CN12_C/D": 1.363981762917933,
        "Pauling_radius_CN12_ABCD_max": 1.818,
        "Pauling_radius_CN12_ABCD_min": 1.316,
        "Pauling_radius_CN12_ABCD_avg": 1.6815,
        "Pauling_EN_A": 1.22,
        "Pauling_EN_B": 1.14,
        "Pauling_EN_C": 1.3,
        "Pauling_EN_D": 1.9,
        "Pauling_EN_A/B": 1.0701754385964912,
        "Pauling_EN_A/C": 0.9384615384615385,
        "Pauling_EN_A/D": 0.6421052631578947,
        "Pauling_EN_B/C": 0.8769230769230768,
        "Pauling_EN_B/D": 0.6,
        "Pauling_EN_C/D": 0.6842105263157895,
        "Pauling_EN_ABCD_max": 1.9,
        "Pauling_EN_ABCD_min": 1.14,
        "Pauling_EN_ABCD_avg": 1.3900000000000001,
        "Martynov_Batsanov_EN_A": 1.41,
        "Martynov_Batsanov_EN_B": 1.2,
        "Martynov_Batsanov_EN_C": 1.3,
        "Martynov_Batsanov_EN_D": 1.98,
        "Martynov_Batsanov_EN_A/B": 1.175,
        "Martynov_Batsanov_EN_A/C": 1.0846153846153845,
        "Martynov_Batsanov_EN_A/D": 0.712121212121212,
        "Martynov_Batsanov_EN_B/C": 0.923076923076923,
        "Martynov_Batsanov_EN_B/D": 0.6060606060606061,
        "Martynov_Batsanov_EN_C/D": 0.6565656565656566,
        "Martynov_Batsanov_EN_ABCD_max": 1.98,
        "Martynov_Batsanov_EN_ABCD_min": 1.2,
        "Martynov_Batsanov_EN_ABCD_avg": 1.4725000000000001,
        "melting_point_K_A": 1796.15,
        "melting_point_K_B": 1289.15,
        "melting_point_K_C": 2023.15,
        "melting_point_K_D": 1683.15,
        "melting_point_K_A/B": 1.3932823953767985,
        "melting_point_K_A/C": 0.8877987297036799,
        "melting_point_K_A/D": 1.067136024715563,
        "melting_point_K_B/C": 0.6371994167511059,
        "melting_point_K_B/D": 0.7659150996643199,
        "melting_point_K_C/D": 1.2020021982592164,
        "melting_point_K_ABCD_max": 2023.15,
        "melting_point_K_ABCD_min": 1289.15,
        "melting_point_K_ABCD_avg": 1697.9,
        "density_A": 4.47,
        "density_B": 7.0,
        "density_C": 11.7,
        "density_D": 2.33,
        "density_A/B": 0.6385714285714286,
        "density_A/C": 0.382051282051282,
        "density_A/D": 1.9184549356223175,
        "density_B/C": 0.5982905982905983,
        "density_B/D": 3.004291845493562,
        "density_C/D": 5.021459227467811,
        "density_ABCD_max": 11.7,
        "density_ABCD_min": 2.33,
        "density_ABCD_avg": 6.375,
        "specific_heat_A": 0.3,
        "specific_heat_B": 0.19,
        "specific_heat_C": 0.12,
        "specific_heat_D": 0.71,
        "specific_heat_A/B": 1.5789473684210527,
        "specific_heat_A/C": 2.5,
        "specific_heat_A/D": 0.4225352112676056,
        "specific_heat_B/C": 1.5833333333333335,
        "specific_heat_B/D": 0.26760563380281693,
        "specific_heat_C/D": 0.16901408450704225,
        "specific_heat_ABCD_max": 0.71,
        "specific_heat_ABCD_min": 0.12,
        "specific_heat_ABCD_avg": 0.32999999999999996,
        "cohesive_energy_A": 4.37,
        "cohesive_energy_B": 3.4,
        "cohesive_energy_C": 6.2,
        "cohesive_energy_D": 4.63,
        "cohesive_energy_A/B": 1.285294117647059,
        "cohesive_energy_A/C": 0.7048387096774194,
        "cohesive_energy_A/D": 0.9438444924406048,
        "cohesive_energy_B/C": 0.5483870967741935,
        "cohesive_energy_B/D": 0.734341252699784,
        "cohesive_energy_C/D": 1.3390928725701945,
        "cohesive_energy_ABCD_max": 6.2,
        "cohesive_energy_ABCD_min": 3.4,
        "cohesive_energy_ABCD_avg": 4.6499999999999995,
        "bulk_modulus_A": 41.2,
        "bulk_modulus_B": 31.8,
        "bulk_modulus_C": 54.0,
        "bulk_modulus_D": 98.0,
        "bulk_modulus_A/B": 1.2955974842767297,
        "bulk_modulus_A/C": 0.7629629629629631,
        "bulk_modulus_A/D": 0.42040816326530617,
        "bulk_modulus_B/C": 0.5888888888888889,
        "bulk_modulus_B/D": 0.32448979591836735,
        "bulk_modulus_C/D": 0.5510204081632653,
        "bulk_modulus_ABCD_max": 98.0,
        "bulk_modulus_ABCD_min": 31.8,
        "bulk_modulus_ABCD_avg": 56.25,
    }

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