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. The XRF map must have dimensions `(ny, nx, ne)`, where `ny` and `nx` define
(
data, *, selection=None, mask=None, chunk_pixels=5000, n_chunks_min=4, progress_bar=None, client=None
)
| 476 | |
| 477 | |
| 478 | def compute_total_spectrum( |
| 479 | data, *, selection=None, mask=None, chunk_pixels=5000, n_chunks_min=4, progress_bar=None, client=None |
| 480 | ): |
| 481 | """ |
| 482 | Parameters |
| 483 | ---------- |
| 484 | data: da.core.Array, np.ndarray or RawHDF5Dataset (this is a custom type) |
| 485 | Raw XRF map represented as Dask array, numpy array or reference to a dataset in |
| 486 | HDF5 file. The XRF map must have dimensions `(ny, nx, ne)`, where `ny` and `nx` |
| 487 | define image size and `ne` is the number of spectrum points |
| 488 | selection: tuple or list or None |
| 489 | selected area represented as (y0, x0, ny_sel, nx_sel) |
| 490 | mask: ndarray or None |
| 491 | mask represented as numpy array with dimensions (ny, nx) |
| 492 | chunk_pixels: int |
| 493 | The number of pixels in a single chunk. The XRF map will be rechunked so that |
| 494 | each block contains approximately `chunk_pixels` pixels and contain all `ne` |
| 495 | spectrum points for each pixel. |
| 496 | n_chunks_min: int |
| 497 | Minimum number of chunks. The algorithm will try to split the map into the number |
| 498 | of chunks equal or greater than `n_chunks_min`. |
| 499 | progress_bar: callable or None |
| 500 | reference to the callable object that implements progress bar. The example of |
| 501 | such a class for progress bar object is `TerminalProgressBar`. |
| 502 | client: dask.distributed.Client or None |
| 503 | Dask client. If None, then local client will be created |
| 504 | |
| 505 | Returns |
| 506 | ------- |
| 507 | result: ndarray |
| 508 | Spectrum averaged over the XRF dataset taking into account mask and selectied area. |
| 509 | """ |
| 510 | |
| 511 | if not isinstance(mask, np.ndarray) and (mask is not None): |
| 512 | raise TypeError(f"Parameter 'mask' must be a numpy array or None: type(mask) = {type(mask)}") |
| 513 | |
| 514 | data, file_obj = prepare_xrf_map(data, chunk_pixels=chunk_pixels, n_chunks_min=n_chunks_min) |
| 515 | mask = _prepare_xrf_mask(data, mask=mask, selection=selection) |
| 516 | |
| 517 | if client is None: |
| 518 | client = dask_client_create() |
| 519 | client_is_local = True |
| 520 | else: |
| 521 | client_is_local = False |
| 522 | |
| 523 | client.run(dask_set_custom_serializers) |
| 524 | dask_set_custom_serializers() |
| 525 | |
| 526 | n_workers = len(client.scheduler_info()["workers"]) |
| 527 | logger.info(f"Dask distributed client: {n_workers} workers") |
| 528 | |
| 529 | if mask is None: |
| 530 | result_fut = da.sum(da.sum(data, axis=0), axis=0).persist(scheduler=client) |
| 531 | else: |
| 532 | result_fut = da.blockwise(_masked_sum, "ijk", data, "ijk", mask, "ij", dtype="float").persist( |
| 533 | scheduler=client |
| 534 | ) |
| 535 |