-
Notifications
You must be signed in to change notification settings - Fork 12
Adds attenuation transmission interconversion calculations #2047
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 4 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
6333d02
Initial commit of transmission conversions
Matt-Carre 1040280
added docstrings
Matt-Carre ef61984
initial commit
Matt-Carre 7e8a3f5
adds pydantic validation
Matt-Carre 64c844c
Added pydantic validation
Matt-Carre 4d48090
changes to NonNegativeFloat (accepting 0) where relevant
Matt-Carre da51855
changes to NonNegativeFloat (accepting 0) where relevant, considering…
Matt-Carre e5c341d
fixes linting. going to move tests next update.
Matt-Carre 5215f96
labels tests
Matt-Carre 3d6a920
Merge branch '2040_attenuation_transmission' into 2041_interconversio…
Matt-Carre 273cbff
added test labels
Matt-Carre c9ef824
split tests
Matt-Carre 7d94b52
split tests
Matt-Carre c32281d
merged in 2040
Matt-Carre 10f65f8
adds specific output
Matt-Carre 1a01d56
Merge branch 'main' into 2041_interconversion_depth_attenuation
Matt-Carre 8896dd1
Merge branch 'main' into 2040_attenuation_transmission
Matt-Carre 9c5f36b
Merge branch 'main' into 2041_interconversion_depth_attenuation
Matt-Carre a4fed4d
Merge branch 'main' into 2040_attenuation_transmission
Matt-Carre d4edcc1
Merge branch 'main' into 2041_interconversion_depth_attenuation
Matt-Carre 0e3b3a9
Merge branch 'main' into 2040_attenuation_transmission
Matt-Carre 1651348
Merge branch '2040_attenuation_transmission' of github.com:DiamondLig…
Matt-Carre c243228
rejoins tests
Matt-Carre 3516fcb
Merge branch '2040_attenuation_transmission' into 2041_interconversio…
Matt-Carre 788ad00
rejoined tests
Matt-Carre 7763449
Merge branch '2041_interconversion_depth_attenuation' of github.com:D…
Matt-Carre 3ce2471
updates based on review
Matt-Carre f1c30c5
renames test
Matt-Carre 1fd5e87
fixed more comments
Matt-Carre 7aa9941
fixed more comments
Matt-Carre b5e6663
adds bools to bad input
Matt-Carre 28fce46
fixed based on comments in 2041
Matt-Carre a706c3a
fixes spelling mistake
Matt-Carre a20395e
changes name
Matt-Carre d0874bf
Merge branch '2041_interconversion_depth_attenuation' into 2040_atten…
Matt-Carre 91cb0c6
Merge branch 'main' into 2040_attenuation_transmission
Matt-Carre fbbd278
alters to make sure bool is rejected
Matt-Carre 18dbcbf
updates docstrings
Matt-Carre 3b533b9
Merge branch '2040_attenuation_transmission' of github.com:DiamondLig…
Matt-Carre 38d30e3
Merge branch 'main' into 2040_attenuation_transmission
Matt-Carre 44bd4e7
Merge branch 'main' into 2040_attenuation_transmission
Matt-Carre eed3807
Merge branch 'main' into 2040_attenuation_transmission
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.
55 changes: 55 additions & 0 deletions
55
src/dodal/common/general_maths/transmission_interconversion.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,55 @@ | ||
| import math | ||
|
|
||
|
|
||
| def attenuation_from_natural_log_of_transmission(ln_t: float): | ||
| """Converts from natural log of transmission fraction into Barnett attenuation units | ||
| . | ||
|
|
||
| Args: | ||
| ln_t (float): natural log of transmission fraction | ||
|
|
||
| Returns: | ||
| (float): Barnett attenuation units | ||
| """ | ||
| _canonical_barnett_conversion = -1.0e3 | ||
|
Matt-Carre marked this conversation as resolved.
Outdated
|
||
| return _canonical_barnett_conversion * ln_t | ||
|
|
||
|
|
||
| def attenuation_from_transmission(transmission_as_fraction: float): | ||
| """Converts from transmission fraction into Barnett attenuation units. | ||
|
|
||
| Args: | ||
| transmission_as_fraction (float): transmission fraction | ||
|
|
||
| Returns: | ||
| (float): Barnett attenuation units. | ||
| """ | ||
| ln_t = math.log(transmission_as_fraction) | ||
| return attenuation_from_natural_log_of_transmission(ln_t) | ||
|
|
||
|
|
||
| def natural_log_of_transmission_from_attenuation(attenuation_bn: float): | ||
| """Converts from Barnett attenuation units into natural log of transmission fraction | ||
| . | ||
|
|
||
| Args: | ||
| attenuation_bn (float): Barnett attenuation units | ||
|
|
||
| Returns: | ||
| (float): natural log of transmission fraction | ||
| """ | ||
| _reverse_barnett_conversion = -1.0e-3 | ||
| return _reverse_barnett_conversion * attenuation_bn | ||
|
|
||
|
|
||
| def transmission_from_attenutation(attenuation_bn: float): | ||
| """Converts from Barnett attenuation units into transmission fraction. | ||
|
|
||
| Args: | ||
| attenuation_bn (float): Barnett attenuation units | ||
|
|
||
| Returns: | ||
| (float): transmission fraction | ||
| """ | ||
| ln_t = natural_log_of_transmission_from_attenuation(attenuation_bn) | ||
| return math.exp(ln_t) | ||
Empty file.
38 changes: 38 additions & 0 deletions
38
tests/common/general_maths/test_attenuation_from_ln_transmission.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,38 @@ | ||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| from dodal.common.general_maths import transmission_interconversion | ||
|
|
||
|
|
||
| # happy path | ||
| @pytest.mark.parametrize( | ||
| "ln_t,result", | ||
| [ | ||
| (-1, 1000), # tests negative unity log of transmission is 1000 (canonical) | ||
| (0, 0), # tests natural log of transparency is zero (canonical) | ||
| ( | ||
| -0.4367, | ||
| 436.7, | ||
| ), # tests log from arbitrary high transmission is -1000 * log | ||
| ( | ||
| -5.9017, | ||
| 5901.7, | ||
| ), # tests log from arbitrary low transmission is -1000 * log | ||
| ], | ||
| ) | ||
| def test_attenuation_from_natural_log_of_transmission(ln_t, result): | ||
| assert transmission_interconversion.attenuation_from_natural_log_of_transmission( | ||
| ln_t | ||
| ) == pytest.approx(result) | ||
|
|
||
|
|
||
| # inauspicious: | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bad_input", ["a", [], None, math.sin, object()]) | ||
| def test_attenuation_from_natural_log_of_transmission_raises_error(bad_input): | ||
| with pytest.raises(TypeError): | ||
| transmission_interconversion.attenuation_from_natural_log_of_transmission( | ||
| bad_input | ||
| ) |
37 changes: 37 additions & 0 deletions
37
tests/common/general_maths/test_attenuation_from_transmission.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,37 @@ | ||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| from dodal.common.general_maths import transmission_interconversion | ||
|
|
||
| # happy: | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "transmission_as_fraction,result", | ||
| [ | ||
| (1, 0), # tests attenuation from transparency is zero (canonical) | ||
| (0.37, 994.25227334), # tests attenuation from 37% is close to 1000 (canonical) | ||
| ( | ||
| 0.871, | ||
| 138.1133, | ||
| ), # tests attenuation from arbitrary high transmission has expected value | ||
| ( | ||
| 4.2e-4, | ||
| 7775.2558, | ||
| ), # tests attenuation from arbitrary high attenuation has expected value | ||
| ], | ||
| ) | ||
| def test_attenuation_from_transmission(transmission_as_fraction, result): | ||
| assert transmission_interconversion.attenuation_from_transmission( | ||
| transmission_as_fraction | ||
| ) == pytest.approx(result) | ||
|
|
||
|
|
||
| # inauspicious: | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bad_input", ["a", [], None, math.sin, object()]) | ||
| def test_attenuation_from_transmission_raises_error(bad_input): | ||
| with pytest.raises(TypeError): | ||
| transmission_interconversion.attenuation_from_transmission(bad_input) |
42 changes: 42 additions & 0 deletions
42
tests/common/general_maths/test_ln_transmission_from_attenuation.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,42 @@ | ||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| from dodal.common.general_maths import transmission_interconversion | ||
|
|
||
|
|
||
| # happy: | ||
| @pytest.mark.parametrize( | ||
| "attenuation_bn,result", | ||
| [ | ||
| (0, 0), # tests natural log of transparency from attenuation (canonical) | ||
| ( | ||
| 1e3, | ||
| -1, | ||
| ), # tests natural log of transmission from canonical transmission is negative | ||
| # unity (canonical) | ||
| ( | ||
| 712.6, | ||
| -0.7126, | ||
| ), # tests attenuation from natural log arbitrary high transmission is -1000 * | ||
| # attenuation | ||
| ( | ||
| 8034.1, | ||
| -8.0341, | ||
| ), # tests attenuation from natural log arbitrary low transmission is -1000 * | ||
| # attenuation | ||
| ], | ||
| ) | ||
| def test_natural_log_of_transmission_from_attenuation(attenuation_bn, result): | ||
| assert transmission_interconversion.natural_log_of_transmission_from_attenuation( | ||
| attenuation_bn | ||
| ) == pytest.approx(result) | ||
|
|
||
|
|
||
| # inauspicious: | ||
| @pytest.mark.parametrize("bad_input", ["a", [], None, math.sin, object()]) | ||
| def test_natural_log_of_transmission_from_attenuation_raises_error(bad_input): | ||
| with pytest.raises(TypeError): | ||
| transmission_interconversion.natural_log_of_transmission_from_attenuation( | ||
| bad_input | ||
| ) |
39 changes: 39 additions & 0 deletions
39
tests/common/general_maths/test_transmission_from_attenuation.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,39 @@ | ||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| from dodal.common.general_maths import transmission_interconversion | ||
|
|
||
|
|
||
| # happy: | ||
| @pytest.mark.parametrize( | ||
| "attenuation_bn,result", | ||
| [ | ||
| (0, 1), # tests transparency from zero attenuation (canonical) | ||
| ( | ||
| 1e3, | ||
| 0.3678794, | ||
| ), # tests transmission from canonical attenuation is 1/e (canonical) | ||
| ( | ||
| 145.1, | ||
| 0.8649358, | ||
| ), # tests transmission from arbitrary weak attenuation is -1000*attenuation | ||
| ( | ||
| 7221.9, | ||
| 7.3041331435179e-4, | ||
| ), # tests transmission from arbitrary strong attenuation is -1000*attenuation | ||
| ], | ||
| ) | ||
| def test_transmission_from_attenutation(attenuation_bn, result): | ||
| assert transmission_interconversion.transmission_from_attenutation( | ||
| attenuation_bn | ||
| ) == pytest.approx(result) | ||
|
|
||
|
|
||
| # inauspicious: | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bad_input", ["a", [], None, math.sin, object()]) | ||
| def test_transmission_from_attenutation_raises_error(bad_input): | ||
| with pytest.raises(TypeError): | ||
| transmission_interconversion.transmission_from_attenutation(bad_input) |
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.