Convert XRF map from it's initial representation to properly chunked Dask array. Parameters ---------- data: da.core.Array, np.ndarray or RawHDF5Dataset (this is a custom type) Raw XRF map represented as Dask array, numpy array or reference to a dataset in HDF5 file
(data, chunk_pixels=5000, n_chunks_min=4)
| 329 | |
| 330 | |
| 331 | def prepare_xrf_map(data, chunk_pixels=5000, n_chunks_min=4): |
| 332 | """ |
| 333 | Convert XRF map from it's initial representation to properly chunked Dask array. |
| 334 | |
| 335 | Parameters |
| 336 | ---------- |
| 337 | data: da.core.Array, np.ndarray or RawHDF5Dataset (this is a custom type) |
| 338 | Raw XRF map represented as Dask array, numpy array or reference to a dataset in |
| 339 | HDF5 file. The XRF map must have dimensions `(ny, nx, ne)`, where `ny` and `nx` |
| 340 | define image size and `ne` is the number of spectrum points |
| 341 | chunk_pixels: int |
| 342 | The number of pixels in a single chunk. The XRF map will be rechunked so that |
| 343 | each block contains approximately `chunk_pixels` pixels and contain all `ne` |
| 344 | spectrum points for each pixel. |
| 345 | n_chunks_min: int |
| 346 | Minimum number of chunks. The algorithm will try to split the map into the number |
| 347 | of chunks equal or greater than `n_chunks_min`. If HDF5 dataset is not chunked, |
| 348 | then the whole map is treated as one chunk. This should happen only to very small |
| 349 | files, so parallelism is not important. |
| 350 | |
| 351 | Returns |
| 352 | ------- |
| 353 | data: da.core.Array |
| 354 | XRF map represented as Dask array with proper chunk size. The XRF map may be loaded |
| 355 | block by block when processing using `dask.array.map_blocks` and `dask.array.blockwise` |
| 356 | functions with Dask multiprocessing scheduler. |
| 357 | file_obj: h5py.File object |
| 358 | File object that points to HDF5 file. `None` if input parameter `data` is Dask or |
| 359 | numpy array. Note, that `file_obj` must be kept alive until processing is completed. |
| 360 | Closing the file will invalidate references to the dataset in the respective |
| 361 | Dask array. |
| 362 | |
| 363 | Raises |
| 364 | ------ |
| 365 | TypeError if input parameter `data` is not one of supported types. |
| 366 | """ |
| 367 | |
| 368 | file_obj = None # It will remain None, unless 'data' is 'RawHDF5Dataset' |
| 369 | |
| 370 | if isinstance(data, da.core.Array): |
| 371 | chunk_size = _compute_optimal_chunk_size( |
| 372 | chunk_pixels=chunk_pixels, |
| 373 | data_chunksize=data.chunksize[0:2], |
| 374 | data_shape=data.shape[0:2], |
| 375 | n_chunks_min=n_chunks_min, |
| 376 | ) |
| 377 | data = data.rechunk(chunks=(*chunk_size, data.shape[2])) |
| 378 | elif isinstance(data, np.ndarray): |
| 379 | data = _array_numpy_to_dask(data, chunk_pixels=chunk_pixels, n_chunks_min=n_chunks_min) |
| 380 | elif isinstance(data, RawHDF5Dataset): |
| 381 | fpath, dset_name = data.abs_path, data.dset_name |
| 382 | |
| 383 | # Note, that the file needs to remain open until the processing is complete !!! |
| 384 | file_obj = h5py.File(fpath, "r") |
| 385 | dset = file_obj[dset_name] |
| 386 | |
| 387 | if dset.ndim != 3: |
| 388 | raise TypeError( |