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

added assign_centroids for impact objects #602

Merged
merged 36 commits into from
Mar 23, 2023

Conversation

timschmi95
Copy link
Collaborator

@timschmi95 timschmi95 commented Dec 9, 2022

Changes proposed in this PR:

  • added util function assign_gdf_centroids to assign hazard centroids to any geodataframe
  • wrapper for impact object to assign hazard centroids to impact objects

This PR supports the calibration of impact functions based with damage data from impact objects

PR Author Checklist

PR Reviewer Checklist

@chahank chahank requested review from aleeciu and removed request for chahank December 9, 2022 13:48
@aleeciu
Copy link
Collaborator

aleeciu commented Dec 13, 2022

The code is quite readable.

I think it can be merged after a more thorough look at the doc string (I pushed few suggestions and made few comments).

Also, it'd be good to have a coupe of tests.

@timschmi95
Copy link
Collaborator Author

@aleeciu I added a test for the imp.assign_centroids() method, and all tests are passing. Shall I covert it to a 'real' pull request?

@timschmi95 timschmi95 marked this pull request as ready for review January 19, 2023 10:57
@timschmi95 timschmi95 self-assigned this Jan 19, 2023
@emanuel-schmid
Copy link
Collaborator

Thanks a lot! The time for refactoring centroids assignment is just right I think. 😁
I suggest to first change the signature and behavior of the util.coordinates.assign_haz_centroids function and add some unittests that take its whole argument space into account.

climada/util/coordinates.py Outdated Show resolved Hide resolved
climada/util/coordinates.py Outdated Show resolved Hide resolved
climada/util/coordinates.py Outdated Show resolved Hide resolved
climada/util/coordinates.py Outdated Show resolved Hide resolved
@emanuel-schmid emanuel-schmid marked this pull request as draft February 7, 2023 12:01
@timschmi95
Copy link
Collaborator Author

@emanuel-schmid I think everything is resolved now. Do you want to have a last look over it before I merge it?

Comment on lines 1766 to 1768
#Assert that the imp crs is epsg:4326, as it is required by the u_coord methods
if not u_coord.equal_crs(self.crs, 'EPSG:4326'):
raise ValueError('Set Impact to lat/lon crs (EPSG:4326)!')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more question (sorry 😁) - which u_coord methods require epsg:4326?
And why can't they speak for themselves, i.e. raise an exception if the crs is wrong?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this in there as afterwards I label the coordinate column 'latitude' and 'longitude', but you are right: as with exposures the functionalities also work with other CRS, it's just the column labels 'latitude'/'longitude' that are not correct then, but this shouldn't be a problem. I think we can savely remove this part.

Copy link
Collaborator

@emanuel-schmid emanuel-schmid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no real assignment anymore, the method name is kind of misleading.
My favorite would be closest_centroids. But then, the same argument is also valid for the coordinates methods assign_centroids_to_gdf, assign_gridpoints and assign_coordinates. Consequently they should be renamed too. Therefore I propose a wimp compromise: assigned_centroids as in "if there was an assignment the assigned centroids would be assigned like this:"

And CHANGELOG.md needs to be updated ... 😁

climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
fake_aai_agg = np.sum(fake_eai_exp)
imp = Impact.from_eih(exp, ENT.impact_funcs, HAZ,
fake_at_event, fake_eai_exp, fake_aai_agg)
imp_centr = imp.assign_centroids(HAZ)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
imp_centr = imp.assign_centroids(HAZ)
imp_centr = imp.assigned_centroids(HAZ)

climada/util/coordinates.py Show resolved Hide resolved
climada/util/coordinates.py Show resolved Hide resolved
@chahank
Copy link
Member

chahank commented Mar 20, 2023

Adding a method assigned_centroids sounds like a terrible idea to me, sorry. This makes just a very confusing naming. What is the purpose of this new method?

The information about the closest centroid from a hazard of an impact object is already in the exposures at the moment of computation.

I really begin to be confused about the purpose of this pull request.

@emanuel-schmid
Copy link
Collaborator

With regard to Impact.assign_centroids - it's for those cases where you still have the impact, but not the exposures anymore I reckon. (?)
Apart from the Impact.asssign_centroids method it's a nice decomposition of Exposures.assign_centroids.

@chahank
Copy link
Member

chahank commented Mar 20, 2023

@timschmi95 : could you please clarify the use case of the methods impact.assign_centroids ?

@timschmi95
Copy link
Collaborator Author

timschmi95 commented Mar 20, 2023

@chahank The impact.assign_centroids is used for the spatially explicit calibration, which we do for hail damages. I.e. the reported damages are read into an impact object, and assigned hazard intensity values for each reported impact allow for a spatially explicit calibration.

It could also be used by if someone is just interested in what hazard intensity led to which (modelled) impacts. Ofc if the imp is produced with ImpactCalc().impact() one could also use the exp.assign_centroids() method instead, but the impact.assign_centroids woud be more direct

@timschmi95
Copy link
Collaborator Author

@emanuel-schmid I am also not sure about the proposed name impact.assigned_centroids.
As you pointed out with the newest change the assigned centroids are not stored in the impact object anymore. Nevertheless, they are assigned to each impact coordinate point (just not stored within the imp object), thus I would vote to keep the name as impact.assign_centroids. Or if preferred I think impact.closest_centroids also works well, even if within the function it then uses the ``assign_centroids_to_gdf``` function.

@emanuel-schmid
Copy link
Collaborator

Many thanks @timschmi95
I've changed all the misleading assign_ names to match_ (with the exception of Exposures.assign_centroids, of course) and simplified the Impact.match_centroids method somewhat.
Now I'm happy. If @chahank approves, we can merge.

@timschmi95
Copy link
Collaborator Author

Thanks @emanuel-schmid the new names are also fine with me! And the use of _build_exp() also makes the method shorter :)

@chahank
Copy link
Member

chahank commented Mar 23, 2023

I still think that there should not be any method impact.macht_centroids. Since it is a single liner and it was only introduced to be used in the calibration module, I would remove it entirely. The pull request was nevertheless very useful as the utility methods were much improved.

@chahank
Copy link
Member

chahank commented Mar 23, 2023

That being said, if you really think it is useful, please feel free to merge.

@timschmi95 timschmi95 merged commit 1e2c99d into develop Mar 23, 2023
@timschmi95 timschmi95 deleted the feature/assign_centroids_impact branch March 23, 2023 20:27
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.

None yet

4 participants