Skip to content

Use position-based index to avoid reaggregation issues in lines_polys_handler#1002

Closed
luseverin wants to merge 4 commits intodevelopfrom
feature/fix_indexing_aggregation
Closed

Use position-based index to avoid reaggregation issues in lines_polys_handler#1002
luseverin wants to merge 4 commits intodevelopfrom
feature/fix_indexing_aggregation

Conversation

@luseverin
Copy link
Collaborator

@luseverin luseverin commented Feb 5, 2025

Changes proposed in this PR:

  • User-inputted exposures indices are ignored during re-aggregation phase of the lines_polys_handler module

This PR fixes #992

Hi!
As discussed in #992 , I tried to implement a quick fix to avoid erreoneous re-aggregation of disaggregated exposures to their original shape which was caused by non-unique index in the exposures GeoDataFrame. To fix it, I simply replaced the original indices by positional indices in the csr_mask used for the impact_mat reaggregation in _aggregate_impact_mat. This fix seems to do the job for this specific case but I am not entirely sure it works as intended as I was a bit confused about what was going on in the _aggregate_impact_mat function. For instance, why did we use the original index values to make the csr_mask as anyways the csr matrices work with positional indices to my understanding. Also, @peanutfun, you can let me know if this fix corresponds to the best solution you mentionned in #992 or not.

As a reminder, this is the code that used to create the error (showing up when plotting):

from shapely.geometry import Point, LineString, Polygon
import geopandas as gpd
gdf_test = gpd.GeoDataFrame
data = {
    "name": ["A", "B", "C"],
    "value": [10, 20, 30],
    "geometry": [
        Point(4, 51),            
        Point(4, 52),
        Point(4, 53)
    ],
}
gdf_test = gpd.GeoDataFrame(data, crs="EPSG:4326") 
gdf_test.index = [0,1,0] #need to specify non-unique index to cause the error!

#hazard and impf
from climada.util.api_client import Client
import climada.util.lines_polys_handler as u_lp
from climada.entity.impact_funcs import ImpactFuncSet
from climada.entity.impact_funcs.storm_europe import ImpfStormEurope
from climada.entity import Exposures
from climada.engine import ImpactCalc


haz = Client().get_hazard("storm_europe", name="test_haz_WS_nl", status="test_dataset")

#exposures
exp_line = Exposures(gdf_test)

#impact function
impf_line = ImpfStormEurope.from_welker()
impf_set = ImpactFuncSet([impf_line])

exp_line.data["impf_WS"] = 1  # specify impact function

#Break down calc_geom_impact
# disaggregate exposure
exp_pnt = u_lp.exp_geom_to_pnt(
    exp=exp_line,
    res=500,
    to_meters=True,
    disagg_met=u_lp.DisaggMethod.FIX,
    disagg_val=1e5,
)
exp_pnt.assign_centroids(haz)

# compute point impact
calc = ImpactCalc(exp_pnt, impf_set, haz)
impact_pnt = calc.impact(save_mat=True, assign_centroids=False)

# re-aggregate impact to original exposure geometry
impact_agg = u_lp.impact_pnt_agg(impact_pnt, exp_pnt.gdf, u_lp.AggMethod.SUM)

# plot
u_lp.plot_eai_exp_geom(impact_agg);

PR Author Checklist

PR Reviewer Checklist

@luseverin luseverin marked this pull request as draft February 5, 2025 09:37
@luseverin luseverin closed this Feb 5, 2025
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

Successfully merging this pull request may close these issues.

1 participant