|
[yoda-svn] r437 - in trunk: include/YODA pyext/yoda pyext/yoda/includeblackhole at projects.hepforge.org blackhole at projects.hepforge.orgWed May 2 01:44:17 BST 2012
Author: buckley Date: Wed May 2 01:44:17 2012 New Revision: 437 Log: More mapping to Python: Profile1D and ReaderYODA are now handled (albeit incompletely) Added: trunk/pyext/yoda/include/30-ProfileBin1D.pyx - copied, changed from r434, trunk/pyext/yoda/include/30-HistoBin1D.pyx trunk/pyext/yoda/include/40-Profile1D.pyx - copied, changed from r434, trunk/pyext/yoda/include/40-Histo1D.pyx trunk/pyext/yoda/include/99-ReaderYODA.pyx - copied, changed from r434, trunk/pyext/yoda/include/99-ReaderAIDA.pyx Modified: trunk/include/YODA/Histo1D.h trunk/include/YODA/Profile1D.h trunk/pyext/yoda/Makefile.am trunk/pyext/yoda/include/40-Histo1D.pyx trunk/pyext/yoda/include/99-ReaderAIDA.pyx trunk/pyext/yoda/shims.h Modified: trunk/include/YODA/Histo1D.h ============================================================================== --- trunk/include/YODA/Histo1D.h Tue May 1 19:40:58 2012 (r436) +++ trunk/include/YODA/Histo1D.h Wed May 2 01:44:17 2012 (r437) @@ -357,9 +357,7 @@ } - /// Divide two histograms - /// - /// The + /// Divide two histograms, with a specified error treatment Scatter2D divide(const Histo1D& numer, const Histo1D& denom, ErrorCombination erropt=QUAD); Modified: trunk/include/YODA/Profile1D.h ============================================================================== --- trunk/include/YODA/Profile1D.h Tue May 1 19:40:58 2012 (r436) +++ trunk/include/YODA/Profile1D.h Wed May 2 01:44:17 2012 (r437) @@ -140,14 +140,19 @@ } /// Bin addition operator + /// @todo Should be addBins()? void addBin(size_t from, size_t to) { _axis.addBin(from, to); } + /// Bin addition operator + /// @todo Should be addBins()? void addBin(const std::vector<double> binedges) { _axis.addBin(binedges); } + /// Bin addition operator + /// @todo Should be addBins()? void addBin(const std::vector<std::pair<double,double> > edges) { _axis.addBin(edges); } @@ -163,6 +168,16 @@ return bins().size(); } + /// Low edge of this histo's axis + double lowEdge() const { + return _axis.lowEdge(); + } + + /// High edge of this histo's axis + double highEdge() const { + return _axis.highEdge(); + } + /// Access the bin vector std::vector<YODA::ProfileBin1D>& bins() { Modified: trunk/pyext/yoda/Makefile.am ============================================================================== --- trunk/pyext/yoda/Makefile.am Tue May 1 19:40:58 2012 (r436) +++ trunk/pyext/yoda/Makefile.am Wed May 2 01:44:17 2012 (r437) @@ -4,7 +4,7 @@ yoda.cpp: yoda.pyx $(PYTHON) -m cython --cplus yoda.pyx -yoda.pyx: include/00-imports.pyx include/10-AnalysisObject.pyx include/20-Dbn1D.pyx include/20-Dbn2D.pyx include/20-Point2D.pyx include/30-HistoBin1D.pyx include/30-HistoBin2D.pyx include/30-Reader.pyx include/30-Scatter2D.pyx include/40-Histo1D.pyx include/40-Histo2D.pyx include/40-WriterAIDA.pyx include/99-ReaderAIDA.pyx +yoda.pyx: include/00-imports.pyx include/10-AnalysisObject.pyx include/20-Dbn1D.pyx include/20-Dbn2D.pyx include/20-Point2D.pyx include/30-HistoBin1D.pyx include/30-ProfileBin1D.pyx include/30-HistoBin2D.pyx include/30-Reader.pyx include/30-Scatter2D.pyx include/40-Histo1D.pyx include/40-Profile1D.pyx include/40-Histo2D.pyx include/40-WriterAIDA.pyx include/99-ReaderAIDA.pyx include/99-ReaderYODA.pyx cat include/*.pyx > yoda.pyx clean-local: Copied and modified: trunk/pyext/yoda/include/30-ProfileBin1D.pyx (from r434, trunk/pyext/yoda/include/30-HistoBin1D.pyx) ============================================================================== --- trunk/pyext/yoda/include/30-HistoBin1D.pyx Tue May 1 17:28:58 2012 (r434, copy source) +++ trunk/pyext/yoda/include/30-ProfileBin1D.pyx Wed May 2 01:44:17 2012 (r437) @@ -1,8 +1,8 @@ -cdef extern from "YODA/HistoBin1D.h" namespace "YODA": +cdef extern from "YODA/ProfileBin1D.h" namespace "YODA": - cdef cppclass cHistoBin1D "YODA::HistoBin1D": - cHistoBin1D (cHistoBin1D &h) - cHistoBin1D (double, double) + cdef cppclass cProfileBin1D "YODA::ProfileBin1D": + cProfileBin1D (cProfileBin1D &h) + cProfileBin1D (double, double) double area() double height() double areaErr() @@ -25,22 +25,28 @@ double xVariance() double xStdDev() double xStdErr() + double mean() + double variance() + double stdDev() + double stdErr() double numEntries() double effNumEntries() double sumW() double sumW2() double sumWX() double sumWX2() + double sumWY() + double sumWY2() #Ugly hack using shim header for Cython 0.13 cdef extern from "shims.h": - cHistoBin1D add_HistoBin1D (cHistoBin1D &, cHistoBin1D &) - cHistoBin1D subtract_HistoBin1D (cHistoBin1D &, cHistoBin1D &) + cProfileBin1D add_ProfileBin1D (cProfileBin1D &, cProfileBin1D &) + cProfileBin1D subtract_ProfileBin1D (cProfileBin1D &, cProfileBin1D &) -cdef class HistoBin1D: - cdef cHistoBin1D *thisptr +cdef class ProfileBin1D: + cdef cProfileBin1D *thisptr cdef bool _dealloc def __cinit__(self): @@ -50,7 +56,7 @@ if self._dealloc: del self.thisptr - cdef HistoBin1D setptr(self, cHistoBin1D *ptr, bool dealloc): + cdef ProfileBin1D setptr(self, cProfileBin1D *ptr, bool dealloc): if self._dealloc: del self.thisptr @@ -58,21 +64,19 @@ self._dealloc = dealloc return self - cdef cHistoBin1D* ptr(self): + cdef cProfileBin1D* ptr(self): return self.thisptr @property def lowEdge(self): """The lower of the two bin edges.""" return self.ptr().lowEdge() - xMin = lowEdge @property def highEdge(self): """The higher of the two bin edges.""" return self.ptr().highEdge() - xMax = highEdge @property @@ -106,6 +110,31 @@ return self.ptr().xStdDev() @property + def xStdErr(self): + """The standard error of the x-values that have filled the bin.""" + return self.ptr().xStdErr() + + @property + def mean(self): + """The mean of the y-values that have filled the bin.""" + return self.ptr().mean() + + @property + def variance(self): + """The variance of the y-values that have filled the bin.""" + return self.ptr().variance() + + @property + def stdDev(self): + """The standard deviation of the y-values that have filled the bin.""" + return self.ptr().stdDev() + + @property + def stdErr(self): + """The standard error of the y-values that have filled the bin.""" + return self.ptr().stdErr() + + @property def numEntries(self): """The number of entries in the bin.""" return self.ptr().numEntries() @@ -137,44 +166,14 @@ return self.ptr().sumWX2() @property - def area(self): - """ - b.area <==> b.sumW - - The area of the bin is the sum of weights of the bin; it is - independent of width. - - """ - return self.ptr().area() - - @property - def height(self): - """ - b.height <==> b.area / b.width - - The height of the bin is defined as the area divided by the - width. - - """ - return self.ptr().height() - - @property - def areaErr(self): - """ - Error computed using binomial statistics on squared sum of bin weights, - i.e. s.areaErr = sqrt(s.sumW2) - - """ - return self.ptr().areaErr() + def sumWY(self): + """Sum of the products of y-values and their weights.""" + return self.ptr().sumWX() @property - def heightErr(self): - """ - Height error - scales the s.areaError by the reciprocal of the - bin width. - - """ - return self.ptr().heightErr() + def sumWY2(self): + """Sum of the products of y-values squared and their weights.""" + return self.ptr().sumWX2() def scaleX(self, double factor): @@ -197,11 +196,11 @@ def __repr__(self): - return 'HistoBin1D(%r)' % self.area + return 'ProfileBin1D(%r)' % self.area -cdef HistoBin1D HistoBin1D_fromptr(cHistoBin1D *ptr, dealloc=False): - # Construct a Python HistoBin1D from a pointer to a cHistoBin1D, +cdef ProfileBin1D ProfileBin1D_fromptr(cProfileBin1D *ptr, dealloc=False): + # Construct a Python ProfileBin1D from a pointer to a cProfileBin1D, # without calling __init__ and excessive memory allocation - cdef HistoBin1D bin = HistoBin1D.__new__(HistoBin1D) + cdef ProfileBin1D bin = ProfileBin1D.__new__(ProfileBin1D) return bin.setptr(ptr, dealloc) Modified: trunk/pyext/yoda/include/40-Histo1D.pyx ============================================================================== --- trunk/pyext/yoda/include/40-Histo1D.pyx Tue May 1 19:40:58 2012 (r436) +++ trunk/pyext/yoda/include/40-Histo1D.pyx Wed May 2 01:44:17 2012 (r437) @@ -25,6 +25,7 @@ cDbn1D &totalDbn() cDbn1D &underflow() cDbn1D &overflow() + #addBin() void eraseBin(size_t index) # Statistical functions Copied and modified: trunk/pyext/yoda/include/40-Profile1D.pyx (from r434, trunk/pyext/yoda/include/40-Histo1D.pyx) ============================================================================== --- trunk/pyext/yoda/include/40-Histo1D.pyx Tue May 1 17:28:58 2012 (r434, copy source) +++ trunk/pyext/yoda/include/40-Profile1D.pyx Wed May 2 01:44:17 2012 (r437) @@ -1,14 +1,14 @@ -cdef extern from "YODA/Histo1D.h" namespace "YODA": - #cHisto1D operator + (cHisto1D &, cHisto1D &) - #cHisto1D operator - (cHisto1D &, cHisto1D &) - #cScatter2D operator / (cHisto1D &, cHisto1D &)""" - - cdef cppclass cHisto1D "YODA::Histo1D"(cAnalysisObject): - cHisto1D(size_t nbins, double lower, double upper, string &path, string &title) - cHisto1D(vector[double] &binedges, string &path, string &title) - cHisto1D(vector[double] &binedges) - cHisto1D(cHisto1D &h, string &path) - cHisto1D(cHisto1D &h) +cdef extern from "YODA/Profile1D.h" namespace "YODA": + #cProfile1D operator + (cProfile1D &, cProfile1D &) + #cProfile1D operator - (cProfile1D &, cProfile1D &) + #cScatter2D operator / (cProfile1D &, cProfile1D &)""" + + cdef cppclass cProfile1D "YODA::Profile1D"(cAnalysisObject): + cProfile1D(size_t nbins, double lower, double upper, string &path, string &title) + cProfile1D(vector[double] &binedges, string &path, string &title) + cProfile1D(vector[double] &binedges) + cProfile1D(cProfile1D &h, string &path) + cProfile1D(cProfile1D &h) void fill(double x, double weight) void reset() @@ -20,33 +20,30 @@ size_t numBins() double lowEdge() double highEdge() - vector[cHistoBin1D] &bins() - cHistoBin1D & bin "bin"(size_t i) - cDbn1D &totalDbn() - cDbn1D &underflow() - cDbn1D &overflow() - void eraseBin(size_t index) + vector[cProfileBin1D] &bins() + cProfileBin1D & bin "bin"(size_t i) + cDbn2D &totalDbn() + cDbn2D &underflow() + cDbn2D &overflow() + #addBin + #void eraseBin(size_t index) # Statistical functions - double integral(bool includeoverflows) - double integral(size_t a, size_t b) + #double integral(bool includeoverflows) + #double integral(size_t a, size_t b) double sumW(bool includeoverflows) double sumW2(bool includeoverflows) - double mean(bool includeoverflows) - double variance(bool includeoverflows) - double stdDev(bool includeoverflows) -cdef extern from "shims.h": - cHisto1D add_Histo1D (cHisto1D &, cHisto1D &) - cHisto1D subtract_Histo1D (cHisto1D &, cHisto1D &) - cScatter2D divide_Histo1D (cHisto1D &, cHisto1D &) - cScatter2D Scatter2D_mkScatter(cHisto1D &) + cProfile1D add(cProfile1D &, cProfile1D &) + cProfile1D subtract(cProfile1D &, cProfile1D &) + cScatter2D divide(cProfile1D &, cProfile1D &) + #cScatter2D mkScatter(cProfile1D &) from cython.operator cimport dereference as deref -cdef class Histo1D(AnalysisObject): +cdef class Profile1D(AnalysisObject): def __init__(self, *args, **kwargs): self._dealloc = True cdef: @@ -60,26 +57,26 @@ nbins, lower, upper = args[0], args[1], args[2] self.setptr( - new cHisto1D(nbins, lower, upper, string(path), string(title)) + new cProfile1D(nbins, lower, upper, string(path), string(title)) ) else: - raise ValueError('Histo1D: Expected 3 arguments') + raise ValueError('Profile1D: Expected 3 arguments') - cdef cHisto1D* ptr(self): - return <cHisto1D *> self.thisptr + cdef cProfile1D* ptr(self): + return <cProfile1D *> self.thisptr - def asScatter(self): - """ - h.asScatter() -> Scatter2D + # def asScatter(self): + # """ + # h.asScatter() -> Scatter2D - Return a 2D scatter data object from the histogram's bins and heights + # Return a 2D scatter data object from the profile's bins and heights - """ - cdef cScatter2D *s = new cScatter2D() - s[0] = Scatter2D_mkScatter(self.ptr()[0]) - return Scatter2D_fromptr(s, True) + # """ + # cdef cScatter2D *s = new cScatter2D() + # s[0] = Scatter2D_mkScatter(self.ptr()[0]) + # return Scatter2D_fromptr(s, True) def fill(self, double x, double weight=1.0): @@ -138,7 +135,7 @@ @property def bins(self): """ - h.bins -> tuple(HistoBin1D) + h.bins -> tuple(ProfileBin1D) Access the bin objects of this histogram. Bin objects are mutable and changes to the bin objects will be propagated back to the histogram @@ -147,11 +144,11 @@ """ cdef size_t numbins = self.ptr().numBins() cdef size_t i - cdef HistoBin1D bin + cdef ProfileBin1D bin out = [] for i in xrange(numbins): - bin = HistoBin1D_fromptr(& self.ptr().bins()[i]) + bin = ProfileBin1D_fromptr(& self.ptr().bins()[i]) out.append(bin) # TODO: Why was this here? # self.ptr().bins() @@ -189,7 +186,7 @@ Return the Distribution1D object representing the total distribution. """ - return Dbn1D_fromptr(&self.ptr().totalDbn()) + return Dbn2D_fromptr(&self.ptr().totalDbn()) @property @@ -200,7 +197,7 @@ Return the Distribution1D object representing the underflow. """ - return Dbn1D_fromptr(&self.ptr().underflow()) + return Dbn2D_fromptr(&self.ptr().underflow()) @property @@ -211,26 +208,15 @@ Return the Distribution1D object representing the overflow. """ - return Dbn1D_fromptr(&self.ptr().overflow()) + return Dbn2D_fromptr(&self.ptr().overflow()) - def __delitem__(self, size_t ix): - self.ptr().eraseBin(ix) + # def __delitem__(self, size_t ix): + # self.ptr().eraseBin(ix) def __getitem__(self, size_t ix): - return HistoBin1D_fromptr(& self.ptr().bin(ix)) - - - def integral(self, bool overflows=True): - """ - s.integral([overflows]) -> float - - Return the total area of the histogram. If overflows is False, ignore - over-and underflow bins. - - """ - return self.ptr().integral(overflows) + return ProfileBin1D_fromptr(& self.ptr().bin(ix)) def sumW(self, bool overflows=True): @@ -256,47 +242,14 @@ return self.ptr().sumW2(overflows) - def mean(self, bool overflows=True): - """ - s.mean([overflows]) -> float - - Return the mean. If overflows is False, ignore the over- and underflow - bins. - - """ - return self.ptr().mean(overflows) - - - def variance(self, bool overflows=True): - """ - s.variance([overflows]) -> float - - Return the variance. If overflows is False, ignore the over- and - underflow bins. - - """ - return self.ptr().variance(overflows) - - - def stdDev(self, bool overflows=True): - """ - s.stdDev([overflows]) -> float - - Return the standard deviation. If overflows is False, ignore over-and - underflow bins. - - """ - return self.ptr().stdDev(overflows) + def __add__(Profile1D a, Profile1D b): + cdef cProfile1D *res = new cProfile1D(add(a.ptr()[0], b.ptr()[0])) + return Profile1D_fromptr(res, True) - def __add__(Histo1D a, Histo1D b): - cdef cHisto1D *res = new cHisto1D(add_Histo1D(a.ptr()[0], b.ptr()[0])) - return Histo1D_fromptr(res, True) - - - def __sub__(Histo1D a, Histo1D b): - cdef cHisto1D *res = new cHisto1D(subtract_Histo1D(a.ptr()[0], b.ptr()[0])) - return Histo1D_fromptr(res, True) + def __sub__(Profile1D a, Profile1D b): + cdef cProfile1D *res = new cProfile1D(subtract(a.ptr()[0], b.ptr()[0])) + return Profile1D_fromptr(res, True) def __mul__(x, y): @@ -304,32 +257,32 @@ Scalar multiplication. Equivalent to scaleW acting on a copy. """ - cdef cHisto1D *res + cdef cProfile1D *res tx, ty = type(x), type(y) - if (tx is int or tx is float) and ty is Histo1D: - histo = <Histo1D> y; factor = <Histo1D> x - elif tx is Histo1D and (ty is int or ty is float): - histo = <Histo1D> x; factor = <Histo1D> y + if (tx is int or tx is float) and ty is Profile1D: + histo = <Profile1D> y; factor = <Profile1D> x + elif tx is Profile1D and (ty is int or ty is float): + histo = <Profile1D> x; factor = <Profile1D> y else: raise RuntimeError('Cannot multiply %r by %r' % (tx, ty)) - res = new cHisto1D(histo.ptr()[0]) + res = new cProfile1D(histo.ptr()[0]) res.scaleW(factor) - return Histo1D_fromptr(res, True) + return Profile1D_fromptr(res, True) - def _div_scalar(Histo1D x, double y): + def _div_scalar(Profile1D x, double y): if y == 0: - raise ArithmeticError('Histo1D: Divide by zero scalar') + raise ArithmeticError('Profile1D: Divide by zero scalar') - cdef cHisto1D *res = new cHisto1D(x.ptr()[0]) + cdef cProfile1D *res = new cProfile1D(x.ptr()[0]) res.scaleW(1.0 / y) - return Histo1D_fromptr(res, True) + return Profile1D_fromptr(res, True) - def _div_histo(Histo1D x, Histo1D y): - cdef cScatter2D s = divide_Histo1D(x.ptr()[0], y.ptr()[0]) + def _div_profile(Profile1D x, Profile1D y): + cdef cScatter2D s = divide(x.ptr()[0], y.ptr()[0]) return Scatter2D_fromptr(&s) @@ -340,19 +293,18 @@ """ tx = type(x); ty = type(y) - if tx is Histo1D: + if tx is Profile1D: if ty is int or ty is float: return x._div_scalar(y) - elif ty is Histo1D: - return x._div_histo(y) - + elif ty is Profile1D: + return x._div_profile(y) raise RuntimeError('Cannot multiply %r by %r' % (tx, ty)) def __repr__(self): - return 'Histo1D%r' % self.bins + return 'Profile1D%r' % self.bins -cdef Histo1D Histo1D_fromptr(cHisto1D *ptr, bool dealloc=False): - cdef Histo1D histo = Histo1D.__new__(Histo1D) - return histo.setptr(ptr, dealloc) +cdef Profile1D Profile1D_fromptr(cProfile1D *ptr, bool dealloc=False): + cdef Profile1D profile = Profile1D.__new__(Profile1D) + return profile.setptr(ptr, dealloc) Modified: trunk/pyext/yoda/include/99-ReaderAIDA.pyx ============================================================================== --- trunk/pyext/yoda/include/99-ReaderAIDA.pyx Tue May 1 19:40:58 2012 (r436) +++ trunk/pyext/yoda/include/99-ReaderAIDA.pyx Wed May 2 01:44:17 2012 (r437) @@ -1,6 +1,7 @@ cdef extern from "YODA/ReaderAIDA.h" namespace "YODA::ReaderAIDA": cReader createReaderAIDA "YODA::ReaderAIDA::create" () + def __read_AIDA(char *filename): """Takes a file, returns a list of AnalysisObjects""" cdef vector[AOptr] vec = vector[AOptr]() @@ -22,6 +23,7 @@ return out + cdef class ReaderAIDA: """Read AIDA files""" read = staticmethod(__read_AIDA) Copied and modified: trunk/pyext/yoda/include/99-ReaderYODA.pyx (from r434, trunk/pyext/yoda/include/99-ReaderAIDA.pyx) ============================================================================== --- trunk/pyext/yoda/include/99-ReaderAIDA.pyx Tue May 1 17:28:58 2012 (r434, copy source) +++ trunk/pyext/yoda/include/99-ReaderYODA.pyx Wed May 2 01:44:17 2012 (r437) @@ -1,13 +1,14 @@ -cdef extern from "YODA/ReaderAIDA.h" namespace "YODA::ReaderAIDA": - cReader createReaderAIDA "YODA::ReaderAIDA::create" () +cdef extern from "YODA/ReaderYODA.h" namespace "YODA::ReaderYODA": + cReader createReaderYODA "YODA::ReaderYODA::create" () -def __read_AIDA(char *filename): + +def __read_YODA(char *filename): """Takes a file, returns a list of AnalysisObjects""" cdef vector[AOptr] vec = vector[AOptr]() cdef size_t i cdef AnalysisObject ana, ana_ - createReaderAIDA().read(string(filename), vec) + createReaderYODA().read(string(filename), vec) out = [] ana_ = AnalysisObject() @@ -15,13 +16,14 @@ for i in range(vec.size()): ana_.thisptr = vec[i] - if ana_.type == 'Scatter2D': - out.append(Scatter2D_fromptr(<cScatter2D*>vec[i], True)) - elif ana_.type == 'Histo1D': - out.append(Scatter2D_fromptr(<cScatter2D*>vec[i], True)) + if ana_.type == 'Histo1D': + out.append(Histo1D_fromptr(<cHisto1D*>vec[i], True)) + elif ana_.type == 'Profile1D': + out.append(Profile1D_fromptr(<cProfile1D*>vec[i], True)) return out -cdef class ReaderAIDA: - """Read AIDA files""" - read = staticmethod(__read_AIDA) + +cdef class ReaderYODA: + """Read YODA files""" + read = staticmethod(__read_YODA) Modified: trunk/pyext/yoda/shims.h ============================================================================== --- trunk/pyext/yoda/shims.h Tue May 1 19:40:58 2012 (r436) +++ trunk/pyext/yoda/shims.h Wed May 2 01:44:17 2012 (r437) @@ -2,6 +2,7 @@ #include "YODA/AnalysisObject.h" #include "YODA/Histo1D.h" +#include "YODA/Profile1D.h" #include "YODA/Scatter2D.h" #include "YODA/HistoBin1D.h" #include "YODA/Histo2D.h" @@ -49,12 +50,12 @@ return a / b; } -void WriterAIDA_write (const std::string& filename, const std::vector<AnalysisObject*>& aos) -{ + +void WriterAIDA_write (const std::string& filename, const std::vector<AnalysisObject*>& aos) { return WriterAIDA::write(filename, aos); } -Scatter2D Scatter2D_mkScatter(const Histo1D& h) -{ + +Scatter2D Scatter2D_mkScatter(const Histo1D& h) { return YODA::mkScatter(h); }
More information about the yoda-svn mailing list |