Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Remove FakeFunc from AddPdf/AddPdfNorm._parts #91

Merged
merged 9 commits into from
Oct 31, 2018
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion probfit/costfunc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions probfit/functor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
36 changes: 19 additions & 17 deletions probfit/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand Down Expand Up @@ -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**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -330,15 +331,15 @@ 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)

bound = (self.edges[0], self.edges[-1])

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'))
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
Binary file modified tests/baseline/draw_residual_blh_norm_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/draw_residual_ulh_norm_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 13 additions & 3 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@
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
from probfit.functor import Extended, AddPdfNorm, AddPdf
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new tolerance seems excessively large. Why is that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else:
special_tol = 2.0

def image_comparison(filename, **kwargs):
"""Decorator to provide a new Figure instance and return it.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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)
Expand Down