<commit_before>"""
Provides Dimension objects for tracking the properties of a value,
axis or map dimension. Also supplies the Dimensioned abstract
baseclass for classes that accept Dimension values.
"""

import numpy as np

import param


class Dimension(param.Parameterized):

    cyclic = param.Boolean(default=False, doc="""
        Whether the range of this feature is cyclic (wraps around at the high
        end).""")

    name = param.String(default="", doc="Name of the Dimension.")

    range = param.Tuple(default=(None, None), doc="""
        Lower and upper values for a Dimension.""")

    type = param.Parameter(default=None, doc="""
        Type associated with Dimension values.""")

    unit = param.String(default=None, doc="Unit string associated with"
                                          "the Dimension.")

    values = param.List(default=[], doc="""
        Values may optionally be specified to specify valid
        dimension values and/or retain a categorical ordering.""")

    format_string = param.String(default="{name}: {val}{unit}")

    def __init__(self, name, **params):
        """
        Initializes the Dimension object with a name.
        """
        if isinstance(name, Dimension):
            existing_params = dict(name.get_param_values())
        else:
            existing_params = {'name': name}
        super(Dimension, self).__init__(**dict(existing_params, **params))


    def __call__(self, name=None, **params):
        settings = dict(self.get_param_values(onlychanged=True), **params)
        if name is not None: settings['name'] = name
        return self.__class__(**settings)


    @property
    def pprint_label(self):
        unit = '' if self.unit is None else ' (%s)' % self.unit
        return self.name + unit


    def pprint_value(self, value, rounding=2):
        """
        Pretty prints the dimension name and value with the format_string
        parameter and if supplied adds the unit string parameter.
        """

        unit = '' if self.unit is None else ' ' + self.unit
        try: # Try formatting numeric types as floats with rounding
            val = round(float(value), rounding)
        except:
            val = value

        return self.format_string.format(name=self.name.capitalize(),
                                         val=val, unit=unit)

    def __hash__(self):
        return sum([hash(value) for name, value in self.get_param_values()])

    def __str__(self):
        return self.pprint_label

    def __eq__(self, other):
        "Dimensions are sorted alphanumerically by name"
        return self.name == other.name if isinstance(other, Dimension) else other

    def __lt__(self, other):
        "Dimensions are sorted alphanumerically by name"
        return self.name < other.name if isinstance(other, Dimension) else other



class Dimensioned(param.Parameterized):
    """
    Abstract baseclass implementing common methods for objects with
    associated dimensions. Dimensioned support three dimension groups:

    * index_dimensions: The Dimensioned objects should implement
                        indexing and slicing for these dimensions.

    * value_dimensions: These dimensions correspond to any data
                        held on the Dimensioned object.

    * deep_dimensions:  These are dynamically computed and
                        correspond to any dimensions on items
                        held on this object. Objects that support
                        this should enable the _deep_indexable
                        flag.

    Dimensioned also provides convenient methods to find the
    range and type of values along a particular Dimension.
    For ranges to work appropriately subclasses should define
    dimension_values methods, which return an array of all the
    values along the supplied dimension.
    """

    index_dimensions = param.List(bounds=(0, None), constant=True, doc="""
       The dimensions the values are indexed by.""")

    label = param.String(default='', doc="""
       Optional label describing the data, e.g. where or how it
       was measured.""")

    value = param.String(default='Dimensioned', doc="""
       A string describing what the data of the object contain.
       By default this should mirror the class name.""")

    value_dimensions = param.List(bounds=(0, None), constant=True, doc="""
       The dimensions the values are indexed by. Subclasses should
       restrict bounds to appropriate number of dimensions.""")

    __abstract = True

    _deep_indexable = False
    _sorted = False
    _dim_groups = ['index_dimensions',
                   'value_dimensions',
                   'deep_dimensions']

    def __init__(self, **params):
        for group in self._dim_groups[0:2]:
            if group in params:
                dimensions = [Dimension(d) if not isinstance(d, Dimension) else d
                              for d in params.pop(group)]
                params[group] = dimensions
        super(Dimensioned, self).__init__(**params)
        self.ndims = len(self.index_dimensions)
        self._cached_index_names = [d.name for d in self.index_dimensions]
        self._cached_value_names = [d.name for d in self.value_dimensions]
<commit_msg>Declaring label and value on Dimensioned as constant parameters<commit_after>"""
Provides Dimension objects for tracking the properties of a value,
axis or map dimension. Also supplies the Dimensioned abstract
baseclass for classes that accept Dimension values.
"""

import numpy as np

import param


class Dimension(param.Parameterized):

    cyclic = param.Boolean(default=False, doc="""
        Whether the range of this feature is cyclic (wraps around at the high
        end).""")

    name = param.String(default="", doc="Name of the Dimension.")

    range = param.Tuple(default=(None, None), doc="""
        Lower and upper values for a Dimension.""")

    type = param.Parameter(default=None, doc="""
        Type associated with Dimension values.""")

    unit = param.String(default=None, doc="Unit string associated with"
                                          "the Dimension.")

    values = param.List(default=[], doc="""
        Values may optionally be specified to specify valid
        dimension values and/or retain a categorical ordering.""")

    format_string = param.String(default="{name}: {val}{unit}")

    def __init__(self, name, **params):
        """
        Initializes the Dimension object with a name.
        """
        if isinstance(name, Dimension):
            existing_params = dict(name.get_param_values())
        else:
            existing_params = {'name': name}
        super(Dimension, self).__init__(**dict(existing_params, **params))


    def __call__(self, name=None, **params):
        settings = dict(self.get_param_values(onlychanged=True), **params)
        if name is not None: settings['name'] = name
        return self.__class__(**settings)


    @property
    def pprint_label(self):
        unit = '' if self.unit is None else ' (%s)' % self.unit
        return self.name + unit


    def pprint_value(self, value, rounding=2):
        """
        Pretty prints the dimension name and value with the format_string
        parameter and if supplied adds the unit string parameter.
        """

        unit = '' if self.unit is None else ' ' + self.unit
        try: # Try formatting numeric types as floats with rounding
            val = round(float(value), rounding)
        except:
            val = value

        return self.format_string.format(name=self.name.capitalize(),
                                         val=val, unit=unit)

    def __hash__(self):
        return sum([hash(value) for name, value in self.get_param_values()])

    def __str__(self):
        return self.pprint_label

    def __eq__(self, other):
        "Dimensions are sorted alphanumerically by name"
        return self.name == other.name if isinstance(other, Dimension) else other

    def __lt__(self, other):
        "Dimensions are sorted alphanumerically by name"
        return self.name < other.name if isinstance(other, Dimension) else other



class Dimensioned(param.Parameterized):
    """
    Abstract baseclass implementing common methods for objects with
    associated dimensions. Dimensioned support three dimension groups:

    * index_dimensions: The Dimensioned objects should implement
                        indexing and slicing for these dimensions.

    * value_dimensions: These dimensions correspond to any data
                        held on the Dimensioned object.

    * deep_dimensions:  These are dynamically computed and
                        correspond to any dimensions on items
                        held on this object. Objects that support
                        this should enable the _deep_indexable
                        flag.

    Dimensioned also provides convenient methods to find the
    range and type of values along a particular Dimension.
    For ranges to work appropriately subclasses should define
    dimension_values methods, which return an array of all the
    values along the supplied dimension.
    """

    index_dimensions = param.List(bounds=(0, None), constant=True, doc="""
       The dimensions the values are indexed by.""")

    label = param.String(default='', constant=True, doc="""
       Optional label describing the data, e.g. where or how it
       was measured.""")

    value = param.String(default='Dimensioned', constant=True, doc="""
       A string describing what the data of the object contain.
       By default this should mirror the class name.""")

    value_dimensions = param.List(bounds=(0, None), constant=True, doc="""
       The dimensions the values are indexed by. Subclasses should
       restrict bounds to appropriate number of dimensions.""")

    __abstract = True

    _deep_indexable = False
    _sorted = False
    _dim_groups = ['index_dimensions',
                   'value_dimensions',
                   'deep_dimensions']

    def __init__(self, **params):
        for group in self._dim_groups[0:2]:
            if group in params:
                dimensions = [Dimension(d) if not isinstance(d, Dimension) else d
                              for d in params.pop(group)]
                params[group] = dimensions
        super(Dimensioned, self).__init__(**params)
        self.ndims = len(self.index_dimensions)
        self._cached_index_names = [d.name for d in self.index_dimensions]
        self._cached_value_names = [d.name for d in self.value_dimensions]

{'fraction_non_alphanumeric': 0.08712908712908712, 'fraction_numerical': 0.000945000945000945, 'mean_word_length': 2.4938923737207, 'pattern_counts': {'":': 0, '<': 5, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7559665', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2609, 'n_words': 970}