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

AdministrativeRegionAnalysis: ValueError: Cannot apply_along_axis when any iteration dimensions are 0 #113

Open
tommylees112 opened this issue Aug 16, 2019 · 2 comments
Labels

Comments

@tommylees112
Copy link
Contributor

## AdministrativeRegionAnalysis
Produces the following error when trying to run the true vs. predicted for regions

Error Trace:

---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
~/miniconda3/envs/crop/lib/python3.7/site-packages/numpy/lib/shape_base.py in apply_along_axis(func1d, axis, arr, *args, **kwargs)
    376     try:
--> 377         ind0 = next(inds)
    378     except StopIteration:

StopIteration:

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-82-fb568627f573> in <module>
     13
     14 admin_analyzer = AdministrativeRegionAnalysis(data_dir)
---> 15 admin_analyzer.analyze()

~/github/ml_drought/src/analysis/region_analysis/administrative_region_analysis.py in analyze(self, compute_global_errors, compute_regional_errors, save_all_df)
    122
    123         for region_data_path in self.region_data_paths:
--> 124             df = self._analyze_single(region_data_path)
    125             all_regions_dfs.append(df)
    126

~/github/ml_drought/src/analysis/region_analysis/administrative_region_analysis.py in _analyze_single(self, region_data_path)
    109         return self._base_analyze_single(
    110             admin_level_name=admin_level_name, region_da=region_da,
--> 111             region_lookup=region_lookup, region_group_name=region_group_name,
    112         )
    113

~/github/ml_drought/src/analysis/region_analysis/base.py in _base_analyze_single(self, admin_level_name, region_da, region_lookup, region_group_name, landcover_das)
    322                     ) = self.compute_mean_statistics(  # type: ignore
    323                         region_da=region_da, true_da=true_da, pred_da=pred_da,
--> 324                         region_lookup=region_lookup, datetime=dt
    325                     )
    326                 else:

~/github/ml_drought/src/analysis/region_analysis/administrative_region_analysis.py in compute_mean_statistics(self, region_da, region_lookup, pred_da, true_da, datetime)
     78             region_names.append(region_lookup[valid_region_id])
     79             predicted_mean_value.append(
---> 80                 pred_da.where(region_da == valid_region_id).mean().values
     81             )
     82             true_mean_value.append(

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/common.py in where(self, cond, other, drop)
    933             cond = cond.isel(**indexers)
    934
--> 935         return ops.where_method(self, cond, other)
    936
    937     def close(self: Any) -> None:

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/ops.py in where_method(self, cond, other)
    170                        dataset_join=join,
    171                        dask='allowed',
--> 172                        keep_attrs=True)
    173
    174

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/computation.py in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, *args)
    966                                      join=join,
    967                                      exclude_dims=exclude_dims,
--> 968                                      keep_attrs=keep_attrs)
    969     elif any(isinstance(a, Variable) for a in args):
    970         return variables_vfunc(*args)

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/computation.py in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args)
    215
    216     data_vars = [getattr(a, 'variable', a) for a in args]
--> 217     result_var = func(*data_vars)
    218
    219     if signature.num_outputs > 1:

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/computation.py in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, output_sizes, keep_attrs, *args)
    539                   if isinstance(arg, Variable)
    540                   else arg
--> 541                   for arg, core_dims in zip(args, signature.input_core_dims)]
    542
    543     if any(isinstance(array, dask_array_type) for array in input_data):

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/computation.py in <listcomp>(.0)
    539                   if isinstance(arg, Variable)
    540                   else arg
--> 541                   for arg, core_dims in zip(args, signature.input_core_dims)]
    542
    543     if any(isinstance(array, dask_array_type) for array in input_data):

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/computation.py in broadcast_compat_data(variable, broadcast_dims, core_dims)
    470 def broadcast_compat_data(variable, broadcast_dims, core_dims):
    471     # type: (Variable, tuple, tuple) -> Any
--> 472     data = variable.data
    473
    474     old_dims = variable.dims

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/variable.py in data(self)
    292             return self._data
    293         else:
--> 294             return self.values
    295
    296     @data.setter

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/variable.py in values(self)
    387     def values(self):
    388         """The variable's data as a numpy.ndarray"""
--> 389         return _as_array_or_item(self._data)
    390
    391     @values.setter

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/variable.py in _as_array_or_item(data)
    208     TODO: remove this (replace with np.asarray) once these issues are fixed
    209     """
--> 210     data = np.asarray(data)
    211     if data.ndim == 0:
    212         if data.dtype.kind == 'M':

~/miniconda3/envs/crop/lib/python3.7/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    536
    537     """
--> 538     return array(a, dtype, copy=False, order=order)
    539
    540

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/indexing.py in __array__(self, dtype)
    630
    631     def __array__(self, dtype=None):
--> 632         self._ensure_cached()
    633         return np.asarray(self.array, dtype=dtype)
    634

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/indexing.py in _ensure_cached(self)
    627     def _ensure_cached(self):
    628         if not isinstance(self.array, NumpyIndexingAdapter):
--> 629             self.array = NumpyIndexingAdapter(np.asarray(self.array))
    630
    631     def __array__(self, dtype=None):

~/miniconda3/envs/crop/lib/python3.7/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    536
    537     """
--> 538     return array(a, dtype, copy=False, order=order)
    539
    540

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/indexing.py in __array__(self, dtype)
    608
    609     def __array__(self, dtype=None):
--> 610         return np.asarray(self.array, dtype=dtype)
    611
    612     def __getitem__(self, key):

~/miniconda3/envs/crop/lib/python3.7/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    536
    537     """
--> 538     return array(a, dtype, copy=False, order=order)
    539
    540

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/indexing.py in __array__(self, dtype)
    514     def __array__(self, dtype=None):
    515         array = as_indexable(self.array)
--> 516         return np.asarray(array[self.key], dtype=None)
    517
    518     def transpose(self, order):

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/backends/netCDF4_.py in __getitem__(self, key)
     62         return indexing.explicit_indexing_adapter(
     63             key, self.shape, indexing.IndexingSupport.OUTER,
---> 64             self._getitem)
     65
     66     def _getitem(self, key):

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/core/indexing.py in explicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)
    782     """
    783     raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
--> 784     result = raw_indexing_method(raw_key.tuple)
    785     if numpy_indices.tuple:
    786         # index the loaded np.ndarray

~/miniconda3/envs/crop/lib/python3.7/site-packages/xarray/backends/netCDF4_.py in _getitem(self, key)
     73             with self.datastore.lock:
     74                 original_array = self.get_array(needs_lock=False)
---> 75                 array = getitem(original_array, key)
     76         except IndexError:
     77             # Catch IndexError in netCDF4 and return a more informative

netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Variable.__getitem__()

~/miniconda3/envs/crop/lib/python3.7/site-packages/netCDF4/utils.py in _StartCountStride(elem, shape, dimensions, grp, datashape, put, use_get_vars)
    435         #    ITERABLE    #
    436         elif np.iterable(e) and np.array(e).dtype.kind in 'i':  # Sequence of integers
--> 437             start[...,i] = np.apply_along_axis(lambda x: e*x, i, np.ones(sdim[:-1]))
    438             indices[...,i] = np.apply_along_axis(lambda x: np.arange(sdim[i])*x, i, np.ones(sdim[:-1], int))
    439

~/miniconda3/envs/crop/lib/python3.7/site-packages/numpy/lib/shape_base.py in apply_along_axis(func1d, axis, arr, *args, **kwargs)
    377         ind0 = next(inds)
    378     except StopIteration:
--> 379         raise ValueError('Cannot apply_along_axis when any iteration dimensions are 0')
    380     res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
    381

ValueError: Cannot apply_along_axis when any iteration dimensions are 0
@tommylees112
Copy link
Contributor Author

This error is caused by the different shapes of the predicted/true xr.DataArray objects at data/models/{experiment}/{model}/*.nc & data/features/{experiment}/train/{timestep}/*.nc

ipdb> pred_da
<xarray.DataArray 'preds' (lat: 224, lon: 176)>
array([[33.623024, 45.5946  , 36.096382, ...,       nan,       nan,       nan],
       [27.531542, 37.75934 , 37.770805, ...,       nan,       nan,       nan],
       [35.528862, 37.54693 , 43.118332, ...,       nan,       nan,       nan],
       ...,
       [49.296875, 55.80316 , 35.041126, ..., 18.268587, 26.510838, 17.255913],
       [57.478218, 37.032967, 49.80915 , ..., 25.295422, 22.875132, 15.904449],
       [43.87786 , 45.465843, 61.007156, ..., 24.108068, 28.35307 , 26.160402]],
      dtype=float32)
Coordinates:
  * lat      (lat) float64 -5.175 -5.125 -5.075 -5.025 ... 5.875 5.925 5.975
  * lon      (lon) float64 33.52 33.57 33.62 33.68 ... 42.12 42.18 42.22 42.27

and the region_da at ``

ipdb> region_da
<xarray.DataArray 'district_l2' (lat: 311, lon: 244)>
array([[nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       ...,
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan],
       [nan, nan, nan, ..., nan, nan, nan]])
Coordinates:
  * lat      (lat) float64 -5.17 -5.134 -5.098 -5.062 ... 5.921 5.957 5.993
  * lon      (lon) float64 33.5 33.54 33.57 33.61 ... 42.14 42.18 42.21 42.25

@tommylees112
Copy link
Contributor Author

Written some assert statements to çapture this error and give some more informative warnings!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant