Reshape array to new shape Parameters ---------- shape : int or tuple of ints The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is
(x, shape, merge_chunks=True, limit=None)
| 281 | |
| 282 | |
| 283 | def reshape(x, shape, merge_chunks=True, limit=None): |
| 284 | """Reshape array to new shape |
| 285 | |
| 286 | Parameters |
| 287 | ---------- |
| 288 | shape : int or tuple of ints |
| 289 | The new shape should be compatible with the original shape. If |
| 290 | an integer, then the result will be a 1-D array of that length. |
| 291 | One shape dimension can be -1. In this case, the value is |
| 292 | inferred from the length of the array and remaining dimensions. |
| 293 | merge_chunks : bool, default True |
| 294 | Whether to merge chunks using the logic in :meth:`dask.array.rechunk` |
| 295 | when communication is necessary given the input array chunking and |
| 296 | the output shape. With ``merge_chunks==False``, the input array will |
| 297 | be rechunked to a chunksize of 1, which can create very many tasks. |
| 298 | limit: int (optional) |
| 299 | The maximum block size to target in bytes. If no limit is provided, |
| 300 | it defaults to using the ``array.chunk-size`` Dask config value. |
| 301 | |
| 302 | Notes |
| 303 | ----- |
| 304 | This is a parallelized version of the ``np.reshape`` function with the |
| 305 | following limitations: |
| 306 | |
| 307 | 1. It assumes that the array is stored in `row-major order`_ |
| 308 | 2. It only allows for reshapings that collapse or merge dimensions like |
| 309 | ``(1, 2, 3, 4) -> (1, 6, 4)`` or ``(64,) -> (4, 4, 4)`` |
| 310 | |
| 311 | .. _`row-major order`: https://en.wikipedia.org/wiki/Row-_and_column-major_order |
| 312 | |
| 313 | When communication is necessary this algorithm depends on the logic within |
| 314 | rechunk. It endeavors to keep chunk sizes roughly the same when possible. |
| 315 | |
| 316 | See :ref:`array-chunks.reshaping` for a discussion the tradeoffs of |
| 317 | ``merge_chunks``. |
| 318 | |
| 319 | See Also |
| 320 | -------- |
| 321 | dask.array.rechunk |
| 322 | numpy.reshape |
| 323 | """ |
| 324 | # Sanitize inputs, look for -1 in shape |
| 325 | from dask.array.slicing import sanitize_index |
| 326 | |
| 327 | shape = tuple(map(sanitize_index, shape)) |
| 328 | known_sizes = [s for s in shape if s != -1] |
| 329 | if len(known_sizes) < len(shape): |
| 330 | if len(shape) - len(known_sizes) > 1: |
| 331 | raise ValueError("can only specify one unknown dimension") |
| 332 | # Fastpath for x.reshape(-1) on 1D arrays, allows unknown shape in x |
| 333 | # for this case only. |
| 334 | if len(shape) == 1 and x.ndim == 1: |
| 335 | return x |
| 336 | missing_size = sanitize_index(x.size / reduce(mul, known_sizes, 1)) |
| 337 | shape = tuple(missing_size if s == -1 else s for s in shape) |
| 338 | |
| 339 | _sanity_checks(x, shape) |
| 340 |
no test coverage detected
searching dependent graphs…