Skip to content

Commit

Permalink
Adapted recentering test based on speckle cross-correlation + annulus…
Browse files Browse the repository at this point in the history
… fit to rely on fiducial dataset
  • Loading branch information
VChristiaens committed Mar 17, 2024
1 parent 31432c5 commit da2085d
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 59 deletions.
96 changes: 67 additions & 29 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ def example_dataset_adi():
-----
We use the helper function ``download_resource`` which handles the request
and puts it to sleep for a defined duration if too many requests are done.
They inherently call the Astropy's ``download_file`` function which uses caching,
so the file is downloaded at most once per test run.
They inherently call the Astropy's ``download_file`` function which uses
caching, so the file is downloaded at most once per test run.
"""
print("downloading data...")

url_prefix = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"
url_d = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"

f1 = download_resource("{}/naco_betapic_cube_cen.fits".format(url_prefix))
f2 = download_resource("{}/naco_betapic_psf.fits".format(url_prefix))
f3 = download_resource("{}/naco_betapic_pa.fits".format(url_prefix))
f1 = download_resource("{}/naco_betapic_cube_cen.fits".format(url_d))
f2 = download_resource("{}/naco_betapic_psf.fits".format(url_d))
f3 = download_resource("{}/naco_betapic_pa.fits".format(url_d))

# load fits
cube = vip.fits.open_fits(f1)
Expand Down Expand Up @@ -93,18 +93,18 @@ def example_dataset_ifs():
-----
We use the helper function ``download_resource`` which handles the request
and puts it to sleep for a defined duration if too many requests are done.
They inherently call the Astropy's ``download_file`` function which uses caching,
so the file is downloaded at most once per test run.
They inherently call the Astropy's ``download_file`` function which uses
caching, so the file is downloaded at most once per test run.
"""
print("downloading data...")

url_prefix = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"
url_d = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"

f1 = download_resource("{}/sphere_v471tau_cube.fits".format(url_prefix))
f2 = download_resource("{}/sphere_v471tau_psf.fits".format(url_prefix))
f3 = download_resource("{}/sphere_v471tau_pa.fits".format(url_prefix))
f4 = download_resource("{}/sphere_v471tau_wl.fits".format(url_prefix))
f1 = download_resource("{}/sphere_v471tau_cube.fits".format(url_d))
f2 = download_resource("{}/sphere_v471tau_psf.fits".format(url_d))
f3 = download_resource("{}/sphere_v471tau_pa.fits".format(url_d))
f4 = download_resource("{}/sphere_v471tau_wl.fits".format(url_d))

# load fits
cube = vip.fits.open_fits(f1)
Expand Down Expand Up @@ -134,7 +134,7 @@ def example_dataset_ifs():
@pytest.fixture(scope="session")
def example_dataset_ifs_crop():
"""
Download example FITS cube from github + prepare HCIDataset object, after
Download example FITS cube from github + prepare HCIDataset object, after\
cropping to only 3 sp. channels (faster NEGFC test).
Returns
Expand All @@ -145,18 +145,18 @@ def example_dataset_ifs_crop():
-----
We use the helper function ``download_resource`` which handles the request
and puts it to sleep for a defined duration if too many requests are done.
They inherently call the Astropy's ``download_file`` function which uses caching,
so the file is downloaded at most once per test run.
They inherently call the Astropy's ``download_file`` function which uses
caching, so the file is downloaded at most once per test run.
"""
print("downloading data...")

url_prefix = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"
url_d = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"

f1 = download_resource("{}/sphere_v471tau_cube.fits".format(url_prefix))
f2 = download_resource("{}/sphere_v471tau_psf.fits".format(url_prefix))
f3 = download_resource("{}/sphere_v471tau_pa.fits".format(url_prefix))
f4 = download_resource("{}/sphere_v471tau_wl.fits".format(url_prefix))
f1 = download_resource("{}/sphere_v471tau_cube.fits".format(url_d))
f2 = download_resource("{}/sphere_v471tau_psf.fits".format(url_d))
f3 = download_resource("{}/sphere_v471tau_pa.fits".format(url_d))
f4 = download_resource("{}/sphere_v471tau_wl.fits".format(url_d))

# load fits
cube = vip.fits.open_fits(f1)
Expand Down Expand Up @@ -186,6 +186,44 @@ def example_dataset_ifs_crop():
return dataset


@pytest.fixture(scope="session")
def example_dataset_ifs_cen():
"""
Download example FITS IFS spectral cube with satellite spots.
Returns
-------
dataset : HCIDataset
Notes
-----
We use the helper function ``download_resource`` which handles the request
and puts it to sleep for a defined duration if too many requests are done.
They inherently call the Astropy's ``download_file`` function which uses
caching, so the file is downloaded at most once per test run.
"""
print("downloading data...")

url_d = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"

f1 = download_resource("{}/sphere_ifs_PDS70_cen.fits".format(url_d))
f2 = download_resource("{}/sphere_ifs_PDS70_psf.fits".format(url_d))
f3 = download_resource("{}/sphere_ifs_PDS70_wl.fits".format(url_d))

# load fits
cube = vip.fits.open_fits(f1)
psf = vip.fits.open_fits(f2)
wl = vip.fits.open_fits(f3)

# create dataset object
dataset = vip.objects.Dataset(cube, psf=psf,
px_scale=vip.config.VLT_SPHERE_IFS["plsc"],
wavelengths=wl)

return dataset


@pytest.fixture(scope="session")
def example_dataset_rdi():
"""
Expand All @@ -199,17 +237,17 @@ def example_dataset_rdi():
-----
We use the helper function ``download_resource`` which handles the request
and puts it to sleep for a defined duration if too many requests are done.
They inherently call the Astropy's ``download_file`` function which uses caching,
so the file is downloaded at most once per test run.
They inherently call the Astropy's ``download_file`` function which uses
caching, so the file is downloaded at most once per test run.
"""
print("downloading data...")

url_prefix = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"
url_d = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"

f1 = download_resource("{}/naco_betapic_cube_cen.fits".format(url_prefix))
f2 = download_resource("{}/naco_betapic_psf.fits".format(url_prefix))
f3 = download_resource("{}/naco_betapic_pa.fits".format(url_prefix))
f1 = download_resource("{}/naco_betapic_cube_cen.fits".format(url_d))
f2 = download_resource("{}/naco_betapic_psf.fits".format(url_d))
f3 = download_resource("{}/naco_betapic_pa.fits".format(url_d))

# load fits
cube = vip.fits.open_fits(f1)
Expand Down Expand Up @@ -255,7 +293,7 @@ def example_dataset_rdi():

@pytest.fixture(autouse=True)
def time_test():
"""Time a test and print out how long it took"""
"""Time a test and print out how long it took."""
before = time.time()
yield
after = time.time()
Expand All @@ -264,7 +302,7 @@ def time_test():

@pytest.fixture(autouse=True, scope="session")
def time_all_tests():
"""Time a test and print out how long it took"""
"""Time all tests and print out how long they took."""
before = time.time()
yield
after = time.time()
Expand Down
100 changes: 71 additions & 29 deletions tests/pre_3_10/test_preproc_recentering.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_cube(example_dataset_adi):


@fixture(scope="module")
def get_ifs_cube(example_dataset_sdi):
def get_ifs_cube_cen(example_dataset_ifs_cen):
"""
Get the ADI+IFS sequence from conftest.py.
Expand All @@ -107,7 +107,7 @@ def get_ifs_cube(example_dataset_sdi):
dsi : VIP Dataset
"""
dsi = copy.copy(example_dataset_sdi)
dsi = copy.copy(example_dataset_ifs_cen)

return dsi

Expand Down Expand Up @@ -141,20 +141,30 @@ def create_cube_with_gauss2d_ring(stddev_inner, stddev_outer, **kwargs):
return outer - inner


def create_cube_with_satspots(n_frames=6, wh=31, star_fwhm=3, debug=False):
def create_cube_with_satspots(n_frames=6, wh=31, star_fwhm=3, debug=False,
pattern='x', diagonal=None):
global seed
shape = (n_frames, wh, wh)
star = create_cube_with_gauss2d(shape=shape, mean=wh // 2, stddev=star_fwhm)

# make sure satspot is neither too close to star nor at the edge of the
# image
diagonal = seed.uniform(4 * star_fwhm, wh // 2)
if diagonal is None:
diagonal = seed.uniform(4 * star_fwhm, wh // 2)
else:
diagonal *= star_fwhm
d = diagonal / np.sqrt(2)

sat1_coords = (wh // 2 - d, wh // 2 + d)
sat2_coords = (wh // 2 + d, wh // 2 + d)
sat3_coords = (wh // 2 - d, wh // 2 - d)
sat4_coords = (wh // 2 + d, wh // 2 - d)
if pattern == 'x':
sat1_coords = (wh // 2 - d, wh // 2 + d)
sat2_coords = (wh // 2 + d, wh // 2 + d)
sat3_coords = (wh // 2 - d, wh // 2 - d)
sat4_coords = (wh // 2 + d, wh // 2 - d)
else:
sat1_coords = (wh // 2 - 2*d, wh // 2)
sat2_coords = (wh // 2 + 2*d, wh // 2)
sat3_coords = (wh // 2, wh // 2 - 2*d)
sat4_coords = (wh // 2, wh // 2 + 2*d)

sat1 = create_cube_with_gauss2d(shape=shape, mean=sat1_coords, stddev=1)
sat2 = create_cube_with_gauss2d(shape=shape, mean=sat2_coords, stddev=1)
Expand Down Expand Up @@ -637,21 +647,27 @@ def test_radon(debug=False):
errormsg = "Error when recentering with Radon transform"
n_frames = 1

# ===== datacube
url_prefix = "https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"
ds = get_ifs_cube_cen

# ===== OLD - datacube
# url_d="https://github.com/vortex-exoplanet/VIP_extras/raw/master/datasets"

f1 = download_resource("{}/sphere_ifs_PDS70_cen.fits".format(url_prefix))
f2 = download_resource("{}/sphere_ifs_PDS70_psf.fits".format(url_prefix))
# f1 = download_resource("{}/sphere_ifs_PDS70_cen.fits".format(url_d))
# f2 = download_resource("{}/sphere_ifs_PDS70_psf.fits".format(url_d))

# # load fits
# cube = vip.fits.open_fits(f1)
# psfifs = vip.fits.open_fits(f2)
# norm_psf, flux, fwhm = vip.fm.normalize_psf(
# psfifs, fwhm="fit", full_output=True, size=15
# )

# load fits
cube = vip.fits.open_fits(f1)
psfifs = vip.fits.open_fits(f2)
norm_psf, flux, fwhm = vip.fm.normalize_psf(
psfifs, fwhm="fit", full_output=True, size=15
ds.psf, fwhm="fit", full_output=True, size=15
)

# Fit BKG star position
med_fr = np.nanmedian(cube, axis=0)
med_fr = np.nanmedian(ds.cube, axis=0)
fit_res = vip.var.fit_2dgaussian(med_fr, crop=True, cropsize=13,
cent=(144, 147), debug=False,
full_output=True)
Expand Down Expand Up @@ -702,7 +718,8 @@ def test_radon(debug=False):
)
# med_y = 146.99531922145198
# med_x = 144.27429227212795
for z in range(cube.shape[0]):
cube = ds.cube.copy()
for z in range(ds.cube.shape[0]):
cube[z] = vip.fm.frame_inject_companion(
cube[z],
norm_psf[z],
Expand Down Expand Up @@ -762,33 +779,58 @@ def test_speckle_recentering(get_cube, debug=False):
method = cube_recenter_via_speckles
errormsg = "Error when recentering via speckles"

# ===== datacube
ds = get_cube
n_frames = ds.cube.shape[0]

# ===== shift
randax = seed.uniform(-1, 1, size=n_frames)
randay = seed.uniform(-1, 1, size=n_frames)

# ===== recenter
types = ["gaus", "ann"]
upsamp_facs = [100, 20]
crop_szs = [35, 81]

for t, ty in enumerate(types):
if t == 0:
# ===== datacube Beta Pic for gaus test
ds = get_cube
cube = ds.cube
n_frames = ds.cube.shape[0]
fwhm = ds.fwhm
method_args_additional = {}
else:
# ===== fiducial datacube with 4 satspots as speckles
size = 61
mean = size // 2 # - 0.5
n_frames = 20
cube_ring = create_cube_with_gauss2d_ring(
shape=(n_frames, size, size), mean=mean, stddev_outer=4,
stddev_inner=2
)
cube_ss1, spotcoords = create_cube_with_satspots(n_frames=n_frames,
wh=size)
cube_ss2, spotcoords = create_cube_with_satspots(n_frames=n_frames,
wh=size,
pattern='+',
diagonal=6)
rand_noise = 1e-2*np.random.random(cube_ss1.shape)
cube = 10*cube_ring + cube_ss1 + 0.1*cube_ss2 + rand_noise
fwhm = 3.
method_args_additional = dict(ann_rad=0.5, ann_rad_search=False,
ann_width=1.5)
# ===== shift
randax = seed.uniform(-1, 1, size=n_frames)
randay = seed.uniform(-1, 1, size=n_frames)

method_args = dict(
plot=False,
full_output=True,
fwhm=4.2,
fwhm=fwhm,
fit_type=ty,
recenter_median=True,
upsample_factor=upsamp_facs[t],
subframesize=35,
subframesize=crop_szs[t],
imlib="opencv",
interpolation="lanczos4",
**method_args_additional
)
do_recenter(
method,
ds.cube,
cube,
randax,
randay,
errormsg=errormsg,
Expand Down
2 changes: 1 addition & 1 deletion vip_hci/preproc/recentering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ def cube_recenter_via_speckles(cube_sci, cube_ref=None, alignment_iter=5,
cum_x_shifts_sci: numpy 1d array
[full_output=True] Vector of x shifts for science frames
cum_y_shifts_sci: numpy 1d array
[full_output=True] Vector of x shifts for science frames
[full_output=True] Vector of y shifts for science frames
cum_x_shifts_ref: numpy 1d array
[full_output=True & cube_ref!=None] Vector of x shifts for ref frames
cum_y_shifts_ref: numpy 1d array
Expand Down

0 comments on commit da2085d

Please sign in to comment.