Skip to content

Commit 1ff60d0

Browse files
committed
Replaced missing deprecated "np.mat()" with "np.asmatrix()"
I noticed several 'np.mat()' instances were still present in the package and limited fix mims-harvard#66 . As such, issue mims-harvard#65 was still present. With this update, I've ensured all instances of 'np.mat()' have been replaced to 'np.asmatrix()'. To do so, I've the following shell command: find * -type f -exec sed -i 's/np.mat(/np.asmatrix(/g' {} \; This should fix several broken functions, hopefully making Nimfa compatible with numpy v.2.0.0+. Resolves issue mims-harvard#65 , building on mims-harvard#66 .
1 parent 3564bf6 commit 1ff60d0

18 files changed

Lines changed: 68 additions & 68 deletions

File tree

nimfa/examples/gene_func_prediction.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ class information exploiting properties of Functional Catalogue hierarchy.
162162
if line_type == "@DATA":
163163
section = 'd'
164164
idxs = set(range(idx)).intersection(used_idx)
165-
attr_data = np.mat(np.zeros((1e4, len(attr2idx))))
166-
class_data = np.mat(np.zeros((1e4, len(class2idx))))
165+
attr_data = np.asmatrix(np.zeros((1e4, len(attr2idx))))
166+
class_data = np.asmatrix(np.zeros((1e4, len(class2idx))))
167167
elif section == 'd':
168168
d, _, comment = line.strip().partition("%")
169169
values = d.split(",")
@@ -287,10 +287,10 @@ def compute_correlations(train, test):
287287
# alternative, it is time consuming - can be used for partial evaluation
288288
"""corrs = {}
289289
for i in xrange(test['W'].shape[0]):
290-
corrs.setdefault(i, np.mat(np.zeros((train['W'].shape[0], 1))))
290+
corrs.setdefault(i, np.asmatrix(np.zeros((train['W'].shape[0], 1))))
291291
for j in xrange(train['W'].shape[0]):
292292
corrs[i][j, 0] = _corr(test['W'][i, :], train['W'][j, :])"""
293-
return np.mat(corrs)
293+
return np.asmatrix(corrs)
294294

295295

296296
def _corr(x, y):

nimfa/examples/orl_images.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def read():
159159
im = open(join(dir + str(subject + 1), str(image + 1) + ".pgm"))
160160
# reduce the size of the image
161161
im = im.resize((46, 56))
162-
V[:, 10 * subject + image] = np.mat(np.asarray(im).flatten()).T
162+
V[:, 10 * subject + image] = np.asmatrix(np.asarray(im).flatten()).T
163163
return V
164164

165165

@@ -174,9 +174,9 @@ def preprocess(V):
174174
"""
175175
print("Data preprocessing")
176176
min_val = V.min(axis=0)
177-
V = V - np.mat(np.ones((V.shape[0], 1))) * min_val
177+
V = V - np.asmatrix(np.ones((V.shape[0], 1))) * min_val
178178
max_val = V.max(axis=0) + 1e-4
179-
V = (255. * V) / (np.mat(np.ones((V.shape[0], 1))) * max_val) / 100.
179+
V = (255. * V) / (np.asmatrix(np.ones((V.shape[0], 1))) * max_val) / 100.
180180
return V
181181

182182

nimfa/examples/synthetic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def run_bd(V):
115115
rank = 10
116116
bd = nimfa.Bd(V, seed="random_c", rank=rank, max_iter=12, alpha=np.zeros((V.shape[0], rank)),
117117
beta=np.zeros((rank, V.shape[1])), theta=.0, k=.0, sigma=1., skip=100,
118-
stride=1, n_w=np.mat(np.zeros((rank, 1))), n_h=np.mat(np.zeros((rank, 1))),
118+
stride=1, n_w=np.asmatrix(np.zeros((rank, 1))), n_h=np.asmatrix(np.zeros((rank, 1))),
119119
n_sigma=False)
120120
fit = bd()
121121
print_info(fit)

nimfa/methods/factorization/bd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ def __init__(self, V, seed=None, W=None, H=None, rank=30, max_iter=30,
187187
nmf_std.Nmf_std.__init__(self, vars())
188188
if self.alpha is None:
189189
self.alpha = sp.csr_matrix((self.V.shape[0], self.rank))
190-
self.alpha = self.alpha.tocsr() if sp.isspmatrix(self.alpha) else np.mat(self.alpha)
190+
self.alpha = self.alpha.tocsr() if sp.isspmatrix(self.alpha) else np.asmatrix(self.alpha)
191191
if self.beta is None:
192192
self.beta = sp.csr_matrix((self.rank, self.V.shape[1]))
193-
self.beta = self.beta.tocsr() if sp.isspmatrix(self.beta) else np.mat(self.beta)
193+
self.beta = self.beta.tocsr() if sp.isspmatrix(self.beta) else np.asmatrix(self.beta)
194194
if self.n_w is None:
195195
self.n_w = np.zeros((self.rank, 1))
196196
if self.n_h is None:

nimfa/methods/factorization/icm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ def __init__(self, V, seed=None, W=None, H=None, H1=None,
162162
nmf_std.Nmf_std.__init__(self, vars())
163163
if self.alpha is None:
164164
self.alpha = sp.rand(self.V.shape[0], self.rank, density=0.8, format='csr')
165-
self.alpha= self.alpha.tocsr() if sp.isspmatrix(self.alpha) else np.mat(self.alpha)
165+
self.alpha= self.alpha.tocsr() if sp.isspmatrix(self.alpha) else np.asmatrix(self.alpha)
166166
if self.beta is None:
167167
self.beta = sp.rand(self.rank, self.V.shape[1], density=0.8, format='csr')
168-
self.beta = self.beta.tocsr() if sp.isspmatrix(self.beta) else np.mat(self.beta)
168+
self.beta = self.beta.tocsr() if sp.isspmatrix(self.beta) else np.asmatrix(self.beta)
169169
self.tracker = mf_track.Mf_track() if self.track_factor and self.n_run > 1 \
170170
or self.track_error else None
171171

nimfa/methods/factorization/lfnmf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ def factorize(self):
157157
for run in range(self.n_run):
158158
self.W, self.H = self.seed.initialize(
159159
self.V, self.rank, self.options)
160-
self.Sw, self.Sb = np.mat(
161-
np.zeros((1, 1))), np.mat(np.zeros((1, 1)))
160+
self.Sw, self.Sb = np.asmatrix(
161+
np.zeros((1, 1))), np.asmatrix(np.zeros((1, 1)))
162162
p_obj = c_obj = sys.float_info.max
163163
best_obj = c_obj if run == 0 else best_obj
164164
iter = 0
@@ -244,7 +244,7 @@ def update(self):
244244
# update within class scatter and between class
245245
self.Sw = sum(sum(dot(self.H[:, c2m[i][j]] - avgs[i], (self.H[:, c2m[i][j]] - avgs[i]).T)
246246
for j in range(len(c2m[i]))) for i in c2m)
247-
avgs_t = np.mat(np.zeros((self.rank, 1)))
247+
avgs_t = np.asmatrix(np.zeros((self.rank, 1)))
248248
for k in avgs:
249249
avgs_t += avgs[k]
250250
avgs_t /= len(avgs)
@@ -259,7 +259,7 @@ def _encoding(self, idxH):
259259
c2m.setdefault(idxH[0, i], [])
260260
c2m[idxH[0, i]].append(i)
261261
# compute mean value of class idx in encoding matrix H
262-
avgs.setdefault(idxH[0, i], np.mat(np.zeros((self.rank, 1))))
262+
avgs.setdefault(idxH[0, i], np.asmatrix(np.zeros((self.rank, 1))))
263263
avgs[idxH[0, i]] += self.H[:, i]
264264
for k in avgs:
265265
avgs[k] /= len(c2m[k])

nimfa/methods/factorization/nmf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def conn(self):
303303
cons = elop(mat1, mat2, eq)
304304
if not hasattr(self, 'consold'):
305305
self.cons = cons
306-
self.consold = np.mat(np.logical_not(cons))
306+
self.consold = np.asmatrix(np.logical_not(cons))
307307
else:
308308
self.consold = self.cons
309309
self.cons = cons

nimfa/methods/factorization/pmfcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __init__(self, V, seed=None, W=None, H=None, rank=30, max_iter=30,
133133
self.aseeds = ["random", "fixed", "nndsvd", "random_c", "random_vcol"]
134134
smf.Smf.__init__(self, vars())
135135
if not self.Theta:
136-
self.Theta = np.mat(np.zeros((self.V.shape[1], self.V.shape[1])))
136+
self.Theta = np.asmatrix(np.zeros((self.V.shape[1], self.V.shape[1])))
137137
self.tracker = mf_track.Mf_track() if self.track_factor and self.n_run > 1 \
138138
or self.track_error else None
139139

nimfa/methods/factorization/psmf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def factorize(self):
188188
self.H = self.V.__class__(
189189
(self.rank, self.V.shape[1]), dtype='d')
190190
else:
191-
self.W = np.mat(np.zeros((self.V.shape[0], self.rank)))
192-
self.H = np.mat(np.zeros((self.rank, self.V.shape[1])))
191+
self.W = np.asmatrix(np.zeros((self.V.shape[0], self.rank)))
192+
self.H = np.asmatrix(np.zeros((self.rank, self.V.shape[1])))
193193
self.s = np.zeros((self.V.shape[0], self.N), int)
194194
self.r = np.zeros((self.V.shape[0], 1), int)
195195
self.psi = np.array(std(self.V, axis=1, ddof=0))

nimfa/methods/factorization/sepnmf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def norm_axis(X, p=None, axis=None):
318318
if sp.isspmatrix(X):
319319
return sla.norm(X, ord=p, axis=axis)
320320
else:
321-
n = nla.norm(np.mat(X), ord=p, axis=axis)
321+
n = nla.norm(np.asmatrix(X), ord=p, axis=axis)
322322
if axis is None:
323323
return n
324324
else:

0 commit comments

Comments
 (0)