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 in landscape.compute_total_adjacency_df() #22

Closed
cisluis opened this issue May 9, 2023 · 1 comment
Closed

Error in landscape.compute_total_adjacency_df() #22

cisluis opened this issue May 9, 2023 · 1 comment

Comments

@cisluis
Copy link

cisluis commented May 9, 2023

  • PyLandStats version: 2.4.2
  • Python version: 3.8.16
  • Operating System: macOS 13.3.1

Description

When running the compute_landscape_metrics_df() function errors for entropy (and other landscape-level metrics) is thrown out: "entropy cannot be computed at the landscape level"

What I Did

plsobj = pls.Landscape(data.astype(int), res=(1, 1), neighborhood_rule='8')

kws = {'total_area': {'hectares': False},
           'edge_density': {'hectares': False},
           'perimeter_area_ratio': {'hectares': False},
           'patch_density': {'hectares': False}}
df = plsobj.compute_landscape_metrics_df(metrics_kws=kws).T.squeeze()

this would throw the error for ANY data and it works if I chose not to calculate any of the diversity measures (like entropy, contagion, etc).

I figured that in the landscape.py library the function compute_total_adjacency_df() was returning a data frame of objects instead of integers (as it should because this is an array of adjacency counts), and then when entropy() uses this function to calculate counts per class, which then is passed to compute_entropy(), the calculation fails because the values in the list aren't numeric, and hence the statement

entropy = -np.sum(pcounts * np.log(pcounts))

throws an error at the numpy log command.

I fixed this problem by forcing the return of compute_total_adcacency_df() to be a data frame of integers:

def compute_total_adjacency_df(self):
        """
        Compute the total adjacency (vertical and horizontal) data frame.

        Returns
        -------
        adjacency_df: pandas.DataFrame
            Adjacency data frame with total adjacencies (vertical and horizontal).
        """
        # first `groupby` and `sum` to sum vertical and horizontal adjacencies
        # (first-level index), then `loc` to overlook the nodata row/column
        df = self._adjacency_df.groupby(level=1).sum().loc[self.classes, self.classes]
        return df.astype('int')

You should consider revising this simple error for the next update

@martibosch
Copy link
Owner

Hello,

sorry for the delay in my response. Thank you for reporting this bug, I believe it should be fixed by 92aa1a7. You can try it out in the v3.0.0rc.

Martí

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

2 participants