To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 8069cb13 authored by Feliks Kiszkurno's avatar Feliks Kiszkurno
Browse files

Prediction on separate batches should work.

parent d270c591
......@@ -8,6 +8,9 @@
<orderEntry type="jdk" jdkName="Python 3.7 (SlopeStability)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PackageRequirementsSettings">
<option name="removeUnused" value="true" />
</component>
<component name="PyDocumentationSettings">
<option name="renderExternalDocumentation" value="true" />
</component>
......
......@@ -41,7 +41,13 @@ if not create_new_data:
# test_results = slopestabilitytools.reassign_classes(test_results, class_type)
# Check if folder structure for figures exists and create it if not
is_success = slopestabilitytools.folder_structure.create_folder_structure()
if settings.settings['use_batches'] is True:
is_success = slopestabilitytools.folder_structure.create_folder_structure(batch_names=test_results['prediction'].keys())
elif settings.settings['use_batches'] is False:
is_success = slopestabilitytools.folder_structure.create_folder_structure()
# Create new data
else:
......
numpy~=1.18.5
matplotlib~=3.3.2
pandas~=1.1.5
matplotlib~=3.4.1
pandas~=1.2.4
pygimli~=1.1.0
scikit-learn~=0.23.2
scipy~=1.5.2
keras~=2.3.1
\ No newline at end of file
scikit-learn~=0.24.1
scipy~=1.6.2
keras~=2.4.3
joblib~=1.0.1
\ No newline at end of file
......@@ -7,6 +7,7 @@ Created on 26.03.2021
"""
import os
import slopestabilitytools
def init():
......@@ -15,14 +16,27 @@ def init():
settings = {}
# Paths
settings['base_folder'] = os.getcwd()
settings['results_folder'] = os.path.join(settings['base_folder'], 'results')
settings['data_folder'] = os.path.join(settings['results_folder'], 'data')
settings['data_folder_grd'] = os.path.join(settings['results_folder'], 'data_grd')
settings['figures_folder'] = os.path.join(settings['results_folder'], 'figures')
settings['clf_folder'] = os.path.join(settings['results_folder'], 'classifiers')
# Training and prediction split
settings['split_proportion'] = 0.75 # Part of available profiles that will be used for prediction
settings['data_split'] = 'predefined' # 'random' or 'predefined'
settings['use_batches'] = True # True or False
if settings['use_batches'] is True:
settings['data_split'] = 'predefined'
settings['reuse_classifier'] = False
settings['retrain_clf'] = False # True trains classifiers for each batch separately
settings['reuse_clf'] = True # Load classifiers if they exist in classifiers folder
if settings['reuse_clf'] is True:
settings['clf_trained'] = slopestabilitytools.find_clf() # List of trained classifiers, they won't be retrained unless retrain_clf is set to True
else:
settings['clf_trained'] = []
# Interpolate results to grid inside create_data script
settings['grd'] = True
......@@ -52,13 +66,6 @@ def init():
settings['optimize_ml'] = False # True - performs hyperparameter search
settings['optimize_ml_type'] = 'exhaustive' # Type of grid search exhaustive or halved
# Paths
settings['results_folder'] = 'results'
settings['data_folder'] = os.path.join(settings['results_folder'], 'data')
settings['data_folder_grd'] = os.path.join(settings['results_folder'], 'data_grd')
settings['figures_folder'] = os.path.join(settings['results_folder'], 'figures')
settings['clf_folder'] = os.path.join(settings['results_folder'], 'classifiers')
# Plots
settings['plot_formats'] = ['png'] # list of formats to save plots as, supported formats: eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff
......
......@@ -37,15 +37,11 @@ def gbc_run(test_results, random_seed):
clf = ensemble.GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
# Train classifier
result_class, accuracy_labels, accuracy_score, accuracy_labels_training, accuracy_score_training,\
depth_estim, depth_true, depth_estim_accuracy, depth_estim_labels, depth_estim_training,\
depth_true_training, depth_estim_accuracy_training, depth_estim_labels_training = \
results, result_class = \
slopestabilityML.run_classification(test_training, test_prediction, test_results, clf, 'GBC')
# Plot
# slopestabilityML.plot_results(accuracy_labels, accuracy_score, 'GBC_prediction')
# slopestabilityML.plot_results(accuracy_labels_training, accuracy_score_training, 'GBC_training')
return result_class, accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training,\
depth_estim, depth_true, depth_estim_accuracy, depth_estim_labels, depth_estim_training,\
depth_true_training, depth_estim_accuracy_training, depth_estim_labels_training
return results, result_class
......@@ -36,15 +36,11 @@ def knn_run(test_results, random_seed):
clf = KNeighborsClassifier(n_neighbors=2, n_jobs=-1)
# Train classifier
result_class, accuracy_labels, accuracy_score, accuracy_labels_training, accuracy_score_training,\
depth_estim, depth_true, depth_estim_accuracy, depth_estim_labels, depth_estim_training,\
depth_true_training, depth_estim_accuracy_training, depth_estim_labels_training = \
results, result_class = \
slopestabilityML.run_classification(test_training, test_prediction, test_results, clf, 'KNN')
# Plot
# slopestabilityML.plot_results(accuracy_labels, accuracy_score, 'KNN_prediction')
# slopestabilityML.plot_results(accuracy_labels_training, accuracy_score_training, 'KNN_training')
return result_class, accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training,\
depth_estim, depth_true, depth_estim_accuracy, depth_estim_labels, depth_estim_training,\
depth_true_training, depth_estim_accuracy_training, depth_estim_labels_training
\ No newline at end of file
return results, result_class
\ No newline at end of file
......@@ -36,15 +36,11 @@ def sgd_run(test_results, random_seed):
clf = SGDClassifier(loss="hinge", penalty="l2", max_iter=5, n_jobs=-1)
# Train classifier
result_class, accuracy_labels, accuracy_score, accuracy_labels_training, accuracy_score_training, \
depth_estim, depth_true, depth_estim_accuracy, depth_estim_labels, depth_estim_training, \
depth_true_training, depth_estim_accuracy_training, depth_estim_labels_training = \
results, result_class = \
slopestabilityML.run_classification(test_training, test_prediction, test_results, clf, 'SGD')
# Plot
# slopestabilityML.plot_results(accuracy_labels, accuracy_score, 'SGD_prediction')
# slopestabilityML.plot_results(accuracy_labels_training, accuracy_score_training, 'SGD_training')
return result_class, accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training, \
depth_estim, depth_true, depth_estim_accuracy, depth_estim_labels, depth_estim_training, \
depth_true_training, depth_estim_accuracy_training, depth_estim_labels_training
return results, result_class
......@@ -41,15 +41,11 @@ def svm_run(test_results, random_seed):
clf = svm.SVC(gamma=0.001, C=100, kernel='linear')
# Train classifier
result_class, accuracy_labels, accuracy_score, accuracy_labels_training, accuracy_score_training, depth_estim, \
depth_true, depth_estim_accuracy, depth_estim_labels, depth_estim_training, depth_true_training, \
depth_estim_accuracy_training, depth_estim_labels_training = \
results, result_class = \
slopestabilityML.run_classification(test_training, test_prediction, test_results, clf, 'SVM')
# Plot
# slopestabilityML.plot_results(accuracy_labels, accuracy_score, 'SVM_prediction')
# slopestabilityML.plot_results(accuracy_labels_training, accuracy_score_training, 'SVM_training')
return result_class, accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training, \
depth_estim, depth_true, depth_estim_accuracy, depth_estim_labels, depth_estim_training, depth_true_training,\
depth_estim_accuracy_training, depth_estim_labels_training
return results, result_class
......@@ -14,7 +14,6 @@ import slopestabilityML
def ask_committee(ml_result_class, test_results, *, random_seed=False):
classes_correct = {}
if any(key in test_results.keys() for key in ('training', 'prediction')):
test_results_orig = test_results.copy()
......@@ -32,7 +31,7 @@ def ask_committee(ml_result_class, test_results, *, random_seed=False):
print('I don\'t know which class to use! Exiting...')
exit(0)
classes_correct[test_name] = class_in
# First create list of datasets and verify if all classifiers has been run over the same sets
test_names_all = []
for method_name in sorted(ml_result_class.keys()):
......@@ -44,14 +43,16 @@ def ask_committee(ml_result_class, test_results, *, random_seed=False):
# Combine results for each data set from each classifier into an array
results_test = {}
for test_name in test_names_all:
results = np.zeros([len(ml_result_class[list(ml_result_class.keys())[0]][test_name]), len(sorted(ml_result_class.keys()))])
results = np.zeros(
[len(ml_result_class[list(ml_result_class.keys())[0]][test_name]), len(sorted(ml_result_class.keys()))])
method_id = 0
for method_name in sorted(ml_result_class.keys()):
if settings.settings['use_labels'] is True:
ml_result_numeric = slopestabilitytools.label2numeric(ml_result_class[method_name][test_name])
else:
ml_result_numeric = ml_result_class[method_name][test_name]
results[:, method_id] = np.array(ml_result_numeric).T.reshape([len(ml_result_class[method_name][test_name])])
results[:, method_id] = np.array(ml_result_numeric).T.reshape(
[len(ml_result_class[method_name][test_name])])
method_id = method_id + 1
results_test[test_name] = results
......@@ -93,4 +94,9 @@ def ask_committee(ml_result_class, test_results, *, random_seed=False):
accuracy_score.append(score * 100)
accuracy_labels.append(test_name)
return accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training
result_com = {'prediction': {'accuracy_score': accuracy_score,
'accuracy_labels': accuracy_labels},
'training': {'accuracy_score': accuracy_score_training,
'accuracy_labels': accuracy_labels_training}}
return result_com
......@@ -10,6 +10,8 @@ import settings
import slopestabilityML
import pandas as pd
import numpy as np
import os
import joblib
from scipy import interpolate
from sklearn.preprocessing import StandardScaler, OneHotEncoder
......@@ -23,9 +25,24 @@ import slopestabilitytools
import test_definitions
def classification_predict():
def classification_predict(test_prediction, test_results, clf_name, *, batch_name=''):
result_class = {}
accuracy_result = []
accuracy_labels = []
depth_estim = []
depth_true = []
depth_estim_accuracy = []
depth_estim_labels = []
if settings.settings['reuse_clf'] is True:
if clf_name in settings.settings['clf_trained']:
clf_name_ext = clf_name + '.sav'
clf_pipeline = joblib.load(os.path.join(settings.settings['clf_folder'], clf_name_ext))
# Predict with classifier
for test_name_pred in test_prediction:
# Prepare data
......@@ -55,27 +72,7 @@ def classification_predict():
y = x_question['Y'].to_numpy()
xi, yi, gridded_data = slopestabilitytools.grid_data(x, y, {'class': y_pred})
y_pred_grid = gridded_data['class']
'''
y_pred_grid = gridded_data['class']
# depth_all = np.zeros(y_pred_grid.shape[0])
# depth_all_correct = np.ones(y_pred_grid.shape[0]) * test_definitions.test_parameters[test_name_pred]['layers_pos'][0]
# for column_id in range(y_pred_grid.shape[0]):
# # if len(np.unique(y_pred_grid[:,column_id])) is not 2:
# depth_id = np.array(np.where(y_pred_grid[:, column_id] == 4))
# if np.size(depth_id) is 0:
# depth = yi[-1]
# else:
# depth_id = depth_id.min()
# depth = yi[depth_id]
# depth_all[column_id] = depth
# depth_interface_estimate = np.mean(depth_all)
#
# depth_interface_accuracy = (mean_absolute_error(depth_all_correct, depth_all) / abs(test_definitions.test_parameters[name]['layers_pos'][0]))*100
# print(depth_interface_accuracy)
# depth_estim.append(depth_interface_estimate)
# depth_estim_accuracy.append(depth_interface_accuracy)
# depth_estim_labels.append(test_name_pred + '_' + str(test_definitions.test_parameters[test_name_pred]['layers_pos'][0]))
'''
result_grid_rolled = np.roll(y_pred_grid, -1, axis=0)
y_pred_grid_deri = y_pred_grid - result_grid_rolled
y_pred_grid_deri[-1, :] = 0
......@@ -125,9 +122,9 @@ def classification_predict():
# depth_accuracy=depth_interface_accuracy)
slopestabilityML.plot_class_overview(test_results[test_name_pred], test_name_pred_orig, class_in, y_pred,
clf_name, training=True, depth_estimate=depth_interface_estimate,
clf_name, training=False, depth_estimate=depth_interface_estimate,
interface_y=y_estimate_interp, interface_x=sorted(x),
depth_accuracy=depth_interface_accuracy)
depth_accuracy=depth_interface_accuracy, batch_name=batch_name)
# Evaluate result
# accuracy_.append(len(np.where(y_pred == y_answer.to_numpy())) / len(y_answer.to_numpy()) * 100)
......@@ -136,4 +133,5 @@ def classification_predict():
# Evaluate
return
return result_class, accuracy_labels, accuracy_result, depth_estim, depth_true, \
depth_estim_accuracy, depth_estim_labels,
......@@ -27,9 +27,16 @@ import test_definitions
def classification_train(test_training, test_results, clf, clf_name):
result_class_training = {}
accuracy_result_training = []
accuracy_labels_training = []
depth_estim_training = []
depth_true_training = []
depth_estim_accuracy_training = []
depth_estim_labels_training = []
num_feat = []
if settings.settings['norm'] is True:
......@@ -86,8 +93,10 @@ def classification_train(test_training, test_results, clf, clf_name):
clf_pipeline.fit(x_train, y_train)
clf_file_name = os.path.join(settings.settings['clf_folder'], clf_name, '.sav')
clf_name_ext = clf_name + '.sav'
clf_file_name = os.path.join(settings.settings['clf_folder'], clf_name_ext)
joblib.dump(clf_pipeline, clf_file_name)
settings.settings['clf_trained'] = slopestabilitytools.find_clf()
for name in test_training:
print(name)
......@@ -100,6 +109,7 @@ def classification_train(test_training, test_results, clf, clf_name):
class_correct = test_results_combined['CLASS'].loc[index]
x_train_temp = x_train.loc[index]
y_pred = clf_pipeline.predict(x_train_temp)
result_class_training[name] = y_pred
score_training = accuracy_score(class_correct, y_pred)
accuracy_result_training.append(score_training * 100)
accuracy_labels_training.append(name)
......@@ -162,5 +172,5 @@ def classification_train(test_training, test_results, clf, clf_name):
del y_pred, y_pred_grid, y_pred_grid_deri, y, x, y_actual, xi, yi, y_estimate_interp, depth_interface_accuracy
del depth_interface_estimate, depth_interface_accuracy_mean, depth_interface_estimate_count, depth_interface_estimate_mean
return depth_estim_training, depth_true_training, depth_estim_accuracy_training,\
depth_estim_labels_training
return result_class_training, depth_estim_training, depth_true_training, depth_estim_accuracy_training,\
depth_estim_labels_training, accuracy_result_training, accuracy_labels_training
......@@ -13,7 +13,7 @@ from pathlib import Path
import slopestabilitytools
def combine_results(ml_results):
def combine_results(ml_results, *, batch_name=''):
print('Plotting the summary...')
......@@ -26,10 +26,20 @@ def combine_results(ml_results):
prediction_score_num = 0
for method_name in sorted(ml_results.keys()):
plt.plot(ml_results[method_name]['labels'], ml_results[method_name]['score'], marker='x',
label=method_name)
prediction_score_sum = prediction_score_sum + np.sum(np.array(ml_results[method_name]['score']))
prediction_score_num = prediction_score_num + len(ml_results[method_name]['score'])
if batch_name != '':
plt.plot(ml_results[method_name]['prediction'][batch_name]['accuracy_labels'],
ml_results[method_name]['prediction'][batch_name]['accuracy_score'], marker='x',
label=method_name)
prediction_score_sum = prediction_score_sum + np.sum(
np.array(ml_results[method_name]['prediction'][batch_name]['accuracy_score']))
prediction_score_num = prediction_score_num + len(ml_results[method_name]['prediction'][batch_name]['accuracy_score'])
else:
plt.plot(ml_results[method_name]['prediction']['accuracy_labels'], ml_results[method_name]['prediction']['accuracy_score'], marker='x',
label=method_name)
prediction_score_sum = prediction_score_sum + np.sum(np.array(ml_results[method_name]['prediction']['accuracy_score']))
prediction_score_num = prediction_score_num + len(ml_results[method_name]['prediction']['accuracy_score'])
prediction_score_avg = prediction_score_sum / prediction_score_num
print('Prediction accuracy: {result:.2f}%'.format(result=prediction_score_avg))
......@@ -41,7 +51,7 @@ def combine_results(ml_results):
plt.ylabel('Correct points [%]')
plt.legend(loc='lower right')
slopestabilitytools.save_plot(fig, '', 'ML_summary_prediction', skip_fileformat=True)
slopestabilitytools.save_plot(fig, '', 'ML_summary_prediction', skip_fileformat=True, batch_name=batch_name)
del fig
......@@ -54,10 +64,20 @@ def combine_results(ml_results):
training_score_num = 0
for method_name in sorted(ml_results.keys()):
plt.plot(ml_results[method_name]['labels_training'], ml_results[method_name]['score_training'], marker='x',
label=method_name)
training_score_sum = training_score_sum + np.sum(np.array(ml_results[method_name]['score_training']))
training_score_num = training_score_num + len(ml_results[method_name]['score_training'])
print(method_name)
if method_name == 'com':
continue
else:
if batch_name != '':
plt.plot(ml_results[method_name]['training']['accuracy_labels'], ml_results[method_name]['training']['accuracy_score'], marker='x',
label=method_name)
training_score_sum = training_score_sum + np.sum(np.array(ml_results[method_name]['training']['accuracy_score']))
training_score_num = training_score_num + len(ml_results[method_name]['training']['accuracy_score'])
else:
plt.plot(ml_results[method_name]['training']['accuracy_labels'], ml_results[method_name]['training']['accuracy_score'], marker='x',
label=method_name)
training_score_sum = training_score_sum + np.sum(np.array(ml_results[method_name]['training']['accuracy_score']))
training_score_num = training_score_num + len(ml_results[method_name]['training']['accuracy_score'])
training_score_avg = training_score_sum / training_score_num
print('Training accuracy: {result:.2f}%'.format(result=training_score_avg))
......@@ -71,7 +91,7 @@ def combine_results(ml_results):
plt.legend(loc='lower right')
#fig.tight_layout()
slopestabilitytools.save_plot(fig, '', 'ML_summary_training', skip_fileformat=True)
slopestabilitytools.save_plot(fig, '', 'ML_summary_training', skip_fileformat=True, batch_name=batch_name)
del fig
......@@ -91,10 +111,20 @@ def combine_results(ml_results):
if method_name is 'com':
print('Skipping com')
else:
plt.plot(ml_results[method_name]['depth_labels'], ml_results[method_name]['depth_accuracy'], marker='x',
label=method_name)
prediction_depth_estim_sum = prediction_depth_estim_sum + np.sum(np.array(ml_results[method_name]['depth_accuracy']))
prediction_depth_estim_num = prediction_depth_estim_num + len(ml_results[method_name]['depth_accuracy'])
if batch_name != '':
plt.plot(ml_results[method_name]['prediction'][batch_name]['accuracy_labels'],
ml_results[method_name]['prediction'][batch_name]['depth_estim_accuracy'], marker='x',
label=method_name)
prediction_depth_estim_sum = prediction_depth_estim_sum + \
np.sum(np.array(ml_results[method_name]['prediction'][batch_name]['depth_estim_accuracy']))
prediction_depth_estim_num = prediction_depth_estim_num + \
len(ml_results[method_name]['prediction'][batch_name]['depth_estim_accuracy'])
else:
plt.plot(ml_results[method_name]['depth_labels'], ml_results[method_name]['prediction']['depth_estim_accuracy'], marker='x',
label=method_name)
prediction_depth_estim_sum = prediction_depth_estim_sum + np.sum(np.array(ml_results[method_name]['prediction']['depth_estim_accuracy']))
prediction_depth_estim_num = prediction_depth_estim_num + len(ml_results[method_name]['prediction']['depth_estim_accuracy'])
prediction_depth_estim_avg = prediction_depth_estim_sum / prediction_depth_estim_num
print('Prediction depth accuracy: {result:.2f}%'.format(result=prediction_depth_estim_avg))
......@@ -108,7 +138,7 @@ def combine_results(ml_results):
plt.ylabel('Accuracy of the interface detection [error%]')
plt.legend(loc='lower right')
slopestabilitytools.save_plot(fig, '', 'ML_summary_prediction_depth_estim', skip_fileformat=True)
slopestabilitytools.save_plot(fig, '', 'ML_summary_prediction_depth_estim', skip_fileformat=True, batch_name=batch_name)
del fig
......@@ -124,10 +154,19 @@ def combine_results(ml_results):
if method_name is 'com':
print('Skipping com')
else:
plt.plot(ml_results[method_name]['depth_labels_training'], ml_results[method_name]['depth_accuracy_training'], marker='x',
label=method_name)
training_depth_estim_sum = training_depth_estim_sum + np.sum(np.array(ml_results[method_name]['depth_accuracy_training']))
training_depth_estim_num = training_depth_estim_num + len(ml_results[method_name]['depth_accuracy_training'])
if batch_name != '':
plt.plot(ml_results[method_name]['training']['depth_estim_labels'],
ml_results[method_name]['training']['depth_estim_accuracy'], marker='x',
label=method_name)
training_depth_estim_sum = training_depth_estim_sum + \
np.sum(np.array(ml_results[method_name]['training']['depth_estim_accuracy']))
training_depth_estim_num = training_depth_estim_num + \
len(ml_results[method_name]['training']['depth_estim_accuracy'])
else:
plt.plot(ml_results[method_name]['depth_labels_training'], ml_results[method_name]['training']['depth_estim_accuracy'], marker='x',
label=method_name)
training_depth_estim_sum = training_depth_estim_sum + np.sum(np.array(ml_results[method_name]['training']['depth_estim_accuracy']))
training_depth_estim_num = training_depth_estim_num + len(ml_results[method_name]['training']['depth_estim_accuracy'])
training_depth_estim_avg = training_depth_estim_sum / training_depth_estim_num
print('Training depth accuracy: {result:.2f}%'.format(result=training_depth_estim_avg))
......@@ -143,4 +182,4 @@ def combine_results(ml_results):
plt.legend(loc='lower right')
# fig.tight_layout()
slopestabilitytools.save_plot(fig, '', 'ML_summary_training_depth_estim', skip_fileformat=True)
\ No newline at end of file
slopestabilitytools.save_plot(fig, '', 'ML_summary_training_depth_estim', skip_fileformat=True, batch_name=batch_name)
\ No newline at end of file
......@@ -15,7 +15,8 @@ import test_definitions
import settings
def plot_class_overview(test_results, test_name, class_in, y_pred, clf_name, *, training=False, depth_estimate='x', interface_y='x', interface_x='x', depth_accuracy='x'):
def plot_class_overview(test_results, test_name, class_in, y_pred, clf_name, *, training=False, depth_estimate='x', \
interface_y='x', interface_x='x', depth_accuracy='x', batch_name=''):
x = test_results['X'].to_numpy()
y = test_results['Y'].to_numpy()
......@@ -135,10 +136,10 @@ def plot_class_overview(test_results, test_name, class_in, y_pred, clf_name, *,
if training is True:
slopestabilitytools.save_plot(fig, clf_name, '_ML_{}_class_overview_training'.format(test_name),
subfolder='ML/training')
subfolder='ML/training', batch_name=batch_name)
else:
slopestabilitytools.save_plot(fig, clf_name, '_ML_{}_class_overview_prediction'.format(test_name),
subfolder='ML/prediction')
subfolder='ML/prediction', batch_name=batch_name)
return
......@@ -11,7 +11,7 @@ import numpy as np
import slopestabilitytools
def plot_depth_true_estim(ml_results):
def plot_depth_true_estim(ml_results, *, batch_name=''):
fig, ax = plt.subplots()
......@@ -24,7 +24,7 @@ def plot_depth_true_estim(ml_results):
depth_estim = []
depth_true = []
for depth_estim_value in ml_results[classifier]['depth_estim']:
for depth_estim_value in ml_results[classifier]['prediction'][batch_name]['depth_estim']:
if isinstance(depth_estim_value, list):
......@@ -38,7 +38,7 @@ def plot_depth_true_estim(ml_results):
del value
for depth_true_value in ml_results[classifier]['depth_true']:
for depth_true_value in ml_results[classifier]['prediction'][batch_name]['depth_true']:
if isinstance(depth_true_value, list):
......@@ -80,4 +80,4 @@ def plot_depth_true_estim(ml_results):
ax.legend()
plt.title('Predicted vs True interface depth')
slopestabilitytools.save_plot(fig, 'All', '_true_vs_pred', subfolder='ML/')
slopestabilitytools.save_plot(fig, 'All', '_true_vs_pred', subfolder='ML/', batch_name=batch_name)
......@@ -5,45 +5,83 @@ Created on 19.01.2021
@author: Feliks Kiszkurno
"""
import joblib
import os
import settings
import slopestabilityML
import pandas as pd
import numpy as np
from scipy import interpolate
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
import slopestabilitytools
import test_definitions
def run_classification(test_training, test_prediction, test_results, clf, clf_name, *, hyperparameters=False):
def run_classification(test_training, test_prediction, test_results, clf, clf_name, *, hyperparameters=False, batch_name=''):
result_training = {}
clf_result_file_ext = clf_name + '_result.sav'
clf_result_file = os.path.join(settings.settings['clf_folder'], clf_result_file_ext)
if clf_name not in settings.settings['clf_trained']:
result_class_training, depth_estim_training, depth_true_training, depth_estim_accuracy_training, \
depth_estim_labels_training, accuracy_score_training, accuracy_labels_training = \
slopestabilityML.classification_train(test_training, test_results, clf, clf_name)
result_training = {'result_class': result_class_training,
'accuracy_score': accuracy_score_training,
'accuracy_labels': accuracy_labels_training,
'depth_estim': depth_estim_training,
'depth_true': depth_true_training,
'depth_estim_accuracy': depth_estim_accuracy_training,
'depth_estim_labels': depth_estim_labels_training}
settings.settings['clf_trained'].append(clf_name)
joblib.dump(result_training, clf_result_file)
else:
result_training = joblib.load(clf_result_file)
#if settings.settings['retrain_clf'] is False:
# settings.settings['clf_trained'] = True
# if settings.settings['clf_trained'] is True & settings.settings['clf_trained'] is True:
# clf_file_name = os.path.join(settings.settings['clf_folder'], clf_name, '.sav')
# clf = joblib.load(clf_file_name)
accuracy_result = []