Commit cf6814f8 authored by Feliks Kuba Kiszkurno's avatar Feliks Kuba Kiszkurno
Browse files

Rewritten the read_data function and method in the main class.

Now get_file_by_extension is called only once, when the data is read. Changes in other files result mostly of required adjustments.
Apart from this. Now read_data method can accept key-word arguments, so using a dictionary to create main class is not mandatory anymore.
parent 4189de08
from os.path import join as op_join
from os import getcwd as getcwd
from os import scandir as scandir
from os import listdir as listdir
import pandas as pd
from warnings import warn
def getfilesbyextension(folder, ext="pvd"):
def get_files_by_extension(folder, ext="pvd"):
files = listdir(folder)
pvdfiles = []
for file in files:
......@@ -33,22 +32,31 @@ def sort_folders(folder_list):
return folder_list_sorted
def detect_experiments(path, sorted_by='name'):
def detect_experiments(path, sorted_by='name', ext="pvd"):
experiments = {}
exp_folders = []
sub_dirs = scandir(path)
#sub_dirs.sort()
for f in sub_dirs:
if f.is_dir():
potential_pvd_files = getfilesbyextension(f, ext="pvd")
if len(potential_pvd_files) == 1:
#exp_folders.append(potential_pvd_files)
exp_folders.append(f)
exp_folders.append(f)
if sorted_by == 'name':
exp_folders_sorted = sort_folders(exp_folders)
else:
exp_folders_sorted = exp_folders
return exp_folders_sorted
for experiment_dir in exp_folders_sorted:
potential_pvd_files = get_files_by_extension(experiment_dir, ext=ext)
if len(potential_pvd_files) > 1:
warn("Multiple suitable files were found in the experiments folder! First one in the list will be used...")
experiments[experiment_dir.name] = potential_pvd_files[0]
elif len(potential_pvd_files) == 1:
experiments[experiment_dir.name] = potential_pvd_files[0]
elif len(potential_pvd_files) < 1:
warn("No suitable file was found in this folder! The experiment {} will be skipped...".format(experiment_dir.name))
return experiments
def extract_params(folder_name):
......@@ -56,9 +64,9 @@ def extract_params(folder_name):
param_a = {}
param_b = {}
param_a['name'] = folder_name.name.split("_")[0]
param_a['value'] = float(folder_name.name.split("_")[1])
param_b['name'] = folder_name.name.split("_")[-2]
param_b['value'] = float(folder_name.name.split("_")[-1])
param_a['name'] = folder_name.split("_")[0]
param_a['value'] = float(folder_name.split("_")[1])
param_b['name'] = folder_name.split("_")[-2]
param_b['value'] = float(folder_name.split("_")[-1])
return param_a, param_b
import os
import vtuIO
from ogs_compare.Tools.detect_experiments import detect_experiments, getfilesbyextension, extract_params
from ogs_compare.Tools.detect_experiments import detect_experiments, get_files_by_extension, extract_params
# TODO: add option to use different tools for
def read_data(experiments_to_include, field_to_read, points, results_folder):
# TODO: add option to use different tools for reading the file
# TODO: handle the difference between 2D and 3D files automatically
#def read_data(experiment_path, experiments_to_include, field_to_read, points):
def read_data(path, field_to_read, points):
SEC2A = 1 / (356 * 24 * 60 * 60) # time in seconds to time in years
reference_experiment = experiments_to_include[0]
experiments = detect_experiments(path)
reference_experiment = list(experiments.keys())[0]
# Load different experiments
experiments_results = {}
experiments_results_param = {}
for experiment_name in experiments_to_include:
for experiment_name in experiments:
# data_path = os.path.join(os.getcwd(), experiment_name, 'Results', file_name) # Select by experiment folder
file_name = getfilesbyextension(os.path.join(os.getcwd(), results_folder, experiment_name))
data_path = os.path.join(results_folder, experiment_name.name, file_name[0]) # Select by results folder
#file_name = get_files_by_extension(os.path.join(path, experiment_name.name))
data_path = os.path.join(path, experiment_name, experiments[experiment_name]) # Select by results folder
data_file = vtuIO.PVDIO(data_path, dim=2)
experiments_results[experiment_name] = data_file
experiments_results_param[experiment_name] \
......@@ -22,4 +28,4 @@ def read_data(experiments_to_include, field_to_read, points, results_folder):
point_names = list(experiments_results_param[reference_experiment].keys())
time_steps = experiments_results[reference_experiment].timesteps * SEC2A
return experiments_results, experiments_results_param, point_names, time_steps
\ No newline at end of file
return list(experiments.keys()), experiments_results, experiments_results_param, point_names, time_steps
from ogs_compare.Tools.points import load_points, plot_points
from ogs_compare.Tools.detect_experiments import detect_experiments, getfilesbyextension
from ogs_compare.Tools.detect_experiments import detect_experiments, get_files_by_extension
from ogs_compare.ogs_compare import OGSDataCompare
......@@ -15,12 +15,17 @@ from ogs_compare.Tools.get_analytical_model import get_analytica_model
class OGSDataCompare:
def __init__(self, settings):
def __init__(self, settings=None):
#TODO: move to a separate file
if settings is None:
settings = {}
self.constants = types.SimpleNamespace()
self.constants.SEC2A = 1 / (356 * 24 * 60 * 60)
self.constants.PA2MPA = 1e-6
self.points = None
self.config = types.SimpleNamespace()
# Mandatory
self.config.path = os.path.join('')
......@@ -36,8 +41,6 @@ class OGSDataCompare:
self.__read_config(settings)
self.config.output_path = os.path.join(self.config.path, 'Results')
self.points = self.read_points()
self.data = types.SimpleNamespace()
self.data.experiments = None
self.data.experiments_results = None
......@@ -46,22 +49,41 @@ class OGSDataCompare:
self.data.time_steps = None
def read_points(self):
points = load_points(self.config.points_file)
self.points = load_points(self.config.points_file)
if self.config.points_plot is True:
plot_points(self.points, self.config.path)
return points
def read_data(self, path=None, experiments=None, parameter=None, points_path=None, points=None):
"""
Key word parameters can be used to override config provided in the settings dict.
So, if you want to rerun the procedure with new parameter, there is no need to change the dict.
"""
if path is None:
path = self.config.path
#print('Reading data... ')
#self.data.experiments = detect_experiments(path)
if experiments is None:
experiments = self.data.experiments
if parameter is None:
parameter = self.config.parameter
def read_data(self):
print('Reading data... ')
self.data.experiments = detect_experiments(self.config.path)
if points is None and points_path is None:
if self.points is None:
self.config.points_file = points_path
self.read_points()
points = self.points
self.data.experiments_results, self.data.experiments_results_param, self.data.point_names, self.data.time_steps = \
read_data(self.data.experiments,
self.config.parameter,
self.points,
self.config.path)
self.data.experiments, self.data.experiments_results, self.data.experiments_results_param, self.data.point_names, self.data.time_steps = \
read_data(path,
#experiments,
parameter,
points
)
print('Finished reading data.')
def ogs_compare(self, analytical_ref=False, ogs_model=None):
......@@ -100,7 +122,7 @@ class OGSDataCompare:
exp_data_plot = self.data.experiments_results_param[reference_experiment][point_name]
experiments_to_include_new = self.data.experiments.copy()
experiments_to_include_new.remove(reference_experiment)
label_temp = self.data.experiments[0].name
label_temp = self.data.experiments[0]
ax[i][j].plot(self.data.time_steps, exp_data_plot, self.config.color_ref,
label=label_temp)
k += 1
......@@ -112,14 +134,13 @@ class OGSDataCompare:
point_name = self.data.point_names[k]
color_iterator = 0
for experiment_name in experiments_to_include_new:
print(experiment_name)
if sub_params_n > 1:
exp_data_plot = self.data.experiments_results_param[experiment_name][point_name][:, sub_param]
else:
exp_data_plot = self.data.experiments_results_param[experiment_name][point_name]
ax[i][j].plot(self.data.time_steps, exp_data_plot, self.config.colors[color_iterator],
label=experiments_to_include_new[color_iterator].name)
label=self.data.experiments[color_iterator])
color_iterator += 1
ax[i][j].set_title(point_name)
k += 1
......@@ -229,18 +250,18 @@ class OGSDataCompare:
y_values.append(
self.data.experiments_results_param[experiment_name][point_name][time_step_id][
sub_param])
x_axis_labels.append(experiment_name.name)
x_axis_labels.append(experiment_name)
else:
y_values.append(
self.data.experiments_results_param[experiment_name][point_name][time_step_id])
x_axis_labels.append(experiment_name.name)
x_axis_labels.append(experiment_name)
k += 1
# Plot the results
# plt.rcParams['figure.figsize'] = (15, 15)
plt.rcParams.update({'figure.autolayout': True})
fig, ax = plt.subplots(1, 1)
ax.plot(x_positions, y_values, 'rx', label=experiment_name.name)
ax.plot(x_positions, y_values, 'rx', label=experiment_name)
plt.xticks(np.arange(0, k, 1), x_axis_labels, rotation=90)
ax.set_ylabel(self.config.parameter)
......@@ -293,7 +314,7 @@ class OGSDataCompare:
plt.rcParams.update({'figure.autolayout': True})
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(a_param_values, b_param_values, z_values, 'rx', label=experiment_name.name)
ax.scatter(a_param_values, b_param_values, z_values, 'rx', label=experiment_name)
ax.set_ylabel(a_param_name)
ax.set_xlabel(b_param_name)
......@@ -315,6 +336,7 @@ class OGSDataCompare:
def __read_config(self, settings):
# Those are mandatory and have no default value
#TODO: Now mandatory files can be provided, later when read_data is called. Change this part accordingly
missing_setup = []
if 'parameter' in settings.keys():
......@@ -329,12 +351,14 @@ class OGSDataCompare:
if 'points_file' in settings.keys():
self.config.points_file = settings['points_file']
self.read_points()
else:
missing_setup.append('points_file')
if len(missing_setup) == 0:
if len(missing_setup) > 0:
print('Following mandatory fields are missing: ')
print(missing_setup)
print('Please provide them as keyword arguments, when calling read_data method')
# Those are optional and have default values
if 'plot_points' in settings.keys():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment