# +
from methods import get_df_magmoms, read_magmom_comp_data

df_magmoms = get_df_magmoms()


data_dict_list = []
for name_i, row_i in df_all_comb.iterrows():
    # #####################################################
    data_dict_i = dict()
    # #####################################################
    index_dict_i = dict(zip(df_all_comb.index.names, name_i))
    # #####################################################

    magmom_data_i = read_magmom_comp_data(name=name_i)
    if magmom_data_i is not None:
        df_magmoms_comp_i = magmom_data_i["df_magmoms_comp"]
        df_magmoms_comp_i = process_df_magmoms_comp_i(df_magmoms_comp_i)

        # tmp = df_magmoms_comp_i.sum_norm_abs_magmom_diff.min()
        # tmp_list.append(tmp)

        job_ids = []
        for ads_j in ["o", "oh", "bare", ]:
            job_id_j = row_i["data"]["job_id_" + ads_j][""]
            if job_id_j is not None:
                job_ids.append(job_id_j)


        sum_norm_abs_magmom_diff_i = None
# +
import os
print(os.getcwd())
import sys

# #########################################################
from methods import get_df_magmoms
from methods import read_magmom_comp_data

from methods import get_df_jobs_anal, get_df_jobs_data, get_df_jobs
# -

# # Read Data

df_magmoms = get_df_magmoms()
magmom_data_dict = read_magmom_comp_data()
df_jobs = get_df_jobs()
df_jobs_anal = get_df_jobs_anal()
df_jobs_data = get_df_jobs_data()

# +
from methods import get_df_oer_groups

df_oer_groups = get_df_oer_groups()

# +
# df_oer_groups

# +
for name_i, row_i in df_oer_groups.iterrows():
    tmp = 42
Ejemplo n.º 3
0
def process_group_magmom_comp(
    name=None,
    group=None,
    write_atoms_objects=False,
    verbose=False,
):
    """
    """
    #| - process_group_magmom_comp
    # #####################################################
    group_w_o = group

    # #####################################################
    out_dict = dict()
    out_dict["df_magmoms_comp"] = None
    out_dict["good_triplet_comb"] = None
    out_dict["job_ids"] = None
    # out_dict[""] =

    job_ids_list = list(set(group.job_id_max.tolist()))

    #| - Reading data
    # #########################################################
    df_jobs = get_df_jobs()

    # #########################################################
    df_atoms_sorted_ind = get_df_atoms_sorted_ind()
    df_atoms_sorted_ind = df_atoms_sorted_ind.set_index("job_id")

    # #########################################################
    df_job_ids = get_df_job_ids()
    df_job_ids = df_job_ids.set_index("job_id")

    from methods import read_magmom_comp_data

    assert name != None, "Must pass name to read previous data"

    magmom_comp_data_prev = read_magmom_comp_data(name=name)
    if magmom_comp_data_prev is not None:
        pair_wise_magmom_comp_data_prev = \
            magmom_comp_data_prev["pair_wise_magmom_comp_data"]
    #__|

    if write_atoms_objects:
        #| - Write atoms objects
        df_i = pd.concat([
            df_job_ids, df_atoms_sorted_ind.loc[group_w_o.job_id_max.tolist()]
        ],
                         axis=1,
                         join="inner")

        # #########################################################
        df_index_i = group_w_o.index.to_frame()
        compenv_i = df_index_i.compenv.unique()[0]
        slab_id_i = df_index_i.slab_id.unique()[0]

        active_sites = [
            i for i in df_index_i.active_site.unique() if i != "NaN"
        ]
        active_site_i = active_sites[0]

        folder_name = compenv_i + "__" + slab_id_i + "__" + str(
            int(active_site_i))
        # #########################################################

        for job_id_i, row_i in df_i.iterrows():
            tmp = 42

            job_id = row_i.name
            atoms = row_i.atoms_sorted_good
            ads = row_i.ads

            file_name = ads + "_" + job_id + ".traj"

            print("Is this saving to the right place d9sf")
            root_file_path = os.path.join("__temp__", folder_name)
            print(os.getcwd(), root_file_path)
            if not os.path.exists(root_file_path):
                os.makedirs(root_file_path)

            file_path = os.path.join(root_file_path, file_name)

            atoms.write(file_path)
        #__|

    # #####################################################
    #| - Getting good triplet combinations
    all_triplet_comb = list(
        itertools.combinations(group_w_o.job_id_max.tolist(), 3))

    good_triplet_comb = []
    for tri_i in all_triplet_comb:
        df_jobs_i = df_jobs.loc[list(tri_i)]

        # Triplet must not contain duplicate ads
        # Must strictly be a *O, *OH, and *bare triplet
        ads_freq_dict = CountFrequency(df_jobs_i.ads.tolist())

        tmp_list = list(ads_freq_dict.values())
        any_repeat_ads = [True if i > 1 else False for i in tmp_list]

        if not any(any_repeat_ads):
            good_triplet_comb.append(tri_i)
    #__|

    # #####################################################
    #| - MAIN LOOP
    if verbose:
        print("Number of viable triplet combinations:", len(good_triplet_comb))

    data_dict_list = []
    pair_wise_magmom_comp_data = dict()
    for tri_i in good_triplet_comb:
        #| - Process triplets
        data_dict_i = dict()

        if verbose:
            print("tri_i:", tri_i)

        all_pairs = list(itertools.combinations(tri_i, 2))

        df_jobs_i = df_jobs.loc[list(tri_i)]

        sum_norm_abs_magmom_diff = 0.
        for pair_i in all_pairs:

            # if pair_i in list(pair_wise_magmom_comp_data_prev.keys()):
            if (magmom_comp_data_prev is not None) and \
               (pair_i in list(pair_wise_magmom_comp_data_prev.keys())):
                magmom_data_out = pair_wise_magmom_comp_data_prev[pair_i]
            else:
                # print("Need to run manually")
                # print("pair_i:", pair_i)
                #| - Process pairs
                row_jobs_0 = df_jobs.loc[pair_i[0]]
                row_jobs_1 = df_jobs.loc[pair_i[1]]

                ads_0 = row_jobs_0.ads
                ads_1 = row_jobs_1.ads

                # #############################################
                if set([ads_0, ads_1]) == set(["o", "oh"]):
                    job_id_0 = df_jobs_i[df_jobs_i.ads == "o"].iloc[0].job_id
                    job_id_1 = df_jobs_i[df_jobs_i.ads == "oh"].iloc[0].job_id
                elif set([ads_0, ads_1]) == set(["o", "bare"]):
                    job_id_0 = df_jobs_i[df_jobs_i.ads ==
                                         "bare"].iloc[0].job_id
                    job_id_1 = df_jobs_i[df_jobs_i.ads == "o"].iloc[0].job_id
                elif set([ads_0, ads_1]) == set(["oh", "bare"]):
                    job_id_0 = df_jobs_i[df_jobs_i.ads ==
                                         "bare"].iloc[0].job_id
                    job_id_1 = df_jobs_i[df_jobs_i.ads == "oh"].iloc[0].job_id
                else:
                    print("Woops something went wrong here")

                # #############################################
                row_atoms_i = df_atoms_sorted_ind.loc[job_id_0]
                # #############################################
                atoms_0 = row_atoms_i.atoms_sorted_good
                magmoms_sorted_good_0 = row_atoms_i.magmoms_sorted_good
                was_sorted_0 = row_atoms_i.was_sorted
                # #############################################

                # #############################################
                row_atoms_i = df_atoms_sorted_ind.loc[job_id_1]
                # #############################################
                atoms_1 = row_atoms_i.atoms_sorted_good
                magmoms_sorted_good_1 = row_atoms_i.magmoms_sorted_good
                was_sorted_1 = row_atoms_i.was_sorted
                # #############################################

                # #############################################
                magmom_data_out = get_magmom_diff_data(
                    ads_atoms=atoms_1,
                    slab_atoms=atoms_0,
                    ads_magmoms=magmoms_sorted_good_1,
                    slab_magmoms=magmoms_sorted_good_0,
                )
                #__|

            pair_wise_magmom_comp_data[pair_i] = magmom_data_out

            tot_abs_magmom_diff = magmom_data_out["tot_abs_magmom_diff"]
            norm_abs_magmom_diff = magmom_data_out["norm_abs_magmom_diff"]
            if verbose:
                print("    ",
                      "pair_i: ",
                      pair_i,
                      ": ",
                      np.round(norm_abs_magmom_diff, 3),
                      sep="")

            sum_norm_abs_magmom_diff += norm_abs_magmom_diff

        # #################################################
        data_dict_i["job_ids_tri"] = set(tri_i)
        data_dict_i["sum_norm_abs_magmom_diff"] = sum_norm_abs_magmom_diff
        # #################################################
        data_dict_list.append(data_dict_i)
        # #################################################

        #__|

    #__|

    # #####################################################
    df_magmoms_i = pd.DataFrame(data_dict_list)

    # #####################################################
    out_dict["df_magmoms_comp"] = df_magmoms_i
    out_dict["good_triplet_comb"] = good_triplet_comb
    out_dict["pair_wise_magmom_comp_data"] = pair_wise_magmom_comp_data
    out_dict["job_ids"] = job_ids_list
    # #####################################################

    return (out_dict)