-
Notifications
You must be signed in to change notification settings - Fork 125
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
Set all ImpactFunc attributes in __init__ #550
Conversation
def __init__( | ||
self, | ||
haz_type: str = "", | ||
id: Union[str, int] = "", | ||
intensity: Optional[np.ndarray] = None, | ||
mdd: Optional[np.ndarray] = None, | ||
paa: Optional[np.ndarray] = None, | ||
intensity_unit: str = "", | ||
name: str = "", | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the __init__
signature with all attributes of the class:
- Use the exact same parameter names as the class attributes
- Add default values for every parameter. For mutable types (objects that can be manipulated), choose
None
. Mutable default arguments are an anti-pattern! The default values will be set in the method body, see below. - Add type hints for each parameter. Import from the
typing
module if suitable. - Order the parameters by "importance". Think about which parameters are the most essential for the class and place them first in the signature.
Pro-Tip: The CLIMADA documentation contains a Developer Guide with tips and best-practices for Python code development!
"""Initialization. | ||
|
||
Parameters | ||
---------- | ||
haz_type : str, optional | ||
Hazard type acronym (e.g. 'TC'). | ||
id : int or str, optional | ||
id of the impact function. Exposures of the same type | ||
will refer to the same impact function id. | ||
intensity : np.array, optional | ||
Intensity values. Defaults to empty array. | ||
mdd : np.array, optional | ||
Mean damage (impact) degree for each intensity (numbers | ||
in [0,1]). Defaults to empty array. | ||
paa : np.array, optional | ||
Percentage of affected assets (exposures) for each | ||
intensity (numbers in [0,1]). Defaults to empty array. | ||
intensity_unit : str, optional | ||
Unit of the intensity. | ||
name : str, optional | ||
Name of the ImpactFunc. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the method docstring.
- If available, copy-paste the documentation of the class attributes (minding the correct order).
- Indicate which parameters are optional (these are the ones that do not have a default value).
- Explain the default values if they differ from the ones indicated in the signature (usually the ones with default value
None
)
self.id = id | ||
self.name = name | ||
self.intensity_unit = intensity_unit | ||
self.haz_type = haz_type | ||
# Followng values defined for each intensity value | ||
self.intensity = np.array([]) | ||
self.mdd = np.array([]) | ||
self.paa = np.array([]) | ||
self.intensity = intensity if intensity is not None else np.array([]) | ||
self.mdd = mdd if mdd is not None else np.array([]) | ||
self.paa = paa if paa is not None else np.array([]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set the class attributes from the parameters.
- For immutable types:
self.param = param
- For mutable types:
self.param = param if param is not None else default
inten_min, threshold, inten_max = intensity | ||
impf.intensity = np.array([inten_min, threshold, threshold, inten_max]) | ||
intensity = np.array([inten_min, threshold, threshold, inten_max]) | ||
paa_min, paa_max = paa | ||
impf.paa = np.array([paa_min, paa_min, paa_max, paa_max]) | ||
paa = np.array([paa_min, paa_min, paa_max, paa_max]) | ||
mdd_min, mdd_max = mdd | ||
impf.mdd = np.array([mdd_min, mdd_min, mdd_max, mdd_max]) | ||
mdd = np.array([mdd_min, mdd_min, mdd_max, mdd_max]) | ||
|
||
return impf | ||
return cls(id=impf_id, intensity=intensity, mdd=mdd, paa=paa) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the classmethod
s that initialize the class with different arguments than __init__
.
- Instead of setting the class attributes, define variables and pass them to the class constructor
cls
. - Use keyword arguments to "skip over" arguments that should remain defaulted.
- Prefer using the new constructor signature over setting the class arguments (which would still be valid)
intensity = np.arange(0, 100, 10) | ||
paa = np.arange(0, 1, 0.1) | ||
mdd = np.arange(0, 1, 0.1) | ||
imp_fun = ImpactFunc(intensity=intensity, paa=paa, mdd=mdd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the tests to use the new constructor signature.
intensity = np.linspace(0, 150, num=100) | ||
mdd = np.repeat(1, len(intensity)) | ||
paa = np.array([sigmoid_function(v, G, v_half, vmin, k) | ||
for v in intensity]) | ||
imp_fun = ImpactFunc(haz_type='TC', | ||
id=_id, | ||
intensity_unit='m/s', | ||
intensity=intensity, | ||
mdd=mdd, | ||
paa=paa) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update docs and tutorials explaining the usage of the class
🙌 |
Changes proposed in this PR:
ImpactFunc.__init__
to set all class attributes during initializationImpactFunc
classmethodsPR Author Checklist
develop
)PR Reviewer Checklist