()
| 436 | |
| 437 | @normalize_token.register_lazy("numpy") |
| 438 | def register_numpy(): |
| 439 | import numpy as np |
| 440 | |
| 441 | @normalize_token.register(np.ndarray) |
| 442 | def normalize_array(x): |
| 443 | if not x.shape: |
| 444 | return (x.item(), x.dtype) |
| 445 | if x.dtype.hasobject: |
| 446 | try: |
| 447 | try: |
| 448 | # string fast-path |
| 449 | data = hash_buffer_hex( |
| 450 | "-".join(x.flat).encode( |
| 451 | encoding="utf-8", errors="surrogatepass" |
| 452 | ) |
| 453 | ) |
| 454 | except UnicodeDecodeError: |
| 455 | # bytes fast-path |
| 456 | data = hash_buffer_hex(b"-".join(x.flat)) |
| 457 | except (TypeError, UnicodeDecodeError): |
| 458 | return normalize_object(x) |
| 459 | else: |
| 460 | try: |
| 461 | data = hash_buffer_hex(x.ravel(order="K").view("i1")) |
| 462 | except (BufferError, AttributeError, ValueError): |
| 463 | data = hash_buffer_hex(x.copy().ravel(order="K").view("i1")) |
| 464 | return (data, x.dtype, x.shape) |
| 465 | |
| 466 | @normalize_token.register(np.memmap) |
| 467 | def normalize_mmap(mm): |
| 468 | return hash_buffer_hex(np.ascontiguousarray(mm)) |
| 469 | |
| 470 | @normalize_token.register(np.ufunc) |
| 471 | def normalize_ufunc(func): |
| 472 | try: |
| 473 | return _normalize_pickle(func) |
| 474 | except Exception: |
| 475 | _maybe_raise_nondeterministic( |
| 476 | f"Cannot tokenize numpy ufunc {func!r}. Please use functions " |
| 477 | "of the dask.array.ufunc module instead. See also " |
| 478 | "https://docs.dask.org/en/latest/array-numpy-compatibility.html" |
| 479 | ) |
| 480 | return uuid.uuid4().hex |
| 481 | |
| 482 | @normalize_token.register(np.dtype) |
| 483 | def normalize_dtype(dtype): |
| 484 | return dtype.str |
| 485 | |
| 486 | |
| 487 | def _tokenize_deterministic(*args, **kwargs) -> str: |
nothing calls this directly
no outgoing calls
no test coverage detected