Commit 745f4383 authored by Feliks Kiszkurno's avatar Feliks Kiszkurno
Browse files

Labels are now fully implemented. Kind of... There is still problem with committee.

parent d6532bae
......@@ -17,8 +17,8 @@ import test_definitions
settings.init()
test_definitions.init()
# Config
create_new_data = True # set to True if you need to reassign the classes
create_new_data_only = True # set to False in order to run ML classifications
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 = False; class_type = 'norm'
# Load existing data instead of creating new one.
......@@ -55,27 +55,8 @@ else:
# 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 = test_definitions.test_definitions
# tests_horizontal = {'hor_11': {'layer_n': 1, 'rho_values': [[1, 10], [2, 12]], 'layers_pos': np.array([-4])}}
......@@ -85,7 +66,7 @@ else:
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)
abs(tests_horizontal[test_name]['layers_pos'].max()))
test_results.update({test_name: test_result_curr})
del test_result_curr
......
......@@ -14,9 +14,9 @@ def init():
settings = {}
# Normalization and classes
settings['norm_class'] = True # True to use normalized classes, False to use class_ids
settings['norm_class'] = False # True to use normalized classes, False to use class_ids
settings['norm_class_num'] = 5 # Number of classes for normalized data
settings['norm'] = True # True to use normalized data, False to use raw data
settings['norm'] = False # True to use normalized data, False to use raw data
settings['use_labels'] = True # True to use labels instead of classes
# Include sensitivity
......
......@@ -9,6 +9,7 @@ Created on 04.04.2021
import numpy as np
import settings
import slopestabilitytools
import slopestabilityML
......@@ -21,6 +22,8 @@ def ask_committee(ml_result_class, test_results, *, random_seed=False):
class_in = test_results[test_name]['CLASSN']
elif settings.settings['norm_class'] is False:
class_in = test_results[test_name]['CLASS']
elif settings.settings['use_labels'] is True:
class_in = slopestabilitytools.label2numeric(class_in)
else:
print('I don\'t know which class to use! Exiting...')
exit(0)
......@@ -33,19 +36,18 @@ def ask_committee(ml_result_class, test_results, *, random_seed=False):
for test_name in sorted(ml_result_class[method_name].keys()):
test_names_all_temp.append(test_name)
test_names_all = set(test_names_all_temp)
#if sorted(test_names_all) == sorted(test_names_all_temp):
# test_names_all = sorted(test_names_all_temp)
#else:
# print("Mismatch in datasets!")
# exit(0)
# 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()):
results[:, method_id] = ml_result_class[method_name][test_name].T.reshape([len(ml_result_class[method_name][test_name])])
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])])
method_id = method_id + 1
results_test[test_name] = results
......@@ -55,7 +57,6 @@ def ask_committee(ml_result_class, test_results, *, random_seed=False):
test_result = results_test[test_name]
shape_test = test_result.shape
result_rows = shape_test[0]
result_cols = shape_test[1]
result_voted = np.zeros([result_rows, 1])
for row_id in range(result_rows):
row_temp = test_result[row_id, :]
......@@ -78,9 +79,9 @@ def ask_committee(ml_result_class, test_results, *, random_seed=False):
for test_group in sorted(tests_ordered.keys()):
for test_name in tests_ordered[test_group]:
classes_correct = class_in.to_numpy()
classes_correct = classes_correct.reshape([len(results_voting[test_name]), 1])
score = len(np.argwhere(results_voting[test_name] == classes_correct)) / len(classes_correct)
classes_correct_temp = classes_correct[test_name].to_numpy()
classes_correct_temp = classes_correct_temp.reshape([len(results_voting[test_name]), 1])
score = len(np.argwhere(results_voting[test_name] == classes_correct_temp)) / len(classes_correct_temp)
if test_group is 'train':
accuracy_score_training.append(score * 100)
accuracy_labels_training.append(test_name)
......
......@@ -12,6 +12,7 @@ from matplotlib import ticker
import slopestabilitytools
import test_definitions
import settings
def plot_class_overview(test_results, test_name, class_in, y_pred, clf_name, *, training=False):
......@@ -22,9 +23,6 @@ def plot_class_overview(test_results, test_name, class_in, y_pred, clf_name, *,
class_in = class_in.to_numpy()
class_in = class_in.reshape(class_in.size)
class_diff = np.zeros_like(y_pred)
class_diff[np.where(class_in == y_pred)] = 1
# Create plot
fig, _ax = plt.subplots(nrows=4, ncols=2, figsize=(1.35*10, 10))
ax = _ax.flatten()
......@@ -33,6 +31,10 @@ def plot_class_overview(test_results, test_name, class_in, y_pred, clf_name, *,
fig.suptitle('Classification overview: ' + test_name + ', ' + clf_name)
fig.subplots_adjust(hspace=0.8)
# Convert labels to numerical for plotting
if settings.settings['use_labels'] is True:
class_in = slopestabilitytools.label2numeric(class_in)
# Plot input classes
im0 = ax[0].scatter(x, y, c=class_in)
for depth in test_definitions.test_definitions[test_name]['layers_pos']:
......@@ -44,6 +46,10 @@ def plot_class_overview(test_results, test_name, class_in, y_pred, clf_name, *,
cb[0].locator = tick_locator
cb[0].update_ticks()
# Convert labels to numerical for plotting
if settings.settings['use_labels'] is True:
y_pred = slopestabilitytools.label2numeric(y_pred)
# Plot prediction
im1 = ax[1].scatter(x, y, c=y_pred)
for depth in test_definitions.test_definitions[test_name]['layers_pos']:
......@@ -66,6 +72,9 @@ def plot_class_overview(test_results, test_name, class_in, y_pred, clf_name, *,
cb[2].locator = tick_locator
cb[2].update_ticks()
class_diff = np.zeros_like(y_pred)
class_diff[np.where(class_in == y_pred)] = 1
# Plot difference between correct and predicted classes
im3 = ax[3].scatter(x, y, c=class_diff)
for depth in test_definitions.test_definitions[test_name]['layers_pos']:
......
......@@ -22,6 +22,8 @@ def preprocess_data(data_set):
if settings.settings['norm_class'] is True:
y_train = pd.DataFrame(data_set['CLASSN'])
elif settings.settings['use_labels'] is True:
y_train = pd.DataFrame(data_set['LABELS'])
else:
y_train = pd.DataFrame(data_set['CLASS'])
......
......@@ -61,8 +61,9 @@ def run_classification(test_training, test_prediction, test_results, clf, clf_na
num_trans = StandardScaler()
if settings.settings['use_labels'] is True:
cat_feat = ['LABEL']
if settings.settings['use_labels'] is True:
cat_feat = ['LABELS']
cat_lab = ['Very Low', 'Low', 'Medium', 'High', 'Very High']
cat_trans = OneHotEncoder(categories=[cat_lab])
preprocessor = ColumnTransformer(transformers=[('num', num_trans, num_feat),
('cat', cat_trans, cat_feat)])
......@@ -107,12 +108,14 @@ def run_classification(test_training, test_prediction, test_results, clf, clf_na
print('MATCH!')
else:
print('MISMATCH!')
print('score: '+str(score))
print('score: {score:.2f} %'.format(score=score*100))
if settings.settings['norm_class'] is True:
class_in = test_results[test_name_pred]['CLASSN']
elif settings.settings['norm_class'] is False:
class_in = test_results[test_name_pred]['CLASS']
elif settings.settings['use_labels'] is True:
class_in = test_results[test_name]['LABELS']
else:
print('I don\'t know which class to use! Exiting...')
exit(0)
......
......@@ -23,6 +23,7 @@ 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 .classes_labels import numeric2label, label2numeric
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 12.04.2021
@author: Feliks Kiszkurno
"""
def numeric2label(input_numeric):
labels_translator = {0: 'Very Low',
1: 'Low',
2: 'Medium',
3: 'High',
4: 'Very High'}
labels = [None] * len(input_numeric)
find_all = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]
for key in labels_translator.keys():
id_new = find_all(key, input_numeric)
for idx in id_new:
labels[idx] = labels_translator[key]
return labels
def label2numeric(input_labels):
labels_translator = {'Very Low': 0,
'Low': 1,
'Medium': 2,
'High': 3,
'Very High': 4}
numeric = [None] * len(input_labels)
find_all = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]
for key in labels_translator.keys():
id_new = find_all(key, input_labels)
for idx in id_new:
numeric[idx] = labels_translator[key]
return numeric
\ No newline at end of file
......@@ -68,7 +68,7 @@ def create_data(test_name, test_config, max_depth):
# SIMULATE ERT MEASUREMENT - START ###
data = ert.simulate(mesh, scheme=measurement_scheme, res=resistivity_map, noiseLevel=1, noiseAbs=1e-6, seed=1337)
data.remove(data['rhoa'] < 0)
data.remove(data['rhoa'] <= 0)
# SIMULATE ERT MEASUREMENT - END ###
ert_manager = ert.ERTManager(sr=False, useBert=True, verbose=True, debug=False)
......@@ -164,6 +164,7 @@ def create_data(test_name, test_config, max_depth):
'CLASS': classes,
'CLASSN': classesn,
'LABELS': labels})
experiment_results = experiment_results[(experiment_results.INMN > 0) & (experiment_results.RESN > 0)]
experiment_results.to_csv(settings.settings['data_folder'] + '/' + test_name + '.csv')
......
......@@ -10,27 +10,67 @@ import numpy as np
def init():
global test_definitions
test_definitions = {'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': 2, 'rho_values': [[1, 4], [2, 15], [3, 25]],
'layers_pos': np.array([-4, -8])},
'hor_16': {'layer_n': 2, 'rho_values': [[1, 5], [2, 20], [3, 50]],
'layers_pos': np.array([-4, -8])}
}
\ No newline at end of file
test_definitions = {'hor1_01': {'layer_n': 1, 'rho_values': [[1, 5], [2, 15]], 'layers_pos': np.array([-5])},
'hor1_02': {'layer_n': 1, 'rho_values': [[1, 5], [2, 50]], 'layers_pos': np.array([-5])},
'hor1_03': {'layer_n': 1, 'rho_values': [[1, 15], [2, 20]], 'layers_pos': np.array([-8])},
'hor1_04': {'layer_n': 1, 'rho_values': [[1, 5], [2, 10]], 'layers_pos': np.array([-3])},
'hor1_05': {'layer_n': 1, 'rho_values': [[1, 5], [2, 25]], 'layers_pos': np.array([-3])},
'hor1_06': {'layer_n': 1, 'rho_values': [[1, 2], [2, 10]], 'layers_pos': np.array([-4])},
'hor1_07': {'layer_n': 1, 'rho_values': [[1, 10], [2, 20]], 'layers_pos': np.array([-6])},
'hor1_08': {'layer_n': 1, 'rho_values': [[1, 5], [2, 25]], 'layers_pos': np.array([-3])},
'hor1_09': {'layer_n': 1, 'rho_values': [[1, 3], [2, 25]], 'layers_pos': np.array([-3])},
'hor1_10': {'layer_n': 1, 'rho_values': [[1, 5], [2, 25]], 'layers_pos': np.array([-7])},
'hor1_11': {'layer_n': 1, 'rho_values': [[1, 10], [2, 12]], 'layers_pos': np.array([-4])},
'hor1_12': {'layer_n': 1, 'rho_values': [[1, 15], [2, 50]], 'layers_pos': np.array([-5])},
'hor1_14': {'layer_n': 1, 'rho_values': [[1, 5], [2, 75]], 'layers_pos': np.array([-5])},
'hor1_15': {'layer_n': 1, 'rho_values': [[1, 15], [2, 50]], 'layers_pos': np.array([-8])},
'hor1_16': {'layer_n': 1, 'rho_values': [[1, 25], [2, 50]], 'layers_pos': np.array([-5])},
'hor1_17': {'layer_n': 1, 'rho_values': [[1, 25], [2, 75]], 'layers_pos': np.array([-5])},
'hor1_18': {'layer_n': 1, 'rho_values': [[1, 5], [2, 75]], 'layers_pos': np.array([-8])},
'hor1_19': {'layer_n': 1, 'rho_values': [[1, 50], [2, 60]], 'layers_pos': np.array([-7])},
'hor1_20': {'layer_n': 1, 'rho_values': [[1, 2], [2, 10]], 'layers_pos': np.array([-15])},
# Tests with two layers
'hor2_01': {'layer_n': 2, 'rho_values': [[1, 3], [2, 5], [3, 15]],
'layers_pos': np.array([-3, -6])},
'hor2_02': {'layer_n': 2, 'rho_values': [[1, 2], [2, 4], [3, 8]],
'layers_pos': np.array([-4, -8])},
'hor2_03': {'layer_n': 2, 'rho_values': [[1, 4], [2, 15], [3, 25]],
'layers_pos': np.array([-4, -8])},
'hor2_04': {'layer_n': 2, 'rho_values': [[1, 5], [2, 20], [3, 50]],
'layers_pos': np.array([-4, -8])},
'hor2_05': {'layer_n': 2, 'rho_values': [[1, 5], [2, 35], [3, 50]],
'layers_pos': np.array([-4, -8])},
'hor2_06': {'layer_n': 2, 'rho_values': [[1, 5], [2, 35], [3, 50]],
'layers_pos': np.array([-4, -8])},
'hor2_07': {'layer_n': 2, 'rho_values': [[1, 5], [2, 10], [3, 70]],
'layers_pos': np.array([-4, -8])},
'hor2_08': {'layer_n': 2, 'rho_values': [[1, 5], [2, 10], [3, 70]],
'layers_pos': np.array([-3, -9])},
'hor2_09': {'layer_n': 2, 'rho_values': [[1, 3], [2, 40], [3, 70]],
'layers_pos': np.array([-4, -8])},
'hor2_10': {'layer_n': 2, 'rho_values': [[1, 3], [2, 40], [3, 70]],
'layers_pos': np.array([-4, -8])},
'hor2_11': {'layer_n': 2, 'rho_values': [[1, 5], [2, 10], [3, 70]],
'layers_pos': np.array([-3, -15])},
'hor2_12': {'layer_n': 2, 'rho_values': [[1, 5], [2, 10], [3, 70]],
'layers_pos': np.array([-4, -6])},
'hor2_13': {'layer_n': 2, 'rho_values': [[1, 5], [2, 35], [3, 50]],
'layers_pos': np.array([-5, -20])},
'hor2_14': {'layer_n': 2, 'rho_values': [[1, 5], [2, 35], [3, 50]],
'layers_pos': np.array([-3, -5])},
'hor2_15': {'layer_n': 2, 'rho_values': [[1, 5], [2, 10], [3, 70]],
'layers_pos': np.array([-5, -15])},
'hor2_16': {'layer_n': 2, 'rho_values': [[1, 5], [2, 10], [3, 70]],
'layers_pos': np.array([-3, -15])},
'hor2_17': {'layer_n': 2, 'rho_values': [[1, 2], [2, 4], [3, 8]],
'layers_pos': np.array([-4, -15])},
'hor2_18': {'layer_n': 2, 'rho_values': [[1, 5], [2, 10], [3, 15]],
'layers_pos': np.array([-3, -15])},
'hor2_19': {'layer_n': 2, 'rho_values': [[1, 15], [2, 20], [3, 30]],
'layers_pos': np.array([-3, -15])},
'hor2_20': {'layer_n': 2, 'rho_values': [[1, 5], [2, 10], [3, 70]],
'layers_pos': np.array([-3, -10])},
}
Markdown is supported
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