Anubis - Bachelor thesis project

prediction_graph.py
Go to the documentation of this file.
1 # Anubis is a program to control industrial cameras and train and use artificial neural networks
2 # Copyright (C) 2021 Lukaszczyk Jakub
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
18 from matplotlib.figure import Figure
19 import operator
20
21 class Prediction_graph(FigureCanvasQTAgg):
22 """!@brief Prediction results plot"""
23 def __init__(self, parent=None, width=5, height=4, dpi=100):
24 """!@brief Initializes prediction graph
25 @details The graph is created as a bar plot with 5 categories all
26 initialized to 0.
27 @param[in] parent Parent widget
28 @param[in] width Width of the created figure
29 @param[in] height Height of the created figure
30 @param[in] dpi Dots per inch of the created figure
31 """
32 self.figure = Figure(figsize=(width, height), dpi=dpi)
33
34 self.axes = self.figure.add_subplot(111)
35 self.N = 0
36 self.categories = ["ctg1", "ctg2", "ctg3", "ctg4", "ctg5"]
37 self.probability = [0, 0, 0, 0, 0]
38 self.w = 1
39
40 self.axes.bar(self.categories,self.probability,self.w)
41 self.axes.set_ylim(-1,1)
42 self.axes.autoscale(False)
43 super(Prediction_graph,self).__init__(self.figure)
44
45 def add_categories(self, categories = []):
46 """!@brief Changes default categories of the plot to categories from
47 method argument
48 @details All categories currently in a plot are removed and new ones
49 are added in their place. Count is not limited
50 @param[in] categories A list with names of the new categories
51 """
52 self.categories.clear()
53
54 for category in categories:
55 if(category != None):
56 self.categories.append(str(category))
57
58 def write_probability(self, probability = []):
59 """!@brief Finds top 5 items in given input and plots them
60 @details The input probability should have the same dimensions as the
61 categories. after finding the top 5, the categories with the same
62 indices are plotted.
63 @param[in] probability Results to be plotted
64 """
65 top_5 = []
66 top_indices = []
67
68 if(len(probability) < 5):
69 top_5 = probability
70 top_indices = range(0, len(probability))
71 else:
72 for i in range(0, 5):
73 top = 0
74 top_index = 0
75
76 for j in range(len(probability)):
77 if probability[j] > top:
78 top = probability[j];
79 top_index = j
80
81 probability.remove(top);
82 top_5.append(top)
83 top_indices.append(top_index)
84
85 self.probability = top_5
86 self.figure.canvas.axes.clear()
87
88 self.axes.set_ylim(-1.1 ,1.1)
89
90 self.axes.bar(operator.itemgetter(*top_indices)(self.categories),top_5,self.w)
91 self.figure.canvas.draw()
prediction_graph.Prediction_graph.categories
categories
Definition: prediction_graph.py:36
prediction_graph.Prediction_graph.__init__
def __init__(self, parent=None, width=5, height=4, dpi=100)
Initializes prediction graph.
Definition: prediction_graph.py:23
prediction_graph.Prediction_graph.axes
axes
Definition: prediction_graph.py:34
prediction_graph.Prediction_graph.figure
figure
Definition: prediction_graph.py:32
prediction_graph.Prediction_graph.probability
probability
Definition: prediction_graph.py:37
prediction_graph.Prediction_graph.write_probability
def write_probability(self, probability=[])
Finds top 5 items in given input and plots them.
Definition: prediction_graph.py:58
prediction_graph.Prediction_graph.add_categories
def add_categories(self, categories=[])
Changes default categories of the plot to categories from method argument.
Definition: prediction_graph.py:45
prediction_graph.Prediction_graph
Prediction results plot.
Definition: prediction_graph.py:21
prediction_graph.Prediction_graph.N
N
Definition: prediction_graph.py:35
prediction_graph.Prediction_graph.w
w
Definition: prediction_graph.py:38