Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured array source detection catalogs #987

Merged
merged 2 commits into from
Nov 14, 2023

Conversation

bmorris3
Copy link
Collaborator

Source Detection produces a 2D ndarray of source IDs, centroids, and fluxes, to use as inputs in tweakreg. @schlafly and I discussed in #841 that we could use a self-documenting and more scalable data structure instead if additional Source Detection results are useful for tweakreg or elsewhere.

This PR swaps the unlabeled 2D ndarray for a structured array, and adds some helper functions for converting between ndarrays, structured arrays, and astropy tables.

Resolves RCAL-709

Checklist

  • added entry in CHANGES.rst under the corresponding subsection
  • updated relevant tests
  • updated relevant documentation
  • updated relevant milestone(s)
  • added relevant label(s)
  • ran regression tests, post a link to the Jenkins job below. How to run regression tests on a PR

@bmorris3 bmorris3 requested a review from a team as a code owner November 10, 2023 18:40
@bmorris3 bmorris3 force-pushed the structured-array-catalogs branch from 5764aa0 to 6f58a89 Compare November 10, 2023 18:44
Copy link

codecov bot commented Nov 10, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (9cf7c04) 73.53% compared to head (df1b05e) 73.55%.
Report is 1 commits behind head on main.

Files Patch % Lines
romancal/source_detection/source_detection_step.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #987      +/-   ##
==========================================
+ Coverage   73.53%   73.55%   +0.01%     
==========================================
  Files         103      103              
  Lines        6704     6711       +7     
==========================================
+ Hits         4930     4936       +6     
- Misses       1774     1775       +1     
Flag Coverage Δ *Carryforward flag
nightly 63.26% <ø> (-0.01%) ⬇️ Carriedforward from 9cf7c04

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bmorris3 bmorris3 force-pushed the structured-array-catalogs branch from 6569baa to 32d5af7 Compare November 10, 2023 19:08
@bmorris3 bmorris3 requested a review from schlafly November 10, 2023 19:09
Copy link
Collaborator

@schlafly schlafly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. I might take it a bit farther and aim to just make 'sources' the catalog (don't bother trimming out columns unless we really think daofind is giving us useless stuff?), and then have the fit_psf branch just ~join the psf_photometry_table onto the sources table + overwrite the x & y so that they get used by tweakreg? Does the psf_photometry_table already include all of the content of the daofind sources table?

It's unclear to me that you actually use or need ndarrays_to_recarrays or recarray_to_ndarrays. I think we can get away without the whole basic_utils.py file, and just rely on Table(ndarray) and Table(ndarray).as_array() to handle all of the conversions between structured arrays and Tables?

return Table(data, names=names)


def astropy_table_to_recarray(x):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function converts specifically to a structured array.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

return np.asarray(x.astype(astype).view(to_dtype).reshape((-1, len(names))))


def recarray_to_astropy_table(x):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just do Table(ndarray).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks you're right! Resolved.

@bmorris3
Copy link
Collaborator Author

bmorris3 commented Nov 10, 2023

I might take it a bit farther and aim to just make 'sources' the catalog (don't bother trimming out columns unless we really think daofind is giving us useless stuff?), and then have the fit_psf branch just ~join the psf_photometry_table onto the sources table + overwrite the x & y so that they get used by tweakreg?

That's almost what's done now. I left the DAO trimming in place, as-is. When fit_psf=True, the photometry results table is saved as the catalog, but renames the centroid columns to be what tweakreg expects. The DAO results are the initial guesses to the PSF fit, so those centroids and fluxes are preserved in the resulting table as x_init, y_init, flux_init.

Does the psf_photometry_table already include all of the content of the daofind sources table?

Not all of the content, but I'd argue that the parts that are relevant are preserved.

@bmorris3 bmorris3 force-pushed the structured-array-catalogs branch 2 times, most recently from 588e40d to 95e3bdc Compare November 13, 2023 17:25
@bmorris3 bmorris3 requested a review from schlafly November 13, 2023 17:25
@@ -103,3 +103,45 @@ def __exit__(self, et, ev, tb):
if self.handler and self.close:
self.handler.close()
# implicit return of None => don't swallow exceptions


def ndarrays_to_recarray(arrays, names):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing it, or do we only use this and its inverse in tests? Can we get rid of this module and its tests and just rely on the library conversions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only used in tests at the moment, that's correct. I left this in the PR until chatting with @mairanteodoro to confirm that we won't ever need to read and convert the old catalogs that are 2D ndarrays.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmorris3 I think we should be good. Thanks!

Copy link
Collaborator

@mairanteodoro mairanteodoro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@@ -103,3 +103,45 @@ def __exit__(self, et, ev, tb):
if self.handler and self.close:
self.handler.close()
# implicit return of None => don't swallow exceptions


def ndarrays_to_recarray(arrays, names):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmorris3 I think we should be good. Thanks!

@bmorris3 bmorris3 force-pushed the structured-array-catalogs branch from 95e3bdc to 6b8de32 Compare November 14, 2023 02:14
@bmorris3 bmorris3 force-pushed the structured-array-catalogs branch from 6b8de32 to df1b05e Compare November 14, 2023 02:14
@bmorris3 bmorris3 merged commit 50219a1 into spacetelescope:main Nov 14, 2023
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants