-
Notifications
You must be signed in to change notification settings - Fork 12
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
Update Camera object #231
Conversation
This fixes feder-observatory#165, which allows a UI to be autogenerated.
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
There was a problem hiding this 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): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
stellarphot/core.py
Outdated
examples=["10.0 electron"], | ||
) | ||
dark_current: QuantityType = Field( | ||
description="unit consisten with read noise, per unit time", |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
This ended up doing too many things -- please let me know if you want me to break it into smaller PRs.
Camera
attribute. I named itmax_data_val
instead, because...data_unit
attribute toCamera
.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.copy
method because pydantic provides one__repr__
because pydantic provides one(and did 5 and 6 to avoid having to update them every time a change is made)