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

Pol convention #1455

Merged
merged 24 commits into from
Jul 23, 2024
Merged

Pol convention #1455

merged 24 commits into from
Jul 23, 2024

Conversation

steven-murray
Copy link
Contributor

@steven-murray steven-murray commented Jul 5, 2024

Description

This adds a new parameter pol_convention to both UVData and UVCal, as well as uvcalibrate which applies it from one to the other. The parameter is not required, but a warning is issued if it is not specified on data that is calibrated, and an error is raised if it is specified when the data is not calibrated.

In uvcalibrate, I am forcing the parameter to be set (either inherited from the UVCal or set directly as a keyword to the function).

Motivation and Context

The primary motivation is to keep track of this convention in data, so that the combination of calibration and power-spectrum estimation can be done consistently without "external knowledge". This is something useful for HERA at the moment.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation change (documentation changes only)
  • Version change
  • Build or continuous integration change

Checklist:

New feature checklist:

  • I have added or updated the docstrings associated with my feature using the numpy docstring format.
  • I have updated the tutorial to highlight my new feature (if appropriate).
  • I have added tests to cover my new feature.
  • All new and existing tests pass.
  • I have updated the CHANGELOG.

Copy link
Member

@bhazelton bhazelton left a comment

Choose a reason for hiding this comment

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

This is a good start but we need ways to round trip this new parameter through the various file formats for UVCal and UVData. I'm happy to help on that front if you need.

src/pyuvdata/utils/uvcalibrate.py Outdated Show resolved Hide resolved
src/pyuvdata/utils/uvcalibrate.py Outdated Show resolved Hide resolved
@steven-murray
Copy link
Contributor Author

Thanks @bhazelton -- I've fixed up the things you suggested, and also added the read/write to uvh5. I don't know enough about fits/ms to help there, so if you could manage that part, I'd be very grateful. I added a test for uvdata to test that warnings/errors are raised appropriately. My guess is that read/write will be automatically covered in round-trip tests.

@steven-murray
Copy link
Contributor Author

However, there might be a lot of new warnings now with old file types.

@jsdillon
Copy link
Contributor

jsdillon commented Jul 8, 2024

Is this branch at the point where I can use it to start writing files with the pol convention attribute, or should I hold off?

@steven-murray
Copy link
Contributor Author

@bhazelton we need to decide how we want to fix the breaking tests:

  1. Update all the lists of expected warnings to include one more
  2. Update (most of) the data files so the warnings aren't raised.
  3. something else?

@bhazelton
Copy link
Member

@bhazelton we need to decide how we want to fix the breaking tests:

  1. Update all the lists of expected warnings to include one more
  2. Update (most of) the data files so the warnings aren't raised.
  3. something else?

I can accept either 1 or 2, although maybe we need a wider discussion about whether we want to warn about this every time we call check, especially on UVData because this is not something that exists on any of the existing formats we support, so it feels a little presumptuous to yell at all our users about it all the time. Maybe we can instead warn only in the uvcalibrate method if it's not set and then advertise the existence of this new attribute in the tutorial and docs?

Copy link
Contributor

@kartographer kartographer left a comment

Choose a reason for hiding this comment

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

I think overall the code is looking in pretty good shape, and have nothing in terms of detailed comments to offer aside from highlighting that the doctest is still failing.

As a more general comment, I do think it would be good to put a longer explanation into the tutorials about this, since I can easily see where this might trip someone up. In particular, I think most of the higher frequency interferometers (e.g., VLA, ALMA, SMA, ATCA, ATA) take the "avg" approach, so it may be good to be explicit about what's typical/where "sum" is definitely used (since I think its the less common option of the two).

@bhazelton
Copy link
Member

I have a question:
Should we error in UVCal.check if the pol_convention is set but the gain_scale isn’t? I feel like setting the pol_convention without the gain_scale isn’t very meaningful. This is already partially handled in uvcalibrate where the pol_convention isn't used if gain_scale isn't set, but I'm wondering about adding this to the UVCal check.

@bhazelton bhazelton added the HERA Things needed to support HERA development. label Jul 15, 2024
Copy link

codecov bot commented Jul 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.93%. Comparing base (3b412bc) to head (f66efe3).
Report is 127 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1455   +/-   ##
=======================================
  Coverage   99.92%   99.93%           
=======================================
  Files          61       61           
  Lines       21380    21461   +81     
=======================================
+ Hits        21365    21446   +81     
  Misses         15       15           

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

@jsdillon
Copy link
Contributor

I think a warning is probably sufficient, but I wouldn't necessarily be opposed to an error in that case.

@steven-murray
Copy link
Contributor Author

I agree with @jsdillon that a warning is the right balance here.

@bhazelton
Copy link
Member

I added the warning and updated the docstrings a bit.

Copy link
Member

@bhazelton bhazelton left a comment

Choose a reason for hiding this comment

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

Thanks for the new docs, they are great. Just a few comments.

Still working on figuring out the circleci issue...

docs/uvcal_tutorial.rst Outdated Show resolved Hide resolved
docs/uvcal_tutorial.rst Outdated Show resolved Hide resolved
docs/uvcal_tutorial.rst Show resolved Hide resolved
src/pyuvdata/utils/uvcalibrate.py Show resolved Hide resolved
tests/utils/test_uvcalibrate.py Show resolved Hide resolved
tests/utils/test_uvcalibrate.py Show resolved Hide resolved
Comment on lines 548 to 563
def _apply_pol_convention_corrections(
uvdata, undo, uvd_pol_convention, uvc_pol_convention
):
if uvd_pol_convention != uvc_pol_convention:
correction = np.ones(uvdata.Npols)
correction[uvdata.polarization_array > 0] *= 2
correction[uvdata.polarization_array < 0] /= 2

if (undo and uvd_pol_convention == "sum") or (
not undo and uvd_pol_convention == "avg"
):
correction = 1 / correction

uvdata.data_array *= correction

uvdata.pol_convention = None if undo else uvd_pol_convention
Copy link
Member

Choose a reason for hiding this comment

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

This is pretty hard to parse and be confident in, but I think your test shows that it's working properly, at least for linear pols. I don't see a test on Stokes visibilities (i.e. when uvdata.polarization_array > 0). Is there a test for that case?

Also, please move this to above uvcalibrate so it's defined before it's called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK cool. I moved the function, wrote a docstring for it to hopefully make the logic more clear, and also added tests for circular polarizations. However, in doing this I found that it seems that it is not possible to calibrate/decalibrate uvdata objects with stokes polarizations (it complains that the polarizations can't be found on the UVCal object).

Is this to be expected?

Copy link
Member

Choose a reason for hiding this comment

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

I don't really know about a case where stokes calibration solutions make sense, but I feel like @kartographer ran into it when he was implementing MS cal read/write?

Copy link
Contributor

Choose a reason for hiding this comment

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

So this is definitely a corner-case (that could be wrapped with a "pyuvdata does not support... if you file an issue..."), but the most common case I ran into was visibilities recorded as (pseudo-)Stokes I, which can have gains tables corresponding also to (pseudo-)Stokes I. Looking at the UVCal class though, we don't currently allow for Jones > 0, which is maybe what's causing the issue here.

This got raised in the MS read/write mostly on the visibility side, since the MS format has a FEED table that I had to match to the data, hence why those pseudo Stokes values are supported there.

Copy link
Contributor

@kartographer kartographer 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 additional comments...

``I = (XX + YY)/2`` (the ``avg`` convention) or ``I = XX + YY`` (the ``sum``
convention). This choice is generally encoded in the sky model to which the visibilities
are calibrated. Different tools and simulators make different choices, generally following
a standard choice for the field. In ``pyuvdata`` either of these choices are OK, but the
Copy link
Contributor

Choose a reason for hiding this comment

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

Different tools and simulators make different choices, generally following a standard choice for the field.

I think we need to be more explicit here about which tools do what, especially if we are going to recommend that users always specify this parameter. In particular, I believe the tasks in CASA (e.g., tclean) and MIRIAD, along with WSClean, all assume the avg convention. I actually am not sure of what uses the sum convention, though I expect we can gather that information.

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'm not very familiar with such tools, but I'll add that particular information.

Copy link
Member

Choose a reason for hiding this comment

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

FHD uses the sum convention and the various HERA packages are moving to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. I added that as well.

if uvc_pol_convention is None and uvcal.pol_convention is None:
warnings.warn(
message=(
"pol_convention is not specified on the UVCal object, and "
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know that I'd want this to error when the pol convention is not specified on either UVCal and UVData, at least not if we're treating pol_convention as an optional parameter. Why can't this just stay a warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry @kartographer, I'm not following -- this is a warning?

Copy link
Contributor

Choose a reason for hiding this comment

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

@steven-murray -- sorry, I mean a general UserWarning, not a DeprecationWarning. I.e., this should alert the user that there's a potential for mismatch, but I don't think this should throw an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I updated all the DeprecationWarning's to UserWarnings. Originally I had intended these to be DeprecationWarnings because we want to push people to use the keywords. However, it would be really annoying to have that warning if you really just don't care about the final units (like, you're just using calibration solutions to multiply your data by 2...)

Copy link
Contributor

@kartographer kartographer left a comment

Choose a reason for hiding this comment

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

Looking good to me -- thanks @steven-murray!

@bhazelton bhazelton merged commit 6148ba9 into main Jul 23, 2024
46 checks passed
@bhazelton bhazelton deleted the pol-convention branch July 23, 2024 20:17
@jsdillon
Copy link
Contributor

Thanks all!

You planning a new minor version release on pypi?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Calibration enhancement HERA Things needed to support HERA development. UVData
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants