Commit 5dc4f8e6 authored by Feliks Kiszkurno's avatar Feliks Kiszkurno
Browse files

Commit after syncing disaster

parent 7f63a68b
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 8 10:29:00 2021
@author: Feliks Kiszkurno
"""
import slopestabilitytools.datamanagement
import slopestabilitytools
import slopestabilityML
import slostabcreatedata
import numpy as np
import settings
settings.init()
# Config
create_new_data = False # set to True if you need to reassign the classes
create_new_data_only = False # set to False in order to run ML classifications
reassign_classes = True; class_type = 'norm'
# Load existing data instead of creating new one.
if not create_new_data:
test_results = slopestabilitytools.datamanagement.import_tests()
# if reassign_classes is True:
# 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()
# Create new data
else:
# Prepare folder structure for output
is_success = slopestabilitytools.folder_structure.create_folder_structure()
# TODO Put this part into a function
# Settings
number_of_tests = 5
rho_spread_factor = 1.5
rho_max = 20
layers_min = 1
layers_max = 2
min_depth = 4
max_depth = 8
# Generate parameters for tests
# tests_horizontal = slopestabilitytools.model_params(number_of_tests,
# rho_spread_factor, rho_max,
# layers_min, layers_max,
# min_depth, max_depth)
tests_horizontal = {'hor_01': {'layer_n': 1, 'rho_values': [[1, 5], [2, 15]], 'layers_pos': np.array([-5])},
'hor_02': {'layer_n': 1, 'rho_values': [[1, 5], [2, 50]], 'layers_pos': np.array([-5])},
'hor_03': {'layer_n': 1, 'rho_values': [[1, 15], [2, 20]], 'layers_pos': np.array([-8])},
'hor_04': {'layer_n': 1, 'rho_values': [[1, 5], [2, 10]], 'layers_pos': np.array([-3])},
'hor_05': {'layer_n': 1, 'rho_values': [[1, 5], [2, 25]], 'layers_pos': np.array([-3])},
'hor_06': {'layer_n': 1, 'rho_values': [[1, 2], [2, 10]], 'layers_pos': np.array([-4])},
'hor_07': {'layer_n': 1, 'rho_values': [[1, 10], [2, 20]], 'layers_pos': np.array([-6])},
'hor_08': {'layer_n': 1, 'rho_values': [[1, 5], [2, 25]], 'layers_pos': np.array([-3])},
'hor_09': {'layer_n': 1, 'rho_values': [[1, 3], [2, 25]], 'layers_pos': np.array([-3])},
'hor_10': {'layer_n': 1, 'rho_values': [[1, 5], [2, 25]], 'layers_pos': np.array([-7])},
'hor_11': {'layer_n': 1, 'rho_values': [[1, 10], [2, 12]], 'layers_pos': np.array([-4])},
'hor_12': {'layer_n': 1, 'rho_values': [[1, 15], [2, 50]], 'layers_pos': np.array([-5])},
'hor_13': {'layer_n': 2, 'rho_values': [[1, 3], [2, 5], [3, 15]],
'layers_pos': np.array([-3, -6])},
'hor_14': {'layer_n': 2, 'rho_values': [[1, 2], [2, 4], [3, 8]],
'layers_pos': np.array([-4, -8])},
'hor_15': {'layer_n': 1, 'rho_values': [[1, 4], [2, 15], [3, 25]],
'layers_pos': np.array([-4, -8])},
'hor_16': {'layer_n': 1, 'rho_values': [[1, 5], [2, 20], [3, 50]],
'layers_pos': np.array([-4, -8])}
}
# tests_horizontal = {'hor_11': {'layer_n': 1, 'rho_values': [[1, 10], [2, 12]], 'layers_pos': np.array([-4])}}
# Create models and invert them
test_results = {}
for test_name in tests_horizontal.keys():
test_result_curr, test_rho_max, test_rho_min = slostabcreatedata.create_data(test_name,
tests_horizontal[test_name],
max_depth)
test_results.update({test_name: test_result_curr})
del test_result_curr
# Plot and save figures
slopestabilitytools.plot_and_save(test_name, test_results[test_name], 'Test: ' + test_name, test_rho_max,
test_rho_min)
# Evaluate data with ML techniques
if not create_new_data_only:
print('Running ML stuff...')
ml_results = slopestabilityML.run_all_tests(test_results)
# Finish the script if ML classifiaction was not executed
elif create_new_data_only:
print('Done')
......@@ -25,7 +25,7 @@ def init():
settings['depth'] = True # True - include depth, False - ignore depth
# Clip data to max and min values from the input model
settings['clip'] = False # True - clip data, False - use unclipped data
settings['clip'] = True # True - clip data, False - use unclipped data
# Paths
settings['results_folder'] = 'results'
......
......@@ -9,7 +9,7 @@ Created on 19.01.2021
from sklearn import ensemble
import slopestabilityML.plot_results
import slopestabilityML.split_dataset
import slopestabilityML.run_classi
import slopestabilityML.run_classification
# TODO: as in svm_run
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 26.03.2021
Created on
@author: Feliks Kiszkurno
@author:
"""
# https://letsfigureout.com/2020/03/08/nearest-neighbor-algorithm-with-python-and-numpy/
from .knn_run import knn_run
\ No newline at end of file
......@@ -36,7 +36,7 @@ def max_grad_classi(test_result):
inds = np.zeros(gradient2.T.shape[0])
classes = np.ones_like(gradient2.T)
print(gradient2.T.shape[1])
for num, column in enumerate(gradient2.T):
# print(num)
inds[num] = np.argmax(column)
......
......@@ -18,6 +18,8 @@ def mgc_run(test_results, random_seed):
accuracy_score_prediction = []
accuracy_labels_prediction = []
accuracy_score_training = []
accuracy_labels_training = []
# Create classifier
clf = 0
......@@ -25,13 +27,29 @@ def mgc_run(test_results, random_seed):
# Train classifier
for test_name in test_prediction:
classes = slopestabilityML.MGC.max_grad_classi(test_results[test_name])
ids_temp = np.argwhere(classes >= 0.5)
classes[ids_temp] = 1
ids_temp = np.argwhere(classes < 0.5)
classes[ids_temp] = 0
classes_correct = test_results[test_name]['CLASS'].to_numpy()
score_prediction = len(np.argwhere(classes == classes_correct)) / len(classes_correct)
accuracy_score_prediction.append(score_prediction * 100)
accuracy_labels_prediction.append(test_name)
slopestabilityML.plot_class_res(test_results, test_name, classes_correct, classes, 'MGC')
slopestabilityML.plot_class_res(test_results, test_name, classes_correct, classes, 'MGC_prediction')
for test_name in test_training:
classes = slopestabilityML.MGC.max_grad_classi(test_results[test_name])
ids_temp = np.argwhere(classes >= 0.5)
classes[ids_temp] = 1
ids_temp = np.argwhere(classes < 0.5)
classes[ids_temp] = 0
classes_correct = test_results[test_name]['CLASS'].to_numpy()
score_prediction = len(np.argwhere(classes == classes_correct)) / len(classes_correct)
accuracy_score_training.append(score_prediction * 100)
accuracy_labels_training.append(test_name)
slopestabilityML.plot_class_res(test_results, test_name, classes_correct, classes, 'MGC_training')
# Plot
slopestabilityML.plot_results(accuracy_labels_prediction, accuracy_score_prediction, 'MGC_training')
return accuracy_score_prediction, accuracy_labels_prediction
return accuracy_score_prediction, accuracy_labels_prediction, accuracy_score_training, accuracy_labels_training
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 19.01.2021
@author: Feliks Kiszkurno
"""
from sklearn.linear_model import SGDClassifier
import slopestabilityML.plot_results
import slopestabilityML.split_dataset
import slopestabilityML.run_classi
def sgd_run(test_results, random_seed):
test_training, test_prediction = slopestabilityML.split_dataset(test_results.keys(), random_seed)
# Create classifier
clf = SGDClassifier(loss="hinge", penalty="l2", max_iter=5)
# Train classifier
accuracy_labels, accuracy_score, accuracy_labels_training, accuracy_score_training = \
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 accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 16.01.2021
@author: Feliks Kiszkurno
"""
from sklearn import svm
import slopestabilityML.plot_results
import slopestabilityML.split_dataset
import slopestabilityML.run_classi
# TODO: for comparability with other ML methods, add option to define which test should be used for training externaly
def svm_run(test_results, random_seed):
# https://stackabuse.com/implementing-svm-and-kernel-svm-with-pythons-scikit-learn/
test_training, test_prediction = slopestabilityML.split_dataset(test_results.keys(), random_seed)
accuracy_score = []
accuracy_labels = []
# Create classifier
clf = svm.SVC(gamma=0.001, C=100, kernel='linear')
# Train classifier
accuracy_labels, accuracy_score, accuracy_labels_training, accuracy_score_training = \
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 accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 16.01.2021
@author: Feliks Kiszkurno
"""
from .combine_results import combine_results
from .plot_results import plot_results
from .run_every import run_all_tests
from .split_dataset import split_dataset
from .run_classi import run_classification
from .preprocess_data import preprocess_data
from .plot_class_res import plot_class_res
from .SVM.svm_run import svm_run
from .GBC.gbc_run import gbc_run
from .SGD.sgd_run import sgd_run
from .KNN.knn_run import knn_run
from .ADABOOST.adaboost_run import adaboost_run
from .RVM.rvm_run import rvm_run
from .MGC.max_grad_classi import max_grad_classi
from .MGC.mgc_run import mgc_run
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 19.01.2021
@author: Feliks Kiszkurno
"""
import matplotlib.pyplot as plt
from pathlib import Path
import slopestabilitytools
def combine_results(ml_results):
# Predictions
fig = plt.figure()
ax = fig.subplots(1)
fig.suptitle('Accuracy of different ML methods: predictions')
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)
plt.xlabel('Test name')
plt.setp(ax.get_xticklabels(), rotation=45)
plt.ylabel('Correct points [%]')
plt.legend(loc='lower right')
slopestabilitytools.save_plot(fig, '', 'ML_summary_prediction', skip_fileformat=True)
# Training
fig = plt.figure()
ax = fig.subplots(1)
fig.suptitle('Accuracy of different ML methods - training')
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)
plt.xlabel('Test name')
plt.setp(ax.get_xticklabels(), rotation=90)
plt.ylabel('Correct points [%]')
plt.legend(loc='lower right')
fig.tight_layout()
slopestabilitytools.save_plot(fig, '', 'ML_summary_training', skip_fileformat=True)
......@@ -12,7 +12,7 @@ from matplotlib import ticker
import slopestabilitytools
def plot_class_res(test_results, test_name_pred, class_in, y_pred, clf_name):
def plot_class_res(test_results, test_name_pred, class_in, y_pred, clf_name, *, training=False):
# TODO: Move plotting to a function for plotting a, b and a-b
x = test_results[test_name_pred]['X']
y = test_results[test_name_pred]['Y']
......@@ -61,7 +61,13 @@ def plot_class_res(test_results, test_name_pred, class_in, y_pred, clf_name):
cb[2].update_ticks()
fig.tight_layout()
slopestabilitytools.save_plot(fig, clf_name, '{}_ML_{}_class_res'.format(test_name_pred, clf_name),
subfolder='ML/prediction')
if training is True:
slopestabilitytools.save_plot(fig, clf_name, '{}_ML_{}_class_res'.format(test_name_pred, clf_name),
subfolder='ML/training')
else:
slopestabilitytools.save_plot(fig, clf_name, '{}_ML_{}_class_res'.format(test_name_pred, clf_name),
subfolder='ML/prediction')
return
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 17.01.2021
@author: Feliks Kiszkurno
"""
import matplotlib.pyplot as plt
from pathlib import Path
import slopestabilitytools
def plot_results(accuracy_labels, accuracy_score, clf_name):
clf_name_title = clf_name.replace("_", " ")
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(accuracy_labels, accuracy_score, marker='x')
plt.setp(ax.get_xticklabels(), rotation=90)
plt.xlabel('Test name')
plt.ylabel('Correct points [%]')
plt.title(clf_name_title+' accuracy score')
print('plot script is executed')
fig.tight_layout()
slopestabilitytools.save_plot(fig, clf_name, '_accuracy', subfolder='ML/')
return
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 19.01.2021
@author: Feliks Kiszkurno
"""
import settings
import pandas as pd
def preprocess_data(data_set):
if settings.settings['norm'] is True:
x_train = data_set.drop(['X', 'Z', 'INM', 'INMN', 'RES', 'CLASS', 'CLASSN'], axis='columns')
else:
x_train = data_set.drop(['X', 'Z', 'INM', 'INMN', 'RESN', 'CLASS', 'CLASSN'], axis='columns')
if settings.settings['sen'] is False:
x_train = x_train.drop(['SEN'], axis='columns')
if settings.settings['norm_class'] is True:
y_train = pd.DataFrame(data_set['CLASSN'])
else:
y_train = pd.DataFrame(data_set['CLASS'])
if settings.settings['depth'] is False:
x_train = x_train.drop(['Y'], axis='columns')
return x_train, y_train
......@@ -25,34 +25,57 @@ def run_classification(test_training, test_prediction, test_results, clf, clf_na
accuracy_score_training = []
accuracy_labels_training = []
if settings.settings['norm'] is True and settings.settings['sen'] is True:
num_feat = ['RESN', 'SEN']
elif settings.settings['norm'] is False and settings.settings['sen'] is True:
num_feat = ['RES', 'SEN']
elif settings.settings['norm'] is False and settings.settings['sen'] is False:
num_feat = ['RES']
elif settings.settings['norm'] is True and settings.settings['sen'] is False:
num_feat = ['RESN']
num_feat = []
if settings.settings['norm'] is True:
num_feat.append('RESN')
else:
num_feat.append('RES')
if settings.settings['sen'] is True:
num_feat.append('SEN')
if settings.settings['depth'] is True:
num_feat.append('Y')
# if settings.settings['norm'] is True and settings.settings['sen'] is True:
# num_feat = ['RESN', 'SEN']
# elif settings.settings['norm'] is False and settings.settings['sen'] is True:
# num_feat = ['RES', 'SEN']
# elif settings.settings['norm'] is False and settings.settings['sen'] is False:
# num_feat = ['RES']
# elif settings.settings['norm'] is True and settings.settings['sen'] is False:
# num_feat = ['RESN']
#cat_feat = ['CLASS']
#cat_lab = [0, 1]
cat_lab = [0, 1]
if settings.settings['norm_class'] is True:
#cat_feat = ['CLASSN']
cat_lab = np.linspace(0, settings.settings['norm_class_num'] - 1, settings.settings['norm_class_num'])
elif settings.settings['norm_class'] is False:
#cat_feat = ['CLASS']
cat_lab = [0, 1]
num_trans = StandardScaler()
#cat_trans = OneHotEncoder(categories=[cat_lab])
preprocessor = ColumnTransformer(transformers=[('num', num_trans, num_feat),])
num_trans = StandardScaler()
preprocessor = ColumnTransformer(transformers=[('num', num_trans, num_feat)])#,
#('cat', cat_trans, cat_feat)])
clf_pipeline_UM = make_pipeline(preprocessor, clf)
clf_pipeline = make_pipeline(preprocessor, clf)
for test_name in test_training:
# Prepare data
print(test_name)
x_train, y_train = slopestabilityML.preprocess_data(test_results[test_name])
#x_train = test_results[test_name]
#y_train = test_results[test_name]
# Train classifier
clf_pipeline_UM.fit(x_train, y_train)
score_training = clf_pipeline_UM.score(x_train, y_train)
clf_pipeline.fit(x_train, y_train)
score_training = clf_pipeline.score(x_train, y_train)
accuracy_score_training.append(score_training * 100)
accuracy_labels_training.append(test_name)
......@@ -62,10 +85,10 @@ def run_classification(test_training, test_prediction, test_results, clf, clf_na
# Prepare data
x_question, y_answer = slopestabilityML.preprocess_data(test_results[test_name_pred])
# y_pred = clf_pipeline_UM.score(x_question, y_answer)
y_pred = clf_pipeline_UM.predict(x_question)
# y_pred = clf_pipeline.score(x_question, y_answer)
y_pred = clf_pipeline.predict(x_question)
# print(y_pred)
score = clf_pipeline_UM.score(x_question, y_answer)
score = clf_pipeline.score(x_question, y_answer)
print('score: '+str(score))
......
......@@ -57,9 +57,10 @@ def run_all_tests(test_results):
# 'score_training': rvm_accuracy_score_training, 'labels_training': rvm_accuracy_labels_training}
print('Running MGC')
mgc_accuracy_score, mgc_accuracy_labels = slopestabilityML.MGC.mgc_run(test_results, random_seed)
mgc_accuracy_score, mgc_accuracy_labels, mgc_accuracy_score_training, mgc_accuracy_labels_training \
= slopestabilityML.MGC.mgc_run(test_results, random_seed)
ml_results['MGC'] = {'score': mgc_accuracy_score, 'labels': mgc_accuracy_labels,
'score_training': [], 'labels_training': []}
'score_training': mgc_accuracy_score_training, 'labels_training': mgc_accuracy_labels_training}
slopestabilityML.combine_results(ml_results)
print('ML classification finished')
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 15.01.2021
@author: Feliks Kiszkurno
"""
""" This module is created to contain all tools, that are not directly related to inversion or processing
List:
model_params: generates parameters used to develop models
directory_structure: create directory structure to contain figures and other files
"""
from .model_params import model_params
from .plot_and_save import plot_and_save
from .set_labels import set_labels
from .set_diff import set_diff
from .normalize import normalize
from .assign_classes import assign_classes
from .def_classes import def_classes
from .save_plot import save_plot
from .reassign_classes import reassign_classes
from .grid_data import grid_data
from .mov_avg import mov_avg
from .folder_structure.create_folder_structure import create_folder_structure
from .folder_structure.create_folder_for_test import create_folder_for_test
from .folder_structure.check_create_folder import check_create_folder
from .datamanagement.import_tests import import_tests
from .datamanagement.read_to_pandas import read_to_pandas
from .datamanagement.test_list import test_list
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 17.01.2021
@author: Feliks Kiszkurno
"""
import slopestabilitytools.datamanagement.test_list
import settings
import pandas as pd
def import_tests():
test_results = {}
test_names = slopestabilitytools.datamanagement.test_list('.csv')
#print('test')
#print(test_names)
for test_name in test_names:
test_result_curr = pd.read_csv(settings.settings['data_folder'] + '/' + test_name + '.csv', index_col=0)
test_results.update({test_name: test_result_curr})
return test_results
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 17.01.2021
@author: Feliks Kiszkurno
"""
import os
import settings