|
a |
|
b/docs/statistics/pca.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.statistics.pca 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.statistics.pca</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 tracemalloc import start |
|
|
30 |
import numpy as np |
|
|
31 |
from scipy.linalg import svd |
|
|
32 |
import vtk |
|
|
33 |
from vtk.util.numpy_support import numpy_to_vtk |
|
|
34 |
from pymskt.mesh.utils import GIF |
|
|
35 |
|
|
|
36 |
def pca_svd(data): |
|
|
37 |
""" |
|
|
38 |
Calculate eigenvalues & eigenvectors of `data` using Singular Value Decomposition (SVD) |
|
|
39 |
|
|
|
40 |
Parameters |
|
|
41 |
---------- |
|
|
42 |
data : numpy.ndarray |
|
|
43 |
MxN matrix |
|
|
44 |
M = # of features / dimensions of data |
|
|
45 |
N = # of trials / participants in dataset |
|
|
46 |
|
|
|
47 |
Returns |
|
|
48 |
------- |
|
|
49 |
tuple (PC = numpy.ndarray, V = numpy.ndarray) |
|
|
50 |
PC - each volumn is a principal component (eigenvector) |
|
|
51 |
V - Mx1 matrix of variances (coinciding with each PC) |
|
|
52 |
|
|
|
53 |
Notes |
|
|
54 |
----- |
|
|
55 |
Adapted from: |
|
|
56 |
"A Tutorial on Principal Component Analysis by Jonathon Shlens" |
|
|
57 |
https://arxiv.org/abs/1404.1100 |
|
|
58 |
Inputs |
|
|
59 |
data = MxN matrix (M dimensions, N trials) |
|
|
60 |
Returns |
|
|
61 |
PC - each column is a PC |
|
|
62 |
V - Mx1 matrix of variances |
|
|
63 |
""" |
|
|
64 |
M, N = data.shape |
|
|
65 |
mn = np.mean(data, axis=1) |
|
|
66 |
data = data - mn[:, None] # produce centered data. If already centered this shouldnt be harmful. |
|
|
67 |
|
|
|
68 |
Y = data.T / np.sqrt(N - 1) |
|
|
69 |
|
|
|
70 |
U, S, V = svd(Y, full_matrices=False) |
|
|
71 |
PC = V.T # V are the principle components (PC) |
|
|
72 |
V = S ** 2 # The squared singular values are the variances (V) |
|
|
73 |
|
|
|
74 |
return PC, V |
|
|
75 |
|
|
|
76 |
def get_ssm_deformation(PCs, Vs, mean_coords, pc=0, n_sds=3): |
|
|
77 |
""" |
|
|
78 |
Function to Statistical Shape Model (SSM) deformed along given Principal Component. |
|
|
79 |
|
|
|
80 |
Parameters |
|
|
81 |
---------- |
|
|
82 |
PCs : numpy.ndarray |
|
|
83 |
NxM ndarray; N = number of points on surface, M = number of principal components in model |
|
|
84 |
Each column is a principal component. |
|
|
85 |
Vs : numpy.ndarray |
|
|
86 |
M ndarray; M = number of principal components in model |
|
|
87 |
Each entry is the variance for the coinciding principal component in PCs |
|
|
88 |
mean_coords : numpy.ndarray |
|
|
89 |
3xN ndarray; N = number of points on surface. |
|
|
90 |
pc : int, optional |
|
|
91 |
The principal component of the SSM to deform, by default 0 |
|
|
92 |
n_sds : int, optional |
|
|
93 |
The number of standard deviations (sd) to deform the SSM. |
|
|
94 |
This can be positive or negative to scale the model in either direction. , by default 3 |
|
|
95 |
|
|
|
96 |
Returns |
|
|
97 |
------- |
|
|
98 |
numpy.ndarray |
|
|
99 |
3xN ndarray; N=number of points on mesh surface. |
|
|
100 |
This includes the x/y/z position of each surface node after deformation using the SSM and |
|
|
101 |
the specified characteristics (pc, n_sds) |
|
|
102 |
""" |
|
|
103 |
|
|
|
104 |
pc_vector = PCs[:, pc] |
|
|
105 |
pc_vector_scale = np.sqrt(Vs[pc]) * n_sds # convert Variances to SDs & multiply by n_sds (negative/positive important) |
|
|
106 |
coords_deformation = pc_vector * pc_vector_scale |
|
|
107 |
deformed_coords = (mean_coords.flatten() + coords_deformation).reshape(mean_coords.shape) |
|
|
108 |
return deformed_coords |
|
|
109 |
|
|
|
110 |
def get_rand_bone_shape(PCs, Vs, mean_coords, n_pcs=100, n_samples=1, mean_=0., sd_=1.0): |
|
|
111 |
""" |
|
|
112 |
Function to get random bones using a Statistical Shape Model (SSM). |
|
|
113 |
|
|
|
114 |
Parameters |
|
|
115 |
---------- |
|
|
116 |
PCs : numpy.ndarray |
|
|
117 |
NxM ndarray; N = number of points on surface, M = number of principal components in model |
|
|
118 |
Each column is a principal component. |
|
|
119 |
Vs : numpy.ndarray |
|
|
120 |
M ndarray; M = number of principal components in model |
|
|
121 |
Each entry is the variance for the coinciding principal component in PCs |
|
|
122 |
mean_coords : numpy.ndarray |
|
|
123 |
3xN ndarray; N = number of points on surface. |
|
|
124 |
n_pcs : int, optional |
|
|
125 |
Number of PCs to randomly sample from (sequentially), by default 100 |
|
|
126 |
n_samples : int, optional |
|
|
127 |
number of bones to create, by default 1 |
|
|
128 |
mean_ : float, optional |
|
|
129 |
Mean of the normal distribution to sample PCs from, by default 0. |
|
|
130 |
sd_ : float, optional |
|
|
131 |
Standard deviation of the normal distribution to sample PCs from, by default 1.0 |
|
|
132 |
|
|
|
133 |
Returns |
|
|
134 |
------- |
|
|
135 |
numpy.ndarray |
|
|
136 |
nx(3xN) ndarray; N=number of points on mesh surface, n=number of new meshes |
|
|
137 |
This includes the x/y/z position of each surface node(N) for the random bones(n). |
|
|
138 |
""" |
|
|
139 |
|
|
|
140 |
rand_pc_scores = np.random.normal(mean_, sd_, size=[n_samples, n_pcs]) |
|
|
141 |
rand_pc_weights = rand_pc_scores * np.sqrt(Vs[:n_pcs]) |
|
|
142 |
rand_data = rand_pc_weights @ PCs[:, :n_pcs].T |
|
|
143 |
rand_data = mean_coords.flatten() + rand_data |
|
|
144 |
|
|
|
145 |
return rand_data |
|
|
146 |
|
|
|
147 |
def create_vtk_mesh_from_deformed_points(mean_mesh, new_points): |
|
|
148 |
""" |
|
|
149 |
Create new vtk mesh (polydata) from a set of points (ndarray) deformed using the SSM. |
|
|
150 |
|
|
|
151 |
Parameters |
|
|
152 |
---------- |
|
|
153 |
mean_mesh : vtk.PolyData |
|
|
154 |
vtk polydata of the mean mesh |
|
|
155 |
new_points : numpy.ndarray |
|
|
156 |
3xN ndarray; N=number of points on mesh surface (same as number of points on mean_mesh). |
|
|
157 |
This includes the x/y/z position of each surface node should be deformed to. |
|
|
158 |
|
|
|
159 |
Returns |
|
|
160 |
------- |
|
|
161 |
vtk.PolyData |
|
|
162 |
vtk polydata of the deformed mesh |
|
|
163 |
""" |
|
|
164 |
|
|
|
165 |
new_mesh = vtk.vtkPolyData() |
|
|
166 |
new_mesh.DeepCopy(mean_mesh) |
|
|
167 |
new_mesh.GetPoints().SetData(numpy_to_vtk(new_points)) |
|
|
168 |
|
|
|
169 |
return new_mesh |
|
|
170 |
|
|
|
171 |
def save_gif( |
|
|
172 |
path_save, |
|
|
173 |
PCs, |
|
|
174 |
Vs, |
|
|
175 |
mean_coords, # mean_coords could be extracted from mean mesh...? |
|
|
176 |
mean_mesh, |
|
|
177 |
pc=0, |
|
|
178 |
min_sd=-3., |
|
|
179 |
max_sd=3., |
|
|
180 |
step=0.25, |
|
|
181 |
color='orange', |
|
|
182 |
show_edges=True, |
|
|
183 |
edge_color='black', |
|
|
184 |
camera_position='xz', |
|
|
185 |
window_size=[3000, 4000], |
|
|
186 |
background_color='white', |
|
|
187 |
verbose=False, |
|
|
188 |
): |
|
|
189 |
""" |
|
|
190 |
Function to save a gif of the SSM deformation. |
|
|
191 |
|
|
|
192 |
Parameters |
|
|
193 |
---------- |
|
|
194 |
path_save : str |
|
|
195 |
Path to save the gif to. |
|
|
196 |
PCs : numpy.ndarray |
|
|
197 |
SSM Principal Components. |
|
|
198 |
Vs : numpy.ndarray |
|
|
199 |
SSM Variances. |
|
|
200 |
mean_coords : numpy.ndarray |
|
|
201 |
NxM ndarray; N = number of meshes, M = number of points x n_dimensions |
|
|
202 |
mean_mesh : vtk.PolyData |
|
|
203 |
vtk polydata of the mean mesh |
|
|
204 |
pc : int, optional |
|
|
205 |
The principal component of the SSM to deform, by default 0 |
|
|
206 |
min_sd : float, optional |
|
|
207 |
The lower bound (minimum) standard deviations (sd) to deform the SSM from |
|
|
208 |
This can be positive or negative to scale the model in either direction. , by default -3. |
|
|
209 |
max_sd : float, optional |
|
|
210 |
The upper bound (maximum) standard deviations (sd) to deform the SSM from |
|
|
211 |
This can be positive or negative to scale the model in either direction. , by default 3. |
|
|
212 |
step : float, optional |
|
|
213 |
The step size (sd) to deform the SSM by, by default 0.25 |
|
|
214 |
color : str, optional |
|
|
215 |
The color of the SSM surface during rendering, by default 'orange' |
|
|
216 |
show_edges : bool, optional |
|
|
217 |
Whether to show the edges of the SSM surface during rendering, by default True |
|
|
218 |
edge_color : str, optional |
|
|
219 |
The color of the edges of the SSM surface during rendering, by default 'black' |
|
|
220 |
camera_position : str, optional |
|
|
221 |
The camera position to use during rendering, by default 'xz' |
|
|
222 |
window_size : list, optional |
|
|
223 |
The window size to use during rendering, by default [3000, 4000] |
|
|
224 |
background_color : str, optional |
|
|
225 |
The background color to use during rendering, by default 'white' |
|
|
226 |
verbose : bool, optional |
|
|
227 |
Whether to print progress to console, by default False |
|
|
228 |
|
|
|
229 |
|
|
|
230 |
""" |
|
|
231 |
# ALTERNATIVELY... could pass a bunch of the above parameters as kwargs..?? but thats less clear |
|
|
232 |
gif = GIF( |
|
|
233 |
path_save=path_save, |
|
|
234 |
color=color, |
|
|
235 |
show_edges=show_edges, |
|
|
236 |
edge_color=edge_color, |
|
|
237 |
camera_position=camera_position, |
|
|
238 |
window_size=window_size, |
|
|
239 |
background_color=background_color, |
|
|
240 |
) |
|
|
241 |
|
|
|
242 |
for idx, sd in enumerate(np.arange(min_sd, max_sd + step, step)): |
|
|
243 |
if verbose is True: |
|
|
244 |
print(f'Deforming SSM with idx={idx} sd={sd}') |
|
|
245 |
pts = get_ssm_deformation(PCs, Vs, mean_coords, pc=pc, n_sds=sd) |
|
|
246 |
|
|
|
247 |
if type(mean_mesh) == dict: |
|
|
248 |
mesh = [] |
|
|
249 |
start_idx = 0 |
|
|
250 |
for mesh_name, mesh_params in mean_mesh.items(): |
|
|
251 |
mesh.append( |
|
|
252 |
create_vtk_mesh_from_deformed_points( |
|
|
253 |
mesh_params['mesh'], |
|
|
254 |
pts[start_idx:start_idx+mesh_params['n_points'], :], |
|
|
255 |
) |
|
|
256 |
) |
|
|
257 |
start_idx += mesh_params['n_points'] |
|
|
258 |
if type(mean_mesh) in (list, tuple): |
|
|
259 |
mesh = [] |
|
|
260 |
start_idx = 0 |
|
|
261 |
for mesh_ in mean_mesh: |
|
|
262 |
n_pts = mesh_.GetNumberOfPoints() |
|
|
263 |
mesh.append( |
|
|
264 |
create_vtk_mesh_from_deformed_points( |
|
|
265 |
mesh_, |
|
|
266 |
pts[start_idx:start_idx+n_pts, :], |
|
|
267 |
) |
|
|
268 |
) |
|
|
269 |
start_idx += n_pts |
|
|
270 |
|
|
|
271 |
else: |
|
|
272 |
mesh = create_vtk_mesh_from_deformed_points(mean_mesh, pts) |
|
|
273 |
|
|
|
274 |
gif.add_mesh_frame(mesh) |
|
|
275 |
|
|
|
276 |
gif.done()</code></pre> |
|
|
277 |
</details> |
|
|
278 |
</section> |
|
|
279 |
<section> |
|
|
280 |
</section> |
|
|
281 |
<section> |
|
|
282 |
</section> |
|
|
283 |
<section> |
|
|
284 |
<h2 class="section-title" id="header-functions">Functions</h2> |
|
|
285 |
<dl> |
|
|
286 |
<dt id="pymskt.statistics.pca.create_vtk_mesh_from_deformed_points"><code class="name flex"> |
|
|
287 |
<span>def <span class="ident">create_vtk_mesh_from_deformed_points</span></span>(<span>mean_mesh, new_points)</span> |
|
|
288 |
</code></dt> |
|
|
289 |
<dd> |
|
|
290 |
<div class="desc"><p>Create new vtk mesh (polydata) from a set of points (ndarray) deformed using the SSM. </p> |
|
|
291 |
<h2 id="parameters">Parameters</h2> |
|
|
292 |
<dl> |
|
|
293 |
<dt><strong><code>mean_mesh</code></strong> : <code>vtk.PolyData</code></dt> |
|
|
294 |
<dd>vtk polydata of the mean mesh</dd> |
|
|
295 |
<dt><strong><code>new_points</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
296 |
<dd>3xN ndarray; N=number of points on mesh surface (same as number of points on mean_mesh). |
|
|
297 |
This includes the x/y/z position of each surface node should be deformed to.</dd> |
|
|
298 |
</dl> |
|
|
299 |
<h2 id="returns">Returns</h2> |
|
|
300 |
<dl> |
|
|
301 |
<dt><code>vtk.PolyData</code></dt> |
|
|
302 |
<dd>vtk polydata of the deformed mesh</dd> |
|
|
303 |
</dl></div> |
|
|
304 |
<details class="source"> |
|
|
305 |
<summary> |
|
|
306 |
<span>Expand source code</span> |
|
|
307 |
</summary> |
|
|
308 |
<pre><code class="python">def create_vtk_mesh_from_deformed_points(mean_mesh, new_points): |
|
|
309 |
""" |
|
|
310 |
Create new vtk mesh (polydata) from a set of points (ndarray) deformed using the SSM. |
|
|
311 |
|
|
|
312 |
Parameters |
|
|
313 |
---------- |
|
|
314 |
mean_mesh : vtk.PolyData |
|
|
315 |
vtk polydata of the mean mesh |
|
|
316 |
new_points : numpy.ndarray |
|
|
317 |
3xN ndarray; N=number of points on mesh surface (same as number of points on mean_mesh). |
|
|
318 |
This includes the x/y/z position of each surface node should be deformed to. |
|
|
319 |
|
|
|
320 |
Returns |
|
|
321 |
------- |
|
|
322 |
vtk.PolyData |
|
|
323 |
vtk polydata of the deformed mesh |
|
|
324 |
""" |
|
|
325 |
|
|
|
326 |
new_mesh = vtk.vtkPolyData() |
|
|
327 |
new_mesh.DeepCopy(mean_mesh) |
|
|
328 |
new_mesh.GetPoints().SetData(numpy_to_vtk(new_points)) |
|
|
329 |
|
|
|
330 |
return new_mesh</code></pre> |
|
|
331 |
</details> |
|
|
332 |
</dd> |
|
|
333 |
<dt id="pymskt.statistics.pca.get_rand_bone_shape"><code class="name flex"> |
|
|
334 |
<span>def <span class="ident">get_rand_bone_shape</span></span>(<span>PCs, Vs, mean_coords, n_pcs=100, n_samples=1, mean_=0.0, sd_=1.0)</span> |
|
|
335 |
</code></dt> |
|
|
336 |
<dd> |
|
|
337 |
<div class="desc"><p>Function to get random bones using a Statistical Shape Model (SSM).</p> |
|
|
338 |
<h2 id="parameters">Parameters</h2> |
|
|
339 |
<dl> |
|
|
340 |
<dt><strong><code>PCs</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
341 |
<dd>NxM ndarray; N = number of points on surface, M = number of principal components in model |
|
|
342 |
Each column is a principal component.</dd> |
|
|
343 |
<dt><strong><code>Vs</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
344 |
<dd>M ndarray; M = number of principal components in model |
|
|
345 |
Each entry is the variance for the coinciding principal component in PCs</dd> |
|
|
346 |
<dt><strong><code>mean_coords</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
347 |
<dd>3xN ndarray; N = number of points on surface.</dd> |
|
|
348 |
<dt><strong><code>n_pcs</code></strong> : <code>int</code>, optional</dt> |
|
|
349 |
<dd>Number of PCs to randomly sample from (sequentially), by default 100</dd> |
|
|
350 |
<dt><strong><code>n_samples</code></strong> : <code>int</code>, optional</dt> |
|
|
351 |
<dd>number of bones to create, by default 1</dd> |
|
|
352 |
<dt><strong><code>mean_</code></strong> : <code>float</code>, optional</dt> |
|
|
353 |
<dd>Mean of the normal distribution to sample PCs from, by default 0.</dd> |
|
|
354 |
<dt><strong><code>sd_</code></strong> : <code>float</code>, optional</dt> |
|
|
355 |
<dd>Standard deviation of the normal distribution to sample PCs from, by default 1.0</dd> |
|
|
356 |
</dl> |
|
|
357 |
<h2 id="returns">Returns</h2> |
|
|
358 |
<dl> |
|
|
359 |
<dt><code>numpy.ndarray</code></dt> |
|
|
360 |
<dd>nx(3xN) ndarray; N=number of points on mesh surface, n=number of new meshes |
|
|
361 |
This includes the x/y/z position of each surface node(N) for the random bones(n).</dd> |
|
|
362 |
</dl></div> |
|
|
363 |
<details class="source"> |
|
|
364 |
<summary> |
|
|
365 |
<span>Expand source code</span> |
|
|
366 |
</summary> |
|
|
367 |
<pre><code class="python">def get_rand_bone_shape(PCs, Vs, mean_coords, n_pcs=100, n_samples=1, mean_=0., sd_=1.0): |
|
|
368 |
""" |
|
|
369 |
Function to get random bones using a Statistical Shape Model (SSM). |
|
|
370 |
|
|
|
371 |
Parameters |
|
|
372 |
---------- |
|
|
373 |
PCs : numpy.ndarray |
|
|
374 |
NxM ndarray; N = number of points on surface, M = number of principal components in model |
|
|
375 |
Each column is a principal component. |
|
|
376 |
Vs : numpy.ndarray |
|
|
377 |
M ndarray; M = number of principal components in model |
|
|
378 |
Each entry is the variance for the coinciding principal component in PCs |
|
|
379 |
mean_coords : numpy.ndarray |
|
|
380 |
3xN ndarray; N = number of points on surface. |
|
|
381 |
n_pcs : int, optional |
|
|
382 |
Number of PCs to randomly sample from (sequentially), by default 100 |
|
|
383 |
n_samples : int, optional |
|
|
384 |
number of bones to create, by default 1 |
|
|
385 |
mean_ : float, optional |
|
|
386 |
Mean of the normal distribution to sample PCs from, by default 0. |
|
|
387 |
sd_ : float, optional |
|
|
388 |
Standard deviation of the normal distribution to sample PCs from, by default 1.0 |
|
|
389 |
|
|
|
390 |
Returns |
|
|
391 |
------- |
|
|
392 |
numpy.ndarray |
|
|
393 |
nx(3xN) ndarray; N=number of points on mesh surface, n=number of new meshes |
|
|
394 |
This includes the x/y/z position of each surface node(N) for the random bones(n). |
|
|
395 |
""" |
|
|
396 |
|
|
|
397 |
rand_pc_scores = np.random.normal(mean_, sd_, size=[n_samples, n_pcs]) |
|
|
398 |
rand_pc_weights = rand_pc_scores * np.sqrt(Vs[:n_pcs]) |
|
|
399 |
rand_data = rand_pc_weights @ PCs[:, :n_pcs].T |
|
|
400 |
rand_data = mean_coords.flatten() + rand_data |
|
|
401 |
|
|
|
402 |
return rand_data </code></pre> |
|
|
403 |
</details> |
|
|
404 |
</dd> |
|
|
405 |
<dt id="pymskt.statistics.pca.get_ssm_deformation"><code class="name flex"> |
|
|
406 |
<span>def <span class="ident">get_ssm_deformation</span></span>(<span>PCs, Vs, mean_coords, pc=0, n_sds=3)</span> |
|
|
407 |
</code></dt> |
|
|
408 |
<dd> |
|
|
409 |
<div class="desc"><p>Function to Statistical Shape Model (SSM) deformed along given Principal Component.</p> |
|
|
410 |
<h2 id="parameters">Parameters</h2> |
|
|
411 |
<dl> |
|
|
412 |
<dt><strong><code>PCs</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
413 |
<dd>NxM ndarray; N = number of points on surface, M = number of principal components in model |
|
|
414 |
Each column is a principal component.</dd> |
|
|
415 |
<dt><strong><code>Vs</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
416 |
<dd>M ndarray; M = number of principal components in model |
|
|
417 |
Each entry is the variance for the coinciding principal component in PCs</dd> |
|
|
418 |
<dt><strong><code>mean_coords</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
419 |
<dd>3xN ndarray; N = number of points on surface.</dd> |
|
|
420 |
<dt><strong><code>pc</code></strong> : <code>int</code>, optional</dt> |
|
|
421 |
<dd>The principal component of the SSM to deform, by default 0</dd> |
|
|
422 |
<dt><strong><code>n_sds</code></strong> : <code>int</code>, optional</dt> |
|
|
423 |
<dd>The number of standard deviations (sd) to deform the SSM. |
|
|
424 |
This can be positive or negative to scale the model in either direction. , by default 3</dd> |
|
|
425 |
</dl> |
|
|
426 |
<h2 id="returns">Returns</h2> |
|
|
427 |
<dl> |
|
|
428 |
<dt><code>numpy.ndarray</code></dt> |
|
|
429 |
<dd>3xN ndarray; N=number of points on mesh surface. |
|
|
430 |
This includes the x/y/z position of each surface node after deformation using the SSM and |
|
|
431 |
the specified characteristics (pc, n_sds)</dd> |
|
|
432 |
</dl></div> |
|
|
433 |
<details class="source"> |
|
|
434 |
<summary> |
|
|
435 |
<span>Expand source code</span> |
|
|
436 |
</summary> |
|
|
437 |
<pre><code class="python">def get_ssm_deformation(PCs, Vs, mean_coords, pc=0, n_sds=3): |
|
|
438 |
""" |
|
|
439 |
Function to Statistical Shape Model (SSM) deformed along given Principal Component. |
|
|
440 |
|
|
|
441 |
Parameters |
|
|
442 |
---------- |
|
|
443 |
PCs : numpy.ndarray |
|
|
444 |
NxM ndarray; N = number of points on surface, M = number of principal components in model |
|
|
445 |
Each column is a principal component. |
|
|
446 |
Vs : numpy.ndarray |
|
|
447 |
M ndarray; M = number of principal components in model |
|
|
448 |
Each entry is the variance for the coinciding principal component in PCs |
|
|
449 |
mean_coords : numpy.ndarray |
|
|
450 |
3xN ndarray; N = number of points on surface. |
|
|
451 |
pc : int, optional |
|
|
452 |
The principal component of the SSM to deform, by default 0 |
|
|
453 |
n_sds : int, optional |
|
|
454 |
The number of standard deviations (sd) to deform the SSM. |
|
|
455 |
This can be positive or negative to scale the model in either direction. , by default 3 |
|
|
456 |
|
|
|
457 |
Returns |
|
|
458 |
------- |
|
|
459 |
numpy.ndarray |
|
|
460 |
3xN ndarray; N=number of points on mesh surface. |
|
|
461 |
This includes the x/y/z position of each surface node after deformation using the SSM and |
|
|
462 |
the specified characteristics (pc, n_sds) |
|
|
463 |
""" |
|
|
464 |
|
|
|
465 |
pc_vector = PCs[:, pc] |
|
|
466 |
pc_vector_scale = np.sqrt(Vs[pc]) * n_sds # convert Variances to SDs & multiply by n_sds (negative/positive important) |
|
|
467 |
coords_deformation = pc_vector * pc_vector_scale |
|
|
468 |
deformed_coords = (mean_coords.flatten() + coords_deformation).reshape(mean_coords.shape) |
|
|
469 |
return deformed_coords</code></pre> |
|
|
470 |
</details> |
|
|
471 |
</dd> |
|
|
472 |
<dt id="pymskt.statistics.pca.pca_svd"><code class="name flex"> |
|
|
473 |
<span>def <span class="ident">pca_svd</span></span>(<span>data)</span> |
|
|
474 |
</code></dt> |
|
|
475 |
<dd> |
|
|
476 |
<div class="desc"><p>Calculate eigenvalues & eigenvectors of <code>data</code> using Singular Value Decomposition (SVD)</p> |
|
|
477 |
<h2 id="parameters">Parameters</h2> |
|
|
478 |
<dl> |
|
|
479 |
<dt><strong><code>data</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
480 |
<dd>MxN matrix |
|
|
481 |
M = # of features / dimensions of data |
|
|
482 |
N = # of trials / participants in dataset</dd> |
|
|
483 |
</dl> |
|
|
484 |
<h2 id="returns">Returns</h2> |
|
|
485 |
<dl> |
|
|
486 |
<dt><code>tuple (PC = numpy.ndarray, V = numpy.ndarray)</code></dt> |
|
|
487 |
<dd>PC - each volumn is a principal component (eigenvector) |
|
|
488 |
V - Mx1 matrix of variances (coinciding with each PC)</dd> |
|
|
489 |
</dl> |
|
|
490 |
<h2 id="notes">Notes</h2> |
|
|
491 |
<p>Adapted from: |
|
|
492 |
"A Tutorial on Principal Component Analysis by Jonathon Shlens" |
|
|
493 |
<a href="https://arxiv.org/abs/1404.1100">https://arxiv.org/abs/1404.1100</a> |
|
|
494 |
Inputs |
|
|
495 |
data = MxN matrix (M dimensions, N trials) |
|
|
496 |
Returns |
|
|
497 |
PC - each column is a PC |
|
|
498 |
V - Mx1 matrix of variances</p></div> |
|
|
499 |
<details class="source"> |
|
|
500 |
<summary> |
|
|
501 |
<span>Expand source code</span> |
|
|
502 |
</summary> |
|
|
503 |
<pre><code class="python">def pca_svd(data): |
|
|
504 |
""" |
|
|
505 |
Calculate eigenvalues & eigenvectors of `data` using Singular Value Decomposition (SVD) |
|
|
506 |
|
|
|
507 |
Parameters |
|
|
508 |
---------- |
|
|
509 |
data : numpy.ndarray |
|
|
510 |
MxN matrix |
|
|
511 |
M = # of features / dimensions of data |
|
|
512 |
N = # of trials / participants in dataset |
|
|
513 |
|
|
|
514 |
Returns |
|
|
515 |
------- |
|
|
516 |
tuple (PC = numpy.ndarray, V = numpy.ndarray) |
|
|
517 |
PC - each volumn is a principal component (eigenvector) |
|
|
518 |
V - Mx1 matrix of variances (coinciding with each PC) |
|
|
519 |
|
|
|
520 |
Notes |
|
|
521 |
----- |
|
|
522 |
Adapted from: |
|
|
523 |
"A Tutorial on Principal Component Analysis by Jonathon Shlens" |
|
|
524 |
https://arxiv.org/abs/1404.1100 |
|
|
525 |
Inputs |
|
|
526 |
data = MxN matrix (M dimensions, N trials) |
|
|
527 |
Returns |
|
|
528 |
PC - each column is a PC |
|
|
529 |
V - Mx1 matrix of variances |
|
|
530 |
""" |
|
|
531 |
M, N = data.shape |
|
|
532 |
mn = np.mean(data, axis=1) |
|
|
533 |
data = data - mn[:, None] # produce centered data. If already centered this shouldnt be harmful. |
|
|
534 |
|
|
|
535 |
Y = data.T / np.sqrt(N - 1) |
|
|
536 |
|
|
|
537 |
U, S, V = svd(Y, full_matrices=False) |
|
|
538 |
PC = V.T # V are the principle components (PC) |
|
|
539 |
V = S ** 2 # The squared singular values are the variances (V) |
|
|
540 |
|
|
|
541 |
return PC, V</code></pre> |
|
|
542 |
</details> |
|
|
543 |
</dd> |
|
|
544 |
<dt id="pymskt.statistics.pca.save_gif"><code class="name flex"> |
|
|
545 |
<span>def <span class="ident">save_gif</span></span>(<span>path_save, PCs, Vs, mean_coords, mean_mesh, pc=0, min_sd=-3.0, max_sd=3.0, step=0.25, color='orange', show_edges=True, edge_color='black', camera_position='xz', window_size=[3000, 4000], background_color='white', verbose=False)</span> |
|
|
546 |
</code></dt> |
|
|
547 |
<dd> |
|
|
548 |
<div class="desc"><p>Function to save a gif of the SSM deformation.</p> |
|
|
549 |
<h2 id="parameters">Parameters</h2> |
|
|
550 |
<dl> |
|
|
551 |
<dt><strong><code>path_save</code></strong> : <code>str</code></dt> |
|
|
552 |
<dd>Path to save the gif to.</dd> |
|
|
553 |
<dt><strong><code>PCs</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
554 |
<dd>SSM Principal Components.</dd> |
|
|
555 |
<dt><strong><code>Vs</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
556 |
<dd>SSM Variances.</dd> |
|
|
557 |
<dt><strong><code>mean_coords</code></strong> : <code>numpy.ndarray</code></dt> |
|
|
558 |
<dd>NxM ndarray; N = number of meshes, M = number of points x n_dimensions</dd> |
|
|
559 |
<dt><strong><code>mean_mesh</code></strong> : <code>vtk.PolyData</code></dt> |
|
|
560 |
<dd>vtk polydata of the mean mesh</dd> |
|
|
561 |
<dt><strong><code>pc</code></strong> : <code>int</code>, optional</dt> |
|
|
562 |
<dd>The principal component of the SSM to deform, by default 0</dd> |
|
|
563 |
<dt><strong><code>min_sd</code></strong> : <code>float</code>, optional</dt> |
|
|
564 |
<dd>The lower bound (minimum) standard deviations (sd) to deform the SSM from |
|
|
565 |
This can be positive or negative to scale the model in either direction. , by default -3.</dd> |
|
|
566 |
<dt><strong><code>max_sd</code></strong> : <code>float</code>, optional</dt> |
|
|
567 |
<dd>The upper bound (maximum) standard deviations (sd) to deform the SSM from |
|
|
568 |
This can be positive or negative to scale the model in either direction. , by default 3.</dd> |
|
|
569 |
<dt><strong><code>step</code></strong> : <code>float</code>, optional</dt> |
|
|
570 |
<dd>The step size (sd) to deform the SSM by, by default 0.25</dd> |
|
|
571 |
<dt><strong><code>color</code></strong> : <code>str</code>, optional</dt> |
|
|
572 |
<dd>The color of the SSM surface during rendering, by default 'orange'</dd> |
|
|
573 |
<dt><strong><code>show_edges</code></strong> : <code>bool</code>, optional</dt> |
|
|
574 |
<dd>Whether to show the edges of the SSM surface during rendering, by default True</dd> |
|
|
575 |
<dt><strong><code>edge_color</code></strong> : <code>str</code>, optional</dt> |
|
|
576 |
<dd>The color of the edges of the SSM surface during rendering, by default 'black'</dd> |
|
|
577 |
<dt><strong><code>camera_position</code></strong> : <code>str</code>, optional</dt> |
|
|
578 |
<dd>The camera position to use during rendering, by default 'xz'</dd> |
|
|
579 |
<dt><strong><code>window_size</code></strong> : <code>list</code>, optional</dt> |
|
|
580 |
<dd>The window size to use during rendering, by default [3000, 4000]</dd> |
|
|
581 |
<dt><strong><code>background_color</code></strong> : <code>str</code>, optional</dt> |
|
|
582 |
<dd>The background color to use during rendering, by default 'white'</dd> |
|
|
583 |
<dt><strong><code>verbose</code></strong> : <code>bool</code>, optional</dt> |
|
|
584 |
<dd>Whether to print progress to console, by default False</dd> |
|
|
585 |
</dl></div> |
|
|
586 |
<details class="source"> |
|
|
587 |
<summary> |
|
|
588 |
<span>Expand source code</span> |
|
|
589 |
</summary> |
|
|
590 |
<pre><code class="python">def save_gif( |
|
|
591 |
path_save, |
|
|
592 |
PCs, |
|
|
593 |
Vs, |
|
|
594 |
mean_coords, # mean_coords could be extracted from mean mesh...? |
|
|
595 |
mean_mesh, |
|
|
596 |
pc=0, |
|
|
597 |
min_sd=-3., |
|
|
598 |
max_sd=3., |
|
|
599 |
step=0.25, |
|
|
600 |
color='orange', |
|
|
601 |
show_edges=True, |
|
|
602 |
edge_color='black', |
|
|
603 |
camera_position='xz', |
|
|
604 |
window_size=[3000, 4000], |
|
|
605 |
background_color='white', |
|
|
606 |
verbose=False, |
|
|
607 |
): |
|
|
608 |
""" |
|
|
609 |
Function to save a gif of the SSM deformation. |
|
|
610 |
|
|
|
611 |
Parameters |
|
|
612 |
---------- |
|
|
613 |
path_save : str |
|
|
614 |
Path to save the gif to. |
|
|
615 |
PCs : numpy.ndarray |
|
|
616 |
SSM Principal Components. |
|
|
617 |
Vs : numpy.ndarray |
|
|
618 |
SSM Variances. |
|
|
619 |
mean_coords : numpy.ndarray |
|
|
620 |
NxM ndarray; N = number of meshes, M = number of points x n_dimensions |
|
|
621 |
mean_mesh : vtk.PolyData |
|
|
622 |
vtk polydata of the mean mesh |
|
|
623 |
pc : int, optional |
|
|
624 |
The principal component of the SSM to deform, by default 0 |
|
|
625 |
min_sd : float, optional |
|
|
626 |
The lower bound (minimum) standard deviations (sd) to deform the SSM from |
|
|
627 |
This can be positive or negative to scale the model in either direction. , by default -3. |
|
|
628 |
max_sd : float, optional |
|
|
629 |
The upper bound (maximum) standard deviations (sd) to deform the SSM from |
|
|
630 |
This can be positive or negative to scale the model in either direction. , by default 3. |
|
|
631 |
step : float, optional |
|
|
632 |
The step size (sd) to deform the SSM by, by default 0.25 |
|
|
633 |
color : str, optional |
|
|
634 |
The color of the SSM surface during rendering, by default 'orange' |
|
|
635 |
show_edges : bool, optional |
|
|
636 |
Whether to show the edges of the SSM surface during rendering, by default True |
|
|
637 |
edge_color : str, optional |
|
|
638 |
The color of the edges of the SSM surface during rendering, by default 'black' |
|
|
639 |
camera_position : str, optional |
|
|
640 |
The camera position to use during rendering, by default 'xz' |
|
|
641 |
window_size : list, optional |
|
|
642 |
The window size to use during rendering, by default [3000, 4000] |
|
|
643 |
background_color : str, optional |
|
|
644 |
The background color to use during rendering, by default 'white' |
|
|
645 |
verbose : bool, optional |
|
|
646 |
Whether to print progress to console, by default False |
|
|
647 |
|
|
|
648 |
|
|
|
649 |
""" |
|
|
650 |
# ALTERNATIVELY... could pass a bunch of the above parameters as kwargs..?? but thats less clear |
|
|
651 |
gif = GIF( |
|
|
652 |
path_save=path_save, |
|
|
653 |
color=color, |
|
|
654 |
show_edges=show_edges, |
|
|
655 |
edge_color=edge_color, |
|
|
656 |
camera_position=camera_position, |
|
|
657 |
window_size=window_size, |
|
|
658 |
background_color=background_color, |
|
|
659 |
) |
|
|
660 |
|
|
|
661 |
for idx, sd in enumerate(np.arange(min_sd, max_sd + step, step)): |
|
|
662 |
if verbose is True: |
|
|
663 |
print(f'Deforming SSM with idx={idx} sd={sd}') |
|
|
664 |
pts = get_ssm_deformation(PCs, Vs, mean_coords, pc=pc, n_sds=sd) |
|
|
665 |
|
|
|
666 |
if type(mean_mesh) == dict: |
|
|
667 |
mesh = [] |
|
|
668 |
start_idx = 0 |
|
|
669 |
for mesh_name, mesh_params in mean_mesh.items(): |
|
|
670 |
mesh.append( |
|
|
671 |
create_vtk_mesh_from_deformed_points( |
|
|
672 |
mesh_params['mesh'], |
|
|
673 |
pts[start_idx:start_idx+mesh_params['n_points'], :], |
|
|
674 |
) |
|
|
675 |
) |
|
|
676 |
start_idx += mesh_params['n_points'] |
|
|
677 |
if type(mean_mesh) in (list, tuple): |
|
|
678 |
mesh = [] |
|
|
679 |
start_idx = 0 |
|
|
680 |
for mesh_ in mean_mesh: |
|
|
681 |
n_pts = mesh_.GetNumberOfPoints() |
|
|
682 |
mesh.append( |
|
|
683 |
create_vtk_mesh_from_deformed_points( |
|
|
684 |
mesh_, |
|
|
685 |
pts[start_idx:start_idx+n_pts, :], |
|
|
686 |
) |
|
|
687 |
) |
|
|
688 |
start_idx += n_pts |
|
|
689 |
|
|
|
690 |
else: |
|
|
691 |
mesh = create_vtk_mesh_from_deformed_points(mean_mesh, pts) |
|
|
692 |
|
|
|
693 |
gif.add_mesh_frame(mesh) |
|
|
694 |
|
|
|
695 |
gif.done()</code></pre> |
|
|
696 |
</details> |
|
|
697 |
</dd> |
|
|
698 |
</dl> |
|
|
699 |
</section> |
|
|
700 |
<section> |
|
|
701 |
</section> |
|
|
702 |
</article> |
|
|
703 |
<nav id="sidebar"> |
|
|
704 |
<h1>Index</h1> |
|
|
705 |
<div class="toc"> |
|
|
706 |
<ul></ul> |
|
|
707 |
</div> |
|
|
708 |
<ul id="index"> |
|
|
709 |
<li><h3>Super-module</h3> |
|
|
710 |
<ul> |
|
|
711 |
<li><code><a title="pymskt.statistics" href="index.html">pymskt.statistics</a></code></li> |
|
|
712 |
</ul> |
|
|
713 |
</li> |
|
|
714 |
<li><h3><a href="#header-functions">Functions</a></h3> |
|
|
715 |
<ul class=""> |
|
|
716 |
<li><code><a title="pymskt.statistics.pca.create_vtk_mesh_from_deformed_points" href="#pymskt.statistics.pca.create_vtk_mesh_from_deformed_points">create_vtk_mesh_from_deformed_points</a></code></li> |
|
|
717 |
<li><code><a title="pymskt.statistics.pca.get_rand_bone_shape" href="#pymskt.statistics.pca.get_rand_bone_shape">get_rand_bone_shape</a></code></li> |
|
|
718 |
<li><code><a title="pymskt.statistics.pca.get_ssm_deformation" href="#pymskt.statistics.pca.get_ssm_deformation">get_ssm_deformation</a></code></li> |
|
|
719 |
<li><code><a title="pymskt.statistics.pca.pca_svd" href="#pymskt.statistics.pca.pca_svd">pca_svd</a></code></li> |
|
|
720 |
<li><code><a title="pymskt.statistics.pca.save_gif" href="#pymskt.statistics.pca.save_gif">save_gif</a></code></li> |
|
|
721 |
</ul> |
|
|
722 |
</li> |
|
|
723 |
</ul> |
|
|
724 |
</nav> |
|
|
725 |
</main> |
|
|
726 |
<footer id="footer"> |
|
|
727 |
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p> |
|
|
728 |
</footer> |
|
|
729 |
</body> |
|
|
730 |
</html> |