Пример #1
0
    def test_createDataset(self):
        "histogram.__init__:  createDataset"
        name = "intensity"
        unit = "1"
        shape = [100,200]
        types = [ 'double', 'float', 'int', 'unsigned' ]
        for datatype in types:
            try:
                ds = createDataset( name, unit, shape=shape, data_type = datatype )
            except:
                raise RuntimeError, datatype
            self.assertEqual( name, ds.name() )
            self.assertEqual( 1, ds.unit() )
            self.assertVectorEqual( shape, ds.shape() )
            self.assertEqual( ds.typecodeAsC(), datatype )
            continue

        from ndarray.NumpyNdArray import NdArray 
        storage = NdArray( "float", 20000, 0 )
        storage.setShape( shape )
        ds2 = createDataset( name, unit, storage = storage )
        self.assertEqual( name, ds2.name() )
        self.assertEqual( 1, ds2.unit() )
        self.assertVectorEqual( shape, ds2.shape() )
        self.assertEqual( ds2.typecodeAsC(), "float" )
        return
    def testSum(self):
        "Dataset: method 'sum'"
        storage = NdArray( 'double', range(6) )
        storage.setShape( (2,3) )
        ds = self.Dataset(name = "distance", unit = "meter",
                          attributes = { }, shape = [2,3], storage = storage)

        self.assertAlmostEqual( ds.sum()/meter, 15. )
        
        ds1 = ds.sum(0)
        expected = NdArray( 'double', [3,5,7] )
        self.assert_( ds1.storage().compare( expected ) )
        return
def test_0( **kwds):

    from histogram.Axis import Axis
    from histogram.NdArrayDataset import Dataset
    name = 'test'
    unit = '1'
    attributes = {'plottable':True, 'nifty':False, 'pi':3.14159, 3.14159:'pi'}
    lengths = [2,3,4]
    dtype = 6  # double

    stor1 = NdArray( dtype, lengths[0]+1, 1.0)
    ax1 = Axis( name+'ax1', unit, attributes, lengths[0],  stor1)
    
    stor2 = NdArray( dtype, lengths[1]+1, 1.0)
    ax2 = Axis( name+'ax2', unit, attributes, lengths[1],  stor2)

    stor3 = NdArray( dtype, lengths[2]+1, 1.0)
    ax3 = Axis( name+'ax3', unit, attributes, lengths[2],  stor3) 

    size = lengths[0]*lengths[1]*lengths[2]
    
    dataStore = NdArray( dtype, size, 1.0); dataStore.setShape( (2, 3,4) )
    data = Dataset( name+'data', unit, attributes, lengths, dataStore)

    errorStore = NdArray( dtype, size, 1.0); errorStore.setShape( (2, 3,4) )
    error = Dataset( name+'error', unit, attributes, lengths, errorStore)

    from histogram.Histogram import Histogram

    histogram = Histogram( 'testHist', data, error, [ax1, ax2, ax3],
                           attributes)

    return True
def test_2( **kwds):

    from histogram.Axis import Axis
    from histogram.NdArrayDataset import Dataset
    name = 'test'
    unit = '1'
    attributes = {'plottable':True, 'nifty':False, 'pi':3.14159, 3.14159:'pi'}
    lengths = [2,3,4]
    dtype = 6  # double

    stor1 = NdArray( dtype, lengths[0]+1, 1.0)
    ax1 = Axis( name+'ax1', unit, attributes, lengths[0],  stor1)
    
    stor2 = NdArray( dtype, lengths[1]+1, 1.0)
    ax2 = Axis( name+'ax2', unit, attributes, lengths[1],  stor2)

    stor3 = NdArray( dtype, lengths[2]+1, 1.0)
    ax3 = Axis( name+'ax3', unit, attributes, lengths[2],  stor3) 

    size = lengths[0]*lengths[1]*lengths[2]
    
    dataStore = NdArray( dtype, size, 1.0); dataStore.setShape( (2, 3,4) )
    data = Dataset( name+'data', unit, attributes, lengths, dataStore)

    errorStore = NdArray( dtype, size, 1.0); errorStore.setShape( (2, 3,4) )
    error = Dataset( name+'error', unit, attributes, lengths, errorStore)

    from histogram.Histogram import Histogram

    hist = Histogram( 'testHist', data, error, [ax1, ax2, ax3],
                           attributes)

    passed = True
    hax1 = hist.axisFromName( 'testax1')
    if hax1 is not ax1:
        passed = False
        log("hax1 was %s instead of %s" % (hax1, ax1))
    return passed
    def test__getitem__(self):
        "Dataset: operator 'a[3:5], a[3]'"
        #1D dataset
        ds = self.Dataset(name = "distance", unit = "meter",
                          attributes = { },
                          shape = [12], storage = NdArray( 'double', range(12) ) )
        #slicing
        self.assert_( ds[3:5].storage().compare( NdArray('double', [3,4] ) ) )
        #indexing
        self.assertAlmostEqual( ds[3]/meter, 3 )

        #2D dataset
        stor = NdArray( 'double', range(12) ); stor.setShape( (3,4) )
        ds = self.Dataset(name = "distance", unit = "meter",
                          attributes = { },
                          shape = [3,4], storage = stor)
        #slicing
        subarr = ds[1:2, 1:2].storage()
        expected = NdArray('double', [5] ) ; expected.setShape( (1,1) )
        self.assert_( subarr.compare( expected ) )
        #indexing
        self.assertAlmostEqual( ds[1,1]/meter, 5 )
        return
    def test__setitem__(self):
        "Dataset: operator 'a[3]=4'"
        #1D dataset
        ds = self.Dataset(name = "distance", unit = "meter",
                          attributes = { },
                          shape = [12], storage = NdArray( 'double', range(12) ) )
        #set slice
        from numpy import array
        ds[3:5] = array([1,2])*meter
        self.assert_( ds[3:5].storage().compare( NdArray('double', [1,2] ) ) )
        #set item
        ds[10] = 11*meter
        self.assertAlmostEqual( ds[10]/meter, 11 )
        
        #2D dataset
        stor = NdArray( 'double', range(12) ); stor.setShape( (3,4) )
        ds = self.Dataset(name = "distance", unit = "meter",
                          attributes = { },
                          shape = [3,4], storage = stor)
        #slicing
        ds[1:2, 1:2] = array([[999]])*meter
        subarr = ds[1:2, 1:2].storage()
        expected = NdArray('double', [999] ) ; expected.setShape( (1,1) )
        self.assert_( subarr.compare( expected ) )
        #indexing
        self.assertAlmostEqual( ds[1,1]/meter, 999 )
        ds[1,1] = 333 * meter
        self.assertAlmostEqual( ds[1,1]/meter, 333 )

        #set slice with datasets
        ds1v = NdArray( 'double', range(12) ); ds1v.setShape( (3,4) )
        ds1 = self.Dataset(
            name = "distance", unit = "meter", shape = [3,4], storage = ds1v )
        ds2v = NdArray( 'double', range(4) ); ds2v.setShape( (2,2) )
        ds2 = self.Dataset(
            name = "distance", unit = "meter", shape = [2,2], storage = ds2v )
        ds1[1:3, 1:3] = ds2

        #set slice with one number
        ds1v = NdArray( 'double', range(12) ); ds1v.setShape( (3,4) )
        ds1 = self.Dataset(
            name = "distance", unit = "meter", shape = [3,4], storage = ds1v )
        ds1[1:3,1:3] = 1*meter
        return