Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalable Machine Learning workflow - fails #1271

Open
gflod opened this issue Oct 2, 2024 · 0 comments
Open

Scalable Machine Learning workflow - fails #1271

gflod opened this issue Oct 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@gflod
Copy link

gflod commented Oct 2, 2024

Describe the bug/issue
The notebook seems to be calling on the incorrect bands for the product being called

Steps to reproduce
Steps to reproduce the behaviour.

  1. Open DEA Sandbox
  2. Launch the "Real_world_examples/Scalable_machine_learning/4_Classify_satellite_data.ipynb" notebook
  3. Run all cells
  4. Code in section "Loop through test locations and predict" produces error "--------------------------------------------------------------------------
    AttributeError Traceback (most recent call last)
    File ~/Real_world_examples/Scalable_machine_learning/../../Tools/dea_tools/bandindices.py:410, in calculate_indices(ds, index, collection, custom_varname, normalise, drop, inplace)
    409 mult = 10000.0 if normalise else 1.0
    --> 410 index_array = index_func(ds.rename(bands_to_rename) / mult)
    411 except AttributeError:

File ~/Real_world_examples/Scalable_machine_learning/../../Tools/dea_tools/bandindices.py:152, in calculate_indices..(ds)
149 # Dictionary containing remote sensing index band recipes
150 index_dict = {
151 # Normalised Difference Vegation Index, Rouse 1973
--> 152 'NDVI': lambda ds: (ds.nir - ds.red) /
153 (ds.nir + ds.red),
154
155 # Non-linear Normalised Difference Vegation Index,
156 # Camps-Valls et al. 2021
157 'kNDVI': lambda ds: np.tanh(((ds.nir - ds.red) /
158 (ds.nir + ds.red)) ** 2),
159
160 # Enhanced Vegetation Index, Huete 2002
161 'EVI': lambda ds: ((2.5 * (ds.nir - ds.red)) /
162 (ds.nir + 6 * ds.red -
163 7.5 * ds.blue + 1)),
164
165 # Leaf Area Index, Boegh 2002
166 'LAI': lambda ds: (3.618 * ((2.5 * (ds.nir - ds.red)) /
167 (ds.nir + 6 * ds.red -
168 7.5 * ds.blue + 1)) - 0.118),
169
170 # Soil Adjusted Vegetation Index, Huete 1988
171 'SAVI': lambda ds: ((1.5 * (ds.nir - ds.red)) /
172 (ds.nir + ds.red + 0.5)),
173
174 # Mod. Soil Adjusted Vegetation Index, Qi et al. 1994
175 'MSAVI': lambda ds: ((2 * ds.nir + 1 -
176 ((2 * ds.nir + 1)**2 -
177 8 * (ds.nir - ds.red))**0.5) / 2),
178
179 # Normalised Difference Moisture Index, Gao 1996
180 'NDMI': lambda ds: (ds.nir - ds.swir1) /
181 (ds.nir + ds.swir1),
182
183 # Normalised Burn Ratio, Lopez Garcia 1991
184 'NBR': lambda ds: (ds.nir - ds.swir2) /
185 (ds.nir + ds.swir2),
186
187 # Burn Area Index, Martin 1998
188 'BAI': lambda ds: (1.0 / ((0.10 - ds.red) ** 2 +
189 (0.06 - ds.nir) ** 2)),
190
191 # Normalised Difference Chlorophyll Index,
192 # (Mishra & Mishra, 2012)
193 'NDCI': lambda ds: (ds.red_edge_1 - ds.red) /
194 (ds.red_edge_1 + ds.red),
195
196 # Normalised Difference Snow Index, Hall 1995
197 'NDSI': lambda ds: (ds.green - ds.swir1) /
198 (ds.green + ds.swir1),
199
200 # Normalised Difference Tillage Index,
201 # Van Deventer et al. 1997
202 'NDTI': lambda ds: (ds.swir1 - ds.swir2) /
203 (ds.swir1 + ds.swir2),
204
205 # Normalised Difference Turbidity Index,
206 # Lacaux et al., 2007
207 'NDTI2': lambda ds: (ds.red - ds.green) /
208 (ds.red + ds.green),
209
210 # Normalised Difference Water Index, McFeeters 1996
211 'NDWI': lambda ds: (ds.green - ds.nir) /
212 (ds.green + ds.nir),
213
214 # Modified Normalised Difference Water Index, Xu 2006
215 'MNDWI': lambda ds: (ds.green - ds.swir1) /
216 (ds.green + ds.swir1),
217
218 # Normalised Difference Built-Up Index, Zha 2003
219 'NDBI': lambda ds: (ds.swir1 - ds.nir) /
220 (ds.swir1 + ds.nir),
221
222 # Built-Up Index, He et al. 2010
223 'BUI': lambda ds: ((ds.swir1 - ds.nir) /
224 (ds.swir1 + ds.nir)) -
225 ((ds.nir - ds.red) /
226 (ds.nir + ds.red)),
227
228 # Built-up Area Extraction Index, Bouzekri et al. 2015
229 'BAEI': lambda ds: (ds.red + 0.3) /
230 (ds.green + ds.swir1),
231
232 # New Built-up Index, Jieli et al. 2010
233 'NBI': lambda ds: (ds.swir1 + ds.red) / ds.nir,
234
235 # Bare Soil Index, Rikimaru et al. 2002
236 'BSI': lambda ds: ((ds.swir1 + ds.red) -
237 (ds.nir + ds.blue)) /
238 ((ds.swir1 + ds.red) +
239 (ds.nir + ds.blue)),
240
241 # Automated Water Extraction Index (no shadows), Feyisa 2014
242 'AWEI_ns': lambda ds: (4 * (ds.green - ds.swir1) -
243 (0.25 * ds.nir * + 2.75 * ds.swir2)),
244
245 # Automated Water Extraction Index (shadows), Feyisa 2014
246 'AWEI_sh': lambda ds: (ds.blue + 2.5 * ds.green -
247 1.5 * (ds.nir + ds.swir1) -
248 0.25 * ds.swir2),
249
250 # Water Index, Fisher 2016
251 'WI': lambda ds: (1.7204 + 171 * ds.green + 3 * ds.red -
252 70 * ds.nir - 45 * ds.swir1 -
253 71 * ds.swir2),
254
255 # Tasseled Cap Wetness, Crist 1985
256 'TCW': lambda ds: (0.0315 * ds.blue + 0.2021 * ds.green +
257 0.3102 * ds.red + 0.1594 * ds.nir +
258 -0.6806 * ds.swir1 + -0.6109 * ds.swir2),
259
260 # Tasseled Cap Greeness, Crist 1985
261 'TCG': lambda ds: (-0.1603 * ds.blue + -0.2819 * ds.green +
262 -0.4934 * ds.red + 0.7940 * ds.nir +
263 -0.0002 * ds.swir1 + -0.1446 * ds.swir2),
264
265 # Tasseled Cap Brightness, Crist 1985
266 'TCB': lambda ds: (0.2043 * ds.blue + 0.4158 * ds.green +
267 0.5524 * ds.red + 0.5741 * ds.nir +
268 0.3124 * ds.swir1 + -0.2303 * ds.swir2),
269
270 # Tasseled Cap Transformations with Sentinel-2 coefficients
271 # after Nedkov 2017 using Gram-Schmidt orthogonalization (GSO)
272 # Tasseled Cap Wetness, Nedkov 2017
273 'TCW_GSO': lambda ds: (0.0649 * ds.blue + 0.2802 * ds.green +
274 0.3072 * ds.red + -0.0807 * ds.nir +
275 -0.4064 * ds.swir1 + -0.5602 * ds.swir2),
276
277 # Tasseled Cap Greeness, Nedkov 2017
278 'TCG_GSO': lambda ds: (-0.0635 * ds.blue + -0.168 * ds.green +
279 -0.348 * ds.red + 0.3895 * ds.nir +
280 -0.4587 * ds.swir1 + -0.4064 * ds.swir2),
281
282 # Tasseled Cap Brightness, Nedkov 2017
283 'TCB_GSO': lambda ds: (0.0822 * ds.blue + 0.136 * ds.green +
284 0.2611 * ds.red + 0.5741 * ds.nir +
285 0.3882 * ds.swir1 + 0.1366 * ds.swir2),
286
287 # Clay Minerals Ratio, Drury 1987
288 'CMR': lambda ds: (ds.swir1 / ds.swir2),
289
290 # Ferrous Minerals Ratio, Segal 1982
291 'FMR': lambda ds: (ds.swir1 / ds.nir),
292
293 # Iron Oxide Ratio, Segal 1982
294 'IOR': lambda ds: (ds.red / ds.blue),
295
296 }
298 # If index supplied is not a list, convert to list. This allows us to
299 # iterate through either multiple or single indices in the loop below

File /env/lib/python3.10/site-packages/xarray/core/common.py:285, in AttrAccessMixin.getattr(self, name)
284 return source[name]
--> 285 raise AttributeError(
286 f"{type(self).name!r} object has no attribute {name!r}"
287 )

AttributeError: 'Dataset' object has no attribute 'nir'

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
Cell In[21], line 15
12 query.update(bounds)
14 # Load data and calculate features
---> 15 data = feature_layers(query).squeeze()
17 # Predict using the imported model
18 predicted = predict_xr(model,
19 data,
20 proba=True,
21 persist=True,
22 clean=True,
23 return_input=True).compute()

Cell In[18], line 11, in feature_layers(query)
7 ds = dc.load(product='ga_ls8c_nbart_gm_cyear_3',
8 **query)
10 # Calculate some band indices
---> 11 da = calculate_indices(ds,
12 index=['NDVI', 'LAI', 'MNDWI'],
13 drop=False,
14 collection='ga_gm_3')
16 # Add Fractional cover percentiles
17 fc = dc.load(product='ga_ls_fc_pc_cyear_3',
18 measurements=['pv_pc_10', 'pv_pc_50', 'pv_pc_90'], # only the PV band
19 like=ds.geobox, # will match geomedian extent
20 time=query.get('time'), # use time if in query
21 dask_chunks=query.get('dask_chunks') # use dask if in query
22 )

File ~/Real_world_examples/Scalable_machine_learning/../../Tools/dea_tools/bandindices.py:412, in calculate_indices(ds, index, collection, custom_varname, normalise, drop, inplace)
410 index_array = index_func(ds.rename(bands_to_rename) / mult)
411 except AttributeError:
--> 412 raise ValueError(f'Please verify that all bands required to '
413 f'compute {index} are present in ds. \n'
414 f'These bands may vary depending on the collection '
415 f'(e.g. the Landsat nbart_nir band \n'
416 f'is equivelent to nbart_nir_1 for Sentinel 2)')
418 # Add as a new variable in dataset
419 output_band_name = custom_varname if custom_varname else index

ValueError: Please verify that all bands required to compute NDVI are present in ds.
These bands may vary depending on the collection (e.g. the Landsat nbart_nir band
is equivelent to nbart_nir_1 for Sentinel 2)"

Expected behaviour
Based on the example notebook this should calculate band indices as directed to be used in the classification model

Environment information
Using DEA Sandbox environment

@gflod gflod added the bug Something isn't working label Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant