All the code below must be run in context. None will work here.
Econcat = np.concatenate([np.linalg.eig(vec)[0].reshape((1, M)) for vec in A])
# Econcat = np.concatenate([np.linalg.eig(vec)[0][None] for vec in A])
print np.allclose(Econcat, Evec)
%%capture out
from IPython.parallel import Client
N = 1000
M = 4
np.random.seed(201)
A = np.random.random((N, M, M))
%timeit Evec = np.array(zip(*map(np.linalg.eig, A))[0])
for Neng in range(1, 9):
##%pxconfig --targets :2
dview = engines[:Neng]
dview.block = True
dview.activate()
dview.scatter('A', A.copy())
%px import numpy as np
%timeit %px Evec = np.array(zip(*map(np.linalg.eig, A))[0])
Evec_parallel = dview.gather('Evec')
#print np.allclose(Evec, Evec_parallel)
print out.stdout.split('\n')[0].split()
values = [l.split()[5] for l in out.stdout.split('\n')[1:9]]
plt.plot(np.arange(1, 9), values)
import pymks
??pymks.bin
In the following, we will just check that one test response is reasonably close to a response calculated with the influence coefficients. We construct a test_microstructre
and use the fipy_response
function to calculate test_response
.
np.random.seed(103)
test_microstructure = np.random.random(N**2)
test_response = fipy_response(test_microstructure, dt=dt, N=N)
binned_test_microstructure = bin(test_microstructure, Nbin).reshape((N, N, Nbin))
Fm = np.fft.fft2(binned_test_microstructure, axes=(0, 1))
Fr = np.sum(Fm * Fcoeff, axis=-1)
calc_response = np.fft.ifft2(Fr, axes=(0, 1)).real.flatten()
print test_response[:20]
print calc_response[:20]
import scipy.signal
import scipy.ndimage
?scipy.signal.fftconvolve
y_alt = scipy.ndimage.convolve(X[0], filter(np.linspace(0, 20, Nspace)), mode='wrap')
print np.allclose(np.roll(y_alt, 40), y[0])
X0 = np.random.random((1, Nspace))
y0 = model.predict(X0)
import scipy.ndimage
y_alt = scipy.ndimage.convolve(X0[0], filter(np.linspace(0, 20, Nspace)), mode='wrap')
print np.allclose(np.roll(y_alt, 40), y0)
import matplotlib.pyplot as plt
mse = metrics.mean_squared_error
Nbins = np.arange(2, 100, 10)
errors = []
for Nbin in Nbins:
print Nbin
model = MKSRegressionModel(Nbin=Nbin)
model.fit(X, y)
errors.append(mse(model.predict(X), y))
plt.plot(Nbins, errors)
plt.xlabel('Nbin')
plt.ylabel('MSE')
errors = []
Nbins = np.arange(2, 20)
for Nbin in Nbins:
model = MKSRegressionModel(Nbin=Nbin)
model.fit(X_train, y_train)
errors.append(mse(model.predict(X_test), y_test))
plt.plot(Nbins, errors)
plt.xlabel('Nbin')
plt.ylabel('MSE')
argmin = np.argmin(errors)
print "optimal Nbin: {0}, mse: {1:1.3e}".format(Nbins[argmin], errors[argmin])
??model.resize_coeff
??FastMKSRegressionModel