|
| 1 | +import json |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +import pytest |
| 7 | +import yirgacheffe as yg |
| 8 | +from shapely.geometry import mapping, Polygon |
| 9 | + |
| 10 | +from aoh.validation.validate_occurences import process_species |
| 11 | + |
| 12 | +def test_empty_species_list() -> None: |
| 13 | + df = pd.DataFrame([], columns=['iucn_taxon_id', 'decimalLatitude', 'decimalLongitude']) |
| 14 | + res = process_species(Path("/some/aohs"), df) |
| 15 | + assert len(res) == 0 |
| 16 | + |
| 17 | +def generate_faux_aoh(filename: Path, shape: Polygon | None = None) -> None: |
| 18 | + |
| 19 | + shapes = [ |
| 20 | + shape if shape is not None else Polygon([(0, 0), (0, 10), (10, 10), (10, 0)]) |
| 21 | + ] |
| 22 | + |
| 23 | + features = [] |
| 24 | + for geom in shapes: |
| 25 | + feature = { |
| 26 | + "type": "Feature", |
| 27 | + "properties": {}, |
| 28 | + "geometry": mapping(geom) |
| 29 | + } |
| 30 | + features.append(feature) |
| 31 | + |
| 32 | + geojson = { |
| 33 | + "type": "FeatureCollection", |
| 34 | + "features": features |
| 35 | + } |
| 36 | + |
| 37 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 38 | + tmpdir_path = Path(tmpdir) |
| 39 | + geojson_path = tmpdir_path / "tmp.geojson" |
| 40 | + with open(geojson_path, 'w', encoding="UTF-8") as f: |
| 41 | + json.dump(geojson, f, indent=2) |
| 42 | + |
| 43 | + with yg.read_shape(geojson_path, ("epsg:4326", (1.0, -1.0))) as shape_layer: |
| 44 | + shape_layer.to_geotiff(filename) |
| 45 | + |
| 46 | +@pytest.mark.parametrize("taxon_id,latitude,longitude,expected",[ |
| 47 | + (42, 5.0, 5.0, True), |
| 48 | + (42, 12.0, 12.0, False), |
| 49 | + (40, 5.0, 5.0, False), |
| 50 | +]) |
| 51 | +def test_simple_match(taxon_id: int, latitude: float, longitude: float, expected: bool) -> None: |
| 52 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 53 | + tmpdir_path = Path(tmpdir) |
| 54 | + |
| 55 | + for test_id in [41, 42, 43]: |
| 56 | + aoh_path = tmpdir_path / f"{test_id}.tif" |
| 57 | + generate_faux_aoh(aoh_path) |
| 58 | + |
| 59 | + df = pd.DataFrame( |
| 60 | + [(taxon_id, latitude, longitude)], |
| 61 | + columns=['iucn_taxon_id', 'decimalLatitude', 'decimalLongitude'] |
| 62 | + ) |
| 63 | + |
| 64 | + res = process_species(tmpdir_path, df) |
| 65 | + |
| 66 | + assert len(res) == len(df) |
| 67 | + occurence = res.occurence[0] |
| 68 | + assert occurence == expected |
| 69 | + |
| 70 | +def test_multiple_match() -> None: |
| 71 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 72 | + tmpdir_path = Path(tmpdir) |
| 73 | + |
| 74 | + for test_id in [41, 42, 43]: |
| 75 | + aoh_path = tmpdir_path / f"{test_id}.tif" |
| 76 | + generate_faux_aoh(aoh_path) |
| 77 | + |
| 78 | + df = pd.DataFrame( |
| 79 | + [ |
| 80 | + (42, 5.0, 5.0, True), |
| 81 | + (42, 12.0, 12.0, False), |
| 82 | + ], |
| 83 | + columns=['iucn_taxon_id', 'decimalLatitude', 'decimalLongitude', 'expected'] |
| 84 | + ) |
| 85 | + |
| 86 | + res = process_species(tmpdir_path, df) |
| 87 | + |
| 88 | + assert len(res) == len(df) |
| 89 | + assert (res.occurence == res.expected).all() |
| 90 | + |
| 91 | +def test_too_many_ids() -> None: |
| 92 | + df = pd.DataFrame( |
| 93 | + [ |
| 94 | + (42, 5.0, 5.0, True), |
| 95 | + (42, 12.0, 12.0, False), |
| 96 | + (40, 5.0, 5.0, False), |
| 97 | + ], |
| 98 | + columns=['iucn_taxon_id', 'decimalLatitude', 'decimalLongitude', 'expected'] |
| 99 | + ) |
| 100 | + |
| 101 | + with pytest.raises(ValueError): |
| 102 | + _ = process_species(Path("/some/aohs"), df) |
| 103 | + |
| 104 | +@pytest.mark.parametrize("taxon_id,latitude,longitude,expected",[ |
| 105 | + (42, 5.0, 5.0, True), |
| 106 | + (42, -5.0, -5.0, True), |
| 107 | + (42, 5.0, -5.0, False), |
| 108 | + (42, -5.0, 5.0, False), |
| 109 | + (40, 5.0, 5.0, False), |
| 110 | +]) |
| 111 | +def test_find_seasonal(taxon_id: int, latitude: float, longitude: float, expected: bool) -> None: |
| 112 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 113 | + tmpdir_path = Path(tmpdir) |
| 114 | + |
| 115 | + for season, shape in [ |
| 116 | + ('breeding', Polygon([(0, 0), (0, 10), (10, 10), (10, 0)])), |
| 117 | + ('nonbreeding', Polygon([(0, 0), (0, -10), (-10, -10), (-10, 0)])), |
| 118 | + ]: |
| 119 | + aoh_path = tmpdir_path / f"42_{season}.tif" |
| 120 | + generate_faux_aoh(aoh_path, shape) |
| 121 | + |
| 122 | + df = pd.DataFrame( |
| 123 | + [(taxon_id, latitude, longitude)], |
| 124 | + columns=['iucn_taxon_id', 'decimalLatitude', 'decimalLongitude'] |
| 125 | + ) |
| 126 | + |
| 127 | + res = process_species(tmpdir_path, df) |
| 128 | + |
| 129 | + assert len(res) == len(df) |
| 130 | + occurence = res.occurence[0] |
| 131 | + assert occurence == expected |
0 commit comments