Skip to content

Commit

Permalink
Bugfix for implementation and test for invalid file type input in mak…
Browse files Browse the repository at this point in the history
…e_cube() (#129)
  • Loading branch information
snbianco authored Sep 24, 2024
1 parent 353af20 commit 890ee87
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
14 changes: 7 additions & 7 deletions astrocut/make_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
47 changes: 16 additions & 31 deletions astrocut/tests/test_make_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 890ee87

Please sign in to comment.