Skip to content

Commit 57431b4

Browse files
committed
Fix Genealogy Chart. Legend was also added to the chart
* #11
1 parent eedfef7 commit 57431b4

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed
Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1+
# -*- coding: utf-8 -*-
12
import time
23

34
from MLC.GUI.Experiment.QtCharts.QtChartWrapper import QtChartWrapper
5+
from MLC.Log.log import get_gui_logger
46
from MLC.mlc_parameters.mlc_parameters import Config
57
from PyQt5.QtCore import Qt
8+
from PyQt5.QtGui import QColor
9+
from PyQt5.QtGui import QFont
10+
11+
logger = get_gui_logger()
612

713

814
class GenealogyChart(QtChartWrapper):
9-
SWIM_LINE_COLORS = [Qt.cyan, Qt.magenta, Qt.gray, Qt.yellow]
10-
INDIV_COLORS = [Qt.black, Qt.red, Qt.green, Qt.blue]
15+
SWIM_LINE_COLORS = [QColor(159, 202, 230),
16+
QColor(241, 190, 169),
17+
QColor(244, 206, 116),
18+
QColor(174, 125, 184),
19+
QColor(119, 172, 48),
20+
QColor(121, 206, 242),
21+
QColor(220, 167, 177)]
22+
23+
INDIV_COLORS = [Qt.blue, Qt.red, Qt.black, Qt.darkYellow]
24+
INDIV_LEGEND = ["Replication", "Mutation", "Crossover", "Elitism"]
1125

1226
def __init__(self, mlc_local, experiment_name, generation, individual):
13-
QtChartWrapper.__init__(self, False)
27+
QtChartWrapper.__init__(self, show_legend=True)
1428
self._mlc_local = mlc_local
1529
self._indivs_per_gen = Config.get_instance().getint("POPULATION", "size")
1630
self._amount_generations = generation
@@ -21,6 +35,28 @@ def __init__(self, mlc_local, experiment_name, generation, individual):
2135
self._add_axis()
2236
self._add_swimming_lines()
2337
self._add_individuals()
38+
self._add_title()
39+
self._add_legend_markers()
40+
41+
def _add_legend_markers(self):
42+
legend = self.get_legend()
43+
# Hide all the legend of the Series added to this moment
44+
for legend_marker in legend.markers():
45+
legend_marker.setVisible(False)
46+
47+
# Add the evolution strategies as four empty curve in order to set the
48+
# graphic legend
49+
for index in xrange(len(GenealogyChart.INDIV_COLORS)):
50+
self.add_line_curve(line_width=.7,
51+
color=GenealogyChart.INDIV_COLORS[index],
52+
legend=GenealogyChart.INDIV_LEGEND[index])
53+
54+
def _add_title(self):
55+
chart_title = 'Generation N°{0} - Individual N°{1}'.format(self._amount_generations,
56+
self._indiv_index)
57+
chart_font = QFont()
58+
chart_font.setWeight(QFont.ExtraBold)
59+
self.set_title(chart_title, chart_font)
2460

2561
def _add_axis(self):
2662
# Set the object name to be able to retrieve it later
@@ -30,28 +66,30 @@ def _add_axis(self):
3066
label_format='%g', tick_count=10)
3167

3268
chart_view = self.get_widget()
33-
chart_view.chart().axisX().setRange(0.5, self._amount_generations + 0.5)
69+
chart_view.chart().axisX().setRange(0.9, self._amount_generations + 0.1)
3470
chart_view.chart().axisY().setRange(1, self._indivs_per_gen)
71+
chart_view.chart().axisX().setGridLineVisible(False)
72+
chart_view.chart().axisY().setGridLineVisible(False)
3573

3674
def _add_swimming_lines(self):
3775
amount_colors = len(GenealogyChart.SWIM_LINE_COLORS)
3876
# TODO: Increase the marker size
3977
# when the individuals amount per generations decrease
40-
marker_size = 3
78+
marker_size = 4
4179

42-
for index in xrange(self._amount_generations + 1):
80+
for index in xrange(self._amount_generations):
4381
self.add_scatter(marker_size=marker_size,
44-
color=GenealogyChart.SWIM_LINE_COLORS[index % amount_colors])
82+
color=GenealogyChart.SWIM_LINE_COLORS[(index - 1) % amount_colors])
4583

46-
for indiv_id in xrange(self._indivs_per_gen):
47-
self.append_point(index, index, indiv_id)
84+
for indiv_id in xrange(1, self._indivs_per_gen + 1):
85+
self.append_point(index, index + 1, indiv_id)
4886

4987
def _add_individuals(self):
88+
logger.debug("[GENEALOGY_CHART] Looping through individuals")
5089
start_time = time.time()
5190
generations = [self._mlc_local.get_generation(self._experiment_name, i)
5291
for i in xrange(1, self._amount_generations + 1)]
5392

54-
gen_method_points = [[], [], [], []]
5593
indivs_to_process = []
5694
new_indivs_to_process = [self._indiv_index]
5795

@@ -64,19 +102,14 @@ def _add_individuals(self):
64102

65103
if gen != 0:
66104
parents = generations[gen].get_parents()[indiv_id - 1]
67-
if type(parents) == list:
68-
for parent_index in range(len(parents)):
69-
new_indivs_to_process.append(parents[parent_index])
70-
self.add_line_curve(line_width=.3, color=GenealogyChart.INDIV_COLORS[gen_method - 1])
71-
self.append_point(self._next_curve, gen + 1, indiv_id)
72-
self.append_point(self._next_curve, gen, parents[parent_index])
73-
self._next_curve += 1
74-
else:
75-
new_indivs_to_process.append(parents)
76-
self.add_line_curve(line_width=.3, color=GenealogyChart.INDIV_COLORS[gen_method - 1])
105+
for parent_index in range(len(parents)):
106+
new_indivs_to_process.append(parents[parent_index])
107+
self.add_line_curve(line_width=.7,
108+
color=GenealogyChart.INDIV_COLORS[gen_method - 1])
77109
self.append_point(self._next_curve, gen + 1, indiv_id)
78110
self.append_point(self._next_curve, gen, parents[parent_index])
79111
self._next_curve += 1
80112

81113
elapsed_time = time.time() - start_time
82-
print elapsed_time
114+
logger.debug("[GENEALOGY_CHART] Individuals added to chart. Time elapsed: {0}"
115+
.format(elapsed_time))

MLC/GUI/Experiment/QtCharts/QtChartWrapper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def __init__(self, show_legend=False):
1818
self._chart.legend().hide()
1919

2020
self._view = QChartView(self._chart)
21-
self._view.setRubberBand(QChartView.RectangleRubberBand)
2221
self._view.setRenderHint(QPainter.Antialiasing)
2322
self._view.setUpdatesEnabled(True)
2423

@@ -83,8 +82,9 @@ def add_data(self, xdata, ydata, line_width=.1, color=None):
8382
curve.attachAxis(self._yaxis)
8483
return self._ncurves - 1
8584

86-
def add_line_curve(self, line_width=.1, color=None):
85+
def add_line_curve(self, line_width=.1, color=None, legend=None):
8786
curve = QLineSeries()
87+
curve.setName(legend)
8888
pen = curve.pen()
8989

9090
if color is not None:
@@ -135,8 +135,8 @@ def get_widget(self):
135135
"""
136136
return self._view
137137

138-
def repaint(self):
139-
self._view.repaint()
138+
def get_legend(self):
139+
return self._chart.legend()
140140

141141
def _series_to_polyline(self, xdata, ydata):
142142
"""Convert series data to QPolygon(F) polyline

0 commit comments

Comments
 (0)