Open simon-tarr opened 3 years ago
Ater some more testing (still within my Conda environment) I've discovered that other methods also don't work e.g. ls.compute_patch_area()
and ls.compute_class_metrics_df()
. The above runtime error is returned in all instances. However, more 'simple' metrics e.g. ls.total_area()
do work.
I will have to do some experimenting to see whether I can replicate this issue outside Conda.
The more testing I do, the less convinced I am that it's an issue with Conda/Azure.
I created a brand new Conda environment with Python 3.8.6 (I don't think pylandstats
is compatible with 3.9.x). With nothing else in the new environment except the default pkg installations (things like pip
etc) I installed pylandstats
with:
conda install -n geotest pylandstats
It took a long time to solve the environment, but it got there eventually!
I then opened up PyCharm, set the interpreter to my brand new conda environment (called 'geotest'). I ran the following:
import rioxarray as rxr
from pylandstats import Landscape
raster = rxr.open_rasterio('/Users/simontarr/Downloads/_ags_mapb92e5c0e36514569be92dd92aaf02298.tif')
print(dir(raster))
ls = Landscape(raster.values, res=(len(raster.x), len(raster.y)), nodata=0)
x = ls.compute_patch_metrics_df()
But received the error:
/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/rioxarray/_io.py:841: NotGeoreferencedWarning: Dataset has no geotransform, gcps, or rpcs. The identity matrix be returned.
warnings.warn(str(rio_warning.message), type(rio_warning.message))
Traceback (most recent call last):
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/pylandstats/landscape.py", line 420, in _patch_class_ser
return self._cached_patch_class_ser
AttributeError: 'Landscape' object has no attribute '_cached_patch_class_ser'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/pylandstats/landscape.py", line 393, in _num_patches_dict
return self._cached_num_patches_dict
AttributeError: 'Landscape' object has no attribute '_cached_num_patches_dict'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/simontarr/PycharmProjects/pythonProject/main.py", line 10, in <module>
x = ls.compute_patch_metrics_df()
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/pylandstats/landscape.py", line 2527, in compute_patch_metrics_df
metrics_dfs = [self._patch_class_ser]
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/pylandstats/landscape.py", line 423, in _patch_class_ser
np.concatenate([
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/pylandstats/landscape.py", line 424, in <listcomp>
np.full(self._num_patches_dict[class_val], class_val)
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/pylandstats/landscape.py", line 395, in _num_patches_dict
self._cached_num_patches_dict = {
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/pylandstats/landscape.py", line 396, in <dictcomp>
class_val: self.class_label(class_val)[1]
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/pylandstats/landscape.py", line 205, in class_label
return ndimage.label(self.landscape_arr == class_val,
File "/Users/simontarr/opt/miniconda3/envs/geotest/lib/python3.8/site-packages/scipy/ndimage/measurements.py", line 183, in label
raise RuntimeError('structure and input must have equal rank')
RuntimeError: structure and input must have equal rank
This can be replicated with the .tif file attached to this comment. example_tif.zip
Hey @simon-tarr ,
I was facing the same issue and looked into the source code a bit.
The issue for me was that my handed raster (as numpy.object) was of higher dimension than the variable structure in _measurements.py
allows. To be more specific: Only ndim
2 is allowed, while I handed ndim
3 which was added due to a masking I performed before handing the variable.
Therefore, maybe check the dimension of your object raster.values:
# Check the dimension
print(type(raster.values))
print(raster.values.shape)
print(raster.values.ndim)
if out_image.ndim >= 3:
raster.values = np.squeeze(raster.values)
Maybe this helps.
Description
Some preamble - I'm actually running
pylandstats
within a conda environment as I'm developing some code to run with Azure Functions. So I'm not sure if the below error is due to a problem with my conda environment, how Azure Functions is executing the code or whether it is actually a problem withpylandstats
.I'm posting this 'bug' here on the off-chance that someone may have seen this error outside of conda.
Please feel free to delete this bug/issue if it's not in any way related to
pylandstats
!Trying to run
compute_patch_metrics_df()
produces the following error:My code:
My
conda
environment has the following libs/packages installed:I have checked which dependencies
pylandstats
requires with:and can't see anything listed there which isn't included in my list of packages/libraries in my Conda environment.
Thanks!