Skip to content

Compatibility with numpy 1.24.x #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions pyttb/ktensor.py
Original file line number Diff line number Diff line change
@@ -116,14 +116,14 @@ def from_data(cls, weights, *factor_matrices):
# Create ktensor and populate data members
k = cls()
k.weights = weights.copy()
if k.weights.dtype != np.float:
print("converting weights from {} to np.float".format(k.weights.dtype))
k.weights = k.weights.astype(np.float)
if k.weights.dtype != float:
print("converting weights from {} to float".format(k.weights.dtype))
k.weights = k.weights.astype(float)
k.factor_matrices = _factor_matrices
for i in range(len(k.factor_matrices)):
if k.factor_matrices[i].dtype != np.float:
print("converting factor_matrices[{}] from {} to np.float".format(i, k.factor_matrices[i].dtype))
k.factor_matrices[i] = k.factor_matrices[i].astype(np.float)
if k.factor_matrices[i].dtype != float:
print("converting factor_matrices[{}] from {} to float".format(i, k.factor_matrices[i].dtype))
k.factor_matrices[i] = k.factor_matrices[i].astype(float)

return k

@@ -358,7 +358,7 @@ def from_vector(cls, data, shape, contains_weights):
>>> rank = 2
>>> shape = np.array([2, 3, 4])
>>> data = np.arange(1, rank*sum(shape)+1).astype(np.float)
>>> data = np.arange(1, rank*sum(shape)+1).astype(float)
>>> K_without_weights = ttb.ktensor.from_vector(data[:], shape, False)
>>> print(K_without_weights)
ktensor of shape 2 x 3 x 4
@@ -378,7 +378,7 @@ def from_vector(cls, data, shape, contains_weights):
Create a `ktensor` from a vector containing elements of both the weights and the factor matrices:
>>> weights = 2 * np.ones(rank).astype(np.float)
>>> weights = 2 * np.ones(rank).astype(float)
>>> weights_and_data = np.concatenate((weights, data), axis=0)
>>> K_with_weights = ttb.ktensor.from_vector(weights_and_data[:], shape, True)
>>> print(K_with_weights)
@@ -882,7 +882,7 @@ def innerprod(self, other):
vecs = []
for n in range(self.ndims):
vecs.append(self.factor_matrices[n][:, r])
res = res + self.weights[r] * other.ttv(np.array(vecs))
res = res + self.weights[r] * other.ttv(vecs)
return res

def isequal(self, other):
@@ -1479,7 +1479,7 @@ def score(self, other, weight_penalty=True, threshold=0.99, greedy=True):

# Option to do greedy matching
if greedy:
best_perm = -1 * np.ones((RA), dtype=np.int)
best_perm = -1 * np.ones((RA), dtype=int)
best_score = 0
for r in range(RB):
idx = np.argmax(C.reshape(np.prod(C.shape),order='F'))
@@ -1807,11 +1807,11 @@ def ttv(self, vector, dims=None):
dims = np.array([dims])

# Check that vector is a list of vectors, if not place single vector as element in list
if len(vector.shape) == 1 and isinstance(vector[0], (int, float, np.int_, np.float_)):
return self.ttv(np.array([vector]), dims)
if len(vector) > 0 and isinstance(vector[0], (int, float, np.int_, np.float_)):
return self.ttv([vector], dims)

# Get sorted dims and index for multiplicands
dims, vidx = ttb.tt_dimscheck(dims, self.ndims, vector.shape[0])
dims, vidx = ttb.tt_dimscheck(dims, self.ndims, len(vector))

# Check that each multiplicand is the right size.
for i in range(dims.size):
12 changes: 6 additions & 6 deletions pyttb/sptensor.py
Original file line number Diff line number Diff line change
@@ -137,9 +137,9 @@ def from_function(cls, function_handle, shape, nonzeros):
if (nonzeros < 0) or (nonzeros >= np.prod(shape)):
assert False, "Requested number of non-zeros must be positive and less than the total size"
elif nonzeros < 1:
nonzeros = np.int(np.ceil(np.prod(shape) * nonzeros))
nonzeros = int(np.ceil(np.prod(shape) * nonzeros))
else:
nonzeros = np.int(np.floor(nonzeros))
nonzeros = int(np.floor(nonzeros))

# Keep iterating until we find enough unique non-zeros or we give up
subs = np.array([])
@@ -731,7 +731,7 @@ def mttkrp(self, U, n):
else:
Z.append(np.array([]))
# Perform ttv multiplication
V[:, r] = self.ttv(np.array(Z), -(n+1)).double()
V[:, r] = self.ttv(Z, -(n+1)).double()

return V

@@ -1027,11 +1027,11 @@ def ttv(self, vector, dims=None):
dims = np.array([dims])

# Check that vector is a list of vectors, if not place single vector as element in list
if len(vector.shape) == 1 and isinstance(vector[0], (int, float, np.int_, np.float_)):
return self.ttv(np.array([vector]), dims)
if len(vector) > 0 and isinstance(vector[0], (int, float, np.int_, np.float_)):
return self.ttv([vector], dims)

# Get sorted dims and index for multiplicands
dims, vidx = ttb.tt_dimscheck(dims, self.ndims, vector.shape[0])
dims, vidx = ttb.tt_dimscheck(dims, self.ndims, len(vector))
remdims = np.setdiff1d(np.arange(0, self.ndims), dims).astype(int)

# Check that each multiplicand is the right size.
10 changes: 4 additions & 6 deletions pyttb/tensor.py
Original file line number Diff line number Diff line change
@@ -1000,13 +1000,11 @@ def ttv(self, vector, dims=None):
dims = np.array([dims])

# Check that vector is a list of vectors, if not place single vector as element in list
if isinstance(vector, list):
return self.ttv(np.array(vector), dims)
if len(vector.shape) == 1 and isinstance(vector[0], (int, float, np.int_, np.float_)):
return self.ttv(np.array([vector]), dims)
if len(vector) > 0 and isinstance(vector[0], (int, float, np.int_, np.float_)):
return self.ttv([vector], dims)

# Get sorted dims and index for multiplicands
dims, vidx = ttb.tt_dimscheck(dims, self.ndims, vector.shape[0])
dims, vidx = ttb.tt_dimscheck(dims, self.ndims, len(vector))

# Check that each multiplicand is the right size.
for i in range(dims.size):
@@ -1076,7 +1074,7 @@ def ttsv(self, vector, dims=None, version = None):
if dnew == 2:
return np.reshape(y, [sz, sz], order='F')
elif dnew > 2:
return ttb.tensor.from_data(np.reshape(y, sz*np.ones(dnew, dtype=np.int), order='F'))
return ttb.tensor.from_data(np.reshape(y, sz*np.ones(dnew, dtype=int), order='F'))
else:
return y
else:
18 changes: 9 additions & 9 deletions tests/test_ktensor.py
Original file line number Diff line number Diff line change
@@ -21,8 +21,8 @@ def sample_ktensor_2way():
def sample_ktensor_3way():
rank = 2
shape = np.array([2, 3, 4])
vector = np.arange(1, rank*sum(shape)+1).astype(np.float)
weights = 2 * np.ones(rank).astype(np.float)
vector = np.arange(1, rank*sum(shape)+1).astype(float)
weights = 2 * np.ones(rank).astype(float)
vector_with_weights = np.concatenate((weights, vector), axis=0)
#vector_with_weights = vector_with_weights.reshape((len(vector_with_weights), 1))
# ground truth
@@ -100,17 +100,17 @@ def test_ktensor_from_data(sample_ktensor_2way, capsys):
weights_int = np.array([1, 2])
K2 = ttb.ktensor.from_data(weights_int, data["factor_matrices"])
out, err = capsys.readouterr()
assert "converting weights from int64 to np.float" in out or \
"converting weights from int32 to np.float" in out
assert "converting weights from int64 to float" in out or \
"converting weights from int32 to float" in out

# Weights that are int should be converted
fm0 = np.array([[1, 2], [3, 4]])
fm1 = np.array([[5, 6], [7, 8]])
factor_matrices = [fm0, fm1]
K3 = ttb.ktensor.from_data(data["weights"], factor_matrices)
out, err = capsys.readouterr()
assert "converting factor_matrices[0] from int64 to np.float" in out or \
"converting factor_matrices[0] from int32 to np.float" in out
assert "converting factor_matrices[0] from int64 to float" in out or \
"converting factor_matrices[0] from int32 to float" in out

@pytest.mark.indevelopment
def test_ktensor_from_function():
@@ -762,15 +762,15 @@ def test_ktensor_ttv(sample_ktensor_3way):
vec2 = np.array([1, 1])
vec3 = np.array([1, 1, 1])
vec4 = np.array([1, 1, 1, 1])
assert K.ttv(np.array([vec2, vec3, vec4])) == 30348
assert K.ttv([vec2, vec3, vec4]) == 30348

# Wrong shape
with pytest.raises(AssertionError) as excinfo:
K.ttv(np.array([vec2, vec3, np.array([1,2])]))
K.ttv([vec2, vec3, np.array([1,2])])
assert "Multiplicand is wrong size" in str(excinfo)

# Multiple dimensions, but fewer than all dimensions, not in same order as ktensor dimensions
K2 = K.ttv(np.array([vec4, vec3]), dims=np.array([2, 1]))
K2 = K.ttv([vec4, vec3], dims=np.array([2, 1]))
weights = np.array([1800., 3564.])
fm0 = np.array([[1., 3.], [2., 4.]])
assert (K2.isequal(ttb.ktensor.from_data(weights, fm0)))
6 changes: 3 additions & 3 deletions tests/test_sptensor.py
Original file line number Diff line number Diff line change
@@ -186,11 +186,11 @@ def test_sptensor_full(sample_sptensor):
def test_sptensor_subdims(sample_sptensor):
(data, sptensorInstance) = sample_sptensor

assert (sptensorInstance.subdims(np.array([[1], [1], [1, 3]])) == np.array([0, 1])).all()
assert (sptensorInstance.subdims([[1], [1], [1, 3]]) == np.array([0, 1])).all()
assert (sptensorInstance.subdims((1, 1, slice(None, None, None))) == np.array([0, 1])).all()

with pytest.raises(AssertionError) as excinfo:
sptensorInstance.subdims(np.array([[1], [1, 3]]))
sptensorInstance.subdims([[1], [1, 3]])
assert "Number of subdimensions must equal number of dimensions" in str(excinfo)

@pytest.mark.indevelopment
@@ -1290,7 +1290,7 @@ def test_sptensor_ttv(sample_sptensor):

# Wrong shape vector
with pytest.raises(AssertionError) as excinfo:
onesSptensor.ttv(np.array([vector, np.array([1,2])]))
onesSptensor.ttv([vector, np.array([1,2])])
assert "Multiplicand is wrong size" in str(excinfo)

# Returns vector shaped object
4 changes: 2 additions & 2 deletions tests/test_tensor.py
Original file line number Diff line number Diff line change
@@ -1092,7 +1092,7 @@ def test_tensor_ttv(sample_tensor_2way, sample_tensor_3way, sample_tensor_4way):
assert (T2.data == np.array([10,14,18])).all()

# Multiply by multiple vectors, infer dimensions
assert tensorInstance2.ttv(np.array([np.array([2, 2]), np.array([1, 1, 1])])) == 42
assert tensorInstance2.ttv([np.array([2, 2]), np.array([1, 1, 1])]) == 42

# Multiply by multiple vectors as list of numpy.ndarrays
assert tensorInstance2.ttv([np.array([2, 2]), np.array([1, 1, 1])]) == 42
@@ -1104,7 +1104,7 @@ def test_tensor_ttv(sample_tensor_2way, sample_tensor_3way, sample_tensor_4way):
assert (T3.data == np.array([[6,30],[14,38],[22,46]])).all()

# Multiply by multiple vectors, infer dimensions
assert tensorInstance3.ttv(np.array([np.array([2, 2]), np.array([1, 1, 1]), np.array([2, 2])])) == 312
assert tensorInstance3.ttv([np.array([2, 2]), np.array([1, 1, 1]), np.array([2, 2])]) == 312

# 4-way Multiply by single vector
T4 = tensorInstance4.ttv(2 * np.ones((tensorInstance4.shape[0],)), 0)