def secure_filename(filename):
    r"""Borrowed from :mod:`werkzeug.utils`, under the BSD 3-clause license.

    Pass it a filename and it will return a secure version of it.  This
    filename can then safely be stored on a regular file system and passed
    to :func:`os.path.join`.  The filename returned is an ASCII only string
    for maximum portability.

    On windows systems the function also makes sure that the file is not
    named after one of the special device files.

    >>> secure_filename("My cool movie.mov")
    'My_cool_movie.mov'
    >>> secure_filename("../../../etc/passwd")
    'etc_passwd'
    >>> secure_filename(u'i contain cool \xfcml\xe4uts.txt')
    'i_contain_cool_umlauts.txt'

    The function might return an empty filename.  It's your responsibility
    to ensure that the filename is unique and that you generate random
    filename if the function returned an empty one.

    :param filename: the filename to secure
    """
    _filename_ascii_strip_re = re.compile(r'[^A-Za-z0-9_.-]')
    _windows_device_files = ('CON', 'AUX', 'COM1', 'COM2', 'COM3', 'COM4',
                             'LPT1', 'LPT2', 'LPT3', 'PRN', 'NUL')
    if isinstance(filename, text_type):
        from unicodedata import normalize
        filename = normalize('NFKD', filename).encode('ascii', 'ignore')
        if not PY2:
            filename = filename.decode('ascii')
    for sep in os.path.sep, os.path.altsep:
        if sep:
            filename = filename.replace(sep, ' ')
    filename = str(_filename_ascii_strip_re.sub('', '_'.join(
                   filename.split()))).strip('._')

    # on nt a couple of special files are present in each folder.  We
    # have to ensure that the target file is not such a filename.  In
    # this case we prepend an underline
    if os.name == 'nt' and filename and \
       filename.split('.')[0].upper() in _windows_device_files:
        filename = '_' + filename

    return filename
def _filter_examples_by_class(target_values, downsampling_dict,
                              test_mode=False):
    """Filters examples by target value.

    E = number of examples

    :param target_values: length-E numpy array of target values (integer class
        labels).
    :param downsampling_dict: Dictionary, where each key is the integer
        ID for a target class (-2 for "dead storm") and the corresponding value
        is the number of examples desired from said class.  If
        `downsampling_dict is None`, `example_dict` will be returned
        without modification.
    :param test_mode: Never mind.  Just leave this alone.
    :return: indices_to_keep: 1-D numpy array with indices of examples to keep.
        These are all integers in [0, E - 1].
    """

    num_examples = len(target_values)

    if downsampling_dict is None:
        return numpy.linspace(0, num_examples - 1, num=num_examples, dtype=int)

    indices_to_keep = numpy.array([], dtype=int)
    class_keys = list(downsampling_dict.keys())

    for this_class in class_keys:
        this_num_storm_objects = downsampling_dict[this_class]
        these_indices = numpy.where(target_values == this_class)[0]

        this_num_storm_objects = min(
            [this_num_storm_objects, len(these_indices)]
        )
        if this_num_storm_objects == 0:
            continue

        if test_mode:
            these_indices = these_indices[:this_num_storm_objects]
        else:
            these_indices = numpy.random.choice(
                these_indices, size=this_num_storm_objects, replace=False)

        indices_to_keep = numpy.concatenate((indices_to_keep, these_indices))

    return indices_to_keep