stevenpawley / Pyspatialml

Machine learning modelling for spatial data
GNU General Public License v3.0
145 stars 29 forks source link

TypeError: read() takes 1 positional argument but 2 were given + AttributeError: 'RasterLayer' object has no attribute 'seek' #48

Open dbaldig opened 1 year ago

dbaldig commented 1 year ago

Hey Steven, first of all, thanks a lot for your package...it's such a great help!

I followed along your Multi-Target Spatial Prediction using the Meuse Dataset tutorial to modify it for a RF implementation. I tried to work with a stacked raster dataset of mine but eventually always run in the same error. For example, when executing this code:

elevation = 'C:/Users/.../DEM_filled.tif'
slope = 'C:/Users/.../slope.tif'
roughness = 'C:/Users/.../roughness.tif'
tri = 'C:/Users/.../tri.tif'
tpi = 'C:/Users/.../tpi.tif'
ndvi = 'C:/Users/.../ndvi.tif'
annual_prec = 'C:/Users/.../annual_prec.tif'
aspect = 'C:/Users/.../aspect.tif'
etc. 

predictor_files = [elevation, slope, roughness, tri, tpi, ndvi, annual_prec, aspect, etc.]
stack = Raster(predictor_files)
stack.names

xy_layer = xy_coordinates(
    layer=stack.iloc[0], 
    file_path=NamedTemporaryFile(suffix=".tif").name
)

it returns an:


TypeError Traceback (most recent call last) TypeError: read() takes 1 positional argument but 2 were given Exception ignored in: 'rasterio._filepath.filepath_read' Traceback (most recent call last): File "[c:...new_venv\lib\site-packages\rasterio\io.py]", line 224, in open return DatasetReader(mempath, driver=driver, sharing=sharing, **kwargs) TypeError: read() takes 1 positional argument but 2 were given

AttributeError Traceback (most recent call last) File rasterio_filepath.pyx:141, in rasterio._filepath.filepath_seek()

AttributeError: 'RasterLayer' object has no attribute 'seek' Exception ignored in: 'rasterio._env.log_error' Traceback (most recent call last): File "rasterio_filepath.pyx", line 141, in rasterio._filepath.filepath_seek AttributeError: 'RasterLayer' object has no attribute 'seek'

Do you have an idea what could cause this? I made sure that all rasters have only one band

dbaldig commented 1 year ago

Sorry, I'm dealing with another issue. Let me know if you want me to open another issue seperately.

elevation = 'C:/Users/.../DEM_filled.tif' slope = 'C:/Users/.../slope.tif' roughness = 'C:/Users/.../roughness.tif' tri = 'C:/Users/.../tri.tif' tpi = 'C:/Users/.../tpi.tif' ndvi = 'C:/Users/.../ndvi.tif' annual_prec = 'C:/Users/.../annual_prec.tif' aspect = 'C:/Users/.../aspect.tif' etc.

predictor_files = [elevation, slope, roughness, tri, tpi, ndvi, annual_prec, aspect, etc.] stack = Raster(predictor_files) stack.names

stack.names returns the actual dict_keys for most of the rasters but as soon as I add annual_prec and/or "aspect" to predictor_files, it returns an empty "dict_keys([])". The path is correct and the resolution is also the same. Any idea why this could be? It seems like it's quite random, cause I had a third raster which didn't work at first, but works now after processing it just the same way as before.

Thanks a lot!

EDIT: So, I can stack the layers with rasterio and then load the stacked raster with pyspatialml. The bands are renamed by pyspatialml to stack_01, stack_02, etc. When I try to rename the the bands, I get the "read() takes 1 positional argument but 2 were given" and "read() takes 1 positional argument but 2 were given" errors but it eventually renames the names. In the end, I got the model running like that.

selvaje commented 1 year ago

Dear @dbaldig & @stevenpawley I'm getting the same error. I'm wondering if you find an alternative solution. Thanks Giuseppe

pyaj0 commented 1 year ago

Hi, same issue. Raster() used to work just fine last year but now, I get

TypeError:` 'NoneType' object is not subscriptable

selvaje commented 1 year ago

see at the bottom of this page for a solution https://spatial-ecology.net/docs/build/html/CASESTUDY/Tree_Height_03RF_pred.html?highlight=pyspatialml