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

JP-3121: initial Code to Implement C-Extensions for Ramp Fit #156

Merged
merged 31 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ca050b0
Adding initial C code to ramp fitting.
kmacdonald-stsci Mar 8, 2023
c36859a
Updating the dark current processing and skipping a failing test.
kmacdonald-stsci Apr 22, 2024
569a660
Cleaning up code.
kmacdonald-stsci Apr 22, 2024
5137e88
Changing long integration test to have the correct dtype for the ramp…
kmacdonald-stsci Apr 22, 2024
ccd4d14
Updating comments.
kmacdonald-stsci Apr 22, 2024
e3848d5
Updating the STCAL code to use the algorithm class variable in the JW…
kmacdonald-stsci Apr 24, 2024
805a6d1
Removing unnecessary symbol.
kmacdonald-stsci May 1, 2024
888e843
Simplifying returns and removing labels.
kmacdonald-stsci May 1, 2024
56ea25e
Checking return values and properly deallocating memory for the optio…
kmacdonald-stsci May 1, 2024
d146ace
Checking return values and managing memory when errors occur for pack…
kmacdonald-stsci May 1, 2024
b383cd6
Removing endianness references to ramp data getters.
kmacdonald-stsci May 1, 2024
cdda0d4
Removing usage of a PyDataMem_FREE in favor of Py_XDECREF.
kmacdonald-stsci May 1, 2024
53aa644
Initializing variable at declaration.
kmacdonald-stsci May 1, 2024
d01ff19
Updating C code for better memory management. Updated change log. R…
kmacdonald-stsci May 7, 2024
d57e43e
Removing unused functions.
kmacdonald-stsci May 7, 2024
201fa0d
Removing unused functions and updating comments.
kmacdonald-stsci May 7, 2024
4a5b7bf
Cleaning up the code, removing unused functions, and expanding comments.
kmacdonald-stsci May 7, 2024
61440fc
Cleaning up code. Updating comments.
kmacdonald-stsci May 7, 2024
76f3528
Updating comments.
kmacdonald-stsci May 7, 2024
501f16a
Updating comments.
kmacdonald-stsci May 7, 2024
1cddaae
Adding extension build tests according to code review feedback.
kmacdonald-stsci May 7, 2024
e0b1c12
Changing the use of C as default to use of the python code as default.
kmacdonald-stsci May 7, 2024
8a3c990
Updating the use of the C code to be able to be selected programmatic…
kmacdonald-stsci May 7, 2024
a8417f4
Adding logging for processing time.
kmacdonald-stsci May 15, 2024
aee16b8
Forcing python code usage.
kmacdonald-stsci May 15, 2024
f135a39
Default to python code, but the C extension can be selected by using …
kmacdonald-stsci May 15, 2024
35b616d
Updating comments.
kmacdonald-stsci May 15, 2024
ba902e5
Expand comments related to endian issues for the C extension.
kmacdonald-stsci May 16, 2024
832a5ea
Expanding comments for clarity.
kmacdonald-stsci May 16, 2024
b476a50
Using incorrect variable name causing errors.
kmacdonald-stsci May 16, 2024
9a44330
Merge branch 'main' into np_c_ext
hbushouse May 20, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ jobs:
# Until we have arm64 runners, we can't automatically test arm64 wheels
- cp3*-macosx_arm64
sdist: true
test_command: python -c "from stcal.ramp_fitting.ols_cas22 import _ramp, _jump, _fit"
test_command: python -c "from stcal.ramp_fitting.ols_cas22 import _ramp, _jump, _fit; from stcal.ramp_fitting import slope_fitter"
secrets:
pypi_token: ${{ secrets.PYPI_PASSWORD_STSCI_MAINTAINER }}
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ ramp_fitting
for catching all-zero variance cases when average dark current was not
specified. [#255]

- Refactor ramp fitting using a C extension to improve performance. [#156]

1.7.0 (2024-03-25)
==================

Expand Down
38 changes: 23 additions & 15 deletions docs/stcal/ramp_fitting/description.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,27 @@ the least-squares fit is calculated with the first and last samples. In most pra
cases, the data will fall somewhere in between, where the weighting is scaled between the
two extremes.

The signal-to-noise ratio :math:`S` used for weighting selection is calculated from the
last sample as:

For segment :math:`k` of length :math:`n`, which includes groups :math:`[g_{k}, ...,
g_{k+n-1}]`, the signal-to-noise ratio :math:`S` used for weighting selection is
calculated from the last sample as:

.. math::
S = \frac{data \times gain} { \sqrt{(read\_noise)^2 + (data \times gain) } } \,,

where :math:`data = g_{k+n-1} - g_{k}`.

The weighting for a sample :math:`i` is given as:

.. math::
w_i = (i - i_{midpoint})^P \,,
w_i = \frac{ [(i - i_{midpoint}) / i_{midpoint}]^P }{ (read\_noise)^2 } \,,

where :math:`i_{midpoint} = \frac{n-1}{2}` and :math:`i = 0, 1, ..., n-1`.

where :math:`i_{midpoint}` is the the sample number of the midpoint of the sequence, and
:math:`P` is the exponent applied to weights, determined by the value of :math:`S`. Fixsen
et al. 2000 found that defining a small number of P values to apply to values of S was
sufficient; they are given as:

is the the sample number of the midpoint of the sequence, and :math:`P` is the exponent
applied to weights, determined by the value of :math:`S`. Fixsen et al. 2000 found that
defining a small number of P values to apply to values of S was sufficient; they are given as:

+-------------------+------------------------+----------+
| Minimum S | Maximum S | P |
Expand All @@ -185,12 +191,14 @@ Segment-specific Computations
+++++++++++++++++++++++++++++
The variance of the slope of a segment due to read noise is:

.. math::
var^R_{s} = \frac{12 \ R^2 }{ (ngroups_{s}^3 - ngroups_{s})(tgroup^2) } \,,
.. math::
var^R_{s} = \frac{12 \ R^2 }{ (ngroups_{s}^3 - ngroups_{s})(tgroup^2)(gain^2) } \,,

where :math:`R` is the noise in the difference between 2 frames,
:math:`ngroups_{s}` is the number of groups in the segment, and :math:`tgroup` is the group
time in seconds (from the keyword TGROUP).
where :math:`R` is the noise in the difference between 2 frames,
:math:`ngroups_{s}` is the number of groups in the segment, and :math:`tgroup` is the group
time in seconds (from the keyword TGROUP). The divide by gain converts to
:math:`DN`. For the special case where as segment has length 1, the
:math:`ngroups_{s}` is set to :math:`2`.

The variance of the slope in a segment due to Poisson noise is:

Expand Down Expand Up @@ -258,10 +266,10 @@ The combined variance of the slope is the sum of the variances:
The square-root of the combined variance is stored in the ERR array of the output product.

The overall slope depends on the slope and the combined variance of the slope of each integration's
segments, and hence is a sum over integrations and segments:
segments, so is a sum over integration values computed from the segements:

.. math::
slope_{o} = \frac{ \sum_{i,s}{ \frac{slope_{i,s}} {var^C_{i,s}}}} { \sum_{i,s}{ \frac{1} {var^C_{i,s}}}}
.. math::
slope_{o} = \frac{ \sum_{i}{ \frac{slope_{i}} {var^C_{i}}}} { \sum_{i}{ \frac{1} {var^C_{i}}}}


.. _ramp_error_propagation:
Expand Down
17 changes: 17 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
Options.docstrings = True
Options.annotate = False

# package_data values are glob patterns relative to each specific subpackage.
package_data = {

Check warning on line 10 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L10

Added line #L10 was not covered by tests
"stcal.ramp_fitting.src": ["*.c"],
}

# Setup C module include directories
include_dirs = [np.get_include()]

Check warning on line 15 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L15

Added line #L15 was not covered by tests

# Setup C module macros
define_macros = [("NUMPY", "1")]

Check warning on line 18 in setup.py

View check run for this annotation

Codecov / codecov/patch

setup.py#L18

Added line #L18 was not covered by tests

# importing these extension modules is tested in `.github/workflows/build.yml`;
# when adding new modules here, make sure to add them to the `test_command` entry there
extensions = [
Expand All @@ -27,6 +38,12 @@
include_dirs=[np.get_include()],
language="c++",
),
Extension(
"stcal.ramp_fitting.slope_fitter",
["src/stcal/ramp_fitting/src/slope_fitter.c"],
include_dirs=include_dirs,
define_macros=define_macros,
),
]

setup(ext_modules=cythonize(extensions))
Loading
Loading