@classmethod
def database_close(cls):
    """
        Close the database connection.

        Closing already closed database
        is not a problem, so continue on.
        """
    if not cls._meta.database.is_closed():
        cls._meta.database.close()


def isolated_connection():
    """
        Context manager that opens a new, distinct connection and
        returns its cursor.

        No matter what happens in the ``with`` block, the connection will be
        dropped afterwards.
        """


def safe_attr(element, attr):
    """Return attribute value as string."""
    value = getattr(element, attr)
    return value if value is not None else ''


def instruction_list_to_easm(instruction_list: list) ->str:
    """Convert a list of instructions into an easm op code string.

    :param instruction_list:
    :return:
    """
    result = ''
    for instruction in instruction_list:
        result += '{} {}'.format(instruction['address'], instruction['opcode'])
        if 'argument' in instruction:
            result += ' ' + instruction['argument']
        result += '\n'
    return result


@staticmethod
def retrieve_node(node_uid):
    """
        通过node_uid 获取node及该节点类型
        """
    from oneid_meta.models import Dept, Group
    if node_uid.startswith(Dept.NODE_PREFIX):
        uid = node_uid.replace(Dept.NODE_PREFIX, '', 1)
        return Dept.valid_objects.filter(uid=uid).first(), 'dept'
    if node_uid.startswith(Group.NODE_PREFIX):
        uid = node_uid.replace(Group.NODE_PREFIX, '', 1)
        return Group.valid_objects.filter(uid=uid).first(), 'group'
    return None, ''


def _SUBS_extended_register(cpu, res_op, reg_op1, reg_op2):
    """
        SUBS (extended register).

        :param res_op: destination register.
        :param reg_op1: source register.
        :param reg_op2: source register.
        """
    cpu._adds_subs_extended_register(res_op, reg_op1, reg_op2, mnem='subs')


def __init__(request):
    """The ``IAuthorizationPlugin`` adapts the ``request``.
        """


def _module_name(*components):
    """Assemble a fully-qualified module name from its components."""
    return '.'.join(components)


def edges(G, nbunch=None):
    """Returns an edge view of edges incident to nodes in nbunch.

    Return all edges if nbunch is unspecified or nbunch=None.

    For digraphs, edges=out_edges
    """
    return G.edges(nbunch)


def _num_ln(keys, num):
    """Arranging buttons"""
    if type(keys[0]) is list:
        keys = keys[0]
    len_k = len(keys)
    num_of_lines = len_k // num
    lst, curr = [], 0
    for d in range(num_of_lines):
        temp_lst = []
        for e in range(curr, num + curr):
            temp_lst.append(keys[e])
        lst.append(temp_lst)
        curr += num
    lst.append(keys[len_k - len_k % num:])
    return lst


def makeCoefNameTerm(s, coef, name):
    """ generate a string representation of the (coef, name) pair with an
        appropriate sign connecting the term to preceding terms

    Keyword arguments:

    s    -- string containing the preceding terms (may be empty)
    coef -- coefficient (positive or negative floating-point value)
    name -- name string to be displayed next to coefficient
    """
    if coef == 0.0:
        return ''
    absCoef = abs(coef)
    if absCoef == 1.0:
        absCoefTerm = '%s' % name
    else:
        absCoefTerm = '%r %s' % (absCoef, name)
    if s == '':
        if coef < 0:
            sign = '-'
        else:
            sign = ''
    elif coef < 0:
        sign = ' - '
    else:
        sign = ' + '
    return sign + absCoefTerm


@classmethod
def add_hook(cls, hook_type, hook_func):
    """Add a new hook of specified type.

        :param cls: class that registers hooks
        :param hook_type: hook type, e.g., '__pre_parse_args__'
        :param hook_func: hook function
        """
    if hook_type not in cls._hooks_map:
        cls._hooks_map[hook_type] = []
    cls._hooks_map[hook_type].append(hook_func)


def remove_ngram(words, ngram):
    """
    Return list with all occurrences of n-gram removed.

    Examples:
        >>> remove_ngram(['foo', 'bar', 'ham', 'spam'], ('bar', 'ham'))
        ['foo', 'spam']
    """
    n = len(ngram)
    result = []
    n_iter = len(words) - n
    i = 0
    while i < n_iter:
        for j, word in enumerate(ngram):
            if word != words[i + j]:
                result.append(words[i + j])
                result.append(words[i])
                break
        else:
            i += n
    result.extend(words[i:])
    return result


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

        :param NoteAclInviteOnlyType val:
        :rtype: EventType
        """
    return cls('note_acl_invite_only', val)


def add_name_combiner(combiner, name):
    """ adding a node's name to each field from the combiner"""
    combiner_changed = []
    for comb in combiner:
        if '.' not in comb:
            combiner_changed.append('{}.{}'.format(name, comb))
        else:
            combiner_changed.append(comb)
    return combiner_changed


def add_info_nested(taxon, info, ds_name):
    """Passed to combine_datasets as the add_info parameter, to nest information
    by source dataset.
    """
    taxon.info[ds_name] = info


def example_simplez2(s):
    """Example of simulation of the program SIMPLEZ2"""
    s.add_var('cont', 46)
    s.add_var('pen', 47)
    s.add_var('ult', 48)
    s.add_var('sig', 49)
    s.add_var('SUM', 50)


def _nearest_overlapping_point(src, point):
    """Find the interval with the closest start point to a given point.

    :param src: IntervalTree instance.
    :param point: query point.

    :returns: Interval instance of interval with closest start.

    """
    items = src.at(point)
    if len(items) == 0:
        return None
    items = sorted(items, key=lambda x: x.end - x.begin, reverse=True)
    items.sort(key=lambda x: abs(x.begin - point))
    return items[0]


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


def combine(clusters):
    """Make new collocation analyses from data in clusters"""
    colls = []
    collocates = clusters[0].collocates
    for c in clusters[1:]:
        collocates = collocates.join(c.collocates, rsuffix='-' + str(c.
            period[0]))
    return collocates


def get_chart_table_args(stats_table):
    """Creates gviz DataTable object from a TensorFlow stats table.

  Args:
    stats_table: A tf_stats_pb2.TfStatsTable.

  Returns:
    Returns a gviz_api.DataTable
  """
    table_description = [('rank', 'number', 'Rank'), ('host_or_device',
        'string', 'Host/device'), ('type', 'string', 'Type'), ('operation',
        'string', 'Operation'), ('occurrences', 'number', '#Occurrences'),
        ('total_time', 'number', 'Total time (us)'), ('avg_time', 'number',
        'Avg. time (us)'), ('total_self_time', 'number',
        'Total self-time (us)'), ('avg_self_time', 'number',
        'Avg. self-time (us)'), ('device_total_self_time_percent', 'number',
        'Total self-time on Device (%)'), (
        'device_cumulative_total_self_time_percent', 'number',
        'Cumulative total-self time on Device (%)'), (
        'host_total_self_time_percent', 'number',
        'Total self-time on Host (%)'), (
        'Host_cumulative_total_self_time_percent', 'number',
        'Cumulative total-self time on Host (%)')]
    data = []
    for record in stats_table.tf_stats_record:
        row = [record.rank, record.host_or_device, record.op_type, record.
            op_name, record.occurrences, record.total_time_in_us, record.
            avg_time_in_us, record.total_self_time_in_us, record.
            avg_self_time_in_us, record.device_total_self_time_as_fraction,
            record.device_cumulative_total_self_time_as_fraction, record.
            host_total_self_time_as_fraction, record.
            host_cumulative_total_self_time_as_fraction]
        data.append(row)
    return table_description, data, []


def unpack_singleton(x):
    """Gets the first element if the iterable has only one value.

    Otherwise return the iterable.

    # Argument:
        x: A list or tuple.

    # Returns:
        The same iterable or the first element.
    """
    if len(x) == 1:
        return x[0]
    return x


def deny_permission_to_role(permission_id, role_id):
    """Deny the permission to the role
        """


def is_private(code):
    """
    Checking code, that is private code.

    :param code: integer value.
    """
    return 4000 <= code <= 4999


@staticmethod
def extract(s, **params):
    """Extract the given property with given parameters from an Atoms
        object.

        | Args:
        |   s (ase.Atoms): the structure from which to extract the property
        |   params: named arguments specific to this type of property
        |

        | Returns:
        |   property: the value of the property for the given structure and
        |             parameters
        |

        """
    pass


@classmethod
def fromFile(cls, memoryMapping, aFile):
    """
            aFile must be able to read().
        """
    return cls(aFile, memoryMapping.start, memoryMapping.end, memoryMapping
        .permissions, memoryMapping.offset, memoryMapping.major_device,
        memoryMapping.minor_device, memoryMapping.inode, memoryMapping.pathname
        )


@staticmethod
def md_escape(text):
    """
        Escape potential Markdown syntax in a string.
        This is meant to be passed to the templates.
        """
    specialchars = '\\`*_{}[]()#+-.!'
    s = ''
    for c in text:
        if c in specialchars:
            s += '\\'
        s += c
    return s


def mixed_newlines(filename):
    """Returns ``True`` if ``filename`` has mixed newlines."""
    f = open(filename, 'U')
    f.read()
    ret = isinstance(f.newlines, tuple)
    f.close()
    return ret


def make_clust(net, dist_type='cosine', run_clustering=True, dendro=True,
    requested_views=['pct_row_sum', 'N_row_sum'], linkage_type='average',
    sim_mat=False, filter_sim=0.1, calc_cat_pval=False, sim_mat_views=[
    'N_row_sum'], run_enrichr=None, enrichrgram=None):
    """
  This will calculate multiple views of a clustergram by filtering the
  data and clustering after each filtering. This filtering will keep the top
  N rows based on some quantity (sum, num-non-zero, etc).
  """
    from copy import deepcopy
    import scipy
    from . import calc_clust, run_filter, make_views, make_sim_mat, cat_pval
    from . import enrichr_functions as enr_fun
    df = net.dat_to_df()
    threshold = 0.0001
    df = run_filter.df_filter_row_sum(df, threshold)
    df = run_filter.df_filter_col_sum(df, threshold)
    define_cat_colors = False
    if run_enrichr is not None:
        df = enr_fun.add_enrichr_cats(df, 'row', run_enrichr)
        define_cat_colors = True
    net.df_to_dat(df, define_cat_colors=True)
    inst_dm = calc_clust.cluster_row_and_col(net, dist_type=dist_type,
        linkage_type=linkage_type, run_clustering=run_clustering, dendro=
        dendro, ignore_cat=False, calc_cat_pval=calc_cat_pval)
    all_views = []
    send_df = deepcopy(df)
    if 'N_row_sum' in requested_views:
        all_views = make_views.N_rows(net, send_df, all_views, dist_type=
            dist_type, rank_type='sum')
    if 'N_row_var' in requested_views:
        all_views = make_views.N_rows(net, send_df, all_views, dist_type=
            dist_type, rank_type='var')
    if 'pct_row_sum' in requested_views:
        all_views = make_views.pct_rows(net, send_df, all_views, dist_type=
            dist_type, rank_type='sum')
    if 'pct_row_var' in requested_views:
        all_views = make_views.pct_rows(net, send_df, all_views, dist_type=
            dist_type, rank_type='var')
    which_sim = []
    if sim_mat == True:
        which_sim = ['row', 'col']
    elif sim_mat == 'row':
        which_sim = ['row']
    elif sim_mat == 'col':
        which_sim = ['col']
    if sim_mat is not False:
        sim_net = make_sim_mat.main(net, inst_dm, which_sim, filter_sim,
            sim_mat_views)
        net.sim = {}
        for inst_rc in which_sim:
            net.sim[inst_rc] = sim_net[inst_rc].viz
            if inst_rc == 'row':
                other_rc = 'col'
            elif inst_rc == 'col':
                other_rc = 'row'
            net.sim[inst_rc]['cat_colors'][inst_rc] = net.viz['cat_colors'][
                inst_rc]
            net.sim[inst_rc]['cat_colors'][other_rc] = net.viz['cat_colors'][
                inst_rc]
    else:
        net.sim = {}
    net.viz['views'] = all_views
    if enrichrgram != None:
        net.viz['enrichrgram'] = enrichrgram
    if 'enrichrgram_lib' in net.dat:
        net.viz['enrichrgram'] = True
        net.viz['enrichrgram_lib'] = net.dat['enrichrgram_lib']
    if 'row_cat_bars' in net.dat:
        net.viz['row_cat_bars'] = net.dat['row_cat_bars']


def set_debug(debugging):
    """Sets the package testing mode.
    """
    global debug
    debug = debugging


def is_default(entity, prop, name):
    """bool: True if the value of a property is equal to its default."""
    return getattr(entity, name) == prop.default


def _add_doc(func, doc):
    """Add documentation to a function."""
    func.__doc__ = doc


def get_user_wallet_name(username):
    """
    Determine wallet name based on a user name (email).
    """
    wallet_name = username.replace('@', '_')
    wallet_name = wallet_name.replace('.', '_')
    return 'i_{}'.format(wallet_name).lower()


def isbinary(s, params, ui, **kwargs):
    """Filter to do nothing with the file."""
    return s


def p_catalog_number(t):
    """catalog : CATALOG NUMBER"""
    t[0] = 'catalog', u'%13.13d' % t[2]


def tile_set(tile: int=(0, 0)):
    """Set UV image tile coordinates

    :param tile: Tile, Tile coordinate
    :type tile: int
    """
    pass


def view_template_id():
    """The id of the view template of the context
        """


@classmethod
def email_open_rate(cls, user_id, campaign_id=None):
    """Get email open rate"""
    if campaign_id:
        return cls.get_count(cls.objects.filter(data__sender_id=str(user_id
            ), data__type='email', data__campaign_id=str(campaign_id)))
    return cls.get_count(cls.objects.filter(data__sender_id=str(user_id),
        data__type='email'))


def get_last_week(the_date):
    """
    Get last ISO week of the date parameter
    :param the_date: the date parameter
    :return: last week and it's year
    """
    current_year, current_week, current_weekday = the_date.isocalendar()
    if current_week == 1:
        last_week = the_date.replace(year=current_year - 1, month=12, day=31)
        last_year = the_date.year - 1
    else:
        last_week = current_week - 1
        last_year = the_date.year
    return last_week, last_year


def relpath(origin, dest):
    """Given two absolute paths, work out a path from origin to destination.

    Assumes UNIX/URL type relative paths.
    If origin doesn't *end* with '/' we assume it's a file rather than
    a directory.

    If the same paths are passed in :
        if the path ends with ('/') then we return ''
        else we return the last part of the path (presumably a filename)

    If origin or dest don't start with '/' then we add it.

    We are *assuming* relative paths on the same device
        (i.e. same top level directory)
    """
    if not origin.startswith('/'):
        origin = '/' + origin
    if not dest.startswith('/'):
        dest = '/' + dest
    orig_list = origin.split('/')[:-1]
    dest_list = dest.split('/')
    i = 0
    for start_seg, dest_seg in zip(orig_list, dest_list):
        if start_seg != dest_seg:
            break
        i += 1
    segments = ['..'] * (len(orig_list) - i)
    segments += dest_list[i:]
    if len(segments) == 0:
        if dest.endswith('/'):
            return ''
        return dest_list[-1]
    else:
        return '/'.join(segments)


@classmethod
def from_exception(cls, exception):
    """Instanciate the class with the given exception."""
    name = type(exception).__name__
    value = str(exception)
    return cls('%s: %s' % (name, value) if value else name)


def p_inner_statement_list(p):
    """inner_statement_list : inner_statement_list inner_statement
                            | empty"""
    if len(p) == 3:
        p[0] = p[1] + [p[2]]
    else:
        p[0] = []


def scale(value, upper, lower, min_, max_):
    """Scales value between upper and lower values, depending on the given
    minimun and maximum value.
    """
    numerator = (lower - upper) * float(value - min_)
    denominator = float(max_ - min_)
    return numerator / denominator + upper


def context_set_float(data_path: str='', value: float=0.0, relative: bool=False
    ):
    """Set a context value

    :param data_path: Context Attributes, RNA context string
    :type data_path: str
    :param value: Value, Assignment value
    :type value: float
    :param relative: Relative, Apply relative to the current value (delta)
    :type relative: bool
    """
    pass


def median(l):
    """Calculate median value of the list

    Args:
        l (list of float): List of numbers
    """
    length = len(l)
    sorted_l = sorted(l)
    index = (length - 1) // 2
    if length % 2 == 0:
        return (sorted_l[index] + sorted_l[index + 1]) / 2.0
    else:
        return sorted_l[index]


def normalize(df, style='mean'):
    """ Returns a normalized version of a DataFrame or Series
    Parameters:
    df - DataFrame or Series
        The data to normalize
    style - function or string, default 'mean'
        The style to use when computing the norms. Takes 'mean' or 'minmax' to
        do mean or min-max normalization respectively. User-defined functions that take
        a pandas Series as input and return a normalized pandas Series are also accepted
    """
    if style == 'mean':
        df_mean, df_std = df.mean(), df.std()
        return (df - df_mean) / df_std
    elif style == 'minmax':
        col_min, col_max = df.min(), df.max()
        return (df - col_min) / (col_max - col_min)
    else:
        return style(df)


def _get_value(value):
    """Interpret null values and return ``None``. Return a list if the value
    contains a comma.
    """
    if not value or value in ['', '.', 'NA']:
        return None
    if ',' in value:
        return value.split(',')
    return value


def account_items():
    """the collection of account items for the client."""


def create_median_otsu_brain_mask(dwi_info, protocol, output_fname=None, **
    kwargs):
    """Create a brain mask and optionally write it.

    It will always return the mask. If output_fname is set it will also write the mask.

    Args:
        dwi_info (string or tuple or image): the dwi info, either:

            - the filename of the input file;
            - or a tuple with as first index a ndarray with the DWI and as second index the header;
            - or only the image as an ndarray

        protocol (string or :class:`~mdt.protocols.Protocol`): The filename of the protocol file or a Protocol object
        output_fname (string): the filename of the output file. If None, no output is written.
            If dwi_info is only an image also no file is written.
        **kwargs: the additional arguments for the function median_otsu.

    Returns:
        ndarray: The created brain mask
    """
    from mdt.lib.masking import create_median_otsu_brain_mask, create_write_median_otsu_brain_mask
    if output_fname:
        if not isinstance(dwi_info, (str, tuple, list)):
            raise ValueError(
                'No header obtainable, can not write the brain mask.')
        return create_write_median_otsu_brain_mask(dwi_info, protocol,
            output_fname, **kwargs)
    return create_median_otsu_brain_mask(dwi_info, protocol, **kwargs)


def yes_no_response(query: str, default_response: bool=True) ->bool:
    """Prompt user with a yes / no response for a given query string.

    :param query: query string
    :param default_response: default return value if user input invalid
    :return:
    """
    default_string = 'Y/n' if default_response else 'y/N'
    response = input(f'{query} [{default_string}] ').lower().strip()
    if response in ('y', 'yes'):
        return True
    elif response in ('n', 'no'):
        return False
    return default_response


def set_close_on_exec(fd):
    """set the clone on exec flag on a file descriptor. Ignore exceptions"""
    try:
        import fcntl
        flags = fcntl.fcntl(fd, fcntl.F_GETFD)
        flags |= fcntl.FD_CLOEXEC
        fcntl.fcntl(fd, fcntl.F_SETFD, flags)
    except Exception:
        pass


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 whitespace_tokenize(text):
    """Runs basic whitespace cleaning and splitting on a peice of text."""
    text = text.strip()
    if not text:
        return []
    tokens = text.split()
    return tokens


def keyitem_add():
    """Add key map item

    """
    pass


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 render_pep440_pre(pieces):
    """TAG[.post.devDISTANCE] -- No -dirty.

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


@classmethod
def get_keyring(cls):
    """Get current keyring backend.

        Returns the Keyring class, the `name` property will return the name
        of the keyring.
        """
    return cls.keyring.get_keyring()


def create_search_name(name):
    """HTML form의 name을 생성합니다.

    :param cls:
    :return:

    """
    return {'word': 'search_{}.word'.format(name), 'type': 'search_{}.type'
        .format(name)}


def get_object_string(swift, container, object_name):
    """Get the object contents as a string """
    data = swift.get_object(container, object_name)[1]
    try:
        return data.decode('utf-8')
    except AttributeError:
        return data


def read_factory_userpref():
    """Load factory default preferences. To make changes to preferences permanent, use “Save Preferences”

    """
    pass


def ban(roomJID, entity, reason=None, sender=None):
    """
        Ban a user from a MUC room.

        @param roomJID: The bare JID of the room.
        @type roomJID: L{JID<twisted.words.protocols.jabber.jid.JID>}

        @param entity: The bare JID of the entity to be banned.
        @type entity: L{JID<twisted.words.protocols.jabber.jid.JID>}

        @param reason: The reason for banning the entity.
        @type reason: C{unicode}

        @param sender: The entity sending the request.
        @type sender: L{JID<twisted.words.protocols.jabber.jid.JID>}
        """


def load_txt(fname, mode='r'):
    """Loads a .txt file to memory.

    Parameters
    ----------
    fname : str
        File name + path.
    mode : str, optional
        File open mode.

    Returns
    -------
    list of strings

    """
    assert fname, 'Must input a valid file name.'
    with open(fname, mode=mode, encoding='utf-8') as f:
        data = f.read()
    split_lines = data.split('\n')
    return split_lines[:-1]


def parse_bwf_description(description):
    """Parses a BWF Description string based on a hard-coded pre-defined convention.

    Args:
        description (str): The BWF Description string.

    Returns:
        dict: Metadata values indexed by the field name. If field is empty, the value is an empty string.
    """
    import re
    md = {}
    m = re.compile(
        'File content: (.+); File use: (.+); Original filename: (.+)').match(
        description)
    if m:
        matches = m.groups()
        md['FileContent'] = matches[0]
        md['FileUse'] = matches[1]
        md['OriginalFilename'] = matches[2]
    else:
        md['FileContent'] = ''
        md['FileUse'] = ''
        md['OriginalFilename'] = ''
    return md


def _default_scale(bound, objective):
    """`max{1.0,|objective|}`"""
    if objective > 1.0:
        return objective
    elif objective < -1.0:
        return -objective
    else:
        return 1.0


def full_dict(ldict, keys):
    """Return Comparison Dictionaries
       from list dict on keys
       keys: a list of keys that when
       combined make the row in the list unique
    """
    if type(keys) == str:
        keys = [keys]
    else:
        keys = keys
    cmp_dict = {}
    for line in ldict:
        index = []
        for key in keys:
            index.append(str(line.get(key, '')))
        index_str = '-'.join(index)
        cmp_dict[index_str] = line
    return cmp_dict


def get_api_interfaces_by_type(api_interfaces, type_str):
    """
    Extract Interface API responses by type
    :param api_interfaces: Interface API response
    :param type_str: Type string to match
    :return: Interfaces API response dict only containing type matching type_str
    """
    interface_list = []
    for interface in api_interfaces:
        if interface.get('type') == type_str:
            interface_list.append(interface)
    return interface_list


def _get_next_pivot(M, s1, s2=None):
    """
    Determines the first r,c indices in the submatrix of M starting
    with row s1 and column s2 index (row,col) that is nonzero,
    if it exists.

    Search starts with the s2th column and looks for the first nonzero
    s1 row. If none is found, search continues to the next column and so
    on.

    Parameters
    ----------
    M : np.array
        matrix represented as np.array
    s1 : int
        index of row position to start submatrix of M
    s2 : int, optional, default = s1
        index of column position to start submatrix of M

    Returns
    -------
    (r,c) : tuple of int or None

    """
    m, n = M.shape
    if not s2:
        s2 = s1
    for c in range(s2, n):
        for r in range(s1, m):
            if M[r, c] != 0:
                return r, c
    return None


def addWriter(writer):
    """
        I add writer to the set of file descriptors to get write events for.

        @param writer: An L{IWriteDescriptor} provider that will be checked for
                       write events until it is removed from the reactor with
                       L{removeWriter}.

        @return: L{None}.
        """


@classmethod
def encode(cls, bytes_encoded):
    """encode a DNA bytes string

        :param int bytes_encoded: encoded DNA string
        :return bytes: decoded bytes DNA sequence
        """
    raise NotImplementedError


def publish_metrics(metrics, configuration):
    """
    Publish a set of metrics via the set of configured publishers.

    :param metrics: The metrics to publish
    :param configuration: The configuration object containing the configured publishers
    """
    for publisher in configuration.publishers:
        publisher.publish(metrics, configuration.error_logger_name,
            configuration.enable_meta_metrics)


def get_requires_for_build_sdist(config_settings=None):
    """Returns a list of requirements for building, as strings"""
    return []


def format_str_len(string, length):
    """Format a string to break before a specified length"""
    words, result, current_line = string.split(), '', 0
    for word in words:
        if current_line + len(word) < length:
            result += ' ' + word
            current_line += 1 + len(word)
        else:
            result += '\n' + word
            current_line = len(word)
    return result


@staticmethod
def default_post_inherit(parent=None):
    """
        Default post inherited function.

        :param parent: the parent structure
        :return: None
        """
    return None


@staticmethod
def checksum_field_bytes(ctx):
    """Gatherd bytes for checksum calculation"""
    x = bytearray(ctx['header'].data)
    x += ctx['_']['token']
    if 'data' in ctx:
        x += ctx['data'].data
    return x


@classmethod
def get_by_url(cls, url):
    """ Note: this method for scraping, but used many place"""
    if not url:
        return 0
    return cls.select().where(cls.url == url).first()


def childConnectionLost(childFD):
    """
        Called when a file descriptor associated with the child process is
        closed.

        @type childFD: C{int}
        @param childFD: The file descriptor which was closed.
        """


@classmethod
def class_name(cls):
    """ Returns the name of the class. The name of the class is also the
            default collection name.

            .. seealso:: :func:`~Document.get_collection_name`
        """
    return cls.__name__


def viewer_border(gesture_mode: int=0, xmin: int=0, xmax: int=0, ymin: int=
    0, ymax: int=0, extend: bool=True):
    """Set the boundaries for viewer operations

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


def __get_string_variable_value(var_config_values):
    """
    Returns the associated string value
    :param var_config_values: the configuration dictionary
    :return: the value contained in the dictionary with the key 'value'
    """
    return var_config_values['value']


def cast_type(obj, *types):
    """
    Attempts to cast object to type. If first type fails, goes through args attempting all given types.

    :param obj: Object to cast to type
    :param types: Types to attempt
    :returns: Object in new type, if converted. Otherwise raises TypeError
    """
    if type(obj) in types:
        return obj
    for t in types:
        try:
            return t(obj)
        except Exception:
            pass
    raise TypeError('Failed to typecast {!r} to {}'.format(obj, ', '.join([
        repr(t) for t in types])))


@classmethod
def build(cls, pathname, min_results, max_results, got_results, or_more=False):
    """Build the message string

        .. note::
           All Exceptions must support being rebuilt from str(self), in order
           to transit from a process pool executor back to the main process
           through pickle/unpickle. This is why we can't just define __init__
           with all these arguments.

        :returns:
            new FileMatchError object
        """
    msg = "File match '%s' produced %d%s results, expected " % (pathname,
        got_results, ' or more' if or_more else '')
    if max_results is None:
        msg += 'at least %d' % min_results
    elif max_results == min_results:
        msg += 'exactly %d' % min_results
    elif min_results > 0:
        msg += 'between %d and %d' % (min_results, max_results)
    else:
        msg += 'up to %d' % max_results
    return cls(msg)


def get(key, default=None):
    """Return the annotation stored under key, or default if not found.
        """


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

        :param MemberSpaceLimitsChangeCustomQuotaType val:
        :rtype: EventType
        """
    return cls('member_space_limits_change_custom_quota', val)


def make_virtual_offset(block_start_offset, within_block_offset):
    """Compute a BGZF virtual offset from block start and within block offsets.

    The BAM indexing scheme records read positions using a 64 bit
    'virtual offset', comprising in C terms:

    block_start_offset << 16 | within_block_offset

    Here block_start_offset is the file offset of the BGZF block
    start (unsigned integer using up to 64-16 = 48 bits), and
    within_block_offset within the (decompressed) block (unsigned
    16 bit integer).

    >>> make_virtual_offset(0, 0)
    0

    >>> make_virtual_offset(0, 1)
    1

    >>> make_virtual_offset(0, 2**16 - 1)
    65535

    >>> make_virtual_offset(0, 2**16)
    Traceback (most recent call last):
    ...
    ValueError: Require 0 <= within_block_offset < 2**16, got 65536

    """
    if within_block_offset < 0 or within_block_offset >= 65536:
        raise ValueError('Require 0 <= within_block_offset < 2**16, got %i' %
            within_block_offset)
    if block_start_offset < 0 or block_start_offset >= 281474976710656:
        raise ValueError('Require 0 <= block_start_offset < 2**48, got %i' %
            block_start_offset)
    return block_start_offset << 16 | within_block_offset


def determine_wgtflag(vwgt, adjwgt):
    """Determine the wgtflag used in many parallel partitioning routines
    """
    if vwgt is None and adjwgt is None:
        return 0
    if vwgt is None:
        return 1
    return 2 if adjwgt is None else 3


def print_then_prompt(line):
    """
    Prints a message on screen, then prints the mplane prompt

    """
    print(line)
    print('|mplane| ', end='', flush=True)
    pass


def is_format_umls(concept='C0007194'):
    """
    CUI style notation
    :param concept:
    :return: boolean
    """
    return str(concept).startswith('C') and 8 == str(concept).__len__()


@classmethod
def get_commissions(cls, contract_values, turnover, **kwargs):
    """
        Return a DataFrame of commissions as percentages of account equity.
        """
    cost_per_contract = (cls.BROKER_COMMISSION_PER_CONTRACT + cls.
        EXCHANGE_FEE_PER_CONTRACT + cls.CARRYING_FEE_PER_CONTRACT)
    commission_rates = float(cost_per_contract) / contract_values
    commissions = commission_rates * turnover
    return commissions


@classmethod
def external(cls, key, data):
    """returns an ApeTagItem of external data

        key is an ASCII string, data is a binary string"""
    return cls(2, 0, key, data)


def _get_logical(source_lines, result, logical_start, logical_end):
    """Return the logical line corresponding to the result.

    Assumes input is already E702-clean.

    """
    row = result['line'] - 1
    col = result['column'] - 1
    ls = None
    le = None
    for i in range(0, len(logical_start), 1):
        assert logical_end
        x = logical_end[i]
        if x[0] > row or x[0] == row and x[1] > col:
            le = x
            ls = logical_start[i]
            break
    if ls is None:
        return None
    original = source_lines[ls[0]:le[0] + 1]
    return ls, le, original


@classmethod
def load_author(cls, api, author):
    """Create an object starting with an author element."""
    user = cls(api)
    setattr(user, 'nick', author.name.text)
    setattr(user, 'avatar', author.FindExtensions('avatar')[0].text)
    slug = None
    if len(author.FindExtensions('slug')) == 1:
        slug = author.FindExtensions('slug')[0].text
    setattr(user, 'slug', slug)
    return user


def _serialize_numeric_array(data):
    """Serializes a numeric numpy array into bytes.

  Args:
    data: A numeric numpy array.

  Returns:
    bytes of the array to be serialized on disk.
  """
    return data.tobytes()


def logical_not(input):
    """Perform a logical not operation.  Equivalent to np.logical_not

    Parameters
    ----------
    input : Tensor
        The input

    Returns
    -------
    Tensor
        The result
    """
    pass


def getLimits():
    """Some useful info regarding barchart maximum usage."""
    return """Every user is able to make 400 getQuote queries and 150 getHistory queries per day.
              Using getQuote, One coud track a single stock for open hours updating once a minute.
              Or one could track a single stock for one hour updating every 9 seconds.
              https://www.barchart.com/ondemand/free-market-data-api/faq."""


def decorate_places_preset_trans(net):
    """
    Decorate places with information useful for the replay

    Parameters
    -------------
    net
        Petri net
    """
    for place in net.places:
        place.ass_trans = set()
    for trans in net.transitions:
        for place in trans.sub_marking:
            place.ass_trans.add(trans)


@staticmethod
def _dump_bs4_value(value):
    """Return a BeautifulSoup object as a string

        :param bs4.BeautifulSoup value: The object to return a string from
        :rtype: str

        """
    return str(value)


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

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


def get_storage_index():
    """Return a string with the (binary) storage index."""


@classmethod
def from_ibis(cls, dtype, nullable=None):
    """
        Return a OmniSciDBDataType correspondent to the given Ibis data type.

        Parameters
        ----------
        dtype : ibis.expr.datatypes.DataType
        nullable : bool

        Returns
        -------
        OmniSciDBDataType

        Raises
        ------
        NotImplementedError
            if the given data type was not implemented.
        """
    dtype_ = type(dtype)
    if dtype_ in cls.ibis_dtypes:
        typename = cls.ibis_dtypes[dtype_]
    elif dtype in cls.ibis_dtypes:
        typename = cls.ibis_dtypes[dtype]
    else:
        raise NotImplementedError('{} dtype not implemented'.format(dtype))
    if nullable is None:
        nullable = dtype.nullable
    return cls(typename, nullable=nullable)


def add_axis_to_dic(dic, adic, n):
    """
    Add an axis dictionary (adic) to a NMRPipe dictionary (dic) as axis n.
    """
    fn = ['FDF2', 'FDF1', 'FDF3', 'FDF4'][n]
    dic[fn + 'SW'] = float(adic['sw'])
    dic[fn + 'OBS'] = float(adic['obs'])
    dic[fn + 'CAR'] = float(adic['car'] / adic['obs'])
    dic[fn + 'LABEL'] = adic['label']
    if adic['complex']:
        dic[fn + 'QUADFLAG'] = 0.0
    else:
        dic[fn + 'QUADFLAG'] = 1.0
    if adic['complex'] and n != 0:
        psize = adic['size'] / 2.0
    else:
        psize = adic['size'] / 1.0
    osize = psize
    if adic['time']:
        dic[fn + 'TDSIZE'] = psize
        dic[fn + 'FTFLAG'] = 0.0
    else:
        dic[fn + 'FTSIZE'] = psize
        dic[fn + 'FTFLAG'] = 1.0
    dic[fn + 'APOD'] = dic[fn + 'TDSIZE']
    if n == 0 or dic['FD2DPHASE'] != 1:
        dic[fn + 'CENTER'] = int(psize / 2.0) + 1.0
    else:
        dic[fn + 'CENTER'] = int(psize / 4.0) + 1
        osize = psize / 2.0
    dic[fn + 'ORIG'] = dic[fn + 'CAR'] * dic[fn + 'OBS'] - dic[fn + 'SW'] * (
        osize - dic[fn + 'CENTER']) / osize
    if n == 0:
        dic['FDSIZE'] = psize
        dic['FDREALSIZE'] = psize
    if n == 1:
        dic['FDSPECNUM'] = float(adic['size'])
        if adic['encoding'] == 'complex':
            dic['FDF1AQSIGN'] = 0
        if adic['encoding'] == 'states':
            dic['FDF1AQSIGN'] = 0
        elif adic['encoding'] == 'states-tppi':
            dic['FDF1AQSIGN'] = 16
    if n == 2:
        if adic['complex']:
            dic['FDF3SIZE'] = psize * 2
        else:
            dic['FDF3SIZE'] = psize
    if n == 3:
        if adic['complex']:
            dic['FDF4SIZE'] = psize * 2
        else:
            dic['FDF3SIZE'] = psize
    return dic


@classmethod
def plot_histogram(cls, x, y, *, nsamples=50000, names=None, **kwargs):
    """
        Plot the posterior distribution as histogram.

        Args:
            x (np.array): a vector of scores for the first model
            y (np.array): a vector of scores for the second model
            nsamples (int): the number of samples (default: 50000)
            names (tuple of str): names of classifiers

        Returns:
            matplotlib figure
        """
    return cls(x, y, rope=0, nsamples=nsamples, **kwargs).plot_histogram(names)


def dump(obj, fp):
    """Serialize obj to fp (a file-like object) in ARPA format."""
    obj.write(fp)


def last_update_time_in_minutes():
    """Return the time this feed was last updated in minutes since epoch.
        """


def window_duplicate():
    """Duplicate the current Blender window

    """
    pass


def tile_previewer(full_extent, tileset_url, output_dir=None, filename=
    'index.html', title='Datashader Tileset', min_zoom=0, max_zoom=40,
    height=None, width=None, **kwargs):
    """Helper function for creating a simple Bokeh figure with
    a WMTS Tile Source.

    Notes
    -----
    - if you don't supply height / width, stretch_both sizing_mode is used.
    - supply an output_dir to write figure to disk.
    """
    try:
        from bokeh.plotting import figure
        from bokeh.models.tiles import WMTSTileSource
        from bokeh.io import output_file, save
        from os import path
    except ImportError:
        raise ImportError(
            'conda install bokeh to enable creation of simple tile viewer')
    if output_dir:
        output_file(filename=path.join(output_dir, filename), title=title)
    xmin, ymin, xmax, ymax = full_extent
    if height and width:
        p = figure(width=width, height=height, x_range=(xmin, xmax),
            y_range=(ymin, ymax), tools='pan,wheel_zoom,reset', **kwargs)
    else:
        p = figure(sizing_mode='stretch_both', x_range=(xmin, xmax),
            y_range=(ymin, ymax), tools='pan,wheel_zoom,reset', **kwargs)
    p.background_fill_color = 'black'
    p.grid.grid_line_alpha = 0
    p.axis.visible = True
    tile_source = WMTSTileSource(url=tileset_url, min_zoom=min_zoom,
        max_zoom=max_zoom)
    p.add_tile(tile_source, render_parents=False)
    if output_dir:
        save(p)
    return p


def gapup(f):
    """Determine whether or not there has been a gap up.

    Parameters
    ----------
    f : pandas.DataFrame
        Dataframe with columns ``open`` and ``close``.

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

    References
    ----------
    *A gap is a break between prices on a chart that occurs when the
    price of a stock makes a sharp move up or down with no trading
    occurring in between* [IP_GAP]_.

    """
    new_column = f['open'] > f['close'].shift(1)
    return new_column


def generate_tclloader_platimg(platform):
    """
    Generate platform-specific .img files.

    :param platform: Platform type (i.e. subdirectory of target/product).
    :type platform: str
    """
    imgs = ['recovery', 'system', 'userdata', 'cache', 'boot']
    if platform in ('bbry_sdm660', 'bbry_sdm636'):
        imgs.append('vendor')
    return imgs


def glGetShaderInfoLog(program, maxLength: int, length: int, infoLog: 'Buffer'
    ):
    """Returns the information log for a shader object.

    :param shader: Specifies the shader object whose information log is to be queried.
    :type shader: int
    :param maxLength: Specifies the size of the character buffer for storing the returned information log.
    :type maxLength: int
    :param length: Returns the length of the string returned in infoLog (excluding the null terminator).
    :type length: int
    :param infoLog: Specifies an array of characters that is used to return the information log.
    :type infoLog: 'Buffer'
    """
    pass


@classmethod
def module(cls):
    """
        The Python module of the UDT.
        """
    raise NotImplementedError('UDT must implement module().')


def addpath(path):
    """Add a path to the collection.

        All directories in the path will be added to the collection.
        """


def ping(*args, **kwargs):
    """
    Check if current application is running.

    :return: Pong response.
    """
    return {'pong': True}


def percentage(x, pos):
    """
    Adds percentage sign to plot ticks.
    """
    return '%.0f%%' % x


def _c(cmd, json_out=True):
    """Helper function to construct commands for the `gcloud` tool"""
    return 'gcloud ' + cmd + (' --format=json' if json_out else '')


def get_from_dicts(dict1, key, default_value, dict2, extra=''):
    """
    Input:  dict1         - first check in this dict (and remove if there)
            key           - key in dict1
            default_value - default value if not found
            dict2         - then check from here

    Output: value
    """
    value = default_value
    if key not in dict1:
        if dict2 != None:
            value = dict2.get(extra + key, default_value)
    else:
        value = dict1[key]
        del dict1[key]
        if dict2 != None:
            dict2[extra + key] = value
    return value


def remove_start_end(token, start, end):
    """Remove the enclosing tags"""
    token = token.strip()
    if not (token.startswith(start) and token.endswith(end)):
        raise ValueError
    return token[1:-1]


def iteritems():
    """iterate over items"""


def rcut(s, sub):
    """
    Cuts off the substring `sub` if string `s` ends with it.
    """
    len_sub = len(sub)
    if s[-len_sub:] == sub:
        return s[:-len_sub]
    return s


def _aespad(data):
    """ipmi demands a certain pad scheme, per table 13-20 AES-CBC encrypted

    payload fields.
    """
    currlen = len(data) + 1
    neededpad = currlen % 16
    if neededpad:
        neededpad = 16 - neededpad
    padval = 1
    pad = bytearray(neededpad)
    while padval <= neededpad:
        pad[padval - 1] = padval
        padval += 1
    pad.append(neededpad)
    return pad


def add_task_name(apps, schema_editor):
    """
    This function loops through all ScheduledTask objects and sets the task_name to a unique value, based on the
    existing task attribute, e.g.
    myapp.mytasks.hello-world
    myapp.mytasks.hello-world-1
    myapp.mytasks.hello-world-2
    """
    ScheduledTask = apps.get_model('carrot', 'ScheduledTask')
    db_alias = schema_editor.connection.alias
    index = 0
    for t in ScheduledTask.objects.using(db_alias).filter(task_name=''):
        index += 1
        task_name = '%s-%i' % (t.task, index)
        t.task_name = task_name
        t.save()


@classmethod
def get_rendition_model(cls):
    """ Get the Rendition model for this Image model """
    return cls.renditions.rel.related_model


def copy_ionized_electrons_batch(i_batch, batch_size, elec_old_Ntot,
    ion_Ntot, cumulative_n_ionized, ionized_from, i_level,
    store_electrons_per_level, elec_x, elec_y, elec_z, elec_inv_gamma,
    elec_ux, elec_uy, elec_uz, elec_w, elec_Ex, elec_Ey, elec_Ez, elec_Bx,
    elec_By, elec_Bz, ion_x, ion_y, ion_z, ion_inv_gamma, ion_ux, ion_uy,
    ion_uz, ion_w, ion_Ex, ion_Ey, ion_Ez, ion_Bx, ion_By, ion_Bz):
    """
    Create the new electrons by copying the properties (position, momentum,
    etc) of the ions that they originate from.

    Particles are handled by batch: this functions goes through one batch
    of ion macroparticles and checks which macroparticles have been ionized
    during the present timestep (using the flag `ionized_from`, which
    is -1 if the ion is not ionized, and has the value of the original level
    otherwise).
    In order to know at which position, in the electron array, this data
    should be copied, the cumulated number of electrons `cumulative_n_ionized`
    (one element per batch) is used.
    """
    elec_index = elec_old_Ntot + cumulative_n_ionized[i_level, i_batch]
    N_max = min((i_batch + 1) * batch_size, ion_Ntot)
    for ion_index in range(i_batch * batch_size, N_max):
        create_electron = False
        if not store_electrons_per_level and ionized_from[ion_index] >= 0:
            create_electron = True
        if store_electrons_per_level and ionized_from[ion_index] == i_level:
            create_electron = True
        if create_electron:
            elec_x[elec_index] = ion_x[ion_index]
            elec_y[elec_index] = ion_y[ion_index]
            elec_z[elec_index] = ion_z[ion_index]
            elec_ux[elec_index] = ion_ux[ion_index]
            elec_uy[elec_index] = ion_uy[ion_index]
            elec_uz[elec_index] = ion_uz[ion_index]
            elec_inv_gamma[elec_index] = ion_inv_gamma[ion_index]
            elec_w[elec_index] = ion_w[ion_index]
            elec_Ex[elec_index] = ion_Ex[ion_index]
            elec_Ey[elec_index] = ion_Ey[ion_index]
            elec_Ez[elec_index] = ion_Ez[ion_index]
            elec_Bx[elec_index] = ion_Bx[ion_index]
            elec_By[elec_index] = ion_By[ion_index]
            elec_Bz[elec_index] = ion_Bz[ion_index]
            elec_index += 1


def validate_fingerprint_if_present(instance, _attribute, value):
    """Validate that the private key matches the fingerprint pin.

    :raises ValueError: if the fingerprint pin is present \\
                        but doesn't match the private key
    """
    if not value:
        return
    if instance.private_key.matches_fingerprint(value):
        return
    raise ValueError(
        f'The private key provided (with a fingerprint of {instance.private_key.fingerprint!s}) does not match the pinned fingerprint value of {value!s}'
        )


def KK_RC13(w, Rs, R_values, t_values):
    """
    Kramers-Kronig Function: -RC-

    Kristian B. Knudsen (kknu@berkeley.edu / kristianbknudsen@gmail.com)
    """
    return Rs + R_values[0] / (1 + w * 1.0j * t_values[0]) + R_values[1] / (
        1 + w * 1.0j * t_values[1]) + R_values[2] / (1 + w * 1.0j * t_values[2]
        ) + R_values[3] / (1 + w * 1.0j * t_values[3]) + R_values[4] / (1 +
        w * 1.0j * t_values[4]) + R_values[5] / (1 + w * 1.0j * t_values[5]
        ) + R_values[6] / (1 + w * 1.0j * t_values[6]) + R_values[7] / (1 +
        w * 1.0j * t_values[7]) + R_values[8] / (1 + w * 1.0j * t_values[8]
        ) + R_values[9] / (1 + w * 1.0j * t_values[9]) + R_values[10] / (1 +
        w * 1.0j * t_values[10]) + R_values[11] / (1 + w * 1.0j * t_values[11]
        ) + R_values[12] / (1 + w * 1.0j * t_values[12])


def stream_attach(stream, circuit):
    """the stream has been attached to a circuit"""


def navmesh_face_copy():
    """Copy the index from the active face

    """
    pass


def hide_columns(column_left, column_right):
    """Returns a CSS class matching the current column status.
        """


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 arena_config(posx, posy, ppm, center=None, flip_y=True):
    """
    This function will convert the position values from the units of bits to centimeters. It will also center the arena
    around the point (0,0) if you provide a center value. I have added a flip_y keyword argument because the Tint
    program flips the y values so I will allow you to flip the values as well (or not).

    :param posx:
    :param posy:
    :param arena:
    :param conversion:
    :param center:
    :param flip_y: bool value that will determine if you want to flip y or not. When recording on Intan we inverted the
    positions due to the camera position. However in the virtualmaze you might not want to flip y values.
    :return:
    """
    if center is not None:
        posx = 100 * (posx - center[0]) / ppm
    else:
        posx = 100 * posx / ppm
    if flip_y:
        if center is not None:
            posy = 100 * (-posy + center[1]) / ppm
        else:
            posy = 100 * -posy / ppm
    elif center is not None:
        posy = 100 * (posy + center[1]) / ppm
    else:
        posy = 100 * posy / ppm
    return posx, posy


def activate_user(UserId=None):
    """
    Activates the specified user. Only active users can access Amazon WorkDocs.
    See also: AWS API Documentation

    :example: response = client.activate_user(
        UserId='string'
    )

    :type UserId: string
    :param UserId: [REQUIRED]
            The ID of the user.

    :rtype: dict
    :return: {
        'User': {
            'Id': 'string',
            'Username': 'string',
            'EmailAddress': 'string',
            'GivenName': 'string',
            'Surname': 'string',
            'OrganizationId': 'string',
            'RootFolderId': 'string',
            'RecycleBinFolderId': 'string',
            'Status': 'ACTIVE'|'INACTIVE'|'PENDING',
            'Type': 'USER'|'ADMIN',
            'CreatedTimestamp': datetime(2015, 1, 1),
            'ModifiedTimestamp': datetime(2015, 1, 1),
            'TimeZoneId': 'string',
            'Locale': 'en'|'fr'|'ko'|'de'|'es'|'ja'|'ru'|'zh_CN'|'zh_TW'|'pt_BR'|'default',
            'Storage': {
                'StorageUtilizedInBytes': 123,
                'StorageRule': {
                    'StorageAllocatedInBytes': 123,
                    'StorageType': 'UNLIMITED'|'QUOTA'
                }
            }
        }
    }

    """
    pass


def floor(attrs, inputs, proto_obj):
    """ Calculate floor value for input """
    return 'floor', attrs, inputs


def linear_combination(c1, c2, k1):
    """Returns medium color between c1 and c2"""
    k2 = 1.0 - k1
    r = int(k1 * c1[0] + k2 * c2[0])
    g = int(k1 * c1[1] + k2 * c2[1])
    b = int(k1 * c1[2] + k2 * c2[2])
    return r, g, b


@classmethod
def all(cls, collection, skip=None, limit=None):
    """
            Returns all documents of the collection

            :param collection Collection instance
            :param skip  The number of documents to skip in the query
            :param limit The maximal amount of documents to return. The skip is applied before the limit restriction.

            :returns Document list
        """
    kwargs = {'skip': skip, 'limit': limit}
    return cls._construct_query(name='all', collection=collection, multiple
        =True, **kwargs)


def premise_text(quantifier, first, second):
    """ Constructs the textual representation of a syllogistic premise from
    a quantifier and two objects.

    Parameters
    ----------
    quantifier : str
        Syllogistic quantifier identifier (e.g., 'A').

    first : str
        First object.

    second : str
        Second object.

    Returns
    -------
    str
        Syllogistic premise text representation (e.g., 'All A are B').

    """
    text = ''
    if quantifier == 'A':
        text = 'All {} are {}'
    elif quantifier == 'I':
        text = 'Some {} are {}'
    elif quantifier == 'E':
        text = 'No {} are {}'
    elif quantifier == 'O':
        text = 'Some {} are not {}'
    else:
        raise ValueError('Invalid premise quantifier')
    return text.format(first, second)


def p_create_definition(p):
    """create_definition : identifier column_definition"""
    p[0] = p[1], p[2]


@classmethod
def from_transport(cls, t, window_size=None, max_packet_size=None):
    """
        Create an SFTP client channel from an open `.Transport`.

        Setting the window and packet sizes might affect the transfer speed.
        The default settings in the `.Transport` class are the same as in
        OpenSSH and should work adequately for both files transfers and
        interactive sessions.

        :param .Transport t: an open `.Transport` which is already
            authenticated
        :param int window_size:
            optional window size for the `.SFTPClient` session.
        :param int max_packet_size:
            optional max packet size for the `.SFTPClient` session..

        :return:
            a new `.SFTPClient` object, referring to an sftp session (channel)
            across the transport

        .. versionchanged:: 1.15
            Added the ``window_size`` and ``max_packet_size`` arguments.
        """
    chan = t.open_session(window_size=window_size, max_packet_size=
        max_packet_size)
    if chan is None:
        return None
    chan.invoke_subsystem('sftp')
    return cls(chan)


def _pathify(s):
    """Convert spaces to hyphens and lowercase a string."""
    return '-'.join(s.split()).lower()


@classmethod
def resolve(cls, url, gw=None, key=None, identity=None, certificates=None,
    auth=None, p_auth=None, crypto=None, internal=False, buf_size=None,
    conf=None, nowait=False, reactor=None, processor=None):
    """See :meth:`versile.orb.url.VUrl.resolve`\\ .

        :param reactor:   a (running) reactor for the link
        :param processor: processor for remote calls
        :type  processor: :class:`versile.common.processor.VProcessor`

        Similar to :meth:`versile.orb.url.VUrl.resolve` but adds
        *reactor* and *processor* as optional keyword arguments which
        are passed to :meth:`connect`\\ . This enables setting reactor
        and/or processor for the link of the resolved VRI.

        """
    if not nowait:
        _res = cls.resolve
        call = _res(url=url, gw=gw, key=key, identity=identity,
            certificates=certificates, auth=auth, p_auth=p_auth, crypto=
            crypto, internal=internal, buf_size=buf_size, conf=conf,
            reactor=reactor, processor=processor, nowait=True)
        return call.result()
    url = cls.parse(url=url)
    result = cls._create_cls_resolve_result()
    result._connect(url=url, gw=gw, key=key, identity=identity,
        certificates=certificates, auth=auth, p_auth=p_auth, crypto=crypto,
        internal=internal, buf_size=buf_size, conf=conf, reactor=reactor,
        processor=processor)
    return result


def replace(source_files):
    """Utility function to replace empty files list"""
    replace_files = list(source_files)
    for o in replace_files:
        o.name = o.name[:-1] + '0'
        o.index = o.index[0], 0
        o.out_index = None
        o.in_index = None
    replace_data = [(o.name, None) for o in replace_files]
    return replace_files, replace_data


def is_output_layer(layer, model):
    """Test if `layer` is an output layer."""
    return layer.name in model.output_names


def insert_bits(source, insert, positions):
    """Distribute bits from the inserted value into the source.

    This function will insert each bit sequentially, meaning that as
    bits are inserted, they will offset the position of later bits.
    The result will affect the position of all existing and added bits
    on each iteration.

    Args:
        source (int): Data in integer form to have bits spliced in.
        insert (int): Data to put into the source.
        positions (list): Ordered integers for splicing data into the
            source. Each number is iterated through in order.

    Returns:
        Integer representing the resulting data.

    """
    length = source.bit_length()
    for i, position in enumerate(positions):
        length = max(length, position)
        bottom_mask = 2 ** position - 1
        top_mask = 2 ** (length + i + 1) - 1 ^ bottom_mask
        insert_bit = (2 ** i & insert) >> i
        source = (top_mask & source) << 1 | source & bottom_mask
        source |= insert_bit << position
    return source


@staticmethod
def get_name(json):
    """Return the name of the package.

        We cannot just rely on the name submitted by the user, since they may
        use the wrong capitalization.
        """
    return json['info']['name']


def convert_biom_ids_to_md5(table):
    """convert biom feature ids to md5 and return new table

    Parameters
    ----------
    table : BIOM table

    Returns
    -------
    BIOM table
        The new BIOM table
    dict
        {original_id: new_id}
    """
    import hashlib
    new_ids = dict()
    for _id in table.ids(axis='observation'):
        m = hashlib.md5()
        m.update(_id.encode('utf-8'))
        new_ids[_id] = m.hexdigest()
    table.update_ids(new_ids, axis='observation')
    return table, new_ids


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

    Like 'git describe --tags --dirty --always'.

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


@classmethod
def fromtextfile(cls, fileobj, endcard=False):
    """
        Read a header from a simple text file or file-like object.

        Equivalent to::

            >>> Header.fromfile(fileobj, sep='\\n', endcard=False,
            ...                 padding=False)

        See Also
        --------
        fromfile
        """
    return cls.fromfile(fileobj, sep='\n', endcard=endcard, padding=False)


def tesla2Oe(tesla):
    """
    Field (Oe) from induction (T).

    Parameters
    ----------
    tesla : ndarray or scalar
        Induction in Tesla.

    Returns
    -------
    o : ndarray or scalar
        field in Oe.

    """
    o = tesla * 10000
    return o


@staticmethod
def _extra_attributes_from_db_object(vnf_sw_image, db_sw_image,
    expected_attrs=None):
    """Method to help with migration of extra attributes to objects.

        """
    if expected_attrs is None:
        expected_attrs = []
    if 'metadata' in expected_attrs:
        setattr(vnf_sw_image, 'metadata', db_sw_image['metadetails'])


def _print_output_tag(end='\n'):
    """
    Print an output prompt, ie a colorized [Output].

    Args:
    end (str):     End of line (passed to print's end kwarg)
    """
    print('\x1b[95m[Output]\x1b[0m', end=end)


def __call__():
    """ Return a list of key, value tuples
        """


def _sanitize_op_name(op_name):
    """
  Sanitize the op name
  - ignore '^' character of control input
  """
    if op_name.startswith('^'):
        return op_name[1:]
    return op_name


def dtype_eq(x, dtype):
    """
    >>> dtype_eq("hogehoge",None)
    True
    >>> dtype_eq("hogehoge",tf.float32)
    False
    >>> dtype_eq(tf.zeros([1,2,3],dtype=tf.float32),tf.float32)
    True
    """
    if dtype == None:
        return True
    elif not hasattr(x, 'dtype'):
        return False
    else:
        return x.dtype == dtype


def frequency_long(frequency_short):
    """Convert short frequency to a human-readable version"""
    frequency = ''
    if frequency_short == 'A':
        frequency = 'Annual'
    elif frequency_short == 'SA':
        frequency = 'Semi-Annual'
    elif frequency_short == 'Q':
        frequency = 'Quarterly'
    elif frequency_short == 'M':
        frequency = 'Monthly'
    elif frequency_short == 'BW':
        frequency = 'Bi-Weekly'
    elif frequency_short == 'W':
        frequency = 'Weekly'
    elif frequency_short == 'D':
        frequency = 'Daily'
    return frequency


def has_single_provenance(provenance_list):
    """ Checks whether the collection is all from the same source
    Runtime: O(n)

    Arguments:
    provenance_list -- list of Glucose-related objects with
                provenance_identifier property

    Output:
    True if the samples are from same source
    """
    try:
        first_provenance = provenance_list[0]
    except IndexError:
        print("Out of bounds error: list doesn't contain objects")
    for provenance in provenance_list:
        if provenance != first_provenance:
            return False
    return True


def __nonzero__():
    """Check if the collection is non-empty.

        Return a true value if the collection is non-empty and a
        false value otherwise.
        """


def calc_bytes(sizestring):
    """
    Calculate a size in bytes for the specified size string.  If the
    string is terminated with the following suffixes the specified
    scaling will be applied:

    'K' or 'k' : kilobytes - value * 1024
    'M' or 'm' : megabytes - value * 1024*1024
    'G' or 'g' : gigabytes - value * 1024*1024*1024

    Returns a size in bytes.
    """
    if sizestring.endswith('k') or sizestring.endswith('K'):
        return int(sizestring[:-1]) * 1024
    elif sizestring.endswith('m') or sizestring.endswith('M'):
        return int(sizestring[:-1]) * 1024 * 1024
    elif sizestring.endswith('g') or sizestring.endswith('G'):
        return int(sizestring[:-1]) * 1024 * 1024 * 1024
    else:
        return int(sizestring)


def normalize_title(title, opt_title):
    """Return a string with that contains the title and opt_title if it's
not the empty string

    See test_normalize_title() for usage

    """
    if opt_title is not '':
        title = opt_title + ' - ' + title
    return title


def save_replaced(soup, path):
    """Save *soup* object into a file  with *path* as filename."""
    with open(path, 'w') as f:
        f.write(str(soup))


def parameters_ui(layout, params):
    """ Create the ui for the rig parameters.
    """
    r = layout.row()
    r.prop(params, 'make_control')
    r = layout.row()
    r.prop(params, 'make_deform')


def _resolve_name(name, package, level):
    """Resolve a relative module name to an absolute one."""
    bits = package.rsplit('.', level - 1)
    if len(bits) < level:
        raise ValueError('attempted relative import beyond top-level package')
    base = bits[0]
    return '{}.{}'.format(base, name) if name else base


def getConfigFilename(arglist, flag='--conf'):
    """Reads argument list to determine if there is a config file provided.

    Used to work with argparse. If `flag` is present in the input array,
    the next argument is assumed to be the filename for the config file.

    Parameters
    ----------
    arglist : list
        List of all arguments to input function, from either sys.argv or a
        custom list
    flag : str (optional) ('--conf')
        Command-line flag to look for in arglist to signify next element
        is filename for config options

    Returns
    -------
    config_filename : str (None)
        Name of config file if 'flag' is present in arglist, otherwise
        defaults to None
    is_first : bool
        Will be true if `flag` is first element of arglist. Used for method
        to allow required args to be passed from config file.

    """
    config_filename = None
    is_first = False
    if flag in arglist:
        idx = arglist.index(flag)
        config_filename = arglist[idx + 1]
        if idx == 0:
            is_first = True
        return config_filename, is_first
    return None, is_first


def _sort_candidates(candidates):
    """Sort candidates by distance."""
    return sorted(candidates, key=lambda match: match.distance)


def get_cache_url(url, params):
    """
    Appends the parameters to the url.
    This is used to cache urls with specific parameters.
    Parameters are sorted by key names since parameters are unordered.

    This may not generate valid urls, this should only be used for redis keys.
    """
    params = map(lambda param: str(param[0]) + '=' + str(param[1]), params.
        items())
    params = '&'.join(sorted(params))
    return url + '?' + params


@classmethod
def from_element(cls, data, session=None):
    """
        Convert XML / Element to object

        Parameters
        ----------
        data : xml-string or Element
            The element to convert
        session : Session, optional
            The requests.Session object

        Returns
        -------
        :class:`FieldSelection`

        """
    return cls(data.attrib['name'], data.attrib['source'])


def shortest_id(name, names):
    """ Return shortest name that isn't a duplicate in names """
    if name in names:
        names.remove(name)
    for i, letter in enumerate(name):
        for other_name in names:
            if other_name[i:i + 1] == letter:
                break
        else:
            break
    return name[0:i + 1]


def as_rna(s):
    """Converts string to uppercase RNA"""
    return s.upper().replace('T', 'U')


@classmethod
def dimsOf(cls, x):
    """Calculate total dimension-set of the elements in x

        This is *extremely* messy, as it has to track nested arrays
        where the arrays could be different sizes on all sorts of
        levels...
        """
    try:
        dimensions = [len(x)]
    except (TypeError, AttributeError, ValueError) as err:
        return []
    else:
        childDimension = None
        for child in x:
            newDimension = cls.dimsOf(child)
            if childDimension is not None:
                if newDimension != childDimension:
                    raise ValueError(
                        'Non-uniform array encountered: %s versus %s' % (
                        newDimension, childDimension), x)


def removeUnreferencedIDs(referencedIDs, identifiedElements):
    """
    Removes the unreferenced ID attributes.

    Returns the number of ID attributes removed
    """
    global _num_ids_removed
    keepTags = ['font']
    num = 0
    for id in identifiedElements:
        node = identifiedElements[id]
        if id not in referencedIDs and node.nodeName not in keepTags:
            node.removeAttribute('id')
            _num_ids_removed += 1
            num += 1
    return num


def all_true(seq):
    """Returns True if all elements of `seq` are true,
       otherwise returns first non-true element.
    """
    for e in seq:
        if not e:
            return e
    else:
        return True


def psi_Ca_CO3_HSO4_HMW84(T, P):
    """c-a-a': calcium carbonate bisulfate [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def resolve_case(caseId=None):
    """
    Takes a caseId and returns the initial state of the case along with the state of the case after the call to  ResolveCase completed.
    See also: AWS API Documentation

    :example: response = client.resolve_case(
        caseId='string'
    )

    :type caseId: string
    :param caseId: The AWS Support case ID requested or returned in the call. The case ID is an alphanumeric string formatted as shown in this example: case-12345678910-2013-c4c1d2bf33c5cf47

    :rtype: dict
    :return: {
        'initialCaseStatus': 'string',
        'finalCaseStatus': 'string'
    }

    """
    pass


def getWordIdx(token, word2Idx):
    """Returns from the word2Idex table the word index for a given token"""
    if token in word2Idx:
        return word2Idx[token]
    elif token.lower() in word2Idx:
        return word2Idx[token.lower()]
    return 0


def _assert_type(element, types, name, clazz):
    """
    Utility to help assert types are only as expected.

    :param element: The element being checked
    :param types: The allowed types
    :param name: The name of the element
    :param clazz: The class containing the element
    :type element: Any
    :type types: Iterable[Any]
    :type name: str
    :type clazz: class
    :return: None
    """
    assert isinstance(types, tuple)
    assert isinstance(name, str)
    if element is not None:
        assert isinstance(element, types
            ), 'Incorrect type ({}) for {}.{}, [{}]'.format(type(element),
            clazz.__qualname__, name, types)


def action_status(cluster, arguments):
    """Display a message about the state of the cluster.

    The return code is also set: 0 indicates that the cluster is running; 1
    indicates that it exists, but is not running; 2 indicates that it does not
    exist.
    """
    if cluster.exists:
        if cluster.running:
            print('%s: running' % cluster.datadir)
            raise SystemExit(0)
        else:
            print('%s: not running' % cluster.datadir)
            raise SystemExit(1)
    else:
        print('%s: not created' % cluster.datadir)
        raise SystemExit(2)


@staticmethod
def adjust_minutes(h, m):
    """Adjust any number of seconds > 60 by rolling over into minutes."""
    hours_overflow = int((m - m % 60) / 60)
    return h + hours_overflow, m - 60 * hours_overflow


@staticmethod
def sanitize_name_as_tag_value(name):
    """take a metric name and sanitize it so it is guaranteed to be a valid tag value"""
    sanitized = name.lstrip('~')
    if len(sanitized) == 0:
        raise Exception(
            'Cannot use metric name %s as tag value, results in emptry string'
             % name)
    return sanitized


def invert_dict_byindex(d, index=0):
    """ Function that invert data by value index number

    parameters:
    d (dictionary): The content of the text file as dictionary
    index(int): The value index to be inverted

    returns:
    dict: Inverted data by value index number as dictionary
    """
    invert_data = dict()
    for key, value in d.items():
        if value[index].upper() not in invert_data:
            invert_data[value[index].upper()] = [key.upper()]
        else:
            invert_data[value[index].upper()].append(key.upper())
    return invert_data


def resolve_key(obj, key):
    """Resolve key given an object and key."""
    if callable(key):
        return key(obj)
    elif hasattr(obj, 'metadata'):
        return obj.metadata[key]
    raise TypeError(
        'Could not resolve key %r. Key must be callable or %s must have `metadata` attribute.'
         % (key, obj.__class__.__name__))


def _or(*words):
    """(INTERNAL) Join C{words} with C{', '} and C{' or '}.
    """
    t, w = '', list(words)
    if w:
        t = w.pop()
        if w:
            w = ', '.join(w)
            t = ' or '.join((w, t))
    return t


def fpLT(a, b):
    """
    Checks if floating point `a` is less than floating point `b`.
    """
    return a < b


def convert_data_back(data, d, m, n):
    """ Helper function to convert data back to the original shape which is
    determined by the values of d, m and n (outputs of :func: convert_data).
    """
    if d == 2:
        data = data.reshape(m, n)
    return data


@classmethod
def create_options(cls, options):
    """
        Wraps ``options`` into a container class
        (see :attr:`~.DocumentMeta.options_container`).

        :param options: a dictionary of options
        :return: an instance of :attr:`~.DocumentMeta.options_container`
        """
    return cls.options_container(**options)


@classmethod
def geom_col_name(cls):
    """
        Returns the name of the metadata column used to store the feature
        geometry column.
        """
    return 'column_name'


def convert_path(*args, **kwargs):
    """
    Handle converter type "path"

    :param args:
    :param kwargs:
    :return: return schema dict
    """
    schema = {'type': 'string', 'format': 'path'}
    return schema


def set_testmode(testing):
    """Sets the package testing mode.
    """
    global testmode
    testmode = testing


def write_to_ram(sim, trajectory_ram):
    """
    Write configurations to a trajectory in ram.
    """
    trajectory_ram.write(sim.system, sim.current_step)


def format_boolean(value):
    """
    Format a boolean value
    """
    return 'Y' and value or 'N'


def ext_left(data):
    """
    Extract the left half of spectrum.

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

    Returns
    -------
    ndata : ndarray
        Left half of spectrum.

    """
    return data[(...), 0:int(data.shape[-1] / 2.0)]


def print_meta(ds, ds_path=None):
    """Prints meta data for subjects in given dataset."""
    print('\n#' + ds_path)
    for sub, cls in ds.classes.items():
        print('{},{}'.format(sub, cls))
    return


def fix_nframes(nframes, profile):
    """
    # make sure we break at the "true scale" max
    #
    # should this be in terms of fps or nframes?
    """
    fix_nframes_even = nframes == 1 or nframes % 2 == 0
    fix_nframes_sin = nframes == 1 or nframes % 4 != 1
    is_div_four_profile = ('0 to scale to -scale to 0' in profile or
        profile == 'sinusoidal: scale to -scale to scale')
    if profile in ['0 to scale to 0', '-scale to scale to -scale'
        ] and fix_nframes_even:
        nframes_div_2 = nframes // 2
        nframes = 2 * (nframes_div_2 + 1) + 1
    if is_div_four_profile and fix_nframes_sin:
        nframes_div_4 = nframes // 4
        nframes = 4 * (nframes_div_4 + 1) + 1
    return nframes


def create_log_stream(awsclient, log_group_name, log_stream_name):
    """Creates a log stream for the specified log group.

    :param log_group_name: log group name
    :param log_stream_name: log stream name
    :return:
    """
    client_logs = awsclient.get_client('logs')
    response = client_logs.create_log_stream(logGroupName=log_group_name,
        logStreamName=log_stream_name)


def __ne__(A, other):
    """Returns true if both objects are not equal"""
    return not A == other


@staticmethod
def unwrapVerticalities(verticalities):
    """
        Unwraps a sequence of `Verticality` objects into a dictionary of
        `Part`:`Horizontality` key/value pairs.

        >>> score = corpus.parse('bwv66.6')
        >>> scoreTree = score.asTimespans(classList=(note.Note,))
        >>> iterator = scoreTree.iterateVerticalitiesNwise()
        >>> verticalities = next(iterator)
        >>> unwrapped = scoreTree.unwrapVerticalities(verticalities)
        >>> for part in sorted(unwrapped, key=lambda x: x.partName):
        ...     print(part)
        ...     horizontality = unwrapped[part]
        ...     for timespan in horizontality:
        ...         print('\\t%r' % timespan)
        ...
        <music21.stream.Part Alto>
            <PitchedTimespan (0.0 to 1.0) <music21.note.Note E>>
            <PitchedTimespan (1.0 to 2.0) <music21.note.Note F#>>
        <music21.stream.Part Bass>
            <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>>
            <PitchedTimespan (0.5 to 1.0) <music21.note.Note G#>>
            <PitchedTimespan (1.0 to 2.0) <music21.note.Note F#>>
        <music21.stream.Part Soprano>
            <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>>
            <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>>
            <PitchedTimespan (1.0 to 2.0) <music21.note.Note A>>
        <music21.stream.Part Tenor>
            <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>>
            <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>>
            <PitchedTimespan (1.0 to 2.0) <music21.note.Note C#>>
        """
    from music21.tree.verticality import VerticalitySequence
    sequence = VerticalitySequence(verticalities)
    unwrapped = sequence.unwrap()
    return unwrapped


def merge_status(main, other):
    """
    Merge two status tuples.
    """
    new_code = main[0] | other[0]
    new_out = '%s%s' % (main[1], other[1])
    new_err = '%s%s' % (main[2], other[2])
    return new_code, new_out, new_err


def inplace_csr_column_scale(X, scale):
    """Inplace column scaling of a CSR matrix.

    Scale each feature of the data matrix by multiplying with specific scale
    provided by the caller assuming a (n_samples, n_features) shape.

    Parameters
    ----------
    X : CSR matrix with shape (n_samples, n_features)
        Matrix to normalize using the variance of the features.

    scale : float array with shape (n_features,)
        Array of precomputed feature-wise values to use for scaling.
    """
    assert scale.shape[0] == X.shape[1]
    X.data *= scale.take(X.indices, mode='clip')


def __renumber(dictionary):
    """Renumber the values of the dictionary from 0 to n
    """
    count = 0
    ret = dictionary.copy()
    new_values = dict([])
    for key in dictionary.keys():
        value = dictionary[key]
        new_value = new_values.get(value, -1)
        if new_value == -1:
            new_values[value] = count
            new_value = count
            count = count + 1
        ret[key] = new_value
    return ret


def _test():
    """Run doctest on fcrypt module."""
    import doctest, fcrypt
    return doctest.testmod(fcrypt)


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


def range_vs_date_hyph(nsw):
    """Return 'True'1' if nsw looks like a date with a hyphen in."""
    if '-' not in nsw:
        return False
    elif nsw.count('-') == 2:
        return 1
    elif nsw[0] == '0':
        return 1
    else:
        hyph = nsw.find('-')
        first = nsw[:hyph]
        second = nsw[hyph + 1:]
        if not (first.isdigit() and second.isdigit()):
            return False
        elif len(first) != len(second):
            return 0
        elif int(first) > 12 and int(second) > 12 or int(first) > 31 or int(
            second) > 31:
            return 0
        elif len(first) == 1:
            return False
        elif int(second) > int(first[-len(second):]):
            return 0
        else:
            return 1


def reclaimNetworkResources(chute):
    """
    Reclaim network resources for a previously running chute.

    This function only applies to the special case in which pd starts up and
    loads a list of chutes that were running.  This function marks their IP
    addresses and interface names as taken so that new chutes will not use the
    same values.
    """
    pass


def ttt_ratio(ts, target):
    """
    Performs the time to target using the sum of deltas. This approach is similar to calculate the ratio considering
    the first and last point.

    TODO: - This implementation is not considering "recharged"

    TODO: - This implementation doesn't remove outliers

    TODO: - This implementation doesn't trait corner cases (example: slope == 0)

    Parameters
    ----------
    ts : Series
        pandas Series used in the training process
    target : number
        The target

    Returns
    -------
    time_to_target : float
        The number of days predicted to reach the target

    References
    ----------

    Examples
    --------
    >>> ts_test = pd.Series(
    >>>     index=pd.to_datetime(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04', '2017-01-05']),
    >>>     data=[10, 5, 10, 5, 4])
    >>> target = 0
    >>> mdspy.ttt.ttt_ratio(ts_test, target)
    2.666666666
    """
    diffs = sum(ts.diff(1).fillna(0.0))
    delta = ts.tail(1).index[0] - ts.head(1).index[0]
    delta_days = delta.days
    last_value = ts.tail(1).values[0]
    ratio = abs(diffs / delta_days)
    return (last_value - target) / ratio


def parse_chaos_params_dictionary(chaos_parameters_dictionary):
    """
    Parse a list of ChaosParameters dictionary input from string.
    chaos_parameters_dictionary is a list of dictionaries of
    "Key": string
    "Value": a dictionary of a chaos_parameters
    """
    from azure.servicefabric.models import ChaosParametersDictionaryItem
    from sfctl.custom_chaos import parse_chaos_parameters
    if chaos_parameters_dictionary is None:
        return list()
    parsed_dictionary = list()
    for dictionary_entry in chaos_parameters_dictionary:
        key = dictionary_entry.get('Key')
        value = parse_chaos_parameters(dictionary_entry.get('Value'))
        parsed_dictionary.append(ChaosParametersDictionaryItem(key=key,
            value=value))
    return parsed_dictionary


def calculate_cumulative_co2_v1(phases_co2_emissions, phases_distances):
    """
    Calculates cumulative CO2 of cycle phases [CO2g].

    :param phases_co2_emissions:
        CO2 emission of cycle phases [CO2g/km].
    :type phases_co2_emissions: numpy.array

    :param phases_distances:
        Cycle phases distances [km].
    :type phases_distances: numpy.array

    :return:
        Cumulative CO2 of cycle phases [CO2g].
    :rtype: numpy.array
    """
    return phases_co2_emissions * phases_distances


def pick_output_filename(input_filename):
    """Pick a filename for the output file."""
    if input_filename.endswith('.gz'):
        return input_filename[:-len('.gz')] + '.html'
    else:
        return input_filename + '.html'


def configuration(screen):
    """Return column configuration.
        """


def _binary(i, width=4):
    """Convert `i` to a binary string of width `width`.

    >>> _binary(0)
    '0000'
    >>> _binary(1)
    '0001'
    """
    str = bin(i)[2:]
    return '0' * (width - len(str)) + str


def csp_property(key):
    """Return a new property object for a content security policy header.
    Useful if you want to add support for a csp extension in a
    subclass.
    """
    return property(lambda x: x._get_value(key), lambda x, v: x._set_value(
        key, v), lambda x: x._del_value(key), 'accessor for %r' % key)


def printLog(string):
    """
		func: greenPrint
		return: log with green color
		"""
    print('\x1b[32m ' + string + '\x1b[0m')


def check_pes(epi_fmaps, pe_dir):
    """Check whether there are images with matched PE."""
    opposed_pe = False
    matched_pe = False
    for _, fmap_pe in epi_fmaps:
        if fmap_pe == pe_dir:
            matched_pe = True
        elif fmap_pe[0] == pe_dir[0]:
            opposed_pe = True
    if not opposed_pe:
        raise ValueError(
            'None of the discovered fieldmaps has the right phase encoding direction. This is possibly a problem with metadata. If not, rerun with ``--ignore fieldmaps`` to skip the distortion correction step.'
            )
    return matched_pe


def get_node_representation(node, print_lemma=False):
    """
    Transform the node into the proper textual representation,
    as will appear in the extracted contexts.

    :param node: An input Node.
    :param print_lemma: If true, the node lemma is used, otherwise the node form.
    :return: A proper node textual representation for the contexts data.

    """
    if print_lemma:
        output = node.lemma
    else:
        output = node.form
    output = output.lower()
    return output


def loop_select(extend: bool=False, deselect: bool=False, toggle: bool=
    False, ring: bool=False):
    """Select a loop of connected edges

    :param extend: Extend Select, Extend the selection
    :type extend: bool
    :param deselect: Deselect, Remove from the selection
    :type deselect: bool
    :param toggle: Toggle Select, Toggle the selection
    :type toggle: bool
    :param ring: Select Ring, Select ring
    :type ring: bool
    """
    pass


def is_integer_like(val):
    """Returns validation of a value as an integer."""
    try:
        int(val)
        return True
    except (TypeError, ValueError, AttributeError):
        return False


def writestandin(repo, standin, hash, executable):
    """write hash to <repo.root>/<standin>"""
    repo.wwrite(standin, hash + b'\n', executable and b'x' or b'')


def return_index(tup):
    """
    test if second element of tup is not empty
    """
    toreturn = False
    if len(tup[1]) > 0:
        toreturn = True
    return toreturn


def activate_ruleset(rulesengine_db, name):
    """activates ruleset <name>"""
    from src.praxxis.sqlite import connection
    from src.praxxis.util import error
    conn = connection.create_connection(rulesengine_db)
    cur = conn.cursor()
    check_if_active = 'SELECT Active FROM "RulesEngine" WHERE Name = ?'
    cur.execute(check_if_active, (name,))
    result = cur.fetchone()
    if result is None:
        raise error.RulesetNotFoundError(name)
    elif result[0] == 1:
        raise error.RulesetActiveError(name)
    else:
        activate_ruleset = 'UPDATE RulesEngine SET Active = 1 WHERE Name = ?'
        cur.execute(activate_ruleset, (name,))
        conn.commit()
    conn.close()


def sort_field():
    """ The index used to sort
        """


def doc():
    """The form submit could not be validated.
        """


def host_and_page(url):
    """ Splits a `url` into the hostname and the rest of the url. """
    url = url.split('//')[1]
    parts = url.split('/')
    host = parts[0]
    page = '/'.join(parts[1:])
    return host, '/' + page


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


def polybuild_split_at_cursor_move(MESH_OT_polybuild_split_at_cursor=None,
    TRANSFORM_OT_translate=None):
    """Undocumented contribute <https://developer.blender.org/T51061>

    :param MESH_OT_polybuild_split_at_cursor: Poly Build Split at Cursor
    :param TRANSFORM_OT_translate: Move, Move selected items
    """
    pass


def group_membership_request_serializer(membership_request):
    """Membership request information serializer."""
    return {'membership_request_id': membership_request.id, 'user': {
        'user_id': membership_request.user_id, 'name': membership_request.
        user.name}, 'requested_at': str(membership_request.requested_at),
        'group': {'group_id': membership_request.group_id, 'name':
        membership_request.group.name}}


def calc_photosynthesis_rate(T, CO2, light):
    """Calculate the rate of photosynthesis from environment properties.

    Args:
        T (float): Temperature.
        CO2 (float): CO2 concentration.
        light (float): Light intensity.

    Returns:
        float: Photosynthesis rate.

    """
    return light * CO2 / T


@staticmethod
def deserialize(byts, protocol_version):
    """
        Given a bytestring, deserialize into a value according to the protocol
        for this type. Note that this does not create a new instance of this
        class; it merely gives back a value that would be appropriate to go
        inside an instance of this class.
        """
    return byts


@classmethod
def name_contains(cls, name_query: str):
    """Build a filter for authors whose names contain the given query."""
    return cls.name.contains(name_query)


def filter_value(value, float_decimals=None):
    """Filter a float value to certain number of decimals"""
    return round(value, float_decimals)


def static_analisys(application):
    """
    Performs a pyflakes static analysis with the same configuration as
    django CMS testsuite
    """
    try:
        from cms.test_utils.util.static_analysis import pyflakes
        application_module = __import__(application)
        report = pyflakes((application_module,))
        if type(report) == tuple:
            assert report[0] == 0
        else:
            assert report == 0
    except ImportError:
        print(
            """Static analysis available only if django CMS and pyflakes are installed.
Install django-app-helper[pyflakes] to fix this."""
            )


def list_runs(client, year=None, data_periods=None):
    """List ATLAS runs.

	Args:
	    :client: the pyAMI client [ pyAMI.client.Client ]
	    :year: the year [ int | None ]
	    :data_periods: the list of data periods [ list<str> | None ]

	Returns:
	    an array of python dictionnaries.
	"""
    command = ['AMIListRuns']
    if year:
        command.append('-projectName="data%02i%%"' % (year % 100))
    result = []
    if data_periods:
        command.append('')
        for data_period in data_periods:
            command[-1] = '-period="%s"' % data_period
            result.extend(client.execute(command, format='dom_object').
                get_rows())
    else:
        result.extend(client.execute(command, format='dom_object').get_rows())
    return result


def get_value_from_dict_safe(d, key, default=None):
    """
    get the value from dict
    args:
        d: {dict}
        key: {a hashable key, or a list of hashable key}
            if key is a list, then it can be assumed the d is a nested dict
        default: return value if the key is not reachable, default is None
    return:
        value
    """
    assert isinstance(d, dict), f'only supports dict input, {type(d)} is given'
    if isinstance(key, (list, tuple)):
        for _k in key[:-1]:
            if _k in d and isinstance(d[_k], dict):
                d = d[_k]
            else:
                return default
        key = key[-1]
    return d.get(key, default)


def replace_handler(selector, path, *args, **kwargs):
    """
    Replace an existing path handler in the selector
    map with a new handler. Usually we want to add a
    new one, but sometimes we just want to replace.
    This makes replacing easy. Courtesy of Luke Arno,
    the Selector author.

    Example:
    `replace_handler(config['selector'], '/', GET=show, PUT=update)`
    """
    if len(args):
        new_handler = args[0]
    else:
        new_handler = kwargs
    for index, (regex, _) in enumerate(selector.mappings):
        if regex.match(path) is not None:
            selector.mappings[index] = regex, new_handler


@classmethod
def modify_url_for_impersonation(cls, url, impersonate_user, username):
    """
        Modify the SQL Alchemy URL object with the user to impersonate if applicable.
        :param url: SQLAlchemy URL object
        :param impersonate_user: Bool indicating if impersonation is enabled
        :param username: Effective username
        """
    if impersonate_user is not None and username is not None:
        url.username = username


def uint256_from_compact(c):
    """Convert compact encoding to uint256

    Used for the nBits compact encoding of the target in the block header.
    """
    nbytes = c >> 24 & 255
    if nbytes <= 3:
        v = (c & 16777215) >> 8 * (3 - nbytes)
    else:
        v = (c & 16777215) << 8 * (nbytes - 3)
    return v


@staticmethod
def starting_nodes_name(flow_name):
    """Create a starting node name for graph of all failures nodes for generated Python config.

        :param flow_name: flow name for which the starting node should be created
        :return: variable name
        :rtype: str
        """
    return '_%s_failure_starting_nodes' % flow_name


def to_unicode(obj):
    """
    convert a object to unicode string
    """
    return obj.decode('utf-8')


def get_related_association_proxy_model(attr):
    """Returns the model class specified by the given SQLAlchemy relation
    attribute, or ``None`` if no such class can be inferred.

    `attr` must be a relation attribute corresponding to an association proxy.

    """
    prop = attr.remote_attr.property
    for attribute in ('mapper', 'parent'):
        if hasattr(prop, attribute):
            return getattr(prop, attribute).class_
    return None


@classmethod
def extract_keywords(cls, line, items):
    """
        Given the keyword string, parse a dictionary of options.
        """
    unprocessed = list(reversed(line.split('=')))
    while unprocessed:
        chunk = unprocessed.pop()
        key = None
        if chunk.strip() in cls.allowed:
            key = chunk.strip()
        else:
            raise SyntaxError('Invalid keyword: %s' % chunk.strip())
        value = unprocessed.pop().strip()
        if len(unprocessed) != 0:
            for option in cls.allowed:
                if value.endswith(option):
                    value = value[:-len(option)].strip()
                    unprocessed.append(option)
                    break
            else:
                raise SyntaxError('Invalid keyword: %s' % value.split()[-1])
        keyword = '%s=%s' % (key, value)
        try:
            items.update(eval('dict(%s)' % keyword))
        except:
            raise SyntaxError('Could not evaluate keyword: %s' % keyword)
    return items


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

        :param SfTeamJoinFromOobLinkDetails val:
        :rtype: EventDetails
        """
    return cls('sf_team_join_from_oob_link_details', val)


@classmethod
def create_var_keyword(cls, name, *, converter=None, validator=None,
    metadata=None) ->'FParameter':
    """
        A factory method for creating :term:`var-keyword`
        :class:`~forge.FParameter` instances.

        :param name: see :paramref:`~forge.FParameter.name`
        :param converter: see :paramref:`~forge.FParameter.converter`
        :param validator: see :paramref:`~forge.FParameter.validator`
        :param metadata: see :paramref:`~forge.FParameter.metadata`
        """
    return cls(kind=cls.VAR_KEYWORD, name=name, converter=converter,
        validator=validator, metadata=metadata)


def _donothing_func(*args, **kwargs):
    """Perform no good and no bad"""
    pass


def first(iterable, default=None):
    """Return the first element of an iterable or the next element of a generator; or default."""
    try:
        return iterable[0]
    except IndexError:
        return default
    except TypeError:
        return next(iterable, default)


def merge_root_keys(doc1, doc2, exclude=None):
    """
    Ex: d1 = {"_id":1,"a":"a","b":{"k":"b"}}
        d2 = {"_id":1,"a":"A","b":{"k":"B"},"c":123}

        Both documents have the same _id, and 2 root keys, "a" and "b".
        Using this storage, the resulting document will be:

        {'_id': 1, 'a': ['A', 'a'], 'b': [{'k': 'B'}, {'k': 'b'}],"c":123}
    """
    exclude = exclude or []
    for k in exclude:
        doc2.pop(k, None)
    for k1 in doc1:
        if k1 in exclude:
            continue
        v2 = doc2.pop(k1, None)
        if not isinstance(v2, list):
            v2 = [v2]
        if v2:
            if isinstance(doc1[k1], list):
                doc1[k1].extend(v2)
            else:
                doc1[k1] = [doc1[k1]] + v2
    doc1.update(doc2)
    return doc1


def convert_none_to_empty_dict(value):
    """Convert the value to an empty dict if it's None.

    :param value: The value to convert.
    :returns: An empty dict if 'value' is None, otherwise 'value'.
    """
    return {} if value is None else value