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
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
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)