Skip to content

Commit

Permalink
Merge pull request #197 from h2020charisma/hdr_custom_meta
Browse files Browse the repository at this point in the history
Custom meta names in hdr_from_multi_exposure
  • Loading branch information
georgievgeorgi authored Dec 12, 2024
2 parents d7b4e2b + eadabf2 commit 6bdf50b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/ramanchada2/spectrum/creators/from_local_file.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down
25 changes: 18 additions & 7 deletions src/ramanchada2/spectrum/creators/hdr_from_multi_exposure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down

0 comments on commit 6bdf50b

Please sign in to comment.