Skip to content

Commit

Permalink
Merge pull request #393 from Ouranosinc/fix-nb-for-new-jupyter-env
Browse files Browse the repository at this point in the history
Extract_geographical_watershed_properties.ipynb: fix run failure in new Jupyter env

Fixes #392 using hints from #388. Closes #388.

Note I am not sure what I am doing as I do not understand the code.  I simply try to get around the errors and produce similar graph.  The old graph had `spatial_ref` in the title, the new one don't.  Wonder if it's something critical.

Clarification from Trevor: 

 ==========
Putting it simply, the object returned (`xarray.DataArray`) is different from what the following cell needed to handle for (`rasterio._io.RasterReader` → `numpy.ndarray`):
```
23:00:58  Input:
23:00:58  # Note that geojson, and rasterio/rioxarray need to be installed for this to work.
23:00:58  # In the next birdy release, the tiff grid will be automatically converted to a DataArray.
23:00:58  # Here we need to manually convert it.
23:00:58  features, statistics, grid = stats_resp.get(asobj=True)
23:00:58
23:00:58  with rasterio.io.MemoryFile(grid[0]) as memfile:
23:00:58      with memfile.open() as dataset:
23:00:58          grid = rio.open_rasterio(dataset)
23:00:58
23:00:58  grid.plot()
```
The `spatial_ref`title is something that `rasterio` adds to the plot, and it isn't at all needed. The notebook should work perfectly so long as the client running it has `rioxarray`available in their ipython kernel.

 ==========

With the new Jupypter env for latest RavenPy `0.5.2` from PR Ouranosinc/PAVICS-e2e-workflow-tests#75 `Extract_geographical_watershed_properties.ipynb` fails with the following error below.

```
23:00:58  _ raven-master/docs/source/notebooks/Extract_geographical_watershed_properties.ipynb::Cell 7 _
23:00:58  Notebook cell execution failed
23:00:58  Cell 7: Cell execution caused an exception
23:00:58
23:00:58  Input:
23:00:58  # Note that geojson, and rasterio/rioxarray need to be installed for this to work.
23:00:58  # In the next birdy release, the tiff grid will be automatically converted to a DataArray.
23:00:58  # Here we need to manually convert it.
23:00:58  features, statistics, grid = stats_resp.get(asobj=True)
23:00:58
23:00:58  with rasterio.io.MemoryFile(grid[0]) as memfile:
23:00:58      with memfile.open() as dataset:
23:00:58          grid = rio.open_rasterio(dataset)
23:00:58
23:00:58  grid.plot()
23:00:58
23:00:58  Traceback:
23:00:58
23:00:58  ---------------------------------------------------------------------------
23:00:58  ValueError                                Traceback (most recent call last)
23:00:58  <ipython-input-8-8b167a3ecdc9> in <module>
23:00:58        4 features, statistics, grid = stats_resp.get(asobj=True)
23:00:58        5
23:00:58  ----> 6 with rasterio.io.MemoryFile(grid[0]) as memfile:
23:00:58        7     with memfile.open() as dataset:
23:00:58        8         grid = rio.open_rasterio(dataset)
23:00:58
23:00:58  /opt/conda/envs/birdy/lib/python3.7/site-packages/rasterio/io.py in __init__(self, file_or_bytes, dirname, filename, ext)
23:00:58      103         """
23:00:58      104         super(MemoryFile, self).__init__(
23:00:58  --> 105             file_or_bytes=file_or_bytes, dirname=dirname, filename=filename, ext=ext)
23:00:58      106
23:00:58      107     @ensure_env
23:00:58
23:00:58  rasterio/_io.pyx in rasterio._io.MemoryFileBase.__init__()
23:00:58
23:00:58  /opt/conda/envs/birdy/lib/python3.7/site-packages/xarray/core/common.py in __bool__(self)
23:00:58      127
23:00:58      128     def __bool__(self: Any) -> bool:
23:00:58  --> 129         return bool(self.values)
23:00:58      130
23:00:58      131     def __float__(self: Any) -> float:
23:00:58
23:00:58  ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
23:00:58
23:00:58  _ raven-master/docs/source/notebooks/Extract_geographical_watershed_properties.ipynb::Cell 10 _
23:00:58  Notebook cell execution failed
23:00:58  Cell 10: Cell execution caused an exception
23:00:58
23:00:58  Input:
23:00:58  unique, counts = numpy.unique(grid.sel(band=1), return_counts=True)
23:00:58  display(unique, counts)
23:00:58
23:00:58  # Pixels values at '127' are NaN and can be ignored.
23:00:58  plt.bar(unique[:-1], counts[:-1])
23:00:58  plt.show()
23:00:58
23:00:58  grid.where(grid != 127).sel(band=1).plot.imshow(cmap="tab20")
23:00:58  plt.show()
23:00:58
23:00:58  Traceback:
23:00:58
23:00:58  ---------------------------------------------------------------------------
23:00:58  AttributeError                            Traceback (most recent call last)
23:00:58  <ipython-input-11-a26c52126e9c> in <module>
23:00:58  ----> 1 unique, counts = numpy.unique(grid.sel(band=1), return_counts=True)
23:00:58        2 display(unique, counts)
23:00:58        3
23:00:58        4 # Pixels values at '127' are NaN and can be ignored.
23:00:58        5 plt.bar(unique[:-1], counts[:-1])
23:00:58
23:00:58  AttributeError: 'list' object has no attribute 'sel'
23:00:58
23:00:58  _ raven-master/docs/source/notebooks/Extract_geographical_watershed_properties.ipynb::Cell 11 _
23:00:58  Notebook cell execution failed
23:00:58  Cell 11: Cell execution caused an exception
23:00:58
23:00:58  Input:
23:00:58  import cartopy.crs as ccrs
23:00:58
23:00:58  # Set a CRS transformation:
23:00:58  crs = ccrs.LambertConformal(
23:00:58      central_latitude=49, central_longitude=-95, standard_parallels=(49, 77)
23:00:58  )
23:00:58
23:00:58  ax = plt.subplot(projection=crs)
23:00:58  grid.name = "Land Use Categories"
23:00:58  grid.where(grid != 127).sel(band=1).plot.imshow(ax=ax, transform=crs, cmap="tab20")
23:00:58  plt.show()
23:00:58
23:00:58  Traceback:
23:00:58
23:00:58  ---------------------------------------------------------------------------
23:00:58  AttributeError                            Traceback (most recent call last)
23:00:58  <ipython-input-12-f9fdb6f0e562> in <module>
23:00:58        7
23:00:58        8 ax = plt.subplot(projection=crs)
23:00:58  ----> 9 grid.name = "Land Use Categories"
23:00:58       10 grid.where(grid != 127).sel(band=1).plot.imshow(ax=ax, transform=crs, cmap="tab20")
23:00:58       11 plt.show()
23:00:58
23:00:58  AttributeError: 'list' object has no attribute 'name'
```
  • Loading branch information
tlvu authored Jun 15, 2021
2 parents 33c4766 + be75ee1 commit 8785a9e
Showing 1 changed file with 181 additions and 73 deletions.
Loading

0 comments on commit 8785a9e

Please sign in to comment.