Skip to content

Commit d111a93

Browse files
committed
Show a warning when the number of rows changes across populations
1 parent 436baf0 commit d111a93

9 files changed

Lines changed: 118 additions & 457 deletions

File tree

examples/.ipynb_checkpoints/5. Bar charts-checkpoint.ipynb

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

examples/.ipynb_checkpoints/7. Population charts-checkpoint.ipynb

Lines changed: 24 additions & 27 deletions
Large diffs are not rendered by default.

examples/5. Bar charts.ipynb

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

examples/7. Population charts.ipynb

Lines changed: 16 additions & 424 deletions
Large diffs are not rendered by default.

examples/exports/5-natural-gas.png

-19.7 KB
Loading
130 Bytes
Binary file not shown.

multiplex/population/population.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from numbers import Number
1010
import os
1111
import sys
12+
import warnings
1213

1314
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
1415
import util
@@ -36,6 +37,8 @@ class Population(LabelledVisualization):
3637
:ivar populations: A list of populations, represented as scatter points.
3738
Each population contains these points, separated by column.
3839
:vartype populations: list of list of list of :class:`matplotlib.collections.PathCollection`
40+
:ivar rows: The number of rows in the populations.
41+
:vartype rows: int
3942
"""
4043

4144
def __init__(self, *args, **kwargs):
@@ -95,6 +98,12 @@ def draw(self, population, rows, name, style_plot=True, height=0.6,
9598
:raise ValueError: If the height is not between 0 and 1.
9699
"""
97100

101+
# check that the number of rows is the same as in previous populations.
102+
if self.rows and rows != self.rows:
103+
warnings.warn(f"The number of rows is different between populations, changed from { self.rows } to { rows }")
104+
self.rows = rows
105+
self.rows = self.rows or rows
106+
98107
# draw a general label
99108
if label:
100109
self._draw_legend(label, label_style, *args, **kwargs)
Binary file not shown.

multiplex/population/tests/test_population.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,69 @@ def test_draw_large_height(self):
143143
viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
144144
self.assertRaises(ValueError, viz.draw_population, 5, 10, '', height=2)
145145

146+
@MultiplexTest.temporary_plot
147+
def test_draw_save_rows_first_time(self):
148+
"""
149+
Test that when drawing a population the first time, the number of rows are saved in the class.
150+
"""
151+
152+
viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
153+
rows = 10
154+
pop = viz.draw_population(5, rows, '', height=1)
155+
self.assertEqual(rows, viz.population.rows)
156+
157+
@MultiplexTest.temporary_plot
158+
def test_draw_save_rows_second_time(self):
159+
"""
160+
Test that when drawing a population the second time, the number of rows are unchanged in the class.
161+
"""
162+
163+
viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
164+
rows = 10
165+
166+
# draw the first population
167+
pop = viz.draw_population(5, rows, '', height=1)
168+
self.assertEqual(rows, viz.population.rows)
169+
170+
# draw the second population
171+
pop = viz.draw_population(5, rows, '', height=1)
172+
self.assertEqual(rows, viz.population.rows)
173+
174+
@MultiplexTest.temporary_plot
175+
def test_draw_save_rows_different(self):
176+
"""
177+
Test that when drawing a population with a different number of rows than before, the class raises a warning.
178+
"""
179+
180+
viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
181+
rows = 10
182+
183+
# draw the first population
184+
pop = viz.draw_population(5, rows, '', height=1)
185+
self.assertEqual(rows, viz.population.rows)
186+
187+
# draw the second population
188+
with self.assertWarns(Warning):
189+
pop = viz.draw_population(5, rows - 1, '', height=1)
190+
191+
@MultiplexTest.temporary_plot
192+
def test_draw_save_rows_different_update(self):
193+
"""
194+
Test that when drawing a population with a different number of rows than before, the class saves the new number of rows after raising a warning.
195+
"""
196+
197+
viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
198+
rows = 10
199+
200+
# draw the first population
201+
pop = viz.draw_population(5, rows, '', height=1)
202+
self.assertEqual(rows, viz.population.rows)
203+
204+
# draw the second population
205+
with self.assertWarns(Warning):
206+
pop = viz.draw_population(5, rows - 1, '', height=1)
207+
self.assertEqual(rows - 1, viz.population.rows)
208+
146209
@MultiplexTest.temporary_plot
147210
def test_draw_save_population(self):
148211
"""

0 commit comments

Comments
 (0)