def visstd(a, s=0.1):
    """Normalize the image range for visualization"""
    return (a - a.mean()) / max(a.std(), 0.0001) * s + 0.5


def subplot_label(axis, N, plot_idx, varlabels):
    """ Returns a label string for the selected axis """
    label = ''
    if axis == 'x':
        if plot_idx % N == 0:
            label = varlabels[0]
        else:
            label = varlabels[N - plot_idx % N]
    elif axis == 'y':
        label = varlabels[N - 1 - (plot_idx - 1) // N]
    return label


def _unique_parnames(names):
    """
    Given a list of parnames, including possible duplicates, returns a new list
    of parnames with duplicates prepended by one or more underscores to make
    them unique.  This is also case insensitive.
    """
    upper_names = set()
    unique_names = []
    for name in names:
        name_upper = name.upper()
        while name_upper in upper_names:
            name = '_' + name
            name_upper = '_' + name_upper
        unique_names.append(name)
        upper_names.add(name_upper)
    return unique_names


def find_prepositions(chunked):
    """ The input is a list of [token, tag, chunk]-items.
        The output is a list of [token, tag, chunk, preposition]-items.
        PP-chunks followed by NP-chunks make up a PNP-chunk.
    """
    for ch in chunked:
        ch.append('O')
    for i, chunk in enumerate(chunked):
        if chunk[2].endswith('PP') and chunk[-1] == 'O':
            if i < len(chunked) - 1 and (chunked[i + 1][2].endswith(('NP',
                'PP')) or chunked[i + 1][1] in ('VBG', 'VBN')):
                chunk[-1] = 'B-PNP'
                pp = True
                for ch in chunked[i + 1:]:
                    if not (ch[2].endswith(('NP', 'PP')) or ch[1] in ('VBG',
                        'VBN')):
                        break
                    if ch[2].endswith('PP') and pp:
                        ch[-1] = 'I-PNP'
                    if not ch[2].endswith('PP'):
                        ch[-1] = 'I-PNP'
                        pp = False
    return chunked


def copy():
    """Copy this object and return the copy."""


@staticmethod
def get_opt(opt_name, args):
    """Return option name and value.

        :param opt_name: name of the option, e.g., "username"
        :param args: parsed arguments
        """
    return opt_name, getattr(args, 'os_%s' % opt_name, None)


def remove_enclosing_dirs(full_path):
    """ Remove any directories in a filepath

    If you pass a filename to this function, meaning it doesn't
    contain the "/" string, it will simply return the input back to you.

    Args:
        full_path (str): a Unix-based path to a file (with extension)

    Returns:
        filename (string): just the filename (with extension) from the path

    """
    if full_path.find('/') != -1:
        filename = full_path.split('/')[-1]
        return filename
    elif full_path.find('/') == -1:
        filename = full_path
        print(' '.join(['Your path', full_path, 'is already a filename.']))
        return filename
    else:
        print("Something went wrong. Your path isn't a path or a filename.")


@staticmethod
def _getAncestor(adaptor, tokenNames, t, goal):
    """Helper for static inContext."""
    while t is not None:
        name = tokenNames[adaptor.getType(t)]
        if name == goal:
            return t
        t = adaptor.getParent(t)
    return None


def sync_frame_initializer(new_frame, reference_frame):
    """Set the initializers of the new_frame to be the same as the reference_frame,
    for both the default initializer and per-column initializers.

    Parameters
    ----------
    new_frame : Frame
        The frame to set initializers
    reference_frame : Frame
        The frame to copy initializers
    """
    new_frame._default_initializer = reference_frame._default_initializer
    new_frame._initializers = reference_frame._initializers


def message_delete_many(transport, request, queue_name, ids, callback=None):
    """Deletes `ids` messages from `queue_name`

    :param transport: Transport instance to use
    :type transport: `transport.base.Transport`
    :param request: Request instance ready to be sent.
    :type request: `transport.request.Request`
    :param queue_name: Queue reference name.
    :type queue_name: `six.text_type`
    :param ids: Ids of the messages to delete
    :type ids: List of `six.text_type`
    :param callback: Optional callable to use as callback.
        If specified, this request will be sent asynchronously.
        (IGNORED UNTIL ASYNC SUPPORT IS COMPLETE)
    :type callback: Callable object.
    """
    request.operation = 'message_delete_many'
    request.params['queue_name'] = queue_name
    request.params['ids'] = ids
    transport.send(request)


def assemble_urls(year, name, subpages=['']):
    """
    Combines team year, name and subpage strings to generate a list of URLs to
    scrape.
    """
    urls = []
    url_base = 'http://' + year + '.igem.org/Team:' + name
    for s in subpages:
        urls.append(url_base + s)
    return urls


def is_python(text, filename='<string>'):
    """Is this string a valid Python script?"""
    try:
        compile(text, filename, 'exec')
    except (SyntaxError, TypeError):
        return False
    else:
        return True


def __new__(cls, name, value, uom=None, min=None, max=None, context=None,
    contextobj=None, resource=None):
    """Creates new Metric instance.

        :param name: short internal identifier for the value -- appears
            also in the performance data
        :param value: data point, usually has a boolen or numeric type,
            but other types are also possible
        :param uom: :term:`unit of measure`, preferrably as ISO
            abbreviation like "s"
        :param min: minimum value or None if there is no known minimum
        :param max: maximum value or None if there is no known maximum
        :param context: name of the associated context (defaults to the
            metric's name if left out)
        :param contextobj: reference to the associated context object
            (set automatically by :class:`~nagiosplugin.check.Check`)
        :param resource: reference to the originating
            :class:`~nagiosplugin.resource.Resource` (set automatically
            by :class:`~nagiosplugin.check.Check`)
        """
    return tuple.__new__(cls, (name, value, uom, min, max, context or name,
        contextobj, resource))


def transaction_open():
    """open transaction scope"""
    return '    @transaction'


@classmethod
def _form_extension_params_segment(cls, extension_params):
    """
        Convert iterable of extension parameters to str segment for header.

        `extension_params` is an iterable where each item is either a parameter
        string or a (name, value) tuple.
        """
    extension_params_segment = ''
    for item in extension_params:
        try:
            extension_params_segment += ';' + item
        except TypeError:
            param_name, param_value = item
            param_value = cls._escape_and_quote_parameter_value(param_value
                =param_value)
            extension_params_segment += ';' + param_name + '=' + param_value
    return extension_params_segment


def inner_function(partial_fn):
    """Returns the wrapped function of either a partial or curried function."""
    fn = partial_fn.func
    if '__module__' not in dir(fn):
        fn = fn.func
    return fn


@staticmethod
def _refcount(tape):
    """ count number of references to any variables on a tape """
    d = {}
    for record in tape:
        for name, value in record.items():
            uid = id(value)
            d[uid] = d.get(uid, 0) + 1
    return d


def authenticated(user):
    """For authenticated users."""
    return user.is_active and user.is_authenticated


def get_request_and_username(context):
    """
    Returns request object and username
    """
    request = context['request']
    view = context['view']
    username = view.kwargs.get('username')
    if not username:
        username = request.user and request.user.username
    return request, username


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

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

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


def select_assessor(xnat, assessor_dict):
    """
    Select the assessor pyxnat Eobject from the assessor dictionary information

    :param xnat: pyxnat.interface object
    :param assessor_dict: assessor dictionary
    :return: assessor pyxnat Eobject
    """
    return xnat.select_assessor(assessor_dict['project_id'], assessor_dict[
        'subject_label'], assessor_dict['session_label'], assessor_id=
        assessor_dict['label'])


def p_case_statement(t):
    """case_statement : CASE value COLON"""
    t[0] = [t[2]]


def bool_attrib(element, attr):
    """
    Cast an XML attribute that should be a boolean to a Python equivalent
    None, 'f', '0' and 'false' all cast to False, everything else to true
    """
    value = element.get(attr)
    if not value or value in ('false', 'f', '0'):
        return False
    return True


def reset_objective(problem):
    """
    Clears all the objective coefficients for the current problem
    by setting all to 0
    """
    names = problem.variables.get_names()
    zeros = [(0) for x in names]
    problem.objective.set_linear(zip(names, zeros))
    problem.objective.set_name('none')


def __new__(cls, *args, **kwargs):
    """ This is required to make the class a singleton class """
    obj = super().__new__(cls, *args, **kwargs)
    obj.__dict__ = cls._shared_state
    return obj


@classmethod
def from_json(cls, json):
    """Builds a LocationAttachment object from JSON provided by the API.

        Args:
            json: A dict containing the JSON representation that is converted
                  into the LocationAttachment object.

        Returns:
            A LocationAttachment object.

        """
    return cls(json['payload']['coordinates']['lat'], json['payload'][
        'coordinates']['long'])


def index_choice0(funcname, args):
    """
    index_choice(status, "live", "not-live", "decommissioned")
    """
    index = args.pop(0)
    s = f'CASE {index} '
    for i, a in enumerate(args):
        s += f'WHEN {i} THEN {a} '
    s += 'END'
    return s


def iterbytes(b):
    """Iterate over bytes, returning bytes instead of ints (python3)"""
    if isinstance(b, memoryview):
        b = b.tobytes()
    i = 0
    while True:
        a = b[i:i + 1]
        i += 1
        if a:
            yield a
        else:
            break


@classmethod
def run_hooks(cls, hook_type, *args, **kwargs):
    """Run all hooks of specified type.

        :param cls: class that registers hooks
        :param hook_type: hook type, e.g., '__pre_parse_args__'
        :param args: args to be passed to every hook function
        :param kwargs: kwargs to be passed to every hook function
        """
    hook_funcs = cls._hooks_map.get(hook_type) or []
    for hook_func in hook_funcs:
        hook_func(*args, **kwargs)


def k_AP(k_obs, mu_obs, alpha_perp, alpha_par):
    """
    Return the `true` k values, given an observed (k, mu).

    This corresponds to the `k` to evaluate P(k,mu) at when including
    the AP effect
    """
    F = alpha_par / alpha_perp
    if F != 1.0:
        return k_obs / alpha_perp * (1 + mu_obs ** 2 * (1.0 / F ** 2 - 1)
            ) ** 0.5
    else:
        return k_obs / alpha_perp


def reset_recent():
    """Reset Recent files

    """
    pass


def set_is_pallets_theme(app):
    """Set the ``is_pallets_theme`` config to ``True`` if the current
    theme is a decedent of the ``pocoo`` theme.
    """
    if app.config.is_pallets_theme is not None:
        return
    theme = getattr(app.builder, 'theme', None)
    while theme is not None:
        if theme.name == 'pocoo':
            app.config.is_pallets_theme = True
            break
        theme = theme.base
    else:
        app.config.is_pallets_theme = False


def Font(*args, **metadata):
    """ Returns a trait whose value must be a GUI toolkit-specific font.

    Description
    -----------
    For wxPython, the returned trait accepts any of the following:

    * a wx.Font instance
    * a wx.FontPtr instance
    * a string describing the font, including one or more of the font family,
      size, weight, style, and typeface name.

    Default Value
    -------------
    For wxPython, 'Arial 10'
    """
    from traitsui.toolkit_traits import FontTrait
    return FontTrait(*args, **metadata)


def get_time(timeformat='%m-%d-%y_%T%p', raw=False, filename_friendly=False,
    replacement_seperator='-'):
    """
    Gets current time in local time zone.
    if raw: True then raw datetime object returned without formatting.
    if filename_friendly: replace ':' with replacement_separator
    """
    from datetime import datetime
    from pytz import timezone
    from tzlocal import get_localzone
    now_utc = datetime.now(timezone('UTC'))
    now_local = now_utc.astimezone(get_localzone())
    if raw == True:
        return now_local
    else:
        now = now_local.strftime(timeformat)
    if filename_friendly == True:
        return now.replace(':', replacement_seperator).lower()
    else:
        return now


@staticmethod
def _coerce_grace(grace):
    """
        returns a grace time that respects the hc api
        """
    grace = max(60, grace)
    grace = min(grace, 2592000)
    return grace


def cache_tree_children(queryset):
    """
    For all items in the queryset, set the '_cached_children' attribute to a
    list. This attribute is in turn used by the 'get_children' method on the
    item, which would otherwise (if '_cached_children' is not set) cause a
    database query.

    The queryset must be ordered by 'lft', or the function will put the children
    in the wrong order.
    """
    parents_dict = {}
    for obj in queryset:
        parents_dict[obj.pk] = obj
        obj._cached_children = []
    for obj in queryset:
        parent = parents_dict.get(obj.parent_id)
        if parent:
            parent._cached_children.append(obj)


@staticmethod
def p_a2ml_block_definition(p):
    """a2ml_block_definition : block a2ml_tag a2ml_type_name"""


@classmethod
def get_next(cls, prefix=None):
    """Return the next available ID in a repository."""
    id_ = cls.get_latest(prefix)
    if id_:
        return id_.incr()
    elif prefix:
        return cls(id_text=cls.FORMAT_IDX % (prefix, cls.SID_0))
    else:
        return cls(id_text=cls.SID_0)


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


def STL_receiver(rec_param, hemisphere='North'):
    """
    hemishpere : 'North' or 'South' hemisphere of the earth where the field located
                if North: the field is in the positive y direction
                if South: the field is in the negtive y direction
                this will influence:
                 (1) the setting of the receiver tilt angle,
                     if the front surface always facing to the field is desirable
                 (2) the position of the virtual target
    """
    rec_w = rec_param[0].astype(float)
    rec_h = rec_param[1].astype(float)
    stlfile = rec_param[2]
    x = rec_param[3].astype(float)
    y = rec_param[4].astype(float)
    z = rec_param[5].astype(float)
    tilt = rec_param[6].astype(float)
    entt = ''
    entt += '\n- entity:\n'
    entt += '    name: STL_receiver_e\n'
    entt += '    primary: 0\n'
    if hemisphere == 'North':
        entt += '    transform: { translation: %s, rotation: %s }\n' % ([x,
            y, z], [-90.0 - tilt, 0, 0])
    else:
        entt += '    transform: { translation: %s, rotation: %s }\n' % ([x,
            y, z], [180.0 + tilt, 0, 0])
    entt += '    geometry:\n'
    entt += '    - material: *material_target\n'
    entt += '      transform: {translation: [0, 0, 0], rotation: [0, 0, 0]}\n'
    entt += '      stl : {path: %s }  \n' % stlfile
    pts = [[-rec_w * 10.0, -rec_h * 10.0], [-rec_w * 10.0, rec_h * 10.0], [
        rec_w * 10.0, rec_h * 10.0], [rec_w * 10.0, -rec_h * 10.0]]
    slices = 4
    entt += '\n- entity:\n'
    entt += '    name: virtual_target_e\n'
    entt += '    primary: 0\n'
    if hemisphere == 'North':
        entt += '    transform: { translation: %s, rotation: %s }\n' % ([x,
            y - 5.0, z], [-90.0 - tilt, 0, 0])
    else:
        entt += '    transform: { translation: %s, rotation: %s }\n' % ([x,
            y + 5.0, z], [90.0 + tilt, 0, 0])
    entt += '    geometry: \n'
    entt += '      - material: *%s\n' % 'material_virtual'
    entt += '        plane: \n'
    entt += '          clip: \n'
    entt += '          - operation: AND \n'
    entt += '            vertices: %s\n' % pts
    entt += '          slices: %d\n' % slices
    rcv = ''
    rcv += '- name: STL_receiver_e \n'
    rcv += '  side: %s \n' % 'FRONT_AND_BACK'
    rcv += '  per_primitive: %s \n' % 'INCOMING_AND_ABSORBED'
    rcv += '- name: virtual_target_e \n'
    rcv += '  side: %s \n' % 'FRONT'
    rcv += '  per_primitive: %s \n' % 'INCOMING'
    return entt, rcv


@classmethod
def get_available_fields(cls, model, db_fields_only=False):
    """
        Returns a list of all the fields that can be used in the API for the
        specified model class.

        Setting db_fields_only to True will remove all fields that do not have
        an underlying column in the database (eg, type/detail_url and any custom
        fields that are callables)
        """
    fields = cls.get_body_fields_names(model) + cls.get_meta_fields_names(model
        )
    if db_fields_only:
        database_fields = set()
        for field in model._meta.get_fields():
            database_fields.add(field.name)
            if hasattr(field, 'attname'):
                database_fields.add(field.attname)
        fields = [field for field in fields if field in database_fields]
    return fields


def h2d(h):
    """Convert hours into degrees."""
    return h * 15.0


def first(iterable):
    """
    Returns the first item returned by iterating over an iterable object.

    Example:

    >>> a = [1, 2, 3]
    >>> first(a)
    1
    """
    return next(iter(iterable))


@staticmethod
def should_halt(seq, stream, values, context, loaded_fields):
    """Determine if the deserializer should stop reading from the input.

        This function should return ``True`` to indicate loading for this field
        should stop, or ``False`` to continue adding elements.

        The default implementation does the following:

        - If ``count`` is an integer, it compares ``count`` against the length
          of ``values``. If ``len(values)`` is equal to or more than ``count``
          it'll return ``True`` (halt), ``False`` otherwise.
        - If ``count`` is a :class:`~binobj.fields.base.Field`, that field should
          already have been loaded and in ``loaded_fields``. The expected array
          size is taken from there, and compared as above.
        - If ``count`` is a string, it's the name of a field already loaded and
          in ``loaded_fields``. The expected array size is taken from there, and
          compared as above.
        - Otherwise, the function assumes the array ends at EOF and only returns
          ``True`` if there's no more data in the stream.

        Subclasses' implementations must handle all four cases.

        :param Array seq:
            The sequence being checked.
        :param io.BufferedIOBase stream:
            The data stream to read from. Except in rare circumstances, this is
            the same stream that was passed to :meth:`.from_stream`. The stream
            pointer should be returned to its original position when the function
            exits.
        :param list values:
            A list of the objects that have been deserialized so far. In general
            this function *should not* modify the list. A possible exception to
            this rule is to remove a sentinel value from the end of the list.
        :param context:
            The ``context`` object passed to :meth:`.from_stream`.
        :param dict loaded_fields:
            The fields in the struct that have been loaded so far.

        :return: ``True`` if the deserializer should stop reading, ``False``
            otherwise.
        :rtype: bool
        """
    if seq.count is not None:
        return seq.get_final_element_count(loaded_fields) <= len(values)
    offset = stream.tell()
    try:
        return stream.read(1) == b''
    finally:
        stream.seek(offset)


def get_collected_details(event):
    """Gets collected details if the technology's poller already described the given asset"""
    return event['detail'].get('collected')


def error(__self, __message, *args, **kwargs):
    """Log ``message.format(*args, **kwargs)`` with severity ``'ERROR'``."""
    __self._log('ERROR', None, False, __self._options, __message, args, kwargs)


def convert_to_percentage(counts, length):
    """Convert dictionary of occurrences to percentages"""
    for key in counts:
        counts[key] /= length
    return counts


@classmethod
def GetParserObjectByName(cls, parser_name):
    """Retrieves a specific parser object by its name.

    Args:
      parser_name (str): name of the parser.

    Returns:
      BaseParser: parser object or None.
    """
    parser_class = cls._parser_classes.get(parser_name, None)
    if parser_class:
        return parser_class()
    return None


def do_float(value, default=0.0):
    """Convert the value into a floating point number. If the
    conversion doesn't work it will return ``0.0``. You can
    override this default using the first parameter.
    """
    try:
        return float(value)
    except (TypeError, ValueError):
        return default


def multiply_by(X, factor):
    """
    Multiplies X by a given factor
    """
    return X * factor


def boolean_yes_no(string):
    """Convert `"yes"`, `"no"` to boolean `True`, `False`."""
    return string == 'yes'


def update_groupings(obj):
    """ Update the groupings dict with the values stored on obj.
        """


def ballot_one(ranking):
    """Ballot for the voter's preferred candidate.

    Parameters
    ----------
    ranking : str
        A ranking.

    Returns
    -------
    str
        The first candidate.

    Examples
    --------
        >>> ballot_one('abc')
        'a'
    """
    return ranking[0]


def is_scheduled(num_words, frequency, words_per_iteration):
    """Checks if an event is scheduled to be performed within given number
    of updates after this point.

    For example, word_per_iteration=9, frequency=2:

    num_words:      1   2   3   4  [5]  6   7   8  [9] 10  11  12
    * frequency:    2   4   6   8  10  12  14  16  18  20  22  24
    modulo:         2   4   6   8   1   3   5   7   0   2   4   6

    :type num_words: int
    :param num_words: number of words so far

    :type frequency: int
    :param frequency: how many times per iteration the event should be performed

    :type words_per_iteration: int
    :param words_per_iteration: number of words in one iteration

    :rtype: bool
    :returns: whether the operation is scheduled to be performed
    """
    modulo = num_words * frequency % words_per_iteration
    return modulo < frequency


def click_extrude():
    """Create a new bone going from the last selected joint to the mouse position

    """
    pass


def chars_to_ranges(s):
    """
    Return a list of character codes consisting of pairs
    [code1a, code1b, code2a, code2b,...] which cover all
    the characters in |s|.
    """
    char_list = list(s)
    char_list.sort()
    i = 0
    n = len(char_list)
    result = []
    while i < n:
        code1 = ord(char_list[i])
        code2 = code1 + 1
        i += 1
        while i < n and code2 >= ord(char_list[i]):
            code2 += 1
            i += 1
        result.append(code1)
        result.append(code2)
    return result


def build_lcms_70(compiler):
    """Link error here on x64"""
    if compiler['platform'] == 'x64':
        return ''
    """Build LCMS on VC2008. This version is only 32bit/Win32"""
    return (
        """
rem Build lcms2
setlocal
set LCMS=%%LCMS-2.7%%
rd /S /Q %%LCMS%%\\Lib
rd /S /Q %%LCMS%%\\Projects\\VC%(vc_version)s\\Release
%%MSBUILD%% %%LCMS%%\\Projects\\VC%(vc_version)s\\lcms2.sln /t:Clean /p:Configuration="Release" /p:Platform=Win32 /m
%%MSBUILD%% %%LCMS%%\\Projects\\VC%(vc_version)s\\lcms2.sln /t:lcms2_static /p:Configuration="Release" /p:Platform=Win32 /p:PlatformToolset=v90 /m
xcopy /Y /E /Q %%LCMS%%\\include %%INCLIB%%
copy /Y /B %%LCMS%%\\Lib\\MS\\*.lib %%INCLIB%%
endlocal
"""
         % compiler)


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

    The ".dev0" means dirty.

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


def 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 put_role_policy(RoleName=None, PolicyName=None, PolicyDocument=None):
    """
    Adds or updates an inline policy document that is embedded in the specified IAM role.
    When you embed an inline policy in a role, the inline policy is used as part of the role's access (permissions) policy. The role's trust policy is created at the same time as the role, using  CreateRole . You can update a role's trust policy using  UpdateAssumeRolePolicy . For more information about IAM roles, go to Using Roles to Delegate Permissions and Federate Identities .
    A role can also have a managed policy attached to it. To attach a managed policy to a role, use  AttachRolePolicy . To create a new managed policy, use  CreatePolicy . For information about policies, see Managed Policies and Inline Policies in the IAM User Guide .
    For information about limits on the number of inline policies that you can embed with a role, see Limitations on IAM Entities in the IAM User Guide .
    See also: AWS API Documentation

    Examples
    The following command adds a permissions policy to the role named Test-Role.
    Expected Output:

    :example: response = client.put_role_policy(
        RoleName='string',
        PolicyName='string',
        PolicyDocument='string'
    )

    :type RoleName: string
    :param RoleName: [REQUIRED]
            The name of the role to associate the policy with.
            This parameter allows (per its regex pattern ) a string of characters consisting of upper and lowercase alphanumeric characters with no spaces. You can also include any of the following characters: _+=,.@-

    :type PolicyName: string
    :param PolicyName: [REQUIRED]
            The name of the policy document.
            This parameter allows (per its regex pattern ) a string of characters consisting of upper and lowercase alphanumeric characters with no spaces. You can also include any of the following characters: =,.@-

    :type PolicyDocument: string
    :param PolicyDocument: [REQUIRED]
            The policy document.
            The regex pattern used to validate this parameter is a string of characters consisting of any printable ASCII character ranging from the space character (u0020) through end of the ASCII character range as well as the printable characters in the Basic Latin and Latin-1 Supplement character set (through u00FF). It also includes the special characters tab (u0009), line feed (u000A), and carriage return (u000D).

    :return: response = client.put_role_policy(
        PolicyDocument='{"Version":"2012-10-17","Statement":{"Effect":"Allow","Action":"s3:*","Resource":"*"}}',
        PolicyName='S3AccessPolicy',
        RoleName='S3Access',
    )

    print(response)

    """
    pass


@classmethod
def insstr(cls, s, x=None, y=None, attr=None):
    """
        Insert a string at cursor or specified position.
        Cursor is not moved and characters to the right are shifted right.

        :param s: the string
        :param x: optional x value
        :param y: optional y value
        :param attr: optional attributes
        """
    x, y = cls._fix_xy(x, y)
    attr = cls._fix_attr(attr)
    if attr is None:
        return cls.WINDOW.insstr(y, x, s)
    else:
        return cls.WINDOW.insstr(y, x, s, attr)


def set_default_app(app):
    """Set default app."""
    global default_app
    default_app = app


def get_trail_by_arn(cloudtrail_client, trail_arn):
    """Gets trail information based on the trail's ARN"""
    trails = cloudtrail_client.describe_trails()['trailList']
    for trail in trails:
        if trail.get('TrailARN', None) == trail_arn:
            return trail
    raise ValueError('A trail could not be found for %s' % trail_arn)


def translate_toks(a_toks, a_translation):
    """Translate tokens and return translated set.

    Args:
      a_toks (iterable): tokens to be translated
      a_translation (dict): - translation dictionary for tokens

    Returns:
      frozenset: translated tokens

    """
    if a_translation is None:
        return a_toks
    ret = set()
    for tok in a_toks:
        for t_tok in a_translation[tok]:
            ret.add(t_tok)
    return frozenset(ret)


def hyper_loop(n, m):
    """
    An nested for loop
    :param n: The size of the samples
    :param m: The numbers in the sample 0..m
    :return:
    """
    idx = []
    for k in range(m ** n):
        for _ in range(n):
            idx.append(k % m)
            k //= m
            if len(idx) == n:
                yield idx[::-1]
                idx.clear()


def view_frame():
    """Reset viewable area to show range around current frame

    """
    pass


def attachment_moved(attachment, old_parent_realm, old_parent_id, old_filename
    ):
    """Called when an attachment is moved."""


def register_handler(handler):
    """
    Install application-specific GRIB image handler.

    :param handler: Handler object.
    """
    global _handler
    _handler = handler


def _astnode(s):
    """Return a Python ast Node compiled from a string."""
    try:
        import ast
    except ImportError:
        return eval(s)
    p = ast.parse('__tempvalue__ = ' + s)
    return p.body[0].value


def get_tags_for_update(**kwargs):
    """Helper to check tags changes"""
    params = {}
    for k, v in kwargs.items():
        if not v:
            continue
        if not isinstance(v, list):
            raise ValueError('Add/remove field value must be a list')
        params[k] = v
    return params


def sure_number_block(ctx, n):
    """The number of good Rosser blocks needed to apply
    Turing method
    References:
    R. P. Brent, On the Zeros of the Riemann Zeta Function
    in the Critical Strip, Math. Comp. 33 (1979) 1361--1372
    T. Trudgian, Improvements to Turing Method, Math. Comp."""
    if n < 9 * 10 ** 5:
        return 2
    g = ctx.grampoint(n - 100)
    lg = ctx._fp.ln(g)
    brent = 0.0061 * lg ** 2 + 0.08 * lg
    trudgian = 0.0031 * lg ** 2 + 0.11 * lg
    N = ctx.ceil(min(brent, trudgian))
    N = int(N)
    return N


def replace_widget_in_layout(old_widget, new_widget, retain_font=True,
    retain_sizePolicy=True, retain_size=True):
    """
    Replace a widget with a new widget instance in an existing layout.

    returns new_widget

    Useful for replacing placeholder widgets created with QtCreator
    with custom widgets
    Note: currently works for widgets in  QGridLayouts only

    inspiration from:
    -- http://stackoverflow.com/questions/4625102/how-to-replace-a-widget-with-another-using-qt
    -- http://stackoverflow.com/questions/24189202/how-to-get-the-row-column-location-of-a-widget-in-a-qgridlayout
    """
    layout = old_widget.parentWidget().layout()
    index = layout.indexOf(old_widget)
    row, column, rowSpan, colSpan = layout.getItemPosition(index)
    if retain_font:
        new_widget.setFont(old_widget.font())
    if retain_sizePolicy:
        new_widget.setSizePolicy(old_widget.sizePolicy())
    if retain_size:
        new_widget.setMinimumSize(old_widget.minimumSize())
        new_widget.setMaximumSize(old_widget.maximumSize())
    layout.removeWidget(old_widget)
    old_widget.close()
    layout.addWidget(new_widget, row, column, rowSpan, colSpan)
    layout.update()
    return new_widget


@staticmethod
def _validate_date(date):
    """validate date has format of YYYY-MM-DD"""
    date_list = date.split('-')
    if len(date_list) == 3:
        if len(date_list[0]) == 4:
            if len(date_list[1]) == 2 and len(date_list[2]) == 2:
                return True
    raise Exception("format should be 'YYYY-MM-DD'")


def get_random_choice(max=1):
    """
    get random choice
    use random.choice
    :param max:int
    :return:int
    """
    import random
    return random.choice(range(max))


@classmethod
def unfinished(cls):
    """
        A list of states indicating that a task either has not completed
        a run or has not even started.
        """
    return [cls.NONE, cls.SCHEDULED, cls.QUEUED, cls.RUNNING, cls.SHUTDOWN,
        cls.UP_FOR_RETRY]


@staticmethod
def _split_not_in_par(string, sep):
    """
        Split string at all occurences of sep but not inside of a parenthesis
        """
    result = []
    count = 0
    split = []
    for char in string:
        if char == '(':
            count += 1
        elif char == ')':
            count -= 1
        if char == sep and count == 0:
            result.append(''.join(split))
            split = []
        else:
            split.append(char)
    if split:
        result.append(''.join(split))
    return result


def hmc_split(in_file, in_bval, ref_num=0, lowbval=5.0):
    """
    Selects the reference and moving volumes from a dwi dataset
    for the purpose of HMC.
    """
    import numpy as np
    import nibabel as nb
    import os.path as op
    from nipype.interfaces.base import isdefined
    im = nb.load(in_file)
    data = im.get_data()
    hdr = im.header.copy()
    bval = np.loadtxt(in_bval)
    lowbs = np.where(bval <= lowbval)[0]
    volid = lowbs[0]
    if isdefined(ref_num) and ref_num < len(lowbs):
        volid = ref_num
    if volid == 0:
        data = data[(...), 1:]
        bval = bval[1:]
    elif volid == data.shape[-1] - 1:
        data = data[(...), :-1]
        bval = bval[:-1]
    else:
        data = np.concatenate((data[(...), :volid], data[(...), volid + 1:]
            ), axis=3)
        bval = np.hstack((bval[:volid], bval[volid + 1:]))
    out_ref = op.abspath('hmc_ref.nii.gz')
    out_mov = op.abspath('hmc_mov.nii.gz')
    out_bval = op.abspath('bval_split.txt')
    refdata = data[..., volid]
    hdr.set_data_shape(refdata.shape)
    nb.Nifti1Image(refdata, im.affine, hdr).to_filename(out_ref)
    hdr.set_data_shape(data.shape)
    nb.Nifti1Image(data, im.affine, hdr).to_filename(out_mov)
    np.savetxt(out_bval, bval)
    return [out_ref, out_mov, out_bval, volid]


def convert():
    """
    Convert a pdf to text

    Parameters:
    path (str): The path to the PDF file

    Returns:
    str: The content of the PDF file
    """
    print('pdf2text')


@classmethod
def _create_and_score_row_stacks(cls, primary_objects, secondary_objects,
    score_function, _ezmk_score_format, *args, **kwargs):
    """Create a list of dicts containing each pairing along with a list of each score."""
    _ezmk_score_formats = {'Normal': [['list_of_lists_of_pair_scores'], [
        'list_of_lists_of_pair_dict']], '&SingleDict': [[
        'list_of_lists_of_pair_scores'], ['list_of_lists_of_pair_dict'], [
        'list_of_lists_of_score_primary_secondary_and_dict']]}
    score_methods = cls._score_methods_internal
    _ezmk_score_exports = tuple([] for _ in _ezmk_score_formats[
        _ezmk_score_format])
    for p_object in primary_objects:
        _ezmk_scores_this_row = tuple([] for _ in _ezmk_score_exports)
        for s_object in secondary_objects:
            new_pair = {cls._primary_default_name: p_object, cls.
                _secondary_default_name: s_object}
            try:
                score = cls._get_score(score_methods, score_function,
                    new_pair, *args, **kwargs)
            except AssertionError:
                raise
            cls._row_append(_ezmk_scores_this_row, [score, new_pair])
        for _ezmk_row_xp_id, _ezmk_row_xp_list in enumerate(
            _ezmk_scores_this_row):
            _ezmk_score_exports[_ezmk_row_xp_id].append(_ezmk_row_xp_list)
    return tuple(_ezmk_score_exports)


def _fix_pdb_ins_code(table):
    """
    Utility that fixes the 'pdbx_PDB_ins_code' column to match what is expected
    in the mmCIF format.

    :param table: pandas DataFrame object
    :return: returns a modified pandas DataFrame
    """
    table['pdbx_PDB_ins_code'] = table['pdbx_PDB_ins_code'].str.replace('\\ |',
        '?')
    table['pdbx_PDB_ins_code'] = table['pdbx_PDB_ins_code'].fillna('?').astype(
        str)
    return table


def pytest_testnodedown(node, error):
    """ Test Node is down. """


def _filter_stretch(barcode, stretches):
    """Test whether {barcode} contains none of the stretches in {stretches}.

    :arg str barcode: A barcode.
    :arg list stretches:

    :returns bool: True if the barcode is clean, False otherwise.
    """
    for i in stretches:
        if i in barcode:
            return False
    return True


def _line_ranges(statements, lines):
    """Produce a list of ranges for `format_lines`."""
    statements = sorted(statements)
    lines = sorted(lines)
    pairs = []
    start = None
    lidx = 0
    for stmt in statements:
        if lidx >= len(lines):
            break
        if stmt == lines[lidx]:
            lidx += 1
            if not start:
                start = stmt
            end = stmt
        elif start:
            pairs.append((start, end))
            start = None
    if start:
        pairs.append((start, end))
    return pairs


def set_configuration(configuration):
    """
        Set the configuration for a given tile
        """


def is_keyed_tuple(obj):
    """Return True if ``obj`` has keyed tuple behavior, such as
    namedtuples or SQLAlchemy's KeyedTuples.
    """
    return isinstance(obj, tuple) and hasattr(obj, '_fields')


@staticmethod
def load_dict(file_path: str, encoding: str='utf-8', delimiter: str='\t'
    ) ->dict:
    """
        文件中包含多行，每个多个元素，以 delimiter代表的字符作为分割，载入为dict
        :param file_path:
        :param encoding:
        :param delimiter:
        :return:
        """
    data = {}
    inf = open(file_path, 'r', encoding=encoding)
    for line in inf:
        line = line.strip()
        box = line.split(delimiter)
        if len(box) == 2:
            data[box[0]] = box[1]
    inf.close()
    return data


def _fill_msa_matrix(msa_matrix, chimerics, msa, seq_index,
    transform_subexon_index=lambda index: index):
    """Function to fill msa_matrix with msa data."""
    record = msa[seq_index]
    subexon2len = chimerics[record.id][1]
    subexons = sorted(subexon2len, key=subexon2len.get)
    seq_len = 0
    subexon_index = 0
    for col_index in range(0, msa_matrix.shape[1]):
        residue = record.seq[col_index]
        if residue != '-':
            seq_len += 1
        if seq_len > subexon2len[subexons[subexon_index]]:
            subexon_index += 1
        if residue not in {'-', 'X'}:
            msa_matrix[seq_index, col_index] = transform_subexon_index(subexons
                [subexon_index])


def convert_attr_name(s):
    """Convert all underline in string name to space and capitalize
    """
    return ' '.join(map(str.capitalize, s.strip().split('_')))


def msgpack_init(**kwargs):
    """Init parameters of msgpack packer and unpacker.
    Usage:
        >>> msgpack_init(pack_encoding='utf-8')
    :param kwargs: See http://pythonhosted.org/msgpack-python/api.html
            default:
            pack_encoding='utf-8'
            pack_params=dict()
            unpack_encoding='utf-8'
            unpack_params=dict(use_list=False)
    :return: None
    """
    global _pack_encoding, _pack_params, _unpack_encoding, _unpack_params
    _pack_encoding = kwargs.pop('pack_encoding', 'utf-8')
    _pack_params = kwargs.pop('pack_params', dict())
    _unpack_encoding = kwargs.pop('unpack_encoding', 'utf-8')
    _unpack_params = kwargs.pop('unpack_params', dict(use_list=False))


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

        :param FileTransfersTransferSendType val:
        :rtype: EventType
        """
    return cls('file_transfers_transfer_send', val)


@classmethod
def is_error(cls, api_response):
    """
        Check if the API response is or contain an error.

        :param dict api_response: The API response.

        :rtype: bool
        """
    if api_response and isinstance(api_response, dict):
        if '@type' in api_response and api_response['@type'] == 'error':
            return True
    return False


def post_hook():
    """A plugin hook called in the second initialization step.

        This is called after the database is initialized.
        """


def unquote(s):
    """
    Undo the effects of quote(). Based heavily on urllib.unquote().
    """
    mychr = chr
    myatoi = int
    list = s.split('_')
    res = [list[0]]
    myappend = res.append
    del list[0]
    for item in list:
        if item[1:2]:
            try:
                myappend(mychr(myatoi(item[:2], 16)) + item[2:])
            except ValueError:
                myappend('_' + item)
        else:
            myappend('_' + item)
    return ''.join(res)


def sqrt(rv):
    """
    Returns the square root of a random variable
    """
    return rv ** 0.5


@classmethod
def available(cls):
    """returns True if the AudioOutput is available on the system"""
    return False


def theta_Cs_H_PK74(T, P):
    """c-c': caesium hydrogen [PK74]."""
    theta = -0.044
    valid = T == 298.15
    return theta, valid


def getWeight(grph, nd1, nd2, weightString='weight', returnType=int):
    """
    A way of getting the weight of an edge with or without weight as a parameter
    returns a the value of the weight parameter converted to returnType if it is given or 1 (also converted) if not
    """
    if not weightString:
        return returnType(1)
    else:
        return returnType(grph.edges[nd1, nd2][weightString])


def get_org_from_analyzer_name(analyzer_name: str) ->str:
    """Given a '/' separated name of analyzer, e.g. r2c/typeflow, returns the org name which is 'r2c'
    """
    names = analyzer_name.split('/')
    assert len(names
        ) == 2, f'Make sure you specified org and analyzer_name as `org/analyzer_name` in your analyzer.json'
    return names[0]


def are_floats(items):
    """
    detect if all items are floats
    """
    for i in items:
        try:
            float(i) if i is not None and len(i) > 0 else None
        except ValueError:
            return False
    return True


@staticmethod
def format_sizeof(num, suffix='', divisor=1000):
    """
        Formats a number (greater than unity) with SI Order of Magnitude
        prefixes.

        Parameters
        ----------
        num  : float
            Number ( >= 1) to format.
        suffix  : str, optional
            Post-postfix [default: ''].
        divisor  : float, optional
            Divisor between prefixes [default: 1000].

        Returns
        -------
        out  : str
            Number with Order of Magnitude SI unit postfix.
        """
    for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']:
        if abs(num) < 999.5:
            if abs(num) < 99.95:
                if abs(num) < 9.995:
                    return '{0:1.2f}'.format(num) + unit + suffix
                return '{0:2.1f}'.format(num) + unit + suffix
            return '{0:3.0f}'.format(num) + unit + suffix
        num /= divisor
    return '{0:3.1f}Y'.format(num) + suffix


@classmethod
def remove_builder(cls, builder_name: str):
    """Remove a registered builder `builder_name`.

        No reason to use this except for tests.
        """
    cls.builders.pop(builder_name, None)
    for hook_spec in cls.hooks.values():
        hook_spec.pop(builder_name, None)


def left(sp, row_index):
    """ Given a SkewPartition and a 0-based row index, return the 0-based column index of the *leftmost* cell in the corresponding row.  (Section 2.1 of [mem]_)

    EXAMPLES::

        sage: left(SkewPartition([[4, 1], [2]]), 0)
        2

        sage: left(SkewPartition([[4, 1], [2]]), 1)
        0

    An input index that is out of the bounds of the skew partition will throw an error::

        sage: left(SkewPartition([[4, 1], [2]]), 2)
        IndexError: list index out of range

    An in-bounds index where no cells exist will return ``None``::

        sage: left(SkewPartition([[2, 1, 1, 1], [1, 1]]), 1) == None
        True

    ..  SEEALSO::

        :meth:`right`, :meth:`top`, :meth:`bottom`
    """
    if sp.row_lengths()[row_index] == 0:
        return None
    outer_row_lengths = sp.outer().to_list()
    inner_row_lengths = sp.inner().to_list()
    if row_index in range(len(outer_row_lengths)) and row_index not in range(
        len(inner_row_lengths)):
        inner_row_length = 0
    else:
        inner_row_length = inner_row_lengths[row_index]
    col_index = inner_row_length
    return col_index


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 _tokenizer(data):
    """Yield structural and non-structural slices from data"""
    last = 0
    for idx, i in enumerate(data):
        if i in '(,)':
            if idx - last > 0:
                yield data[last:idx]
            yield i
            last = idx + 1
    if data[-1] != ';':
        raise ValueError('Newick string is not semicolon terminated')
    if idx - last > 0:
        yield data[last:idx]
    yield data[-1]


def clipping(fontid: int, xmin: float, ymin: float, xmax: float, ymax: float):
    """Set the clipping, enable/disable using CLIPPING.

    :param fontid: The id of the typeface as returned by blf.load(), for default font use 0.
    :type fontid: int
    :param xmin: Clip the drawing area by these bounds.
    :type xmin: float
    :param ymin: Clip the drawing area by these bounds.
    :type ymin: float
    :param xmax: Clip the drawing area by these bounds.
    :type xmax: float
    :param ymax: Clip the drawing area by these bounds.
    :type ymax: float
    """
    pass


@classmethod
def format_string(cls, s):
    """
        Format a `LocalizedString` instance
        """
    return u'/*{}*/\n"{}" = "{}";\n'.format(s.comment, s.key, s.value)


def _pad_none(args, length):
    """Pad a list of arguments with None to specified length.

    """
    return (list(args) + [None] * length)[:length]


@classmethod
def get_layout_aliases(cls):
    """

        Yield a series of (ALIAS, repl) tuples that cause a name ALIAS in a
        layout based on this actor to be replaced by its replacement `repl`.

        """
    return []


def rect(scan, debug_poly=None):
    """
    Dummy function to test debug polygons of lidar viewer
    """
    if debug_poly is not None:
        debug_poly.append([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])


def split(p):
    """Same as posixpath.split, but faster

    >>> import posixpath
    >>> for f in [b'/absolute/path/to/file',
    ...           b'relative/path/to/file',
    ...           b'file_alone',
    ...           b'path/to/directory/',
    ...           b'/multiple/path//separators',
    ...           b'/file_at_root',
    ...           b'///multiple_leading_separators_at_root',
    ...           b'']:
    ...     assert split(f) == posixpath.split(f), f
    """
    ht = p.rsplit(b'/', 1)
    if len(ht) == 1:
        return b'', p
    nh = ht[0].rstrip(b'/')
    if nh:
        return nh, ht[1]
    return ht[0] + b'/', ht[1]


@staticmethod
def heat_utilities_by_agent(heat_utilities):
    """Return a dictionary of heat utilities sorted by agent."""
    heat_utilities_by_agent = {}
    for heat_utility in heat_utilities:
        agent = heat_utility.agent
        if not agent:
            continue
        if agent in heat_utilities_by_agent:
            heat_utilities_by_agent[agent].append(heat_utility)
        else:
            heat_utilities_by_agent[agent] = [heat_utility]
    return heat_utilities_by_agent


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

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

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

    where p=2 or p='inf'.

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

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

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

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

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


def df_option(string):
    """Return an integer or float, depending on the value in the given string.  Needed because
    fit_transform does a check on dtype and it's important to cast the input correctly."""
    try:
        return int(string)
    except ValueError:
        return float(string)


def get(isdsAppliance, check_mode=False, force=False):
    """
    Get host records
    """
    return isdsAppliance.invoke_get('Retrieving current host records',
        '/host_records')


def lower(string):
    """
    Makes string all lowercase.
    """
    return string.lower()


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


def file_type(path):
    """Identifies what the type of file is."""
    signature = {'\x1f\x8b\x08': 'gz', 'BZh': 'bz2', 'PK\x03\x04': 'zip'}
    with open(path, 'r') as f:
        for sig, f_type in signature.items():
            if f.read(4).startswith(sig):
                return f_type


@staticmethod
def parse_val(val):
    """Parse a single list value from string to appropriate native type.

        :param val: A single value to parse.
        :return: The converted value.
        """
    return val


def hide(unselected: bool=False):
    """Hide selected curves from Graph Editor view

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


def inner(items):
    """
    Sorted items to display as compatable js objects (eg bool,regex)
    """
    for k, v in sorted(items):
        if k == 'is_':
            k = 'is'
        if isinstance(v, bool):
            yield '%s: %s' % (k, repr(v).lower())
        elif k == 'pattern':
            yield '%s: new RegExp(/%s/)' % (k, v)
        else:
            yield '%s: %r' % (k, v)


def admin_list_devices(UserPoolId=None, Username=None, Limit=None,
    PaginationToken=None):
    """
    Lists devices, as an administrator.
    Requires developer credentials.
    See also: AWS API Documentation

    :example: response = client.admin_list_devices(
        UserPoolId='string',
        Username='string',
        Limit=123,
        PaginationToken='string'
    )

    :type UserPoolId: string
    :param UserPoolId: [REQUIRED]
            The user pool ID.

    :type Username: string
    :param Username: [REQUIRED]
            The user name.

    :type Limit: integer
    :param Limit: The limit of the devices request.

    :type PaginationToken: string
    :param PaginationToken: The pagination token.

    :rtype: dict
    :return: {
        'Devices': [
            {
                'DeviceKey': 'string',
                'DeviceAttributes': [
                    {
                        'Name': 'string',
                        'Value': 'string'
                    },
                ],
                'DeviceCreateDate': datetime(2015, 1, 1),
                'DeviceLastModifiedDate': datetime(2015, 1, 1),
                'DeviceLastAuthenticatedDate': datetime(2015, 1, 1)
            },
        ],
        'PaginationToken': 'string'
    }

    """
    pass


def _init_hook2(cls, name, bases, dct):
    """ Overloaded in flexx.app.AppComponentMeta.
        """
    pass


def join_version(version_tuple):
    """Return a string representation of version from given VERSION tuple"""
    version = '%s.%s.%s' % version_tuple[:3]
    if version_tuple[3] != 'final':
        version += '-%s' % version_tuple[3]
    return version


def undo_redo():
    """Undo and redo previous action

    """
    pass


@classmethod
def by_col(cls, df, e, b, inplace=False, **kwargs):
    """
        Compute smoothing by columns in a dataframe.

        Parameters
        -----------
        df      :  pandas.DataFrame
                   a dataframe containing the data to be smoothed
        e       :  string or list of strings
                   the name or names of columns containing event variables to be
                   smoothed
        b       :  string or list of strings
                   the name or names of columns containing the population
                   variables to be smoothed
        inplace :  bool
                   a flag denoting whether to output a copy of `df` with the
                   relevant smoothed columns appended, or to append the columns
                   directly to `df` itself.
        **kwargs:  optional keyword arguments
                   optional keyword options that are passed directly to the
                   smoother.

        Returns
        ---------
        a copy of `df` containing the columns. Or, if `inplace`, this returns
        None, but implicitly adds columns to `df`.
        """
    if not inplace:
        new = df.copy()
        cls.by_col(new, e, b, inplace=True, **kwargs)
        return new
    if isinstance(e, str):
        e = [e]
    if isinstance(b, str):
        b = [b]
    if len(b) == 1 and len(e) > 1:
        b = b * len(e)
    try:
        assert len(e) == len(b)
    except AssertionError:
        raise ValueError(
            'There is no one-to-one mapping between event variable and population at risk variable!'
            )
    for ei, bi in zip(e, b):
        ename = ei
        bname = bi
        ei = df[ename]
        bi = df[bname]
        outcol = '_'.join(('-'.join((ename, bname)), cls.__name__.lower()))
        df[outcol] = cls(ei, bi, **kwargs).r


def on_enter(bridge, ev):
    """Callback for bridge enter events.

    When channels enter the bridge, play tones to the whole bridge.

    :param bridge: Bridge entering the channel.
    :param ev: Event.
    """
    if ev['channel']['name'].startswith('Announcer/'):
        return
    bridge.play(media='sound:ascending-2tone')


def test():
    """Tests by comparing the analytical and numerical greek values.

    >>> S =  49
    >>> K = 50
    >>> r = .05
    >>> t = 0.3846
    >>> sigma = 0.2
    >>> flag = 'c'

    >>> epsilon = .0001

    >>> v1 = delta(flag, S, K, t, r, sigma)
    >>> v2 = adelta(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    >>> v1 = gamma(flag, S, K, t, r, sigma)
    >>> v2 = agamma(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    >>> v1 = rho(flag, S, K, t, r, sigma)
    >>> v2 = arho(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    >>> v1 = vega(flag, S, K, t, r, sigma)
    >>> v2 = avega(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    >>> v1 = theta(flag, S, K, t, r, sigma)
    >>> v2 = atheta(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    Test PUT flag

    >>> flag = 'p'

    >>> v1 = delta(flag, S, K, t, r, sigma)
    >>> v2 = adelta(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    >>> v1 = gamma(flag, S, K, t, r, sigma)
    >>> v2 = agamma(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    >>> v1 = rho(flag, S, K, t, r, sigma)
    >>> v2 = arho(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    >>> v1 = vega(flag, S, K, t, r, sigma)
    >>> v2 = avega(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    >>> v1 = theta(flag, S, K, t, r, sigma)
    >>> v2 = atheta(flag, S, K, t, r, sigma)
    >>> abs(v1-v2)<epsilon
    True

    """
    pass


@staticmethod
def _latexify_ascii(ascii_string):
    """function computing a fallback LaTeX representation of an plain-text string"""
    _lpn = ascii_string.replace('_', '\\_')
    return '{\\tt %s}' % (_lpn,)


def hms_str_to_tuple(hms_string):
    """Converts a string of the form HH:MM:SS or HH MM SS to a tuple of the form
    (HH, MM, SS).

    Parameters
    ----------

    hms_string : str
        A RA coordinate string of the form 'HH:MM:SS.sss' or 'HH MM SS.sss'.

    Returns
    -------

    tuple
        A three element tuple is returned (HH, MM, SS.ssss...)

    """
    if ':' in hms_string:
        separator = ':'
    else:
        separator = ' '
    hh, mm, ss = hms_string.split(separator)
    return int(hh), int(mm), float(ss)


@classmethod
def natural_key_fields(cls):
    """
        Digest is used as a natural key for ModulemdDefaults.
        """
    return 'digest',


def dot_vectors_xy(u, v):
    """Compute the dot product of two vectors, assuming they lie in the XY-plane.

    Parameters
    ----------
    u : sequence of float
        XY(Z) coordinates of the first vector.
    v : sequence of float
        XY(Z) coordinates of the second vector.

    Returns
    -------
    float
        The dot product of the XY components of the two vectors.

    Examples
    --------
    >>> dot_vectors_xy([1.0, 0], [2.0, 0])
    2.0

    >>> dot_vectors_xy([1.0, 0, 0], [2.0, 0, 0])
    2.0

    >>> dot_vectors_xy([1.0, 0, 1], [2.0, 0, 1])
    2.0

    """
    return u[0] * v[0] + u[1] * v[1]


@classmethod
def by_id(cls, client=None, id=None, full_dehydrate='no'):
    """Retrieve a user record by id and create a User instance from it.

        Args:
            client (Client): an instance of the API Client
            id (str): id of the user to retrieve
            full_dehyrdate (optional, str): if 'yes' returns all info on user

        Returns:
            User: a User instance corresponding to the record
        """
    response = client.users.get(id, full_dehydrate=full_dehydrate)
    return cls.from_response(client, response)


def plot_station_mappings(mapping_results):
    """ Plot a list of mapping results on a map.

    Requires matplotlib and cartopy.

    Parameters
    ----------
    mapping_results : list of MappingResult objects
        Mapping results to plot
    """
    try:
        import matplotlib.pyplot as plt
    except ImportError:
        raise ImportError('Plotting requires matplotlib.')
    try:
        import cartopy.crs as ccrs
        import cartopy.feature as cfeature
    except ImportError:
        raise ImportError('Plotting requires cartopy.')
    lats = []
    lngs = []
    t_lats = []
    t_lngs = []
    n_discards = 0
    for mapping_result in mapping_results:
        if not mapping_result.is_empty():
            lat, lng = mapping_result.isd_station.coords
            t_lat, t_lng = map(float, mapping_result.target_coords)
            lats.append(lat)
            lngs.append(lng)
            t_lats.append(t_lat)
            t_lngs.append(t_lng)
        else:
            n_discards += 1
    print('Discarded {} empty mappings'.format(n_discards))
    fig = plt.figure(figsize=(60, 60))
    ax = plt.subplot(1, 1, 1, projection=ccrs.Mercator())
    all_lngs = lngs + t_lngs
    all_lats = lats + t_lats
    x_max = max(all_lngs)
    x_min = min(all_lngs)
    x_diff = x_max - x_min
    y_max = max(all_lats)
    y_min = min(all_lats)
    y_diff = y_max - y_min
    x_pad = 0.1 * x_diff
    y_pad = 0.1 * y_diff
    left = x_min - x_pad
    right = x_max + x_pad
    bottom = y_min - y_pad
    top = y_max + y_pad
    width_ratio = 2.0
    height_ratio = 1.0
    if (right - left) / (top - bottom) > height_ratio / width_ratio:
        goal = (right - left) * height_ratio / width_ratio
        diff = goal - (top - bottom)
        bottom = bottom - diff / 2.0
        top = top + diff / 2.0
    else:
        goal = (top - bottom) * width_ratio / height_ratio
        diff = goal - (right - left)
        left = left - diff / 2.0
        right = right + diff / 2.0
    left = max(left, -179.9)
    right = min(right, 179.9)
    bottom = max([bottom, -89.9])
    top = min([top, 89.9])
    ax.set_extent([left, right, bottom, top])
    ax.add_feature(cfeature.NaturalEarthFeature('physical', 'ocean', '50m',
        edgecolor='face', facecolor=cfeature.COLORS['water']))
    ax.add_feature(cfeature.NaturalEarthFeature('physical', 'land', '50m',
        edgecolor='face', facecolor=cfeature.COLORS['land']))
    ax.add_feature(cfeature.NaturalEarthFeature('cultural',
        'admin_0_boundary_lines_land', '50m', edgecolor='black', facecolor=
        'none'))
    ax.add_feature(cfeature.NaturalEarthFeature('physical', 'lakes', '50m',
        edgecolor='face', facecolor=cfeature.COLORS['water']))
    ax.add_feature(cfeature.NaturalEarthFeature('physical', 'coastline',
        '50m', edgecolor='black', facecolor='none'))
    ax.plot([lngs, t_lngs], [lats, t_lats], color='k', linestyle='-',
        transform=ccrs.Geodetic(), linewidth=0.3)
    ax.plot(lngs, lats, 'bo', markersize=1, transform=ccrs.Geodetic())
    plt.title('Location to weather station mapping')
    plt.show()


@classmethod
def get_sites(cls):
    """ Get testbed sites description
        May be run unauthicated

        :returns JSONObject
        """
    return cls._get_with_cache('sites')


def set_identity_headers_in_notifications_enabled(Identity=None,
    NotificationType=None, Enabled=None):
    """
    Given an identity (an email address or a domain), sets whether Amazon SES includes the original email headers in the Amazon Simple Notification Service (Amazon SNS) notifications of a specified type.
    This action is throttled at one request per second.
    For more information about using notifications with Amazon SES, see the Amazon SES Developer Guide .
    See also: AWS API Documentation

    Examples
    The following example configures Amazon SES to include the original email headers in the Amazon SNS bounce notifications for an identity:
    Expected Output:

    :example: response = client.set_identity_headers_in_notifications_enabled(
        Identity='string',
        NotificationType='Bounce'|'Complaint'|'Delivery',
        Enabled=True|False
    )

    :type Identity: string
    :param Identity: [REQUIRED]
            The identity for which to enable or disable headers in notifications. Examples: user@example.com , example.com .

    :type NotificationType: string
    :param NotificationType: [REQUIRED]
            The notification type for which to enable or disable headers in notifications.

    :type Enabled: boolean
    :param Enabled: [REQUIRED]
            Sets whether Amazon SES includes the original email headers in Amazon SNS notifications of the specified notification type. A value of true specifies that Amazon SES will include headers in notifications, and a value of false specifies that Amazon SES will not include headers in notifications.
            This value can only be set when NotificationType is already set to use a particular Amazon SNS topic.

    :rtype: dict
    :return: {}

    """
    pass


def human_readable_number(number, suffix=''):
    """
    Format the given number into a human-readable string.

    Code adapted from http://stackoverflow.com/a/1094933

    :param variant number: the number (int or float)
    :param string suffix: the unit of the number
    :rtype: string
    """
    for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
        if abs(number) < 1024.0:
            return '%3.1f%s%s' % (number, unit, suffix)
        number /= 1024.0
    return '%.1f%s%s' % (number, 'Y', suffix)


def getIds():
    """Return an IFSet of docids."""


@classmethod
def get_by_id(cls, context, portgroup_id):
    """Find a portgroup by its integer ID and return a Portgroup object.

        :param cls: the :class:`Portgroup`
        :param context: Security context
        :param portgroup_id: The ID of a portgroup.
        :returns: A :class:`Portgroup` object.
        :raises: PortgroupNotFound

        """
    db_portgroup = cls.dbapi.get_portgroup_by_id(portgroup_id)
    portgroup = cls._from_db_object(context, cls(), db_portgroup)
    return portgroup


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


def language(language: str=''):
    """Set the current language for this console

    :param language: Language
    :type language: str
    """
    pass


def get_rating(html):
    """Rating of an episode
    Works for any page with a rating
    Incase no rating found, returns None
    """
    try:
        div = html.find('div', {'class': 'titlePageSprite star-box-giga-star'})
        return div.text.strip(' ')
    except AttributeError:
        return None


def __get_remote(repo, remote_name):
    """
    Get a Remote from a repo if it exists.

    :param repo: Repo object.
    :param remote_name: Remote name to get.
    :return: Remote if it exists. None otherwise.
    """
    remote = None
    for r in repo.remotes:
        if r.name == remote_name:
            remote = r
            break
    return remote


@classmethod
def _get_apic_classes(cls):
    """
        Get the APIC classes used by this acitoolkit class.
        :returns: list of strings containing APIC class names
        """
    return ['healthInst']


def check_chr(file, lines=100):
    """Checks if a file has lines starting with 'chr'."""
    chrom = False
    count = 0
    with open(file, 'r') as f:
        for line in f:
            if not line.startswith('#'):
                if line.startswith('chr'):
                    chrom = True
                    break
                count += 1
                if count == lines:
                    break
    return chrom


@staticmethod
def p_init_segment(p):
    """init_segment : INIT_SEGMENT NUMERIC"""


def reset():
    """Clear the rules used for policy checks."""
    global _rules
    _rules = None


def asFloatOrNone(val):
    """Converts floats, integers and string representations of either to floats.
    If val is "NaN" (case irrelevant) or "?" returns None.

    Raises ValueError or TypeError for all other values

    """
    if hasattr(val, 'lower') and val.lower() in ('nan', '?'):
        return None
    else:
        return float(val)


def get_storage_node_drives(session, cloud_name, sn_id, return_type=None,
    **kwargs):
    """
    Get all Storage Node drives

    :type session: zadarapy.session.Session
    :param session: A valid zadarapy.session.Session object.  Required.

    :type cloud_name: str
    :param cloud_name: Cloud Name: i.e: zadaraqa9

    :type sn_id: str
    :param sn_id: Storage Node ID

    :type return_type: str
    :param return_type: If this is set to the string 'json', this function
        will return a JSON string.  Otherwise, it will return a Python
        dictionary.  Optional (will return a Python dictionary by default).

    :rtype: dict, str
    :returns: A dictionary or JSON data set as a string depending on
        return_type parameter.
    """
    path = '/api/clouds/{0}/nodes/{1}/drives.json'.format(cloud_name, sn_id)
    return session.get_api(path=path, return_type=return_type, **kwargs)


def place_in_dict(t, model, dictionary: dict, key: str, value):
    """Appends an entry in the list corresponding to key if key exists in dictionary.
    Otherwise creates a single entry in the dictionary as {key: [value]}"""
    if t == model.time[0]:
        dictionary.update({key: [value]})
    else:
        dictionary[key].append(value)


def edge_loops_from_edges(mesh, edges=None):
    """closed loops have matching start and end values.

    """
    pass


@classmethod
def _init_from_self(cls, model):
    """helper method for dict_to_h5py"""
    return cls(model)


@staticmethod
def p_if_data_xcp_optional(p):
    """if_data_xcp_optional : protocol_layer
                                | daq
                                | pag
                                | pgm
                                | segment
                                | daq_event
                                | xcp_on_can
                                | generic_parameter_list"""
    p[0] = p.slice[1].type, p[1]


@classmethod
def double_round(cls, x):
    """Perform two rounds of ChaCha cipher"""
    for a, b, c, d in cls._round_mixup_box:
        xa = x[a]
        xb = x[b]
        xc = x[c]
        xd = x[d]
        xa = xa + xb & 4294967295
        xd = xd ^ xa
        xd = xd << 16 & 4294967295 | xd >> 16
        xc = xc + xd & 4294967295
        xb = xb ^ xc
        xb = xb << 12 & 4294967295 | xb >> 20
        xa = xa + xb & 4294967295
        xd = xd ^ xa
        xd = xd << 8 & 4294967295 | xd >> 24
        xc = xc + xd & 4294967295
        xb = xb ^ xc
        xb = xb << 7 & 4294967295 | xb >> 25
        x[a] = xa
        x[b] = xb
        x[c] = xc
        x[d] = xd


def diagnose(verbosity=0):
    """
    Diagnosis function
    Find available devices when NUFFT.offload() fails.
    """
    from reikna import cluda
    import reikna.transformations
    from reikna.cluda import functions, dtypes
    try:
        api = cluda.cuda_api()
        if verbosity > 0:
            print('cuda interface is available')
        available_cuda_device = cluda.find_devices(api)
        cuda_flag = 1
        if verbosity > 0:
            print('try to load cuda interface:')
            print(api, available_cuda_device)
            for api_n in available_cuda_device.keys():
                print("API='cuda',  ", 'platform_number=', api_n,
                    ', device_number=', available_cuda_device[api_n][0])
    except:
        cuda_flag = 0
        if verbosity > 0:
            print('cuda interface is not available')
    try:
        api = cluda.ocl_api()
        available_ocl_device = cluda.find_devices(api)
        ocl_flag = 1
        if verbosity > 0:
            print('ocl interface is available')
            print(api, available_ocl_device)
            print('try to load ocl interface with:')
            for api_n in available_ocl_device.keys():
                print("API='ocl',  ", 'platform_number=', api_n,
                    ', device_number=', available_ocl_device[api_n][0])
    except:
        print('ocl interface is not available')
        ocl_flag = 0
    return cuda_flag, ocl_flag


def calc_t_ramp(t_int, n_reset, t_frame):
    """Calculates the ramp time -- or the integration time plus overhead for resets.

    Parameters
    ----------
    t_int : float
        Integration time (in seconds).
    n_reset : int
        Rest frames per integration
    t_frame : float
        Frame time (in seconds).

    Returns
    -------
    t_ramp : float
        Ramp time (in seconds).
    """
    t_ramp = t_int + (n_reset - 1) * t_frame
    return t_ramp


def from_serialized(ref, the_data):
    """Unserialize the element

    Used for the retention store

    :param ref: Not used
    :type ref:
    :param the_data: dictionary to convert
    :type the_data: dict
    :return: serialized data
    :rtype: dict
    """
    if not the_data:
        return {}
    if not getattr(the_data, 'unserialize', None):
        return the_data
    return the_data.unserialize()


def DateFormatswithDashes():
    """Yield all combinations of valid date formats."""
    from itertools import permutations
    formats = []
    date = '%Y-%m-%d'
    hours = '%H', ''
    minutes = '%M', ''
    seconds = '%S', ''
    for hour in hours:
        for minute in minutes:
            for second in seconds:
                for combo in permutations([second, minute, hour]):
                    time = ':'.join(combo).strip(':')
                    formats.append((date + ' ' + time).strip(' '))
    formats = list(dict.fromkeys(formats))
    formats = sorted(formats, key=len, reverse=True)
    formats = ['%Y-%m-%d %H:%M:%S'] + formats
    return formats


@staticmethod
def get_main_window():
    """
        Returns Qt object that references to the main DCC window
        :return:
        """
    return None


def lookup(id, template_path):
    """Look up template.

        id - the id of the collection
        template_path - the relative path (or filename) of the template
                        itself, under the path of the collection
        """


def transitions(deactivated=False):
    """ Returns a list of :py:class:`ITransition` s (including inherited)
        """


def get_pos_counts(tagged_text):
    """ Calculate the total number of types for each part of speech taggers. """
    pos_counts = {}
    for item in tagged_text:
        if item[1] in pos_counts.keys():
            pos_counts[item[1]] += 1
        else:
            pos_counts.update({item[1]: 1})
    return pos_counts


@staticmethod
def gcd(a, b):
    """
        Calculates the Greatest Common Divisor.

        :param a: the first number.
        :param b: the second number.
        :return: GCD(a, b)
        """
    while b:
        a, b = b, a % b
    return a


def set_permissions_and_owner(tarinfo, permissions, uid=0, username='root'):
    """
    Sets the given permissions and owner for the given tarinfo.

    :param tarinfo: The tarinfo to set information for
    :type tarinfo: tarfile.Tarinfo
    :param permissions: The permission bits to set for the given tarinfo
    :param uid: The user id to set for the given tarinfo
    :param username: The owner of the given tarinfo
    """
    tarinfo.uid = uid
    tarinfo.gid = uid
    tarinfo.uname = username
    tarinfo.gname = username
    tarinfo.mode = permissions


def SizeBenefitOther(t):
    """Other benefit per policy"""
    return 0


def create_index(hdf_data, table_name='groundparticles', progress=False):
    """Create a completely sorted index for the x column

    This can speed up queries to select data based on the x column.

    """
    table = hdf_data.get_node('/', table_name)
    if progress:
        print('Ensuring the x column for table %s is indexed.' % table_name)
    try:
        table.cols.x.create_csindex()
    except ValueError:
        table.reindex_dirty()


def fillNoneValues(column):
    """Fill all NaN/NaT values of a column with an empty string

    Args:
        column (pandas.Series): A Series object with all rows.

    Returns:
        column: Series with filled NaN values.
    """
    if column.dtype == object:
        column.fillna('', inplace=True)
    return column


def reset_stream(stream):
    """
    Resets the cursor of a stream to the beginning.

    This is implemented with a try-except because not all file-like objects are guaranteed to have
    a ``seek()`` method, so we carry on if we cannot reset the pointer.

    Parameters
    ----------
    stream : file-like
        A stream that may or may not implement ``seek()``.

    """
    try:
        stream.seek(0)
    except AttributeError:
        pass


@classmethod
def from_points(cls, p1, p2):
    """Create a rectangle, knowing 2 points on the plane.
        We assume that p1 is in the upper left (closer to the origin)

        :param p1: Point instance
        :param p2: Point instance
        :return: a Point instance
        """
    inst = cls()
    inst._p1 = p1
    inst._p2 = p2
    return inst


def check_first_arg(first, *args):
    """
    Check first arg is true.

    :param first:
        First arg.
    :type first: T

    :return:
        If first arg is true.
    :rtype: bool
    """
    return bool(first)


def pumpfile(from_file, to_file, read_length=-1, buff_size=32768,
    report_activity=None, direction='read'):
    """Copy contents of one file to another.

    The read_length can either be -1 to read to end-of-file (EOF) or
    it can specify the maximum number of bytes to read.

    The buff_size represents the maximum size for each read operation
    performed on from_file.

    :param report_activity: Call this as bytes are read, see
        Transport._report_activity
    :param direction: Will be passed to report_activity

    :return: The number of bytes copied.
    """
    length = 0
    if read_length >= 0:
        while read_length > 0:
            num_bytes_to_read = min(read_length, buff_size)
            block = from_file.read(num_bytes_to_read)
            if not block:
                break
            if report_activity is not None:
                report_activity(len(block), direction)
            to_file.write(block)
            actual_bytes_read = len(block)
            read_length -= actual_bytes_read
            length += actual_bytes_read
    else:
        while True:
            block = from_file.read(buff_size)
            if not block:
                break
            if report_activity is not None:
                report_activity(len(block), direction)
            to_file.write(block)
            length += len(block)
    return length


def convert_recurrence_unit_forward(apps, schema_editor):
    """Copy integer-based unit to char-based."""
    PlanCost = apps.get_model('subscriptions', 'PlanCost')
    for cost in PlanCost.objects.all():
        old_unit = cost.old_recurrence_unit
        new_unit = str(old_unit)
        cost.recurrence_unit = new_unit
        cost.save()


def getDrafts(userId, targetKey):
    """Get a list mappping of all drafts under the given userId and
        target key. The returned mapping should not be modified directly.
        """


def widget_name(widget):
    """Return the widget name"""
    return str(widget.objectName())


def file_and_extension(path: str):
    """return filename and their extension."""
    filename = path.split('/')[-1].split('.')[:-1][0]
    extension = path.split('.')[-1][0]
    return filename, extension


def export_3dpdf(client, file_=None, filename=None, dirname=None, height=
    None, width=None, dpi=None, use_drawing_settings=None):
    """Export a model to a 3D PDF file.

    Args:
        client (obj):
            creopyson Client.
        `file_` (str, optional):
            Model name. Defaults is current active model.
        filename (str, optional):
            Destination file name. May also contain a path to the file.
            Defaults is the model name with the appropriate file extension,
            in Creo's working directory.
        dirname (str, optional):
            Destination directory. Defaults is Creo's current
            working directory.
        height (float, optional):
            PDF image height. Defaults is Creo default export height.
        width (float, optional):
            PDF image width. Defaults is Creo default export width.
        dpi (int, optional):
            PDF Image DPI. Default is Creo default export DPI.
        use_drawing_settings (boolean, optional):
            Whether to use special settings for exporting drawings.
            Defaut is False.

    Returns:
        dict:
            dirname (str): Directory of the output file
            filename (str): Name of the output file

    """
    data = {}
    if file_ is not None:
        data['file'] = file_
    else:
        active_file = client.file_get_active()
        if active_file is not None:
            data['file'] = active_file['file']
    if filename:
        data['filename'] = filename
    if dirname:
        data['dirname'] = dirname
    if height:
        data['height'] = height
    if width:
        data['width'] = width
    if dpi:
        data['dpi'] = dpi
    if use_drawing_settings:
        data['use_drawing_settings'] = use_drawing_settings
    return client._creoson_post('interface', 'export_3dpdf', data)


@staticmethod
def verify_parameters(params):
    """Verify that dict values are set"""
    for key in params.keys():
        if params[key] is None:
            raise TypeError('%s not set' % key)


def broadcast_mod(lhs=None, rhs=None, out=None, name=None, **kwargs):
    """Returns element-wise modulo of the input arrays with broadcasting.

    Example::

       x = [[ 8.,  8.,  8.],
            [ 8.,  8.,  8.]]

       y = [[ 2.],
            [ 3.]]

       broadcast_mod(x, y) = [[ 0.,  0.,  0.],
                              [ 2.,  2.,  2.]]

    Defined in src/operator/tensor/elemwise_binary_broadcast_op_basic.cc:L222

    Parameters
    ----------
    lhs : NDArray
        First input to the function
    rhs : NDArray
        Second input to the function

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

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


def get_container_addables(require=None):
    """Get a list of content that can be added in the adapter
        container (they can be added to the site, and are not
        restricted in that container).

        If require is not None, it is an interface that the all the
        returned addable must implement.
        """


@classmethod
def is_archive_file(cls, filename):
    """Check if the filename is allowed for the Archive"""
    for regext in cls.ALLOWED_EXTENSIONS:
        if regext.search(filename):
            return regext.split(filename)[0]
    return False


def find_bbox(thresh, g):
    """Calculate bounding box for gaussian.

    This function calculates size of the box for evaluating
    gaussian, so that value of gaussian is smaller than threshold
    outside of the box.

    Parameters:
    thres: threshold
    g: Gaussian object or list of paramters
    """
    from math import ceil, sqrt, log
    if isinstance(g, list):
        A = g[0]
        S = g[3]
    else:
        A = g.peak_flux
        S = g.size_pix[0]
    if A == 0.0:
        return ceil(S * 1.5)
    if thresh / A >= 1.0 or thresh / A <= 0.0:
        return ceil(S * 1.5)
    return ceil(S * sqrt(-2 * log(thresh / A)))


def cell_empty(value) ->bool:
    """Checks if a cell is empty. Cells contain True or False return False"""
    if isinstance(value, bool):
        return False
    if not value:
        return True
    return False


def bg_mask(in_file, in_mask, out_file=None):
    """
    Rough mask of background from brain masks
    """
    import nibabel as nb
    import numpy as np
    from scipy.ndimage import binary_dilation
    import scipy.ndimage as nd
    import os.path as op
    if out_file is None:
        fname, ext = op.splitext(op.basename(in_file))
        if ext == '.gz':
            fname, ext2 = op.splitext(fname)
            ext = ext2 + ext
        out_file = op.abspath('%s_bgmask%s' % (fname, ext))
    im = nb.load(in_file)
    hdr = im.header.copy()
    hdr.set_data_dtype(np.uint8)
    hdr.set_xyzt_units('mm')
    msk = nb.load(in_mask).get_data()
    msk = 1 - binary_dilation(msk, structure=np.ones((20, 20, 20)))
    nb.Nifti1Image(msk.astype(np.uint8), im.affine, hdr).to_filename(out_file)
    return out_file


@staticmethod
def int_to_bytestring(i, padding=8):
    """
        Turns an integer to the OATH specified
        bytestring, which is fed to the HMAC
        along with the secret
        """
    result = bytearray()
    while i != 0:
        result.append(i & 255)
        i >>= 8
    return bytes(bytearray(reversed(result)).rjust(padding, b'\x00'))


def ibm2Ieee(ibm_float):
    """
    ibm2Ieee(ibm_float)
    Used by permission
    (C) Secchi Angelo
    with thanks to Howard Lightstone and Anton Vredegoor.
    """
    """
    I = struct.unpack('>I',ibm_float)[0]
    sign = [1,-1][bool(i & 0x100000000L)]
    characteristic = ((i >> 24) & 0x7f) - 64
    fraction = (i & 0xffffff)/float(0x1000000L)
    return sign*16**characteristic*fraction
    """


@classmethod
def reset_requests(cls):
    """
        This method resets the list of request to be empty

        CHANGELOG

        Added 10.06.2019

        :return:
        """
    cls._REQUESTS = []
    cls.REQUESTS = []


def copyaxes(inp, out):
    """
    copy axes values from NPKDAta in to out.

    internal use
    """
    for ii in range(inp.dim):
        i = ii + 1
        setattr(out, 'axis%1d' % i, inp.axes(i).copy())


@staticmethod
def _get_uri(plex_server):
    """Generate the websocket URI using the `plexapi` library."""
    return plex_server.url('/:/websockets/notifications', includeToken=True
        ).replace('http', 'ws')


def help(update, context):
    """Send a message when the command /help is issued."""
    update.message.reply_text(
        """Send:
/plot start end i,j,k...
to get a plot of cultures No. i, j, k...
"""
         +
        """from the time moment start  till the end.
Example:
/plot 5h 1w 0,1,2,3,4,5,6
"""
         +
        'plots the OD in 0th, 1st, ..., 6th tubes from t=5h to t = 1week \n\n'
         +
        """/pumpdata for pump_data.csv
/rawsignal for raw_signal.csv

""" +
        '/getvolumes to get death volumes\n' +
        '/getthresholds to get thresholds\n' + '/lastdata\n' +
        """/setvolumes 0,1,2,3,4,5,6 to change volumes. Only for the registered IDs!
"""
         +
        '/setthresholds 0,1,2,3,4,5,6 to change thresholds. Only for the registered IDs!\n'
         + """/plotOD
 /convertODs
/setconditions tube,vol,ODraw""")


def _unpack_image(pixels):
    """Flatten out pixels and returns a tuple. The first entry is the size of each pixel."""
    unpacked_pixels = []
    try:
        for pix in pixels:
            for val in pix:
                unpacked_pixels.append(val)
        return len(pixels[0]), bytes(unpacked_pixels)
    except TypeError:
        return 1, bytes(pixels)


def tile_bounds(tsinfo, z, x, y, width=1, height=1):
    """
    Get the coordinate boundaries for the given tile.

    Parameters:
    -----------
    tsinfo: { min_pos: [], max_pos [] }
        Tileset info containing the bounds of the dataset
    z: int
        The zoom level
    x: int
        The x position
    y: int
        The y position
    width: int
        Return bounds for a region encompassing multiple tiles
    height: int
        Return bounds for a region encompassing multiple tiles
    """
    min_pos = tsinfo['min_pos']
    max_pos = tsinfo['max_pos']
    max_width = max(max_pos[0] - min_pos[0], max_pos[1] - min_pos[1])
    tile_width = max_width / 2 ** z
    from_x = min_pos[0] + x * tile_width
    to_x = min_pos[0] + (x + width) * tile_width
    from_y = min_pos[1] + y * tile_width
    to_y = min_pos[1] + (y + height) * tile_width
    return [from_x, from_y, to_x, to_y]


def solidsFluxPembWake(rhop, emf, umf, us):
    """
    Calculates the solids entrainment flux for the surface of a bubbling bed
    with the bubble wake ejection model of Pemberton and Davidsion (Chem. Eng.
    Sci., 1986, 41, pp. 243-251). This model is suitable if multiple bubbles
    coalesce at the surface before bursting.

    Parameters
    ----------
    rhop : float
        Mass density of the particles
    emf : float
        Void fraction at minimum fluidization [-]
    umf : float
        Minimum fluidization velocity [m/s]
    us : float
        Superficial gas velocity [m/s]

    Returns
    -------
    F0 : float
        Solids entrainment flux at the top of the bed [kg/m^2/s]
    """
    F0 = 0.1 * rhop * (1 - emf) * (us - umf)
    return F0


def monthly_iter(start, end):
    """
    Iterates on a monthly basis, and wraps around dateutil.rrule

    Example:
    In [1]: from zc_common import timezone

    In [2]: for m in timezone.monthly_iter(timezone.datetime(2014, 10, 1), timezone.now()):
    ...:     print m
    ...:
    2014-10-01 00:00:00-07:00
    2014-11-01 00:00:00-07:00

    In [3]: for m in timezone.monthly_iter(timezone.datetime(2014, 10, 19), timezone.now()):
    ...:     print m
    ...:
    2014-10-19 00:00:00-07:00

    In [4]: timezone.now()
    Out[4]: datetime.datetime(
        2014, 11, 18, 16, 36, 54, 994666, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
    """
    from dateutil import rrule
    for date in rrule.rrule(rrule.MONTHLY, dtstart=start, until=end):
        yield date


def create_treelist(dataclass, uri):
    """ create a list of resources out of a tree

    This function is used for the COPY, MOVE and DELETE methods

    uri - the root of the subtree to flatten

    It will return the flattened tree as list

    """
    queue = [uri]
    list = [uri]
    while len(queue):
        element = queue[-1]
        if dataclass.is_collection(element):
            childs = dataclass.get_childs(element)
        else:
            childs = []
        if len(childs):
            list = list + childs
        del queue[-1]
        if len(childs):
            queue = queue + childs
    return list


@staticmethod
def _tuple_from_bbox(bbox):
    """ Converts a BBox instance into a tuple

        :param bbox: An instance of the BBox type
        :return: tuple (min_x, min_y, max_x, max_y)
        """
    return bbox.lower_left + bbox.upper_right


@classmethod
def fromconstructs(cls, constructs, copy=False):
    """Create a domain from existing metadata constructs.

    The new domain act as a view to the given constructs, i.e. changes
    to the domain, such as the addition or removal of a construct,
    will also affect the input `Constructs` instance.

    .. versionadded:: 1.7.0

    :Parameters:

        constructs: `Constructs`
            The constructs from which to create the new domain. Cell
            method and field ancillary constucts are ignored.

        copy: bool, optional
            If True then deep copy the metadata constructs prior to
            initialization. By default the metadata constructs are not
            copied.

    :Returns:

        `Domain`
            The domain created from a view of the constructs.

    **Examples:**

    >>> d = Domain.fromconstructs(f.constructs)

        """
    domain = cls()
    domain._set_component('constructs', constructs._view(ignore=(
        'cell_method', 'field_ancillary')), copy=copy)
    domain.constructs._field_data_axes = None
    return domain


def reveal_metaelems():
    """Reveal all hidden metaelements

    """
    pass


@classmethod
def new_from_obj(cls, obj, data=None, var=None, copy=False, unit=None):
    """Create a new object from another one, copying its attributes.

        Parameters
        ----------
        obj  : `mpdaf.obj.DataArray`
            The object to use as the template for the new object. This
            should be an object based on DataArray, such as an Image,
            Cube or Spectrum.
        data : array-like, optional
            An optional data array, or None to indicate that
            ``obj.data`` should be used. The default is None.
        var : array-like, optional
            An optional variance array, or None to indicate that
            ``obj.var`` should be used, or False to indicate that the
            new object should not have any variances. The default
            is None.
        copy : bool
            Copy the data and variance arrays if True (default False).

        """
    data = obj.data if data is None else data
    if var is None:
        var = obj._var
    elif var is False:
        var = None
    if unit is None:
        unit = obj.unit
    kwargs = dict(filename=obj.filename, data=data, unit=unit, var=var, ext
        =(obj._data_ext, obj._var_ext, obj._dq_ext), copy=copy, data_header
        =obj.data_header.copy(), primary_header=obj.primary_header.copy())
    if cls._has_wcs:
        kwargs['wcs'] = obj.wcs
    if cls._has_wave:
        kwargs['wave'] = obj.wave
    return cls(**kwargs)


def scrub(table_name):
    """
    Snippet to prevent SQL injection attcks! PEW PEW PEW!
    """
    return ''.join(chr for chr in table_name if chr.isalnum())


def _compute_num_derivatives(dimension, suffix=''):
    """Computes the number of derivatives of order 'n' as dimension()^n.

    Dimension will be the element topological dimension for the number
    of derivatives in local coordinates, and the geometric dimension
    for the number of derivatives in phyisical coordinates.
    """
    f_int = format['int']
    f_num_derivs = format['num derivatives'](suffix)
    code = [format['comment']('Compute number of derivatives.')]
    code.append(format['declaration'](format['uint declaration'],
        f_num_derivs, f_int(1)))
    loop_vars = [(format['free indices'][0], 0, format[
        'argument derivative order'])]
    lines = [format['imul'](f_num_derivs, f_int(dimension))]
    code += format['generate loop'](lines, loop_vars)
    return code


@classmethod
def get_real_child_model(cls, data):
    """Returns the real base class specified by the discriminator"""
    discriminator_value = data[cls.json_discriminator_key]
    return cls.discriminator_value_class_map.get(discriminator_value)


def read_from_narrative(narratives):
    """Read from narratives the defined
    value for the last defined timestep

    Arguments
    ---------
    narratives : lives
        Narratives

    Returns
    -------
    last_value : float
        Value of last defined timestep narrative
    """
    last_year = 0
    for narrative in narratives:
        if narrative['end_yr'] > last_year:
            last_value = narrative['value_ey']
            last_year = narrative['end_yr']
    return last_value


def get_metadata(output, key, mimetype=None):
    """Resolve an output metadata key

    If mimetype given, resolve at mimetype level first,
    then fallback to top-level.
    Otherwise, just resolve at top-level.
    Returns None if no data found.
    """
    md = output.get('metadata') or {}
    if mimetype and mimetype in md:
        value = md[mimetype].get(key)
        if value is not None:
            return value
    return md.get(key)


def sanitize(msg):
    """Sanitize the existing msg, use before adding color annotations."""
    msg = msg.replace('@', '@@')
    msg = msg.replace('{', '{{')
    msg = msg.replace('}', '}}')
    msg = msg.replace('@@!', '@{atexclimation}')
    msg = msg.replace('@@/', '@{atfwdslash}')
    msg = msg.replace('@@_', '@{atunderscore}')
    msg = msg.replace('@@|', '@{atbar}')
    return msg


def parse_chocophlan_gene_indexes(annotation_gene_index):
    """ Parse the chocophlan gene index input """
    chocophlan_gene_indexes = []
    for index in annotation_gene_index.split(','):
        if ':' in index:
            split_index = index.split(':')
            start = split_index[0]
            end = split_index[1]
            try:
                start = int(start)
                end = int(end)
                chocophlan_gene_indexes += range(start, end)
            except ValueError:
                pass
        else:
            try:
                index = int(index)
                chocophlan_gene_indexes.append(index)
            except ValueError:
                pass
    return chocophlan_gene_indexes