@@ -2313,97 +2313,162 @@ def gradient_boosting_regressor_train_cli(
2313
2313
typer .echo ("Gradient boosting regressor training completed" )
2314
2314
2315
2315
2316
- # EVALUATE ML MODEL
2316
+ # TEST CLASSIFIER ML MODEL
2317
2317
@app .command ()
2318
- def evaluate_trained_model_cli (
2318
+ def classifier_test_cli (
2319
2319
input_rasters : INPUT_FILES_ARGUMENT ,
2320
2320
target_labels : INPUT_FILE_OPTION ,
2321
2321
model_file : INPUT_FILE_OPTION ,
2322
- output_raster : OUTPUT_FILE_OPTION ,
2323
- validation_metrics : Annotated [List [str ], typer .Option ()],
2322
+ output_raster_probability : OUTPUT_FILE_OPTION ,
2323
+ output_raster_classified : OUTPUT_FILE_OPTION ,
2324
+ classification_threshold : float = 0.5 ,
2325
+ test_metrics : Annotated [List [ClassifierMetrics ], typer .Option (case_sensitive = False )] = [ClassifierMetrics .accuracy ],
2324
2326
):
2325
- """Predict and evaluate a trained machine learning model by predicting and scoring."""
2326
- from sklearn .base import is_classifier
2327
-
2327
+ """Test trained machine learning classifier model by predicting and scoring."""
2328
2328
from eis_toolkit .evaluation .scoring import score_predictions
2329
2329
from eis_toolkit .prediction .machine_learning_general import load_model , prepare_data_for_ml , reshape_predictions
2330
- from eis_toolkit .prediction .machine_learning_predict import predict_classifier , predict_regressor
2330
+ from eis_toolkit .prediction .machine_learning_predict import predict_classifier
2331
2331
2332
2332
X , y , reference_profile , nodata_mask = prepare_data_for_ml (input_rasters , target_labels )
2333
2333
typer .echo ("Progress: 30%" )
2334
2334
2335
2335
model = load_model (model_file )
2336
- if is_classifier (model ):
2337
- predictions , probabilities = predict_classifier (X , model , True )
2338
- probabilities = probabilities [:, 1 ]
2339
- probabilities = probabilities .astype (np .float32 )
2340
- probabilities_reshaped = reshape_predictions (
2341
- probabilities , reference_profile ["height" ], reference_profile ["width" ], nodata_mask
2336
+ predictions , probabilities = predict_classifier (X , model , classification_threshold , True )
2337
+ probabilities_reshaped = reshape_predictions (
2338
+ probabilities , reference_profile ["height" ], reference_profile ["width" ], nodata_mask
2339
+ )
2340
+ predictions_reshaped = reshape_predictions (
2341
+ predictions , reference_profile ["height" ], reference_profile ["width" ], nodata_mask
2342
+ )
2343
+
2344
+ metrics_dict = score_predictions (y , predictions , get_enum_values (test_metrics ))
2345
+ typer .echo ("Progress: 80%" )
2346
+
2347
+ out_profile = reference_profile .copy ()
2348
+ out_profile .update ({"count" : 1 , "dtype" : np .float32 })
2349
+
2350
+ with rasterio .open (output_raster_probability , "w" , ** out_profile ) as dst :
2351
+ dst .write (probabilities_reshaped , 1 )
2352
+ with rasterio .open (output_raster_classified , "w" , ** out_profile ) as dst :
2353
+ dst .write (predictions_reshaped , 1 )
2354
+
2355
+ typer .echo ("\n " )
2356
+ for key , value in metrics_dict .items ():
2357
+ typer .echo (f"{ key } : { value } " )
2358
+ typer .echo ("\n " )
2359
+
2360
+ typer .echo ("Progress: 100%" )
2361
+ typer .echo (
2362
+ (
2363
+ "Testing classifier model completed, writing rasters to "
2364
+ f"{ output_raster_probability } and { output_raster_classified } ."
2342
2365
)
2343
- else :
2344
- predictions = predict_regressor (X , model )
2366
+ )
2345
2367
2346
- metrics_dict = score_predictions (y , predictions , validation_metrics )
2347
2368
2369
+ # TEST REGRESSOR ML MODEL
2370
+ @app .command ()
2371
+ def regressor_test_cli (
2372
+ input_rasters : INPUT_FILES_ARGUMENT ,
2373
+ target_labels : INPUT_FILE_OPTION ,
2374
+ model_file : INPUT_FILE_OPTION ,
2375
+ output_raster : OUTPUT_FILE_OPTION ,
2376
+ test_metrics : Annotated [List [RegressorMetrics ], typer .Option (case_sensitive = False )] = [RegressorMetrics .mse ],
2377
+ ):
2378
+ """Test trained machine learning regressor model by predicting and scoring."""
2379
+ from eis_toolkit .evaluation .scoring import score_predictions
2380
+ from eis_toolkit .prediction .machine_learning_general import load_model , prepare_data_for_ml , reshape_predictions
2381
+ from eis_toolkit .prediction .machine_learning_predict import predict_regressor
2382
+
2383
+ X , y , reference_profile , nodata_mask = prepare_data_for_ml (input_rasters , target_labels )
2384
+ typer .echo ("Progress: 30%" )
2385
+
2386
+ model = load_model (model_file )
2387
+ predictions = predict_regressor (X , model )
2348
2388
predictions_reshaped = reshape_predictions (
2349
2389
predictions , reference_profile ["height" ], reference_profile ["width" ], nodata_mask
2350
2390
)
2351
2391
2392
+ metrics_dict = score_predictions (y , predictions , get_enum_values (test_metrics ))
2352
2393
typer .echo ("Progress: 80%" )
2353
2394
2354
- json_str = json .dumps (metrics_dict )
2355
-
2356
2395
out_profile = reference_profile .copy ()
2357
2396
out_profile .update ({"count" : 1 , "dtype" : np .float32 })
2358
2397
2359
- if is_classifier (model ):
2360
- directory = os .path .split (output_raster )[0 ]
2361
- name = os .path .splitext (os .path .basename (output_raster ))[0 ]
2362
- labels_output = os .path .join (directory , name + "_labels" + ".tif" )
2363
- probabilities_output = os .path .join (directory , name + "_probabilities" + ".tif" )
2364
- for output_path , output_data in zip (
2365
- [labels_output , probabilities_output ], [predictions_reshaped , probabilities_reshaped ]
2366
- ):
2367
- with rasterio .open (output_path , "w" , ** out_profile ) as dst :
2368
- dst .write (output_data , 1 )
2369
- else :
2370
- with rasterio .open (output_raster , "w" , ** out_profile ) as dst :
2371
- dst .write (predictions_reshaped , 1 )
2398
+ with rasterio .open (output_raster , "w" , ** out_profile ) as dst :
2399
+ dst .write (predictions_reshaped , 1 )
2372
2400
2373
- typer .echo ("Progress: 100%" )
2374
- typer .echo (f"Results: { json_str } " )
2401
+ typer .echo ("\n " )
2402
+ for key , value in metrics_dict .items ():
2403
+ typer .echo (f"{ key } : { value } " )
2404
+ typer .echo ("\n " )
2375
2405
2376
- typer .echo ("Evaluating trained model completed" )
2406
+ typer .echo ("Progress: 100%\n " )
2407
+
2408
+ typer .echo (f"Testing regressor model completed, writing raster to { output_raster } ." )
2377
2409
2378
2410
2379
2411
# PREDICT WITH TRAINED ML MODEL
2380
2412
@app .command ()
2381
- def predict_with_trained_model_cli (
2413
+ def classifier_predict_cli (
2382
2414
input_rasters : INPUT_FILES_ARGUMENT ,
2383
2415
model_file : INPUT_FILE_OPTION ,
2384
- output_raster : OUTPUT_FILE_OPTION ,
2416
+ output_raster_probability : OUTPUT_FILE_OPTION ,
2417
+ output_raster_classified : OUTPUT_FILE_OPTION ,
2418
+ classification_threshold : float = 0.5 ,
2385
2419
):
2386
- """Predict with a trained machine learning model."""
2387
- from sklearn .base import is_classifier
2388
-
2420
+ """Predict with a trained machine learning classifier model."""
2389
2421
from eis_toolkit .prediction .machine_learning_general import load_model , prepare_data_for_ml , reshape_predictions
2390
- from eis_toolkit .prediction .machine_learning_predict import predict_classifier , predict_regressor
2422
+ from eis_toolkit .prediction .machine_learning_predict import predict_classifier
2391
2423
2392
2424
X , _ , reference_profile , nodata_mask = prepare_data_for_ml (input_rasters )
2393
2425
2394
2426
typer .echo ("Progress: 30%" )
2395
2427
2396
2428
model = load_model (model_file )
2397
- if is_classifier (model ):
2398
- predictions , probabilities = predict_classifier (X , model , True )
2399
- probabilities = probabilities [:, 1 ]
2400
- probabilities = probabilities .astype (np .float32 )
2401
- probabilities_reshaped = reshape_predictions (
2402
- probabilities , reference_profile ["height" ], reference_profile ["width" ], nodata_mask
2429
+ predictions , probabilities = predict_classifier (X , model , classification_threshold , True )
2430
+ probabilities_reshaped = reshape_predictions (
2431
+ probabilities , reference_profile ["height" ], reference_profile ["width" ], nodata_mask
2432
+ )
2433
+ predictions_reshaped = reshape_predictions (
2434
+ predictions , reference_profile ["height" ], reference_profile ["width" ], nodata_mask
2435
+ )
2436
+ typer .echo ("Progress: 80%" )
2437
+
2438
+ out_profile = reference_profile .copy ()
2439
+ out_profile .update ({"count" : 1 , "dtype" : np .float32 })
2440
+
2441
+ with rasterio .open (output_raster_probability , "w" , ** out_profile ) as dst :
2442
+ dst .write (probabilities_reshaped , 1 )
2443
+ with rasterio .open (output_raster_classified , "w" , ** out_profile ) as dst :
2444
+ dst .write (predictions_reshaped , 1 )
2445
+
2446
+ typer .echo ("Progress: 100%" )
2447
+ typer .echo (
2448
+ (
2449
+ "Predicting with classifier model completed, writing rasters to "
2450
+ f"{ output_raster_probability } and { output_raster_classified } ."
2403
2451
)
2404
- else :
2405
- predictions = predict_regressor ( X , model )
2452
+ )
2453
+
2406
2454
2455
+ # PREDICT WITH TRAINED ML MODEL
2456
+ @app .command ()
2457
+ def regressor_predict_cli (
2458
+ input_rasters : INPUT_FILES_ARGUMENT ,
2459
+ model_file : INPUT_FILE_OPTION ,
2460
+ output_raster : OUTPUT_FILE_OPTION ,
2461
+ ):
2462
+ """Predict with a trained machine learning regressor model."""
2463
+ from eis_toolkit .prediction .machine_learning_general import load_model , prepare_data_for_ml , reshape_predictions
2464
+ from eis_toolkit .prediction .machine_learning_predict import predict_regressor
2465
+
2466
+ X , _ , reference_profile , nodata_mask = prepare_data_for_ml (input_rasters )
2467
+
2468
+ typer .echo ("Progress: 30%" )
2469
+
2470
+ model = load_model (model_file )
2471
+ predictions = predict_regressor (X , model )
2407
2472
predictions_reshaped = reshape_predictions (
2408
2473
predictions , reference_profile ["height" ], reference_profile ["width" ], nodata_mask
2409
2474
)
@@ -2413,22 +2478,11 @@ def predict_with_trained_model_cli(
2413
2478
out_profile = reference_profile .copy ()
2414
2479
out_profile .update ({"count" : 1 , "dtype" : np .float32 })
2415
2480
2416
- if is_classifier (model ):
2417
- directory = os .path .split (output_raster )[0 ]
2418
- name = os .path .splitext (os .path .basename (output_raster ))[0 ]
2419
- labels_output = os .path .join (directory , name + "_labels" + ".tif" )
2420
- probabilities_output = os .path .join (directory , name + "_probabilities" + ".tif" )
2421
- for output_path , output_data in zip (
2422
- [labels_output , probabilities_output ], [predictions_reshaped , probabilities_reshaped ]
2423
- ):
2424
- with rasterio .open (output_path , "w" , ** out_profile ) as dst :
2425
- dst .write (output_data , 1 )
2426
- else :
2427
- with rasterio .open (output_raster , "w" , ** out_profile ) as dst :
2428
- dst .write (predictions_reshaped , 1 )
2481
+ with rasterio .open (output_raster , "w" , ** out_profile ) as dst :
2482
+ dst .write (predictions_reshaped , 1 )
2429
2483
2430
2484
typer .echo ("Progress: 100%" )
2431
- typer .echo ("Predicting completed" )
2485
+ typer .echo (f "Predicting with regressor model completed, writing raster to { output_raster } . " )
2432
2486
2433
2487
2434
2488
# FUZZY OVERLAYS
0 commit comments