# Path: third_party/xiuminglib/xiuminglib/os.py
# def open_file(path, mode):
#     """Opens a file.
# 
#     Supports Google Colossus if ``gfile`` can be imported.
# 
#     Args:
#         path (str): Path to open.
#         mode (str): ``'r'``, ``'rb'``, ``'w'``, or ``'wb'``.
# 
#     Returns:
#         File handle that can be used as a context.
#     """
#     gfile = preset_import('gfile')
# 
#     open_func = open if gfile is None else gfile.Open
#     handle = open_func(path, mode)
# 
#     return handle
# 
# def exists_isdir(path):
#     """Determines whether a path exists, and if so, whether it is a file
#     or directory.
# 
#     Supports Google Colossus (CNS) paths by using ``gfile`` (preferred for
#     speed) or the ``fileutil`` CLI.
# 
#     Args:
#         path (str): A path.
# 
#     Returns:
#         tuple:
#             - **exists** (*bool*) -- Whether the path exists.
#             - **isdir** (*bool*) -- Whether the path is a file or directory.
#               ``None`` if the path doesn't exist.
#     """
#     path = _no_trailing_slash(path)
# 
#     # If local path, do the job quickly and return
#     if not _is_cnspath(path):
#         path_exists = exists(path)
#         path_isdir = isdir(path) if path_exists else None
#         return path_exists, path_isdir
# 
#     gfile = preset_import('gfile', assert_success=True)
# 
#     # Using fileutil CLI
#     if gfile is None:
#         testf, _, _ = call('fileutil test -f %s' % path)
#         testd, _, _ = call('fileutil test -d %s' % path)
#         if testf == 1 and testd == 1:
#             path_exists = False
#             path_isdir = None
#         elif testf == 1 and testd == 0:
#             path_exists = True
#             path_isdir = True
#         elif testf == 0 and testd == 1:
#             path_exists = True
#             path_isdir = False
#         else:
#             raise NotImplementedError("What does this even mean?")
# 
#     # Using gfile
#     else:
#         path_exists = gfile.Exists(path)
#         if path_exists:
#             path_isdir = gfile.IsDirectory(path)
#         else:
#             path_isdir = None
# 
#     return path_exists, path_isdir
# 
# def makedirs(directory, rm_if_exists=False):
#     """Wraps :func:`os.makedirs` to support removing the directory if it
#     alread exists.
# 
#     Google Colossus-compatible: it tries to use ``gfile`` first for speed. This
#     will fail if Blaze is not used, in which case it then falls back to using
#     ``fileutil`` CLI as external process calls.
# 
#     Args:
#         directory (str)
#         rm_if_exists (bool, optional): Whether to remove the directory (and
#             its contents) if it already exists.
#     """
#     def exists_cns_cli(directory):
#         cmd = 'fileutil test -d %s' % directory
#         retcode, _, _ = call(cmd, quiet=True)
#         if retcode == 0:
#             return True
#         if retcode == 1:
#             return False
#         raise ValueError(retcode)
# 
#     def mkdir_cns_cli(directory):
#         cmd = 'fileutil mkdir -p %s' % directory
#         _call_assert_success(cmd, quiet=True)
# 
#     if _is_cnspath(directory):
#         # Is a CNS path
#         gfile = preset_import('gfile')
#         if gfile is None:
#             exists_func = exists_cns_cli
#             mkdir_func = mkdir_cns_cli
#         else:
#             exists_func = gfile.Exists
#             mkdir_func = gfile.MakeDirs
#     else:
#         # Is just a regular local path
#         exists_func = exists
#         mkdir_func = os.makedirs
# 
#     # Do the job
#     if exists_func(directory):
#         if rm_if_exists:
#             rm(directory)
#             mkdir_func(directory)
#             logger.info("Removed and then remade:\n\t%s", directory)
#     else:
#         mkdir_func(directory)
# 
# Path: third_party/xiuminglib/xiuminglib/log.py
# def get_logger(level=None):
#     """Creates a logger for functions in the library.
# 
#     Args:
#         level (str, optional): Logging level. Defaults to ``logging.INFO``.
# 
#     Returns:
#         logging.Logger: Logger created.
#     """
#     if level is None:
#         level = logging.INFO
#     logging.basicConfig(level=level)
#     logger = logging.getLogger()
#     return logger


from os.path import dirname
from ..os import open_file, exists_isdir, makedirs
from ..log import get_logger
import numpy as np
            deciding how to load the data.
        fallback (function, optional): Fallback function used if data file
            doesn't exist. Its return will be saved to ``data_f`` for future
            loadings. It should not take arguments, but if yours requires taking
            arguments, just wrap yours with::

                fallback=lambda: your_fancy_func(var0, var1)

    Returns:
        Data loaded if ``data_f`` exists; otherwise, ``fallback``'s return
        (``None`` if no fallback).

    Writes
        - Return by the fallback, if provided.
    """
    # Decide data file type
    ext = data_f.split('.')[-1].lower()

    def load_func(path):
        with open_file(path, 'rb') as h:
            data = np.load(h)
        return data

    def save_func(data, path):
        if ext == 'npy':
            save = np.save
        elif ext == 'npz':
            save = np.savez
        else:
            raise NotImplementedError(ext)

# Path: third_party/xiuminglib/xiuminglib/os.py
# def open_file(path, mode):
#     """Opens a file.
# 
#     Supports Google Colossus if ``gfile`` can be imported.
# 
#     Args:
#         path (str): Path to open.
#         mode (str): ``'r'``, ``'rb'``, ``'w'``, or ``'wb'``.
# 
#     Returns:
#         File handle that can be used as a context.
#     """
#     gfile = preset_import('gfile')
# 
#     open_func = open if gfile is None else gfile.Open
#     handle = open_func(path, mode)
# 
#     return handle
# 
# def exists_isdir(path):
#     """Determines whether a path exists, and if so, whether it is a file
#     or directory.
# 
#     Supports Google Colossus (CNS) paths by using ``gfile`` (preferred for
#     speed) or the ``fileutil`` CLI.
# 
#     Args:
#         path (str): A path.
# 
#     Returns:
#         tuple:
#             - **exists** (*bool*) -- Whether the path exists.
#             - **isdir** (*bool*) -- Whether the path is a file or directory.
#               ``None`` if the path doesn't exist.
#     """
#     path = _no_trailing_slash(path)
# 
#     # If local path, do the job quickly and return
#     if not _is_cnspath(path):
#         path_exists = exists(path)
#         path_isdir = isdir(path) if path_exists else None
#         return path_exists, path_isdir
# 
#     gfile = preset_import('gfile', assert_success=True)
# 
#     # Using fileutil CLI
#     if gfile is None:
#         testf, _, _ = call('fileutil test -f %s' % path)
#         testd, _, _ = call('fileutil test -d %s' % path)
#         if testf == 1 and testd == 1:
#             path_exists = False
#             path_isdir = None
#         elif testf == 1 and testd == 0:
#             path_exists = True
#             path_isdir = True
#         elif testf == 0 and testd == 1:
#             path_exists = True
#             path_isdir = False
#         else:
#             raise NotImplementedError("What does this even mean?")
# 
#     # Using gfile
#     else:
#         path_exists = gfile.Exists(path)
#         if path_exists:
#             path_isdir = gfile.IsDirectory(path)
#         else:
#             path_isdir = None
# 
#     return path_exists, path_isdir
# 
# def makedirs(directory, rm_if_exists=False):
#     """Wraps :func:`os.makedirs` to support removing the directory if it
#     alread exists.
# 
#     Google Colossus-compatible: it tries to use ``gfile`` first for speed. This
#     will fail if Blaze is not used, in which case it then falls back to using
#     ``fileutil`` CLI as external process calls.
# 
#     Args:
#         directory (str)
#         rm_if_exists (bool, optional): Whether to remove the directory (and
#             its contents) if it already exists.
#     """
#     def exists_cns_cli(directory):
#         cmd = 'fileutil test -d %s' % directory
#         retcode, _, _ = call(cmd, quiet=True)
#         if retcode == 0:
#             return True
#         if retcode == 1:
#             return False
#         raise ValueError(retcode)
# 
#     def mkdir_cns_cli(directory):
#         cmd = 'fileutil mkdir -p %s' % directory
#         _call_assert_success(cmd, quiet=True)
# 
#     if _is_cnspath(directory):
#         # Is a CNS path
#         gfile = preset_import('gfile')
#         if gfile is None:
#             exists_func = exists_cns_cli
#             mkdir_func = mkdir_cns_cli
#         else:
#             exists_func = gfile.Exists
#             mkdir_func = gfile.MakeDirs
#     else:
#         # Is just a regular local path
#         exists_func = exists
#         mkdir_func = os.makedirs
# 
#     # Do the job
#     if exists_func(directory):
#         if rm_if_exists:
#             rm(directory)
#             mkdir_func(directory)
#             logger.info("Removed and then remade:\n\t%s", directory)
#     else:
#         mkdir_func(directory)
# 
# Path: third_party/xiuminglib/xiuminglib/log.py
# def get_logger(level=None):
#     """Creates a logger for functions in the library.
# 
#     Args:
#         level (str, optional): Logging level. Defaults to ``logging.INFO``.
# 
#     Returns:
#         logging.Logger: Logger created.
#     """
#     if level is None:
#         level = logging.INFO
#     logging.basicConfig(level=level)
#     logger = logging.getLogger()
#     return logger


# Path: third_party/xiuminglib/xiuminglib/io/np.py
from os.path import dirname
from ..os import open_file, exists_isdir, makedirs
from ..log import get_logger
import numpy as np

            deciding how to load the data.
        fallback (function, optional): Fallback function used if data file
            doesn't exist. Its return will be saved to ``data_f`` for future
            loadings. It should not take arguments, but if yours requires taking
            arguments, just wrap yours with::

                fallback=lambda: your_fancy_func(var0, var1)

    Returns:
        Data loaded if ``data_f`` exists; otherwise, ``fallback``'s return
        (``None`` if no fallback).

    Writes
        - Return by the fallback, if provided.
    """
    # Decide data file type
    ext = data_f.split('.')[-1].lower()

    def load_func(path):
        with open_file(path, 'rb') as h:
            data = np.load(h)
        return data

    def save_func(data, path):
        if ext == 'npy':
            save = np.save
        elif ext == 'npz':
            save = np.savez
        else:
            raise NotImplementedError(ext)

        with open_file(path, 'wb') as h: