I tried to compute class and landscape metrics on a ZonalAnalysis object.
What happened: Contagion (and other metrics that rely on the adjacency matrix) produced a key error when self._adjacency_df.groupby(level=1, sort=False).sum()[i] is called.
What went wrong: self._adjacency_df.groupby(level=1, sort=False) does not produce the expected output.
What I expected to happen: Somehow the adjacency_df is malformatted, but I could not figure out what is wrong with it. I managed to "repair" it by converting it to a dict and back with adjacency_df = pd.DataFrame.from_dict(adjacency_df.to_dict()).
What I Did
import numpy as np
import pylandstats as pls
masks, values = GEMEINDEN_raster.masks() # custom function
ls = pls.Landscape(LANDCOVER, nodata=0)
za = pls.ZonalAnalysis(ls, masks_arr=masks, attribute_values=values)
ls_metric_df = za.compute_landscape_metrics_df(metrics=['contagion'])
KeyError: 1
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\envs\woelt_basic\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
~\AppData\Local\Continuum\anaconda3\envs\woelt_basic\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
~\AppData\Local\Continuum\anaconda3\envs\woelt_basic\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
C:\Users\FLORIA~1.FRO\AppData\Local\Temp/ipykernel_7688/415773056.py in <module>
16 za = pls.ZonalAnalysis(ls, masks_arr=masks, attribute_values=values)
17
---> 18 ls_metric_df = za.compute_landscape_metrics_df(metrics=['contagion'])
~\AppData\Local\Continuum\anaconda3\envs\woelt_basic\lib\site-packages\pylandstats\multilandscape.py in compute_landscape_metrics_df(self, metrics, metrics_kws)
226 self.landscapes):
227 landscape_metrics_df.loc[attribute_value, columns] = \
--> 228 landscape.compute_landscape_metrics_df(
229 metrics,
230 metrics_kws=metrics_kws).iloc[0]
~\AppData\Local\Continuum\anaconda3\envs\woelt_basic\lib\site-packages\pylandstats\landscape.py in compute_landscape_metrics_df(self, metrics, metrics_kws)
2674 metric_kws = {}
2675
-> 2676 metrics_dict[metric] = getattr(self, metric)(**metric_kws)
2677
2678 except AttributeError:
~\AppData\Local\Continuum\anaconda3\envs\woelt_basic\lib\site-packages\pylandstats\landscape.py in contagion(self, percent)
2436 for i in self.classes:
2437 p_i = np.sum(self._get_patch_area_ser(i)) / self.landscape_area
-> 2438 g_i = self._adjacency_df.groupby(level=1, sort=False).sum()[i]
2439 # print(g_i)
2440 g_i_sum = np.sum(g_i)
~\AppData\Local\Continuum\anaconda3\envs\woelt_basic\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3453 if self.columns.nlevels > 1:
3454 return self._getitem_multilevel(key)
-> 3455 indexer = self.columns.get_loc(key)
3456 if is_integer(indexer):
3457 indexer = [indexer]
~\AppData\Local\Continuum\anaconda3\envs\woelt_basic\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 1
Description
I tried to compute class and landscape metrics on a ZonalAnalysis object.
What happened: Contagion (and other metrics that rely on the adjacency matrix) produced a key error when
self._adjacency_df.groupby(level=1, sort=False).sum()[i]
is called.What went wrong:
self._adjacency_df.groupby(level=1, sort=False)
does not produce the expected output.What I expected to happen: Somehow the adjacency_df is malformatted, but I could not figure out what is wrong with it. I managed to "repair" it by converting it to a dict and back with
adjacency_df = pd.DataFrame.from_dict(adjacency_df.to_dict())
.What I Did