<commit_before>"""
A plot to visualize trees
"""

from __future__ import absolute_import, division, print_function

import numpy as np

from glue.core.edit_subset_mode import EditSubsetMode
from glue.core.state import lookup_class_with_patches
from glue.core.subset import CategorySubsetState
from glue.core.roi import PointROI
from glue.core.callback_property import CallbackProperty, add_callback, delay_callback
from glue.core.data import IncompatibleAttribute, Data
from glue.viewers.common.viz_client import GenericMplClient
from glue.plugins.dendro_viewer.layer_artist import DendroLayerArtist
from glue.utils import nonpartial


class DendroClient(GenericMplClient):

    height_attr = CallbackProperty()
    parent_attr = CallbackProperty()
    order_attr = CallbackProperty()
    ylog = CallbackProperty(False)
    display_data = CallbackProperty(None)
    select_substruct = CallbackProperty(True)

    def __init__(self, *args, **kwargs):
        super(DendroClient, self).__init__(*args, **kwargs)
        self._layout = None
        self.axes.set_xticks([])
        self.axes.spines['top'].set_visible(False)
        self.axes.spines['bottom'].set_visible(False)

    def _connect(self):
        add_callback(self, 'ylog', self._set_ylog)
        add_callback(self, 'height_attr', nonpartial(self._relayout))
        add_callback(self, 'order_attr', nonpartial(self._relayout))
        add_callback(self, 'parent_attr', nonpartial(self._relayout))

    def _default_attributes(self):
        assert self.display_data is not None

        fallback = self.display_data.components[0]
        with delay_callback(self, 'height_attr', 'parent_attr',
                            'order_attr'):

            if self.height_attr is None:
                comp = self.display_data.find_component_id('height') or fallback
                self.height_attr = comp

            if self.parent_attr is None:
                comp = self.display_data.find_component_id('parent') or fallback
                self.parent_attr = comp

            if self.order_attr is None:
                self.order_attr = self.height_attr

    def new_layer_artist(self, layer):
        return DendroLayerArtist(layer, self.axes)

    def _set_ylog(self, log):
        self.axes.set_yscale('log' if log else 'linear')
        self._redraw()

    def _relayout(self):

        if self.display_data is None:<commit_msg>Fix failure in dendrogram, which assumed the first component was the pixel component (which is not safe to assume)<commit_after>"""
A plot to visualize trees
"""

from __future__ import absolute_import, division, print_function

import numpy as np

from glue.core.edit_subset_mode import EditSubsetMode
from glue.core.state import lookup_class_with_patches
from glue.core.subset import CategorySubsetState
from glue.core.roi import PointROI
from glue.core.callback_property import CallbackProperty, add_callback, delay_callback
from glue.core.data import IncompatibleAttribute, Data
from glue.viewers.common.viz_client import GenericMplClient
from glue.plugins.dendro_viewer.layer_artist import DendroLayerArtist
from glue.utils import nonpartial


class DendroClient(GenericMplClient):

    height_attr = CallbackProperty()
    parent_attr = CallbackProperty()
    order_attr = CallbackProperty()
    ylog = CallbackProperty(False)
    display_data = CallbackProperty(None)
    select_substruct = CallbackProperty(True)

    def __init__(self, *args, **kwargs):
        super(DendroClient, self).__init__(*args, **kwargs)
        self._layout = None
        self.axes.set_xticks([])
        self.axes.spines['top'].set_visible(False)
        self.axes.spines['bottom'].set_visible(False)

    def _connect(self):
        add_callback(self, 'ylog', self._set_ylog)
        add_callback(self, 'height_attr', nonpartial(self._relayout))
        add_callback(self, 'order_attr', nonpartial(self._relayout))
        add_callback(self, 'parent_attr', nonpartial(self._relayout))

    def _default_attributes(self):

        assert self.display_data is not None

        fallback = self.display_data.pixel_component_ids[0]

        with delay_callback(self, 'height_attr', 'parent_attr',
                            'order_attr'):

            if self.height_attr is None:
                comp = self.display_data.find_component_id('height') or fallback
                self.height_attr = comp

            if self.parent_attr is None:
                comp = self.display_data.find_component_id('parent') or fallback
                self.parent_attr = comp

            if self.order_attr is None:
                self.order_attr = self.height_attr

    def new_layer_artist(self, layer):
        return DendroLayerArtist(layer, self.axes)

    def _set_ylog(self, log):
        self.axes.set_yscale('log' if log else 'linear')
        self._redraw()

    def _relayout(self):

        if self.display_data is None:
{'fraction_non_alphanumeric': 0.08662861801875255, 'fraction_numerical': 0.00040766408479412964, 'mean_word_length': 3.3157431838170623, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3381641', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1414, 'n_words': 329}