Skip to content

Distance to anomaly

distance_to_anomaly(anomaly_raster_profile, anomaly_raster_data, threshold_criteria_value, threshold_criteria)

Calculate distance from raster cell to nearest anomaly.

The criteria for what is anomalous can be defined as a single number and criteria text of "higher" or "lower". Alternatively, the definition can be a range where values inside (criteria text of "within") or outside are marked as anomalous (criteria text of "outside"). If anomaly_raster_profile does contain "nodata" key, np.nan is assumed to correspond to nodata values.

Parameters:

Name Type Description Default
anomaly_raster_profile Union[Profile, dict]

The raster profile in which the distances to the nearest anomalous value are determined.

required
anomaly_raster_data ndarray

The raster data in which the distances to the nearest anomalous value are determined.

required
threshold_criteria_value Union[Tuple[Number, Number], Number]

Value(s) used to define anomalous. If the threshold criteria requires a tuple of values, the first value should be the minimum and the second the maximum value.

required
threshold_criteria Literal[lower, higher, in_between, outside]

Method to define anomalous.

required

Returns:

Type Description
ndarray

A 2D numpy array with the distances to anomalies computed

Union[Profile, dict]

and the original anomaly raster profile.

Source code in eis_toolkit/raster_processing/distance_to_anomaly.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@beartype
def distance_to_anomaly(
    anomaly_raster_profile: Union[profiles.Profile, dict],
    anomaly_raster_data: np.ndarray,
    threshold_criteria_value: Union[Tuple[Number, Number], Number],
    threshold_criteria: Literal["lower", "higher", "in_between", "outside"],
) -> Tuple[np.ndarray, Union[profiles.Profile, dict]]:
    """Calculate distance from raster cell to nearest anomaly.

    The criteria for what is anomalous can be defined as a single number and
    criteria text of "higher" or "lower". Alternatively, the definition can be
    a range where values inside (criteria text of "within") or outside are
    marked as anomalous (criteria text of "outside"). If anomaly_raster_profile does
    contain "nodata" key, np.nan is assumed to correspond to nodata values.

    Args:
        anomaly_raster_profile: The raster profile in which the distances
            to the nearest anomalous value are determined.
        anomaly_raster_data: The raster data in which the distances
            to the nearest anomalous value are determined.
        threshold_criteria_value: Value(s) used to define anomalous.
            If the threshold criteria requires a tuple of values,
            the first value should be the minimum and the second
            the maximum value.
        threshold_criteria: Method to define anomalous.

    Returns:
        A 2D numpy array with the distances to anomalies computed
        and the original anomaly raster profile.

    """
    check_raster_profile(raster_profile=anomaly_raster_profile)
    _check_threshold_criteria_and_value(
        threshold_criteria=threshold_criteria, threshold_criteria_value=threshold_criteria_value
    )

    out_image = _distance_to_anomaly(
        anomaly_raster_profile=anomaly_raster_profile,
        anomaly_raster_data=anomaly_raster_data,
        threshold_criteria=threshold_criteria,
        threshold_criteria_value=threshold_criteria_value,
    )
    return out_image, anomaly_raster_profile

distance_to_anomaly_gdal(anomaly_raster_profile, anomaly_raster_data, threshold_criteria_value, threshold_criteria, output_path, verbose=False)

Calculate distance from raster cell to nearest anomaly.

Distance is calculated for each cell in the anomaly raster and saved to a new raster at output_path. The criteria for what is anomalous can be defined as a single number and criteria text of "higher" or "lower". Alternatively, the definition can be a range where values inside (criteria text of "within") or outside are marked as anomalous (criteria text of "outside"). If anomaly_raster_profile does contain "nodata" key, np.nan is assumed to correspond to nodata values.

Does not work on Windows.

Parameters:

Name Type Description Default
anomaly_raster_profile Union[Profile, dict]

The raster profile in which the distances to the nearest anomalous value are determined.

required
anomaly_raster_data ndarray

The raster data in which the distances to the nearest anomalous value are determined.

required
threshold_criteria_value Union[Tuple[Number, Number], Number]

Value(s) used to define anomalous.

required
threshold_criteria Literal[lower, higher, in_between, outside]

Method to define anomalous.

required
output_path Path

The path to the raster with the distances to anomalies calculated.

required
verbose bool

Whether to print gdal_proximity output.

False

Returns:

Type Description
Path

The path to the raster with the distances to anomalies calculated.

Source code in eis_toolkit/raster_processing/distance_to_anomaly.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
@beartype
def distance_to_anomaly_gdal(
    anomaly_raster_profile: Union[profiles.Profile, dict],
    anomaly_raster_data: np.ndarray,
    threshold_criteria_value: Union[Tuple[Number, Number], Number],
    threshold_criteria: Literal["lower", "higher", "in_between", "outside"],
    output_path: Path,
    verbose: bool = False,
) -> Path:
    """Calculate distance from raster cell to nearest anomaly.

    Distance is calculated for each cell in the anomaly raster and saved to a
    new raster at output_path. The criteria for what is anomalous can be
    defined as a single number and criteria text of "higher" or "lower".
    Alternatively, the definition can be a range where values inside
    (criteria text of "within") or outside are marked as anomalous
    (criteria text of "outside"). If anomaly_raster_profile does
    contain "nodata" key, np.nan is assumed to correspond to nodata values.

    Does not work on Windows.

    Args:
        anomaly_raster_profile: The raster profile in which the distances
            to the nearest anomalous value are determined.
        anomaly_raster_data: The raster data in which the distances
            to the nearest anomalous value are determined.
        threshold_criteria_value: Value(s) used to define anomalous.
        threshold_criteria: Method to define anomalous.
        output_path: The path to the raster with the distances to anomalies
            calculated.
        verbose: Whether to print gdal_proximity output.

    Returns:
        The path to the raster with the distances to anomalies calculated.
    """
    check_raster_profile(raster_profile=anomaly_raster_profile)
    _check_threshold_criteria_and_value(
        threshold_criteria=threshold_criteria, threshold_criteria_value=threshold_criteria_value
    )

    return _distance_to_anomaly_gdal(
        output_path=output_path,
        anomaly_raster_profile=anomaly_raster_profile,
        anomaly_raster_data=anomaly_raster_data,
        threshold_criteria=threshold_criteria,
        threshold_criteria_value=threshold_criteria_value,
        verbose=verbose,
    )