stevenpawley / Pyspatialml

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

Raster.rename creates extra names #32

Closed aazuspan closed 3 years ago

aazuspan commented 3 years ago

Hi @stevenpawley,

I came across some strange behavior in the Raster.rename method. If you attempt to rename the bands of a Raster, extra default band names will be created. In other words, if you rename 3 bands, you'll end up with 9 bands: 3 correctly named bands and 6 new bands.

import rasterio, pyspatialml, numpy as np

# Create a fake 3-band image for testing
arr = np.random.rand(3, 64, 64)
with rasterio.open("test.tif", "w", width=64, height=64, count=3, dtype=np.float32) as dst:
    dst.write(arr)

r = pyspatialml.Raster("test.tif")

renamed = r.rename(dict(zip(r.names, ["Red", "Green", "Blue"])))
>>> r.names
['test_1', 'test_2', 'test_3']

>>> renamed.names
['Red', 'Green', 'Blue', 'test_4', 'test_5', 'test_6', 'test_7', 'test_8', 'test_9']

I'm running the latest Pyspatialml installed from Github.

stevenpawley commented 3 years ago

Thanks for reporting this; probably an issue caused by some recent refactoring.

stevenpawley commented 3 years ago

Should be fixed now, thanks for reporting this