diff --git a/.travis.yml b/.travis.yml index c92852a..da7e610 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ before_script: install: - pip install Cython - pip install numpy - - pip install matplotlib + - pip install "matplotlib<=3.0.0" - pip install iminuit - pip install pytest pytest-cov pytest-mpl - pip install flake8 pylint diff --git a/probfit/costfunc.pyx b/probfit/costfunc.pyx index a4daebe..aafb3b8 100644 --- a/probfit/costfunc.pyx +++ b/probfit/costfunc.pyx @@ -77,7 +77,7 @@ cdef class SimultaneousFit: p = self.prefix[findex] if self.prefix is not None else None #values = dict((remove_prefix(k, p), v) for k,v in minuit.values.items()) keys = [minuit.parameters[j] for j in self.allpos[i]] - ret_val = construct_arg(minuit.args, self.allpos[i]) + ret_val = construct_arg(tuple(minuit.args), self.allpos[i]) errors = dict((remove_prefix(k, p), minuit.errors[k]) for k in keys) return ret_val, errors diff --git a/probfit/functor.pyx b/probfit/functor.pyx index 3cf41b7..2db3e5c 100644 --- a/probfit/functor.pyx +++ b/probfit/functor.pyx @@ -358,8 +358,9 @@ cdef class AddPdf: return ret tmp.__name__ = getattr(self.allf[findex],'__name__','unnamedpart') - ret = FakeFunc(tmp) - ret.func_code = self.func_code + ret = tmp + #ret = FakeFunc(tmp) + #ret.func_code = self.func_code return ret def eval_parts(self,*arg): @@ -505,8 +506,9 @@ cdef class AddPdfNorm: return fac*self.allf[findex](*this_arg) tmp.__name__ = getattr(self.allf[findex],'__name__','unnamedpart') - ret = FakeFunc(tmp) - ret.func_code = self.func_code + ret = tmp + #ret = FakeFunc(tmp) + #ret.func_code = self.func_code return ret def eval_parts(self,*arg): diff --git a/probfit/plotting.py b/probfit/plotting.py index 0408bfe..f834845 100644 --- a/probfit/plotting.py +++ b/probfit/plotting.py @@ -103,10 +103,11 @@ def draw_ulh(self, minuit=None, bins=100, ax=None, bound=None, else: raise ValueError('show_errbars must be \'normal\' or \'sumw2\'') if not no_plot: - pp = ax.errorbar(mid(e), n, np.sqrt(w2), fmt='b.', capsize=0) + ax.errorbar(mid(e), n, np.sqrt(w2), fmt='b.', capsize=0, + zorder=0) # bound = (e[0], e[-1]) - draw_arg = [('lw', 2)] + draw_arg = [('lw', 2), ('zorder', 2)] if not parts: draw_arg.append(('color', 'r')) @@ -138,8 +139,8 @@ def draw_residual(x, y, yerr, xerr, **kwargs): """Draw a residual plot on the axis. - By default, if show_errbars if True, residuals are drawn as blue points - with errorbars with no endcaps. If show_errbars is False, residuals are + By default, if show_errbars if True, residuals are drawn as blue points + with errorbars with no endcaps. If show_errbars is False, residuals are drawn as a bar graph with black bars. **Arguments** @@ -174,14 +175,14 @@ def draw_residual(x, y, yerr, xerr, if show_errbars: plotopts = dict(fmt='b.', capsize=0) plotopts.update(kwargs) - pp = ax.errorbar(x, y, yerr, xerr, **plotopts) + pp = ax.errorbar(x, y, yerr, xerr, zorder=0, **plotopts) else: plotopts = dict(color='k') plotopts.update(kwargs) pp = ax.bar(x - xerr, y, width=2*xerr, **plotopts) if zero_line: - ax.plot([x[0] - xerr[0], x[-1] + xerr[-1]], [0, 0], 'r-') + ax.plot([x[0] - xerr[0], x[-1] + xerr[-1]], [0, 0], 'r-', zorder=2) # Take the `grid` kwarg to mean 'add a grid if True'; if grid is False and # we called ax.grid(False) then any existing grid on ax would be turned off @@ -254,9 +255,9 @@ def draw_x2(self, minuit=None, ax=None, parmloc=(0.05, 0.95), print_par=True, if not no_plot: ax.plot(x, y, '+') err_ret = (np.ones(len(self.x)), np.ones(len(self.x))) else: - if not no_plot: ax.errorbar(x, y, data_err, fmt='.') + if not no_plot: ax.errorbar(x, y, data_err, fmt='.', zorder=0) err_ret = (data_err, data_err) - draw_arg = [('lw', 2)] + draw_arg = [('lw', 2), ('zorder', 2)] draw_arg.append(('color', 'r')) total_ret = draw_pdf_with_midpoints(self.f, arg, x, ax=ax, no_plot=no_plot, **dict(draw_arg)) @@ -330,7 +331,7 @@ def draw_bx2(self, minuit=None, parmloc=(0.05, 0.95), nfbins=500, ax=None, m = mid(self.edges) if not no_plot: - ax.errorbar(m, self.h, self.err, fmt='.') + ax.errorbar(m, self.h, self.err, fmt='.', zorder=0) data_ret = (self.edges, self.h) error_ret = (self.err, self.err) @@ -338,7 +339,7 @@ def draw_bx2(self, minuit=None, parmloc=(0.05, 0.95), nfbins=500, ax=None, scale = nfbins / float(self.bins) # scale back to bins - draw_arg = [('lw', 2)] + draw_arg = [('lw', 2), ('zorder', 2)] if not parts: draw_arg.append(('color', 'r')) @@ -399,11 +400,11 @@ def draw_blh(self, minuit=None, parmloc=(0.05, 0.95), scale = dataint if not self.extended else 1.0 if not no_plot: - ax.errorbar(m, n, err, fmt='.') + ax.errorbar(m, n, err, fmt='.', zorder=0) data_ret = (self.edges, n) error_ret = (err, err) - draw_arg = [('lw', 2)] + draw_arg = [('lw', 2), ('zorder', 2)] if not parts: draw_arg.append(('color', 'r')) bound = (self.edges[0], self.edges[-1]) @@ -480,11 +481,12 @@ def draw_compare(f, arg, edges, data, errors=None, ax=None, grid=True, normed=Fa yf = vector_apply(f, x, *arg) total = np.sum(data) if normed: - ax.errorbar(x, data / bw / total, errors / bw / total, fmt='.b') - ax.plot(x, yf, 'r', lw=2) + ax.errorbar(x, data / bw / total, errors / bw / total, fmt='.b', + zorder=0) + ax.plot(x, yf, 'r', lw=2, zorder=2) else: - ax.errorbar(x, data, errors, fmt='.b') - ax.plot(x, yf * bw, 'r', lw=2) + ax.errorbar(x, data, errors, fmt='.b', zorder=0) + ax.plot(x, yf * bw, 'r', lw=2, zorder=2) # now draw the parts if parts: @@ -536,7 +538,7 @@ def draw_pdf(f, arg, bound, bins=100, scale=1.0, density=True, * The rest of keyword argument will be pass to pyplot.plot **Returns** - + x, y of what's being plot """ edges = np.linspace(bound[0], bound[1], bins) diff --git a/tests/baseline/draw_residual_blh_norm_options.png b/tests/baseline/draw_residual_blh_norm_options.png index ccbef3a..cf20b61 100644 Binary files a/tests/baseline/draw_residual_blh_norm_options.png and b/tests/baseline/draw_residual_blh_norm_options.png differ diff --git a/tests/baseline/draw_residual_ulh_norm_options.png b/tests/baseline/draw_residual_ulh_norm_options.png index 4597903..620b258 100644 Binary files a/tests/baseline/draw_residual_ulh_norm_options.png and b/tests/baseline/draw_residual_ulh_norm_options.png differ diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 5fa3b92..a2602e6 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -16,6 +16,7 @@ import numpy as np from matplotlib import pyplot as plt from iminuit import Minuit +import sys from probfit.plotting import draw_pdf, draw_compare_hist from probfit.pdf import gaussian, linear from probfit.funcutil import rename @@ -23,6 +24,11 @@ from probfit.costfunc import UnbinnedLH, BinnedLH, BinnedChi2, Chi2Regression, \ SimultaneousFit +major, minor = sys.version_info[0:2] +if major >= 3 and minor >= 5: + special_tol = 60.0 +else: + special_tol = 2.0 def image_comparison(filename, **kwargs): """Decorator to provide a new Figure instance and return it. @@ -52,7 +58,6 @@ def test_draw_pdf_linear(): f = linear draw_pdf(f, {'m': 1., 'c': 2.}, bound=(-10, 10)) - # There is a slight difference in the x-axis tick label positioning for this # plot between Python 2 and 3, it's not important here so increase the RMS # slightly such that it's ignored @@ -105,9 +110,11 @@ def test_draw_residual_ulh_norm(): data = np.random.randn(1000) ulh = UnbinnedLH(gaussian, data) ulh.draw_residual(args=(0., 1.), norm=True) + plt.ylim(-7., 3.) + plt.xlim(-4., 3.) -@image_comparison('draw_residual_ulh_norm_no_errbars.png') +@image_comparison('draw_residual_ulh_norm_no_errbars.png', tolerance=special_tol) def test_draw_residual_ulh_norm(): np.random.seed(0) data = np.random.randn(1000) @@ -130,6 +137,7 @@ def test_draw_ulh_extend_residual_norm(): data = np.random.randn(1000) ulh = UnbinnedLH(Extended(gaussian), data, extended=True) ulh.draw_residual(args=(0., 1., 1000), norm=True) + plt.ylim(-7.,3.) @image_comparison('draw_ulh_with_minuit.png') @@ -171,6 +179,8 @@ def test_draw_residual_blh_norm(): data = np.random.randn(1000) blh = BinnedLH(gaussian, data) blh.draw_residual(args=(0., 1.), norm=True) + plt.ylim(-4., 3.) + plt.xlim(-4., 3.) @image_comparison('draw_residual_blh_norm_options.png') @@ -182,7 +192,7 @@ def test_draw_residual_blh_norm_options(): grid=False, zero_line=False) -@image_comparison('draw_residual_blh_norm_no_errbars.png') +@image_comparison('draw_residual_blh_norm_no_errbars.png', tolerance=special_tol) def test_draw_residual_blh_norm(): np.random.seed(0) data = np.random.randn(1000)