def setcachekey(key):
    """Record the derived cache key for this request.

        Instances may mutate the key for internal usage, as desired. e.g.
        instances may wish to prepend the repo name, introduce path
        components for filesystem or URL addressing, etc. Behavior is up to
        the cache.

        Returns a bool indicating if the request is cacheable by this
        instance.
        """


def inverse_permutation(permutation, j):
    """ Inverse the permutation for given input j, that is, it finds i such that p[i] = j.

    >>> permutation = [1, 0, 3, 2]
    >>> inverse_permutation(permutation, 1)
    0
    >>> inverse_permutation(permutation, 0)
    1
    """
    for i, pi in enumerate(permutation):
        if pi == j:
            return i
    raise ValueError('inverse_permutation({}, {}) failed.'.format(
        permutation, j))


@staticmethod
def _compute_generic_dominators(head, reachable_cb, prev_cb, next_cb):
    """Generic algorithm to compute either the dominators or postdominators
        of the graph.
        @head: the head/leaf of the graph
        @reachable_cb: sons/parents of the head/leaf
        @prev_cb: return predecessors/successors of a node
        @next_cb: return successors/predecessors of a node
        """
    nodes = set(reachable_cb(head))
    dominators = {}
    for node in nodes:
        dominators[node] = set(nodes)
    dominators[head] = set([head])
    todo = set(nodes)
    while todo:
        node = todo.pop()
        if node == head:
            continue
        new_dom = None
        for pred in prev_cb(node):
            if not pred in nodes:
                continue
            if new_dom is None:
                new_dom = set(dominators[pred])
            new_dom.intersection_update(dominators[pred])
        assert new_dom is not None
        new_dom.update(set([node]))
        if new_dom == dominators[node]:
            continue
        dominators[node] = new_dom
        for succ in next_cb(node):
            todo.add(succ)
    return dominators


def get_root_storage_index_string():
    """Return the storage index (abbreviated human-readable string) of
        the first object checked."""


def correct_gear_box_torque_in(gear_box_torque_out, gear_box_torque_in,
    gear, gear_box_ratios):
    """
    Corrects the torque when the gear box ratio is equal to 1.

    :param gear_box_torque_out:
        Torque gear_box [N*m].
    :type gear_box_torque_out: float

    :param gear_box_torque_in:
        Torque required [N*m].
    :type gear_box_torque_in: float

    :param gear:
        Gear [-].
    :type gear: int

    :param gear_box_ratios:
        Gear box ratios [-].
    :type gear_box_ratios: dict[int | float]

    :return:
        Corrected torque required [N*m].
    :rtype: float
    """
    gbr = gear_box_ratios
    if gbr is None or gear is None:
        return gear_box_torque_in
    return gear_box_torque_out if gbr.get(gear, 0) == 1 else gear_box_torque_in


def parse(string, start, divider, type=str):
    """general purpose tokenizer, used below"""
    if string.startswith(start):
        string = string[len(start):]
        try:
            result, string = string.split(divider, 1)
        except ValueError:
            result, string = string, ''
    else:
        result = None
    if result is not None:
        result = type(result)
    return result, string


def load_sql_csv(connection, input_dir):
    """
    Load the CSV files we created into PostgreSQL using the `copy_from`
    method, which is the same as the COPY command at the psql command line.
    """
    for filename, tablename in [(input_dir + '/relations.csv', 'relations'),
        (input_dir + '/nodes.csv', 'nodes'), (input_dir + '/edges.csv',
        'edges'), (input_dir + '/sources.csv', 'sources'), (input_dir +
        '/edges_gin.shuf.csv', 'edges_gin'), (input_dir +
        '/edge_features.csv', 'edge_features')]:
        with connection.cursor() as cursor:
            with open(filename, 'rb') as file:
                cursor.copy_from(file, tablename)
        connection.commit()


def render_git_describe_long(pieces):
    """TAG-DISTANCE-gHEX[-dirty].

    Like 'git describe --tags --dirty --always -long'.
    The distance/hash is unconditional.

    Exceptions:
    1: no tags. HEX[-dirty]  (note: no 'g' prefix)
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        rendered += '-%d-g%s' % (pieces['distance'], pieces['short'])
    else:
        rendered = pieces['short']
    if pieces['dirty']:
        rendered += '-dirty'
    return rendered


def data2dict(data):
    """Convert a DFTData object into a a dictionary of json-izable objects."""
    nruter = dict()
    nruter['BoltzTraP2_type'] = 'DFTData'
    nruter['ebands'] = data.ebands
    nruter['mommat'] = data.mommat
    nruter['kpoints'] = data.kpoints
    nruter['atoms'] = data.atoms
    nruter['fermi'] = data.fermi
    nruter['dosweight'] = data.dosweight
    nruter['nelect'] = data.nelect
    nruter['magmom'] = data.magmom
    return nruter


def rand_unit_complex(shape, ftype='double'):
    """Return random complex values in the [0, 1) interval.

    Parameters
    ----------
    shape : int or sequence of ints
        Output shape. For instance, if the shape is ``(m, n)``, then ``m * n``
        samples are drawn.
    ftype : str or dtype, optional
        Output float type. Choose between 'single', 'double', or 'longdouble'.
        Default is 'double', in which case the output array will be composed of
        128-bit complex floats.

    Returns
    -------
    out : ndarray of complex floats
    """
    from numpy.random import ranf
    return ranf(shape).astype(ftype) + 1.0j * ranf(shape).astype(ftype)


@classmethod
def _from_dict(cls, _dict):
    """Initialize a GatewayDelete object from a json dictionary."""
    args = {}
    if 'gateway_id' in _dict:
        args['gateway_id'] = _dict.get('gateway_id')
    if 'status' in _dict:
        args['status'] = _dict.get('status')
    return cls(**args)


def duplicate_move(MESH_OT_duplicate=None, TRANSFORM_OT_translate=None):
    """Duplicate mesh and move

    :param MESH_OT_duplicate: Duplicate, Duplicate selected vertices, edges or faces
    :param TRANSFORM_OT_translate: Translate, Translate (move) selected items
    """
    pass


def normal(mu, sigma, random_state):
    """
    The probability density function of the normal distribution,
    first derived by De Moivre and 200 years later by both Gauss and Laplace independently.
    mu: float or array_like of floats
        Mean (“centre”) of the distribution.
    sigma: float or array_like of floats
           Standard deviation (spread or “width”) of the distribution.
    random_state: an object of numpy.random.RandomState
    """
    return random_state.normal(mu, sigma)


@classmethod
def _guess_candidates(cls, base, directories, files):
    """
        This is a class method that accepts a directory (base), a list of files
        in that directory, and a list of subdirectories.  It should return a
        list of filenames (defined relative to the supplied directory) and a
        boolean as to whether or not further directories should be recursed.

        This function doesn't need to catch all possibilities, nor does it need
        to filter possibilities.
        """
    return [], True


@staticmethod
def concat_url(endpoint, url):
    """Concatenate endpoint and final URL.

        E.g., "http://keystone/v2.0/" and "/tokens" are concatenated to
        "http://keystone/v2.0/tokens".

        :param endpoint: the base URL
        :param url: the final URL
        """
    return '%s/%s' % (endpoint.rstrip('/'), url.strip('/'))


def kwargs_from_keyword(from_kwargs, to_kwargs, keyword, clean_origin=True):
    """
	Looks for keys of the format keyword_value.
	And return a dictionary with {keyword:value} format

	Parameters:
	-----------
		from_kwargs : dict
			Original dictionary
		to_kwargs : dict
			Dictionary where the items will be appended
		keyword : string
			Keyword to look for in the orginal dictionary
		clean_origin : bool
			If True then the k,v pairs from the original
			dictionary are deleted
	"""
    for k in list(from_kwargs.keys()):
        if '{0}_'.format(keyword) in k:
            to_kwargs[k.replace('{0}_'.format(keyword), '')] = from_kwargs[k]
            if clean_origin:
                del from_kwargs[k]
    return to_kwargs


def get_calibration_from_position(position):
    """Compute the size, scale and offset of a linear axis from coordinates.

    This function assumes rastering on a regular grid for the full size of
    each dimension before rastering over another one. Fox example: a11, a12,
    a13, a21, a22, a23 for a 2x3 grid.

    Parameters
    ----------
    position: numpy array.
        Position coordinates of the axis. Normally as in PositionX/Y of the
        ser file.

    Returns
    -------
    axis_attr: dictionary with `size`, `scale`, `offeset` keys.

    """
    offset = position[0]
    for i, x in enumerate(position):
        if x != position[0]:
            break
    if i == len(position) - 1:
        scale = 0
        size = 0
    else:
        scale = x - position[0]
        if i == 1:
            for j, x in enumerate(position[1:]):
                if x == position[0]:
                    break
            size = j + 1
        else:
            size = len(position) / i
    axis_attr = {'size': size, 'scale': scale, 'offset': offset}
    return axis_attr


def get_price(item):
    """Given an SoftLayer_Product_Item, returns its default price id"""
    for price in item.get('prices', []):
        if not price.get('locationGroupId'):
            return price.get('id')
    return 0


@classmethod
def get_integration_Methfessel_Paxton_order(cls, calc, **kwargs):
    """Return the order of Methfessel-Paxton approximation if used."""
    if cls.get_integration_smearing_method(calc, **kwargs
        ) == 'Methfessel-Paxton':
        return 1


def _sanity_check_kwargs(args):
    """Sanity check after setting up input arguments, handling back compatibility
    """
    if not args.get('workflow') and not args.get('run_info_yaml'):
        return (
            'Require a sample YAML file describing inputs: https://bcbio-nextgen.readthedocs.org/en/latest/contents/configuration.html'
            )


@staticmethod
def is_tag():
    """Return true is instance is a tag object."""
    return True


@staticmethod
def _clean_field(field):
    """
        Dimension can be passed as dictionary or str. str works as abbreviation for basic requests
        and will be converted to proper dictionary form.
        """
    if isinstance(field, str):
        return {'id': field}
    elif isinstance(field, dict):
        return field
    else:
        raise ValueError(
            'Unexpected type of dimension. Please use str or dict.')


def standaardfragmenten_dict_sample(sample, df):
    """Turn dataframe with standard fragments into dict and return list of dicts."""
    sample_frags = list()
    all_frags = df.to_dict('records')
    for d in all_frags:
        if d['FRACTIENR'] == sample:
            sample_frags.append(d)
    return sample_frags


def __windows_taskbar_compat():
    """Enable icon and taskbar grouping for Windows 7+"""
    import ctypes
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(
        u'pyblish_lite')


def __iter__():
    """Iterate over paths in the manifest."""


def get_inch_multiplier() ->float:
    """
    Get the multiplier needed for converting pixels to inches.
    :return: a floating point value representing the multiplier.
    """
    return 0.254


def camera_preset_add(name: str='', remove_active: bool=False,
    use_focal_length: bool=True):
    """Add or remove a Tracking Camera Intrinsics Preset

    :param name: Name, Name of the preset, used to make the path name
    :type name: str
    :param remove_active: remove_active
    :type remove_active: bool
    :param use_focal_length: Include Focal Length, Include focal length into the preset
    :type use_focal_length: bool
    """
    pass


def camera_to_view():
    """Set camera view to active view

    """
    pass


def cut_line_with_context(line):
    """
    A intelligent line cutting algorithm with context info.
    :param line: a LineString instance of shapely
    :return: a tuple of (sampled_points_indices, distances)
    """
    assert line.geom_type == 'LineString'
    pass


def paste():
    """Paste the material settings and nodes

    """
    pass


def _isint(x):
    """Test for int-conversible value"""
    try:
        int(x)
        return True
    except ValueError:
        return False


def reverse_lookup(dic, val):
    """
    Perform reverse lookup of any key that has the provided value.

    @param dic: Dictionary to perform reverse lookup on.
    @type  dic: C{dict}

    @param val: Value being searched.
    @type  val: an existing value.

    @return: A key such that C{dict[key] == val}.
    @rtype:  Type of the matching key.
    """
    for k, v in dic.items():
        if v == val:
            return k
    raise AssertionError('Value not found in the dictionary.')


def strip(value, chars=None):
    """
    >>> strip(' 	abc def	 ')
    'abc def'
    >>> strip('"abc"', '"')
    'abc'
    """
    return value.strip(chars)


@classmethod
def align_address_to_page_end(cls, address):
    """
        Align the given address to the end of the page it occupies.
        That is, to point to the start of the next page.

        @type  address: int
        @param address: Memory address.

        @rtype:  int
        @return: Aligned memory address.
        """
    return address + cls.pageSize - address % cls.pageSize


def manual_variable_initialization(value):
    """Sets the manual variable initialization flag.

    This boolean flag determines whether
    variables should be initialized
    as they are instantiated (default), or if
    the user should handle the initialization
    (e.g. via `tf.initialize_all_variables()`).

    # Arguments
        value: Python boolean.
    """
    global _MANUAL_VAR_INIT
    _MANUAL_VAR_INIT = value


def top_level_only(attrs, new=False):
    """
    Helper function used when linkifying with bleach.
    """
    if not new:
        return attrs
    text = attrs['_text']
    if not text.startswith(('http:', 'https:')):
        return None
    return attrs


def vert_connect_concave():
    """Make all faces convex

    """
    pass


def __fix_context(context):
    """Return a new context dict based on original context.

    The new context will be a copy of the original, and some mutable
    members (such as script and css files) will also be copied to
    prevent polluting shared context.
    """
    COPY_LISTS = 'script_files', 'css_files'
    for attr in COPY_LISTS:
        if attr in context:
            context[attr] = context[attr][:]
    return context


def _computer_create_temp_file(transport, scheduler, authinfo):
    """
    Internal test to check if it is possible to create a temporary file
    and then delete it in the work directory

    :note: exceptions could be raised

    :param transport: an open transport
    :param scheduler: the corresponding scheduler class
    :param authinfo: the AuthInfo object (from which one can get computer and aiidauser)
    :return: tuple of boolean indicating success or failure and an optional string message
    """
    import tempfile
    import datetime
    import os
    file_content = "Test from 'verdi computer test' on {}".format(datetime.
        datetime.now().isoformat())
    workdir = authinfo.get_workdir().format(username=transport.whoami())
    try:
        transport.chdir(workdir)
    except IOError:
        transport.makedirs(workdir)
        transport.chdir(workdir)
    with tempfile.NamedTemporaryFile(mode='w+') as tempf:
        fname = os.path.split(tempf.name)[1]
        remote_file_path = os.path.join(workdir, fname)
        tempf.write(file_content)
        tempf.flush()
        transport.putfile(tempf.name, remote_file_path)
    if not transport.path_exists(remote_file_path):
        return False, 'failed to create the file `{}` on the remote'.format(
            remote_file_path)
    handle, destfile = tempfile.mkstemp()
    os.close(handle)
    try:
        transport.getfile(remote_file_path, destfile)
        with open(destfile, encoding='utf8') as dfile:
            read_string = dfile.read()
        if read_string != file_content:
            message = (
                'retrieved file content is different from what was expected')
            message += '\n  Expected: {}'.format(file_content)
            message += '\n  Retrieved: {}'.format(read_string)
            return False, message
    finally:
        os.remove(destfile)
    transport.remove(remote_file_path)
    return True, None


def disp_df_head_tail(df, n_head=3, n_tail=3, head_capt='df.head',
    tail_capt='df.tail'):
    """Displays the df.head(n_head) and df.tail(n_tail) and sets captions using df.style"""
    from IPython.display import display
    import pandas as pd
    df_h = df.head(n_head).style.set_caption(head_capt)
    df_t = df.tail(n_tail).style.set_caption(tail_capt)
    display(df_h, df_t)


@classmethod
def enable(cls):
    """Enables application cache domain notifications.
        """
    return cls.build_send_payload('enable', {}), None


def add_point(location: int=(0, 0)):
    """Add New Paint Curve Point

    :param location: Location, Location of vertex in area space
    :type location: int
    """
    pass


def addr_eq(a, b):
    """
    Address comparisson. Allocation free.
    :param a:
    :param b:
    :return:
    """
    return (a.spend_public_key == b.spend_public_key and a.view_public_key ==
        b.view_public_key)


def brunt_index_correct_range(alpha_low, alpha_up):
    """
    Upper and low error ranges. See `brunt_index_correct`.
    """
    return (1.52 - 0.18) * alpha_low - (0.19 + 0.2), (1.52 + 0.18
        ) * alpha_up - (0.19 - 0.2)


@staticmethod
def filtered_iteritems(d, filter_keys):
    """A generator that filters a dict's items to keys in `filter_keys`.

        Args:
            d (:obj:`dict`): dictionary to filter.
            filter_keys (:obj:`list` of :obj:`str`): list of keys to retain.

        Yields:
            :obj:`tuple`: (key, value) tuples from `d` whose keys are in `filter_keys`.
        """
    for key, val in d.items():
        if key not in filter_keys:
            continue
        yield key, val


@classmethod
def fromkeys(cls, iterable, value=None):
    """OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
        and values equal to v (which defaults to None).

        """
    d = cls()
    for key in iterable:
        d[key] = value
    return d


def plus_or_dot(pieces):
    """Return a + if we don't already have one, else return a ."""
    if '+' in pieces.get('closest-tag', ''):
        return '.'
    return '+'


def __delitem__(name):
    """Delete a file or directory inside this directory
        """


def cross_context(contextss):
    """
    Cross product of all contexts
    [[a], [b], [c]] -> [[a] x [b] x [c]]

    """
    if not contextss:
        return []
    product = [{}]
    for contexts in contextss:
        tmp_product = []
        for c in contexts:
            for ce in product:
                c_copy = c.copy()
                c_copy.update(ce)
                tmp_product.append(c_copy)
        product = tmp_product
    return product


@staticmethod
def positional_to_model(value):
    """Converts a positional argument to a model value"""
    return value


@staticmethod
def get_sequenceinsertion(sequence, indent) ->str:
    """Return the insertion string required for the given sequence.

        >>> from hydpy.auxs.xmltools import XSDWriter
        >>> from hydpy import prepare_model
        >>> model = prepare_model('hland_v1')
        >>> print(XSDWriter.get_sequenceinsertion(model.sequences.fluxes.pc, 1))
            <element
                name="pc"
                minOccurs="0"/>
        """
    blanks = ' ' * (indent * 4)
    return (
        f'{blanks}<element\n{blanks}    name="{sequence.name}"\n{blanks}    minOccurs="0"/>'
        )


def to_list(l):
    """
    Helper function for py2/py3 compatibility used e.g. in
    list(dict.keys())

    Don't use it for structures like list(zip(x, y)), where six.moves.zip is
    used, so it is always an iterator.
    """
    if isinstance(l, list):
        return l
    else:
        return list(l)


@classmethod
def export(cls, options=None):
    """Export a repository"""
    cls.command_sub = 'export'
    return cls.execute(cls._construct_command(options), output_format='csv',
        ignore_stderr=True)


@staticmethod
def to_genotype(phenotype):
    """
        Converts environment representation of a state to LCS
        representation.
        """
    return phenotype


def ratio_str(to_str, a, b):
    """
    Example: to_str = duration_str, a = 60, b = 120 => "1m / 2m (50%)"
    """
    return '%s / %s (%.1f%%)' % (to_str(a), to_str(b), 100.0 * a / b)


def ebs_settings_validator(param_key, param_value, pcluster_config):
    """
    Validate the following cases.

    Number of EBS volume specified is lower than maximum supported
    Parameter shared_dir is specified in every EBS section when using more than 1 volume
    User is not specifying /NONE or NONE as shared_dir in EBS sections
    """
    errors = []
    warnings = []
    list_of_shared_dir = []
    for section_label in param_value.split(','):
        section = pcluster_config.get_section('ebs', section_label.strip())
        list_of_shared_dir.append(section.get_param_value('shared_dir'))
    max_number_of_ebs_volumes = 5
    num_volumes_specified = len(list_of_shared_dir)
    if num_volumes_specified > max_number_of_ebs_volumes:
        errors.append(
            'Invalid number of EBS volumes ({0}) specified. Currently only supports upto {1} EBS volumes'
            .format(num_volumes_specified, max_number_of_ebs_volumes))
    if num_volumes_specified > 1 and None in list_of_shared_dir:
        errors.append(
            'When using more than 1 EBS volume, shared_dir is required under each EBS section'
            )
    return errors, warnings


def tile_region(rname, start, end, step):
    """ Make non-overlapping tiled windows from the specified region in
    the UCSC-style string format.

    >>> list(tile_region('chr1', 1, 250, 100))
    ['chr1:1-100', 'chr1:101-200', 'chr1:201-250']
    >>> list(tile_region('chr1', 1, 200, 100))
    ['chr1:1-100', 'chr1:101-200']
    """
    while start + step <= end:
        yield '%s:%d-%d' % (rname, start, start + step - 1)
        start += step
    if start < end:
        yield '%s:%d-%d' % (rname, start, end)


def acceleration_aug(friction_coefficient, gravity):
    """Usage: Find acceleration using friction coefficient and gravitational constant"""
    return friction_coefficient * gravity


def minute_to_s(m):
    """
    >>> minute_to_s('[00:38.36]')
    38
    """
    time_str = m.strip('[|]')
    try:
        m_str, s_str = time_str.split(':')
        second = int(m_str) * 60 + int(float(s_str))
    except ValueError:
        second = 0
    return second


def accuracy(output, target, topk=(1,)):
    """
    Computes the precision@k for the specified values of k

    Parameters
    ----------
    output : pytorch tensor
        output, e.g., predicted value
    target : pytorch tensor
        label
    topk : tuple
        specify top1 and top5

    Returns
    -------
    list
        accuracy of top1 and top5
    """
    maxk = max(topk)
    batch_size = target.size(0)
    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))
    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res


def snap_cursor_to_center():
    """Snap 3D cursor to the world origin

    """
    pass


def reserve():
    """
        Attempt to open the reserved file descriptor; if this fails
        because of C{EMFILE}, internal state is reset so that another
        reservation attempt can be made.

        @raises: Any exception except an L{OSError} or L{IOError}
            whose errno is L{EMFILE}.
        """


@classmethod
def load(cls, filepath, **options):
    """Load a problem instance from a text file.

        Any keyword options are passed to the class constructor. If a keyword
        argument has the same name as a field then they will collide and cause
        an error.

        :param str filepath: path to a problem file
        :param options: any keyword arguments to pass to the constructor
        :return: problem instance
        :rtype: :class:`Problem`
        """
    with open(filepath) as f:
        return cls.read(f, **options)


def bind_bottom_up(lower, upper, __fval=None, **fval):
    "Bind 2 layers for dissection.\n    The upper layer will be chosen for dissection on top of the lower layer, if\n    ALL the passed arguments are validated. If multiple calls are made with the same  # noqa: E501\n    layers, the last one will be used as default.\n\n    ex:\n        >>> bind_bottom_up(Ether, SNAP, type=0x1234)\n        >>> Ether(b'ÿÿÿÿÿÿÐP\x99VÝù\x124\x00\x00\x00\x00\x00')  # noqa: E501\n        <Ether  dst=ff:ff:ff:ff:ff:ff src=d0:50:99:56:dd:f9 type=0x1234 |<SNAP  OUI=0x0 code=0x0 |>>  # noqa: E501\n    "
    if __fval is not None:
        fval.update(__fval)
    lower.payload_guess = lower.payload_guess[:]
    lower.payload_guess.append((fval, upper))


@classmethod
def _is_override(meta, method):
    """Checks whether given class or instance method has been marked
        with the ``@override`` decorator.
        """
    from taipan.objective.modifiers import _OverriddenMethod
    return isinstance(method, _OverriddenMethod)


def matches(options, key, default=None):
    """
    Match a key with a dictionary key
    and return the dictionary value.
    Allows to pass custom default parameter
    to avoid returning None types.

    :param options: dictionary of possible key, values
    :param key: key that will tried to be matched with a options key
    :returns value: value that corresponds to given key or default if no matche
    """
    return options.get(key, default)


@classmethod
def deserialize(cls, item):
    """Deserialize from a dictionary."""
    return cls(**item)


def object_remove():
    """Remove Rigid Body settings from Object

    """
    pass


def get_component_registry():
    """Returns the site manager contained in this object.

        If there isn't a site manager, raise a component lookup.
        """


def html5_field(name, base):
    """
    Takes a Django form field class and returns a subclass of
    it with the given name as its input type.
    """
    return type(str(''), (base,), {'input_type': name})


def groupCount(grp):
    """
    function to count the number of samples that share the same mutation
    Input: Acts on a pandas groupby object
    Oputput: returns a DataFrame
    DEPRICATED: This has been reduced to a faster lambda function
    """
    return len(grp['sample'].unique())


def write_int(encoder, datum, schema=None):
    """int and long values are written using variable-length, zig-zag coding.
    """
    encoder.write_int(datum)


def dependencies(cls):
    """Returns dict of dependencies of a class declared with
    ``@injections.has``
    """
    return getattr(cls, '__injections__', {})


def colormap_hess(transition=0.5, width=0.1):
    """Colormap often used in H.E.S.S. collaboration publications.

    This colormap goes black -> blue -> red -> yellow -> white.

    A sharp blue -> red -> yellow transition is often used for significance images
    with a value of red at ``transition ~ 5`` or ``transition ~ 7``
    so that the following effect is achieved:

    - black, blue: non-significant features, not well visible
    - red: features at the detection threshold ``transition``
    - yellow, white: significant features, very well visible

    The transition parameter is defined between 0 and 1. To calculate the value
    from data units an `~astropy.visualization.mpl_normalize.ImageNormalize`
    instance should be used (see example below).

    Parameters
    ----------
    transition : float (default = 0.5)
        Value of the transition to red (between 0 and 1).
    width : float (default = 0.5)
        Width of the blue-red color transition (between 0 and 1).

    Returns
    -------
    colormap : `matplotlib.colors.LinearSegmentedColormap`
        Colormap

    Examples
    --------
    >>> from gammapy.visualization import colormap_hess
    >>> from astropy.visualization.mpl_normalize import ImageNormalize
    >>> from astropy.visualization import LinearStretch
    >>> normalize = ImageNormalize(vmin=-5, vmax=15, stretch=LinearStretch())
    >>> transition = normalize(5)
    >>> cmap = colormap_hess(transition=transition)
    """
    from matplotlib.colors import LinearSegmentedColormap
    red = float(transition)
    if width > red:
        blue = 0.1 * red
    else:
        blue = red - width
    yellow = 2.0 / 3.0 * (1 - red) + red
    black, white = 0, 1
    colors = [(black, 'k'), (blue, (0, 0, 0.8)), (red, 'r'), (yellow, (1.0,
        1.0, 0)), (white, 'w')]
    return LinearSegmentedColormap.from_list(name='hess', colors=colors)


def get_preferred_sparse_format():
    """Get the preferred sparse matrix format supported by the backend.

    Different backends have their preferred backend. This info is useful when
    constructing a sparse matrix.
    """
    return 'coo'


@staticmethod
def _set_object_transform_matrix(obj, vtk_tm):
    """Set ``obj``'s pose according to the transform ``vtk_tm``.

		:param obj: object to be modified
		:type obj: :class:`vtk.vtkProp3D`
		:param vtk_tm: homogeneous transform
		:type vtk_tm: :class:`vtk.vtkMatrix4x4`

		"""
    obj.PokeMatrix(vtk_tm)


@classmethod
def open_unsupported(klass, base):
    """Open a branch which is not supported."""
    return klass.open(base, _unsupported=True)


def count_typos(s1, s2, flags1, flags2, typo_table):
    """
    Check unmatched characters in strings 's1' and 's2' for typos.

    The typos (or known phonetic or character recognition errors) are
    defined in the 'typo_table' argument, which defines the mapping between
    similar characters, and a score to assign to each. [See the
    typo_tables.py module].

    The function returns the total typo score, and updates the 'field2' bit
    array to mark which characters were adjudged similar.
    """
    assert 0 in flags1
    typo_score = 0
    for i, flag1 in enumerate(flags1):
        if flag1:
            continue
        row = s1[i]
        if row not in typo_table:
            continue
        typo_row = typo_table[row]
        for j, flag2 in enumerate(flags2):
            if flag2:
                continue
            col = s2[j]
            if col not in typo_row:
                continue
            typo_score += typo_row[col]
            flags2[j] = 2
            break
    return typo_score, flags2


def split_concept_cd(concept_cd, join_char='+'):
    """Split up the full concept path into a category_cd and a data label."""
    category_code, data_label = concept_cd.rsplit(join_char, 1)
    return category_code, data_label


def _ka_display_col_type(data):
    """See column type distribution

       Parameters
       ----------
       data: pandas dataframe

       Return
       ------
       dataframe
    """
    column_type = data.dtypes.reset_index()
    column_type.columns = ['count', 'column type']
    return column_type.groupby(['column type']).agg('count').reset_index()


def add_line_breaks(string, min_length):
    """! @brief Adds line breaks on the next space after the minimum length of a line"""
    requirement = min_length
    replace = False
    positions = []
    for i, char in enumerate(string):
        if i > requirement:
            replace = True
        if replace and char == ' ':
            positions.append(i)
            requirement = i + min_length
            replace = False
    for rep in positions:
        string = string[:rep] + '\n' + string[rep + 1:]
    return string


def t_ip_range(val):
    """
    Convert/validate: IP range.

    :param any val: Value to be converted/validated
    :return: IP range
    :rtype: str
    """
    return str(val)


def create_dict(item_list):
    """
    Create a dictionary of items from a list of list of items.
    """
    assert type(item_list) is list
    dico = {}
    for items in item_list:
        for item in items:
            if item not in dico:
                dico[item] = 1
            else:
                dico[item] += 1
    return dico


def get_schema():
    """Schema structure of this resource

    :return: schema dictionary
    :rtype: dict
    """
    return {'schema': {'schema_version': {'type': 'integer', 'default': 1},
        'imported_from': {'schema_version': 1, 'title': 'Imported from',
        'comment': 'Item importation source (alignak-backend-import, ...)',
        'type': 'string', 'default': 'unknown'}, 'definition_order': {
        'schema_version': 1, 'title': 'Definition order', 'comment':
        'Priority level if several elements have the same name', 'type':
        'integer', 'default': 100}, 'name': {'schema_version': 1, 'title':
        'Service dependency name', 'type': 'string', 'empty': False,
        'unique': True}, 'alias': {'schema_version': 1, 'title': 'Alias',
        'comment': 'Element friendly name used by the Web User Interface.',
        'type': 'string', 'default': ''}, 'notes': {'schema_version': 1,
        'title': 'Notes', 'comment':
        'Element notes. Free text to store element information.', 'type':
        'string', 'default': ''}, 'dependent_hosts': {'schema_version': 1,
        'title': 'Dependent hosts', 'comment':
        'List of the hosts that are depending.', 'type': 'list', 'schema':
        {'type': 'objectid', 'data_relation': {'resource': 'host',
        'embeddable': True}}}, 'dependent_hostgroups': {'schema_version': 1,
        'title': 'Dependent hosts groups', 'comment':
        'List of the hosts groups that are depending.', 'type': 'list',
        'schema': {'type': 'objectid', 'data_relation': {'resource':
        'hostgroup', 'embeddable': True}}}, 'services': {'schema_version':
        1, 'title': 'Services', 'comment':
        'List of the services involved in the dependency.', 'type': 'list',
        'schema': {'type': 'objectid', 'data_relation': {'resource':
        'service', 'embeddable': True}}}, 'dependent_services': {
        'schema_version': 1, 'title': 'Dependent services', 'comment':
        'List of the services that are depending.', 'type': 'list',
        'schema': {'type': 'objectid', 'data_relation': {'resource':
        'service', 'embeddable': True}}}, 'hosts': {'schema_version': 1,
        'title': 'Hosts', 'comment':
        'List of the hosts involved in the dependency.', 'type': 'list',
        'schema': {'type': 'objectid', 'data_relation': {'resource': 'host',
        'embeddable': True}}}, 'hostgroups': {'schema_version': 1, 'title':
        'Hosts groups', 'comment':
        'List of the hosts groups involved in the dependency.', 'type':
        'list', 'schema': {'type': 'objectid', 'data_relation': {'resource':
        'hostgroup', 'embeddable': True}}}, 'inherits_parent': {
        'schema_version': 1, 'title': 'Parent inheritance', 'comment':
        'See Alginak doc about dependency checks.', 'type': 'boolean',
        'default': False}, 'execution_failure_criteria': {'schema_version':
        1, 'title': 'Execution criteria', 'comment':
        'See Alginak doc about dependency checks.', 'type': 'list',
        'default': ['u', 'c', 'w'], 'allowed': ['o', 'w', 'u', 'c', 'p',
        'n']}, 'notification_failure_criteria': {'schema_version': 1,
        'title': 'Notification criteria', 'comment':
        'See Alginak doc about dependency checks.', 'type': 'list',
        'default': ['u', 'c', 'w'], 'allowed': ['o', 'w', 'u', 'c', 'p',
        'n']}, 'dependency_period': {'schema_version': 1, 'title':
        'Dependency period', 'comment':
        'Time period during which the dependency checks are done.', 'type':
        'objectid', 'data_relation': {'resource': 'timeperiod',
        'embeddable': True}, 'required': True}, 'explode_hostgroup': {
        'schema_version': 1, 'type': 'boolean', 'default': False}, '_realm':
        {'schema_version': 1, 'title': 'Realm', 'comment':
        'Realm this element belongs to.', 'type': 'objectid',
        'data_relation': {'resource': 'realm', 'embeddable': True},
        'required': True}, '_sub_realm': {'schema_version': 1, 'title':
        'Sub-realms', 'comment':
        'Is this element visible in the sub-realms of its realm?', 'type':
        'boolean', 'default': True}, '_users_read': {'schema_version': 1,
        'type': 'list', 'schema': {'type': 'objectid', 'data_relation': {
        'resource': 'user', 'embeddable': True}}}, '_users_update': {
        'schema_version': 1, 'type': 'list', 'schema': {'type': 'objectid',
        'data_relation': {'resource': 'user', 'embeddable': True}}},
        '_users_delete': {'schema_version': 1, 'type': 'list', 'schema': {
        'type': 'objectid', 'data_relation': {'resource': 'user',
        'embeddable': True}}}}, 'schema_deleted': {}}


def get(category_name, discriminator, default=None):
    """ Get the IIntrospectable related to the category_name and the
        discriminator (or discriminator hash) ``discriminator``.  If it does
        not exist in the introspector, return the value of ``default`` """


def identify_max_engine_coolant_temperature(engine_coolant_temperatures):
    """
    Identifies maximum engine coolant temperature [°C].

    :param engine_coolant_temperatures:
        Engine coolant temperature vector [°C].
    :type engine_coolant_temperatures: numpy.array

    :return:
        Maximum engine coolant temperature [°C].
    :rtype: float
    """
    return engine_coolant_temperatures.max()


def pretty_field(value: str) ->str:
    """
    Format a snake_case field for display (snake_case -> Snake case)
    and make some other adjustments (identifier => id, remove meta_).
    :param value: Raw field name
    :return: Field to display
    """
    return value.replace('meta_', '').capitalize().replace('_', ' ').replace(
        'identifier', 'id')


def S_IFMT(mode):
    """Return the portion of the file's mode that describes the
    file type.
    """
    return mode & 61440


def remove(timeentry):
    """ Removes the given timeentry
        """


def get_supported_domains():
    """
    Get a list of supported domains for creating a Geomap

    ### Returns

    - *list of strings*: list of supported projections
    """
    return ['CONUS', 'global', 'NA', 'US']


def concat_couples(a, b):
    """the set of x+y for x in a, y in b"""
    s = set()
    for x in a:
        for y in b:
            s.add(x + y)
    return s


@classmethod
def from_json(cls, filename, key='data', dims=None, edges=None, **kwargs):
    """
        Initialize a BinnedStatistic from a JSON file.

        The JSON file should contain a dictionary, where the data to load is stored
        as the ``key`` entry, with an ``edges`` entry specifying bin edges, and
        optionally, a ``attrs`` entry giving a dict of meta-data

        .. note::
            This uses :class:`nbodykit.utils.JSONDecoder` to load the
            JSON file

        Parameters
        ----------
        filename : str
            the name of the file to load
        key : str, optional
            the name of the key in the JSON file holding the data to load
        dims : list, optional
            list of names specifying the dimensions, i.e., ``['k']`` or ``['k', 'mu']``;
            must be supplied if not given in the JSON file

        Returns
        -------
        dset : BinnedStatistic
            the BinnedStatistic holding the data from file
        """
    import json
    from nbodykit.utils import JSONDecoder
    with open(filename, 'r') as ff:
        state = json.load(ff, cls=JSONDecoder)
    if key not in state:
        args = key, tuple(state.keys())
        raise ValueError(
            "no data entry found in JSON format for '%s' key; valid keys are %s"
             % args)
    data = state[key]
    dims = state.pop('dims', dims)
    if dims is None:
        raise ValueError(
            'no `dims` found in JSON file; please specify as keyword argument')
    edges = state.pop('edges', edges)
    if edges is None:
        raise ValueError(
            'no `edges` found in JSON file; please specify as keyword argument'
            )
    coords = state.pop('coords', None)
    attrs = state.pop('attrs', {})
    attrs.update(kwargs)
    return cls(dims, edges, data, coords=coords, **attrs)


@classmethod
def file_transfers_transfer_delete_details(cls, val):
    """
        Create an instance of this class set to the
        ``file_transfers_transfer_delete_details`` tag with value ``val``.

        :param FileTransfersTransferDeleteDetails val:
        :rtype: EventDetails
        """
    return cls('file_transfers_transfer_delete_details', val)


def ncols(series):
    """
    Determine # of columns
    """
    if not hasattr(series, 'shape'):
        return 1
    if len(series.shape) == 1:
        return 1
    else:
        return series.shape[1]


def _always_run(*args, **kwargs) ->bool:
    """ This returns False to indicate that the step is not already completed. """
    return False


def _build_map_of_offset_to_line_number(source):
    """
    For a multiline source file, return {character_pos: line}
    """
    start_index = 0
    index_to_line_number = {}
    for line_number, line in enumerate(source.splitlines(), 1):
        for index in range(start_index, start_index + len(line) + 1):
            index_to_line_number[index] = line_number
        start_index += len(line) + 1
    return index_to_line_number


def listenMulticast(port, protocol, interface='', maxPacketSize=8192,
    listenMultiple=False):
    """
        Connects a given
        L{DatagramProtocol<twisted.internet.protocol.DatagramProtocol>} to the
        given numeric UDP port.

        @param listenMultiple: If set to True, allows multiple sockets to
            bind to the same address and port number at the same time.
        @type listenMultiple: C{bool}

        @returns: An object which provides L{IListeningPort}.

        @see: L{twisted.internet.interfaces.IMulticastTransport}
        @see: U{http://twistedmatrix.com/documents/current/core/howto/udp.html}
        """


@classmethod
def unavailable(cls, val):
    """
        Create an instance of this class set to the ``unavailable`` tag with
        value ``val``.

        :param str val:
        :rtype: AddSecondaryEmailResult
        """
    return cls('unavailable', val)


def day_of_year(localdate):
    """
    returns the day of year [1..365 or 366] for a particular date and time

    input:
    - date and time in Python datetime format including timezone
    (time and timezone may be omitted)

    returns:
    - day of year 'd', integer
    the returned day of year is for the UTC date and time corresponding
    to a local date, time, and timezone. If time or timezone are omitted,
    the day od year 'd' refers to the local date.

    """
    d = localdate.utctimetuple().tm_yday
    return d


def limit_offset(builder):
    """
  helper function to append the limit and offset suffix
  :param builder: the current query builder
  :return:
  """
    return builder.append(' LIMIT {limit} OFFSET {offset}').replace('limit',
        int).replace('offset', int).arg('query')


def iter_2_range(iterator, has_left_outer=True, has_right_outer=True):
    """
    将一个 N 长度的 iterator 生成 N + 1 个区间
    例如：[1,2,3] --> [[None, 1], [1, 2] [2, 3] [3, None]]
    :param iterator:
    :param has_left_outer:
    :param has_right_outer:
    :return:
    """
    last_val = None
    for val in iterator:
        if last_val is not None or has_left_outer:
            yield last_val, val
        last_val = val
    else:
        if last_val is not None and has_right_outer:
            yield last_val, None


@staticmethod
def __process_path(filepath):
    """
        This will retrieve data from the file
        """
    in_file = open(filepath, 'r')
    regions = []
    for line in in_file:
        if line.startswith('#'):
            continue
        line = line.strip('\n').split('\t')
        regions.append(line)
    return regions


def remove(func):
    """Remove all elements from the queue for which func(element) is true,
        and return the number of removed elements.
        """


@staticmethod
def readfile(file_path):
    """
        return the content of the file <file_path>
        :param file_path: File path to read
        :return: Str content of the file
        """
    file_to_read = open(file_path, 'r')
    return file_to_read.read()


@classmethod
def block_header(cls, block, height):
    """Return the block header bytes"""
    deserializer = cls.DESERIALIZER(block)
    return deserializer.read_header(cls.HEADER_SIZE_NO_SOLUTION)


def is_fits(filename):
    """
    Checks if file is a FITS file based on extension

    Parameters
    ----------
    filename : str

    Returns
    -------
    flag : bool

    """
    flag = False
    if filename is not None:
        if filename.split('.')[-1].lower() in 'fits':
            flag = True
    return flag


@classmethod
def from_fields(cls, field_list):
    """Make an event from a list for (name, value) field tuples."""
    field_dict = dict(field_list)
    return cls(**field_dict)


def dict_lookup(dict):
    """
    Lookup a value in dictionary
    :param dict: Dictionary to use
    :return: Lambda-function to perform the lookup in the dictionary
    """
    return lambda x: dict[x]


def d(value: bytes) ->str:
    """
    Decode a bytestring for interpolating into an error message.

    """
    return value.decode(errors='backslashreplace')


def _dummy_log(data, level, ctx, fmt, args):
    """
    A dummy callback function for VLC so it doesn't write to stdout.
    Should probably do something in the future
    """
    pass


@classmethod
def create(cls, name, sections):
    """Create a template with the specified name and sections.

        Parameters
        ----------
        name : str
            the name of the template. Must be unique for your user.
        sections : list
            list of section objects

        Returns
        -------
        template : ComplianceDocTemplate
            the created template
        """
    resp = cls._client.post(cls._root_path, data={'name': name, 'sections':
        sections})
    return cls.from_location(resp.headers['Location'])


def order_chunks(hub, chunks):
    """
    Sort the chunk list verifying that the chunks follow the order
    specified in the order options.
    """
    cap = 1
    for chunk in chunks:
        if 'order' in chunk:
            if not isinstance(chunk['order'], int):
                continue
            chunk_order = chunk['order']
            if chunk_order > cap - 1 and chunk_order > 0:
                cap = chunk_order + 100
    for chunk in chunks:
        if 'order' not in chunk:
            chunk['order'] = cap
            continue
        if not isinstance(chunk['order'], (int, float)):
            if chunk['order'] == 'last':
                chunk['order'] = cap + 1000000
            elif chunk['order'] == 'first':
                chunk['order'] = 0
            else:
                chunk['order'] = cap
        if 'name_order' in chunk:
            chunk['order'] = chunk['order'] + chunk.pop('name_order') / 10000.0
        if chunk['order'] < 0:
            chunk['order'] = cap + 1000000 + chunk['order']
    chunks.sort(key=lambda chunk: (chunk['order'],
        '{0[state]}{0[name]}{0[fun]}'.format(chunk)))
    return chunks


@classmethod
def _get_where_no_index(cls, var_name, value, new_args=None, new_kwargs=None):
    """
        Generator for objects, loading all to filter on var_name
        """
    all_objs = cls.all(new_args=new_args, new_kwargs=new_kwargs)
    for obj in all_objs:
        if getattr(obj, var_name) == value:
            yield obj


@classmethod
def _load(cls, item_data):
    """ Load an item from the dictionary provided. """
    new_item = cls()
    if 'id' in item_data:
        new_item._id = item_data['id']
    for arg, value in item_data['config'].items():
        setattr(new_item, arg, value)
    return new_item


def onPlayerSync(game, player, oldPlayers):
    """
    Event Hook: onPlayerSync
    Apply the updates to other player attributes from the server
    """
    isRiding = False
    if game.player.ridingEntity:
        vehicle = game.getVehicle(game.player.ridingEntity)
        if vehicle:
            isRiding = game.player.name in vehicle.riders['other']
        else:
            print(game.player.ridingEntity)
    if player.name == game.player.name:
        if isRiding:
            game.player.health = player.health
            game.player.pos = player.pos
        return
    for p in range(len(oldPlayers)):
        if oldPlayers[p].name == player.name:
            oldPlayers[p].health = player.health
            if oldPlayers[p].ridingEntity:
                oldPlayers[p].pos = game.getVehicle(oldPlayers[p].ridingEntity
                    ).pos
            else:
                oldPlayers[p].lastPos = oldPlayers[p].pos
                props = oldPlayers[p].getProperty('worldUpdate')
                props.newPos = player.pos
                props.updateTick = game.tick
                oldPlayers[p].setProperty('worldUpdate', props)
            return
    prop = game.getModInstance('ClientMod').worldUpdateProperty.copy()
    prop.newPos = player.pos
    player.setProperty('worldUpdate', prop)
    oldPlayers.append(player)


@classmethod
def fromiterable(cls, itr):
    """Initialize from iterable"""
    x, y, z = itr
    return cls(x, y, z)


def get_location_id(manager, location):
    """Returns location id

    :param manager: The storage manager which calls this function.
    :param location: Datacenter short name
    :return: Returns location id
    """
    loc_svc = manager.client['Location_Datacenter']
    datacenters = loc_svc.getDatacenters(mask='mask[longName,id,name]')
    for datacenter in datacenters:
        if datacenter['name'] == location:
            location = datacenter['id']
            return location
    raise ValueError('Invalid datacenter name specified.')


@staticmethod
def to_fahrenheit(celsius):
    """ Convert celsius to fahrenheit """
    return celsius * 9 / 5 + 32


def unlock():
    """Unlock the active strip so that it can’t be transformed

    """
    pass


def point_inside_circle(x, y, center_x, center_y, radius):
    """Check if a point is inside a circle.

    Args:
        x (float): x coordinate of the point.
        y (float): y coordinate of the point.
        center_x (float): x coordinate of the center of the circle.
        center_y (float): y coordinate of the center of the circle.
        radius (float): radius of the circle (m).

    Returns:
        bool: True if the point is inside the circle.

    """
    dx = abs(x - center_x)
    dy = abs(y - center_y)
    if dx > radius:
        return False
    if dy > radius:
        return False
    if dx + dy <= radius:
        return True
    if dx ** 2 + dy ** 2 <= radius ** 2:
        return True
    else:
        return False


def extract_nodes(soup, nodename, attr=None, value=None):
    """
    Returns a list of tags (nodes) from the given soup matching the given nodename.
    If an optional attribute and value are given, these are used to filter the results
    further."""
    tags = soup.find_all(nodename)
    if attr != None and value != None:
        return list(filter(lambda tag: tag.get(attr) == value, tags))
    return list(tags)


@classmethod
def _download_contact(cls, mm_contact, file):
    """
        Specialized version of .download_media() for contacts.
        Will make use of the vCard 4.0 format.
        """
    first_name = mm_contact.first_name
    last_name = mm_contact.last_name
    phone_number = mm_contact.phone_number
    first_name = first_name.replace(';', '')
    last_name = (last_name or '').replace(';', '')
    result = (
        """BEGIN:VCARD
VERSION:4.0
N:{f};{l};;;
FN:{f} {l}
TEL;TYPE=cell;VALUE=uri:tel:+{p}
END:VCARD
"""
        .format(f=first_name, l=last_name, p=phone_number).encode('utf-8'))
    if file is bytes:
        return result
    elif isinstance(file, str):
        file = cls._get_proper_filename(file, 'contact', '.vcard',
            possible_names=[first_name, phone_number, last_name])
        f = open(file, 'wb')
    else:
        f = file
    try:
        f.write(result)
    finally:
        if isinstance(file, str):
            f.close()
    return file


def psi_Ba_Cs_Cl_PK74(T, P):
    """c-c'-a: barium caesium chloride [PK74]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def print_csv(secrets):
    """print csv formatted key,value pairs to stdout"""
    print('Key,Value')
    for secret in secrets:
        for key in secret:
            print(f'{key},{secret[key]}')


def parse_url_name_args(string):
    """
    Parse and return url_name and kwargs as a tuple from the node's
    url_name parameter (which can be just the url_name or additionally define
    some kwargs)

    Example: node['url_name'] = 'url_name|kwarg1:value,kwarg2:value'

    """
    chunks = string.split('|')
    url_name = chunks[0]
    kwargs = {}
    if len(chunks) > 1:
        for pair in chunks[1].split(','):
            k, v = pair.strip().split(':')
            k = k.strip()
            v = v.strip()
            if k and v:
                kwargs[k] = v
    return url_name, kwargs


def pad_packed_tensor(input, lengths, value, l_min=None):
    """Pads a packed batch of variable length tensors with given value.

    Parameters
    ----------
    input : Tensor
        The input tensor with shape :math:`(N, *)`
    lengths : list or tensor
        The array of tensor lengths (of the first dimension) :math:`L`.
        It should satisfy :math:`\\sum_{i=1}^{B}L_i = N`,
        where :math:`B` is the length of :math:`L`.
    value : float
        The value to fill in the tensor.
    l_min : int or None, defaults to None.
        The minimum length each tensor need to be padded to, if set to None,
        then there is no minimum length requirement.

    Returns
    -------
    Tensor
        The obtained tensor with shape :math:`(B, \\max(\\max_i(L_i), l_{min}), *)`
    """
    pass


def _validate_names(names):
    """
    Raise ValueError if the `names` parameter contains duplicates.

    Parameters
    ----------
    names : array-like or None
        An array containing a list of the names used for the output DataFrame.

    Raises
    ------
    ValueError
        If names are not unique.
    """
    if names is not None:
        if len(names) != len(set(names)):
            raise ValueError('Duplicate names are not allowed.')


@staticmethod
def p_characteristic_optional(p):
    """characteristic_optional : display_identifier
                                   | format
                                   | byte_order
                                   | bit_mask
                                   | function_list
                                   | number
                                   | extended_limits
                                   | read_only
                                   | guard_rails
                                   | map_list
                                   | max_refresh
                                   | dependent_characteristic
                                   | virtual_characteristic
                                   | ref_memory_segment
                                   | annotation
                                   | comparison_quantity
                                   | if_data_characteristic
                                   | axis_descr
                                   | calibration_access
                                   | matrix_dim
                                   | ecu_address_extension"""
    p[0] = p.slice[1].type, p[1]


@classmethod
def get_password_hashers(cls):
    """Return all the PasswordHasher supported by Modoboa"""
    return cls.__subclasses__()


def __getitem__(menu_id):
    """Get menu information

        Return a sequence of dictionaries with labels and
        actions, where actions are relative URLs.
        """


def assign_to_vector(v, a):
    """
    Assign the value of the array a to the dolfin vector v
    """
    lr = v.local_range()
    v[:] = a[lr[0]:lr[1]]


def patch_fetch_format(options):
    """
    When upload type is fetch, remove the format options.
    In addition, set the fetch_format options to the format value unless it was already set.
    Mutates the options parameter!

    :param options: URL and transformation options
    """
    if options.get('type', 'upload') != 'fetch':
        return
    resource_format = options.pop('format', None)
    if 'fetch_format' not in options:
        options['fetch_format'] = resource_format


def upper(value):
    """A safe version of upper"""
    if not value:
        return None
    return value.upper()


def graphics_listen_addrs(migrate_data):
    """Returns listen addresses of vnc/spice from a LibvirtLiveMigrateData"""
    listen_addrs = None
    if migrate_data.obj_attr_is_set('graphics_listen_addr_vnc'
        ) or migrate_data.obj_attr_is_set('graphics_listen_addr_spice'):
        listen_addrs = {'vnc': None, 'spice': None}
    if migrate_data.obj_attr_is_set('graphics_listen_addr_vnc'):
        listen_addrs['vnc'] = str(migrate_data.graphics_listen_addr_vnc)
    if migrate_data.obj_attr_is_set('graphics_listen_addr_spice'):
        listen_addrs['spice'] = str(migrate_data.graphics_listen_addr_spice)
    return listen_addrs


@staticmethod
def complex_concentration(system_parameters: dict, y):
    """  Readout as complex concentration, (redundant as NULL achieves the same)"""
    return None, y


@classmethod
def _from_dict(cls, _dict):
    """Initialize a Nested object from a json dictionary."""
    args = {}
    if 'path' in _dict:
        args['path'] = _dict.get('path')
    return cls(**args)


def remove_task_name(apps, schema_editor):
    """
    This doesn't need to be implemented as the task names will be deleted when the field is removed
    """
    return


def get_service_type(f):
    """Retrieves service type from function."""
    return getattr(f, 'service_type', None)


def add_harmonic_contribution(input_dict, E_sampling, Cv_sampling):
    """ adds the constant harmonic contribution to the energy and the heat capacity """
    input_dict['E'] += E_sampling
    input_dict['Cv'] += Cv_sampling
    return


@staticmethod
def mimetype():
    """Return most common media type for format."""
    return 'text/yaml'


def add_ri(data):
    """
    Add real and imaginary components.

    Parameters
    ----------
    data : ndarray
        Array of complex NMR data.

    Returns
    -------
    ndata : ndarray
        Sum of real and imaginary component of NMR data.

    """
    return data.real + data.imag


@classmethod
def _cleanup_iso_image(cls, task):
    """Deletes the ISO if it was created for the instance.

        :param task: an ironic node object.
        """
    iso_object_name = cls._get_iso_image_name(task.node)
    cls._unpublish_image(iso_object_name)


def calc_L_curve(model, timedata, ax=None, plot=False, nLam=150, lam_max=
    100.0, lam_min=0.001):
    """
	Function to calculate the L-curve for a given model and timedata
	instance in order to choose the best-fit smoothing parameter, `lambda`.

	Parameters
	----------
	model : rp.Model
		``rp.Model`` instance containing the A matrix to use for L curve
		calculation.

	timedata : rp.TimeData
		``rp.TimeData`` instance containing the time and fraction remaining
		arrays to use in L curve calculation.

	Keyword Arguments
	-----------------
	ax : None or matplotlib.axis
		Axis to plot on. If `None` and ``plot = True``, automatically
		creates a ``matplotlip.axis`` instance to return. Defaults to
		`None`.

	plot : Boolean
		Tells the method to plot the resulting L curve or not.

	lam_min : int
		Minimum lambda value to search. Defaults to 1e-3.

	lam_max : int
		Maximum lambda value to search. Defaults to 1e2.

	nLam : int
		Number of lambda values to consider. Defaults to 150.

	Returns
	-------
	lam_best : float
		The calculated best-fit lambda value.

	axis : None or matplotlib.axis
		If ``plot = True``, returns an updated axis handle with plot.

	Raises
	------
	ScalarError
		If `lam_max` or `lam_min` are not int or float.

	ScalarError
		If `nLam` is not int.

	See Also
	--------
	Daem.calc_L_curve
		Instance method for ``calc_L_curve``.

	References
	----------
	[1] D.C. Forney and D.H. Rothman (2012) Inverse method for calculating
		respiration rates from decay time series. *Biogeosciences*, **9**,
		3601-3612.

	[2] P.C. Hansen (1994) Regularization tools: A Matlab package for analysis
		and solution of discrete ill-posed problems. *Numerical Algorithms*,
		**6**, 1-35.
	"""
    a = model.calc_L_curve(timedata, ax=ax, plot=plot, nLam=nLam, lam_max=
        lam_max, lam_min=lam_max)
    return a


def t_BOOLEAN(t):
    """([Tt][Rr][Uu][Ee])|([Ff][Aa][Ll][Ss][Ee])"""
    t.value = False if t.value.lower() == 'false' else True
    return t


def connect_all(states):
    """
	Connects a list of state objects sequentially with probability 1. The first and
	last states are unconnected.

	Parameters
	----------
	+ states `list` state objects, probably all related (e.g. donors)
	"""
    for i in range(len(states) - 1):
        states[i].next[states[i + 1].name] = 1


def count_files(local_folder, reporter):
    """
    Count all of the files in a local folder.

    :param b2sdk.sync.folder.AbstractFolder local_folder: a folder object.
    :param reporter: reporter object
    """
    for _ in local_folder.all_files(None):
        reporter.update_local(1)
    reporter.end_local()


def guess_extension(content_type):
    """
    Python's `mimetypes.guess_extension` is no use because it simply returns
    the first of an unordered set. We use the same set of media types here,
    but take a reasonable preference on what extension to map to.
    """
    return {'application/javascript': '.js', 'application/msword': '.doc',
        'application/octet-stream': '.bin', 'application/oda': '.oda',
        'application/pdf': '.pdf', 'application/pkcs7-mime': '.p7c',
        'application/postscript': '.ps', 'application/vnd.apple.mpegurl':
        '.m3u', 'application/vnd.ms-excel': '.xls',
        'application/vnd.ms-powerpoint': '.ppt', 'application/x-bcpio':
        '.bcpio', 'application/x-cpio': '.cpio', 'application/x-csh':
        '.csh', 'application/x-dvi': '.dvi', 'application/x-gtar': '.gtar',
        'application/x-hdf': '.hdf', 'application/x-latex': '.latex',
        'application/x-mif': '.mif', 'application/x-netcdf': '.nc',
        'application/x-pkcs12': '.p12', 'application/x-pn-realaudio':
        '.ram', 'application/x-python-code': '.pyc', 'application/x-sh':
        '.sh', 'application/x-shar': '.shar',
        'application/x-shockwave-flash': '.swf', 'application/x-sv4cpio':
        '.sv4cpio', 'application/x-sv4crc': '.sv4crc', 'application/x-tar':
        '.tar', 'application/x-tcl': '.tcl', 'application/x-tex': '.tex',
        'application/x-texinfo': '.texinfo', 'application/x-troff': '.tr',
        'application/x-troff-man': '.man', 'application/x-troff-me': '.me',
        'application/x-troff-ms': '.ms', 'application/x-ustar': '.ustar',
        'application/x-wais-source': '.src', 'application/xml': '.xml',
        'application/zip': '.zip', 'audio/basic': '.au', 'audio/mpeg':
        '.mp3', 'audio/x-aiff': '.aif', 'audio/x-pn-realaudio': '.ra',
        'audio/x-wav': '.wav', 'image/gif': '.gif', 'image/ief': '.ief',
        'image/jpeg': '.jpe', 'image/png': '.png', 'image/svg+xml': '.svg',
        'image/tiff': '.tiff', 'image/vnd.microsoft.icon': '.ico',
        'image/x-cmu-raster': '.ras', 'image/x-ms-bmp': '.bmp',
        'image/x-portable-anymap': '.pnm', 'image/x-portable-bitmap':
        '.pbm', 'image/x-portable-graymap': '.pgm',
        'image/x-portable-pixmap': '.ppm', 'image/x-rgb': '.rgb',
        'image/x-xbitmap': '.xbm', 'image/x-xpixmap': '.xpm',
        'image/x-xwindowdump': '.xwd', 'message/rfc822': '.eml', 'text/css':
        '.css', 'text/csv': '.csv', 'text/html': '.html', 'text/plain':
        '.txt', 'text/richtext': '.rtx', 'text/tab-separated-values':
        '.tsv', 'text/x-python': '.py', 'text/x-setext': '.etx',
        'text/x-sgml': '.sgml', 'text/x-vcard': '.vcf', 'text/xml': '.xml',
        'video/mp4': '.mp4', 'video/mpeg': '.mpeg', 'video/quicktime':
        '.mov', 'video/webm': '.webm', 'video/x-msvideo': '.avi',
        'video/x-sgi-movie': '.movie'}.get(content_type, '')


def relative_script(lines):
    """Return a script that'll work in a relocatable environment."""
    activate = (
        "import os; activate_this=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'activate_this.py'); exec(compile(open(activate_this).read(), activate_this, 'exec'), dict(__file__=activate_this)); del os, activate_this"
        )
    activate_at = None
    for idx, line in reversed(list(enumerate(lines))):
        if line.split()[:3] == ['from', '__future__', 'import']:
            activate_at = idx + 1
            break
    if activate_at is None:
        activate_at = 1
    return lines[:activate_at] + ['', activate, ''] + lines[activate_at:]


def _up_sample_tick_to_tick(df, target_index):
    """Up sample tick data to a tick based target index."""
    new_df = df.resample(target_index.freq).ffill()
    return new_df


def indexes_to_path(indexes):
    """
    Change derivation indexes to path.

    :param indexes: Bytom derivation indexes.
    :type indexes: list.
    :return: str -- Bytom derivation path.

    >>> from pybytom.wallet.tools import indexes_to_path
    >>> indexes_to_path(["2c000000", "99000000", "01000000", "00000000", "01000000"])
    "m/44/153/1/0/1"
    """
    if not isinstance(indexes, list):
        raise TypeError('indexes must be list format')
    path = 'm/'
    for i, index in enumerate(indexes, 1):
        number = int.from_bytes(bytes.fromhex(index), byteorder='little')
        if i == len(indexes):
            path = path + str(number)
        else:
            path = path + str(number) + '/'
    return path


@classmethod
def readable_fields(cls):
    """Get all metadata fields: the writable ones from
        :meth:`fields` and also other audio properties.
        """
    for property in cls.fields():
        yield property
    for property in ('length', 'samplerate', 'bitdepth', 'bitrate',
        'channels', 'format'):
        yield property


@classmethod
def configure_arggroup(cls, parser):
    """Configure argument parser."""
    parser.add_argument('--no-phenoecm', action='store_true', help=
        'Disables the phenomenological ECM model building.')
    parser.add_argument('--iterations', type=int, default=10, help=
        'Number of outer-loop iterations (e.g. time loop) during benchmarking. Default is 10, but actual number will be adapted to at least 0.2s runtime.'
        )
    parser.add_argument('--ignore-warnings', action='store_true', help=
        'Ignore warnings about missmatched CPU model and frequency.')


def clip_(in_tensor, max_or_min, min_or_max):
    """limit the values in in_tensor to be within [min,max].
    values larger than max will be cut to max, respectively for mins.
    the order of max/min doesn't matter.
    the operation is not in-place, that saves you alot troubles.
    Notice: this clip is not inplace.
            But, for clipped values, the gradients will always be passed backwards.
            This is useful when you want `clip` as a value-normalizer, but don not want the gradient to vanish.
    """
    minv, maxv = sorted((max_or_min, min_or_max))
    greater_pos = (in_tensor.detach() > maxv).type_as(in_tensor)
    diff = (maxv - in_tensor.detach()) * greater_pos
    in_tensor = in_tensor + diff
    less_pos = (in_tensor.detach() < minv).type_as(in_tensor)
    diff = (minv - in_tensor.detach()) * less_pos
    in_tensor = in_tensor + diff
    return in_tensor


def set_device_context(dev, ctx):
    """
    Set a device as current active device

    Parameters
    ----------

    dev  : cl_device_id

    ctx  : cl_context

    """
    import ctypes as ct
    from .util import safe_call as safe_call
    from .library import backend
    if backend.name() != 'opencl':
        raise RuntimeError('Invalid backend loaded')
    safe_call(backend.get().afcl_set_device_context(dev, ctx))


def in_region(pos, regions):
    """Find whether a position is included in a region.

    Parameters
    ----------
    pos : int
        DNA base position.
    regions : list of tuples
        List of (start, end) position integers.

    Returns
    -------
    bool
        True if the position is within an of the regions, False otherwise.

    Examples
    --------
    # Empty list
    >>> in_region(1, [])
    False

    # In list
    >>> in_region(10, [(3, 5), (9, 12)])
    True

    # Not in list
    >>> in_region(10, [(3, 5), (11, 12)])
    False
    """
    for region in regions:
        if pos >= region[0] and pos <= region[1]:
            return True
    return False


def __init__(context):
    """Adapts a context.
        """


def update_stack(StackName=None, TemplateBody=None, TemplateURL=None,
    UsePreviousTemplate=None, StackPolicyDuringUpdateBody=None,
    StackPolicyDuringUpdateURL=None, Parameters=None, Capabilities=None,
    ResourceTypes=None, RoleARN=None, StackPolicyBody=None, StackPolicyURL=
    None, NotificationARNs=None, Tags=None, ClientRequestToken=None):
    """
    Updates a stack as specified in the template. After the call completes successfully, the stack update starts. You can check the status of the stack via the  DescribeStacks action.
    To get a copy of the template for an existing stack, you can use the  GetTemplate action.
    For more information about creating an update template, updating a stack, and monitoring the progress of the update, see Updating a Stack .
    See also: AWS API Documentation

    Examples
    This example adds two stack notification topics to the specified stack.
    Expected Output:

    :example: response = client.update_stack(
        StackName='string',
        TemplateBody='string',
        TemplateURL='string',
        UsePreviousTemplate=True|False,
        StackPolicyDuringUpdateBody='string',
        StackPolicyDuringUpdateURL='string',
        Parameters=[
            {
                'ParameterKey': 'string',
                'ParameterValue': 'string',
                'UsePreviousValue': True|False
            },
        ],
        Capabilities=[
            'CAPABILITY_IAM'|'CAPABILITY_NAMED_IAM',
        ],
        ResourceTypes=[
            'string',
        ],
        RoleARN='string',
        StackPolicyBody='string',
        StackPolicyURL='string',
        NotificationARNs=[
            'string',
        ],
        Tags=[
            {
                'Key': 'string',
                'Value': 'string'
            },
        ],
        ClientRequestToken='string'
    )

    :type StackName: string
    :param StackName: [REQUIRED]
            The name or unique stack ID of the stack to update.

    :type TemplateBody: string
    :param TemplateBody: Structure containing the template body with a minimum length of 1 byte and a maximum length of 51,200 bytes. (For more information, go to Template Anatomy in the AWS CloudFormation User Guide.)
            Conditional: You must specify only one of the following parameters: TemplateBody , TemplateURL , or set the UsePreviousTemplate to true .

    :type TemplateURL: string
    :param TemplateURL: Location of file containing the template body. The URL must point to a template that is located in an Amazon S3 bucket. For more information, go to Template Anatomy in the AWS CloudFormation User Guide.
            Conditional: You must specify only one of the following parameters: TemplateBody , TemplateURL , or set the UsePreviousTemplate to true .

    :type UsePreviousTemplate: boolean
    :param UsePreviousTemplate: Reuse the existing template that is associated with the stack that you are updating.
            Conditional: You must specify only one of the following parameters: TemplateBody , TemplateURL , or set the UsePreviousTemplate to true .

    :type StackPolicyDuringUpdateBody: string
    :param StackPolicyDuringUpdateBody: Structure containing the temporary overriding stack policy body. You can specify either the StackPolicyDuringUpdateBody or the StackPolicyDuringUpdateURL parameter, but not both.
            If you want to update protected resources, specify a temporary overriding stack policy during this update. If you do not specify a stack policy, the current policy that is associated with the stack will be used.

    :type StackPolicyDuringUpdateURL: string
    :param StackPolicyDuringUpdateURL: Location of a file containing the temporary overriding stack policy. The URL must point to a policy (max size: 16KB) located in an S3 bucket in the same region as the stack. You can specify either the StackPolicyDuringUpdateBody or the StackPolicyDuringUpdateURL parameter, but not both.
            If you want to update protected resources, specify a temporary overriding stack policy during this update. If you do not specify a stack policy, the current policy that is associated with the stack will be used.

    :type Parameters: list
    :param Parameters: A list of Parameter structures that specify input parameters for the stack. For more information, see the Parameter data type.
            (dict) --The Parameter data type.
            ParameterKey (string) --The key associated with the parameter. If you don't specify a key and value for a particular parameter, AWS CloudFormation uses the default value that is specified in your template.
            ParameterValue (string) --The value associated with the parameter.
            UsePreviousValue (boolean) --During a stack update, use the existing parameter value that the stack is using for a given parameter key. If you specify true , do not specify a parameter value.

    :type Capabilities: list
    :param Capabilities: A list of values that you must specify before AWS CloudFormation can update certain stacks. Some stack templates might include resources that can affect permissions in your AWS account, for example, by creating new AWS Identity and Access Management (IAM) users. For those stacks, you must explicitly acknowledge their capabilities by specifying this parameter.
            The only valid values are CAPABILITY_IAM and CAPABILITY_NAMED_IAM . The following resources require you to specify this parameter: AWS::IAM::AccessKey , AWS::IAM::Group , AWS::IAM::InstanceProfile , AWS::IAM::Policy , AWS::IAM::Role , AWS::IAM::User , and AWS::IAM::UserToGroupAddition . If your stack template contains these resources, we recommend that you review all permissions associated with them and edit their permissions if necessary.
            If you have IAM resources, you can specify either capability. If you have IAM resources with custom names, you must specify CAPABILITY_NAMED_IAM . If you don't specify this parameter, this action returns an InsufficientCapabilities error.
            For more information, see Acknowledging IAM Resources in AWS CloudFormation Templates .
            (string) --

    :type ResourceTypes: list
    :param ResourceTypes: The template resource types that you have permissions to work with for this update stack action, such as AWS::EC2::Instance , AWS::EC2::* , or Custom::MyCustomInstance .
            If the list of resource types doesn't include a resource that you're updating, the stack update fails. By default, AWS CloudFormation grants permissions to all resource types. AWS Identity and Access Management (IAM) uses this parameter for AWS CloudFormation-specific condition keys in IAM policies. For more information, see Controlling Access with AWS Identity and Access Management .
            (string) --

    :type RoleARN: string
    :param RoleARN: The Amazon Resource Name (ARN) of an AWS Identity and Access Management (IAM) role that AWS CloudFormation assumes to update the stack. AWS CloudFormation uses the role's credentials to make calls on your behalf. AWS CloudFormation always uses this role for all future operations on the stack. As long as users have permission to operate on the stack, AWS CloudFormation uses this role even if the users don't have permission to pass it. Ensure that the role grants least privilege.
            If you don't specify a value, AWS CloudFormation uses the role that was previously associated with the stack. If no role is available, AWS CloudFormation uses a temporary session that is generated from your user credentials.

    :type StackPolicyBody: string
    :param StackPolicyBody: Structure containing a new stack policy body. You can specify either the StackPolicyBody or the StackPolicyURL parameter, but not both.
            You might update the stack policy, for example, in order to protect a new resource that you created during a stack update. If you do not specify a stack policy, the current policy that is associated with the stack is unchanged.

    :type StackPolicyURL: string
    :param StackPolicyURL: Location of a file containing the updated stack policy. The URL must point to a policy (max size: 16KB) located in an S3 bucket in the same region as the stack. You can specify either the StackPolicyBody or the StackPolicyURL parameter, but not both.
            You might update the stack policy, for example, in order to protect a new resource that you created during a stack update. If you do not specify a stack policy, the current policy that is associated with the stack is unchanged.

    :type NotificationARNs: list
    :param NotificationARNs: Amazon Simple Notification Service topic Amazon Resource Names (ARNs) that AWS CloudFormation associates with the stack. Specify an empty list to remove all notification topics.
            (string) --

    :type Tags: list
    :param Tags: Key-value pairs to associate with this stack. AWS CloudFormation also propagates these tags to supported resources in the stack. You can specify a maximum number of 10 tags.
            If you don't specify this parameter, AWS CloudFormation doesn't modify the stack's tags. If you specify an empty value, AWS CloudFormation removes all associated tags.
            (dict) --The Tag type enables you to specify a key-value pair that can be used to store information about an AWS CloudFormation stack.
            Key (string) --
            Required . A string used to identify this tag. You can specify a maximum of 128 characters for a tag key. Tags owned by Amazon Web Services (AWS) have the reserved prefix: aws: .
            Value (string) --
            Required . A string containing the value for this tag. You can specify a maximum of 256 characters for a tag value.

    :type ClientRequestToken: string
    :param ClientRequestToken: A unique identifier for this UpdateStack request. Specify this token if you plan to retry requests so that AWS CloudFormation knows that you're not attempting to update a stack with the same name. You might retry UpdateStack requests to ensure that AWS CloudFormation successfully received them.

    :rtype: dict
    :return: {
        'StackId': 'string'
    }

    """
    pass


def proxnonconv_square(Z, r, p, gamma, mode=None, tol=1e-12):
    """
    Non-convex prox of half of the squared Frobenius
    and spectral norm + indicator function of a matrix with at most rank r.

    X = PROXNONCONV_SQUARE(Z,r,p,gamma) determines the prox of
    0.5*gamma*||X||_{ell_p}^2+i_{rank(X)<=r}(X), where i_{rank(X)<=r} is the
    idicator function of all matrices with at most rank r. This is, X is a
    solution to the optimization problem:

        minimize_X 0.5*gamma*||X||_{ell_p}^2+0.5*||X-Z||_{ell_2}^2
                s.t. rank(X) <= r

    where p=2 or p='inf'.

    X = PROXNONCONV(Z,r,p,gamma,option) allows us to specify furhter options:
        1. X = PROXNONCONV(Z,r,p,gamma,...,mode='vec') is used to flag that the
        vector-valued problem is to be solved.
        2. X = PROXNONCONV(Z,r,p,gamma,...,tol=tol_val) sets the relative
        tolerance of the deciding about zeros e.g. if Z is matrix then
        for all i: sigma(X)_i = 0 if |sigma(X)_i| <= tol_val*||Z||_{ell_p,r).
        Default value: tol_val = 1e-12.

    --------------
    References:
    - C. Grussler and A. Rantzer and P. Giselsson (2018):
    "Low-Rank Optimization with Convex Constraints",
    IEEE Transactions on Automatic Control, DOI: 10.1109/TAC.2018.2813009.

    - C. Grussler and P. Giselsson (2016):
    "Low-Rank Inducing Norms With Optimality Interpreations",
    arXiv:1612.03186v1.

    - C. Grussler and P. Giselsson (2017):
    "Local convergence of proximal splitting methods for rank constrained
    problems", pp. 702-708, IEEE 56th Annual Conference on Decision and Control
    (CDC), DOI: 10.1109/CDC.2017.8263743.

    - C. Grussler (2017):
    "Rank reduction with convex constraints", PhD Thesis,
    Department of Automatic Control, Lund Institute of Technology,
    Lund University, ISBN 978-91-7753-081-7.
    """
    from lripy.proxnormrast_square import proxnormrast_square
    import numpy as np
    if p != 2 and p != 'inf':
        raise ValueError('p can only be equal to 2 or inf')
    dim = Z.shape
    max_mn = np.max(dim)
    min_mn = np.min(dim)
    if mode == 'vec':
        if r > max_mn:
            raise ValueError('r is larger than max(Z.shape)')
    elif r > min_mn:
        raise ValueError('r is larger than min(Z.shape)')
    U, S, V = np.linalg.svd(Z, full_matrices=False)
    Z = U[:, 0:r].dot(np.diag(S[0:r])).dot(V[0:r, :])
    X, _ = proxnormrast_square(Z, r, p, gamma, mode, tol=tol)
    return X


@classmethod
def get_server(cls):
    """
        Return an instance of `server_class` initialized with a `validator_class`
        object
        """
    server_class = cls.get_server_class()
    validator_class = cls.get_validator_class()
    return server_class(validator_class())


def to_data(value):
    """Standardize data types. Converts PyTorch tensors to Numpy arrays,
    and Numpy scalars to Python scalars."""
    if value.__class__.__module__.startswith('torch'):
        import torch
        if isinstance(value, torch.nn.parameter.Parameter):
            value = value.data
        if isinstance(value, torch.Tensor):
            if value.requires_grad:
                value = value.detach()
            value = value.cpu().numpy().copy()
        if not value.shape:
            value = value.item()
    if (value.__class__.__module__ == 'numpy' and value.__class__.__name__ !=
        'ndarray'):
        value = value.item()
    return value


def validate_one(word, desc, partial=False):
    """
    validate_one(word, desc, partial=False)

    validate word against the constructed instance of the type
    in desc.  May raise exception.  If it returns false (and doesn't
    raise an exception), desc.instance.val will
    contain the validated value (in the appropriate type).
    """
    desc.instance.valid(word, partial)
    desc.numseen += 1
    if desc.N:
        desc.n = desc.numseen + 1


def edit(text=None, editor=None, env=None, require_save=True, extension=
    '.txt', filename=None):
    """Edits the given text in the defined editor.  If an editor is given
    (should be the full path to the executable but the regular operating
    system search path is used for finding the executable) it overrides
    the detected editor.  Optionally, some environment variables can be
    used.  If the editor is closed without changes, `None` is returned.  In
    case a file is edited directly the return value is always `None` and
    `require_save` and `extension` are ignored.

    If the editor cannot be opened a :exc:`UsageError` is raised.

    Note for Windows: to simplify cross-platform usage, the newlines are
    automatically converted from POSIX to Windows and vice versa.  As such,
    the message here will have ``\\n`` as newline markers.

    :param text: the text to edit.
    :param editor: optionally the editor to use.  Defaults to automatic
                   detection.
    :param env: environment variables to forward to the editor.
    :param require_save: if this is true, then not saving in the editor
                         will make the return value become `None`.
    :param extension: the extension to tell the editor about.  This defaults
                      to `.txt` but changing this might change syntax
                      highlighting.
    :param filename: if provided it will edit this file instead of the
                     provided text contents.  It will not use a temporary
                     file as an indirection in that case.
    """
    from ._termui_impl import Editor
    editor = Editor(editor=editor, env=env, require_save=require_save,
        extension=extension)
    if filename is None:
        return editor.edit(text)
    editor.edit_file(filename)


def modifier_convert(modifier: str=''):
    """Convert particles to a mesh object

    :param modifier: Modifier, Name of the modifier to edit
    :type modifier: str
    """
    pass


def _lookup(t: str, types: [str]) ->int:
    """
    Looks up the value, t, from the array of types
    :param t: value to lookup
    :param types: list of types from which to lookup
    :return: an integer value > 0 if found, or 0 if not found
    """
    try:
        return types.index(t.strip().upper()) + 1
    except ValueError:
        return 0


def dump_np_dtype(obj, h5f, **kwargs):
    """ dumps an np dtype object to h5py file"""
    h5f.create_dataset('data', data=obj)
    h5f.create_dataset('type', data=['np_dtype'])


@staticmethod
def soft_update(local_model, target_model, tau):
    """Soft update model parameters.
        θ_target = τ*θ_local + (1 - τ)*θ_target
        Params
        ======
            local_model: PyTorch model (weights will be copied from)
            target_model: PyTorch model (weights will be copied to)
            tau (float): interpolation parameter
        """
    for target_param, local_param in zip(target_model.parameters(),
        local_model.parameters()):
        target_param.data.copy_(tau * local_param.data + (1.0 - tau) *
            target_param.data)


def bysize(words):
    """
    take a list of words and return a dict of sets sorted by word length
    e.g.
    ret[3]=set(['ant', 'cat', 'dog', 'pig'])
    ret[4]=set(['frog', 'goat'])
    ret[5]=set(['horse'])
    ret[8]=set(['elephant'])
    """
    ret = {}
    for w in words:
        if len(w) not in ret:
            ret[len(w)] = set()
        ret[len(w)].add(w)
    return ret


@classmethod
def get_request(cls, target, instance_key, dev_mode=False, args={},
    url_override=False):
    """
        public class method - make a get request to the api
        """
    operator = cls(instance_key, dev_mode=dev_mode)
    return operator.get_request_inner(target, args, url_override=url_override)


def t_filter_type(val):
    """
    Convert/validate: Reporting filter type.
    """
    if str(val) == 'simple':
        val = 'basic'
    return val


def render_pep440_pre(pieces):
    """TAG[.post.devDISTANCE] -- No -dirty.

    Exceptions:
    1: no tags. 0.post.devDISTANCE
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance']:
            rendered += '.post.dev%d' % pieces['distance']
    else:
        rendered = '0.post.dev%d' % pieces['distance']
    return rendered


def _create_model_request(name, role, container_def=None, tags=None):
    """Placeholder docstring"""
    request = {'ModelName': name, 'ExecutionRoleArn': role}
    if isinstance(container_def, list):
        request['Containers'] = container_def
    else:
        request['PrimaryContainer'] = container_def
    if tags:
        request['Tags'] = tags
    return request


def WMA(df, n, price='Close'):
    """
    Weighted Moving Average
    """


def getText(node):
    """Get child text nodes
    @param node: dom Node
    @return: joined unicode text of all nodes

    """
    data = []
    for child in node.childNodes:
        if child.nodeType == child.TEXT_NODE:
            data.append(child.wholeText)
    return u''.join(data)


def parse(idx_query):
    """Takes a query against a single index (the value part of a
        {'index_name': idx_query} pair).

        `idx_query` can therefore be
          - a single string value
          - a list of string values
          - a dictionary with one or more query options, among them the actual
            query value identified by the 'query' key

        Returns a transformed `idx_query` whose query options and query values
        have been reconstructed to the proper data types that the adapted
        index expects.
        """


def is_int(string):
    """ 检查字符串是不是合法的 int. validate if string is a valid int.

    Examples::

        if is_int('wrong string'): ...

    :rtype: bool
    """
    try:
        int(string)
        return True
    except ValueError:
        return False


def get_401_error_hint(ctx, opts, exc):
    """Get the hint for a 401/Unauthorised error."""
    if opts.api_key:
        return (
            "Since you have an API key set, this probably means you don't have the permision to perform this action."
            )
    if ctx.info_name == 'token':
        return (
            'The login failed - Either your email address and/or your password was incorrect. Please check them and try again!'
            )
    return (
        "You don't have an API key set, but it seems this action requires authentication - Try getting your API key via 'cloudsmith token' first then try again."
        )


def plus_or_dot(pieces):
    """Return a + if we don't already have one, else return a ."""
    if '+' in pieces.get('closest-tag', ''):
        return '.'
    return '+'


def distance(parallax, parallax_error):
    """Computes distance from Gaia parallaxes using the Bayesian method of Bailer-Jones 2015.
    Input: parallax [mas], parallax error [mas]
    Output: distance [pc], 1-sigma uncertainty in distance [pc]
    """
    import numpy as np
    L = 1350
    parallax, parallax_error = parallax / 1000.0, parallax_error / 1000.0
    coeff = np.array([1.0 / L, -2, parallax / parallax_error ** 2, -(1.0 /
        parallax_error ** 2)])
    g = np.roots(coeff)
    reals = np.isreal(g)
    realsum = np.sum(reals)
    if realsum == 1:
        gd = np.real(g[np.where(reals)[0]])
    elif realsum == 3:
        if parallax >= 0:
            gd = np.min(g)
        elif parallax < 0:
            gd = g[np.where(g > 0)[0]]
    from scipy.optimize import brentq
    rmax = 1000000.0
    rmode = gd[0]
    M = rmode ** 2 * np.exp(-rmode / L) / parallax_error * np.exp(-1.0 / (2 *
        parallax_error ** 2) * (parallax - 1.0 / rmode) ** 2)
    lo = brentq(lambda x: 2 * np.log(x) - x / L - (parallax - 1.0 / x) ** 2 /
        (2 * parallax_error ** 2) + np.log(2) - np.log(M) - np.log(
        parallax_error), 0.001, rmode)
    hi = brentq(lambda x: 2 * np.log(x) - x / L - (parallax - 1.0 / x) ** 2 /
        (2 * parallax_error ** 2) + np.log(2) - np.log(M) - np.log(
        parallax_error), rmode, rmax)
    fwhm = hi - lo
    sigma = fwhm / 2.355
    return gd[0], sigma


def runfcgi(func, addr=('localhost', 8000)):
    """Runs a WSGI function as a FastCGI server."""
    import flup.server.fcgi as flups
    return flups.WSGIServer(func, multiplexed=True, bindAddress=addr, debug
        =False).run()


@classmethod
def parse_request_title(cls, title):
    """Return the subreddit and view for the request."""
    match = cls.RE_REQUEST.match(title)
    return match.groupdict() if match else None


@staticmethod
def is_group(update):
    """
        wrapper to get to check wether message comes from group or not
        """
    return update.message.chat.type == 'group'


def _validate_websocket_connection(environment):
    """Checks if the websocket connection was established.

    If the client is not connected then tries to initialize the connection.

    :param environment: Environment used.
    :type environment: Environment (Enum).
    """
    if not globals.environment_config[environment]['ws_client'].is_connected():
        globals.environment_config[environment]['ws_client'].connect()


def verify_tersoff_2body_symmetry(tersoff_params):
    """
    Given a list of tersoff parameters, verify that they are such that
    the two-body parameters are symmetric.  What this means is that when
    we consider A-B B and B-A A, the two body parameters must be the same.
    This is necessary as LAMMPs will randomly isolate the two body
    interaction (A-B or B-A) and use the corresponding parameters.

    **Parameters**

        tersoff_params: *list,* :class:`squid.forcefields.tersoff.Tersoff`
            A list of Tersoff objects.

    **Returns**

        None
    """
    for i, ters_A in enumerate(tersoff_params):
        for j, ters_B in enumerate(tersoff_params):
            if i >= j:
                continue
            if all([ters_A.indices[0] == ters_B.indices[1], ters_A.indices[
                1] == ters_B.indices[0], ters_A.indices[1] == ters_A.
                indices[2], ters_B.indices[1] == ters_B.indices[2]]):
                assert all([ters_A.lambda1 == ters_B.lambda1, ters_A.
                    lambda2 == ters_B.lambda2, ters_A.A == ters_B.A, ters_A
                    .B == ters_B.B]
                    ), 'Error: 2-body symmetry requirements failed for %s' % str(
                    ters_A.indices[:2])


def write(fieldNode, schema, field):
    """Write the metadata for the field in the given schema to the
        fieldNode. The node will already exist and be populated with
        standard data.
        """


@classmethod
def from_moneyline_odds_on(cls, numerator=0, denominator=None):
    """Create a probability from moneyline odds against."""
    n, d = cls._format_moneyline(numerator, denominator)
    return cls(n, n + d)


def disable():
    """Disable the cache.
    """
    global _fftw_cache
    _fftw_cache = None


def extract_chromosome(header):
    """Extract chromosome information for epilocus header."""
    return header.split(';')[0].split(':')[0]


@staticmethod
def get_name():
    """
        The name of the template

        :return: The name of the template
        """
    return 'client-template'


def do_project(satellite):
    """Wrap the project method to use multiprocessing pool"""
    return satellite.project()


def add(message):
    """Add the message to the store.

        :param message: An email.message.Message instance containing at least
            a unique Message-ID header.  The message will be given an
            Message-ID-Hash header, overriding any existing such header.  If
            the message already exists in the store, it is not added again.
        :returns: The calculated Message-ID-Hash header or None if the message
            already exists in the store.
        :rtype: str or None
        :raises ValueError: if the message is missing a Message-ID header.
        """


def disable_alarm_actions(AlarmNames=None):
    """
    Disables the actions for the specified alarms. When an alarm's actions are disabled, the alarm actions do not execute when the alarm state changes.
    See also: AWS API Documentation

    :example: response = client.disable_alarm_actions(
        AlarmNames=[
            'string',
        ]
    )

    :type AlarmNames: list
    :param AlarmNames: [REQUIRED]
            The names of the alarms.
            (string) --

    """
    pass


def compat(data):
    """
    Check data type, transform to string if needed.

    Args:
        data: The data.

    Returns:
        The data as a string, trimmed.
    """
    if not isinstance(data, str):
        data = data.decode()
    return data.rstrip()


def new_example():
    """New example keyword which just returns 'new example'.
    """
    return 'new example in demo'


@classmethod
def from_api_data(cls, data):
    """Creates a new Customer object from API data from ticketswitch.

        Args:
            data (dict): the part of the response from a ticketswitch API call
                that concerns a customer.

        Returns:
            :class:`Customer <pyticketswitch.customer.Customer>`: a new
            :class:`Customer <pyticketswitch.customer.Customer>` object
            populated with the data from the api.

        """
    kwargs = {'first_name': data.get('first_name'), 'first_name_latin':
        data.get('first_name_latin'), 'last_name': data.get('last_name'),
        'last_name_latin': data.get('last_name_latin'), 'title': data.get(
        'title'), 'title_latin': data.get('title_latin'), 'initials': data.
        get('initials'), 'initials_latin': data.get('initials_latin'),
        'suffix': data.get('suffix'), 'suffix_latin': data.get(
        'suffix_latin'), 'country_code': data.get('country_code'),
        'post_code': data.get('postcode'), 'post_code_latin': data.get(
        'postcode_latin'), 'town': data.get('town'), 'town_latin': data.get
        ('town_latin'), 'county': data.get('county'), 'county_latin': data.
        get('county_latin'), 'country': data.get('country'),
        'country_latin': data.get('country_latin'), 'email': data.get(
        'email_addr'), 'phone': data.get('phone'), 'home_phone': data.get(
        'home_phone'), 'work_phone': data.get('work_phone'),
        'agent_reference': data.get('agent_ref'), 'supplier_can_use_data':
        data.get('dp_supplier'), 'user_can_use_data': data.get('dp_user'),
        'world_can_use_data': data.get('dp_world')}
    address_line_one = data.get('addr_line_one')
    address_line_two = data.get('addr_line_two')
    address_line_one_latin = data.get('addr_line_one_latin')
    address_line_two_latin = data.get('addr_line_two_latin')
    address_lines = []
    if address_line_one:
        address_lines.append(address_line_one)
    if address_line_two:
        address_lines.append(address_line_two)
    address_lines_latin = []
    if address_line_one_latin:
        address_lines_latin.append(address_line_one_latin)
    if address_line_two_latin:
        address_lines_latin.append(address_line_two_latin)
    kwargs.update(address_lines=address_lines, address_lines_latin=
        address_lines_latin)
    return cls(**kwargs)


@property
def __members__(cls):
    """Returns a mapping of member name->value.

        This mapping lists all enum members, including aliases. Note that this
        is a copy of the internal mapping.

        """
    return cls._member_map_.copy()