Skip to content

Commit 15c85c6

Browse files
authored
Lint and format with ruff (pre-commit + CI) (#80)
1 parent 481a35b commit 15c85c6

17 files changed

Lines changed: 608 additions & 258 deletions

‎.github/workflows/test.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ jobs:
1212
uses: astral-sh/setup-uv@v10.0.1
1313
- name: Setup environment
1414
run: make .venv
15+
- name: Run ruff format
16+
run: uv run ruff format --check .
17+
- name: Run ruff check
18+
run: uv run ruff check .
1519
- name: Run ty check
1620
run: make ty
1721
- name: Run vulture

‎.pre-commit-config.yaml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.16.7
4+
hooks:
5+
- id: ruff-check
6+
args: [--fix]
7+
- id: ruff-format

‎pyproject.toml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ dev = [
3131
"pytest-cov>=3.0.0",
3232
"tensordata",
3333
"ty>=0.0.1",
34-
"ruff>=0.6.4",
34+
"ruff>=0.16.1",
35+
"pre-commit>=4.0",
3536
"pytest-xdist>=3.8.0",
3637
"vulture>=2.16",
3738
]
@@ -48,6 +49,10 @@ build-backend = "hatchling.build"
4849
[tool.hatch.build.targets.wheel]
4950
packages = ["tensorpack"]
5051

52+
[tool.ruff.lint.per-file-ignores]
53+
# Public module name kept for backwards compatibility.
54+
"tensorpack/SVD_impute.py" = ["N999"]
55+
5156
[tool.pytest.ini_options]
5257
addopts = "-n auto"
5358
testpaths = ["tensorpack"]

‎tensorpack/SVD_impute.py‎

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13-
from tensorly.tenalg import svd_interface
1413
import numpy as np
14+
from tensorly.tenalg import svd_interface
1515

1616

17-
class IterativeSVD(object):
17+
class IterativeSVD:
1818
def __init__(
19-
self,
20-
rank,
21-
convergence_threshold=1e-7,
22-
max_iters=500,
23-
random_state=None,
24-
min_value=None,
25-
max_value=None,
26-
verbose=False):
27-
self.min_value=min_value
28-
self.max_value=max_value
19+
self,
20+
rank,
21+
convergence_threshold=1e-7,
22+
max_iters=500,
23+
random_state=None,
24+
min_value=None,
25+
max_value=None,
26+
verbose=False,
27+
):
28+
self.min_value = min_value
29+
self.max_value = max_value
2930
self.rank = rank
3031
self.max_iters = max_iters
3132
self.convergence_threshold = convergence_threshold
@@ -81,11 +82,10 @@ def _converged(self, X_old, X_new, missing_mask):
8182
old_missing_values = X_old[missing_mask]
8283
new_missing_values = X_new[missing_mask]
8384
difference = old_missing_values - new_missing_values
84-
ssd = np.sum(difference ** 2)
85-
old_norm_squared = (old_missing_values ** 2).sum()
85+
ssd = np.sum(difference**2)
86+
old_norm_squared = (old_missing_values**2).sum()
8687
# edge cases
87-
if old_norm_squared == 0 or \
88-
(old_norm_squared < F32PREC and ssd > F32PREC):
88+
if old_norm_squared == 0 or (old_norm_squared < F32PREC and ssd > F32PREC):
8989
return False
9090
else:
9191
return (ssd / old_norm_squared) < self.convergence_threshold
@@ -95,22 +95,23 @@ def solve(self, X, missing_mask):
9595
X_filled = X
9696
for i in range(self.max_iters):
9797
curr_rank = self.rank
98-
self.U, S, V = svd_interface(X_filled, method="truncated_svd", n_eigenvecs=curr_rank,
99-
random_state=self.random_state)
98+
self.U, S, V = svd_interface(
99+
X_filled,
100+
method="truncated_svd",
101+
n_eigenvecs=curr_rank,
102+
random_state=self.random_state,
103+
)
100104
X_reconstructed = self.U @ np.diag(S) @ V
101105
X_reconstructed = self.clip(X_reconstructed)
102106

103107
# Masked mae
104108
mae = np.mean(np.abs(X[observed_mask] - X_reconstructed[observed_mask]))
105109

106110
if self.verbose:
107-
print(
108-
"[IterativeSVD] Iter %d: observed MAE=%0.6f" % (
109-
i + 1, mae))
111+
print(f"[IterativeSVD] Iter {i + 1}: observed MAE={mae:0.6f}")
110112
converged = self._converged(
111-
X_old=X_filled,
112-
X_new=X_reconstructed,
113-
missing_mask=missing_mask)
113+
X_old=X_filled, X_new=X_reconstructed, missing_mask=missing_mask
114+
)
114115
X_filled[missing_mask] = X_reconstructed[missing_mask]
115116
if converged:
116117
break

‎tensorpack/__init__.py‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
__version__ = '0.1.2'
2-
3-
from .cmtf import *
1+
from .cmtf import (
2+
buildMat,
3+
calcR2X,
4+
cp_normalize,
5+
delete_component,
6+
initialize_cmtf,
7+
initialize_cp,
8+
perform_CMTF,
9+
perform_CP,
10+
reorient_factors,
11+
sort_factors,
12+
tensor_degFreedom,
13+
)
14+
from .coupled import CoupledTensor
415
from .decomposition import Decomposition
516
from .tucker import tucker_decomp
617
from .xplots import xplot_components
7-
from .coupled import CoupledTensor
18+
19+
__version__ = "0.1.2"
20+
21+
__all__ = [
22+
"CoupledTensor",
23+
"Decomposition",
24+
"buildMat",
25+
"calcR2X",
26+
"cp_normalize",
27+
"delete_component",
28+
"initialize_cmtf",
29+
"initialize_cp",
30+
"perform_CMTF",
31+
"perform_CP",
32+
"reorient_factors",
33+
"sort_factors",
34+
"tensor_degFreedom",
35+
"tucker_decomp",
36+
"xplot_components",
37+
]

‎tensorpack/cmtf.py‎

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22
Coupled Matrix Tensor Factorization
33
"""
44

5+
from copy import deepcopy
6+
57
import numpy as np
6-
from tensorly.tenalg import svd_interface
78
import tensorly as tl
8-
from tensorly.tenalg import khatri_rao # ty: ignore[unresolved-import] # backend-dispatched at runtime
9-
from copy import deepcopy
10-
from tensorly.decomposition._cp import initialize_cp
9+
from tensorly.tenalg import (
10+
khatri_rao, # ty: ignore[unresolved-import] # backend-dispatched at runtime
11+
svd_interface,
12+
)
1113
from tqdm import tqdm
12-
from .SVD_impute import IterativeSVD
13-
from .linalg import mlstsq, calcR2X_TnB
1414

15+
from .linalg import calcR2X_TnB, mlstsq
16+
from .SVD_impute import IterativeSVD
1517

16-
tl.set_backend('numpy')
18+
tl.set_backend("numpy")
1719

1820

1921
def buildMat(tFac):
20-
""" Build the matrix in CMTF from the factors. """
21-
if hasattr(tFac, 'mWeights'):
22+
"""Build the matrix in CMTF from the factors."""
23+
if hasattr(tFac, "mWeights"):
2224
return tFac.factors[0] @ (tFac.mFactor * tFac.mWeights).T
2325
return tFac.factors[0] @ tFac.mFactor.T
2426

2527

2628
def calcR2X(tFac, tIn=None, mIn=None):
27-
""" Calculate R2X. Optionally it can be calculated for only the tensor or matrix. """
29+
"""Calculate R2X. Optionally it can be calculated for only the tensor or matrix."""
2830
assert (tIn is not None) or (mIn is not None)
2931
vTop, vBottom = 0.0, 0.0
3032

@@ -41,17 +43,17 @@ def calcR2X(tFac, tIn=None, mIn=None):
4143

4244

4345
def tensor_degFreedom(tFac) -> int:
44-
""" Calculate the degrees of freedom within a tensor factorization. """
46+
"""Calculate the degrees of freedom within a tensor factorization."""
4547
deg = np.sum([f.size for f in tFac.factors])
4648

47-
if hasattr(tFac, 'mFactor'):
49+
if hasattr(tFac, "mFactor"):
4850
deg += tFac.mFactor.size
4951

5052
return deg
5153

5254

5355
def reorient_factors(tFac):
54-
""" This function ensures that factors are negative on at most one direction. """
56+
"""This function ensures that factors are negative on at most one direction."""
5557
# Flip the types to be positive
5658
tMeans = np.sign(np.mean(tFac.factors[2], axis=0))
5759
tFac.factors[1] *= tMeans[np.newaxis, :]
@@ -62,13 +64,13 @@ def reorient_factors(tFac):
6264
tFac.factors[0] *= rMeans[np.newaxis, :]
6365
tFac.factors[1] *= rMeans[np.newaxis, :]
6466

65-
if hasattr(tFac, 'mFactor'):
67+
if hasattr(tFac, "mFactor"):
6668
tFac.mFactor *= rMeans[np.newaxis, :]
6769
return tFac
6870

6971

7072
def sort_factors(tFac):
71-
""" Sort the components from the largest variance to the smallest. """
73+
"""Sort the components from the largest variance to the smallest."""
7274
tensor = deepcopy(tFac)
7375

7476
# Variance separated by component
@@ -77,15 +79,21 @@ def sort_factors(tFac):
7779
norm *= np.sum(np.square(factor), axis=0)
7880

7981
# Add the variance of the matrix
80-
if hasattr(tFac, 'mFactor'):
81-
norm += np.sum(np.square(tFac.factors[0]), axis=0) * np.sum(np.square(tFac.mFactor), axis=0) * tFac.mWeights
82+
if hasattr(tFac, "mFactor"):
83+
norm += (
84+
np.sum(np.square(tFac.factors[0]), axis=0)
85+
* np.sum(np.square(tFac.mFactor), axis=0)
86+
* tFac.mWeights
87+
)
8288

8389
order = np.flip(np.argsort(norm))
8490
tensor.weights = tensor.weights[order]
8591
tensor.factors = [fac[:, order] for fac in tensor.factors]
86-
np.testing.assert_allclose(tl.cp_to_tensor(tFac), tl.cp_to_tensor(tensor), atol=1e-9)
92+
np.testing.assert_allclose(
93+
tl.cp_to_tensor(tFac), tl.cp_to_tensor(tensor), atol=1e-9
94+
)
8795

88-
if hasattr(tFac, 'mFactor'):
96+
if hasattr(tFac, "mFactor"):
8997
tensor.mFactor = tensor.mFactor[:, order]
9098
tensor.mWeights = tensor.mWeights[order]
9199
np.testing.assert_allclose(buildMat(tFac), buildMat(tensor), atol=1e-9)
@@ -94,7 +102,7 @@ def sort_factors(tFac):
94102

95103

96104
def delete_component(tFac, compNum):
97-
""" Delete the indicated component. """
105+
"""Delete the indicated component."""
98106
tensor = deepcopy(tFac)
99107
compNum = np.array(compNum, dtype=int)
100108

@@ -105,21 +113,20 @@ def delete_component(tFac, compNum):
105113
tensor.rank -= compNum.size
106114
tensor.weights = np.delete(tensor.weights, compNum)
107115

108-
if hasattr(tFac, 'mFactor'):
116+
if hasattr(tFac, "mFactor"):
109117
tensor.mFactor = np.delete(tensor.mFactor, compNum, axis=1)
110118
tensor.mWeights = np.delete(tensor.mWeights, compNum)
111119

112120
tensor.factors = [np.delete(fac, compNum, axis=1) for fac in tensor.factors]
113121
return tensor
114122

115123

116-
117124
def cp_normalize(tFac):
118-
""" Normalize the factors using the inf norm. """
125+
"""Normalize the factors using the inf norm."""
119126
for i, factor in enumerate(tFac.factors):
120127
scales = np.linalg.norm(factor, ord=np.inf, axis=0)
121128
tFac.weights *= scales
122-
if i == 0 and hasattr(tFac, 'mFactor'):
129+
if i == 0 and hasattr(tFac, "mFactor"):
123130
mScales = np.linalg.norm(tFac.mFactor, ord=np.inf, axis=0)
124131
tFac.mWeights = scales * mScales
125132
tFac.mFactor /= mScales
@@ -172,7 +179,7 @@ def initialize_cp(tensor: np.ndarray, rank: int):
172179
An initial cp tensor.
173180
"""
174181
factors = [np.ones((tensor.shape[i], rank)) for i in range(tensor.ndim)]
175-
contain_missing = (np.sum(~np.isfinite(tensor)) > 0)
182+
contain_missing = np.sum(~np.isfinite(tensor)) > 0
176183

177184
# SVD init mode whose size is larger than rank
178185
for mode in range(tensor.ndim):
@@ -182,25 +189,31 @@ def initialize_cp(tensor: np.ndarray, rank: int):
182189
si = IterativeSVD(rank)
183190
unfold = si.fit_transform(unfold)
184191

185-
factors[mode] = svd_interface(unfold, method="truncated_svd", n_eigenvecs=rank, flip_sign=True)[0]
192+
factors[mode] = svd_interface(
193+
unfold, method="truncated_svd", n_eigenvecs=rank, flip_sign=True
194+
)[0]
186195

187196
return tl.cp_tensor.CPTensor((None, factors))
188197

189198

190199
def perform_CP(tOrig, r=6, tol=1e-6, maxiter=50, progress=False, callback=None):
191-
""" Perform CP decomposition. """
192-
if callback: callback.begin()
200+
"""Perform CP decomposition."""
201+
if callback:
202+
callback.begin()
193203
tFac = initialize_cp(tOrig, r)
194204

195205
# Pre-unfold
196206
unfolded = [tl.unfold(tOrig, i) for i in range(tOrig.ndim)]
197207

198208
R2X_last = -np.inf
199209
tFac.R2X = calcR2X(tFac, tOrig)
200-
if callback: callback.first_entry(tFac)
210+
if callback:
211+
callback.first_entry(tFac)
201212

202213
# Precalculate the missingness patterns
203-
uniqueInfo = [np.unique(np.isfinite(B.T), axis=1, return_inverse=True) for B in unfolded]
214+
uniqueInfo = [
215+
np.unique(np.isfinite(B.T), axis=1, return_inverse=True) for B in unfolded
216+
]
204217

205218
tq = tqdm(range(maxiter), disable=(not progress))
206219
for i in tq:
@@ -213,7 +226,8 @@ def perform_CP(tOrig, r=6, tol=1e-6, maxiter=50, progress=False, callback=None):
213226
tFac.R2X = calcR2X(tFac, tOrig)
214227
tq.set_postfix(R2X=tFac.R2X, delta=tFac.R2X - R2X_last, refresh=False)
215228
assert tFac.R2X > 0.0
216-
if callback: callback.update(tFac)
229+
if callback:
230+
callback.update(tFac)
217231

218232
if tFac.R2X - R2X_last < tol:
219233
break
@@ -223,12 +237,12 @@ def perform_CP(tOrig, r=6, tol=1e-6, maxiter=50, progress=False, callback=None):
223237

224238
if r > 1:
225239
tFac = sort_factors(tFac)
226-
240+
227241
return tFac
228242

229243

230244
def perform_CMTF(tOrig, mOrig, r=9, tol=1e-6, maxiter=50, progress=True):
231-
""" Perform CMTF decomposition. """
245+
"""Perform CMTF decomposition."""
232246
assert tOrig.dtype == float
233247
assert mOrig.dtype == float
234248
tFac = initialize_cmtf(tOrig, mOrig, r)
@@ -242,7 +256,10 @@ def perform_CMTF(tOrig, mOrig, r=9, tol=1e-6, maxiter=50, progress=True):
242256
# Precalculate the missingness patterns
243257
uniqueInfo = np.unique(np.isfinite(unfolded.T), axis=1, return_inverse=True)
244258
tUnfolded = {m: tl.unfold(tOrig, m).T for m in [1, 2]}
245-
tUniqueInfo = {m: np.unique(np.isfinite(tUnfolded[m]), axis=1, return_inverse=True) for m in [1, 2]}
259+
tUniqueInfo = {
260+
m: np.unique(np.isfinite(tUnfolded[m]), axis=1, return_inverse=True)
261+
for m in [1, 2]
262+
}
246263

247264
tq = tqdm(range(maxiter), disable=(not progress))
248265
for _ in tq:
@@ -272,4 +289,4 @@ def perform_CMTF(tOrig, mOrig, r=9, tol=1e-6, maxiter=50, progress=True):
272289
tFac = sort_factors(tFac)
273290
tFac.R2X = R2X
274291

275-
return tFac
292+
return tFac

0 commit comments

Comments
 (0)