Beispiel #1
0
    def to_mask(self, data, view=None):

        if view is None:
            view = Ellipsis
        elif isinstance(view, slice) or np.isscalar(view):
            view = [view]

        # Figure out the shape of the final mask given the requested view
        shape = view_shape(data.shape, view)

        if data is self.reference_data:

            slices = self.slices

        else:

            # Check if we can transform list of slices to match this dataset
            order = data.pixel_aligned_data.get(self.reference_data, None)

            if order is None:
                # We use broadcast_to for minimal memory usage
                return broadcast_to(False, shape)
            else:
                # Reorder slices
                slices = [self.slices[idx] for idx in order]

        if (isinstance(view, np.ndarray) or
                (isinstance(view, (tuple, list)) and isinstance(view[0], np.ndarray))):
            mask = np.zeros(data.shape, dtype=bool)
            mask[slices] = True
            return mask[view]

        # The original slices assume the full array, not the array with the view
        # applied, so we need to now adjust the slices accordingly.
        if view is Ellipsis:
            subslices = slices
        else:
            subslices = []
            for i in range(data.ndim):
                if i >= len(view):
                    subslices.append(slices[i])
                elif np.isscalar(view[i]):
                    beg, end, stp = slices[i].indices(data.shape[i])
                    if view[i] < beg or view[i] >= end or (view[i] - beg) % stp != 0:
                        return broadcast_to(False, shape)
                elif isinstance(view[i], slice):
                    if view[i].step is not None and view[i].step < 0:
                        beg, end, step = view[i].indices(data.shape[i])
                        v = slice(end + 1, beg + 1, -step)
                    else:
                        v = view[i]
                    subslices.append(combine_slices(v, slices[i], data.shape[i]))
                else:
                    raise TypeError("Unexpected view item: {0}".format(view[i]))

        # Create mask with final shape
        mask = np.zeros(shape, dtype=bool)
        mask[subslices] = True

        return mask
Beispiel #2
0
    def to_mask(self, data, view=None):

        if view is None:
            view = Ellipsis
        elif isinstance(view, slice) or np.isscalar(view):
            view = [view]

        # Figure out the shape of the final mask given the requested view
        shape = view_shape(data.shape, view)

        if data is self.reference_data:

            slices = self.slices

        else:

            # Check if we can transform list of slices to match this dataset
            order = data.pixel_aligned_data.get(self.reference_data, None)

            if order is None:
                # We use broadcast_to for minimal memory usage
                return broadcast_to(False, shape)
            else:
                # Reorder slices
                slices = [self.slices[idx] for idx in order]

        if (isinstance(view, np.ndarray) or
                (isinstance(view, (tuple, list)) and isinstance(view[0], np.ndarray))):
            mask = np.zeros(data.shape, dtype=bool)
            mask[slices] = True
            return mask[view]

        # The original slices assume the full array, not the array with the view
        # applied, so we need to now adjust the slices accordingly.
        if view is Ellipsis:
            subslices = slices
        else:
            subslices = []
            for i in range(data.ndim):
                if i >= len(view):
                    subslices.append(slices[i])
                elif np.isscalar(view[i]):
                    beg, end, stp = slices[i].indices(data.shape[i])
                    if view[i] < beg or view[i] >= end or (view[i] - beg) % stp != 0:
                        return broadcast_to(False, shape)
                elif isinstance(view[i], slice):
                    if view[i].step is not None and view[i].step < 0:
                        beg, end, step = view[i].indices(data.shape[i])
                        v = slice(end + 1, beg + 1, -step)
                    else:
                        v = view[i]
                    subslices.append(combine_slices(v, slices[i], data.shape[i]))
                else:
                    raise TypeError("Unexpected view item: {0}".format(view[i]))

        # Create mask with final shape
        mask = np.zeros(shape, dtype=bool)
        mask[subslices] = True

        return mask
Beispiel #3
0
 def shape(self):
     """
     The shape of the 2D view into the data
     """
     result = view_shape(self.data.shape, self.view)
     if self.transpose:
         result = result[::-1]
     return result
Beispiel #4
0
 def shape(self):
     """
     Shape of the 2D view into the subset mask
     """
     result = view_shape(self.subset.data.shape, self.view)
     if self.transpose:
         result = result[::-1]
     return tuple(list(result) + [4])  # 4th dim is RGBA channels
Beispiel #5
0
 def shape(self):
     """
     The shape of the 2D view into the data
     """
     result = view_shape(self.data.shape, self.view)
     if self.transpose:
         result = result[::-1]
     return result
Beispiel #6
0
 def shape(self):
     """
     Shape of the 2D view into the subset mask
     """
     result = view_shape(self.subset.data.shape, self.view)
     if self.transpose:
         result = result[::-1]
     return tuple(list(result) + [4])  # 4th dim is RGBA channels
Beispiel #7
0
    def get_sliced_data_shape(self, view=None):

        if (self.viewer_state.reference_data is None or
            self.viewer_state.x_att is None or
                self.viewer_state.y_att is None):
            return None

        x_axis = self.viewer_state.x_att.axis
        y_axis = self.viewer_state.y_att.axis

        shape = self.viewer_state.reference_data.shape
        shape_slice = shape[y_axis], shape[x_axis]

        if view is None:
            return shape_slice
        else:
            return view_shape(shape_slice, view)
Beispiel #8
0
    def get_sliced_data_shape(self, view=None):

        if (self.viewer_state.reference_data is None
                or self.viewer_state.x_att is None
                or self.viewer_state.y_att is None):
            return None

        x_axis = self.viewer_state.x_att.axis
        y_axis = self.viewer_state.y_att.axis

        shape = self.viewer_state.reference_data.shape
        shape_slice = shape[y_axis], shape[x_axis]

        if view is None:
            return shape_slice
        else:
            return view_shape(shape_slice, view)
Beispiel #9
0
    def __getitem__(self, key):
        """ Shortcut syntax to access the numerical data in a component.
        Equivalent to:

        ``component = data.get_component(component_id).data``

        :param key:
          The component to fetch data from

        :type key: :class:`~glue.core.component_id.ComponentID`

        :returns: :class:`~numpy.ndarray`
        """

        key, view = split_component_view(key)
        if isinstance(key, six.string_types):
            _k = key
            key = self.find_component_id(key)
            if key is None:
                raise IncompatibleAttribute(_k)

        if isinstance(key, ComponentLink):
            return key.compute(self, view)

        if key in self._components:
            comp = self._components[key]
        elif key in self._externally_derivable_components:
            comp = self._externally_derivable_components[key]
        else:
            raise IncompatibleAttribute(key)

        shp = view_shape(self.shape, view)

        if view is not None:
            result = comp[view]
        else:
            if comp.categorical:
                result = comp.codes
            else:
                result = comp.data

        assert result.shape == shp, \
            "Component view returned bad shape: %s %s" % (result.shape, shp)
        return result
Beispiel #10
0
        def calc_random(view):
            import dask.array as da
            import numpy as np
            from glue.utils import view_shape
            # x = da.from_zarr('/mnt/cephfs/smltar_numpyarr/zarr_data_full')
            # print(x)
            # y = x[0:30]
            # z = x[100:130]
            # m = x[1000:1030]
            # n = x[1500:1530]
            # p = x[1700:1730]

            # sum = (y + z - m + p) * n
            # print(sum)
            # fu = client.compute(sum)
            # #print(r.result())
            # re = fu.result()
            result = np.random.random(view_shape((4096, 4096, 4096), view))
            return result
Beispiel #11
0
    def __getitem__(self, key):
        """ Shortcut syntax to access the numerical data in a component.
        Equivalent to:

        ``component = data.get_component(component_id).data``

        :param key:
          The component to fetch data from

        :type key: :class:`~glue.core.component_id.ComponentID`

        :returns: :class:`~numpy.ndarray`
        """

        key, view = split_component_view(key)
        if isinstance(key, six.string_types):
            _k = key
            key = self.find_component_id(key)
            if key is None:
                raise IncompatibleAttribute(_k)

        if isinstance(key, ComponentLink):
            return key.compute(self, view)

        try:
            comp = self._components[key]
        except KeyError:
            raise IncompatibleAttribute(key)

        shp = view_shape(self.shape, view)

        if view is not None:
            result = comp[view]
        else:
            if comp.categorical:
                result = comp.codes
            else:
                result = comp.data

        assert result.shape == shp, \
            "Component view returned bad shape: %s %s" % (result.shape, shp)
        return result
Beispiel #12
0
 def to_mask(self, data, view=None):
     shp = view_shape(data.shape, view)
     return broadcast_to(False, shp)
Beispiel #13
0
    def __getitem__(self, view):

        img = None
        visible_layers = 0

        for uuid in sorted(self.layers, key=lambda x: self.layers[x]['zorder']):

            layer = self.layers[uuid]

            if not layer['visible']:
                continue

            interval = ManualInterval(*layer['clim'])
            contrast_bias = ContrastBiasStretch(layer['contrast'], layer['bias'])

            if callable(layer['array']):
                array = layer['array'](view=view)
            else:
                array = layer['array']

            if array is None:
                continue

            if not callable(layer['array']):
                array = array[view]

            if np.isscalar(array):
                scalar = True
                array = np.atleast_2d(array)
            else:
                scalar = False

            data = STRETCHES[layer['stretch']]()(contrast_bias(interval(array)))

            if isinstance(layer['color'], Colormap):

                if img is None:
                    img = np.ones(data.shape + (4,))

                # Compute colormapped image
                plane = layer['color'](data)

                alpha_plane = layer['alpha'] * plane[:, :, 3]

                # Use traditional alpha compositing
                plane[:, :, 0] = plane[:, :, 0] * alpha_plane
                plane[:, :, 1] = plane[:, :, 1] * alpha_plane
                plane[:, :, 2] = plane[:, :, 2] * alpha_plane

                img[:, :, 0] *= (1 - alpha_plane)
                img[:, :, 1] *= (1 - alpha_plane)
                img[:, :, 2] *= (1 - alpha_plane)
                img[:, :, 3] = 1

            else:

                if img is None:
                    img = np.zeros(data.shape + (4,))

                # Get color and pre-multiply by alpha values
                color = COLOR_CONVERTER.to_rgba_array(layer['color'])[0]
                color *= layer['alpha']

                # We should treat NaN values as zero (post-stretch), which means
                # that those pixels don't contribute towards the final image.
                reset = np.isnan(data)
                if np.any(reset):
                    data[reset] = 0.

                plane = data[:, :, np.newaxis] * color
                plane[:, :, 3] = 1

                visible_layers += 1

            if scalar:
                plane = plane[0, 0]

            img += plane

        if img is None:
            if self.shape is None:
                return None
            else:
                img = np.zeros(view_shape(self.shape, view) + (4,))
        else:
            img = np.clip(img, 0, 1)

        return img
Beispiel #14
0
 def to_mask(self, data, view=None):
     shp = view_shape(data.shape, view)
     return np.zeros(shp, dtype=bool)
Beispiel #15
0
 def to_mask(self, data, view=None):
     shp = view_shape(data.shape, view)
     return np.zeros(shp, dtype=bool)
Beispiel #16
0
 def get_data(self, cid, view=None):
     if cid in self.pixel_component_ids:
         return super(RandomData, self).get_data(cid, view=view)
     else:
         return np.random.random(view_shape(self.shape, view))
Beispiel #17
0
def calc_random(view):
    import numpy as np
    from glue.utils import view_shape
    return np.random.random(view_shape((512, 512, 512), view))
Beispiel #18
0
    def __getitem__(self, view):

        img = None
        visible_layers = 0

        for uuid in sorted(self.layers,
                           key=lambda x: self.layers[x]['zorder']):

            layer = self.layers[uuid]

            if not layer['visible']:
                continue

            interval = ManualInterval(*layer['clim'])
            contrast_bias = ContrastBiasStretch(layer['contrast'],
                                                layer['bias'])

            if callable(layer['array']):
                array = layer['array'](view=view)
            else:
                array = layer['array']

            if array is None:
                continue

            if not callable(layer['array']):
                array = array[view]

            if np.isscalar(array):
                scalar = True
                array = np.atleast_2d(array)
            else:
                scalar = False

            data = STRETCHES[layer['stretch']]()(contrast_bias(
                interval(array)))

            if isinstance(layer['color'], Colormap):

                if img is None:
                    img = np.ones(data.shape + (4, ))

                # Compute colormapped image
                plane = layer['color'](data)

                alpha_plane = layer['alpha'] * plane[:, :, 3]

                # Use traditional alpha compositing
                plane[:, :, 0] = plane[:, :, 0] * alpha_plane
                plane[:, :, 1] = plane[:, :, 1] * alpha_plane
                plane[:, :, 2] = plane[:, :, 2] * alpha_plane

                img[:, :, 0] *= (1 - alpha_plane)
                img[:, :, 1] *= (1 - alpha_plane)
                img[:, :, 2] *= (1 - alpha_plane)
                img[:, :, 3] = 1

            else:

                if img is None:
                    img = np.zeros(data.shape + (4, ))

                # Get color and pre-multiply by alpha values
                color = COLOR_CONVERTER.to_rgba_array(layer['color'])[0]
                color *= layer['alpha']

                # We should treat NaN values as zero (post-stretch), which means
                # that those pixels don't contribute towards the final image.
                reset = np.isnan(data)
                if np.any(reset):
                    data[reset] = 0.

                plane = data[:, :, np.newaxis] * color
                plane[:, :, 3] = 1

                visible_layers += 1

            if scalar:
                plane = plane[0, 0]

            img += plane

        if img is None:
            if self.shape is None:
                return None
            else:
                img = np.zeros(view_shape(self.shape, view) + (4, ))
        else:
            img = np.clip(img, 0, 1)

        return img
Beispiel #19
0
 def get_data(self, cid, view=None):
     if cid in self.pixel_component_ids:
         return super(RandomData, self).get_data(cid, view=view)
     else:
         return np.random.random(view_shape(self.shape, view))
Beispiel #20
0
 def get_data(self, cid, view=None):
     print(cid)
     return np.random.random(view_shape(self.shape, view))