6
6
import pandas as pd
7
7
import rasterio
8
8
from beartype import beartype
9
- from beartype .typing import Dict , List , Literal , Optional , Sequence , Tuple
10
-
11
- from eis_toolkit .exceptions import ClassificationFailedException , InvalidColumnException , InvalidParameterValueException
9
+ from beartype .typing import Dict , List , Literal , Optional , Sequence , Tuple , Union
10
+
11
+ from eis_toolkit .exceptions import (
12
+ ClassificationFailedException ,
13
+ InvalidColumnException ,
14
+ InvalidParameterValueException ,
15
+ NonMatchingRasterMetadataException ,
16
+ )
17
+ from eis_toolkit .utilities .checks .raster import check_raster_grids
12
18
from eis_toolkit .vector_processing .rasterize_vector import rasterize_vector
13
19
from eis_toolkit .warnings import ClassificationFailedWarning , InvalidColumnWarning
14
20
68
74
}
69
75
70
76
71
- def _read_and_preprocess_evidence (
77
+ def _read_and_preprocess_raster_data (
72
78
raster : rasterio .io .DatasetReader , nodata : Optional [Number ] = None , band : int = 1
73
79
) -> np .ndarray :
74
80
"""Read raster data and handle NoData values."""
@@ -244,6 +250,22 @@ def _generate_arrays_from_metrics(
244
250
return array_dict
245
251
246
252
253
+ def _calculate_nr_of_deposit_pixels (array : np .ndarray , df : pd .DataFrame ) -> Tuple [int , int ]:
254
+ masked_array = array [~ np .isnan (array )]
255
+ nr_of_pixels = int (np .size (masked_array ))
256
+
257
+ pixels_column = df ["Pixel count" ]
258
+
259
+ match = pixels_column == nr_of_pixels
260
+ if match .any ():
261
+ nr_of_deposits = df .loc [match , "Deposit count" ].iloc [0 ]
262
+ else :
263
+ nr_of_pixels = df ["Pixel count" ].sum ()
264
+ nr_of_deposits = df ["Deposit count" ].sum ()
265
+
266
+ return nr_of_deposits , nr_of_pixels
267
+
268
+
247
269
@beartype
248
270
def generalize_weights_cumulative (
249
271
df : pd .DataFrame ,
@@ -349,7 +371,7 @@ def generalize_weights_cumulative(
349
371
@beartype
350
372
def weights_of_evidence_calculate_weights (
351
373
evidential_raster : rasterio .io .DatasetReader ,
352
- deposits : gpd .GeoDataFrame ,
374
+ deposits : Union [ gpd .GeoDataFrame , rasterio . io . DatasetReader ] ,
353
375
raster_nodata : Optional [Number ] = None ,
354
376
weights_type : Literal ["unique" , "categorical" , "ascending" , "descending" ] = "unique" ,
355
377
studentized_contrast_threshold : Number = 1 ,
@@ -360,7 +382,7 @@ def weights_of_evidence_calculate_weights(
360
382
361
383
Args:
362
384
evidential_raster: The evidential raster.
363
- deposits: Vector data representing the mineral deposits or occurences point data.
385
+ deposits: Vector or raster data representing the mineral deposits or occurences point data.
364
386
raster_nodata: If nodata value of raster is wanted to specify manually. Optional parameter, defaults to None
365
387
(nodata from raster metadata is used).
366
388
weights_type: Accepted values are 'unique', 'categorical', 'ascending' and 'descending'.
@@ -409,13 +431,22 @@ def weights_of_evidence_calculate_weights(
409
431
metrics_to_arrays = arrays_to_generate .copy ()
410
432
411
433
# 1. Preprocess data
412
- evidence_array = _read_and_preprocess_evidence (evidential_raster , raster_nodata )
434
+ evidence_array = _read_and_preprocess_raster_data (evidential_raster , raster_nodata )
413
435
raster_meta = evidential_raster .meta
436
+ raster_profile = evidential_raster .profile
414
437
415
- # Rasterize deposits
416
- deposit_array = rasterize_vector (
417
- geodataframe = deposits , raster_profile = raster_meta , default_value = 1.0 , fill_value = 0.0
418
- )
438
+ # Rasterize deposits if vector data
439
+ if isinstance (deposits , gpd .GeoDataFrame ):
440
+ deposit_array = rasterize_vector (
441
+ geodataframe = deposits , raster_profile = raster_meta , default_value = 1.0 , fill_value = 0.0
442
+ )
443
+ else :
444
+ deposit_profile = deposits .profile
445
+
446
+ if check_raster_grids ([raster_profile , deposit_profile ], same_extent = True ):
447
+ deposit_array = _read_and_preprocess_raster_data (deposits , raster_nodata )
448
+ else :
449
+ raise NonMatchingRasterMetadataException ("Input rasters should have the same grid properties." )
419
450
420
451
# Mask NaN out of the array
421
452
nodata_mask = np .isnan (evidence_array )
@@ -482,7 +513,7 @@ def weights_of_evidence_calculate_weights(
482
513
483
514
@beartype
484
515
def weights_of_evidence_calculate_responses (
485
- output_arrays : Sequence [Dict [str , np .ndarray ]], nr_of_deposits : int , nr_of_pixels : int
516
+ output_arrays : Sequence [Dict [str , np .ndarray ]], weights_df : pd . DataFrame
486
517
) -> Tuple [np .ndarray , np .ndarray , np .ndarray ]:
487
518
"""Calculate the posterior probabilities for the given generalized weight arrays.
488
519
@@ -491,14 +522,17 @@ def weights_of_evidence_calculate_responses(
491
522
For each dictionary, generalized weight and generalized standard deviation arrays are used and summed
492
523
together pixel-wise to calculate the posterior probabilities. If generalized arrays are not found,
493
524
the W+ and S_W+ arrays are used (so if outputs from unique weight calculations are used for this function).
494
- nr_of_deposits: Number of deposit pixels in the input data for weights of evidence calculations.
495
- nr_of_pixels: Number of evidence pixels in the input data for weights of evidence calculations .
525
+ weights_df: Output dataframe of WofE calculate weights algorithm. Used for determining number of deposits and
526
+ number of pixels .
496
527
497
528
Returns:
498
529
Array of posterior probabilites.
499
530
Array of standard deviations in the posterior probability calculations.
500
531
Array of confidence of the prospectivity values obtained in the posterior probability array.
501
532
"""
533
+ array = list (output_arrays [0 ].values ())[0 ]
534
+ nr_of_deposits , nr_of_pixels = _calculate_nr_of_deposit_pixels (array , weights_df )
535
+
502
536
gen_weights_sum = sum (
503
537
[
504
538
item [GENERALIZED_WEIGHT_PLUS_COLUMN ]
@@ -531,7 +565,7 @@ def weights_of_evidence_calculate_responses(
531
565
532
566
@beartype
533
567
def agterberg_cheng_CI_test (
534
- posterior_probabilities : np .ndarray , posterior_probabilities_std : np .ndarray , nr_of_deposits : int
568
+ posterior_probabilities : np .ndarray , posterior_probabilities_std : np .ndarray , weights_df : pd . DataFrame
535
569
) -> Tuple [bool , bool , bool , float , str ]:
536
570
"""Perform the conditional independence test presented by Agterberg-Cheng (2002).
537
571
@@ -541,7 +575,8 @@ def agterberg_cheng_CI_test(
541
575
Args:
542
576
posterior_probabilities: Array of posterior probabilites.
543
577
posterior_probabilities_std: Array of standard deviations in the posterior probability calculations.
544
- nr_of_deposits: Number of deposit pixels in the input data for weights of evidence calculations.
578
+ weights_df: Output dataframe of WofE calculate weights algorithm. Used for determining number of deposits.
579
+
545
580
Returns:
546
581
Whether the conditional hypothesis can be accepted for the evidence layers that the input
547
582
posterior probabilities and standard deviations of posterior probabilities are calculated from.
@@ -550,12 +585,8 @@ def agterberg_cheng_CI_test(
550
585
Ratio T/n. Results > 1, may be because of lack of conditional independence of layers.
551
586
T should not exceed n by more than 15% (Bonham-Carter 1994, p. 316).
552
587
A summary of the the conditional independence calculations.
553
-
554
- Raises:
555
- InvalidParameterValueException: Value of nr_of_deposits is not at least 1.
556
588
"""
557
- if nr_of_deposits < 1 :
558
- raise InvalidParameterValueException ("Expected input deposits count to be at least 1." )
589
+ nr_of_deposits , _ = _calculate_nr_of_deposit_pixels (posterior_probabilities , weights_df )
559
590
560
591
# One-tailed significance test according to Agterberg-Cheng (2002):
561
592
# Conditional independence must satisfy:
0 commit comments