Commit 7f63a68b authored by Feliks Kiszkurno's avatar Feliks Kiszkurno
Browse files

Working MGC.

Added functions for plots, gridding of the data and moving average.
Bug fixes and stuff.
parent aead35f8
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" serverName="mw516732@login18-2.hpc.itc.rwth-aachen.de:22" remoteFilesAllowedToDisappearOnAutoupload="false">
<serverData>
<paths name="mw516732@login18-2.hpc.itc.rwth-aachen.de:22">
<serverdata>
<mappings>
<mapping deploy="/home/mw516732/Dokumente/SlopeStability" local="$PROJECT_DIR$" />
</mappings>
</serverdata>
</paths>
</serverData>
</component>
</project>
\ No newline at end of file
......@@ -17,7 +17,7 @@ settings.init()
# Config
create_new_data = False # set to True if you need to reassign the classes
create_new_data_only = False
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.
......@@ -25,8 +25,8 @@ 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)
# 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()
......
......@@ -19,13 +19,13 @@ def init():
settings['norm'] = True # True to use normalized data, False to use raw data
# Include sensitivity
settings['sen'] = False # True - include sensitivity, False - ignore sensitivity
settings['sen'] = True # True - include sensitivity, False - ignore sensitivity
# Include depth
settings['depth'] = True # True - include depth, False - ignore depth
# Clip data to max and min values from the input model
settings['clip'] = True # True - clip data, False - use unclipped data
settings['clip'] = False # True - clip data, False - use unclipped data
# Paths
settings['results_folder'] = 'results'
......@@ -33,4 +33,4 @@ def init():
settings['figures_folder'] = settings['results_folder'] + '/figures/'
# Plots
settings['plot_formats'] = ['png', 'pdf'] # list of formats to save plots as, supported formats: eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 01.04.2021
@author: Feliks Kiszkurno
"""
from .max_grad_classi import max_grad_classi
from .mgc_run import mgc_run
\ No newline at end of file
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 31.03.2021
@author: Feliks Kiszkurno
"""
import numpy as np
from scipy import interpolate
import math
import slopestabilitytools
import settings
def max_grad_classi(test_result):
x = test_result['X']
y = test_result['Y']
if settings.settings['norm'] is True:
result = test_result['RESN']
elif settings.settings['norm'] is False:
result = test_result['RES']
data = {'result': result}
xi, yi, data_gridded = slopestabilitytools.grid_data(x, y, data, regular_grid=True)
result_grid = data_gridded['result']
gradient = np.gradient(result_grid, axis=0)
gradient2 = np.gradient(gradient, axis=0)
gradient2 = gradient2 / np.amax(gradient2)
ind = gradient2 < 0.15
gradient2[ind] = 0
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)
inds = slopestabilitytools.mov_avg(inds, math.ceil(len(inds) / 10))
for num, column in enumerate(classes):
column[:int(inds[num])] = 0
classes = classes.T
classes_interp_f = interpolate.interp2d(xi, yi, classes)
classes_interp = np.zeros_like(x)
for cell_id in range(len(x)):
classes_interp[cell_id] = classes_interp_f(x[cell_id], y[cell_id])
return classes_interp
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 31.03.2021
@author: Feliks Kiszkurno
"""
# THIS IS NOT A PROPER ML METHOD
import slopestabilityML
import numpy as np
import pandas as pd
def mgc_run(test_results, random_seed):
test_training, test_prediction = slopestabilityML.split_dataset(test_results.keys(), random_seed)
accuracy_score_prediction = []
accuracy_labels_prediction = []
# Create classifier
clf = 0
# Train classifier
for test_name in test_prediction:
classes = slopestabilityML.MGC.max_grad_classi(test_results[test_name])
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')
# Plot
slopestabilityML.plot_results(accuracy_labels_prediction, accuracy_score_prediction, 'MGC_training')
return accuracy_score_prediction, accuracy_labels_prediction
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 31.03.2021
@author: Feliks Kiszkurno
"""
from .rvm_run import rvm_run
\ No newline at end of file
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 31.03.2021
@author: Feliks Kiszkurno
"""
from sklearn_rvm import EMRVC
import slopestabilityML.plot_results
import slopestabilityML.split_dataset
import slopestabilityML.run_classi
def rvm_run(test_results, random_seed):
test_training, test_prediction = slopestabilityML.split_dataset(test_results.keys(), random_seed)
accuracy_score = []
accuracy_labels = []
# Create classifier
clf = EMRVC(kernel="rbf")
# Train classifier
accuracy_labels, accuracy_score, accuracy_labels_training, accuracy_score_training = \
slopestabilityML.run_classification(test_training, test_prediction, test_results, clf, 'RVM')
# Plot
slopestabilityML.plot_results(accuracy_labels, accuracy_score, 'RVM_prediction')
slopestabilityML.plot_results(accuracy_labels_training, accuracy_score_training, 'RVM_training')
return accuracy_score, accuracy_labels, accuracy_score_training, accuracy_labels_training
\ No newline at end of file
......@@ -7,11 +7,11 @@ Created on 16.01.2021
"""
from sklearn import svm
import numpy as np
import slopestabilityML.plot_results
import slopestabilityML.split_dataset
import slopestabilityML.run_classi
import random
# TODO: for comparability with other ML methods, add option to define which test should be used for training externaly
......
......@@ -12,10 +12,14 @@ 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
......@@ -9,10 +9,11 @@ Created on 19.01.2021
import matplotlib.pyplot as plt
from pathlib import Path
import slopestabilitytools
def combine_results(ml_results):
# TODO avoid reusing the same code twice
# Predictions
fig = plt.figure()
ax = fig.subplots(1)
......@@ -27,9 +28,7 @@ def combine_results(ml_results):
plt.ylabel('Correct points [%]')
plt.legend(loc='lower right')
# fig.savefig(Path('results/figures/ML_summary_prediction.eps'))
fig.savefig(Path('results/figures/ML_summary_prediction.png'))
# fig.savefig(Path('results/figures/ML_summary_prediction.pdf'))
slopestabilitytools.save_plot(fig, '', 'ML_summary_prediction', skip_fileformat=True)
# Training
fig = plt.figure()
......@@ -46,6 +45,4 @@ def combine_results(ml_results):
plt.legend(loc='lower right')
fig.tight_layout()
# fig.savefig(Path('results/figures/ML_summary_training.eps'), bbox_inches="tight")
fig.savefig(Path('results/figures/ML_summary_training.png'), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML_summary_training.pdf'), bbox_inches="tight")
slopestabilitytools.save_plot(fig, '', 'ML_summary_training', skip_fileformat=True)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 31.03.2021
@author: Feliks Kiszkurno
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker
import slopestabilitytools
def plot_class_res(test_results, test_name_pred, class_in, y_pred, clf_name):
# 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']
class_out = y_pred
data = {'class_in': class_in, 'class_out': class_out}
xi, yi, data_gridded = slopestabilitytools.grid_data(x, y, data)
class_in_i = data_gridded['class_in']
class_out_i = data_gridded['class_out']
class_diff = np.zeros_like(class_out_i)
class_diff[np.where(class_in_i == class_out_i)] = 1
cb = []
fig, _ax = plt.subplots(nrows=3, ncols=1)
ax = _ax.flatten()
fig.suptitle(test_name_pred + ' ' + clf_name)
fig.subplots_adjust(hspace=0.8)
im0 = ax[0].contourf(xi, yi, class_in_i)
ax[0].set_title('Input classes')
ax[0] = slopestabilitytools.set_labels(ax[0])
cb.append(plt.colorbar(im0, ax=ax[0], label='Class')) # , shrink=0.9)
tick_locator = ticker.MaxNLocator(nbins=4)
cb[0].locator = tick_locator
cb[0].update_ticks()
im1 = ax[1].contourf(xi, yi, class_out_i)
ax[1].set_title('Result of classification')
ax[1] = slopestabilitytools.set_labels(ax[1])
cb.append(plt.colorbar(im1, ax=ax[1], label='Class')) # , shrink=0.9)
tick_locator = ticker.MaxNLocator(nbins=4)
cb[1].locator = tick_locator
cb[1].update_ticks()
im2 = ax[2].contourf(xi, yi, class_diff)
ax[2].set_title('Difference')
ax[2] = slopestabilitytools.set_labels(ax[2])
cb.append(plt.colorbar(im2, ax=ax[2], label='Is class correct?')) # , shrink=0.9)
tick_locator = ticker.MaxNLocator(nbins=4)
cb[2].locator = tick_locator
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')
return
......@@ -25,9 +25,6 @@ def plot_results(accuracy_labels, accuracy_score, clf_name):
plt.title(clf_name_title+' accuracy score')
print('plot script is executed')
fig.tight_layout()
slopestabilitytools.save_plot(fig, clf_name, '_accuracy', subfolder='ML')
# fig.savefig(Path('results/figures/ML/'+clf_name+'.eps'), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML/'+clf_name+'.pdf'), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML/'+clf_name+'.png'), bbox_inches="tight")
slopestabilitytools.save_plot(fig, clf_name, '_accuracy', subfolder='ML/')
return
......@@ -9,24 +9,16 @@ Created on 19.01.2021
import settings
import slopestabilityML
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt
from matplotlib import ticker
from scipy import interpolate
import slopestabilitytools
from pathlib import Path
def run_classification(test_training, test_prediction, test_results, clf, clf_name):
# global settings
accuracy_score = []
accuracy_labels = []
......@@ -57,10 +49,8 @@ def run_classification(test_training, test_prediction, test_results, clf, clf_na
# Prepare data
print(test_name)
x_train, y_train = slopestabilityML.preprocess_data(test_results[test_name])
# print('stop')
# Train classifier
# print(type(x_train))
# print(type(y_train))
clf_pipeline_UM.fit(x_train, y_train)
score_training = clf_pipeline_UM.score(x_train, y_train)
......@@ -78,64 +68,16 @@ def run_classification(test_training, test_prediction, test_results, clf, clf_na
score = clf_pipeline_UM.score(x_question, y_answer)
print('score: '+str(score))
# 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']
class_in = test_results[test_name]['CLASS']
class_out = y_pred
x_min = np.min(x)
x_max = np.max(x)
x_n = len(x)
y_min = np.min(y)
y_max = np.max(y)
y_start = y_max
y_end = y_min
y_n = len(y)
xi = np.linspace(x_min, x_max, x_n)
yi = np.linspace(y_start, y_end, y_n)
xx, yy = np.meshgrid(xi, yi)
class_in_i = interpolate.griddata((x, y), class_in, (xx, yy), method='nearest')
class_out_i = interpolate.griddata((x, y), class_out, (xx, yy), method='nearest')
class_diff = np.zeros_like(class_out_i)
class_diff[np.where(class_in_i == class_out_i)] = 1
cb = []
fig, _ax = plt.subplots(nrows=3, ncols=1)
ax = _ax.flatten()
fig.suptitle(test_name_pred+' '+clf_name)
fig.subplots_adjust(hspace=0.8)
im0 = ax[0].contourf(xi, yi, class_in_i)
ax[0].set_title('Input classes')
ax[0] = slopestabilitytools.set_labels(ax[0])
cb.append(plt.colorbar(im0, ax=ax[0], label='Class')) # , shrink=0.9)
tick_locator = ticker.MaxNLocator(nbins=4)
cb[0].locator = tick_locator
cb[0].update_ticks()
im1 = ax[1].contourf(xi, yi, class_out_i)
ax[1].set_title('Result of classification')
ax[1] = slopestabilitytools.set_labels(ax[1])
cb.append(plt.colorbar(im1, ax=ax[1], label='Class')) # , shrink=0.9)
tick_locator = ticker.MaxNLocator(nbins=4)
cb[1].locator = tick_locator
cb[1].update_ticks()
im2 = ax[2].contourf(xi, yi, class_diff)
ax[2].set_title('Difference')
ax[2] = slopestabilitytools.set_labels(ax[2])
cb.append(plt.colorbar(im2, ax=ax[2], label='Is class correct?')) # , shrink=0.9)
tick_locator = ticker.MaxNLocator(nbins=4)
cb[2].locator = tick_locator
cb[2].update_ticks()
fig.tight_layout()
# fig.savefig(Path('results/figures/ML/prediction/eps/{}_ML_{}_class_res.eps'.format(test_name_pred, clf_name)), bbox_inches="tight")
fig.savefig(Path('results/figures/ML/prediction/png/{}_ML_{}_class_res.png'.format(test_name_pred, clf_name)), bbox_inches="tight")
# fig.savefig(Path('results/figures/ML/prediction/pdf/{}_ML_{}_class_res.pdf'.format(test_name_pred, clf_name)), bbox_inches="tight")
if settings.settings['norm_class'] is True:
class_in = test_results[test_name]['CLASSN']
elif settings.settings['norm_class'] is False:
class_in = test_results[test_name]['CLASS']
else:
print('I don\'t know which class to use! Exiting...')
exit(0)
slopestabilityML.plot_class_res(test_results, test_name_pred, class_in, y_pred, clf_name)
# Evaluate result
#accuracy_score.append(len(np.where(y_pred == y_answer.to_numpy())) / len(y_answer.to_numpy()) * 100)
......
......@@ -50,6 +50,17 @@ def run_all_tests(test_results):
ml_results['ADA'] = {'score': ada_accuracy_score, 'labels': ada_accuracy_labels,
'score_training': ada_accuracy_score_training, 'labels_training': ada_accuracy_labels_training}
# print('Running RVM...')
# rvm_accuracy_score, rvm_accuracy_labels, rvm_accuracy_score_training, rvm_accuracy_labels_training = \
# slopestabilityML.RVM.rvm_run(test_results, random_seed)
# ml_results['RVM'] = {'score': rvm_accuracy_score, 'labels': rvm_accuracy_labels,
# '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)
ml_results['MGC'] = {'score': mgc_accuracy_score, 'labels': mgc_accuracy_labels,
'score_training': [], 'labels_training': []}
slopestabilityML.combine_results(ml_results)
print('ML classification finished')
......
......@@ -21,6 +21,8 @@ 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
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 31.03.2021
@author: Feliks Kiszkurno
"""
import numpy as np
from scipy import interpolate
import math
def grid_data(x, y, data, *, regular_grid=False): # data has to be a dictionary, output in dictionary in the same order as input
x_min = math.ceil(np.min(x))
x_max = math.floor(np.max(x))
y_min = math.ceil(np.min(y))
y_max = math.floor(np.max(y))
if regular_grid is False:
x_n = len(x)
y_n = len(y)
elif regular_grid is True:
x_n = ((x_max - x_min) + 1) * 20
y_n = ((y_max - y_min) + 1) * 20
y_start = y_max
y_end = y_min
xi = np.linspace(x_min, x_max, x_n)
yi = np.linspace(y_start, y_end, y_n)
xx, yy = np.meshgrid(xi, yi)
data_out = {}
for key in data.keys():
result_temp = interpolate.griddata((x, y), data[key], (xx, yy), method='nearest')
data_out[key] = result_temp
return xi, yi, data_out
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 31.03.2021
@author: Feliks Kiszkurno
"""
import numpy as np
def mov_avg(array, window_length, *, method='same'):
array_avg = np.convolve(array, np.ones(window_length), method) / window_length
return array_avg
......@@ -6,41 +6,38 @@ Created on 15.01.2021
@author: Feliks Kiszkurno
"""
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import ticker
from scipy import interpolate
import numpy as np
import slopestabilitytools
from pathlib import Path
import settings
def plot_and_save(test_name, test_result, plot_title, rho_max, rho_min):
x = test_result['X']
y = test_result['Y']
inm = test_result['INM']
res = test_result['RES']
cov = test_result['SEN']
x_min = np.min(x)
x_max = np.max(x)
x_n = len(x)
if settings.settings['norm'] is True:
plot_title = plot_title + '_norm'
data = {'INM': test_result['INMN'],
'RES': test_result['RESN'],
'COV': test_result['SEN']}
elif settings.settings['norm'] is False:
data = {'INM': test_result['INM'],
'RES': test_result['RES'],
'COV': test_result['SEN']}
y_min = np.min(y)
y_max = np.max(y)
y_start = y_max
y_end = y_min
y_n = len(y)
else:
print('I don\'t know which kind of data to use! Exiting...')
exit(0)
xi = np.linspace(x_min, x_max, x_n)
yi = np.linspace(y_start, y_end, y_n)
xx, yy = np.meshgrid(xi, yi)
inm_i = interpolate.griddata((x, y), inm, (xx, yy), method='nearest')
res_i = interpolate.griddata((x, y), res, (xx, yy), method='nearest')
cov_i = interpolate.griddata((x, y), cov, (xx, yy), method='nearest')
xi, yi, data_gridded = slopestabilitytools.grid_data(x, y, data)
print('plot_and_save')