Skip to content

Commit

Permalink
Merge pull request #627 from E-W-Jones/norm_point
Browse files Browse the repository at this point in the history
Add an option to normalize at one point
  • Loading branch information
minouHub committed Apr 22, 2024
2 parents 1ddae00 + 8d5c94a commit 3596f14
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
57 changes: 43 additions & 14 deletions radis/spectrum/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5260,8 +5260,9 @@ def normalize(
how to normalize. ``'max'`` is the default but may not be suited for very
noisy experimental spectra. ``'area'`` will normalize the integral to 1.
``'mean'`` will normalize by the mean amplitude value
wrange: tuple
if not empty, normalize on this range
wrange: tuple of floats, float
normalize on this range if a tuple, else normalise at that point.
If an empty tuple, use the entire range.
wunit: ``"nm"``, ``"cm-1"``, ``"nm_vac"``
unit of the normalisation range above. If ``None``, use the
spectrum default waveunit.
Expand Down Expand Up @@ -5304,29 +5305,31 @@ def normalize(
if wunit is None:
wunit = s.get_waveunit()

if wrange is not None and len(wrange) > 0:
wmin, wmax = wrange
if isinstance(wrange, (int, float)):
# Use user provided point. Only makes sense to normalize using
# normalize_how='max', otherwise raise an error.
w, I = s.get(
var, wunit=wunit, Iunit=s.units[var], copy=False
) # (faster not to copy)
b = (w > wmin) & (w < wmax)
if normalize_how == "max":
norm = np.nanmax(I[b])
norm_unit = s.units[var]
elif normalize_how == "mean":
norm = np.nanmean(I[b])
# Find the value of I closest to the given w
norm = I[np.argmin(np.abs(w - wrange))]
norm_unit = s.units[var]
elif normalize_how == "area":
norm = np.abs(nantrapz(I[b], w[b]))
norm_unit = Unit(s.units[var]) * Unit(wunit)
elif normalize_how == "mean" or normalize_how == "area":
raise ValueError(
"Unexpected `normalize_how`: {0}. "
"For a single value wrange only `normalize_how='max'` is "
"defined.".format(normalize_how)
)
else:
raise ValueError(
"Unexpected `normalize_how`: {0}".format(normalize_how)
)

out = multiply(s, 1 / (norm * Unit(norm_unit)), inplace=inplace)

else:
elif len(wrange) == 0:
# Use entire wavelength range
if normalize_how == "max":
norm = np.nanmax(
s.get(var, wunit=wunit, Iunit=s.units[var], copy=False)[1]
Expand All @@ -5341,13 +5344,39 @@ def normalize(
w, I = s.get(var, wunit=wunit, Iunit=s.units[var], copy=False)
norm = nantrapz(I, w)
norm_unit = Unit(s.units[var]) * Unit(wunit)

else:
raise ValueError(
"Unexpected `normalize_how`: {0}".format(normalize_how)
)
# Ensure we use the same unit system!
out = multiply(s, 1 / (norm * Unit(norm_unit)), inplace=inplace)

elif len(wrange) == 2:
# Use the user provided range
wmin, wmax = wrange
w, I = s.get(
var, wunit=wunit, Iunit=s.units[var], copy=False
) # (faster not to copy)
b = (w > wmin) & (w < wmax)
if normalize_how == "max":
norm = np.nanmax(I[b])
norm_unit = s.units[var]
elif normalize_how == "mean":
norm = np.nanmean(I[b])
norm_unit = s.units[var]
elif normalize_how == "area":
norm = np.abs(nantrapz(I[b], w[b]))
norm_unit = Unit(s.units[var]) * Unit(wunit)
else:
raise ValueError(
"Unexpected `normalize_how`: {0}".format(normalize_how)
)

out = multiply(s, 1 / (norm * Unit(norm_unit)), inplace=inplace)

else:
raise ValueError("Unexpected `wrange`: {0}".format(wrange))

if verbose:
print("Normalization factor : {0}".format(norm))
if return_norm:
Expand Down
12 changes: 12 additions & 0 deletions radis/test/spectrum/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,18 @@ def test_normalization(*args, **kwargs):
s3.crop(2125, 2150).get_integral("radiance", wunit=s3.get_waveunit()), 1
)

# Test the spectrum can be normalized at a single point,
w, I = s.get("radiance")
s4 = s.normalize(wrange=w[18047])
assert np.isclose(s4.get("radiance")[1][18047], 1)

# and the errors are raised if a user tries to pass nonsense options.
with pytest.raises(ValueError):
s.normalize(wrange=w[18047], normalize_how="mean")

with pytest.raises(ValueError):
s.normalize(wrange=w[18047], normalize_how="area")


@pytest.mark.fast
def test_sort(*args, **kwargs):
Expand Down

0 comments on commit 3596f14

Please sign in to comment.