Skip to content

Commit 99a2565

Browse files
committed
Ran black
1 parent 4daf7a6 commit 99a2565

9 files changed

+52
-115
lines changed

src/matcouply/_unimodal_regression.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def _merge_intervals_inplace(merge_target, merger, sum_weighted_y, sum_weighted_
2222

2323

2424
@jit(
25-
nopython=True,
26-
cache=True,
25+
nopython=True, cache=True,
2726
)
2827
def prefix_isotonic_regression(y, weights=None, non_negativity=False):
2928
if weights is None:
@@ -137,7 +136,6 @@ def unimodal_regression(y, non_negativity=False):
137136
y2 = np.ascontiguousarray(y)
138137
y2 = y2.reshape(y2.shape[0], -1)
139138
unfolded_output = np.stack(
140-
[_unimodal_regression(y2[:, r], non_negativity=non_negativity)[0] for r in range(y2.shape[1])],
141-
axis=1,
139+
[_unimodal_regression(y2[:, r], non_negativity=non_negativity)[0] for r in range(y2.shape[1])], axis=1,
142140
)
143141
return unfolded_output.reshape(y.shape)

src/matcouply/data.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import scipy.stats as stats
1212
import tensorly as tl
1313
from scipy.io import loadmat
14+
1415
try:
1516
import pandas as pd
1617
import requests

src/matcouply/decomposition.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def admm_update_C(
327327

328328

329329
def _root_sum_squared_list(x_list):
330-
return tl.sqrt(sum(tl.sum(x**2) for x in x_list))
330+
return tl.sqrt(sum(tl.sum(x ** 2) for x in x_list))
331331

332332

333333
def compute_feasibility_gaps(cmf, regs, A_aux_list, B_aux_list, C_aux_list):
@@ -570,11 +570,11 @@ def _compute_l2_penalty(cmf, l2_parameters):
570570
weights, (A, B_is, C) = cmf
571571
l2reg = 0
572572
if l2_parameters[0]:
573-
l2reg += 0.5 * l2_parameters[0] * tl.sum(A**2)
573+
l2reg += 0.5 * l2_parameters[0] * tl.sum(A ** 2)
574574
if l2_parameters[1]:
575-
l2reg += 0.5 * l2_parameters[1] * sum(tl.sum(B_i**2) for B_i in B_is)
575+
l2reg += 0.5 * l2_parameters[1] * sum(tl.sum(B_i ** 2) for B_i in B_is)
576576
if l2_parameters[2]:
577-
l2reg += 0.5 * l2_parameters[2] * tl.sum(C**2)
577+
l2reg += 0.5 * l2_parameters[2] * tl.sum(C ** 2)
578578

579579
return l2reg
580580

@@ -853,16 +853,8 @@ def cmf_aoadmm(
853853
regs[2] = []
854854

855855
# TODO (Improvement): Include cmf to initialize functions in case other init schemes require that?
856-
(
857-
A_aux_list,
858-
B_is_aux_list,
859-
C_aux_list,
860-
) = initialize_aux(matrices, rank, regs, random_state=random_state)
861-
(
862-
A_dual_list,
863-
B_is_dual_list,
864-
C_dual_list,
865-
) = initialize_dual(matrices, rank, regs, random_state=random_state)
856+
(A_aux_list, B_is_aux_list, C_aux_list,) = initialize_aux(matrices, rank, regs, random_state=random_state)
857+
(A_dual_list, B_is_dual_list, C_dual_list,) = initialize_dual(matrices, rank, regs, random_state=random_state)
866858
norm_matrices = _root_sum_squared_list(matrices)
867859
rec_errors = []
868860
feasibility_gaps = []
@@ -877,7 +869,7 @@ def cmf_aoadmm(
877869
+ sum(C_reg.penalty(cmf[1][2]) for C_reg in regs[2])
878870
)
879871
l2_reg = _compute_l2_penalty(cmf, l2_penalty)
880-
losses.append(0.5 * rec_error**2 + l2_reg + reg_penalty)
872+
losses.append(0.5 * rec_error ** 2 + l2_reg + reg_penalty)
881873

882874
A_gaps, B_gaps, C_gaps = compute_feasibility_gaps(cmf, regs, A_aux_list, B_is_aux_list, C_aux_list)
883875
feasibility_gaps.append((A_gaps, B_gaps, C_gaps))
@@ -976,7 +968,7 @@ def cmf_aoadmm(
976968
)
977969

978970
l2_reg = _compute_l2_penalty(cmf, l2_penalty)
979-
losses.append(0.5 * rec_error**2 + l2_reg + reg_penalty)
971+
losses.append(0.5 * rec_error ** 2 + l2_reg + reg_penalty)
980972

981973
if verbose and it % verbose == 0 and verbose > 0:
982974
A_gaps, B_gaps, C_gaps = curr_feasibility_gaps

src/matcouply/penalties.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,7 @@ class GeneralizedL2Penalty(MatrixPenalty):
703703
"""
704704

705705
def __init__(
706-
self,
707-
norm_matrix,
708-
svd="truncated_svd",
709-
aux_init="random_uniform",
710-
dual_init="random_uniform",
711-
validate=True,
706+
self, norm_matrix, svd="truncated_svd", aux_init="random_uniform", dual_init="random_uniform", validate=True,
712707
):
713708
super().__init__(aux_init, dual_init)
714709
self.norm_matrix = norm_matrix
@@ -807,11 +802,7 @@ class TotalVariationPenalty(MatrixPenalty):
807802
"""
808803

809804
def __init__(
810-
self,
811-
reg_strength,
812-
l1_strength=0,
813-
aux_init="random_uniform",
814-
dual_init="random_uniform",
805+
self, reg_strength, l1_strength=0, aux_init="random_uniform", dual_init="random_uniform",
815806
):
816807
if not HAS_TV:
817808
raise ModuleNotFoundError(
@@ -929,7 +920,7 @@ def __init__(self, norm_bound, non_negativity=False, aux_init="random_uniform",
929920
def factor_matrix_update(self, factor_matrix, feasibility_penalty, aux):
930921
if self.non_negativity:
931922
factor_matrix = tl.clip(factor_matrix, 0, float("inf"))
932-
column_norms = tl.sqrt(tl.sum(factor_matrix**2, axis=0))
923+
column_norms = tl.sqrt(tl.sum(factor_matrix ** 2, axis=0))
933924
column_norms = tl.clip(column_norms, self.norm_bound, float("inf"))
934925
return factor_matrix * self.norm_bound / column_norms
935926

src/matcouply/testing/admm_penalty.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,7 @@ def test_validating_given_init_dual(self, rng, random_ragged_cmf, aux_init):
345345
penalty.init_dual(matrices, rank, 1, random_state=rng)
346346

347347
@pytest.mark.parametrize(
348-
"dual_init,",
349-
["random_uniform", "random_standard_normal", "zeros"],
348+
"dual_init,", ["random_uniform", "random_standard_normal", "zeros"],
350349
)
351350
@pytest.mark.parametrize("aux_init", ["random_uniform", "random_standard_normal", "zeros"])
352351
def test_rank_and_mode_validation_for_init_dual(self, rng, random_ragged_cmf, dual_init, aux_init):
@@ -559,4 +558,4 @@ def test_penalty(self, random_ragged_cmf):
559558
penalty = self.PenaltyType(**self.penalty_default_kwargs)
560559
assert penalty.penalty(A) == 0
561560
assert penalty.penalty(B_is) == 0
562-
assert penalty.penalty(C) == 0
561+
assert penalty.penalty(C) == 0

src/matcouply/testing/fixtures.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def random_regular_shapes(rng):
4040

4141
@pytest.fixture
4242
def random_ragged_cmf(
43-
rng,
44-
random_ragged_shapes,
43+
rng, random_ragged_shapes,
4544
):
4645
from matcouply.random import random_coupled_matrices
4746

@@ -65,8 +64,7 @@ def random_rank5_ragged_cmf(rng):
6564

6665
@pytest.fixture
6766
def random_regular_cmf(
68-
rng,
69-
random_regular_shapes,
67+
rng, random_regular_shapes,
7068
):
7169
from matcouply.random import random_coupled_matrices
7270

src/matcouply/testing/utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# MIT License: Copyright (c) 2022, Marie Roald.
22
# See the LICENSE file in the root directory for full license text.
33

4+
45
def random_length(rng, min=2, mean=5):
56
"""Generate a random dimension length.
67

tests/test_decomposition.py

+28-67
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
def normalize(X):
25-
ssX = tl.sum(X**2, 0)
25+
ssX = tl.sum(X ** 2, 0)
2626
ssX = tl.reshape(ssX, (1, *tl.shape(ssX)))
2727
return X / tl.sqrt(ssX)
2828

@@ -74,8 +74,7 @@ def test_initialize_cmf_invalid_init(rng):
7474

7575

7676
@pytest.mark.parametrize(
77-
"rank",
78-
[1, 2, 5],
77+
"rank", [1, 2, 5],
7978
)
8079
def test_initialize_aux(rng, rank):
8180
shapes = ((5, 10), (10, 10), (15, 10))
@@ -98,8 +97,7 @@ def test_initialize_aux(rng, rank):
9897

9998

10099
@pytest.mark.parametrize(
101-
"rank",
102-
[1, 2, 5],
100+
"rank", [1, 2, 5],
103101
)
104102
def test_initialize_dual(rng, rank):
105103
shapes = ((5, 10), (10, 10), (15, 10))
@@ -128,7 +126,7 @@ def test_cmf_reconstruction_error(rng, random_ragged_cmf):
128126
# Add random noise
129127
noise = [tl.tensor(rng.standard_normal(size=shape)) for shape in shapes]
130128
noisy_matrices = [matrix + n for matrix, n in zip(matrices, noise)]
131-
noise_norm = tl.sqrt(sum(tl.sum(n**2) for n in noise))
129+
noise_norm = tl.sqrt(sum(tl.sum(n ** 2) for n in noise))
132130

133131
# Check that the error is equal to the noise magnitude
134132
error = decomposition._cmf_reconstruction_error(noisy_matrices, cmf)
@@ -1457,15 +1455,11 @@ def test_cmf_aoadmm(rng, random_ragged_cmf):
14571455

14581456
# Construct matrices and compute their norm
14591457
matrices = nn_cmf.to_matrices()
1460-
norm_matrices = tl.sqrt(sum(tl.sum(matrix**2) for matrix in matrices))
1458+
norm_matrices = tl.sqrt(sum(tl.sum(matrix ** 2) for matrix in matrices))
14611459

14621460
# Decompose matrices with cmf_aoadmm with no constraints
14631461
out_cmf, (aux, dual), diagnostics = decomposition.cmf_aoadmm(
1464-
matrices,
1465-
rank,
1466-
n_iter_max=5_000,
1467-
return_errors=True,
1468-
return_admm_vars=True,
1462+
matrices, rank, n_iter_max=5_000, return_errors=True, return_admm_vars=True,
14691463
)
14701464

14711465
# Check that reconstruction error is low
@@ -1615,12 +1609,7 @@ def test_parafac2_makes_nn_cmf_unique(rng):
16151609
regularized_loss = [float("inf")]
16161610
for init in range(5):
16171611
out, diagnostics = decomposition.cmf_aoadmm(
1618-
matrices,
1619-
rank,
1620-
n_iter_max=1_000,
1621-
return_errors=True,
1622-
non_negative=[True, True, True],
1623-
parafac2=True,
1612+
matrices, rank, n_iter_max=1_000, return_errors=True, non_negative=[True, True, True], parafac2=True,
16241613
)
16251614

16261615
if diagnostics.regularized_loss[-1] < regularized_loss[-1] and diagnostics.satisfied_feasibility_condition:
@@ -1650,11 +1639,7 @@ def test_cmf_aoadmm_not_updating_A_works(rng, random_rank5_ragged_cmf):
16501639

16511640
# Decompose matrices with cmf_aoadmm with no constraints
16521641
out_cmf = decomposition.cmf_aoadmm(
1653-
matrices,
1654-
rank,
1655-
n_iter_max=5,
1656-
update_A=False,
1657-
init=(None, (wrong_A_copy, B_is_copy, C_copy)),
1642+
matrices, rank, n_iter_max=5, update_A=False, init=(None, (wrong_A_copy, B_is_copy, C_copy)),
16581643
)
16591644

16601645
out_weights, (out_A, out_B_is, out_C) = out_cmf
@@ -1677,11 +1662,7 @@ def test_cmf_aoadmm_not_updating_C_works(rng, random_rank5_ragged_cmf):
16771662

16781663
# Decompose matrices with cmf_aoadmm with no constraints
16791664
out_cmf = decomposition.cmf_aoadmm(
1680-
matrices,
1681-
rank,
1682-
n_iter_max=5,
1683-
update_C=False,
1684-
init=(None, (A_copy, B_is_copy, wrong_C_copy)),
1665+
matrices, rank, n_iter_max=5, update_C=False, init=(None, (A_copy, B_is_copy, wrong_C_copy)),
16851666
)
16861667

16871668
out_weights, (out_A, out_B_is, out_C) = out_cmf
@@ -1703,11 +1684,7 @@ def test_cmf_aoadmm_not_updating_B_is_works(rng, random_rank5_ragged_cmf):
17031684

17041685
# Decompose matrices with cmf_aoadmm with no constraints
17051686
out_cmf = decomposition.cmf_aoadmm(
1706-
matrices,
1707-
rank,
1708-
n_iter_max=5,
1709-
update_B_is=False,
1710-
init=(None, (A_copy, wrong_B_is_copy, C_copy)),
1687+
matrices, rank, n_iter_max=5, update_B_is=False, init=(None, (A_copy, wrong_B_is_copy, C_copy)),
17111688
)
17121689

17131690
out_weights, (out_A, out_B_is, out_C) = out_cmf
@@ -1732,9 +1709,9 @@ def test_compute_l2_penalty(rng, random_ragged_cmf):
17321709
cmf, shapes, rank = random_ragged_cmf
17331710
weights, (A, B_is, C) = cmf
17341711

1735-
SS_A = tl.sum(A**2)
1736-
SS_B = sum(tl.sum(B_i**2) for B_i in B_is)
1737-
SS_C = tl.sum(C**2)
1712+
SS_A = tl.sum(A ** 2)
1713+
SS_B = sum(tl.sum(B_i ** 2) for B_i in B_is)
1714+
SS_C = tl.sum(C ** 2)
17381715

17391716
assert decomposition._compute_l2_penalty(cmf, [0, 0, 0]) == 0
17401717
assert decomposition._compute_l2_penalty(cmf, [1, 0, 0]) == pytest.approx(0.5 * SS_A)
@@ -1750,46 +1727,32 @@ def test_l2_penalty_is_included(rng, random_ragged_cmf):
17501727

17511728
# Decompose matrices with cmf_aoadmm with no constraints
17521729
out_cmf, diagnostics = decomposition.cmf_aoadmm(
1753-
matrices,
1754-
rank,
1755-
n_iter_max=5,
1756-
return_errors=True,
1757-
update_B_is=False,
1730+
matrices, rank, n_iter_max=5, return_errors=True, update_B_is=False,
17581731
)
17591732

17601733
rel_sse = diagnostics.rec_errors[-1] ** 2
17611734
assert diagnostics.regularized_loss[-1] == pytest.approx(0.5 * rel_sse)
17621735

17631736
out_cmf, diagnostics = decomposition.cmf_aoadmm(
1764-
matrices,
1765-
rank,
1766-
n_iter_max=5,
1767-
l2_penalty=1,
1768-
return_errors=True,
1769-
update_B_is=False,
1737+
matrices, rank, n_iter_max=5, l2_penalty=1, return_errors=True, update_B_is=False,
17701738
)
17711739

17721740
out_weights, (out_A, out_B_is, out_C) = out_cmf
17731741
rel_sse = diagnostics.rec_errors[-1] ** 2
1774-
SS_A = tl.sum(out_A**2)
1775-
SS_B = sum(tl.sum(out_B_i**2) for out_B_i in out_B_is)
1776-
SS_C = tl.sum(out_C**2)
1742+
SS_A = tl.sum(out_A ** 2)
1743+
SS_B = sum(tl.sum(out_B_i ** 2) for out_B_i in out_B_is)
1744+
SS_C = tl.sum(out_C ** 2)
17771745
assert diagnostics.regularized_loss[-1] == pytest.approx(0.5 * rel_sse + 0.5 * (SS_A + SS_B + SS_C))
17781746

17791747
out_cmf, diagnostics = decomposition.cmf_aoadmm(
1780-
matrices,
1781-
rank,
1782-
n_iter_max=5,
1783-
l2_penalty=[1, 2, 3],
1784-
return_errors=True,
1785-
update_B_is=False,
1748+
matrices, rank, n_iter_max=5, l2_penalty=[1, 2, 3], return_errors=True, update_B_is=False,
17861749
)
17871750

17881751
out_weights, (out_A, out_B_is, out_C) = out_cmf
17891752
rel_sse = diagnostics.rec_errors[-1] ** 2
1790-
SS_A = tl.sum(out_A**2)
1791-
SS_B = sum(tl.sum(out_B_i**2) for out_B_i in out_B_is)
1792-
SS_C = tl.sum(out_C**2)
1753+
SS_A = tl.sum(out_A ** 2)
1754+
SS_B = sum(tl.sum(out_B_i ** 2) for out_B_i in out_B_is)
1755+
SS_C = tl.sum(out_C ** 2)
17931756
assert diagnostics.regularized_loss[-1] == pytest.approx(0.5 * rel_sse + 0.5 * (1 * SS_A + 2 * SS_B + 3 * SS_C))
17941757

17951758

@@ -1890,11 +1853,11 @@ def test_first_loss_value_is_correct(random_ragged_cmf):
18901853
l1_C = tl.sum(tl.abs(C))
18911854
l1_reg_penalty = l1_A + l1_B + l1_C
18921855
l2_A = tl.sum((weights * A) ** 2)
1893-
l2_B = sum(tl.sum(B_i**2) for B_i in B_is)
1894-
l2_C = tl.sum(C**2)
1856+
l2_B = sum(tl.sum(B_i ** 2) for B_i in B_is)
1857+
l2_C = tl.sum(C ** 2)
18951858
l2_reg_penalty = l2_A + l2_B + l2_C
18961859
rec_error = 0
1897-
initial_loss = 0.5 * rec_error**2 + 0.5 * 0.1 * l2_reg_penalty + 0.2 * l1_reg_penalty
1860+
initial_loss = 0.5 * rec_error ** 2 + 0.5 * 0.1 * l2_reg_penalty + 0.2 * l1_reg_penalty
18981861

18991862
# Check that we get correct output when none of the conditions are met
19001863
out_cmf, diagnostics = decomposition.cmf_aoadmm(
@@ -1913,8 +1876,7 @@ def test_first_loss_value_is_correct(random_ragged_cmf):
19131876

19141877

19151878
@pytest.mark.parametrize(
1916-
"n_iter_max",
1917-
[-1, 0],
1879+
"n_iter_max", [-1, 0],
19181880
)
19191881
def test_cmf_aoadmm_works_with_zero_iteration(random_ragged_cmf, n_iter_max):
19201882
cmf, shapes, rank = random_ragged_cmf
@@ -1960,7 +1922,6 @@ def test_regs_list_is_not_modified(random_ragged_cmf, regs):
19601922
assert regs == regs_unmodified
19611923

19621924

1963-
19641925
@pytest.mark.parametrize("constant_feasibility_penalty", ["", "AB", "C"])
19651926
def test_constant_feasibility_penalty_fails_with_invalid(random_ragged_cmf, constant_feasibility_penalty):
19661927

@@ -1977,7 +1938,7 @@ def test_constant_feasibility_penalty_fails_with_invalid(random_ragged_cmf, cons
19771938
verbose=False,
19781939
non_negative=True,
19791940
parafac2=True,
1980-
constant_feasibility_penalty=constant_feasibility_penalty
1941+
constant_feasibility_penalty=constant_feasibility_penalty,
19811942
)
19821943

19831944

@@ -1994,5 +1955,5 @@ def test_constant_feasibility_penalty_works_with_valid(random_ragged_cmf, consta
19941955
verbose=False,
19951956
non_negative=True,
19961957
parafac2=True,
1997-
constant_feasibility_penalty=constant_feasibility_penalty
1958+
constant_feasibility_penalty=constant_feasibility_penalty,
19981959
)

0 commit comments

Comments
 (0)