Skip to content

Commit

Permalink
Refactor my_logspace to wl_logspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-neal committed Mar 25, 2019
1 parent f7672a4 commit 9b0cee6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
42 changes: 30 additions & 12 deletions eniric/resample.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
"""
Functions for file resampling.
"""
"""Functions for spectral resampling."""
from typing import Union

import numpy as np
from numpy import ndarray


def log_resample(
wavelength, sampling: Union[int, float], resolution: Union[int, float]
) -> np.ndarray:
) -> ndarray:
"""Resample spectrum with a given sampling per resolution element.
Uses faster method using log and powers of a base.
The base is (1.0 + 1.0/(sampling*resolution).
Parameters
----------
wavelength: np.ndarray
wavelength: numpy.ndarray
Wavelength array.
sampling: int, float
Points to sample per resolution element
resolution: int, float
Instrumental resolution
Uses faster method using log and powers of a base.
The base is (1.0 + 1.0/(sampling*resolution).
Returns
-------
logspace: ndarray
Array of points with a set sampling per resolution element.
Note
----
Almost equivalent to using
np.logspace(np.log(wavelength)/np.log(base), np.log(wavelength)/np.log(base),
np.log(wavelength_end / wavelength_start) / np.log(base), base).
Expand All @@ -33,10 +38,10 @@ def log_resample(

# Create grid using logarithms with base of (1.0 + 1.0/(sampling*resolution))
base = 1.0 + 1.0 / (float(sampling) * float(resolution)) # type : float
return my_logspace(wavelength_start, wavelength_end, base, end_point=True)
return wl_logspace(wavelength_start, wavelength_end, base, end_point=True)


def my_logspace(start, stop, base, end_point: bool = False):
def wl_logspace(start, stop, base, end_point: bool = False):
"""Like np.logspace but start and stop in wavelength units.
Parameters
Expand Down Expand Up @@ -65,14 +70,27 @@ def my_logspace(start, stop, base, end_point: bool = False):
return start * base ** powers


def log_chunks(wavelength, percent):
def log_chunks(wavelength: ndarray, percent: float) -> ndarray:
r"""Define the bounds at which $(\Delta \lambda)/\lambda = X\%$.
Allows spectrum to be split into chunks in which the size is X% of the given wavelength.
This takes logarithmic steps with a base of (1+X/100).
Parameters
----------
wavelength: ndarry
Wavelength array.
percent: float
Base step in percentage.
Returns
-------
logspace: ndarray
Array of points with a growth in wavelength spanned of a given percent.
"""
base = 1 + percent / 100.0
wl_min = np.nanmin(wavelength)
wl_max = np.nanmax(wavelength)

return my_logspace(wl_min, wl_max, base, end_point=True)
return wl_logspace(wl_min, wl_max, base, end_point=True)
12 changes: 6 additions & 6 deletions tests/test_resample.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from eniric.resample import log_resample, my_logspace
from eniric.resample import log_resample, wl_logspace


@pytest.mark.parametrize("wav_start, wav_stop", [(2.1, 2.2), (1657.11, 1695)])
Expand All @@ -25,10 +25,10 @@ def logspace_params(request):
return request.param


def test_my_logspace_endpoint(logspace_params):
def test_wl_logspace_endpoint(logspace_params):
start, stop, base = logspace_params
resample1 = my_logspace(start, stop, base=base, end_point=False)
resample2 = my_logspace(start, stop, base=base, end_point=True)
resample1 = wl_logspace(start, stop, base=base, end_point=False)
resample2 = wl_logspace(start, stop, base=base, end_point=True)

assert len(resample1) < len(resample2)

Expand All @@ -37,9 +37,9 @@ def test_my_logspace_endpoint(logspace_params):
assert resample2[-1] < stop * base ** 2 # Not big


def test_my_logspace_increases_by_bnd(logspace_params):
def test_wl_logspace_increases_by_bnd(logspace_params):
start, stop, base = logspace_params
resample1 = my_logspace(start, stop, base=base, end_point=False)
resample1 = wl_logspace(start, stop, base=base, end_point=False)
assert np.allclose(
resample1[1:] / resample1[:-1], base * np.ones_like(resample1[:-1])
)

0 comments on commit 9b0cee6

Please sign in to comment.