[9173ee]: / testing / mesh / meshTools / rand_notes / general_test.py

Download this file

290 lines (231 with data), 10.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# from turtle import width
# import pytest
# from pymskt.mesh import meshTools
# import vtk
# import numpy as np
# from vtk.util.numpy_support import numpy_to_vtk, vtk_to_numpy
# from numpy.testing import assert_allclose
# NP_POINTS = np.asarray([
# [0, 0, 0],
# [1, 1, 1],
# [10, 10, 10]
# ])
# #
# # get_mesh_physical_point_coords
# #
# def test_get_mesh_physical_point_coords(np_points=NP_POINTS):
# vtk_points_ = numpy_to_vtk(np_points)
# vtk_points_.SetName('test')
# vtk_points = vtk.vtkPoints()
# vtk_points.SetData(vtk_points_)
# mesh = vtk.vtkPolyData()
# mesh.SetPoints(vtk_points)
# assert_allclose(meshTools.get_mesh_physical_point_coords(mesh), np_points)
# #
# # smooth_scalars_from_second_mesh_onto_base
# #
# def test_smooth_scalars_from_second_mesh_onto_base(
# sigma=1,
# width_height=10,
# resolution=0.1,
# magnitude_impulse=100
# ):
# # Use the same logic as the above test.
# # This test should be the same except that it will create identical meshes (no scalars, yet).
# # Create the mesh
# n_points = int(width_height * (1/resolution) + 1)
# x = np.linspace(0, width_height, n_points)
# xv, yv = np.meshgrid(x, x)
# np_points = np.ones((len(xv.flatten()), 3))
# np_points[:,0] = xv.flatten(order='F')
# np_points[:,1] = yv.flatten(order='F')
# np_points[:,2] = 1
# vtk_points_ = numpy_to_vtk(np_points)
# vtk_points_.SetName('test')
# vtk_points = vtk.vtkPoints()
# vtk_points.SetData(vtk_points_)
# mesh1 = vtk.vtkPolyData()
# mesh1.SetPoints(vtk_points)
# mesh2 = vtk_deep_copy(mesh1)
# # Apply impulse to mesh1.
# # Create the scalars (zeros)
# np_scalars = np.zeros(len(xv.flatten()))
# # Add impulse at the center
# np_scalars[int(len(xv.flatten('F'))/2)] = magnitude_impulse
# # Apply scalars (and impulse) to mesh.
# vtk_scalars = numpy_to_vtk(np.copy(np_scalars))
# vtk_scalars.SetName('test')
# mesh1.GetPointData().AddArray(vtk_scalars)
# mesh1.GetPointData().SetActiveScalars('test')
# # smooth (gaussian) mesh1 scalars onto mesh2.
# # gaussian filter the points.
# smoothed_scalars = meshTools.smooth_scalars_from_second_mesh_onto_base(
# base_mesh=mesh2,
# second_mesh=mesh1,
# sigma=sigma,
# idx_coords_to_smooth_base=None,
# idx_coords_to_smooth_second=None,
# set_non_smoothed_scalars_to_zero=True
# )
# unraveled = np.reshape(smoothed_scalars, (n_points, n_points), order="F")
# # calculate the theoretical normal distribution (based on sigma etc)
# edge_sd = width_height / 2 / sigma
# x = np.linspace(-edge_sd, edge_sd, n_points)
# pdf = norm.pdf(x)
# # Normalized pdf to magnitude of the scalars:
# # This scales the whole curve based on the size of the peak (center)
# # of the curve in relation to our calcualted distribution.
# middle_idx = int((n_points-1)/2)
# pdf = pdf / (pdf[middle_idx] / unraveled[middle_idx, middle_idx])
# # assert that the x & y axies (down the middle) follow the expected normal distribution.
# assert_allclose(pdf, unraveled[middle_idx,:], atol=1e-4)
# assert_allclose(pdf, unraveled[:, middle_idx], atol=1e-4)
# #
# # transfer_mesh_scalars_get_weighted_average_n_closest
# #
# def test_transfer_mesh_scalars_get_weighted_average_n_closest(n_points=1000):
# np_points = np.ones((n_points, 3))
# np_points[:,0] = np.arange(n_points)
# np_points[:,1] = np.arange(n_points)
# np_points[:,2] = np.arange(n_points)
# vtk_points_ = numpy_to_vtk(np_points)
# vtk_points_.SetName('test')
# vtk_points = vtk.vtkPoints()
# vtk_points.SetData(vtk_points_)
# mesh = vtk.vtkPolyData()
# mesh.SetPoints(vtk_points)
# mesh2 = vtk_deep_copy(mesh)
# np_scalars = np.random.random(n_points)
# vtk_scalars = numpy_to_vtk(np_scalars)
# vtk_scalars.SetName('test')
# # mesh.GetPointData().SetScalars(vtk_scalars)
# mesh.GetPointData().AddArray(vtk_scalars)
# transfered_scalars = meshTools.transfer_mesh_scalars_get_weighted_average_n_closest(mesh2, mesh, n=1)
# assert_allclose(np_scalars, np.squeeze(transfered_scalars))
# #
# # get_smoothed_scalars
# #
# def dist(diff, epsilon=1e-7):
# return np.sqrt(np.sum(np.square(diff + epsilon)))
# def test_get_smoothed_scalars(
# max_dist=1.1, # use 1.1 so only need to get single points in-line(x/y) & no diag for testing - but dont want 1.0 otherwise weighting = 0 for all other points.
# order=2.
# ):
# # Create small mesh that can easily manually calculate the outcomes.
# width_height = 1
# resolution = 1
# n_points = int(width_height * (1/resolution) + 1)
# x = np.linspace(0, width_height, n_points)
# xv, yv = np.meshgrid(x, x)
# np_points = np.ones((len(xv.flatten()), 3))
# np_points[:,0] = xv.flatten(order='F')
# np_points[:,1] = yv.flatten(order='F')
# np_points[:,2] = 1
# vtk_points_ = numpy_to_vtk(np_points)
# vtk_points_.SetName('test')
# vtk_points = vtk.vtkPoints()
# vtk_points.SetData(vtk_points_)
# mesh = vtk.vtkPolyData()
# mesh.SetPoints(vtk_points)
# np_scalars = np.random.randint(0, 1000, len(xv.flatten()))
# np_scalars_reshaped = np.reshape(np_scalars, (width_height*2, width_height*2), order='F')
# np_scalars_smoothed_test = np.zeros_like(np_scalars_reshaped).astype(float)
# for i in range(width_height + 1):
# for j in range(width_height + 1):
# distances = [dist(0)]
# scalars = [np_scalars_reshaped[i, j]]
# if i > 0:
# scalars.append(np_scalars_reshaped[i-1, j])
# distances.append(dist(1.0))
# if j > 0:
# scalars.append(np_scalars_reshaped[i, j-1])
# distances.append(dist(1.0))
# if i < width_height:
# scalars.append(np_scalars_reshaped[i+1, j])
# distances.append(dist(1.0))
# if j < width_height:
# scalars.append(np_scalars_reshaped[i, j+1])
# distances.append(dist(1.0))
# weights = (max_dist - np.asarray(distances))**order
# weighted_scalars = weights * np.asarray(scalars)
# normalized_point = np.sum(weighted_scalars) / np.sum(weights)
# np_scalars_smoothed_test[i, j] = normalized_point
# # Apply scalars (and impulse) to mesh.
# vtk_scalars = numpy_to_vtk(np.copy(np_scalars))
# vtk_scalars.SetName('test')
# mesh.GetPointData().AddArray(vtk_scalars)
# mesh.GetPointData().SetActiveScalars('test')
# scalars_smoothed = meshTools.get_smoothed_scalars(
# mesh,
# max_dist=max_dist,
# order=order,
# gaussian=False
# )
# scalars_smoothed = np.reshape(scalars_smoothed, (width_height+1, width_height+1), order='F')
# assert_allclose(np_scalars_smoothed_test, scalars_smoothed, rtol=1e-03)
# #
# # gaussian_smooth_surface_scalars
# #
# def test_gaussian_smooth_surface_scalars(
# sigma=1,
# width_height=10,
# resolution=0.1,
# magnitude_impulse=100
# ):
# # This function gaussian filters an impulse & ensures that the resulting grid follows a normal distribution.
# # Calculates the smoothed mesh/points & then compares lines through the center in the x & y axes to a normal
# # distribution.
# # Create the mesh
# n_points = int(width_height * (1/resolution) + 1)
# x = np.linspace(0, width_height, n_points)
# xv, yv = np.meshgrid(x, x)
# np_points = np.ones((len(xv.flatten()), 3))
# np_points[:,0] = xv.flatten(order='F')
# np_points[:,1] = yv.flatten(order='F')
# np_points[:,2] = 1
# vtk_points_ = numpy_to_vtk(np_points)
# vtk_points_.SetName('test')
# vtk_points = vtk.vtkPoints()
# vtk_points.SetData(vtk_points_)
# mesh = vtk.vtkPolyData()
# mesh.SetPoints(vtk_points)
# # Create the scalars (zeros)
# np_scalars = np.zeros(len(xv.flatten()))
# # Add impulse at the center
# np_scalars[int(len(xv.flatten('F'))/2)] = magnitude_impulse
# # Apply scalars (and impulse) to mesh.
# vtk_scalars = numpy_to_vtk(np.copy(np_scalars))
# vtk_scalars.SetName('test')
# mesh.GetPointData().AddArray(vtk_scalars)
# mesh.GetPointData().SetActiveScalars('test')
# # gaussian filter the points.
# mesh2 = meshTools.gaussian_smooth_surface_scalars(
# mesh=mesh,
# sigma=sigma,
# idx_coords_to_smooth=None,
# array_name='test',
# array_idx=None
# )
# # retrieve and re-shape the filtered scalars.
# smoothed_scalars = vtk_to_numpy(mesh2.GetPointData().GetScalars())
# unraveled = np.reshape(smoothed_scalars, (n_points, n_points), order="F")
# # calculate the theoretical normal distribution (based on sigma etc)
# edge_sd = width_height / 2 / sigma
# x = np.linspace(-edge_sd, edge_sd, n_points)
# pdf = norm.pdf(x)
# # Normalized pdf to magnitude of the scalars:
# # This scales the whole curve based on the size of the peak (center)
# # of the curve in relation to our calcualted distribution.
# middle_idx = int((n_points-1)/2)
# pdf = pdf / (pdf[middle_idx] / unraveled[middle_idx, middle_idx])
# # assert that the x & y axies (down the middle) follow the expected normal distribution.
# assert_allclose(pdf, unraveled[middle_idx,:], atol=1e-4)
# assert_allclose(pdf, unraveled[:, middle_idx], atol=1e-4)
# def test_gaussian_smooth_surface_scalars_use_idx_for_base_mesh():
# raise Exception('Test not implemented')
# def test_smooth_scalars_from_second_mesh_onto_base_use_idx_coords_to_smooth():
# raise Exception('Test not implemented')
# def test_smooth_scalars_from_second_mesh_onto_base_use_idx_for_second_mesh():
# raise Exception('Test not implemented')
# def test_get_cartilage_properties_at_points():
# raise Exception('Test not implemented')