openforis-whisp 1.0.0a1__py3-none-any.whl → 2.0.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
openforis_whisp/stats.py CHANGED
@@ -5,7 +5,6 @@ from .datasets import combine_datasets
5
5
  import json
6
6
  import country_converter as coco
7
7
  from openforis_whisp.parameters.config_runtime import (
8
- percent_or_ha,
9
8
  plot_id_column,
10
9
  geo_id_column,
11
10
  geometry_type_column,
@@ -35,39 +34,49 @@ from .reformat import validate_dataframe_using_lookups
35
34
 
36
35
  def whisp_formatted_stats_geojson_to_df(
37
36
  input_geojson_filepath: Path | str,
38
- external_id_column=None, # This variable is expected to be a string or None
37
+ external_id_column=None,
39
38
  remove_geom=False,
39
+ national_codes=None,
40
+ unit_type="ha",
40
41
  ) -> pd.DataFrame:
41
42
  """
42
- Main function for most users.
43
- Converts a GeoJSON file to a pandas DataFrame containing Whisp stats for the input ROI.
44
- Output df is validated against a panderas schema (created on the fly from the two lookup CSVs).
45
-
46
- This function first converts the provided GeoJSON file into an Earth Engine FeatureCollection.
47
- It then processes the FeatureCollection to extract relevant Whisp statistics,
48
- returning a structured DataFrame that aligns with the expected schema.
49
-
50
- If `external_id_column` is provided, it will be used to link external identifiers
51
- from the input GeoJSON to the output DataFrame.
52
-
53
- Parameters
54
- ----------
55
- input_geojson_filepath : Path | str
56
- The filepath to the GeoJSON of the ROI to analyze.
57
- external_id_column : str, optional
58
- The column in the GeoJSON containing external IDs to be preserved in the output DataFrame in the external_id column.
59
- remove_geom : bool, default=True
60
- If True, the geometry of the GeoJSON is removed from the output DataFrame.
43
+ Main function for most users.
44
+ Converts a GeoJSON file to a pandas DataFrame containing Whisp stats for the input ROI.
45
+ Output df is validated against a panderas schema (created on the fly from the two lookup CSVs).
46
+
47
+ This function first converts the provided GeoJSON file into an Earth Engine FeatureCollection.
48
+ It then processes the FeatureCollection to extract relevant Whisp statistics,
49
+ returning a structured DataFrame that aligns with the expected schema.
50
+
51
+ If `external_id_column` is provided, it will be used to link external identifiers
52
+ from the input GeoJSON to the output DataFrame.
53
+
54
+ Parameters
55
+ ----------
56
+ input_geojson_filepath : Path | str
57
+ The filepath to the GeoJSON of the ROI to analyze.
58
+ external_id_column : str, optional
59
+ The column in the GeoJSON containing external IDs to be preserved in the output DataFrame.
60
+ remove_geom : bool, default=False
61
+ If True, the geometry of the GeoJSON is removed from the output DataFrame.
62
+ national_codes : list, optional
63
+ List of ISO2 country codes to include national datasets.
64
+ unit_type: str, optional
65
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
61
66
 
62
67
  Returns
63
- -------
64
- df_stats : pd.DataFrame
65
- The DataFrame containing the Whisp stats for the input ROI.
68
+ -------
69
+ df_stats : pd.DataFrame
70
+ The DataFrame containing the Whisp stats for the input ROI.
66
71
  """
67
72
  feature_collection = convert_geojson_to_ee(str(input_geojson_filepath))
68
73
 
69
74
  return whisp_formatted_stats_ee_to_df(
70
- feature_collection, external_id_column, remove_geom
75
+ feature_collection,
76
+ external_id_column,
77
+ remove_geom,
78
+ national_codes=national_codes,
79
+ unit_type=unit_type, # Fixed: now it's a keyword argument
71
80
  )
72
81
 
73
82
 
@@ -76,6 +85,8 @@ def whisp_formatted_stats_geojson_to_geojson(
76
85
  output_geojson_filepath,
77
86
  external_id_column=None,
78
87
  geo_column: str = "geo",
88
+ national_codes=None,
89
+ unit_type="ha",
79
90
  ):
80
91
  """
81
92
  Convert a formatted GeoJSON file with a geo column into a GeoJSON file containing Whisp stats.
@@ -90,6 +101,10 @@ def whisp_formatted_stats_geojson_to_geojson(
90
101
  The name of the column containing external IDs, by default None.
91
102
  geo_column : str, optional
92
103
  The name of the column containing GeoJSON geometries, by default "geo".
104
+ national_codes : list, optional
105
+ List of ISO2 country codes to include national datasets.
106
+ unit_type : str, optional
107
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
93
108
 
94
109
  Returns
95
110
  -------
@@ -98,6 +113,8 @@ def whisp_formatted_stats_geojson_to_geojson(
98
113
  df = whisp_formatted_stats_geojson_to_df(
99
114
  input_geojson_filepath=input_geojson_filepath,
100
115
  external_id_column=external_id_column,
116
+ national_codes=national_codes,
117
+ unit_type=unit_type,
101
118
  )
102
119
  # Convert the df to GeoJSON
103
120
  convert_df_to_geojson(df, output_geojson_filepath, geo_column)
@@ -108,8 +125,10 @@ def whisp_formatted_stats_geojson_to_geojson(
108
125
  def whisp_formatted_stats_ee_to_geojson(
109
126
  feature_collection: ee.FeatureCollection,
110
127
  output_geojson_filepath: str,
111
- external_id_column=None, # This variable is expected to be a string or None
128
+ external_id_column=None,
112
129
  geo_column: str = "geo",
130
+ national_codes=None,
131
+ unit_type="ha",
113
132
  ):
114
133
  """
115
134
  Convert an Earth Engine FeatureCollection to a GeoJSON file containing Whisp stats.
@@ -122,17 +141,23 @@ def whisp_formatted_stats_ee_to_geojson(
122
141
  The filepath to save the output GeoJSON file.
123
142
  external_id_column : str, optional
124
143
  The name of the column containing external IDs, by default None.
125
- remove_geom : bool, optional
126
- Whether to remove the geometry column, by default False.
127
144
  geo_column : str, optional
128
145
  The name of the column containing GeoJSON geometries, by default "geo".
129
-
146
+ national_codes : list, optional
147
+ List of ISO2 country codes to include national datasets.
148
+ unit_type : str, optional
149
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
130
150
  Returns
131
151
  -------
132
152
  None
133
153
  """
134
154
  # Convert ee feature collection to a pandas dataframe
135
- df_stats = whisp_formatted_stats_ee_to_df(feature_collection, external_id_column)
155
+ df_stats = whisp_formatted_stats_ee_to_df(
156
+ feature_collection,
157
+ external_id_column,
158
+ national_codes=national_codes,
159
+ unit_type=unit_type,
160
+ )
136
161
 
137
162
  # Convert the df to GeoJSON
138
163
  convert_df_to_geojson(df_stats, output_geojson_filepath, geo_column)
@@ -142,14 +167,26 @@ def whisp_formatted_stats_ee_to_geojson(
142
167
 
143
168
  def whisp_formatted_stats_ee_to_df(
144
169
  feature_collection: ee.FeatureCollection,
145
- external_id_column=None, # This variable is expected to be a string or None
170
+ external_id_column=None,
146
171
  remove_geom=False,
172
+ national_codes=None,
173
+ unit_type="ha",
147
174
  ) -> pd.DataFrame:
148
175
  """
176
+ Convert a feature collection to a validated DataFrame with Whisp statistics.
177
+
149
178
  Parameters
150
179
  ----------
151
180
  feature_collection : ee.FeatureCollection
152
181
  The feature collection of the ROI to analyze.
182
+ external_id_column : str, optional
183
+ The name of the external ID column, by default None.
184
+ remove_geom : bool, optional
185
+ Whether to remove the geometry column, by default False.
186
+ national_codes : list, optional
187
+ List of ISO2 country codes to include national datasets.
188
+ unit_type : str, optional
189
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
153
190
 
154
191
  Returns
155
192
  -------
@@ -157,9 +194,18 @@ def whisp_formatted_stats_ee_to_df(
157
194
  The validated dataframe containing the Whisp stats for the input ROI.
158
195
  """
159
196
  # Convert ee feature collection to a pandas dataframe
160
- df_stats = whisp_stats_ee_to_df(feature_collection, external_id_column, remove_geom)
197
+ df_stats = whisp_stats_ee_to_df(
198
+ feature_collection,
199
+ external_id_column,
200
+ remove_geom,
201
+ national_codes=national_codes,
202
+ unit_type=unit_type,
203
+ )
161
204
 
162
- validated_df = validate_dataframe_using_lookups(df_stats)
205
+ # Pass national_codes to validation function to filter schema
206
+ validated_df = validate_dataframe_using_lookups(
207
+ df_stats, national_codes=national_codes
208
+ )
163
209
  return validated_df
164
210
 
165
211
 
@@ -168,15 +214,26 @@ def whisp_formatted_stats_ee_to_df(
168
214
 
169
215
  def whisp_stats_geojson_to_df(
170
216
  input_geojson_filepath: Path | str,
171
- external_id_column=None, # This variable is expected to be a string or None
217
+ external_id_column=None,
172
218
  remove_geom=False,
219
+ national_codes=None,
220
+ unit_type="ha",
173
221
  ) -> pd.DataFrame:
174
222
  """
223
+ Convert a GeoJSON file to a pandas DataFrame with Whisp statistics.
175
224
 
176
225
  Parameters
177
226
  ----------
178
227
  input_geojson_filepath : Path | str
179
228
  The filepath to the GeoJSON of the ROI to analyze.
229
+ external_id_column : str, optional
230
+ The name of the external ID column, by default None.
231
+ remove_geom : bool, optional
232
+ Whether to remove the geometry column, by default False.
233
+ national_codes : list, optional
234
+ List of ISO2 country codes to include national datasets.
235
+ unit_type : str, optional
236
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
180
237
 
181
238
  Returns
182
239
  -------
@@ -185,32 +242,50 @@ def whisp_stats_geojson_to_df(
185
242
  """
186
243
  feature_collection = convert_geojson_to_ee(str(input_geojson_filepath))
187
244
 
188
- return whisp_stats_ee_to_df(feature_collection, external_id_column, remove_geom)
245
+ return whisp_stats_ee_to_df(
246
+ feature_collection,
247
+ external_id_column,
248
+ remove_geom,
249
+ national_codes=national_codes,
250
+ unit_type=unit_type,
251
+ )
189
252
 
190
253
 
191
254
  def whisp_stats_geojson_to_ee(
192
255
  input_geojson_filepath: Path | str,
193
- external_id_column=None, # This variable is expected to be a string or None
256
+ external_id_column=None,
257
+ national_codes=None,
194
258
  ) -> ee.FeatureCollection:
195
259
  """
260
+ Convert a GeoJSON file to an Earth Engine FeatureCollection with Whisp statistics.
196
261
 
197
262
  Parameters
198
263
  ----------
199
264
  input_geojson_filepath : Path | str
200
265
  The filepath to the GeoJSON of the ROI to analyze.
266
+ external_id_column : str, optional
267
+ The name of the external ID column, by default None.
268
+ national_codes : list, optional
269
+ List of ISO2 country codes to include national datasets.
201
270
 
202
271
  Returns
203
272
  -------
204
- df_stats : pd.DataFrame
205
- The dataframe containing the Whisp stats for the input ROI.
273
+ ee.FeatureCollection
274
+ The feature collection containing the Whisp stats for the input ROI.
206
275
  """
207
276
  feature_collection = convert_geojson_to_ee(str(input_geojson_filepath))
208
277
 
209
- return whisp_stats_ee_to_ee(feature_collection, external_id_column)
278
+ return whisp_stats_ee_to_ee(
279
+ feature_collection, external_id_column, national_codes=national_codes
280
+ )
210
281
 
211
282
 
212
283
  def whisp_stats_geojson_to_geojson(
213
- input_geojson_filepath, output_geojson_filepath, external_id_column=None
284
+ input_geojson_filepath,
285
+ output_geojson_filepath,
286
+ external_id_column=None,
287
+ national_codes=None,
288
+ unit_type="ha",
214
289
  ):
215
290
  """
216
291
  Convert a GeoJSON file to a GeoJSON object containing Whisp stats for the input ROI.
@@ -222,7 +297,11 @@ def whisp_stats_geojson_to_geojson(
222
297
  output_geojson_filepath : str
223
298
  The filepath to save the output GeoJSON file.
224
299
  external_id_column : str, optional
225
- The name of the external ID column, by default None.
300
+ The name of the column containing external IDs, by default None.
301
+ national_codes : list, optional
302
+ List of ISO2 country codes to include national datasets.
303
+ unit_type : str, optional
304
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
226
305
 
227
306
  Returns
228
307
  -------
@@ -233,7 +312,10 @@ def whisp_stats_geojson_to_geojson(
233
312
 
234
313
  # Get stats as a FeatureCollection
235
314
  stats_feature_collection = whisp_stats_ee_to_ee(
236
- feature_collection, external_id_column
315
+ feature_collection,
316
+ external_id_column,
317
+ national_codes=national_codes,
318
+ unit_type=unit_type,
237
319
  )
238
320
 
239
321
  # Convert the stats FeatureCollection to GeoJSON
@@ -246,45 +328,61 @@ def whisp_stats_geojson_to_geojson(
246
328
 
247
329
  def whisp_stats_geojson_to_drive(
248
330
  input_geojson_filepath: Path | str,
249
- external_id_column=None, # This variable is expected to be a string or None
331
+ external_id_column=None,
332
+ national_codes=None,
333
+ unit_type="ha",
250
334
  ):
251
335
  """
336
+ Export Whisp statistics for a GeoJSON file to Google Drive.
337
+
252
338
  Parameters
253
339
  ----------
254
- geojson_filepath : Path | str
340
+ input_geojson_filepath : Path | str
255
341
  The filepath to the GeoJSON of the ROI to analyze.
342
+ external_id_column : str, optional
343
+ The name of the external ID column, by default None.
344
+ national_codes : list, optional
345
+ List of ISO2 country codes to include national datasets.
346
+ unit_type : str, optional
347
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
256
348
 
257
349
  Returns
258
350
  -------
259
351
  Message showing location of file in Google Drive
260
352
  """
261
-
262
353
  try:
263
354
  input_geojson_filepath = Path(input_geojson_filepath)
264
355
  if not input_geojson_filepath.exists():
265
356
  raise FileNotFoundError(f"File {input_geojson_filepath} does not exist.")
266
357
 
267
- # Assuming geojson_to_ee is properly imported from data_conversion.py
268
358
  feature_collection = convert_geojson_to_ee(str(input_geojson_filepath))
269
359
 
270
- return whisp_stats_ee_to_drive(feature_collection, external_id_column)
360
+ return whisp_stats_ee_to_drive(
361
+ feature_collection,
362
+ external_id_column,
363
+ national_codes=national_codes,
364
+ unit_type=unit_type,
365
+ )
271
366
 
272
367
  except Exception as e:
273
368
  print(f"An error occurred: {e}")
274
369
 
275
370
 
276
- def whisp_stats_ee_to_ee(feature_collection, external_id_column):
371
+ def whisp_stats_ee_to_ee(
372
+ feature_collection, external_id_column, national_codes=None, unit_type="ha"
373
+ ):
277
374
  """
278
375
  Process a feature collection to get statistics for each feature.
279
376
 
280
377
  Parameters:
281
378
  feature_collection (ee.FeatureCollection): The input feature collection.
282
379
  external_id_column (str): The name of the external ID column to check.
380
+ national_codes (list, optional): List of ISO2 country codes to include national datasets.
381
+ unit_type (str): Whether to use hectares ("ha") or percentage ("percent"), default "ha".
283
382
 
284
383
  Returns:
285
384
  ee.FeatureCollection: The output feature collection with statistics.
286
385
  """
287
-
288
386
  if external_id_column is not None:
289
387
  try:
290
388
  # Check if external_id_column is a property in feature_collection (server-side)
@@ -322,15 +420,19 @@ def whisp_stats_ee_to_ee(feature_collection, external_id_column):
322
420
  f"An error occurred when trying to set the external_id_column: {external_id_column}. Error: {e}"
323
421
  )
324
422
 
325
- fc = get_stats(feature_collection)
423
+ fc = get_stats(
424
+ feature_collection, national_codes=national_codes, unit_type=unit_type
425
+ )
326
426
 
327
427
  return add_id_to_feature_collection(dataset=fc, id_name=plot_id_column)
328
428
 
329
429
 
330
430
  def whisp_stats_ee_to_df(
331
431
  feature_collection: ee.FeatureCollection,
332
- external_id_column=None, # This variable is expected to be a string or None
432
+ external_id_column=None,
333
433
  remove_geom=False,
434
+ national_codes=None,
435
+ unit_type="ha",
334
436
  ) -> pd.DataFrame:
335
437
  """
336
438
  Convert a Google Earth Engine FeatureCollection to a pandas DataFrame and convert ISO3 to ISO2 country codes.
@@ -343,6 +445,10 @@ def whisp_stats_ee_to_df(
343
445
  The name of the external ID column, by default None.
344
446
  remove_geom : bool, optional
345
447
  Whether to remove the geometry column, by default True.
448
+ national_codes : list, optional
449
+ List of ISO2 country codes to include national datasets.
450
+ unit_type : str, optional
451
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
346
452
 
347
453
  Returns
348
454
  -------
@@ -351,7 +457,12 @@ def whisp_stats_ee_to_df(
351
457
  """
352
458
  try:
353
459
  df_stats = convert_ee_to_df(
354
- ee_object=whisp_stats_ee_to_ee(feature_collection, external_id_column),
460
+ ee_object=whisp_stats_ee_to_ee(
461
+ feature_collection,
462
+ external_id_column,
463
+ national_codes=national_codes,
464
+ unit_type=unit_type,
465
+ ),
355
466
  remove_geom=remove_geom,
356
467
  )
357
468
  except Exception as e:
@@ -372,12 +483,36 @@ def whisp_stats_ee_to_df(
372
483
 
373
484
 
374
485
  def whisp_stats_ee_to_drive(
375
- feature_collection: ee.FeatureCollection, external_id_column=None
486
+ feature_collection: ee.FeatureCollection,
487
+ external_id_column=None,
488
+ national_codes=None,
489
+ unit_type="ha",
376
490
  ):
377
-
491
+ """
492
+ Export Whisp statistics for a feature collection to Google Drive.
493
+
494
+ Parameters
495
+ ----------
496
+ feature_collection : ee.FeatureCollection
497
+ The feature collection to analyze.
498
+ external_id_column : str, optional
499
+ The name of the external ID column, by default None.
500
+ national_codes : list, optional
501
+ List of ISO2 country codes to include national datasets.
502
+ unit_type : str, optional
503
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
504
+ Returns
505
+ -------
506
+ None
507
+ """
378
508
  try:
379
509
  task = ee.batch.Export.table.toDrive(
380
- collection=whisp_stats_ee_to_ee(feature_collection, external_id_column),
510
+ collection=whisp_stats_ee_to_ee(
511
+ feature_collection,
512
+ external_id_column,
513
+ national_codes=national_codes,
514
+ unit_type=unit_type,
515
+ ),
381
516
  description="whisp_output_table",
382
517
  # folder="whisp_results",
383
518
  fileFormat="CSV",
@@ -392,28 +527,78 @@ def whisp_stats_ee_to_drive(
392
527
 
393
528
  #### main stats functions
394
529
 
530
+
395
531
  # Get stats for a feature or feature collection
396
- def get_stats(feature_or_feature_col):
532
+ def get_stats(feature_or_feature_col, national_codes=None, unit_type="ha"):
533
+ """
534
+ Get stats for a feature or feature collection with optional filtering by national codes.
535
+
536
+ Parameters
537
+ ----------
538
+ feature_or_feature_col : ee.Feature or ee.FeatureCollection
539
+ The input feature or feature collection to analyze
540
+ national_codes : list, optional
541
+ List of ISO2 country codes to include national datasets
542
+ unit_type : str, optional
543
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
544
+ Returns
545
+ -------
546
+ ee.FeatureCollection
547
+ Feature collection with calculated statistics
548
+ """
397
549
  # Check if the input is a Feature or a FeatureCollection
398
550
  if isinstance(feature_or_feature_col, ee.Feature):
399
551
  # If the input is a Feature, call the server-side function for processing
400
552
  print("feature")
401
- output = ee.FeatureCollection([get_stats_feature(feature_or_feature_col)])
553
+ # For a single feature, we need to combine datasets with the national_codes filter
554
+ img_combined = combine_datasets(national_codes=national_codes)
555
+ output = ee.FeatureCollection(
556
+ [
557
+ get_stats_feature(
558
+ feature_or_feature_col, img_combined, unit_type=unit_type
559
+ )
560
+ ]
561
+ )
402
562
  elif isinstance(feature_or_feature_col, ee.FeatureCollection):
403
563
  # If the input is a FeatureCollection, call the server-side function for processing
404
- output = get_stats_fc(feature_or_feature_col)
564
+ output = get_stats_fc(
565
+ feature_or_feature_col, national_codes=national_codes, unit_type=unit_type
566
+ )
405
567
  else:
406
568
  output = "Check inputs: not an ee.Feature or ee.FeatureCollection"
407
569
  return output
408
570
 
409
571
 
410
572
  # Get statistics for a feature collection
411
- def get_stats_fc(feature_col):
412
-
413
- img_combined = combine_datasets() # imported function
573
+ def get_stats_fc(feature_col, national_codes=None, unit_type="ha"):
574
+ """
575
+ Calculate statistics for a feature collection using Whisp datasets.
576
+
577
+ Parameters
578
+ ----------
579
+ feature_col : ee.FeatureCollection
580
+ The input feature collection to analyze
581
+ national_codes : list, optional
582
+ List of ISO2 country codes (e.g., ["BR", "US"]) to include national datasets.
583
+ If provided, only national datasets for these countries and global datasets will be used.
584
+ If None (default), only global datasets will be used.
585
+ unit_type : str, optional
586
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
587
+ Returns
588
+ -------
589
+ ee.FeatureCollection
590
+ Feature collection with calculated statistics
591
+ """
592
+ img_combined = combine_datasets(
593
+ national_codes=national_codes
594
+ ) # Pass national_codes to combine_datasets
414
595
 
415
596
  out_feature_col = ee.FeatureCollection(
416
- feature_col.map(lambda feature: get_stats_feature(feature, img_combined))
597
+ feature_col.map(
598
+ lambda feature: get_stats_feature(
599
+ feature, img_combined, unit_type=unit_type
600
+ )
601
+ )
417
602
  )
418
603
  # print(out_feature_col.first().getInfo()) # for testing
419
604
 
@@ -421,10 +606,26 @@ def get_stats_fc(feature_col):
421
606
 
422
607
 
423
608
  # Get statistics for a single feature
424
- def get_stats_feature(feature, img_combined):
425
609
 
426
- # img_combined = combine_datasets()
427
610
 
611
+ def get_stats_feature(feature, img_combined, unit_type="ha"):
612
+ """
613
+ Get statistics for a single feature using a pre-combined image.
614
+
615
+ Parameters
616
+ ----------
617
+ feature : ee.Feature
618
+ The feature to analyze
619
+ img_combined : ee.Image
620
+ Pre-combined image with all the datasets
621
+ unit_type : str, optional
622
+ Whether to use hectares ("ha") or percentage ("percent"), by default "ha".
623
+
624
+ Returns
625
+ -------
626
+ ee.Feature
627
+ Feature with calculated statistics
628
+ """
428
629
  reduce = img_combined.reduceRegion(
429
630
  reducer=ee.Reducer.sum(),
430
631
  geometry=feature.geometry(),
@@ -437,7 +638,7 @@ def get_stats_feature(feature, img_combined):
437
638
  feature_info = get_type_and_location(feature)
438
639
 
439
640
  # add statistics unit type (e.g., percentage or hectares) to dictionary
440
- stats_unit_type = ee.Dictionary({stats_unit_type_column: percent_or_ha})
641
+ stats_unit_type = ee.Dictionary({stats_unit_type_column: unit_type})
441
642
 
442
643
  # Now, modified_dict contains all keys with the prefix added
443
644
  reduce_ha = reduce.map(
@@ -472,9 +673,9 @@ def get_stats_feature(feature, img_combined):
472
673
  ee.Dictionary(reducer_stats_percent)
473
674
  ).combine(stats_unit_type)
474
675
 
475
- # Choose whether to use hectares or percentage based on the `percent_or_ha` variable
676
+ # Choose whether to use hectares or percentage based on the parameter instead of global variable
476
677
  out_feature = ee.Algorithms.If(
477
- percent_or_ha == "ha",
678
+ unit_type == "ha",
478
679
  feature.set(properties_ha), # .setGeometry(None),
479
680
  feature.set(properties_percent), # .setGeometry(None),
480
681
  )
@@ -744,9 +945,9 @@ def convert_iso3_to_iso2(df, iso3_column, iso2_column):
744
945
 
745
946
  # Apply conversion from ISO3 to ISO2
746
947
  df[iso2_column] = df[iso3_column].apply(
747
- lambda x: coco.convert(names=x, to="ISO2")
748
- if x
749
- else "not found (disputed territory)"
948
+ lambda x: (
949
+ coco.convert(names=x, to="ISO2") if x else "not found (disputed territory)"
950
+ )
750
951
  )
751
952
 
752
953
  return df