Commit 7418f269 authored by Feliks Kuba Kiszkurno's avatar Feliks Kuba Kiszkurno
Browse files

Added __save_figure function

Now saving of figures is moved to a separate function shared by all plot functions. Adjusted naming system accordingly.
parent b7fe62ff
from ogs_compare import OGS_data_compare
from ogs_compare import OGSDataCompare
import os
settings = {'parameter': ['temperature_interpolated'],
settings = {'parameter': 'displacement',
'path': os.path.join('/home/kiszkurn/Dev/Data/Experiment1'),
'points_file': os.path.join('/home/kiszkurn/Dev/Data', 'points_isotropic.csv'),
......@@ -10,10 +10,10 @@ settings = {'parameter': ['temperature_interpolated'],
'include_legend': True
}
data_compare = OGS_data_compare(settings)
data_compare = OGSDataCompare(settings)
data_compare.read_data()
data_compare.ogs_compare()
data_compare.ogs_compare_separate()
data_compare.ogs_compare_time_point(time_step=[1.0, 2.0, 3.0, 4.0, 5.0])
data_compare.ogs_compare_3D_view(time_step=[1.0, 2.0, 3.0, 4.0, 5.0])
data_compare.ogs_compare_3d_view(time_step=[1.0, 2.0, 3.0, 4.0, 5.0])
from ogs_compare.Tools.load_points import load_points
from ogs_compare.Tools.plot_points import plot_points
from ogs_compare.Tools.detect_experiments import detect_experiments, getfilesbyextension
from ogs_compare.ogs_compare import OGS_data_compare
from ogs_compare.ogs_compare import OGSDataCompare
......@@ -14,7 +14,7 @@ from ogs_compare.Tools.match_time_step import match_time_step
from ogs_compare.Tools.get_analytical_model import get_analytica_model
class OGS_data_compare():
class OGSDataCompare:
def __init__(self, settings):
......@@ -65,15 +65,14 @@ class OGS_data_compare():
self.config.path)
print('Finished reading data.')
def ogs_compare(self, analytical_ref=True, ogs_model=None):
def ogs_compare(self, analytical_ref=False, ogs_model=None):
reference_experiment = self.data.experiments[0]
experiment_name = self.data.experiments[0]
# Plot everything
sub_params_n = self.data.experiments_results_param[experiment_name][self.data.point_names[0]][
self.config.parameter[0]].shape
sub_params_n = self.data.experiments_results_param[experiment_name][self.data.point_names[0]].shape
if len(sub_params_n) > 1:
sub_params_n = sub_params_n[1]
else:
......@@ -84,11 +83,12 @@ class OGS_data_compare():
fig, ax = plt.subplots(4, 4) # (2, 3) # (4, 4)
# Plot reference line first
experiments_to_include_new = None
k = 0
for i, j in itertools.product(range(ax.shape[0]), range(ax.shape[1])):
point_name = self.data.point_names[k]
if analytical_ref is True:
exp_data_plot = get_analytica_model(self.config.parameter[0], self.points[point_name][0],
exp_data_plot = get_analytica_model(self.config.parameter, self.points[point_name][0],
self.points[point_name][1], self.points[point_name][2],
self.data.time_steps / self.constants.SEC2A,
ogs_model=ogs_model)
......@@ -96,11 +96,10 @@ class OGS_data_compare():
label_temp = 'analytical_model'
else:
if sub_params_n > 1:
exp_data_plot = self.data.experiments_results_param[reference_experiment][point_name][
self.config.parameter[0]][:, sub_param]
exp_data_plot = self.data.experiments_results_param[reference_experiment][point_name][:,
sub_param]
else:
exp_data_plot = self.data.experiments_results_param[reference_experiment][point_name][
self.config.parameter[0]]
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
......@@ -113,19 +112,17 @@ class OGS_data_compare():
for i, j in itertools.product(range(ax.shape[0]), range(ax.shape[1])):
point_name = self.data.point_names[k]
l = 0
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][
self.config.parameter[0]][:, sub_param]
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][
self.config.parameter[0]]
ax[i][j].plot(self.data.time_steps, exp_data_plot, self.config.colors[l],
label=experiments_to_include_new[l].name)
l += 1
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)
color_iterator += 1
ax[i][j].set_title(point_name)
k += 1
......@@ -137,17 +134,12 @@ class OGS_data_compare():
plt.setp(ax[-1, :], xlabel='Time [a]')
plt.setp(ax[:, 0], ylabel=self.config.parameter) # TODO add units
if sub_params_n > 1:
fig.suptitle(self.config.parameter[0] + '_' + str(sub_param))
fig.suptitle(self.config.parameter + '_' + str(sub_param))
else:
fig.suptitle(self.config.parameter[0])
fig.suptitle(self.config.parameter)
for ext in self.config.plot_ext:
if sub_params_n > 1:
fig.savefig(os.path.join(self.config.output_path,
"{}.{}".format(self.config.parameter[0] + '_' + str(sub_param), ext)))
else:
fig.savefig(os.path.join(self.config.output_path,
"{}.{}".format(self.config.parameter[0], ext)))
output_file_name = "compare_{}_{}".format(self.config.parameter, str(sub_param))
self.__save_figure(fig, output_file_name)
def ogs_compare_separate(self):
......@@ -157,7 +149,7 @@ class OGS_data_compare():
# Plot separate diagrams
# plt.rcParams['figure.figsize'] = (8, 8)
sub_params_n = self.data.experiments_results_param[experiment_name][self.data.point_names[0]][self.config.parameter[0]].shape
sub_params_n = self.data.experiments_results_param[experiment_name][self.data.point_names[0]].shape
if len(sub_params_n) > 1:
sub_params_n = sub_params_n[1]
else:
......@@ -168,27 +160,25 @@ class OGS_data_compare():
fig_sep, ax_sep = plt.subplots(1, figsize=(14, 14))
point_name = self.data.point_names[m]
if sub_params_n > 1:
exp_data_plot = self.data.experiments_results_param[reference_experiment][point_name][self.config.parameter[0]][:,
sub_param]
exp_data_plot = self.data.experiments_results_param[reference_experiment][point_name][:, sub_param]
else:
exp_data_plot = self.data.experiments_results_param[reference_experiment][point_name][
self.config.parameter[0]]
exp_data_plot = self.data.experiments_results_param[reference_experiment][point_name]
ax_sep.plot(self.data.time_steps * self.constants.SEC2A, exp_data_plot,
self.config.colors[0],
label=self.data.experiments[0])
l = 0
color_iterator = 0
for experiment_name in self.data.experiments:
if sub_params_n > 1:
exp_data_plot = self.data.experiments_results_param[experiment_name][point_name][self.config.parameter[0]][:,
sub_param]
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][
self.config.parameter[0]]
exp_data_plot = self.data.experiments_results_param[experiment_name][point_name]
ax_sep.plot(self.data.time_steps * self.constants.SEC2A, exp_data_plot,
self.config.colors[l + 1],
label=self.data.experiments[l])
l += 1
R = np.sqrt(self.points[point_name][0] ** 2 + self.points[point_name][1] ** 2 + self.points[point_name][2] ** 2)
self.config.colors[color_iterator + 1],
label=self.data.experiments[color_iterator])
color_iterator += 1
R = np.sqrt(
self.points[point_name][0] ** 2 + self.points[point_name][1] ** 2 + self.points[point_name][
2] ** 2)
ax_sep.set_title("Pressure observed at {}, R={:.2f}m".format(point_name, R))
lines, labels = ax_sep.get_legend_handles_labels()
......@@ -197,11 +187,13 @@ class OGS_data_compare():
ax_sep.set_ylabel('p_LR / MPa') # TODO add units
# fig.suptitle('Pressure')
for ext in self.config.plot_ext:
fig_sep.set_size_inches(8, 8)
fig_sep.set_dpi(200)
fig_sep.savefig(os.path.join(self.config.output_path, "{}.{}".format(
self.config.parameter[0] + '_' + str(sub_param) + '_' + point_name, ext)), dpi=200)
fig_sep.set_size_inches(8, 8)
fig_sep.set_dpi(200)
output_file_name = "compare_separate_{}_{}_{}".format(self.config.parameter,
str(sub_param),
point_name)
self.__save_figure(fig_sep, output_file_name)
if self.config.plot_legend is True:
# Plot and save legend as separate file
......@@ -209,15 +201,16 @@ class OGS_data_compare():
fig_legend.legend(lines, labels)
fig_legend.tight_layout()
fig_legend.show()
for ext in self.config.plot_ext:
fig_legend.savefig(os.path.join(self.config.output_path, "{}.{}".format('legend', ext)), dpi=200)
output_file_name = "compare_separate_legend"
self.__save_figure(fig_legend, output_file_name)
def ogs_compare_time_point(self, time_step, analytical_ref=True, ogs_model=None):
experiment_name = self.data.experiments[0]
# Prepare results
sub_params_n = self.data.experiments_results_param[experiment_name][self.data.point_names[0]][self.config.parameter[0]].shape
sub_params_n = self.data.experiments_results_param[experiment_name][self.data.point_names[0]].shape
if len(sub_params_n) > 1:
sub_params_n = sub_params_n[1]
else:
......@@ -236,12 +229,12 @@ class OGS_data_compare():
x_positions.append(k)
if sub_params_n > 1:
y_values.append(
self.data.experiments_results_param[experiment_name][point_name][self.config.parameter[0]][time_step_id][
self.data.experiments_results_param[experiment_name][point_name][time_step_id][
sub_param])
x_axis_labels.append(experiment_name.name)
else:
y_values.append(
self.data.experiments_results_param[experiment_name][point_name][self.config.parameter[0]][time_step_id])
self.data.experiments_results_param[experiment_name][point_name][time_step_id])
x_axis_labels.append(experiment_name.name)
k += 1
......@@ -252,32 +245,22 @@ class OGS_data_compare():
ax.plot(x_positions, y_values, 'rx', label=experiment_name.name)
plt.xticks(np.arange(0, k, 1), x_axis_labels, rotation=90)
ax.set_ylabel(self.config.parameter[0])
ax.set_ylabel(self.config.parameter)
ax.set_xlabel('Experiment name')
point_and_time = "{}_{:.2f}".format(point_name, self.data.time_steps[time_step_id])
output_file_name = "compare_time_point_{}_{}_t_{:.2f}_{}".format(self.config.parameter,
str(sub_param),
self.data.time_steps[time_step_id],
point_name)
if sub_params_n > 1:
fig.suptitle("{} at {} at {:.2f}".format(self.config.parameter[0] + str(sub_params_n), point_name,
self.data.time_steps[time_step_id]))
for ext in self.config.plot_ext:
fig.savefig(os.path.join(self.config.output_path,
"{}_{}.{}".format(self.config.parameter[0] + '_' + str(sub_param),
point_and_time,
ext)))
else:
fig.suptitle("{} at {} at {:.2f}".format(self.config.parameter[0], point_name,
self.data.time_steps[time_step_id]))
for ext in self.config.plot_ext:
fig.savefig(os.path.join(self.config.output_path,
"{}_{}.{}".format(self.config.parameter[0], point_and_time, ext)))
self.__save_figure(fig, output_file_name)
def ogs_compare_3D_view(self, time_step=None):
def ogs_compare_3d_view(self, time_step=None):
experiment_name = self.data.experiments[0]
# Prepare results
sub_params_n = self.data.experiments_results_param[experiment_name][self.data.point_names[0]][self.config.parameter[0]].shape
sub_params_n = self.data.experiments_results_param[experiment_name][self.data.point_names[0]].shape
if len(sub_params_n) > 1:
sub_params_n = sub_params_n[1]
else:
......@@ -300,12 +283,12 @@ class OGS_data_compare():
b_param_values.append(b_param['value'])
if sub_params_n > 1:
z_values.append(
self.data.experiments_results_param[experiment_name][point_name][self.config.parameter[0]][time_step_id][
self.data.experiments_results_param[experiment_name][point_name][time_step_id][
sub_param])
else:
z_values.append(
self.data.experiments_results_param[experiment_name][point_name][self.config.parameter[0]][time_step_id])
self.data.experiments_results_param[experiment_name][point_name][time_step_id])
# Plot the results
# plt.rcParams['figure.figsize'] = (15, 15)
......@@ -316,31 +299,21 @@ class OGS_data_compare():
ax.set_ylabel(a_param_name)
ax.set_xlabel(b_param_name)
ax.set_zlabel(self.config.parameter[0])
ax.set_zlabel(self.config.parameter)
ax.view_init(azim=20, elev=45)
point_and_time = "{}_{:.2f}".format(point_name, self.data.time_steps[time_step_id])
output_file_name = "3D_values_{}_{}_t_{:.2f}_{}".format(self.config.parameter,
str(sub_param),
self.data.time_steps[time_step_id],
point_name)
if sub_params_n > 1:
fig.suptitle("{} at {} at {:.2f}".format(self.config.parameter[0] + str(sub_param), point_name,
self.data.time_steps[time_step_id]))
for ext in self.config.plot_ext:
fig.savefig(os.path.join(self.config.output_path,
"{}_{}_3D_values.{}".format(
self.config.parameter[0] + '_' + str(sub_param),
point_and_time,
ext)))
else:
fig.suptitle("{} at {} at {:.2f}".format(self.config.parameter[0], point_name,
self.data.time_steps[time_step_id]))
for ext in self.config.plot_ext:
fig.savefig(os.path.join(self.config.output_path,
"{}_{}_3D_values.{}".format(self.config.parameter[0], point_and_time,
ext)))
self.__save_figure(fig, output_file_name)
def __save_figure(self):
pass
def __save_figure(self, fig, output_file_name):
for ext in self.config.plot_ext:
fig.savefig(os.path.join(self.config.output_path,
"{}.{}".format(output_file_name, ext)))
def __read_config(self, settings):
# Those are mandatory and have no default value
......@@ -394,4 +367,3 @@ class OGS_data_compare():
if 'include_legend' in settings.keys():
self.config.include_legend = settings['include_legend']
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