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

Add impact classmethod to concatenate several impact objects #529

Merged
merged 58 commits into from
Jan 27, 2023

Conversation

raphael-portmann
Copy link
Collaborator

@raphael-portmann raphael-portmann commented Aug 8, 2022

UPDATES

  • now includes tutorial example
  • Now includes test
  • checks are defined as separate functions
  • ISSUE: due to the many checks, the code is rather complex

Changes proposed in this PR:

  • add a classmethod with two concatenation options to concatenate two or more impact objects with the same hazard type, frequency, and coordinate system that either (1) have the same exposure but contain events with different dates / time periods of the year (e.g. seasonal data) or (2) have the same event dates but a different exposure (e.g. two different countries)

To Do list

  • implement exposure concatenation option
  • make tests

Open questions

  • Is option (2) really needed or would one just merge the exposure before the impact calculation?
  • Is it a problem that, after concatenation, the data is not sorted by date (e.g. in the impact matrix and event ids)?

Pull Request Checklist

  • Correct target branch selected (if unsure, select develop)
  • Source branch up-to-date with target branch
  • Docs updated
  • Tests updated
  • Tests passing

Copy link
Member

@chahank chahank left a comment

Choose a reason for hiding this comment

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

Thanks @raphael-portmann ! This will come in very handy :D

For the moment, the code is still a bit raw. Please update the cosmetics (white spaces, lines, etc.) to make the code cleaner.

In addition, I suggest that you group all the checks into individual functions that can then be called. This will make the code much more readable and modular.

I also suggest that you now write test methods. Try also to do some testing with 'real data' from colleagues to find the hidden issues quickly.

climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
raphael-portmann and others added 16 commits August 8, 2022 15:04
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Chahan M. Kropf <[email protected]>
@emanuel-schmid emanuel-schmid marked this pull request as draft August 10, 2022 11:30
Parameters
----------
imp_list : list of climada.engine.Impact objects
list of Impact object to concatenate
Copy link
Member

Choose a reason for hiding this comment

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

I think your spacing for the docstrings are off.

raise ValueError(
"Not all impacts have a hazard type."
"Please specify the same hazard type for each impact."
"The impacts are incompatible and cannot be concatenated.")
Copy link
Member

Choose a reason for hiding this comment

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

I do not really like the sentence you added to all errors The impacts are incompatible and cannot be concatenated. What is that supposed to mean?

Copy link
Member

Choose a reason for hiding this comment

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

I second this. The error traceback shows that it occurs within the concat method, so there is no need to mention that it did not work as expected 😉

tot_value = imp_list[0].tot_value

#fill remaining attributes
tag = imp_list[0].tag
Copy link
Member

Choose a reason for hiding this comment

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

I still think it is not ok to discard all tags other than for the first instance of impact.

def test_concat_pass(self):
"""test concatenation of impacts"""

def test_haztype_unit_crs_attrs(self,imp1,imp2,imp3,imp3_3,imp3_2):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def test_haztype_unit_crs_attrs(self,imp1,imp2,imp3,imp3_3,imp3_2):
def test_haztype_unit_crs_attrs(self, imp1, imp2, imp3, imp3_3, imp3_2):

raise ValueError(
"Not all impacts have a hazard type."
"Please specify the same hazard type for each impact."
"The impacts are incompatible and cannot be concatenated.")
Copy link
Member

Choose a reason for hiding this comment

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

I second this. The error traceback shows that it occurs within the concat method, so there is no need to mention that it did not work as expected 😉

climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/test/test_impact.py Outdated Show resolved Hide resolved
climada/engine/test/test_impact.py Outdated Show resolved Hide resolved
Copy link
Member

@peanutfun peanutfun left a comment

Choose a reason for hiding this comment

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

Almost there! Please use "handmade" test data and check that concetation works based on the resulting values and not just the lengths of the arrays. Also, the docstring could explain a little bit better what's happing internally. Please make sure that the docs can be built and check the appearance of the docstring in the preview

climada/engine/test/test_impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
climada/engine/impact.py Outdated Show resolved Hide resolved
doc/tutorial/climada_engine_Impact.ipynb Outdated Show resolved Hide resolved
doc/tutorial/climada_engine_Impact.ipynb Outdated Show resolved Hide resolved
* Simplify concatenation of Impact attributes.
* Add 'frequency_unit' attribute to concatenation.
* Fix docstring formatting.
* Update tests to use fixtures and concat empty impact matrices.
* Format code using black.
@peanutfun peanutfun marked this pull request as ready for review January 27, 2023 14:53
@peanutfun
Copy link
Member

I reworked this a bit, tidying up the code and the docstring and improving the tests. Now even the linter is happy! Merging...

@peanutfun peanutfun merged commit d3f585b into develop Jan 27, 2023
@peanutfun
Copy link
Member

@raphael-portmann Thanks for your contribution! 🥳

@emanuel-schmid emanuel-schmid deleted the feature/impact-merge branch January 31, 2023 17:44
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.

4 participants