Skip to content

Commit

Permalink
Adding function to set pixels surrounded by NaNs to desired value.
Browse files Browse the repository at this point in the history
  • Loading branch information
AarynnCarter committed Dec 11, 2023
1 parent 11fdf6b commit 2696fb1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
7 changes: 4 additions & 3 deletions spaceKLIP/analysistools.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from spaceKLIP.psf import get_offsetpsf, JWST_PSF
from spaceKLIP.starphot import get_stellar_magnitudes, read_spec_file
from spaceKLIP.pyklippipeline import get_pyklip_filepaths
from spaceKLIP.utils import write_starfile
from spaceKLIP.utils import write_starfile, set_surrounded_pixels

import logging
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -223,15 +223,15 @@ def raw_contrast(self,
nanmask = np.pad(nanmask, pad)

# Upsample array to improve centering.
samp = 15 #Upsampling factor
samp = 1 #Upsampling factor
nanmask = nanmask.repeat(samp, axis=0).repeat(samp, axis=1)

# Define rectangle edges
rect_width = 10*samp #pixels
thinrect_width = 2*samp #pixels

cent_rect = [(center[0]+pad)*samp,(center[0]+pad)*samp,
(center[1]+pad)*samp,(center[1]+pad)*samp]
(center[1]+pad)*samp,(center[1]+pad)*samp]
rect = [int(cent_rect[i]-(rect_width/2*(-1)**(i%2))) for i in range(4)]
thinrect = [int(cent_rect[i]-(thinrect_width/2*(-1)**(i%2))) for i in range(4)]

Expand Down Expand Up @@ -268,6 +268,7 @@ def raw_contrast(self,
# Downsample, remove padding, and mask data
nanmask = nanmask[::samp,::samp]
nanmask = nanmask[pad:-pad,pad:-pad]
nanmask = set_surrounded_pixels(nanmask)
data *= nanmask
elif self.database.red[key]['EXP_TYPE'][j] in ['MIR_LYOT']:
raise NotImplementedError()
Expand Down
34 changes: 31 additions & 3 deletions spaceKLIP/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,11 @@ def write_starfile(starfile,
Parameters
----------
starfile : str
Path to original stellar spectrum file
Path to original stellar spectrum file.
new_starfile_path : str
Path to new stellar spectrum file
Path to new stellar spectrum file.
new_header : str
Header to be inserted
Header to be inserted.
Returns
-------
Expand All @@ -639,3 +639,31 @@ def write_starfile(starfile,
new_starfile.write(text)
else:
new_starfile.write(new_header+text)

def set_surrounded_pixels(array, user_value=np.nan):
"""
Identifies pixels in a 2D array surrounded by NaN values
on all eight sides and sets them to a user-defined value.
Parameters
----------
array : numpy.ndarray
2D array containing numeric values and NaNs.
user_value : float or any valid value type, optional
Value to set for pixels surrounded by NaNs on all eight sides. Defaults to NaN.
Returns
-------
numpy.ndarray
The input array with pixels surrounded by NaNs on all eight sides set to the user-defined value.
"""
nan_mask = np.isnan(array)
surrounded_pixels = (
~nan_mask[1:-1, 1:-1] &
nan_mask[:-2, :-2] & nan_mask[:-2, 1:-1] & nan_mask[:-2, 2:] &
nan_mask[1:-1, :-2] & nan_mask[1:-1, 2:] &
nan_mask[2:, :-2] & nan_mask[2:, 1:-1] & nan_mask[2:, 2:]
)

array[1:-1, 1:-1][surrounded_pixels] = user_value
return array

0 comments on commit 2696fb1

Please sign in to comment.