コード例 #1
0
    def add_binned_data(self, key, data, flat=True):
        ''' add data to binned_data

        key : string

        data : PISA Map or (array, binning)-tuple

        flat : bool
            is the data already flattened (i.e. the binning dimesnions unrolled)
        '''
        # TODO: logic to not copy back and forth

        if isinstance(data, Map):
            flat_array = data.hist.ravel()
            self.binned_data[key] = (SmartArray(flat_array), data.binning)

        elif isinstance(data, Sequence) and len(data) == 2:
            binning, array = data
            assert isinstance(binning, MultiDimBinning)
            if isinstance(array, SmartArray):
                array = array.get('host')
            if flat:
                flat_array = array
            else:
                # first dimesnions must match
                assert array.shape[:binning.num_dims] == binning.shape
                #flat_shape = [-1] + [d for d in array.shape[binning.num_dims-1:-1]]
                flat_shape = [binning.size, -1]
                #print(flat_shape)
                flat_array = array.reshape(flat_shape)
            if not isinstance(flat_array, SmartArray):
                flat_array = SmartArray(flat_array.astype(FTYPE))
            self.binned_data[key] = (binning, flat_array)
        else:
            raise TypeError('unknown dataformat')
コード例 #2
0
 def test_astype(self):
     a = SmartArray(np.int32([42, 8, -5]))
     aa = a.astype(np.float64)
     self.assertIsInstance(aa, SmartArray)
     # verify that SmartArray.astype() operates like ndarray.astype()...
     self.assertPreciseEqual(aa.get('host'), a.get('host').astype(np.float64))
     # ...and that both actually yield the expected dtype.
     self.assertPreciseEqual(aa.get('host').dtype.type, np.float64)
     self.assertIs(aa.dtype.type, np.float64)
コード例 #3
0
 def test_astype(self):
     a = SmartArray(np.int32([42, 8, -5]))
     aa = a.astype(np.float64)
     self.assertIsInstance(aa, SmartArray)
     # verify that SmartArray.astype() operates like ndarray.astype()...
     self.assertPreciseEqual(aa.get('host'),
                             a.get('host').astype(np.float64))
     # ...and that both actually yield the expected dtype.
     self.assertPreciseEqual(aa.get('host').dtype.type, np.float64)
     self.assertIs(aa.dtype.type, np.float64)