ks905383 / xagg

Aggregating gridded data (xarray) to polygons
https://xagg.readthedocs.io/
GNU General Public License v3.0
76 stars 14 forks source link

xagg use on google colab #3

Open jamesafranke opened 3 years ago

jamesafranke commented 3 years ago

Hello - Thanks for the excellent package!

I believe I have the google colab dependencies worked out for those who may be inclined to go that route. First, you need to get miniconda env for esmpy. This cell will automatically restart your notebook.

!pip install -q condacolab
import condacolab
condacolab.install()

Next, install the dependencies (not 100% sure on rtree or pygeos...so I just installed them both).

%%capture
!conda install -c conda-forge esmpy
!pip install rtree pygeos
!pip install xesmf
!pip install geopandas
!pip install xagg

Restart the notebook 1 more time... and you are good to import your packages and start aggregating!

HaynesStephens commented 2 years ago

Hey, I think some changed versions/dependencies may have screwed with this recipe. Everything still works up until the xagg.aggregate() function, where it hits a snag (see code below).

It seems like this error is due to pip being unable to load the latest version of xagg due to dependency issues.

I can't seem to get xagg installed on Colab using the conda install -c conda-forge xagg command, but perhaps getting that to work would be the proper fix.

adjusting grid... (this may happen because only a subset of pixels were used for aggregation for efficiency - i.e. [subset_bbox=True] in xa.pixel_overlaps())
grid adjustment successful
aggregating tas...
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-70-46fd7cb2f46c> in <module>()
----> 1 aggregated = xa.aggregate( ds, weightmap )
      2 
      3 ### export ###
      4 # aggregated.to_netcdf('/content/drive/Shareddrives/ClimateAg/data/pr_agmerra_ag_districts.nc4')
5 frames
/usr/local/lib/python3.7/site-packages/xagg/core.py
 in aggregate(ds, wm)
    386                 #   the grid have just nan values for this variable
    387                 # in both cases; the "aggregated variable" is just a vector of nans.
--> 388                 if not np.isnan(wm.agg.iloc[poly_idx,:].pix_idxs).all():
    389                     # Get the dimensions of the variable that aren't "loc" (location)
    390                     other_dims = [k for k in np.atleast_1d(ds[var].dims) if k != 'loc']

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in __getitem__(self, key)
    923                 with suppress(KeyError, IndexError):
    924                     return self.obj._get_value(*key, takeable=self._takeable)
--> 925             return self._getitem_tuple(key)
    926         else:
    927             # we by definition only have the 0th axis

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
   1504     def _getitem_tuple(self, tup: tuple):
   1505 
-> 1506         self._has_valid_tuple(tup)
   1507         with suppress(IndexingError):
   1508             return self._getitem_lowerdim(tup)

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _has_valid_tuple(self, key)
    752         for i, k in enumerate(key):
    753             try:
--> 754                 self._validate_key(k, i)
    755             except ValueError as err:
    756                 raise ValueError(

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _validate_key(self, key, axis)
   1407             return
   1408         elif is_integer(key):
-> 1409             self._validate_integer(key, axis)
   1410         elif isinstance(key, tuple):
   1411             # a tuple should already have been caught by this point

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _validate_integer(self, key, axis)
   1498         len_axis = len(self.obj._get_axis(axis))
   1499         if key >= len_axis or key < -len_axis:
-> 1500             raise IndexError("single positional indexer is out-of-bounds")
   1501 
   1502     # -------------------------------------------------------------------
IndexError: single positional indexer is out-of-bounds