Skip to content

Commit 56f1333

Browse files
authored
Merge pull request #29 from quantifyearth/mwd-small-updates
Misc small updates
2 parents 1294c91 + 8323848 commit 56f1333

8 files changed

Lines changed: 33 additions & 15 deletions

File tree

prepare_species/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
"full_habitat_code",
2525
"scientific_name",
2626
"family_name",
27+
"order_name",
2728
"class_name",
29+
"phylum_name",
30+
"kingdom_name",
2831
"threats",
2932
"category",
3033
"category_weight",
@@ -225,7 +228,7 @@ def tidy_reproject_save(
225228
elevation_seperation=ELEVATION_SPREAD,
226229
)
227230
os.makedirs(output_directory_path, exist_ok=True)
228-
output_path = output_directory_path / f"{grow.id_no}.geojson"
231+
output_path = output_directory_path / f"range_T{grow.id_no}A{grow.assessment_id}_{grow.season}.geojson"
229232
res = gpd.GeoDataFrame(grow.to_frame().transpose(), crs=src_crs, geometry="geometry")
230233

231234
# Ensure proper dtypes for JSON serialization

prepare_species/extract_species_data_psql.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
(assessment_supplementary_infos.supplementary_fields->>'ElevationUpper.limit')::numeric AS elevation_upper,
4747
taxons.scientific_name,
4848
taxons.family_name,
49+
taxons.order_name,
50+
taxons.phylum_name,
51+
taxons.kingdom_name,
4952
red_list_category_lookup.code
5053
FROM
5154
assessments
@@ -133,7 +136,8 @@ def process_row(
133136
cursor = connection.cursor()
134137

135138
id_no, assessment_id, assessment_year, possibly_extinct, possibly_extinct_in_the_wild, \
136-
elevation_lower, elevation_upper, scientific_name, family_name, category = row
139+
elevation_lower, elevation_upper, scientific_name, family_name, \
140+
order_name, phylum_name, kingom_name, category = row
137141

138142
report = SpeciesReport(id_no, assessment_id, scientific_name)
139143
report.has_api_data = True
@@ -212,7 +216,10 @@ def process_row(
212216
'|'.join(habitats_list),
213217
scientific_name,
214218
family_name,
219+
order_name,
215220
class_name,
221+
phylum_name,
222+
kingom_name,
216223
json_ready_threats,
217224
category,
218225
category_weight,

prepare_species/extract_species_data_redlist.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ def process_species(
182182
assessment_year = assessment_dict['assessment_date'].year
183183
category = assessment_dict['red_list_category']
184184
family_name = assessment_dict['family_name']
185+
order_name = assessment_dict['order_name']
186+
phylum_name = assessment_dict['phylum_name']
187+
kingdom_name = assessment_dict['kingdom_name']
185188
possibly_extinct = assessment_dict['possibly_extinct']
186189
possibly_extinct_in_the_wild = assessment_dict['possibly_extinct_in_the_wild']
187190
infrarank = assessment_dict['infrarank']
@@ -261,7 +264,10 @@ def process_species(
261264
'|'.join(habitats_list),
262265
scientific_name,
263266
family_name,
267+
order_name,
264268
class_name,
269+
phylum_name,
270+
kingdom_name,
265271
json_ready_threats,
266272
category,
267273
CATEGORY_WEIGHTS[category],

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ scikit-image
99
redlistapi
1010
boto3
1111
yirgacheffe>=1.12,<2.0
12-
aoh[validation]>=2.0.3,<3.0
12+
aoh[validation]>=2.1.0,<3.0
1313

1414
# Snakemake workflow management
1515
snakemake>=8.0

threats/threat_processing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def threat_processing_per_species(
2525
os.makedirs(output_directory_path, exist_ok=True)
2626

2727
taxon_id = data.id_no[0]
28+
assessment_id = data.assessment_id[0]
2829

2930
# Due to validation we generate AOHs for many more species than
3031
# is needed for STAR, but we need to ensure those don't slip into
@@ -54,7 +55,7 @@ def threat_processing_per_species(
5455

5556
threat_dir_path = output_directory_path / str(threat_id)
5657
os.makedirs(threat_dir_path, exist_ok=True)
57-
output_path = threat_dir_path / f"{taxon_id}.tif"
58+
output_path = threat_dir_path / f"threat_T{taxon_id}A{assessment_id}.tif"
5859
per_threat_per_species_score.to_geotiff(output_path)
5960

6061
# This script generates a bunch of rasters, but snakemake needs one

workflow/rules/aoh.smk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def aoh_species_inputs(wildcards):
5353
/ "species-info"
5454
/ wildcards.taxa
5555
/ SCENARIO
56-
/ f"{wildcards.species_id}.geojson",
56+
/ f"range_{wildcards.species_id}.geojson",
5757
# Base layers (precious - won't trigger rebuilds)
5858
"habitat_sentinel": ancient(
5959
DATADIR / "habitat_layers" / SCENARIO / ".habitat_complete"
@@ -88,11 +88,11 @@ rule generate_aoh:
8888
unpack(aoh_species_inputs),
8989
output:
9090
# Only declare JSON as output - TIF is optional (not created for empty AOHs)
91-
metadata=DATADIR / "aohs" / SCENARIO / "{taxa}" / "{species_id}_all.json",
91+
metadata=DATADIR / "aohs" / SCENARIO / "{taxa}" / "aoh_{species_id}.json",
9292
params:
9393
habitat_dir=DATADIR / "habitat_layers" / SCENARIO,
9494
log:
95-
DATADIR / "logs" / "aoh" / "{taxa}" / "{species_id}_all.log",
95+
DATADIR / "logs" / "aoh" / "{taxa}" / "{species_id}.log",
9696
resources:
9797
# Limit concurrent AOH jobs if needed (e.g., for memory)
9898
aoh_slots=1,
@@ -126,7 +126,7 @@ def get_species_ids_for_taxa(wildcards):
126126
taxa=wildcards.taxa
127127
).output[0]
128128
geojson_dir = Path(checkpoint_output).parent
129-
return [p.stem for p in geojson_dir.glob("*.geojson")]
129+
return [p.stem[6:] for p in geojson_dir.glob("range_*.geojson")]
130130

131131

132132
def get_all_aoh_metadata_for_taxa(wildcards):
@@ -135,7 +135,7 @@ def get_all_aoh_metadata_for_taxa(wildcards):
135135
"""
136136
species_ids = get_species_ids_for_taxa(wildcards)
137137
return [
138-
DATADIR / "aohs" / SCENARIO / wildcards.taxa / f"{sid}_all.json"
138+
DATADIR / "aohs" / SCENARIO / wildcards.taxa / f"aoh_{sid}.json"
139139
for sid in species_ids
140140
]
141141

workflow/rules/threats.smk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def get_star_species_for_taxa(wildcards):
3939

4040
star_species = []
4141
for geojson_path in geojson_dir.glob("*.geojson"):
42-
species_id = geojson_path.stem
42+
species_id = geojson_path.stem[6:]
4343
aoh_path = (
44-
DATADIR / "aohs" / SCENARIO / wildcards.taxa / f"{species_id}_all.tif"
44+
DATADIR / "aohs" / SCENARIO / wildcards.taxa / f"aoh_{species_id}.tif"
4545
)
4646

4747
# Check if species should be in STAR and has an AOH
@@ -88,8 +88,8 @@ rule generate_threat_rasters:
8888
/ "species-info"
8989
/ "{taxa}"
9090
/ SCENARIO
91-
/ "{species_id}.geojson",
92-
aoh=DATADIR / "aohs" / SCENARIO / "{taxa}" / "{species_id}_all.tif",
91+
/ "range_{species_id}.geojson",
92+
aoh=DATADIR / "aohs" / SCENARIO / "{taxa}" / "aoh_{species_id}.tif",
9393
output:
9494
# Sentinel file since actual outputs depend on species' threats
9595
sentinel=DATADIR / "threat_rasters" / "{taxa}" / ".{species_id}_complete",

workflow/rules/validation.smk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ rule validate_gbif_occurrences:
9696
gbif_data=lambda wildcards: DATADIR
9797
/ "validation"
9898
/ "occurrences"
99-
/ wildcards.taxa,
99+
/ wildcards.taxa
100+
/ "points.csv",
100101
species_data=lambda wildcards: DATADIR
101102
/ "species-info"
102103
/ wildcards.taxa
@@ -106,7 +107,7 @@ rule validate_gbif_occurrences:
106107
DATADIR / "logs" / "validate_gbif_{taxa}.log",
107108
shell:
108109
"""
109-
aoh-validate-occurrences \
110+
aoh-validate-occurences \
110111
--gbif_data_path {params.gbif_data} \
111112
--species_data {params.species_data} \
112113
--aoh_results {params.aoh_results} \

0 commit comments

Comments
 (0)