Skip to content

Commit

Permalink
black the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Simpson committed May 3, 2021
1 parent a4fccc8 commit f8475d8
Show file tree
Hide file tree
Showing 40 changed files with 1,453 additions and 1,279 deletions.
15 changes: 8 additions & 7 deletions benchmark/C1/bm-C1-plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import matplotlib.pyplot as plt

import os

my_path = os.path.dirname(__file__)

output = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'output'))
output = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "output"))


data = np.load(os.path.join(my_path, 'bm-C1.npz'))
data = np.load(os.path.join(my_path, "bm-C1.npz"))

ticks = [str(tuple(size)) for size in data["SIZES"]]

Expand All @@ -19,7 +20,7 @@
T = data[name]
N_size, N_data = T.shape
mean = np.mean(T, axis=1)
std = np.std(T, axis=1)/np.sqrt(N_data)
std = np.std(T, axis=1) / np.sqrt(N_data)
label = algo[i]
plt.errorbar(range(N_size), mean, yerr=std, label=label)

Expand All @@ -33,13 +34,13 @@
plt.savefig(os.path.join(output, "bm-C1-times.png"))
plt.close()

ref = np.min( [np.mean(data[name], axis=1) for name in ["L_pa", "L_cvx"] ], axis=0)
ref = np.min([np.mean(data[name], axis=1) for name in ["L_pa", "L_cvx"]], axis=0)
for i, name in enumerate(["L_pa", "L_cvx"]):
L = data[name]
N_size, N_data = L.shape
mean = np.mean(L, axis=1)
label = algo[i]
plt.plot(range(N_size), mean-ref, label=label)
plt.plot(range(N_size), mean - ref, label=label)

plt.title("Loss - minimum(loss)")
plt.xticks(range(N_size), ticks)
Expand All @@ -54,7 +55,7 @@
C = data[name]
N_size, N_data = C.shape
mean = np.mean(C, axis=1)
std = np.std(C, axis=1)/np.sqrt(N_data)
std = np.std(C, axis=1) / np.sqrt(N_data)
label = algo[i]
plt.plot(range(N_size), mean, label=label)

Expand All @@ -65,4 +66,4 @@
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(output, "bm-C1-constraint.png"))
plt.close()
plt.close()
46 changes: 23 additions & 23 deletions benchmark/C1/bm-C1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from time import time

import os
my_path = os.path.dirname(__file__)

l = [1, 2, 5, 7]
my_path = os.path.dirname(__file__)

l = [1, 2, 5, 7]


def loss(X, y, lamb, beta):
return np.sum(np.maximum(1 - y*X.dot(beta), 0)**2) + lamb*np.sum(abs(beta))
return np.sum(np.maximum(1 - y * X.dot(beta), 0) ** 2) + lamb * np.sum(abs(beta))


d_nonzero = 5
Expand All @@ -27,8 +27,8 @@ def loss(X, y, lamb, beta):
SIZES = []
for i in range(len(S)):
SIZES.append((S[i], S[i]))
if i+1<len(S):
SIZES.append((S[i], S[i+1]))
if i + 1 < len(S):
SIZES.append((S[i], S[i + 1]))

N_sizes = len(SIZES)

Expand All @@ -41,29 +41,27 @@ def loss(X, y, lamb, beta):
C_cvx = np.zeros((N_sizes, N_data))



for s in range(N_sizes):

m, d = SIZES[s]


for i in range(N_data):
(X, C, y), sol = random_data(m, d, d_nonzero, 1, sigma, zerosum=True, seed=i)
y = np.sign(y)
lamb = lam*2*LA.norm(X.T.dot(y),np.infty)
lamb = lam * 2 * LA.norm(X.T.dot(y), np.infty)

t0 = time()
# classo Path-Alg
b_pa = []
for j in range(N_per_data):
problem = classo_problem(X, y, C)
problem.formulation.concomitant = False
problem.formulation.classification =True
problem.formulation.classification = True
problem.model_selection.StabSel = False
problem.model_selection.LAMfixed = True
problem.model_selection.LAMfixedparameters.rescaled_lam = False
problem.model_selection.LAMfixedparameters.lam = lamb
problem.model_selection.LAMfixedparameters.numerical_method = 'Path-Alg'
problem.model_selection.LAMfixedparameters.numerical_method = "Path-Alg"
problem.solve()
b_pa.append(problem.solution.LAMfixed.beta)
b_pa = np.array(b_pa)
Expand All @@ -75,30 +73,32 @@ def loss(X, y, lamb, beta):
for j in range(N_per_data):
beta = cp.Variable(d)
cp.pos(1 - cp.multiply(y, X @ beta))
objective, constraints = cp.Minimize(cp.sum_squares(cp.pos(1 - cp.multiply(y, X @ beta)))+ lamb*cp.norm(beta, 1)), [C@beta == 0]
objective, constraints = cp.Minimize(
cp.sum_squares(cp.pos(1 - cp.multiply(y, X @ beta)))
+ lamb * cp.norm(beta, 1)
), [C @ beta == 0]
prob = cp.Problem(objective, constraints)
result = prob.solve(warm_start = False, eps_abs = 1e-5)
result = prob.solve(warm_start=False, eps_abs=1e-5)
b_cvx.append(beta.value)
b_cvx = np.array(b_cvx)

t2 = time()

t2 = time()

T_pa[s, i] = (t1 - t0) / N_per_data
L_pa[s, i] = loss(X, y, lamb, np.mean(b_pa, axis=0))
C_pa[s, i] = np.linalg.norm(C.dot(np.mean(b_pa, axis=0)))

T_cvx[s, i] = (t2 - t1) / N_per_data
T_cvx[s, i] = (t2 - t1) / N_per_data
L_cvx[s, i] = loss(X, y, lamb, np.mean(b_cvx, axis=0))
C_cvx[s, i] = np.linalg.norm(C.dot(np.mean(b_cvx, axis=0)))

np.savez(
os.path.join(my_path, 'bm-C1.npz'),
T_pa = T_pa,
L_pa = L_pa,
C_pa = C_pa,
T_cvx = T_cvx,
L_cvx = L_cvx,
C_cvx = C_cvx,
SIZES = np.array(SIZES)
os.path.join(my_path, "bm-C1.npz"),
T_pa=T_pa,
L_pa=L_pa,
C_pa=C_pa,
T_cvx=T_cvx,
L_cvx=L_cvx,
C_cvx=C_cvx,
SIZES=np.array(SIZES),
)
15 changes: 9 additions & 6 deletions benchmark/R1/bm-R1-plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import matplotlib.pyplot as plt

import os

my_path = os.path.dirname(__file__)

output = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'output'))
output = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "output"))


data = np.load(os.path.join(my_path, 'bm-R1.npz'))
data = np.load(os.path.join(my_path, "bm-R1.npz"))

ticks = [str(tuple(size)) for size in data["SIZES"]]

Expand All @@ -19,7 +20,7 @@
T = data[name]
N_size, N_data = T.shape
mean = np.mean(T, axis=1)
std = np.std(T, axis=1)/np.sqrt(N_data)
std = np.std(T, axis=1) / np.sqrt(N_data)
label = algo[i]
plt.errorbar(range(N_size), mean, yerr=std, label=label)

Expand All @@ -33,7 +34,9 @@
plt.savefig(os.path.join(output, "bm-R1-times.png"))
plt.close()

ref = np.min( [np.mean(data[name], axis=1) for name in ["L_pa", "L_pds", "L_dr", "L_cvx"] ], axis=0)
ref = np.min(
[np.mean(data[name], axis=1) for name in ["L_pa", "L_pds", "L_dr", "L_cvx"]], axis=0
)
for i, name in enumerate(["L_pa", "L_pds", "L_dr", "L_cvx"]):
L = data[name]
N_size, N_data = L.shape
Expand All @@ -54,7 +57,7 @@
C = data[name]
N_size, N_data = C.shape
mean = np.mean(C, axis=1)
std = np.std(C, axis=1)/np.sqrt(N_data)
std = np.std(C, axis=1) / np.sqrt(N_data)
label = algo[i]
plt.plot(range(N_size), mean, label=label)

Expand All @@ -65,4 +68,4 @@
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(output, "bm-R1-constraint.png"))
plt.close()
plt.close()
59 changes: 29 additions & 30 deletions benchmark/R1/bm-R1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from time import time

import os
my_path = os.path.dirname(__file__)

l = [1, 2, 5, 7]
my_path = os.path.dirname(__file__)

l = [1, 2, 5, 7]


def loss(X, y, lamb, beta):
return np.sum((X.dot(beta) - y)**2) + lamb*np.sum(abs(beta))
return np.sum((X.dot(beta) - y) ** 2) + lamb * np.sum(abs(beta))


d_nonzero = 5
Expand All @@ -27,8 +27,8 @@ def loss(X, y, lamb, beta):
SIZES = []
for i in range(len(S)):
SIZES.append((S[i], S[i]))
if i+1<len(S):
SIZES.append((S[i], S[i+1]))
if i + 1 < len(S):
SIZES.append((S[i], S[i + 1]))
N_sizes = len(SIZES)

T_pa = np.zeros((N_sizes, N_data))
Expand All @@ -48,15 +48,13 @@ def loss(X, y, lamb, beta):
C_cvx = np.zeros((N_sizes, N_data))



for s in range(N_sizes):

m, d = SIZES[s]


for i in range(N_data):
(X, C, y), sol = random_data(m, d, d_nonzero, 1, sigma, zerosum=True, seed=i)
lamb = lam*2*LA.norm(X.T.dot(y),np.infty)
lamb = lam * 2 * LA.norm(X.T.dot(y), np.infty)

t0 = time()
# classo Path-Alg
Expand All @@ -68,7 +66,7 @@ def loss(X, y, lamb, beta):
problem.model_selection.LAMfixed = True
problem.model_selection.LAMfixedparameters.rescaled_lam = False
problem.model_selection.LAMfixedparameters.lam = lamb
problem.model_selection.LAMfixedparameters.numerical_method = 'Path-Alg'
problem.model_selection.LAMfixedparameters.numerical_method = "Path-Alg"
problem.solve()
b_pa.append(problem.solution.LAMfixed.beta)
b_pa = np.array(b_pa)
Expand All @@ -83,7 +81,7 @@ def loss(X, y, lamb, beta):
problem.model_selection.LAMfixed = True
problem.model_selection.LAMfixedparameters.rescaled_lam = False
problem.model_selection.LAMfixedparameters.lam = lamb
problem.model_selection.LAMfixedparameters.numerical_method = 'P-PDS'
problem.model_selection.LAMfixedparameters.numerical_method = "P-PDS"
problem.solve()
b_pds.append(problem.solution.LAMfixed.beta)
b_pds = np.array(b_pds)
Expand All @@ -98,7 +96,7 @@ def loss(X, y, lamb, beta):
problem.model_selection.LAMfixed = True
problem.model_selection.LAMfixedparameters.rescaled_lam = False
problem.model_selection.LAMfixedparameters.lam = lamb
problem.model_selection.LAMfixedparameters.numerical_method = 'P-PDS'
problem.model_selection.LAMfixedparameters.numerical_method = "P-PDS"
problem.solve()
b_dr.append(problem.solution.LAMfixed.beta)
b_dr = np.array(b_dr)
Expand All @@ -108,14 +106,15 @@ def loss(X, y, lamb, beta):
b_cvx = []
for j in range(N_per_data):
beta = cp.Variable(d)
objective, constraints = cp.Minimize(cp.sum_squares(X@beta-y)+ lamb*cp.norm(beta, 1)), [C@beta == 0]
objective, constraints = cp.Minimize(
cp.sum_squares(X @ beta - y) + lamb * cp.norm(beta, 1)
), [C @ beta == 0]
prob = cp.Problem(objective, constraints)
result = prob.solve(warm_start = False, eps_abs = 1e-5)
result = prob.solve(warm_start=False, eps_abs=1e-5)
b_cvx.append(beta.value)
b_cvx = np.array(b_cvx)

t4 = time()

t4 = time()

T_pa[s, i] = (t1 - t0) / N_per_data
L_pa[s, i] = loss(X, y, lamb, np.mean(b_pa, axis=0))
Expand All @@ -129,23 +128,23 @@ def loss(X, y, lamb, beta):
L_dr[s, i] = loss(X, y, lamb, np.mean(b_dr, axis=0))
C_dr[s, i] = np.linalg.norm(C.dot(np.mean(b_dr, axis=0)))

T_cvx[s, i] = (t4 - t3) / N_per_data
T_cvx[s, i] = (t4 - t3) / N_per_data
L_cvx[s, i] = loss(X, y, lamb, np.mean(b_cvx, axis=0))
C_cvx[s, i] = np.linalg.norm(C.dot(np.mean(b_cvx, axis=0)))

np.savez(
os.path.join(my_path, 'bm-R1.npz'),
T_pa = T_pa,
L_pa = L_pa,
C_pa = C_pa,
T_pds = T_pds,
L_pds = L_pds,
C_pds = C_pds,
T_dr = T_dr,
L_dr = L_dr,
C_dr = C_dr,
T_cvx = T_cvx,
L_cvx = L_cvx,
C_cvx = C_cvx,
SIZES = np.array(SIZES)
os.path.join(my_path, "bm-R1.npz"),
T_pa=T_pa,
L_pa=L_pa,
C_pa=C_pa,
T_pds=T_pds,
L_pds=L_pds,
C_pds=C_pds,
T_dr=T_dr,
L_dr=L_dr,
C_dr=C_dr,
T_cvx=T_cvx,
L_cvx=L_cvx,
C_cvx=C_cvx,
SIZES=np.array(SIZES),
)
Loading

0 comments on commit f8475d8

Please sign in to comment.