Skip to content
Snippets Groups Projects
Commit 36a09681 authored by Laura Christine Kühle's avatar Laura Christine Kühle
Browse files

Added option to select colors for classification plots.

parent 36f233b4
Branches
No related tags found
No related merge requests found
......@@ -156,11 +156,12 @@ def read_training_data(directory):
return TensorDataset(*map(torch.tensor, (np.load(input_file), np.load(output_file))))
def evaluate_models(models, directory, num_iterations=100, measures=None):
if measures is None:
measures = ['Accuracy', 'Precision', 'Recall', 'F-Score', 'AUROC']
def evaluate_models(models, directory, num_iterations=100, colors = None):
if colors is None:
colors = {'Accuracy': 'red', 'Precision': 'yellow', 'Recall': 'blue',
'F-Score': 'green', 'AUROC': 'purple'}
dataset = read_training_data(directory)
classification_stats = {measure: {model: [] for model in models} for measure in measures}
classification_stats = {measure: {model: [] for model in models} for measure in colors}
for iteration in range(num_iterations):
for train_index, test_index in KFold(n_splits=5, shuffle=True).split(dataset):
# print("TRAIN:", train_index, "TEST:", test_index)
......@@ -169,13 +170,13 @@ def evaluate_models(models, directory, num_iterations=100, measures=None):
for model in models:
result = models[model].test_model(training_set, test_set)
for measure in measures:
for measure in colors:
classification_stats[measure][model].append(result[measure])
plot_boxplot(models.keys(), classification_stats)
plot_boxplot(classification_stats, colors)
classification_stats = {measure: {model: np.array(classification_stats[measure][model]).mean()
for model in models} for measure in measures}
plot_classification_accuracy(models.keys(), classification_stats)
for model in models} for measure in colors}
plot_classification_accuracy(classification_stats, colors)
# Set paths for plot files if not existing already
plot_dir = directory + '/model evaluation'
......
......@@ -4,6 +4,7 @@
TODO: Give option to select plotting color
TODO: Improve classification plotting -> Done
TODO: Give option to select classification color -> Done
TODO: Add documentation to plot_boxplot()
"""
......@@ -190,7 +191,7 @@ def calculate_approximate_solution(projection, points, polynomial_degree, basis)
def calculate_exact_solution(mesh, cell_len, wave_speed, final_time, interval_len, quadrature,
init_cond):
""""Calculates exact solution.
"""Calculates exact solution.
Parameters
----------
......@@ -237,7 +238,7 @@ def calculate_exact_solution(mesh, cell_len, wave_speed, final_time, interval_le
return grid, exact
def plot_classification_accuracy(model_names, evaluation_dict):
def plot_classification_accuracy(evaluation_dict, colors):
"""Plots classification accuracy.
Plots the accuracy, precision, and recall in a bar plot.
......@@ -254,6 +255,7 @@ def plot_classification_accuracy(model_names, evaluation_dict):
List of strings for x-axis labels.
"""
model_names = evaluation_dict[list(colors.keys())[0]].keys()
pos = np.arange(len(model_names))
width = 1/(3*len(model_names))
fig = plt.figure('classification_accuracy')
......@@ -262,7 +264,7 @@ def plot_classification_accuracy(model_names, evaluation_dict):
adjustment = -(len(model_names)//2)*step_len
for measure in evaluation_dict:
model_eval = [evaluation_dict[measure][model] for model in evaluation_dict[measure]]
ax.bar(pos + adjustment*width, model_eval, width, label=measure)
ax.bar(pos + adjustment*width, model_eval, width, label=measure, color=colors[measure])
adjustment += step_len
ax.set_xticks(pos)
ax.set_xticklabels(model_names)
......@@ -274,7 +276,8 @@ def plot_classification_accuracy(model_names, evaluation_dict):
# fig.tight_layout()
def plot_boxplot(model_names, evaluation_dict):
def plot_boxplot(evaluation_dict, colors):
model_names = evaluation_dict[list(colors.keys())[0]].keys()
fig = plt.figure('boxplot_accuracy')
ax = fig.add_axes([0.15, 0.1, 0.75, 0.8])
step_len = 1.5
......@@ -282,16 +285,13 @@ def plot_boxplot(model_names, evaluation_dict):
adjustment = -(len(model_names)//2)*step_len
pos = np.arange(len(model_names))
width = 1/(5*len(model_names))
colors = ['red', 'yellow', 'blue', 'tan', 'green']
count = 0
for measure in evaluation_dict:
model_eval = [evaluation_dict[measure][model] for model in evaluation_dict[measure]]
boxplot = ax.boxplot(model_eval, positions=pos + adjustment*width, widths=width,
meanline=True, showmeans=True, patch_artist=True)
for patch in boxplot['boxes']:
patch.set(facecolor=colors[count])
patch.set(facecolor=colors[measure])
boxplots.append(boxplot)
count += 1
adjustment += step_len
ax.set_xticks(pos)
......
......@@ -25,13 +25,15 @@ rule test_model:
DIR+'/log/test_model.log'
output:
DIR+'/model evaluation/classification_accuracy/' + '_'.join(MODELS.keys()) + '.pdf'
params:
colors = config['classification_colors']
run:
models = {}
for model in MODELS:
trainer= ANN_Training.ModelTrainer({'model_name': model, 'dir': DIR,
'model_dir': DIR, **MODELS[model]})
models[model] = trainer
evaluate_models(models, DIR, 2)
evaluate_models(models, DIR, 2, params.colors)
rule generate_data:
output:
......
......@@ -23,7 +23,13 @@ functions:
HeavisideTwoSided:
adjustment: 0
# Parameter for Model Training
# Parameter for Model Training and Evaluation
classification_colors:
Accuracy: 'magenta'
Precision: 'red'
Recall: 'tan'
F-Score: 'green'
AUROC: 'yellow'
models:
Adam:
num_epochs: 1000
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment