def reaction_count(data_dir, top_n=50, norm=False): """ reaction occurence in a path multiply by pathway probability """ print(data_dir) f_n_n = os.path.join(data_dir, "output", "pathway_name_candidate.csv") f_n_p = os.path.join(data_dir, "output", "pathway_prob.csv") pathway_name = np.genfromtxt(f_n_n, dtype=str, delimiter='\n') pathway_prob = np.genfromtxt(f_n_p, dtype=float, delimiter='\n') reaction_map = dict() for _, (p_n, p_p) in enumerate(zip(pathway_name, pathway_prob)): map_tmp = parse_pattern.parse_reaction(p_n) for key, value in map_tmp.items(): if key not in reaction_map: reaction_map[key] = value * p_p else: reaction_map[key] += value * p_p d_f = pd.DataFrame(list( sorted(reaction_map.items(), key=lambda x: x[1], reverse=True)), columns=['reaction', 'frequency']) if norm is True: total = sum(d_f['frequency']) d_f['frequency'] /= total f_n_out1 = os.path.join(data_dir, "output", "reaction_count_index.csv") d_f[0:top_n].to_csv(f_n_out1, header=False, index=False, sep=',', columns=['reaction', 'frequency']) # old/chemkin reaction index # load reaction info new_old_ind_dict, new_ind_reaction_dict = psri.parse_reaction_and_its_index( data_dir) d_f2 = deepcopy(d_f) d_f2['reaction'] = d_f2['reaction'].apply( lambda x: new_old_ind_dict[x[1::]]) f_n_out1_5 = os.path.join(data_dir, "output", "reaction_count_old_index.csv") d_f2[0:top_n].to_csv(f_n_out1_5, header=False, index=False, sep=',', columns=['reaction', 'frequency']) # convert species reaction index to real species and reactions d_f['reaction'] = d_f['reaction'].apply( lambda x: psri.reaction_name_to_real_reaction(new_ind_reaction_dict, x ).strip()) # print(d_f['reaction']) f_n_out2 = os.path.join(data_dir, "output", "reaction_count_name.csv") # replace [] with "" d_f['reaction'] = d_f['reaction'].apply(lambda x: x.replace("[", "")) d_f['reaction'] = d_f['reaction'].apply(lambda x: x.replace("]", "")) d_f[0:top_n].to_csv(f_n_out2, header=False, index=False, sep=',', columns=['reaction', 'frequency'])
def species_production_path(data_dir, spe='OH', top_n=50, norm=False): """ species production in a path multiply by pathway probability, count pathway or sub-pathway ends with a species """ print(data_dir) f_n_n = os.path.join(data_dir, "output", "pathway_name_candidate.csv") f_n_p = os.path.join(data_dir, "output", "pathway_prob.csv") pathway_name = np.genfromtxt(f_n_n, dtype=str, delimiter='\n') pathway_prob = np.genfromtxt(f_n_p, dtype=float, delimiter='\n') # load spe and reaction info spe_ind_name_dict, spe_name_ind_dict = psri.parse_spe_info(data_dir) _, new_ind_reaction_dict = psri.parse_reaction_and_its_index(data_dir) species_production_map = dict() for _, (p_n, p_p) in enumerate(zip(pathway_name, pathway_prob)): map_tmp = parse_pattern.parse_species_production_path( p_n, 'S' + spe_name_ind_dict[spe]) for key, value in map_tmp.items(): if key not in species_production_map: species_production_map[key] = value * p_p else: species_production_map[key] += value * p_p d_f = pd.DataFrame(list( sorted(species_production_map.items(), key=lambda x: x[1], reverse=True)), columns=['species', 'frequency']) if norm is True: total = sum(d_f['frequency']) d_f['frequency'] /= total f_n_out1 = os.path.join(data_dir, "output", spe + "_production_path_index.csv") d_f[0:top_n].to_csv(f_n_out1, header=False, index=False, sep=',', columns=['species', 'frequency']) # convert species reaction index to real species and reactions d_f['species'] = d_f['species'].apply( lambda x: psri.pathname_to_real_spe_reaction( spe_ind_name_dict, new_ind_reaction_dict, x).strip()) f_n_out2 = os.path.join(data_dir, "output", spe + "_production_path_name.csv") d_f[0:top_n].to_csv(f_n_out2, header=False, index=False, sep=',', columns=['species', 'frequency'])
def initiate_fast_reaction(data_dir): """ intiate file named "reaction_info_base.json" based on file named "reaction_labelling.csv" """ new_old_index_dict, new_ind_reaction_dict = psri.parse_reaction_and_its_index( data_dir) rxn_pair_dict = dict() # un-paired species unpaired = dict() for _, val1 in enumerate(new_old_index_dict): # print(idx, val1, new_old_index_dict[val1]) this_value = int(new_old_index_dict[val1]) neg_value = -1 * this_value if (neg_value in unpaired): # print(int(val1), unpaired[neg_value]) rxn_pair_dict.update({int(val1): unpaired[neg_value]}) rxn_pair_dict.update({unpaired[neg_value]: int(val1)}) unpaired.pop(neg_value) else: unpaired.update({this_value: int(val1)}) rxn_info = {} for _, val1 in enumerate(new_ind_reaction_dict): entry = { str(val1): { "formula": new_ind_reaction_dict[val1], # default value, 10^-100, assume the reaction is super slow "time_scale": -100, "reverse_reaction": "None" } } if int(val1) in rxn_pair_dict: entry[str(val1)]["reverse_reaction"] = \ str(rxn_pair_dict[int(val1)]) rxn_info.update(entry) fn0 = os.path.join(data_dir, "input", "reaction_info_base_backup.json") fn1 = os.path.join(data_dir, "input", "reaction_info_base.json") if os.path.isfile(fn1): copy2(fn1, fn0) rwc.write_configuration(rxn_info, fn1)
def plot_network(data_dir, fname="", pathname="", pathprob=1.0, path_idx=None, end_t=1.0, suffix="", atom_followed="C", species_path=False): """ plot network manually """ print(fname) n_coordinate = get_names_coordinates(data_dir, fname) prefix = "" if species_path is True: prefix = "species_" # figure name if suffix is "": fig_name = prefix + "network_path_" + str(path_idx) + ".jpg" else: fig_name = prefix + "network_path_" + \ str(path_idx) + str(suffix) + ".jpg" # specify label for lines labels = [] x = [] y = [] name_idx_dict = dict() for i_tmp, val in enumerate(n_coordinate): labels.append(val) name_idx_dict[val] = i_tmp x.append(float(n_coordinate[val][0])) y.append(float(n_coordinate[val][1])) # read in species index name spe_idx_name_dict, spe_name_idx_dict = psri.parse_spe_info(data_dir) spe_alias_latex = read_spe_alias( os.path.join(data_dir, "input", "spe_alias_latex.json")) _, new_ind_reaction_dict = psri.parse_reaction_and_its_index(data_dir) # modify labels spe_union_find_group = global_settings.get_union_find_group( DATA_DIR, atom_followed) for idx, val in enumerate(labels): spe_i = spe_name_idx_dict[val] if spe_i in spe_union_find_group: labels[idx] = ",".join([ str(spe_idx_name_dict[str(x)]) for x in spe_union_find_group[spe_i] ]) print(labels) for idx, val in enumerate(labels): labels[idx] = change_spe_name(val, spe_alias_latex, None) print(labels) fig, a_x = plt.subplots(1, 1, sharex=True, sharey=False) # background a_x.scatter(x, y, color='b', marker="o", alpha=0.3) for i, _ in enumerate(x): t_h = a_x.annotate(labels[i], (x[i], y[i])) t_h.set_alpha(0.15) # get rid of R-1000003S90, don't need it here pathname = re.sub(r"R-\d+S\d+", r'', pathname) # parse pathway matched_spe = re.findall(r"S(\d+)", pathname) matched_reaction = re.findall(r"R(\d+)", pathname) print(matched_spe, matched_reaction) node_list = [ name_idx_dict[change_spe_name(str(x), spe_idx_name_dict, spe_union_find_group)] for x in matched_spe ] print(node_list) for idx, curr_idx in enumerate(node_list): if idx >= 1: pre_idx = node_list[idx - 1] a_h = a_x.annotate('', xy=(x[curr_idx], y[curr_idx]), xytext=(x[pre_idx], y[pre_idx]), arrowprops={ 'arrowstyle': '->', 'lw': 4, 'color': 'red' }, va='center') a_h.set_alpha(0.9) # re-draw points and labels on canvas for _, val in enumerate(node_list): a_x.scatter(x[val], y[val], color='r', marker="o", alpha=0.9) t_h = a_x.annotate(labels[val], (x[val], y[val])) t_h.set_alpha(0.9) # draw reaction along path if species_path is False: # check for duplicate transition idx_label_dict = {} for idx, curr_idx in enumerate(node_list): if idx >= 1: pre_idx = node_list[idx - 1] rxn_idx = matched_reaction[idx - 1] if tuple([pre_idx, curr_idx, rxn_idx]) in idx_label_dict: idx_label_dict[tuple([pre_idx, curr_idx, rxn_idx])] += "," + str(idx) else: idx_label_dict[tuple([pre_idx, curr_idx, rxn_idx])] = str(idx) for idx, curr_idx in enumerate(node_list): if idx >= 1: pre_idx = node_list[idx - 1] rxn_idx = matched_reaction[idx - 1] rxn_name = idx_label_dict[tuple( [pre_idx, curr_idx, rxn_idx])] + ": " + str( new_ind_reaction_dict[matched_reaction[idx - 1]]) if x[pre_idx] <= x[curr_idx]: x_tmp = x[pre_idx] else: x_tmp = x[curr_idx] y_tmp = y[pre_idx] * 0.7 + y[curr_idx] * 0.3 t_h = a_x.annotate(rxn_name, (x_tmp, y_tmp), color='g', size=8.0) t_h.set_alpha(0.5) else: # build idx->label idx_label_dict = {} for idx, curr_idx in enumerate(node_list): if idx >= 1: pre_idx = node_list[idx - 1] if tuple([pre_idx, curr_idx]) in idx_label_dict: idx_label_dict[tuple([pre_idx, curr_idx])] += "," + str(idx) else: idx_label_dict[tuple([pre_idx, curr_idx])] = str(idx) for idx, curr_idx in enumerate(node_list): if idx >= 1: pre_idx = node_list[idx - 1] rxn_name = idx_label_dict[tuple([pre_idx, curr_idx])] t_h = a_x.annotate(rxn_name, (x[pre_idx] * 0.7 + x[curr_idx] * 0.3, y[pre_idx] * 0.7 + y[curr_idx] * 0.3), color='g', size=8.0) t_h.set_alpha(0.5) a_x.set_xlim([ np.min(x) - 0.01 * (np.max(x) - np.min(x)), np.max(x) + 0.25 * (np.max(x) - np.min(x)) ]) # a_x.grid('on') a_x.axis('off') a_x.set_frame_on(False) a_x.set_xticks([]) # this is needed for bbox_inches a_x.set_yticks([]) if (path_idx == 1): a_x.set_title("P$_{" + str(path_idx) + "}$" + " = " + "{:.6e}".format(float(pathprob))) else: a_x.set_title("P$_{" + str(path_idx) + "}$" + " = " + "{:.2e}".format(float(pathprob))) # fig.tight_layout() # plt.subplots_adjust(left=0.01, right=0.9, top=0.9, bottom=0.01) fig.savefig(os.path.join(data_dir, "output", fig_name), bbox_inches='tight', dpi=500) # bbox_inches='tight', pad_inches=0, dpi=500) plt.close() return
def plot_reaction_rates(data_dir, reaction_idx=None, tau=10.0, end_t=1.0, tag="fraction"): """ plot reaction rates give reaction index list """ colors, markers, _ = get_colors_markers_linestyles() _, rxn_idx_n = psri.parse_reaction_and_its_index(data_dir) rxn_idx_n["-1"] = "Temp" reaction_idx.append(-1) if reaction_idx is None: reaction_idx = [0] time = np.loadtxt(os.path.join( data_dir, "output", "time_dlsode_" + str(tag) + ".csv"), delimiter=",") rxn_rates = np.loadtxt(os.path.join(data_dir, "output", "reaction_rate_dlsode_" + str(tag) + ".csv"), delimiter=",") temp = np.loadtxt(os.path.join(data_dir, "output", "temperature_dlsode_" + str(tag) + ".csv"), delimiter=",") counter = 0 # the time point where reference time tau is tau_time_point = float(tau) / time[-1] * len(time) end_point = int(end_t * tau_time_point) delta_n = int(end_point / 10) if delta_n is 0: delta_n = 1 fig, a_x_left = plt.subplots(1, 1, sharex=True, sharey=False) for s_idx in reaction_idx: if s_idx == -1: a_x_right = a_x_left.twinx() a_x_right.plot(time[0:end_point], temp[0:end_point], color=colors[-1], label=rxn_idx_n[str(s_idx)], markevery=delta_n) else: if counter < len(colors) - 1: m_k = None else: m_k = markers[(counter + 1 - len(colors)) % (len(markers))] a_x_left.semilogy(time[0:end_point], rxn_rates[0:end_point, s_idx], marker=m_k, color=colors[counter % (len(colors) - 1)], label=rxn_idx_n[str(s_idx)], markevery=delta_n) counter += 1 leg_left = a_x_left.legend(loc=8, fancybox=True, prop={'size': 10.0}) leg_right = a_x_right.legend(loc=2, fancybox=True, prop={'size': 10.0}) leg_left.get_frame().set_alpha(0.7) leg_right.get_frame().set_alpha(0.7) a_x_left.grid() a_x_left.xaxis.set_major_formatter(FormatStrFormatter('%.1e')) a_x_left.set_xlabel("Time/sec") a_x_left.set_ylabel("R") a_x_right.set_ylabel("T/K") rxn_idx_str = "_".join(str(x) for x in reaction_idx) plt.title("reaction rates and Temp") fig.savefig(os.path.join(data_dir, "output", "reaction_rate_" + rxn_idx_str + "_" + str(end_t) + ".jpg"), dpi=500) plt.close()