Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the possibility to use custom hash function #1130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 36 additions & 14 deletions joblib/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from ._store_backends import StoreBackendBase, FileSystemStoreBackend



FIRST_LINE_TEXT = "# first line:"

# TODO: The following object should have a data store object as a sub
Expand Down Expand Up @@ -136,7 +135,6 @@ def _store_backend_factory(backend, location, verbose=0, backend_options=None):
"supported by joblib. Returning None instead.".format(
location.__class__.__name__), UserWarning)


return None


Expand Down Expand Up @@ -219,6 +217,7 @@ class MemorizedResult(Logger):
timestamp, metadata: string
for internal use only.
"""

def __init__(self, location, func, args_id, backend='local',
mmap_mode=None, verbose=0, timestamp=None, metadata=None):
Logger.__init__(self)
Expand Down Expand Up @@ -345,6 +344,7 @@ class NotMemorizedFunc(object):
Original undecorated function.
"""
# Should be a light as possible (for speed)

def __init__(self, func):
self.func = func

Expand Down Expand Up @@ -402,13 +402,19 @@ class MemorizedFunc(Logger):
verbose: int, optional
The verbosity flag, controls messages that are issued as
the function is evaluated.

hash_func: callable, optional
Parameters: (obj: object, hash_name: string, coerce_mmap: Boolean)
Return the hash from an object. Default is None.
If hash_func is None hashing.hash is used
"""
# ------------------------------------------------------------------------
# Public interface
# ------------------------------------------------------------------------

def __init__(self, func, location, backend='local', ignore=None,
mmap_mode=None, compress=False, verbose=1, timestamp=None):
mmap_mode=None, compress=False, verbose=1,
timestamp=None, hash_func=None):
Logger.__init__(self)
self.mmap_mode = mmap_mode
self.compress = compress
Expand All @@ -434,9 +440,11 @@ def __init__(self, func, location, backend='local', ignore=None,
if timestamp is None:
timestamp = time.time()
self.timestamp = timestamp
self.hash_func = hashing.hash if hash_func is None else hash_func

try:
functools.update_wrapper(self, func)
except:
except BaseException:
" Objects like ufunc don't like that "
if inspect.isfunction(func):
doc = pydoc.TextDoc().document(func)
Expand Down Expand Up @@ -611,8 +619,10 @@ def __getstate__(self):
# ------------------------------------------------------------------------

def _get_argument_hash(self, *args, **kwargs):
return hashing.hash(filter_args(self.func, self.ignore, args, kwargs),
coerce_mmap=(self.mmap_mode is not None))
return self.hash_func(
filter_args(self.func, self.ignore, args, kwargs),
coerce_mmap=self.mmap_mode is not None
)

def _get_output_identifiers(self, *args, **kwargs):
"""Return the func identifier and input parameter hash of a result."""
Expand Down Expand Up @@ -683,8 +693,8 @@ def _check_previous_func_code(self, stacklevel=2):
extract_first_line(
self.store_backend.get_cached_func_code([func_id]))
except (IOError, OSError): # some backend can also raise OSError
self._write_func_code(func_code, first_line)
return False
self._write_func_code(func_code, first_line)
return False
if old_func_code == func_code:
return True

Expand Down Expand Up @@ -887,14 +897,19 @@ class Memory(Logger):
backend_options: dict, optional
Contains a dictionnary of named parameters used to configure
the store backend.

hash_func: callable, optional
Parameters: (obj: object, hash_name: string, coerce_mmap: Boolean)
Returns the hash from an object.
If hash_func is None (default), hashing.hash is used.
"""
# ------------------------------------------------------------------------
# Public interface
# ------------------------------------------------------------------------

def __init__(self, location=None, backend='local', cachedir=None,
mmap_mode=None, compress=False, verbose=1, bytes_limit=None,
backend_options=None):
backend_options=None, hash_func=None):
# XXX: Bad explanation of the None value of cachedir
Logger.__init__(self)
self._verbose = verbose
Expand All @@ -906,6 +921,7 @@ def __init__(self, location=None, backend='local', cachedir=None,
if backend_options is None:
backend_options = {}
self.backend_options = backend_options
self.hash_func = hash_func

if compress and mmap_mode is not None:
warnings.warn('Compressed results cannot be memmapped',
Expand Down Expand Up @@ -986,11 +1002,17 @@ def cache(self, func=None, ignore=None, verbose=None, mmap_mode=False):
mmap_mode = self.mmap_mode
if isinstance(func, MemorizedFunc):
func = func.func
return MemorizedFunc(func, location=self.store_backend,
backend=self.backend,
ignore=ignore, mmap_mode=mmap_mode,
compress=self.compress,
verbose=verbose, timestamp=self.timestamp)
return MemorizedFunc(
func,
location=self.store_backend,
backend=self.backend,
ignore=ignore,
mmap_mode=mmap_mode,
compress=self.compress,
verbose=verbose,
timestamp=self.timestamp,
hash_func=self.hash_func
)

def clear(self, warn=True):
""" Erase the complete cache directory.
Expand Down