Ejemplo n.º 1
0
def test_iter_isinstance():
    obj = (1, 'x', 2.5)
    assert cu.iter_isinstance(obj, (int, str, float))
    assert cu.iter_isinstance(obj, ((int, float), (str, int), float))
    assert not cu.iter_isinstance(obj, (int, str))
    assert not cu.iter_isinstance(obj, (int, str, float, int))
    assert not cu.iter_isinstance(1, (int))  # only works for tuples
    assert not cu.iter_isinstance(int, (float))
    assert cu.iter_isinstance(obj, (int, str), (int, str, float), (float, int))
Ejemplo n.º 2
0
def test_iter_isinstance():
    obj = (1, 'x', 2.5)
    assert cu.iter_isinstance(obj, (int, str, float))
    assert cu.iter_isinstance(obj, ((int, float), (str, int), float))
    assert not cu.iter_isinstance(obj, (int, str))
    assert not cu.iter_isinstance(obj, (int, str, float, int))
    assert not cu.iter_isinstance(1, (int))  # only works for tuples
    assert not cu.iter_isinstance(int, (float))
    assert cu.iter_isinstance(obj, (int, str), (int, str, float), (float, int))
Ejemplo n.º 3
0
    def slice_to_cube(self, axis, chunk, **kwargs):
        """
        For a hypercube, return a 3-D cube that has been cut along the given
        axis and with data corresponding to the given chunk.

        Parameters
        ----------
        axis: int
            The axis to cut from the hypercube
        chunk: int, astropy Quantity or tuple:
            The data to take from the axis
        """
        if self.data.ndim == 3:
            raise cu.CubeError(4, 'Can only slice a hypercube into a cube')

        item = [slice(None, None, None) for _ in range(4)]
        if isinstance(chunk, tuple):
            if cu.iter_isinstance(chunk, (u.Quantity, u.Quantity)):
                pixel0 = cu.convert_point(chunk[0].value, chunk[0].unit,
                                          self.axes_wcs, axis)
                pixel1 = cu.convert_point(chunk[1].value, chunk[1].unit,
                                          self.axes_wcs, axis)
                item[axis] = slice(pixel0, pixel1, None)
            elif cu.iter_isinstance((chunk, int, int)):
                item[axis] = slice(chunk[0], chunk[1], None)
            else:
                raise cu.CubeError(5, "Parameters must be of the same type")
            newdata = self.data[item].sum(axis)
        else:
            unit = chunk.unit if isinstance(chunk, u.Quantity) else None
            pixel = cu.convert_point(chunk, unit, self.axes_wcs, axis)
            item[axis] = pixel
            newdata = self.data[item]
        wcs_indices = [0, 1, 2, 3]
        wcs_indices.remove(3 - axis)
        newwcs = wu.reindex_wcs(self.axes_wcs, np.array(wcs_indices))
        if axis == 2 or axis == 3:
            newwcs = wu.add_celestial_axis(newwcs)
            newwcs.was_augmented = True
        cube = Cube(newdata, newwcs, meta=self.meta, **kwargs)
        return cube
Ejemplo n.º 4
0
    def slice_to_cube(self, axis, chunk, **kwargs):
        """
        For a hypercube, return a 3-D cube that has been cut along the given
        axis and with data corresponding to the given chunk.

        Parameters
        ----------
        axis: int
            The axis to cut from the hypercube
        chunk: int, astropy Quantity or tuple:
            The data to take from the axis
        """
        if self.data.ndim == 3:
            raise cu.CubeError(4, 'Can only slice a hypercube into a cube')

        item = [slice(None, None, None) for _ in range(4)]
        if isinstance(chunk, tuple):
            if cu.iter_isinstance(chunk, (u.Quantity, u.Quantity)):
                pixel0 = cu.convert_point(chunk[0].value, chunk[0].unit,
                                          self.axes_wcs, axis)
                pixel1 = cu.convert_point(chunk[1].value, chunk[1].unit,
                                          self.axes_wcs, axis)
                item[axis] = slice(pixel0, pixel1, None)
            elif cu.iter_isinstance((chunk, int, int)):
                item[axis] = slice(chunk[0], chunk[1], None)
            else:
                raise cu.CubeError(5, "Parameters must be of the same type")
            newdata = self.data[item].sum(axis)
        else:
            unit = chunk.unit if isinstance(chunk, u.Quantity) else None
            pixel = cu.convert_point(chunk, unit, self.axes_wcs, axis)
            item[axis] = pixel
            newdata = self.data[item]
        wcs_indices = [0, 1, 2, 3]
        wcs_indices.remove(3 - axis)
        newwcs = wu.reindex_wcs(self.axes_wcs, np.array(wcs_indices))
        if axis == 2 or axis == 3:
            newwcs = wu.add_celestial_axis(newwcs)
            newwcs.was_augmented = True
        cube = Cube(newdata, newwcs, meta=self.meta, **kwargs)
        return cube
Ejemplo n.º 5
0
 def __getitem__(self, item):
     if item is None or (isinstance(item, tuple) and None in item):
         raise IndexError("None indices not supported")
     if isinstance(item, tuple) and len(item) > 2:
         spectral_slice = item[2]
     else:
         spectral_slice = slice(None, None, None)
     if isinstance(item, int):
         pixels = cu.pixelize_slice(item, self.wcs, _source='other')
     else:
         pixels = cu.pixelize_slice(item[:2], self.wcs, _source='other')
     if cu.iter_isinstance(pixels, (int, int)):
         return self.spectra[pixels][spectral_slice]
     else:
         newspectra = self.spectra[item]
         for i in newspectra.shape[0]:
             for j in newspectra.shape[1]:
                 spec = newspectra[i, j]
                 newspectra[i, j] = spec[spectral_slice]
Ejemplo n.º 6
0
 def __getitem__(self, item):
     if item is None or (isinstance(item, tuple) and None in item):
         raise IndexError("None indices not supported")
     if isinstance(item, tuple) and len(item) > 2:
         spectral_slice = item[2]
     else:
         spectral_slice = slice(None, None, None)
     if isinstance(item, int):
         pixels = cu.pixelize_slice(item, self.wcs, _source='other')
     else:
         pixels = cu.pixelize_slice(item[:2], self.wcs, _source='other')
     if cu.iter_isinstance(pixels, (int, int)):
         return self.spectra[pixels][spectral_slice]
     else:
         newspectra = self.spectra[item]
         for i in newspectra.shape[0]:
             for j in newspectra.shape[1]:
                 spec = newspectra[i, j]
                 newspectra[i, j] = spec[spectral_slice]