stevenpawley / Pyspatialml

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

UnboundLocalError: local variable 'xys' referenced before assignment #41

Closed pyaj0 closed 1 year ago

pyaj0 commented 2 years ago

Hi @stevenpawley, thanks for this nice library!

I am having an issue with the extract_vector function

Issue

df_points = stack.extract_vector(training_pt)

# error output
UnboundLocalError                         Traceback (most recent call last)
Input In [22], in <module>
----> 1 df_points = stack.extract_vector(training_pt)

File ~\anaconda3\envs\gisml\lib\site-packages\pyspatialml\raster.py:2207, in Raster.extract_vector(self, gdf, return_array, progress)
   2205 # extract raster pixels
   2206 dtype = np.find_common_type([np.float32], self.dtypes)
-> 2207 X = np.ma.zeros((xys.shape[0], self.count), dtype=dtype)
   2209 for i, (layer, pbar) in enumerate(zip(self.iloc,
   2210                                       tqdm(self.iloc, total=self.count,
   2211                                            disable=not progress))):
   2212     sampler = sample_gen(dataset=layer.ds, xy=xys, indexes=layer.bidx,
   2213                          masked=True)

UnboundLocalError: local variable 'xys' referenced before assignment

Context

# select predictors
predictors = ['elevation.tif', 'aspect.tif', 'slope.tif', 'tpi.tif', 'tri.tif', 'pr.tif', 'temp.tif', 'hydrogeo.tif', 'river.tif', 'lu.tif']

# stack predictors
stack = Raster(predictors)

# check stack names
stack.names

# output
['elevation',
 'aspect',
 'slope',
 'tpi',
 'tri',
 'pr',
 'temp',
 'hydrogeo',
 'river',
 'lu']

# load disaster location shapefile
training_pt = gpd.read_file('../location_disaster/locations.shp')

# see training points
print(training_pt)

# output
     class                   geometry
0        0   POINT (-1.57129 6.98642)
1        0   POINT (-0.96799 9.91261)
2        0  POINT (-0.75949 10.56098)
3        0   POINT (-1.22277 7.30034)
4        0   POINT (-2.74765 6.27622)
..     ...                        ...
445      2   POINT (-2.77501 5.72500)
446      2   POINT (-1.87500 5.67500)
447      2   POINT (-2.87500 5.87500)
448      2   POINT (-2.92500 6.27500)
449      2   POINT (-3.17500 6.27500)

[450 rows x 2 columns]
stevenpawley commented 2 years ago

Sorry for the delayed response. Is this using the dev version or a version installed from pip?

pyaj0 commented 2 years ago

Hi sorry I sampled to rasters with another method. But it would still be nice if we find a solution to this issue. I installed from pip

stevenpawley commented 2 years ago

If you try installing the package directly form GitHub then I think that this issue may have already been fixed