Skip to content

Possible bug in impact calculation #933

@aleeciu

Description

@aleeciu

Describe the bug
I noticed a weird behavior in the impact calculation. Inputting a valid exposure, hazard and impact function set leads to:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

the error occurs at this line, i.e. when looping the impact matrix generator. After spending few hours, I still couldn't figure out what conditions lead to the error.

The code below reproduces it using one exposure point, one centroid and two hazard events. Interestingly enough, using an exposure with twice the same point makes it run just fine.

To Reproduce
Run the code below.

Code example:

import numpy as np
import pandas as pd
from scipy import sparse
import geopandas as gpd
from shapely.geometry import Point

from climada.entity import Exposures
from climada.entity.impact_funcs import ImpactFuncSet, ImpfTropCyclone
from climada.engine import ImpactCalc
from climada.hazard import Centroids, Hazard

intensity = np.array([[31.5], [19.0]])
centroids = Centroids.from_lat_lon([-26.16], [28.20])

tc = Hazard(
    intensity=sparse.csr_matrix(intensity),
    event_id=np.arange(2),
    event_name=[0,1],
    frequency=np.ones(2) / 2,
    fraction=sparse.csr_matrix(np.zeros((2,1))),
    date=np.array([736018, 736019]),
    centroids=centroids,
    haz_type='TC'
)

impf_tc = ImpfTropCyclone.from_emanuel_usa()
impf_set = ImpactFuncSet([impf_tc])
impf_set.check()

exposure = gpd.GeoDataFrame(
    {'value': 1., 'geometry': [Point(28.22, -26.17)]}, crs="EPSG:4326"
)
exposure['latitude'] = exposure.geometry.y
exposure['longitude'] = exposure.geometry.x
exposure['impf_TC'] = 1

exp = Exposures(exposure)
#exp = Exposures(pd.concat([exposure, exposure]).reset_index()) # Use this and it works

impact = ImpactCalc(exp, impf_set, tc).impact(save_mat=True)

Expected behavior
The expected behaviour would be to get an impact object, the actual result is:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

Climada Version: 4.1.2.dev0

System Information (please complete the following information):

  • Operating system and version: macOS Sonoma 14.6
  • Python version: 3.11.6

Any idea/clue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting pull requestContribute by raising a pull request to resolve this issue!bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions