Skip to content
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ test_autoarray/mask/files/mask/mask_out.fits
test_autoarray/plot/wrap/base/files/
test_autoarray/structures/grids/files/grid_2d.fits

test_autoarray/dataset/files/array/output_test/uv_wavelengths.fits
test_autoarray/dataset/files/array/output_test/visibilities.fits
# Test output: tests write results into output_test/ dirs and read them back.
# Never track them -- a change to the autonerves FITS writer would otherwise
# rewrite a committed binary and dirty every contributor's tree (PyAutoArray#483).
test_autoarray/**/output_test/
test_autoarray/dataset/plot/files/
test_autoarray/fit/plot/files/
test_autoarray/structures/arrays/one_d/files/array/
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
58 changes: 19 additions & 39 deletions test_autoarray/dataset/imaging/test_dataset.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import copy
import os

import numpy as np
import pytest
import shutil

import autoarray as aa

Expand All @@ -13,18 +11,6 @@
test_data_path = Path(Path(__file__).resolve().parent) / "files"


@pytest.fixture(name="test_data_path")
def make_test_data_path():
test_data_path = Path(__file__).resolve().parent / "files" / "array" / "output_test"

if test_data_path.exists():
shutil.rmtree(test_data_path)

os.makedirs(test_data_path)

return test_data_path


def test__grid__uses_mask_and_settings__lp_grid_matches_grid_2d_7x7(
image_7x7,
noise_map_7x7,
Expand Down Expand Up @@ -162,27 +148,25 @@ def test__from_fits__all_data_in_one_fits_file_multiple_hdus__loads_data_psf_noi
assert dataset.noise_map.mask.pixel_scales == (0.1, 0.1)


def test__from_fits__small_datasets_env_caps_data_and_noise_map(
test_data_path, monkeypatch
):
def test__from_fits__small_datasets_env_caps_data_and_noise_map(tmp_path, monkeypatch):
"""When PYAUTO_SMALL_DATASETS=1, Imaging.from_fits center-crops data and
noise_map to (15, 15) at pixel_scales=0.6 so they stay shape-consistent
with masks built via Mask2D.circular under the same env var. PSF is left
alone."""
from astropy.io import fits

fits.writeto(
Path(test_data_path) / "data_30x30.fits",
tmp_path / "data_30x30.fits",
data=np.ones((30, 30), dtype=np.float64),
overwrite=True,
)
fits.writeto(
Path(test_data_path) / "noise_map_30x30.fits",
tmp_path / "noise_map_30x30.fits",
data=2.0 * np.ones((30, 30), dtype=np.float64),
overwrite=True,
)
fits.writeto(
Path(test_data_path) / "psf_5x5.fits",
tmp_path / "psf_5x5.fits",
data=(1.0 / 25.0) * np.ones((5, 5), dtype=np.float64),
overwrite=True,
)
Expand All @@ -191,9 +175,9 @@ def test__from_fits__small_datasets_env_caps_data_and_noise_map(

dataset = aa.Imaging.from_fits(
pixel_scales=0.08,
data_path=Path(test_data_path) / "data_30x30.fits",
psf_path=Path(test_data_path) / "psf_5x5.fits",
noise_map_path=Path(test_data_path) / "noise_map_30x30.fits",
data_path=tmp_path / "data_30x30.fits",
psf_path=tmp_path / "psf_5x5.fits",
noise_map_path=tmp_path / "noise_map_30x30.fits",
)

assert dataset.data.shape_native == (16, 16)
Expand All @@ -202,20 +186,18 @@ def test__from_fits__small_datasets_env_caps_data_and_noise_map(
assert dataset.psf.kernel.shape_native == (5, 5)


def test__from_fits__small_datasets_env_unset__shape_unchanged(
test_data_path, monkeypatch
):
def test__from_fits__small_datasets_env_unset__shape_unchanged(tmp_path, monkeypatch):
"""Sanity: with the env var unset, from_fits returns the on-disk shape
unchanged, even for files larger than the cap."""
from astropy.io import fits

fits.writeto(
Path(test_data_path) / "data_30x30.fits",
tmp_path / "data_30x30.fits",
data=np.ones((30, 30), dtype=np.float64),
overwrite=True,
)
fits.writeto(
Path(test_data_path) / "noise_map_30x30.fits",
tmp_path / "noise_map_30x30.fits",
data=2.0 * np.ones((30, 30), dtype=np.float64),
overwrite=True,
)
Expand All @@ -224,34 +206,32 @@ def test__from_fits__small_datasets_env_unset__shape_unchanged(

dataset = aa.Imaging.from_fits(
pixel_scales=0.08,
data_path=Path(test_data_path) / "data_30x30.fits",
noise_map_path=Path(test_data_path) / "noise_map_30x30.fits",
data_path=tmp_path / "data_30x30.fits",
noise_map_path=tmp_path / "noise_map_30x30.fits",
)

assert dataset.data.shape_native == (30, 30)
assert dataset.noise_map.shape_native == (30, 30)
assert dataset.pixel_scales == (0.08, 0.08)


def test__output_to_fits__round_trips_data_psf_noise_map_correctly(
imaging_7x7, test_data_path
):
def test__output_to_fits__round_trips_data_psf_noise_map_correctly(imaging_7x7, tmp_path):

from autoarray.dataset.plot.imaging_plots import fits_imaging

fits_imaging(
dataset=imaging_7x7,
data_path=Path(test_data_path) / "data.fits",
psf_path=Path(test_data_path) / "psf.fits",
noise_map_path=Path(test_data_path) / "noise_map.fits",
data_path=tmp_path / "data.fits",
psf_path=tmp_path / "psf.fits",
noise_map_path=tmp_path / "noise_map.fits",
overwrite=True,
)

dataset = aa.Imaging.from_fits(
pixel_scales=0.1,
data_path=Path(test_data_path) / "data.fits",
psf_path=Path(test_data_path) / "psf.fits",
noise_map_path=Path(test_data_path) / "noise_map.fits",
data_path=tmp_path / "data.fits",
psf_path=tmp_path / "psf.fits",
noise_map_path=tmp_path / "noise_map.fits",
)

assert (dataset.data.native == np.ones((7, 7))).all()
Expand Down
23 changes: 7 additions & 16 deletions test_autoarray/dataset/interferometer/test_dataset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import numpy as np
import os
import shutil

import autoarray as aa
import pytest
Expand Down Expand Up @@ -127,7 +125,7 @@ def test__from_fits__all_files_in_one_fits__load_using_different_hdus(mask_2d_7x
assert (dataset.uv_wavelengths[:, 1] == 3.0 * np.ones(3)).all()


def test__output_all_arrays(mask_2d_7x7):
def test__output_all_arrays(mask_2d_7x7, tmp_path):
test_data_path = Path(Path(__file__).resolve().parent) / "files"

dataset = aa.Interferometer.from_fits(
Expand All @@ -137,28 +135,21 @@ def test__output_all_arrays(mask_2d_7x7):
uv_wavelengths_path=Path(test_data_path) / "3x2_fives_sixes.fits",
)

test_data_path = Path(Path(__file__).resolve().parent) / "files" / "array" / "output_test"

if Path(test_data_path).exists():
shutil.rmtree(test_data_path)

os.makedirs(test_data_path)

from autoarray.dataset.plot.interferometer_plots import fits_interferometer

fits_interferometer(
dataset=dataset,
data_path=Path(test_data_path) / "data.fits",
noise_map_path=Path(test_data_path) / "noise_map.fits",
uv_wavelengths_path=Path(test_data_path) / "uv_wavelengths.fits",
data_path=tmp_path / "data.fits",
noise_map_path=tmp_path / "noise_map.fits",
uv_wavelengths_path=tmp_path / "uv_wavelengths.fits",
overwrite=True,
)

dataset = aa.Interferometer.from_fits(
real_space_mask=mask_2d_7x7,
data_path=Path(test_data_path) / "data.fits",
noise_map_path=Path(test_data_path) / "noise_map.fits",
uv_wavelengths_path=Path(test_data_path) / "uv_wavelengths.fits",
data_path=tmp_path / "data.fits",
noise_map_path=tmp_path / "noise_map.fits",
uv_wavelengths_path=tmp_path / "uv_wavelengths.fits",
)

assert (dataset.data == np.array([1.0 + 2.0j, 1.0 + 2.0j, 1.0 + 2.0j])).all()
Expand Down
19 changes: 6 additions & 13 deletions test_autoarray/mask/test_mask_2d.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from astropy.io import fits
import os
import numpy as np
import pytest
import shutil

import autoarray as aa
from autoarray import exc
Expand Down Expand Up @@ -407,30 +405,25 @@ def test__from_pixel_coordinates__two_coordinates_buffer_1__two_separate_unmaske
# ---------------------------------------------------------------------------


def test__from_fits__output_to_fits__roundtrip_preserves_values_pixel_scales_and_header():
def test__from_fits__output_to_fits__roundtrip_preserves_values_pixel_scales_and_header(
tmp_path,
):
mask = aa.Mask2D.from_fits(
file_path=Path(test_data_path) / "3x3_ones.fits",
hdu=0,
pixel_scales=(1.0, 1.0),
)

output_path = Path(Path(__file__).resolve().parent) / "files" / "array" / "output_test"

if Path(output_path).exists():
shutil.rmtree(output_path)

os.makedirs(output_path)

from autonerves.fitsable import output_to_fits
output_to_fits(
values=mask.astype("float"),
file_path=Path(output_path) / "mask.fits",
file_path=tmp_path / "mask.fits",
header_dict=mask.header_dict,
ext_name="mask",
)

mask = aa.Mask2D.from_fits(
file_path=Path(output_path) / "mask.fits",
file_path=tmp_path / "mask.fits",
hdu=0,
pixel_scales=(1.0, 1.0),
origin=(2.0, 2.0),
Expand All @@ -440,7 +433,7 @@ def test__from_fits__output_to_fits__roundtrip_preserves_values_pixel_scales_and
assert mask.pixel_scales == (1.0, 1.0)
assert mask.origin == (2.0, 2.0)

header = aa.header_obj_from(file_path=Path(output_path) / "mask.fits", hdu=0)
header = aa.header_obj_from(file_path=tmp_path / "mask.fits", hdu=0)

assert header["PIXSCAY"] == 1.0
assert header["PIXSCAX"] == 1.0
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

This file was deleted.

15 changes: 4 additions & 11 deletions test_autoarray/structures/arrays/test_uniform_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

fits_path = Path(__file__).resolve().parent / "files" / "array_1d"

test_data_path = Path(__file__).resolve().parent / "files" / "array" / "output_test"


def create_fits(
fits_path,
Expand Down Expand Up @@ -154,25 +152,20 @@ def test__from_fits__4_element_fits__header_bitpix_is_minus_64():
clean_fits(fits_path=fits_path)


def test__output_to_fits__ones_array__fits_file_has_correct_values_and_header():
def test__output_to_fits__ones_array__fits_file_has_correct_values_and_header(tmp_path):
arr = aa.Array1D.ones(shape_native=(3,), pixel_scales=1.0)

if test_data_path.exists():
shutil.rmtree(test_data_path)

os.makedirs(test_data_path)

from autonerves.fitsable import output_to_fits
output_to_fits(values=arr.native.array.astype("float"), file_path=test_data_path / "array.fits", header_dict=arr.mask.header_dict)
output_to_fits(values=arr.native.array.astype("float"), file_path=tmp_path / "array.fits", header_dict=arr.mask.header_dict)

array_from_out = aa.Array1D.from_fits(
file_path=test_data_path / "array.fits", hdu=0, pixel_scales=1.0
file_path=tmp_path / "array.fits", hdu=0, pixel_scales=1.0
)

assert (array_from_out.native == np.ones((3,))).all()

header_load = aa.header_obj_from(
file_path=test_data_path / "array.fits", hdu=0
file_path=tmp_path / "array.fits", hdu=0
)

assert header_load["PIXSCA"] == 1.0
Expand Down
16 changes: 5 additions & 11 deletions test_autoarray/structures/arrays/test_uniform_2d.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from astropy.io import fits
import os
from pathlib import Path

import numpy as np
import pytest
import shutil

import autoarray as aa

Expand Down Expand Up @@ -191,24 +189,20 @@ def test__from_yx_and_values__3x2_grid__native_matches_expected_pixel_layout():
assert (array_2d.native == np.array([[3.0, 2.0], [6.0, 4.0], [5.0, 1.0]])).all()


def test__output_to_fits__3x3_ones__fits_file_has_ones_and_correct_pixel_scale_header():
def test__output_to_fits__3x3_ones__fits_file_has_ones_and_correct_pixel_scale_header(
tmp_path,
):
files_path = Path(__file__).resolve().parent / "files"

array_2d = aa.Array2D.from_fits(
file_path=files_path / "3x3_ones.fits", hdu=0, pixel_scales=1.0
)

output_test_path = files_path / "array" / "output_test"
if output_test_path.exists():
shutil.rmtree(output_test_path)

os.makedirs(output_test_path)

from autonerves.fitsable import output_to_fits
output_to_fits(values=array_2d.native.array.astype("float"), file_path=output_test_path / "array.fits", header_dict=array_2d.mask.header_dict)
output_to_fits(values=array_2d.native.array.astype("float"), file_path=tmp_path / "array.fits", header_dict=array_2d.mask.header_dict)

array_from_fits = aa.Array2D.from_fits(
file_path=output_test_path / "array.fits", hdu=0, pixel_scales=1.0
file_path=tmp_path / "array.fits", hdu=0, pixel_scales=1.0
)

assert (array_from_fits.native == np.ones((3, 3))).all()
Expand Down
15 changes: 3 additions & 12 deletions test_autoarray/structures/test_visibilities.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
from pathlib import Path
import numpy as np
import pytest
import shutil

import autoarray as aa
from autoarray.structures import visibilities as vis
Expand Down Expand Up @@ -86,25 +84,18 @@ def test__from_fits__makes_visibilities_without_other_inputs():
assert (visibilities.slim == np.array([2.0 + 2.0j, 2.0 + 2.0j, 2.0 + 2.0j])).all()


def test__output_to_fits():
def test__output_to_fits(tmp_path):
files_path = Path(__file__).resolve().parent / "files"

visibilities = aa.Visibilities.from_fits(
file_path=files_path / "3x2_ones.fits", hdu=0
)

output_test_path = files_path / "output_test"

if output_test_path.exists():
shutil.rmtree(output_test_path)

os.makedirs(output_test_path)

from autonerves.fitsable import output_to_fits
output_to_fits(values=visibilities.in_array, file_path=output_test_path / "data.fits")
output_to_fits(values=visibilities.in_array, file_path=tmp_path / "data.fits")

visibilities_from_out = aa.Visibilities.from_fits(
file_path=output_test_path / "data.fits", hdu=0
file_path=tmp_path / "data.fits", hdu=0
)
assert (
visibilities_from_out.slim == np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
Expand Down
Loading