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

extra dump before decode #287

Merged
merged 8 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion oda_api/data_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import typing

import traceback

from json_tricks import numpy_encode,dumps,loads,numeric_types_hook,hashodict,json_numpy_obj_hook
from astropy.io import fits as pf
from astropy.io import ascii as astropy_io_ascii
Expand Down Expand Up @@ -295,9 +297,12 @@
if name is None or name == '':
name=hdu.name

return cls(data=hdu.data,
r = cls(data=hdu.data,
data_header={k:v for k, v in hdu.header.items()},
hdu_type=cls._map_hdu_type(hdu),name=name)
# this is needed to re-read the file due to variable length file
r.to_fits_hdu()
return r


def to_fits_hdu(self):
Expand Down Expand Up @@ -325,6 +330,9 @@
header=pf.header.Header(self.header),
hdu_type=self.hdu_type,units_dict=self.units_dict)
except Exception as e:
error_message = 'an exception occurred in oda_api when binary products are formatted to fits header: ' + repr(e)
logger.error(error_message)
logger.error('traceback: %s', traceback.format_exc())

Check warning on line 335 in oda_api/data_products.py

View check run for this annotation

Codecov / codecov/patch

oda_api/data_products.py#L333-L335

Added lines #L333 - L335 were not covered by tests
raise Exception("an exception occurred in oda_api when binary products are formatted to fits header: " + repr(e))


Expand Down Expand Up @@ -638,6 +646,7 @@
hdul=_hdul

return cls(data_unit=[NumpyDataUnit.from_fits_hdu(h) for h in hdul],meta_data=meta_data,name=name)


@classmethod
def decode(cls, encoded_obj: typing.Union[str, dict], from_json=False):
Expand Down
Binary file added tests/test_data/isgri_rmf_Crab.fits
Binary file not shown.
51 changes: 50 additions & 1 deletion tests/test_data_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
from astropy.table import Table
from matplotlib import pyplot as plt

import base64
import pickle

secret_key = 'secretkey_test'
default_exp_time = int(time.time()) + 5000
default_token_payload = dict(
Expand All @@ -33,6 +36,52 @@
mssub=True
)


def test_variable_length_rmf():
# "compressed" (by removing small matrix values, special RMF spec) RMF is stored in variable length table
# which is not well supported by astropy

isgri_rmf_dp_post_scan = NumpyDataProduct.from_fits_file("tests/test_data/isgri_rmf_Crab.fits")
isgri_rmf_dp_post_scan.data_unit[2].to_fits_hdu()
encoded_numpy_data_prod_postscan = isgri_rmf_dp_post_scan.encode()
decoded_numpy_data_prod_postscan = NumpyDataProduct.decode(encoded_numpy_data_prod_postscan)
decoded_numpy_data_prod_postscan.data_unit[2].to_fits_hdu()

isgri_rmf_dp = NumpyDataProduct.from_fits_file("tests/test_data/isgri_rmf_Crab.fits")
encoded_numpy_data_prod = isgri_rmf_dp.encode()
decoded_numpy_data_prod = NumpyDataProduct.decode(encoded_numpy_data_prod)
decoded_numpy_data_prod.data_unit[2].to_fits_hdu()


def test_rmf():
isgri_rmf_dp = NumpyDataProduct.from_fits_file("tests/test_data/isgri_rmf_Crab.fits")

for ID, _d in enumerate(isgri_rmf_dp.data_unit):
print(ID, _d.header['EXTNAME'], _d.to_fits_hdu())

encoded_numpy_data_prod = isgri_rmf_dp.encode()
decoded_numpy_data_prod = NumpyDataProduct.decode(encoded_numpy_data_prod)

# this is the higher-level call causing the above error in nb2workflow
_hdul = fits.HDUList()
for ID, _d in enumerate(decoded_numpy_data_prod.data_unit):
print(ID, _d.header['EXTNAME'])
try:
_hdul.append(_d.to_fits_hdu())
except Exception as ee:

Check warning on line 71 in tests/test_data_products.py

View check run for this annotation

Codecov / codecov/patch

tests/test_data_products.py#L71

Added line #L71 was not covered by tests
# print(ee)
raise Exception(ee)

Check warning on line 73 in tests/test_data_products.py

View check run for this annotation

Codecov / codecov/patch

tests/test_data_products.py#L73

Added line #L73 was not covered by tests

# this reproduces the commands done inside the above call (?)
binarys = base64.b64decode(encoded_numpy_data_prod['data_unit_list'][2]['binarys'])
try:
pickle.loads(binarys, encoding='bytes')
print('pickle test successful')
except Exception as ee:
raise Exception(ee)

Check warning on line 81 in tests/test_data_products.py

View check run for this annotation

Codecov / codecov/patch

tests/test_data_products.py#L80-L81

Added lines #L80 - L81 were not covered by tests



# TODO: adapt to new product types and implement corresponding tests
def encode_decode(ndp: typing.Union[NumpyDataProduct,
ODAAstropyTable,
Expand Down Expand Up @@ -188,4 +237,4 @@
decoded.write_file('decbinprd.foo')
assert filecmp.cmp(infile, 'decbinprd.foo')
os.remove('decbinprd.foo')


Loading