-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_sparse_matrix.py
More file actions
49 lines (40 loc) · 1.53 KB
/
make_sparse_matrix.py
File metadata and controls
49 lines (40 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/local/enthought/bin.python
"""
Script for constructing sparse matrix of images for performing and SVD.
"""
import image_analysis as ia
import numpy as np
import scipy.sparse as sparse
from scipy.sparse.linalg import svds
try:
import cPickle as pickle
except ImportError:
import pickle
#Construct a list of values, rows, and columns for specifying the sparse matrix
data = ia.interpolate_to_center(0)
emittances = np.array(ia.emittance(data))
l, w = data.shape
arr = data.reshape(l*w)
columns, = arr.nonzero()
values = np.array([arr[i] for i in columns])
rows = np.array([0 for i in range(len(values))])
print 'Progress = 0 out of {}'.format(ia.max_number-1)
ia.max_number = 8 #for testing purposes
for num in xrange(1, ia.max_number):
data = ia.interpolate_to_center(num)
emit = np.array(ia.emittance(data))
emittances = np.hstack((emittances, emit))
arr = data.reshape(l*w)
pos, = arr.nonzero()
vals = np.array([arr[i] for i in pos])
new_row = np.array([num for i in range(len(vals))])
columns = np.append(columns, pos)
rows = np.append(rows, new_row)
values = np.append(values, vals)
print 'Progress = {} out of {}'.format(num, ia.max_number-1)
sparse_M = sparse.coo_matrix((values, (rows, columns)), shape =
(ia.max_number, l*w)).tocsc() #ia.max_number
Uk, Sk, Vtk = svds(sparse_M)#, k = 15)
outdict = {'Uk': Uk, 'Sk': Sk, 'Vtk': Vtk, 'emittances': emittances}
with open('B1hor_sparse_SVD.pkl','wb') as outfile:
pickle.dump(outdict, outfile, protocol = -1)