From 6b0b818513dbcec1fef6661abaf23c9a4b4b3dff Mon Sep 17 00:00:00 2001 From: Will Barnes Date: Wed, 4 Sep 2024 14:36:33 -0400 Subject: [PATCH] Remove use of deprecated `pkg_resources` (#283) * remove deprecated pkg_resources * test name; precommit * fix the precommit * Update xrtpy/image_correction/tests/test_deconvolve.py Co-authored-by: Nabil Freij --------- Co-authored-by: Joy <74623359+joyvelasquez@users.noreply.github.com> Co-authored-by: Nabil Freij --- .../data_analysis/Remove_lightleak.ipynb | 11 ++- .../image_correction/tests/test_deconvolve.py | 86 +++++++------------ 2 files changed, 36 insertions(+), 61 deletions(-) diff --git a/docs/notebooks/data_analysis/Remove_lightleak.ipynb b/docs/notebooks/data_analysis/Remove_lightleak.ipynb index 4940bbf5..b300a1e9 100644 --- a/docs/notebooks/data_analysis/Remove_lightleak.ipynb +++ b/docs/notebooks/data_analysis/Remove_lightleak.ipynb @@ -31,7 +31,9 @@ "metadata": {}, "outputs": [], "source": [ - "from pathlib import Path" + "from pathlib import Path\n", + "\n", + "import sunpy.map" ] }, { @@ -41,8 +43,7 @@ "metadata": {}, "outputs": [], "source": [ - "import pkg_resources\n", - "import sunpy.map\n", + "from astropy.utils.data import get_pkg_data_path\n", "\n", "from xrtpy.image_correction.remove_lightleak import remove_lightleak" ] @@ -62,9 +63,7 @@ "metadata": {}, "outputs": [], "source": [ - "directory = pkg_resources.resource_filename(\n", - " \"xrtpy\", \"image_correction/data/example_data\"\n", - ")\n", + "directory = get_pkg_data_path(\"example_data\", package=\"xrtpy.image_correction.data\")\n", "data_file = Path(directory) / \"comp_XRT20150621_055911.7.fits\"\n", "\n", "print(\"File used:\\n\", data_file.name)" diff --git a/xrtpy/image_correction/tests/test_deconvolve.py b/xrtpy/image_correction/tests/test_deconvolve.py index 1c789656..7d3d97ec 100644 --- a/xrtpy/image_correction/tests/test_deconvolve.py +++ b/xrtpy/image_correction/tests/test_deconvolve.py @@ -1,66 +1,42 @@ from pathlib import Path import numpy as np -import pkg_resources +import pytest +from astropy.utils.data import get_pkg_data_path from sunpy.map import Map from xrtpy.image_correction.deconvolve import deconvolve -test_file = "L1_XRT20120605_215839.9.fits" -idl_result_file = "L1_XRT20120605_215839.9.deconv.fits" -test_file_binned = "L1_XRT20210730_175810.1.fits" -idl_result_file_binned = "L1_XRT20210730_175810.1_deconv.fits" - -def get_observed_data(): - directory = pkg_resources.resource_filename("xrtpy", "image_correction/tests/data") - data_file = Path(directory) / test_file - - return data_file - - -def get_IDL_results_data(): - directory = pkg_resources.resource_filename("xrtpy", "image_correction/tests/data") - results_file = Path(directory) / idl_result_file - - return results_file - - -def get_observed_binned_data(): - directory = pkg_resources.resource_filename("xrtpy", "image_correction/tests/data") - data_file = Path(directory) / test_file_binned - - return data_file - - -def get_IDL_results_binned_data(): - directory = pkg_resources.resource_filename("xrtpy", "image_correction/tests/data") - results_file = Path(directory) / idl_result_file_binned - - return results_file - - -def test_unbinned(): - """ - Test case where the image is at full resolution, i.e. chip_sum = 1 - """ - test_data = get_observed_data() - in_map = Map(test_data) - IDL_result_image = get_IDL_results_data() - IDL_result = Map(IDL_result_image) - out_map = deconvolve(in_map) - assert np.allclose(out_map.data, IDL_result.data, atol=1.0e-7) - - -def test_binned(): +@pytest.fixture +def data_dir(): + return Path(get_pkg_data_path("data", package="xrtpy.image_correction.tests")) + + +@pytest.mark.parametrize( + ("observed_file", "idl_file", "atol", "rtol"), + [ + ( + "L1_XRT20120605_215839.9.fits", + "L1_XRT20120605_215839.9.deconv.fits", + 0, + 1e-7, + ), + # Case where image is binned, i.e. has chip_sum = 2 + # Needs higher tolerances because the binned PSF for the IDL and Python codes don't match + ( + "L1_XRT20210730_175810.1.fits", + "L1_XRT20210730_175810.1_deconv.fits", + 4.1, + 0.008, + ), + ], +) +def test_deconvolve(data_dir, observed_file, idl_file, rtol, atol): """ - Test case where the image is binned, i.e. has chip_sum = 2 - This case needs higher tolerances (atol, rtol) because the binned PSF for - the IDL and python codes don't match + Test deconvolution against IDL results for binned and unbinned cases """ - test_data = get_observed_binned_data() - in_map = Map(test_data) - IDL_result_image = get_IDL_results_binned_data() - IDL_result = Map(IDL_result_image) + in_map = Map(data_dir / observed_file) + IDL_result = Map(data_dir / idl_file) out_map = deconvolve(in_map) - assert np.allclose(out_map.data, IDL_result.data, atol=4.1, rtol=0.008) + assert np.allclose(out_map.data, IDL_result.data, rtol=rtol, atol=atol)