-
Notifications
You must be signed in to change notification settings - Fork 1
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
Cif 247 increase unit test coverage for cif #75
Merged
kcartier-wri
merged 15 commits into
main
from
CIF-247-Increase-unit-test-coverage-for-CIF
Sep 13, 2024
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
47f529b
First work on adding more extensive results testing for layers
kcartier-wri 6014a9c
Implemented additional dimensional tests on layers
kcartier-wri 181f858
Further standardizing and extending tests
kcartier-wri 10b2205
Extending testing
kcartier-wri 7977d11
Additional tests
kcartier-wri 0271e19
Changed output file type for vector layers
kcartier-wri 949e097
Delete populated directories during diretory cleanup
kcartier-wri a3936fe
Added metrics tests
kcartier-wri 315377c
Merge branch 'main' into CIF-247-Increase-unit-test-coverage-for-CIF
kcartier-wri f2c09f8
Added exception evaluation
kcartier-wri 66e536d
Merge branch 'CIF-247-Increase-unit-test-coverage-for-CIF' of https:/…
kcartier-wri e95d189
Merge branch 'CIF-204-CORE-Use-Conda-for-running-tests-in-Github-Acti…
kcartier-wri 2ab01bf
Merge branch 'main' into CIF-247-Increase-unit-test-coverage-for-CIF
kcartier-wri 1a23574
Edits following review
kcartier-wri 3719d7a
Merge branch 'main' into CIF-247-Increase-unit-test-coverage-for-CIF
kcartier-wri 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 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 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 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
Binary file modified
BIN
+1.53 KB
(110%)
tests/resources/layer_dumps_for_br_lauro_de_freitas/layers_for_br_lauro_de_freitas.qgz
Binary file not shown.
This file contains 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 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 |
---|---|---|
@@ -1,19 +1,128 @@ | ||
from city_metrix.layers import NdviSentinel2 | ||
import ee | ||
import pytest | ||
|
||
from city_metrix.layers import NdviSentinel2, TreeCover, Albedo, AlosDSM | ||
from tests.resources.bbox_constants import BBOX_BRA_LAURO_DE_FREITAS_1 | ||
from city_metrix.layers.layer import get_image_collection | ||
from tests.tools.general_tools import post_process_layer | ||
|
||
EE_IMAGE_DIMENSION_TOLERANCE = 1 # Tolerance compensates for variable results from GEE service | ||
COUNTRY_CODE_FOR_BBOX = 'BRA' | ||
BBOX = BBOX_BRA_LAURO_DE_FREITAS_1 | ||
|
||
def test_read_image_collection(): | ||
ic = ee.ImageCollection("ESA/WorldCover/v100") | ||
data = get_image_collection(ic, BBOX, 10, "test") | ||
|
||
expected_crs = 32724 | ||
expected_x_dimension = 187 | ||
expected_y_dimension = 199 | ||
|
||
assert data.rio.crs == expected_crs | ||
assert ( | ||
pytest.approx(expected_x_dimension, rel=EE_IMAGE_DIMENSION_TOLERANCE) == "x", | ||
pytest.approx(expected_y_dimension, rel=EE_IMAGE_DIMENSION_TOLERANCE) == "y" | ||
) | ||
|
||
def test_read_image_collection_scale(): | ||
ic = ee.ImageCollection("ESA/WorldCover/v100") | ||
data = get_image_collection(ic, BBOX, 100, "test") | ||
expected_x_dimension = 19 | ||
expected_y_dimension = 20 | ||
assert data.dims == {"x": expected_x_dimension, "y": expected_y_dimension} | ||
|
||
def test_albedo_dimensions(): | ||
data = Albedo().get_data(BBOX) | ||
analysis_data = post_process_layer(data, value_threshold=0.1, convert_to_percentage=True) | ||
|
||
expected_min = 0 | ||
jterry64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected_max = 34 | ||
expected_peak_value = 15 | ||
# peak_value, peak_count = get_count_by_value(analysis_data, expected_min, expected_max) | ||
|
||
# Bounding values | ||
actual_min = analysis_data.values.min() | ||
actual_max = analysis_data.values.max() | ||
|
||
# Peak frequency | ||
full_count = analysis_data.size | ||
mid_count_pct = get_value_percent(analysis_data, expected_peak_value, full_count, 0) | ||
|
||
# Value range | ||
assert actual_min == expected_min | ||
assert actual_max == expected_max | ||
# Peak frequency | ||
assert mid_count_pct == 21 | ||
|
||
def test_alos_dsm_dimensions(): | ||
analysis_data = AlosDSM().get_data(BBOX) | ||
|
||
expected_min = 16 | ||
expected_max = 86 | ||
expected_peak_value = 56 | ||
peak_value, peak_count = get_count_by_value(analysis_data, expected_min, expected_max) | ||
|
||
# Bounding values | ||
actual_min = analysis_data.values.min() | ||
actual_max = analysis_data.values.max() | ||
|
||
# Peak frequency | ||
full_count = analysis_data.size | ||
mid_count_pct = get_value_percent(analysis_data, expected_peak_value, full_count, 0) | ||
|
||
# Value range | ||
assert actual_min == expected_min | ||
assert actual_max == expected_max | ||
# Peak frequency | ||
assert mid_count_pct == 3 | ||
|
||
def test_ndvi_dimensions(): | ||
data = NdviSentinel2(year=2023).get_data(BBOX) | ||
data_for_map = post_process_layer(data, value_threshold=0.4, convert_to_percentage=True) | ||
analysis_data = post_process_layer(data, value_threshold=0.4, convert_to_percentage=True) | ||
|
||
expected_min = 0 | ||
actual_min = data_for_map.values.min() | ||
expected_max = 85 | ||
actual_max = data_for_map.values.max() | ||
expected_peak_value = 78 | ||
# peak_value, peak_count = get_count_by_value(analysis_data, expected_min, expected_max) | ||
|
||
# Bounding values | ||
actual_min = analysis_data.values.min() | ||
actual_max = analysis_data.values.max() | ||
|
||
# Peak frequency | ||
full_count = analysis_data.size | ||
mid_count_pct = get_value_percent(analysis_data, expected_peak_value, full_count, 0) | ||
|
||
# Value range | ||
assert actual_min == expected_min | ||
assert actual_max == expected_max | ||
# Peak frequency | ||
assert mid_count_pct == 11 | ||
|
||
|
||
def test_tree_cover(): | ||
actual = TreeCover().get_data(BBOX).mean() | ||
expected = 54.0 | ||
tolerance = 0.1 | ||
assert ( | ||
pytest.approx(expected, rel=tolerance) == actual | ||
) | ||
|
||
def get_value_percent(data, value, full_count, precision): | ||
jterry64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
count_for_value = data.values[data.values == value].size | ||
percent_of_cells_with_value = get_rounded_pct(full_count, count_for_value, precision) | ||
return percent_of_cells_with_value | ||
|
||
def get_rounded_pct(full_count, this_count, precision): | ||
return round((this_count/full_count)*100, precision) | ||
|
||
def get_count_by_value(data, min_value, max_value): | ||
peak_value = None | ||
peak_count = 0 | ||
for x in range(min_value, max_value): | ||
count = data.values[data.values == x].size | ||
if count > peak_count: | ||
peak_count = count | ||
peak_value = x | ||
|
||
return peak_value, peak_count |
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.
Is "dimensions" abstract here or mean x/y dimensions? It feels like you're mostly test x/y dimensions, but then actually test stats about the values in these two functions.
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.
"Dimensions" was abstract and I've renamed. I also renamed the overall file to "test_layer_metrics" and added a description near top of file.