patternpythonMinor
Organization of complex empirical equations
Viewed 0 times
complexequationsorganizationempirical
Problem
I am working on coding up a set of models that are based on empirical models. These models tend to be quite complicated. In some cases, the logic used by the equations makes it impossible to compute the results using numpy fast arrays. As a result, I have decided to use list comprehension to do the math. However, this results in the functions being indent 3 levels. Is this bad? Is there a better way to organize this code? Any other comments on the style or organization would be most welcome.
Currently, the code is organized with classes, which I think makes the most sense. Any opinions on this would be welcome too.
Here are a few examples of the code:
```
import logging
import math
import numpy as np
class Model(object):
'''Abstract class for ground motion prediction models.'''
INDICES_PSA = []
INDEX_PGA = None
INDEX_PGV = None
INDEX_PGD = None
LIMITS = dict()
def __init__(self, name, abbrev, **kwds):
'''Compute the response predicted the model. No default implementation.
Input names:
mag: float
moment magnitude of the event
dist_closest: float
closest distance to the rupture (km)
v_s30: float
time-averaged shear-wave velocity over the top 30 m of the site (m/s)
'''
super(Model, self).__init__()
self.name = name
self.abbrev = abbrev
self.parameters = kwds
self._ln_resp = None
self._ln_std = None
self._check_inputs(**self.parameters)
def interp_spec_accels(self, periods):
return np.interp(periods,
self.periods, self._ln_resp[self.INDICES_PSA])
@property
def periods(self):
return self.PERIODS[self.INDICES_PSA]
@property
def spec_accels(self):
return self._resp(self.INDICES_PSA)
@property
def pga(self):
'''Return the PGA [g]'''
return self._resp(self.INDEX_PGA)
@property
de
Currently, the code is organized with classes, which I think makes the most sense. Any opinions on this would be welcome too.
Here are a few examples of the code:
```
import logging
import math
import numpy as np
class Model(object):
'''Abstract class for ground motion prediction models.'''
INDICES_PSA = []
INDEX_PGA = None
INDEX_PGV = None
INDEX_PGD = None
LIMITS = dict()
def __init__(self, name, abbrev, **kwds):
'''Compute the response predicted the model. No default implementation.
Input names:
mag: float
moment magnitude of the event
dist_closest: float
closest distance to the rupture (km)
v_s30: float
time-averaged shear-wave velocity over the top 30 m of the site (m/s)
'''
super(Model, self).__init__()
self.name = name
self.abbrev = abbrev
self.parameters = kwds
self._ln_resp = None
self._ln_std = None
self._check_inputs(**self.parameters)
def interp_spec_accels(self, periods):
return np.interp(periods,
self.periods, self._ln_resp[self.INDICES_PSA])
@property
def periods(self):
return self.PERIODS[self.INDICES_PSA]
@property
def spec_accels(self):
return self._resp(self.INDICES_PSA)
@property
def pga(self):
'''Return the PGA [g]'''
return self._resp(self.INDEX_PGA)
@property
de
Solution
These models tend to be quite complicated.
Maybe that's standard in your field, but they definitely are complicated, and converting those equations into code will never be pretty (I'm looking at you,
However, this results in the functions being indent 3 levels. Is this bad?
No, it's not bad per se. You did an excellent job to keep the nesting under control with those helper functions, and the code is actually easy to read and follow.
Is there a better way to organize this code? Any other comments on the style or organization would be most welcome.
The code actually looks good. A few suggestions:
Maybe that's standard in your field, but they definitely are complicated, and converting those equations into code will never be pretty (I'm looking at you,
calc_ln_resp_ref!).However, this results in the functions being indent 3 levels. Is this bad?
No, it's not bad per se. You did an excellent job to keep the nesting under control with those helper functions, and the code is actually easy to read and follow.
Is there a better way to organize this code? Any other comments on the style or organization would be most welcome.
The code actually looks good. A few suggestions:
- Feel free to organize the different models into different files if your current file is going to continue growing.
- Add a docstring to the module (your existing docstrings in classes and methods are great).
- Get more confident and keep up the good job! ^_^
Context
StackExchange Code Review Q#36336, answer score: 4
Revisions (0)
No revisions yet.