Skip to content

Commit

Permalink
Merge pull request #201 from tomasstolker/isz_none
Browse files Browse the repository at this point in the history
BUG: fix calling `select_clean_data` with default `isz`
  • Loading branch information
DrSoulain authored Jan 3, 2024
2 parents 46339e3 + b1ce354 commit ed1cf04
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
34 changes: 20 additions & 14 deletions amical/data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def _get_3d_bad_pixels(bad_map, add_bad, data):

def show_clean_params(
filename,
isz,
isz=None,
r1=None,
dr=None,
bad_map=None,
Expand All @@ -380,7 +380,7 @@ def show_clean_params(
-----------
`filename` {str}: filename containing the datacube,\n
`isz` {int}: Size of the cropped image (default: 256)\n
`isz` {int}: Size of the cropped image (default: None)\n
`r1` {int}: Radius of the rings to compute background sky (default: 100)\n
`dr` {int}: Outer radius to compute sky (default: 10)\n
`bad_map` {array}: Bad pixel map with 0 and 1 where 1 set for a bad pixel (default: None),\n
Expand All @@ -403,14 +403,6 @@ def show_clean_params(
img0 = data[nframe]
dims = img0.shape

if isz is None:
print(
"Warning: isz not found (None by default). isz is set to the original image size (%i)"
% (dims[0]),
file=sys.stderr,
)
isz = dims[0]

bad_map, add_bad = _get_3d_bad_pixels(bad_map, add_bad, data)
bmap0 = bad_map[nframe]
ab0 = add_bad[nframe]
Expand All @@ -424,8 +416,22 @@ def show_clean_params(
img1 = fix_bad_pixels(img0, bmap0, add_bad=ab0)
else:
img1 = img0.copy()
cropped_infos = crop_max(img1, isz, offx=offx, offy=offy, f=f_kernel)
pos = cropped_infos[1]

if isz is None:
pos = (img1.shape[0] // 2, img1.shape[1] // 2)

print(
"Warning: isz not found (None by default). "
f"isz is set to the original image size ({dims[0]}).",
file=sys.stderr,
)

isz = dims[0]

else:
# Get expected center for sky correction
filtmed = f_kernel is not None
_, pos = crop_max(img1, isz, offx=offx, offy=offy, filtmed=filtmed, f=f_kernel)

noBadPixel = False
bad_pix_x, bad_pix_y = [], []
Expand Down Expand Up @@ -653,7 +659,7 @@ def clean_data(

def select_clean_data(
filename,
isz=256,
isz=None,
r1=None,
dr=None,
edge=0,
Expand Down Expand Up @@ -683,7 +689,7 @@ def select_clean_data(
-----------
`filename` {str}: filename containing the datacube,\n
`isz` {int}: Size of the cropped image (default: {256})\n
`isz` {int}: Size of the cropped image (default: {None})\n
`r1` {int}: Radius of the rings to compute background sky (default: {100})\n
`dr` {int}: Outer radius to compute sky (default: {10})\n
`edge` {int}: Patch the edges of the image (VLT/SPHERE artifact, default: {0}),\n
Expand Down
29 changes: 29 additions & 0 deletions amical/tests/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,32 @@ def test_clean_crop_order():
img_correct_crop = crop_max(correct, isz, filtmed=False)[0]

assert np.all(np.abs(img_correct_crop - img_cube_clean) < 10 * np.finfo(float).eps)


@pytest.mark.usefixtures("close_figures")
def test_isz_none(global_datadir):
fits_file = global_datadir / "test.fits"

clean_param = {
"isz": None,
"r1": 35,
"dr": 2,
"apod": True,
"window": 65,
"f_kernel": 3,
}

fig = amical.show_clean_params(fits_file, **clean_param)

assert isinstance(fig, plt.Figure)
assert fig.axes[0].get_xlim() == (0.0, 80.0)
assert fig.axes[0].get_ylim() == (0.0, 80.0)

# Set clip=False such that the shape does not changes
cube_clean = amical.select_clean_data(fits_file, clip=False, **clean_param)

# Get the shape of the array with input images
fits_images = fits.getdata(fits_file)

assert isinstance(cube_clean, np.ndarray)
assert cube_clean.shape == fits_images.shape

0 comments on commit ed1cf04

Please sign in to comment.