Ejemplo n.º 1
0
    def test_DataProcessorBasic(self):

        dc_in = DataContainer(numpy.arange(10), True)
        dc_out = dc_in.copy()

        ax = AX()
        ax.scalar = 2
        ax.set_input(dc_in)

        #check results with out
        out_gold = dc_in*2
        ax.get_output(out=dc_out)
        numpy.testing.assert_array_equal(dc_out.as_array(), out_gold.as_array())

        #check results with return
        dc_out2 = ax.get_output()
        numpy.testing.assert_array_equal(dc_out2.as_array(), out_gold.as_array())

        #check call method
        dc_out2 = ax(dc_in)
        numpy.testing.assert_array_equal(dc_out2.as_array(), out_gold.as_array())

        #check storage mode
        self.assertFalse(ax.store_output)
        self.assertTrue(ax.output == None)
        ax.store_output = True
        self.assertTrue(ax.store_output)

        #check storing a copy and not a reference
        dc_out = ax.get_output()
        numpy.testing.assert_array_equal(ax.output.as_array(), out_gold.as_array())
        self.assertFalse(id(ax.output.as_array()) == id(dc_out.as_array()))

        #check recalculation on argument change
        ax.scalar = 3
        out_gold = dc_in*3
        ax.get_output(out=dc_out)
        numpy.testing.assert_array_equal(dc_out.as_array(), out_gold.as_array())

        #check recalculation on input change
        dc_in2 = dc_in.copy()
        dc_in2 *=2
        out_gold = dc_in2*3
        ax.set_input(dc_in2)
        ax.get_output(out=dc_out)
        numpy.testing.assert_array_equal(dc_out.as_array(), out_gold.as_array())

        #check recalculation on input modified (won't pass)
        dc_in2 *= 2
        out_gold = dc_in2*3
        ax.get_output(out=dc_out)
Ejemplo n.º 2
0
    def test_DataContainer(self):
        arr = numpy.arange(0, 120).reshape(2, 3, 4, 5)
        data = DataContainer(arr, True, dimension_labels=['c', 'z', 'y', 'x'])

        data_new = data.get_slice(c=1)
        self.assertEquals(data_new.shape, (3, 4, 5))
        numpy.testing.assert_array_equal(data_new.array, arr[1])

        data_new = data.get_slice(c=1, y=3)
        self.assertEquals(data_new.shape, (3, 5))
        numpy.testing.assert_array_equal(data_new.array, arr[1, :, 3, :])

        data_new = data.get_slice(c=1, y=3, z=1)
        self.assertEquals(data_new.shape, (5, ))
        numpy.testing.assert_array_equal(data_new.array, arr[1, 1, 3, :])
Ejemplo n.º 3
0
    def test_reverse_operand_algebra(self):
        print ("Test reverse operand algebra")

        number = 3/2
        
        X, Y, Z = 32, 64, 128
        a = numpy.ones((X, Y, Z), dtype='float32')
        ds = DataContainer(a * 3, False, ['X', 'Y', 'Z'])

        # rdiv
        b = number / ds
        numpy.testing.assert_array_almost_equal(a * 0.5, b.as_array())
        # radd
        number = 1
        b = number + ds
        numpy.testing.assert_array_almost_equal(a * 4, b.as_array())
        # rsub
        number = 3
        b = number - ds
        numpy.testing.assert_array_almost_equal(numpy.zeros_like(a), b.as_array())
        # rmul
        number = 1/3
        b = number * ds
        numpy.testing.assert_array_almost_equal(a, b.as_array())
        # rpow
        number = 2
        b = number ** ds
        numpy.testing.assert_array_almost_equal(a * 8, b.as_array())
Ejemplo n.º 4
0
 def test_negation(self):
     X, Y, Z = 256, 512, 512
     a = numpy.ones((X, Y, Z), dtype='int32')
     
     ds = - DataContainer(a, False, ['X', 'Y', 'Z'])
     
     numpy.testing.assert_array_equal(ds.as_array(), -a)
Ejemplo n.º 5
0
 def create_DataContainer(self, X,Y,Z, value=1):
     steps = [timer()]
     a = value * numpy.ones((X, Y, Z), dtype='float32')
     steps.append(timer())
     t0 = dt(steps)
     #print("a refcount " , sys.getrefcount(a))
     ds = DataContainer(a, False, ['X', 'Y', 'Z'])
     return ds
Ejemplo n.º 6
0
 def test_creation_copy(self):
     shape = (2, 3, 4, 5)
     size = shape[0]
     for i in range(1, len(shape)):
         size = size * shape[i]
     #print("a refcount " , sys.getrefcount(a))
     a = numpy.asarray([i for i in range(size)])
     #print("a refcount " , sys.getrefcount(a))
     a = numpy.reshape(a, shape)
     #print("a refcount " , sys.getrefcount(a))
     ds = DataContainer(a, True, ['X', 'Y', 'Z', 'W'])
     #print("a refcount " , sys.getrefcount(a))
     self.assertEqual(sys.getrefcount(a), 2)
Ejemplo n.º 7
0
    def binary_multiply(self):
        print("Test binary multiply")
        X, Y, Z = 1024, 512, 512
        X, Y, Z = 256, 512, 512
        steps = [timer()]
        a = numpy.ones((X, Y, Z), dtype='float32')
        steps.append(timer())
        t0 = dt(steps)

        #print("a refcount " , sys.getrefcount(a))
        ds = DataContainer(a, False, ['X', 'Y', 'Z'])
        ds1 = ds.copy()

        steps.append(timer())
        ds.multiply(ds1, out=ds)
        steps.append(timer())
        t1 = dt(steps)
        print("ds.multiply(ds1, out=ds)", dt(steps))
        steps.append(timer())
        ds2 = ds.multiply(ds1)
        steps.append(timer())
        t2 = dt(steps)
        print("ds2 = ds.multiply(ds1)", dt(steps))

        #self.assertLess(t1, t2)

        ds0 = ds
        ds0.multiply(2, out=ds0)
        steps.append(timer())
        print("ds0.multiply(2,out=ds0)", dt(
            steps), 2., ds0.as_array()[0][0][0])
        self.assertEqual(2., ds0.as_array()[0][0][0])

        dt1 = dt(steps)
        ds3 = ds0.multiply(2)
        steps.append(timer())
        print("ds3 = ds0.multiply(2)", dt(steps), 4., ds3.as_array()[0][0][0])
        dt2 = dt(steps)
        #self.assertLess(dt1, dt2)
        self.assertEqual(4., ds3.as_array()[0][0][0])
        self.assertEqual(2., ds.as_array()[0][0][0])
        
        ds.multiply(2.5, out=ds0)
        self.assertEqual(2.5*2., ds0.as_array()[0][0][0])
Ejemplo n.º 8
0
 def test_creation_nocopy(self):
     shape = (2, 3, 4, 5)
     size = shape[0]
     for i in range(1, len(shape)):
         size = size * shape[i]
     #print("a refcount " , sys.getrefcount(a))
     a = numpy.asarray([i for i in range(size)])
     #print("a refcount " , sys.getrefcount(a))
     a = numpy.reshape(a, shape)
     #print("a refcount " , sys.getrefcount(a))
     ds = DataContainer(a, False, ['X', 'Y', 'Z', 'W'])
     #print("a refcount " , sys.getrefcount(a))
     self.assertEqual(id(a), id(ds.array))
     self.assertEqual(ds.dimension_labels, ('X', 'Y', 'Z', 'W'))
Ejemplo n.º 9
0
 def test_exp_log(self):
     a0 = numpy.asarray([1 for i in range(2*3*4)])
             
     ds0 = DataContainer(numpy.reshape(a0,(2,3,4)), suppress_warning=True)
     # ds1 = DataContainer(numpy.reshape(a1,(2,3,4)), suppress_warning=True)
     b = ds0.exp().log()
     self.assertNumpyArrayEqual(ds0.as_array(), b.as_array())
     
     self.assertEqual(ds0.exp().as_array()[0][0][0], numpy.exp(1))
     self.assertEqual(ds0.log().as_array()[0][0][0], 0.)
Ejemplo n.º 10
0
    def binary_divide(self):
        print("Test binary divide")
        X, Y, Z = 1024, 512, 512
        X, Y, Z = 256, 512, 512
        steps = [timer()]
        a = numpy.ones((X, Y, Z), dtype='float32')
        steps.append(timer())
        t0 = dt(steps)

        #print("a refcount " , sys.getrefcount(a))
        ds = DataContainer(a, False, ['X', 'Y', 'Z'])
        ds1 = ds.copy()

        t1 = 0 
        t2 = 0
        for i in range(10):
            steps.append(timer())
            ds.divide(ds1, out=ds)
            steps.append(timer())
            t1 += dt(steps)/10.
            print("ds.divide(ds1, out=ds)", dt(steps))
            steps.append(timer())
            ds2 = ds.divide(ds1)
            steps.append(timer())
            t2 += dt(steps)/10.
            print("ds2 = ds.divide(ds1)", dt(steps))

        #self.assertLess(t1, t2)
        self.assertEqual(ds.as_array()[0][0][0], 1.)

        ds0 = ds
        ds0.divide(2, out=ds0)
        steps.append(timer())
        print("ds0.divide(2,out=ds0)", dt(steps), 0.5, ds0.as_array()[0][0][0])
        self.assertEqual(0.5, ds0.as_array()[0][0][0])

        dt1 = dt(steps)
        ds3 = ds0.divide(2)
        steps.append(timer())
        print("ds3 = ds0.divide(2)", dt(steps), 0.25, ds3.as_array()[0][0][0])
        dt2 = dt(steps)
        #self.assertLess(dt1, dt2)
        self.assertEqual(.25, ds3.as_array()[0][0][0])
        self.assertEqual(.5, ds.as_array()[0][0][0])
Ejemplo n.º 11
0
 def setUp(self):
     IG = ImageGeometry(voxel_num_x=10,
                     voxel_num_y=10)
     
     self.data_init = IG.allocate('random')
     
     self.data = self.data_init.copy()
     
     self.data.as_array()[2,3] = float('inf')
     self.data.as_array()[4,5] = float('nan')
     
     mask_manual = numpy.ones((10,10), dtype=numpy.bool)
     mask_manual[2,3] = 0
     mask_manual[4,5] = 0
     
     self.mask_manual = DataContainer(mask_manual, dimension_labels=self.data.dimension_labels) 
     self.mask_generated = MaskGenerator.special_values()(self.data)
Ejemplo n.º 12
0
    def binary_subtract(self):
        print("Test binary subtract")
        X, Y, Z = 512, 512, 512
        steps = [timer()]
        a = numpy.ones((X, Y, Z), dtype='float32')
        steps.append(timer())
        t0 = dt(steps)

        #print("a refcount " , sys.getrefcount(a))
        ds = DataContainer(a, False, ['X', 'Y', 'Z'])
        ds1 = ds.copy()
        out = ds.copy()

        steps.append(timer())
        ds.subtract(ds1, out=out)
        steps.append(timer())
        t1 = dt(steps)
        print("ds.subtract(ds1, out=ds)", dt(steps))
        self.assertEqual(0., out.as_array()[0][0][0])

        steps.append(timer())
        ds2 = out.subtract(ds1)
        self.assertEqual(-1., ds2.as_array()[0][0][0])

        steps.append(timer())
        t2 = dt(steps)
        print("ds2 = ds.subtract(ds1)", dt(steps))

        #self.assertLess(t1, t2)

        del ds1
        ds0 = ds.copy()
        steps.append(timer())
        ds0.subtract(2, out=ds0)
        #ds0.__isub__( 2 )
        steps.append(timer())
        print("ds0.subtract(2,out=ds0)", dt(
            steps), -1., ds0.as_array()[0][0][0])
        self.assertEqual(-1., ds0.as_array()[0][0][0])

        dt1 = dt(steps)
        ds3 = ds0.subtract(2)
        steps.append(timer())
        print("ds3 = ds0.subtract(2)", dt(steps), 0., ds3.as_array()[0][0][0])
        dt2 = dt(steps)
        #self.assertLess(dt1, dt2)
        self.assertEqual(-1., ds0.as_array()[0][0][0])
        self.assertEqual(-3., ds3.as_array()[0][0][0])
Ejemplo n.º 13
0
 def testGb_creation_nocopy(self):
     X, Y, Z = 512, 512, 512
     X, Y, Z = 256, 512, 512
     steps = [timer()]
     a = numpy.ones((X, Y, Z), dtype='float32')
     steps.append(timer())
     t0 = dt(steps)
     print("test clone")
     #print("a refcount " , sys.getrefcount(a))
     ds = DataContainer(a, False, ['X', 'Y', 'Z'])
     #print("a refcount " , sys.getrefcount(a))
     self.assertEqual(sys.getrefcount(a), 3)
     ds1 = ds.copy()
     self.assertNotEqual(aid(ds.as_array()), aid(ds1.as_array()))
     ds1 = ds.clone()
     self.assertNotEqual(aid(ds.as_array()), aid(ds1.as_array()))
Ejemplo n.º 14
0
    def binary_add(self):
        print("Test binary add")
        X, Y, Z = 512, 512, 512
        #X, Y, Z = 1024, 512, 512
        steps = [timer()]
        a = numpy.ones((X, Y, Z), dtype='float32')
        steps.append(timer())
        t0 = dt(steps)

        #print("a refcount " , sys.getrefcount(a))
        ds = DataContainer(a, False, ['X', 'Y', 'Z'])
        ds1 = ds.copy()
        out = ds.copy()

        steps.append(timer())
        ds.add(ds1, out=out)
        steps.append(timer())
        t1 = dt(steps)
        print("ds.add(ds1, out=ds)", dt(steps))
        steps.append(timer())
        ds2 = ds.add(ds1)
        steps.append(timer())
        t2 = dt(steps)
        print("ds2 = ds.add(ds1)", dt(steps))

        #self.assertLess(t1, t2)
        self.assertEqual(out.as_array()[0][0][0], 2.)
        self.assertNumpyArrayEqual(out.as_array(), ds2.as_array())
        
        ds0 = ds
        dt1 = 0
        dt2 = 0
        for i in range(10):
            steps.append(timer())
            ds0.add(2, out=out)
            steps.append(timer())
            print("ds0.add(2,out=out)", dt(steps), 3, ds0.as_array()[0][0][0])
            self.assertEqual(3., out.as_array()[0][0][0])

            dt1 += dt(steps)/10
            steps.append(timer())
            ds3 = ds0.add(2)
            steps.append(timer())
            print("ds3 = ds0.add(2)", dt(steps), 5, ds3.as_array()[0][0][0])
            dt2 += dt(steps)/10
        
        self.assertNumpyArrayEqual(out.as_array(), ds3.as_array())
Ejemplo n.º 15
0
 def test_DataProcessorChaining(self):
     shape = (2,3,4,5)
     size = shape[0]
     for i in range(1, len(shape)):
         size = size * shape[i]
     #print("a refcount " , sys.getrefcount(a))
     a = numpy.asarray([i for i in range( size )])
     a = numpy.reshape(a, shape)
     ds = DataContainer(a, False, ['X', 'Y','Z' ,'W'])
     c = ds.subset(Y=0)
     c = c.subset(['Z','W','X'])
     arr = c.as_array()
     #[ 0 60  1 61  2 62  3 63  4 64  5 65  6 66  7 67  8 68  9 69 10 70 11 71
     # 12 72 13 73 14 74 15 75 16 76 17 77 18 78 19 79]
     
     print(arr)
 
     ax = AX()
     ax.scalar = 2
     ax.set_input(c)
     #ax.apply()
     print ("ax  in {0} out {1}".format(c.as_array().flatten(),
            ax.get_output().as_array().flatten()))
     
     numpy.testing.assert_array_equal(ax.get_output().as_array(), arr*2)
             
     
     print("check call method of DataProcessor")
     numpy.testing.assert_array_equal(ax(c).as_array(), arr*2)
     
     cast = CastDataContainer(dtype=numpy.float32)
     cast.set_input(c)
     out = cast.get_output()
     self.assertTrue(out.as_array().dtype == numpy.float32)
     out *= 0 
     axm = AX()
     axm.scalar = 0.5
     axm.set_input(c)
     axm.get_output(out)
     numpy.testing.assert_array_equal(out.as_array(), arr*0.5)
     
     print("check call method of DataProcessor")
     numpy.testing.assert_array_equal(axm(c).as_array(), arr*0.5)        
 
     
     # check out in DataSetProcessor
     #a = numpy.asarray([i for i in range( size )])
        
     # create a PixelByPixelDataProcessor
     
     #define a python function which will take only one input (the pixel value)
     pyfunc = lambda x: -x if x > 20 else x
     clip = PixelByPixelDataProcessor()
     clip.pyfunc = pyfunc 
     clip.set_input(c)    
     #clip.apply()
     v = clip.get_output().as_array()
     
     self.assertTrue(v.max() == 19)
     self.assertTrue(v.min() == -79)
     
     print ("clip in {0} out {1}".format(c.as_array(), clip.get_output().as_array()))
     
     #dsp = DataProcessor()
     #dsp.set_input(ds)
     #dsp.input = a
     # pipeline
 
     chain = AX()
     chain.scalar = 0.5
     chain.set_input_processor(ax)
     print ("chain in {0} out {1}".format(ax.get_output().as_array(), chain.get_output().as_array()))
     numpy.testing.assert_array_equal(chain.get_output().as_array(), arr)
     
     print("check call method of DataProcessor")
     numpy.testing.assert_array_equal(ax(chain(c)).as_array(), arr)        
Ejemplo n.º 16
0
 def test_DataContainer(self):
     arr = numpy.arange(0, 120).reshape(2, 3, 4, 5)
     data = DataContainer(arr, True, dimension_labels=['c', 'z', 'y', 'x'])
     data.reorder(['x', 'y', 'z', 'c'])
     self.assertEquals(data.shape, (5, 4, 3, 2))
     numpy.testing.assert_array_equal(data.array, arr.transpose(3, 2, 1, 0))
Ejemplo n.º 17
0
    def test_unary_operations(self):
        print("Test unary operations")
        X, Y, Z = 1024, 512, 512
        X, Y, Z = 256, 512, 512
        steps = [timer()]
        a = -numpy.ones((X, Y, Z), dtype='float32')
        steps.append(timer())
        t0 = dt(steps)
        print(t0)
        #print("a refcount " , sys.getrefcount(a))
        ds = DataContainer(a, False, ['X', 'Y', 'Z'])

        ds.sign(out=ds)
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], -1.)

        ds.abs(out=ds)
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 1.)

        ds.__imul__(2)
        ds.sqrt(out=ds)
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0],
                         numpy.sqrt(2., dtype='float32'))
Ejemplo n.º 18
0
 def test_dot(self):
     a0 = numpy.asarray([i for i in range(2*3*4)])
     a1 = numpy.asarray([2*i for i in range(2*3*4)])
     
             
     ds0 = DataContainer(numpy.reshape(a0,(2,3,4)))
     ds1 = DataContainer(numpy.reshape(a1,(2,3,4)))
     
     numpy.testing.assert_equal(ds0.dot(ds1), a0.dot(a1))
     
     a2 = numpy.asarray([2*i for i in range(2*3*5)])
     ds2 = DataContainer(numpy.reshape(a2,(2,3,5)))
     
     # it should fail if the shape is wrong
     try:
         ds2.dot(ds0)
         self.assertTrue(False)
     except ValueError as ve:
         self.assertTrue(True)
         
     print ("test dot numpy")
     n0 = (ds0 * ds1).sum()
     n1 = ds0.as_array().ravel().dot(ds1.as_array().ravel())
     self.assertEqual(n0, n1)
Ejemplo n.º 19
0
    def testInlineAlgebra(self):
        print("Test Inline Algebra")
        X, Y, Z = 1024, 512, 512
        X, Y, Z = 256, 512, 512
        steps = [timer()]
        a = numpy.ones((X, Y, Z), dtype='float32')
        steps.append(timer())
        t0 = dt(steps)
        print(t0)
        #print("a refcount " , sys.getrefcount(a))
        ds = DataContainer(a, False, ['X', 'Y', 'Z'])
        #ds.__iadd__( 2 )
        ds += 2
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 3.)
        #ds.__isub__( 2 )
        ds -= 2
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 1.)
        #ds.__imul__( 2 )
        ds *= 2
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 2.)
        #ds.__idiv__( 2 )
        ds /= 2
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 1.)

        ds1 = ds.copy()
        #ds1.__iadd__( 1 )
        ds1 += 1
        #ds.__iadd__( ds1 )
        ds += ds1
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 3.)
        #ds.__isub__( ds1 )
        ds -= ds1
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 1.)
        #ds.__imul__( ds1 )
        ds *= ds1
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 2.)
        #ds.__idiv__( ds1 )
        ds /= ds1
        steps.append(timer())
        print(dt(steps))
        self.assertEqual(ds.as_array()[0][0][0], 1.)
Ejemplo n.º 20
0
    def process(self, out=None):

        data = self.get_input()
        ndim = data.number_of_dimensions

        geometry_0 = data.geometry
        geometry = geometry_0.copy()

        dimension_labels = list(geometry_0.dimension_labels)

        if self.roi != None:
            for key in self.roi.keys():
                if key not in data.dimension_labels:
                    raise ValueError(
                        'Wrong label is specified for roi, expected {}.'.
                        format(data.dimension_labels))

        slice_object = self._construct_slice_object(self.roi, data.shape,
                                                    dimension_labels)

        for key in self.roi.keys():
            idx = data.dimension_labels.index(key)
            n_elements = numpy.int32(
                numpy.ceil((slice_object[idx].stop - slice_object[idx].start) /
                           numpy.abs(slice_object[idx].step)))

            if (isinstance(data, ImageData)):

                if key == 'channel':
                    if n_elements > 1:
                        geometry.channels = n_elements
                    else:
                        geometry = geometry.get_slice(
                            channel=slice_object[idx].start)
                elif key == 'vertical':
                    if n_elements > 1:
                        geometry.voxel_num_z = n_elements
                    else:
                        geometry = geometry.get_slice(
                            vertical=slice_object[idx].start)
                elif key == 'horizontal_x':
                    if n_elements > 1:
                        geometry.voxel_num_x = n_elements
                    else:
                        geometry = geometry.get_slice(
                            horizontal_x=slice_object[idx].start)
                elif key == 'horizontal_y':
                    if n_elements > 1:
                        geometry.voxel_num_y = n_elements
                    else:
                        geometry = geometry.get_slice(
                            horizontal_y=slice_object[idx].start)

            # if AcquisitionData
            else:
                if key == 'channel':
                    if n_elements > 1:
                        geometry.set_channels(num_channels=n_elements)
                    else:
                        geometry = geometry.get_slice(
                            channel=slice_object[idx].start)
                elif key == 'angle':
                    if n_elements > 1:
                        geometry.config.angles.angle_data = geometry_0.config.angles.angle_data[
                            slice_object[idx]]
                    else:
                        geometry = geometry.get_slice(
                            angle=slice_object[idx].start)
                elif key == 'vertical':
                    if n_elements > 1:
                        geometry.config.panel.num_pixels[1] = n_elements
                    else:
                        geometry = geometry.get_slice(
                            vertical=slice_object[idx].start)
                elif key == 'horizontal':
                    if n_elements > 1:
                        geometry.config.panel.num_pixels[0] = n_elements
                    else:
                        geometry = geometry.get_slice(
                            horizontal=slice_object[idx].start)

        if geometry is not None:
            data_sliced = geometry.allocate()
            data_sliced.fill(
                numpy.squeeze(data.as_array()[tuple(slice_object)]))
            if out == None:
                return data_sliced
            else:
                out = data_sliced
        else:
            if self.force == False:
                raise ValueError(
                    "Cannot calculate system geometry. Use 'force=True' to return DataContainer instead."
                )
            else:
                return DataContainer(numpy.squeeze(
                    data.as_array()[tuple(slice_object)]),
                                     deep_copy=False,
                                     dimension_labels=dimension_labels,
                                     suppress_warning=True)
Ejemplo n.º 21
0
    def test_subset(self):
        shape = (4, 3, 2)
        a = [i for i in range(2*3*4)]
        a = numpy.asarray(a)
        a = numpy.reshape(a, shape)
        ds = DataContainer(a, True, ['X', 'Y', 'Z'])
        sub = ds.subset(['X'])
        res = True
        try:
            numpy.testing.assert_array_equal(sub.as_array(),
                                             numpy.asarray([0, 6, 12, 18]))
        except AssertionError as err:
            res = False
            print(err)
        self.assertTrue(res)

        sub = ds.subset(['X'], Y=2, Z=0)
        res = True
        try:
            numpy.testing.assert_array_equal(sub.as_array(),
                                             numpy.asarray([4, 10, 16, 22]))
        except AssertionError as err:
            res = False
            print(err)
        self.assertTrue(res)

        sub = ds.subset(['Y'])
        try:
            numpy.testing.assert_array_equal(
                sub.as_array(), numpy.asarray([0, 2, 4]))
            res = True
        except AssertionError as err:
            res = False
            print(err)
        self.assertTrue(res)

        sub = ds.subset(['Z'])
        try:
            numpy.testing.assert_array_equal(
                sub.as_array(), numpy.asarray([0, 1]))
            res = True
        except AssertionError as err:
            res = False
            print(err)
        self.assertTrue(res)
        sub = ds.subset(['Z'], X=1, Y=2)
        try:
            numpy.testing.assert_array_equal(
                sub.as_array(), numpy.asarray([10, 11]))
            res = True
        except AssertionError as err:
            res = False
            print(err)
        self.assertTrue(res)

        print(a)
        sub = ds.subset(['X', 'Y'], Z=1)
        res = True
        try:
            numpy.testing.assert_array_equal(sub.as_array(),
                                             numpy.asarray([[1,  3,  5],
                                                            [7,  9, 11],
                                                            [13, 15, 17],
                                                            [19, 21, 23]]))
        except AssertionError as err:
            res = False
            print(err)
        self.assertTrue(res)