当前位置: 首页>>代码示例>>Python>>正文


Python Memory.startswith方法代码示例

本文整理汇总了Python中sklearn.externals.joblib.Memory.startswith方法的典型用法代码示例。如果您正苦于以下问题:Python Memory.startswith方法的具体用法?Python Memory.startswith怎么用?Python Memory.startswith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.externals.joblib.Memory的用法示例。


在下文中一共展示了Memory.startswith方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _check_memory

# 需要导入模块: from sklearn.externals.joblib import Memory [as 别名]
# 或者: from sklearn.externals.joblib.Memory import startswith [as 别名]
def _check_memory(memory, verbose=0):
    """Function to ensure an instance of a joblib.Memory object.

    Parameters
    ----------
    memory: None or instance of joblib.Memory or str
        Used to cache the masking process.
        If a str is given, it is the path to the caching directory.

    verbose : int, optional (default 0)
        Verbosity level.

    Returns
    -------
    instance of joblib.Memory.
    """
    if memory is None:
        memory = Memory(cachedir=None, verbose=verbose)
    if isinstance(memory, _basestring):
        cache_dir = memory
        if nilearn.EXPAND_PATH_WILDCARDS:
            cache_dir = os.path.expanduser(cache_dir)

        # Perform some verifications on given path.
        split_cache_dir = os.path.split(cache_dir)
        if (len(split_cache_dir) > 1 and
                (not os.path.exists(split_cache_dir[0]) and
                    split_cache_dir[0] != '')):
            if (not nilearn.EXPAND_PATH_WILDCARDS and
                    cache_dir.startswith("~")):
                # Maybe the user want to enable expanded user path.
                error_msg = ("Given cache path parent directory doesn't "
                             "exists, you gave '{0}'. Enabling "
                             "nilearn.EXPAND_PATH_WILDCARDS could solve "
                             "this issue.".format(split_cache_dir[0]))
            elif memory.startswith("~"):
                # Path built on top of expanded user path doesn't exist.
                error_msg = ("Given cache path parent directory doesn't "
                             "exists, you gave '{0}' which was expanded "
                             "as '{1}' but doesn't exist either. Use "
                             "nilearn.EXPAND_PATH_WILDCARDS to deactivate "
                             "auto expand user path (~) behavior."
                             .format(split_cache_dir[0],
                                     os.path.dirname(memory)))
            else:
                # The given cache base path doesn't exist.
                error_msg = ("Given cache path parent directory doesn't "
                             "exists, you gave '{0}'."
                             .format(split_cache_dir[0]))
            raise ValueError(error_msg)

        memory = Memory(cachedir=cache_dir, verbose=verbose)
    return memory
开发者ID:jeromedockes,项目名称:nilearn,代码行数:55,代码来源:cache_mixin.py

示例2: CacheMixin

# 需要导入模块: from sklearn.externals.joblib import Memory [as 别名]
# 或者: from sklearn.externals.joblib.Memory import startswith [as 别名]
class CacheMixin(object):
    """Mixin to add caching to a class.

    This class is a thin layer on top of joblib.Memory, that mainly adds a
    "caching level", similar to a "log level".

    Usage: to cache the results of a method, wrap it in self._cache()
    defined by this class. Caching is performed only if the user-specified
    cache level (self._memory_level) is greater than the value given as a
    parameter to self._cache(). See _cache() documentation for details.
    """
    def _cache(self, func, func_memory_level=1, **kwargs):
        """Return a joblib.Memory object.

        The memory_level determines the level above which the wrapped
        function output is cached. By specifying a numeric value for
        this level, the user can to control the amount of cache memory
        used. This function will cache the function call or not
        depending on the cache level.

        Parameters
        ----------
        func: function
            The function the output of which is to be cached.

        memory_level: int
            The memory_level from which caching must be enabled for the wrapped
            function.

        Returns
        -------
        mem: joblib.Memory
            object that wraps the function func. This object may be
            a no-op, if the requested level is lower than the value given
            to _cache()). For consistency, a joblib.Memory object is always
            returned.

        """

        verbose = getattr(self, 'verbose', 0)

        # Creates attributes if they don't exist
        # This is to make creating them in __init__() optional.
        if not hasattr(self, "memory_level"):
            self.memory_level = 0
        if not hasattr(self, "memory"):
            self.memory = Memory(cachedir=None, verbose=verbose)
        if isinstance(self.memory, _basestring):
            cache_dir = self.memory
            if nilearn.EXPAND_PATH_WILDCARDS:
                cache_dir = os.path.expanduser(cache_dir)

            # Perform some verifications on given path.
            split_cache_dir = os.path.split(cache_dir)
            if (len(split_cache_dir) > 1 and
                    (not os.path.exists(split_cache_dir[0]) and
                     split_cache_dir[0] != '')):
                if (not nilearn.EXPAND_PATH_WILDCARDS and
                        cache_dir.startswith("~")):
                    # Maybe the user want to enable expanded user path.
                    error_msg = ("Given cache path parent directory doesn't "
                                 "exists, you gave '{0}'. Enabling "
                                 "nilearn.EXPAND_PATH_WILDCARDS could solve "
                                 "this issue.".format(split_cache_dir[0]))
                elif self.memory.startswith("~"):
                    # Path built on top of expanded user path doesn't exist.
                    error_msg = ("Given cache path parent directory doesn't "
                                 "exists, you gave '{0}' which was expanded "
                                 "as '{1}' but doesn't exist either. Use "
                                 "nilearn.EXPAND_PATH_WILDCARDS to deactivate "
                                 "auto expand user path (~) behavior."
                                 .format(split_cache_dir[0],
                                         os.path.dirname(self.memory)))
                else:
                    # The given cache base path doesn't exist.
                    error_msg = ("Given cache path parent directory doesn't "
                                 "exists, you gave '{0}'."
                                 .format(split_cache_dir[0]))
                raise ValueError(error_msg)

            self.memory = Memory(cachedir=cache_dir, verbose=verbose)

        # If cache level is 0 but a memory object has been provided, set
        # memory_level to 1 with a warning.
        if self.memory_level == 0 and self.memory.cachedir is not None:
            warnings.warn("memory_level is currently set to 0 but "
                          "a Memory object has been provided. "
                          "Setting memory_level to 1.")
            self.memory_level = 1

        return cache(func, self.memory, func_memory_level=func_memory_level,
                     memory_level=self.memory_level, **kwargs)
开发者ID:CandyPythonFlow,项目名称:nilearn,代码行数:94,代码来源:cache_mixin.py


注:本文中的sklearn.externals.joblib.Memory.startswith方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。