Skip to content
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

deactivate irrigation processing #100

Merged
merged 8 commits into from
Jan 24, 2023
41 changes: 32 additions & 9 deletions src/ewoc_classif/classif.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import shutil
import traceback
import csv
from json import dump, load
from pathlib import Path
from tempfile import gettempdir
Expand Down Expand Up @@ -258,7 +259,8 @@ def run_classif(
upload_block: bool = True,
postprocess: bool = False,
out_dirpath: Path = Path(gettempdir()),
clean=True
clean:bool=True,
no_tir:bool=False
) -> None:
"""
Perform EWoC classification
Expand Down Expand Up @@ -297,6 +299,8 @@ def run_classif(
:type postprocess: bool
:param out_dirpath: Output directory path
:type out_dirpath: Path
:param no_tir: Boolean specifying if the csv file containing details on ARD TIR is empty or not
:type no_tir: bool
:return: None
"""
uid = uuid4().hex[:6]
Expand All @@ -320,6 +324,13 @@ def run_classif(
if tir_csv is None:
tir_csv = out_dirpath / f"{uid}_satio_tir.csv"
ewoc_ard_bucket.tir_to_satio_csv(tile_id, production_id, filepath=tir_csv)
else:
with open(Path(tir_csv), 'r', encoding='utf8') as tir_file:
tir_dict = [row for row in csv.DictReader(tir_file)]
if len(tir_dict) <= 1:
logger.warning(f"TIR ARD is empty for the tile {tile_id} => No irrigation computed!")
no_tir=True

if agera5_csv is None:
agera5_csv = out_dirpath / f"{uid}_satio_agera5.csv"
ewoc_aux_data_bucket = EWOCAuxDataBucket()
Expand All @@ -329,14 +340,24 @@ def run_classif(
if end_season_year == 2022:
logger.info('Add additional croptype')
add_croptype = True

if not no_tir:
csv_dict = {
"OPTICAL": str(optical_csv),
"SAR": str(sar_csv),
"TIR": str(tir_csv),
"DEM": "s3://ewoc-aux-data/CopDEM_20m",
"METEO": str(agera5_csv),
}
else:
csv_dict = {
"OPTICAL": str(optical_csv),
"SAR": str(sar_csv),
"DEM": "s3://ewoc-aux-data/CopDEM_20m",
"METEO": str(agera5_csv),
}


csv_dict = {
"OPTICAL": str(optical_csv),
"SAR": str(sar_csv),
"TIR": str(tir_csv),
"DEM": "s3://ewoc-aux-data/CopDEM_20m",
"METEO": str(agera5_csv),
}
ewoc_config = generate_config_file(
ewoc_detector,
end_season_year,
Expand All @@ -346,9 +367,11 @@ def run_classif(
croptype_model_version,
irr_model_version,
csv_dict,
feature_blocks_dir= feature_blocks_dir,
feature_blocks_dir= feature_blocks_dir,
no_tir_data=no_tir,
add_croptype = add_croptype
)

ewoc_config_filepath = out_dirpath / f"{uid}_ewoc_config.json"
if data_folder is not None:
ewoc_config = update_config(ewoc_config, ewoc_detector, data_folder)
Expand Down
40 changes: 26 additions & 14 deletions src/ewoc_classif/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def generate_config_file(
irr_model_version: str,
csv_dict: Dict,
feature_blocks_dir: Path,
no_tir_data: bool,
add_croptype:bool = False
) -> Dict:
"""
Expand All @@ -104,6 +105,8 @@ def generate_config_file(
:type csv_dict: Dict
:param feature_blocks_dir: Block features dir
:type feature_blocks_dir: Path
:param no_tir: Boolean specifying if the csv file containing details on ARD TIR is empty or not
:type no_tir: bool
:param add_croptype: Additional croptype
:type add_croptype: bool
:return: Dict
Expand Down Expand Up @@ -146,20 +149,29 @@ def generate_config_file(
parameters["filtersettings"] = {"kernelsize": 7, "conf_threshold": 0.75}
parameters["save_features"]= True
parameters["features_dir"]=str(feature_blocks_dir)
parameters.update(
{
"active_marker": True,
"cropland_mask": cropland_mask_bucket,
"irrigation": True,
"irrparameters": "irrigation",
"irrmodels": {
"irrigation": f"{ewoc_model_prefix}/models/WorldCerealPixelCatBoost/{irr_model_version}/irrigation_detector_WorldCerealPixelCatBoost_{irr_model_version}/config.json"
},
}
)
logger.info(
f"[{featuresettings}] - Using Irrigation model version: {irr_model_version}"
)
if not no_tir_data:
parameters.update(
{
"active_marker": True,
"cropland_mask": cropland_mask_bucket,
"irrigation": True,
"irrparameters": "irrigation",
"irrmodels": {
"irrigation": f"{ewoc_model_prefix}/models/WorldCerealPixelCatBoost/{irr_model_version}/irrigation_detector_WorldCerealPixelCatBoost_{irr_model_version}/config.json"
},
}
)
logger.info(
f"[{featuresettings}] - Using Irrigation model version: {irr_model_version}"
)
else:
parameters.update(
{
"active_marker": True,
"cropland_mask": cropland_mask_bucket,
"irrigation" : False
}
)
if ewoc_season == "summer1":
models = {
"maize": f"{ewoc_model_prefix}/models/WorldCerealPixelCatBoost/{croptype_model_version}/maize_detector_WorldCerealPixelCatBoost_{croptype_model_version}/config.json",
Expand Down
31 changes: 30 additions & 1 deletion tests/test_classif.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,33 @@ def test_run_classif_croptype_winter_36UWA_4_2022(self):
upload_block=True,
ewoc_detector=EWOC_CROPTYPE_DETECTOR,
ewoc_season=EWOC_SUPPORTED_SEASONS[0],
clean=False)
clean=False)


@unittest.skipIf(os.getenv("EWOC_TEST_VAL_TEST") is None,"env variable not set")
def test_run_classif_winter_40KEC_71(self):
""" Nominal case with no tir detected

Island case (Mauritius)
"""
run_classif('40KEC',
'c728b264-5c97-4f4c-81fe-1500d4c4dfbd_9026_20220926141535',
block_ids=[71],
upload_block=False,
clean=False,
tir_csv="./tests/tir_preprocessed_path.csv",
ewoc_detector=EWOC_CROPTYPE_DETECTOR,
ewoc_season=EWOC_SUPPORTED_SEASONS[0])

def test_run_classif_winter_40KEC_71_no_csv(self):
""" Nominal case with no tir detected

Island case (Mauritius)
"""
run_classif('40KEC',
'c728b264-5c97-4f4c-81fe-1500d4c4dfbd_9026_20220926141535',
block_ids=[71],
upload_block=False,
clean=False,
ewoc_detector=EWOC_CROPTYPE_DETECTOR,
ewoc_season=EWOC_SUPPORTED_SEASONS[0])
1 change: 1 addition & 0 deletions tests/tir_preprocessed_path.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,date,tile,level,path