# First load RESSPyLab and necessary packages
import numpy as np
import RESSPyLab as rpl

"""
Explanation: Post-processing Examples
This notebook provides some examples for using the post-processing features in RESSPyLab.
Automatic table generation and calculation of the consistency metric $\xi_2$ are shown for both the original and updated Voce-Chaboche (UVC) models.
Note that there is an example for plotting output in each of the calibration examples.
End of explanation
"""


# Identify the material
material_def = {'material_id': ['Example 1'], 'load_protocols': ['1,5']}
# Set the path to the x log file
x_log_file_1 = './output/x_log.txt'
x_logs_all = [x_log_file_1]
# Load the data
data_files_1 = ['example_1.csv']
data_1 = rpl.load_data_set(data_files_1)
data_all = [data_1]

# Make the tables
param_table, metric_table = rpl.summary_tables_maker_vc(material_def, x_logs_all, data_all)

"""
Explanation: Original Voce-Chaboche model
First we will use RESSPyLab to generate a formatted table of parameters including the relative error metric, $\bar{\varphi}$.
The inputs to this function are: 
1. Information about the name of the data set and the load protocols used in the optimization.
2. The file containing the history of parameters (generated from the optimization).
3. The data used in the optimization.
Two tables are returned (as pandas DataFrames) and are printed to screen in LaTeX format.
If you want the tables in some other format it is best to operate on the DataFrames directly (e.g., use to_csv()).
End of explanation
"""


# Load the base parameters, we want the last entry in the file
x_base = np.loadtxt(x_log_file_1, delimiter=' ')
x_base = x_base[-1]
# Load (or set) the sample parameters
x_sample = np.array([179750., 318.47, 100.72, 8.00, 11608.17, 145.22, 1026.33, 4.68])

# Calculate the metric
consistency_metric = rpl.vc_consistency_metric(x_base, x_sample, data_1)
print consistency_metric

"""
Explanation: Tables can be easily generated following a standard format for several data sets by appending additional entries to the lists of values in material_def and to x_logs_all and data_all.
Now we will generate the consistency metric, $\xi_2$.
The input arguments are:
1. The parameters of the base case.
2. The parameters of the case that you would like to compare with.
3. The set of data to compute this metric over.
The metric is returned (the raw value, NOT as a percent) directly from this function.
End of explanation
"""


# Identify the material
material_def = {'material_id': ['Example 1'], 'load_protocols': ['1']}
# Set the path to the x log file
x_log_file_2 = './output/x_log_upd.txt'
x_logs_all = [x_log_file_2]
# Load the data
data_files_2 = ['example_1.csv']
data_2 = rpl.load_data_set(data_files_2)
data_all = [data_2]

# Make the tables
param_table, metric_table = rpl.summary_tables_maker_uvc(material_def, x_logs_all, data_all)

"""
Explanation: The value of $\xi_2 = 65$ %, indicating that the two sets of parameters are inconsistent for this data set.
Updated Voce-Chaboche model
The inputs to generate the tables are the same as for the original model, however the input parameters have to come from optimization using the updated model.
End of explanation
"""


