From 890ee8751ab0bf4177bf683741d665a21e53ab94 Mon Sep 17 00:00:00 2001 From: Sam Bianco <70121323+snbianco@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:13:03 -0400 Subject: [PATCH] Bugfix for implementation and test for invalid file type input in make_cube() (#129) --- astrocut/make_cube.py | 14 +++++----- astrocut/tests/test_make_cube.py | 47 +++++++++++--------------------- 2 files changed, 23 insertions(+), 38 deletions(-) diff --git a/astrocut/make_cube.py b/astrocut/make_cube.py index 53221693..feeb2020 100644 --- a/astrocut/make_cube.py +++ b/astrocut/make_cube.py @@ -19,8 +19,8 @@ from mmap import MADV_SEQUENTIAL __all__ = ['CubeFactory', 'TicaCubeFactory'] -ERROR_MSG = "One or more incorrect file types were input. Please input TICA FFI files when using\ - ``TicaCubeFactory``, and SPOC FFI files when using ``CubeFactory``." +ERROR_MSG = ("One or more incorrect file types were input. Please input TICA FFI files when using " + "``TicaCubeFactory``, and SPOC FFI files when using ``CubeFactory``.") class CubeFactory(): @@ -66,7 +66,10 @@ def _configure_cube(self, file_list, **extra_keywords): ffi_data = fits.open(ffi, mode='denywrite', memmap=True) - start_times[i] = ffi_data[1].header.get(self.time_keyword) + try: + start_times[i] = ffi_data[1].header.get(self.time_keyword) + except IndexError: + raise ValueError(ERROR_MSG) if image_shape is None: # Only need to fill this once image_shape = ffi_data[1].data.shape @@ -87,10 +90,7 @@ def _configure_cube(self, file_list, **extra_keywords): # Working out the block size and number of blocks needed for writing the cube # without using too much memory - try: - slice_size = image_shape[1] * len(self.file_list) * 2 * 4 # in bytes (float32) - except IndexError: - raise ValueError(ERROR_MSG) + slice_size = image_shape[1] * len(self.file_list) * 2 * 4 # in bytes (float32) max_block_size = int((self.max_memory * 1e9)//slice_size) self.num_blocks = int(image_shape[0]/max_block_size + 1) diff --git a/astrocut/tests/test_make_cube.py b/astrocut/tests/test_make_cube.py index a9213a91..63c3ed43 100644 --- a/astrocut/tests/test_make_cube.py +++ b/astrocut/tests/test_make_cube.py @@ -2,10 +2,8 @@ import os import pytest -from astropy.coordinates import SkyCoord from astropy.io import fits from astropy.table import Table -from astroquery.mast import Observations from re import findall from os import path @@ -173,36 +171,23 @@ def test_iteration(tmpdir, capsys): @pytest.mark.parametrize("ffi_type", ["TICA", "SPOC"]) def test_invalid_inputs(tmpdir, ffi_type): - - coordinates = SkyCoord(289.0979, -29.3370, unit="deg") - + """ + Test that an error is raised when users attempt to make cubes with an invalid file type. + """ # Assigning some variables - target_name = "TICA FFI" if ffi_type == "TICA" else "TESS FFI" - value_error = "One or more incorrect file types were input. Please input TICA FFI files when using\ - ``TicaCubeFactory``, and SPOC FFI files when using ``CubeFactory``." - - # Getting TESS sector 27 observations for the given coordinate - observations = Observations.query_criteria(coordinates=coordinates, - target_name=target_name, - dataproduct_type="image", - sequence_number=27) + product = "TICA" if ffi_type == "TICA" else "SPOC" + value_error = ("One or more incorrect file types were input. Please input TICA FFI files when using " + "``TicaCubeFactory``, and SPOC FFI files when using ``CubeFactory``.") - # Getting a list of products. Keeping it small so we don't have to download so many. - products = Observations.get_product_list(observations[0])[:2] - - manifest = Observations.download_products(products, download_dir=str(tmpdir)) - - if ffi_type == "TICA": - cube_maker = CubeFactory() - elif ffi_type == "SPOC": - cube_maker = TicaCubeFactory() - - with pytest.raises(ValueError) as error_msg: - cube_maker.make_cube(manifest["Local Path"]) - assert value_error in str(error_msg.value) - + # Create test FFI files + num_images = 100 + ffi_files = create_test_ffis(img_size=10, + num_images=num_images, + dir_name=tmpdir, + product=product) + cube_maker = CubeFactory() if ffi_type == "TICA" else TicaCubeFactory() - - - + # Should raise a Value Error due to incorrect file type + with pytest.raises(ValueError, match=value_error): + cube_maker.make_cube(ffi_files)