コード例 #1
0
    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', list(range(12))))
        #slicing
        self.assertTrue(ds[3:5].storage().compare(NdArray('double', [3, 4])))
        #indexing
        self.assertAlmostEqual(ds[3] / meter, 3)

        #2D dataset
        stor = NdArray('double', list(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.assertTrue(subarr.compare(expected))
        #indexing
        self.assertAlmostEqual(ds[1, 1] / meter, 5)
        return
コード例 #2
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 histogram.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
コード例 #3
0
    def test__imul__(self):
        "Dataset: operator 'a*=b'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds *= 2
        v = ds.storage().asNumarray() * ds.unit() / meter
        self.assertVectorAlmostEqual(v, [2, 4, 6])

        ds *= ds
        v = ds.storage().asNumarray() * ds.unit() / meter / meter
        self.assertVectorAlmostEqual(v, [4, 16, 36])

        self.assertRaises(NotImplementedError, ds.__imul__, "a")

        ds1 = self.Dataset(
            'time',
            unit='second',
            shape=[3],
            storage=NdArray('double', [1, 2, 3]),
        )
        ds *= ds1
        return
コード例 #4
0
    def test__iadd__(self):
        "Dataset: operator 'a+=b'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds += 1 * meter
        v = ds.storage().asNumarray() * ds.unit() / meter
        self.assertVectorAlmostEqual(v, [2, 3, 4])

        ds += ds
        v = ds.storage().asNumarray() * ds.unit() / meter
        self.assertVectorAlmostEqual(v, [4, 6, 8])

        self.assertRaises(NotImplementedError, ds.__iadd__, "a")

        ds = self.Dataset(name='intensity',
                          unit='-1',
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds += 1
        self.assertAlmostEqual(ds[0], 0)

        return
コード例 #5
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 histogram.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
コード例 #6
0
 def testCtor(self):
     """ Dataset: ctor """
     ds = self.Dataset(name="distance",
                       unit="meter",
                       attributes={},
                       shape=[3],
                       storage=NdArray('double', list(range(3))))
     self.assertTrue(ds.storage().compare(NdArray('double',
                                                  list(range(3)))))
     return
コード例 #7
0
    def testSum(self):
        "Dataset: method 'sum'"
        storage = NdArray('double', list(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.assertTrue(ds1.storage().compare(expected))
        return
コード例 #8
0
 def testChangeUnit(self):
     'Dataset: changeUnit'
     storage = NdArray('double', list(range(3)))
     ds = self.Dataset(name="distance", unit="meter", storage=storage)
     ds.changeUnit('mm')
     self.assertVectorAlmostEqual(ds.storage().asNumarray(),
                                  [0, 1000, 2000])
     return
コード例 #9
0
 def testSetShape(self):
     "Dataset: method 'setShape'"
     ds = self.Dataset(name="distance",
                       unit="meter",
                       attributes={},
                       shape=[12],
                       storage=NdArray('double', list(range(12))))
     ds.setShape((3, 4))
     self.assertTrue(ds._storage.shape() == (3, 4))
     return
コード例 #10
0
    def testCopy(self):
        "Dataset: method 'copy'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[12],
                          storage=NdArray('double', list(range(12))))

        ds.storage().compare(ds.copy().storage())
        return
コード例 #11
0
 def test__neg__(self):
     "Dataset: operator '-a'"
     ds = self.Dataset(name="distance",
                       unit="meter",
                       attributes={},
                       shape=[3],
                       storage=NdArray('double', list(range(3))))
     ds2 = -ds
     v = ds2.storage().asNumarray() * ds2.unit() / meter
     self.assertVectorAlmostEqual(v, [-0, -1, -2])
     return
コード例 #12
0
    def test__add__(self):
        "Dataset: operator 'a+b'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds2 = ds + 1 * meter
        v = ds2.storage().asNumarray() * ds2.unit() / meter
        self.assertVectorAlmostEqual(v, [2, 3, 4])

        #ds2 = 1*meter + ds
        #self.assert_( ds2.storage().compare( NdArray('double', [2,3,4] ) ) )

        ds3 = ds + Dataset(unit="meter", storage=NdArray('double', [2, 3, 4]))
        v = ds3.storage().asNumarray() * ds3.unit() / meter
        self.assertVectorAlmostEqual(v, [3, 5, 7])

        self.assertRaises(NotImplementedError, ds.__add__, "a")
        return
コード例 #13
0
    def testReverse(self):
        "Dataset: ds -> 1./ds"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds.reverse()
        v = ds.storage().asNumarray() * ds.unit() * meter
        self.assertVectorAlmostEqual(v, [1, 1. / 2, 1. / 3])

        return
コード例 #14
0
    def test__idiv__2(self):
        'Dataset: a[?]*=b'
        a = self.Dataset(name='a',
                         unit='meter',
                         shape=[5],
                         storage=NdArray('double', list(range(5))))
        a[1:3] /= 2.

        try:
            a[1:3] /= 2 * meter
            raise "Should raise ValueError"
        except ValueError:
            pass
        return
コード例 #15
0
    def test__div__(self):
        "Dataset: operator 'a/b'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds2 = ds / 2.
        v = ds2.storage().asNumarray() * ds2.unit() / meter
        self.assertVectorAlmostEqual(v, [0.5, 1, 1.5])

        ds2 = 1. / ds
        v = ds2.storage().asNumarray() * ds2.unit() * meter
        self.assertVectorAlmostEqual(v, [1., 1. / 2, 1. / 3])
        return
コード例 #16
0
    def test__rdiv__(self):
        "Dataset: operator 'number/b'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        dsunit = ds.unit()

        ds2 = 1. / ds
        v = ds2.storage().asNumarray()
        self.assertVectorAlmostEqual(v, [1., 1. / 2, 1. / 3])
        self.assertEqual(ds2.unit(), 1. / ds.unit())
        self.assertEqual(ds.unit(), dsunit)
        return
コード例 #17
0
    def test__sub__(self):
        "Dataset: operator 'a-b'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds2 = ds - 2 * meter
        v = ds2.storage().asNumarray() * ds2.unit() / meter
        self.assertVectorAlmostEqual(v, [-1, 0, 1])

        ds2 = ds - ds
        v = ds2.storage().asNumarray() * ds2.unit() / meter
        self.assertVectorAlmostEqual(v, [0, 0, 0])

        return
コード例 #18
0
    def test__idiv__(self):
        "Dataset: operator 'a/=b'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds /= 2.
        v = ds.storage().asNumarray() * ds.unit() / meter
        self.assertVectorAlmostEqual(v, [0.5, 1, 1.5])

        ds /= ds
        v = ds.storage().asNumarray() * ds.unit()
        self.assertVectorAlmostEqual(v, [1, 1, 1])

        self.assertRaises(NotImplementedError, ds.__idiv__, "a")
        return
コード例 #19
0
    def test__mul__(self):
        "Dataset: operator 'a*b'"
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[3],
                          storage=NdArray('double', [1, 2, 3]))
        ds2 = ds * 2
        v = ds2.storage().asNumarray() * ds2.unit() / meter
        self.assertVectorAlmostEqual(v, [2, 4, 6])

        ds2 = 2 * ds
        v = ds2.storage().asNumarray() * ds2.unit() / meter
        self.assertVectorAlmostEqual(v, [2, 4, 6])

        ds2 = ds * ds
        v = ds2.storage().asNumarray() * ds2.unit() / meter / meter
        self.assertVectorAlmostEqual(v, [1, 4, 9])
        return
コード例 #20
0
 def testSqrt(self):
     'Dataset: sqrt'
     storage = NdArray('double', list(range(3)))
     ds = self.Dataset(name="distance", unit="meter", storage=storage)
     ds.sqrt()
     return
コード例 #21
0
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
コード例 #22
0
def test_1(**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.axisFromId(1)
    if hax1 is not ax1:
        passed = False
        log("hax1 was %s instead of %s" % (hax1, ax1))
    return passed
コード例 #23
0
    def test__setitem__(self):
        "Dataset: operator 'a[3]=4'"
        #1D dataset
        ds = self.Dataset(name="distance",
                          unit="meter",
                          attributes={},
                          shape=[12],
                          storage=NdArray('double', list(range(12))))
        #set slice
        from numpy import array
        ds[3:5] = array([1, 2]) * meter
        self.assertTrue(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', list(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.assertTrue(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', list(range(12)))
        ds1v.setShape((3, 4))
        ds1 = self.Dataset(name="distance",
                           unit="meter",
                           shape=[3, 4],
                           storage=ds1v)
        ds2v = NdArray('double', list(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', list(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