Esempio n. 1
0
class ImageSampler(param.Parameterized):
    """
    A class of objects that, when called, sample an image.
    """
    __abstract = True

    def _get_image(self):
        # CB: In general, might need to consider caching to avoid
        # loading of image/creation of scs and application of wpofs
        # every time/whatever the sampler does to set up the image
        # before sampling
        return self._image

    def _set_image(self, image):
        self._image = image

    def _del_image(self):
        del self._image

    # As noted by JP in FastImageSampler, this isn't easy to figure out.
    def __call__(self,
                 image,
                 x,
                 y,
                 sheet_xdensity,
                 sheet_ydensity,
                 width=1.0,
                 height=1.0):
        raise NotImplementedError

    image = overridable_property(_get_image, _set_image, _del_image)
Esempio n. 2
0
class SheetMask(param.Parameterized):
    """
    An abstract class that defines a mask over a ProjectionSheet object.

    This class is typically used for optimization, where mask
    indicates which neurons are active and should be processed
    further. A mask can also be used for lesion experiments, to
    specify which units should be kept inactive.

    See the code for CFProjection and CFResponseFn to see how this
    class can be used to restrict the computation to only those
    neurons that the Mask lists as active.
    """

    # JPALERT: Is there anything about this class that assumes its
    # sheet is a ProjectionSheet?

    def _get_data(self):
        assert (self._sheet != None)
        return self._data

    def _set_data(self, data):
        assert (self._sheet != None)
        self._data = data

    data = overridable_property(_get_data,
                                _set_data,
                                doc="""
    Ensure that whenever somebody accesses the data they are not None.""")

    def _get_sheet(self):
        assert (self._sheet != None)
        return self._sheet

    def _set_sheet(self, sheet):
        self._sheet = sheet
        if (self._sheet != None): self.reset()

    sheet = overridable_property(_get_sheet, _set_sheet)

    def __init__(self, sheet=None, **params):
        super(SheetMask, self).__init__(**params)
        self.sheet = sheet

    def __and__(self, mask):
        return AndMask(self._sheet, submasks=[self, mask])

    def __or__(self, mask):
        return OrMask(self._sheet, submasks=[self, mask])

    # JABALERT: Shouldn't this just keep one matrix around and zero it out,
    # instead of allocating a new one each time?
    def reset(self):
        """Initialize mask to default value (with no neurons masked out)."""
        self.data = ones(self.sheet.shape)

    def calculate(self):
        """
        Calculate a new mask based on the activity of the sheet.

        For instance, in an algorithm like LISSOM that is based on a
        process of feedforward activation followed by lateral settling,
        the calculation is done at the beginning of each iteration after
        the feedforward activity has been calculated.

        Subclasses should override this method to compute some non-default
        mask.
        """
        pass

    # JABALERT: Not clear what the user should do with this.
    def update(self):
        """
        Update the current mask based on the current activity and a previous mask.

        Should be called only if calculate() has already been called since the last
        reset(); potentially faster to compute than redoing the entire calculate().

        Subclasses should override this method to compute some non-default
        mask.
        """
        pass