Skip to content

BUG: Fix VOTable unit format on write so it does not hardcode to CDS format always#17570

Merged
pllim merged 5 commits into
astropy:mainfrom
pllim:vo-jybeam
Feb 11, 2025
Merged

BUG: Fix VOTable unit format on write so it does not hardcode to CDS format always#17570
pllim merged 5 commits into
astropy:mainfrom
pllim:vo-jybeam

Conversation

@pllim

@pllim pllim commented Dec 19, 2024

Copy link
Copy Markdown
Member

Description

This pull request is to fix #17497 . Also see #11032 (v4.3)

Blocked by

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@github-actions

Copy link
Copy Markdown
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

@pllim pllim marked this pull request as ready for review December 19, 2024 20:31
Comment thread astropy/io/votable/tree.py Outdated
if "unit" in attrib:
attrib["unit"] = self.unit.to_string("cds")
format = _get_unit_format(self._config)
attrib["unit"] = self.unit.to_string(format)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for writing the PR!

An other issue pointed by Tom in the original thread was that the standard does not impose a format for the units, but only recommends it. So using any format (even out of cds and vounit) should raise (at most) a warning and not an error.

Maybe the call of unit.to_string should catch any ValueError and transform it into a warning?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We could do that but I fear the side effects. Would be nice if we have a realistic use case of such violation that should be allowed to add to the test suite. Since I am not VO expert, do you have any suggestion on such a test? I would like to use it to make sure both read/write work. Thanks!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@ManonMarchand , are you able to assist in providing a test case to push this forward? Thanks!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, sorry.

Could be an exact reproducer of #17497 :

from astropy.table import Table
import astropy.units as u
import tempfile
table = Table({'a': [1, 2, 3]})
table["a"].unit = u.Unit("mJy / beam")
fp = tempfile.TemporaryFile()
table.write(fp, format="votable")

This raises an error while it should be a warning (at most).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@ManonMarchand , what I am looking for is a use case where a blind catch of ValueError is actually necessary and essential for VO operations. The example you provided in #17570 (comment) currently throws ValueError on main without this patch, but works fine with this patch already.

p.s. Instead of tempfile.TemporaryFile(), I used io.BytesIO() but that shouldn't matter.

@ManonMarchand ManonMarchand Feb 6, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good that it works now. Then, since the standard only recommends to use standard units but does not enforce it, things like deprecated units

table["a"].unit = u.Unit("Crab", format="ogip")

or even invented ones

table["a"].unit = u.def_unit("my_unit")

Should pass too. This time with warnings for sure.

I think this would translate Tom's comment here #17497 (comment)
We also need to make sure that these can be read too

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reading #17497 (comment) again, I am a little worried that it goes against the philosophy of "lenient on read, strict on write." Turning exception to warning would encourage "bad behaviors" of continuing to write out non-standard information ("they are not stopping us, why fix?"). That said, I was not in those VO discussions, so I will see what I can do to this PR to honor the request. Thanks for the examples!

@ManonMarchand ManonMarchand Feb 6, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wasn't there neither. @tomdonaldson : what are your thoughts?
Since @msdemlei is one of the authors of the standard, maybe you have a clearer view on what should be done in terms of writing non-VOunits in VOTables? Warn or prevent it?

Comment thread docs/changes/io.votable/17570.bugfix.rst Outdated
@msdemlei

msdemlei commented Feb 6, 2025 via email

Copy link
Copy Markdown
Contributor

@ManonMarchand

Copy link
Copy Markdown
Member

Thanks Markus!
Thanks a lot @pllim for writing the PR, all green lights on my side

@pllim

pllim commented Feb 6, 2025

Copy link
Copy Markdown
Member Author

@msdemlei , thank you for your detailed explanation. I am afraid that auto fixing invalid input units so they are compliant on read would be beyond my VO skills. Is this PR okay as is? If not, we can also close without merge and let someone expert in VO take over. Please advise. Thanks!

Failed: DID NOT WARN.

I need to figure out how test should handle warning in double run. Does not affect actual proposed functional code.

@msdemlei

msdemlei commented Feb 6, 2025 via email

Copy link
Copy Markdown
Contributor

@pllim

pllim commented Feb 6, 2025

Copy link
Copy Markdown
Member Author

On the testing side, the double run is giving me pain. I tried several ways but nothing would make the warnings behave consistently on the second run for this new test. I might need to investigate in a separate PR for the double run job itself before I can get back to this.

@saimn saimn modified the milestones: v7.0.1, v7.0.2 Feb 6, 2025
@pllim pllim force-pushed the vo-jybeam branch 2 times, most recently from 0db9094 to b4104fb Compare February 8, 2025 01:37
Comment thread astropy/io/votable/tree.py Outdated
assert stored_votable["sfr"].unit == u.solMass / u.year


def test_write_jybeam_unit(tmp_path, recwarn):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Please do review carefully whether the tested behavior makes sense to you.

Might wanna ignore the double run rabbit hole. I tried tweaking

max_warnings = _config.ConfigItem(

or calling

def reset_vo_warnings():

but that just broke even more tests...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Update: double run should be handled now via recwarn. Please do still review carefully.

@msdemlei

msdemlei commented Feb 10, 2025 via email

Copy link
Copy Markdown
Contributor

@pllim

pllim commented Feb 10, 2025

Copy link
Copy Markdown
Member Author

W50 should probably be derived from UnitsWarning, too, with the rationale that people who have silenced UnitsWarning-s probably won't want to see W50 either; and people who have silenced VOTable warnings won't want to see it either, so we'd have multiple inheritance and need to hope that the warnings filter actually works the way I hope it does.

While that makes sense, it is out of scope here and probably a very breaking change. io.votable and units were developed separately. io.votable has its own way of defining and handling warnings/exceptions. To have only W50 controlled by something else (in addition to existing machinery) might turn into a headache.

The decision in this PR solely is about whether to emit a W50 or UnitsWarning, without changing behavior of either.

I can also try ask the pyvo channel on Astropy Slack and see. Thanks!

@msdemlei

msdemlei commented Feb 10, 2025 via email

Copy link
Copy Markdown
Contributor

pllim added a commit to pllim/astropy-project that referenced this pull request Feb 10, 2025
pllim and others added 5 commits February 10, 2025 15:01
on write so it does not hardcode to CDS format always.
Co-authored-by: Eero Vaher <eero.vaher@gmail.com>
Update test and change log to match.
with votable warning machinery
@pllim

pllim commented Feb 10, 2025

Copy link
Copy Markdown
Member Author

I changed the votable warning to W50 and also rebased. FYI.

@pllim pllim requested a review from msdemlei February 10, 2025 20:19

@msdemlei msdemlei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am probably missing the intricacies of the test_jbeam_unit test, so all I can say is that it looks plausible. Other than that, I think this is the behaviour we want, so: Let's go.

@pllim

pllim commented Feb 11, 2025

Copy link
Copy Markdown
Member Author

Thanks, @msdemlei !

@ManonMarchand , does your approval from last week still stand?

@ManonMarchand

Copy link
Copy Markdown
Member

It does, thanks again for fixing this!

@pllim pllim merged commit d19147c into astropy:main Feb 11, 2025
@pllim pllim deleted the vo-jybeam branch February 11, 2025 16:17
meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Feb 11, 2025
@pllim

pllim commented Feb 11, 2025

Copy link
Copy Markdown
Member Author

Thanks for your reviews! Please let us know if this is still insufficient. This patch would land in nightly wheel tomorrow but if you need it sooner, please also let me know.

pllim added a commit that referenced this pull request Feb 11, 2025
…570-on-v7.0.x

Backport PR #17570 on branch v7.0.x (BUG: Fix VOTable unit format on write so it does not hardcode to CDS format always)
pllim added a commit to astropy/astropy-project that referenced this pull request Mar 7, 2025
* Add policy for adding new role

* Address astropy/astropy#17570 (review)

* Address dhomeier comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot write votable with units equivalent to Jy/beam

5 participants