| 54 | |
| 55 | |
| 56 | class FromArray(IO): |
| 57 | _parameters = [ |
| 58 | "array", |
| 59 | "chunks", |
| 60 | "lock", |
| 61 | "getitem", |
| 62 | "inline_array", |
| 63 | "meta", |
| 64 | "asarray", |
| 65 | "fancy", |
| 66 | ] |
| 67 | _defaults = { |
| 68 | "getitem": None, |
| 69 | "inline_array": False, |
| 70 | "meta": None, |
| 71 | "asarray": None, |
| 72 | "fancy": True, |
| 73 | "lock": False, |
| 74 | } |
| 75 | |
| 76 | @property |
| 77 | def chunks(self): |
| 78 | return normalize_chunks( |
| 79 | self.operand("chunks"), self.array.shape, dtype=self.array.dtype |
| 80 | ) |
| 81 | |
| 82 | @functools.cached_property |
| 83 | def _meta(self): |
| 84 | if self.operand("meta") is not None: |
| 85 | return meta_from_array(self.operand("meta"), dtype=self.array.dtype) |
| 86 | return meta_from_array(self.array, dtype=getattr(self.array, "dtype", None)) |
| 87 | |
| 88 | @functools.cached_property |
| 89 | def asarray_arg(self): |
| 90 | if self.operand("asarray") is None: |
| 91 | return not hasattr(self.array, "__array_function__") |
| 92 | else: |
| 93 | return self.operand("asarray") |
| 94 | |
| 95 | def _layer(self): |
| 96 | lock = self.operand("lock") |
| 97 | if lock is True: |
| 98 | lock = SerializableLock() |
| 99 | |
| 100 | is_ndarray = type(self.array) in (np.ndarray, np.ma.core.MaskedArray) |
| 101 | is_single_block = all(len(c) == 1 for c in self.chunks) |
| 102 | # Always use the getter for h5py etc. Not using isinstance(x, np.ndarray) |
| 103 | # because np.matrix is a subclass of np.ndarray. |
| 104 | if is_ndarray and not is_single_block and not lock: |
| 105 | # eagerly slice numpy arrays to prevent memory blowup |
| 106 | # GH5367, GH5601 |
| 107 | slices = slices_from_chunks(self.chunks) |
| 108 | keys = product([self._name], *(range(len(bds)) for bds in self.chunks)) |
| 109 | values = [self.array[slc] for slc in slices] |
| 110 | dsk = dict(zip(keys, values)) |
| 111 | elif is_ndarray and is_single_block: |
| 112 | # No slicing needed |
| 113 | dsk = {(self._name,) + (0,) * self.array.ndim: self.array} |
no outgoing calls
no test coverage detected
searching dependent graphs…