diff --git a/CHANGES.md b/CHANGES.md index 7be773cb..8393af55 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +1.15.1 (2024-02-29) +------------------- +- Minor fixes in photometry when there are bad pixels near the image edges + 1.15.0 (2024-02-14) ------------------- - Migrated photometry extraction to be done by astropy's photutils instead of SEP. diff --git a/banzai/photometry.py b/banzai/photometry.py index e48fce32..bb8862e4 100755 --- a/banzai/photometry.py +++ b/banzai/photometry.py @@ -51,10 +51,10 @@ def flag_edge_sources(image, sources, flag_value=8): minor_xmax = sources['x'] + sources['b'] * sources['kronrad'] * np.sin(np.deg2rad(sources['theta'])) minor_ymin = sources['y'] - sources['b'] * sources['kronrad'] * np.cos(np.deg2rad(sources['theta'])) minor_ymax = sources['y'] + sources['b'] * sources['kronrad'] * np.cos(np.deg2rad(sources['theta'])) - major_ymin = sources['y'] - sources['a'] * sources['kronrad'] * np.sin(np.deg2rad(sources['theta'])) - major_ymax = sources['y'] + sources['a'] * sources['kronrad'] * np.sin(np.deg2rad(sources['theta'])) - major_xmin = sources['x'] - sources['a'] * sources['kronrad'] * np.cos(np.deg2rad(sources['theta'])) - major_xmax = sources['x'] + sources['a'] * sources['kronrad'] * np.cos(np.deg2rad(sources['theta'])) + major_ymin = sources['y'] - sources['a'] * sources['kronrad'] * np.cos(np.deg2rad(sources['theta'])) + major_ymax = sources['y'] + sources['a'] * sources['kronrad'] * np.cos(np.deg2rad(sources['theta'])) + major_xmin = sources['x'] - sources['a'] * sources['kronrad'] * np.sin(np.deg2rad(sources['theta'])) + major_xmax = sources['x'] + sources['a'] * sources['kronrad'] * np.sin(np.deg2rad(sources['theta'])) # Note we are already 1 indexed here sources_off = np.logical_or(minor_xmin < 1, major_xmin < 1) @@ -64,7 +64,7 @@ def flag_edge_sources(image, sources, flag_value=8): sources_off = np.logical_or(sources_off, major_xmax > nx) sources_off = np.logical_or(sources_off, minor_ymax > ny) sources_off = np.logical_or(sources_off, major_ymax > ny) - sources[sources_off]['flag'] |= flag_value + sources['flag'][sources_off] |= flag_value class SourceDetector(Stage): @@ -145,10 +145,20 @@ def do_stage(self, image): # Flag = 16 if source has cosmic ray pixels flag_sources(sources, catalog.labels, deblended_seg_map, image.mask, flag=16, mask_value=8) - sources = array_utils.prune_nans_from_table(sources) + rows_with_nans = array_utils.find_nans_in_table(sources) + catalog = catalog[~rows_with_nans] + sources = sources[~rows_with_nans] # Cut individual bright pixels. Often cosmic rays - sources = sources[sources['fluxrad50'] > 0.5] + not_individual_bright_pixels = sources['fluxrad50'] > 0.5 + catalog = catalog[not_individual_bright_pixels] + sources = sources[not_individual_bright_pixels] + + # Cut sources that are less than 2 pixels wide + thin_sources = np.logical_or((catalog.bbox_ymax - catalog.bbox_ymin) < 1.5, + (catalog.bbox_xmax - catalog.bbox_xmin) < 1.5) + catalog = catalog[~thin_sources] + sources = sources[~thin_sources] # Calculate the FWHMs of the stars: sources['fwhm'] = np.nan diff --git a/banzai/tests/test_array_utils.py b/banzai/tests/test_array_utils.py index cd72ac8c..b6a8e81c 100644 --- a/banzai/tests/test_array_utils.py +++ b/banzai/tests/test_array_utils.py @@ -6,6 +6,7 @@ pytestmark = pytest.mark.array_utils + def test_pruning_nans(): a = np.arange(100, dtype=float) b = np.arange(100, dtype=float) + 100 @@ -16,7 +17,7 @@ def test_pruning_nans(): c[78] = np.nan t = Table([a, b, c], names=('a', 'b', 'c')) - t = array_utils.prune_nans_from_table(t) + t = t[~array_utils.find_nans_in_table(t)] assert len(t) == 97 assert 51 not in t['a'] assert 32 not in t['a'] diff --git a/banzai/utils/array_utils.py b/banzai/utils/array_utils.py index 5ed107e0..23f80894 100644 --- a/banzai/utils/array_utils.py +++ b/banzai/utils/array_utils.py @@ -6,8 +6,8 @@ def array_indices_to_slices(a): return tuple(slice(0, x, 1) for x in a.shape) -def prune_nans_from_table(table): +def find_nans_in_table(table): nan_in_row = np.zeros(len(table), dtype=bool) for col in table.colnames: nan_in_row |= np.isnan(table[col]) - return table[~nan_in_row] + return nan_in_row