MCPcopy Create free account
hub / github.com/NSLS2/PyXRF / _array_numpy_to_dask

Function _array_numpy_to_dask

pyxrf/core/map_processing.py:292–328  ·  view source on GitHub ↗

Convert an array (e.g. XRF map) from numpy array to chunked Dask array. Select chunk size based on the desired number of pixels `chunk_pixels`. The array is considered as an image with pixels along axes 0 and 1. The array is chunked only along axes 0 and 1. Parameters ---------

(data, chunk_pixels, n_chunks_min=4)

Source from the content-addressed store, hash-verified

290
291
292def _array_numpy_to_dask(data, chunk_pixels, n_chunks_min=4):
293 """
294 Convert an array (e.g. XRF map) from numpy array to chunked Dask array. Select chunk
295 size based on the desired number of pixels `chunk_pixels`. The array is considered
296 as an image with pixels along axes 0 and 1. The array is chunked only along axes 0 and 1.
297
298 Parameters
299 ----------
300 data: ndarray(float), 3D
301 Numpy array of the shape `(ny, nx, ...)` with at least 2 dimensions. If `data` is
302 an image, then `ny` and `nx` represent the image dimensions.
303 chunk_pixels: int
304 Desired number of pixels in a chunk. The actual number of pixels may differ from
305 the desired number to accommodate minimum requirements on the number of chunks or
306 limited size of the dataset.
307 n_chunks_min: int
308 minimum number of chunks, which should be selected based on the minimum number of
309 workers that should be used to process the map. Each chunk will contain at least
310 one pixel: if there is not enough pixels, then the number of chunks will be reduced.
311
312 Results
313 -------
314 Dask array of the same shape as `data` with chunks selected based on the desired number
315 of pixels `chunk_pixels`.
316 """
317
318 if not isinstance(data, np.ndarray) or (data.ndim < 2):
319 raise ValueError(f"Parameter 'data' must numpy array with at least 2 dimensions: type(data)={type(data)}")
320
321 ny, nx = data.shape[0:2]
322 # Since numpy array is not chunked by default, set the original chunk size to (1,1)
323 # because here we are performing 'original' chunking
324 chunk_y, chunk_x = _compute_optimal_chunk_size(
325 chunk_pixels=chunk_pixels, data_chunksize=(1, 1), data_shape=(ny, nx), n_chunks_min=n_chunks_min
326 )
327
328 return _chunk_numpy_array(data, (chunk_y, chunk_x))
329
330
331def prepare_xrf_map(data, chunk_pixels=5000, n_chunks_min=4):

Callers 6

test_fit_xrf_map1Method · 0.90
prepare_xrf_mapFunction · 0.85

Calls 2

_chunk_numpy_arrayFunction · 0.85

Tested by 5

test_fit_xrf_map1Method · 0.72