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

Synthetic wavelength calibration data #165

Merged
merged 32 commits into from
Apr 12, 2023
Merged

Conversation

tepickering
Copy link
Contributor

This PR adds a utility for creating realistic models of 2D spectroscopic images of arc lamp (or sky background) calibration observations. This will be key for testing the various stages of the wavelength calibration code and process as it's developed. Tilts/curvature along the spatial axis is modeled using 1D polynomials. Non-linear dispersion relations are modeled using the ideal disperser model implemented within the FITS WCS standard. Examples of the use of this routine are included in its docstrings.

A few other notes:

  • Use of the specreduce_data dependency has been deprecated. Cleaner to just always download/cache the data as needed.
  • https://github.com/pypeit/PypeIt is used as the source for comparison line lists because it has a large selection of well-vetted ones.
  • In the example where a non-linear dispersion relation is created using the WCS ideal disperser model, I create the model by putting together the appropriate FITS header manually and then loading that into astropy.wcs.WCS. It would be useful to have a documented functional interface for building WCS spectrograph models that provides meaningful parameter names and units handling. However, I think that is outside of the scope of this PR. It's also an open question whether such a utility should live here or within specutils.

…downloads for everything; add support for loading pypeit reference data; add list of pypeit-supported line lists:
…cific about arcs since sky lines are often used in lieu of or in addition to lamps
@tepickering tepickering requested a review from kecnry March 28, 2023 19:32
Copy link
Member

@kecnry kecnry left a comment

Choose a reason for hiding this comment

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

Just a few minor comments/suggestions, but otherwise the code and tests look reasonable to me. This will definitely be useful to use as we write more tests for wavelength calibration and spectral extraction.

Comment on lines 16 to 17
'make_2dspec_image',
'make_2d_arc_image'
Copy link
Member

Choose a reason for hiding this comment

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

should we change the function name to make_2darc_image to be consistent with the already-existing make_2dspec_image?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how about renaming make_2dspec_image to make_2d_trace_image? that's a more accurate description of what it does. this would open up the possibility of a make_2d_spec_image that combines make_2d_trace_image and make_2d_arc_image. might be worth implementing that as part of this PR.

wave_air : bool (default: False)
If True, convert the vacuum wavelengths used by ``pypeit`` to air wavelengths.
wave_unit : `~astropy.units.Quantity`
If ``wcs`` is not provides, this defines the wavelength units of the
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
If ``wcs`` is not provides, this defines the wavelength units of the
If ``wcs`` is not provided, this defines the wavelength units of the

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gah, fat fingers...

linelists : str or list of str (default: ['HeI'])
Specification for linelists to load from ``pypeit``
amplitude_scale : float (default: 1)
Scale factor to apply to amplitudes provides in the linelists
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Scale factor to apply to amplitudes provides in the linelists
Scale factor to apply to amplitudes provided in the linelists

of the dispersion axis.
wave_air : bool (default: False)
If True, convert the vacuum wavelengths used by ``pypeit`` to air wavelengths.
wave_unit : `~astropy.units.Quantity`
Copy link
Member

Choose a reason for hiding this comment

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

should this be a Quantity or Unit object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah ha! good point. it should in fact be Unit...

wcs = WCS(naxis=2)
wcs.wcs.ctype[0] = 'WAVE'
wcs.wcs.ctype[1] = 'PIXEL'
wcs.wcs.cunit[0] = wave_unit
Copy link
Member

Choose a reason for hiding this comment

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

do we need to check if the unit is valid (u.Unit and spectral)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think so and i'll add that check.

Comment on lines +114 to +115
wave_air : bool (default: False)
If True, convert the vacuum wavelengths used by ``pypeit`` to air wavelengths.
Copy link
Member

Choose a reason for hiding this comment

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

having this between the two arguments that are only applicable if wcs is not provided might be confusing - could we move it below extent and wave_unit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'll at least do that if there's not a more clever way around it...

@codecov
Copy link

codecov bot commented Apr 6, 2023

Codecov Report

Merging #165 (3eb4344) into main (799ff49) will increase coverage by 3.38%.
The diff coverage is 93.85%.

@@            Coverage Diff             @@
##             main     #165      +/-   ##
==========================================
+ Coverage   76.81%   80.20%   +3.38%     
==========================================
  Files           9        9              
  Lines         716      798      +82     
==========================================
+ Hits          550      640      +90     
+ Misses        166      158       -8     
Impacted Files Coverage Δ
specreduce/calibration_data.py 80.00% <85.41%> (+10.63%) ⬆️
specreduce/utils/synth_data.py 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@tepickering
Copy link
Contributor Author

thanks for the review, @kecnry! i think i addressed all of the comments. i also added a test i forgot to earlier and added a few more checks to flesh out coverage. if there are no other comments/concerns, i'll go ahead and merge in the next day or two.

"""
Basic function to take a path to a file and load it via ``pkg_resources`` if
the ``specreduce_data`` package is available and load it via GitHub raw user content if not.
Utility to load reference data via GitHub raw user content. By default the ``specreduce_data``
Copy link
Member

Choose a reason for hiding this comment

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

I thought you said specreduce-data is "deprecated" but it is still used here with no deprecation warning. Did I misunderstand?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the use of the package is deprecated. the repository is still there and will be there unless and until something better comes along.

Copy link
Member

Choose a reason for hiding this comment

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


def get_reference_file_path(path=None, cache=False, show_progress=False):

def get_reference_file_path(
Copy link
Member

Choose a reason for hiding this comment

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

Instead of rolling your own here, why not use the stuff in astropy.utils.data but use your own conf.dataurl?

https://github.com/astropy/astropy/blob/main/astropy/utils/data.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

when this was originally implemented years ago, i followed examples in https://docs.astropy.org/en/stable/utils/data.html and just wrapped astropy.utils.data.download_file() with some error handling. it looks like those examples haven't changed much since. i'll look into refactoring this to use pure-astropy at a higher level. that's out of scope for this PR, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i opened #169 to cover this. it's a little unclear to me how best to handle multiple data URLs with the conf.dataurl scheme. will play around more when i get the chance.

@@ -8,3 +8,12 @@ def test_get_reference_file_path():
test_path = "extinction/apoextinct.dat"
p = get_reference_file_path(path=test_path)
assert p is not None


def test_get_pypeit_data_path():
Copy link
Member

Choose a reason for hiding this comment

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

If your test downloads data, you might wanna mark it with @pytest.mark.remote_data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call. i'll add that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. i tweaked the CI workflow so that --remote-data is only enabled for the one test where coverage is measured. this should hopefully help with the occasional failures due to downloads timing out. no longer parallel tests getting the same files at the same time...

@pllim
Copy link
Member

pllim commented Apr 7, 2023

Mine was just a drive-by review and not thorough. I'll leave it to specreduce maintainers to do the rest. Thanks!

@tepickering
Copy link
Contributor Author

drive-by or not, it was useful so thanks again, @pllim!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants