def create_vol_geom(*varargin):
    """Create a volume geometry structure.

This method can be called in a number of ways:

``create_vol_geom(N)``:
    :returns: A 2D volume geometry of size :math:`N \\times N`.

``create_vol_geom((Y, X))``:
    :returns: A 2D volume geometry of size :math:`Y \\times X`.

``create_vol_geom(Y, X)``:
    :returns: A 2D volume geometry of size :math:`Y \\times X`.

``create_vol_geom(Y, X, minx, maxx, miny, maxy)``:
    :returns: A 2D volume geometry of size :math:`Y \\times X`, windowed as :math:`minx \\leq x \\leq maxx` and :math:`miny \\leq y \\leq maxy`.

``create_vol_geom((Y, X, Z))``:
    :returns: A 3D volume geometry of size :math:`Y \\times X \\times Z`.

``create_vol_geom(Y, X, Z)``:
    :returns: A 3D volume geometry of size :math:`Y \\times X \\times Z`.

``create_vol_geom(Y, X, Z, minx, maxx, miny, maxy, minz, maxz)``:
    :returns: A 3D volume geometry of size :math:`Y \\times X \\times Z`, windowed as :math:`minx \\leq x \\leq maxx` and :math:`miny \\leq y \\leq maxy` and :math:`minz \\leq z \\leq maxz` .

"""
    vol_geom = {'option': {}}
    if len(varargin) == 1 and isinstance(varargin[0], int) == 1:
        vol_geom['GridRowCount'] = varargin[0]
        vol_geom['GridColCount'] = varargin[0]
        vol_geom['option']['WindowMinX'] = -varargin[0] / 2.0
        vol_geom['option']['WindowMaxX'] = varargin[0] / 2.0
        vol_geom['option']['WindowMinY'] = -varargin[0] / 2.0
        vol_geom['option']['WindowMaxY'] = varargin[0] / 2.0
    elif len(varargin) == 1 and len(varargin[0]) == 2:
        vol_geom['GridRowCount'] = varargin[0][0]
        vol_geom['GridColCount'] = varargin[0][1]
        vol_geom['option']['WindowMinX'] = -varargin[0][1] / 2.0
        vol_geom['option']['WindowMaxX'] = varargin[0][1] / 2.0
        vol_geom['option']['WindowMinY'] = -varargin[0][0] / 2.0
        vol_geom['option']['WindowMaxY'] = varargin[0][0] / 2.0
    elif len(varargin) == 1 and len(varargin[0]) == 3:
        vol_geom['GridRowCount'] = varargin[0][0]
        vol_geom['GridColCount'] = varargin[0][1]
        vol_geom['GridSliceCount'] = varargin[0][2]
        vol_geom['option']['WindowMinX'] = -varargin[0][1] / 2.0
        vol_geom['option']['WindowMaxX'] = varargin[0][1] / 2.0
        vol_geom['option']['WindowMinY'] = -varargin[0][0] / 2.0
        vol_geom['option']['WindowMaxY'] = varargin[0][0] / 2.0
        vol_geom['option']['WindowMinZ'] = -varargin[0][2] / 2.0
        vol_geom['option']['WindowMaxZ'] = varargin[0][2] / 2.0
    elif len(varargin) == 2:
        vol_geom['GridRowCount'] = varargin[0]
        vol_geom['GridColCount'] = varargin[1]
        vol_geom['option']['WindowMinX'] = -varargin[1] / 2.0
        vol_geom['option']['WindowMaxX'] = varargin[1] / 2.0
        vol_geom['option']['WindowMinY'] = -varargin[0] / 2.0
        vol_geom['option']['WindowMaxY'] = varargin[0] / 2.0
    elif len(varargin) == 6:
        vol_geom['GridRowCount'] = varargin[0]
        vol_geom['GridColCount'] = varargin[1]
        vol_geom['option']['WindowMinX'] = varargin[2]
        vol_geom['option']['WindowMaxX'] = varargin[3]
        vol_geom['option']['WindowMinY'] = varargin[4]
        vol_geom['option']['WindowMaxY'] = varargin[5]
    elif len(varargin) == 3:
        vol_geom['GridRowCount'] = varargin[0]
        vol_geom['GridColCount'] = varargin[1]
        vol_geom['GridSliceCount'] = varargin[2]
    elif len(varargin) == 9:
        vol_geom['GridRowCount'] = varargin[0]
        vol_geom['GridColCount'] = varargin[1]
        vol_geom['GridSliceCount'] = varargin[2]
        vol_geom['option']['WindowMinX'] = varargin[3]
        vol_geom['option']['WindowMaxX'] = varargin[4]
        vol_geom['option']['WindowMinY'] = varargin[5]
        vol_geom['option']['WindowMaxY'] = varargin[6]
        vol_geom['option']['WindowMinZ'] = varargin[7]
        vol_geom['option']['WindowMaxZ'] = varargin[8]
    return vol_geom


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

        :param SharePathError val:
        :rtype: ShareFolderErrorBase
        """
    return cls('bad_path', val)


def get_goal_players(play, event, players):
    """
    Get all the players on ice for a goal (modify play object)

    :param play: Our play info
    :param event: PBP play info
    :param players: Player id -> Name conversion

    :return: None
    """
    if play['ev_team'] == play['home_team']:
        ptype_team = {'plus': 'home', 'minus': 'away'}
    else:
        ptype_team = {'plus': 'away', 'minus': 'home'}
    for ptype in ['plus', 'minus']:
        for num in range(1, 7):
            play['{}Player{}_id'.format(ptype_team[ptype], num)] = event[
                'play_actions'][1].get('{}_player_{}'.format(ptype, num))
            if play['{}Player{}_id'.format(ptype_team[ptype], num)]:
                play['{}Player{}'.format(ptype_team[ptype], num)
                    ] = players.get(int(play['{}Player{}_id'.format(
                    ptype_team[ptype], num)]))
            else:
                play['{}Player{}'.format(ptype_team[ptype], num)] = None


def format_lmns(lmns):
    """ Checks if the format of the parameter lmns is correct, returning the
    appropriate format if not, and raise an error if nmodes=0.
    The required format for the ringdown approximants is a list of lmn modes
    as strings, with n the number of overtones desired. For instance,
    lmns = ['223','331'] are the modes 220, 221, 222, and 330.
    The ConfigParser of a workflow might convert that to a single string
    (case 1 below) or a list with a single string (case 2), and this function
    will return the appropriate list of strings. If a different format is
    given, raise an error.
    """
    if isinstance(lmns, str):
        lmns = lmns.strip('[]').split(',')
    elif len(lmns) == 1 and isinstance(lmns[0], str) and len(lmns[0]) > 3:
        lmns = lmns[0].split(',')
    out = []
    for lmn in lmns:
        lmn = str(int(lmn))
        if len(lmn) != 3:
            raise ValueError(
                'Format of parameter lmns not recognized. See approximant documentation for more info.'
                )
        elif int(lmn[2]) == 0:
            raise ValueError(
                'Number of overtones (nmodes) must be greater than zero in lmn={}.'
                .format(lmn))
        out.append(lmn)
    return out


def _pairAnchors(anchorDict1, anchorDict2):
    """
    Anchors are paired using the following rules:

    Matching Identifiers
    --------------------
    >>> anchors1 = {
    ...     "test" : [
    ...         (None, 1, 2, None),
    ...         ("identifier 1", 3, 4, None)
    ...      ]
    ... }
    >>> anchors2 = {
    ...     "test" : [
    ...         ("identifier 1", 1, 2, None),
    ...         (None, 3, 4, None)
    ...      ]
    ... }
    >>> expected = [
    ...     (
    ...         dict(name="test", identifier=None, x=1, y=2, color=None),
    ...         dict(name="test", identifier=None, x=3, y=4, color=None)
    ...     ),
    ...     (
    ...         dict(name="test", identifier="identifier 1", x=3, y=4, color=None),
    ...         dict(name="test", identifier="identifier 1", x=1, y=2, color=None)
    ...     )
    ... ]
    >>> _pairAnchors(anchors1, anchors2) == expected
    True

    Mismatched Identifiers
    ----------------------
    >>> anchors1 = {
    ...     "test" : [
    ...         ("identifier 1", 3, 4, None)
    ...      ]
    ... }
    >>> anchors2 = {
    ...     "test" : [
    ...         ("identifier 2", 1, 2, None),
    ...      ]
    ... }
    >>> expected = [
    ...     (
    ...         dict(name="test", identifier="identifier 1", x=3, y=4, color=None),
    ...         dict(name="test", identifier="identifier 2", x=1, y=2, color=None)
    ...     )
    ... ]
    >>> _pairAnchors(anchors1, anchors2) == expected
    True
    """
    pairs = []
    for name, anchors1 in anchorDict1.items():
        if name not in anchorDict2:
            continue
        anchors2 = anchorDict2[name]
        removeFromAnchors1 = []
        for anchor1 in anchors1:
            match = None
            identifier = anchor1[0]
            for anchor2 in anchors2:
                if anchor2[0] == identifier:
                    match = anchor2
                    break
            if match is not None:
                anchor2 = match
                anchors2.remove(anchor2)
                removeFromAnchors1.append(anchor1)
                a1 = dict(name=name, identifier=identifier)
                a1['x'], a1['y'], a1['color'] = anchor1[1:]
                a2 = dict(name=name, identifier=identifier)
                a2['x'], a2['y'], a2['color'] = anchor2[1:]
                pairs.append((a1, a2))
        for anchor1 in removeFromAnchors1:
            anchors1.remove(anchor1)
        if not anchors1 or not anchors2:
            continue
        while 1:
            anchor1 = anchors1.pop(0)
            anchor2 = anchors2.pop(0)
            a1 = dict(name=name)
            a1['identifier'], a1['x'], a1['y'], a1['color'] = anchor1
            a2 = dict(name=name, identifier=identifier)
            a2['identifier'], a2['x'], a2['y'], a2['color'] = anchor2
            pairs.append((a1, a2))
            if not anchors1:
                break
            if not anchors2:
                break
    return pairs


def selected_army(info):
    """
        Process 'user selected army' event.

        Input:
        `info`  # An object with information about event's time, user's
                # callsign, army name and position on map.
        """


def psi_Mg_Cl_NO3_PK74(T, P):
    """c-a-a': magnesium chloride nitrate [PK74]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


@staticmethod
def get_bcrypt():
    """Returns the `py-bcrypt` library for internal usage.

        :returns: `py-bcrypt` package.
        :raises: :exc:`ImportError` if `py-bcrypt` was not found.
        """
    try:
        import bcrypt
    except ImportError:
        raise ImportError(
            "PasswordField requires 'py-bcrypt' library to hash the passwords."
            )
    else:
        return bcrypt


def get_injected_files(personality):
    """Create a list of injected files from the personality attribute.

    At this time, injected_files must be formatted as a list of
    (file_path, file_content) pairs for compatibility with the
    underlying compute service.
    """
    injected_files = []
    for item in personality:
        injected_files.append((item['path'], item['contents']))
    return injected_files


def padded_column_string(string1, string2, unit, shift=10, max_len_string1=25):
    """
    Padded_column_string is a helper function which returns a formatted string
    representing object, quantity and unit of a piece of information.

    :param string1: information
    :type string1: str
    :param string2: quantity
    :type string2: str
    :param unit: unit of the information
    :type unit: str
    :param shift: whitespace shift to the right (indentation)
    :type shift: int
    :param max_len_string1: needed to calculate padding to the right
    :type max_len_string1: int
    :return:
    :rtype:
    """
    string = f' ' * shift + f'{string1:<{max_len_string1}} {string2} {unit}'
    if type(string2) == float:
        string = (f' ' * shift +
            f'{string1:<{max_len_string1}} {string2:0.6f} {unit}')
    return string


def maybe_tuple(t):
    """Return tuple of one element if ``t`` is a scalar."""
    return t if t is None or isinstance(t, tuple) else (t,)


@classmethod
def parse_marker(cls, line):
    """
        Returns a pair (prepend, leader) iff the line has a valid leader.
        """
    match_obj = cls.pattern.match(line)
    if match_obj is None:
        return None
    leader = match_obj.group(1)
    content = match_obj.group(0).replace(leader + '\t', leader + '   ', 1)
    prepend = len(content)
    if prepend == len(line.rstrip('\n')):
        prepend = match_obj.end(1) + 1
    else:
        spaces = match_obj.group(2)
        if spaces.startswith('\t'):
            spaces = spaces.replace('\t', '   ', 1)
        spaces = spaces.replace('\t', '    ')
        n_spaces = len(spaces)
        if n_spaces > 4:
            prepend = match_obj.end(1) + 1
    return prepend, leader


def sint16_to_int(bytes):
    """
    Convert a signed 16-bit integer to integer
    :param bytes:
    :return:
    """
    return int.from_bytes(bytes, byteorder='little', signed=True)


def subdivision_set(level: int=1, relative: bool=False):
    """Sets a Subdivision Surface Level (1-5)

    :param level: Level
    :type level: int
    :param relative: Relative, Apply the subsurf level as an offset relative to the current level
    :type relative: bool
    """
    pass


@classmethod
def header_hash(cls, header):
    """Given a header return the hash."""
    return cls.HEADER_HASH(header)


def get_html_subsection(name):
    """
    Return a subsection as HTML, with the given name

    :param name: subsection name
    :type name: str
    :rtype: str
    """
    return '<h2>{}</h2>'.format(name)


@classmethod
def safe_decode(cls, text):
    """Validate the format of the text and decode it

        First check if the text is in the format of the cipher, if so decode it.
        If the text is not in the format, return empty list

        :param text: The cipher-text
        :type text: str

        :returns: List of the plain-texts (or plain-text) after decode
        :rtype: list
        """
    if cls.validate(text):
        return cls.decode(text)
    else:
        return []


def collect_info(task_name, result):
    """Collect information from a celery result."""
    return {'id': result.id, 'status': result.status, 'info': result.info,
        'name': task_name}


def checksum(msg):
    """
   this function is used for the SYN flood checksum.

   it takes an input and returns it checksum.

"""
    s = 0
    for i in range(0, len(msg), 2):
        if i + 1 == len(msg):
            w = ord(msg[i])
            s += w
        else:
            w = ord(msg[i]) + (ord(msg[i + 1]) << 8)
            s += w
    s = (s >> 16) + (s & 65535)
    s = s + (s >> 16)
    s = ~s & 65535
    return s


def get_nodes_from(component_set):
    """
    Get the list of nodes specified in the given component set
    """
    index = component_set.find('#')
    if index == -1:
        return component_set
    return component_set[0:index]


def mark(name: str) ->str:
    """
    To put a custom tag within the text, use the <mark> tag.
    Amazon Polly takes no action on the tag, but returns the location of the tag in the SSML metadata.

    :param name: name of your mark
    """
    return f'<mark name="{name}"/>'


def common_filters(marker=None, limit=None, sort_key=None, sort_dir=None):
    """Generate common filters for any list request.

    :param marker: entity ID from which to start returning entities.
    :param limit: maximum number of entities to return.
    :param sort_key: field to use for sorting.
    :param sort_dir: direction of sorting: 'asc' or 'desc'.
    :returns: list of string filters.
    """
    filters = []
    if isinstance(limit, int):
        filters.append('limit=%s' % limit)
    if marker is not None:
        filters.append('marker=%s' % marker)
    if sort_key is not None:
        filters.append('sort_key=%s' % sort_key)
    if sort_dir is not None:
        filters.append('sort_dir=%s' % sort_dir)
    return filters


def psi_Ca_Mg_SO4_HMW84(T, P):
    """c-c'-a: calcium magnesium sulfate [HMW84]."""
    psi = 0.024
    valid = T == 298.15
    return psi, valid


@classmethod
def get(cls, client, name):
    """
        Get a network by name.

        :param client: client instance
        :type client: :class:`~pylxd.client.Client`
        :param name: name of the network
        :type name: str
        :returns: network instance (if exists)
        :rtype: :class:`Network`
        :raises: :class:`~pylxd.exceptions.NotFound` if network does not exist
        """
    response = client.api.networks[name].get()
    return cls(client, **response.json()['metadata'])


def cross_section_area_BlckBrn(TopWdth):
    """
    Calculate channel cross sectional area from channel Top Width using Blackburn et
    al. 2017 equations
    """
    cs_area = 0.75 * ((TopWdth / 2.44) ** (1.0 / 0.34)) ** 0.53
    return cs_area


def get_dbadmin_commands():
    """Returns a list of available commands for dbadmin

        :returns: a C{list} of available commands
        """
    pass


def quick_pagination(queryset, page, limit, max_limit=1000):
    """
    Функция быстрой паджинации для больших таблиц, не вызывающая count(*).
    """
    try:
        page = int(page)
    except ValueError:
        page = 1
    try:
        limit = int(limit)
        assert limit <= max_limit
    except ValueError:
        limit = 10
    except AssertionError:
        limit = max_limit
    offset = (page - 1) * limit
    queryset = queryset[offset:offset + limit + 1]
    result = queryset
    if len(result) > limit:
        has_next = True
        result = result[:limit]
    else:
        has_next = False
    return result, page, limit, has_next


def get_session_token(DurationSeconds=None, SerialNumber=None, TokenCode=None):
    """
    Returns a set of temporary credentials for an AWS account or IAM user. The credentials consist of an access key ID, a secret access key, and a security token. Typically, you use GetSessionToken if you want to use MFA to protect programmatic calls to specific AWS APIs like Amazon EC2 StopInstances . MFA-enabled IAM users would need to call GetSessionToken and submit an MFA code that is associated with their MFA device. Using the temporary security credentials that are returned from the call, IAM users can then make programmatic calls to APIs that require MFA authentication. If you do not supply a correct MFA code, then the API returns an access denied error. For a comparison of GetSessionToken with the other APIs that produce temporary credentials, see Requesting Temporary Security Credentials and Comparing the AWS STS APIs in the IAM User Guide .
    The GetSessionToken action must be called by using the long-term AWS security credentials of the AWS account or an IAM user. Credentials that are created by IAM users are valid for the duration that you specify, from 900 seconds (15 minutes) up to a maximum of 129600 seconds (36 hours), with a default of 43200 seconds (12 hours); credentials that are created by using account credentials can range from 900 seconds (15 minutes) up to a maximum of 3600 seconds (1 hour), with a default of 1 hour.
    The temporary security credentials created by GetSessionToken can be used to make API calls to any AWS service with the following exceptions:
    The permissions associated with the temporary security credentials returned by GetSessionToken are based on the permissions associated with account or IAM user whose credentials are used to call the action. If GetSessionToken is called using root account credentials, the temporary credentials have root account permissions. Similarly, if GetSessionToken is called using the credentials of an IAM user, the temporary credentials have the same permissions as the IAM user.
    For more information about using GetSessionToken to create temporary credentials, go to Temporary Credentials for Users in Untrusted Environments in the IAM User Guide .
    See also: AWS API Documentation

    Examples
    Expected Output:

    :example: response = client.get_session_token(
        DurationSeconds=123,
        SerialNumber='string',
        TokenCode='string'
    )

    :type DurationSeconds: integer
    :param DurationSeconds: The duration, in seconds, that the credentials should remain valid. Acceptable durations for IAM user sessions range from 900 seconds (15 minutes) to 129600 seconds (36 hours), with 43200 seconds (12 hours) as the default. Sessions for AWS account owners are restricted to a maximum of 3600 seconds (one hour). If the duration is longer than one hour, the session for AWS account owners defaults to one hour.

    :type SerialNumber: string
    :param SerialNumber: The identification number of the MFA device that is associated with the IAM user who is making the GetSessionToken call. Specify this value if the IAM user has a policy that requires MFA authentication. The value is either the serial number for a hardware device (such as GAHT12345678 ) or an Amazon Resource Name (ARN) for a virtual device (such as arn:aws:iam::123456789012:mfa/user ). You can find the device for an IAM user by going to the AWS Management Console and viewing the user's security credentials.
            The regex used to validated this parameter is a string of characters consisting of upper- and lower-case alphanumeric characters with no spaces. You can also include underscores or any of the following characters: =,.@:/-

    :type TokenCode: string
    :param TokenCode: The value provided by the MFA device, if MFA is required. If any policy requires the IAM user to submit an MFA code, specify this value. If MFA authentication is required, and the user does not provide a code when requesting a set of temporary security credentials, the user will receive an 'access denied' response when requesting resources that require MFA authentication.
            The format for this parameter, as described by its regex pattern, is a sequence of six numeric digits.

    :rtype: dict
    :return: {
        'Credentials': {
            'AccessKeyId': 'string',
            'SecretAccessKey': 'string',
            'SessionToken': 'string',
            'Expiration': datetime(2015, 1, 1)
        }
    }

    :returns:
    DurationSeconds (integer) -- The duration, in seconds, that the credentials should remain valid. Acceptable durations for IAM user sessions range from 900 seconds (15 minutes) to 129600 seconds (36 hours), with 43200 seconds (12 hours) as the default. Sessions for AWS account owners are restricted to a maximum of 3600 seconds (one hour). If the duration is longer than one hour, the session for AWS account owners defaults to one hour.
    SerialNumber (string) -- The identification number of the MFA device that is associated with the IAM user who is making the GetSessionToken call. Specify this value if the IAM user has a policy that requires MFA authentication. The value is either the serial number for a hardware device (such as GAHT12345678 ) or an Amazon Resource Name (ARN) for a virtual device (such as arn:aws:iam::123456789012:mfa/user ). You can find the device for an IAM user by going to the AWS Management Console and viewing the user's security credentials.
    The regex used to validated this parameter is a string of characters consisting of upper- and lower-case alphanumeric characters with no spaces. You can also include underscores or any of the following characters: =,.@:/-

    TokenCode (string) -- The value provided by the MFA device, if MFA is required. If any policy requires the IAM user to submit an MFA code, specify this value. If MFA authentication is required, and the user does not provide a code when requesting a set of temporary security credentials, the user will receive an "access denied" response when requesting resources that require MFA authentication.
    The format for this parameter, as described by its regex pattern, is a sequence of six numeric digits.

    """
    pass


def calc_displacements(volreg):
    """
    This function calculate volume displacement from motion parameter
    """
    import numpy as np
    import pandas as pd
    output = dict()
    columns = volreg.columns
    output['FD'] = np.abs(np.insert(np.diff(volreg, axis=0), 0, 0, axis=0)
        ).sum(axis=1)
    output['ARD'] = np.abs(np.insert(np.diff(volreg[columns[:3]], axis=0),
        0, 0, axis=0)).sum(axis=1)
    output['ATD'] = np.abs(np.insert(np.diff(volreg[columns[3:]], axis=0),
        0, 0, axis=0)).sum(axis=1)
    return pd.DataFrame(output)


def ms_300_to_od(ms):
    """Convert the milliseconds to score a 300 into an OD value.

    Parameters
    ----------
    ms : float
        The length of the 300 window in milliseconds.

    Returns
    -------
    od : float
        The OD value that produces a 300 window of length ``ms``.

    See Also
    --------
    :func:`slider.mod.od_to_ms_300`
    """
    return (ms - 79.5) / -6


def _check_and_assemble(inputs):
    """
    Prepare a grompp job, with the given inputs.
    """
    if not 0 in inputs.keys():
        raise RuntimeError('No executable specified.')
    if not '-f' in inputs.keys():
        raise RuntimeError('No input (mdp) file specified.')
    if not '-c' in inputs.keys():
        raise RuntimeError('No structure (.gro) file specified.')
    if not '-p' in inputs.keys():
        raise RuntimeError('No topology (.top) file specified.')
    result = inputs.copy()
    execstr = inputs[0] + ' '
    for key in inputs:
        if key != 0:
            execstr = execstr + key + ' '
            if inputs[key] is not None:
                execstr = execstr + inputs[key] + ' '
    return execstr


@classmethod
def info(cls):
    """
        @param: empty intended
        return list of possbile join combinations
        """
    return list(cls.__support__)


@staticmethod
def _split(x, n_factors, n_variables, n_lower_diag):
    """
        Given a one-dimensional array, split it
        into a set of multi-dimensional loading,
        error variance, factor variance, and factor
        covariance matrices.

        Parameters
        ----------
        x : array-like
            The combined (X, 1) array to split.
        n_factors : int
            The number of factors.
        n_variables : int
            The number of variables.
        n_lower_diag : int
            The number of elements in the `factor_covs`
            array, which is equal to the lower.

        Returns
        -------
        loadings : array-like
            The loadings matrix (n_factors * n_variables)
        error_vars : array-like (n_variables * 1)
            The error variance array.
        factor_vars : array-like (n_factors * 1)
            The factor variance array.
        factor_covs : array-like (n_lower_diag * 1)
            The factor covariance array
        """
    loadings_ix = int(n_factors * n_variables)
    error_vars_ix = n_variables + loadings_ix
    factor_vars_ix = n_factors + error_vars_ix
    factor_covs_ix = n_lower_diag + factor_vars_ix
    return x[:loadings_ix].reshape((n_variables, n_factors), order='F'), x[
        loadings_ix:error_vars_ix].reshape((n_variables, 1), order='F'), x[
        error_vars_ix:factor_vars_ix].reshape((n_factors, 1), order='F'), x[
        factor_vars_ix:factor_covs_ix].reshape((n_lower_diag, 1), order='F')


def footer(preamble_toc, cfr_toc, full_id):
    """Generate "navigation" context which allows the user to move between
    sections in the footer"""
    items = preamble_toc + cfr_toc
    nav = {'previous': None, 'next': None, 'page_type': 'preamble-section'}
    for idx, item in enumerate(items):
        if item.markup_id == full_id:
            if idx > 0:
                nav['previous'] = items[idx - 1]
            if idx < len(items) - 1:
                nav['next'] = items[idx + 1]
    return nav


def get_lomb_y_offset(lomb_model):
    """Get the y-intercept of a fitted Lomb-Scargle model."""
    return lomb_model['freq_fits'][0]['y_offset']


def default_refl_mask(ref_data):
    """
    Example of masking using the reflectance.

    :param ref_data: reflectance dataset from which to derive mask
    :type ref_data: xarray
    :rtype 2D numpy array (BOOL)
    """
    blue = ref_data.blue.where(ref_data.blue != ref_data.blue.attrs['nodata'])
    nir = ref_data.nir.where(ref_data.nir != ref_data.nir.attrs['nodata'])
    ratio = (blue - nir) / (nir + blue)
    mask = ratio > 0.0
    mask = mask.values.squeeze()
    return mask


def fail_adapt(oUnknown, sCls):
    """Generic failed to adapt handler"""
    raise NotImplementedError("Can't adapt %r to %s" % (oUnknown, sCls))
    return oUnknown


def value_occurence(s):
    """Count the number of times each value occurs.

    This function returns the counts for each row, in contrast with
    `pandas.value_counts <http://pandas.pydata.org/pandas-
    docs/stable/generated/pandas.Series.value_counts.html>`_.

    Returns
    -------
    pandas.Series
        A Series with value counts.

    """
    value_count = s.fillna('NAN')
    return value_count.groupby(by=value_count).transform('count')


def _get(obj, name):
    """
    Get the indexable value with given `name` from `obj`, which may be
    a `dict` (or subclass) or a non-dict that has a `__getitem__` method.
    """
    try:
        return dict.__getitem__(obj, name)
    except TypeError:
        if isinstance(obj, dict):
            raise
        return obj[name]


def normalize(returns, starting_value=1):
    """
    Normalizes a returns timeseries based on the first value.

    Parameters
    ----------
    returns : pd.Series
        Daily returns of the strategy, noncumulative.
         - See full explanation in tears.create_full_tear_sheet.
    starting_value : float, optional
       The starting returns (default 1).

    Returns
    -------
    pd.Series
        Normalized returns.
    """
    return starting_value * (returns / returns.iloc[0])


def dict_update_new(target, source, fail=False):
    """Update values in `source` that are new (not present) in `target`.

    If `fail` is True and a value is already set, an error is raised.

    """
    for key in source:
        if key not in target:
            target[key] = source[key]
        elif fail:
            raise TypeError('key "{}" already in target'.format(key))


@classmethod
def AddArguments(cls, argument_group):
    """Adds command line arguments to an argument group.

    This function takes an argument parser or an argument group object and adds
    to it all the command line arguments this helper supports.

    Args:
      argument_group (argparse._ArgumentGroup|argparse.ArgumentParser):
          argparse group.
    """
    argument_group.add_argument('--yara_rules', '--yara-rules', dest=
        'yara_rules_path', type=str, metavar='PATH', action='store', help=
        'Path to a file containing Yara rules definitions.')


def _on_message_callback(client, userdata, message):
    """Internal callback"""
    userdata['callback'](client, userdata['userdata'], message)


def shade_flat():
    """Set shading to flat

    """
    pass


def pathto(f, cwd=None):
    """Obtain the relative path to a file."""


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 _formatter_self(name, value):
    """Format the "self" variable and value on instance methods.
    """
    __mname = value.__module__
    if __mname != '__main__':
        return '%s = <%s.%s object at 0x%x>' % (name, __mname, value.
            __class__.__name__, id(value))
    else:
        return '%s = <%s object at 0x%x>' % (name, value.__class__.__name__,
            id(value))


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 logPrefix():
    """
        @return: Prefix used during log formatting to indicate context.
        @rtype: C{str}
        """


def f2(x):
    """f2 is a symmetric parabola, x**2 - 1"""
    return x ** 2 - 1


def toggle(collection, item):
    """ Toggles an item in a set. """
    if item is None:
        return collection
    if item in collection:
        return collection - {item}
    else:
        return collection | {item}


def perf_clock():
    """
    Backwards compatibility wrapper for system clock.
    """
    try:
        from time import perf_counter as clock
    except ImportError:
        from time import clock
    finally:
        wclock = clock()
    return wclock


@staticmethod
def postgresql_replica_lag(host, *args):
    """Check the postgresql replica lag."""
    pass


def verify_probability(probability):
    """ Makes sure it's a probability. """
    return probability >= 0 and probability <= 1


def cg_steihaug(vs, Avp, g, z0, Delta, rtol, maxiter=1000, monitor=None, C=None
    ):
    """ best effort solving for y = A^{-1} g with cg,
        given the trust-region constraint;

        This is roughly ported from Jeff Regier's

            https://github.com/jeff-regier/Celeste.jl/blob/316cae12efcc394a5253ee799056c5513c2efc67/src/cg_trust_region.jl

        the algorithm is almost identical to the one on NWU wiki,

            https://optimization.mccormick.northwestern.edu/index.php/Trust-region_methods

        if given B, update approximated Hessian, see Morale and Nocedal, 2000

            http://epubs.siam.org/doi/abs/10.1137/S1052623497327854

        C(v, direction) the preconditioner operator. C^{direction}.
        Usually, C is close to A, such that C^{-1} A has a lower condition number.

        One particular case is if C is the diagonal of A.

        See Steihaug's paper. https://epubs.siam.org/doi/pdf/10.1137/0720042

    """
    if C is None:
        C = lambda x, direction: x
    dot = vs.dot
    mul = vs.mul
    addmul = vs.addmul
    if z0 is None:
        z0 = vs.zeros_like(g)
    r0 = addmul(Avp(z0), g, -1)
    mr0 = C(r0, -1)
    d0 = mul(mr0, 1)
    j = 0
    rho_init = dot(mr0, r0)
    rho0 = rho_init
    while True:
        Bd0 = Avp(d0)
        dBd0 = dot(d0, Bd0)
        alpha = rho0 / dBd0
        p0 = addmul(z0, d0, -alpha)
        message = ''
        if dBd0 == 0:
            rho1 = 0
            z1 = z0
            r1 = r0
            mr1 = mr0
            d1 = d0
            message = 'zero hessian'
        elif dBd0 <= 0 or dot(p0, C(p0, 1)) ** 0.5 >= Delta:
            a_ = dot(d0, C(d0, 1))
            b_ = 2 * dot(z0, C(d0, 1))
            c_ = dot(z0, C(z0, 1)) - Delta ** 2
            cond = b_ ** 2 - 4 * a_ * c_
            if a_ == 0 or cond < 0:
                rho1 = -1
                tau = 1.0
                if c_ > 0:
                    tau = Delta / c_ ** 0.5
                z1 = mul(z0, tau)
                if a_ == 0:
                    message = 'already at the right direction'
                    rho1 = -1
                else:
                    message = (
                        'no solution to second order equation, restarting ')
                    z0 = vs.zeros_like(g)
                    r0 = addmul(Avp(z0), g, -1)
                    mr0 = C(r0, -1)
                    d0 = mul(mr0, 1)
                    rho1 = dot(mr0, r0)
            else:
                tau = (-b_ - cond ** 0.5) / (2 * a_)
                z1 = addmul(z0, d0, tau)
                if dBd0 <= 0:
                    rho1 = -1
                else:
                    rho1 = 0
                if dBd0 <= 0:
                    message = 'negative curvature '
                else:
                    message = 'truncation'
            r1 = r0
            mr1 = mr0
            d1 = d0
        else:
            z1 = addmul(z0, d0, -alpha)
            r1 = addmul(r0, Bd0, -alpha)
            mr1 = C(r1, -1)
            rho1 = dot(mr1, r1)
            d1 = mul(mr1, 1)
            d1 = addmul(d1, d0, rho1 / rho0)
            message = 'regular iteration'
        r0 = r1
        mr0 = mr1
        d0 = d1
        z0 = z1
        rho0 = rho1
        if monitor is not None:
            z0norm = dot(z0, z0)
            zgnorm = dot(z0, g)
            gnorm = dot(g, g)
            monitor(j, message, rho0, rho_init, rtol, zgnorm / z0norm **
                0.5 / gnorm ** 0.5)
        if rho1 / rho_init < rtol ** 2:
            break
        if j >= maxiter:
            break
        j = j + 1
    return z0


def num_slopes_at_best(q, f, covered_intervals, uncovered_intervals=None):
    """
    Returns an upper bound on the final number of slopes,
    given current covered_intervals (and optionally, uncovered_intervals
    that provides connected components of non-covered intervals).

    EXAMPLES::

        sage: from cutgeneratingfunctionology.igp import *
        sage: covered_intervals = [set([0,2])]
        sage: uncovered_intervals = [set([1]), set([3,4])]
        sage: num_slopes_at_best(5, 3, covered_intervals)
        3
        sage: num_slopes_at_best(5, 3, covered_intervals, uncovered_intervals)
        3
    """
    if uncovered_intervals is None:
        to_cover = set(list(range(0, (f + 1) // 2)) + list(range(f, (f + q +
            1) // 2)))
        for component in covered_intervals:
            to_cover -= component
        uncovered_num = len(to_cover)
    else:
        uncovered_num = len(uncovered_intervals)
    return uncovered_num + len(covered_intervals)


def console(message, color='blue'):
    """
	Allows user-friend console logging using ANSI escape codes.

	Parameters
	----------
	message : :class:`str`
		Message to send to console.
	color : :class:`str`
		Name of color or ANSI escape event to use.

	Returns
	-------
	result : :class:`str`
		ANSI escape code.

	Examples
	--------
	>>> console(self, message, color='blue)

	Notes
	-----
	Colors are produced using ASCII Oct format. For example: '``. See http://jafrog.com/2013/11/23/colors-in-terminal.html
	for more.
	"""
    _c = dict(black='40m', red='41m', green='42m', orange='43m', purple=
        '45m', blue='46m', grey='47m')
    result = '\x1b[' + _c[color] + message + '\x1b[0m'
    return print(result)


def get_ext(name):
    """
	Return the extension only for a name (like a filename)

	>>> get_ext('foo.bar')
	'bar'
	>>> get_ext('.only')
	'only'
	>>> get_ext('')
	''
	>>> get_ext('noext')
	''
	>>> get_ext('emptyext.')
	''

	Note that for any non-empty string, the result will never be the
	same as the input. This is a useful property for basepack.
	"""
    other, sep, ext = name.partition('.')
    return ext


def get_stride(resnet_unit_id, block_id):
    """
    Determines the convolution stride.

    :param int resnet_unit_id: Which resnet unit
    (e.g. 0 is conv2_x, 1 is conv3_x)
    :param int block_id: Which block in the resnet unit (i.e. in the third
    unit of a 152-layer resnet, block_id=36)
    :return: Stride
    """
    if resnet_unit_id == 0 or block_id != 0:
        return 1
    else:
        return 2


@staticmethod
def min_val() ->float:
    """
        Get minimum value of metric. This used for correct histogram visualisation in some monitors

        :return: minimum value
        """
    return 0


def dominance(tree):
    """Return all ancestors of this tree including the tree itself.
    """
    parent = tree
    yield parent
    while not parent.parent is None:
        parent = parent.parent
        yield parent


def _is_dynamic(module):
    """
    Return True if the module is special module that cannot be imported by its
    name.
    """
    if hasattr(module, '__file__'):
        return False
    if hasattr(module, '__spec__'):
        return module.__spec__ is None
    else:
        import imp
        try:
            path = None
            for part in module.__name__.split('.'):
                if path is not None:
                    path = [path]
                f, path, description = imp.find_module(part, path)
                if f is not None:
                    f.close()
        except ImportError:
            return True
        return False


def get_local_group_information(task, portgroup):
    """Extract the portgroup information.

    The information is returned in the form of::

        {
            'id': portgroup.uuid,
            'name': portgroup.name,
            'bond_mode': portgroup.mode,
            'bond_properties': {
                'bond_propertyA': 'valueA',
                'bond_propertyB': 'valueB',
            }
        }

    :param task: a task containing the Node object.
    :param portgroup: Ironic portgroup object to extract data for.
    :returns: port group information as a dict
    """
    portgroup_properties = {}
    for prop, value in portgroup.properties.items():
        key = prop if prop.startswith('bond') else 'bond_%s' % prop
        portgroup_properties[key] = value
    return {'id': portgroup.uuid, 'name': portgroup.name, 'bond_mode':
        portgroup.mode, 'bond_properties': portgroup_properties}


def scan_setup_py():
    """Validate the contents of setup.py against Versioneer's expectations."""
    found = set()
    setters = False
    errors = 0
    with open('setup.py', 'r') as f:
        for line in f.readlines():
            if 'import versioneer' in line:
                found.add('import')
            if 'versioneer.get_cmdclass()' in line:
                found.add('cmdclass')
            if 'versioneer.get_version()' in line:
                found.add('get_version')
            if 'versioneer.VCS' in line:
                setters = True
            if 'versioneer.versionfile_source' in line:
                setters = True
    if len(found) != 3:
        print('')
        print('Your setup.py appears to be missing some important items')
        print('(but I might be wrong). Please make sure it has something')
        print('roughly like the following:')
        print('')
        print(' import versioneer')
        print(' setup( version=versioneer.get_version(),')
        print('        cmdclass=versioneer.get_cmdclass(),  ...)')
        print('')
        errors += 1
    if setters:
        print("You should remove lines like 'versioneer.VCS = ' and")
        print("'versioneer.versionfile_source = ' . This configuration")
        print('now lives in setup.cfg, and should be removed from setup.py')
        print('')
        errors += 1
    return errors


def _contrib_sign_ste(data=None, out=None, name=None, **kwargs):
    """Straight-through-estimator of `sign()`.

    In forward pass, returns element-wise sign of the input (same as `sign()`).

    In backward pass, returns gradients of ``1`` everywhere (instead of ``0`` everywhere as in ``sign()``):
    :math:`\\frac{d}{dx}{sign\\_ste(x)} = 1` vs. :math:`\\frac{d}{dx}{sign(x)} = 0`.
    This is useful for quantized training.

    Reference: Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation.

    Example::
      x = sign_ste([-2, 0, 3])
      x.backward()
      x = [-1.,  0., 1.]
      x.grad() = [1.,  1., 1.]

    The storage type of ``sign_ste`` output depends upon the input storage type:
      - round_ste(default) = default
      - round_ste(row_sparse) = row_sparse
      - round_ste(csr) = csr

    Defined in src/operator/contrib/stes_op.cc:L80

    Parameters
    ----------
    data : NDArray
        The input array.

    out : NDArray, optional
        The output NDArray to hold the result.

    Returns
    -------
    out : NDArray or list of NDArrays
        The output of this function.
    """
    return 0,


def create_xz(archive, compression, cmd, verbosity, interactive, filenames):
    """Create an XZ archive."""
    cmdlist = [cmd, 'a']
    if not interactive:
        cmdlist.append('-y')
    cmdlist.extend(['-txz', '-mx=9', '--', archive])
    cmdlist.extend(filenames)
    return cmdlist


@classmethod
def deserialize(cls: 'Type[CType]', serialized: 'Any') ->'CType':
    """
        Deserialize a serialized instance of this component.

        Will get the output of `serialize` as an argument.

        Override this in all your Component classes to make use of snecs'
        full-World serialization feature.
        """
    raise AttributeError(
        f"{cls} doesn't define deserialize()and so is not deserializable.")


def added(object, newParent=None, newName=None):
    """Send an :class:`~.IObjectAddedEvent` for ``object``.

        ``newParent`` is the container ``object`` was added to.
        ``newName`` is the name used to store ``object`` in the container.

        If either of these is not provided or is ``None``, they will
        be taken from the values of ``object.__parent__`` or
        ``object.__name__``, respectively.
        """


@classmethod
def GetCredentials(cls, path_spec):
    """Retrieves the credentials for a specific path specification.

    Args:
      path_spec (PathSpec): path specification.

    Returns:
      Credentials: credentials or None if the path specification has no
          credentials support.
    """
    if not path_spec:
        return None
    return cls._credentials.get(path_spec.type_indicator, None)


def reraise(exc_class, exc_obj, exc_tb, _marker=object()):
    """Re-raise an exception received from sys.exc_info() or similar."""
    raise exc_obj.with_traceback(exc_tb)


def merge_ptr_read(known, ptrs):
    """
    Merge common memory parts in a multiple byte memory.
    @ptrs: memory bytes list
    @known: ptrs' associated boolean for present/unpresent memory part in the
    store
    """
    assert known
    out = []
    known.append(None)
    ptrs.append(None)
    last, value, size = known[0], ptrs[0], 8
    for index, part in enumerate(known[1:], 1):
        if part == last:
            size += 8
        else:
            out.append((last, value, size))
            last, value, size = part, ptrs[index], 8
    return out


def close_log(log):
    """Detach log handlers; flush to disk"""
    handlers = log.handlers[:]
    for handler in handlers:
        handler.close()
        log.removeHandler(handler)


@classmethod
def register_tool(cls, tool_name: str, reset_function):
    """
        Identifies that the given Tool should be made available.
        Args:
            tool_name: A unique string identifying this tool.
            reset_function: The function to call to reset the Tool.

        Returns:

        """
    if tool_name in cls.TOOLS:
        raise Exception('Tool namespace {} already registered.')
    cls.TOOLS[tool_name] = {'reset': reset_function}


def raw_off(serial):
    """
    Takes the device out of raw mode.
    """
    serial.write(b'\x02')


def upload_project_run(upload_context):
    """
    Function run by CreateProjectCommand to create the project.
    Runs in a background process.
    :param upload_context: UploadContext: contains data service setup and project name to create.
    """
    data_service = upload_context.make_data_service()
    project_name = upload_context.project_name_or_id.get_name_or_raise()
    result = data_service.create_project(project_name, project_name)
    return result.json()['id']


def _get_block_sizes(resnet_size):
    """Retrieve the size of each block_layer in the ResNet model.

  The number of block layers used for the Resnet model varies according
  to the size of the model. This helper grabs the layer set we want, throwing
  an error if a non-standard size has been selected.

  Args:
    resnet_size: The number of convolutional layers needed in the model.

  Returns:
    A list of block sizes to use in building the model.

  Raises:
    KeyError: if invalid resnet_size is received.
  """
    choices = {(18): [2, 2, 2, 2], (34): [3, 4, 6, 3], (50): [3, 4, 6, 3],
        (101): [3, 4, 23, 3], (152): [3, 8, 36, 3], (200): [3, 24, 36, 3]}
    try:
        return choices[resnet_size]
    except KeyError:
        err = (
            """Could not find layers for selected Resnet size.
Size received: {}; sizes allowed: {}."""
            .format(resnet_size, choices.keys()))
        raise ValueError(err)


@staticmethod
def _init_ms_mpf(row):
    """Multiprocessing function to initialize the MS data of a single SampleRun (a row of a SampleSet)."""
    row['run'].init_ms()
    return row


def select_border(gesture_mode: int=0, xmin: int=0, xmax: int=0, ymin: int=
    0, ymax: int=0, extend: bool=True, axis_range: bool=False):
    """Use box selection to grab NLA-Strips

    :param gesture_mode: Gesture Mode
    :type gesture_mode: int
    :param xmin: X Min
    :type xmin: int
    :param xmax: X Max
    :type xmax: int
    :param ymin: Y Min
    :type ymin: int
    :param ymax: Y Max
    :type ymax: int
    :param extend: Extend, Extend selection instead of deselecting everything first
    :type extend: bool
    :param axis_range: Axis Range
    :type axis_range: bool
    """
    pass


@staticmethod
def name_is_left(side, patterns=None):
    """
        Returns whether given side is a valid left side or not
        :param side: str
        :param patterns: list<str>
        :return: bool
        """
    if not patterns:
        patterns = ['L', 'l', 'Left', 'left', 'lf']
    if str(side) in patterns:
        return True
    return False


def _apply_affine_scalar(i, j, k, affine_matrix):
    """
    Applies an affine matrix to the given coordinates. The 3 values i, j and k must be scalars. The affine matrix consists of a 3x3 rotation matrix and a 3x1 transposition matrix (plus the last row).

    Parameters
    ----------
    i, j, k: numeric scalars
        The source coordinates.

    affine_matrix: numpy 2D float array with shape (4, 4)
        The affine matrix

    Returns
    -------
    The coordinate vector after applying the matrix.
    """
    rotation = affine_matrix[:3, :3]
    translation = affine_matrix[:3, (3)]
    return rotation.dot([i, j, k]) + translation


@classmethod
def add_arguments(cls):
    """
        check cliez depends envrioument.
        """
    return [(('--debug',), dict(action='store_true', help='open debug mode'
        )), (('--verbose', '-v'), dict(action='count'))]


def transports():
    """Return a list of supported transport names."""


def statistics(input_dict):
    """
    Statistics of Annotated Document Corpus.

    :param adc: Annotated Document Corpus.
    :return doc_count (int): Number of Documents.
    :return feature_count (int): Number of Features.
    :return description (string): Statistics.
    """
    output_dict = {}
    output_dict['doc_count'] = len(input_dict['adc'].documents)
    output_dict['feature_count'] = len(input_dict['adc'].features)
    output_dict['description'] = 'Number of documents is ' + str(output_dict
        ['doc_count']) + ', number of corpus features is ' + str(output_dict
        ['feature_count']) + '.'
    return output_dict


def make_list(val):
    """ If a scalar or string is passed make it a list, otherwise do nothing.
    """
    try:
        len(val)
        if not isinstance(val, str):
            return val
    except TypeError:
        pass
    return [val]


def remap_oscar_billing_address(address):
    """Remaps Oscar billing address dictionary to Helcim dictionary.

        Parameters:
            address (dict): A dictionary of the billing address details
                provided by Django Oscar.

        Returns:
            dict: Billing address details formatted for
                django-helcim.
    """
    if address.get('first_name') and address.get('last_name'):
        contact_name = '{} {}'.format(address.get('first_name'), address.
            get('last_name'))
    elif address.get('first_name'):
        contact_name = address.get('first_name')
    elif address.get('last_name'):
        contact_name = address.get('last_name')
    else:
        contact_name = None
    return {'billing_contact_name': contact_name, 'billing_street_1':
        address.get('line1'), 'billing_street_2': address.get('line2'),
        'billing_city': address.get('line4'), 'billing_province': address.
        get('state'), 'billing_postal_code': address.get('postcode'),
        'billing_country': address.get('country'), 'billing_phone': address
        .get('phone_number')}


@classmethod
def get_xsd_schemas(cls):
    """Get a set of distinct XSD schema URLs for this type and its
        subtypes.

        Returns
        -------
        set of str
            A set of XSD schema URLs.

        """
    xsd_schemas = set()
    fields = cls.get_fields(source='xml', include_subtypes=True)
    for f in fields.values():
        if 'xsd_type' in f:
            xsd_schemas.add(f['xsd_schema'])
    return xsd_schemas


@classmethod
def add_finalizer(cls, id, weak_ref):
    """Registers a finalizer with an id.

        :param id: The id of the object referenced by the weak reference.
        :param weak_ref: The weak reference to register.
        """
    with cls.lock:
        cls.finalizers[id] = weak_ref


def _next_token(iterable, comments):
    """
	Yields only non-whitespace, non-comment lines.
	"""
    for line in iterable:
        parts = iter(line.strip().split(None, 1))
        key = next(parts, None)
        if not key:
            continue
        elif key == b'COMMENT':
            comments.append(next(parts, b''))
        else:
            yield key, next(parts, None)


def get_equiv_class(fsm, gamma, state):
    """
    Return the equivalence class for gamma state belongs to. The equivalence
    class depends on semantics; The distributed knowledge based
    equivalence fo gamma containing state is returned.

    fsm -- the model
    gamma -- a set of agents of fsm
    """
    return fsm.equivalent_states(state, gamma) & fsm.reachable_states


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

        :param SharedLinkChangeVisibilityType val:
        :rtype: EventType
        """
    return cls('shared_link_change_visibility', val)


def add_crosshair_info(ax, info):
    """Callback when crosshair updated like so: info(x,y,xtext,ytext); the info()
       callback must return two values: xtext and ytext."""
    ax.crosshair.infos.append(info)


def normalizeXRI(xri):
    """Normalize an XRI, stripping its scheme if present"""
    if xri.startswith('xri://'):
        xri = xri[6:]
    return xri


def _pipe_density(nufft, maxiter):
    """
    Private: create the density function in the data space by a iterative solution
    Pipe et al. 1999
    """
    try:
        if maxiter < nufft.last_iter:
            W = nufft.st['W']
        else:
            W = nufft.st['W']
            for pp in range(0, maxiter - nufft.last_iter):
                E = nufft.forward(nufft.adjoint(W))
                W = W / E
            nufft.last_iter = maxiter
    except:
        W = nufft.thr.copy_array(nufft.y)
        W.fill(0.0 + 0.0j)
        for pp in range(0, 1):
            E = nufft._forward_legacy(nufft._adjoint_legacy(W))
            W /= E
    return W


@classmethod
def match_subscription_from_subscription_name(cls, subscription_name):
    """Parses the subscription from a subscription resource.

        Args:
          subscription_name (string): A fully-qualified path representing a subscription
            resource.

        Returns:
          A string representing the subscription.
        """
    return cls._SUBSCRIPTION_PATH_TEMPLATE.match(subscription_name).get(
        'subscription')


def vertex_color_hsv(h: float=0.5, s: float=1.0, v: float=1.0):
    """Adjust vertex color HSV values

    :param h: Hue
    :type h: float
    :param s: Saturation
    :type s: float
    :param v: Value
    :type v: float
    """
    pass


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 aws_to_torch(settings, key: str) ->str:
    """
    Impute libtorch specific type from user provided "human-readable" form.

    See `type_mapping` in source code for exact mapping.

    Parameters
    ----------
    settings : typing.Dict
        YAML parsed to dict

    Returns
    -------
    str:
        String specifying type, e.g. "torch::kFloat16"
    """
    type_mapping = {'bool': 'torch::kInt8', 'int': 'torch::kInt32', 'long':
        'torch::kInt64', 'double': 'torch::kFloat64'}
    if settings['return'][key] is None:
        return ''
    return type_mapping[settings['return'][key]['type'].lower()]


def _string_contains(arg, substr):
    """
    Determine if indicated string is exactly contained in the calling string.

    Parameters
    ----------
    substr : str or ibis.expr.types.StringValue

    Returns
    -------
    contains : ibis.expr.types.BooleanValue
    """
    return arg.find(substr) >= 0


def render_changed():
    """Render current scene, when input node’s layer has been changed

    """
    pass


@staticmethod
def _is_clone_snapshot_name(snapshot):
    """Check if snapshot is created for cloning."""
    name = snapshot.split('@')[-1]
    return name.startswith('cinder-clone-snapshot-')


def spoof_run(cmd):
    """Spoff a fabric.api.run call."""
    pass


def __check_view(active_views, view_index):
    """
        Removes the view in active_views at index view_index if
            - The two lines are coolinear
            - The lines pass through either extremity
    """
    shallow_line = active_views[view_index].shallow_line
    steep_line = active_views[view_index].steep_line
    if shallow_line.line_collinear(steep_line) and (shallow_line.
        p_collinear(0, 1) or shallow_line.p_collinear(1, 0)):
        del active_views[view_index]
        return False
    else:
        return True


def round_e_back(w, t):
    """Return distance centroid–back for elliptic cross-sections.

    e_back_round = t/2
    Source: Lueger 1904

    Args:
        w: width
        t: thickness

    Returns:
        A scalar.
    """
    return t / 2


@staticmethod
def to_megabytes(num: int) ->float:
    """ Converts a number (assume floats, 4 bytes each) to megabytes. """
    assert num >= 0
    return num * 4.0 / 1024 ** 2.0


def get_module_version_attr(module):
    """
    Simply return the __version__ attribute as recommended by PEP396
    See https://www.python.org/dev/peps/pep-0396/#specification

    Note that even if some packages use custom attribute names such as 'version' (without underscores) it does not
    seem a good idea to try them. Indeed we would have no guarantee that the semantic meaning is the same.
    See https://stackoverflow.com/questions/31146153/get-python-tornado-version/31146188.

    :param module:
    :return:
    """
    return module.__version__


def _remove_by_index_list(text, index_list):
    """Remove all substrings inside a string, thanks to the given list of indexes """
    len_removed = 0
    for i in index_list:
        i_start = i[0] - len_removed
        i_end = i[1] - len_removed
        text = text[:i_start] + text[i_end:]
        len_removed += i[1] - i[0]
    return text


def parse(dictionary, arguments):
    """
    Given the input dictionary and arguments,
    return a (possibly, modified) dictionary.

    The returned dictionary might be the same (input) instance,
    or a new one.

    :param dictionary: the (raw) input dictionary
    :type  dictionary: Dictionary
    :param arguments: the command line arguments
    :type  arguments: Namespace from argparse
    :rtype: Dictionary
    """
    return dictionary


@classmethod
def ExternalDepFound(cls, name, version):
    """Inform that an optional dependency was found.

        A new `_ExternalDep` object will be created and stored
        in `config`.

        :Parameters:
          name
            identifier of the optional dependency. This should
            be a valid python identifier, because it will be
            accessible as ``mdp.config.has_<name>`` attribute.
          version
            an object convertible to ``str``, which will be displayed in
            ``mdp.config.info()`` output. Something like ``'0.4.3'``.
        """
    return cls._ExternalDep(name, version=version)


@staticmethod
def button_bind(parent, name, func, what):
    """Find function in QMAinWindow layout as child and bind to it the action.

        :param parent: name of parent where look for items
        :param name: name of child in gui
        :param func: function to bind
        :param what: what element need to find
        """
    action = parent.findChild(what, name)
    action.clicked.connect(func)


def concatcsv(in_files):
    """ This function will contatenate two "comma-separated value"
    text files, but remove the first row (usually column headers) from
    all but the first file.
    """
    import os.path as op
    from nipype.utils.filemanip import split_filename
    if not isinstance(in_files, list):
        return in_files
    if isinstance(in_files[0], list):
        in_files = in_files[0]
    first = open(in_files[0], 'r')
    path, name, ext = split_filename(in_files[0])
    out_name = op.abspath('concat.csv')
    out_file = open(out_name, 'w')
    out_file.write(first.readline())
    first.close()
    for in_file in in_files:
        file_to_read = open(in_file, 'r')
        file_to_read.readline()
        for line in file_to_read:
            out_file.write(line)
    return out_name


def extend_extents(extents, factor=1.1):
    """Extend a given bounding box

    The bounding box (x1, y1, x2, y2) is centrally stretched by the given factor.

    :param extents: The bound box extents
    :param factor: The factor for stretching
    :return: (x1, y1, x2, y2) of the extended bounding box
    """
    width = extents[2] - extents[0]
    height = extents[3] - extents[1]
    add_width = (factor - 1) * width
    add_height = (factor - 1) * height
    x1 = extents[0] - add_width / 2
    x2 = extents[2] + add_width / 2
    y1 = extents[1] - add_height / 2
    y2 = extents[3] + add_height / 2
    return x1, y1, x2, y2


def right_screen_edge(desktop, rect):
    """ Get the x-coordinate of the effective right screen edge.

    Parameters
    ----------
    desktop : QDesktopWidget
        The desktop widget for the application.

    rect : QRect
        The rect of interest.

    Returns
    -------
    result : int
        the x-coordinate of the effective right screen edge.

    """
    p1 = rect.topRight()
    p2 = rect.bottomRight()
    g1 = desktop.availableGeometry(p1)
    g2 = desktop.availableGeometry(p2)
    return min(p1.x(), g1.right(), g2.right())


def line_break():
    """Insert line break at cursor position

    """
    pass


@classmethod
def keys_delete(cls, fqdn, key):
    """Delete a key for a domain."""
    return cls.json_delete('%s/domains/%s/keys/%s' % (cls.api_url, fqdn, key))


def _sample_and_map(proj, parset, progset, progset_instructions,
    result_names, mapping_function, max_attempts, **kwargs):
    """
    Helper function to sample

    This function runs a sampled simulation and also calls an Ensemble's mapping
    function prior to returning. This means that the Result goes out of scope and
    is discarded. Used when performing parallel simulations via `Ensemble.run_sims()`
    (which is used for memory-constrained simulations)

    """
    results = proj.run_sampled_sims(n_samples=1, parset=parset, progset=
        progset, progset_instructions=progset_instructions, result_names=
        result_names, max_attempts=max_attempts)
    plotdata = mapping_function(results[0], **kwargs)
    return plotdata


def get_default_value(key):
    """ Gives default values according to the given key """
    default_values = {'start': 1, 'end': -1, 'step': 1, 'snapshot': 1,
        'format': 'netcdf', 'mask': 'all-atoms', 'reference': 'first',
        'average': 'MyAvg', 'instructions_file': 'instructions.in',
        'cpptraj_path': 'cpptraj', 'Average': {'in_parameters': {'start': 1,
        'end': -1, 'step': 1, 'mask': 'all-atoms'}, 'out_parameters': {
        'format': 'pdb'}}, 'Bfactor': {'in_parameters': {'start': 1, 'end':
        -1, 'step': 1, 'mask': 'all-atoms', 'reference': 'first'}},
        'Convert': {'in_parameters': {'start': 1, 'end': -1, 'step': 1,
        'mask': 'all-atoms'}, 'out_parameters': {'format': 'netcdf'}},
        'Dry': {'in_parameters': {'start': 1, 'end': -1, 'step': 1, 'mask':
        'all-atoms'}, 'out_parameters': {'format': 'netcdf'}}, 'Image': {
        'in_parameters': {'start': 1, 'end': -1, 'step': 1, 'mask':
        'all-atoms'}, 'out_parameters': {'format': 'netcdf'}}, 'Mask': {
        'in_parameters': {'start': 1, 'end': -1, 'step': 1, 'mask':
        'all-atoms'}, 'out_parameters': {'format': 'netcdf'}}, 'Rgyr': {
        'in_parameters': {'start': 1, 'end': -1, 'step': 1, 'mask':
        'all-atoms'}}, 'Rms': {'in_parameters': {'start': 1, 'end': -1,
        'step': 1, 'mask': 'all-atoms', 'reference': 'first'}}, 'Rmsf': {
        'in_parameters': {'start': 1, 'end': -1, 'step': 1, 'mask':
        'all-atoms', 'reference': 'first'}}, 'Slice': {'in_parameters': {
        'start': 1, 'end': -1, 'step': 1, 'mask': 'all-atoms'},
        'out_parameters': {'format': 'netcdf'}}, 'Snapshot': {
        'in_parameters': {'snapshot': 12, 'mask': 'all-atoms'},
        'out_parameters': {'format': 'pdb'}}, 'Strip': {'in_parameters': {
        'start': 1, 'end': -1, 'step': 1, 'mask': 'all-atoms'},
        'out_parameters': {'format': 'netcdf'}}}
    return default_values[key]


def define_instacart_db(db):
    """
    Add the instacart database tables as attributes of the Database class.
    """
    db.departments = db.tables['departments']
    db.orders = db.tables['orders']
    db.aisles = db.tables['aisles']
    db.order_products__prior = db.tables['order_products__prior']
    db.products = db.tables['products']
    db.order_products__train = db.tables['order_products__train']
    db.orders_test = db.tables['orders_test']
    db.orders_no_tests = db.tables['orders_no_tests']


@classmethod
def from_headers(cls, headers):
    """Create RateLimitsInfo from HTTP headers."""
    try:
        data = {'interval': headers['X-RateLimit-Interval'], 'limit':
            headers['X-RateLimit-Limit'], 'remaining': headers[
            'X-RateLimit-Remaining'], 'reset': headers['X-RateLimit-Reset']}
    except KeyError:
        data = {}
    return cls.from_dict(data)


def _get_attribute_tensors(onnx_model_proto):
    """Create an iterator of tensors from node attributes of an ONNX model."""
    for node in onnx_model_proto.graph.node:
        for attribute in node.attribute:
            if attribute.HasField('t'):
                yield attribute.t
            for tensor in attribute.tensors:
                yield tensor


def psi_H_Li_Br_PK74(T, P):
    """c-c'-a: hydrogen lithium bromide [PK74]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def almostequal(first, second, places=7, printit=True):
    """
    Test if two values are equal to a given number of places.
    This is based on python's unittest so may be covered by Python's
    license.

    """
    if first == second:
        return True
    if round(abs(second - first), places) != 0:
        if printit:
            print(round(abs(second - first), places))
            print('notalmost: %s != %s to %i places' % (first, second, places))
        return False
    else:
        return True


def p_track_flag_no_pre_emphasis(t):
    """track_flag : NO PRE_EMPHASIS"""
    from audiotools.toc import TOCFlag_PRE_EMPHASIS
    t[0] = TOCFlag_PRE_EMPHASIS(False)


def building_destroyed_by_user(info):
    """
        Process 'building was destroyed by user' event.

        Input:
        `info`  # An object with information about event's time, building's
                # name, user's callsign and aircraft, position on map.
        """


def my_range(number):
    """range iterator with consistent semantics over Python 2.x and 3.x"""
    current_value = 0
    while current_value < number:
        yield current_value
        current_value += 1


def _determine_pad_amount(image_array, max_height, max_width):
    """
    Given an image and desired max height/width,
    find how much height and width is needed to make
    the image the appropriate size
    """
    height, width, _ = image_array.shape
    height_needed = max_height - height
    width_needed = max_width - width
    return height_needed, width_needed


def col_iscat(df, col_name=None):
    """ Returns a list of columns that are of type 'category'. If col_name is specified, returns
    whether the column in the DataFrame is of type 'category' instead.
    Parameters:
    df - DataFrame
        DataFrame to check
    col_name - string, default None
        If specified, this function will True if df[col_name] is of type 'category'
    """
    col_list = df.select_dtypes(include='category').columns
    if col_name is None:
        return col_list
    else:
        return col_name in col_list


@staticmethod
def list(uri='', recursive=False, human_readable=False, summarize=False):
    """
        List files/folders in a bucket if a URI is specified or list available buckets.

        :param uri: S3 bucket URI
        :param recursive: Recursively list files/folders
        :param human_readable: Displays file sizes in human readable format
        :param summarize: Displays summary information (number of objects, total size)
        :return: Command string
        """
    cmd = 'aws s3 ls {uri}'
    cmd += ' --recursive' if recursive else ''
    cmd += ' --human-readable' if human_readable else ''
    cmd += ' --summarize' if summarize else ''
    return cmd.format(uri=uri)


def convert_neg(arr, dim):
    """
    Convert any negative indices into their positive equivalent.

    Parameters
    ----------
    arr : ndarray
        Array having negative indices converted.
    dim : int
        Dimension of the array.

    Returns
    -------
    ndarray
        The converted array.
    """
    arr[arr < 0] += dim
    return arr


def load_samples(sample_file_paths):
    """Read in processed sample files, returning flat list."""
    samples = []
    for sample_file_path in sample_file_paths:
        with open(sample_file_path, 'r') as sample_file:
            for sample in sample_file:
                sample = sample.strip()
                samples.append(sample)
    return samples


def split_lines(text, lead='', tail='', width=74):
    """Split a string across multiple lines without
dividing up words.  All combinations of whitespace
characters are interpreted as a single space except
repeated newlines, which represent a paragraph break.

Optional keywords are:

'lead'  (def: lead='')
Indicates a string that will be inserted at the
beginning of each line.

'tail'  (def: tail='')
A string that will be inserted at the end of each
line.

'width' (def: width=74)
The maximum line width in characters.  If the lead
and the tail add to more than 'width' characters,
then 'print_lines' returns -1.
"""
    NL = len(lead)
    NT = len(tail)
    tail += '\n'
    Nmax = int(width - NL - NT)
    if Nmax <= 0:
        return -1
    out = lead
    pars = text.split('\n\n')
    for par in pars:
        words = par.split()
        Nline = 0
        for word in words:
            NW = len(word)
            while NW > Nmax:
                if Nline > 0:
                    out += tail
                out += lead + word[:Nmax] + tail + lead
                word = word[Nmax:]
                NW -= Nmax
                Nline = 0
            if Nline + 1 + NW > Nmax:
                out += tail + lead + word
                Nline = NW
            elif Nline == 0:
                out += word
                Nline = NW
            else:
                out += ' ' + word
                Nline += 1 + NW
        out += tail
    return out


def _BuildMessageFromTypeName(type_name, descriptor_pool):
    """Returns a protobuf message instance.

  Args:
    type_name: Fully-qualified protobuf  message type name string.
    descriptor_pool: DescriptorPool instance.

  Returns:
    A Message instance of type matching type_name, or None if the a Descriptor
    wasn't found matching type_name.
  """
    if descriptor_pool is None:
        from google.protobuf import descriptor_pool as pool_mod
        descriptor_pool = pool_mod.Default()
    from google.protobuf import symbol_database
    database = symbol_database.Default()
    try:
        message_descriptor = descriptor_pool.FindMessageTypeByName(type_name)
    except KeyError:
        return None
    message_type = database.GetPrototype(message_descriptor)
    return message_type()


@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 prop_edges(graph, edges_generator, message_func='default', reduce_func=
    'default', apply_node_func='default'):
    """Functional method for :func:`dgl.DGLGraph.prop_edges`.

    Parameters
    ----------
    edges_generator : generator
        The generator of edge frontiers.
    message_func : callable, optional
        The message function.
    reduce_func : callable, optional
        The reduce function.
    apply_node_func : callable, optional
        The update function.

    See Also
    --------
    dgl.DGLGraph.prop_edges
    """
    graph.prop_edges(edges_generator, message_func, reduce_func,
        apply_node_func)


def freestyle_module_add():
    """Add a style module into the list of modules

    """
    pass


def start(st_reg_number):
    """Checks the number valiaty for the Tocantins state"""
    divisor = 11
    if len(st_reg_number) > 11:
        return False
    if len(st_reg_number) < 11:
        return False
    if st_reg_number[2:4] not in ['01', '02', '03', '99']:
        return False
    sum_total = 0
    peso = 9
    for i in range(len(st_reg_number) - 1):
        if i != 2 and i != 3:
            sum_total = sum_total + int(st_reg_number[i]) * peso
            peso = peso - 1
    rest_division = sum_total % divisor
    digit = divisor - rest_division
    if rest_division < 2:
        digit = 0
    return digit == int(st_reg_number[len(st_reg_number) - 1])


def word_count():
    """Return the number of words currently indexed."""


def __new__(cls, loop=None):
    """ The local is singleton per loop. """
    key = '%s%s' % (cls, id(loop))
    if key not in cls.__loops__:
        cls.__loops__[key] = object.__new__(cls)
    return cls.__loops__[key]


def copy_to(input, ctx):
    """Copy the given tensor to the context.

    Parameters
    ----------
    input : Tensor
        The input tensor
    ctx :
        A framework-specific context object.

    Returns
    -------
    Tensor
        The tensor on the given context.
    """
    pass


def list_rindex(lst, el):
    """Index of the last occurrence of an item in a list. Return None is item not found."""
    for i in reversed(range(len(lst))):
        if lst[i] == el:
            return i
    return None


def df_find_nan(data):
    """
    Finds nans and provides a dataframe with the column names and fraction of
    nans in the column.

    """
    temp = data.transpose().isnull()
    temp = temp.transpose().describe()
    temp = temp.transpose().drop('count', axis=1)
    temp.unique = temp.unique == 1
    temp.freq = temp.freq / len(data)
    temp.freq = temp.freq - temp.top
    temp = temp.drop('top', axis=1)
    temp.columns = ['no_nans', 'quality']
    temp_list = []
    for value in temp['quality']:
        if value < 0:
            temp_list.append(1 - value - 1)
        else:
            temp_list.append(value)
    temp['quality'] = temp_list
    return temp


def unpack_argument(session, service_name, operation_name, cli_argument, value
    ):
    """
    Unpack an argument's value from the commandline. This is part one of a two
    step process in handling commandline arguments. Emits the load-cli-arg
    event with service, operation, and parameter names. Example::

        load-cli-arg.ec2.describe-instances.foo

    """
    param_name = getattr(cli_argument, 'name', 'anonymous')
    value_override = session.emit_first_non_none_response(
        'load-cli-arg.%s.%s.%s' % (service_name, operation_name, param_name
        ), param=cli_argument, value=value, service_name=service_name,
        operation_name=operation_name)
    if value_override is not None:
        value = value_override
    return value


def writeMCMC(cache, mcmc_store, process_mcmc_store):
    """write out the mcmc data so it can be plotted"""
    process_mcmc_store(cache, mcmc_store)
    mcmc_store.save()


def _edge_generator_from_csr(csr_matrix):
    """Yield weighted edge triples for use by NetworkX from a CSR matrix.

    This function is a straight rewrite of
    `networkx.convert_matrix._csr_gen_triples`. Since that is a private
    function, it is safer to include our own here.

    Parameters
    ----------
    csr_matrix : scipy.sparse.csr_matrix
        The input matrix. An edge (i, j, w) will be yielded if there is a
        data value for coordinates (i, j) in the matrix, even if that value
        is 0.

    Yields
    ------
    i, j, w : (int, int, float) tuples
        Each value `w` in the matrix along with its coordinates (i, j).

    Examples
    --------

    >>> dense = np.eye(2, dtype=np.float)
    >>> csr = sparse.csr_matrix(dense)
    >>> edges = _edge_generator_from_csr(csr)
    >>> list(edges)
    [(0, 0, 1.0), (1, 1, 1.0)]
    """
    nrows = csr_matrix.shape[0]
    values = csr_matrix.data
    indptr = csr_matrix.indptr
    col_indices = csr_matrix.indices
    for i in range(nrows):
        for j in range(indptr[i], indptr[i + 1]):
            yield i, col_indices[j], values[j]


def delimiter_groups(line, begin_delim=begin_delim, end_delim=end_delim):
    """Split a line into alternating groups.
       The first group cannot have a line feed inserted,
       the next one can, etc.
    """
    text = []
    line = iter(line)
    while True:
        for item in line:
            text.append(item)
            if item in begin_delim:
                break
        if not text:
            break
        yield text
        level = 0
        text = []
        for item in line:
            if item in begin_delim:
                level += 1
            elif item in end_delim:
                level -= 1
                if level < 0:
                    yield text
                    text = [item]
                    break
            text.append(item)
        else:
            assert not text, text
            break


@classmethod
def on_post_start(cls, name, *args):
    """
        Add cnames to a host that starts a specified container

        :param name: name of the container
        :param manifest_description: manifest record for the service.
        :return:
        """
    pass


def putCallBack(epicsArgs, userArgs):
    """
    This is the generic callback function used by the epicsPV.putWait() method.
    It simply sets the callBack.putComplete flag to 1.
    """
    userArgs[0].putComplete = 1


def iterable(obj):
    """ Return True if obj is iterable with a well-defined len()."""
    if hasattr(obj, '__iter__'):
        return True
    try:
        len(obj)
    except:
        return False
    return True


@classmethod
def get_resource_type(cls, rtype):
    """Return the type of the Class"""
    return cls._resource_types.get(rtype)


def multires_external_pack():
    """Pack displacements from an external file

    """
    pass


def _is_type(t):
    """
    Factory for a type checking function of type ``t`` or tuple of types.
    """
    return lambda x: isinstance(x.value, t)


def pymc3_mse(y_true, y_pred_rv):
    """
    Allows you to get a trace of a model's mean square error

    :param y_true: array like
    :param y_pred_rv: PyMC3 Rv trace
    :return:
    """
    import pymc3.math as pmath
    n = len(y_true)
    return pmath.sum(pmath.sqr(y_true - y_pred_rv)) / n


def _defaults_for_key(key):
    """
    Default values for metadata.
    """
    defaults = {'key': key, 'description': None, 'data_dir': '.',
        'remote_data_dir': None, 'data_file': None, 'io_class': None,
        'io_args': None, 'rec_datetime': None, 'filters': None,
        'annotations_file': None, 'epoch_encoder_file': None,
        'epoch_encoder_possible_labels': [], 'amplitude_discriminators':
        None, 'burst_detectors': None, 'tridesclous_file': None,
        'tridesclous_channels': None, 'tridesclous_merge': None,
        'firing_rates': None, 'video_file': None, 'video_offset': None,
        'video_jumps': None, 'video_rate_correction': None, 'plots': None,
        't_width': 40, 'past_fraction': 0.3, 'rauc_baseline': None,
        'rauc_bin_duration': None}
    return defaults


def demix1(servo1, servo2, gain=0.5):
    """de-mix a mixed servo output"""
    s1 = servo1 - 1500
    s2 = servo2 - 1500
    out1 = (s1 + s2) * gain
    out2 = (s1 - s2) * gain
    return out1 + 1500


def DemoCallback(quantdata):
    """
    DemoCallback 是订阅时提供的回调函数模板。该函数只有一个为ResultData类型的参数quantdata
    :param quantdata:ResultData
    :return:
    """
    print(quantdata.data)


@classmethod
def NewPathSpec(cls, type_indicator, **kwargs):
    """Creates a new path specification for the specific type indicator.

    Args:
      type_indicator (str): type indicator.
      kwargs (dict): keyword arguments depending on the path specification.

    Returns:
      PathSpec: path specification.

    Raises:
      KeyError: if path specification is not registered.
    """
    if type_indicator not in cls._path_spec_types:
        raise KeyError('Path specification type: {0:s} not set.'.format(
            type_indicator))
    if 'parent' in kwargs and kwargs['parent'] is None:
        del kwargs['parent']
    path_spec_type = cls._path_spec_types[type_indicator]
    return path_spec_type(**kwargs)


@classmethod
def isword(cls, nextchar):
    """ Whether or not the next character is part of a word """
    return nextchar.isalpha()


def praat_string(text):
    """Return a string formatted to be recognized by Praat.

    Parameters
    ----------
    text : str
        String to be formatted for Praat.

    Returns
    -------
    str

    """
    return '"' + text.replace('"', '""') + '"'


def _get_comments(f):
    """
    return JUST the comment lines from a csv file
    """
    for l in f:
        line = l.rstrip()
        if line and line.startswith('#'):
            yield line


@staticmethod
def get_help(function):
    """
        Get help with specific function
        :param function: function to display help for
        :return: Help doc for requested function
        """
    try:
        return function.__doc__
    except Exception as error:
        return 'Please input proper function from classes provided'


def amod(a, b):
    """Modulus function which returns numerator if modulus is zero"""
    modded = int(a % b)
    return b if modded is 0 else modded


def _get_backend():
    """
    Importing the backend from cryptography has the side effect of activating
    the osrandom engine. This mutates the global state of OpenSSL in the
    process and causes issues for various programs that use subinterpreters or
    embed Python. By putting the import in this function we can avoid
    triggering this side effect unless _get_backend is called.
    """
    from cryptography.hazmat.backends.openssl.backend import backend
    return backend


def enfunc(x):
    """
    So-called K combinator. Given any value `x`, returns a function, that for any
    input value returns `x`
    :param x: value to be returned
    :return: function that returns `x` for any input
    """
    return lambda _: x


@classmethod
def get_by_example(cls, collection, example_data, allow_multiple=False,
    skip=None, limit=None):
    """
            This will find all documents matching a given example.

            :param collection Collection instance
            :param example_data The example document
            :param allow_multiple If the query can return multiple documents
            :param skip  The number of documents to skip in the query
            :param limit The maximal amount of documents to return. The skip is applied before the limit restriction.

            :returns Single document / Document list
        """
    kwargs = {'skip': skip, 'limit': limit}
    return cls._construct_query(name='by-example', collection=collection,
        example=example_data, multiple=allow_multiple, **kwargs)


def __nonzero__():
    """
        Same as __bool__ for Python 2.
        """


def higher(f, c, o=1):
    """Determine whether or not a series value is higher than
    the value ``o`` periods back.

    Parameters
    ----------
    f : pandas.DataFrame
        Dataframe containing the column ``c``.
    c : str
        Name of the column in the dataframe ``f``.
    o : int, optional
        Offset value for shifting the series.

    Returns
    -------
    new_column : pandas.Series (bool)
        The array containing the new feature.

    """
    new_column = f[c] > f[c].shift(o)
    return new_column


def load_np_list(group):
    """ load a numpy list """
    np_list = []
    for key in sorted(group.keys()):
        data = group[key][:]
        np_list.append(data)
    return np_list


def boolean(value):
    """Parse the string ``"true"`` or ``"false"`` as a boolean (case
    insensitive). Also accepts ``"1"`` and ``"0"`` as ``True``/``False``
    (respectively). If the input is from the request JSON body, the type is
    already a native python boolean, and will be passed through without
    further parsing.
    """
    if isinstance(value, bool):
        return value
    if not value:
        raise ValueError('boolean type must be non-null')
    value = value.lower()
    if value in ('true', '1'):
        return True
    if value in ('false', '0'):
        return False
    raise ValueError('Invalid literal for boolean(): {0}'.format(value))


def select_pattern(pattern: str='*', case_sensitive: bool=False, extend:
    bool=True):
    """Select objects matching a naming pattern

    :param pattern: Pattern, Name filter using ‘*’, ‘?’ and ‘[abc]’ unix style wildcards
    :type pattern: str
    :param case_sensitive: Case Sensitive, Do a case sensitive compare
    :type case_sensitive: bool
    :param extend: Extend, Extend the existing selection
    :type extend: bool
    """
    pass


def guess_contains_expat(lines):
    """Tries to determine if a file includes expat
    """
    for line in lines:
        if (
            'reserved prefix (xml) must not be undeclared or bound to another namespace name'
             in line):
            return 1
        if 'cannot change setting once parsing has begun' in line:
            return 1
        if 'requested feature requires XML_DTD support in Expat' in line:
            return 1
    return 0


def get_pic_file_rel_path(file_prefix, num_bits, ZL=True):
    """
    Returns path to Picture file

    Returns
    -------
    str

    """
    return file_prefix + '_' + str(num_bits) + ('_ZL' if ZL else '_ZF'
        ) + 'pic.txt'


def hi(some_number, mood='excited', verbose=False):
    """
    Fancy hello world function. :math:`\\sum_{i=1}^{10} badDocs*BadlyWrittenCode = frustration`

    :param some_number: (int) Something to return.
    :param mood: (str) A string describing a mood.
    :param verbose: (boolean) Where or not to be a gushing hello world program.
    :return: (int) some_number

    :examples:

      >>> from helloJosh import welcome
      >>> welcome.hi(1, 'thrilled', verbose=True) # doctest: +NORMALIZE_WHITESPACE
      hello world! I am thrilled to be a program.
      I am veeeeery thrilled to meet you
      1

    """
    print('hello world! I am %s to be a program.' % mood)
    if verbose:
        print('I am veeeeery %s to meet you' % mood)
    return some_number


@classmethod
def get(cls, f):
    """ decorator to accept GET method """
    return cls._accept_method(['GET'], f)


def describe_resize(ClusterIdentifier=None):
    """
    Returns information about the last resize operation for the specified cluster. If no resize operation has ever been initiated for the specified cluster, a HTTP 404 error is returned. If a resize operation was initiated and completed, the status of the resize remains as SUCCEEDED until the next resize.
    A resize operation can be requested using  ModifyCluster and specifying a different number or type of nodes for the cluster.
    See also: AWS API Documentation

    :example: response = client.describe_resize(
        ClusterIdentifier='string'
    )

    :type ClusterIdentifier: string
    :param ClusterIdentifier: [REQUIRED]
            The unique identifier of a cluster whose resize progress you are requesting. This parameter is case-sensitive.
            By default, resize operations for all clusters defined for an AWS account are returned.

    :rtype: dict
    :return: {
        'TargetNodeType': 'string',
        'TargetNumberOfNodes': 123,
        'TargetClusterType': 'string',
        'Status': 'string',
        'ImportTablesCompleted': [
            'string',
        ],
        'ImportTablesInProgress': [
            'string',
        ],
        'ImportTablesNotStarted': [
            'string',
        ],
        'AvgResizeRateInMegaBytesPerSecond': 123.0,
        'TotalResizeDataInMegaBytes': 123,
        'ProgressInMegaBytes': 123,
        'ElapsedTimeInSeconds': 123,
        'EstimatedTimeToCompletionInSeconds': 123
    }

    :returns:
    (string) --

    """
    pass


def _is_descriptor(obj):
    """Returns True if obj is a descriptor, False otherwise."""
    return hasattr(obj, '__get__') or hasattr(obj, '__set__') or hasattr(obj,
        '__delete__')


def services_by_user(account, days_back, user_arn, user_name):
    """ Returns query for eventsource (service) / actions performed by user. """
    query_string = f"""SELECT DISTINCT eventsource, eventname FROM behold
        WHERE account = '{account}'
        AND (useridentity.arn = '{user_arn}')
        AND from_iso8601_timestamp(eventtime) > date_add('day', -{days_back}, now())
        ORDER BY eventsource, eventname;"""
    return (query_string,
        f'athena_results/services_by_user/{account}/{user_name}')


def to_bool(obj):
    """Convert a boolean-like object.
    >>> to_bool(1)
    True
    >>> to_bool(0)
    False
    >>> to_bool(' True ')
    True
    >>> to_bool('F')
    False
    """
    if isinstance(obj, str):
        return obj.lower().strip() in ('yes', 'true', 'enabled', '1')
    else:
        return bool(obj)


@staticmethod
def _has_valid_extension(filename: str):
    """Check if a filename has a valid extension."""
    return filename.endswith('.pkl') or filename.endswith('.json')


def convert_data(activities_object):
    """
    get result of query and translate it serializable format
    """
    data = []
    for i in activities_object:
        data.append({'id': i.id, 'title': i.__str__(), 'color': i.color,
            'date': str(i.date.day), 'start_time': str(int(str(i.start_time
            )[0:2])), 'finish_time': str(int(str(i.finish_time)[0:2]))})
    return data


def t_UPDATE_CONFLICT_DETECTION(t):
    """update-conflict-detection"""
    return t


def right_pad_list(lst, length, value):
    """Returns a copy of the lst padded to the length specified with the
    given value.
    """
    return lst + [value] * (length - len(lst))


def is_in_t(pt):
    """Checks if a point is in the template triangle T.

       :param pt: The point to be checked.
       :type pt: tuple(float,float).

       :returns: True if pt is in T.
       :rtype: bool.
    """
    flag = True
    if pt[0] < 0 or pt[1] < 0:
        flag = False
    if pt[0] + pt[1] > 1:
        flag = False
    return flag


@classmethod
def parse_text_version(cls, text):
    """Method which extracts latest number from the string.

        Empty string implies 0. No number also implies 0.

        :param str text: Line to search in.
        :return: Latest number
        :rtype: int
        """
    if not text:
        return 0
    matcher = cls.TEXT_VERSION_REGEXP.search(text)
    return int(matcher.group(0)) if matcher is not None else 0


def print_matrix(matrix):
    """Print out a matrix for debugging purposes."""
    matrixT = [[] for x in range(len(matrix[0]))]
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            matrixT[j].append(len(str(matrix[i][j])))
    ndigits = [max(x) for x in matrixT]
    for i in range(len(matrix)):
        print(' '.join('%*s ' % (ndigits[j], matrix[i][j]) for j in range(
            len(matrix[i]))))


def filter_lambda(Input_Dict):
    """
    Filter lambda parameter.

    :param Input_Dict: input parameters dictionary
    :type Input_Dict : dict
    :return: modified dictionary
    """
    try:
        if Input_Dict['lambda'] > 23:
            Input_Dict['lambda'] = 23
            print(
                '[Warning] Opem Automatically Set Lambda To Maximum Value (23) '
                )
        elif Input_Dict['lambda'] < 14:
            Input_Dict['lambda'] = 23
            print(
                '[Warning] Opem Automatically Set Lambda To Minimum Value (14) '
                )
        return Input_Dict
    except Exception:
        return Input_Dict


def start_find():
    """Start searching text

    """
    pass


def bezier(point, line, check=False):
    """
    Apply bezier algorithm to return a value t from 0 to 1 in x and y,
    that is tx and ty, if point is inside line where t would be the
     percentage of the distance from point1 to point2. If point
    is outside line then t<0 or t>1. If line is horizontal then ty is not
    percentage but the distance from the horizontal and conversely if line
    is vertical then tx is not percentage but the distance from the vertical.

    :param point: point
    :param line: (point1, point2)
    :param check: True to check tx and ty and return None if point is not
        between point1 and point2 in the line.
    :return: tx, ty
    """
    px, py = point
    (x1, y1), (x2, y2) = line
    dx = x1 - x2
    dy = y1 - y2
    tx = x1 - px
    if dx != 0:
        tx /= dx
        if check and not 0 <= tx <= 1:
            return None
    elif check and px != x1:
        return None
    ty = y1 - py
    if dy != 0:
        ty /= dy
        if check and not 0 <= ty <= 1:
            return None
    elif check and py != y1:
        return None
    return tx, ty


def filter_by_resources(ctrl_list, resources, hardware_interface=None,
    match_any=False):
    """
    Filter controller state list by claimed resources.

    @param ctrl_list: Controller state list
    @type ctrl_list: [controller_manager_msgs/ControllerState]
    @param resources: Controller resources
    @type resources: [str]
    @param hardware_interface Controller hardware interface where to look for
    resources. If specified, the requested resources will only be searched for
    in this interface. If unspecified, all controller hardware interfaces will
    be searched for; i.e., if a controller claims resources from multiple
    interfaces, the method will succeed if _any_ interface contains the
    requested resources (any or all, depending on the value of C{match_any}).
    Specifying this parameter allows finer control over determining which
    interfaces claim specific resources.
    @param match_any: If set to False, all elements in C{resources} must
    be claimed by the interface specified in C{hardware_interface} (or _any_
    interface, if C{hardware_interface} is unspecified) for a positive match.
    Note that a controller's resources can contain additional entries than
    those in C{resources}).
    If set to True, at least one element in C{resources} must be claimed by
    the interface specified in C{hardware_interface} (or _any_ interface, if
    C{hardware_interface} is unspecified) for a positive match.
    @type match_any: bool
    @return: Controllers matching the specified hardware interface
    @rtype: [controller_manager_msgs/ControllerState]
    """
    list_out = []
    for ctrl in ctrl_list:
        for resource_set in ctrl.claimed_resources:
            if (hardware_interface is None or hardware_interface ==
                resource_set.hardware_interface):
                for res in resources:
                    add_ctrl = not match_any
                    if res in resource_set.resources:
                        if match_any:
                            add_ctrl = True
                            break
                    elif not match_any:
                        add_ctrl = False
                        break
                if add_ctrl:
                    list_out.append(ctrl)
                    break
    return list_out


@staticmethod
def value_json(o):
    """Apply as_json() method on object to get value,
        otherwise return object itself as the value.
        """
    if hasattr(o, 'as_json'):
        return o.as_json()
    return o


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 suite_teardown(suite):
    """Called after every suite has run.

    Add an implementation of this to your own lifecycle module.

    :param unittest_jasmine.unittest.Suite suite: The suite that has completed.
    """
    pass


@staticmethod
def __extract_fasta_string_from_file(file_path):
    """
        Open and read FASTA txt file
        :param file_path: string
        :return: fasta_string: string
        """
    fasta_file = open(file_path, 'r')
    fasta_string = fasta_file.read()
    return fasta_string


@staticmethod
def decode_large_value(current_char, stream):
    """
        If we encounter a value larger than 127, we have to consume from the
        stram until we encounter a value below 127 and recombine them.

        See: https://msdn.microsoft.com/en-us/library/bb540809(v=vs.85).aspx
        """
    buffer = []
    while current_char > 127:
        buffer.append(current_char ^ 128)
        current_char = next(stream)
    total = current_char
    for i, digit in enumerate(reversed(buffer)):
        total += digit * 128 ** (i + 1)
    return total


def csv_list(value):
    """
    Convert a comma separated string into a list

    Parameters
    ----------
    value : str
        The string object to convert to a list

    Returns
    -------
    list
        A list based on splitting the string on the ',' character
    """
    if value:
        result = []
        for item in value.split(','):
            item = item.strip()
            if item:
                result.append(item)
        return result
    return []


def is_collection(value) ->bool:
    """
        Check if value is a collection
    """
    return any([isinstance(value, list), isinstance(value, set), isinstance
        (value, tuple)])


@classmethod
def column_to_number(cls, index):
    """
        Преобразует строковое представление колонки в число

        :param index: номер
        :type index: int
        """
    s = 0
    pow_ = 1
    for letter in index[::-1]:
        d = int(letter, 36) - 9
        s += pow_ * d
        pow_ *= 26
    return s - 1


def screen_set(delta: int=0):
    """Cycle through available screens

    :param delta: Delta
    :type delta: int
    """
    pass


def convolve(y, Ck):
    """Convolve y with Ck"""