def create(noerror=False): from histogram import createContinuousAxis, createDiscreteAxis, arange #create axis E axisE = createContinuousAxis('E', 'meter', arange(0.5, 3.5, 1.0)) #create axis "tubeId" axisTubeId = createDiscreteAxis("tubeId", [1, 3, 99], 'int') #create histogram from histogram import createDataset from ndarray.StdVectorNdArray import NdArray dataStorage = NdArray('double', range(9)) dataStorage.setShape((3, 3)) errorsStorage = NdArray('double', range(9)) errorsStorage.setShape((3, 3)) data = createDataset('data', '', storage=dataStorage) if noerror: errors = None else: errors = createDataset('errors', '', storage=errorsStorage) from histogram.Histogram import Histogram histogram = Histogram(name='I(E, TubeId)', data=data, errors=errors, axes=[axisE, axisTubeId]) return histogram
def create(noerror = False): from histogram import createContinuousAxis, createDiscreteAxis, arange #create axis E axisE = createContinuousAxis( 'E', 'meter', arange( 0.5, 3.5, 1.0 ) ) #create axis "tubeId" axisTubeId = createDiscreteAxis( "tubeId", [1, 3, 99], 'int') #create histogram from histogram import createDataset from ndarray.StdVectorNdArray import NdArray dataStorage = NdArray( 'double', range(9) ); dataStorage.setShape( (3,3) ) errorsStorage = NdArray( 'double', range(9) ); errorsStorage.setShape( (3,3) ) data = createDataset('data', '', storage = dataStorage ) if noerror: errors = None else: errors = createDataset('errors', '', storage = errorsStorage ) from histogram.Histogram import Histogram histogram = Histogram( name = 'I(E, TubeId)', data = data, errors = errors, axes = [axisE, axisTubeId]) return histogram
# # {LicenseText} # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # import unittest from unittest import TestCase from ndarray.NumpyNdArray import NdArray from histogram import createDataset from histogram.DatasetContainer import DatasetContainer ds = createDataset( "ds", shape = (3,4) ) ds2 = createDataset( "ds2", shape = (3,4) ) class DatasetContainer_TestCase(TestCase): def testCtor(self): """ DatasetContainer: ctor """ dc = DatasetContainer() return def testAddDataset(self): """ DatasetContainer: addDataset""" dc = DatasetContainer()
def testSetSlice(self): "Histogram: h[ slices ] = rhs" histogram = self._histogram.copy() #set element histogram[1.5,1] = (2,4) self.assertVectorEqual( histogram[1.5,1], (2,4) ) #set slice from histogram.SlicingInfo import SlicingInfo, all, front, back from histogram import createDataset from numpy import array data = createDataset( "data", shape = [2,2] ) data[:,:] = array( [ [1,2], [3,4], ] ) errs = createDataset( "errs", shape = [2,2] ) errs[:,:] = array( [ [1,2], [3,4], ] ) histogram[SlicingInfo((0.5, 1.5)), SlicingInfo((1,3))] = data,errs self.assertVectorEqual( histogram[0.5,1], (1,1) ) self.assertVectorEqual( histogram[0.5,3], (2,2) ) self.assertVectorEqual( histogram[1,1], (3,3) ) self.assertVectorEqual( histogram[1,3], (4,4) ) histogram[(0.5, 1.5),(1,3)] = data,errs self.assertVectorEqual( histogram[0.5,1], (1,1) ) self.assertVectorEqual( histogram[0.5,3], (2,2) ) self.assertVectorEqual( histogram[1,1], (3,3) ) self.assertVectorEqual( histogram[1,3], (4,4) ) from histogram import makeHistogram name = "h" axes = [ ('x', [1,2]), ('y', [1,2]), ] data = array( [ [1,2], [3,4] ] ) errs = array( [ [1,2], [3,4] ] ) h2 = makeHistogram( name, axes, data, errs ) histogram[SlicingInfo((0.5, 1.5)), SlicingInfo((1,3))] = h2 #setslice: rhs is a histogram histogram[(0.5, 1.5), (1,3)] = h2 # setslice: rhs is a list of arrays histogram[(0.5, 1.5), (1,3)] = data, errs # setslice: rhs is a list of arrays including None histogram[(0.5, 1.5), (1,3)] = data, None # setslice: rhs is a lit of arrays. histogram is 1D data = array([1,2]); errs = data; histogram[ 0.5, () ] [ (1,3) ] = data, errs # setslice: rhs is a tuple of two numbers histogram[ 0.5, () ] [ (1,3 )] = 0., 0. #setitem using dictionary histogram[ {'E':1.5, 'tubeId':3} ] = 3, 3 self.assertVectorAlmostEqual( histogram[ {'E':1.5, 'tubeId':3} ], (3, 3) ) histogram[ {'E':1.5} ][ {'tubeId':3} ] = 3, 3 self.assertVectorAlmostEqual( histogram[ {'E':1.5, 'tubeId':3} ], (3, 3) ) # histogram[ {'E':1.5} ] /= 3,3 histogram[ {'E':1.5} ] /= 3,3 return
def testSetSlice(self): "Histogram: h[ slices ] = rhs" histogram = self._histogram.copy() #set element histogram[1.5,1] = (2,4) self.assertVectorEqual( histogram[1.5,1], (2,4) ) #set slice from histogram.SlicingInfo import SlicingInfo, all, front, back from histogram import createDataset from numpy import array data = createDataset( "data", shape = [2,2] ) data[:,:] = array( [ [1,2], [3,4], ] ) errs = createDataset( "errs", shape = [2,2] ) errs[:,:] = array( [ [1,2], [3,4], ] ) histogram[SlicingInfo((0.5, 1.5)), SlicingInfo((1,3))] = data,errs self.assertVectorEqual( histogram[0.5,1], (1,1) ) self.assertVectorEqual( histogram[0.5,3], (2,2) ) self.assertVectorEqual( histogram[1,1], (3,3) ) self.assertVectorEqual( histogram[1,3], (4,4) ) histogram[(0.5, 1.5),(1,3)] = data,errs self.assertVectorEqual( histogram[0.5,1], (1,1) ) self.assertVectorEqual( histogram[0.5,3], (2,2) ) self.assertVectorEqual( histogram[1,1], (3,3) ) self.assertVectorEqual( histogram[1,3], (4,4) ) from histogram import makeHistogram name = "h" axes = [ ('x', [1,2]), ('y', [1,2]), ] data = array( [ [1,2], [3,4] ] ) errs = array( [ [1,2], [3,4] ] ) h2 = makeHistogram( name, axes, data, errs ) histogram[SlicingInfo((0.5, 1.5)), SlicingInfo((1,3))] = h2 #setslice: rhs is a histogram histogram[(0.5, 1.5), (1,3)] = h2 # setslice: rhs is a list of arrays histogram[(0.5, 1.5), (1,3)] = data, errs # setslice: rhs is a list of arrays including None histogram[(0.5, 1.5), (1,3)] = data, None # setslice: rhs is a lit of arrays. histogram is 1D data = array([1,2]); errs = data; histogram[ 0.5, () ] [ (1,3) ] = data, errs # setslice: rhs is a tuple of two numbers histogram[ 0.5, () ] [ (1,3 )] = 0., 0. #setitem using dictionary histogram[ {'E':1.5, 'tubeId':3} ] = 3, 3 self.assertVectorAlmostEqual( histogram[ {'E':1.5, 'tubeId':3} ], (3, 3) ) histogram[ {'E':1.5} ][ {'tubeId':3} ] = 3, 3 self.assertVectorAlmostEqual( histogram[ {'E':1.5, 'tubeId':3} ], (3, 3) ) # histogram[ {'E':1.5} ] /= 3.,3. histogram[ {'E':1.5} ] /= 3.,3. return