def update_check(MAPI_KEY): """ This function tests to see if BatteryData.csv is up to date. The function checks the length of the BatteryData.csv and compares it to the length of the list produced by getting all of the battery IDs. If the lengths are the same, it returns that the data is up to date. If the lengths are not the same, it recommends to run get_bat_dat() to update the csv file. """ current_df = pd.read_csv('BatteryData.csv') current_len = len(current_df) from pymatgen import MPRester # if __name__ == "__main__": mpr = MPRester(MAPI_KEY) all_bat_ids_list = (mpr._make_request('/battery/all_ids')) matproj_len = len(all_bat_ids_list) if current_len == matproj_len: print("The Current BatteryData.csv file is up to date!") else: print("Data is not up to date. Please run get_bat_dat() \ function to obtain new .csv file.") return
#__| #__| # | - Main Code **************************************************************** #Entries are the basic unit for thermodynamic and other analyses in pymatgen. entries = mpr.get_entries_in_chemsys(['O', 'H']) # | - Ion Reference Data #Dictionary of reference state:experimental formation energy # ion_dict_Co = mpr._make_request('/pourbaix_diagram/reference_data/Ru') # ion_dict_S = mpr._make_request('/pourbaix_diagram/reference_data/Y') ion_dict_Co = mpr._make_request('/pourbaix_diagram/reference_data/Ir') ion_dict_S = mpr._make_request('/pourbaix_diagram/reference_data/Sr') for i, dummy in enumerate(ion_dict_Co): print(ion_dict_Co[i]) print('hi') # for i,dummy in enumerate(ion_dict_S): # print ion_dict_S[i]['Name'], ion_dict_S[i]['Energy'] ion_dict = ion_dict_Co # + ion_dict_S #ion_dict = ion_dict_Co + ion_dict_S #__| #NOTE This line assumes that the every entry in the experimental ion energy # has the same ref. st. solid
def get_bat_dat(mapi_key): """ Takes materials project API key as input and returns dataframe of all battery material properies This function returns a dataframe of all the battery materials by cycling through each battery ID """ # MAPI_KEY is the API key obtained from the materials project mpr = MPRester(mapi_key) def get_battery_data(self, formula_or_batt_id): """ Returns batteries from a batt id or formula. Examples: get_battery("mp-300585433") get_battery("LiFePO4") """ return mpr._make_request('/battery/%s' % formula_or_batt_id) # adding get_battery_data function to MPRester MPRester.get_battery_data = get_battery_data # import the crytsal system table from repository crystal = pd.read_csv('CrystalSystemTable.csv') # making a list of all the battery IDs all_bat_ids_list = (mpr._make_request('/battery/all_ids')) # making an empty dataframe to hold all of the battery data all_battery_dataframe = pd.DataFrame([]) # looping through every id in the list of all battery IDs for batt_id in all_bat_ids_list: # gets all the data for one battery id and stores it in a result df result_bat_id = pd.DataFrame(mpr.get_battery_data(batt_id)) # this block of code goes into the adj_pairs element of the dataframe, # makes it a list, and extracts something from that list adj_pairs = result_bat_id['adj_pairs'] adj_pairs_list = list(adj_pairs) in_list = pd.DataFrame(list(adj_pairs_list[0])) # volume change, charge and discharge properties of the materials max_d_vol = pd.DataFrame(in_list['max_delta_volume']) formula_charge = pd.DataFrame(in_list['formula_charge']) formula_discharge = pd.DataFrame(in_list['formula_discharge']) result_bat_id['Max Delta Volume'] = max_d_vol result_bat_id['Charge Formula'] = formula_charge result_bat_id['Discharge Formula'] = formula_discharge # go into spacegroup column and extract the crystal system number spacegroup_list = list(result_bat_id['spacegroup']) in_list_space = pd.DataFrame(spacegroup_list) crystal_number = in_list_space['number'][0] # use crystal dataframe to see what crystal system the number # corresponds to and store crystal system as system system = crystal.iloc[crystal_number-1]['Crystal'] # append crystal system number and lattice to results result_bat_id['Spacegroup Number'] = crystal_number result_bat_id['Crystal Lattice'] = system # appending the results to the final dataframe all_battery_dataframe = all_battery_dataframe.append(result_bat_id) # cleaning the dataframe all_battery_dataframe.rename( columns={'battid': 'Battery ID', 'reduced_cell_formula': 'Reduced Cell Formula', 'average_voltage': 'Average Voltage (V)', 'min_voltage': 'Min Voltage (V)', 'max_voltage': 'Max Voltage (V)', 'nsteps': 'Number of Steps', 'min_instability': 'Min Instability', 'capacity_grav': 'Gravimetric Capacity (mAh/g)', 'capacity_vol': 'Volumetric Capacity (Ah/L)', 'working_ion': 'Working Ion', 'min_frac': 'Min Fraction', 'max_frac': 'Max Fraction', 'reduced_cell_composition': 'Reduced Cell Composition', 'framework': 'Framework', 'adj_pairs': 'Adjacent Pairs', 'spacegroup': 'Spacegroup', 'energy_grav': 'Specific Energy (Wh/kg)', 'energy_vol': 'Energy Density (Wh/L)', 'numsites': 'Number of Sites', 'type': 'Type'}, inplace=True) # setting index to the battery id clean_battery_df = all_battery_dataframe.set_index('Battery ID') # exports the clean dataframe as a csv to the repository # this ensures it does not need to be run each time clean_battery_df.to_csv(path_or_buf='./BatteryData.csv') # returns the cleaned dataframe return clean_battery_df