Skip to content

Fix for Matplotlib issues in TravisCI #475

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

Merged
merged 15 commits into from
Jan 27, 2014
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
25 changes: 14 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
language: python

python:
- "2.7"
- "3.3"

before_install:
- sudo apt-get install liblapack-dev gfortran python-scipy libblas-dev libsuitesparse-dev swig
- sudo ln -Tsf /{run,dev}/shm
- if [ ${TRAVIS_PYTHON_VERSION:0:1} == "2" ]; then wget http://repo.continuum.io/miniconda/Miniconda-2.2.2-Linux-x86_64.sh -O miniconda.sh; else wget http://repo.continuum.io/miniconda/Miniconda3-2.2.2-Linux-x86_64.sh -O miniconda.sh; fi
- chmod +x miniconda.sh
- ./miniconda.sh -b
- export PATH=/home/travis/anaconda/bin:$PATH

install:
- pip install git+https://github.com/numpy/numpy.git
- pip install scipy
- pip install matplotlib
- conda create -n testenv --yes pip python=$TRAVIS_PYTHON_VERSION
- source activate testenv
- conda install --yes ipython==1.1.0 pyzmq numpy scipy nose matplotlib pandas numdifftools Cython scikits.sparse patsy statsmodels
- pip install git+https://github.com/Theano/Theano.git
- pip install pandas
- pip install numdifftools
- pip install Cython
- pip install scikits.sparse
- pip install patsy
- pip install statsmodels
- python setup.py build_ext --inplace
- pip install git+https://github.com/mahmoudimus/nose-timer.git
env:
- pip install git+https://github.com/mahmoudimus/nose-timer.git

env:
- TESTCMD="nosetests -vv --with-timer --timer-verbose -e test_glm -e test_examples -e test_distributions"
- TESTCMD="nosetests -vv --with-timer --timer-verbose pymc.tests.test_distributions"
- TESTCMD="nosetests -vv --with-timer --timer-verbose pymc.tests.test_examples:test_examples0"
- TESTCMD="nosetests -vv --with-timer --timer-verbose pymc.tests.test_examples:test_examples1"
- TESTCMD="nosetests -vv --with-timer --timer-verbose pymc.tests.test_examples:test_examples2"

script: "$TESTCMD"
4 changes: 3 additions & 1 deletion pymc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__version__ = "3.0"
import matplotlib
matplotlib.use('Agg')

from .core import *
from .distributions import *
Expand All @@ -18,4 +20,4 @@
from .tests import test

from . import glm
from .data import *
from .data import *
2 changes: 1 addition & 1 deletion pymc/examples/banana.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
def run(n = 3000):
if n == "short":
n = 50
with model:
with model:
trace = sample(n, step, start)

pl.figure()
Expand Down
4 changes: 2 additions & 2 deletions pymc/examples/glm_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def run(n=2000):

plt.plot(x, y, 'x')
glm.plot_posterior_predictive(trace)
plt.show()
# plt.show()

if __name__ == '__main__':
run()

Expand Down
2 changes: 1 addition & 1 deletion pymc/examples/glm_robust.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def run(n=2000):
plt.plot(x, y, 'x')
glm.plot_posterior_predictive(trace)
plt.show()

if __name__ == '__main__':
run()

Expand Down
24 changes: 12 additions & 12 deletions pymc/examples/simpletest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pylab import *
import matplotlib.pyplot as plt
from pymc import *
import numpy as np
import theano
Expand Down Expand Up @@ -27,16 +27,16 @@ def run(n=1000):
with model:
trace = sample(1e3, step)

subplot(2, 2, 1)
plot(trace[x][:, 0, 0])
subplot(2, 2, 2)
hist(trace[x][:, 0, 0])

subplot(2, 2, 3)
plot(trace[x][:, 1, 0])
subplot(2, 2, 4)
hist(trace[x][:, 1, 0])
show()
plt.subplot(2, 2, 1)
plt.plot(trace[x][:, 0, 0])
plt.subplot(2, 2, 2)
plt.hist(trace[x][:, 0, 0])

plt.subplot(2, 2, 3)
plt.plot(trace[x][:, 1, 0])
plt.subplot(2, 2, 4)
plt.hist(trace[x][:, 1, 0])
plt.show()

if __name__ == '__main__':
run()
2 changes: 1 addition & 1 deletion pymc/glm/glm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import matplotlib.pyplot as plt
import numpy as np
from pymc import *
import patsy
Expand Down Expand Up @@ -179,6 +178,7 @@ def plot_posterior_predictive(trace, eval=None, lm=None, samples=30, **kwargs):
Additional keyword arguments are passed to pylab.plot().

"""
import matplotlib.pyplot as plt

if lm is None:
lm = lambda x, sample: sample['Intercept'] + sample['x'] * x
Expand Down
75 changes: 37 additions & 38 deletions pymc/plots.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
from pylab import *
import matplotlib.pyplot as plt
try:
import matplotlib.gridspec as gridspec
except ImportError:
gridspec = None
import numpy as np
from scipy.stats import kde
from .stats import *
Expand Down Expand Up @@ -40,7 +34,7 @@ def traceplot(trace, vars=None, figsize=None,
fig : figure object

"""

import matplotlib.pyplot as plt
if vars is None:
vars = trace.varnames

Expand Down Expand Up @@ -138,7 +132,7 @@ def kde2plot(x, y, grid=200):

def autocorrplot(trace, vars=None, fontmap=None, max_lag=100):
"""Bar plot of the autocorrelation function for a trace"""

import matplotlib.pyplot as plt
try:
# MultiTrace
traces = trace.traces
Expand All @@ -159,7 +153,7 @@ def autocorrplot(trace, vars=None, fontmap=None, max_lag=100):
chains = len(traces)

n = len(samples[0])
f, ax = subplots(n, chains, squeeze=False)
f, ax = plt.subplots(n, chains, squeeze=False)

max_lag = min(len(samples[0][vars[0]])-1, max_lag)

Expand All @@ -169,7 +163,7 @@ def autocorrplot(trace, vars=None, fontmap=None, max_lag=100):

d = np.squeeze(samples[j][v])

ax[i, j].acorr(d, detrend=mlab.detrend_mean, maxlags=max_lag)
ax[i, j].acorr(d, detrend=plt.mlab.detrend_mean, maxlags=max_lag)

if not j:
ax[i, j].set_ylabel("correlation")
Expand All @@ -179,11 +173,11 @@ def autocorrplot(trace, vars=None, fontmap=None, max_lag=100):
ax[i, j].set_title("chain {0}".format(j+1))

# Smaller tick labels
tlabels = gca().get_xticklabels()
setp(tlabels, 'fontsize', fontmap[1])
tlabels = plt.gca().get_xticklabels()
plt.setp(tlabels, 'fontsize', fontmap[1])

tlabels = gca().get_yticklabels()
setp(tlabels, 'fontsize', fontmap[1])
tlabels = plt.gca().get_yticklabels()
plt.setp(tlabels, 'fontsize', fontmap[1])


def var_str(name, shape):
Expand Down Expand Up @@ -255,6 +249,11 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,
Location of vertical reference line (defaults to 0).

"""
import matplotlib.pyplot as plt
try:
import matplotlib.gridspec as gridspec
except ImportError:
gridspec = None

if not gridspec:
print_('\nYour installation of matplotlib is not recent enough to ' +
Expand Down Expand Up @@ -322,7 +321,7 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,
gs = gridspec.GridSpec(1, 1)

# Subplot for confidence intervals
interval_plot = subplot(gs[0])
interval_plot = plt.subplot(gs[0])

for j, tr in enumerate(traces):

Expand Down Expand Up @@ -381,9 +380,9 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,

if quartiles:
# Plot median
plot(q[2], y, 'bo', markersize=4)
plt.plot(q[2], y, 'bo', markersize=4)
# Plot quartile interval
errorbar(
plt.errorbar(
x=(q[1],
q[3]),
y=(y,
Expand All @@ -393,10 +392,10 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,

else:
# Plot median
plot(q[1], y, 'bo', markersize=4)
plt.plot(q[1], y, 'bo', markersize=4)

# Plot outer interval
errorbar(
plt.errorbar(
x=(q[0],
q[-1]),
y=(y,
Expand All @@ -411,9 +410,9 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,

if quartiles:
# Plot median
plot(quants[2], y, 'bo', markersize=4)
plt.plot(quants[2], y, 'bo', markersize=4)
# Plot quartile interval
errorbar(
plt.errorbar(
x=(quants[1],
quants[3]),
y=(y,
Expand All @@ -422,10 +421,10 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,
color='b')
else:
# Plot median
plot(quants[1], y, 'bo', markersize=4)
plt.plot(quants[1], y, 'bo', markersize=4)

# Plot outer interval
errorbar(
plt.errorbar(
x=(quants[0],
quants[-1]),
y=(y,
Expand All @@ -443,27 +442,27 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,
gs.update(left=left_margin, right=0.95, top=0.9, bottom=0.05)

# Define range of y-axis
ylim(-var + 0.5, -0.5)
plt.ylim(-var + 0.5, -0.5)

datarange = plotrange[1] - plotrange[0]
xlim(plotrange[0] - 0.05 * datarange, plotrange[1] + 0.05 * datarange)
plt.xlim(plotrange[0] - 0.05 * datarange, plotrange[1] + 0.05 * datarange)

# Add variable labels
yticks([-(l + 1) for l in range(len(labels))], labels)
plt.yticks([-(l + 1) for l in range(len(labels))], labels)

# Add title
if main is not False:
plot_title = main or str(int((
1 - alpha) * 100)) + "% Credible Intervals"
title(plot_title)
plt.title(plot_title)

# Add x-axis label
if xtitle is not None:
xlabel(xtitle)
plt.xlabel(xtitle)

# Constrain to specified range
if xrange is not None:
xlim(*xrange)
plt.xlim(*xrange)

# Remove ticklines on y-axes
for ticks in interval_plot.yaxis.get_major_ticks():
Expand All @@ -478,23 +477,23 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,
spine.set_color('none') # don't draw spine

# Reference line
axvline(vline, color='k', linestyle='--')
plt.axvline(vline, color='k', linestyle='--')

# Genenerate Gelman-Rubin plot
if rhat and chains > 1:

# If there are multiple chains, calculate R-hat
rhat_plot = subplot(gs[1])
rhat_plot = plt.subplot(gs[1])

if main is not False:
title("R-hat")
plt.title("R-hat")

# Set x range
xlim(0.9, 2.1)
plt.xlim(0.9, 2.1)

# X axis labels
xticks((1.0, 1.5, 2.0), ("1", "1.5", "2+"))
yticks([-(l + 1) for l in range(len(labels))], "")
plt.xticks((1.0, 1.5, 2.0), ("1", "1.5", "2+"))
plt.yticks([-(l + 1) for l in range(len(labels))], "")

i = 1
for varname in vars:
Expand All @@ -503,15 +502,15 @@ def forestplot(trace_obj, vars=None, alpha=0.05, quartiles=True, rhat=True,
k = np.size(value)

if k > 1:
plot([min(r, 2) for r in R[varname]], [-(j + i)
plt.plot([min(r, 2) for r in R[varname]], [-(j + i)
for j in range(k)], 'bo', markersize=4)
else:
plot(min(R[varname], 2), -i, 'bo', markersize=4)
plt.plot(min(R[varname], 2), -i, 'bo', markersize=4)

i += k

# Define range of y-axis
ylim(-i + 0.5, -0.5)
plt.ylim(-i + 0.5, -0.5)

# Remove ticklines on y-axes
for ticks in rhat_plot.yaxis.get_major_ticks():
Expand Down
4 changes: 3 additions & 1 deletion pymc/tests/test_plots.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#from ..plots import *
import matplotlib
matplotlib.use('Agg', warn=False)

from pymc.plots import *
from pymc import psample, Slice, Metropolis, find_hessian, sample

Expand Down