From 4434a6ba94d62bb2cd18a5ca21b51fa5f4980db5 Mon Sep 17 00:00:00 2001 From: Saku Hakamaa <sajuhak@utu.fi> Date: Tue, 27 Nov 2018 13:13:06 +0200 Subject: [PATCH] reorder code --- sim_controller.py | 6 +-- sim_utilities.py | 102 ++++++++++++++++++++++++++++++++++++++++- vis.py | 112 +++------------------------------------------- 3 files changed, 111 insertions(+), 109 deletions(-) diff --git a/sim_controller.py b/sim_controller.py index fbbf263..f61774a 100644 --- a/sim_controller.py +++ b/sim_controller.py @@ -124,9 +124,9 @@ class SimController(object): for element, item in self.figure_params.items() if element in ase.data.chemical_symbols]) print("Drawing figures") - z_positions, dict_concs_mean, energy_list, frame_list = vis.read_conc_data(figure_out_path, - symbol_marker_color_dict, - figure_range_dict) + z_positions, dict_concs_mean, energy_list, frame_list = sim_utilities.read_conc_data(figure_out_path, + symbol_marker_color_dict, + figure_range_dict) vis.draw_figures(z_positions, dict_concs_mean, diff --git a/sim_utilities.py b/sim_utilities.py index ef5a309..60c8375 100644 --- a/sim_utilities.py +++ b/sim_utilities.py @@ -26,6 +26,26 @@ import ase import numpy as np +def average_filter_function(data): + """ + Applies a 3-wide average filter to data to make it smoother. + + :param data: the list of data to filter + :return: filtered data + """ + temp_data = [] + for i in range(len(data)): + sum = data[i] + if i == 0: + sum += data[i] + data[i + 1] + elif i == len(data) - 1: + sum += data[i - 1] + data[i] + else: + sum += data[i - 1] + data[i + 1] + temp_data.append(sum / 3) + return temp_data + + class ParsingError(Exception): pass @@ -43,6 +63,7 @@ def compress_frames(job_out_path): os.rmdir(sim_frames_folder_path) +# TODO: combine all three range parsing methods def convert_bool_to_range(range_list): if range_list is True: return [(0, math.inf)] @@ -205,7 +226,6 @@ def parse_conf(conf_filename): figure_params = None return job_params, sim_params, lattice_params, bulk_dopants, empty_space_dopants, figure_params -# TODO: combine all three range parsing methods def parse_figure_range_str(figure_params): @@ -241,6 +261,7 @@ def parse_param_str_to_bool(parameter): raise ParsingError('unknown boolean type: "{}"'.format(parameter)) +# TODO: combine all three range parsing methods def parse_range_str(parameter, lim_min=0, lim_max=math.inf): """ Converts a range specified in a string into a list of upper and lower bounds. @@ -266,6 +287,7 @@ def parse_range_str(parameter, lim_min=0, lim_max=math.inf): return ids +# TODO: combine all three range parsing methods def range_list_to_indexes(ranges, indexes): ids = set() for index in indexes: @@ -282,6 +304,84 @@ def range_list_to_indexes(ranges, indexes): return sorted(list(ids)) +def read_conc_data(in_path, symbol_marker_color_dict, figure_range_dict): + # ------------------------------ + # CALCULATING MEAN + # ------------------------------ + # TODO: make easier to understand + # TODO: stop shadowing names from outer scope + frame_list = [] + dict_concs_per_element = {} + energy_list = [] + z_positions = [] + + # read concentrations + conc_path = os.path.join(in_path, "sim_conc") + file_list = [entry for entry in os.listdir(conc_path) if entry.endswith(".txt")] + + with open(os.path.join(in_path, "energy.txt"), 'r') as energy_file: + energy_file_header = energy_file.readline() + while True: + energy_file_line = energy_file.readline() + if energy_file_line == "": + break + + energy_list.append(float(energy_file_line.split()[1])) + + for filename in file_list: + conc_file_path = os.path.join(conc_path, filename) + with open(conc_file_path, 'r') as conc_file: + symbols = conc_file.readline().split()[2:] + dict_concs_per_z = {} + for symbol1, symbol2 in zip(symbols, symbol_marker_color_dict): + dict_concs_per_z[symbol1] = [] + + if not symbol1 == symbol2: + raise Exception + + while True: + conc_file_line = conc_file.readline() + if conc_file_line == "": + break + + values = conc_file_line.split() + z_pos = float(values.pop(0)) + if z_pos not in z_positions: + z_positions.append(z_pos) + + for symbol, value in zip(symbol_marker_color_dict, values): + dict_concs_per_z[symbol].append(float(value)) + + frame_list.append(dict_concs_per_z) + + # convert list of frame data to lists of data per element + for i, dict_concs_per_z in enumerate(frame_list): + for symbol, concs_list in dict_concs_per_z.items(): + if symbol not in dict_concs_per_element.keys(): + dict_concs_per_element[symbol] = [] + for z_pos in z_positions: + dict_concs_per_element[symbol].append([]) + for i, z_pos in enumerate(z_positions): + dict_concs_per_element[symbol][i].append(concs_list[i]) + + # collect the average concentrations into a list per symbol and z-level + dict_concs_mean = {} + profile_mean_range = figure_range_dict['profile_mean_range'] + for symbol, concs_list in dict_concs_per_element.items(): + if symbol not in dict_concs_mean.keys(): + dict_concs_mean[symbol] = [0] * len(z_positions) + for z, concs in enumerate(concs_list): + indexes = sim_utilities.range_list_to_indexes(profile_mean_range, range(len(concs))) + array_mask = [True] * len(concs) + for i, _ in enumerate(concs): + if i in indexes: + array_mask[i] = False + masked_concs = np.ma.array(concs, mask=array_mask) + dict_concs_mean[symbol][z] = np.mean(masked_concs) + + return z_positions, dict_concs_mean, energy_list, frame_list + + def read_job_name_and_end_energy(report_file): energy_report_str = "Final potential energy: " job_name = report_file.readline() diff --git a/vis.py b/vis.py index 8649a6b..c4e6bb6 100644 --- a/vis.py +++ b/vis.py @@ -17,7 +17,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # ------------------------------ import os -import math import argparse # import matplotlib.animation as ani @@ -30,104 +29,6 @@ import ase import sim_utilities -def average_filter_function(data): - """ - Applies a 3-wide average filter to data to make it smoother. - - :param data: the list of data to filter - :return: filtered data - """ - temp_data = [] - for i in range(len(data)): - sum = data[i] - if i == 0: - sum += data[i] + data[i + 1] - elif i == len(data) - 1: - sum += data[i - 1] + data[i] - else: - sum += data[i - 1] + data[i + 1] - temp_data.append(sum / 3) - return temp_data - - -def read_conc_data(in_path, symbol_marker_color_dict, figure_range_dict): - # ------------------------------ - # CALCULATING MEAN - # ------------------------------ - # TODO: make easier to understand - # TODO: stop shadowing names from outer scope - frame_list = [] - dict_concs_per_element = {} - energy_list = [] - z_positions = [] - - # read concentrations - conc_path = os.path.join(in_path, "sim_conc") - file_list = [entry for entry in os.listdir(conc_path) if entry.endswith(".txt")] - - with open(os.path.join(in_path, "energy.txt"), 'r') as energy_file: - energy_file_header = energy_file.readline() - while True: - energy_file_line = energy_file.readline() - if energy_file_line == "": - break - - energy_list.append(float(energy_file_line.split()[1])) - - for filename in file_list: - conc_file_path = os.path.join(conc_path, filename) - with open(conc_file_path, 'r') as conc_file: - symbols = conc_file.readline().split()[2:] - dict_concs_per_z = {} - for symbol1, symbol2 in zip(symbols, symbol_marker_color_dict): - dict_concs_per_z[symbol1] = [] - - if not symbol1 == symbol2: - raise Exception - - while True: - conc_file_line = conc_file.readline() - if conc_file_line == "": - break - - values = conc_file_line.split() - z_pos = float(values.pop(0)) - if z_pos not in z_positions: - z_positions.append(z_pos) - - for symbol, value in zip(symbol_marker_color_dict, values): - dict_concs_per_z[symbol].append(float(value)) - - frame_list.append(dict_concs_per_z) - - # convert list of frame data to lists of data per element - for i, dict_concs_per_z in enumerate(frame_list): - for symbol, concs_list in dict_concs_per_z.items(): - if symbol not in dict_concs_per_element.keys(): - dict_concs_per_element[symbol] = [] - for z_pos in z_positions: - dict_concs_per_element[symbol].append([]) - for i, z_pos in enumerate(z_positions): - dict_concs_per_element[symbol][i].append(concs_list[i]) - - # collect the average concentrations into a list per symbol and z-level - dict_concs_mean = {} - profile_mean_range = figure_range_dict['profile_mean_range'] - for symbol, concs_list in dict_concs_per_element.items(): - if symbol not in dict_concs_mean.keys(): - dict_concs_mean[symbol] = [0] * len(z_positions) - for z, concs in enumerate(concs_list): - indexes = sim_utilities.range_list_to_indexes(profile_mean_range, range(len(concs))) - array_mask = [True] * len(concs) - for i, _ in enumerate(concs): - if i in indexes: - array_mask[i] = False - masked_concs = np.ma.array(concs, mask=array_mask) - dict_concs_mean[symbol][z] = np.mean(masked_concs) - - return z_positions, dict_concs_mean, energy_list, frame_list - - def draw_figures(z_positions, dict_concs_mean, energy_list, frame_list, fig_range_dict, symbol_marker_color_dict, no_gui, average_filter, out_path): import matplotlib if no_gui: @@ -179,7 +80,7 @@ def draw_figures(z_positions, dict_concs_mean, energy_list, frame_list, fig_rang # draw on filtered figure if average_filter: plt.figure(figure_mean_filtered.number) - plt.plot(z_positions, average_filter_function(data), marker=marker, color=color, label=label, + plt.plot(z_positions, sim_utilities.average_filter_function(data), marker=marker, color=color, label=label, zorder=2) # set mean figure appearance and save it plt.figure(figure_mean.number) @@ -227,7 +128,7 @@ def draw_figures(z_positions, dict_concs_mean, energy_list, frame_list, fig_rang atom_dict_concs = frame_list[0] data = atom_dict_concs[symbol] if average_filter: - data = average_filter_function(data) + data = sim_utilities.average_filter_function(data) line = plt.plot(z_positions, data, marker=marker, color=color, label=label, zorder=2) lines.append((symbol, line[0])) @@ -251,7 +152,7 @@ def draw_figures(z_positions, dict_concs_mean, energy_list, frame_list, fig_rang for symbol, line in lines: data = atom_dict_concs[symbol] if average_filter: - data = average_filter_function(data) + data = sim_utilities.average_filter_function(data) line.set_data(z_positions, data) figure_anim.set_size_inches(7, 7) n_frames_width = len(str(len(frame_list))) @@ -461,9 +362,10 @@ if __name__ == '__main__': symbol_marker_color_dict = OrderedDict([(element, (item.split(',')[0], item.split(',')[1])) for element, item in figure_params.items() if element in ase.data.chemical_symbols]) - z_positions, dict_concs_mean, energy_list, frame_list = read_conc_data(sim_path, - symbol_marker_color_dict, - figure_range_dict) + z_positions, dict_concs_mean, \ + energy_list, frame_list = sim_utilities.read_conc_data(sim_path, + symbol_marker_color_dict, + figure_range_dict) if not multi_enable: print("Drawing figures for {}...".format(os.path.basename(sim_path)), end="") draw_figures(z_positions, -- GitLab