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