Switch to unified view

a b/docs/mesh/anatomical/femur_cylinder.html
1
<!doctype html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
6
<meta name="generator" content="pdoc 0.10.0" />
7
<title>pymskt.mesh.anatomical.femur_cylinder API documentation</title>
8
<meta name="description" content="" />
9
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
10
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
11
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
12
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
13
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
14
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
15
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
16
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
17
</head>
18
<body>
19
<main>
20
<article id="content">
21
<header>
22
<h1 class="title">Module <code>pymskt.mesh.anatomical.femur_cylinder</code></h1>
23
</header>
24
<section id="section-intro">
25
<details class="source">
26
<summary>
27
<span>Expand source code</span>
28
</summary>
29
<pre><code class="python">from scipy.optimize import least_squares
30
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
31
import numpy as np
32
from pymskt.statistics.pca import pca_svd
33
34
class FitCylinderFemur:
35
    def __init__(
36
        self,
37
        femur,
38
        labels_name=&#39;labels&#39;,
39
        labels=(12, 13, 14, 15),
40
        z_resolution=50,
41
        theta_resolution=50,
42
        cylinder_percent_bone_width=0.9,
43
#         copy_femur=True,
44
        ftol=1e-4
45
        
46
    ):
47
        # not using the copy method becuase the femur is a pymskt object not vtk so it 
48
        # doesnt work with vtk_deep_copy - would need to create new femur object. 
49
#         if copy_femur is True:
50
#             self.femur = mskt.mesh.utils.vtk_deep_copy(femur)
51
#         else:
52
#             self.femur = femur
53
        self.femur = femur
54
        self.labels_name = labels_name
55
        if (type(labels) == int) or (type(labels) == float):
56
            self.labels = [labels,]
57
        else:
58
            self.labels = labels
59
        self.z_resolution = z_resolution
60
        self.theta_resolution = theta_resolution
61
        self.cylinder_percent_bone_width = cylinder_percent_bone_width
62
        self.ftol = ftol
63
        
64
        self.pts_articular_cylinder = None
65
        self.inertial_matrix_artic_surf = None
66
        self.inertial_aligned_pts_articular_cylinder = None
67
        self._height = None
68
        self._origin = None
69
        self._vector = None
70
        self._radius = None
71
        self.bounds = None
72
        self.params = None
73
74
    def get_initial_parameters(self):
75
        self.get_articular_surf_points()
76
        self.guess_height()
77
        self.guess_origin()
78
        self.guess_vector()
79
        self.guess_radius()
80
        
81
    def get_articular_surf_points(self):
82
        label_idx = vtk_to_numpy(self.femur.mesh.GetPointData().GetArray(self.labels_name))
83
        cylinder_labels = label_idx == self.labels[0] 
84
        if len(self.labels) &gt; 1:
85
            for idx in range(1, len(self.labels)):
86
                cylinder_labels += (label_idx == self.labels[idx])
87
        cylinder_labels = np.asarray(cylinder_labels, dtype=int)
88
        cylinder_scalars = numpy_to_vtk(cylinder_labels)
89
        cylinder_scalars.SetName(&#39;cylinder labels&#39;)
90
        self.femur.mesh.GetPointData().AddArray(cylinder_scalars)
91
        
92
        self.pts_articular_cylinder = self.femur.point_coords[cylinder_labels == 1, :]
93
    
94
    def get_inertial_matrix_articular_surface(self):
95
        self.inertial_matrix_artic_surf, _ = pca_svd(self.pts_articular_cylinder.T)
96
        self.inv_inertial_matrix_artic_surf = np.linalg.inv(self.inertial_matrix_artic_surf)
97
    
98
    def get_artic_pts_aligned_inertial_matrix(self):
99
        if self.inertial_matrix_artic_surf is None:
100
            self.get_inertial_matrix_articular_surface()
101
        self.inertial_aligned_pts_articular_cylinder = self.inertial_matrix_artic_surf @ self.pts_articular_cylinder.T
102
    
103
    def guess_height(self):
104
        if self.inertial_aligned_pts_articular_cylinder is None:
105
            self.get_artic_pts_aligned_inertial_matrix()
106
        height_guess = self.inertial_aligned_pts_articular_cylinder[0,:].max() - self.inertial_aligned_pts_articular_cylinder[0,:].min()
107
        self._height = self.cylinder_percent_bone_width * height_guess
108
109
    def guess_origin(self):
110
        if self.inertial_matrix_artic_surf is None:
111
            self.get_inertial_matrix_articular_surface()
112
        min_x = self.inertial_aligned_pts_articular_cylinder[0,:].min() # this is going to be fully medial or laterl
113
        max_x = self.inertial_aligned_pts_articular_cylinder[0,:].max() # this is going to be fully medial or laterl (opposite above)
114
        mean_y = self.inertial_aligned_pts_articular_cylinder[1,:].mean() # use this as the origin y 
115
        mean_z = self.inertial_aligned_pts_articular_cylinder[2,:].mean() # I think this is going to be too close to the articular surface... but maybe good enought start? 
116
117
        # Get points in roughly the center of the cylinder of the condyle on the medial &amp; lateral sides. 
118
        origin1 = np.asarray([[min_x, mean_y, mean_z],])
119
        origin2 = np.asarray([[max_x, mean_y, mean_z],])
120
121
        origin1 = self.inv_inertial_matrix_artic_surf @ origin1.T
122
        origin1 = np.squeeze(origin1.T)
123
        origin2 = self.inv_inertial_matrix_artic_surf @ origin2.T
124
        origin2 = np.squeeze(origin2.T)
125
126
        # Set the origin to a point just inside of the extreme on the min_x side (whether thats medial or lateral)
127
        origin = (origin2 - origin1) * 0.05 + origin1
128
        
129
        self._origin = origin
130
131
    def guess_vector(self):
132
        if self.inertial_matrix_artic_surf is None:
133
            self.get_inertial_matrix_articular_surface()
134
        vector = np.asarray([
135
            self.inertial_matrix_artic_surf[0,0], # vector X
136
            self.inertial_matrix_artic_surf[1,0], # vector Y
137
            self.inertial_matrix_artic_surf[2,0], # vector Z
138
        ], dtype=float)
139
        
140
        if np.linalg.norm(vector) != 1:
141
            vector = vector/np.linalg.norm(vector)
142
        self._vector = vector
143
144
    def guess_radius(self):
145
        if self.inertial_aligned_pts_articular_cylinder is None:
146
            self.get_artic_pts_aligned_inertial_matrix()
147
148
        range_y = self.inertial_aligned_pts_articular_cylinder[1,:].max() - self.inertial_aligned_pts_articular_cylinder[1,:].min()
149
        radius = range_y/2
150
        
151
        self._radius = radius
152
    
153
    @staticmethod
154
    def get_unit_cylinder(z_resolution, theta_resolution):
155
        theta = np.linspace(0, 2*np.pi, theta_resolution)
156
        
157
        unit_cylinder = np.zeros((theta_resolution * z_resolution, 3))  
158
        unit_cylinder[:, 0] = np.tile(np.cos(theta), z_resolution)
159
        unit_cylinder[:, 1] = np.tile(np.sin(theta), z_resolution)
160
161
    #     for i in range(z_resolution):
162
        unit_cylinder[:, 2] = np.repeat(np.linspace(0, 1, z_resolution), theta_resolution)
163
164
        return unit_cylinder
165
166
    def cylinder_function(self, origin, height, radius, vector, z_resolution=None, theta_resolution=None):
167
        if z_resolution is None:
168
            z_resolution = self.z_resolution
169
        if theta_resolution is None:
170
            theta_resolution = self.theta_resolution
171
        
172
        # ensure vector is np array of floats. 
173
        vector = np.asarray(vector, dtype=float)
174
        if np.linalg.norm(vector) != 1:
175
            vector = vector/np.linalg.norm(vector)
176
177
        # scale the size of the cylinder
178
        unit_cylinder = FitCylinderFemur.get_unit_cylinder(z_resolution=z_resolution, theta_resolution=theta_resolution)
179
        unit_cylinder[:, 0] = unit_cylinder[:, 0] * radius
180
        unit_cylinder[:, 1] = unit_cylinder[:, 1] * radius
181
        unit_cylinder[:, 2] = unit_cylinder[:, 2] * height
182
183
        # Create rotation matrix to rotate cylinder axis
184
        #make some vector not in the same direction as v
185
        not_v = np.array([1, 0, 0])
186
        if (vector == not_v).all():
187
            not_v = np.array([0, 1, 0])
188
        #make vector perpendicular to v
189
        norm1 = np.cross(vector, not_v)
190
        #normalize n1
191
        norm1 /= np.linalg.norm(norm1)
192
        #make unit vector perpendicular to v and n1
193
        norm2 = np.cross(vector, norm1)
194
195
        rot_matrix = np.zeros((3,3))
196
        rot_matrix[:,0] = norm1
197
        rot_matrix[:,1] = norm2
198
        rot_matrix[:,2] = vector
199
200
        # rotate the cylinder along the vector axis
201
        unit_cylinder = rot_matrix @ unit_cylinder.T
202
        unit_cylinder = unit_cylinder.T
203
        unit_cylinder += origin
204
205
        return unit_cylinder
206
    
207
    @staticmethod
208
    def residuals(points, cylinder):
209
        &#34;&#34;&#34;
210
        Find closest point on cylinder for each point. Calcualte 
211
        &#34;&#34;&#34;
212
213
        diff = points[None, :, :] - cylinder[:, None, :]
214
        diff = np.sqrt(np.sum(diff **2, axis=-1))
215
        resids = diff.min(axis=0)
216
        return resids
217
218
    def get_func(self):
219
        &#34;&#34;&#34;
220
        Function to create the function that we want to minimize. The returned function
221
        returns the residuals of the points vs the generated cylinder. 
222
        &#34;&#34;&#34;
223
        def func(params):
224
            cylinder = self.cylinder_function(
225
                origin=[params[0], params[1], params[2]],
226
                height=params[3],
227
                radius=params[4],
228
                vector=np.asarray([params[5],params[6],params[7]], dtype=float),
229
                z_resolution=self.z_resolution, 
230
                theta_resolution=self.theta_resolution
231
            )
232
233
            resid = FitCylinderFemur.residuals(self.pts_articular_cylinder, cylinder)
234
235
            return resid
236
        return func
237
    
238
    def get_bounds(self):
239
        self.bounds = [
240
            [
241
                self.pts_articular_cylinder[:,0].min(),
242
                self.pts_articular_cylinder[:,1].min(),
243
                self.pts_articular_cylinder[:,2].min(),
244
                self._height - self._height * 0.2,
245
                self._radius - self._radius * 0.2,
246
                -1,
247
                -1,
248
                -1
249
250
            ],
251
            [
252
                self.pts_articular_cylinder[:,0].max(),
253
                self.pts_articular_cylinder[:,1].max(),
254
                self.pts_articular_cylinder[:,2].max(),
255
                self._height + self._height * 0.2,
256
                self._radius + self._radius * 0.2,
257
                1,
258
                1,
259
                1 
260
            ]
261
        ]
262
    
263
    def get_params(self):
264
        if (self._origin is None) or (self._height is None) or (self._radius is None) or (self._vector is None):
265
            self.get_initial_parameters()
266
        self.params = [
267
            self._origin[0],
268
            self._origin[1],
269
            self._origin[2],
270
            self._height,
271
            self._radius,
272
            self._vector[0],
273
            self._vector[1],
274
            self._vector[2]
275
        ]
276
    def fit(self):
277
        if self.params is None:
278
            self.get_params()
279
        if self.bounds is None:
280
            self.get_bounds()
281
            
282
        func = self.get_func()
283
        
284
        result = least_squares(
285
            func,
286
            self.params,
287
            bounds=self.bounds,
288
            ftol=self.ftol                    
289
        )
290
        
291
        self.optimization_success = result[&#39;success&#39;]
292
        self.params = result[&#39;x&#39;]
293
        self._origin = np.array([self.params[0], self.params[1], self.params[2]])
294
        self._height = self.params[3]
295
        self._radius = self.params[4]
296
        self._vector = np.array([self.params[5], self.params[6], self.params[7]])
297
        self._vector /= np.linalg.norm(self._vector)
298
        
299
        if self.optimization_success is True:
300
            print(&#39;Fitting cylinder to condyles completed successfully!&#39;)
301
        else:
302
            print(&#39;Fitting cylinder to condyles did not converge properly:\n&#39;, result)
303
    
304
    @property
305
    def height(self):
306
        return self._height
307
    
308
    @property
309
    def radius(self):
310
        return self._radius
311
    
312
    @property
313
    def origin(self):
314
        return self._origin
315
    
316
    @property
317
    def vector(self):
318
        return self._vector
319
    
320
    @property
321
    def cylinder(self):
322
        return self.cylinder_function(
323
            origin=self._origin, 
324
            height=self._height, 
325
            radius=self._radius, 
326
            vector=self._vector
327
        )</code></pre>
328
</details>
329
</section>
330
<section>
331
</section>
332
<section>
333
</section>
334
<section>
335
</section>
336
<section>
337
<h2 class="section-title" id="header-classes">Classes</h2>
338
<dl>
339
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur"><code class="flex name class">
340
<span>class <span class="ident">FitCylinderFemur</span></span>
341
<span>(</span><span>femur, labels_name='labels', labels=(12, 13, 14, 15), z_resolution=50, theta_resolution=50, cylinder_percent_bone_width=0.9, ftol=0.0001)</span>
342
</code></dt>
343
<dd>
344
<div class="desc"></div>
345
<details class="source">
346
<summary>
347
<span>Expand source code</span>
348
</summary>
349
<pre><code class="python">class FitCylinderFemur:
350
    def __init__(
351
        self,
352
        femur,
353
        labels_name=&#39;labels&#39;,
354
        labels=(12, 13, 14, 15),
355
        z_resolution=50,
356
        theta_resolution=50,
357
        cylinder_percent_bone_width=0.9,
358
#         copy_femur=True,
359
        ftol=1e-4
360
        
361
    ):
362
        # not using the copy method becuase the femur is a pymskt object not vtk so it 
363
        # doesnt work with vtk_deep_copy - would need to create new femur object. 
364
#         if copy_femur is True:
365
#             self.femur = mskt.mesh.utils.vtk_deep_copy(femur)
366
#         else:
367
#             self.femur = femur
368
        self.femur = femur
369
        self.labels_name = labels_name
370
        if (type(labels) == int) or (type(labels) == float):
371
            self.labels = [labels,]
372
        else:
373
            self.labels = labels
374
        self.z_resolution = z_resolution
375
        self.theta_resolution = theta_resolution
376
        self.cylinder_percent_bone_width = cylinder_percent_bone_width
377
        self.ftol = ftol
378
        
379
        self.pts_articular_cylinder = None
380
        self.inertial_matrix_artic_surf = None
381
        self.inertial_aligned_pts_articular_cylinder = None
382
        self._height = None
383
        self._origin = None
384
        self._vector = None
385
        self._radius = None
386
        self.bounds = None
387
        self.params = None
388
389
    def get_initial_parameters(self):
390
        self.get_articular_surf_points()
391
        self.guess_height()
392
        self.guess_origin()
393
        self.guess_vector()
394
        self.guess_radius()
395
        
396
    def get_articular_surf_points(self):
397
        label_idx = vtk_to_numpy(self.femur.mesh.GetPointData().GetArray(self.labels_name))
398
        cylinder_labels = label_idx == self.labels[0] 
399
        if len(self.labels) &gt; 1:
400
            for idx in range(1, len(self.labels)):
401
                cylinder_labels += (label_idx == self.labels[idx])
402
        cylinder_labels = np.asarray(cylinder_labels, dtype=int)
403
        cylinder_scalars = numpy_to_vtk(cylinder_labels)
404
        cylinder_scalars.SetName(&#39;cylinder labels&#39;)
405
        self.femur.mesh.GetPointData().AddArray(cylinder_scalars)
406
        
407
        self.pts_articular_cylinder = self.femur.point_coords[cylinder_labels == 1, :]
408
    
409
    def get_inertial_matrix_articular_surface(self):
410
        self.inertial_matrix_artic_surf, _ = pca_svd(self.pts_articular_cylinder.T)
411
        self.inv_inertial_matrix_artic_surf = np.linalg.inv(self.inertial_matrix_artic_surf)
412
    
413
    def get_artic_pts_aligned_inertial_matrix(self):
414
        if self.inertial_matrix_artic_surf is None:
415
            self.get_inertial_matrix_articular_surface()
416
        self.inertial_aligned_pts_articular_cylinder = self.inertial_matrix_artic_surf @ self.pts_articular_cylinder.T
417
    
418
    def guess_height(self):
419
        if self.inertial_aligned_pts_articular_cylinder is None:
420
            self.get_artic_pts_aligned_inertial_matrix()
421
        height_guess = self.inertial_aligned_pts_articular_cylinder[0,:].max() - self.inertial_aligned_pts_articular_cylinder[0,:].min()
422
        self._height = self.cylinder_percent_bone_width * height_guess
423
424
    def guess_origin(self):
425
        if self.inertial_matrix_artic_surf is None:
426
            self.get_inertial_matrix_articular_surface()
427
        min_x = self.inertial_aligned_pts_articular_cylinder[0,:].min() # this is going to be fully medial or laterl
428
        max_x = self.inertial_aligned_pts_articular_cylinder[0,:].max() # this is going to be fully medial or laterl (opposite above)
429
        mean_y = self.inertial_aligned_pts_articular_cylinder[1,:].mean() # use this as the origin y 
430
        mean_z = self.inertial_aligned_pts_articular_cylinder[2,:].mean() # I think this is going to be too close to the articular surface... but maybe good enought start? 
431
432
        # Get points in roughly the center of the cylinder of the condyle on the medial &amp; lateral sides. 
433
        origin1 = np.asarray([[min_x, mean_y, mean_z],])
434
        origin2 = np.asarray([[max_x, mean_y, mean_z],])
435
436
        origin1 = self.inv_inertial_matrix_artic_surf @ origin1.T
437
        origin1 = np.squeeze(origin1.T)
438
        origin2 = self.inv_inertial_matrix_artic_surf @ origin2.T
439
        origin2 = np.squeeze(origin2.T)
440
441
        # Set the origin to a point just inside of the extreme on the min_x side (whether thats medial or lateral)
442
        origin = (origin2 - origin1) * 0.05 + origin1
443
        
444
        self._origin = origin
445
446
    def guess_vector(self):
447
        if self.inertial_matrix_artic_surf is None:
448
            self.get_inertial_matrix_articular_surface()
449
        vector = np.asarray([
450
            self.inertial_matrix_artic_surf[0,0], # vector X
451
            self.inertial_matrix_artic_surf[1,0], # vector Y
452
            self.inertial_matrix_artic_surf[2,0], # vector Z
453
        ], dtype=float)
454
        
455
        if np.linalg.norm(vector) != 1:
456
            vector = vector/np.linalg.norm(vector)
457
        self._vector = vector
458
459
    def guess_radius(self):
460
        if self.inertial_aligned_pts_articular_cylinder is None:
461
            self.get_artic_pts_aligned_inertial_matrix()
462
463
        range_y = self.inertial_aligned_pts_articular_cylinder[1,:].max() - self.inertial_aligned_pts_articular_cylinder[1,:].min()
464
        radius = range_y/2
465
        
466
        self._radius = radius
467
    
468
    @staticmethod
469
    def get_unit_cylinder(z_resolution, theta_resolution):
470
        theta = np.linspace(0, 2*np.pi, theta_resolution)
471
        
472
        unit_cylinder = np.zeros((theta_resolution * z_resolution, 3))  
473
        unit_cylinder[:, 0] = np.tile(np.cos(theta), z_resolution)
474
        unit_cylinder[:, 1] = np.tile(np.sin(theta), z_resolution)
475
476
    #     for i in range(z_resolution):
477
        unit_cylinder[:, 2] = np.repeat(np.linspace(0, 1, z_resolution), theta_resolution)
478
479
        return unit_cylinder
480
481
    def cylinder_function(self, origin, height, radius, vector, z_resolution=None, theta_resolution=None):
482
        if z_resolution is None:
483
            z_resolution = self.z_resolution
484
        if theta_resolution is None:
485
            theta_resolution = self.theta_resolution
486
        
487
        # ensure vector is np array of floats. 
488
        vector = np.asarray(vector, dtype=float)
489
        if np.linalg.norm(vector) != 1:
490
            vector = vector/np.linalg.norm(vector)
491
492
        # scale the size of the cylinder
493
        unit_cylinder = FitCylinderFemur.get_unit_cylinder(z_resolution=z_resolution, theta_resolution=theta_resolution)
494
        unit_cylinder[:, 0] = unit_cylinder[:, 0] * radius
495
        unit_cylinder[:, 1] = unit_cylinder[:, 1] * radius
496
        unit_cylinder[:, 2] = unit_cylinder[:, 2] * height
497
498
        # Create rotation matrix to rotate cylinder axis
499
        #make some vector not in the same direction as v
500
        not_v = np.array([1, 0, 0])
501
        if (vector == not_v).all():
502
            not_v = np.array([0, 1, 0])
503
        #make vector perpendicular to v
504
        norm1 = np.cross(vector, not_v)
505
        #normalize n1
506
        norm1 /= np.linalg.norm(norm1)
507
        #make unit vector perpendicular to v and n1
508
        norm2 = np.cross(vector, norm1)
509
510
        rot_matrix = np.zeros((3,3))
511
        rot_matrix[:,0] = norm1
512
        rot_matrix[:,1] = norm2
513
        rot_matrix[:,2] = vector
514
515
        # rotate the cylinder along the vector axis
516
        unit_cylinder = rot_matrix @ unit_cylinder.T
517
        unit_cylinder = unit_cylinder.T
518
        unit_cylinder += origin
519
520
        return unit_cylinder
521
    
522
    @staticmethod
523
    def residuals(points, cylinder):
524
        &#34;&#34;&#34;
525
        Find closest point on cylinder for each point. Calcualte 
526
        &#34;&#34;&#34;
527
528
        diff = points[None, :, :] - cylinder[:, None, :]
529
        diff = np.sqrt(np.sum(diff **2, axis=-1))
530
        resids = diff.min(axis=0)
531
        return resids
532
533
    def get_func(self):
534
        &#34;&#34;&#34;
535
        Function to create the function that we want to minimize. The returned function
536
        returns the residuals of the points vs the generated cylinder. 
537
        &#34;&#34;&#34;
538
        def func(params):
539
            cylinder = self.cylinder_function(
540
                origin=[params[0], params[1], params[2]],
541
                height=params[3],
542
                radius=params[4],
543
                vector=np.asarray([params[5],params[6],params[7]], dtype=float),
544
                z_resolution=self.z_resolution, 
545
                theta_resolution=self.theta_resolution
546
            )
547
548
            resid = FitCylinderFemur.residuals(self.pts_articular_cylinder, cylinder)
549
550
            return resid
551
        return func
552
    
553
    def get_bounds(self):
554
        self.bounds = [
555
            [
556
                self.pts_articular_cylinder[:,0].min(),
557
                self.pts_articular_cylinder[:,1].min(),
558
                self.pts_articular_cylinder[:,2].min(),
559
                self._height - self._height * 0.2,
560
                self._radius - self._radius * 0.2,
561
                -1,
562
                -1,
563
                -1
564
565
            ],
566
            [
567
                self.pts_articular_cylinder[:,0].max(),
568
                self.pts_articular_cylinder[:,1].max(),
569
                self.pts_articular_cylinder[:,2].max(),
570
                self._height + self._height * 0.2,
571
                self._radius + self._radius * 0.2,
572
                1,
573
                1,
574
                1 
575
            ]
576
        ]
577
    
578
    def get_params(self):
579
        if (self._origin is None) or (self._height is None) or (self._radius is None) or (self._vector is None):
580
            self.get_initial_parameters()
581
        self.params = [
582
            self._origin[0],
583
            self._origin[1],
584
            self._origin[2],
585
            self._height,
586
            self._radius,
587
            self._vector[0],
588
            self._vector[1],
589
            self._vector[2]
590
        ]
591
    def fit(self):
592
        if self.params is None:
593
            self.get_params()
594
        if self.bounds is None:
595
            self.get_bounds()
596
            
597
        func = self.get_func()
598
        
599
        result = least_squares(
600
            func,
601
            self.params,
602
            bounds=self.bounds,
603
            ftol=self.ftol                    
604
        )
605
        
606
        self.optimization_success = result[&#39;success&#39;]
607
        self.params = result[&#39;x&#39;]
608
        self._origin = np.array([self.params[0], self.params[1], self.params[2]])
609
        self._height = self.params[3]
610
        self._radius = self.params[4]
611
        self._vector = np.array([self.params[5], self.params[6], self.params[7]])
612
        self._vector /= np.linalg.norm(self._vector)
613
        
614
        if self.optimization_success is True:
615
            print(&#39;Fitting cylinder to condyles completed successfully!&#39;)
616
        else:
617
            print(&#39;Fitting cylinder to condyles did not converge properly:\n&#39;, result)
618
    
619
    @property
620
    def height(self):
621
        return self._height
622
    
623
    @property
624
    def radius(self):
625
        return self._radius
626
    
627
    @property
628
    def origin(self):
629
        return self._origin
630
    
631
    @property
632
    def vector(self):
633
        return self._vector
634
    
635
    @property
636
    def cylinder(self):
637
        return self.cylinder_function(
638
            origin=self._origin, 
639
            height=self._height, 
640
            radius=self._radius, 
641
            vector=self._vector
642
        )</code></pre>
643
</details>
644
<h3>Static methods</h3>
645
<dl>
646
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_unit_cylinder"><code class="name flex">
647
<span>def <span class="ident">get_unit_cylinder</span></span>(<span>z_resolution, theta_resolution)</span>
648
</code></dt>
649
<dd>
650
<div class="desc"></div>
651
<details class="source">
652
<summary>
653
<span>Expand source code</span>
654
</summary>
655
<pre><code class="python">@staticmethod
656
def get_unit_cylinder(z_resolution, theta_resolution):
657
    theta = np.linspace(0, 2*np.pi, theta_resolution)
658
    
659
    unit_cylinder = np.zeros((theta_resolution * z_resolution, 3))  
660
    unit_cylinder[:, 0] = np.tile(np.cos(theta), z_resolution)
661
    unit_cylinder[:, 1] = np.tile(np.sin(theta), z_resolution)
662
663
#     for i in range(z_resolution):
664
    unit_cylinder[:, 2] = np.repeat(np.linspace(0, 1, z_resolution), theta_resolution)
665
666
    return unit_cylinder</code></pre>
667
</details>
668
</dd>
669
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.residuals"><code class="name flex">
670
<span>def <span class="ident">residuals</span></span>(<span>points, cylinder)</span>
671
</code></dt>
672
<dd>
673
<div class="desc"><p>Find closest point on cylinder for each point. Calcualte</p></div>
674
<details class="source">
675
<summary>
676
<span>Expand source code</span>
677
</summary>
678
<pre><code class="python">@staticmethod
679
def residuals(points, cylinder):
680
    &#34;&#34;&#34;
681
    Find closest point on cylinder for each point. Calcualte 
682
    &#34;&#34;&#34;
683
684
    diff = points[None, :, :] - cylinder[:, None, :]
685
    diff = np.sqrt(np.sum(diff **2, axis=-1))
686
    resids = diff.min(axis=0)
687
    return resids</code></pre>
688
</details>
689
</dd>
690
</dl>
691
<h3>Instance variables</h3>
692
<dl>
693
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.cylinder"><code class="name">var <span class="ident">cylinder</span></code></dt>
694
<dd>
695
<div class="desc"></div>
696
<details class="source">
697
<summary>
698
<span>Expand source code</span>
699
</summary>
700
<pre><code class="python">@property
701
def cylinder(self):
702
    return self.cylinder_function(
703
        origin=self._origin, 
704
        height=self._height, 
705
        radius=self._radius, 
706
        vector=self._vector
707
    )</code></pre>
708
</details>
709
</dd>
710
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.height"><code class="name">var <span class="ident">height</span></code></dt>
711
<dd>
712
<div class="desc"></div>
713
<details class="source">
714
<summary>
715
<span>Expand source code</span>
716
</summary>
717
<pre><code class="python">@property
718
def height(self):
719
    return self._height</code></pre>
720
</details>
721
</dd>
722
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.origin"><code class="name">var <span class="ident">origin</span></code></dt>
723
<dd>
724
<div class="desc"></div>
725
<details class="source">
726
<summary>
727
<span>Expand source code</span>
728
</summary>
729
<pre><code class="python">@property
730
def origin(self):
731
    return self._origin</code></pre>
732
</details>
733
</dd>
734
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.radius"><code class="name">var <span class="ident">radius</span></code></dt>
735
<dd>
736
<div class="desc"></div>
737
<details class="source">
738
<summary>
739
<span>Expand source code</span>
740
</summary>
741
<pre><code class="python">@property
742
def radius(self):
743
    return self._radius</code></pre>
744
</details>
745
</dd>
746
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.vector"><code class="name">var <span class="ident">vector</span></code></dt>
747
<dd>
748
<div class="desc"></div>
749
<details class="source">
750
<summary>
751
<span>Expand source code</span>
752
</summary>
753
<pre><code class="python">@property
754
def vector(self):
755
    return self._vector</code></pre>
756
</details>
757
</dd>
758
</dl>
759
<h3>Methods</h3>
760
<dl>
761
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.cylinder_function"><code class="name flex">
762
<span>def <span class="ident">cylinder_function</span></span>(<span>self, origin, height, radius, vector, z_resolution=None, theta_resolution=None)</span>
763
</code></dt>
764
<dd>
765
<div class="desc"></div>
766
<details class="source">
767
<summary>
768
<span>Expand source code</span>
769
</summary>
770
<pre><code class="python">def cylinder_function(self, origin, height, radius, vector, z_resolution=None, theta_resolution=None):
771
    if z_resolution is None:
772
        z_resolution = self.z_resolution
773
    if theta_resolution is None:
774
        theta_resolution = self.theta_resolution
775
    
776
    # ensure vector is np array of floats. 
777
    vector = np.asarray(vector, dtype=float)
778
    if np.linalg.norm(vector) != 1:
779
        vector = vector/np.linalg.norm(vector)
780
781
    # scale the size of the cylinder
782
    unit_cylinder = FitCylinderFemur.get_unit_cylinder(z_resolution=z_resolution, theta_resolution=theta_resolution)
783
    unit_cylinder[:, 0] = unit_cylinder[:, 0] * radius
784
    unit_cylinder[:, 1] = unit_cylinder[:, 1] * radius
785
    unit_cylinder[:, 2] = unit_cylinder[:, 2] * height
786
787
    # Create rotation matrix to rotate cylinder axis
788
    #make some vector not in the same direction as v
789
    not_v = np.array([1, 0, 0])
790
    if (vector == not_v).all():
791
        not_v = np.array([0, 1, 0])
792
    #make vector perpendicular to v
793
    norm1 = np.cross(vector, not_v)
794
    #normalize n1
795
    norm1 /= np.linalg.norm(norm1)
796
    #make unit vector perpendicular to v and n1
797
    norm2 = np.cross(vector, norm1)
798
799
    rot_matrix = np.zeros((3,3))
800
    rot_matrix[:,0] = norm1
801
    rot_matrix[:,1] = norm2
802
    rot_matrix[:,2] = vector
803
804
    # rotate the cylinder along the vector axis
805
    unit_cylinder = rot_matrix @ unit_cylinder.T
806
    unit_cylinder = unit_cylinder.T
807
    unit_cylinder += origin
808
809
    return unit_cylinder</code></pre>
810
</details>
811
</dd>
812
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.fit"><code class="name flex">
813
<span>def <span class="ident">fit</span></span>(<span>self)</span>
814
</code></dt>
815
<dd>
816
<div class="desc"></div>
817
<details class="source">
818
<summary>
819
<span>Expand source code</span>
820
</summary>
821
<pre><code class="python">def fit(self):
822
    if self.params is None:
823
        self.get_params()
824
    if self.bounds is None:
825
        self.get_bounds()
826
        
827
    func = self.get_func()
828
    
829
    result = least_squares(
830
        func,
831
        self.params,
832
        bounds=self.bounds,
833
        ftol=self.ftol                    
834
    )
835
    
836
    self.optimization_success = result[&#39;success&#39;]
837
    self.params = result[&#39;x&#39;]
838
    self._origin = np.array([self.params[0], self.params[1], self.params[2]])
839
    self._height = self.params[3]
840
    self._radius = self.params[4]
841
    self._vector = np.array([self.params[5], self.params[6], self.params[7]])
842
    self._vector /= np.linalg.norm(self._vector)
843
    
844
    if self.optimization_success is True:
845
        print(&#39;Fitting cylinder to condyles completed successfully!&#39;)
846
    else:
847
        print(&#39;Fitting cylinder to condyles did not converge properly:\n&#39;, result)</code></pre>
848
</details>
849
</dd>
850
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_artic_pts_aligned_inertial_matrix"><code class="name flex">
851
<span>def <span class="ident">get_artic_pts_aligned_inertial_matrix</span></span>(<span>self)</span>
852
</code></dt>
853
<dd>
854
<div class="desc"></div>
855
<details class="source">
856
<summary>
857
<span>Expand source code</span>
858
</summary>
859
<pre><code class="python">def get_artic_pts_aligned_inertial_matrix(self):
860
    if self.inertial_matrix_artic_surf is None:
861
        self.get_inertial_matrix_articular_surface()
862
    self.inertial_aligned_pts_articular_cylinder = self.inertial_matrix_artic_surf @ self.pts_articular_cylinder.T</code></pre>
863
</details>
864
</dd>
865
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_articular_surf_points"><code class="name flex">
866
<span>def <span class="ident">get_articular_surf_points</span></span>(<span>self)</span>
867
</code></dt>
868
<dd>
869
<div class="desc"></div>
870
<details class="source">
871
<summary>
872
<span>Expand source code</span>
873
</summary>
874
<pre><code class="python">def get_articular_surf_points(self):
875
    label_idx = vtk_to_numpy(self.femur.mesh.GetPointData().GetArray(self.labels_name))
876
    cylinder_labels = label_idx == self.labels[0] 
877
    if len(self.labels) &gt; 1:
878
        for idx in range(1, len(self.labels)):
879
            cylinder_labels += (label_idx == self.labels[idx])
880
    cylinder_labels = np.asarray(cylinder_labels, dtype=int)
881
    cylinder_scalars = numpy_to_vtk(cylinder_labels)
882
    cylinder_scalars.SetName(&#39;cylinder labels&#39;)
883
    self.femur.mesh.GetPointData().AddArray(cylinder_scalars)
884
    
885
    self.pts_articular_cylinder = self.femur.point_coords[cylinder_labels == 1, :]</code></pre>
886
</details>
887
</dd>
888
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_bounds"><code class="name flex">
889
<span>def <span class="ident">get_bounds</span></span>(<span>self)</span>
890
</code></dt>
891
<dd>
892
<div class="desc"></div>
893
<details class="source">
894
<summary>
895
<span>Expand source code</span>
896
</summary>
897
<pre><code class="python">def get_bounds(self):
898
    self.bounds = [
899
        [
900
            self.pts_articular_cylinder[:,0].min(),
901
            self.pts_articular_cylinder[:,1].min(),
902
            self.pts_articular_cylinder[:,2].min(),
903
            self._height - self._height * 0.2,
904
            self._radius - self._radius * 0.2,
905
            -1,
906
            -1,
907
            -1
908
909
        ],
910
        [
911
            self.pts_articular_cylinder[:,0].max(),
912
            self.pts_articular_cylinder[:,1].max(),
913
            self.pts_articular_cylinder[:,2].max(),
914
            self._height + self._height * 0.2,
915
            self._radius + self._radius * 0.2,
916
            1,
917
            1,
918
            1 
919
        ]
920
    ]</code></pre>
921
</details>
922
</dd>
923
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_func"><code class="name flex">
924
<span>def <span class="ident">get_func</span></span>(<span>self)</span>
925
</code></dt>
926
<dd>
927
<div class="desc"><p>Function to create the function that we want to minimize. The returned function
928
returns the residuals of the points vs the generated cylinder.</p></div>
929
<details class="source">
930
<summary>
931
<span>Expand source code</span>
932
</summary>
933
<pre><code class="python">def get_func(self):
934
    &#34;&#34;&#34;
935
    Function to create the function that we want to minimize. The returned function
936
    returns the residuals of the points vs the generated cylinder. 
937
    &#34;&#34;&#34;
938
    def func(params):
939
        cylinder = self.cylinder_function(
940
            origin=[params[0], params[1], params[2]],
941
            height=params[3],
942
            radius=params[4],
943
            vector=np.asarray([params[5],params[6],params[7]], dtype=float),
944
            z_resolution=self.z_resolution, 
945
            theta_resolution=self.theta_resolution
946
        )
947
948
        resid = FitCylinderFemur.residuals(self.pts_articular_cylinder, cylinder)
949
950
        return resid
951
    return func</code></pre>
952
</details>
953
</dd>
954
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_inertial_matrix_articular_surface"><code class="name flex">
955
<span>def <span class="ident">get_inertial_matrix_articular_surface</span></span>(<span>self)</span>
956
</code></dt>
957
<dd>
958
<div class="desc"></div>
959
<details class="source">
960
<summary>
961
<span>Expand source code</span>
962
</summary>
963
<pre><code class="python">def get_inertial_matrix_articular_surface(self):
964
    self.inertial_matrix_artic_surf, _ = pca_svd(self.pts_articular_cylinder.T)
965
    self.inv_inertial_matrix_artic_surf = np.linalg.inv(self.inertial_matrix_artic_surf)</code></pre>
966
</details>
967
</dd>
968
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_initial_parameters"><code class="name flex">
969
<span>def <span class="ident">get_initial_parameters</span></span>(<span>self)</span>
970
</code></dt>
971
<dd>
972
<div class="desc"></div>
973
<details class="source">
974
<summary>
975
<span>Expand source code</span>
976
</summary>
977
<pre><code class="python">def get_initial_parameters(self):
978
    self.get_articular_surf_points()
979
    self.guess_height()
980
    self.guess_origin()
981
    self.guess_vector()
982
    self.guess_radius()</code></pre>
983
</details>
984
</dd>
985
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_params"><code class="name flex">
986
<span>def <span class="ident">get_params</span></span>(<span>self)</span>
987
</code></dt>
988
<dd>
989
<div class="desc"></div>
990
<details class="source">
991
<summary>
992
<span>Expand source code</span>
993
</summary>
994
<pre><code class="python">def get_params(self):
995
    if (self._origin is None) or (self._height is None) or (self._radius is None) or (self._vector is None):
996
        self.get_initial_parameters()
997
    self.params = [
998
        self._origin[0],
999
        self._origin[1],
1000
        self._origin[2],
1001
        self._height,
1002
        self._radius,
1003
        self._vector[0],
1004
        self._vector[1],
1005
        self._vector[2]
1006
    ]</code></pre>
1007
</details>
1008
</dd>
1009
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_height"><code class="name flex">
1010
<span>def <span class="ident">guess_height</span></span>(<span>self)</span>
1011
</code></dt>
1012
<dd>
1013
<div class="desc"></div>
1014
<details class="source">
1015
<summary>
1016
<span>Expand source code</span>
1017
</summary>
1018
<pre><code class="python">def guess_height(self):
1019
    if self.inertial_aligned_pts_articular_cylinder is None:
1020
        self.get_artic_pts_aligned_inertial_matrix()
1021
    height_guess = self.inertial_aligned_pts_articular_cylinder[0,:].max() - self.inertial_aligned_pts_articular_cylinder[0,:].min()
1022
    self._height = self.cylinder_percent_bone_width * height_guess</code></pre>
1023
</details>
1024
</dd>
1025
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_origin"><code class="name flex">
1026
<span>def <span class="ident">guess_origin</span></span>(<span>self)</span>
1027
</code></dt>
1028
<dd>
1029
<div class="desc"></div>
1030
<details class="source">
1031
<summary>
1032
<span>Expand source code</span>
1033
</summary>
1034
<pre><code class="python">def guess_origin(self):
1035
    if self.inertial_matrix_artic_surf is None:
1036
        self.get_inertial_matrix_articular_surface()
1037
    min_x = self.inertial_aligned_pts_articular_cylinder[0,:].min() # this is going to be fully medial or laterl
1038
    max_x = self.inertial_aligned_pts_articular_cylinder[0,:].max() # this is going to be fully medial or laterl (opposite above)
1039
    mean_y = self.inertial_aligned_pts_articular_cylinder[1,:].mean() # use this as the origin y 
1040
    mean_z = self.inertial_aligned_pts_articular_cylinder[2,:].mean() # I think this is going to be too close to the articular surface... but maybe good enought start? 
1041
1042
    # Get points in roughly the center of the cylinder of the condyle on the medial &amp; lateral sides. 
1043
    origin1 = np.asarray([[min_x, mean_y, mean_z],])
1044
    origin2 = np.asarray([[max_x, mean_y, mean_z],])
1045
1046
    origin1 = self.inv_inertial_matrix_artic_surf @ origin1.T
1047
    origin1 = np.squeeze(origin1.T)
1048
    origin2 = self.inv_inertial_matrix_artic_surf @ origin2.T
1049
    origin2 = np.squeeze(origin2.T)
1050
1051
    # Set the origin to a point just inside of the extreme on the min_x side (whether thats medial or lateral)
1052
    origin = (origin2 - origin1) * 0.05 + origin1
1053
    
1054
    self._origin = origin</code></pre>
1055
</details>
1056
</dd>
1057
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_radius"><code class="name flex">
1058
<span>def <span class="ident">guess_radius</span></span>(<span>self)</span>
1059
</code></dt>
1060
<dd>
1061
<div class="desc"></div>
1062
<details class="source">
1063
<summary>
1064
<span>Expand source code</span>
1065
</summary>
1066
<pre><code class="python">def guess_radius(self):
1067
    if self.inertial_aligned_pts_articular_cylinder is None:
1068
        self.get_artic_pts_aligned_inertial_matrix()
1069
1070
    range_y = self.inertial_aligned_pts_articular_cylinder[1,:].max() - self.inertial_aligned_pts_articular_cylinder[1,:].min()
1071
    radius = range_y/2
1072
    
1073
    self._radius = radius</code></pre>
1074
</details>
1075
</dd>
1076
<dt id="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_vector"><code class="name flex">
1077
<span>def <span class="ident">guess_vector</span></span>(<span>self)</span>
1078
</code></dt>
1079
<dd>
1080
<div class="desc"></div>
1081
<details class="source">
1082
<summary>
1083
<span>Expand source code</span>
1084
</summary>
1085
<pre><code class="python">def guess_vector(self):
1086
    if self.inertial_matrix_artic_surf is None:
1087
        self.get_inertial_matrix_articular_surface()
1088
    vector = np.asarray([
1089
        self.inertial_matrix_artic_surf[0,0], # vector X
1090
        self.inertial_matrix_artic_surf[1,0], # vector Y
1091
        self.inertial_matrix_artic_surf[2,0], # vector Z
1092
    ], dtype=float)
1093
    
1094
    if np.linalg.norm(vector) != 1:
1095
        vector = vector/np.linalg.norm(vector)
1096
    self._vector = vector</code></pre>
1097
</details>
1098
</dd>
1099
</dl>
1100
</dd>
1101
</dl>
1102
</section>
1103
</article>
1104
<nav id="sidebar">
1105
<h1>Index</h1>
1106
<div class="toc">
1107
<ul></ul>
1108
</div>
1109
<ul id="index">
1110
<li><h3>Super-module</h3>
1111
<ul>
1112
<li><code><a title="pymskt.mesh.anatomical" href="index.html">pymskt.mesh.anatomical</a></code></li>
1113
</ul>
1114
</li>
1115
<li><h3><a href="#header-classes">Classes</a></h3>
1116
<ul>
1117
<li>
1118
<h4><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur">FitCylinderFemur</a></code></h4>
1119
<ul class="">
1120
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.cylinder" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.cylinder">cylinder</a></code></li>
1121
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.cylinder_function" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.cylinder_function">cylinder_function</a></code></li>
1122
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.fit" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.fit">fit</a></code></li>
1123
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_artic_pts_aligned_inertial_matrix" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_artic_pts_aligned_inertial_matrix">get_artic_pts_aligned_inertial_matrix</a></code></li>
1124
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_articular_surf_points" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_articular_surf_points">get_articular_surf_points</a></code></li>
1125
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_bounds" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_bounds">get_bounds</a></code></li>
1126
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_func" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_func">get_func</a></code></li>
1127
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_inertial_matrix_articular_surface" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_inertial_matrix_articular_surface">get_inertial_matrix_articular_surface</a></code></li>
1128
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_initial_parameters" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_initial_parameters">get_initial_parameters</a></code></li>
1129
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_params" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_params">get_params</a></code></li>
1130
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_unit_cylinder" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.get_unit_cylinder">get_unit_cylinder</a></code></li>
1131
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_height" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_height">guess_height</a></code></li>
1132
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_origin" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_origin">guess_origin</a></code></li>
1133
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_radius" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_radius">guess_radius</a></code></li>
1134
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_vector" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.guess_vector">guess_vector</a></code></li>
1135
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.height" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.height">height</a></code></li>
1136
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.origin" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.origin">origin</a></code></li>
1137
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.radius" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.radius">radius</a></code></li>
1138
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.residuals" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.residuals">residuals</a></code></li>
1139
<li><code><a title="pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.vector" href="#pymskt.mesh.anatomical.femur_cylinder.FitCylinderFemur.vector">vector</a></code></li>
1140
</ul>
1141
</li>
1142
</ul>
1143
</li>
1144
</ul>
1145
</nav>
1146
</main>
1147
<footer id="footer">
1148
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p>
1149
</footer>
1150
</body>
1151
</html>