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

Update Camera object #231

Merged
merged 10 commits into from
Dec 24, 2023

Conversation

mwcraig
Copy link
Contributor

@mwcraig mwcraig commented Dec 22, 2023

This ended up doing too many things -- please let me know if you want me to break it into smaller PRs.

  1. Adds "max_adu" as a Camera attribute. I named it max_data_val instead, because...
  2. ...allow any data unit, but require user to say what it is by adding a data_unit attribute to Camera.
  3. Fix Modify Camera model so that it can generate a JSON schema so that we can generate a UI #165 by indicating how schema should be created for the types we define.
  4. Adds a bit of description to each field and some examples, which end up being displayed in the auto-generated UI.
  5. Drops the copy method because pydantic provides one
  6. Drops the __repr__ because pydantic provides one

(and did 5 and 6 to avoid having to update them every time a change is made)

@codecov-commenter
Copy link

codecov-commenter commented Dec 22, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d35a332) 53.80% compared to head (cd69d8c) 54.44%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #231      +/-   ##
==========================================
+ Coverage   53.80%   54.44%   +0.64%     
==========================================
  Files          23       23              
  Lines        2881     2911      +30     
==========================================
+ Hits         1550     1585      +35     
+ Misses       1331     1326       -5     

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

JuanCab
JuanCab previously approved these changes Dec 22, 2023
Copy link
Contributor

@JuanCab JuanCab left a comment

Choose a reason for hiding this comment

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

As far as I can tell, it looks good. I am not that experienced with pydantic, but otherwise, everything seemed reasonable. I'll approve it.

@@ -30,6 +30,36 @@
# https://docs.gammapy.org/dev/_modules/gammapy/analysis/config.html


class UnitType(Unit):
Copy link
Contributor

Choose a reason for hiding this comment

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

So I assume this is adding pydantic validators to the astropy units class? Do you know if it is pydantic v2 compatible? There is a part of me that worries that pydantic v2 is still waiting to bite us in the future.

yield cls.validate

@classmethod
def validate(cls, v):
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this validate doing? It just returns the Unit cast version of v? Does it actually validate anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the creation of the unit fails (e.g. Unit("nonsense")) then it generates a ValidationError


@classmethod
def __modify_schema__(cls, field_schema, field):
# Set default values for the schema in case the field doesn't provide them
Copy link
Contributor

Choose a reason for hiding this comment

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

I am honestly not sure how to assess what is going on here with __modify_schema__, need to dig into pydantic again or trust your tests. :). I will note __modify_schema__ doesn't appear to be supported in v2 of pydantic (based on https://docs.pydantic.dev/latest/errors/usage_errors/#custom-json-schema).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To the extent I understand it, this is providing some of the information needed to generate a json schema for types that are either not built in to Python or pydantic.

I wish ipyautoui had already made the 1 to 2 transition so we could just code for 2. It looks like there is a replacement for __modify_schema__ in 2.

name = "Unit"
description = "An astropy unit"

name = field.name or name
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this setting name to a true/false value based on field.name or name as a boolean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This sets name to field.name unless field.name evaluates to False, in which case name is used.

examples=["10.0 electron"],
)
dark_current: QuantityType = Field(
description="unit consisten with read noise, per unit time",
Copy link
Contributor

Choose a reason for hiding this comment

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

"unit consistent" (typo)

@@ -22,10 +22,25 @@
)
from stellarphot.settings import ApertureSettings

# Constants for the tests

GAINS = [1.0, 1.5, 2.0]
# Make sure the tests are deterministic by using a random seed
Copy link
Contributor

Choose a reason for hiding this comment

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

Really not 'using a random seed', rather you are forcing a 'fixed seed for random number generator'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is true...

GAINS = [1.0, 1.5, 2.0]
# Make sure the tests are deterministic by using a random seed
SEED = 5432985

SHIFT_TOLERANCE = 6
Copy link
Contributor

Choose a reason for hiding this comment

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

Good call to move these toward the front of the testing code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good, seems a pretty straight forward set of changes to using proper capitalized constants and to removing max_adu as an argument.

c = Camera(
max_adu = 50000 * u.adu

# All 5 of the attributes after data_unit will be checked for units
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, I didn't know you could do this with pytest, counting the number of errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It isn't counting the errors, pydantic is -- pytest is just checking that the text of the error message contains "5 validation errors"



def test_camera_schema():
# Check that we can generate a schema for a Camera and that it
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this schema method is a feature of pydantic...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah; it is used by ipyautoui when generating the UI.

@mwcraig mwcraig merged commit 13eb762 into feder-observatory:main Dec 24, 2023
9 checks passed
@mwcraig mwcraig deleted the photometry-notebook-settings branch February 8, 2024 15:24
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.

Modify Camera model so that it can generate a JSON schema so that we can generate a UI
3 participants