<commit_before>from __future__ import absolute_import, division, print_function

import uuid
import numpy as np

from glue.utils import defer_draw

from glue.viewers.image.state import ImageLayerState, ImageSubsetLayerState
from glue.viewers.matplotlib.layer_artist import MatplotlibLayerArtist
from glue.core.exceptions import IncompatibleAttribute
from glue.utils import color2rgb
from glue.core.link_manager import is_equivalent_cid
from glue.core import Data, HubListener
from glue.core.message import ComponentsChangedMessage


class BaseImageLayerArtist(MatplotlibLayerArtist, HubListener):

    def __init__(self, axes, viewer_state, layer_state=None, layer=None):

        super(BaseImageLayerArtist, self).__init__(axes, viewer_state,
                                                   layer_state=layer_state, layer=layer)

        self.reset_cache()

        # Watch for changes in the viewer state which would require the
        # layers to be redrawn
        self._viewer_state.add_global_callback(self._update_image)
        self.state.add_global_callback(self._update_image)

        # TODO: following is temporary
        self.state.data_collection = self._viewer_state.data_collection
        self.data_collection = self._viewer_state.data_collection

        def is_data_object(message):
            if isinstance(self.layer, Data):
                return message.sender is self.layer
            else:
                return message.sender is self.layer.data

        self.data_collection.hub.subscribe(self, ComponentsChangedMessage,
                                           handler=self._update_compatibility,
                                           filter=is_data_object)

        self._update_compatibility()

    def reset_cache(self):
        self._last_viewer_state = {}
        self._last_layer_state = {}

    def _update_image(self, force=False, **kwargs):
        raise NotImplementedError()

    @defer_draw
    def _update_compatibility(self, *args, **kwargs):
        """
        Determine compatibility of data with reference data. For the data to be
        compatible with the reference data, the number of dimensions has to
        match and the pixel component IDs have to be equivalent.
        """

        if self.layer is self._viewer_state.reference_data:
            self._compatible_with_reference_data = True
            self.enable()
            return

        # Check whether the pixel component IDs of the dataset are equivalent
        # to that of the reference dataset. In future this is where we could
        # allow for these to be different and implement reprojection.
        if self.layer.ndim != self._viewer_state.reference_data.ndim:
            self._compatible_with_reference_data = False
            self.disable('Data dimensions do not match reference data')
            return<commit_msg>Make _is_layer_artist into a method otherwise references to class inside function prevents cleanup<commit_after>from __future__ import absolute_import, division, print_function

import uuid
import numpy as np

from glue.utils import defer_draw

from glue.viewers.image.state import ImageLayerState, ImageSubsetLayerState
from glue.viewers.matplotlib.layer_artist import MatplotlibLayerArtist
from glue.core.exceptions import IncompatibleAttribute
from glue.utils import color2rgb
from glue.core.link_manager import is_equivalent_cid
from glue.core import Data, HubListener
from glue.core.message import ComponentsChangedMessage


class BaseImageLayerArtist(MatplotlibLayerArtist, HubListener):

    def __init__(self, axes, viewer_state, layer_state=None, layer=None):

        super(BaseImageLayerArtist, self).__init__(axes, viewer_state,
                                                   layer_state=layer_state, layer=layer)

        self.reset_cache()

        # Watch for changes in the viewer state which would require the
        # layers to be redrawn
        self._viewer_state.add_global_callback(self._update_image)
        self.state.add_global_callback(self._update_image)

        # TODO: following is temporary
        self.state.data_collection = self._viewer_state.data_collection
        self.data_collection = self._viewer_state.data_collection

        self.data_collection.hub.subscribe(self, ComponentsChangedMessage,
                                           handler=self._update_compatibility,
                                           filter=self._is_data_object)

        self._update_compatibility()

    def _is_data_object(self, message):
        if isinstance(self.layer, Data):
            return message.sender is self.layer
        else:
            return message.sender is self.layer.data

    def reset_cache(self):
        self._last_viewer_state = {}
        self._last_layer_state = {}

    def _update_image(self, force=False, **kwargs):
        raise NotImplementedError()

    @defer_draw
    def _update_compatibility(self, *args, **kwargs):
        """
        Determine compatibility of data with reference data. For the data to be
        compatible with the reference data, the number of dimensions has to
        match and the pixel component IDs have to be equivalent.
        """

        if self.layer is self._viewer_state.reference_data:
            self._compatible_with_reference_data = True
            self.enable()
            return

        # Check whether the pixel component IDs of the dataset are equivalent
        # to that of the reference dataset. In future this is where we could
        # allow for these to be different and implement reprojection.
        if self.layer.ndim != self._viewer_state.reference_data.ndim:
            self._compatible_with_reference_data = False
            self.disable('Data dimensions do not match reference data')
            return
{'fraction_non_alphanumeric': 0.05827586206896552, 'fraction_numerical': 0.0003448275862068965, 'mean_word_length': 2.7067092651757187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4642846', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1421, 'n_words': 426}