예제 #1
0
    def _add_data_and_errors(self, data, errors ):
        #check sanity

        #1. check shape
        if errors is not None and data.shape() != errors.shape():
            msg = "Incompatible shapes between data (%s) and errors (%s)" % (
                data.shape(), errors.shape())
            raise IndexError, msg

        shape = tuple(self.__shapeFromAxes())
        dshape = tuple(data.shape())
        assert shape == dshape, \
            "shape mismatch: data shape %s, axes shape %s" % (
            dshape, shape )
        self._setShape( shape )

        #2. check type code
        if errors is not None and data.typecode() != errors.typecode():
            msg = "Incompatible type codes between data (%s) and errors (%s)" \
                  % (data.typecode(), errors.typecode())
            raise TypeError, msg
        self._typeCode = data.typecode()

        #3. check unit
        unit = tounit( self.unit() )
        dunit = tounit( data.unit() )
        eunit = dunit*dunit
        if errors: eunit = tounit( errors.unit() )
        if not _equalUnit( unit, dunit) or not _equalUnit( unit*unit, eunit ):
            msg = "Unit mismatch: histogram unit: %r, data unit: %r, "\
                  "errors unit: %r." % (
                unit, dunit, eunit )
            raise ValueError, msg

        self.addDataset( 'data', data )
        self.addDataset( 'error', errors )
        self._data = data; self._errors = errors
        return
예제 #2
0
    def __init__( self, name = '', data = None, errors = None,
                  axes = [], attributes = None, unit = '1', **kwds):
        """Histogram( name, data, errors, axes, attributes) -> new Histogram
        Create a new histogram instance.
        Inputs:
            name: name of histogram (string)
            data: dataset (implements DatasetBase)
            errors: dataset (implements DatasetBase)
            axes: list of Axes objects (implements Axis)
            attributes: optional dictionary of user-defined attributes
        Output:
            None
        Exceptions: IndexError, TypeError
        Notes: (1) IndexError if data and errors don't have same shape
        (2) TypeError if data and errors don't have same type"""

        # whether the histogram is a slice (reference not copy) of another histogram
        self._isslice = kwds.get('isslice') or False

        if attributes is None: attributes = dict()
        AttributeCont.__init__( self, attributes)
        self.setAttribute( 'name', name)
        self.setAttribute( 'unit', tounit(unit) )

        from DatasetContainer import DatasetContainer as DC

        self._axisCont = DC()

        for i, axis in enumerate( axes):
            axNum = i + 1 #by default axes ids are 1,2,3,...
            self._axisCont.addDataset( axis.name(), axNum, axis)
            continue
        if len(axes) == 1: self._axis = axes[0]

        self._lastDatasetID = 0
        self._dataCont = DC()
        
        if data is None: raise ValueError, "No data provided"
        self._add_data_and_errors( data, errors )
        return