Skip to content

Conversation

@ValentinGebhart
Copy link
Collaborator

@ValentinGebhart ValentinGebhart commented Jul 1, 2025

Changes proposed in this PR:

  • Removed hard-coded intensity_thres attribute from Hazard class (including minor changes in some local exceedance functions that made use of this attribute).
  • The attribute still exists in the classes StormEurope and TropCyclone, as it is used in data storing there. The handling of the attribute was different in both classes, which I have adapted such that both now use the handling as inTropCyclone.

This PR fixes #1007.

PR Author Checklist

PR Reviewer Checklist

@ValentinGebhart ValentinGebhart changed the title Feature/remove intensity thres Remove intensity_thres attribute from Hazard class Jul 1, 2025
@ValentinGebhart ValentinGebhart changed the base branch from main to develop July 1, 2025 08:42
@ValentinGebhart
Copy link
Collaborator Author

ValentinGebhart commented Jul 1, 2025

@chahank: As we discussed, I now removed the intensity_thres attribute from the Hazard class. I kept it in the StormEurope and TropCyclone classes as it seems useful there. However, if you think it makes more sense to set the default to 0 instead of 17.5 for cyclones and 14.6 for winter storms, we can also do that.

@chahank
Copy link
Member

chahank commented Jul 1, 2025

Great! Does setting the value to 0 change some results and/or impact performance?

@ValentinGebhart
Copy link
Collaborator Author

Great! Does setting the value to 0 change some results and/or impact performance?

I did not check but I assume yes: Having values between e.g. 0 and 17.5 means there are many more nonzero entries in the sparse intensity matrix, and thus also in saved hazard files, right? I guess also the results would be marginally changed, depending on the impact function.

@chahank
Copy link
Member

chahank commented Jul 1, 2025

ok, then we should probably leave it as is in the TC and winterstorm models. Maybe we can add a note in the docstring somewhere to make sure people know about it?

return_periods=(25, 50, 100, 250),
method="interpolate",
min_intensity=None,
min_intensity=0,
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that all sparse data is projected onto a dense matrix? This might lead to problems no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The calculation of local exceedance intensity is centroid wise, so yes, the sparse matrix is converted to a dense matrix column by column. The min_intensity parameter being zero or positive does not change this.

It might be that a positive min_intensity parameter here would result in larger geodataframes (ie. centroids with only very small intensities would still lead to a computation and nonzero values, instead of faster handling if all intensities are considered zero). However, as we do not have a minimum intensity for a general hazard object, I think this should be set to zero per default, and can be adapted by the user if wanted.

Does this answer your question?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, thanks!

@ValentinGebhart
Copy link
Collaborator Author

ok, then we should probably leave it as is in the TC and winterstorm models. Maybe we can add a note in the docstring somewhere to make sure people know about it?

Sounds good. We could add some info to the docstring of the two classes (StormEurope and TropCyclone)? Or do you mean the (class) methods that make use of these attributes? These should already have some info in the docstrings.

@chahank
Copy link
Member

chahank commented Jul 1, 2025

Sounds good. We could add some info to the docstring of the two classes (StormEurope and TropCyclone)?

This sounds good.

Or do you mean the (class) methods that make use of these attributes? These should already have some info in the docstrings.

@ValentinGebhart
Copy link
Collaborator Author

@chahank should we merge this?

Comment on lines 95 to 96
intensity_thres = DEF_INTENSITY_THRES
"""Intensity threshold for storage in m/s."""
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the late extra comment, but is the class attribute then still needed? It looks like the intensity_thresh is now a parameter of all relevant methods no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, intensity_thresh is an optional parameter of all relevant methods. This class attribute is used as the default values for these methods. If you change the attribute for a specific instance, the default value of all corresponding methods changes, and you do not have to change it manually each time.

Does this make sense? If there is a smarter implementation, happy to change this.

Copy link
Member

Choose a reason for hiding this comment

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

It makes sense, although I am not sure the current implementation is doing what you want.

The code now has an class attribute, but actually never uses it, since the value of this attribute is always passed as parameter to all relevant methods. Thus, the class attribute seems redundant to me. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, almost agree. There is one place in StormEurope and one in TropCyclone where the correpsonding class attributes are used (called on class instances though):
In StormEurope: in the function calc_ssi
In TropCyclone: in the function set_from_tracks which has a deprecation warning.

It looks to me as if we can remove these uses and then also remove the class attributes. I have to think about the calc_ssi though. If you confirm that it is better to remove them, I can try to implement that :)

Copy link
Member

Choose a reason for hiding this comment

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

How about we add the parameter to the methods calc_ssi, remove the method set_from_tracks, adjust the method from_tracks (remove the haz.intensity_thres = intensity_thres), and remove the class parameter?

Copy link
Collaborator Author

@ValentinGebhart ValentinGebhart Jul 22, 2025

Choose a reason for hiding this comment

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

I removed the class attribute from StormEurope and TropCyclone, and the deprecated TropCyclone.set_from_tracks. In the StormEurope.calc_ssi method, there was already an optional parameter threshold for the calculation of SSI. Before, the method checked if this parameter was larger than the class attribute intensity_thres and, if not, gave an error "threshold cannot be below threshold upon read_footprint". I don't understand why it should be a problem though if the calculation threshold is below the read-in threshold...

Let me know what you think, for me this looks good.

@ValentinGebhart
Copy link
Collaborator Author

I have tried to check if there there is any problem with writing or reading "old" hazard files after removing the intensity_thres class attribute. I realized that, in the old version,

  • the write_hdf5 method only saves instance attributes, not class attributes. Thus, the intensity_thres was only stored if it was changed manually. The default class attribute was not stored.
  • the from_hdf5 method did not read any intensity_thres attribute, even if it was written to the file. This is because the method first creates an empty instance of the corresponding class (e.g. StormEurope) and then reads all attributes of the instance (but NOT class attributes).

This is code to see this in action, use e.g., main or develop branches. The first print command resulted in 18., the second in 14.7 (the default class attribute).

from climada.hazard import StormEurope
from climada.util.constants import WS_DEMO_NC

storm_instance = StormEurope.from_footprints(WS_DEMO_NC)
storm_instance.intensity_thres = 18.  # change intensity threshold
print(storm_instance.intensity_thres)

storm_instance.write_hdf5('storm_with_attribute.h5')
storm_instance = StormEurope.from_hdf5('storm_with_attribute.h5')
print(storm_instance.intensity_thres)

Of course, in the new version, there is simply no intensity_thres attribute anymore. I assume that it should not create any problems, as 1) it was usually never saved, and 2) even if it was written before to the file, it was usually never read.

@chahank Would this be enough check and can we merge?

@chahank
Copy link
Member

chahank commented Jul 28, 2025

I think we just need to adjust the docstring to remove the class attribute mention.

Also, 3 tests are failing, one for the supply chain module and one for the unsequa module. Not sure if this is related atm.

@chahank
Copy link
Member

chahank commented Jul 28, 2025

Also, 3 tests are failing, one for the supply chain module and one for the unsequa module. Not sure if this is related atm.

The supply chain one looks unrelated; there is no hazard involved as far as I can see. For unsequa it could be due to the reading of the tropical cyclone datasets tc_fl_1990_2004.h5.

Co-authored-by: Chahan M. Kropf <chahan.kropf@usys.ethz.ch>
@ValentinGebhart
Copy link
Collaborator Author

Thanks for checking the docstrings, this is now up to date. For the test, I don't know, for me locally it runs through.

@chahank
Copy link
Member

chahank commented Jul 28, 2025

Thanks for checking the docstrings, this is now up to date. For the test, I don't know, for me locally it runs through.

Ok, let's wait for the last round of tests. Do also the integration tests work for you locally?

@ValentinGebhart
Copy link
Collaborator Author

To me the failing test don't seem related to the changes, right? And when running the integration tests locally I get a few errors but they seem all to be related to LitPop.

@chahank
Copy link
Member

chahank commented Jul 30, 2025

I do not see why the failures would be related to litpop, as the build seem to fail before the tests even start. But I agree that this looks not related. I would thus merge.

@ValentinGebhart
Copy link
Collaborator Author

ValentinGebhart commented Jul 31, 2025

Ok great, thanks! Yes, I was saying that when running the integration tests locally there is some issue with Litpop data, but maybe that is some problem due to my local setup etc

@ValentinGebhart ValentinGebhart merged commit ac1f537 into develop Jul 31, 2025
14 of 16 checks passed
@ValentinGebhart ValentinGebhart deleted the feature/remove_intensity_thres branch July 31, 2025 07:37
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.

3 participants