-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcombine.py
More file actions
332 lines (280 loc) · 9.16 KB
/
combine.py
File metadata and controls
332 lines (280 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
80
81
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""Combination methods for MCDA criterion layers.
All functions take an ``xr.Dataset`` of standardized (0-1) criterion
layers and return a single composite ``xr.DataArray``.
"""
from __future__ import annotations
import numpy as np
import xarray as xr
def _validate_criteria(criteria: xr.Dataset) -> None:
if not isinstance(criteria, xr.Dataset):
raise TypeError(
f"criteria must be an xr.Dataset, got {type(criteria).__name__}"
)
if len(criteria.data_vars) == 0:
raise ValueError("criteria Dataset has no variables")
def _validate_weights(
weights: dict[str, float], criteria: xr.Dataset
) -> None:
criteria_names = set(criteria.data_vars)
weight_names = set(weights)
missing = criteria_names - weight_names
if missing:
raise ValueError(f"Missing weights for criteria: {missing}")
extra = weight_names - criteria_names
if extra:
raise ValueError(
f"Weights contain keys not in criteria Dataset: {extra}"
)
total = sum(weights.values())
if abs(total - 1.0) > 0.01:
raise ValueError(
f"Weights must sum to ~1.0, got {total:.4f}"
)
def wlc(
criteria: xr.Dataset,
weights: dict[str, float],
name: str = "wlc",
) -> xr.DataArray:
"""Weighted Linear Combination.
Fully compensatory: per-pixel ``sum(w_i * x_i)``.
Parameters
----------
criteria : xr.Dataset
Standardized criterion layers (0-1).
weights : dict
``{criterion_name: weight}``, must sum to ~1.0.
name : str
Name of the output DataArray.
Returns
-------
xr.DataArray
Composite suitability surface.
"""
_validate_criteria(criteria)
_validate_weights(weights, criteria)
result = None
for var_name in criteria.data_vars:
w = weights[var_name]
term = criteria[var_name] * w
result = term if result is None else result + term
result.name = name
return result
def wpm(
criteria: xr.Dataset,
weights: dict[str, float],
name: str = "wpm",
) -> xr.DataArray:
"""Weighted Product Model.
Multiplicative combination: per-pixel ``product(x_i ^ w_i)``.
More sensitive to low scores than WLC.
Parameters
----------
criteria : xr.Dataset
Standardized criterion layers (0-1).
weights : dict
``{criterion_name: weight}``, must sum to ~1.0.
name : str
Name of the output DataArray.
Returns
-------
xr.DataArray
Composite suitability surface.
"""
_validate_criteria(criteria)
_validate_weights(weights, criteria)
result = None
for var_name in criteria.data_vars:
w = weights[var_name]
term = criteria[var_name] ** w
result = term if result is None else result * term
result.name = name
return result
def owa(
criteria: xr.Dataset,
criterion_weights: dict[str, float],
order_weights: list[float],
name: str = "owa",
) -> xr.DataArray:
"""Ordered Weighted Averaging.
Generalizes WLC by adding position-based order weights that control
risk attitude. Order weights are applied to criterion values sorted
by magnitude at each pixel, not by criterion identity.
Parameters
----------
criteria : xr.Dataset
Standardized criterion layers (0-1).
criterion_weights : dict
``{criterion_name: weight}``, must sum to ~1.0.
order_weights : list of float
Weights applied by rank position (index 0 = highest value).
Must have the same length as the number of criteria and
sum to ~1.0.
name : str
Name of the output DataArray.
Returns
-------
xr.DataArray
Composite suitability surface.
"""
_validate_criteria(criteria)
_validate_weights(criterion_weights, criteria)
n = len(criteria.data_vars)
if len(order_weights) != n:
raise ValueError(
f"order_weights length ({len(order_weights)}) must match "
f"number of criteria ({n})"
)
ow_sum = sum(order_weights)
if abs(ow_sum - 1.0) > 0.01:
raise ValueError(
f"order_weights must sum to ~1.0, got {ow_sum:.4f}"
)
order_weights_arr = np.array(order_weights, dtype=np.float64)
# First apply criterion weights
weighted_layers = []
for var_name in criteria.data_vars:
w = criterion_weights[var_name]
weighted_layers.append(criteria[var_name] * w * n)
# Stack, sort descending along criterion axis, apply order weights
stacked = xr.concat(weighted_layers, dim="__mcda_criterion")
sorted_data = _sort_descending(stacked.data, axis=0)
# Apply order weights along the criterion axis
# Reshape order weights for broadcasting
shape = [n] + [1] * (sorted_data.ndim - 1)
try:
import dask.array as da
if isinstance(sorted_data, da.Array):
ow = da.from_array(order_weights_arr.reshape(shape),
chunks=-1)
else:
ow = order_weights_arr.reshape(shape)
except ImportError:
ow = order_weights_arr.reshape(shape)
result_data = (sorted_data * ow).sum(axis=0)
# Pick dims/coords from first data var
first_var = list(criteria.data_vars)[0]
template = criteria[first_var]
return xr.DataArray(
result_data, name=name, dims=template.dims,
coords=template.coords, attrs=template.attrs,
)
def _sort_descending(data, axis):
"""Sort array descending along axis, supporting dask."""
try:
import dask.array as da
if isinstance(data, da.Array):
# Negate, sort, negate back to get descending order
return -da.sort(-data, axis=axis)
except ImportError:
pass
try:
import cupy
if isinstance(data, cupy.ndarray):
return -cupy.sort(-data, axis=axis)
except ImportError:
pass
return -np.sort(-data, axis=axis)
def fuzzy_overlay(
criteria: xr.Dataset,
operator: str = "and",
gamma: float = 0.9,
name: str = "fuzzy_overlay",
) -> xr.DataArray:
"""Combine criteria using fuzzy set operators.
No explicit weights. All criteria contribute equally through the
chosen fuzzy operator.
Parameters
----------
criteria : xr.Dataset
Standardized criterion layers (0-1).
operator : str
Fuzzy operator: ``"and"`` (min), ``"or"`` (max),
``"sum"``, ``"product"``, or ``"gamma"``.
gamma : float
Gamma parameter for the ``"gamma"`` operator (0 = product,
1 = sum). Only used when ``operator="gamma"``.
name : str
Name of the output DataArray.
Returns
-------
xr.DataArray
Composite suitability surface.
"""
_validate_criteria(criteria)
var_names = list(criteria.data_vars)
layers = [criteria[v] for v in var_names]
if operator == "and":
result = layers[0]
for layer in layers[1:]:
result = np.minimum(result, layer)
elif operator == "or":
result = layers[0]
for layer in layers[1:]:
result = np.maximum(result, layer)
elif operator == "product":
result = layers[0]
for layer in layers[1:]:
result = result * layer
elif operator == "sum":
# Fuzzy algebraic sum: 1 - product(1 - x_i)
complement = layers[0] * 0.0 + 1.0
for layer in layers:
complement = complement * (1.0 - layer)
result = 1.0 - complement
elif operator == "gamma":
if not 0 <= gamma <= 1:
raise ValueError(f"gamma must be in [0, 1], got {gamma}")
# product part
prod = layers[0]
for layer in layers[1:]:
prod = prod * layer
# sum part
complement = layers[0] * 0.0 + 1.0
for layer in layers:
complement = complement * (1.0 - layer)
fuzzy_sum = 1.0 - complement
result = (fuzzy_sum ** gamma) * (prod ** (1.0 - gamma))
else:
raise ValueError(
f"Unknown operator {operator!r}. Choose from "
"'and', 'or', 'sum', 'product', 'gamma'"
)
result.name = name
return result
def boolean_overlay(
criteria: dict[str, xr.DataArray],
operator: str = "and",
name: str = "boolean_overlay",
) -> xr.DataArray:
"""Combine binary (boolean) criterion masks.
Parameters
----------
criteria : dict of str to xr.DataArray
Named boolean/binary criterion masks.
operator : str
``"and"`` (intersection) or ``"or"`` (union).
name : str
Name of the output DataArray.
Returns
-------
xr.DataArray
Binary suitability mask.
"""
if not criteria:
raise ValueError("criteria dict is empty")
# Cast to bool to handle numeric input (0/1, float thresholds)
layers = [v.astype(bool) for v in criteria.values()]
if operator == "and":
result = layers[0]
for layer in layers[1:]:
result = result & layer
elif operator == "or":
result = layers[0]
for layer in layers[1:]:
result = result | layer
else:
raise ValueError(
f"Unknown operator {operator!r}. Choose from 'and', 'or'"
)
result.name = name
return result