-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/calculate nrd #34
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
12 commits
Select commit
Hold shift + click to select a range
d4ce159
add fucntion and test for building vertical strata
mdupaysign 1567279
add function and test for computng Ni and N
mdupaysign 08214ef
add commentary
mdupaysign e039af4
delete np.allclose
mdupaysign 829206f
add compute Ni and N in function pad_profile
mdupaysign 3985b70
rectify test and fucntion pad_profile
mdupaysign 9ec1323
rectify test without default parameters and refacto tests
mdupaysign 77a81f0
add function and test : compute nrd
mdupaysign d83de29
modify configs for pad_profil and update main function
mdupaysign cf62172
add compute nrd in calculate pad profile
mdupaysign 3e06696
Merge branch 'dev' into feat/calculate_nrd
mdupaysign 1f9291f
rectify test for main_pad_profil
mdupaysign 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
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
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
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
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 @@ | ||
| """ | ||
| Calculate the fractions of incoming rays intercepted for each "NRD" stratum. | ||
|
|
||
| """ | ||
| import numpy as np | ||
|
|
||
|
|
||
| def compute_nrd( | ||
| Ni: np.ndarray, | ||
| N: np.ndarray, | ||
| ) -> np.ndarray: | ||
| """Calculate the fractions of incoming rays intercepted for each "NRD" stratum. | ||
|
|
||
| Args: | ||
| Ni (np.ndarray): each of length `nlayers`, vegetation/ground hit count per stratum. | ||
| N (np.ndarray): each of length `nlayers`, cumulative entering-ray count per stratum (non-decreasing). | ||
|
|
||
| Returns: | ||
| np.ndarray: NRD values, corrected for boundary cases (NRD=0 or NRD=1) | ||
| per equations 23-24 of Pimont et al. 2018. | ||
| """ | ||
| # General case: | ||
| # Compute the fraction of intercepted rays per stratum. | ||
| # /!\ np.errstate(invalid="ignore") suppresses the RuntimeWarning triggered by 0/0 (when N=0 and Ni=0). | ||
| with np.errstate(invalid="ignore"): | ||
| nrd = Ni / N | ||
|
|
||
| # When N=0 then Ni=0 (no incoming rays → no interception): | ||
| # 0/0 yields NaN, replaced by 0 (null intercepted fraction). | ||
| nrd[np.isnan(nrd)] = 0.0 | ||
|
|
||
| # Boundary cases NRD=0 or NRD=1: | ||
| # Laplace Bayesian correction (Pimont et al. 2018, eq. 23-24). | ||
| # These extreme values make the PAD computation unstable. | ||
| # => (Ni+1)/(N+2) pulls the extremes slightly toward the center without affecting intermediate values. | ||
| i_nrdc = (nrd == 0) | (nrd == 1) | ||
| nrd[i_nrdc] = (Ni[i_nrdc] + 1) / (N[i_nrdc] + 2) | ||
|
|
||
| return nrd |
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
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,72 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from lidar_for_fuel.pad_profile.compute_nrd import compute_nrd | ||
|
|
||
| # ── NRD values for various Ni/N combinations ────────────────────────────────── | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "Ni,N,expected", | ||
| [ | ||
| ( | ||
| # Intermediate values: NRD in (0, 1) -> no Laplace correction applied | ||
| np.array([2, 3], dtype=float), | ||
| np.array([5, 6], dtype=float), | ||
| np.array([2 / 5, 3 / 6]), | ||
| ), | ||
| ( | ||
| # NRD=0 (Ni=0, N>0): Laplace correction -> (0+1) / (N+2) | ||
| np.array([0], dtype=float), | ||
| np.array([5], dtype=float), | ||
| np.array([1 / 7]), | ||
| ), | ||
| ( | ||
| # NRD=1 (Ni=N, N>0): Laplace correction -> (N+1) / (N+2) | ||
| np.array([5], dtype=float), | ||
| np.array([5], dtype=float), | ||
| np.array([6 / 7]), | ||
| ), | ||
| ( | ||
| # Ni=0, N=0: 0/0 -> NaN -> set to 0 -> Laplace correction -> 1/2 | ||
| np.array([0], dtype=float), | ||
| np.array([0], dtype=float), | ||
| np.array([0.5]), | ||
| ), | ||
| ( | ||
| # Mixed: covers NaN, NRD=0, intermediate, and NRD=1 in one array | ||
| np.array([0, 0, 2, 5], dtype=float), | ||
| np.array([0, 5, 5, 5], dtype=float), | ||
| np.array([0.5, 1 / 7, 0.4, 6 / 7]), | ||
| ), | ||
| ], | ||
| ids=[ | ||
| "intermediate_no_correction", | ||
| "nrd_zero_laplace_correction", | ||
| "nrd_one_laplace_correction", | ||
| "nan_case_ni0_n0", | ||
| "mixed_all_cases", | ||
| ], | ||
| ) | ||
| def test_compute_nrd(Ni, N, expected): | ||
| result = compute_nrd(Ni, N) | ||
| np.testing.assert_allclose(result, expected) | ||
|
|
||
|
|
||
| # ── invariants ──────────────────────────────────────────────────────────────── | ||
|
|
||
|
|
||
| def test_output_strictly_between_zero_and_one(): | ||
| rng = np.random.default_rng(42) | ||
| N = rng.integers(0, 20, size=100).astype(float) | ||
| Ni = np.array([rng.integers(0, int(n) + 1) for n in N], dtype=float) | ||
| result = compute_nrd(Ni, N) | ||
| assert np.all(result > 0) | ||
| assert np.all(result < 1) | ||
|
|
||
|
|
||
| def test_no_nan_in_output(): | ||
| Ni = np.array([0, 0, 3], dtype=float) | ||
| N = np.array([0, 5, 5], dtype=float) | ||
| result = compute_nrd(Ni, N) | ||
| assert not np.any(np.isnan(result)) |
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
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.
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.
c'est un allclose volontaire cette fois-ci ?
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.
oui : pour calculer (0+1)/(5+2) en flottant.