Skip to content

Commit

Permalink
Error handling for CubeFactory and TicaCubeFactory (#85)
Browse files Browse the repository at this point in the history
* Error handling implemented on CubeFactory

* Error handling for TicaCubeFactory

* codestyle

* New unit tests to verify error handling

* Adding astroquery as a dependency

* .

* .

* .

* Astroquery is a dependency required only for testing

* Making error message more general, and a global variable

* Making error msg even more general

* .
  • Loading branch information
jaymedina authored Jul 18, 2023
1 parent 0baa937 commit f0b4c45
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
14 changes: 11 additions & 3 deletions astrocut/make_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +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``."


class CubeFactory():
Expand Down Expand Up @@ -85,7 +87,10 @@ 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
slice_size = image_shape[1] * len(self.file_list) * 2 * 4 # in bytes (float32)
try:
slice_size = image_shape[1] * len(self.file_list) * 2 * 4 # in bytes (float32)
except IndexError:
raise ValueError(ERROR_MSG)
max_block_size = int((self.max_memory * 1e9)//slice_size)

self.num_blocks = int(image_shape[0]/max_block_size + 1)
Expand Down Expand Up @@ -310,7 +315,7 @@ def make_cube(self, file_list, cube_file="img-cube.fits", sector=None, max_memor

# Set up the basic cube parameters
sector = (sector, "Observing sector")

self._configure_cube(file_list, sector=sector)

if verbose:
Expand Down Expand Up @@ -405,7 +410,10 @@ def _configure_cube(self, file_list, **extra_keywords):
start_times[i] = ffi_data[0].header.get(self.time_keyword)

if image_shape is None: # Only need to fill this once
image_shape = ffi_data[0].data.shape
try:
image_shape = ffi_data[0].data.shape
except AttributeError:
raise ValueError(ERROR_MSG)

if self.template_file is None: # Only check this if we don't already have it

Expand Down
33 changes: 33 additions & 0 deletions astrocut/tests/test_make_cube.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import numpy as np
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 @@ -167,7 +170,37 @@ def test_iteration(tmpdir, capsys):

assert np.alltrue(cube_1 == ecube), "Cube values do not match expected values"


@pytest.mark.parametrize("ffi_type", ["TICA", "SPOC"])
def test_invalid_inputs(tmpdir, ffi_type):

coordinates = SkyCoord(289.0979, -29.3370, unit="deg")

# 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)

# 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)




Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ console_scripts =
[options.extras_require]
test =
pytest-astropy
astroquery>=0.4.6
docs =
sphinx != 4.1.0
docutils == 0.16
Expand Down
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ description =
astropy52: with astropy 5.2.*
numpy120: with numpy 1.20.*
numpy123: with numpy 1.23.*
astroquery04: with astroquery 0.4.*

# The following provides some specific pinnings for key packages
deps =
Expand All @@ -46,8 +47,11 @@ deps =

astropy52: astropy==5.2.*

astroquery04: astroquery==0.4.*

devdeps: git+https://github.com/numpy/numpy.git#egg=numpy
devdeps: git+https://github.com/astropy/astropy.git#egg=astropy
devdeps: git+https://github.com/astropy/astroquery.git

# The following indicates which extras_require from setup.cfg will be installed
extras =
Expand Down

0 comments on commit f0b4c45

Please sign in to comment.