From eadabf28c910b17d483abc7cdb615c1ff8ad08d3 Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Tue, 10 Dec 2024 00:39:47 +0200 Subject: [PATCH] Custom meta names in hdr_from_multi_exposure Also custom metadata can be provided in from_local_file --- .../spectrum/creators/from_local_file.py | 9 ++++--- .../creators/hdr_from_multi_exposure.py | 25 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/ramanchada2/spectrum/creators/from_local_file.py b/src/ramanchada2/spectrum/creators/from_local_file.py index fdea1fd8..4b200c0e 100644 --- a/src/ramanchada2/spectrum/creators/from_local_file.py +++ b/src/ramanchada2/spectrum/creators/from_local_file.py @@ -1,7 +1,7 @@ """Create spectrum from local files.""" import os -from typing import Literal, Union +from typing import Literal, Union, Dict import spc_io from pydantic import validate_call @@ -22,7 +22,8 @@ def from_local_file( filetype: Union[None, Literal['spc', 'sp', 'spa', '0', '1', '2', 'wdf', 'ngs', 'jdx', 'dx', 'txt', 'txtr', 'csv', 'prn', 'rruf', 'spe', 'cha']] = None, - backend: Union[None, Literal['native', 'rc1_parser']] = None): + backend: Union[None, Literal['native', 'rc1_parser']] = None, + custom_meta: Dict = {}): """ Read experimental spectrum from a local file. @@ -35,6 +36,8 @@ def from_local_file( `None` used to determine by extension of the file. backend: `native`, `rc1_parser` or `None`. `None` means both. + custom_meta: Dict + Add custom metadata fields in the created spectrum Raises: ValueError: @@ -63,10 +66,10 @@ def load_native(): meta = spc.log_book.text elif ft in {'spe'}: x, y, meta = read_spe(in_file_name) - spe = Spectrum(x=x, y=y, metadata=meta) # type: ignore else: raise ValueError(f'filetype {ft} not supported') meta["Original file"] = os.path.basename(in_file_name) + meta.update(custom_meta) spe = Spectrum(x=x, y=y, metadata=meta) # type: ignore return spe diff --git a/src/ramanchada2/spectrum/creators/hdr_from_multi_exposure.py b/src/ramanchada2/spectrum/creators/hdr_from_multi_exposure.py index dffa1166..5a895bdc 100644 --- a/src/ramanchada2/spectrum/creators/hdr_from_multi_exposure.py +++ b/src/ramanchada2/spectrum/creators/hdr_from_multi_exposure.py @@ -10,22 +10,33 @@ @add_spectrum_constructor(set_applied_processing=True) @validate_call(config=dict(arbitrary_types_allowed=True)) -def hdr_from_multi_exposure(spes_in: List[Spectrum]): - """Create an HDR spectrum from several spectra with different exposures. +def hdr_from_multi_exposure(spes_in: List[Spectrum], + meta_exposure_time: str = 'intigration times(ms)', + meta_ymax: str = 'yaxis_max'): + """ + Create an HDR spectrum from several spectra with different exposures. The resulting spectrum will have the details in low-intensity peaks from long-exposure-time spectrum. As long-exposure-time spectrum might be sturated, the information for high-intensity peaks will be taken from short-exposure-time spectrum. - This function will work on a very limited number of spectra, - because we still do not have standardized metadata. + + Args: + spes_in (List[Spectrum]): Set of spectra with different exposures + meta_exposure_time (str, optional): The name of the metadata parameter + having the exposure/integration time. The units should be the same + for all the spectra. Defaults to 'intigration times(ms)'. + meta_ymax (str, optional): The name fo the metadata parameter having + the maximum ADC value. This value will be used as a threshold. If + value in a spectrum is higher, the value will be taken from a + spectrum with shorter exposure. Defaults to 'yaxis_max'. """ - spes = list(sorted(spes_in, key=lambda s: float(s.meta['intigration times(ms)']))) # type: ignore + spes = list(sorted(spes_in, key=lambda s: float(s.meta[meta_exposure_time]))) # type: ignore if not np.all([spes[0].x == s.x for s in spes]): raise ValueError('x-axes of the spectra should be equal') - spes_cpms = np.array([s.y / float(s.meta['intigration times(ms)']) for s in spes]) # type: ignore - masks = np.array(list(map(lambda s: s.y > s.meta['yaxis_max'], spes))) # type: ignore + spes_cpms = np.array([s.y / float(s.meta[meta_exposure_time]) for s in spes]) # type: ignore + masks = np.array(list(map(lambda s: s.y > s.meta[meta_ymax], spes))) # type: ignore y = spes_cpms[0] for si in range(1, len(spes_cpms)): y[~masks[si]] = spes_cpms[si][~masks[si]]