-
Notifications
You must be signed in to change notification settings - Fork 12
Adds basic unit conversions for attenuator in general_maths folder #2043
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9ed276e
initial commit
Matt-Carre 7079d52
moved
Matt-Carre 3300cef
working?
Matt-Carre e1d4b12
fixes last parts
Matt-Carre 5014607
first draft complete
Matt-Carre 9e14520
clears up some comments
Matt-Carre c260c91
Merge branch 'main' into 2038_basic_conversions
Matt-Carre f5e3e61
Merge branch 'main' into 2038_basic_conversions
Matt-Carre 47e1ae1
fixes after first code review
Matt-Carre 3be60a6
Merge branch '2038_basic_conversions' of github.com:DiamondLightSourc…
Matt-Carre e92ad39
splits testing up
Matt-Carre 4cd0a9f
fixes most of comments, still need pydantic stuff
Matt-Carre 3c86b4a
Adds pydantic validation (at last)
Matt-Carre 7618002
edits error message
Matt-Carre e5461e9
Merge branch 'main' of github.com:DiamondLightSource/dodal into 2038_…
Matt-Carre 782b147
Merge branch 'main' into 2038_basic_conversions
Matt-Carre 032aa4d
Merge branch 'main' into 2038_basic_conversions
Matt-Carre 7c3c57b
alters tests back to mirroring SRC file structure
Matt-Carre b2b5341
Merge branch '2038_basic_conversions' of github.com:DiamondLightSourc…
Matt-Carre 01d41bb
Merge branch 'main' into 2038_basic_conversions
Matt-Carre 9868b82
allows for boolean input to be rejected
Matt-Carre 3d4e637
adds strictfloat validation
Matt-Carre 1383712
Merge branch 'main' into 2038_basic_conversions
Matt-Carre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
107 changes: 107 additions & 0 deletions
107
src/dodal/common/general_maths/arithmetic_conversions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| """Provides functions to convert between common units for attenuation.""" | ||
|
|
||
| from pydantic import StrictFloat, validate_call | ||
|
|
||
|
|
||
| @validate_call | ||
| def convert_percentage_to_factor(pc: StrictFloat) -> float: | ||
| """Takes a percentage value and converts it the corresponding multiplication factor. | ||
|
|
||
| Args: | ||
| pc (StrictFloat): a percentage | ||
| Returns: | ||
| pc (float): a factor | ||
| """ | ||
| return pc * 1e-2 | ||
|
|
||
|
|
||
| @validate_call | ||
| def convert_factor_to_percentage(f: StrictFloat) -> float: | ||
| """Takes a multiplication factor and converts it to the corresponding percentage. | ||
|
|
||
| Args: | ||
| f (StrictFloat): a factor | ||
|
|
||
| Returns: | ||
| f (float): a percentage | ||
| """ | ||
| return f * 1e2 | ||
|
|
||
|
|
||
| @validate_call | ||
| def convert_microns_to_cm(t_um: StrictFloat) -> float: | ||
| """Takes the numerical part of a distance in microns and converts this to cm. | ||
|
|
||
| Args: | ||
| t_um (StrictFloat): a distance in microns | ||
|
|
||
| Returns: | ||
| t_um (float): a distance in cm | ||
| """ | ||
| return t_um * 1e-4 | ||
|
|
||
|
|
||
| @validate_call | ||
| def convert_microns_to_mm(v_um: StrictFloat) -> float: | ||
| """Takes the numerical part of a distance in microns and converts this to mm. | ||
|
|
||
| Args: | ||
| v_um (StrictFloat): a distance in microns | ||
|
|
||
| Returns: | ||
| v_um (float): a distance in mm | ||
| """ | ||
| return v_um * 1e-3 | ||
|
|
||
|
|
||
| @validate_call | ||
| def convert_mm_to_microns(w_mm: StrictFloat) -> float: | ||
| """Takes the numerical part of a distance in mm and converts this to microns. | ||
|
|
||
| Args: | ||
| w_mm (StrictFloat): a distance in mm | ||
|
|
||
| Returns: | ||
| w_um (float): a distance in microns | ||
| """ | ||
| return w_mm * 1e3 | ||
|
|
||
|
|
||
| @validate_call | ||
| def convert_mm_to_cm(x_mm: StrictFloat) -> float: | ||
| """Takes the numerical part of a distance in mm and converts this to cm. | ||
|
|
||
| Args: | ||
| x_mm (StrictFloat): a distance in mm | ||
|
|
||
| Returns: | ||
| w_um (float): a distance in cm | ||
| """ | ||
| return x_mm * 1e-1 | ||
|
|
||
|
|
||
| @validate_call | ||
| def convert_cm_to_mm(y_cm: StrictFloat) -> float: | ||
| """Takes the numerical part of a distance in cm and converts this to mm. | ||
|
|
||
| Args: | ||
| y_cm (StrictFloat): a distance in cm | ||
|
|
||
| Returns: | ||
| w_um (float): a distance in mm | ||
| """ | ||
| return y_cm * 1e1 | ||
|
|
||
|
|
||
| @validate_call | ||
| def convert_ev_to_kev(energy_ev: StrictFloat) -> float: | ||
| """Takes the numerical part of an x-ray energy in electron volts and converts this | ||
| to keV. | ||
|
|
||
| Args: | ||
| energy_ev (StrictFloat): a value of electron volts | ||
|
|
||
| Returns: | ||
| energy_ev (float): a value of kilo-electron volts | ||
| """ | ||
| return energy_ev * 1e-3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """Provides a checker to determine if a value is within a certain range.""" | ||
|
|
||
| from pydantic import BaseModel, StrictFloat, model_validator, validate_call | ||
|
|
||
|
|
||
| def subordinate_range_test( | ||
| lower_bound: StrictFloat, upper_bound: StrictFloat, x: StrictFloat | ||
| ) -> bool: | ||
| outside_range = x < lower_bound or x > upper_bound | ||
| return not outside_range | ||
|
|
||
|
|
||
| @validate_call | ||
| def is_within_range( | ||
| lower_bound: StrictFloat, | ||
| upper_bound: StrictFloat, | ||
| tested_value: StrictFloat, | ||
| ) -> bool: | ||
| """Checks if a single value falls between two bounds, a lower (first) and an upper | ||
| (second). | ||
|
|
||
| Args: | ||
| lower_bound (StrictFloat): the smaller of the two values | ||
| upper_bound (StrictFloat): the greater of the two values | ||
| tested_value (StrictFloat): the value to be tested | ||
|
|
||
| Returns: | ||
| outside_range (bool): True if the tested value falls between, or False if not | ||
| """ | ||
|
|
||
| class ValueRange(BaseModel): | ||
| lo: StrictFloat | ||
| hi: StrictFloat | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_bounds(self) -> "ValueRange": | ||
| if self.hi < self.lo: | ||
| raise ValueError(f"Upper bound {self.hi} < Lower bound {self.lo}") | ||
| return self | ||
|
|
||
| ValueRange(lo=lower_bound, hi=upper_bound) | ||
| return subordinate_range_test( | ||
| lower_bound, | ||
| upper_bound, | ||
| tested_value, | ||
| ) |
Empty file.
129 changes: 129 additions & 0 deletions
129
tests/common/general_maths/test_arithmetic_conversions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import math | ||
|
|
||
| import pydantic | ||
| import pytest | ||
|
|
||
| from dodal.common.general_maths.arithmetic_conversions import ( | ||
| convert_cm_to_mm, | ||
| convert_ev_to_kev, | ||
| convert_factor_to_percentage, | ||
| convert_microns_to_cm, | ||
| convert_microns_to_mm, | ||
| convert_mm_to_cm, | ||
| convert_mm_to_microns, | ||
| convert_percentage_to_factor, | ||
| ) | ||
|
|
||
|
|
||
| # expected success tests (the 'Happy Path'): All numbers here are arbitrary | ||
| @pytest.mark.parametrize("input,result", [(1.0, 0.1), (100.0, 10.0)]) | ||
| def test_conversion_from_millimetres_to_centimetres(input, result): | ||
| assert convert_mm_to_cm(input) == pytest.approx(result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,result", [(1.0, 10), (0.1, 1.0)]) | ||
| def test_conversion_from_centimetres_to_millimetres(input, result): | ||
| assert convert_cm_to_mm(input) == pytest.approx(result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,result", [(0.01, 1.0), (1.0, 100.0)]) | ||
| def test_conversion_to_percentage_from_factor(input, result): | ||
| assert convert_factor_to_percentage(input) == pytest.approx(result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,result", [(1.0, 0.01), (100, 1.0)]) | ||
| def test_conversion_to_factor_from_percentage(input, result): | ||
| assert convert_percentage_to_factor(input) == pytest.approx(result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,result", [(1000.0, 1.0), (10000.0, 10.0)]) | ||
| def test_conversion_from_microns_to_millimeters(input, result): | ||
| assert convert_microns_to_mm(input) == pytest.approx(result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,result", [(1.0, 1000.0), (10, 10000.0)]) | ||
| def test_conversion_from_millimeters_to_microns(input, result): | ||
| assert convert_mm_to_microns(input) == pytest.approx(result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,result", [(1000, 1.0), (100, 0.1)]) | ||
| def test_conversion_from_electronvolts_to_kiloelectronvolts(input, result): | ||
| assert convert_ev_to_kev(input) == pytest.approx(result) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input,result", [(10000.0, 1.0), (1000, 0.1)]) | ||
| def test_conversion_from_microns_to_centimetres(input, result): | ||
| assert convert_microns_to_cm(input) == pytest.approx(result) | ||
|
|
||
|
|
||
| # The inauspicuous path | ||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| ["a", [], None, math.sin, object(), False], | ||
| ) | ||
| def test_convert_microns_to_cm_raises_error_with_bad_input(bad_input): | ||
| with pytest.raises(pydantic.ValidationError): | ||
| convert_microns_to_cm(bad_input) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| ["a", [], None, math.sin, object(), False], | ||
| ) | ||
| def test_convert_ev_to_kev_raises_error_with_bad_input(bad_input): | ||
| with pytest.raises(pydantic.ValidationError): | ||
| convert_ev_to_kev(bad_input) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| ["a", [], None, math.sin, object(), False], | ||
| ) | ||
| def test_convert_microns_to_mm_raises_error_with_bad_input(bad_input): | ||
| with pytest.raises(pydantic.ValidationError): | ||
| convert_microns_to_mm(bad_input) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| ["a", [], None, math.sin, object(), False], | ||
| ) | ||
| def test_convert_mm_to_microns_raises_error_with_bad_input(bad_input): | ||
| with pytest.raises(pydantic.ValidationError): | ||
| convert_mm_to_microns(bad_input) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| ["a", [], None, math.sin, object(), False], | ||
| ) | ||
| def test_convert_factor_to_percentage_raises_error_with_bad_input(bad_input): | ||
| with pytest.raises(pydantic.ValidationError): | ||
| convert_factor_to_percentage(bad_input) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| ["a", [], None, math.sin, object(), False], | ||
| ) | ||
| def test_convert_percentage_to_factor_raises_error_with_bad_input(bad_input): | ||
| with pytest.raises(pydantic.ValidationError): | ||
| convert_percentage_to_factor(bad_input) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| ["a", [], None, math.sin, object(), False], | ||
| ) | ||
| def test_convert_mm_to_cm_raises_error_with_bad_input(bad_input): | ||
| with pytest.raises(pydantic.ValidationError): | ||
| convert_mm_to_cm(bad_input) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| ["a", [], None, math.sin, object(), False], | ||
| ) | ||
| def test_convert_cm_to_mm_raises_error_with_bad_input(bad_input): | ||
| with pytest.raises(pydantic.ValidationError): | ||
| convert_cm_to_mm(bad_input) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import math | ||
|
|
||
| import pydantic_core | ||
| import pytest | ||
|
|
||
| from dodal.common.general_maths.check_bounds import is_within_range | ||
|
|
||
|
|
||
| # Happy Path | ||
| @pytest.mark.parametrize( | ||
| "lower_bound,upper_bound,tested_value,result", | ||
| [ | ||
| (1.0, 3.0, 2.0, True), | ||
| (-3.0, -1.0, -2, True), | ||
| (-1.0, 1.0, 0.0, True), | ||
| (-1.0, -0.1, -0.5, True), | ||
| (0.1, 1, 0.5, True), | ||
| ], | ||
| ) | ||
| def test_is_within_range( | ||
| lower_bound: float, upper_bound: float, tested_value: float, result: bool | ||
| ): | ||
| assert is_within_range(lower_bound, upper_bound, tested_value) == result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "lower_bound,upper_bound,tested_value,result", | ||
| [ | ||
| (1, 2, 3, False), | ||
| (-3, -1, -4, False), | ||
| (1, 2, 0, False), | ||
| (-1, -0.1, -10.0, False), | ||
| (0.1, 1, 1.5, False), | ||
| ], | ||
| ) | ||
| def test_is_outside_range( | ||
| lower_bound: float, upper_bound: float, tested_value: float, result: bool | ||
| ): | ||
| assert is_within_range(lower_bound, upper_bound, tested_value) == result | ||
|
|
||
|
|
||
| # Inauspicious Path | ||
| def test_has_misordered_inputs(): | ||
| with pytest.raises(ValueError): | ||
| is_within_range(4, -4, 1) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| [ | ||
| ( | ||
| "a", | ||
| None, | ||
| math.sin, | ||
| object(), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_is_within_range_raises_error_with_bad_tested_value( | ||
| bad_input, | ||
| ): | ||
| with pytest.raises(pydantic_core.ValidationError): | ||
| is_within_range(-4, 4, bad_input) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| [ | ||
| ( | ||
| "a", | ||
| None, | ||
| math.sin, | ||
| object(), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_is_within_range_raises_error_with_bad_upper_bound( | ||
| bad_input, | ||
| ): | ||
| with pytest.raises(pydantic_core.ValidationError): | ||
| is_within_range(-4, bad_input, 4) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_input", | ||
| [ | ||
| ( | ||
| "a", | ||
| None, | ||
| math.sin, | ||
| object(), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_is_within_range_raises_error_with_bad_lower_bound( | ||
| bad_input, | ||
| ): | ||
| with pytest.raises(pydantic_core.ValidationError): | ||
| is_within_range(bad_input, 4, 0) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.