Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache fourier sin/cos computation #236

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions src/ctapipe_io_lst/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
import astropy.units as u
from numba import njit
from numba.typed import Dict
from numba.core import types
import tables

from ctapipe.core import TelescopeComponent
Expand Down Expand Up @@ -1102,18 +1104,36 @@





_fourier_cache_key_type = types.int16
_fourier_cache_value_type = types.UniTuple(types.float64[:], 2)


@njit(cache=True)
def _create_fourier_cache():
return Dict.empty(

Check warning on line 1115 in src/ctapipe_io_lst/calibration.py

View check run for this annotation

Codecov / codecov/patch

src/ctapipe_io_lst/calibration.py#L1115

Added line #L1115 was not covered by tests
key_type=_fourier_cache_key_type,
value_type=_fourier_cache_value_type,
)


@njit(cache=True)
def calc_drs4_time_correction_gain_selected(
first_capacitors, selected_gain_channel, fan, fbn
):
_n_gains, n_pixels, n_harmonics = fan.shape
time = np.zeros(n_pixels)

cache = _create_fourier_cache()

Check warning on line 1128 in src/ctapipe_io_lst/calibration.py

View check run for this annotation

Codecov / codecov/patch

src/ctapipe_io_lst/calibration.py#L1128

Added line #L1128 was not covered by tests
for pixel in range(n_pixels):
gain = selected_gain_channel[pixel]
first_capacitor = first_capacitors[gain, pixel]
time[pixel] = calc_fourier_time_correction(
first_capacitor, fan[gain, pixel], fbn[gain, pixel]
first_capacitor,
fan[gain, pixel],
fbn[gain, pixel],
cache,
)
return time

Expand All @@ -1124,28 +1144,40 @@
):
time = np.zeros((N_GAINS, N_PIXELS))

cache = _create_fourier_cache()

Check warning on line 1147 in src/ctapipe_io_lst/calibration.py

View check run for this annotation

Codecov / codecov/patch

src/ctapipe_io_lst/calibration.py#L1147

Added line #L1147 was not covered by tests
for gain in range(N_GAINS):
for pixel in range(N_PIXELS):
first_capacitor = first_capacitors[gain, pixel]
time[gain, pixel] = calc_fourier_time_correction(
first_capacitor, fan[gain, pixel], fbn[gain, pixel]
first_capacitor,
fan[gain, pixel],
fbn[gain, pixel],
cache,
)
return time



@njit(cache=True)
def calc_fourier_time_correction(first_capacitor, fan, fbn):
def calc_fourier_time_correction(first_capacitor, fan, fbn, cache):
n_harmonics = len(fan)

time = 0
first_capacitor = first_capacitor % N_CAPACITORS_CHANNEL

for harmonic in range(1, n_harmonics):
a = fan[harmonic]
b = fbn[harmonic]
omega = harmonic * (2 * np.pi / N_CAPACITORS_CHANNEL)
cache_key = types.int16(first_capacitor)
cache_val = cache.get(cache_key)
if cache_val is not None:
sin_terms, cos_terms = cache_val

Check warning on line 1171 in src/ctapipe_io_lst/calibration.py

View check run for this annotation

Codecov / codecov/patch

src/ctapipe_io_lst/calibration.py#L1168-L1171

Added lines #L1168 - L1171 were not covered by tests
else:
n = np.arange(n_harmonics)
omega = 2 * np.pi / N_CAPACITORS_CHANNEL
sin_terms = np.sin(n * omega * first_capacitor)
cos_terms = np.cos(n * omega * first_capacitor)
cache[cache_key] = sin_terms, cos_terms

Check warning on line 1177 in src/ctapipe_io_lst/calibration.py

View check run for this annotation

Codecov / codecov/patch

src/ctapipe_io_lst/calibration.py#L1173-L1177

Added lines #L1173 - L1177 were not covered by tests

time += a * np.cos(omega * first_capacitor)
time += b * np.sin(omega * first_capacitor)
for harmonic in range(1, n_harmonics):
time += fan[harmonic] * cos_terms[harmonic]
time += fbn[harmonic] * sin_terms[harmonic]

Check warning on line 1181 in src/ctapipe_io_lst/calibration.py

View check run for this annotation

Codecov / codecov/patch

src/ctapipe_io_lst/calibration.py#L1179-L1181

Added lines #L1179 - L1181 were not covered by tests

return time
Loading