Skip to content

Commit 121cbd6

Browse files
committed
added autoapproximate functionality for cos basis
1 parent 9e47805 commit 121cbd6

File tree

3 files changed

+74
-37
lines changed

3 files changed

+74
-37
lines changed

src/pyANOVAapprox/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from scipy.optimize import bisect
99
from scipy.sparse.linalg import lsqr
1010
from scipy.special import erf
11+
import csv
12+
import os
1113

1214
# from sklearn.metrics import roc_auc_score
1315

src/pyANOVAapprox/approx.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ def __init__(
187187
):
188188

189189
self.X = X
190+
191+
if X.shape[0] != y.shape[0]:
192+
raise ValueError("X and y have different lengths.")
190193

191194
setting = approx_setting(
192195
parent=self,
@@ -384,7 +387,7 @@ def _autoapproximate(
384387
B,
385388
maxiter,
386389
solver,
387-
verbose,
390+
verbosity,
388391
solver_max_iter,
389392
solver_weights,
390393
solver_verbose,
@@ -398,8 +401,16 @@ def _autoapproximate(
398401

399402
D = dict([(u, tuple([1.0] * len(u))) for u in setting.U])
400403
t = dict([(u, tuple([1.0] * len(u))) for u in setting.U])
404+
405+
if verbosity>3:
406+
if not os.path.exists("log"):
407+
os.mkdir("log")
408+
with open('log/log.csv', 'w', newline='') as csvfile:
409+
csv.writer(csvfile, delimiter=',').writerow(["it"] + setting.U)
401410

402411
for idx in range(maxiter):
412+
if verbosity > 0:
413+
print("===== Iteration ", str(idx + 1), " =====")
403414
bw = compute_bandwidth(B, D, t)
404415
if setting.N is not None:
405416
self.addSetting(setting)
@@ -409,9 +420,14 @@ def _autoapproximate(
409420
setting.N = [bw[i] for i in setting.U]
410421
self.addTrafo()
411422

412-
if verbose:
413-
print("bw in iteration", str(idx + 1), "are", str(bw))
414-
423+
if verbosity > 0:
424+
for i in setting.U:
425+
print("bw in", str(i), ":", bw[i])
426+
#print("bw in iteration", str(idx + 1), "are", str(bw))
427+
print()
428+
if verbosity > 3:
429+
with open('log/log.csv', 'a', newline='') as csvfile:
430+
csv.writer(csvfile, delimiter=',').writerow(["bw in it"+str(idx+1)] + [str(bw[i]) for i in setting.U])
415431
self.approximate(
416432
lam=lam,
417433
solver=solver,
@@ -421,16 +437,25 @@ def _autoapproximate(
421437
tol=solver_tol,
422438
)
423439

424-
D, t = self.estimate_rates(lam=lam, verbose=verbose)
425-
if verbose:
426-
print(
427-
"estimated rates in iteration",
428-
str(idx + 1),
429-
"are D =",
430-
str(D),
431-
"and t =",
432-
str(t),
433-
)
440+
D, t = self.estimate_rates(lam=lam, verbosity=verbosity)
441+
if verbosity > 1:
442+
for i in setting.U:
443+
print("estimated rates for", str(i), ": D = ", str(D[i]), "and t = ", str(t[i]) )
444+
print()
445+
446+
#print(
447+
# "estimated rates in iteration",
448+
# str(idx + 1),
449+
# "are D =",
450+
# str(D),
451+
# "and t =",
452+
# str(t),
453+
#)
454+
if verbosity > 3:
455+
with open('log/log.csv', 'a', newline='') as csvfile:
456+
wr = csv.writer(csvfile, delimiter=',')
457+
wr.writerow(["D in it"+str(idx+1)] + [str(D[i]) for i in setting.U])
458+
wr.writerow(["t in it"+str(idx+1)] + [str(t[i]) for i in setting.U])
434459
return D, t
435460

436461
def autoapproximate(
@@ -440,7 +465,7 @@ def autoapproximate(
440465
B=None,
441466
maxiter=2,
442467
solver="lsqr",
443-
verbose=False,
468+
verbosity=0,
444469
solver_max_iter=50,
445470
solver_weights=None,
446471
solver_verbose=False,
@@ -462,7 +487,7 @@ def autoapproximate(
462487
B=B,
463488
maxiter=maxiter,
464489
solver=solver,
465-
verbose=verbose,
490+
verbosity=verbosity,
466491
solver_max_iter=solver_max_iter,
467492
solver_weights=solver_weights,
468493
solver_verbose=solver_verbose,
@@ -476,7 +501,7 @@ def autoapproximate(
476501
B=B,
477502
maxiter=maxiter,
478503
solver=solver,
479-
verbose=verbose,
504+
verbosity=verbosity,
480505
solver_max_iter=solver_max_iter,
481506
solver_weights=solver_weights,
482507
solver_verbose=solver_verbose,

src/pyANOVAapprox/bandwidth.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ def getfcu(ghat, u):
1313
return fcu
1414

1515

16-
def getaxissum(ghat, u, j):
16+
def getaxissum(ghat, u, j, system):
1717
fcu = getfcu(ghat, u)
1818
idx = [s.u for s in ghat.settings].index(u)
1919
bws = ghat.settings[idx].bandwidths
2020

2121
fcuj = np.sum(abs(fcu) ** 2, axis=tuple([i for i in range(len(u)) if i != j]))
2222

23-
fcuj = (
24-
fcuj[np.mod(range(int(bws[j] / 2), bws[j]), bws[j] - 1)]
25-
+ fcuj[range(int(bws[j] / 2) - 1, -1, -1)]
26-
)
23+
if system == "exp":
24+
fcuj = (
25+
fcuj[np.mod(range(int(bws[j] / 2), bws[j]), bws[j] - 1)]
26+
+ fcuj[range(int(bws[j] / 2) - 1, -1, -1)]
27+
)
28+
elif system == "cos":
29+
pass
30+
else:
31+
raise ValueError("For this basis is estimate rates not implemented")
2732
# fcuj = np.concatenate(fcuj[math.ceil(bws[j]/2):] + [fcuj[0]]) + fcuj[math.ceil(bws[j]/2)-1::-1]
2833
return fcuj
2934

@@ -106,7 +111,7 @@ def most_common_value(data):
106111
return t[max_index]
107112

108113

109-
def estimate_rates(self, lam, settingnr=None, verbose=False):
114+
def estimate_rates(self, lam, settingnr=None, verbosity=0):
110115
us = [s.u for s in self.getTrafo(settingnr).settings]
111116
nhat = GroupedCoefficients(
112117
self.getTrafo(settingnr).settings,
@@ -118,25 +123,28 @@ def estimate_rates(self, lam, settingnr=None, verbose=False):
118123

119124
mcl = np.exp(most_common_value(np.log(abs(self.getFc(settingnr)[lam].data))))
120125
threshold = 100 * mcl**2
121-
122-
if verbose:
123-
ps = []
126+
127+
if verbosity>5:
128+
if not os.path.exists(os.path.join("log","figures")):
129+
os.mkdir(os.path.join("log","figures"))
130+
num = 0
131+
132+
system = self.getTrafo(settingnr).system
124133

125134
for u in us:
126135
if len(u) == 0:
127136
continue
128137

129-
if verbose:
138+
if verbosity>5:
130139
fig, ax = plt.subplots()
131140
ax.set_xscale("log")
132141
ax.set_yscale("log")
133142
ax.set_title(str(u))
134-
ps.append(ax)
135143

136144
for j in range(len(u)):
137-
axissum = getaxissum(self.getFc(settingnr)[lam], u, j)
145+
axissum = getaxissum(self.getFc(settingnr)[lam], u, j, system)
138146

139-
axissumnum = getaxissum(nhat, u, j)
147+
axissumnum = getaxissum(nhat, u, j, system)
140148
idx = next(
141149
(
142150
i
@@ -146,7 +154,7 @@ def estimate_rates(self, lam, settingnr=None, verbose=False):
146154
None,
147155
)
148156

149-
if verbose:
157+
if verbosity>5:
150158

151159
y = np.cumsum(axissum[::-1])[::-1]
152160
ax.plot(
@@ -166,18 +174,20 @@ def estimate_rates(self, lam, settingnr=None, verbose=False):
166174
D[u][j] = Duj
167175
t[u][j] = -tuj / 2
168176

169-
if verbose:
177+
if verbosity>5:
170178
x = np.arange(1, idx + 1)
171179
ax.plot(
172180
x,
173181
D[u][j] * x ** (-2 * t[u][j]),
174182
linewidth=2,
175183
# color=j
176184
)
177-
if verbose:
178-
plt.figure(figsize=(12, 9))
179-
for ax in ps:
180-
plt.sca(ax)
181-
plt.show()
185+
if verbosity>5:
186+
#plt.figure(figsize=(12, 9))
187+
#for ax in ps:
188+
# plt.sca(ax)
189+
fig.savefig(os.path.join("log","figures", str(num).strip()+ "_rates_" + str(u).strip() + ".png"))
190+
num = num + 1
191+
plt.close(fig)
182192

183193
return D, t

0 commit comments

Comments
 (0)