Skip to content

Commit 67e4af8

Browse files
authored
Merge branch 'openradar:main' into leise_filter
2 parents 9f32eeb + d1c2a4c commit 67e4af8

7 files changed

Lines changed: 235 additions & 165 deletions

File tree

pydda/cost_functions/_cost_functions_jax.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def calculate_smoothness_gradient(
267267
return y.flatten()
268268

269269

270-
def calculate_point_cost(u, v, x, y, z, point_list, Cp=1e-3, roi=500.0):
270+
def calculate_point_cost(u, v, x, y, z, point_list, Cp=1e-3):
271271
"""
272272
Calculates the cost function related to point observations. A mean square error cost
273273
function term is applied to points that are within the sphere of influence
@@ -305,14 +305,15 @@ def calculate_point_cost(u, v, x, y, z, point_list, Cp=1e-3, roi=500.0):
305305
"""
306306
J = 0.0
307307
for the_point in point_list:
308-
the_box = jnp.logical_and(
309-
jnp.logical_and(
310-
jnp.abs(x - the_point["x"]) < roi, jnp.abs(y - the_point["y"]) < roi
311-
),
312-
jnp.abs(z - the_point["z"]) < roi,
308+
dist = jnp.sqrt(
309+
(x - the_point["x"]) ** 2
310+
+ (y - the_point["y"]) ** 2
311+
+ (z - the_point["z"]) ** 2
313312
)
314-
the_box = jnp.where(the_box, 1.0, 0.0)
315-
J += jnp.sum(((u - the_point["u"]) ** 2 + (v - the_point["v"]) ** 2) * the_box)
313+
dist = jnp.maximum(dist, 1.0)
314+
weight = 1 / dist**2
315+
weight = weight / jnp.sum(weight)
316+
J += jnp.sum(weight * ((u - the_point["u"]) ** 2 + (v - the_point["v"]) ** 2))
316317

317318
return J * Cp
318319

@@ -358,18 +359,16 @@ def calculate_point_gradient(u, v, x, y, z, point_list, Cp=1e-3, roi=500.0):
358359
gradJ_w = jnp.zeros_like(u)
359360

360361
for the_point in point_list:
361-
the_box = jnp.where(
362-
jnp.logical_and(
363-
jnp.logical_and(
364-
np.abs(x - the_point["x"]) < roi, np.abs(y - the_point["y"]) < roi
365-
),
366-
np.abs(z - the_point["z"]) < roi,
367-
),
368-
1.0,
369-
0.0,
362+
dist = jnp.sqrt(
363+
(x - the_point["x"]) ** 2
364+
+ (y - the_point["y"]) ** 2
365+
+ (z - the_point["z"]) ** 2
370366
)
371-
gradJ_u += 2 * (u - the_point["u"]) * the_box
372-
gradJ_v += 2 * (v - the_point["v"]) * the_box
367+
dist = jnp.maximum(dist, 1.0)
368+
weight = 1 / dist**2
369+
weight = weight / jnp.sum(weight)
370+
gradJ_u += 2 * (u - the_point["u"]) * weight
371+
gradJ_v += 2 * (v - the_point["v"]) * weight
373372

374373
gradJ = jnp.stack([gradJ_u, gradJ_v, gradJ_w], axis=0).flatten()
375374
return gradJ * Cp

pydda/cost_functions/_cost_functions_numpy.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def calculate_smoothness_gradient(
303303
return y.flatten()
304304

305305

306-
def calculate_point_cost(u, v, x, y, z, point_list, Cp=1e-3, roi=500.0):
306+
def calculate_point_cost(u, v, x, y, z, point_list, Cp=1e-3, power=2):
307307
"""
308308
Calculates the cost function related to point observations. A mean square error cost
309309
function term is applied to points that are within the sphere of influence
@@ -339,18 +339,17 @@ def calculate_point_cost(u, v, x, y, z, point_list, Cp=1e-3, roi=500.0):
339339
for the_point in point_list:
340340
# Instead of worrying about whole domain, just find points in radius of influence
341341
# Since we know that the weight will be zero outside the sphere of influence anyways
342-
the_box = np.where(
343-
np.logical_and.reduce(
344-
(
345-
np.abs(x - the_point["x"]) < roi,
346-
np.abs(y - the_point["y"]) < roi,
347-
np.abs(z - the_point["z"]) < roi,
348-
)
349-
)
350-
)
351-
J += np.sum(
352-
((u[the_box] - the_point["u"]) ** 2 + (v[the_box] - the_point["v"]) ** 2)
342+
343+
dist = np.sqrt(
344+
(x - the_point["x"]) ** 2
345+
+ (y - the_point["y"]) ** 2
346+
+ (z - the_point["z"]) ** 2
353347
)
348+
dist = np.maximum(dist, 1.0)
349+
weight = 1 / dist**2
350+
weight = weight / np.max(weight)
351+
352+
J += np.sum(weight * ((u - the_point["u"]) ** 2 + (v - the_point["v"]) ** 2))
354353

355354
return J * Cp
356355

@@ -392,17 +391,16 @@ def calculate_point_gradient(u, v, x, y, z, point_list, Cp=1e-3, roi=500.0):
392391
gradJ_w = np.zeros_like(u)
393392

394393
for the_point in point_list:
395-
the_box = np.where(
396-
np.logical_and.reduce(
397-
(
398-
np.abs(x - the_point["x"]) < roi,
399-
np.abs(y - the_point["y"]) < roi,
400-
np.abs(z - the_point["z"]) < roi,
401-
)
402-
)
394+
dist = np.sqrt(
395+
(x - the_point["x"]) ** 2
396+
+ (y - the_point["y"]) ** 2
397+
+ (z - the_point["z"]) ** 2
403398
)
404-
gradJ_u[the_box] += 2 * (u[the_box] - the_point["u"])
405-
gradJ_v[the_box] += 2 * (v[the_box] - the_point["v"])
399+
dist = np.maximum(dist, 1.0)
400+
weight = 1 / dist**2
401+
weight = weight / np.max(weight)
402+
gradJ_u += 2 * weight * (u - the_point["u"])
403+
gradJ_v += 2 * weight * (v - the_point["v"])
406404

407405
gradJ = np.stack([gradJ_u, gradJ_v, gradJ_w], axis=0).flatten()
408406
return gradJ * Cp

pydda/cost_functions/_cost_functions_tensorflow.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -333,23 +333,18 @@ def calculate_point_cost(u, v, x, y, z, point_list, Cp=1e-3, roi=500.0):
333333
for the_point in point_list:
334334
# Instead of worrying about whole domain, just find points in radius of influence
335335
# Since we know that the weight will be zero outside the sphere of influence anyways
336-
xp = tf.ones_like(x) * the_point["x"]
337-
yp = tf.ones_like(y) * the_point["y"]
338-
zp = tf.ones_like(z) * the_point["z"]
339336
up = tf.ones_like(u) * the_point["u"]
340337
vp = tf.ones_like(v) * the_point["v"]
341-
342-
the_box = tf.where(
343-
tf.math.logical_and(
344-
tf.math.logical_and(
345-
tf.math.abs(x - xp) < roi, tf.math.abs(y - yp) < roi
346-
),
347-
tf.math.abs(z - zp) < roi,
348-
),
349-
1.0,
350-
0.0,
338+
dist = tf.math.sqrt(
339+
(x - the_point["x"]) ** 2
340+
+ (y - the_point["y"]) ** 2
341+
+ (z - the_point["z"]) ** 2
351342
)
352-
J.assign_add(tf.math.reduce_sum(((u - up) ** 2 + (v - vp) ** 2) * the_box))
343+
dist = tf.math.maximum(dist, 1.0)
344+
weight = 1 / dist**2
345+
weight = weight / tf.reduce_max(weight)
346+
347+
J.assign_add(tf.math.reduce_sum(((u - up) ** 2 + (v - vp) ** 2) * weight))
353348

354349
return J * Cp
355350

@@ -394,24 +389,19 @@ def calculate_point_gradient(u, v, x, y, z, point_list, Cp=1e-3, roi=500.0):
394389
for the_point in point_list:
395390
# Instead of worrying about whole domain, just find points in radius of influence
396391
# Since we know that the weight will be zero outside the sphere of influence anyways
397-
xp = tf.ones_like(x, dtype=tf.float32) * the_point["x"]
398-
yp = tf.ones_like(y, dtype=tf.float32) * the_point["y"]
399-
zp = tf.ones_like(z, dtype=tf.float32) * the_point["z"]
400392
up = tf.ones_like(u, dtype=tf.float32) * the_point["u"]
401393
vp = tf.ones_like(v, dtype=tf.float32) * the_point["v"]
402394

403-
the_box = tf.where(
404-
tf.math.logical_and(
405-
tf.math.logical_and(
406-
tf.math.abs(x - xp) < roi, tf.math.abs(y - yp) < roi
407-
),
408-
tf.math.abs(z - zp) < roi,
409-
),
410-
1.0,
411-
0.0,
395+
dist = tf.math.sqrt(
396+
(x - the_point["x"]) ** 2
397+
+ (y - the_point["y"]) ** 2
398+
+ (z - the_point["z"]) ** 2
412399
)
413-
gradJ_u.assign_add((2 * (u - up) * the_box))
414-
gradJ_v.assign_add((2 * (v - vp) * the_box))
400+
dist = tf.math.maximum(dist, 1.0)
401+
weight = 1 / dist**2
402+
weight = weight / tf.reduce_max(weight)
403+
gradJ_u.assign_add((2 * (u - up) * weight))
404+
gradJ_v.assign_add((2 * (v - vp) * weight))
415405
gradJ = tf.stack([gradJ_u, gradJ_v, gradJ_w], axis=0)
416406
gradJ = tf.reshape(gradJ, (3 * np.prod(u.shape),))
417407
return gradJ * Cp

0 commit comments

Comments
 (0)