-
Notifications
You must be signed in to change notification settings - Fork 4
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
fix: handle sparse matrix reading for big-endian byte order #163
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -629,6 +629,12 @@ def _read_response(self, file: str, response_id: int) -> dict: | |
else: | ||
reduced_matrix = np.hstack(matrix) | ||
|
||
# if the byte order is not native, | ||
# then convert it to be compatible with scipy.sparse | ||
if reduced_matrix.dtype.byteorder != '=': | ||
order = reduced_matrix.dtype.newbyteorder() | ||
reduced_matrix = reduced_matrix.astype(order) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider centralizing duplicated byte order conversion logic. The same pattern for converting non‐native byte order appears in both this file and in 'base.py'. Extracting this logic into a shared utility function could improve maintainability and reduce code duplication. |
||
|
||
sparse_matrix = coo_array( | ||
(reduced_matrix, (rows, cols)), | ||
shape=(len(response_data), channel_number), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from elisa.data.grouping import significance_gv, significance_lima | ||
from elisa.data.ogip import Response, ResponseData | ||
from elisa.models import PowerLaw | ||
|
||
|
||
|
@@ -117,3 +119,28 @@ def test_data_grouping(): | |
data.spec_counts, data.back_counts, data.back_errors, data.back_ratio | ||
) | ||
assert np.all(sig >= scale) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'file', | ||
[ | ||
'../docs/notebooks/data/P011160500104_LE.rsp', | ||
'../docs/notebooks/data/P011160500104_ME.rsp', | ||
'../docs/notebooks/data/P011160500104_HE.rsp', | ||
], | ||
) | ||
def test_load_response(file): | ||
# Test Response against big-endian files | ||
assert np.all(Response(file).channel_fwhm > 0) | ||
|
||
|
||
def test_response(): | ||
# Test ResponseData against different endianness | ||
photon_egrid = np.linspace(1.0, 100.0, 101) | ||
channel = np.arange(100) | ||
channel_emin = photon_egrid[:-1] | ||
channel_emax = photon_egrid[1:] | ||
matrix1 = np.eye(100).astype('<f4') | ||
matrix2 = np.eye(100).astype('>f4') | ||
ResponseData(photon_egrid, channel_emin, channel_emax, matrix1, channel) | ||
ResponseData(photon_egrid, channel_emin, channel_emax, matrix2, channel) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (testing): These tests are missing assertions. Simply creating the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question (bug_risk): Revisit the conditional conversion for response_matrix.
Currently, the conversion to native byte order is skipped if response_matrix is already an instance of sparray. Ensure that sparse matrices are always natively ordered or consider if they might also need conversion to avoid potential incompatibility issues with scipy.sparse.