@classmethod
def reconstruct_common(cls, t, x, y, z=None, initial=None):
    """Reconstruct angles from 3 or more detections

        This function converts the arguments to be suitable for the
        algorithm.

        :param t: arrival times of the detectors in ns.
        :param x,y,z: positions of the detectors in m. The height
                      for all detectors will be set to 0 if not given.
        :param initial: dictionary containing values from previous
                        reconstructions is ignored.

        """
    if z is None:
        z = [0] * len(x)
    return cls.reconstruct(t, x, y, z)


@staticmethod
def get(config, key, default=None):
    """Gets a value from the config object, or return a default value if
        the parameter does not exist, like :py:meth:`dict.get` does.
        """
    if hasattr(config, key):
        return getattr(config, key)
    else:
        return default


def get_paginator(operation_name=None):
    """
    Create a paginator for an operation.

    :type operation_name: string
    :param operation_name: The operation name. This is the same name
            as the method name on the client. For example, if the
            method name is create_foo, and you'd normally invoke the
            operation as client.create_foo(**kwargs), if the
            create_foo operation can be paginated, you can use the
            call client.get_paginator('create_foo').

    :rtype: L{botocore.paginate.Paginator}
    """
    pass


def download_file_insecure(url, target):
    """
    Use Python to download the file, even though it cannot authenticate the
    connection.
    """
    try:
        from urllib.request import urlopen
    except ImportError:
        from urllib2 import urlopen
    src = dst = None
    try:
        src = urlopen(url)
        data = src.read()
        dst = open(target, 'wb')
        dst.write(data)
    finally:
        if src:
            src.close()
        if dst:
            dst.close()


def provider_parser(subparser):
    """Return the parser for this provider"""
    subparser.add_argument('--auth-token', help=
        'specify token for authentication')


def generate_prototype_list(friendly=False):
    """Generate a list of all prototype files."""
    import glob
    import os
    folder = os.path.expanduser('~/.local/share/applications/')
    pattern = folder + 'one.liberty.*'
    result = glob.glob(pattern)
    if not friendly:
        return result
    elif len(result) == 0:
        return 'No prototype files found.'
    else:
        return ', '.join(result).replace(os.environ['HOME'], '~')


def get_all(isamAppliance, check_mode=False, force=False):
    """
    Get all snmp objects
    """
    return isamAppliance.invoke_get('Get all snmp objects',
        '/core/rsp_snmp_objs')


def register_volume(Ec2VolumeId=None, StackId=None):
    """
    Registers an Amazon EBS volume with a specified stack. A volume can be registered with only one stack at a time. If the volume is already registered, you must first deregister it by calling  DeregisterVolume . For more information, see Resource Management .
    See also: AWS API Documentation

    :example: response = client.register_volume(
        Ec2VolumeId='string',
        StackId='string'
    )

    :type Ec2VolumeId: string
    :param Ec2VolumeId: The Amazon EBS volume ID.

    :type StackId: string
    :param StackId: [REQUIRED]
            The stack ID.

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

    """
    pass


def truncated_normal_(tensor, mean=0.0, std=1.0):
    """Fills the given tensor in-place with elements sampled from the truncated normal
    distribution parameterized by mean and std.

    The generated values follow a normal distribution with specified mean and
    standard deviation, except that values whose magnitude is more than 2 std
    from the mean are dropped.

    We credit to Ruotian Luo for this implementation:
    https://discuss.pytorch.org/t/implementing-truncated-normal-initializer/4778/15.

    Parameters
    ----------
    tensor : Float32 tensor of arbitrary shape
        Tensor to be filled.
    mean : float
        Mean of the truncated normal distribution.
    std : float
        Standard deviation of the truncated normal distribution.
    """
    shape = tensor.shape
    tmp = tensor.new_empty(shape + (4,)).normal_()
    valid = (tmp < 2) & (tmp > -2)
    ind = valid.max(-1, keepdim=True)[1]
    tensor.data.copy_(tmp.gather(-1, ind).squeeze(-1))
    tensor.data.mul_(std).add_(mean)


def validate_post_plugin_load():
    """Checks if the configuration variables are valid.

    If the configuration is invalid then the method will return an error
    message. If all is OK then it will return None.
    """
    message = None
    return message


def build_option(value, label=None):
    """
    Returns value/label inputs in a dictionary for use in
    :dash:`dash_core_components.Dropdown <dash-core-components/Dropdown>`
    """
    return {'label': label or value, 'value': value}


def close_quietly(a_file):
    """Closes a file and does not report an error."""
    try:
        if a_file:
            a_file.close()
    except Exception:
        pass


def getint(tree, attribute):
    """Return an attribute value as an integer (or None)."""
    value = tree.get(attribute)
    if value:
        return int(value)


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 multires_base_apply(modifier: str=''):
    """Modify the base mesh to conform to the displaced mesh

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


def resolve_name(name):
    """Resolve a name like ``module.object`` to an object and return it.

    Raise `ImportError` if the module or name is not found.
    """
    parts = name.split('.')
    cursor = len(parts) - 1
    module_name = parts[:cursor]
    attr_name = parts[-1]
    while cursor > 0:
        try:
            ret = __import__('.'.join(module_name), fromlist=[attr_name])
            break
        except ImportError:
            if cursor == 0:
                raise
            cursor -= 1
            module_name = parts[:cursor]
            attr_name = parts[cursor]
            ret = ''
    for part in parts[cursor:]:
        try:
            ret = getattr(ret, part)
        except AttributeError:
            raise ImportError(name)
    return ret


@staticmethod
def get_creation_arguments_model():
    """
        This is needed so the BaseState can validate the provided
        creation arguments as well as for auto documentation.
        return = {
            'required_creation_arguments': {
                'required_creation_arg_1': type,
                'required_creation_arg_2': type,
            },
            'optional_creation_arguments': {
                'optional_creation_arg_1': type,
                'optional_creation_arg_2': type,
            },
        }
        """
    return {'required_creation_arguments': {},
        'optional_creation_arguments': {'topic': str}}


def mask_hash(hash, show=6, char='*'):
    """
    Return the given hash, with only the first ``show`` number shown. The
    rest are masked with ``char`` for security reasons.
    """
    masked = hash[:show]
    masked += char * len(hash[show:])
    return masked


@classmethod
def from_hint_entry(cls, file_id, entry):
    """Return a KeydirEntry from a file_id + HintEntry."""
    return cls(file_id, entry.tstamp, entry.value_sz, entry.value_pos)


def add_angle_unit_arg(parser):
    """
    Add angle unit selectiona argument to parser.

    Is defined by ``-a`` and ``--aunit``.

    Wether angles are to be calculated in degrees or radians.

    Parameters
    ----------
    parser : `argparse.ArgumentParser <https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser>`_
        The argument parser to add the topology positionl argument.
    """
    parser.add_argument('-a', '--aunit', help=
        'Angular unit, either degrees or radians.', choices=['degrees',
        'radians'], default='degrees')


def duplicate(frames: int=0):
    """Duplicate selected time marker(s)

    :param frames: Frames
    :type frames: int
    """
    pass


def parse(owners_lines):
    """
    takes a list of lines from a OWNERS text file and returns a list of
    :param owners_lines: a list of strings, one for each line of a OWNERS file
    :return: list of (ID, glob) tuples, where glob can be None
    """
    results = []
    for owner_line in owners_lines:
        if owner_line.startswith('@'):
            owner_line = owner_line[1:]
        owner_line = owner_line.strip()
        if not owner_line:
            continue
        if owner_line.startswith('#'):
            continue
        if ' ' in owner_line:
            result = tuple(owner_line.split(' ', 1))
        else:
            result = owner_line, None
        results.append(result)
    return results


@classmethod
def extra_table_metadata(cls, database, table_name, schema_name):
    """Returns engine-specific table metadata"""
    return {}


def LID(val):
    """LocationIdentifier"""
    return val


def upgrade():
    """Upgrade database."""
    pass


def star_extract(input_string):
    """
    This function extract stared repo from raw_html
    :param input_string: raw input html
    :param follower_name: follower_name
    :type input_string:str
    :type follower_name:str
    :return: user_list as list
    """
    user_list = []
    index = 0
    try:
        while index != -1:
            index = input_string.find('<a class="muted-link mr-3', index +
                33, len(input_string))
            length = input_string[index + 33:].find('stargazers">\n')
            star_repo = input_string[index + 34:index + 33 + length]
            if star_repo.find('<svg') == -1 and len(star_repo) != 0:
                user_list.append(star_repo)
        return user_list
    except Exception as ex:
        pass


def _render_jinja2(recipe_dir):
    """Evaluate Conda recipe as a jinja template."""
    try:
        import jinja2
    except ImportError:
        raise Exception(
            'Failed to import jinja2 for evaluating Conda recipe templates.')
    loaders = [jinja2.FileSystemLoader(recipe_dir)]
    env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders))
    template = env.get_or_select_template('meta.yaml')
    return template.render(environment=env)


def nspath_eval(xpath, namespaces):
    """ Return an etree friendly xpath """
    out = []
    for chunks in xpath.split('/'):
        namespace, element = chunks.split(':')
        out.append('{%s}%s' % (namespaces[namespace], element))
    return '/'.join(out)


def wf_action_data(node):
    """
    This function is only applicable to action nodes that produce output data on
    completion.

    The output data is in a Java Properties format and via this EL function it
    is available as a Map.
    """


def SG_to_API(SG):
    """
    Return specific gravity of a liquid given its API, as shown in
    [1]_.

    .. math::
        \\text{API gravity} = \\frac{141.5}{\\text{SG}} - 131.5

    Parameters
    ----------
    SG : float
        Specific gravity of the fluid at 60 degrees Farenheight [-]

    Returns
    -------
    API : float
        API of the fluid [-]

    Notes
    -----
    Defined only at 60 degrees Fahrenheit.

    Examples
    --------
    >>> SG_to_API(0.7365)
    60.62491513917175

    References
    ----------
    .. [1] API Technical Data Book: General Properties & Characterization.
    American Petroleum Institute, 7E, 2005.

    """
    return 141.5 / SG - 131.5


def titlecase(name):
    """Title-case body `name` if it looks safe to do so."""
    return name if name.startswith(('1', 'C/', 'DSS-')) else name.title()


@classmethod
def _from_et(cls, root, parent):
    """
        Create a :class:`Sheet` from a :code:`sheet` element.

        :param root: The element tree tree to parse.
        :param parent: :class:`EagleFilePart` that should hold the resulting :class:`EagleFilePart`
        :rtype: :class:`Sheet`
        """
    n = cls()
    n._init_from_et(root, parent)
    return n


def create_listener(LoadBalancerArn=None, Protocol=None, Port=None,
    SslPolicy=None, Certificates=None, DefaultActions=None):
    """
    Creates a listener for the specified Application Load Balancer.
    You can create up to 10 listeners per load balancer.
    To update a listener, use  ModifyListener . When you are finished with a listener, you can delete it using  DeleteListener . If you are finished with both the listener and the load balancer, you can delete them both using  DeleteLoadBalancer .
    For more information, see Listeners for Your Application Load Balancers in the Application Load Balancers Guide .
    See also: AWS API Documentation

    Examples
    This example creates an HTTP listener for the specified load balancer that forwards requests to the specified target group.
    Expected Output:
    This example creates an HTTPS listener for the specified load balancer that forwards requests to the specified target group. Note that you must specify an SSL certificate for an HTTPS listener. You can create and manage certificates using AWS Certificate Manager (ACM). Alternatively, you can create a certificate using SSL/TLS tools, get the certificate signed by a certificate authority (CA), and upload the certificate to AWS Identity and Access Management (IAM).
    Expected Output:

    :example: response = client.create_listener(
        LoadBalancerArn='string',
        Protocol='HTTP'|'HTTPS',
        Port=123,
        SslPolicy='string',
        Certificates=[
            {
                'CertificateArn': 'string'
            },
        ],
        DefaultActions=[
            {
                'Type': 'forward',
                'TargetGroupArn': 'string'
            },
        ]
    )

    :type LoadBalancerArn: string
    :param LoadBalancerArn: [REQUIRED]
            The Amazon Resource Name (ARN) of the load balancer.

    :type Protocol: string
    :param Protocol: [REQUIRED]
            The protocol for connections from clients to the load balancer.

    :type Port: integer
    :param Port: [REQUIRED]
            The port on which the load balancer is listening.

    :type SslPolicy: string
    :param SslPolicy: The security policy that defines which ciphers and protocols are supported. The default is the current predefined security policy.

    :type Certificates: list
    :param Certificates: The SSL server certificate. You must provide exactly one certificate if the protocol is HTTPS.
            (dict) --Information about an SSL server certificate deployed on a load balancer.
            CertificateArn (string) --The Amazon Resource Name (ARN) of the certificate.

    :type DefaultActions: list
    :param DefaultActions: [REQUIRED]
            The default action for the listener.
            (dict) --Information about an action.
            Type (string) -- [REQUIRED]The type of action.
            TargetGroupArn (string) -- [REQUIRED]The Amazon Resource Name (ARN) of the target group.

    :rtype: dict
    :return: {
        'Listeners': [
            {
                'ListenerArn': 'string',
                'LoadBalancerArn': 'string',
                'Port': 123,
                'Protocol': 'HTTP'|'HTTPS',
                'Certificates': [
                    {
                        'CertificateArn': 'string'
                    },
                ],
                'SslPolicy': 'string',
                'DefaultActions': [
                    {
                        'Type': 'forward',
                        'TargetGroupArn': 'string'
                    },
                ]
            },
        ]
    }

    """
    pass


def bisect_left(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e < x, and all e in
    a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
    insert just before the leftmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo + hi) // 2
        if a[mid] < x:
            lo = mid + 1
        else:
            hi = mid
    return lo


def set_shared_steemd_instance(steemd_instance):
    """ This method allows us to override default steem instance for all users of
    _shared_steemd_instance.
    """
    global _shared_steemd_instance
    _shared_steemd_instance = steemd_instance


def _padd(secret, blocksize=16, padding=' '):
    """Pads secret if not legal AES block size (16, 24, 32)"""
    if not len(secret) in (16, 24, 32):
        return secret + (blocksize - len(secret)) * padding
    return secret


def assertFrameEqual(df1, df2, **kwds):
    """Return True if two DataFrames are equal, ignoring order of columns (REF 014).

    Parameters
    ----------
    {df1, df2} : DataFrame
        Compare two objects using pandas testing tools.
    **kwds : dict-like
        Keywords for `pandas.util.testing.assert_frame_equal()`.

    Returns
    -------
    None
        Uses tools.pandas.testing; raises exceptions if not equal.

    """
    from pandas.util.testing import assert_frame_equal
    return assert_frame_equal(df1.sort_index(axis=1), df2.sort_index(axis=1
        ), check_names=True, **kwds)


def GetSNConstant(*args, **kwargs):
    """ Stub Anduin function."""
    print('GetSNConstant', args, kwargs)


def palettecolor_hide(unselected: bool=False):
    """Hide selected/unselected Grease Pencil colors

    :param unselected: Unselected, Hide unselected rather than selected colors
    :type unselected: bool
    """
    pass


def col_to_boxstats(frame, name):
    """
    Allow inter-op with matplotlib using pre-calculated stats
    See test_data.py
    """
    stats = []
    for index, row in frame.iterrows():
        if row[name]:
            stats.append({'label': index, 'whislo': row[name][0], 'q1': row
                [name][1], 'med': row[name][2], 'q3': row[name][3],
                'whishi': row[name][4]})
    return stats


@classmethod
def patch_ports(cls, pair):
    """Wrap ports with int or use None."""
    if pair[0] in ('remote_port', 'local_port'):
        return pair[0], pair[1] and int(pair[1]) or None
    return pair


def getFields():
    """Return all the fields of the schema as a list.
        """


def gather_all_unsorted(threadpool, func, params, *args, **kwargs):
    """ Runs multiple tasks concurrently and returns all results in the order
		of execution finish as soon as possible.
	"""
    for i, param in enumerate(params):
        threadpool.add_task(i, func, param, *args, **kwargs)
    for i in range(len(params)):
        yield threadpool.get()


def break_sentence(sentence, max_sent_len):
    """
  For example, for a sentence with 70 words, supposing the the `max_sent_len'
  is 30, break it into 3 sentences.

  :param sentence: list[str] the sentence
  :param max_sent_len:
  :return:
  """
    ret = []
    cur = 0
    length = len(sentence)
    while cur < length:
        if cur + max_sent_len + 5 >= length:
            ret.append(sentence[cur:length])
            break
        ret.append(sentence[cur:min(length, cur + max_sent_len)])
        cur += max_sent_len
    return ret


@staticmethod
def get_field_by_name(field_name, field_list):
    """Helper function to return a field via a string name match given a field and field list

        Args:
            field_name (str): The string name of the desired field
            field_list (list): A list of field objects

        Returns:
            dict: The field object if found, `None` otherwise
        """
    for field in field_list:
        if field['field']['field_name'] == field_name:
            return field
    return None


def clear_screen():
    """
    Clears the terminal buffer
    """
    print('\x1bc')


def vector_title(flag, n, cmplx_part=None):
    """
    Creates a plot title for each left/right singular vector
    Returns a raw formatted string using LaTeX

    Parameters:
    flag: a string, either 'left' or 'right'
    n: a nonnegative integer
    dtype: data type (real or complex)
    """
    if flag == 'left':
        if cmplx_part == 'real':
            title = (
                '$\\mathrm{Re}\\{\\widehat{\\phi}_{%d}(\\mathbf{x}_r,\\nu)\\}$'
                 % n)
        elif cmplx_part == 'imag':
            title = (
                '$\\mathrm{Im}\\{\\widehat{\\phi}_{%d}(\\mathbf{x}_r,\\nu)\\}$'
                 % n)
        else:
            title = 'Left-Singular Vector $\\phi_{%d}(\\mathbf{x}_r,t)$' % n
    elif flag == 'right':
        if cmplx_part == 'real':
            title = (
                '$\\mathrm{Re}\\{\\widehat{\\psi}_{%d}(\\mathbf{x}_s,\\nu)\\}$'
                 % n)
        elif cmplx_part == 'imag':
            title = (
                '$\\mathrm{Im}\\{\\widehat{\\psi}_{%d}(\\mathbf{x}_s,\\nu)\\}$'
                 % n)
        else:
            title = 'Right-Singular Vector $\\psi_{%d}(\\mathbf{x}_s,t)$' % n
    return title


@classmethod
def add_provider(cls, provider, locale=None):
    """Add a new Faker provider for the specified locale"""
    cls._get_faker(locale).add_provider(provider)


def listify(var):
    """Always return item as list"""
    if type(var) is list:
        return var
    else:
        return [var]


@classmethod
def from_dict(cls, json_dict):
    """Return an instance of this class formed from ``json_dict``."""
    return cls(json_dict)


@classmethod
def decode(cls, stream):
    """Decodes presentation data value item from data stream

        Presentation data value is left in raw string format.
        The Application Entity is responsible for dealing with it.

        :param stream: raw data stream
        :return: decoded presentation data value item
        """
    item_length, context_id = cls.header.unpack(stream.read(5))
    data_value = stream.read(int(item_length) - 1)
    return cls(context_id, data_value)


def psi_Ca_Mg_HCO3_HMW84(T, P):
    """c-c'-a: calcium magnesium bicarbonate [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def replace_iam_instance_profile_association(IamInstanceProfile=None,
    AssociationId=None):
    """
    Replaces an IAM instance profile for the specified running instance. You can use this action to change the IAM instance profile that's associated with an instance without having to disassociate the existing IAM instance profile first.
    Use  DescribeIamInstanceProfileAssociations to get the association ID.
    See also: AWS API Documentation

    :example: response = client.replace_iam_instance_profile_association(
        IamInstanceProfile={
            'Arn': 'string',
            'Name': 'string'
        },
        AssociationId='string'
    )

    :type IamInstanceProfile: dict
    :param IamInstanceProfile: [REQUIRED]
            The IAM instance profile.
            Arn (string) --The Amazon Resource Name (ARN) of the instance profile.
            Name (string) --The name of the instance profile.

    :type AssociationId: string
    :param AssociationId: [REQUIRED]
            The ID of the existing IAM instance profile association.

    :rtype: dict
    :return: {
        'IamInstanceProfileAssociation': {
            'AssociationId': 'string',
            'InstanceId': 'string',
            'IamInstanceProfile': {
                'Arn': 'string',
                'Id': 'string'
            },
            'State': 'associating'|'associated'|'disassociating'|'disassociated',
            'Timestamp': datetime(2015, 1, 1)
        }
    }

    """
    pass


def psi_H_Mnjj_Cl_PK74(T, P):
    """c-c'-a: hydrogen manganese(II) chloride [PK74]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def assign_role_to_principal_no_inherit(role_id, principal_id):
    """Assign the role to the principal."""


def michea_m(num_types, freq_spectrum):
    """Calculate Michéa’s M (1969, 1971).

    Used formula:
        .. math::
            M = rac{V}{D}

    Parameters:
        num_types (int): Absolute number of types.
        freq_spectrum (dict): Counted occurring frequencies.
    """
    return num_types / freq_spectrum[2]


def from_u16(n: int) ->int:
    """Reinterpret the unsigned 16-bit integer `n` as a signed integer."""
    if n >= 2 ** 15:
        return -(2 ** 16 - n)
    else:
        return n


@staticmethod
def do_quit(unused_inp):
    """Exit without applying changes"""
    return True


def is_string(s):
    """
    Returns whether the object is a string.
    :param s: the object
    :return: whether is a string
    """
    return type(s) is str


def write_crf_file(filename, document, separator=' '):
    """
    Takes a tagged document (a list of tagged sentences) and writes it
    to a file which is structured like y w pos. Here y denotes the label
    of the token, w is the token itself and pos is the pos tag of the token

    Example:
    Input: [[(0, 6, 'Verden', 'O', 'NE'), (6, 7, '/', 'O', '$['), (7, 19, 'MünchenNeuer', 'O', 'NE'), (20, 32, 'gigantischer', 'O', 'ADJA'), (33, 38, 'Daten', 'O', 'NN'), ... ]]

    Output:
    O Verden NE
    O / $[
    O MünchenNeuer NE
    O gigantischer ADJA
    O Daten NN
    ...

    :param filename: the file to write the output to
    :param document: the tagged documents data that should be transformed into a crf file
    :param seperator: the seperator to use in oder to sperate the elements
    """
    output = open(filename, 'w')
    for sentence in document:
        for token in sentence:
            output.write(token[3] + separator + token[2] + separator +
                token[4] + '\n')
        output.write('\n')
    output.close()


def _auth_plugin_available(ext):
    """Read the value of available for whether to load this plugin."""
    return ext.obj.available


def assure_list_from_str(s, sep='\n'):
    """Given a multiline string convert it to a list of return None if empty

    Parameters
    ----------
    s: str or list
    """
    if not s:
        return None
    if isinstance(s, list):
        return s
    return s.split(sep)


def play_rendered_anim():
    """Play back rendered frames/movies using an external player

    """
    pass


@classmethod
def decode_ssh_public(cls, packet):
    """Decode an SSH format SK ECDSA public key"""
    curve_id = packet.get_string()
    public_value = packet.get_string()
    application = packet.get_string()
    return curve_id, public_value, application


def get_KEGG_comps(db, kegg_db, model_ids):
    """Get KEGG IDs from KEGG MINE database for compounds in model(s)."""
    kegg_ids, _ids = set(), set()
    for model_id in model_ids:
        comp_ids = kegg_db.models.find_one({'_id': model_id}, {'Compounds': 1}
            )['Compounds']
        for comp_id in comp_ids:
            kegg_ids.add(comp_id)
    for kegg_id in kegg_ids:
        for comp in db.compounds.find({'DB_links.KEGG': kegg_id}, {'_id': 1}):
            _ids.add(comp['_id'])
    return _ids


def process(proc_data):
    """
    Final processing to conform to the schema.

    Parameters:

        proc_data:   (dictionary) raw structured data to process

    Returns:

        List of dictionaries. Structured data with the following schema:

        [
          {
            "foo":     string,
            "bar":     boolean,
            "baz":     integer
          }
        ]
    """
    return proc_data


def mean_estimator(data_variance, n, ddof=1):
    """
    Get the variance of the mean from a data variance term (e.g. an eigenvalue)
    and return an estimator of the precision of the mean (e.g. the variance of
    the mean itself.)

    Note: this is not used in the actual calculation of PCA planar
    fitting errors; it is present for testing purposes.
    """
    return data_variance / (n - ddof)


def new_oids(cursor):
    """
        Return a new :class:`list` of new, unused integer OIDs.

        The list should be contiguous and must be in sorted order from
        highest to lowest. It must never contain 0.
        """


def circle(lat, lon, radius):
    """
    Creates an API-ready circle from the given latitude, longitude,
    and radius parameters
    """
    return {'$circle': {'$center': [lat, lon], '$meters': radius}}


def _url_collapse_path(path):
    """
    Given a URL path, remove extra '/'s and '.' path elements and collapse
    any '..' references and returns a colllapsed path.

    Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
    The utility of this function is limited to is_cgi method and helps
    preventing some security attacks.

    Returns: A tuple of (head, tail) where tail is everything after the final /
    and head is everything before it.  Head will always start with a '/' and,
    if it contains anything else, never have a trailing '/'.

    Raises: IndexError if too many '..' occur within the path.

    """
    path_parts = path.split('/')
    head_parts = []
    for part in path_parts[:-1]:
        if part == '..':
            head_parts.pop()
        elif part and part != '.':
            head_parts.append(part)
    if path_parts:
        tail_part = path_parts.pop()
        if tail_part:
            if tail_part == '..':
                head_parts.pop()
                tail_part = ''
            elif tail_part == '.':
                tail_part = ''
    else:
        tail_part = ''
    splitpath = '/' + '/'.join(head_parts), tail_part
    collapsed_path = '/'.join(splitpath)
    return collapsed_path


def minKey(key=None):
    """Return the minimum key.

        If a key argument if provided and not None, return the smallest key
        that is greater than or equal to the argument.  Raise an exception
        if no such key exists.
        """


@staticmethod
def _is_header(line):
    """Abstract method to check if a line is a header line."""
    raise NotImplementedError


def jump(line: int=1):
    """Jump cursor to line

    :param line: Line, Line number to jump to
    :type line: int
    """
    pass


def is_analysis_descendent(exclude_functions=None):
    """
    Check whether any call in the traceback comes from a function part of a
    :meth:`ngs_toolkit.Analysis` object.

    Parameters
    ----------
    exclude_functions : :obj:`list`
        List of function names to exclude from.

    Returns
    ----------
    tuple
        If is descentent, returns tuple of (Analysis instance, function name),
        othewise returns :obj:`False`.
    """
    import inspect
    from ngs_toolkit import Analysis
    if exclude_functions is None:
        exclude_functions = list()
    for s in inspect.stack():
        if s.function in exclude_functions:
            return False
        if 'self' not in s.frame.f_locals:
            continue
        if isinstance(s.frame.f_locals['self'], Analysis):
            return s.frame.f_locals['self'], s.function
    return False


def encode_html(text, quotes=True):
    """Return text that's safe for an HTML attribute."""
    a = ('&', '&'), ('<', '<'), ('>', '>')
    if quotes:
        a = a + (("'", '''), ('"', '"'))
    for k, v in a:
        text = text.replace(k, v)
    return text


def noop(stream):
    """
    Used when we didn't explicitly specify a stream to avoid using
    codecs.get{reader,writer}
    """
    return stream


def _reshape_for_est(X_del):
    """Convert X_del to a sklearn-compatible shape."""
    n_times, n_epochs, n_feats, n_delays = X_del.shape
    X_del = X_del.reshape(n_times, n_epochs, -1)
    X_del = X_del.reshape(n_times * n_epochs, -1, order='F')
    return X_del


def check_network_status(*args):
    """Check current network status, it will return True if
       current network is inactive, otherwise, return False
    """
    networkname, conn, logger = args
    net_list = conn.listNetworks()
    logger.debug('current active network list:')
    logger.debug(net_list)
    if networkname not in net_list:
        return True
    else:
        return False


def check_numbers(matrix, n_clusters):
    """Check if the given matrix has enough rows and columns for the given
    number of co-clusters.

    Parameters
    ----------
    matrix:
        The input matrix

    n_clusters: int
        Number of co-clusters

    Raises
    ------
    ValueError
        If the data matrix has not enough rows or columns.
    """
    if matrix.shape[0] < n_clusters or matrix.shape[1] < n_clusters:
        raise ValueError('data matrix has not enough rows or columns')


def list(session, attributes='Name,State,StartMode'):
    """
    Description: return list of all services on windows machine

    :param session: instance of Windows, which hold session to win machine
    :type session: winremote.Windows
    :param attributes: comma delimited name of attributes to be returned
    :type attributes: str
    :returns: list of services info
    :rtype: list of dict
    """
    return session._wmi.query('select %s from Win32_service' % attributes)


@staticmethod
def bound(position):
    """Normalize the position."""
    if position is None:
        return None
    position = int(position)
    if position <= 5:
        return 0
    if position >= 95:
        return 100
    return position


def slice_axis(data, axis, begin, end):
    """Slice along a given axis.
    Returns an array slice along a given axis starting from :attr:`begin` index to :attr:`end` index.

    Parameters
    ----------
    data : Tensor
        The data tensor.
    axis : int
        The axis along to slice the tensor.
    begin : int
        Indicates the begin index.
    end : int
        Indicates the end index.
    Returns:
    --------
    Tensor
        The sliced tensor.
    """
    pass


@staticmethod
def separate_valid_and_failed_hashes(file_hashes):
    """
        Given a list of file hashes seperate them into a list of valid and a list of failed.
        :param file_hashes: [FileHash]
        :return: [FileHash], [FileHash]: valid_file_hashes, failed_file_hashes
        """
    valid_file_hashes = []
    failed_file_hashes = []
    for file_hash in file_hashes:
        if file_hash.is_valid():
            valid_file_hashes.append(file_hash)
        else:
            failed_file_hashes.append(file_hash)
    return valid_file_hashes, failed_file_hashes


def is_channel(mm):
    """Check whether the measurement was performed in the channel

    If the chip region is not set, then it is assumed to be a
    channel measurement (for backwards compatibility and user-
    friendliness).
    """
    if 'setup' in mm.config and 'chip region' in mm.config['setup']:
        region = mm.config['setup']['chip region']
        if region == 'channel':
            return True
        else:
            return False
    else:
        return True


def model_to_dict(model):
    """Turns a Keras model into a Python dictionary

    :param model: Keras model instance
    :return: dictionary with model information
    """
    return dict(model=model.to_json(), weights=model.get_weights())


def all_station_manipulation(df_1, df_2):
    """
    Performs math operations on both station's data together to calculate the differences between them, average and
    maximum values. These results are currently appended to the dataframe of station_1 but will eventually be stored in
    their own dataframe.

    All equations/constants are lifted from read_data_JETS.

    :param df_1: dataframe containing DAF data from station_1
    :param df_2: dataframe containing DAF data from station_2
    :return:
    """
    df_1['vel_diff'] = abs(df_1['vel'] - df_2['vel'])
    df_1['vel_avg'] = (df_1['vel'] + df_2['vel']) / 2
    df_1['max_abs_mag_diff'] = df_1['max_mag'] - df_2['max_mag']
    df_1['avg_abs_mag'] = df_1['max_mag'].where(df_1['max_mag'] < df_2[
        'max_mag'], df_2['max_mag'])
    df_1['intg_luminosity'] = df_1['intg_luminosity'].where(df_1[
        'intg_luminosity'] > df_2['intg_luminosity'], df_2['intg_luminosity'])
    df_1['intg_luminosity_err'] = df_1['intg_luminosity_err'].where(df_1[
        'intg_luminosity_err'] > df_2['intg_luminosity_err'], df_2[
        'intg_luminosity_err'])
    return df_1


def hump_to_underscore(name):
    """
    Convert Hump style to underscore

    :param name: Hump Character
    :return: str
    """
    new_name = ''
    pos = 0
    for c in name:
        if pos == 0:
            new_name = c.lower()
        elif 65 <= ord(c) <= 90:
            new_name += '_' + c.lower()
            pass
        else:
            new_name += c
        pos += 1
        pass
    return new_name


def compare_gpstime(g0, g1):
    """
    Returns the index of the earlier GPSTIME (wn,tow) tow.
    """
    if g0[0] < g1[0]:
        return 0
    elif g0[0] > g1[0]:
        return 1
    elif g0[1] < g1[1]:
        return 0
    elif g0[1] > g1[1]:
        return 1
    else:
        return 0


@staticmethod
def is_valid(_by='', path=None):
    """Returns true if the selenium selector is valid

        :param str _by: Selenium By locator
        :param str path: Locator value
        :return: True, if the selenium selector is valid
        :rtype: bool
        """
    return True if _by in ('class name', 'css selector', 'id', 'link text',
        'name', 'partial link text', 'tag name', 'xpath') and path else False


def add_text(pic, x, y, s, col):
    """Draw str s in Color col on Picture pic starting at (x, y)."""
    pic.add_text(col, x, y, s)


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

        :param FileRequestReceiveFileDetails val:
        :rtype: EventDetails
        """
    return cls('file_request_receive_file_details', val)


def _scrub_headers(headers):
    """scrub auth info from headers"""
    headers = dict(headers)
    if 'Authorization' in headers:
        auth = headers['Authorization']
        if auth.startswith('token '):
            headers['Authorization'] = 'token [secret]'
    return headers


def get_bucket(module, blade):
    """Return Bucket or None"""
    s3bucket = None
    buckets = blade.buckets.list_buckets()
    for bucket in range(0, len(buckets.items)):
        if buckets.items[bucket].name == module.params['name']:
            s3bucket = buckets.items[bucket]
    return s3bucket


@classmethod
def remove_repository(cls, options=None):
    """Disassociate a resource"""
    cls.command_sub = 'remove-repository'
    return cls.execute(cls._construct_command(options))


def evidence_from_inversion_terms(chi_squared, regularization_term,
    log_curvature_regularization_term, log_regularization_term,
    noise_normalization):
    """Compute the log evidence of an inversion's fit to a dataset, where the log evidence includes a number of terms which
    quantify the complexity of an inversion's reconstruction (see the *inversion* module):

    Log Evidence = -0.5*[Chi_Squared_Term + Regularization_Term + Log(Covariance_Regularization_Term) -
                           Log(Regularization_Matrix_Term) + Noise_Term]

    Parameters
    ----------
    chi_squared : float
        The chi-squared term of the inversion's fit to the dataset.
    regularization_term : float
        The regularization term of the inversion, which is the sum of the difference between reconstructed         flux of every pixel multiplied by the regularization coefficient.
    log_curvature_regularization_term : float
        The log of the determinant of the sum of the curvature and regularization matrices.
    log_regularization_term : float
        The log of the determinant o the regularization matrix.
    noise_normalization : float
        The normalization noise_map-term for the dataset's noise map.
    """
    return -0.5 * (chi_squared + regularization_term +
        log_curvature_regularization_term - log_regularization_term +
        noise_normalization)


@classmethod
def create(cls, a0: float, a1: float, a2: float):
    """
        Create configured Pump object passing the pump coefficients that describe the pump curve.
        The pump curve is expressed by the equation: dp = a0 + a1 * V + a2 * V **2

        **Parameters:**

        - `a0`: (*float*)
        - `a1`: (*float*)
        - `a2`: (*float*)

        """
    p = cls()
    p._a0 = a0
    p._a1 = a1
    p._a2 = a2
    return p


@classmethod
def beliefs(cls):
    """
        Not applicable for gliders
        """
    return {}


def ask(value, func, *args, **kwargs):
    """
    Execute function with the given arguments if value is None and return.
    Otherwise, return value.
    """
    if value is None:
        return func(*args, **kwargs)
    else:
        return value


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 df_column_dtypes(df, dtype: str=None, target: str=None, max_cardinality:
    int=10):
    """Return the columns grouped by their dtypes or for a specific dtype"""
    numeric_cols = []
    cat_cols = []
    for col in df.columns:
        if col == target:
            continue
        if df[col].dtype in (int, float) and df[col].nunique(
            ) > max_cardinality:
            numeric_cols.append(col)
        else:
            cat_cols.append(col)
    dtypes = dict(numeric=numeric_cols, categorical=cat_cols)
    return dtypes[dtype] if dtype else dtypes


@classmethod
def get_all(cls):
    """
        Get a list of all requests (in order of their ID).
        """
    return sorted(cls._cache.values(), key=lambda r: r._id)


@classmethod
def extract(cls, val):
    """
        Extracts value from Optional() instance

        :param val:
        :return: original value if it's not Optional instance else
            value of instance
        """
    if isinstance(val, cls):
        return val.getval()
    return val


def render_pep440_old(pieces):
    """TAG[.postDISTANCE[.dev0]] .

    The ".dev0" means dirty.

    Eexceptions:
    1: no tags. 0.postDISTANCE[.dev0]
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance'] or pieces['dirty']:
            rendered += '.post%d' % pieces['distance']
            if pieces['dirty']:
                rendered += '.dev0'
    else:
        rendered = '0.post%d' % pieces['distance']
        if pieces['dirty']:
            rendered += '.dev0'
    return rendered


def c_uchar(i):
    """
    Convert arbitrary integer to c unsigned char type range as if casted in c.

    >>> c_uchar(0x12345678)
    120
    >>> (c_uchar(-123), c_uchar(-1), c_uchar(255), c_uchar(256))
    (133, 255, 255, 0)
    """
    return i & 255


def _ones_complement_sum_carry_16(a, b):
    """Compute ones complement sum and carry at 16 bits.

    :type a: int
    :type b: int

    :return: Sum of a and b, ones complement, carry at 16 bits.
    """
    pre_sum = a + b
    return (pre_sum & 65535) + (pre_sum >> 16)


def stroke_lock_color():
    """Lock any color not used in any selected stroke

    """
    pass


def unit_func(x):
    """Dummy quantity function for histogrammar objects

    :param x: value
    :returns: the same value
    """
    return x


def get_book_message_type(message):
    """Reads raw orderbook channel messages and extracts the message type and
    message body"""
    if isinstance(message, list) and message[1] == 'hb':
        message_type = 'hb'
    elif isinstance(message, list) and isinstance(message[1][0], list):
        message_type = 'snapshot'
    elif isinstance(message, list) and len(message[1]) >= 2:
        message_type = 'update'
    else:
        message_type = message['event']
    return message_type, message


def summary_counters(result):
    """Return a set of statistics from a Cypher statement execution."""
    return result.summary().counters


def get_survey_cols(cols, s_cols, prefix):
    """
    Returns the names of the magnitudes in a specific survey

    :param cols:
    :param s_cols:
    :param prefix:
    :return:
    """
    out = []
    prefix_cond = len(prefix) == 0
    for i, c in enumerate(cols):
        for j, s in enumerate(s_cols):
            if s in c and prefix in c and not prefix_cond or prefix_cond and (
                c == s or 'mag' in c or 'Petro' in c):
                out.append(c)
                break
    cols_new = []
    for i, c in enumerate(cols):
        if c not in out:
            cols_new.append(c)
    return cols_new, out


def add_auth_arguments(parser, usr_required=False):
    """ Add 'user' and 'password' arguments
    :param user_required: set 'user' argument as required or not  """
    parser.add_argument('-u', '--user', dest='username', required=usr_required)
    parser.add_argument('-p', '--password', dest='password')


@classmethod
def create_qos_policy(cls, name, description=None, shared=False, project_id
    =None, is_default=False):
    """Wrapper utility that returns a test QoS policy."""
    body = cls.admin_client.create_qos_policy(name, description, shared,
        project_id, is_default)
    qos_policy = body['policy']
    cls.qos_policies.append(qos_policy)
    return qos_policy


def populate_session_msg_context(request, context):
    """
    Takes an existing ``context`` object (as constructed in
    ``get_context_data()`` and populates any session messages
    we may have.
    """
    for varname in ['success', 'fail']:
        session_var = 'exordium_msg_%s' % varname
        ctx_var = 'messages_%s' % varname
        if session_var in request.session:
            if len(request.session[session_var]) > 0:
                context[ctx_var] = request.session[session_var]
                del request.session[session_var]


def is_slice_index(index):
    """see if index is a slice index or has slice in it"""
    if isinstance(index, slice):
        return True
    if isinstance(index, tuple):
        for i in index:
            if isinstance(i, slice):
                return True
    return False


def reset_instance_attribute(DryRun=None, InstanceId=None, Attribute=None):
    """
    Resets an attribute of an instance to its default value. To reset the kernel or ramdisk , the instance must be in a stopped state. To reset the sourceDestCheck , the instance can be either running or stopped.
    The sourceDestCheck attribute controls whether source/destination checking is enabled. The default value is true , which means checking is enabled. This value must be false for a NAT instance to perform NAT. For more information, see NAT Instances in the Amazon Virtual Private Cloud User Guide .
    See also: AWS API Documentation

    :example: response = client.reset_instance_attribute(
        DryRun=True|False,
        InstanceId='string',
        Attribute='instanceType'|'kernel'|'ramdisk'|'userData'|'disableApiTermination'|'instanceInitiatedShutdownBehavior'|'rootDeviceName'|'blockDeviceMapping'|'productCodes'|'sourceDestCheck'|'groupSet'|'ebsOptimized'|'sriovNetSupport'|'enaSupport'
    )

    :type DryRun: boolean
    :param DryRun: Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation . Otherwise, it is UnauthorizedOperation .

    :type InstanceId: string
    :param InstanceId: [REQUIRED]
            The ID of the instance.

    :type Attribute: string
    :param Attribute: [REQUIRED]
            The attribute to reset.
            Warning
            You can only reset the following attributes: kernel | ramdisk | sourceDestCheck . To change an instance attribute, use ModifyInstanceAttribute .

    """
    pass


@staticmethod
def format(value):
    """Format CRC value to string."""
    return '0x%04X' % value


def after_pack(oid_int, tid_int):
    """
        Called after an object state has been removed by packing.

        Removes the corresponding blob file.
        """


def register(me, client, real):
    """register as clients/listeners."""
    par = me.par()
    if not par:
        return
    clid = id(client)
    if real:
        par.clients[clid] = client
    else:
        del par.clients[clid]


def size(matrix, dim=None):
    """Returns the size of a matrix (m,n).
    Dim can be set to 0 to return m (rows) or 1 to return n (columns)

    :param matrix: pose
    :type matrix: :class:`.Mat`
    :param dim: dimension
    :type dim: int
    """
    return matrix.size(dim)


def snap_selected_to_grid():
    """Snap selected item(s) to nearest grid division

    """
    pass


def maybe_s_to_ms(v):
    """Convert seconds to milliseconds, but return None for None."""
    return int(float(v) * 1000.0) if v is not None else v


@classmethod
def fixup(cls, store):
    """Perform optional fixups on store."""
    return


def writeModelProperties(temp, model):
    """Radiative transfer properties."""
    temp.append('void input(inputPars *par, image *img){\n\n')
    temp.append('\tpar->radius = %.5f*AU;\n' % model.radius)
    temp.append('\tpar->minScale = %.5f*AU;\n' % model.minScale)
    temp.append('\tpar->pIntensity = %.d;\n' % model.pIntensity)
    temp.append('\tpar->sinkPoints = %.d;\n' % model.sinkPoints)
    temp.append('\tpar->sampling = %d;\n' % model.sampling)
    temp.append('\tpar->moldatfile[0] = "%s";\n' % model.moldatfile)
    if model.dust is not None:
        temp.append('\tpar->dust = "%s";\n' % model.dust)
    temp.append('\tpar->lte_only = %d;\n' % model.lte_only)
    if not model.lte_only:
        temp.append('\tpar->nSolveIters = 7;\n')
    temp.append('\tpar->blend = %d;\n' % model.blend)
    temp.append('\tpar->antialias = %d;\n' % model.antialias)
    temp.append('\tpar->traceRayAlgorithm = %d;\n' % model.traceRayAlgorithm)
    temp.append('\tpar->nThreads = %d;\n' % model.nThreads)
    if model.gridOutFile:
        temp.append('\tpar->gridOutFiles[4] = "%s.ds";\n' % model.name)
    temp.append('\n\n')
    return


def generic_find_fk_constraint_name(table, columns, referenced, insp):
    """Utility to find a foreign-key constraint name in alembic migrations"""
    for fk in insp.get_foreign_keys(table):
        if fk['referred_table'] == referenced and set(fk['referred_columns']
            ) == columns:
            return fk['name']


def _override(schema_name, schema, overrides):
    """Override default values in the schema if necessary."""
    if schema_name in overrides:
        defaults = overrides[schema_name]
        for key in defaults:
            if key in schema['properties']:
                schema['properties'][key]['default'] = defaults[key]
            else:
                schema['properties'][key] = dict(default=defaults[key])
    return schema


@classmethod
def parse(cls, api, json):
    """Parse a JSON object into a model instance."""
    raise NotImplementedError


def feet22m2(area_ft2):
    """Converts area value from feet squared to meters squared"""
    return area_ft2 / 10.7639


def lambd_NH3_COO2_CB89(T, P):
    """n-a: ammonia oxalate [CB89]."""
    lambd = 0.012
    valid = T == 298.15
    return lambd, valid


def fail_on_shallow(wd):
    """experimental, may change at any time"""
    if wd.is_shallow():
        raise ValueError(
            '%r is shallow, please correct with "git fetch --unshallow"' %
            wd.path)


@staticmethod
def addition(chromosome):
    """
		Calculates fitness score of a chromosome as sum of genes

		Parameters :
		-----------
		chromosome : list
					The chromosome whose fitness is to be calculated

		Returns :
		----------
		Fitness score

		"""
    return sum(chromosome)


def get_service_from_condition_key(condition_key):
    """Given a condition key, return the service prefix"""
    elements = condition_key.split(':', 2)
    return elements[0]


def add_vert(eptm, edge):
    """Adds a vertex in the middle of the edge,

    which is split as is its opposite(s)

    Parameters
    ----------
    eptm : a :class:`Epithelium` instance
    edge : int
    the index of one of the half-edges to split

    Returns
    -------
    new_vert : int
    the index to the new vertex
    new_edges : int or list of ints
    index to the new edge(s). For a sheet, returns
    a single index, for a 3D epithelium, returns
    the list of all the new parallel edges
    new_opp_edges : int or list of ints
    index to the new opposite edge(s). For a sheet, returns
    a single index, for a 3D epithelium, returns
    the list of all the new parallel edges

    In the simple case whith two half-edge, returns
    indices to the new edges, with the following convention:

    s    e    t
      ------>
    * <------ *
    oe

    s    e       ne   t
      ------   ----->
    * <----- * ------ *
        oe   nv   noe

    where "e" is the passed edge as argument, "s" its source "t" its
    target and "oe" its opposite. The returned edges are the ones
    between the new vertex and the input edge's original target.
    """
    srce, trgt = eptm.edge_df.loc[edge, ['srce', 'trgt']]
    opposites = eptm.edge_df[(eptm.edge_df['srce'] == trgt) & (eptm.edge_df
        ['trgt'] == srce)]
    parallels = eptm.edge_df[(eptm.edge_df['srce'] == srce) & (eptm.edge_df
        ['trgt'] == trgt)]
    new_vert = eptm.vert_df.loc[srce:srce]
    eptm.vert_df = eptm.vert_df.append(new_vert, ignore_index=True)
    new_vert = eptm.vert_df.index[-1]
    eptm.vert_df.loc[new_vert, eptm.coords] = eptm.vert_df.loc[[srce, trgt],
        eptm.coords].mean()
    new_edges = []
    for p, p_data in parallels.iterrows():
        eptm.edge_df.loc[p, 'trgt'] = new_vert
        eptm.edge_df = eptm.edge_df.append(p_data, ignore_index=True)
        new_edge = eptm.edge_df.index[-1]
        eptm.edge_df.loc[new_edge, 'srce'] = new_vert
        eptm.edge_df.loc[new_edge, 'trgt'] = trgt
        new_edges.append(new_edge)
    new_opp_edges = []
    for o, o_data in opposites.iterrows():
        eptm.edge_df.loc[o, 'srce'] = new_vert
        eptm.edge_df = eptm.edge_df.append(o_data, ignore_index=True)
        new_opp_edge = eptm.edge_df.index[-1]
        eptm.edge_df.loc[new_opp_edge, 'trgt'] = new_vert
        eptm.edge_df.loc[new_opp_edge, 'srce'] = trgt
        new_opp_edges.append(new_opp_edge)
    if len(new_edges) == 1:
        new_edges = new_edges[0]
    if len(new_opp_edges) == 1:
        new_opp_edges = new_opp_edges[0]
    elif len(new_opp_edges) == 0:
        new_opp_edges = None
    return new_vert, new_edges, new_opp_edges


@classmethod
def _get_origin(cls):
    """Return list of Model names for origin Reference"""
    return [cls.__name__, 'stock.inventory.line']


def shift_into_range(val, ctype):
    """Shift a numerical value into range for given integral ctype."""
    if ctype.signed:
        max_val = 1 << ctype.size * 8 - 1
        range = 2 * max_val
    else:
        max_val = 1 << ctype.size * 8
        range = max_val
    val = val % range
    if val >= max_val:
        val -= range
    return val


def normalize_polys(geometry):
    """Turn MultiPolygons into their constituent geometries, and Polygons into singleton lists.

    When we take the union of all the highlight lines, we may get 1 or n distinct polygons. This
    is used to treat both cases identically.
    """
    if geometry.geom_type == 'MultiPolygon':
        return geometry.geoms
    if geometry.geom_type == 'Polygon':
        return [geometry]
    raise ValueError(f'Unexpected geometry type: {geometry.geom_type}')


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 count_thresholds(stain, channel_lightness, thresh_channel,
    thresh_empty_default):
    """
    Counts thresholds. "stain" is a distribution map of stain, channel_lightness is a L channel from
    original image in HSL color space. The output are the thresholded images of stain-positive areas and
    empty areas.
    """
    thresh_stain = stain > thresh_channel
    thresh_empty = channel_lightness > thresh_empty_default
    return thresh_stain, thresh_empty


def scale_bounding_box(box, percent=0):
    """
	Scales up a bounding box in X percent

	:param box: Bounding box
	:type box: list
	:param percent: Percent to scale up
	:type percent: int
	"""
    x, y, w, h = box
    if percent:
        offset_x = 0.01 * percent * w
        offset_y = 0.01 * percent * h
        return [(x - offset_x, y - offset_y), (x + w + offset_x, y + h +
            offset_y)]
    else:
        return box


def validate_tabix(path):
    """
    Validate TABIX file

    Parameters
    ----------
    path : str
        File to check
    """
    raise NotImplementedError()


@classmethod
def passwordManager(cls):
    """
        Class method to get a reference to the password manager.

        @return reference to the password manager (PasswordManager)
        """
    if cls._passwordManager is None:
        from .Passwords.PasswordManager import PasswordManager
        cls._passwordManager = PasswordManager()
    return cls._passwordManager


def format_date(d):
    """
    Format a python date object into a value suitable for the API
    :param d: date timestamp
    :return:
    """
    return d.strftime('%d/%m/%Y')


def change_character(delta: int=1):
    """Change font character code

    :param delta: Delta, Number to increase or decrease character code with
    :type delta: int
    """
    pass


@staticmethod
def _frange(start, stop, step):
    """range for float (including stop)."""
    while start <= stop:
        yield start
        start += step


def getHost():
    """
        Returns L{IPv4Address} or L{IPv6Address}.
        """


def calculate_number_of_shuffles_required_under_default_random_function(
    node_number: int) ->int:
    """
        Python's random number function USES the Mersenne Twister algorithm, which has a period of 2**19937-1.If the total permutation of graph nodes is larger than the random function period, the card cannot be shuffled only once.
        The total permutation of a graph node is "factorial of the number of nodes".The number of binary digits of the total number of permutations can be calculated by Stirling's formula.

        >>> calculate_number_of_shuffles_required_under_default_random_function(1000)
        1
        >>> calculate_number_of_shuffles_required_under_default_random_function(10000)
        6
    """
    import math
    if node_number > 0:
        bit_length_of_permutation_number = math.ceil(math.log2(2 * math.pi *
            node_number) / 2 + math.log2(node_number / math.e) * node_number)
        shuffle_number = math.ceil(bit_length_of_permutation_number / 19937)
    else:
        shuffle_number = 0
    return shuffle_number


def _build_simple_row(padded_cells, rowfmt):
    """Format row according to DataRow format without padding."""
    begin, sep, end = rowfmt
    return (begin + sep.join(padded_cells) + end).rstrip()


def is_none_slice(index):
    """Is the slice `index` a slice(None, None, None)? True or False."""
    if index.start is not None:
        return False
    if index.stop is not None:
        return False
    if index.step is not None:
        return False
    return True


def surf_to_phi(surf, phi, x):
    """
    move the surf density back to the full phi density function
    """
    for ii in range(len(x)):
        for jj in range(len(x)):
            kk = len(x) - 1 - ii - jj
            phi[ii, jj, kk] = surf[ii, jj]
    return phi


def vertex_group_copy_to_linked():
    """Replace vertex groups of all users of the same geometry data by vertex groups of active object

    """
    pass


def get_default_num_parties_jouees():
    """
    The number of AI self-play games to find out which of two AI is the best one.
    """
    num_parties_jouees = 10
    return num_parties_jouees


def write_file(outname, report_txt):
    """Write a generated report to a given file.

    Parameters
    ----------
    outname : string
        The name of the file to write/create.
    report_txt : string
        This is the generated report as a string.

    Returns
    -------
    out : string
        The name of the file written.

    """
    with open(outname, 'wt') as report_fh:
        try:
            report_fh.write(report_txt)
        except UnicodeEncodeError:
            report_fh.write(report_txt.encode('utf-8'))
    return outname


def megamanicHackRequestClear():
    """Clears the request after multiple editing of objects."""


@classmethod
def search(cls, *operator, **criterion):
    """
        Object search with criterions given in arguments.
        Returns a :class:`sqlachemy.orm.query.Query` object.

        Filters can be chained.
        """
    query = cls.session.query(cls)
    joined_class = []
    clauses = []
    for operator in list(operator):
        clauses.append(operator(cls, joined_class))
    clauses.extend(cls.process_params(joined_class, **criterion))
    query = query.join(*joined_class)
    return query.filter(*clauses)


def bC_MgOH_Cl_HMW84(T, P):
    """c-a: magnesium-hydroxide chloride [HMW84]."""
    b0 = -0.1
    b1 = 1.658
    b2 = 0.0
    C0 = 0
    C1 = 0
    alph1 = 2
    alph2 = -9
    omega = -9
    valid = T == 298.15
    return b0, b1, b2, C0, C1, alph1, alph2, omega, valid


def adapt_proj(proj, params: dict, param_map: dict):
    """Changes the parametrization of a circuit.

    Parameters
    ----------
    proj
        ANNarchy projection instance.
    params
        Key-value pairs of the parameters that should be changed.
    param_map
        Map between the keys in params and the circuit variables.

    Returns
    -------
    Projection
        Updated projection instance.

    """
    for key in params.keys():
        val = params[key]
        for op, var in param_map[key]['var']:
            edges = param_map[key]['edges'] if 'edges' in param_map[key] else [
                ]
            for source, target, edge in edges:
                if (source in proj.name and target in proj.name and edge in
                    proj.name):
                    try:
                        proj.set({var: float(val)})
                    except TypeError:
                        proj.set({var: val})
                    except [ValueError, KeyError]:
                        pass
    return proj


def currency(conv, val, symbol=True, grouping=False, international=False):
    """Formats val according to the currency settings
    in the current locale."""
    digits = conv[international and 'int_frac_digits' or 'frac_digits']
    if digits == 127:
        raise ValueError(
            "Currency formatting is not possible using the 'C' locale.")
    s = format('%%.%if' % digits, abs(val), grouping, monetary=True)
    s = '<' + s + '>'
    if symbol:
        smb = conv[international and 'int_curr_symbol' or 'currency_symbol']
        precedes = conv[val < 0 and 'n_cs_precedes' or 'p_cs_precedes']
        separated = conv[val < 0 and 'n_sep_by_space' or 'p_sep_by_space']
        if precedes:
            s = smb + (separated and ' ' or '') + s
        else:
            s = s + (separated and ' ' or '') + smb
    sign_pos = conv[val < 0 and 'n_sign_posn' or 'p_sign_posn']
    sign = conv[val < 0 and 'negative_sign' or 'positive_sign']
    if sign_pos == 0:
        s = '(' + s + ')'
    elif sign_pos == 1:
        s = sign + s
    elif sign_pos == 2:
        s = s + sign
    elif sign_pos == 3:
        s = s.replace('<', sign)
    elif sign_pos == 4:
        s = s.replace('>', sign)
    else:
        s = sign + s
    return s.replace('<', '').replace('>', '')


@classmethod
def de_json(cls, json_string):
    """
        Returns an instance of this class from the given json dict or string.

        This function must be overridden by subclasses.
        :return: an instance of this class created from the given json dict or string.
        """
    raise NotImplementedError


def _isLeft(P0, P1, P2):
    """
    Test if point P2 is Left|On|Right of the line P0 to P1.
    returns: >0 for left, 0 for on, and <0 for right of the line.
    """
    return (P1.x - P0.x) * (P2.y - P0.y) - (P2.x - P0.x) * (P1.y - P0.y)


def check_domain_exists(conn, guestname, logger):
    """ check if the domain exists, may or may not be active """
    guest_names = []
    ids = conn.listDomainsID()
    for id in ids:
        obj = conn.lookupByID(id)
        guest_names.append(obj.name())
    guest_names += conn.listDefinedDomains()
    if guestname not in guest_names:
        logger.error("%s doesn't exist" % guestname)
        return False
    return True


def object2SpacedString(o):
    """Answers the object as string.

    >>> object2SpacedString(2)
    '2'
    >>> object2SpacedString('ABC')
    'ABC'
    >>> object2SpacedString(['ABC', 12])
    "['ABC', 12]"
    """
    return str(o)


def is_scalar(x):
    """
    >>> is_scalar(1)
    True
    >>> is_scalar(1.1)
    True
    >>> is_scalar([1, 2, 3])
    False
    >>> is_scalar((1, 2, 3))
    False
    >>> is_scalar({'a': 1})
    False
    >>> is_scalar({1, 2, 3})
    False
    >>> is_scalar('spam')
    True
    """
    try:
        len(x)
    except TypeError:
        return True
    else:
        return isinstance(x, (str, bytes))


def rule_move_down():
    """Move boid rule down in the list

    """
    pass


def isnan(x):
    """
    Return True if x is NaN where x can be of any type
    :param x: any object for which (in)equality can be checked
    """
    return x != x


def admin_update_user_attributes(UserPoolId=None, Username=None,
    UserAttributes=None):
    """
    Updates the specified user's attributes, including developer attributes, as an administrator. Works on any user.
    For custom attributes, you must prepend the custom: prefix to the attribute name.
    In addition to updating user attributes, this API can also be used to mark phone and email as verified.
    Requires developer credentials.
    See also: AWS API Documentation

    :example: response = client.admin_update_user_attributes(
        UserPoolId='string',
        Username='string',
        UserAttributes=[
            {
                'Name': 'string',
                'Value': 'string'
            },
        ]
    )

    :type UserPoolId: string
    :param UserPoolId: [REQUIRED]
            The user pool ID for the user pool where you want to update user attributes.

    :type Username: string
    :param Username: [REQUIRED]
            The user name of the user for whom you want to update user attributes.

    :type UserAttributes: list
    :param UserAttributes: [REQUIRED]
            An array of name-value pairs representing user attributes.
            For custom attributes, you must prepend the custom: prefix to the attribute name.
            (dict) --Specifies whether the attribute is standard or custom.
            Name (string) -- [REQUIRED]The name of the attribute.
            Value (string) --The value of the attribute.

    :rtype: dict
    :return: {}

    """
    pass


@classmethod
def from_dict(cls, dct):
    """Instantiate an agent using the specified dictionary.

        TAG: USER
        """
    return cls(**dct)


def getStartPhase(phases, V=None):
    """
    Find the key for the high-T phase.

    Parameters
    ----------
    phases : dict
        Output from :func:`traceMultiMin`.
    V : callable
        The potential V(x,T). Only necessary if there are
        multiple phases with the same Tmax.
    """
    startPhases = []
    startPhase = None
    Tmax = None
    assert len(phases) > 0
    for i in phases.keys():
        if phases[i].T[-1] == Tmax:
            startPhases.append(i)
        elif Tmax is None or phases[i].T[-1] > Tmax:
            startPhases = [i]
            Tmax = phases[i].T[-1]
    if len(startPhases) == 1 or V is None:
        startPhase = startPhases[0]
    else:
        Vmin = None
        for i in startPhases:
            V_ = V(phases[i].X[-1], phases[i].T[-1])
            if Vmin is None or V_ < Vmin:
                Vmin = V_
                startPhase = i
    assert startPhase in phases
    return startPhase


def get_required_field_value(data, field_name, pk, fetch_model):
    """Gets the value of a model field either from serializer data or from the database.
    "Required" means it's an issue if the field value is not present hence function will fail in such case.

    If there's no field in data, then model if fetched via function provided and looked up instead.
    Field can be missing in data in case of partial update (PATCH) or in case action allows that some other way.

    Parameters:

    - data #dict: The serializer's data to look up the field in first.
    - field_name #str: The name of the field to look up.
    - pk #object: The value of corresponding entity's PK to fetch model in case there's no field in data.
    - fetch_model #function: A function which is expected to return model instance if executed with its PK as argument.

    Returns: the value of the field if present else #None.
    """
    if field_name in data:
        return data.get(field_name)
    assert pk is not None, 'Update or partial update is assumed'
    field_value = getattr(fetch_model(pk), field_name)
    assert field_value is not None, 'Unexpectedly required field value is None'
    return field_value


def runscgi(func, addr=('localhost', 4000)):
    """Runs a WSGI function as an SCGI server."""
    import flup.server.scgi as flups
    return flups.WSGIServer(func, bindAddress=addr, debug=False).run()


@staticmethod
def create(class_name, from_event=None):
    """
        Creates a new event of the specified class, while preserving the values
        of the origin event (as much as possible).
        :param class_name: The name of the type of event to create
        :param from_event: The event from wich the common values will be copied
        """
    new_event = eval(class_name)()
    if from_event is not None:
        for key in new_event.__dict__.keys():
            if key in from_event.__dict__ and key != 'type':
                new_event.__dict__[key] = from_event.__dict__[key]
    return new_event


def remote(fn, name=None, types=None):
    """Decorator that adds a remote attribute to a function.

    fn -- function being decorated
    name -- aliased name of the function, used for remote proxies
    types -- a argument type specifier, can be used to ensure
             arguments are of the correct type
    """
    if not name:
        name = fn.__name__
    fn.remote = {'name': name, 'types': types}
    return fn


def _chain_from_iterable_of_lists(iterable):
    """
    Specialized implementation of itertools.chain.from_iterable.
    Each item in *iterable* should be a list.  This function is
    careful not to keep references to yielded objects.
    """
    for element in iterable:
        element.reverse()
        while element:
            yield element.pop()


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 get_roles_for_permission(permission):
    """Return the role grants for the permission

        The role grants are an iterable of role, setting tuples, where
        setting is either Allow or Deny.
        """


def fmt_color(text: str, color: str) ->str:
    """Format a string in a certain color (`<span>`).

    Args:
      text: The text to format.
      color: Any valid CSS color.

    Returns:
        A `<span>` that contains the colored text.
    """
    return f'{text}'


@staticmethod
def _sink_hive():
    """method to add hive as sink."""
    pass


def fib(n):
    """Fibonacci example function

    Args:
      n (int): integer

    Returns:
      int: n-th Fibonacci number
    """
    assert n > 0
    a, b = 1, 1
    for i in range(n - 1):
        a, b = b, a + b
    return a


@classmethod
def from_charge_id(cls, charge_id):
    """
        Class method to create and an instance of `Charge` and fill it
        from the Strike server.

        Args:
            - charge_id (str): The id of a charge on Strike's server.

        Returns:
            - An instance of `Charge`, filled from the attributes of
              the charge with the given `charge_id`.

        """
    charge = cls(0, cls.CURRENCY_BTC, create=False)
    charge.id = charge_id
    charge.update()
    return charge


def p_songwriter(t):
    """songwriter : SONGWRITER STRING"""
    t[0] = 'songwriter', t[2]


def stop_format_server(process):
    """
    Stops a given format server.
    """
    process.kill()


def convert_annotations(img_tag, names):
    """Find each ad in an img_tag and convert them to yolo-format annotations.

    Parameters
    ----------
    img_tag : xml.etree.ElementTree.Element
        CVAT-style tag for an image
    names : list
        An ordered list of labels, which describe boxes in images

    Returns
    -------
    (yolo_annotations, names) : tuple(str, list)
        yolo_annotations is a string containing all yolo-style annotations
            for the given img_tag, separated by '
' newline characters.
        names is a list containing all passed-in names, plus any new labels.

    """
    width = int(img_tag.get('width'))
    height = int(img_tag.get('height'))
    yolo_annotations = []
    for box in img_tag.findall('box'):
        label = box.get('label')
        xmin = float(box.get('xtl'))
        ymin = float(box.get('ytl'))
        xmax = float(box.get('xbr'))
        ymax = float(box.get('ybr'))
        if label not in names:
            names.append(label)
        label_id = names.index(label)
        yolo_x = (xmin + (xmax - xmin) / 2) / width
        yolo_y = (ymin + (ymax - ymin) / 2) / height
        yolo_w = (xmax - xmin) / width
        yolo_h = (ymax - ymin) / height
        yolo_annotations.append(
            f'{label_id} {yolo_x:.6f} {yolo_y:.6f} {yolo_w:.6f} {yolo_h:.6f}')
    return '\n'.join(yolo_annotations), names


def JD(year, month, day):
    """
----------------------------------------------------------------------
Purpose:   Calculate Julian day number (Julian date)
Input:     year (nnnn), month (nn), day (nn.n),
Returns:   Julian day number 'jd'
Reference: Meeus, Astronomical formula for Calculators, 2nd ed, 1982
Notes:     Months start at 1. Days start at 1. The Julian day begins at
           Greenwich mean noon, i.e. at 12h. So Jan 1, 1984 at 0h is
           entered as JD(1984,1,1) and Jan 1, 1984 at 12h is entered
           as JD(1984,1,1.5)
           There is a jump at JD(1582,10,15) caused by the change of
           calendars. So for dates after 1582-10-15 one enters a date
           from the Julian calendar and before this date you enter a
           date from the Julian calendar.
Examples:  Julian date of JD reference:
           print celestial.JD(-4712,1,1.5) ==> 0.0
           The first day of 1 B.C.:
           print celestial.JD(0,1,1) ==> 1721057.5
           Last day before Gregorian reform:
           print celestial.JD(1582,10,4) ==> 2299159.5
           First day of Gregorian reform:
           print celestial.JD(1582,10,15) ==> 2299170.5
           Half a day later:
           print celestial.JD(1582,10,15.5) ==> 2299161.0
           Unix reference:
           print celestial.JD(1970,1,1) ==> 2440587.5
----------------------------------------------------------------------
   """
    if month > 2:
        y = year
        m = month
    elif month == 1 or month == 2:
        y = year - 1
        m = month + 12
    calday = year + month / 100.0 + day / 10000.0
    if calday > 1582.1015:
        A = int(y / 100.0)
        B = 2 - A + int(A / 4.0)
    else:
        B = 0
    if calday > 0.0229:
        jd = int(365.25 * y) + int(30.6001 * (m + 1)) + day + 1720994.5 + B
    else:
        jd = int(365.25 * y - 0.75) + int(30.6001 * (m + 1)
            ) + day + 1720994.5 + B
    return jd


@classmethod
def get_name(cls):
    """Returns backend specific name for the driver."""
    return cls.__name__


def despine(chart):
    """Despine altair chart.
    """
    chart = chart.configure_axis(ticks=False, grid=False, domain=False,
        labels=False)
    return chart


@staticmethod
def cpp_TTreeReaderValue(datatype, var, TTreeReader, branch_name):
    """
        C++ ``TTreeReaderValue`` initializer snippet.
        """
    return 'TTreeReaderValue<{0}> {1}({2}, "{3}");\n'.format(datatype, var,
        TTreeReader, branch_name)


def get(isamAppliance, check_mode=False, force=False):
    """
    Retrieve available updates
    """
    return isamAppliance.invoke_get('Retrieving available updates',
        '/updates/available.json')


def glDeleteLists(list: int, range: int):
    """Delete a contiguous group of display lists

    :param list: Specifies the integer name of the first display list to delete
    :type list: int
    :param range: Specifies the number of display lists to delete
    :type range: int
    """
    pass


def _level_and_children(elements):
    """Split a list of elements into elements on the current level (i.e.
    that share the same depth as the first element) and segmented children
    (children of each of those elements)"""
    if not elements:
        return [], []
    depth = elements[0][2]
    level = []
    grouped_children = []
    children = []
    for el in elements:
        if el[2] == depth:
            level.append(el)
            if children:
                grouped_children.append(children)
            children = []
        else:
            children.append(el)
    if children:
        grouped_children.append(children)
    return level, grouped_children


def stale(stale_error):
    """
        Produce a new object that is the stale version of this object
        and return it.

        :return: Another `IStaleAware`.
        """


def _check_int(name, value):
    """Check for integer value."""
    if value.isdigit():
        return int(value)
    raise ValueError('{name}: not an integer value: {value}'.format(name=
        name, value=value))


@staticmethod
def has_provider(handler):
    """
        Find out if any provider decorator is used in a given handler.

        :param handler: supercell request handler
        :return: true if any provider is used, else false
        """
    return hasattr(handler, '_PROD_CONTENT_TYPES')


def parse_and_update(key, new_data, config_separator):
    """
    Parse and update key
    :param key: the key to update
    :param new_data: the new data
    :param config_separator: the config separator
    :return: the key updated
    """
    parsed_data_array = key.split(config_separator)
    new_config = {}
    current_dict = new_config
    for i, _ in enumerate(parsed_data_array):
        if i > 0:
            if i == len(parsed_data_array) - 1:
                current_dict[parsed_data_array[i]] = new_data
            else:
                current_dict[parsed_data_array[i]] = {}
        else:
            new_config[parsed_data_array[i]] = {}
        current_dict = current_dict[parsed_data_array[i]]
    return new_config


def cell_remover(contents, key):
    """This function will remove the cells that contain a specific key
    and ensure that the last cell has the correct comma at the end."""
    lines = contents.split('\n')
    line_list = []
    for index in range(len(lines)):
        if key in lines[index]:
            line_list.append(index)
    for element in line_list:
        old_cell_text = ''
        element_current = element
        for lines_before in reversed(lines[:element]):
            element_current += -1
            a = 0
            old_cell_text = lines_before + '\n' + old_cell_text
            if lines_before == '  {':
                a = 1
                for lines_after in lines[element:]:
                    if '  },' in lines_after:
                        old_cell_text = old_cell_text + '  },\n'
                        break
                    elif '  }' in lines_after:
                        old_cell_text = old_cell_text + '  }\n'
                        break
                    else:
                        old_cell_text = old_cell_text + lines_after + '\n'
            if a == 1:
                break
        contents = contents.replace(old_cell_text, '')
    lines = contents.split('\n')
    kernel_spec_index = None
    for index in range(len(lines)):
        if '"kernelspec": {' in lines[index]:
            kernel_spec_index = index
            break
    kernel_spec_text = ''
    if kernel_spec_index is not None:
        for line in reversed(lines[:kernel_spec_index]):
            if line == '  }':
                break
            elif line == '  },':
                contents = contents.replace('  },\n' + kernel_spec_text,
                    '  }\n' + kernel_spec_text)
                break
            else:
                kernel_spec_text = line + '\n' + kernel_spec_text
    return contents


def set_gpu(gpu_mon, args):
    """
    Sets the GPU visibility based on the passed arguments. Takes an already
    initialized GPUMonitor object. Sets GPUs according to args.force_GPU, if
    specified, otherwise sets first args.num_GPUs free GPUs on the system.

    Stops the GPUMonitor process once GPUs have been set
    If gpu_mon is None, this function does nothing

    Args:
        gpu_mon: An initialized GPUMonitor object or None
        args: argparse arguments

    Returns: The number of GPUs that was actually set (different from
    args.num_GPUs if args.force_GPU is set to more than 1 GPU)
    """
    num_GPUs = args.num_GPUs
    if gpu_mon is not None:
        if not args.force_GPU:
            gpu_mon.await_and_set_free_GPU(N=num_GPUs, sleep_seconds=120)
        else:
            gpu_mon.set_GPUs = args.force_GPU
            num_GPUs = len(args.force_GPU.split(','))
        gpu_mon.stop()
    return num_GPUs


def merge_dicts(dict1, *dicts):
    """returns a dictionary consisting of all the dicts, overriding key values in the order they're passed in"""
    merged = dict1.copy()
    for d in dicts:
        merged.update(d)
    return merged


def ar_2(provider):
    """Send A-RELEASE indication primitive."""
    provider.to_service_user.put(provider.primitive)
    return 'Sta8'


def segmentation_figure(label, cat, segfile):
    """
    Make a figure showing a cutout of the segmentation file
    """
    import matplotlib.pyplot as plt
    import numpy as np
    import astropy.io.fits as pyfits
    import astropy.wcs as pywcs
    from grizli import utils
    plt.ioff()
    seg = pyfits.open(segfile)
    seg_data = seg[0].data
    seg_wcs = pywcs.WCS(seg[0].header)
    np.random.seed(hash(label.split('_')[0]) % 10 ** 8)
    rnd_ids = np.append([0], np.argsort(np.random.rand(len(cat))) + 1)
    th = pyfits.open('{0}.thumb.fits'.format(label), mode='update')
    th_wcs = pywcs.WCS(th[0].header)
    blot_seg = utils.blot_nearest_exact(seg_data, seg_wcs, th_wcs, stepsize
        =-1, scale_by_pixel_area=False)
    rnd_seg = rnd_ids[np.cast[int](blot_seg)] * 1.0
    th_ids = np.unique(blot_seg)
    sh = th[0].data.shape
    yp, xp = np.indices(sh)
    thumb_height = 2.0
    fig = plt.figure(figsize=[thumb_height * sh[1] / sh[0], thumb_height])
    ax = fig.add_subplot(111)
    rnd_seg[rnd_seg == 0] = np.nan
    ax.imshow(rnd_seg, aspect='equal', cmap='terrain_r', vmin=-0.05 * len(
        cat), vmax=1.05 * len(cat))
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ix = utils.column_values_in_list(cat['number'], th_ids)
    xc, yc = th_wcs.all_world2pix(cat['ra'][ix], cat['dec'][ix], 0)
    xc = np.clip(xc, 0.09 * sh[1], 0.91 * sh[1])
    yc = np.clip(yc, 0.08 * sh[0], 0.92 * sh[0])
    for th_id, x_i, y_i in zip(cat['number'][ix], xc, yc):
        if th_id == 0:
            continue
        ax.text(x_i, y_i, '{0:.0f}'.format(th_id), ha='center', va='center',
            fontsize=8, color='w')
        ax.text(x_i, y_i, '{0:.0f}'.format(th_id), ha='center', va='center',
            fontsize=8, color='k', alpha=0.95)
    ax.set_xlim(0, sh[1] - 1)
    ax.set_ylim(0, sh[0] - 1)
    ax.set_axis_off()
    fig.tight_layout(pad=0.01)
    fig.savefig('{0}.seg.png'.format(label))
    plt.close(fig)
    seg_hdu = pyfits.ImageHDU(data=np.cast[int](blot_seg), name='SEG')
    if 'SEG' in th:
        th.pop('SEG')
    th.append(seg_hdu)
    th.writeto('{0}.thumb.fits'.format(label), overwrite=True,
        output_verify='fix')
    th.close()


def pop(cpu, size):
    """
        Gets a value from the stack.

        :rtype: int
        :param size: the size of the value to consume from the stack.
        :return: the value from the stack.
        """
    assert size in (16, cpu.address_bit_size)
    base, _, _ = cpu.get_descriptor(cpu.SS)
    address = cpu.STACK + base
    value = cpu.read_int(address, size)
    cpu.STACK = cpu.STACK + size // 8
    return value


def linear(F_CH4, ratio=0.15):
    """Calculates radiative forcing from oxidation of methane to H2O.

    Stratospheric water vapour forcing follows a practically linear
    relationship with the CH4 radiative forcing in MAGICC and AR5.
    """
    F_H2O = ratio * F_CH4
    return F_H2O


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 add_header(name, value):
    """Similar to set_header() except that, if there is already a response
        header by ``name`` present, ``value`` will be appended to existing
        value using ',' seperator. Returns the new value for header name as
        byte-string.

        ``name``,
            byte-string of header field name, in lower case.

        ``value``,
            Any type which can be converted to string.
        """


def _retrieve_Moster_default_dict():
    """
    Dictionary of default values of all model parameters set to the
    column 2 values in Table 2 of Moster et al. (2010)

    Returns
    --------
    d : `dict`
        Dictionary containing default parameters for the Stellar-Halo
        Mass relation of Moster et al. (2010)

    Notes
    ----------
    All calculations are done internally using the same h=0.7 units as
    in Moster et al. (2010), ['arXiv:0903.4682'] so the parameter values
    here are the same as in Table 2, even though the `mean_log_halo_mass`
    and `mean_stellar_mass` methods use accept and return arguments in
    h=1 units.
    """
    d = {'smhm_m1': 11.899, 'smhm_m_M_0': 0.02817, 'smhm_beta': 1.068,
        'smhm_gamma': 0.611}
    return d


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

        :param LogoutType val:
        :rtype: EventType
        """
    return cls('logout', val)


def fill(on_back: bool=False):
    """Fill with color the shape formed by strokes

    :param on_back: Draw On Back, Send new stroke to Back
    :type on_back: bool
    """
    pass


def Table(description=''):
    """
    Adds numbered Table HTML anchors. Link to them in MarkDown using: [to Table 1](#Table-1)
    """
    return object('Table', description)


@classmethod
def deserialize(cls, doc):
    """Take the deserializer to validate the document."""
    if cls.__objective__ is not None:
        return cls.__objective__.deserialize(doc)
    return doc.copy()


def intersection(c1, c2):
    """Compute the intersection of c1 and c2.

        If c1 is None, then c2 is returned, otherwise, if c2 is None,
        then c1 is returned.

        The output is a Set containing matching keys from the input
        collections.
        """


@classmethod
def _from_et(cls, root, parent):
    """
        Create a :class:`Moduleinst` from a :code:`moduleinst` element.

        :param root: The element tree tree to parse.
        :param parent: :class:`EagleFilePart` that should hold the resulting :class:`EagleFilePart`
        :rtype: :class:`Moduleinst`
        """
    n = cls()
    n._init_from_et(root, parent)
    return n


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

        :param MissingDetails val:
        :rtype: EventDetails
        """
    return cls('missing_details', val)


def fromEnvironment(cls):
    """
        Get as many of the platform-specific error translation objects as
        possible and return an instance of C{cls} created with them.
        """
    try:
        from ctypes import WinError
    except ImportError:
        WinError = None
    try:
        from win32api import FormatMessage
    except ImportError:
        FormatMessage = None
    try:
        from socket import errorTab
    except ImportError:
        errorTab = None
    return cls(WinError, FormatMessage, errorTab)


def get_attribute(obj, value):
    """
    Normally the result of list_items for listviews are a set of model objects.
    But when you want a GROUP_BY query (with 'values' method), than
    the result will be a dict. This method will help you find an item for
    either objects or dictionaries.
    """
    if type(obj) == dict:
        return dict.get(obj, value)
    else:
        return getattr(obj, value)


@classmethod
def get_qrcode_url(cls, session, uuid):
    """Get authorize qrcode url."""
    return '{schema}://{endpoint}{url}/{uuid}'.format(schema=cls.schema,
        endpoint=cls.get_login_endpoint(session), url=cls.qrcode_url, uuid=uuid
        )


def escapeMarkdown(text):
    """
    Escapes the * and _ Markdown meta-characters by \\* and \\_.

    text (string):
    The text to escape.

    returns (string):
    The text with * and _ replaced with \\* and \\_, respectively.
    """
    escapedText = ''
    escapeSet = set(['*', '_'])
    for c in text:
        if c in escapeSet:
            escapedText += '\\'
        escapedText += c
    return escapedText


@staticmethod
def reverse_velocities(system):
    """Reverse the velocities in the phase points."""
    system.particles.reverse_velocities()


def _makeVaryingMap(vertVaryings, fragVaryings):
    """Generates texture unit indices for all varying attributes. """
    indices = range(len(vertVaryings))
    varyingMap = dict(zip(vertVaryings, indices))
    return varyingMap


def is_success(code):
    """Checks if response code is successful."""
    return 200 <= code <= 299


def parse_string(value):
    """
    parses string for python keywords or numeric value

    Parameters
    ----------
    value : str or float
        the value to be parsed (true, false, none, null, na, or float)

    Returns
    -------
    boolean, None, float, original value
    """
    if value is None:
        return None
    value = value.strip()
    if value.lower() == 'true':
        return True
    if value.lower() == 'false':
        return False
    if value.lower() in ['none', 'null', 'na']:
        return None
    try:
        if '.' in value:
            v = float(value)
        else:
            v = int(value)
        return v
    except ValueError:
        return value


@classmethod
def get_declared_fields(mcs, klass, cls_fields, inherited_fields, dict_cls):
    """Returns a dictionary of field_name => `Field` pairs declard on the class.
        This is exposed mainly so that plugins can add additional fields, e.g. fields
        computed from class Meta options.

        :param type klass: The class object.
        :param list cls_fields: The fields declared on the class, including those added
            by the ``include`` class Meta option.
        :param list inherited_fields: Inherited fields.
        :param type dict_class: Either `dict` or `OrderedDict`, depending on the whether
            the user specified `ordered=True`.
        """
    return dict_cls(inherited_fields + cls_fields)


def overridemethod(func):
    """Sugar. Does nothing, just used to label override methods."""
    return func


def index(obj, attributes=None):
    """ queue an index operation for the given object and attributes """


def length_at_least(n):
    """Returns a function that checks whether a list has length at least ``n``.

    Args:
        n: The length to check against.

    Returns:
        A function that takes in a list ``l`` and returns ``True`` if
        ``len(l)`` is greater than or equal to ``n``.
    """
    return lambda l: len(l) >= n


def kelvin_to_celsius(temp):
    """
    Convert Kelvin to Celsius
    :param temp: temperature in Kelvin
    :type temp: float
    :return: temperature in Celsius
    :rtype: int
    """
    return int(temp - 273.15)


def to_cfradial2(volume, filename, timestep=None):
    """ Save XRadVol/XRadVolume to CfRadial2.0 compliant file.

    Parameters
    ----------
    volume : XRadVol/XRadVolume object
    filename : str
        output filename
    timestep : int
        timestep of wanted volume
    """
    volume.root.load()
    root = volume.root.copy(deep=True)
    root.attrs['Conventions'] = 'Cf/Radial'
    root.attrs['version'] = '2.0'
    root.to_netcdf(filename, mode='w', group='/')
    for idx, key in enumerate(root.sweep_group_name.values):
        try:
            swp = volume[key]
        except TypeError:
            swp = volume[idx][timestep].data
        swp.load()
        dims = list(swp.dims)
        dims.remove('range')
        dim0 = dims[0]
        try:
            swp = swp.swap_dims({dim0: 'time'})
        except ValueError:
            swp = swp.drop_vars('time').rename({'rtime': 'time'})
            swp = swp.swap_dims({dim0: 'time'})
        swp.drop_vars(['x', 'y', 'z', 'gr', 'rays', 'bins'], errors='ignore')
        swp.to_netcdf(filename, mode='a', group=key)


def delete_error(err_id):
    """ delete error from storage
        """


def getCategories(blogid, username, password):
    """Returns a struct containing description, htmlUrl and rssUrl.
        """


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


@staticmethod
def p_if_data_characteristic(p):
    """if_data_characteristic : if_data_memory_layout"""


def bin_mapping(value_to_classify, nr_of_classes, class_bins,
    placeholder_zero_color):
    """Maps values to a bin.
    The mapped values must start at 0 and end at 1.

    Arguments
    ---------
    value_to_classify : float
        Value to classify
    class_bins : list
        Bins to use for classification

    Returns
    -------
    classified value
    """
    round_digits = 4
    value_to_classify = round(value_to_classify, round_digits)
    if value_to_classify == 0 or value_to_classify == -0:
        """for idx, bound in enumerate(class_bins):
            if value_to_classify == bound:
                return idx / (len(class_bins) - 1.0)"""
        return placeholder_zero_color
    for idx, bound in enumerate(class_bins):
        if value_to_classify < bound:
            return idx / (nr_of_classes - 1.0)