<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.
"""
from __future__ import unicode_literals
import re
import datetime as dt
from operator import itemgetter

import numpy as np
import param

from ..core.util import (basestring, sanitize_identifier,
                         group_sanitizer, label_sanitizer, max_range,
                         find_range, dimension_sanitizer, OrderedDict,
                         bytes_to_unicode, unicode, dt64_to_dt, unique_array)
from .options import Store, StoreOptions
from .pprint import PrettyPrinter


# Alias parameter support for pickle loading

ALIASES = {'key_dimensions': 'kdims', 'value_dimensions': 'vdims',
           'constant_dimensions': 'cdims'}

title_format = "{name}: {val}{unit}"

def param_aliases(d):
    """
    Called from __setstate__ in LabelledData in order to load
    old pickles with outdated parameter names.

    Warning: We want to keep pickle hacking to a minimum!
    """
    for old, new in ALIASES.items():
        old_param = '_%s_param_value' % old
        new_param = '_%s_param_value' % new
        if old_param in d:
            d[new_param] = d.pop(old_param)
    return d


def replace_dimensions(dimensions, overrides):
    """
    Replaces dimensions in a list with a dictionary of overrides.
    Overrides should be indexed by the dimension name with values that
    is either a Dimension object, a string name or a dictionary
    specifying the dimension parameters to override.
    """
    replaced = []
    for d in dimensions:
        if d.name in overrides:
            override = overrides[d.name]
        elif d.label in overrides:
            override = overrides[d.label]
        else:
            override = None

        if override is None:
            replaced.append(d)
        elif isinstance(override, (basestring, tuple)):
            replaced.append(d(override))
        elif isinstance(override, Dimension):
            replaced.append(override)
        elif isinstance(override, dict):
            replaced.append(d(**override))
        else:
            raise ValueError('Dimension can only be overridden '
                             'with another dimension or a dictionary '
                             'of attributes')
    return replaced


class Dimension(param.Parameterized):
    """
    Dimension objects are used to specify some important general
    features that may be associated with a collection of values.

    For instance, a Dimension may specify that a set of numeric values
    actually correspond to 'Height' (dimension name), in units of
    meters, and that allowed values must be floats greater than zero.

    In addition, Dimensions can be declared as cyclic, support
    categorical data using a finite set of allowed, ordered values and
    support a custom, pretty-printed representation.
    """

    name = param.String(doc="""
       Short name associated with the Dimension, such as 'height' or
       'weight'. Valid Python identifiers make good names, because they
       can be used conveniently as a keyword in many contexts.""")

    cyclic = param.Boolean(default=False, doc="""
        Whether the range of this feature is cyclic such that the
        maximum allowed value (defined by the range parameter) is
        continuous with the minimum allowed value.""")

    value_format = param.Callable(default=None, doc="""
        Formatting function applied to each value before display.""")

    range = param.Tuple(default=(None, None), doc="""
        Specifies the minimum and maximum allowed values for a
        Dimension. None is used to represent an unlimited bound.""")

    soft_range = param.Tuple(default=(None, None), doc="""
        Specifies a minimum and maximum reference value, which
        may be overridden by the data.""")

    type = param.Parameter(default=None, doc="""
        Optional type associated with the Dimension values. The type
        may be an inbuilt constructor (such as int, str, float) or a
        custom class object.""")

    unit = param.String(default=None, allow_None=True, doc="""
        Optional unit string associated with the Dimension. For
        instance, the string 'm' may be used represent units of meters
        and 's' to represent units of seconds.""")

    values = param.List(default=[], doc="""
        Optional specification of the allowed value set for the
        dimension that may also be used to retain a categorical
        ordering.""")

    # Defines default formatting by type
    type_formatters = {}
    unit_format = ' ({unit})'
    presets = {} # A dictionary-like mapping name, (name,) or
                 # (name, unit) to a preset Dimension object

    def __init__(self, name, **params):
        """
        Initializes the Dimension object with the given name.
        """
        if isinstance(name, Dimension):
            existing_params = dict(name.get_param_values())
        elif (name, params.get('unit', None)) in self.presets.keys():
            preset = self.presets[(str(name), str(params['unit']))]
            existing_params = dict(preset.get_param_values())
        elif name in self.presets.keys():
            existing_params = dict(self.presets[str(name)].get_param_values())
        elif (name,) in self.presets.keys():
            existing_params = dict(self.presets[(str(name),)].get_param_values())
        else:
            existing_params = {'name': name}

        all_params = dict(existing_params, **params)
        name = all_params['name']
        label = name
        if isinstance(name, tuple):
            name, label = name
            all_params['name'] = name
        self.label = label

        values = params.get('values', [])
        if isinstance(values, basestring) and values == 'initial':
            self.warning("The 'initial' string for dimension values is no longer supported.")
            values = []

        all_params['values'] = sorted(list(unique_array(values)))
        super(Dimension, self).__init__(**all_params)


    def __call__(self, name=None, **overrides):
        """
        Derive a new Dimension that inherits existing parameters
        except for the supplied, explicit overrides
        """
        settings = dict(self.get_param_values(onlychanged=True), **overrides)
        if name is not None: settings['name'] = name
        return self.__class__(**settings)


    @property<commit_msg>Declared Dimension.label as a parameter<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.
"""
from __future__ import unicode_literals
import re
import datetime as dt
from operator import itemgetter

import numpy as np
import param

from ..core.util import (basestring, sanitize_identifier,
                         group_sanitizer, label_sanitizer, max_range,
                         find_range, dimension_sanitizer, OrderedDict,
                         bytes_to_unicode, unicode, dt64_to_dt, unique_array)
from .options import Store, StoreOptions
from .pprint import PrettyPrinter


# Alias parameter support for pickle loading

ALIASES = {'key_dimensions': 'kdims', 'value_dimensions': 'vdims',
           'constant_dimensions': 'cdims'}

title_format = "{name}: {val}{unit}"

def param_aliases(d):
    """
    Called from __setstate__ in LabelledData in order to load
    old pickles with outdated parameter names.

    Warning: We want to keep pickle hacking to a minimum!
    """
    for old, new in ALIASES.items():
        old_param = '_%s_param_value' % old
        new_param = '_%s_param_value' % new
        if old_param in d:
            d[new_param] = d.pop(old_param)
    return d


def replace_dimensions(dimensions, overrides):
    """
    Replaces dimensions in a list with a dictionary of overrides.
    Overrides should be indexed by the dimension name with values that
    is either a Dimension object, a string name or a dictionary
    specifying the dimension parameters to override.
    """
    replaced = []
    for d in dimensions:
        if d.name in overrides:
            override = overrides[d.name]
        elif d.label in overrides:
            override = overrides[d.label]
        else:
            override = None

        if override is None:
            replaced.append(d)
        elif isinstance(override, (basestring, tuple)):
            replaced.append(d(override))
        elif isinstance(override, Dimension):
            replaced.append(override)
        elif isinstance(override, dict):
            replaced.append(d(**override))
        else:
            raise ValueError('Dimension can only be overridden '
                             'with another dimension or a dictionary '
                             'of attributes')
    return replaced


class Dimension(param.Parameterized):
    """
    Dimension objects are used to specify some important general
    features that may be associated with a collection of values.

    For instance, a Dimension may specify that a set of numeric values
    actually correspond to 'Height' (dimension name), in units of
    meters, and that allowed values must be floats greater than zero.

    In addition, Dimensions can be declared as cyclic, support
    categorical data using a finite set of allowed, ordered values and
    support a custom, pretty-printed representation.
    """

    name = param.String(doc="""
       Short name associated with the Dimension, such as 'height' or
       'weight'. Valid Python identifiers make good names, because they
       can be used conveniently as a keyword in many contexts.""")

    label = param.String(doc="""
        Unrestricted label used to describe the dimension. A label
        should succinctly describe the dimension and may contain any
        characters, including Unicode and LaTeX expression.""")

    cyclic = param.Boolean(default=False, doc="""
        Whether the range of this feature is cyclic such that the
        maximum allowed value (defined by the range parameter) is
        continuous with the minimum allowed value.""")

    value_format = param.Callable(default=None, doc="""
        Formatting function applied to each value before display.""")

    range = param.Tuple(default=(None, None), doc="""
        Specifies the minimum and maximum allowed values for a
        Dimension. None is used to represent an unlimited bound.""")

    soft_range = param.Tuple(default=(None, None), doc="""
        Specifies a minimum and maximum reference value, which
        may be overridden by the data.""")

    type = param.Parameter(default=None, doc="""
        Optional type associated with the Dimension values. The type
        may be an inbuilt constructor (such as int, str, float) or a
        custom class object.""")

    unit = param.String(default=None, allow_None=True, doc="""
        Optional unit string associated with the Dimension. For
        instance, the string 'm' may be used represent units of meters
        and 's' to represent units of seconds.""")

    values = param.List(default=[], doc="""
        Optional specification of the allowed value set for the
        dimension that may also be used to retain a categorical
        ordering.""")

    # Defines default formatting by type
    type_formatters = {}
    unit_format = ' ({unit})'
    presets = {} # A dictionary-like mapping name, (name,) or
                 # (name, unit) to a preset Dimension object

    def __init__(self, name, **params):
        """
        Initializes the Dimension object with the given name.
        """
        if isinstance(name, Dimension):
            existing_params = dict(name.get_param_values())
        elif (name, params.get('unit', None)) in self.presets.keys():
            preset = self.presets[(str(name), str(params['unit']))]
            existing_params = dict(preset.get_param_values())
        elif name in self.presets.keys():
            existing_params = dict(self.presets[str(name)].get_param_values())
        elif (name,) in self.presets.keys():
            existing_params = dict(self.presets[(str(name),)].get_param_values())
        else:
            existing_params = {'name': name}

        all_params = dict(existing_params, **params)
        name = all_params['name']
        label = name
        if isinstance(name, tuple):
            name, label = name
            all_params['name'] = name
            if 'label' in params:
                self.warning('Using label as supplied by keyword, ignoring tuple format.')

        all_params['label'] = params.get('label', label)

        values = params.get('values', [])
        if isinstance(values, basestring) and values == 'initial':
            self.warning("The 'initial' string for dimension values is no longer supported.")
            values = []

        all_params['values'] = sorted(list(unique_array(values)))
        super(Dimension, self).__init__(**all_params)


    def __call__(self, name=None, **overrides):
        """
        Derive a new Dimension that inherits existing parameters
        except for the supplied, explicit overrides
        """
        settings = dict(self.get_param_values(onlychanged=True), **overrides)
        if name is not None: settings['name'] = name
        return self.__class__(**settings)


    @property
{'fraction_non_alphanumeric': 0.08752962085308057, 'fraction_numerical': 0.0002962085308056872, 'mean_word_length': 2.66585233441911, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4709824', 'n_tokens_mistral': 3527, 'n_tokens_neox': 3419, 'n_words': 1313}