Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove check for matplotlib installed #1086

Merged
merged 1 commit into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions axelrod/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@

from typing import List, Union

matplotlib_installed = True
try:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
from mpl_toolkits.axes_grid1 import make_axes_locatable
except ImportError: # pragma: no cover
matplotlib_installed = False
except RuntimeError: # pragma: no cover
matplotlib_installed = False
warnings.warn(
'Matplotlib failed to import and so no plots will be produced. This '
'could be caused by using a virtual environment on OSX. See '
'http://matplotlib.org/faq/virtualenv_faq.html for details.')
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
from mpl_toolkits.axes_grid1 import make_axes_locatable

titleType = List[str]
namesType = List[str]
Expand All @@ -37,7 +27,6 @@ def default_cmap(version: str = "2.0") -> str:
class Plot(object):
def __init__(self, result_set: ResultSet) -> None:
self.result_set = result_set
self.matplotlib_installed = matplotlib_installed
self.nplayers = self.result_set.nplayers
self.players = self.result_set.players

Expand All @@ -46,8 +35,6 @@ def _violinplot(
ax: matplotlib.axes.SubplotBase = None
) -> matplotlib.figure.Figure:
"""For making violinplots."""
if not self.matplotlib_installed: # pragma: no cover
return None

if ax is None:
_, ax = plt.subplots()
Expand Down Expand Up @@ -112,8 +99,6 @@ def winplot(
self, title: titleType = None, ax: matplotlib.axes.SubplotBase = None
) -> matplotlib.figure.Figure:
"""Plots the distributions for the number of wins for each strategy."""
if not self.matplotlib_installed: # pragma: no cover
return None

data, names = self._winplot_dataset
figure = self._violinplot(data, names, title=title, ax=ax)
Expand Down Expand Up @@ -184,8 +169,6 @@ def _payoff_heatmap(
ax: matplotlib.axes.SubplotBase = None
) -> matplotlib.figure.Figure:
"""Generic heatmap plot"""
if not self.matplotlib_installed: # pragma: no cover
return None

if ax is None:
_, ax = plt.subplots()
Expand Down Expand Up @@ -233,8 +216,6 @@ def stackplot(
self, eco, title: titleType = None, logscale: bool = True,
ax: matplotlib.axes.SubplotBase =None
) -> matplotlib.figure.Figure:
if not self.matplotlib_installed: # pragma: no cover
return None

populations = eco.population_sizes

Expand Down
233 changes: 93 additions & 140 deletions axelrod/tests/unit/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
from numpy import mean
import tempfile

matplotlib_installed = True
try:
import matplotlib.pyplot
plt = matplotlib.pyplot
except ImportError: # pragma: no cover
matplotlib_installed = False
import matplotlib.pyplot
plt = matplotlib.pyplot


class TestPlot(unittest.TestCase):
Expand Down Expand Up @@ -105,7 +101,6 @@ def test_default_cmap(self):
def test_init(self):
plot = axelrod.Plot(self.test_result_set)
self.assertEqual(plot.result_set, self.test_result_set)
self.assertEqual(matplotlib_installed, plot.matplotlib_installed)

def test_init_from_resulsetfromfile(self):
tmp_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
Expand All @@ -121,7 +116,6 @@ def test_init_from_resulsetfromfile(self):

plot = axelrod.Plot(rs)
self.assertEqual(plot.result_set, rs)
self.assertEqual(matplotlib_installed, plot.matplotlib_installed)

def test_boxplot_dataset(self):
plot = axelrod.Plot(self.test_result_set)
Expand All @@ -142,40 +136,30 @@ def test_boxplot_xticks_labels(self):
self.expected_boxplot_xticks_labels)

def test_boxplot(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig = plot.boxplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig = plot.boxplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)

def test_boxplot_with_passed_axes(self):
# Test that can plot on a given matplotlib axes
if matplotlib_installed:
fig, axarr = plt.subplots(2, 2)
self.assertEqual(axarr[0, 1].get_ylim(), (0, 1))
plot = axelrod.Plot(self.test_result_set)
plot.boxplot(ax=axarr[0, 1])
self.assertNotEqual(axarr[0, 1].get_ylim(), (0, 1))

# Plot on another axes with a title
plot.boxplot(title="dummy title", ax=axarr[1, 0])
self.assertNotEqual(axarr[1, 0].get_ylim(), (0, 1))
self.assertEqual(axarr[1, 0].get_title(), "dummy title")
fig, axarr = plt.subplots(2, 2)
self.assertEqual(axarr[0, 1].get_ylim(), (0, 1))
plot = axelrod.Plot(self.test_result_set)
plot.boxplot(ax=axarr[0, 1])
self.assertNotEqual(axarr[0, 1].get_ylim(), (0, 1))

else: # pragma: no cover
self.skipTest('matplotlib not installed')
# Plot on another axes with a title
plot.boxplot(title="dummy title", ax=axarr[1, 0])
self.assertNotEqual(axarr[1, 0].get_ylim(), (0, 1))
self.assertEqual(axarr[1, 0].get_title(), "dummy title")

def test_boxplot_with_title(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig = plot.boxplot(title="title")
self.assertIsInstance(fig,
matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig = plot.boxplot(title="title")
self.assertIsInstance(fig,
matplotlib.pyplot.Figure)
plt.close(fig)

def test_winplot_dataset(self):
plot = axelrod.Plot(self.test_result_set)
Expand All @@ -184,22 +168,16 @@ def test_winplot_dataset(self):
self.expected_winplot_dataset)

def test_winplot(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig = plot.winplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig = plot.winplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)

def test_sdvplot(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig = plot.sdvplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig = plot.sdvplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)

def test_lengthplot_dataset(self):
plot = axelrod.Plot(self.test_result_set)
Expand All @@ -208,22 +186,16 @@ def test_lengthplot_dataset(self):
self.expected_winplot_dataset)

def test_lengthplot(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig = plot.lengthplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig = plot.lengthplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)

def test_pdplot(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig = plot.pdplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig = plot.pdplot()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)

def test_payoff_dataset(self):
plot = axelrod.Plot(self.test_result_set)
Expand All @@ -232,94 +204,75 @@ def test_payoff_dataset(self):
self.expected_payoff_dataset)

def test_payoff(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig = plot.payoff()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig = plot.payoff()
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)

def test_payoff_with_title(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig = plot.payoff(title="dummy title")
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig = plot.payoff(title="dummy title")
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)

def test_payoff_with_passed_axes(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
fig, axarr = plt.subplots(2, 2)
self.assertEqual(axarr[0, 1].get_xlim(), (0, 1))

plot.payoff(ax=axarr[0, 1])
self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1))
# Ensure color bar draw at same location as boxplot
color_bar_bbox = fig.axes[-1].get_position().get_points()
payoff_bbox_coord = fig.axes[1].get_position().get_points()
self.assertEqual(color_bar_bbox[1, 1], payoff_bbox_coord[1, 1],
msg="Color bar is not in correct location.")

# Plot on another axes with a title
plot.payoff(title="dummy title", ax=axarr[1, 0])
self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1))
self.assertEqual(axarr[1, 0].get_xlabel(), "dummy title")
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
fig, axarr = plt.subplots(2, 2)
self.assertEqual(axarr[0, 1].get_xlim(), (0, 1))

plot.payoff(ax=axarr[0, 1])
self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1))
# Ensure color bar draw at same location as boxplot
color_bar_bbox = fig.axes[-1].get_position().get_points()
payoff_bbox_coord = fig.axes[1].get_position().get_points()
self.assertEqual(color_bar_bbox[1, 1], payoff_bbox_coord[1, 1],
msg="Color bar is not in correct location.")

# Plot on another axes with a title
plot.payoff(title="dummy title", ax=axarr[1, 0])
self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1))
self.assertEqual(axarr[1, 0].get_xlabel(), "dummy title")

def test_stackplot(self):
if matplotlib_installed:
eco = axelrod.Ecosystem(self.test_result_set)
eco.reproduce(100)

plot = axelrod.Plot(self.test_result_set)
fig = plot.stackplot(eco)
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
fig = plot.stackplot(eco, title="dummy title")
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
fig = plot.stackplot(eco, logscale=False)
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
else: # pragma: no cover
self.skipTest('matplotlib not installed')
eco = axelrod.Ecosystem(self.test_result_set)
eco.reproduce(100)

plot = axelrod.Plot(self.test_result_set)
fig = plot.stackplot(eco)
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
fig = plot.stackplot(eco, title="dummy title")
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)
fig = plot.stackplot(eco, logscale=False)
self.assertIsInstance(fig, matplotlib.pyplot.Figure)
plt.close(fig)

def test_stackplot_with_passed_axes(self):
# Test that can plot on a given matplotlib axes
if matplotlib_installed:
eco = axelrod.Ecosystem(self.test_result_set)
eco.reproduce(100)
plot = axelrod.Plot(self.test_result_set)

fig, axarr = plt.subplots(2, 2)
self.assertEqual(axarr[0, 1].get_xlim(), (0, 1))
eco = axelrod.Ecosystem(self.test_result_set)
eco.reproduce(100)
plot = axelrod.Plot(self.test_result_set)

plot.stackplot(eco, ax=axarr[0, 1])
self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1))
fig, axarr = plt.subplots(2, 2)
self.assertEqual(axarr[0, 1].get_xlim(), (0, 1))

# Plot on another axes with a title
plot.stackplot(eco ,title="dummy title", ax=axarr[1, 0])
self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1))
self.assertEqual(axarr[1, 0].get_title(), "dummy title")
plot.stackplot(eco, ax=axarr[0, 1])
self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1))

else: # pragma: no cover
self.skipTest('matplotlib not installed')
# Plot on another axes with a title
plot.stackplot(eco ,title="dummy title", ax=axarr[1, 0])
self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1))
self.assertEqual(axarr[1, 0].get_title(), "dummy title")


def test_all_plots(self):
if matplotlib_installed:
plot = axelrod.Plot(self.test_result_set)
# Test that this method does not crash.
self.assertIsNone(
plot.save_all_plots(prefix="test_outputs/",
progress_bar=False))
self.assertIsNone(
plot.save_all_plots(prefix="test_outputs/",
title_prefix="A prefix",
progress_bar=True))
else: # pragma: no cover
self.skipTest('matplotlib not installed')
plot = axelrod.Plot(self.test_result_set)
# Test that this method does not crash.
self.assertIsNone(
plot.save_all_plots(prefix="test_outputs/",
progress_bar=False))
self.assertIsNone(
plot.save_all_plots(prefix="test_outputs/",
title_prefix="A prefix",
progress_bar=True))