Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 59 additions & 71 deletions rainymotion/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,21 @@ class Sparse:
Return 3D numpy array of shape (lead_steps, dim_x, dim_y).

"""
def __init__(self):

self.of_params = {'st_pars': dict(maxCorners=200, qualityLevel=0.2,
minDistance=7, blockSize=21),
'lk_pars': dict(winSize=(20, 20), maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0))}

self.extrapolation = "linear"

self.warper = "affine"

self.input_data = None

self.scaler = RYScaler

self.inverse_scaler = inv_RYScaler

self.lead_steps = 12
def __init__(self, extrapolation="linear", warper='affine', input_data=None,
scaler=RYScaler, inverse_scaler=inv_RYScaler, lead_steps=12,
of_params={'st_pars': dict(maxCorners=200, qualityLevel=0.2,
minDistance=7, blockSize=21),
'lk_pars': dict(winSize=(20, 20), maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0))}):

self.of_params = of_params
self.extrapolation = extrapolation
self.warper = warper
self.input_data = input_data
self.scaler = scaler
self.inverse_scaler = inverse_scaler
self.lead_steps = lead_steps

def run(self):
"""
Expand Down Expand Up @@ -378,24 +375,21 @@ class SparseSD:

"""

def __init__(self):

self.of_params = {'st_pars': dict(maxCorners=200, qualityLevel=0.2,
minDistance=7, blockSize=21),
'lk_pars': dict(winSize=(20, 20), maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0))}

self.extrapolation = "simple_delta"

self.warper = "affine"

self.input_data = None

self.scaler = RYScaler

self.inverse_scaler = inv_RYScaler

self.lead_steps = 12
def __init__(self, extrapolation="simple_delta", warper='affine',
input_data=None, scaler=RYScaler, lead_steps=12,
inverse_scaler=inv_RYScaler,
of_params={'st_pars': dict(maxCorners=200, qualityLevel=0.2,
minDistance=7, blockSize=21),
'lk_pars': dict(winSize=(20, 20), maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0))}):

self.of_params = of_params
self.extrapolation = extrapolation
self.warper = warper
self.input_data = input_data
self.scaler = scaler
self.inverse_scaler = inverse_scaler
self.lead_steps = lead_steps

def run(self):
"""
Expand Down Expand Up @@ -474,7 +468,8 @@ def _fill_holes(of_instance, threshold=0):
range(of_instance.shape[0]))

# source
coord_source_i, coord_source_j = coord_target_i[~zero_holes], coord_target_j[~zero_holes]
coord_source_i = coord_target_i[~zero_holes]
coord_source_j = coord_target_j[~zero_holes]
delta_x_source = of_instance[::, ::, 0][~zero_holes]
delta_y_source = of_instance[::, ::, 1][~zero_holes]

Expand Down Expand Up @@ -531,7 +526,9 @@ def _calculate_of(data_instance,

if method in ["Farneback", "SimpleFlow"]:
# variational refinement
delta = cv2.optflow.createVariationalFlowRefinement().calc(prev_frame, next_frame, delta)
delta = cv2.optflow.createVariationalFlowRefinement().calc(prev_frame,
next_frame,
delta)
delta = np.nan_to_num(delta)
delta = _fill_holes(delta)

Expand Down Expand Up @@ -653,7 +650,7 @@ class Dense:
Attributes
----------
input_data: 3D numpy array (frames, dim_x, dim_y) of radar data for
previous hours. "frames" dimension must be > 2.
previous hours. "frames" dimension must be > 2.

scaler: function, default=rainymotion.utils.RYScaler
Corner identification and optical flow algorithms require specific data
Expand Down Expand Up @@ -694,21 +691,17 @@ class Dense:

"""

def __init__(self):
def __init__(self, input_data=None, scaler=RYScaler, of_method='DIS',
direction='backward', advection='constant-vector',
interpolation='idw', lead_steps=12):

self.input_data = None

self.scaler = RYScaler

self.lead_steps = 12

self.of_method = "DIS"

self.direction = "backward"

self.advection = "constant-vector"

self.interpolation = "idw"
self.input_data = input_data
self.scaler = scaler
self.of_method = of_method
self.direction = direction
self.advection = advection
self.interpolation = interpolation
self.lead_steps = lead_steps

def run(self):
"""
Expand Down Expand Up @@ -755,7 +748,7 @@ class DenseRotation:
To run your nowcasting model you first have to set up a class instance
as follows:

`model = Dense()`
`model = DenseRotation()`

and then use class attributes to set up model parameters, e.g.:

Expand Down Expand Up @@ -813,21 +806,17 @@ class DenseRotation:

"""

def __init__(self):
def __init__(self, input_data=None, scaler=RYScaler, lead_steps=12,
of_method='DIS', direction='backward',
advection='semi-lagrangian', interpolation='idw'):

self.input_data = None

self.scaler = RYScaler

self.lead_steps = 12

self.of_method = "DIS"

self.direction = "backward"

self.advection = "semi-lagrangian"

self.interpolation = "idw"
self.input_data = input_data
self.scaler = scaler
self.lead_steps = lead_times
self.of_method = of_method
self.direction = direction
self.advection = advection
self.interpolation = interpolation

def run(self):
"""
Expand Down Expand Up @@ -905,11 +894,10 @@ class Persistence:

"""

def __init__(self):

self.input_data = None
def __init__(self, input_data=None, lead_steps=12):

self.lead_steps = 12
self.input_data = input_data
self.lead_steps = lead_steps

def run(self):
"""
Expand Down
8 changes: 4 additions & 4 deletions rainymotion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def rfl2dbz(z):
'''
.. based on wradlib.trafo.decibel function

.. z --> d
.. z --> dbz
'''
return 10. * np.log10(z)

Expand All @@ -96,8 +96,8 @@ def rfl2dbz(z):
X_dbz[X_dbz < 0] = 0

# MinMaxScaling
c1 = X_dbz.min()
c2 = X_dbz.max()
c1 = np.nanmin(X_dbz)
c2 = np.nanmax(X_dbz)

return ((X_dbz - c1) / (c2 - c1) * 255).astype(np.uint8), c1, c2

Expand All @@ -121,7 +121,7 @@ def dbz2rfl(d):
'''
.. based on wradlib.trafo.idecibel function

.. d --> z
.. dbz --> z
'''
return 10. ** (d / 10.)

Expand Down