Skip to content

Commit f818dd1

Browse files
committed
Tried to understand linalgerrors and added a plotting function.
1 parent 00ccb2f commit f818dd1

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/astrohack/utils/ray_tracing_general.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
import pickle
33
from scipy.spatial import distance_matrix
44
from numba import njit
5+
from numpy.linalg import LinAlgError
56

67
from astrohack.utils.algorithms import least_squares, create_2d_array_reconstruction_array, create_coordinate_images, regrid_data_onto_2d_grid
8+
from astrohack.visualization.plot_tools import *
79

810
nanvec3d = np.array([np.nan, np.nan, np.nan])
911
return_line = "\033[F"
1012

13+
1114
def generalized_dot(vec_map_a, vec_map_b):
1215
return np.sum(vec_map_a * vec_map_b, axis=-1)
1316

@@ -73,7 +76,11 @@ def np_qps_fitting(pcd):
7376
sys_matrix[npnt:, :npnt] = 1.0
7477

7578
sys_vector[:npnt] = pcd[:, 2]
76-
qps_coeffs, _, _ = least_squares(sys_matrix, sys_vector)
79+
try:
80+
qps_coeffs, _, _ = least_squares(sys_matrix, sys_vector)
81+
except LinAlgError:
82+
print(sys_matrix, sys_vector)
83+
raise LinAlgError
7784
return qps_coeffs
7885

7986

@@ -143,13 +150,15 @@ def qps_image_jit(global_pcd, local_qps_coeffs, local_pcds, points):
143150
npnt = points.shape[0]
144151
new_zval = np.empty(npnt, dtype=np.float64)
145152
new_norm = np.empty((npnt, 3), dtype=np.float64)
153+
print()
146154
for ipnt in range(npnt):
155+
print(return_line, 100*ipnt/npnt, '% done ')
147156
dist = np.sum((global_pcd[:, 0:2]-points[ipnt])**2, axis=-1)
148157
i_closest = np.argmin(dist)
149158
pnt, norm = qps_compute_point_and_normal_jit(points[ipnt], local_qps_coeffs[i_closest], local_pcds[i_closest])
150159
new_zval[ipnt] = pnt[2]
151160
new_norm[ipnt] = norm
152-
161+
print('Done')
153162
return new_zval, new_norm
154163

155164

@@ -177,14 +186,14 @@ def __init__(self):
177186
self.high_res_v_axis = None
178187

179188
@classmethod
180-
def from_pcd(cls, pcd_data, local_qps_n_pnt=20, displacement=(0,0,0)):
189+
def from_pcd(cls, pcd_data, local_qps_n_pnt=20, displacement=(0, 0, 0)):
181190
new_obj = cls()
182191
new_obj._init_from_pcd(pcd_data, local_qps_n_pnt, displacement)
183192
return new_obj
184193

185194
def _init_from_pcd(self, pcd_data, local_qps_n_pnt, displacement):
186195
self.global_pcd = pcd_data
187-
self.global_pcd -= np.array(displacement)
196+
self.global_pcd[:, ] -= np.array(displacement)
188197
self.npnt = self.global_pcd.shape[0]
189198
self.local_qps_n_pnt = local_qps_n_pnt
190199
self.local_pcds = np.empty([self.npnt, self.local_qps_n_pnt, 3])
@@ -261,10 +270,17 @@ def execute_selection(pcd_selection, point_sel):
261270

262271
return z_val, z_norm
263272

264-
def plot_z_val_and_z_cos(self, colormap, zlim, dpi, display):
273+
def plot_z_val_and_z_cos(self, colormap='viridis', zlim=None, dpi=300, display=False):
274+
fig, ax = create_figure_and_axes(None, [1, 2])
275+
simple_imshow_map_plot(ax[0], fig, self.current_u_axis, self.current_v_axis, self.current_z_val,
276+
'Z value', colormap, zlim, z_label='Z value [m]', transpose=True)
277+
simple_imshow_map_plot(ax[1], fig, self.current_u_axis, self.current_v_axis, self.current_z_cos,
278+
'Z Cosine', colormap, zlim, z_label='Z cosine []', transpose=True)
279+
close_figure(fig, 'Gridded surface and cosine of surface angle to Z axis', 'zval_zcos.png',
280+
dpi, display)
265281
return
266282

267-
def compute_gridded_z_val_and_z_cos(self, u_axis, v_axis, mask, gridding_engine='2D regrid', light=(0,0,-1),
283+
def compute_gridded_z_val_and_z_cos(self, u_axis, v_axis, mask, gridding_engine='2D regrid', light=(0, 0, -1),
268284
vectorized=True):
269285
light = np.array(light)
270286

0 commit comments

Comments
 (0)