Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with landscape.Landscape._adjacency_df #16

Open
ffrosch opened this issue Aug 12, 2021 · 0 comments
Open

Error with landscape.Landscape._adjacency_df #16

ffrosch opened this issue Aug 12, 2021 · 0 comments

Comments

@ffrosch
Copy link

ffrosch commented Aug 12, 2021

  • PyLandStats version: 2.3.0
  • Python version: 3.8.10
  • Operating System: Windows

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.

Output Adjacency DF
Output Adjacency DF GroupBy Sum

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()).

Output Adjacency DF Repaired
Output Adjacency DF Repaired GroupBy Sum

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant