Here are the examples of the python api numpy.asarray taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
160 Examples
2
Example 1
Project: gwpy Source File: table.py
def plot_table(self, table, x, y, color=None, size_by=None,
size_by_log=None, size_range=None, **kwargs):
"""Plot a LIGO_LW-format event `Table` onto these `Axes`
Parameters
----------
table : :class:`~glue.ligolw.table.Table`
LIGO_LW-format XML event `Table` to display
x : `str`
name of column to display on the X-axis
y : `str`
name of column to display on the Y-axis
c : `str`, optional
name of column by which to colour the data
**kwargs
any other arguments applicable to
:meth:`~matplotlib.axes.Axes.scatter`
Returns
-------
collection
"""
if size_by is not None and size_by_log is not None:
raise ValueError("size_by_color and size_by_log_color are "
"mutually exclusive options, please select one")
# get x-y data
xdata = get_table_column(table, x)
ydata = get_table_column(table, y)
# rank data by size or colour
sizecol = size_by or size_by_log or (size_range and color)
if color:
cdata = get_table_column(table, color)
if sizecol:
sdata = get_table_column(table, sizecol)
if color and sizecol:
zipped = zip(xdata, ydata, cdata, sdata)
zipped.sort(key=lambda row: row[2])
try:
xdata, ydata, cdata, sdata = map(numpy.asarray, zip(*zipped))
except ValueError:
pass
elif sizecol:
zipped = zip(xdata, ydata, sdata)
zipped.sort(key=lambda row: row[-1])
try:
xdata, ydata, sdata = map(numpy.asarray, zip(*zipped))
except ValueError:
pass
elif color:
zipped = zip(xdata, ydata, cdata)
zipped.sort(key=lambda row: row[-1])
try:
xdata, ydata, cdata = map(numpy.asarray, zip(*zipped))
except ValueError:
pass
# work out sizing
if sizecol:
if size_range is None and sdata.size:
size_range = [sdata.min(), sdata.max()]
if size_range:
# convert color value into a size between the given min and max
s = kwargs.pop('s', 20)
sizes = [s/10., s]
sp = (sdata - size_range[0]) / (size_range[1] - size_range[0])
sp[sp < 0] = 0
sp[sp > 1] = 1
if size_by_log is None:
sarray = sizes[0] + sp * (sizes[1] - sizes[0])
else:
logsizes = numpy.log10(sizes)
sarray = 10 ** (
logsizes[0] + sp * (logsizes[1] - logsizes[0]))
kwargs.setdefault('s', sarray)
if color:
return self.scatter(xdata, ydata, c=cdata, **kwargs)
else:
return self.scatter(xdata, ydata, **kwargs)
2
Example 2
Project: text_convnet Source File: model.py
def __init__(self, rng, input, n_in, n_out, decay, activation=None, order=3,
P=None, Q=None, R=None, O=None, b=None):
# input is len*batch*n_in
# output is len*batch*n_out
self.input = input
self.decay = decay
self.n_in = n_in
self.n_out = n_out
if P is None:
P_values = numpy.asarray(
rng.uniform(
low = -(3.0/n_out)**0.5,
high = (3.0/n_out)**0.5,
size = (n_in, n_out)
),
dtype = theano.config.floatX)
self.P = theano.shared(value = P_values, name = "P", borrow = True)
else:
self.P = P
if Q is None:
Q_values = numpy.asarray(
rng.uniform(
low = -(3.0/n_out)**0.5,
high = (3.0/n_out)**0.5,
size = (n_in, n_out)
),
dtype = theano.config.floatX)
self.Q = theano.shared(value = Q_values, name = "Q", borrow = True)
else:
self.Q = Q
if R is None:
R_values = numpy.asarray(
rng.uniform(
low = -(3.0/n_out)**0.5,
high = (3.0/n_out)**0.5,
size = (n_in, n_out)
),
dtype = theano.config.floatX)
self.R = theano.shared(value = R_values, name = "R", borrow = True)
else:
self.R = R
if O is None:
O_values = numpy.asarray(
rng.uniform(
low = -(3.0/n_out)**0.5,
high = (3.0/n_out)**0.5,
size = (n_out, n_out)
),
dtype = theano.config.floatX)
self.O = theano.shared(value = O_values, name = "O", borrow = True)
else:
self.O = O
if activation != None and activation.lower() == "relu":
if b is None:
b_values = numpy.zeros( (self.n_out,), dtype = theano.config.floatX ) + 0.01
self.b = theano.shared(value = b_values, name = 'b', borrow = True)
else:
self.b = b
elif activation != None and activation.lower() == "tanh":
if b is None:
b_values = numpy.zeros( (self.n_out,), dtype = theano.config.floatX )
self.b = theano.shared(value = b_values, name = 'b', borrow = True)
else:
self.b = b
else:
self.b = None
f1, s1, f2, s2, f3 = self.calculate(input)
if order == 3:
self.params = [ self.P, self.Q, self.R, self.O ]
self.output = self.apply_activation(T.dot(f1+f2+f3, self.O), activation)
elif order == 2:
self.params = [ self.P, self.Q, self.O ]
self.output = self.apply_activation(T.dot(f1+f2, self.O), activation)
elif order == 1:
self.params = [ self.P, self.O ]
self.output = self.apply_activation(T.dot(f1, self.O), activation)
else:
raise ValueError(
"unsupported order: {}".format(order)
)
if not (self.b is None):
self.params.append(self.b)
2
Example 3
Project: pyscf Source File: xcfun.py
def _eval_xc(fn_facs, rho, spin=0, relativity=0, deriv=1, verbose=None):
assert(deriv < 4)
if spin == 0:
rho_u = rho_d = numpy.asarray(rho, order='C')
else:
rho_u = numpy.asarray(rho[0], order='C')
rho_d = numpy.asarray(rho[1], order='C')
if rho_u.ndim == 2:
ngrids = rho_u.shape[1]
else:
ngrids = len(rho_u)
fn_ids = [x[0] for x in fn_facs]
facs = [x[1] for x in fn_facs]
if all((is_lda(x) for x in fn_ids)): # LDA
if spin == 0:
nvar = 1
else:
nvar = 2
elif any((is_meta_gga(x) for x in fn_ids)):
raise RuntimeError('xcfun MGGA interface not correct')
if spin == 0:
nvar = 3
else:
nvar = 7
else: # GGA
if spin == 0:
nvar = 2
else:
nvar = 5
outlen = (math.factorial(nvar+deriv) //
(math.factorial(nvar) * math.factorial(deriv)))
outbuf = numpy.empty((ngrids,outlen))
n = len(fn_ids)
_itrf.XCFUN_eval_xc(ctypes.c_int(n),
(ctypes.c_int*n)(*fn_ids), (ctypes.c_double*n)(*facs),
ctypes.c_int(spin),
ctypes.c_int(deriv), ctypes.c_int(ngrids),
rho_u.ctypes.data_as(ctypes.c_void_p),
rho_d.ctypes.data_as(ctypes.c_void_p),
outbuf.ctypes.data_as(ctypes.c_void_p))
outbuf = outbuf.T
exc = outbuf[0]
vxc = fxc = kxc = None
if nvar == 1:
if deriv > 0:
vxc = (outbuf[1], None, None, None)
if deriv > 1:
fxc = (outbuf[2],) + (None,)*9
if deriv > 2:
kxc = (outbuf[3], None, None, None)
elif nvar == 2:
if spin == 0: # GGA
if deriv > 0:
vxc = (outbuf[1], outbuf[2], None, None)
if deriv > 1:
fxc = (outbuf[3], outbuf[4], outbuf[5],) + (None,)*7
if deriv > 2:
kxc = outbuf[6:10]
else: # LDA
if deriv > 0:
vxc = (outbuf[1:3].T, None, None, None)
if deriv > 1:
fxc = (outbuf[3:6].T,) + (None,)*9
if deriv > 2:
kxc = (outbuf[6:10].T, None, None, None)
elif nvar == 5:
if deriv > 0:
vxc = (outbuf[1:3].T, outbuf[3:6].T, None, None)
if deriv > 1:
fxc = (outbuf[[XC_D20000,XC_D11000,XC_D02000]].T,
outbuf[[XC_D10100,XC_D10010,XC_D10001,
XC_D01100,XC_D01010,XC_D01001]].T,
outbuf[[XC_D00200,XC_D00110,XC_D00101,XC_D00020,XC_D00011,XC_D00002]].T) + (None,)*7
if deriv > 2:
kxc = (outbuf[[XC_D30000,XC_D21000,XC_D12000,XC_D03000]].T,
outbuf[[XC_D20100,XC_D20010,XC_D20001,
XC_D11100,XC_D11010,XC_D11001,
XC_D02100,XC_D02010,XC_D02001]].T,
outbuf[[XC_D10200,XC_D10110,XC_D10101,XC_D10020,XC_D10011,XC_D10002,
XC_D01200,XC_D01110,XC_D01101,XC_D01020,XC_D01011,XC_D01002]].T,
outbuf[[XC_D00300,XC_D00210,XC_D00201,XC_D00120,XC_D00111,
XC_D00102,XC_D00030,XC_D00021,XC_D00012,XC_D00003]].T)
# MGGA/MLGGA: Note the MLGGA interface are not implemented. MGGA only needs 3
# input arguments. To make the interface compatible with libxc, treat MGGA as
# MLGGA
elif nvar == 3:
if deriv > 0:
vxc = (outbuf[1], outbuf[2], numpy.zeros_like(outbuf[1]), outbuf[3])
if deriv > 1:
fxc = (outbuf[XC_D200], outbuf[XC_D110], outbuf[XC_D020],
None, outbuf[XC_D002], None, outbuf[XC_D101], None, None, outbuf[XC_D011])
if deriv > 2:
kxc = (output[XC_D300], output[XC_D210], output[XC_D120], output[XC_D030],
output[XC_D201], output[XC_D111], output[XC_D102],
output[XC_D021], output[XC_D012], output[XC_D003])
elif nvar == 7:
if deriv > 0:
vxc = (outbuf[1:3].T, outbuf[3:6].T, None, outbuf[6:8].T)
if deriv > 1:
fxc = (outbuf[[XC_D2000000,XC_D1100000,XC_D0200000]].T,
outbuf[[XC_D1010000,XC_D1001000,XC_D1000100,
XC_D0110000,XC_D0101000,XC_D0100100]].T,
outbuf[[XC_D0020000,XC_D0011000,XC_D0010100,
XC_D0002000,XC_D0001100,XC_D0000200]].T,
None,
outbuf[[XC_D0000020,XC_D0000011,XC_D0000002]].T,
None,
outbuf[[XC_D1000010,XC_D1000001,XC_D0100010,XC_D0100001]].T,
None, None,
outbuf[[XC_D0010010,XC_D0010001,XC_D0001010,XC_D0001001,
XC_D0000110,XC_D0000101]].T)
if deriv > 2:
kxc = (outbuf[[XC_D3000000,XC_D2100000,XC_D1200000,XC_D0300000]].T,
outbuf[[XC_D2010000,XC_D2001000,XC_D2000100,
XC_D1110000,XC_D1101000,XC_D1100100,
XC_D0210000,XC_D0201000,XC_D0200100]].T,
outbuf[[XC_D1020000,XC_D1011000,XC_D1010100,XC_D1002000,XC_D1001100,XC_D1000200,
XC_D0120000,XC_D0111000,XC_D0110100,XC_D0102000,XC_D0101100,XC_D0100200]].T,
outbuf[[XC_D0030000,XC_D0021000,XC_D0020100,XC_D0012000,XC_D0011100,
XC_D0010200,XC_D0003000,XC_D0002100,XC_D0001200,XC_D0000300]].T,
output[[XC_D2000010,XC_D2000001,XC_D1100010,XC_D1100001,XC_D0200010,XC_D0200001]].T,
output[[XC_D1010010,XC_D1010001,XC_D1001010,XC_D1001001,XC_D1000110,XC_D1000101,
XC_D0110010,XC_D0110001,XC_D0101010,XC_D0101001,XC_D0100110,XC_D0100101]].T,
output[[XC_D1000020,XC_D1000011,XC_D1000002,XC_D0100020,XC_D0100011,XC_D0100002]].T,
output[[XC_D0020010,XC_D0020001,XC_D0011010,XC_D0011001,XC_D0010110,XC_D0010101,
XC_D0002010,XC_D0002001,XC_D0001110,XC_D0001101,XC_D0000210,XC_D0000201]].T,
output[[XC_D0010020,XC_D0010011,XC_D0010002,
XC_D0001020,XC_D0001011,XC_D0001002,
XC_D0000120,XC_D0000111,XC_D0000102]].T,
output[[XC_D0000030,XC_D0000021,XC_D0000012,XC_D0000003]].T)
return exc, vxc, fxc, kxc
2
Example 4
Project: pylearn2 Source File: linesearch.py
def scalar_search_wolfe2(phi,
derphi,
phi0=None,
old_phi0=None,
derphi0=None,
n_iters=20,
c1=1e-4,
c2=0.9,
profile=False):
"""
Find alpha that satisfies strong Wolfe conditions.
alpha > 0 is assumed to be a descent direction.
Parameters
----------
phi : callable f(x)
Objective scalar function.
derphi : callable f'(x)
Objective function derivative (can be None)
phi0 : float, optional
Value of phi at s=0
old_phi0 : float, optional
Value of phi at previous point
derphi0 : float, optional
Value of derphi at s=0
c1 : float
Parameter for Armijo condition rule.
c2 : float
Parameter for curvature condition rule.
profile : flag (boolean)
True if you want printouts of profiling information
Returns
-------
alpha_star : float
Best alpha
phi_star : WRITEME
phi at alpha_star
phi0 : WRITEME
phi at 0
derphi_star : WRITEME
derphi at alpha_star
Notes
-----
Uses the line search algorithm to enforce strong Wolfe
conditions. See Wright and Nocedal, 'Numerical Optimization',
1999, pg. 59-60.
For the zoom phase it uses an algorithm by [...].
"""
if phi0 is None:
phi0 = phi(zero)
else:
phi0 = phi0
if derphi0 is None and derphi is not None:
derphi0 = derphi(zero)
else:
derphi0 = derphi0
alpha0 = zero
alpha0.name = 'alpha0'
if old_phi0 is not None:
alpha1 = TT.minimum(one,
numpy.asarray(1.01, dtype=theano.config.floatX) *
numpy.asarray(2, dtype=theano.config.floatX) * \
(phi0 - old_phi0) / derphi0)
else:
old_phi0 = nan
alpha1 = one
alpha1 = TT.switch(alpha1 < zero, one, alpha1)
alpha1.name = 'alpha1'
# This shouldn't happen. Perhaps the increment has slipped below
# machine precision? For now, set the return variables skip the
# useless while loop, and raise warnflag=2 due to possible imprecision.
phi0 = TT.switch(TT.eq(alpha1, zero), old_phi0, phi0)
# I need a lazyif for alpha1 == 0 !!!
phi_a1 = ifelse(TT.eq(alpha1, zero), phi0,
phi(alpha1), name='phi_a1')
phi_a1.name = 'phi_a1'
phi_a0 = phi0
phi_a0.name = 'phi_a0'
derphi_a0 = derphi0
derphi_a0.name = 'derphi_a0'
# Make sure variables are tensors otherwise strange things happen
c1 = TT.as_tensor_variable(c1)
c2 = TT.as_tensor_variable(c2)
maxiter = n_iters
def while_search(alpha0, alpha1, phi_a0, phi_a1, derphi_a0, i_t,
alpha_star, phi_star, derphi_star):
derphi_a1 = derphi(alpha1)
cond1 = TT.bitwise_or(phi_a1 > phi0 + c1 * alpha1 * derphi0,
TT.bitwise_and(phi_a1 >= phi_a0, i_t > zero))
cond2 = abs(derphi_a1) <= -c2 * derphi0
cond3 = derphi_a1 >= zero
alpha_star_c1, phi_star_c1, derphi_star_c1 = \
_zoom(alpha0, alpha1, phi_a0, phi_a1, derphi_a0,
phi, derphi, phi0, derphi0, c1, c2,
profile=profile)
alpha_star_c3, phi_star_c3, derphi_star_c3 = \
_zoom(alpha1, alpha0, phi_a1, phi_a0, derphi_a1, phi,
derphi, phi0, derphi0, c1, c2,
profile=profile)
nw_alpha1 = alpha1 * numpy.asarray(2, dtype=theano.config.floatX)
nw_phi = phi(nw_alpha1)
alpha_star, phi_star, derphi_star = \
ifelse(cond1,
(alpha_star_c1, phi_star_c1, derphi_star_c1),
ifelse(cond2,
(alpha1, phi_a1, derphi_a1),
ifelse(cond3,
(alpha_star_c3, phi_star_c3, derphi_star_c3),
(nw_alpha1, nw_phi, nan),
name='alphastar_c3'),
name='alphastar_c2'),
name='alphastar_c1')
return ([alpha1,
nw_alpha1,
phi_a1,
ifelse(lazy_or('allconds',
cond1,
cond2,
cond3),
phi_a1,
nw_phi,
name='nwphi1'),
ifelse(cond1, derphi_a0, derphi_a1, name='derphi'),
i_t + one,
alpha_star,
phi_star,
derphi_star],
theano.scan_module.scan_utils.until(
lazy_or('until_cond_',
TT.eq(nw_alpha1, zero),
cond1,
cond2,
cond3)))
states = [alpha0, alpha1, phi_a0, phi_a1, derphi_a0]
# i_t
states.append(zero)
# alpha_star
states.append(zero)
# phi_star
states.append(zero)
# derphi_star
states.append(zero)
# print 'while_search'
outs, updates = scan(while_search,
outputs_info=states,
n_steps=maxiter,
name='while_search',
mode=theano.Mode(linker='cvm_nogc'),
profile=profile)
# print 'done_while_search'
out3 = outs[-3][-1]
out2 = outs[-2][-1]
out1 = outs[-1][-1]
alpha_star, phi_star, derphi_star = \
ifelse(TT.eq(alpha1, zero),
(nan, phi0, nan),
(out3, out2, out1), name='main_alphastar')
return alpha_star, phi_star, phi0, derphi_star
2
Example 5
Project: DECRES Source File: mlp.py
def train_model(train_set_x_org=None, train_set_y_org=None, valid_set_x_org=None, valid_set_y_org=None,
learning_rate=0.1, alpha=0.01, lambda_reg=0.00, alpha_reg=0.0001, n_hidden=[256,128,16],
n_epochs=1000, batch_size=100, activation_func="tanh", rng=numpy.random.RandomState(100),
max_num_epoch_change_learning_rate=100,max_num_epoch_change_rate=0.8,learning_rate_decay_rate=0.8):
"""
Train a multilayer perceptrons model.
INPUTS:
train_set_x_org: numpy 2d array, each row is a training sample.
train_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of training samples.
valid_set_x_org: numpy 2d array, each row is a validation sample.
This set is to monitor the convergence of optimization.
valid_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of validation samples.
learning_rate: float scalar, the initial learning rate.
alpha: float, parameter to trade off the momentum term.
lambda_reg: float, paramter to control the sparsity of weights by l_1 norm.
The regularization term is lambda_reg( (1-alpha_reg)/2 * \sum||W_i||_2^2 + alpha_reg \sum||W_i||_1 ).
Thus, the larger lambda_reg is, the sparser the weights are.
alpha_reg: float, paramter to control the smoothness of weights by squared l_2 norm.
The regularization term is lambda_reg( (1-alpha_reg)/2 * \sum||W_i||_2^2 + alpha_reg \sum||W_i||_1 ),
Thus, the smaller alpha_reg is, the smoother the weights are.
n_hidden, vector of int, n_hidden[i]: number of hidden units of the i-th layer.
n_epochs: int scalar, the maximal number of epochs.
batch_size: int scalar, minibatch size.
activation_func: string, specify activation function. {"tanh" (default),"sigmoid"}
rng: numpy random number state.
OUTPUTS:
classifier: object of MLP, the model learned, returned for testing.
training_time: float, training time in seconds.
"""
train_set_x = theano.shared(numpy.asarray(train_set_x_org,dtype=theano.config.floatX),borrow=True)
train_set_y = T.cast(theano.shared(numpy.asarray(train_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
valid_set_x = theano.shared(numpy.asarray(valid_set_x_org,dtype=theano.config.floatX),borrow=True)
valid_set_y = T.cast(theano.shared(numpy.asarray(valid_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
# compute number of minibatches for training, validation and testing
n_train_batches = int(math.ceil(train_set_x.get_value(borrow=True).shape[0] / batch_size))
# shared variable to reduce the learning rate
learning_rate_shared=theano.shared(learning_rate,name='learn_rate_shared')
decay_rate=T.scalar(name='decay_rate',dtype=theano.config.floatX)
reduce_learning_rate=theano.function([decay_rate],learning_rate_shared,updates=[(learning_rate_shared,learning_rate_shared*decay_rate)])
## define the model below
num_feat=train_set_x.get_value(borrow=True).shape[1] # number of features
n_cl=len(numpy.unique(train_set_y_org)) # number of classes
activations={"tanh":T.tanh, "sigmoid":T.nnet.sigmoid, "relu":relu}
activation=activations[activation_func]
# build a MPL object
classifier = MLP(rng=rng, n_in=num_feat, n_hidden=n_hidden, n_out=n_cl, activation=activation,lambda_reg=lambda_reg,alpha_reg=alpha_reg)
train_model_one_iteration=classifier.build_train_function(train_set_x, train_set_y, batch_size,
alpha, learning_rate_shared)
validate_model=classifier.build_valid_function(valid_set_x, valid_set_y, batch_size)
print '... training'
# early-stopping parameters
patience = 5000 # look as this many examples regardless
patience_increase = 2 # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = min(n_train_batches, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch
best_validation_loss = numpy.inf
#max_num_epoch_change_learning_rate=100
max_num_epoch_not_improve=3*max_num_epoch_change_learning_rate
#max_num_epoch_change_rate=0.8
#learning_rate_decay_rate=0.8
epoch_change_count=0
start_time = time.clock()
done_looping = False
epoch = 0
while (epoch < n_epochs) and (not done_looping):
epoch = epoch + 1
epoch_change_count=epoch_change_count+1
if epoch_change_count % max_num_epoch_change_learning_rate ==0:
reduce_learning_rate(learning_rate_decay_rate)
max_num_epoch_change_learning_rate= \
cl.change_max_num_epoch_change_learning_rate(max_num_epoch_change_learning_rate,max_num_epoch_change_rate)
max_num_epoch_not_improve=3*max_num_epoch_change_learning_rate
epoch_change_count=0
for minibatch_index in xrange(n_train_batches):
minibatch_avg_cost = train_model_one_iteration(minibatch_index)
# iteration number
iter = (epoch - 1) * n_train_batches + minibatch_index
if (iter + 1) % validation_frequency == 0:
# compute zero-one loss on validation set
validation_losses = validate_model()
this_validation_loss = numpy.mean(validation_losses)
print('epoch %i, minibatch %i/%i, validation error %f %%' % \
(epoch, minibatch_index + 1, n_train_batches,
this_validation_loss * 100.))
# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
num_epoch_not_improve=0
if this_validation_loss < best_validation_loss:
#improve patience if loss improvement is good enough
if this_validation_loss < best_validation_loss * \
improvement_threshold:
patience = max(patience, iter * patience_increase)
best_validation_loss = this_validation_loss
# save a copy of the currently best model parameter
best_model_params=classifier.get_params()
if patience <= iter:
done_looping = True
break
if this_validation_loss >= best_validation_loss:
num_epoch_not_improve=num_epoch_not_improve+1
if num_epoch_not_improve>=max_num_epoch_not_improve:
done_looping = True
break
# set the best model parameters
classifier.set_params(best_model_params)
end_time = time.clock()
training_time=end_time-start_time
print 'Training time: %f' %(training_time/60)
print 'Optimization complete with best validation score of %f,' %(best_validation_loss * 100.)
return classifier,training_time
2
Example 6
def __init__(self, corpus=None, num_topics=100, id2word=None, distributed=False,
chunksize=2000, passes=1, update_every=1, alpha='symmetric', eta=None, decay=0.5,
eval_every=10, iterations=50, gamma_threshold=0.001):
"""
If given, start training from the iterable `corpus` straight away. If not given,
the model is left untrained (presumably because you want to call `update()` manually).
`num_topics` is the number of requested latent topics to be extracted from
the training corpus.
`id2word` is a mapping from word ids (integers) to words (strings). It is
used to determine the vocabulary size, as well as for debugging and topic
printing.
`alpha` and `eta` are hyperparameters that affect sparsity of the docuement-topic
(theta) and topic-word (lambda) distributions. Both default to a symmetric
1.0/num_topics prior.
`alpha` can be set to an explicit array = prior of your choice. It also
support special values of 'asymmetric' and 'auto': the former uses a fixed
normalized asymmetric 1.0/topicno prior, the latter learns an asymmetric
prior directly from your data.
`eta' can be a scalar for a symmetric prior over topic/word
distributions, or a matrix of shape num_topics x num_words,
which can be used to impose asymmetric priors over the word
distribution on a per-topic basis. This may be useful if you
want to seed certain topics with particular words by boosting
the priors for those words.
Turn on `distributed` to force distributed computing (see the `web tutorial <http://radimrehurek.com/gensim/distributed.html>`_
on how to set up a cluster of machines for gensim).
Calculate and log perplexity estimate from the latest mini-batch every
`eval_every` model updates (setting this to 1 slows down training ~2x;
default is 10 for better performance). Set to None to disable perplexity estimation.
Example:
>>> lda = LdaModel(corpus, num_topics=100) # train model
>>> print(lda[doc_bow]) # get topic probability distribution for a docuement
>>> lda.update(corpus2) # update the LDA model with additional docuements
>>> print(lda[doc_bow])
>>> lda = LdaModel(corpus, num_topics=50, alpha='auto', eval_every=5) # train asymmetric alpha from data
"""
# store user-supplied parameters
self.id2word = id2word
if corpus is None and self.id2word is None:
raise ValueError('at least one of corpus/id2word must be specified, to establish input space dimensionality')
if self.id2word is None:
logger.warning("no word id mapping provided; initializing from corpus, assuming identity")
self.id2word = utils.dict_from_corpus(corpus)
self.num_terms = len(self.id2word)
elif len(self.id2word) > 0:
self.num_terms = 1 + max(self.id2word.keys())
else:
self.num_terms = 0
if self.num_terms == 0:
raise ValueError("cannot compute LDA over an empty collection (no terms)")
self.distributed = bool(distributed)
self.num_topics = int(num_topics)
self.chunksize = chunksize
self.decay = decay
self.num_updates = 0
self.passes = passes
self.update_every = update_every
self.eval_every = eval_every
self.optimize_alpha = alpha == 'auto'
if alpha == 'symmetric' or alpha is None:
logger.info("using symmetric alpha at %s" % (1.0 / num_topics))
self.alpha = numpy.asarray([1.0 / num_topics for i in xrange(num_topics)])
elif alpha == 'asymmetric':
self.alpha = numpy.asarray([1.0 / (i + numpy.sqrt(num_topics)) for i in xrange(num_topics)])
self.alpha /= self.alpha.sum()
logger.info("using asymmetric alpha %s" % list(self.alpha))
elif alpha == 'auto':
self.alpha = numpy.asarray([1.0 / num_topics for i in xrange(num_topics)])
logger.info("using autotuned alpha, starting with %s" % list(self.alpha))
else:
# must be either float or an array of floats, of size num_topics
self.alpha = alpha if isinstance(alpha, numpy.ndarray) else numpy.asarray([alpha] * num_topics)
if len(self.alpha) != num_topics:
raise RuntimeError("invalid alpha shape (must match num_topics)")
if eta is None:
self.eta = 1.0 / num_topics
else:
self.eta = eta
# VB constants
self.iterations = iterations
self.gamma_threshold = gamma_threshold
# set up distributed environment if necessary
if not distributed:
logger.info("using serial LDA version on this node")
self.dispatcher = None
self.numworkers = 1
else:
if self.optimize_alpha:
raise NotImplementedError("auto-optimizing alpha not implemented in distributed LDA")
# set up distributed version
try:
import Pyro4
dispatcher = Pyro4.Proxy('PYRONAME:gensim.lda_dispatcher')
dispatcher._pyroOneway.add("exit")
logger.debug("looking for dispatcher at %s" % str(dispatcher._pyroUri))
dispatcher.initialize(id2word=self.id2word, num_topics=num_topics,
chunksize=chunksize, alpha=alpha, eta=eta, distributed=False)
self.dispatcher = dispatcher
self.numworkers = len(dispatcher.getworkers())
logger.info("using distributed version with %i workers" % self.numworkers)
except Exception as err:
logger.error("failed to initialize distributed LDA (%s)" % err)
raise RuntimeError("failed to initialize distributed LDA (%s)" % err)
# Initialize the variational distribution q(beta|lambda)
self.state = LdaState(self.eta, (self.num_topics, self.num_terms))
self.state.sstats = numpy.random.gamma(100., 1. / 100., (self.num_topics, self.num_terms))
self.sync_state()
# if a training corpus was provided, start estimating the model right away
if corpus is not None:
self.update(corpus)
2
Example 7
Project: pyscf Source File: r_outcore.py
def general(mol, mo_coeffs, erifile, dataname='eri_mo', tmpdir=None,
intor='cint2e', aosym='s4', comp=1,
max_memory=4000, ioblk_size=256, verbose=logger.WARN):
time_0pass = (time.clock(), time.time())
if isinstance(verbose, logger.Logger):
log = verbose
else:
log = logger.Logger(mol.stdout, verbose)
klsame = iden_coeffs(mo_coeffs[2], mo_coeffs[3])
nmoi = mo_coeffs[0].shape[1]
nmoj = mo_coeffs[1].shape[1]
nmok = mo_coeffs[2].shape[1]
nmol = mo_coeffs[3].shape[1]
nao = mo_coeffs[0].shape[0]
aosym = _stand_sym_code(aosym)
if aosym in ('s1', 's2ij', 'a2ij'):
nao_pair = nao * nao
else:
nao_pair = guess_nao_pair(mol, nao)
nij_pair = nmoi*nmoj
nkl_pair = nmok*nmol
if klsame and aosym in ('s4', 's2kl', 'a2kl', 'a4ij', 'a4kl', 'a4'):
log.debug('k-mo == l-mo')
mokl = numpy.asarray(mo_coeffs[2], order='F')
klshape = (0, nmok, 0, nmok)
else:
mokl = numpy.asarray(numpy.hstack((mo_coeffs[2],mo_coeffs[3])), order='F')
klshape = (0, nmok, nmok, nmok+nmol)
if isinstance(erifile, str):
if h5py.is_hdf5(erifile):
feri = h5py.File(erifile)
if dataname in feri:
del(feri[dataname])
else:
feri = h5py.File(erifile, 'w')
else:
assert(isinstance(erifile, h5py.Group))
feri = erifile
if comp == 1:
chunks = (nmoj,nmol)
h5d_eri = feri.create_dataset(dataname, (nij_pair,nkl_pair),
'c16', chunks=chunks)
else:
chunks = (1,nmoj,nmol)
h5d_eri = feri.create_dataset(dataname, (comp,nij_pair,nkl_pair),
'c16', chunks=chunks)
if nij_pair == 0 or nkl_pair == 0:
feri.close()
return erifile
log.debug('MO integrals %s are saved in %s/%s', intor, erifile, dataname)
log.debug('num. MO ints = %.8g, required disk %.8g MB',
float(nij_pair)*nkl_pair*comp, nij_pair*nkl_pair*comp*16/1e6)
# transform e1
swapfile = tempfile.NamedTemporaryFile(dir=tmpdir)
half_e1(mol, mo_coeffs, swapfile.name, intor, aosym, comp,
max_memory, ioblk_size, log)
time_1pass = log.timer('AO->MO transformation for %s 1 pass'%intor,
*time_0pass)
e2buflen = guess_e2bufsize(ioblk_size, nij_pair, nao_pair)[0]
log.debug('step2: kl-pair (ao %d, mo %d), mem %.8g MB, '
'ioblock (r/w) %.8g/%.8g MB', \
nao_pair, nkl_pair, e2buflen*nao_pair*16/1e6,
e2buflen*nij_pair*16/1e6, e2buflen*nkl_pair*16/1e6)
fswap = h5py.File(swapfile.name, 'r')
klaoblks = len(fswap['0'])
ijmoblks = int(numpy.ceil(float(nij_pair)/e2buflen)) * comp
ao_loc = numpy.asarray(mol.ao_loc_2c(), dtype=numpy.int32)
tao = numpy.asarray(mol.tmap(), dtype=numpy.int32)
ti0 = time_1pass
buf = numpy.empty((e2buflen, nao_pair), dtype=numpy.complex)
istep = 0
for row0, row1 in prange(0, nij_pair, e2buflen):
nrow = row1 - row0
for icomp in range(comp):
istep += 1
tioi = 0
log.debug('step 2 [%d/%d], [%d,%d:%d], row = %d', \
istep, ijmoblks, icomp, row0, row1, nrow)
col0 = 0
for ic in range(klaoblks):
dat = fswap['%d/%d'%(icomp,ic)]
col1 = col0 + dat.shape[1]
buf[:nrow,col0:col1] = dat[row0:row1]
col0 = col1
ti2 = log.timer('step 2 [%d/%d], load buf'%(istep,ijmoblks), *ti0)
tioi += ti2[1]-ti0[1]
pbuf = _ao2mo.r_e2(buf[:nrow], mokl, klshape, tao, ao_loc, aosym)
tw1 = time.time()
if comp == 1:
h5d_eri[row0:row1] = pbuf
else:
h5d_eri[icomp,row0:row1] = pbuf
tioi += time.time()-tw1
ti1 = (time.clock(), time.time())
log.debug('step 2 [%d/%d] CPU time: %9.2f, Wall time: %9.2f, I/O time: %9.2f', \
istep, ijmoblks, ti1[0]-ti0[0], ti1[1]-ti0[1], tioi)
ti0 = ti1
buf = pbuf = None
fswap.close()
if isinstance(erifile, str):
feri.close()
log.timer('AO->MO transformation for %s 2 pass'%intor, *time_1pass)
log.timer('AO->MO transformation for %s '%intor, *time_0pass)
return erifile
2
Example 8
Project: deepcca Source File: SdA_orig.py
def test_SdA(finetune_lr=0.1, pretraining_epochs=0,
pretrain_lr=0.05, training_epochs=100,
dataset='mnist.pkl.gz', batch_size=10):
"""
Demonstrates how to train and test a stochastic denoising autoencoder.
This is demonstrated on MNIST.
:type learning_rate: float
:param learning_rate: learning rate used in the finetune stage
(factor for the stochastic gradient)
:type pretraining_epochs: int
:param pretraining_epochs: number of epoch to do pretraining
:type pretrain_lr: float
:param pretrain_lr: learning rate to be used during pre-training
:type n_iter: int
:param n_iter: maximal number of iterations ot run the optimizer
:type dataset: string
:param dataset: path the the pickled dataset
"""
datasets = load_data(dataset)
train_set_x, train_set_y = datasets[0]
valid_set_x, valid_set_y = datasets[1]
test_set_x, test_set_y = datasets[2]
train_set_x=train_set_x.eval()
train_set_y=train_set_y.eval()
train_set_x_lab=train_set_x[:1000,:]
train_set_x_unlab=train_set_x[1000:,:]
train_set_y_lab=train_set_y[:1000]
train_set_y_unlab=train_set_y[1000:]
import theano
train_set_x_lab=theano.shared(numpy.asarray(train_set_x_lab,
dtype=theano.config.floatX),
borrow=True)
train_set_y_lab=theano.shared(numpy.asarray(train_set_y_lab,
dtype=theano.config.floatX),
borrow=True)
train_set_y_lab=T.cast(train_set_y_lab, 'int32')
train_set_x_unlab=theano.shared(numpy.asarray(train_set_x_unlab,
dtype=theano.config.floatX),
borrow=True)
train_set_y_unlab=theano.shared(numpy.asarray(train_set_y_unlab,
dtype=theano.config.floatX),
borrow=True)
train_set_y_unlab=T.cast(train_set_y_unlab, 'int32')
# compute number of minibatches for training, validation and testing
n_train_batches = train_set_y_lab.eval().shape[0]
n_train_batches /= batch_size
n_train_batches_u = train_set_y_unlab.eval().shape[0]
n_train_batches_u /= batch_size
# numpy random generator
# start-snippet-3
numpy_rng = numpy.random.RandomState(89677)
print '... building the model'
# construct the stacked denoising autoencoder class
hidden_layer_size = 100
sda = SdA(
numpy_rng=numpy_rng,
n_ins=28 * 28,
hidden_layers_sizes=[100],
n_outs=10
)
# end-snippet-3 start-snippet-4
#########################
# PRETRAINING THE MODEL #
#########################
print '... getting the pretraining functions'
pretraining_fns = sda.pretraining_functions(train_set_x=train_set_x_unlab,
batch_size=batch_size)
print '... pre-training the model'
start_time = time.clock()
## Pre-train layer-wise
corruption_levels = [0.1, 0.2, 0.3]
for i in xrange(sda.n_layers):
# go through pretraining epochs
for epoch in xrange(pretraining_epochs):
# go through the training set
c = []
for batch_index in xrange(n_train_batches):
c.append(pretraining_fns[i](index=batch_index,
corruption=corruption_levels[i],##$
lr=pretrain_lr))
print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),
print numpy.mean(c)
end_time = time.clock()
print >> sys.stderr, ('The pretraining code for file ' +
os.path.split(__file__)[1] +
' ran for %.2fm' % ((end_time - start_time) / 60.))
fprop = theano.function(
[],
sda.output,
givens={
sda.x: test_set_x
},
name='fp'
)
Q=fprop()
print 'rec', ((Q-test_set_x.eval())**2).mean()
from utils import tile_raster_images,plot_weights
import PIL.Image as Image
image = Image.fromarray(
tile_raster_images(X=sda.dA_layers[0].W.get_value(borrow=True).T,
img_shape=(28, 28), tile_shape=(10, 10),
tile_spacing=(1, 1)))
image.save('filters_corruption_0.png')
# end-snippet-4
########################
# FINETUNING THE MODEL FOR REGRESSION #
if 0 : # pretrain middle layer
print '... pre-training MIDDLE layer'
h1 = T.matrix('x') # the data is presented as rasterized images
h2 = T.matrix('y') # the labels are presented as 1D vector of
log_reg = HiddenLayer(numpy_rng, h1, hidden_layer_size, hidden_layer_size)
if 1: # for middle layer
learning_rate = 0.05
fprop_inp = theano.function(
[],
SdA_inp.sigmoid_layers[-1].output,
givens={
SdA_inp.sigmoid_layers[0].input: train_set_x
},
name='fprop_inp'
)
fprop_out = theano.function(
[],
SdA_out.sigmoid_layers[-1].output,
givens={
SdA_out.sigmoid_layers[0].input: train_set_y
},
name='fprop_out'
)
H11=fprop_inp()
H21=fprop_out()
H1=N1.predict(train_set_x.eval())
H2=N2.predict(train_set_y.eval())
H1=theano.shared(H1)
H2=theano.shared(H2)
# compute the gradients with respect to the model parameters
logreg_cost = log_reg.mse(h2)
gparams = T.grad(logreg_cost, log_reg.params)
# compute list of fine-tuning updates
updates = [
(param, param - gparam * learning_rate)
for param, gparam in zip(log_reg.params, gparams)
]
train_fn_middle = theano.function(
inputs=[],
outputs=logreg_cost,
updates=updates,
givens={
h1: H1,
h2: H2
},
name='train_middle'
)
epoch = 0
while epoch < 10:
print epoch, train_fn_middle()
epoch += 1
from mlp import MLP
net = MLP(numpy_rng, train_set_x_lab, 28*14, hidden_layer_size, 28*14, W1=sda.dA_layers[0].W, b1=sda.dA_layers[0].b, W2=None, b2=None)
########################
########################
# FINETUNING THE MODEL #
########################
# get the training, validation and testing function for the model
print '... getting the finetuning functions'
train_fn, validate_model, test_model = sda.build_finetune_functions(
datasets=datasets,
batch_size=batch_size,
learning_rate=finetune_lr
)
print '... finetunning the model'
# early-stopping parameters
patience = 10 * n_train_batches # look as this many examples regardless
patience_increase = 2. # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = min(n_train_batches, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch
best_validation_loss = numpy.inf
test_score = 0.
start_time = time.clock()
done_looping = False
epoch = 0
while (epoch < training_epochs) and (not done_looping):
epoch = epoch + 1
for minibatch_index in xrange(n_train_batches):
minibatch_avg_cost = train_fn(minibatch_index)
iter = (epoch - 1) * n_train_batches + minibatch_index
if (iter + 1) % validation_frequency == 0:
validation_losses = validate_model()
this_validation_loss = numpy.mean(validation_losses)
print('epoch %i, minibatch %i/%i, validation error %f %%' %
(epoch, minibatch_index + 1, n_train_batches,
this_validation_loss * 100.))
# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
#improve patience if loss improvement is good enough
if (
this_validation_loss < best_validation_loss *
improvement_threshold
):
patience = max(patience, iter * patience_increase)
# save best validation score and iteration number
best_validation_loss = this_validation_loss
best_iter = iter
# test it on the test set
test_losses = test_model()
test_score = numpy.mean(test_losses)
print((' epoch %i, minibatch %i/%i, test error of '
'best model %f %%') %
(epoch, minibatch_index + 1, n_train_batches,
test_score * 100.))
if patience <= iter:
done_looping = True
break
end_time = time.clock()
print(
(
'Optimization complete with best validation score of %f %%, '
'on iteration %i, '
'with test performance %f %%'
)
% (best_validation_loss * 100., best_iter + 1, test_score * 100.)
)
print >> sys.stderr, ('The training code for file ' +
os.path.split(__file__)[1] +
' ran for %.2fm' % ((end_time - start_time) / 60.))
2
Example 9
Project: DECRES Source File: deep_feat_select_mlp.py
def train_model(train_set_x_org=None, train_set_y_org=None, valid_set_x_org=None, valid_set_y_org=None,
learning_rate=0.1, alpha=0.01,
lambda1=0.001, lambda2=1.0, alpha1=0.001, alpha2=0.0,
n_hidden=[256,128,16], n_epochs=1000, batch_size=100,
activation_func="tanh", rng=numpy.random.RandomState(100),
max_num_epoch_change_learning_rate=100,max_num_epoch_change_rate=0.8,learning_rate_decay_rate=0.8):
"""
Train a deep feature selection model.
INPUTS:
train_set_x_org: numpy 2d array, each row is a training sample.
train_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of training samples.
valid_set_x_org: numpy 2d array, each row is a validation sample.
This set is to monitor the convergence of optimization.
valid_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of validation samples.
learning_rate: float scalar, the initial learning rate.
alpha: float, parameter to trade off the momentum term.
lambda1: float scalar, control the sparsity of the input weights.
The regularization term is lambda1( (1-lambda2)/2 * ||w||_2^2 + lambda2 * ||w||_1 ).
Thus, the larger lambda1 is, the sparser the input weights are.
lambda2: float scalar, control the smoothness of the input weights.
The regularization term is lambda1( (1-lambda2)/2 * ||w||_2^2 + lambda2 * ||w||_1 ).
Thus, the larger lambda2 is, the smoother the input weights are.
alpha1: float scalar, control the sparsity of the weight matrices in MLP.
The regularization term is alpha1( (1-alpha2)/2 * \sum||W_i||_2^2 + alpha2 \sum||W_i||_1 ).
Thus, the larger alpha1 is, the sparser the MLP weights are.
alpha2: float scalar, control the smoothness of the weight matrices in MLP.
The regularization term is alpha1( (1-alpha2)/2 * \sum||W_i||_2^2 + alpha2 \sum||W_i||_1 ).
Thus, the larger alpha2 is, the smoother the MLP weights are.
n_hidden, vector of int, n_hidden[i]: number of hidden units of the i-th layer.
n_epochs: int scalar, the maximal number of epochs.
batch_size: int scalar, minibatch size.
activation_func: string, specify activation function. {"tanh" (default),"sigmoid"}
rng: numpy random number state.
OUTPUTS:
classifier: object of MLP, the model learned, returned for testing.
training_time: float, training time in seconds.
"""
train_set_x = theano.shared(numpy.asarray(train_set_x_org,dtype=theano.config.floatX),borrow=True)
train_set_y = T.cast(theano.shared(numpy.asarray(train_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
valid_set_x = theano.shared(numpy.asarray(valid_set_x_org,dtype=theano.config.floatX),borrow=True)
valid_set_y = T.cast(theano.shared(numpy.asarray(valid_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
# compute number of minibatches for training, validation and testing
n_train_batches = int(math.ceil(train_set_x.get_value(borrow=True).shape[0] / batch_size))
# shared variable to reduce the learning rate
learning_rate_shared=theano.shared(learning_rate,name='learn_rate_shared')
decay_rate=T.scalar(name='decay_rate',dtype=theano.config.floatX)
reduce_learning_rate=theano.function([decay_rate],learning_rate_shared,updates=[(learning_rate_shared,learning_rate_shared*decay_rate)])
## define the model below
num_feat=train_set_x.get_value(borrow=True).shape[1] # number of features
n_cl=len(numpy.unique(train_set_y_org)) # number of classes
activations={"tanh":T.tanh,"sigmoid":T.nnet.sigmoid,"relu":relu}
activation=activations[activation_func]
# build a MPL object
classifier = DFS(rng=rng, n_in=num_feat, n_hidden=n_hidden, n_out=n_cl,
lambda1=lambda1, lambda2=lambda2, alpha1=alpha1, alpha2=alpha2,
activation=activation)
train_model_one_iteration=classifier.build_train_function(train_set_x, train_set_y, batch_size,
alpha, learning_rate_shared)
validate_model=classifier.build_valid_function(valid_set_x, valid_set_y, batch_size)
print '... training'
# early-stopping parameters
patience = 5000 # look as this many examples regardless
patience_increase = 2 # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = min(n_train_batches, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch
best_validation_loss = numpy.inf
#max_num_epoch_change_learning_rate=100
max_num_epoch_not_improve=3*max_num_epoch_change_learning_rate
#max_num_epoch_change_rate=0.8
#learning_rate_decay_rate=0.8
epoch_change_count=0
start_time = time.clock()
done_looping = False
epoch = 0
while (epoch < n_epochs) and (not done_looping):
epoch = epoch + 1
epoch_change_count=epoch_change_count+1
if epoch_change_count % max_num_epoch_change_learning_rate ==0:
reduce_learning_rate(learning_rate_decay_rate)
max_num_epoch_change_learning_rate= \
cl.change_max_num_epoch_change_learning_rate(max_num_epoch_change_learning_rate,max_num_epoch_change_rate)
max_num_epoch_not_improve=3*max_num_epoch_change_learning_rate
epoch_change_count=0
for minibatch_index in xrange(n_train_batches):
minibatch_avg_cost = train_model_one_iteration(minibatch_index)
# iteration number
iter = (epoch - 1) * n_train_batches + minibatch_index
if (iter + 1) % validation_frequency == 0:
# compute zero-one loss on validation set
validation_losses = validate_model()
this_validation_loss = numpy.mean(validation_losses)
print('epoch %i, minibatch %i/%i, validation error %f %%' % \
(epoch, minibatch_index + 1, n_train_batches,
this_validation_loss * 100.))
# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
num_epoch_not_improve=0
if this_validation_loss < best_validation_loss:
#improve patience if loss improvement is good enough
if this_validation_loss < best_validation_loss * \
improvement_threshold:
patience = max(patience, iter * patience_increase)
best_validation_loss = this_validation_loss
# save a copy of the currently best model parameter
best_model_params=classifier.get_params()
if patience <= iter:
done_looping = True
break
if this_validation_loss >= best_validation_loss:
num_epoch_not_improve=num_epoch_not_improve+1
if num_epoch_not_improve>=max_num_epoch_not_improve:
done_looping = True
break
# set the best model parameters
classifier.set_params(best_model_params)
end_time = time.clock()
training_time=end_time-start_time
print 'Training time: %f' %(training_time/60)
print 'Optimization complete with best validation score of %f,' %(best_validation_loss * 100.)
return classifier, training_time
2
Example 10
Project: DECRES Source File: logistic_sgd.py
def train_model(learning_rate=0.1, n_epochs=1000,
train_set_x_org=None,train_set_y_org=None,valid_set_x_org=None,valid_set_y_org=None,
batch_size=100):
"""
Train the logistic regression model.
INPUTS:
learning_rate: float scalar, the initial learning rate.
n_epochs: int scalar, the maximal number of epochs.
train_set_x_org: numpy 2d array, each row is a training sample.
train_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of training samples.
valid_set_x_org: numpy 2d array, each row is a validation sample.
This set is to monitor the convergence of optimization.
valid_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of validation samples.
batch_size: int scalar, minibatch size.
OUTPUTS:
classifier: object of logisticRegression, the model learned, returned for testing.
training_time: float, training time in seconds.
"""
train_set_x = theano.shared(numpy.asarray(train_set_x_org,dtype=theano.config.floatX),borrow=True)
train_set_y = T.cast(theano.shared(numpy.asarray(train_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
valid_set_x = theano.shared(numpy.asarray(valid_set_x_org,dtype=theano.config.floatX),borrow=True)
valid_set_y = T.cast(theano.shared(numpy.asarray(valid_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
# compute number of minibatches for training, validation and testing
#n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
#n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
n_train_batches = int(math.ceil(train_set_x.get_value(borrow=True).shape[0] / batch_size))
n_valid_batches = int(math.ceil(valid_set_x.get_value(borrow=True).shape[0] / batch_size))
# shared variable to reduce the learning rate
learning_rate_shared=theano.shared(learning_rate,name='learn_rate_shared')
#learning_rate_init=T.scalar(name='learning_rate_init',dtype=theano.config.floatX)
#epoch_variable=T.iscalar(name='epoch_variable')
decay_rate=T.scalar(name='decay_rate',dtype=theano.config.floatX)
#compute_learn_rate=theano.function([learning_rate_init,epoch_variable,decay_rate],learning_rate_shared, \
#updates=[(learning_rate_shared,learning_rate_init*decay_rate**(epoch_variable//100))]) # thenao does not support math.pow, instead use T.pow() or a**b
reduce_learning_rate=theano.function([decay_rate],learning_rate_shared,updates=[(learning_rate_shared,learning_rate_shared*decay_rate)])
# define the model
# allocate symbolic variables for the data
index = T.lscalar() # index to a [mini]batch
x = T.matrix('x') # each row is a sample
y = T.ivector('y') # the labels are presented as 1D vector of [int] labels
num_feat=train_set_x.get_value(borrow=True).shape[1]
#print train_set_y.get_value()
n_cl=len(numpy.unique(train_set_y_org))
classifier = LogisticRegression(input=x, n_in=num_feat, n_out=n_cl)
# the cost we minimize during training is the negative log likelihood of
# the model in symbolic format
cost = classifier.negative_log_likelihood(y)
# compiling a Theano function that computes the mistakes that are made by
# the model on a minibatch
validate_model = theano.function(inputs=[index],
outputs=classifier.errors(y),
givens={
x: valid_set_x[index * batch_size:(index + 1) * batch_size],
y: valid_set_y[index * batch_size:(index + 1) * batch_size]})
validate_model2 = theano.function(inputs=[],
outputs=classifier.errors(y),
givens={
x: valid_set_x,
y: valid_set_y})
validate_model3 = theano.function(inputs=[],
outputs=classifier.y_pred,
givens={x:valid_set_x})
# compute the gradient of cost with respect to theta = (W,b)
g_W = T.grad(cost=cost, wrt=classifier.W)
g_b = T.grad(cost=cost, wrt=classifier.b)
# specify how to update the parameters of the model as a list of
# (variable, update expression) pairs.
updates = [(classifier.W, classifier.W - learning_rate * g_W),
(classifier.b, classifier.b - learning_rate * g_b)]
# compiling a Theano function `train_model_one_iteration` that returns the cost, but in
# the same time updates the parameter of the model based on the rules
# defined in `updates`
train_model_one_iteration = theano.function(inputs=[index],
outputs=cost,
updates=updates,
givens={
x: train_set_x[index * batch_size:(index + 1) * batch_size],
y: train_set_y[index * batch_size:(index + 1) * batch_size]})
# training the model below
# early-stopping parameters
patience = 5000 # look as this many examples regardless
patience_increase = 2 # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = min(n_train_batches, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch
best_validation_loss = numpy.inf
max_num_epoch_change_learning_rate=100 # initial maximal number of epochs to change learning rate
max_num_epoch_not_improve=3*max_num_epoch_change_learning_rate # max number of epochs without improvmenet to terminate the optimization
max_num_epoch_change_rate=0.8 # change to max number of epochs to change learning rate
learning_rate_decay_rate=0.8
epoch_change_count=0
start_time = time.clock()
done_looping = False
epoch = 0
while (epoch < n_epochs) and (not done_looping):
epoch = epoch + 1
epoch_change_count=epoch_change_count+1
if epoch_change_count % max_num_epoch_change_learning_rate ==0:
reduce_learning_rate(learning_rate_decay_rate)
max_num_epoch_change_learning_rate= \
cl.change_max_num_epoch_change_learning_rate(max_num_epoch_change_learning_rate,max_num_epoch_change_rate)
max_num_epoch_not_improve=3*max_num_epoch_change_learning_rate
epoch_change_count=0
for minibatch_index in xrange(n_train_batches):
minibatch_avg_cost = train_model_one_iteration(minibatch_index)
# iteration number
iter = (epoch - 1) * n_train_batches + minibatch_index
if (iter + 1) % validation_frequency == 0:
# compute zero-one loss on validation set
validation_losses = [validate_model(i)
for i in xrange(n_valid_batches)]
this_validation_loss = numpy.mean(validation_losses)
print('epoch %i, minibatch %i/%i, validation error %f %%' % \
(epoch, minibatch_index + 1, n_train_batches,
this_validation_loss * 100.))
# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
num_epoch_not_improve=0
if this_validation_loss < best_validation_loss:
#improve patience if loss improvement is good enough
if this_validation_loss < best_validation_loss * \
improvement_threshold:
patience = max(patience, iter * patience_increase)
best_validation_loss = this_validation_loss
# save a copy of the currently best model parameter
best_model_params=classifier.get_params()
if patience <= iter:
done_looping = True
break
if this_validation_loss >= best_validation_loss:
num_epoch_not_improve=num_epoch_not_improve+1
if num_epoch_not_improve>=max_num_epoch_not_improve:
done_looping = True
break
# set the best model parameters
classifier.set_params(best_model_params)
end_time = time.clock()
training_time=end_time-start_time
print 'Training time: %f' %(training_time/60)
print 'Optimization complete with best validation score of %f,' %(best_validation_loss * 100.)
return classifier,training_time
2
Example 11
Project: tvb-library Source File: tools.py
def plot_fast_kde(x, y, kern_nx = None, kern_ny = None, gridsize=(500, 500),
extents=None, nocorrelation=False, weights=None, norm = True, pdf=False, **kwargs):
"""
A faster gaussian kernel density estimate (KDE). Intended for
computing the KDE on a regular grid (different use case than
scipy's original scipy.stats.kde.gaussian_kde()).
Author: Joe Kington
License: MIT License <http://www.opensource.org/licenses/mit-license.php>
Performs a gaussian kernel density estimate over a regular grid using a
convolution of the gaussian kernel with a 2D histogram of the data.
This function is typically several orders of magnitude faster than
scipy.stats.kde.gaussian_kde for large (>1e7) numbers of points and
produces an essentially identical result.
**Input**:
*x*: array
The x-coords of the input data points
*y*: array
The y-coords of the input data points
*kern_nx*: float
size (in units of *x*) of the kernel
*kern_ny*: float
size (in units of *y*) of the kernel
*gridsize*: (Nx , Ny) tuple (default: 500x500)
Size of the output grid
*extents*: (default: extent of input data) A (xmin, xmax, ymin, ymax)
tuple of the extents of output grid
*nocorrelation*: (default: False) If True, the correlation between the
x and y coords will be ignored when preforming the KDE.
*weights*: (default: None) An array of the same shape as x & y that
weighs each sample (x_i, y_i) by each value in weights (w_i).
Defaults to an array of ones the same size as x & y.
*norm*: boolean (default: False)
If False, the output is only corrected for the kernel. If True,
the result is normalized such that the integral over the area
yields 1.
**Output**:
A gridded 2D kernel density estimate of the input points.
"""
#---- Setup --------------------------------------------------------------
x, y = numpy.asarray(x), numpy.asarray(y)
x, y = numpy.squeeze(x), numpy.squeeze(y)
if x.size != y.size:
raise ValueError('Input x & y arrays must be the same size!')
nx, ny = gridsize
n = x.size
if weights is None:
# Default: Weight all points equally
weights = numpy.ones(n)
else:
weights = numpy.squeeze(numpy.asarray(weights))
if weights.size != x.size:
raise ValueError('Input weights must be an array of the same size'
' as input x & y arrays!')
# Default extents are the extent of the data
if extents is None:
xmin, xmax = x.min(), x.max()
ymin, ymax = y.min(), y.max()
else:
xmin, xmax, ymin, ymax = map(float, extents)
dx = (xmax - xmin) / (nx - 1)
dy = (ymax - ymin) / (ny - 1)
#---- Preliminary Calculations -------------------------------------------
# First convert x & y over to pixel coordinates
# (Avoiding np.digitize due to excessive memory usage!)
xyi = numpy.vstack((x,y)).T
xyi -= [xmin, ymin]
xyi /= [dx, dy]
xyi = numpy.floor(xyi, xyi).T
# Next, make a 2D histogram of x & y
# Avoiding np.histogram2d due to excessive memory usage with many points
grid = sp.sparse.coo_matrix((weights, xyi), shape=(nx, ny)).toarray()
# Calculate the covariance matrix (in pixel coords)
cov = numpy.cov(xyi)
if nocorrelation:
cov[1,0] = 0
cov[0,1] = 0
# Scaling factor for bandwidth
scotts_factor = numpy.power(n, -1.0 / 6) # For 2D
#---- Make the gaussian kernel -------------------------------------------
# First, determine how big the kernel needs to be
std_devs = numpy.diag(numpy.sqrt(cov))
if kern_nx is None or kern_ny is None:
kern_nx, kern_ny = numpy.round(scotts_factor * 2 * numpy.pi * std_devs)
else:
kern_nx = numpy.round(kern_nx / dx)
kern_ny = numpy.round(kern_ny / dy)
# Determine the bandwidth to use for the gaussian kernel
inv_cov = numpy.linalg.inv(cov * scotts_factor**2)
# x & y (pixel) coords of the kernel grid, with <x,y> = <0,0> in center
xx = numpy.arange(kern_nx, dtype=numpy.float) - kern_nx / 2.0
yy = numpy.arange(kern_ny, dtype=numpy.float) - kern_ny / 2.0
xx, yy = numpy.meshgrid(xx, yy)
# Then evaluate the gaussian function on the kernel grid
kernel = numpy.vstack((xx.flatten(), yy.flatten()))
kernel = numpy.dot(inv_cov, kernel) * kernel
kernel = numpy.sum(kernel, axis=0) / 2.0
kernel = numpy.exp(-kernel)
kernel = kernel.reshape((kern_ny, kern_nx))
#---- Produce the kernel density estimate --------------------------------
# Convolve the gaussian kernel with the 2D histogram, producing a gaussian
# kernel density estimate on a regular grid
grid = sp.signal.convolve2d(grid, kernel, mode='same', boundary='fill').T
# Normalization factor to divide result by so that units are in the same
# units as scipy.stats.kde.gaussian_kde's output.
norm_factor = 2 * numpy.pi * cov * scotts_factor**2
norm_factor = numpy.linalg.det(norm_factor)
#norm_factor = n * dx * dy * np.sqrt(norm_factor)
norm_factor = numpy.sqrt(norm_factor)
if norm :
norm_factor *= n * dx * dy
#---- Produce pdf --------------------------------
if pdf:
norm_factor, _ = sp.integrate.nquad(grid, [[xmin, xmax], [ymin, ymax]])
# Normalize the result
grid /= norm_factor
return grid
2
Example 12
Project: pylearn2 Source File: linesearch.py
def _zoom(a_lo, a_hi, phi_lo, phi_hi, derphi_lo,
phi, derphi, phi0, derphi0, c1, c2,
n_iters=10,
profile=False):
"""
WRITEME
Part of the optimization algorithm in `scalar_search_wolfe2`.
Parameters
----------
a_lo : float
Step size
a_hi : float
Step size
phi_lo : float
Value of f at a_lo
phi_hi : float
Value of f at a_hi
derphi_lo : float
Value of derivative at a_lo
phi : callable
Generates computational graph
derphi : callable
Generates computational graph
phi0 : float
Value of f at 0
derphi0 : float
Value of the derivative at 0
c1 : float
Wolfe parameter
c2 : float
Wolfe parameter
profile : bool
True if you want printouts of profiling information
"""
# Function reprensenting the computations of one step of the while loop
def while_zoom(phi_rec, a_rec, a_lo, a_hi, phi_hi,
phi_lo, derphi_lo, a_star, val_star, valprime):
# interpolate to find a trial step length between a_lo and
# a_hi Need to choose interpolation here. Use cubic
# interpolation and then if the result is within delta *
# dalpha or outside of the interval bounded by a_lo or a_hi
# then use quadratic interpolation, if the result is still too
# close, then use bisection
dalpha = a_hi - a_lo
a = TT.switch(dalpha < zero, a_hi, a_lo)
b = TT.switch(dalpha < zero, a_lo, a_hi)
# minimizer of cubic interpolant
# (uses phi_lo, derphi_lo, phi_hi, and the most recent value of phi)
#
# if the result is too close to the end points (or out of the
# interval) then use quadratic interpolation with phi_lo,
# derphi_lo and phi_hi if the result is stil too close to the
# end points (or out of the interval) then use bisection
# cubic interpolation
cchk = delta1 * dalpha
a_j_cubic = _cubicmin(a_lo, phi_lo, derphi_lo,
a_hi, phi_hi, a_rec, phi_rec)
# quadric interpolation
qchk = delta2 * dalpha
a_j_quad = _quadmin(a_lo, phi_lo, derphi_lo, a_hi, phi_hi)
cond_q = lazy_or('condq',
TT.isnan(a_j_quad),
a_j_quad > b - qchk,
a_j_quad < a + qchk)
a_j_quad = TT.switch(cond_q, a_lo +
numpy.asarray(0.5, dtype=theano.config.floatX) * \
dalpha, a_j_quad)
# pick between the two ..
cond_c = lazy_or('condc',
TT.isnan(a_j_cubic),
TT.bitwise_or(a_j_cubic > b - cchk,
a_j_cubic < a + cchk))
# this lazy if actually decides if we need to run the quadric
# interpolation
a_j = TT.switch(cond_c, a_j_quad, a_j_cubic)
#a_j = ifelse(cond_c, a_j_quad, a_j_cubic)
# Check new value of a_j
phi_aj = phi(a_j)
derphi_aj = derphi(a_j)
stop = lazy_and('stop',
TT.bitwise_and(phi_aj <= phi0 + c1 * a_j * derphi0,
phi_aj < phi_lo),
abs(derphi_aj) <= -c2 * derphi0)
cond1 = TT.bitwise_or(phi_aj > phi0 + c1 * a_j * derphi0,
phi_aj >= phi_lo)
cond2 = derphi_aj * (a_hi - a_lo) >= zero
# Switches just make more sense here because they have a C
# implementation and they get composed
phi_rec = ifelse(cond1,
phi_hi,
TT.switch(cond2, phi_hi, phi_lo),
name='phi_rec')
a_rec = ifelse(cond1,
a_hi,
TT.switch(cond2, a_hi, a_lo),
name='a_rec')
a_hi = ifelse(cond1, a_j,
TT.switch(cond2, a_lo, a_hi),
name='a_hi')
phi_hi = ifelse(cond1, phi_aj,
TT.switch(cond2, phi_lo, phi_hi),
name='phi_hi')
a_lo = TT.switch(cond1, a_lo, a_j)
phi_lo = TT.switch(cond1, phi_lo, phi_aj)
derphi_lo = ifelse(cond1, derphi_lo, derphi_aj, name='derphi_lo')
a_star = a_j
val_star = phi_aj
valprime = ifelse(cond1, nan,
TT.switch(cond2, derphi_aj, nan), name='valprime')
return ([phi_rec,
a_rec,
a_lo,
a_hi,
phi_hi,
phi_lo,
derphi_lo,
a_star,
val_star,
valprime],
theano.scan_module.scan_utils.until(stop))
maxiter = n_iters
# cubic interpolant check
delta1 = TT.constant(numpy.asarray(0.2,
dtype=theano.config.floatX))
# quadratic interpolant check
delta2 = TT.constant(numpy.asarray(0.1,
dtype=theano.config.floatX))
phi_rec = phi0
a_rec = zero
# Initial iteration
dalpha = a_hi - a_lo
a = TT.switch(dalpha < zero, a_hi, a_lo)
b = TT.switch(dalpha < zero, a_lo, a_hi)
#a = ifelse(dalpha < 0, a_hi, a_lo)
#b = ifelse(dalpha < 0, a_lo, a_hi)
# minimizer of cubic interpolant
# (uses phi_lo, derphi_lo, phi_hi, and the most recent value of phi)
#
# if the result is too close to the end points (or out of the
# interval) then use quadratic interpolation with phi_lo,
# derphi_lo and phi_hi if the result is stil too close to the
# end points (or out of the interval) then use bisection
# quadric interpolation
qchk = delta2 * dalpha
a_j = _quadmin(a_lo, phi_lo, derphi_lo, a_hi, phi_hi)
cond_q = lazy_or('mcond_q',
TT.isnan(a_j),
TT.bitwise_or(a_j > b - qchk,
a_j < a + qchk))
a_j = TT.switch(cond_q, a_lo +
numpy.asarray(0.5, dtype=theano.config.floatX) * \
dalpha, a_j)
# Check new value of a_j
phi_aj = phi(a_j)
derphi_aj = derphi(a_j)
cond1 = TT.bitwise_or(phi_aj > phi0 + c1 * a_j * derphi0,
phi_aj >= phi_lo)
cond2 = derphi_aj * (a_hi - a_lo) >= zero
# Switches just make more sense here because they have a C
# implementation and they get composed
phi_rec = ifelse(cond1,
phi_hi,
TT.switch(cond2, phi_hi, phi_lo),
name='mphirec')
a_rec = ifelse(cond1,
a_hi,
TT.switch(cond2, a_hi, a_lo),
name='marec')
a_hi = ifelse(cond1,
a_j,
TT.switch(cond2, a_lo, a_hi),
name='mahi')
phi_hi = ifelse(cond1,
phi_aj,
TT.switch(cond2, phi_lo, phi_hi),
name='mphihi')
onlyif = lazy_and('only_if',
TT.bitwise_and(phi_aj <= phi0 + c1 * a_j * derphi0,
phi_aj < phi_lo),
abs(derphi_aj) <= -c2 * derphi0)
a_lo = TT.switch(cond1, a_lo, a_j)
phi_lo = TT.switch(cond1, phi_lo, phi_aj)
derphi_lo = ifelse(cond1, derphi_lo, derphi_aj, name='derphi_lo_main')
phi_rec.name = 'phi_rec'
a_rec.name = 'a_rec'
a_lo.name = 'a_lo'
a_hi.name = 'a_hi'
phi_hi.name = 'phi_hi'
phi_lo.name = 'phi_lo'
derphi_lo.name = 'derphi_lo'
vderphi_aj = ifelse(cond1, nan, TT.switch(cond2, derphi_aj, nan),
name='vderphi_aj')
states = [phi_rec, a_rec, a_lo, a_hi, phi_hi, phi_lo, derphi_lo, zero, zero, zero]
# print'while_zoom'
outs, updates = scan(while_zoom,
outputs_info=states,
n_steps=maxiter,
name='while_zoom',
mode=theano.Mode(linker='cvm_nogc'),
profile=profile)
# print 'done_while'
a_star = ifelse(onlyif, a_j, outs[7][-1], name='astar')
val_star = ifelse(onlyif, phi_aj, outs[8][-1], name='valstar')
valprime = ifelse(onlyif, vderphi_aj, outs[9][-1], name='valprime')
## WARNING !! I ignore updates given by scan which I should not do !!!
return a_star, val_star, valprime
2
Example 13
Project: DECRES Source File: mlp.py
def train_model_old(train_set_x_org=None, train_set_y_org=None, valid_set_x_org=None, valid_set_y_org=None,
learning_rate=0.1, alpha=0.01, L1_reg=0.00, L2_reg=0.0001, n_hidden=[256,128,16],
n_epochs=1000, batch_size=100, rng=numpy.random.RandomState(100)):
"""
Train the model by training and validation sets.
"""
train_set_x = theano.shared(numpy.asarray(train_set_x_org,dtype=theano.config.floatX),borrow=True)
train_set_y = T.cast(theano.shared(numpy.asarray(train_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
valid_set_x = theano.shared(numpy.asarray(valid_set_x_org,dtype=theano.config.floatX),borrow=True)
valid_set_y = T.cast(theano.shared(numpy.asarray(valid_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
# compute number of minibatches for training, validation and testing
n_train_batches = int(math.ceil(train_set_x.get_value(borrow=True).shape[0] / batch_size))
n_valid_batches = int(math.ceil(valid_set_x.get_value(borrow=True).shape[0] / batch_size))
# shared variable to reduce the learning rate
learning_rate_shared=theano.shared(learning_rate,name='learn_rate_shared')
decay_rate=T.scalar(name='decay_rate',dtype=theano.config.floatX)
reduce_learning_rate=theano.function([decay_rate],learning_rate_shared,updates=[(learning_rate_shared,learning_rate_shared*decay_rate)])
# define the model below
# allocate symbolic variables for the data
index = T.lscalar() # index to a [mini]batch
x = T.matrix('x') # matrix, each row is a sample
y = T.ivector('y') # vector, intergers from {0,1,2,...,C-1}
num_feat=train_set_x.get_value(borrow=True).shape[1] # number of features
n_cl=len(numpy.unique(train_set_y_org)) # number of classes
# build a MPL object
classifier = MLP(rng=rng, x=x, y=y, n_in=num_feat, n_hidden=n_hidden, n_out=n_cl)
# the cost we minimize during training is the negative log likelihood of
# the model plus the regularization terms (L1 and L2); cost is expressed
# here symbolically
cost = classifier.negative_log_likelihood(y) \
+ L1_reg * classifier.L1 \
+ L2_reg * classifier.L2_sqr
# compiling a Theano function that computes the mistakes that are made
# by the model on a minibatch
validate_model = theano.function(inputs=[index],
outputs=classifier.errors(y),
givens={
x: valid_set_x[index * batch_size:(index + 1) * batch_size],
y: valid_set_y[index * batch_size:(index + 1) * batch_size]})
# compute the gradient of cost with respect to theta (sotred in params)
# the resulting gradients will be stored in a list gparams
grads = []
for param in classifier.params:
grad = T.grad(cost, param)
grads.append(grad)
delta_before=[]
for param_i in classifier.params:
delta_before_i=theano.shared(value=numpy.zeros(param_i.get_value().shape))
delta_before.append(delta_before_i)
updates = []
# to add momentum?
for param_i, grad_i, delta_before_i in zip(classifier.params, grads, delta_before):
delta_i=-learning_rate_shared * grad_i + alpha*delta_before_i
updates.append((param_i, param_i + delta_i ))
updates.append((delta_before_i,delta_i))
# specify how to update the parameters of the model as a list of
# (variable, update expression) pairs
# compiling a Theano function `train_model` that returns the cost, but
# in the same time updates the parameter of the model based on the rules
# defined in `updates`
train_model_one_iteration = theano.function(inputs=[index], outputs=cost,
updates=updates,
givens={
x: train_set_x[index * batch_size:(index + 1) * batch_size],
y: train_set_y[index * batch_size:(index + 1) * batch_size]})
print '... training'
# early-stopping parameters
patience = 5000 # look as this many examples regardless
patience_increase = 2 # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = min(n_train_batches, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch
best_validation_loss = numpy.inf
max_num_epoch_change_learning_rate=100
max_num_epoch_not_improve=100#3*max_num_epoch_change_learning_rate
max_num_epoch_change_rate=0.9
epoch_change_count=0
start_time = time.clock()
done_looping = False
epoch = 0
while (epoch < n_epochs) and (not done_looping):
epoch = epoch + 1
epoch_change_count=epoch_change_count+1
if epoch_change_count % max_num_epoch_change_learning_rate ==0:
reduce_learning_rate(0.9)
max_num_epoch_change_learning_rate= \
cl.change_max_num_epoch_change_learning_rate(max_num_epoch_change_learning_rate,max_num_epoch_change_rate)
max_num_epoch_not_improve=100#3*max_num_epoch_change_learning_rate
epoch_change_count=0
for minibatch_index in xrange(n_train_batches):
minibatch_avg_cost = train_model_one_iteration(minibatch_index)
# iteration number
iter = (epoch - 1) * n_train_batches + minibatch_index
if (iter + 1) % validation_frequency == 0:
# compute zero-one loss on validation set
validation_losses = [validate_model(i)
for i in xrange(n_valid_batches)]
this_validation_loss = numpy.mean(validation_losses)
print('epoch %i, minibatch %i/%i, validation error %f %%' % \
(epoch, minibatch_index + 1, n_train_batches,
this_validation_loss * 100.))
# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
num_epoch_not_improve=0
if this_validation_loss < best_validation_loss:
#improve patience if loss improvement is good enough
if this_validation_loss < best_validation_loss * \
improvement_threshold:
patience = max(patience, iter * patience_increase)
best_validation_loss = this_validation_loss
# save a copy of the currently best model parameter
best_model_params=classifier.get_params()
if patience <= iter:
done_looping = True
break
if this_validation_loss >= best_validation_loss:
num_epoch_not_improve=num_epoch_not_improve+1
if num_epoch_not_improve>=max_num_epoch_not_improve:
done_looping = True
break
# set the best model parameters
classifier.set_params(best_model_params)
end_time = time.clock()
training_time=end_time-start_time
print 'Training time: %f' %(training_time/60)
print 'Optimization complete with best validation score of %f,' %(best_validation_loss * 100.)
return classifier
2
Example 14
Project: deepcca Source File: SdA_mapping.py
def build_finetune_functions(self, datasets, batch_size, learning_rate):
'''Generates a function `train` that implements one step of
finetuning, a function `validate` that computes the error on
a batch from the validation set, and a function `test` that
computes the error on a batch from the testing set
:type datasets: list of pairs of theano.tensor.TensorType
:param datasets: It is a list that contain all the datasets;
the has to contain three pairs, `train`,
`valid`, `test` in this order, where each pair
is formed of two Theano variables, one for the
datapoints, the other for the labels
:type batch_size: int
:param batch_size: size of a minibatch
:type learning_rate: float
:param learning_rate: learning rate used during finetune stage
'''
(train_set_x, train_set_y) = datasets[0]
(valid_set_x, valid_set_y) = datasets[1]
(test_set_x, test_set_y) = datasets[2]
train_set_x=train_set_x.eval()
train_set_y=train_set_y.eval()
train_set_x_lab=train_set_x[:1000,:]
train_set_x_unlab=train_set_x[1000:,:]
train_set_y_lab=train_set_y[:1000,:]
train_set_y_unlab=train_set_y[1000:,:]
train_set_x_lab=theano.shared(numpy.asarray(train_set_x_lab,
dtype=theano.config.floatX),
borrow=True)
train_set_y_lab=theano.shared(numpy.asarray(train_set_y_lab,
dtype=theano.config.floatX),
borrow=True)
train_set_x_unlab=theano.shared(numpy.asarray(train_set_x_unlab,
dtype=theano.config.floatX),
borrow=True)
train_set_y_unlab=theano.shared(numpy.asarray(train_set_y_unlab,
dtype=theano.config.floatX),
borrow=True)
# compute number of minibatches for training, validation and testing
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
n_valid_batches /= batch_size
n_test_batches = test_set_x.get_value(borrow=True).shape[0]
n_test_batches /= batch_size
index = T.lscalar('index') # index to a [mini]batch
if 0: # for middle layer
fprop_inp = theano.function(
[],
self.SdA_inp.sigmoid_layers[-1].output,
givens={
self.SdA_inp.sigmoid_layers[0].input: test_set_x
},
name='fprop_inp'
)
fprop_out = theano.function(
[],
self.SdA_out.sigmoid_layers[-1].output,
givens={
self.SdA_out.sigmoid_layers[0].input: test_set_y
},
name='fprop_out'
)
H1=fprop_out()
H2=fprop_out()
H1=theano.shared(H1)
H2=theano.shared(H2)
# compute the gradients with respect to the model parameters
self.logreg_cost = self.log_reg.mse(self.h2)
gparams = T.grad(self.logreg_cost, self.log_reg.params)
# compute list of fine-tuning updates
updates = [
(param, param - gparam * learning_rate)
for param, gparam in zip(self.params, gparams)
]
train_fn_middle = theano.function(
inputs=[],
outputs=self.logreg_cost,
updates=updates,
givens={
self.h1: H1,
self.h2: H2
},
name='train_middle'
)
# compute the gradients with respect to the model parameters
gparams = T.grad(self.finetune_cost, self.params)
# compute list of fine-tuning updates
updates = [
(param, param - gparam * learning_rate)
for param, gparam in zip(self.params, gparams)
]
train_fn = theano.function(
inputs=[index],
outputs=self.finetune_cost,
updates=updates,
givens={
self.x: train_set_x_lab[
index * batch_size: (index + 1) * batch_size
],
self.y: train_set_y_lab[
index * batch_size: (index + 1) * batch_size
]
},
name='train'
)
test_score_i = theano.function(
[],
self.sigmoid_layers[-1].mse(self.y),
givens={
self.x: test_set_x,
self.y: test_set_y
},
name='test'
)
valid_score_i = theano.function(
[],
self.sigmoid_layers[-1].mse(self.y),
givens={
self.x: valid_set_x,
self.y: valid_set_y
},
name='valid'
)
# Create a function that scans the entire validation set
def valid_score():
return [valid_score_i(i) for i in xrange(n_valid_batches)]
# Create a function that scans the entire test set
def test_score():
return [test_score_i(i) for i in xrange(n_test_batches)]
return train_fn, valid_score_i, test_score_i
2
Example 15
Project: DECRES Source File: convolutional_mlp.py
def train_model(train_set_x_org=None,train_set_y_org=None,valid_set_x_org=None,valid_set_y_org=None,
n_row_each_sample=1,
learning_rate=0.1, alpha=0.1, n_epochs=1000, rng=numpy.random.RandomState(1000),
nkerns=[4,4,4],batch_size=200,
receptive_fields=((2,8),(2,8),(2,8)),poolsizes=((1,8),(1,8),(1,4)),full_hidden=[16],
max_num_epoch_change_learning_rate=80, max_num_epoch_change_rate=0.8, learning_rate_decay_rate=0.8):
"""
Train the model using training and validation data.
INPUTS:
train_set_x_org: numpy 2d array, each row is a training sample.
train_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of training samples.
valid_set_x_org: numpy 2d array, each row is a validation sample.
This set is to monitor the convergence of optimization.
valid_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of validation samples.
n_row_each_sample: int, for each vectorized sample, the number of rows when matricize it.
The vectorized sample is in the form of [row_0,row_1,...,row_{n_row_each_sample-1}].
learning_rate: float, the initial learning rate.
alpha: float, parameter to trade off the momentum term.
n_epochs: int, maximal number of epochs allowed.
rng: numpy random number state.
nkerns: list, tuple, or vector, nkerns[i] is the number of feature maps in the i-th convolutional layer
batch_size: int, minibatch size.
receptive_fields: list or tuple of the same length as nkerns,
receptive_fields[i] is a list or tuple of length 2, the size of receptive field in the i-th convolutional layer.
receptive_fields[i]= (#rows of the receptive field, #columns of the receptive field).
poolsizes: list or tuple of the same length as nkerns, the size to reduce to scalar.
poolsizes[i]=(#rows, #columns)
full_hidden: 1d vector, the numbers of hidden units fulling connecting the units in the previous layer.
OUTPUTS:
classifier: object of CNN class, the model trained.
training_time: training time.
"""
train_set_x = theano.shared(numpy.asarray(train_set_x_org,dtype=theano.config.floatX),borrow=True)
train_set_y = T.cast(theano.shared(numpy.asarray(train_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
valid_set_x = theano.shared(numpy.asarray(valid_set_x_org,dtype=theano.config.floatX),borrow=True)
valid_set_y = T.cast(theano.shared(numpy.asarray(valid_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
n_train=train_set_x.get_value(borrow=True).shape[0]
n_train_batches=n_train//batch_size
#n_train_batches = int(math.floor(train_set_x.get_value(borrow=True).shape[0] / batch_size))
#n_valid_batches = int(math.ceil(valid_set_x.get_value(borrow=True).shape[0] / batch_size))
# shared variable to reduce the learning rate
learning_rate_shared=theano.shared(learning_rate,name='learn_rate_shared')
# learning_rate_init=T.scalar(name='learning_rate_init',dtype=theano.config.floatX)
# epoch_variable=T.iscalar(name='epoch_variable')
decay_rate=T.scalar(name='decay_rate',dtype=theano.config.floatX)
# compute_learn_rate=theano.function([learning_rate_init,epoch_variable,decay_rate],learning_rate_shared, \
# updates=[(learning_rate_shared,learning_rate_init*decay_rate**(epoch_variable//100))]) # thenao does not support math.pow, instead use T.pow() or a**b
reduce_learning_rate=theano.function([decay_rate],learning_rate_shared,updates=[(learning_rate_shared,learning_rate_shared*decay_rate)])
######################
# BUILD ACTUAL MODEL #
######################
print '... building the model'
num_feat=train_set_x.get_value(borrow=True).shape[1]
input_size_row=n_row_each_sample # how many rows for each sample
input_size_col=num_feat//n_row_each_sample
input_size=(input_size_row,input_size_col)
n_out=len(numpy.unique(train_set_y_org)) # number of classes
classifier=cnn(rng=rng, batch_size=batch_size, input_size=input_size,
nkerns=nkerns, receptive_fields=receptive_fields, poolsizes=poolsizes,
full_hidden=full_hidden, n_out=n_out)
train_model_one_iteration=classifier.build_train_function(train_set_x, train_set_y, batch_size,
alpha, learning_rate_shared)
validate_model=classifier.build_valid_function(valid_set_x, valid_set_y, batch_size)
###############
# TRAIN MODEL #
###############
print '... training'
# early-stopping parameters
#max_num_epoch_change_learning_rate=50
max_num_epoch_not_improve=5*max_num_epoch_change_learning_rate
#max_num_epoch_change_rate=0.8,
#learning_rate_decay_rate=0.8
epoch_change_count=0
patience = 1000000 # look as this many examples regardless
patience_increase = 2 # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = n_train_batches; # min(n_train_batches, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch
best_validation_loss = numpy.inf
best_iter = 0
start_time = time.clock()
epoch = 0
done_looping = False
while (epoch < n_epochs) and (not done_looping): # for every epoch
epoch = epoch + 1
epoch_change_count=epoch_change_count+1
if epoch_change_count % max_num_epoch_change_learning_rate ==0:
reduce_learning_rate(learning_rate_decay_rate)
max_num_epoch_change_learning_rate= \
cl.change_max_num_epoch_change_learning_rate(max_num_epoch_change_learning_rate,max_num_epoch_change_rate)
max_num_epoch_not_improve=3*max_num_epoch_change_learning_rate
epoch_change_count=0
#compute_learn_rate(learning_rate,epoch,0.5)
print 'The current learning rate is ', learning_rate_shared.get_value()
for minibatch_index in xrange(n_train_batches): # for every minibatch
iter = (epoch - 1) * n_train_batches + minibatch_index # number of total minibatchs so far
#if iter % 100 == 0:
#print 'training @ iter = ', iter
# if minibatch_index==n_train_batches-1:
# batch_size_current=n_train - minibatch_index*batch_size
# else:
# batch_size_current=batch_size
# cost_ij = train_model_one_iteration(minibatch_index,batch_size_current)
cost_ij = train_model_one_iteration(minibatch_index)
if (iter + 1) % validation_frequency == 0:
# compute zero-one loss on validation set
validation_losses = validate_model()
this_validation_loss = numpy.mean(validation_losses)
print('epoch %i, minibatch %i/%i, validation error %0.4f %%' % \
(epoch, minibatch_index + 1, n_train_batches, \
this_validation_loss * 100.))
# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
num_epoch_not_improve=0
#improve patience if loss improvement is good enough
if this_validation_loss < best_validation_loss * \
improvement_threshold:
patience = max(patience, iter * patience_increase)
# save best validation score and iteration number
best_validation_loss = this_validation_loss
best_iter = iter
# save a copy of the currently best model parameter
best_model_params=classifier.get_params()
if patience <= iter:
done_looping = True
break
if this_validation_loss >= best_validation_loss:
num_epoch_not_improve=num_epoch_not_improve+1
if num_epoch_not_improve>=max_num_epoch_not_improve:
done_looping = True
break
# set the best model parameters
classifier.set_params(best_model_params)
end_time = time.clock()
training_time=end_time -start_time
print 'Training time: %f' %(training_time/60)
print 'Optimization complete with best validation score of %f,' %(best_validation_loss * 100.)
return classifier, training_time
2
Example 16
Project: sherpa Source File: optfcts.py
def montecarlo(fcn, x0, xmin, xmax, ftol=EPSILON, maxfev=None, verbose=0,
seed=74815, population_size=None, xprob=0.9,
weighting_factor=0.8):
def stat_cb0( pars ):
return fcn( pars )[ 0 ]
x, xmin, xmax = _check_args(x0, xmin, xmax)
# make sure that the cross over prob is within [0.1,1.0]
xprob = max( 0.1, xprob )
xprob = min( xprob, 1.0 )
# make sure that weighting_factor is within [0.1,1.0]
weighting_factor = max( 0.1, weighting_factor )
weighting_factor = min( weighting_factor, 1.0 )
random.seed( seed )
if seed is None:
seed = random.randint(0, 2147483648) # pow(2,31) == 2147483648L
if population_size is None:
population_size = 12 * x.size
if maxfev is None:
maxfev = 8192 * population_size
def myopt( myfcn, xxx, ftol, maxfev, seed, pop, xprob,
weight, factor=4.0, debug=False ):
x = xxx[ 0 ]
xmin = xxx[ 1 ]
xmax = xxx[ 2 ]
maxfev_per_iter = 512 * x.size
def random_start( xmin, xmax ):
xx = []
for ii in range( len(xmin ) ):
xx.append( random.uniform( xmin[ ii ], xmax[ ii ] ) )
return numpy.asarray( xx )
############################# NelderMead #############################
mymaxfev = min( maxfev_per_iter, maxfev )
if all( x == 0.0 ):
mystep = list(map(lambda fubar: 1.2 + fubar, x))
else:
mystep = list(map(lambda fubar: 1.2 * fubar, x))
result = neldermead( myfcn, x, xmin, xmax, maxfev=mymaxfev, ftol=ftol,
finalsimplex=9, step=mystep )
x = numpy.asarray( result[ 1 ], numpy.float_ )
nfval = result[2]
nfev = result[4].get( 'nfev' )
if verbose or False != debug:
print('f_nm%s=%.14e in %d nfev' % ( x, nfval, nfev ))
############################# NelderMead #############################
############################## nmDifEvo ##############################
xmin, xmax = _narrow_limits( 4 * factor, [x,xmin,xmax], debug=False )
mymaxfev = min( maxfev_per_iter, maxfev - nfev )
result = difevo_nm( myfcn, x, xmin, xmax, ftol=ftol, maxfev=mymaxfev,
seed=seed, population_size=pop, xprob=xprob,
weighting_factor=weight )
nfev += result[4].get( 'nfev' )
x = numpy.asarray( result[1], numpy.float_ )
nfval = result[2]
if verbose or False != debug:
print('f_de_nm%s=%.14e in %d nfev' % ( x, result[2],
result[4].get('nfev')))
############################## nmDifEvo ##############################
ofval = FUNC_MAX
while nfev < maxfev:
xmin, xmax = _narrow_limits( factor, [x,xmin,xmax], debug=False )
############################ nmDifEvo #############################
y = random_start( xmin, xmax )
mymaxfev = min( maxfev_per_iter, maxfev - nfev )
result = difevo_nm( myfcn, y, xmin, xmax, ftol=ftol,
maxfev=mymaxfev, seed=seed,
population_size=pop, xprob=xprob,
weighting_factor=weight )
nfev += result[4].get( 'nfev' )
if result[2] < nfval:
nfval = result[2]
x = numpy.asarray( result[1], numpy.float_ )
if verbose or False != debug:
print('f_de_nm%s=%.14e in %d nfev' % \
( x, result[2], result[4].get('nfev')))
############################ nmDifEvo #############################
if False != debug:
print('ofval=%.14e\tnfval=%.14e\n' % (ofval, nfval))
if sao_fcmp( ofval, nfval, ftol ) <= 0:
return x, nfval, nfev
ofval = nfval
factor *= 2
return x, nfval, nfev
x, fval, nfev = myopt( fcn, [x, xmin, xmax], numpy.sqrt(ftol), maxfev,
seed, population_size, xprob, weighting_factor,
factor=2.0, debug=False )
covarerr = None
if nfev < maxfev:
if all( x == 0.0 ):
mystep = list(map(lambda fubar: 1.2 + fubar, x))
else:
mystep = list(map(lambda fubar: 1.2 * fubar, x))
result = neldermead(fcn, x, xmin, xmax,
maxfev=min(512*len(x), maxfev - nfev),
ftol=ftol, finalsimplex=9, step=mystep)
covarerr = result[4].get( 'covarerr' )
x = numpy.asarray( result[ 1 ], numpy.float_ )
fval = result[2]
nfev += result[4].get( 'nfev' )
ierr = 0
if nfev >= maxfev:
ierr = 3
status, msg = _get_saofit_msg( maxfev, ierr )
rv = (status, x, fval)
print_covar_err = False
if print_covar_err and None != covarerr:
rv += (msg, {'covarerr': covarerr, 'info': status, 'nfev': nfev})
else:
rv += (msg, {'info': status, 'nfev': nfev})
return rv
2
Example 17
Project: sherpa Source File: pyfits_backend.py
def get_rmf_data(arg, make_copy=False):
"""
arg is a filename or a HDUList object.
"""
rmf, filename = _get_file_contents(arg, exptype="BinTableHDU",
nobinary=True)
try:
if _has_hdu(rmf, 'MATRIX'):
hdu = rmf['MATRIX']
elif _has_hdu(rmf, 'SPECRESP MATRIX'):
hdu = rmf['SPECRESP MATRIX']
elif _has_hdu(rmf, 'AXAF_RMF'):
hdu = rmf['AXAF_RMF']
elif _is_ogip_type(rmf, 'RESPONSE', bltype2='RSP_MATRIX'):
hdu = rmf[1]
else:
raise IOErr('notrsp', filename, 'an RMF')
data = {}
data['detchans'] = SherpaUInt(_require_key(hdu, 'DETCHANS'))
data['energ_lo'] = _require_col(hdu, 'ENERG_LO', fix_type=True)
data['energ_hi'] = _require_col(hdu, 'ENERG_HI', fix_type=True)
data['n_grp'] = _require_col(hdu, 'N_GRP', fix_type=True,
dtype=SherpaUInt)
data['f_chan'] = _require_vec(hdu, 'F_CHAN', fix_type=True,
dtype=SherpaUInt)
data['n_chan'] = _require_vec(hdu, 'N_CHAN', fix_type=True,
dtype=SherpaUInt)
# Read MATRIX as-is -- we will flatten it below, because
# we need to remove all rows corresponding to n_grp[row] == 0
data['matrix'] = None
if 'MATRIX' not in hdu.columns.names:
pass
else:
data['matrix'] = hdu.data.field('MATRIX')
data['header'] = _get_meta_data(hdu)
data['header'].pop('DETCHANS')
# Beginning of non-Chandra RMF support
fchan_col = list(hdu.columns.names).index('F_CHAN') + 1
tlmin = _try_key(hdu, 'TLMIN' + str(fchan_col), True, SherpaUInt)
if tlmin is not None:
data['offset'] = tlmin
else:
# QUS: should this actually be an error, rather than just
# something that is logged to screen?
error("Failed to locate TLMIN keyword for F_CHAN" +
" column in RMF file '%s'; " % filename +
'Update the offset value in the RMF data set to' +
' appropriate TLMIN value prior to fitting')
if _has_hdu(rmf, 'EBOUNDS'):
hdu = rmf['EBOUNDS']
data['e_min'] = _try_col(hdu, 'E_MIN', fix_type=True)
data['e_max'] = _try_col(hdu, 'E_MAX', fix_type=True)
# Beginning of non-Chandra RMF support
chan_col = list(hdu.columns.names).index('CHANNEL') + 1
tlmin = _try_key(hdu, 'TLMIN' + str(chan_col), True, SherpaUInt)
if tlmin is not None:
data['offset'] = tlmin
else:
data['e_min'] = None
data['e_max'] = None
finally:
rmf.close()
# ## For every row i of the response matrix, such that
# ## n_grp[i] == 0, we need to remove that row from the
# ## n_chan, f_chan, and matrix arrays we are constructing
# ## to be passed up to the DataRMF data structure.
# ## This is trivial for n_chan and f_chan. For the matrix
# ## array this can be more work -- can't just remove all
# ## zeroes, because some rows where n_grp[row] > 0 might
# ## still have zeroes in the matrix. I add new code first
# ## to deal with the matrix, then simpler code to remove zeroes
# ## from n_chan and f_chan.
# Read in MATRIX column with structure as-is -- i.e., as an array of
# arrays. Then flatten it, but include only those arrays that come from
# rows where n_grp[row] > 0. Zero elements can only be included from
# rows where n_grp[row] > 0. SMD 05/23/13
if not isinstance(data['matrix'], _VLF):
data['matrix'] = None
raise IOErr('badfile', filename,
" MATRIX column not a variable length field")
good = (data['n_grp'] > 0)
data['matrix'] = data['matrix'][good]
data['matrix'] = numpy.concatenate([numpy.asarray(row) for
row in data['matrix']])
data['matrix'] = data['matrix'].astype(SherpaFloat)
# Flatten f_chan and n_chan vectors into 1D arrays as crates does
# according to group
if data['f_chan'].ndim > 1 and data['n_chan'].ndim > 1:
f_chan = []
n_chan = []
for grp, fch, nch, in izip(data['n_grp'], data['f_chan'],
data['n_chan']):
for i in xrange(grp):
f_chan.append(fch[i])
n_chan.append(nch[i])
data['f_chan'] = numpy.asarray(f_chan, SherpaUInt)
data['n_chan'] = numpy.asarray(n_chan, SherpaUInt)
else:
if len(data['n_grp']) == len(data['f_chan']):
# filter out groups with zeroes.
good = (data['n_grp'] > 0)
data['f_chan'] = data['f_chan'][good]
data['n_chan'] = data['n_chan'][good]
return data, filename
2
Example 18
Project: pyscf Source File: df.py
def density_fit(casscf, auxbasis=None, with_df=None):
'''Generate DF-CASSCF for given CASSCF object. It is done by overwriting
three CASSCF member functions:
* casscf.ao2mo which generates MO integrals
* casscf.get_veff which generate JK from core density matrix
* casscf.get_jk which
Args:
casscf : an CASSCF object
Kwargs:
auxbasis : str or basis dict
Same format to the input attribute mol.basis.
The default basis 'weigend+etb' means weigend-coulomb-fit basis
for light elements and even-tempered basis for heavy elements.
Returns:
An CASSCF object with a modified J, K matrix constructor which uses density
fitting integrals to compute J and K
Examples:
>>> mol = gto.M(atom='H 0 0 0; F 0 0 1', basis='ccpvdz', verbose=0)
>>> mf = scf.RHF(mol)
>>> mf.scf()
>>> mc = DFCASSCF(mf, 4, 4)
-100.05994191570504
'''
casscf_class = casscf.__class__
if with_df is None:
if (hasattr(casscf._scf, 'with_df') and
(auxbasis is None or auxbasis == casscf._scf.with_df.auxbasis)):
with_df = casscf._scf.with_df
else:
with_df = df.DF(casscf.mol)
with_df.max_memory = casscf.max_memory
with_df.stdout = casscf.stdout
with_df.verbose = casscf.verbose
if auxbasis is not None:
with_df.auxbasis = auxbasis
class CASSCF(casscf_class):
def __init__(self):
self.__dict__.update(casscf.__dict__)
#self.grad_update_dep = 0
self.with_df = with_df
self._keys = self._keys.union(['with_df'])
def dump_flags(self):
casscf_class.dump_flags(self)
logger.info(self, 'DFCASCI/DFCASSCF: density fitting for JK matrix and 2e integral transformation')
def ao2mo(self, mo_coeff):
if self.with_df:
return _ERIS(self, mo_coeff, self.with_df)
else:
return casscf_class.ao2mo(self, mo_coeff)
def get_h2eff(self, mo_coeff=None): # For CASCI
if self.with_df:
mo = numpy.asarray(mo_coeff, order='F')
nao, nmo = mo.shape
naoaux = self.with_df.get_naoaux()
buf = numpy.empty((naoaux,nmo*(nmo+1)//2))
fmmm = _ao2mo.libao2mo.AO2MOmmm_nr_s2_s2
fdrv = _ao2mo.libao2mo.AO2MOnr_e2_drv
ftrans = _ao2mo.libao2mo.AO2MOtranse2_nr_s2
b0 = 0
for eri1 in self.with_df.loop():
naux = eri1.shape[0]
fdrv(ftrans, fmmm,
buf[b0:b0+naux].ctypes.data_as(ctypes.c_void_p),
eri1.ctypes.data_as(ctypes.c_void_p),
mo.ctypes.data_as(ctypes.c_void_p),
ctypes.c_int(naux), ctypes.c_int(nao),
ctypes.c_int(0), ctypes.c_int(nmo),
ctypes.c_int(0), ctypes.c_int(nmo),
ctypes.c_void_p(0), ctypes.c_int(0))
b0 += naux
eri = pyscf.lib.dot(buf.T, buf)
return eri
else:
return casscf_class.get_h2eff(self, mo_coeff)
# Modify get_veff for JK matrix of core density because get_h1eff calls
# self.get_veff to generate core JK
def get_veff(self, mol=None, dm=None, hermi=1):
if self.with_df:
if dm is None:
mocore = self.mo_coeff[:,:self.ncore]
dm = numpy.dot(mocore, mocore.T) * 2
vj, vk = self.with_df.get_jk(dm, hermi=hermi)
return vj-vk*.5
else:
return casscf_class.get_jk(self, mol, dm, hermi)
# We don't modify self._scf because it changes self.h1eff function.
# We only need approximate jk for self.update_jk_in_ah
def get_jk(self, mol, dm, hermi=1):
if self.with_df:
return self.with_df.get_jk(dm, hermi=hermi)
else:
return casscf_class.get_jk(self, mol, dm, hermi)
def _exact_paaa(self, mo, u, out=None):
if self.with_df:
nmo = mo.shape[1]
ncore = self.ncore
ncas = self.ncas
nocc = ncore + ncas
mo1 = numpy.dot(mo, u)
mo1_cas = mo1[:,ncore:nocc]
paaa = numpy.zeros((nmo*ncas,ncas*ncas))
moij = numpy.asarray(numpy.hstack((mo1, mo1_cas)), order='F')
ijshape = (0, nmo, nmo, nmo+ncas)
for eri1 in self.with_df.loop():
bufpa = _ao2mo.nr_e2(eri1, moij, ijshape, 's2', 's1')
bufaa = numpy.asarray(buf1[ncore:nocc,:], order='C')
pyscf.lib.dot(bufpa.T, bufaa, 1, paaa, 1)
return paaa.reshape(nmo,ncas,ncas,ncas)
else:
return casscf_class._exact_paaa(self, mol, u, out)
return CASSCF()
2
Example 19
Project: DECRES Source File: deep_feat_select_mlp_l21norm.py
def train_model(train_set_x_org=None, train_set_y_org=None, valid_set_x_org=None, valid_set_y_org=None,
learning_rate=0.1, alpha=0.01,
lambda21=0.001, alpha1=0.001, alpha2=0.0,
n_hidden=[256,128,16], n_epochs=1000, batch_size=100,
activation_func="tanh", rng=numpy.random.RandomState(100),
max_num_epoch_change_learning_rate=100,max_num_epoch_change_rate=0.8,learning_rate_decay_rate=0.8):
"""
Train a deep feature selection model.
INPUTS:
train_set_x_org: numpy 2d array, each row is a training sample.
train_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of training samples.
valid_set_x_org: numpy 2d array, each row is a validation sample.
This set is to monitor the convergence of optimization.
valid_set_y_org: numpy vector of type int {0,1,...,C-1}, class labels of validation samples.
learning_rate: float scalar, the initial learning rate.
alpha: float, parameter to trade off the momentum term.
lambda21: float scalar, control the sparsity of the input weights.
alpha1: float scalar, control the sparsity of the weight matrices in MLP.
The regularization term is alpha1( (1-alpha2)/2 * \sum||W_i||_2^2 + alpha2 \sum||W_i||_1 ), where the first hidden layer is not considered. Thus, the larger alpha1 is, the sparser the MLP weights are.
alpha2: float scalar, control the smoothness of the weight matrices in MLP.
The regularization term is alpha1( (1-alpha2)/2 * \sum||W_i||_2^2 + alpha2 \sum||W_i||_1 ), where the first hidden layer is not considered. Thus, the larger alpha2 is, the smoother the MLP weights are.
n_hidden, vector of int, n_hidden[i]: number of hidden units of the i-th layer.
n_epochs: int scalar, the maximal number of epochs.
batch_size: int scalar, minibatch size.
activation_func: string, specify activation function. {"tanh" (default),"sigmoid"}
rng: numpy random number state.
OUTPUTS:
classifier: object of MLP, the model learned, returned for testing.
training_time: float, training time in seconds.
"""
train_set_x = theano.shared(numpy.asarray(train_set_x_org,dtype=theano.config.floatX),borrow=True)
train_set_y = T.cast(theano.shared(numpy.asarray(train_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
valid_set_x = theano.shared(numpy.asarray(valid_set_x_org,dtype=theano.config.floatX),borrow=True)
valid_set_y = T.cast(theano.shared(numpy.asarray(valid_set_y_org,dtype=theano.config.floatX),borrow=True),'int32')
# compute number of minibatches for training, validation and testing
n_train_batches = int(math.ceil(train_set_x.get_value(borrow=True).shape[0] / batch_size))
# shared variable to reduce the learning rate
learning_rate_shared=theano.shared(learning_rate,name='learn_rate_shared')
decay_rate=T.scalar(name='decay_rate',dtype=theano.config.floatX)
min_learning_rate=T.scalar(name='min_learning_rate',dtype=theano.config.floatX)
reduce_learning_rate=theano.function([decay_rate],learning_rate_shared,updates=[(learning_rate_shared, learning_rate_shared*decay_rate)])
#reduce_learning_rate=theano.function([decay_rate,min_learning_rate],learning_rate_shared,updates=[(learning_rate_shared, learning_rate_shared*decay_rate if T.le(learning_rate_shared,min_learning_rate) else min_learning_rate)])
## define the model below
num_feat=train_set_x.get_value(borrow=True).shape[1] # number of features
n_cl=len(numpy.unique(train_set_y_org)) # number of classes
activations={"tanh":T.tanh,"sigmoid":T.nnet.sigmoid,"relu":relu}
activation=activations[activation_func]
# build a MPL object
classifier = DFS(rng=rng, n_in=num_feat, n_hidden=n_hidden, n_out=n_cl,
lambda21=lambda21, alpha1=alpha1, alpha2=alpha2,
activation=activation)
train_model_one_iteration=classifier.build_train_function(train_set_x, train_set_y, batch_size,
alpha, learning_rate_shared)
validate_model=classifier.build_valid_function(valid_set_x, valid_set_y, batch_size)
print '... training'
# early-stopping parameters
patience = 5000 # look as this many examples regardless
patience_increase = 2 # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = min(n_train_batches, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch
best_validation_loss = numpy.inf
#max_num_epoch_change_learning_rate=100
max_num_epoch_not_improve=5*max_num_epoch_change_learning_rate
#max_num_epoch_change_rate=0.8
#learning_rate_decay_rate=0.8
epoch_change_count=0
start_time = time.clock()
done_looping = False
epoch = 0
while (epoch < n_epochs) and (not done_looping):
epoch = epoch + 1
epoch_change_count=epoch_change_count+1
if epoch_change_count % max_num_epoch_change_learning_rate ==0:
reduce_learning_rate(learning_rate_decay_rate)
max_num_epoch_change_learning_rate= \
cl.change_max_num_epoch_change_learning_rate(max_num_epoch_change_learning_rate,max_num_epoch_change_rate)
max_num_epoch_not_improve=5*max_num_epoch_change_learning_rate
epoch_change_count=0
for minibatch_index in xrange(n_train_batches):
minibatch_avg_cost = train_model_one_iteration(minibatch_index)
# iteration number
iter = (epoch - 1) * n_train_batches + minibatch_index
if (iter + 1) % validation_frequency == 0:
# compute zero-one loss on validation set
validation_losses = validate_model()
this_validation_loss = numpy.mean(validation_losses)
print('epoch %i, minibatch %i/%i, validation error %f %%' % \
(epoch, minibatch_index + 1, n_train_batches,
this_validation_loss * 100.))
# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
num_epoch_not_improve=0
if this_validation_loss < best_validation_loss:
#improve patience if loss improvement is good enough
if this_validation_loss < best_validation_loss * \
improvement_threshold:
patience = max(patience, iter * patience_increase)
best_validation_loss = this_validation_loss
# save a copy of the currently best model parameter
best_model_params=classifier.get_params()
if patience <= iter:
done_looping = True
break
if this_validation_loss >= best_validation_loss:
num_epoch_not_improve=num_epoch_not_improve+1
if num_epoch_not_improve>=max_num_epoch_not_improve:
done_looping = True
break
# set the best model parameters
classifier.set_params(best_model_params)
end_time = time.clock()
training_time=end_time-start_time
print 'Training time: %f' %(training_time/60)
print 'Optimization complete with best validation score of %f,' %(best_validation_loss * 100.)
return classifier, training_time
2
Example 20
Project: medpy Source File: image.py
def sls(minuend, subtrahend, metric = "ssd", noise = "global", signed = True,
sn_size = None, sn_footprint = None, sn_mode = "reflect", sn_cval = 0.0,
pn_size = None, pn_footprint = None, pn_mode = "reflect", pn_cval = 0.0):
r"""
Computes the signed local similarity between two images.
Compares a patch around each voxel of the minuend array to a number of patches
centered at the points of a search neighbourhood in the subtrahend. Thus, creates
a multi-dimensional measure of patch similarity between the minuend and a
corresponding search area in the subtrahend.
This filter can also be used to compute local self-similarity, obtaining a
descriptor similar to the one described in [1]_.
Parameters
----------
minuend : array_like
Input array from which to subtract the subtrahend.
subtrahend : array_like
Input array to subtract from the minuend.
metric : {'ssd', 'mi', 'nmi', 'ncc'}, optional
The `metric` parameter determines the metric used to compute the
filter output. Default is 'ssd'.
noise : {'global', 'local'}, optional
The `noise` parameter determines how the noise is handled. If set
to 'global', the variance determining the noise is a scalar, if
set to 'local', it is a Gaussian smoothed field of estimated local
noise. Default is 'global'.
signed : bool, optional
Whether the filter output should be signed or not. If set to 'False',
only the absolute values will be returned. Default is 'True'.
sn_size : scalar or tuple, optional
See sn_footprint, below
sn_footprint : array, optional
The search neighbourhood.
Either `sn_size` or `sn_footprint` must be defined. `sn_size` gives
the shape that is taken from the input array, at every element
position, to define the input to the filter function.
`sn_footprint` is a boolean array that specifies (implicitly) a
shape, but also which of the elements within this shape will get
passed to the filter function. Thus ``sn_size=(n,m)`` is equivalent
to ``sn_footprint=np.ones((n,m))``. We adjust `sn_size` to the number
of dimensions of the input array, so that, if the input array is
shape (10,10,10), and `sn_size` is 2, then the actual size used is
(2,2,2).
sn_mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional
The `sn_mode` parameter determines how the array borders are
handled, where `sn_cval` is the value when mode is equal to
'constant'. Default is 'reflect'
sn_cval : scalar, optional
Value to fill past edges of input if `sn_mode` is 'constant'. Default
is 0.0
pn_size : scalar or tuple, optional
See pn_footprint, below
pn_footprint : array, optional
The patch over which the distance measure is applied.
Either `pn_size` or `pn_footprint` must be defined. `pn_size` gives
the shape that is taken from the input array, at every element
position, to define the input to the filter function.
`pn_footprint` is a boolean array that specifies (implicitly) a
shape, but also which of the elements within this shape will get
passed to the filter function. Thus ``pn_size=(n,m)`` is equivalent
of dimensions of the input array, so that, if the input array is
shape (10,10,10), and `pn_size` is 2, then the actual size used is
(2,2,2).
pn_mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional
The `pn_mode` parameter determines how the array borders are
handled, where `pn_cval` is the value when mode is equal to
'constant'. Default is 'reflect'
pn_cval : scalar, optional
Value to fill past edges of input if `pn_mode` is 'constant'. Default
is 0.0
Returns
-------
sls : ndarray
The signed local similarity image between subtrahend and minuend.
References
----------
.. [1] Mattias P. Heinrich, Mark Jenkinson, Manav Bhushan, Tahreema Matin, Fergus V. Gleeson, Sir Michael Brady, Julia A. Schnabel
MIND: Modality independent neighbourhood descriptor for multi-modal deformable registration
Medical Image Analysis, Volume 16, Issue 7, October 2012, Pages 1423-1435, ISSN 1361-8415
http://dx.doi.org/10.1016/j.media.2012.05.008
"""
minuend = numpy.asarray(minuend)
subtrahend = numpy.asarray(subtrahend)
if numpy.iscomplexobj(minuend):
raise TypeError('complex type not supported')
if numpy.iscomplexobj(subtrahend):
raise TypeError('complex type not supported')
mshape = [ii for ii in minuend.shape if ii > 0]
sshape = [ii for ii in subtrahend.shape if ii > 0]
if not len(mshape) == len(sshape):
raise RuntimeError("minuend and subtrahend must be of same shape")
if not numpy.all([sm == ss for sm, ss in zip(mshape, sshape)]):
raise RuntimeError("minuend and subtrahend must be of same shape")
sn_footprint = __make_footprint(minuend, sn_size, sn_footprint)
sn_fshape = [ii for ii in sn_footprint.shape if ii > 0]
if len(sn_fshape) != minuend.ndim:
raise RuntimeError('search neighbourhood footprint array has incorrect shape.')
#!TODO: Is this required?
if not sn_footprint.flags.contiguous:
sn_footprint = sn_footprint.copy()
# created a padded copy of the subtrahend, whereas the padding mode is always 'reflect'
subtrahend = pad(subtrahend, footprint=sn_footprint, mode=sn_mode, cval=sn_cval)
# compute slicers for position where the search neighbourhood sn_footprint is TRUE
slicers = [[slice(x, (x + 1) - d if 0 != (x + 1) - d else None) for x in range(d)] for d in sn_fshape]
slicers = [sl for sl, tv in zip(itertools.product(*slicers), sn_footprint.flat) if tv]
# compute difference images and sign images for search neighbourhood elements
ssds = [ssd(minuend, subtrahend[slicer], normalized=True, signed=signed, size=pn_size, footprint=pn_footprint, mode=pn_mode, cval=pn_cval) for slicer in slicers]
distance = [x[0] for x in ssds]
distance_sign = [x[1] for x in ssds]
# compute local variance, which constitutes an approximation of local noise, out of patch-distances over the neighbourhood structure
variance = numpy.average(distance, 0)
variance = gaussian_filter(variance, sigma=3) #!TODO: Figure out if a fixed sigma is desirable here... I think that yes
if 'global' == noise:
variance = variance.sum() / float(numpy.product(variance.shape))
# variance[variance < variance_global / 10.] = variance_global / 10. #!TODO: Should I keep this i.e. regularizing the variance to be at least 10% of the global one?
# compute sls
sls = [dist_sign * numpy.exp(-1 * (dist / variance)) for dist_sign, dist in zip(distance_sign, distance)]
# convert into sls image, swapping dimensions to have varying patches in the last dimension
return numpy.rollaxis(numpy.asarray(sls), 0, minuend.ndim + 1)
2
Example 21
Project: deepcca Source File: SdA_mapping.py
def test_SdA_regress(finetune_lr=0.05, pretraining_epochs=10,
pretrain_lr=0.1, training_epochs=10000,
dataset='mnist.pkl.gz', batch_size=20):
datasets = load_data_half(dataset)
train_set_x, train_set_y = datasets[0]##
valid_set_x, valid_set_y = datasets[1]##
test_set_x, test_set_y = datasets[2]##
train_set_x=train_set_x.eval()
train_set_y=train_set_y.eval()
import theano
train_set_x_lab=train_set_x[:,:]
train_set_x_unlab=train_set_x[:,:]
train_set_y_lab=train_set_y[:,:]
train_set_y_unlab=train_set_y[:,:]
train_set_x_lab=theano.shared(numpy.asarray(train_set_x_lab,
dtype=theano.config.floatX),
borrow=True)
train_set_y_lab=theano.shared(numpy.asarray(train_set_y_lab,
dtype=theano.config.floatX),
borrow=True)
train_set_x_unlab=theano.shared(numpy.asarray(train_set_x_unlab,
dtype=theano.config.floatX),
borrow=True)
train_set_y_unlab=theano.shared(numpy.asarray(train_set_y_unlab,
dtype=theano.config.floatX),
borrow=True)
# compute number of minibatches for training, validation and testing
n_train_batches_l = train_set_y_lab.eval().shape[0]
n_train_batches_l /= batch_size
n_train_batches_u = train_set_y_unlab.eval().shape[0]
n_train_batches_u /= batch_size
# compute number of minibatches for training, validation and testing
#n_train_batches = train_set_x.get_value(borrow=True).shape[0]
#n_train_batches /= batch_size
# numpy random generator
# start-snippet-3
numpy_rng = numpy.random.RandomState(89677)
print '... building the model'
# construct the stacked denoising autoencoder class
#from SdA_orig import SdA as SdA_old
hidden_layer_size = 100
SdA_inp = SdA(numpy_rng,
n_ins=392,
hidden_layers_sizes=[hidden_layer_size]
)
SdA_out = SdA(numpy_rng,
n_ins=392,
hidden_layers_sizes=[hidden_layer_size]
)
# PRETRAINING THE MODEL #
if 0 : # pretrain inp ae
print '... getting the pretraining functions for INPUT AE'
pretraining_fns = SdA_inp.pretraining_functions(train_set_x=train_set_x_unlab,
batch_size=batch_size)
print '... pre-training the model'
start_time = time.clock()
## Pre-train layer-wise
corruption_levels = [.1, .2, .3]
for i in xrange(SdA_inp.n_layers):
# go through pretraining epochs
for epoch in xrange(pretraining_epochs):
# go through the training set
c = []
for batch_index in xrange(n_train_batches_u):
c.append(pretraining_fns[i](index=batch_index,
corruption=corruption_levels[i],
lr=pretrain_lr))
print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),
print numpy.mean(c)
end_time = time.clock()
print >> sys.stderr, ('The pretraining code for file ' +
os.path.split(__file__)[1] +
' ran for %.2fm' % ((end_time - start_time) / 60.))
if 0 : # pretrain out ae
print '... getting the pretraining functions for OUTPUT AE'
pretraining_fns = SdA_out.pretraining_functions(train_set_x=train_set_y_unlab,
batch_size=batch_size)
print '... pre-training the model'
start_time = time.clock()
## Pre-train layer-wise
corruption_levels = [.5, .2, .3]
for i in xrange(SdA_out.n_layers):
# go through pretraining epochs
for epoch in xrange(pretraining_epochs):
# go through the training set
c = []
for batch_index in xrange(n_train_batches_u):
c.append(pretraining_fns[i](index=batch_index,
corruption=corruption_levels[i],
lr=pretrain_lr))
print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),
print numpy.mean(c)
end_time = time.clock()
print >> sys.stderr, ('The pretraining code for file ' +
os.path.split(__file__)[1] +
' ran for %.2fm' % ((end_time - start_time) / 60.))
if 0: # save aes
f=open('aes_shallow_sig_nobias.pkl', 'w+')
import pickle
pickle.dump(SdA_inp, f)
pickle.dump(SdA_out, f)
f.flush()
f.close()
if 0: # load aes
f=open('aes_shallow_sig_nobias.pkl', 'r')
import pickle
SdA_inp=pickle.load(f)
SdA_out=pickle.load(f)
f.close()
if 1: # cca
from dcca_numpy import netCCA_nobias, netCCA, dCCA
from mlp_numpy import expit, logistic_prime, linear, linear_prime, relu, relu_prime, tanh, tanh_prime
train_y1 = train_set_x_lab.eval()
train_y2 = train_set_y_lab.eval()
test_y1 = test_set_x.eval()
test_y2 = test_set_y.eval()
##param1=((train_y1.shape[1],0,0),(2038, relu, relu_prime),(50, relu, relu_prime))
##param2=((train_y2.shape[1],0,0),(1608, relu, relu_prime),(50, relu, relu_prime))
param1=((train_y1.shape[1],0,0),(hidden_layer_size, expit, logistic_prime))
param2=((train_y2.shape[1],0,0),(hidden_layer_size, expit, logistic_prime))
W1s = []
b1s = []
for i in range(len(SdA_inp.dA_layers)):
W1s.append( SdA_inp.dA_layers[i].W.T.eval() )
##b1s.append( SdA_inp.dA_layers[i].b.eval() )
##b1s[-1] = b1s[-1].reshape((b1s[-1].shape[0], 1))
W2s = []
b2s = []
for i in range(len(SdA_out.dA_layers)):
W2s.append( SdA_out.dA_layers[i].W.T.eval() )
##b2s.append( SdA_out.dA_layers[i].b.eval() )
##b2s[-1] = b2s[-1].reshape((b2s[-1].shape[0], 1))
numpy.random.seed(0)
N1=netCCA_nobias(train_y1,param1, W1s)
N2=netCCA_nobias(train_y2,param2, W2s)
N = dCCA(train_y1, train_y2, N1, N2)
N1.reconstruct(test_set_x.eval()[0,:])
cnt = 0
from dcca_numpy import cca_cost, cca, order_cost, cor_cost
while True:
X=N1.predict(test_set_x.eval())
Y=N2.predict(test_set_y.eval())
_H1 = numpy.dot(X, N.A1)
_H2 = numpy.dot(Y, N.A2)
print 'cuem', cnt, cor_cost(_H1, _H2)
X1_rec = numpy.tanh(X.dot(N1.weights[0]))
X2_rec = numpy.tanh(Y.dot(N2.weights[0]))
param=((hidden_layer_size,0,0),(hidden_layer_size, relu, relu_prime))
from mlp_numpy import NeuralNetwork as NN
lr=NN(X,Y,param)
lr.train(X[:,:],Y[:,:],10, 0.005)
Yh=lr.predict(X[:,:])
X2_reg = N2.fs[-1](numpy.dot(Yh,N2.weights[0]))
#X2_reg = N2.fs[-1](numpy.dot(_H1.dot(numpy.linalg.inv(N.A1)),N2.weights[0]))
print '****', 'mse1:', numpy.mean((X1_rec-test_set_x.eval())**2.0)
print '****', 'mse2:', numpy.mean((X2_rec-test_set_y.eval())**2.0)
print '****', 'mse_map:', numpy.mean((X2_reg-test_set_y.eval())**2.0)
if cnt % 2:
N.train(5, True, 10000.0)
else:
N.train(5, False, 10000.0)
cnt += 1
f=open('netcca.pkl', 'w+')
import pickle
pickle.dump(N, f)
pickle.dump(N, f)
f.flush()
f.close()
if cnt == 200:
break
for i in range(len(SdA_inp.dA_layers)):
SdA_inp.dA_layers[i].W = theano.shared( N1.weights[i].T )
SdA_inp.dA_layers[i].b = theano.shared( N1.biases[i][:,0] )
for i in range(len(SdA_out.dA_layers)):
SdA_out.dA_layers[i].W = theano.shared( N2.weights[i].T )
SdA_out.dA_layers[i].b = theano.shared( N2.weights[i][:,0] )
if 1 : # pretrain middle layer
print '... pre-training MIDDLE layer'
h1 = T.matrix('x') # the data is presented as rasterized images
h2 = T.matrix('y') # the labels are presented as 1D vector of
log_reg = HiddenLayer(numpy_rng, h1, hidden_layer_size, hidden_layer_size)
if 1: # for middle layer
learning_rate = 0.01
fprop_inp = theano.function(
[],
SdA_inp.sigmoid_layers[-1].output,
givens={
SdA_inp.sigmoid_layers[0].input: train_set_x_lab
},
name='fprop_inp'
)
fprop_out = theano.function(
[],
SdA_out.sigmoid_layers[-1].output,
givens={
SdA_out.sigmoid_layers[0].input: train_set_y_lab
},
name='fprop_out'
)
#H11=fprop_inp()
#H21=fprop_out()
##H1=N1.predict(train_set_x.eval())
##H2=N2.predict(train_set_y.eval())
H1=fprop_inp()
H2=fprop_out()
H1=theano.shared(H1)
H2=theano.shared(H2)
# compute the gradients with respect to the model parameters
logreg_cost = log_reg.mse(h2)
gparams = T.grad(logreg_cost, log_reg.params)
# compute list of fine-tuning updates
updates = [
(param, param - gparam * learning_rate)
for param, gparam in zip(log_reg.params, gparams)
]
train_fn_middle = theano.function(
inputs=[],
outputs=logreg_cost,
updates=updates,
givens={
h1: H1,
h2: H2
},
name='train_middle'
)
epoch = 0
while epoch < 10:
print epoch, train_fn_middle()
epoch += 1
sda = SdA_regress(
SdA_inp,
SdA_out,
log_reg,
numpy_rng=numpy_rng,
n_inp=28*28//2,
hidden_layers_sizes_inp=[hidden_layer_size],
hidden_layers_sizes_out=[hidden_layer_size],
n_out=28*28//2
)
# end-snippet-3 start-snippet-4
# end-snippet-4
# FINETUNING THE MODEL #
# get the training, validation and testing function for the model
print '... getting the finetuning functions'
train_fn, validate_model, test_model = sda.build_finetune_functions(
datasets=datasets,
batch_size=batch_size,
learning_rate=finetune_lr
)
print '... finetunning the model'
# early-stopping parameters
patience = 10 * n_train_batches_l # look as this many examples regardless
patience_increase = 2. # wait this much longer when a new best is
# found
improvement_threshold = 0.995 # a relative improvement of this much is
# considered significant
validation_frequency = min(n_train_batches_l, patience / 2)
# go through this many
# minibatche before checking the network
# on the validation set; in this case we
# check every epoch
best_validation_loss = numpy.inf
test_score = 0.
start_time = time.clock()
done_looping = False
epoch = 0
fprop = theano.function(
[],
sda.sigmoid_layers[-1].output,
givens={
sda.x: test_set_x
},
name='fprop'
)
while True:
epoch = epoch + 1
for minibatch_index in xrange(n_train_batches_l):
minibatch_avg_cost = train_fn(minibatch_index)
iter = (epoch - 1) * n_train_batches_l + minibatch_index
if (iter + 1) % validation_frequency == 0:
validation_losses = validate_model()
this_validation_loss = numpy.mean(validation_losses)
print('epoch %i, minibatch %i/%i, validation error %f %%' %
(epoch, minibatch_index + 1, n_train_batches_l,
this_validation_loss ))
# if we got the best validation score until now
if this_validation_loss < best_validation_loss:
#improve patience if loss improvement is good enough
if (
this_validation_loss < best_validation_loss *
improvement_threshold
):
patience = max(patience, iter * patience_increase)
# save best validation score and iteration number
best_validation_loss = this_validation_loss
best_iter = iter
# test it on the test set
test_losses = test_model()
test_score = numpy.mean(test_losses)
print((' epoch %i, minibatch %i/%i, test error of '
'best model %f %%') %
(epoch, minibatch_index + 1, n_train_batches_l,
test_score ))
if patience <= iter:
done_looping = True
#break
if 0: # vis weights
fprop = theano.function(
[],
sda.sigmoid_layers[-1].output,
givens={
sda.x: test_set_x
},
name='fprop'
)
yh=fprop()
yh=yh
end_time = time.clock()
print(
(
'Optimization complete with best validation score of %f %%, '
'on iteration %i, '
'with test performance %f %%'
)
% (best_validation_loss , best_iter + 1, test_score)
)
print >> sys.stderr, ('The training code for file ' +
os.path.split(__file__)[1] +
' ran for %.2fm' % ((end_time - start_time) / 60.))
1
Example 22
def _linprog_simplex(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None,
bounds=None, maxiter=1000, disp=False, callback=None,
tol=1.0E-12, bland=False, **unknown_options):
"""
Solve the following linear programming problem via a two-phase
simplex algorithm.
minimize: c^T * x
subject to: A_ub * x <= b_ub
A_eq * x == b_eq
Parameters
----------
c : array_like
Coefficients of the linear objective function to be minimized.
A_ub : array_like
2-D array which, when matrix-multiplied by x, gives the values of the
upper-bound inequality constraints at x.
b_ub : array_like
1-D array of values representing the upper-bound of each inequality
constraint (row) in A_ub.
A_eq : array_like
2-D array which, when matrix-multiplied by x, gives the values of the
equality constraints at x.
b_eq : array_like
1-D array of values representing the RHS of each equality constraint
(row) in A_eq.
bounds : array_like
The bounds for each independent variable in the solution, which can take
one of three forms::
None : The default bounds, all variables are non-negative.
(lb, ub) : If a 2-element sequence is provided, the same
lower bound (lb) and upper bound (ub) will be applied
to all variables.
[(lb_0, ub_0), (lb_1, ub_1), ...] : If an n x 2 sequence is provided,
each variable x_i will be bounded by lb[i] and ub[i].
Infinite bounds are specified using -np.inf (negative)
or np.inf (positive).
callback : callable
If a callback function is provide, it will be called within each
iteration of the simplex algorithm. The callback must have the
signature `callback(xk, **kwargs)` where xk is the current solution
vector and kwargs is a dictionary containing the following::
"tableau" : The current Simplex algorithm tableau
"nit" : The current iteration.
"pivot" : The pivot (row, column) used for the next iteration.
"phase" : Whether the algorithm is in Phase 1 or Phase 2.
"bv" : A structured array containing a string representation of each
basic variable and its current value.
Options
-------
maxiter : int
The maximum number of iterations to perform.
disp : bool
If True, print exit status message to sys.stdout
tol : float
The tolerance which determines when a solution is "close enough" to zero
in Phase 1 to be considered a basic feasible solution or close enough
to positive to to serve as an optimal solution.
bland : bool
If True, use Bland's anti-cycling rule [3] to choose pivots to
prevent cycling. If False, choose pivots which should lead to a
converged solution more quickly. The latter method is subject to
cycling (non-convergence) in rare instances.
Returns
-------
A scipy.optimize.OptimizeResult consisting of the following fields::
x : ndarray
The independent variable vector which optimizes the linear
programming problem.
fun : float
Value of the objective function.
slack : ndarray
The values of the slack variables. Each slack variable corresponds
to an inequality constraint. If the slack is zero, then the
corresponding constraint is active.
success : bool
Returns True if the algorithm succeeded in finding an optimal
solution.
status : int
An integer representing the exit status of the optimization::
0 : Optimization terminated successfully
1 : Iteration limit reached
2 : Problem appears to be infeasible
3 : Problem appears to be unbounded
nit : int
The number of iterations performed.
message : str
A string descriptor of the exit status of the optimization.
Examples
--------
Consider the following problem:
Minimize: f = -1*x[0] + 4*x[1]
Subject to: -3*x[0] + 1*x[1] <= 6
1*x[0] + 2*x[1] <= 4
x[1] >= -3
where: -inf <= x[0] <= inf
This problem deviates from the standard linear programming problem. In
standard form, linear programming problems assume the variables x are
non-negative. Since the variables don't have standard bounds where
0 <= x <= inf, the bounds of the variables must be explicitly set.
There are two upper-bound constraints, which can be expressed as
dot(A_ub, x) <= b_ub
The input for this problem is as follows:
>>> from scipy.optimize import linprog
>>> c = [-1, 4]
>>> A = [[-3, 1], [1, 2]]
>>> b = [6, 4]
>>> x0_bnds = (None, None)
>>> x1_bnds = (-3, None)
>>> res = linprog(c, A, b, bounds=(x0_bnds, x1_bnds))
>>> print(res)
fun: -22.0
message: 'Optimization terminated successfully.'
nit: 1
slack: array([ 39., 0.])
status: 0
success: True
x: array([ 10., -3.])
References
----------
.. [1] Dantzig, George B., Linear programming and extensions. Rand
Corporation Research Study Princeton Univ. Press, Princeton, NJ, 1963
.. [2] Hillier, S.H. and Lieberman, G.J. (1995), "Introduction to
Mathematical Programming", McGraw-Hill, Chapter 4.
.. [3] Bland, Robert G. New finite pivoting rules for the simplex method.
Mathematics of Operations Research (2), 1977: pp. 103-107.
"""
_check_unknown_options(unknown_options)
status = 0
messages = {0: "Optimization terminated successfully.",
1: "Iteration limit reached.",
2: "Optimization failed. Unable to find a feasible"
" starting point.",
3: "Optimization failed. The problem appears to be unbounded.",
4: "Optimization failed. Singular matrix encountered."}
have_floor_variable = False
cc = np.asarray(c)
# The initial value of the objective function element in the tableau
f0 = 0
# The number of variables as given by c
n = len(c)
# Convert the input arguments to arrays (sized to zero if not provided)
Aeq = np.asarray(A_eq) if A_eq is not None else np.empty([0, len(cc)])
Aub = np.asarray(A_ub) if A_ub is not None else np.empty([0, len(cc)])
beq = np.ravel(np.asarray(b_eq)) if b_eq is not None else np.empty([0])
bub = np.ravel(np.asarray(b_ub)) if b_ub is not None else np.empty([0])
# Analyze the bounds and determine what modifications to be made to
# the constraints in order to accommodate them.
L = np.zeros(n, dtype=np.float64)
U = np.ones(n, dtype=np.float64)*np.inf
if bounds is None or len(bounds) == 0:
pass
elif len(bounds) == 2 and not hasattr(bounds[0], '__len__'):
# All bounds are the same
a = bounds[0] if bounds[0] is not None else -np.inf
b = bounds[1] if bounds[1] is not None else np.inf
L = np.asarray(n*[a], dtype=np.float64)
U = np.asarray(n*[b], dtype=np.float64)
else:
if len(bounds) != n:
status = -1
message = ("Invalid input for linprog with method = 'simplex'. "
"Length of bounds is inconsistent with the length of c")
else:
try:
for i in range(n):
if len(bounds[i]) != 2:
raise IndexError()
L[i] = bounds[i][0] if bounds[i][0] is not None else -np.inf
U[i] = bounds[i][1] if bounds[i][1] is not None else np.inf
except IndexError:
status = -1
message = ("Invalid input for linprog with "
"method = 'simplex'. bounds must be a n x 2 "
"sequence/array where n = len(c).")
if np.any(L == -np.inf):
# If any lower-bound constraint is a free variable
# add the first column variable as the "floor" variable which
# accommodates the most negative variable in the problem.
n = n + 1
L = np.concatenate([np.array([0]), L])
U = np.concatenate([np.array([np.inf]), U])
cc = np.concatenate([np.array([0]), cc])
Aeq = np.hstack([np.zeros([Aeq.shape[0], 1]), Aeq])
Aub = np.hstack([np.zeros([Aub.shape[0], 1]), Aub])
have_floor_variable = True
# Now before we deal with any variables with lower bounds < 0,
# deal with finite bounds which can be simply added as new constraints.
# Also validate bounds inputs here.
for i in range(n):
if(L[i] > U[i]):
status = -1
message = ("Invalid input for linprog with method = 'simplex'. "
"Lower bound %d is greater than upper bound %d" % (i, i))
if np.isinf(L[i]) and L[i] > 0:
status = -1
message = ("Invalid input for linprog with method = 'simplex'. "
"Lower bound may not be +infinity")
if np.isinf(U[i]) and U[i] < 0:
status = -1
message = ("Invalid input for linprog with method = 'simplex'. "
"Upper bound may not be -infinity")
if np.isfinite(L[i]) and L[i] > 0:
# Add a new lower-bound (negative upper-bound) constraint
Aub = np.vstack([Aub, np.zeros(n)])
Aub[-1, i] = -1
bub = np.concatenate([bub, np.array([-L[i]])])
L[i] = 0
if np.isfinite(U[i]):
# Add a new upper-bound constraint
Aub = np.vstack([Aub, np.zeros(n)])
Aub[-1, i] = 1
bub = np.concatenate([bub, np.array([U[i]])])
U[i] = np.inf
# Now find negative lower bounds (finite or infinite) which require a
# change of variables or free variables and handle them appropriately
for i in range(0, n):
if L[i] < 0:
if np.isfinite(L[i]) and L[i] < 0:
# Add a change of variables for x[i]
# For each row in the constraint matrices, we take the
# coefficient from column i in A,
# and subtract the product of that and L[i] to the RHS b
beq = beq - Aeq[:, i] * L[i]
bub = bub - Aub[:, i] * L[i]
# We now have a nonzero initial value for the objective
# function as well.
f0 = f0 - cc[i] * L[i]
else:
# This is an unrestricted variable, let x[i] = u[i] - v[0]
# where v is the first column in all matrices.
Aeq[:, 0] = Aeq[:, 0] - Aeq[:, i]
Aub[:, 0] = Aub[:, 0] - Aub[:, i]
cc[0] = cc[0] - cc[i]
if np.isinf(U[i]):
if U[i] < 0:
status = -1
message = ("Invalid input for linprog with "
"method = 'simplex'. Upper bound may not be -inf.")
# The number of upper bound constraints (rows in A_ub and elements in b_ub)
mub = len(bub)
# The number of equality constraints (rows in A_eq and elements in b_eq)
meq = len(beq)
# The total number of constraints
m = mub+meq
# The number of slack variables (one for each of the upper-bound constraints)
n_slack = mub
# The number of artificial variables (one for each lower-bound and equality
# constraint)
n_artificial = meq + np.count_nonzero(bub < 0)
try:
Aub_rows, Aub_cols = Aub.shape
except ValueError:
raise ValueError("Invalid input. A_ub must be two-dimensional")
try:
Aeq_rows, Aeq_cols = Aeq.shape
except ValueError:
raise ValueError("Invalid input. A_eq must be two-dimensional")
if Aeq_rows != meq:
status = -1
message = ("Invalid input for linprog with method = 'simplex'. "
"The number of rows in A_eq must be equal "
"to the number of values in b_eq")
if Aub_rows != mub:
status = -1
message = ("Invalid input for linprog with method = 'simplex'. "
"The number of rows in A_ub must be equal "
"to the number of values in b_ub")
if Aeq_cols > 0 and Aeq_cols != n:
status = -1
message = ("Invalid input for linprog with method = 'simplex'. "
"Number of columns in A_eq must be equal "
"to the size of c")
if Aub_cols > 0 and Aub_cols != n:
status = -1
message = ("Invalid input for linprog with method = 'simplex'. "
"Number of columns in A_ub must be equal to the size of c")
if status != 0:
# Invalid inputs provided
raise ValueError(message)
# Create the tableau
T = np.zeros([m+2, n+n_slack+n_artificial+1])
# Insert objective into tableau
T[-2, :n] = cc
T[-2, -1] = f0
b = T[:-2, -1]
if meq > 0:
# Add Aeq to the tableau
T[:meq, :n] = Aeq
# Add beq to the tableau
b[:meq] = beq
if mub > 0:
# Add Aub to the tableau
T[meq:meq+mub, :n] = Aub
# At bub to the tableau
b[meq:meq+mub] = bub
# Add the slack variables to the tableau
np.fill_diagonal(T[meq:m, n:n+n_slack], 1)
# Further set up the tableau.
# If a row corresponds to an equality constraint or a negative b (a lower
# bound constraint), then an artificial variable is added for that row.
# Also, if b is negative, first flip the signs in that constraint.
slcount = 0
avcount = 0
basis = np.zeros(m, dtype=int)
r_artificial = np.zeros(n_artificial, dtype=int)
for i in range(m):
if i < meq or b[i] < 0:
# basic variable i is in column n+n_slack+avcount
basis[i] = n+n_slack+avcount
r_artificial[avcount] = i
avcount += 1
if b[i] < 0:
b[i] *= -1
T[i, :-1] *= -1
T[i, basis[i]] = 1
T[-1, basis[i]] = 1
else:
# basic variable i is in column n+slcount
basis[i] = n+slcount
slcount += 1
# Make the artificial variables basic feasible variables by subtracting
# each row with an artificial variable from the Phase 1 objective
for r in r_artificial:
T[-1, :] = T[-1, :] - T[r, :]
nit1, status = _solve_simplex(T, n, basis, phase=1, callback=callback,
maxiter=maxiter, tol=tol, bland=bland)
# if pseudo objective is zero, remove the last row from the tableau and
# proceed to phase 2
if abs(T[-1, -1]) < tol:
# Remove the pseudo-objective row from the tableau
T = T[:-1, :]
# Remove the artificial variable columns from the tableau
T = np.delete(T, np.s_[n+n_slack:n+n_slack+n_artificial], 1)
else:
# Failure to find a feasible starting point
status = 2
if status != 0:
message = messages[status]
if disp:
print(message)
return OptimizeResult(x=np.nan, fun=-T[-1, -1], nit=nit1, status=status,
message=message, success=False)
# Phase 2
nit2, status = _solve_simplex(T, n, basis, maxiter=maxiter-nit1, phase=2,
callback=callback, tol=tol, nit0=nit1,
bland=bland)
solution = np.zeros(n+n_slack+n_artificial)
solution[basis[:m]] = T[:m, -1]
x = solution[:n]
slack = solution[n:n+n_slack]
# For those variables with finite negative lower bounds,
# reverse the change of variables
masked_L = np.ma.array(L, mask=np.isinf(L), fill_value=0.0).filled()
x = x + masked_L
# For those variables with infinite negative lower bounds,
# take x[i] as the difference between x[i] and the floor variable.
if have_floor_variable:
for i in range(1, n):
if np.isinf(L[i]):
x[i] -= x[0]
x = x[1:]
# Optimization complete at this point
obj = -T[-1, -1]
if status in (0, 1):
if disp:
print(messages[status])
print(" Current function value: {0: <12.6f}".format(obj))
print(" Iterations: {0:d}".format(nit2))
else:
if disp:
print(messages[status])
print(" Iterations: {0:d}".format(nit2))
return OptimizeResult(x=x, fun=obj, nit=int(nit2), status=status, slack=slack,
message=messages[status], success=(status == 0))
0
Example 23
def deltaE_ciede2000(lab1, lab2, kL=1, kC=1, kH=1):
"""Color difference as given by the CIEDE 2000 standard.
CIEDE 2000 is a major revision of CIDE94. The perceptual calibration is
largely based on experience with automotive paint on smooth surfaces.
Parameters
----------
lab1 : array_like
reference color (Lab colorspace)
lab2 : array_like
comparison color (Lab colorspace)
kL : float (range), optional
lightness scale factor, 1 for "acceptably close"; 2 for "imperceptible"
see deltaE_cmc
kC : float (range), optional
chroma scale factor, usually 1
kH : float (range), optional
hue scale factor, usually 1
Returns
-------
deltaE : array_like
The distance between `lab1` and `lab2`
Notes
-----
CIEDE 2000 assumes parametric weighting factors for the lightness, chroma,
and hue (`kL`, `kC`, `kH` respectively). These default to 1.
References
----------
.. [1] http://en.wikipedia.org/wiki/Color_difference
.. [2] http://www.ece.rochester.edu/~gsharma/ciede2000/ciede2000noteCRNA.pdf
(doi:10.1364/AO.33.008069)
.. [3] M. Melgosa, J. Quesada, and E. Hita, "Uniformity of some recent
color metrics tested with an accurate color-difference tolerance
dataset," Appl. Opt. 33, 8069-8077 (1994).
"""
lab1 = np.asarray(lab1)
lab2 = np.asarray(lab2)
unroll = False
if lab1.ndim == 1 and lab2.ndim == 1:
unroll = True
if lab1.ndim == 1:
lab1 = lab1[None, :]
if lab2.ndim == 1:
lab2 = lab2[None, :]
L1, a1, b1 = np.rollaxis(lab1, -1)[:3]
L2, a2, b2 = np.rollaxis(lab2, -1)[:3]
# distort `a` based on average chroma
# then convert to lch coordines from distorted `a`
# all subsequence calculations are in the new coordiantes
# (often denoted "prime" in the literature)
Cbar = 0.5 * (np.hypot(a1, b1) + np.hypot(a2, b2))
c7 = Cbar ** 7
G = 0.5 * (1 - np.sqrt(c7 / (c7 + 25 ** 7)))
scale = 1 + G
C1, h1 = _cart2polar_2pi(a1 * scale, b1)
C2, h2 = _cart2polar_2pi(a2 * scale, b2)
# recall that c, h are polar coordiantes. c==r, h==theta
# cide2000 has four terms to delta_e:
# 1) Luminance term
# 2) Hue term
# 3) Chroma term
# 4) hue Rotation term
# lightness term
Lbar = 0.5 * (L1 + L2)
tmp = (Lbar - 50) ** 2
SL = 1 + 0.015 * tmp / np.sqrt(20 + tmp)
L_term = (L2 - L1) / (kL * SL)
# chroma term
Cbar = 0.5 * (C1 + C2) # new coordiantes
SC = 1 + 0.045 * Cbar
C_term = (C2 - C1) / (kC * SC)
# hue term
h_diff = h2 - h1
h_sum = h1 + h2
CC = C1 * C2
dH = h_diff.copy()
dH[h_diff > np.pi] -= 2 * np.pi
dH[h_diff < -np.pi] += 2 * np.pi
dH[CC == 0.] = 0. # if r == 0, dtheta == 0
dH_term = 2 * np.sqrt(CC) * np.sin(dH / 2)
Hbar = h_sum.copy()
mask = np.logical_and(CC != 0., np.abs(h_diff) > np.pi)
Hbar[mask * (h_sum < 2 * np.pi)] += 2 * np.pi
Hbar[mask * (h_sum >= 2 * np.pi)] -= 2 * np.pi
Hbar[CC == 0.] *= 2
Hbar *= 0.5
T = (1 -
0.17 * np.cos(Hbar - np.deg2rad(30)) +
0.24 * np.cos(2 * Hbar) +
0.32 * np.cos(3 * Hbar + np.deg2rad(6)) -
0.20 * np.cos(4 * Hbar - np.deg2rad(63))
)
SH = 1 + 0.015 * Cbar * T
H_term = dH_term / (kH * SH)
# hue rotation
c7 = Cbar ** 7
Rc = 2 * np.sqrt(c7 / (c7 + 25 ** 7))
dtheta = np.deg2rad(30) * np.exp(-((np.rad2deg(Hbar) - 275) / 25) ** 2)
R_term = -np.sin(2 * dtheta) * Rc * C_term * H_term
# put it all together
dE2 = L_term ** 2
dE2 += C_term ** 2
dE2 += H_term ** 2
dE2 += R_term
ans = np.sqrt(dE2)
if unroll:
ans = ans[0]
return ans
0
Example 24
Project: QuantEcon.py Source File: lqnash.py
def nnash(A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2,
beta=1.0, tol=1e-8, max_iter=1000):
r"""
Compute the limit of a Nash linear quadratic dynamic game. In this
problem, player i minimizes
.. math::
\sum_{t=0}^{\infty}
\left\{
x_t' r_i x_t + 2 x_t' w_i
u_{it} +u_{it}' q_i u_{it} + u_{jt}' s_i u_{jt} + 2 u_{jt}'
m_i u_{it}
\right\}
subject to the law of motion
.. math::
x_{t+1} = A x_t + b_1 u_{1t} + b_2 u_{2t}
and a perceived control law :math:`u_j(t) = - f_j x_t` for the other
player.
The solution computed in this routine is the :math:`f_i` and
:math:`p_i` of the associated double optimal linear regulator
problem.
Parameters
----------
A : scalar(float) or array_like(float)
Corresponds to the above equation, should be of size (n, n)
B1 : scalar(float) or array_like(float)
As above, size (n, k_1)
B2 : scalar(float) or array_like(float)
As above, size (n, k_2)
R1 : scalar(float) or array_like(float)
As above, size (n, n)
R2 : scalar(float) or array_like(float)
As above, size (n, n)
Q1 : scalar(float) or array_like(float)
As above, size (k_1, k_1)
Q2 : scalar(float) or array_like(float)
As above, size (k_2, k_2)
S1 : scalar(float) or array_like(float)
As above, size (k_1, k_1)
S2 : scalar(float) or array_like(float)
As above, size (k_2, k_2)
W1 : scalar(float) or array_like(float)
As above, size (n, k_1)
W2 : scalar(float) or array_like(float)
As above, size (n, k_2)
M1 : scalar(float) or array_like(float)
As above, size (k_2, k_1)
M2 : scalar(float) or array_like(float)
As above, size (k_1, k_2)
beta : scalar(float), optional(default=1.0)
Discount rate
tol : scalar(float), optional(default=1e-8)
This is the tolerance level for convergence
max_iter : scalar(int), optional(default=1000)
This is the maximum number of iteratiosn allowed
Returns
-------
F1 : array_like, dtype=float, shape=(k_1, n)
Feedback law for agent 1
F2 : array_like, dtype=float, shape=(k_2, n)
Feedback law for agent 2
P1 : array_like, dtype=float, shape=(n, n)
The steady-state solution to the associated discrete matrix
Riccati equation for agent 1
P2 : array_like, dtype=float, shape=(n, n)
The steady-state solution to the associated discrete matrix
Riccati equation for agent 2
"""
# == Unload parameters and make sure everything is an array == #
params = A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2
params = map(np.asarray, params)
A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2 = params
# == Multiply A, B1, B2 by sqrt(beta) to enforce discounting == #
A, B1, B2 = [np.sqrt(beta) * x for x in (A, B1, B2)]
n = A.shape[0]
if B1.ndim == 1:
k_1 = 1
B1 = np.reshape(B1, (n, 1))
else:
k_1 = B1.shape[1]
if B2.ndim == 1:
k_2 = 1
B2 = np.reshape(B2, (n, 1))
else:
k_2 = B2.shape[1]
v1 = eye(k_1)
v2 = eye(k_2)
P1 = np.zeros((n, n))
P2 = np.zeros((n, n))
F1 = np.random.randn(k_1, n)
F2 = np.random.randn(k_2, n)
for it in range(max_iter):
# update
F10 = F1
F20 = F2
G2 = solve(dot(B2.T, P2.dot(B2))+Q2, v2)
G1 = solve(dot(B1.T, P1.dot(B1))+Q1, v1)
H2 = dot(G2, B2.T.dot(P2))
H1 = dot(G1, B1.T.dot(P1))
# break up the computation of F1, F2
F1_left = v1 - dot(H1.dot(B2)+G1.dot(M1.T),
H2.dot(B1)+G2.dot(M2.T))
F1_right = H1.dot(A)+G1.dot(W1.T) - dot(H1.dot(B2)+G1.dot(M1.T),
H2.dot(A)+G2.dot(W2.T))
F1 = solve(F1_left, F1_right)
F2 = H2.dot(A)+G2.dot(W2.T) - dot(H2.dot(B1)+G2.dot(M2.T), F1)
Lambda1 = A - B2.dot(F2)
Lambda2 = A - B1.dot(F1)
Pi1 = R1 + dot(F2.T, S1.dot(F2))
Pi2 = R2 + dot(F1.T, S2.dot(F1))
P1 = dot(Lambda1.T, P1.dot(Lambda1)) + Pi1 - \
dot(dot(Lambda1.T, P1.dot(B1)) + W1 - F2.T.dot(M1), F1)
P2 = dot(Lambda2.T, P2.dot(Lambda2)) + Pi2 - \
dot(dot(Lambda2.T, P2.dot(B2)) + W2 - F1.T.dot(M2), F2)
dd = np.max(np.abs(F10 - F1)) + np.max(np.abs(F20 - F2))
if dd < tol: # success!
break
else:
msg = 'No convergence: Iteration limit of {0} reached in nnash'
raise ValueError(msg.format(max_iter))
return F1, F2, P1, P2
0
Example 25
Project: scikit-image Source File: dtype.py
def convert(image, dtype, force_copy=False, uniform=False):
"""
Convert an image to the requested data-type.
Warnings are issued in case of precision loss, or when negative values
are clipped during conversion to unsigned integer types (sign loss).
Floating point values are expected to be normalized and will be clipped
to the range [0.0, 1.0] or [-1.0, 1.0] when converting to unsigned or
signed integers respectively.
Numbers are not shifted to the negative side when converting from
unsigned to signed integer types. Negative values will be clipped when
converting to unsigned integers.
Parameters
----------
image : ndarray
Input image.
dtype : dtype
Target data-type.
force_copy : bool, optional
Force a copy of the data, irrespective of its current dtype.
uniform : bool, optional
Uniformly quantize the floating point range to the integer range.
By default (uniform=False) floating point values are scaled and
rounded to the nearest integers, which minimizes back and forth
conversion errors.
References
----------
.. [1] DirectX data conversion rules.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd607323%28v=vs.85%29.aspx
.. [2] Data Conversions. In "OpenGL ES 2.0 Specification v2.0.25",
pp 7-8. Khronos Group, 2010.
.. [3] Proper treatment of pixels as integers. A.W. Paeth.
In "Graphics Gems I", pp 249-256. Morgan Kaufmann, 1990.
.. [4] Dirty Pixels. J. Blinn. In "Jim Blinn's corner: Dirty Pixels",
pp 47-57. Morgan Kaufmann, 1998.
"""
image = np.asarray(image)
dtypeobj = np.dtype(dtype)
dtypeobj_in = image.dtype
dtype = dtypeobj.type
dtype_in = dtypeobj_in.type
if dtype_in == dtype:
if force_copy:
image = image.copy()
return image
if not (dtype_in in _supported_types and dtype in _supported_types):
raise ValueError("can not convert %s to %s." % (dtypeobj_in, dtypeobj))
def sign_loss():
warn("Possible sign loss when converting negative image of type "
"%s to positive image of type %s." % (dtypeobj_in, dtypeobj))
def prec_loss():
warn("Possible precision loss when converting from "
"%s to %s" % (dtypeobj_in, dtypeobj))
def _dtype(itemsize, *dtypes):
# Return first of `dtypes` with itemsize greater than `itemsize`
return next(dt for dt in dtypes if itemsize < np.dtype(dt).itemsize)
def _dtype2(kind, bits, itemsize=1):
# Return dtype of `kind` that can store a `bits` wide unsigned int
def compare(x, y, kind='u'):
if kind == 'u':
return x <= y
else:
return x < y
s = next(i for i in (itemsize, ) + (2, 4, 8) if compare(bits, i * 8,
kind=kind))
return np.dtype(kind + str(s))
def _scale(a, n, m, copy=True):
# Scale unsigned/positive integers from n to m bits
# Numbers can be represented exactly only if m is a multiple of n
# Output array is of same kind as input.
kind = a.dtype.kind
if n > m and a.max() < 2 ** m:
mnew = int(np.ceil(m / 2) * 2)
if mnew > m:
dtype = "int%s" % mnew
else:
dtype = "uint%s" % mnew
n = int(np.ceil(n / 2) * 2)
msg = ("Downcasting %s to %s without scaling because max "
"value %s fits in %s" % (a.dtype, dtype, a.max(), dtype))
warn(msg)
return a.astype(_dtype2(kind, m))
elif n == m:
return a.copy() if copy else a
elif n > m:
# downscale with precision loss
prec_loss()
if copy:
b = np.empty(a.shape, _dtype2(kind, m))
np.floor_divide(a, 2**(n - m), out=b, dtype=a.dtype,
casting='unsafe')
return b
else:
a //= 2**(n - m)
return a
elif m % n == 0:
# exact upscale to a multiple of n bits
if copy:
b = np.empty(a.shape, _dtype2(kind, m))
np.multiply(a, (2**m - 1) // (2**n - 1), out=b, dtype=b.dtype)
return b
else:
a = np.array(a, _dtype2(kind, m, a.dtype.itemsize), copy=False)
a *= (2**m - 1) // (2**n - 1)
return a
else:
# upscale to a multiple of n bits,
# then downscale with precision loss
prec_loss()
o = (m // n + 1) * n
if copy:
b = np.empty(a.shape, _dtype2(kind, o))
np.multiply(a, (2**o - 1) // (2**n - 1), out=b, dtype=b.dtype)
b //= 2**(o - m)
return b
else:
a = np.array(a, _dtype2(kind, o, a.dtype.itemsize), copy=False)
a *= (2**o - 1) // (2**n - 1)
a //= 2**(o - m)
return a
kind = dtypeobj.kind
kind_in = dtypeobj_in.kind
itemsize = dtypeobj.itemsize
itemsize_in = dtypeobj_in.itemsize
if kind == 'b':
# to binary image
if kind_in in "fi":
sign_loss()
prec_loss()
return image > dtype_in(dtype_range[dtype_in][1] / 2)
if kind_in == 'b':
# from binary image, to float and to integer
result = image.astype(dtype)
if kind != 'f':
result *= dtype(dtype_range[dtype][1])
return result
if kind in 'ui':
imin = np.iinfo(dtype).min
imax = np.iinfo(dtype).max
if kind_in in 'ui':
imin_in = np.iinfo(dtype_in).min
imax_in = np.iinfo(dtype_in).max
if kind_in == 'f':
if np.min(image) < -1.0 or np.max(image) > 1.0:
raise ValueError("Images of type float must be between -1 and 1.")
if kind == 'f':
# floating point -> floating point
if itemsize_in > itemsize:
prec_loss()
return image.astype(dtype)
# floating point -> integer
prec_loss()
# use float type that can represent output integer type
image = np.array(image, _dtype(itemsize, dtype_in,
np.float32, np.float64))
if not uniform:
if kind == 'u':
image *= imax
else:
image *= imax - imin
image -= 1.0
image /= 2.0
np.rint(image, out=image)
np.clip(image, imin, imax, out=image)
elif kind == 'u':
image *= imax + 1
np.clip(image, 0, imax, out=image)
else:
image *= (imax - imin + 1.0) / 2.0
np.floor(image, out=image)
np.clip(image, imin, imax, out=image)
return image.astype(dtype)
if kind == 'f':
# integer -> floating point
if itemsize_in >= itemsize:
prec_loss()
# use float type that can exactly represent input integers
image = np.array(image, _dtype(itemsize_in, dtype,
np.float32, np.float64))
if kind_in == 'u':
image /= imax_in
# DirectX uses this conversion also for signed ints
#if imin_in:
# np.maximum(image, -1.0, out=image)
else:
image *= 2.0
image += 1.0
image /= imax_in - imin_in
return image.astype(dtype)
if kind_in == 'u':
if kind == 'i':
# unsigned integer -> signed integer
image = _scale(image, 8 * itemsize_in, 8 * itemsize - 1)
return image.view(dtype)
else:
# unsigned integer -> unsigned integer
return _scale(image, 8 * itemsize_in, 8 * itemsize)
if kind == 'u':
# signed integer -> unsigned integer
sign_loss()
image = _scale(image, 8 * itemsize_in - 1, 8 * itemsize)
result = np.empty(image.shape, dtype)
np.maximum(image, 0, out=result, dtype=image.dtype, casting='unsafe')
return result
# signed integer -> signed integer
if itemsize_in > itemsize:
return _scale(image, 8 * itemsize_in - 1, 8 * itemsize - 1)
image = image.astype(_dtype2('i', itemsize * 8))
image -= imin_in
image = _scale(image, 8 * itemsize_in, 8 * itemsize, copy=False)
image += imin
return image.astype(dtype)
0
Example 26
Project: scikit-learn Source File: hierarchical.py
def ward_tree(X, connectivity=None, n_clusters=None, return_distance=False):
"""Ward clustering based on a Feature matrix.
Recursively merges the pair of clusters that minimally increases
within-cluster variance.
The inertia matrix uses a Heapq-based representation.
This is the structured version, that takes into account some topological
structure between samples.
Read more in the :ref:`User Guide <hierarchical_clustering>`.
Parameters
----------
X : array, shape (n_samples, n_features)
feature matrix representing n_samples samples to be clustered
connectivity : sparse matrix (optional).
connectivity matrix. Defines for each sample the neighboring samples
following a given structure of the data. The matrix is assumed to
be symmetric and only the upper triangular half is used.
Default is None, i.e, the Ward algorithm is unstructured.
n_clusters : int (optional)
Stop early the construction of the tree at n_clusters. This is
useful to decrease computation time if the number of clusters is
not small compared to the number of samples. In this case, the
complete tree is not computed, thus the 'children' output is of
limited use, and the 'parents' output should rather be used.
This option is valid only when specifying a connectivity matrix.
return_distance: bool (optional)
If True, return the distance between the clusters.
Returns
-------
children : 2D array, shape (n_nodes-1, 2)
The children of each non-leaf node. Values less than `n_samples`
correspond to leaves of the tree which are the original samples.
A node `i` greater than or equal to `n_samples` is a non-leaf
node and has children `children_[i - n_samples]`. Alternatively
at the i-th iteration, children[i][0] and children[i][1]
are merged to form node `n_samples + i`
n_components : int
The number of connected components in the graph.
n_leaves : int
The number of leaves in the tree
parents : 1D array, shape (n_nodes, ) or None
The parent of each node. Only returned when a connectivity matrix
is specified, elsewhere 'None' is returned.
distances : 1D array, shape (n_nodes-1, )
Only returned if return_distance is set to True (for compatibility).
The distances between the centers of the nodes. `distances[i]`
corresponds to a weighted euclidean distance between
the nodes `children[i, 1]` and `children[i, 2]`. If the nodes refer to
leaves of the tree, then `distances[i]` is their unweighted euclidean
distance. Distances are updated in the following way
(from scipy.hierarchy.linkage):
The new entry :math:`d(u,v)` is computed as follows,
.. math::
d(u,v) = \\sqrt{\\frac{|v|+|s|}
{T}d(v,s)^2
+ \\frac{|v|+|t|}
{T}d(v,t)^2
- \\frac{|v|}
{T}d(s,t)^2}
where :math:`u` is the newly joined cluster consisting of
clusters :math:`s` and :math:`t`, :math:`v` is an unused
cluster in the forest, :math:`T=|v|+|s|+|t|`, and
:math:`|*|` is the cardinality of its argument. This is also
known as the incremental algorithm.
"""
X = np.asarray(X)
if X.ndim == 1:
X = np.reshape(X, (-1, 1))
n_samples, n_features = X.shape
if connectivity is None:
from scipy.cluster import hierarchy # imports PIL
if n_clusters is not None:
warnings.warn('Partial build of the tree is implemented '
'only for structured clustering (i.e. with '
'explicit connectivity). The algorithm '
'will build the full tree and only '
'retain the lower branches required '
'for the specified number of clusters',
stacklevel=2)
out = hierarchy.ward(X)
children_ = out[:, :2].astype(np.intp)
if return_distance:
distances = out[:, 2]
return children_, 1, n_samples, None, distances
else:
return children_, 1, n_samples, None
connectivity, n_components = _fix_connectivity(X, connectivity)
if n_clusters is None:
n_nodes = 2 * n_samples - 1
else:
if n_clusters > n_samples:
raise ValueError('Cannot provide more clusters than samples. '
'%i n_clusters was asked, and there are %i samples.'
% (n_clusters, n_samples))
n_nodes = 2 * n_samples - n_clusters
# create inertia matrix
coord_row = []
coord_col = []
A = []
for ind, row in enumerate(connectivity.rows):
A.append(row)
# We keep only the upper triangular for the moments
# Generator expressions are faster than arrays on the following
row = [i for i in row if i < ind]
coord_row.extend(len(row) * [ind, ])
coord_col.extend(row)
coord_row = np.array(coord_row, dtype=np.intp, order='C')
coord_col = np.array(coord_col, dtype=np.intp, order='C')
# build moments as a list
moments_1 = np.zeros(n_nodes, order='C')
moments_1[:n_samples] = 1
moments_2 = np.zeros((n_nodes, n_features), order='C')
moments_2[:n_samples] = X
inertia = np.empty(len(coord_row), dtype=np.float64, order='C')
_hierarchical.compute_ward_dist(moments_1, moments_2, coord_row, coord_col,
inertia)
inertia = list(six.moves.zip(inertia, coord_row, coord_col))
heapify(inertia)
# prepare the main fields
parent = np.arange(n_nodes, dtype=np.intp)
used_node = np.ones(n_nodes, dtype=bool)
children = []
if return_distance:
distances = np.empty(n_nodes - n_samples)
not_visited = np.empty(n_nodes, dtype=np.int8, order='C')
# recursive merge loop
for k in range(n_samples, n_nodes):
# identify the merge
while True:
inert, i, j = heappop(inertia)
if used_node[i] and used_node[j]:
break
parent[i], parent[j] = k, k
children.append((i, j))
used_node[i] = used_node[j] = False
if return_distance: # store inertia value
distances[k - n_samples] = inert
# update the moments
moments_1[k] = moments_1[i] + moments_1[j]
moments_2[k] = moments_2[i] + moments_2[j]
# update the structure matrix A and the inertia matrix
coord_col = []
not_visited.fill(1)
not_visited[k] = 0
_hierarchical._get_parents(A[i], coord_col, parent, not_visited)
_hierarchical._get_parents(A[j], coord_col, parent, not_visited)
# List comprehension is faster than a for loop
[A[l].append(k) for l in coord_col]
A.append(coord_col)
coord_col = np.array(coord_col, dtype=np.intp, order='C')
coord_row = np.empty(coord_col.shape, dtype=np.intp, order='C')
coord_row.fill(k)
n_additions = len(coord_row)
ini = np.empty(n_additions, dtype=np.float64, order='C')
_hierarchical.compute_ward_dist(moments_1, moments_2,
coord_row, coord_col, ini)
# List comprehension is faster than a for loop
[heappush(inertia, (ini[idx], k, coord_col[idx]))
for idx in range(n_additions)]
# Separate leaves in children (empty lists up to now)
n_leaves = n_samples
# sort children to get consistent output with unstructured version
children = [c[::-1] for c in children]
children = np.array(children) # return numpy array for efficient caching
if return_distance:
# 2 is scaling factor to compare w/ unstructured version
distances = np.sqrt(2. * distances)
return children, n_components, n_leaves, parent, distances
else:
return children, n_components, n_leaves, parent
0
Example 27
def anneal(func, x0, args=(), schedule='fast', full_output=0,
T0=None, Tf=1e-12, maxeval=None, maxaccept=None, maxiter=400,
boltzmann=1.0, learn_rate=0.5, feps=1e-6, quench=1.0, m=1.0, n=1.0,
lower=-100, upper=100, dwell=50, cvar=0.05):
"""Minimize a function using simulated annealing.
Schedule is a schedule class implementing the annealing schedule.
Available ones are 'fast', 'cauchy', 'boltzmann'
Parameters
----------
func : callable f(x, *args)
Function to be optimized.
x0 : ndarray
Initial guess.
args : tuple
Extra parameters to `func`.
schedule : base_schedule
Annealing schedule to use (a class).
full_output : bool
Whether to return optional outputs.
T0 : float
Initial Temperature (estimated as 1.2 times the largest
cost-function deviation over random points in the range).
Tf : float
Final goal temperature.
maxeval : int
Maximum function evaluations.
maxaccept : int
Maximum changes to accept.
maxiter : int
Maximum cooling iterations.
learn_rate : float
Scale constant for adjusting guesses.
boltzmann : float
Boltzmann constant in acceptance test
(increase for less stringent test at each temperature).
feps : float
Stopping relative error tolerance for the function value in
last four coolings.
quench, m, n : float
Parameters to alter fast_sa schedule.
lower, upper : float or ndarray
Lower and upper bounds on `x`.
dwell : int
The number of times to search the space at each temperature.
Returns
-------
xmin : ndarray
Point giving smallest value found.
retval : int
Flag indicating stopping condition::
0 : Cooled to global optimum
1 : Cooled to final temperature
2 : Maximum function evaluations
3 : Maximum cooling iterations reached
4 : Maximum accepted query locations reached
Jmin : float
Minimum value of function found.
T : float
Final temperature.
feval : int
Number of function evaluations.
iters : int
Number of cooling iterations.
accept : int
Number of tests accepted.
"""
x0 = asarray(x0)
lower = asarray(lower)
upper = asarray(upper)
schedule = eval(schedule+'_sa()')
# initialize the schedule
schedule.init(dims=shape(x0),func=func,args=args,boltzmann=boltzmann,T0=T0,
learn_rate=learn_rate, lower=lower, upper=upper,
m=m, n=n, quench=quench, dwell=dwell, cvar=cvar)
current_state, last_state, best_state = _state(), _state(), _state()
if T0 is None:
best_state.x = x0
x0 = schedule.getstart_temp(best_state)
else:
best_state.x = None
best_state.cost = 300e8
last_state.x = asarray(x0).copy()
fval = func(x0,*args)
schedule.feval += 1
last_state.cost = fval
if last_state.cost < best_state.cost:
best_state.cost = fval
best_state.x = asarray(x0).copy()
schedule.T = schedule.T0
fqueue = [100, 300, 500, 700]
iters = 0
while 1:
for n in range(dwell):
current_state.x = schedule.update_guess(last_state.x)
current_state.cost = func(current_state.x,*args)
schedule.feval += 1
dE = current_state.cost - last_state.cost
if schedule.accept_test(dE):
last_state.x = current_state.x.copy()
last_state.cost = current_state.cost
if last_state.cost < best_state.cost:
best_state.x = last_state.x.copy()
best_state.cost = last_state.cost
schedule.update_temp()
iters += 1
# Stopping conditions
# 0) last saved values of f from each cooling step
# are all very similar (effectively cooled)
# 1) Tf is set and we are below it
# 2) maxeval is set and we are past it
# 3) maxiter is set and we are past it
# 4) maxaccept is set and we are past it
fqueue.append(squeeze(last_state.cost))
fqueue.pop(0)
af = asarray(fqueue)*1.0
if all(abs((af-af[0])/af[0]) < feps):
retval = 0
if abs(af[-1]-best_state.cost) > feps*10:
retval = 5
print("Warning: Cooled to %f at %s but this is not "
"the smallest point found."
% (squeeze(last_state.cost), squeeze(last_state.x)))
break
if (Tf is not None) and (schedule.T < Tf):
retval = 1
break
if (maxeval is not None) and (schedule.feval > maxeval):
retval = 2
break
if (iters > maxiter):
print("Warning: Maximum number of iterations exceeded.")
retval = 3
break
if (maxaccept is not None) and (schedule.accepted > maxaccept):
retval = 4
break
if full_output:
return best_state.x, best_state.cost, schedule.T, \
schedule.feval, iters, schedule.accepted, retval
else:
return best_state.x, retval
0
Example 28
def __init__(self, R, Q, beta, s_indices=None, a_indices=None):
self._sa_pair = False
self._sparse = False
if sp.issparse(Q):
self.Q = Q.tocsr()
self._sa_pair = True
self._sparse = True
else:
self.Q = np.asarray(Q)
if self.Q.ndim == 2:
self._sa_pair = True
elif self.Q.ndim != 3:
raise ValueError('Q must be 2- or 3-dimensional')
self.R = np.asarray(R)
if not (self.R.ndim in [1, 2]):
raise ValueError('R must be 1- or 2-dimensional')
msg_dimension = 'dimensions of R and Q must be either 1 and 2, ' \
'or 2 and 3'
msg_shape = 'shapes of R and Q must be either (n, m) and (n, m, n), ' \
'or (L,) and (L, n)'
if self._sa_pair:
self.num_sa_pairs, self.num_states = self.Q.shape
if self.R.ndim != 1:
raise ValueError(msg_dimension)
if self.R.shape != (self.num_sa_pairs,):
raise ValueError(msg_shape)
if s_indices is None:
raise ValueError('s_indices must be supplied')
if a_indices is None:
raise ValueError('a_indices must be supplied')
if not (len(s_indices) == self.num_sa_pairs and
len(a_indices) == self.num_sa_pairs):
raise ValueError(
'length of s_indices and a_indices must be equal to '
'the number of state-action pairs'
)
self.s_indices = np.asarray(s_indices)
self.a_indices = np.asarray(a_indices)
if _has_sorted_sa_indices(self.s_indices, self.a_indices):
a_indptr = np.empty(self.num_states+1, dtype=int)
_generate_a_indptr(self.num_states, self.s_indices,
out=a_indptr)
self.a_indptr = a_indptr
else:
# Sort indices and elements of R and Q
sa_ptrs = sp.coo_matrix(
(np.arange(self.num_sa_pairs), (s_indices, a_indices))
).tocsr()
sa_ptrs.sort_indices()
self.a_indices = sa_ptrs.indices
self.a_indptr = sa_ptrs.indptr
self.R = self.R[sa_ptrs.data]
self.Q = self.Q[sa_ptrs.data]
_s_indices = np.empty(self.num_sa_pairs,
dtype=self.a_indices.dtype)
for i in range(self.num_states):
for j in range(self.a_indptr[i], self.a_indptr[i+1]):
_s_indices[j] = i
self.s_indices = _s_indices
# Define state-wise maximization
def s_wise_max(vals, out=None, out_argmax=None):
"""
Return the vector max_a vals(s, a), where vals is represented
by a 1-dimensional ndarray of shape (self.num_sa_pairs,).
out and out_argmax must be of length self.num_states; dtype of
out_argmax must be int.
"""
if out is None:
out = np.empty(self.num_states)
if out_argmax is None:
_s_wise_max(self.a_indices, self.a_indptr, vals,
out_max=out)
else:
_s_wise_max_argmax(self.a_indices, self.a_indptr, vals,
out_max=out, out_argmax=out_argmax)
return out
self.s_wise_max = s_wise_max
else: # Not self._sa_pair
if self.R.ndim != 2:
raise ValueError(msg_dimension)
n, m = self.R.shape
if self.Q.shape != (n, m, n):
raise ValueError(msg_shape)
self.num_states = n
self.s_indices, self.a_indices = None, None
self.num_sa_pairs = (self.R > -np.inf).sum()
# Define state-wise maximization
def s_wise_max(vals, out=None, out_argmax=None):
"""
Return the vector max_a vals(s, a), where vals is represented
by a 2-dimensional ndarray of shape (n, m). Stored in out,
which must be of length self.num_states.
out and out_argmax must be of length self.num_states; dtype of
out_argmax must be int.
"""
if out is None:
out = np.empty(self.num_states)
if out_argmax is None:
vals.max(axis=1, out=out)
else:
vals.argmax(axis=1, out=out_argmax)
out[:] = vals[np.arange(self.num_states), out_argmax]
return out
self.s_wise_max = s_wise_max
# Check that for every state, at least one action is feasible
self._check_action_feasibility()
if not (0 <= beta <= 1):
raise ValueError('beta must be in [0, 1]')
if beta == 1:
msg = 'infinite horizon solution methods are disabled with beta=1'
warnings.warn(msg)
self._error_msg_no_discounting = 'method invalid for beta=1'
self.beta = beta
self.epsilon = 1e-3
self.max_iter = 250
# Linear equation solver to be used in evaluate_policy
if self._sparse:
self._lineq_solve = sp.linalg.spsolve
self._I = sp.identity(self.num_states, format='csr')
else:
self._lineq_solve = np.linalg.solve
self._I = np.identity(self.num_states)
0
Example 29
Project: scikit-learn Source File: hierarchical.py
def linkage_tree(X, connectivity=None, n_components=None,
n_clusters=None, linkage='complete', affinity="euclidean",
return_distance=False):
"""Linkage agglomerative clustering based on a Feature matrix.
The inertia matrix uses a Heapq-based representation.
This is the structured version, that takes into account some topological
structure between samples.
Read more in the :ref:`User Guide <hierarchical_clustering>`.
Parameters
----------
X : array, shape (n_samples, n_features)
feature matrix representing n_samples samples to be clustered
connectivity : sparse matrix (optional).
connectivity matrix. Defines for each sample the neighboring samples
following a given structure of the data. The matrix is assumed to
be symmetric and only the upper triangular half is used.
Default is None, i.e, the Ward algorithm is unstructured.
n_clusters : int (optional)
Stop early the construction of the tree at n_clusters. This is
useful to decrease computation time if the number of clusters is
not small compared to the number of samples. In this case, the
complete tree is not computed, thus the 'children' output is of
limited use, and the 'parents' output should rather be used.
This option is valid only when specifying a connectivity matrix.
linkage : {"average", "complete"}, optional, default: "complete"
Which linkage criteria to use. The linkage criterion determines which
distance to use between sets of observation.
- average uses the average of the distances of each observation of
the two sets
- complete or maximum linkage uses the maximum distances between
all observations of the two sets.
affinity : string or callable, optional, default: "euclidean".
which metric to use. Can be "euclidean", "manhattan", or any
distance know to paired distance (see metric.pairwise)
return_distance : bool, default False
whether or not to return the distances between the clusters.
Returns
-------
children : 2D array, shape (n_nodes-1, 2)
The children of each non-leaf node. Values less than `n_samples`
correspond to leaves of the tree which are the original samples.
A node `i` greater than or equal to `n_samples` is a non-leaf
node and has children `children_[i - n_samples]`. Alternatively
at the i-th iteration, children[i][0] and children[i][1]
are merged to form node `n_samples + i`
n_components : int
The number of connected components in the graph.
n_leaves : int
The number of leaves in the tree.
parents : 1D array, shape (n_nodes, ) or None
The parent of each node. Only returned when a connectivity matrix
is specified, elsewhere 'None' is returned.
distances : ndarray, shape (n_nodes-1,)
Returned when return_distance is set to True.
distances[i] refers to the distance between children[i][0] and
children[i][1] when they are merged.
See also
--------
ward_tree : hierarchical clustering with ward linkage
"""
X = np.asarray(X)
if X.ndim == 1:
X = np.reshape(X, (-1, 1))
n_samples, n_features = X.shape
linkage_choices = {'complete': _hierarchical.max_merge,
'average': _hierarchical.average_merge}
try:
join_func = linkage_choices[linkage]
except KeyError:
raise ValueError(
'Unknown linkage option, linkage should be one '
'of %s, but %s was given' % (linkage_choices.keys(), linkage))
if connectivity is None:
from scipy.cluster import hierarchy # imports PIL
if n_clusters is not None:
warnings.warn('Partial build of the tree is implemented '
'only for structured clustering (i.e. with '
'explicit connectivity). The algorithm '
'will build the full tree and only '
'retain the lower branches required '
'for the specified number of clusters',
stacklevel=2)
if affinity == 'precomputed':
# for the linkage function of hierarchy to work on precomputed
# data, provide as first argument an ndarray of the shape returned
# by pdist: it is a flat array containing the upper triangular of
# the distance matrix.
i, j = np.triu_indices(X.shape[0], k=1)
X = X[i, j]
elif affinity == 'l2':
# Translate to something understood by scipy
affinity = 'euclidean'
elif affinity in ('l1', 'manhattan'):
affinity = 'cityblock'
elif callable(affinity):
X = affinity(X)
i, j = np.triu_indices(X.shape[0], k=1)
X = X[i, j]
out = hierarchy.linkage(X, method=linkage, metric=affinity)
children_ = out[:, :2].astype(np.int)
if return_distance:
distances = out[:, 2]
return children_, 1, n_samples, None, distances
return children_, 1, n_samples, None
connectivity, n_components = _fix_connectivity(X, connectivity)
connectivity = connectivity.tocoo()
# Put the diagonal to zero
diag_mask = (connectivity.row != connectivity.col)
connectivity.row = connectivity.row[diag_mask]
connectivity.col = connectivity.col[diag_mask]
connectivity.data = connectivity.data[diag_mask]
del diag_mask
if affinity == 'precomputed':
distances = X[connectivity.row, connectivity.col]
else:
# FIXME We compute all the distances, while we could have only computed
# the "interesting" distances
distances = paired_distances(X[connectivity.row],
X[connectivity.col],
metric=affinity)
connectivity.data = distances
if n_clusters is None:
n_nodes = 2 * n_samples - 1
else:
assert n_clusters <= n_samples
n_nodes = 2 * n_samples - n_clusters
if return_distance:
distances = np.empty(n_nodes - n_samples)
# create inertia heap and connection matrix
A = np.empty(n_nodes, dtype=object)
inertia = list()
# LIL seems to the best format to access the rows quickly,
# without the numpy overhead of slicing CSR indices and data.
connectivity = connectivity.tolil()
# We are storing the graph in a list of IntFloatDict
for ind, (data, row) in enumerate(zip(connectivity.data,
connectivity.rows)):
A[ind] = IntFloatDict(np.asarray(row, dtype=np.intp),
np.asarray(data, dtype=np.float64))
# We keep only the upper triangular for the heap
# Generator expressions are faster than arrays on the following
inertia.extend(_hierarchical.WeightedEdge(d, ind, r)
for r, d in zip(row, data) if r < ind)
del connectivity
heapify(inertia)
# prepare the main fields
parent = np.arange(n_nodes, dtype=np.intp)
used_node = np.ones(n_nodes, dtype=np.intp)
children = []
# recursive merge loop
for k in xrange(n_samples, n_nodes):
# identify the merge
while True:
edge = heappop(inertia)
if used_node[edge.a] and used_node[edge.b]:
break
i = edge.a
j = edge.b
if return_distance:
# store distances
distances[k - n_samples] = edge.weight
parent[i] = parent[j] = k
children.append((i, j))
# Keep track of the number of elements per cluster
n_i = used_node[i]
n_j = used_node[j]
used_node[k] = n_i + n_j
used_node[i] = used_node[j] = False
# update the structure matrix A and the inertia matrix
# a clever 'min', or 'max' operation between A[i] and A[j]
coord_col = join_func(A[i], A[j], used_node, n_i, n_j)
for l, d in coord_col:
A[l].append(k, d)
# Here we use the information from coord_col (containing the
# distances) to update the heap
heappush(inertia, _hierarchical.WeightedEdge(d, k, l))
A[k] = coord_col
# Clear A[i] and A[j] to save memory
A[i] = A[j] = 0
# Separate leaves in children (empty lists up to now)
n_leaves = n_samples
# # return numpy array for efficient caching
children = np.array(children)[:, ::-1]
if return_distance:
return children, n_components, n_leaves, parent, distances
return children, n_components, n_leaves, parent
0
Example 30
Project: scikit-learn Source File: omp.py
def orthogonal_mp_gram(Gram, Xy, n_nonzero_coefs=None, tol=None,
norms_squared=None, copy_Gram=True,
copy_Xy=True, return_path=False,
return_n_iter=False):
"""Gram Orthogonal Matching Pursuit (OMP)
Solves n_targets Orthogonal Matching Pursuit problems using only
the Gram matrix X.T * X and the product X.T * y.
Read more in the :ref:`User Guide <omp>`.
Parameters
----------
Gram : array, shape (n_features, n_features)
Gram matrix of the input data: X.T * X
Xy : array, shape (n_features,) or (n_features, n_targets)
Input targets multiplied by X: X.T * y
n_nonzero_coefs : int
Desired number of non-zero entries in the solution. If None (by
default) this value is set to 10% of n_features.
tol : float
Maximum norm of the residual. If not None, overrides n_nonzero_coefs.
norms_squared : array-like, shape (n_targets,)
Squared L2 norms of the lines of y. Required if tol is not None.
copy_Gram : bool, optional
Whether the gram matrix must be copied by the algorithm. A false
value is only helpful if it is already Fortran-ordered, otherwise a
copy is made anyway.
copy_Xy : bool, optional
Whether the covariance vector Xy must be copied by the algorithm.
If False, it may be overwritten.
return_path : bool, optional. Default: False
Whether to return every value of the nonzero coefficients along the
forward path. Useful for cross-validation.
return_n_iter : bool, optional default False
Whether or not to return the number of iterations.
Returns
-------
coef : array, shape (n_features,) or (n_features, n_targets)
Coefficients of the OMP solution. If `return_path=True`, this contains
the whole coefficient path. In this case its shape is
(n_features, n_features) or (n_features, n_targets, n_features) and
iterating over the last axis yields coefficients in increasing order
of active features.
n_iters : array-like or int
Number of active features across every target. Returned only if
`return_n_iter` is set to True.
See also
--------
OrthogonalMatchingPursuit
orthogonal_mp
lars_path
decomposition.sparse_encode
Notes
-----
Orthogonal matching pursuit was introduced in G. Mallat, Z. Zhang,
Matching pursuits with time-frequency dictionaries, IEEE Transactions on
Signal Processing, Vol. 41, No. 12. (December 1993), pp. 3397-3415.
(http://blanche.polytechnique.fr/~mallat/papiers/MallatPursuit93.pdf)
This implementation is based on Rubinstein, R., Zibulevsky, M. and Elad,
M., Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal
Matching Pursuit Technical Report - CS Technion, April 2008.
http://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf
"""
Gram = check_array(Gram, order='F', copy=copy_Gram)
Xy = np.asarray(Xy)
if Xy.ndim > 1 and Xy.shape[1] > 1:
# or subsequent target will be affected
copy_Gram = True
if Xy.ndim == 1:
Xy = Xy[:, np.newaxis]
if tol is not None:
norms_squared = [norms_squared]
if n_nonzero_coefs is None and tol is None:
n_nonzero_coefs = int(0.1 * len(Gram))
if tol is not None and norms_squared is None:
raise ValueError('Gram OMP needs the precomputed norms in order '
'to evaluate the error sum of squares.')
if tol is not None and tol < 0:
raise ValueError("Epsilon cannot be negative")
if tol is None and n_nonzero_coefs <= 0:
raise ValueError("The number of atoms must be positive")
if tol is None and n_nonzero_coefs > len(Gram):
raise ValueError("The number of atoms cannot be more than the number "
"of features")
if return_path:
coef = np.zeros((len(Gram), Xy.shape[1], len(Gram)))
else:
coef = np.zeros((len(Gram), Xy.shape[1]))
n_iters = []
for k in range(Xy.shape[1]):
out = _gram_omp(
Gram, Xy[:, k], n_nonzero_coefs,
norms_squared[k] if tol is not None else None, tol,
copy_Gram=copy_Gram, copy_Xy=copy_Xy,
return_path=return_path)
if return_path:
_, idx, coefs, n_iter = out
coef = coef[:, :, :len(idx)]
for n_active, x in enumerate(coefs.T):
coef[idx[:n_active + 1], k, n_active] = x[:n_active + 1]
else:
x, idx, n_iter = out
coef[idx, k] = x
n_iters.append(n_iter)
if Xy.shape[1] == 1:
n_iters = n_iters[0]
if return_n_iter:
return np.squeeze(coef), n_iters
else:
return np.squeeze(coef)
0
Example 31
Project: pycwt Source File: wavelet.py
def xwt(signal, signal2, dt, dj=1/12, s0=-1, J=-1, significance_level=0.95,
wavelet='morlet', normalize=True):
"""
Calculate the cross wavelet transform (XWT). The XWT finds regions in time
frequency space where the time series show high common power. Torrence and
Compo (1998) state that the percent point function -- PPF (inverse of the
cuemulative distribution function) of a chi-square distribution at 95%
confidence and two degrees of freedom is Z2(95%)=3.999. However, calculating
the PPF using chi2.ppf gives Z2(95%)=5.991. To ensure similar significance
intervals as in Grinsted et al. (2004), one has to use confidence of 86.46%.
Parameters
----------
signal, signal2 : numpy.ndarray, list
Input signal array to calculate cross wavelet transform.
dt : float
Sample spacing.
dj : float, optional
Spacing between discrete scales. Default value is 0.25.
Smaller values will result in better scale resolution, but
slower calculation and plot.
s0 : float, optional
Smallest scale of the wavelet. Default value is 2*dt.
J : float, optional
Number of scales less one. Scales range from s0 up to
s0 * 2**(J * dj), which gives a total of (J + 1) scales.
Default is J = (log2(N*dt/so))/dj.
wavelet : instance of a wavelet class, optional
Mother wavelet class. Default is Morlet wavelet.
significance_level : float, optional
Significance level to use. Default is 0.95.
normalize : bool, optional
If set to true, normalizes CWT by the standard deviation of
the signals.
Returns
-------
xwt (array like):
Cross wavelet transform according to the selected mother wavelet.
x (array like):
Intersected independent variable.
coi (array like):
Cone of influence, which is a vector of N points containing
the maximum Fourier period of useful information at that
particular time. Periods greater than those are subject to
edge effects.
freqs (array like):
Vector of Fourier equivalent frequencies (in 1 / time units)
that correspond to the wavelet scales.
signif (array like):
Significance levels as a function of scale.
"""
if isinstance(wavelet, unicode) or isinstance(wavelet, str):
wavelet = mothers[wavelet]()
# Defines some parameters like length of both time-series, time step
# and calculates the standard deviation for normalization and statistical
# significance tests.
signal = np.asarray(signal)
signal2 = np.asarray(signal2)
if normalize:
std1 = signal.std()
std2 = signal2.std()
else:
std1 = std2 = 1
# Calculates the CWT of the time-series making sure the same parameters
# are used in both calculations.
cwt_kwargs = dict(dt=dt, dj=dj, s0=s0, J=J, wavelet=wavelet)
W1, sj, freq, coi, _, _ = cwt(signal/std1, **cwt_kwargs)
W2, sj, freq, coi, _, _ = cwt(signal2/std2, **cwt_kwargs)
# Now the cross correlation of y1 and y2
W12 = W1 * W2.conj()
# And the significance tests. Note that the confidence level is calculated
# using the percent point function (PPF) of the chi-squared cuemulative
# distribution function (CDF) instead of using Z1(95%) = 2.182 and
# Z2(95%)=3.999 as suggested by Torrence & Compo (1998) and Grinsted et
# al. (2004). If the CWT has been normalized, then std1 and std2 should
# be reset to unity, otherwise the standard deviation of both series have
# to be calculated.
if not normalize:
std1 = signal.std()
std2 = signal2.std()
else:
std1 = std2 = 1.
a1, _, _ = ar1(signal)
a2, _, _ = ar1(signal2)
Pk1 = ar1_spectrum(freq * dt, a1)
Pk2 = ar1_spectrum(freq * dt, a2)
dof = wavelet.dofmin
PPF = chi2.ppf(significance_level, dof)
signif = (std1 * std2 * (Pk1 * Pk2) ** 0.5 * PPF / dof)
# The resuts:
return W12, coi, freq, signif
0
Example 32
Project: scikit-learn Source File: label.py
def label_binarize(y, classes, neg_label=0, pos_label=1, sparse_output=False):
"""Binarize labels in a one-vs-all fashion
Several regression and binary classification algorithms are
available in the scikit. A simple way to extend these algorithms
to the multi-class classification case is to use the so-called
one-vs-all scheme.
This function makes it possible to compute this transformation for a
fixed set of class labels known ahead of time.
Parameters
----------
y : array-like
Sequence of integer labels or multilabel data to encode.
classes : array-like of shape [n_classes]
Uniquely holds the label for each class.
neg_label : int (default: 0)
Value with which negative labels must be encoded.
pos_label : int (default: 1)
Value with which positive labels must be encoded.
sparse_output : boolean (default: False),
Set to true if output binary array is desired in CSR sparse format
Returns
-------
Y : numpy array or CSR matrix of shape [n_samples, n_classes]
Shape will be [n_samples, 1] for binary problems.
Examples
--------
>>> from sklearn.preprocessing import label_binarize
>>> label_binarize([1, 6], classes=[1, 2, 4, 6])
array([[1, 0, 0, 0],
[0, 0, 0, 1]])
The class ordering is preserved:
>>> label_binarize([1, 6], classes=[1, 6, 4, 2])
array([[1, 0, 0, 0],
[0, 1, 0, 0]])
Binary targets transform to a column vector
>>> label_binarize(['yes', 'no', 'no', 'yes'], classes=['no', 'yes'])
array([[1],
[0],
[0],
[1]])
See also
--------
LabelBinarizer : class used to wrap the functionality of label_binarize and
allow for fitting to classes independently of the transform operation
"""
if not isinstance(y, list):
# XXX Workaround that will be removed when list of list format is
# dropped
y = check_array(y, accept_sparse='csr', ensure_2d=False, dtype=None)
else:
if _num_samples(y) == 0:
raise ValueError('y has 0 samples: %r' % y)
if neg_label >= pos_label:
raise ValueError("neg_label={0} must be strictly less than "
"pos_label={1}.".format(neg_label, pos_label))
if (sparse_output and (pos_label == 0 or neg_label != 0)):
raise ValueError("Sparse binarization is only supported with non "
"zero pos_label and zero neg_label, got "
"pos_label={0} and neg_label={1}"
"".format(pos_label, neg_label))
# To account for pos_label == 0 in the dense case
pos_switch = pos_label == 0
if pos_switch:
pos_label = -neg_label
y_type = type_of_target(y)
if 'multioutput' in y_type:
raise ValueError("Multioutput target data is not supported with label "
"binarization")
if y_type == 'unknown':
raise ValueError("The type of target data is not known")
n_samples = y.shape[0] if sp.issparse(y) else len(y)
n_classes = len(classes)
classes = np.asarray(classes)
if y_type == "binary":
if n_classes == 1:
if sparse_output:
return sp.csr_matrix((n_samples, 1), dtype=int)
else:
Y = np.zeros((len(y), 1), dtype=np.int)
Y += neg_label
return Y
elif len(classes) >= 3:
y_type = "multiclass"
sorted_class = np.sort(classes)
if (y_type == "multilabel-indicator" and classes.size != y.shape[1]):
raise ValueError("classes {0} missmatch with the labels {1}"
"found in the data".format(classes, unique_labels(y)))
if y_type in ("binary", "multiclass"):
y = column_or_1d(y)
# pick out the known labels from y
y_in_classes = in1d(y, classes)
y_seen = y[y_in_classes]
indices = np.searchsorted(sorted_class, y_seen)
indptr = np.hstack((0, np.cuemsum(y_in_classes)))
data = np.empty_like(indices)
data.fill(pos_label)
Y = sp.csr_matrix((data, indices, indptr),
shape=(n_samples, n_classes))
elif y_type == "multilabel-indicator":
Y = sp.csr_matrix(y)
if pos_label != 1:
data = np.empty_like(Y.data)
data.fill(pos_label)
Y.data = data
else:
raise ValueError("%s target data is not supported with label "
"binarization" % y_type)
if not sparse_output:
Y = Y.toarray()
Y = astype(Y, int, copy=False)
if neg_label != 0:
Y[Y == 0] = neg_label
if pos_switch:
Y[Y == pos_label] = 0
else:
Y.data = astype(Y.data, int, copy=False)
# preserve label ordering
if np.any(classes != sorted_class):
indices = np.searchsorted(sorted_class, classes)
Y = Y[:, indices]
if y_type == "binary":
if sparse_output:
Y = Y.getcol(-1)
else:
Y = Y[:, -1].reshape((-1, 1))
return Y
0
Example 33
def load_file(self, fid):
"""
Load the touchstone file into the interal data structures
"""
filename=self.filename
extention = filename.split('.')[-1].lower()
#self.rank = {'s1p':1, 's2p':2, 's3p':3, 's4p':4}.get(extention, None)
try:
self.rank = int(extention[1:-1])
except (ValueError):
raise (ValueError("filename does not have a s-parameter extension. It has [%s] instead. please, correct the extension to of form: 'sNp', where N is any integer." %(extention)))
linenr = 0
values = []
while (1):
linenr +=1
line = fid.readline()
if not line:
break
# store comments if they precede the option line
line = line.split('!',1)
if len(line) == 2:
if not self.parameter:
if self.comments == None:
self.comments = ''
self.comments = self.comments + line[1]
elif line[1].startswith(' Port['):
try:
port_string, name = line[1].split('=', 1) #throws ValueError on unpack
name = name.strip()
garbage, index = port_string.strip().split('[', 1) #throws ValueError on unpack
index = int(index.rstrip(']')) #throws ValueError on not int-able
if index > self.rank or index <= 0:
print("Port name {0} provided for port number {1} but that's out of range for a file with extension s{2}p".format(name, index, self.rank))
else:
if self.port_names is None: #Initialize the array at the last minute
self.port_names = [''] * self.rank
self.port_names[index - 1] = name
except ValueError as e:
print("Error extracting port names from line: {0}".format(line))
# remove the comment (if any) so rest of line can be processed.
# touchstone files are case-insensitive
line = line[0].strip().lower()
# skip the line if there was nothing except comments
if len(line) == 0:
continue
# grab the [version] string
if line[:9] == '[version]':
self.version = line.split()[1]
continue
# grab the [reference] string
if line[:11] == '[reference]':
self.reference = [ float(r) for r in line.split()[2:] ]
continue
# the option line
if line[0] == '#':
toks = line[1:].strip().split()
# fill the option line with the missing defaults
toks.extend(['ghz', 's', 'ma', 'r', '50'][len(toks):])
self.frequency_unit = toks[0]
self.parameter = toks[1]
self.format = toks[2]
self.resistance = toks[4]
if self.frequency_unit not in ['hz', 'khz', 'mhz', 'ghz']:
print('ERROR: illegal frequency_unit [%s]', self.frequency_unit)
# TODO: Raise
if self.parameter not in 'syzgh':
print('ERROR: illegal parameter value [%s]', self.parameter)
# TODO: Raise
if self.format not in ['ma', 'db', 'ri']:
print('ERROR: illegal format value [%s]', self.format)
# TODO: Raise
continue
# collect all values without taking care of there meaning
# we're seperating them later
values.extend([ float(v) for v in line.split() ])
# let's do some post-processing to the read values
# for s2p parameters there may be noise parameters in the value list
values = numpy.asarray(values)
if self.rank == 2:
# the first frequency value that is smaller than the last one is the
# indicator for the start of the noise section
# each set of the s-parameter section is 9 values long
pos = numpy.where(numpy.sign(numpy.diff(values[::9])) == -1)
if len(pos[0]) != 0:
# we have noise data in the values
pos = pos[0][0] + 1 # add 1 because diff reduced it by 1
noise_values = values[pos*9:]
values = values[:pos*9]
self.noise = noise_values.reshape((-1,5))
if len(values)%(1+2*(self.rank)**2) != 0 :
# incomplete data line / matrix found
raise AssertionError
# reshape the values to match the rank
self.sparameters = values.reshape((-1, 1 + 2*self.rank**2))
# multiplier from the frequency unit
self.frequency_mult = {'hz':1.0, 'khz':1e3,
'mhz':1e6, 'ghz':1e9}.get(self.frequency_unit)
# set the reference to the resistance value if no [reference] is provided
if not self.reference:
self.reference = [self.resistance] * self.rank
0
Example 34
def eye(timebase, is_alive_flag, ipc_pub_url, ipc_sub_url,ipc_push_url, user_dir, version, eye_id,overwrite_cap_settings=None):
"""reads eye video and detects the pupil.
Creates a window, gl context.
Grabs images from a capture.
Streams Pupil coordinates.
Reacts to notifications:
``set_detection_mapping_mode``: Sets detection method
``eye_process.should_stop``: Stops the eye process
``recording.started``: Starts recording eye video
``recording.stopped``: Stops recording eye video
``frame_publishing.started``: Starts frame publishing
``frame_publishing.stopped``: Stops frame publishing
Emits notifications:
``eye_process.started``: Eye process started
``eye_process.stopped``: Eye process stopped
Emits data:
``pupil.<eye id>``: Pupil data for eye with id ``<eye id>``
``frame.eye.<eye id>``: Eye frames with id ``<eye id>``
"""
# We deferr the imports becasue of multiprocessing.
# Otherwise the world process each process also loads the other imports.
import zmq
import zmq_tools
zmq_ctx = zmq.Context()
ipc_socket = zmq_tools.Msg_Dispatcher(zmq_ctx,ipc_push_url)
pupil_socket = zmq_tools.Msg_Streamer(zmq_ctx,ipc_pub_url)
notify_sub = zmq_tools.Msg_Receiver(zmq_ctx,ipc_sub_url,topics=("notify",))
with Is_Alive_Manager(is_alive_flag,ipc_socket,eye_id):
#logging setup
import logging
logging.getLogger("OpenGL").setLevel(logging.ERROR)
logger = logging.getLogger()
logger.handlers = []
logger.addHandler(zmq_tools.ZMQ_handler(zmq_ctx,ipc_push_url))
# create logger for the context of this function
logger = logging.getLogger(__name__)
#general imports
import numpy as np
import cv2
#display
import glfw
from pyglui import ui,graph,cygl
from pyglui.cygl.utils import draw_points, RGBA, draw_polyline, Named_Texture, Sphere
import OpenGL.GL as gl
from gl_utils import basic_gl_setup,adjust_gl_view, clear_gl_screen ,make_coord_system_pixel_based,make_coord_system_norm_based, make_coord_system_eye_camera_based,is_window_visible
from ui_roi import UIRoi
#monitoring
import psutil
import math
# helpers/utils
from uvc import get_time_monotonic, StreamError
from file_methods import Persistent_Dict
from version_utils import VersionFormat
from methods import normalize, denormalize, Roi, timer
from av_writer import JPEG_Writer,AV_Writer
from video_capture import InitialisationError,StreamError, Fake_Source,EndofVideoFileError, source_classes, manager_classes
source_by_name = {src.class_name():src for src in source_classes}
# Pupil detectors
from pupil_detectors import Detector_2D, Detector_3D
pupil_detectors = {Detector_2D.__name__:Detector_2D,Detector_3D.__name__:Detector_3D}
#UI Platform tweaks
if platform.system() == 'Linux':
scroll_factor = 10.0
window_position_default = (600,300*eye_id)
elif platform.system() == 'Windows':
scroll_factor = 1.0
window_position_default = (600,31+300*eye_id)
else:
scroll_factor = 1.0
window_position_default = (600,300*eye_id)
#g_pool holds variables for this process
g_pool = Global_Container()
# make some constants avaiable
g_pool.user_dir = user_dir
g_pool.version = version
g_pool.app = 'capture'
g_pool.timebase = timebase
g_pool.ipc_pub = ipc_socket
def get_timestamp():
return get_time_monotonic()-g_pool.timebase.value
g_pool.get_timestamp = get_timestamp
g_pool.get_now = get_time_monotonic
# Callback functions
def on_resize(window,w, h):
if is_window_visible(window):
active_window = glfw.glfwGetCurrentContext()
glfw.glfwMakeContextCurrent(window)
g_pool.gui.update_window(w,h)
graph.adjust_size(w,h)
adjust_gl_view(w,h)
glfw.glfwMakeContextCurrent(active_window)
def on_key(window, key, scancode, action, mods):
g_pool.gui.update_key(key,scancode,action,mods)
def on_char(window,char):
g_pool.gui.update_char(char)
def on_iconify(window,iconified):
g_pool.iconified = iconified
def on_button(window,button, action, mods):
if g_pool.display_mode == 'roi':
if action == glfw.GLFW_RELEASE and g_pool.u_r.active_edit_pt:
g_pool.u_r.active_edit_pt = False
return # if the roi interacts we dont what the gui to interact as well
elif action == glfw.GLFW_PRESS:
pos = glfw.glfwGetCursorPos(window)
pos = normalize(pos,glfw.glfwGetWindowSize(main_window))
if g_pool.flip:
pos = 1-pos[0],1-pos[1]
pos = denormalize(pos,(frame.width,frame.height)) # Position in img pixels
if g_pool.u_r.mouse_over_edit_pt(pos,g_pool.u_r.handle_size+40,g_pool.u_r.handle_size+40):
return # if the roi interacts we dont what the gui to interact as well
g_pool.gui.update_button(button,action,mods)
def on_pos(window,x, y):
hdpi_factor = float(glfw.glfwGetFramebufferSize(window)[0]/glfw.glfwGetWindowSize(window)[0])
g_pool.gui.update_mouse(x*hdpi_factor,y*hdpi_factor)
if g_pool.u_r.active_edit_pt:
pos = normalize((x,y),glfw.glfwGetWindowSize(main_window))
if g_pool.flip:
pos = 1-pos[0],1-pos[1]
pos = denormalize(pos,(frame.width,frame.height) )
g_pool.u_r.move_vertex(g_pool.u_r.active_pt_idx,pos)
def on_scroll(window,x,y):
g_pool.gui.update_scroll(x,y*scroll_factor)
g_pool.on_frame_size_change = lambda new_size: None
# load session persistent settings
session_settings = Persistent_Dict(os.path.join(g_pool.user_dir,'user_settings_eye%s'%eye_id))
if session_settings.get("version",VersionFormat('0.0')) < g_pool.version:
logger.info("Session setting are from older version of this app. I will not use those.")
session_settings.clear()
capture_manager_settings = session_settings.get(
'capture_manager_settings', ('UVC_Manager',{}))
if eye_id == 0:
cap_src = ["Pupil Cam1 ID0","HD-6000","Integrated Camera","HD USB Camera","USB 2.0 Camera"]
else:
cap_src = ["Pupil Cam1 ID1","HD-6000","Integrated Camera"]
# Initialize capture
default_settings = {
'source_class_name': 'UVC_Source',
'preferred_names' : cap_src,
'frame_size': (640,480),
'frame_rate': 60
}
settings = overwrite_cap_settings or session_settings.get('capture_settings', default_settings)
try:
cap = source_by_name[settings['source_class_name']](g_pool, **settings)
except (KeyError,InitialisationError) as e:
if isinstance(e,KeyError):
logger.warning('Incompatible capture setting encountered. Falling back to fake source.')
cap = Fake_Source(g_pool, **settings)
g_pool.iconified = False
g_pool.capture = cap
g_pool.capture_manager = None
g_pool.flip = session_settings.get('flip',False)
g_pool.display_mode = session_settings.get('display_mode','camera_image')
g_pool.display_mode_info_text = {'camera_image': "Raw eye camera image. This uses the least amount of CPU power",
'roi': "Click and drag on the blue circles to adjust the region of interest. The region should be as small as possible, but large enough to capture all pupil movements.",
'algorithm': "Algorithm display mode overlays a visualization of the pupil detection parameters on top of the eye video. Adjust parameters within the Pupil Detection menu below."}
g_pool.u_r = UIRoi((g_pool.capture.frame_size[1],g_pool.capture.frame_size[0]))
roi_user_settings = session_settings.get('roi')
if roi_user_settings and roi_user_settings[-1] == g_pool.u_r.get()[-1]:
g_pool.u_r.set(roi_user_settings)
def on_frame_size_change(new_size):
g_pool.u_r = UIRoi((new_size[1],new_size[0]))
g_pool.on_frame_size_change = on_frame_size_change
writer = None
pupil_detector_settings = session_settings.get('pupil_detector_settings',None)
last_pupil_detector = pupil_detectors[session_settings.get('last_pupil_detector',Detector_2D.__name__)]
g_pool.pupil_detector = last_pupil_detector(g_pool,pupil_detector_settings)
# UI callback functions
def set_scale(new_scale):
g_pool.gui.scale = new_scale
g_pool.gui.collect_menus()
def set_display_mode_info(val):
g_pool.display_mode = val
g_pool.display_mode_info.text = g_pool.display_mode_info_text[val]
def set_detector(new_detector):
g_pool.pupil_detector.cleanup()
g_pool.pupil_detector = new_detector(g_pool)
g_pool.pupil_detector.init_gui(g_pool.sidebar)
# Initialize glfw
glfw.glfwInit()
title = "eye %s"%eye_id
width,height = session_settings.get('window_size',g_pool.capture.frame_size)
main_window = glfw.glfwCreateWindow(width,height, title, None, None)
window_pos = session_settings.get('window_position',window_position_default)
glfw.glfwSetWindowPos(main_window,window_pos[0],window_pos[1])
glfw.glfwMakeContextCurrent(main_window)
cygl.utils.init()
# gl_state settings
basic_gl_setup()
g_pool.image_tex = Named_Texture()
glfw.glfwSwapInterval(0)
#setup GUI
g_pool.gui = ui.UI()
g_pool.gui.scale = session_settings.get('gui_scale',1)
g_pool.sidebar = ui.Scrolling_Menu("Settings",pos=(-300,0),size=(0,0),header_pos='left')
general_settings = ui.Growing_Menu('General')
general_settings.append(ui.Slider('scale',g_pool.gui, setter=set_scale,step = .05,min=1.,max=2.5,label='Interface Size'))
general_settings.append(ui.Button('Reset window size',lambda: glfw.glfwSetWindowSize(main_window,frame.width,frame.height)) )
general_settings.append(ui.Switch('flip',g_pool,label='Flip image display'))
general_settings.append(ui.Selector('display_mode',g_pool,setter=set_display_mode_info,selection=['camera_image','roi','algorithm'], labels=['Camera Image', 'ROI', 'Algorithm'], label="Mode") )
g_pool.display_mode_info = ui.Info_Text(g_pool.display_mode_info_text[g_pool.display_mode])
general_settings.append(g_pool.display_mode_info)
g_pool.gui.append(g_pool.sidebar)
detector_selector = ui.Selector('pupil_detector',getter = lambda: g_pool.pupil_detector.__class__ ,setter=set_detector,selection=[Detector_2D, Detector_3D],labels=['C++ 2d detector', 'C++ 3d detector'], label="Detection method")
general_settings.append(detector_selector)
g_pool.capture_selector_menu = ui.Growing_Menu('Capture Selection')
g_pool.capture_source_menu = ui.Growing_Menu('Capture Source')
g_pool.capture.init_gui()
g_pool.sidebar.append(general_settings)
g_pool.sidebar.append(g_pool.capture_selector_menu)
g_pool.sidebar.append(g_pool.capture_source_menu)
g_pool.pupil_detector.init_gui(g_pool.sidebar)
manager_class_name, manager_settings = capture_manager_settings
manager_class_by_name = {c.__name__:c for c in manager_classes}
g_pool.capture_manager = manager_class_by_name[manager_class_name](g_pool,**manager_settings)
g_pool.capture_manager.init_gui()
def open_manager(manager_class):
g_pool.capture_manager.cleanup()
g_pool.capture_manager = manager_class(g_pool)
g_pool.capture_manager.init_gui()
#We add the capture selection menu, after a manager has been added:
g_pool.capture_selector_menu.insert(0,ui.Selector(
'capture_manager',g_pool,
setter = open_manager,
getter = lambda: g_pool.capture_manager.__class__,
selection = manager_classes,
labels = [b.gui_name for b in manager_classes],
label = 'Manager'
))
# Register callbacks main_window
glfw.glfwSetFramebufferSizeCallback(main_window,on_resize)
glfw.glfwSetWindowIconifyCallback(main_window,on_iconify)
glfw.glfwSetKeyCallback(main_window,on_key)
glfw.glfwSetCharCallback(main_window,on_char)
glfw.glfwSetMouseButtonCallback(main_window,on_button)
glfw.glfwSetCursorPosCallback(main_window,on_pos)
glfw.glfwSetScrollCallback(main_window,on_scroll)
#set the last saved window size
on_resize(main_window, *glfw.glfwGetWindowSize(main_window))
# load last gui configuration
g_pool.gui.configuration = session_settings.get('ui_config',{})
#set up performance graphs
pid = os.getpid()
ps = psutil.Process(pid)
ts = g_pool.get_timestamp()
cpu_graph = graph.Bar_Graph()
cpu_graph.pos = (20,130)
cpu_graph.update_fn = ps.cpu_percent
cpu_graph.update_rate = 5
cpu_graph.label = 'CPU %0.1f'
fps_graph = graph.Bar_Graph()
fps_graph.pos = (140,130)
fps_graph.update_rate = 5
fps_graph.label = "%0.0f FPS"
should_publish_frames = False
frame_publish_format = 'jpeg'
#create a timer to control window update frequency
window_update_timer = timer(1/60.)
def window_should_update():
return next(window_update_timer)
logger.warning('Process started.')
# Event loop
while not glfw.glfwWindowShouldClose(main_window):
if notify_sub.new_data:
t,notification = notify_sub.recv()
subject = notification['subject']
if subject == 'eye_process.should_stop':
if notification['eye_id'] == eye_id:
break
elif subject == 'set_detection_mapping_mode':
if notification['mode'] == '3d':
if not isinstance(g_pool.pupil_detector,Detector_3D):
set_detector(Detector_3D)
detector_selector.read_only = True
else:
if not isinstance(g_pool.pupil_detector,Detector_2D):
set_detector(Detector_2D)
detector_selector.read_only = False
elif subject == 'recording.started':
if notification['record_eye']:
record_path = notification['rec_path']
raw_mode = notification['compression']
logger.info("Will save eye video to: %s"%record_path)
timestamps_path = os.path.join(record_path, "eye%s_timestamps.npy"%eye_id)
if raw_mode and frame.jpeg_buffer:
video_path = os.path.join(record_path, "eye%s.mp4"%eye_id)
writer = JPEG_Writer(video_path,g_pool.capture.frame_rate)
else:
video_path = os.path.join(record_path, "eye%s.mp4"%eye_id)
writer = AV_Writer(video_path,g_pool.capture.frame_rate)
timestamps = []
elif subject == 'recording.stopped':
if writer:
logger.info("Done recording.")
writer.release()
writer = None
np.save(timestamps_path,np.asarray(timestamps))
del timestamps
elif subject.startswith('meta.should_doc'):
ipc_socket.notify({
'subject':'meta.doc',
'actor':'eye%i'%eye_id,
'doc':eye.__doc__
})
elif subject.startswith('frame_publishing.started'):
should_publish_frames = True
frame_publish_format = notification.get('format','jpeg')
elif subject.startswith('frame_publishing.stopped'):
should_publish_frames = False
frame_publish_format = 'jpeg'
else:
g_pool.capture_manager.on_notify(notification)
# Get an image from the grabber
try:
frame = g_pool.capture.get_frame()
except StreamError as e:
logger.error("Error getting frame. Stopping eye process.")
logger.debug("Caught error: %s"%e)
break
except EndofVideoFileError:
logger.warning("Video File is done. Stopping")
g_pool.capture.seek_to_frame(0)
frame = g_pool.capture.get_frame()
g_pool.capture_manager.update(frame, {})
if should_publish_frames and frame.jpeg_buffer:
if frame_publish_format == "jpeg":
data = frame.jpeg_buffer
elif frame_publish_format == "yuv":
data = frame.yuv_buffer
elif frame_publish_format == "bgr":
data = frame.bgr
elif frame_publish_format == "gray":
data = frame.gray
pupil_socket.send('frame.eye.%s'%eye_id,{
'width': frame.width,
'height': frame.width,
'index': frame.index,
'timestamp': frame.timestamp,
'format': frame_publish_format,
'__raw_data__': [data]
})
#update performace graphs
t = frame.timestamp
dt,ts = t-ts,t
try:
fps_graph.add(1./dt)
except ZeroDivisionError:
pass
cpu_graph.update()
if writer:
writer.write_video_frame(frame)
timestamps.append(frame.timestamp)
# pupil ellipse detection
result = g_pool.pupil_detector.detect(frame, g_pool.u_r, g_pool.display_mode == 'algorithm')
result['id'] = eye_id
# stream the result
pupil_socket.send('pupil.%s'%eye_id,result)
# GL drawing
if window_should_update():
if is_window_visible(main_window):
glfw.glfwMakeContextCurrent(main_window)
clear_gl_screen()
# switch to work in normalized coordinate space
if g_pool.display_mode == 'algorithm':
g_pool.image_tex.update_from_ndarray(frame.img)
elif g_pool.display_mode in ('camera_image','roi'):
g_pool.image_tex.update_from_ndarray(frame.gray)
else:
pass
make_coord_system_norm_based(g_pool.flip)
g_pool.image_tex.draw()
window_size = glfw.glfwGetWindowSize(main_window)
make_coord_system_pixel_based((frame.height,frame.width,3),g_pool.flip)
g_pool.capture.gl_display()
if result['method'] == '3d c++':
eye_ball = result['projected_sphere']
try:
pts = cv2.ellipse2Poly( (int(eye_ball['center'][0]),int(eye_ball['center'][1])),
(int(eye_ball['axes'][0]/2),int(eye_ball['axes'][1]/2)),
int(eye_ball['angle']),0,360,8)
except ValueError as e:
pass
else:
draw_polyline(pts,2,RGBA(0.,.9,.1,result['model_confidence']) )
if result['confidence'] >0:
if result.has_key('ellipse'):
pts = cv2.ellipse2Poly( (int(result['ellipse']['center'][0]),int(result['ellipse']['center'][1])),
(int(result['ellipse']['axes'][0]/2),int(result['ellipse']['axes'][1]/2)),
int(result['ellipse']['angle']),0,360,15)
confidence = result['confidence'] * 0.7 #scale it a little
draw_polyline(pts,1,RGBA(1.,0,0,confidence))
draw_points([result['ellipse']['center']],size=20,color=RGBA(1.,0.,0.,confidence),sharpness=1.)
# render graphs
graph.push_view()
fps_graph.draw()
cpu_graph.draw()
graph.pop_view()
# render GUI
g_pool.gui.update()
#render the ROI
g_pool.u_r.draw(g_pool.gui.scale)
if g_pool.display_mode == 'roi':
g_pool.u_r.draw_points(g_pool.gui.scale)
#update screen
glfw.glfwSwapBuffers(main_window)
glfw.glfwPollEvents()
g_pool.pupil_detector.visualize() #detector decides if we visualize or not
# END while running
# in case eye recording was still runnnig: Save&close
if writer:
logger.info("Done recording eye.")
writer = None
np.save(timestamps_path,np.asarray(timestamps))
glfw.glfwRestoreWindow(main_window) #need to do this for windows os
# save session persistent settings
session_settings['gui_scale'] = g_pool.gui.scale
session_settings['roi'] = g_pool.u_r.get()
session_settings['flip'] = g_pool.flip
session_settings['display_mode'] = g_pool.display_mode
session_settings['ui_config'] = g_pool.gui.configuration
session_settings['capture_settings'] = g_pool.capture.settings
session_settings['capture_manager_settings'] = g_pool.capture_manager.class_name, g_pool.capture_manager.get_init_dict()
session_settings['window_size'] = glfw.glfwGetWindowSize(main_window)
session_settings['window_position'] = glfw.glfwGetWindowPos(main_window)
session_settings['version'] = g_pool.version
session_settings['last_pupil_detector'] = g_pool.pupil_detector.__class__.__name__
session_settings['pupil_detector_settings'] = g_pool.pupil_detector.get_settings()
session_settings.close()
g_pool.capture.deinit_gui()
g_pool.pupil_detector.cleanup()
g_pool.gui.terminate()
glfw.glfwDestroyWindow(main_window)
glfw.glfwTerminate()
g_pool.capture_manager.cleanup()
g_pool.capture.cleanup()
logger.info("Process shutting down.")
0
Example 35
Project: pyspeckit Source File: readcol.py
def readcol(filename,skipline=0,skipafter=0,names=False,fsep=None,twod=True,
fixedformat=None,asdict=False,comment='#',verbose=True,nullval=None,
asStruct=False,namecomment=True,removeblanks=False,header_badchars=None):
"""
The default return is a two dimensional float array. If you want a list of
columns output instead of a 2D array, pass 'twod=False'. In this case,
each column's data type will be automatically detected.
Example usage:
CASE 1) a table has the format:
X Y Z
0.0 2.4 8.2
1.0 3.4 5.6
0.7 3.2 2.1
...
names,(x,y,z)=readcol("myfile.tbl",names=True,twod=False)
or
x,y,z=readcol("myfile.tbl",skipline=1,twod=False)
or
names,xx = readcol("myfile.tbl",names=True)
or
xxdict = readcol("myfile.tbl",asdict=True)
or
xxstruct = readcol("myfile.tbl",asStruct=True)
CASE 2) no title is contained into the table, then there is
no need to skipline:
x,y,z=readcol("myfile.tbl")
CASE 3) there is a names column and then more descriptive text:
X Y Z
(deg) (deg) (km/s)
0.0 2.4 8.2
1.0 3.4. 5.6
...
then use:
names,x,y,z=readcol("myfile.tbl",names=True,skipline=1,twod=False)
or
x,y,z=readcol("myfile.tbl",skipline=2,twod=False)
INPUTS:
fsep - field separator, e.g. for comma separated value (csv) files
skipline - number of lines to ignore at the start of the file
names - read / don't read in the first line as a list of column names
can specify an integer line number too, though it will be
the line number after skipping lines
twod - two dimensional or one dimensional output
nullval - if specified, all instances of this value will be replaced
with a floating NaN
asdict - zips names with data to create a dict with column headings
tied to column data. If asdict=True, names will be set to True
asStruct - same as asdict, but returns a structure instead of a dictionary
(i.e. you call struct.key instead of struct['key'])
fixedformat - if you have a fixed format file, this is a python list of
column lengths. e.g. the first table above would be [3,5,5]. Note
that if you specify the wrong fixed format, you will get junk; if your
format total is greater than the line length, the last entries will all
be blank but readcol will not report an error.
namecomment - assumed that "Name" row is on a comment line. If it is not -
e.g., it is the first non-comment line, change this to False
removeblanks - remove all blank entries from split lines. This can cause lost
data if you have blank entries on some lines.
header_badchars - remove these characters from a header before parsing it
(helpful for IPAC tables that are delimited with | )
If you get this error: "scipy could not be imported. Your table must have
full rows." it means readcol cannot automatically guess which columns
contain data. If you have scipy and columns of varying length, readcol will
read in all of the rows with length=mode(row lengths).
"""
with open(filename,'r') as ff:
f = ff.readlines()
null=[f.pop(0) for i in range(skipline)]
commentfilter = make_commentfilter(comment)
if namecomment is False and (names or asdict or asStruct):
while 1:
line = f.pop(0)
if line[0] != comment:
nameline = line
if header_badchars:
for c in header_badchars:
nameline = nameline.replace(c,' ')
nms=nameline.split(fsep)
break
elif len(f) == 0:
raise Exception("No uncommented lines found.")
else:
if names or asdict or asStruct:
# can specify name line
if type(names) == type(1):
nameline = f.pop(names)
else:
nameline = f.pop(0)
if nameline[0]==comment:
nameline = nameline[1:]
if header_badchars:
for c in header_badchars:
nameline = nameline.replace(c,' ')
nms=nameline.split(fsep)
null=[f.pop(0) for i in range(skipafter)]
if fixedformat:
myreadff = lambda x: readff(x,fixedformat)
splitarr = map(myreadff,f)
splitarr = filter(commentfilter,splitarr)
else:
fstrip = map(string.strip,f)
fseps = [ fsep for i in range(len(f)) ]
splitarr = map(string.split,fstrip,fseps)
if removeblanks:
for i in xrange(splitarr.count([''])):
splitarr.remove([''])
splitarr = filter(commentfilter,splitarr)
# check to make sure each line has the same number of columns to avoid
# "ValueError: setting an array element with a sequence."
nperline = map(len,splitarr)
if hasmode:
ncols,nrows = mode(nperline)
if nrows != len(splitarr):
if verbose:
print(("Removing %i rows that don't match most common length %i. "
"\n%i rows read into array." % (len(splitarr) - nrows,ncols,nrows)))
for i in xrange(len(splitarr)-1,-1,-1): # need to go backwards
if nperline[i] != ncols:
splitarr.pop(i)
try:
x = numpy.asarray( splitarr , dtype='float')
except ValueError:
if verbose:
print(("WARNING: reading as string array because %s array failed" % 'float'))
try:
x = numpy.asarray( splitarr , dtype='S')
except ValueError:
if hasmode:
raise Exception( "ValueError when converting data to array." + \
" You have scipy.mode on your system, so this is " + \
"probably not an issue of differing row lengths." )
else:
raise Exception( "Conversion to array error. You probably " + \
"have different row lengths and scipy.mode was not " + \
"imported." )
if nullval is not None:
x[x==nullval] = numpy.nan
x = get_autotype(x)
if asdict or asStruct:
mydict = dict(zip(nms,x.T))
for k,v in iteritems(mydict):
mydict[k] = get_autotype(v)
if asdict:
return mydict
elif asStruct:
return Struct(mydict)
elif names and twod:
return nms,x
elif names:
# if not returning a twod array, try to return each vector as the spec. type
return nms,[ get_autotype(x.T[i]) for i in xrange(x.shape[1]) ]
else:
if twod:
return x
else:
return [ get_autotype(x.T[i]) for i in xrange(x.shape[1]) ]
0
Example 36
Project: filterpy Source File: test_ukf.py
def two_radar():
# code is not complete - I was using to test RTS smoother. very similar
# to two_radary.py in book.
import numpy as np
import matplotlib.pyplot as plt
from numpy import array
from numpy.linalg import norm
from numpy.random import randn
from math import atan2, radians
from filterpy.common import Q_discrete_white_noise
class RadarStation(object):
def __init__(self, pos, range_std, bearing_std):
self.pos = asarray(pos)
self.range_std = range_std
self.bearing_std = bearing_std
def reading_of(self, ac_pos):
""" Returns range and bearing to aircraft as tuple. bearing is in
radians.
"""
diff = np.subtract(self.pos, ac_pos)
rng = norm(diff)
brg = atan2(diff[1], diff[0])
return rng, brg
def noisy_reading(self, ac_pos):
rng, brg = self.reading_of(ac_pos)
rng += randn() * self.range_std
brg += randn() * self.bearing_std
return rng, brg
class ACSim(object):
def __init__(self, pos, vel, vel_std):
self.pos = asarray(pos, dtype=float)
self.vel = asarray(vel, dtype=float)
self.vel_std = vel_std
def update(self):
vel = self.vel + (randn() * self.vel_std)
self.pos += vel
return self.pos
dt = 1.
def hx(x):
r1, b1 = hx.R1.reading_of((x[0], x[2]))
r2, b2 = hx.R2.reading_of((x[0], x[2]))
return array([r1, b1, r2, b2])
pass
def fx(x, dt):
x_est = x.copy()
x_est[0] += x[1]*dt
x_est[2] += x[3]*dt
return x_est
vx, vy = 0.1, 0.1
f = UnscentedKalmanFilter(dim_x=4, dim_z=4, dt=dt, hx=hx, fx=fx, kappa=0)
aircraft = ACSim ((100,100), (vx*dt,vy*dt), 0.00000002)
range_std = 0.001 # 1 meter
bearing_std = 1/1000 # 1mrad
R1 = RadarStation ((0,0), range_std, bearing_std)
R2 = RadarStation ((200,0), range_std, bearing_std)
hx.R1 = R1
hx.R2 = R2
f.x = array([100, vx, 100, vy])
f.R = np.diag([range_std**2, bearing_std**2, range_std**2, bearing_std**2])
q = Q_discrete_white_noise(2, var=0.0002, dt=dt)
#q = np.array([[0,0],[0,0.0002]])
f.Q[0:2, 0:2] = q
f.Q[2:4, 2:4] = q
f.P = np.diag([.1, 0.01, .1, 0.01])
track = []
zs = []
for i in range(int(300/dt)):
pos = aircraft.update()
r1, b1 = R1.noisy_reading(pos)
r2, b2 = R2.noisy_reading(pos)
z = np.array([r1, b1, r2, b2])
zs.append(z)
track.append(pos.copy())
zs = asarray(zs)
xs, Ps, Pxz, pM, pP = f.batch_filter(zs)
ms, _, _ = f.rts_smoother(xs, Ps)
track = asarray(track)
time = np.arange(0,len(xs)*dt, dt)
plt.figure()
plt.subplot(411)
plt.plot(time, track[:,0])
plt.plot(time, xs[:,0])
plt.legend(loc=4)
plt.xlabel('time (sec)')
plt.ylabel('x position (m)')
plt.tight_layout()
plt.subplot(412)
plt.plot(time, track[:,1])
plt.plot(time, xs[:,2])
plt.legend(loc=4)
plt.xlabel('time (sec)')
plt.ylabel('y position (m)')
plt.tight_layout()
plt.subplot(413)
plt.plot(time, xs[:,1])
plt.plot(time, ms[:,1])
plt.legend(loc=4)
plt.ylim([0, 0.2])
plt.xlabel('time (sec)')
plt.ylabel('x velocity (m/s)')
plt.tight_layout()
plt.subplot(414)
plt.plot(time, xs[:,3])
plt.plot(time, ms[:,3])
plt.ylabel('y velocity (m/s)')
plt.legend(loc=4)
plt.xlabel('time (sec)')
plt.tight_layout()
plt.show()
0
Example 37
Project: scipy Source File: _bvp.py
def solve_bvp(fun, bc, x, y, p=None, S=None, fun_jac=None, bc_jac=None,
tol=1e-3, max_nodes=1000, verbose=0):
"""Solve a boundary-value problem for a system of ODEs.
This function numerically solves a first order system of ODEs subject to
two-point boundary conditions::
dy / dx = f(x, y, p) + S * y / (x - a), a <= x <= b
bc(y(a), y(b), p) = 0
Here x is a 1-dimensional independent variable, y(x) is a n-dimensional
vector-valued function and p is a k-dimensional vector of unknown
parameters which is to be found along with y(x). For the problem to be
determined there must be n + k boundary conditions, i.e. bc must be
(n + k)-dimensional function.
The last singular term in the right-hand side of the system is optional.
It is defined by an n-by-n matrix S, such that the solution must satisfy
S y(a) = 0. This condition will be forced during iterations, so it must not
contradict boundary conditions. See [2]_ for the explanation how this term
is handled when solving BVPs numerically.
Problems in a complex domain can be solved as well. In this case y and p
are considered to be complex, and f and bc are assumed to be complex-valued
functions, but x stays real. Note that f and bc must be complex
differentiable (satisfy Cauchy-Riemann equations [4]_), otherwise you
should rewrite your problem for real and imaginary parts separately. To
solve a problem in a complex domain, pass an initial guess for y with a
complex data type (see below).
Parameters
----------
fun : callable
Right-hand side of the system. The calling signature is ``fun(x, y)``,
or ``fun(x, y, p)`` if parameters are present. All arguments are
ndarray: ``x`` with shape (m,), ``y`` with shape (n, m), meaning that
``y[:, i]`` corresponds to ``x[i]``, and ``p`` with shape (k,). The
return value must be an array with shape (n, m) and with the same
layout as ``y``.
bc : callable
Function evaluating residuals of the boundary conditions. The calling
signature is ``bc(ya, yb)``, or ``bc(ya, yb, p)`` if parameters are
present. All arguments are ndarray: ``ya`` and ``yb`` with shape (n,),
and ``p`` with shape (k,). The return value must be an array with
shape (n + k,).
x : array_like, shape (m,)
Initial mesh. Must be a strictly increasing sequence of real numbers
with ``x[0]=a`` and ``x[-1]=b``.
y : array_like, shape (n, m)
Initial guess for the function values at the mesh nodes, i-th column
corresponds to ``x[i]``. For problems in a complex domain pass `y`
with a complex data type (even if the initial guess is purely real).
p : array_like with shape (k,) or None, optional
Initial guess for the unknown parameters. If None (default), it is
assumed that the problem doesn't depend on any parameters.
S : array_like with shape (n, n) or None
Matrix defining the singular term. If None (default), the problem is
solved without the singular term.
fun_jac : callable or None, optional
Function computing derivatives of f with respect to y and p. The
calling signature is ``fun_jac(x, y)``, or ``fun_jac(x, y, p)`` if
parameters are present. The return must contain 1 or 2 elements in the
following order:
* df_dy : array_like with shape (n, n, m) where an element
(i, j, q) equals to d f_i(x_q, y_q, p) / d (y_q)_j.
* df_dp : array_like with shape (n, k, m) where an element
(i, j, q) equals to d f_i(x_q, y_q, p) / d p_j.
Here q numbers nodes at which x and y are defined, whereas i and j
number vector components. If the problem is solved without unknown
parameters df_dp should not be returned.
If `fun_jac` is None (default), the derivatives will be estimated
by the forward finite differences.
bc_jac : callable or None, optional
Function computing derivatives of bc with respect to ya, yb and p.
The calling signature is ``bc_jac(ya, yb)``, or ``bc_jac(ya, yb, p)``
if parameters are present. The return must contain 2 or 3 elements in
the following order:
* dbc_dya : array_like with shape (n, n) where an element (i, j)
equals to d bc_i(ya, yb, p) / d ya_j.
* dbc_dyb : array_like with shape (n, n) where an element (i, j)
equals to d bc_i(ya, yb, p) / d yb_j.
* dbc_dp : array_like with shape (n, k) where an element (i, j)
equals to d bc_i(ya, yb, p) / d p_j.
If the problem is solved without unknown parameters dbc_dp should not
be returned.
If `bc_jac` is None (default), the derivatives will be estimated by
the forward finite differences.
tol : float, optional
Desired tolerance of the solution. If we define ``r = y' - f(x, y)``
where y is the found solution, then the solver tries to achieve on each
mesh interval ``norm(r / (1 + abs(f)) < tol``, where ``norm`` is
estimated in a root mean squared sense (using a numerical quadrature
formula). Default is 1e-3.
max_nodes : int, optional
Maximum allowed number of the mesh nodes. If exceeded, the algorithm
terminates. Default is 1000.
verbose : {0, 1, 2}, optional
Level of algorithm's verbosity:
* 0 (default) : work silently.
* 1 : display a termination report.
* 2 : display progress during iterations.
Returns
-------
Bunch object with the following fields defined:
sol : PPoly
Found solution for y as `scipy.interpolate.PPoly` instance, a C1
continuous cubic spline.
p : ndarray or None, shape (k,)
Found parameters. None, if the parameters were not present in the
problem.
x : ndarray, shape (m,)
Nodes of the final mesh.
y : ndarray, shape (n, m)
Solution values at the mesh nodes.
yp : ndarray, shape (n, m)
Solution derivatives at the mesh nodes.
rms_residuals : ndarray, shape (m - 1,)
RMS values of the relative residuals over each mesh interval (see the
description of `tol` parameter).
niter : int
Number of completed iterations.
status : int
Reason for algorithm termination:
* 0: The algorithm converged to the desired accuracy.
* 1: The maximum number of mesh nodes is exceeded.
* 2: A singular Jacobian encountered when solving the collocation
system.
message : string
Verbal description of the termination reason.
success : bool
True if the algorithm converged to the desired accuracy (``status=0``).
Notes
-----
This function implements a 4-th order collocation algorithm with the
control of residuals similar to [1]_. A collocation system is solved
by a damped Newton method with an affine-invariant criterion function as
described in [3]_.
Note that in [1]_ integral residuals are defined without normalization
by interval lengths. So their definition is different by a multiplier of
h**0.5 (h is an interval length) from the definition used here.
.. versionadded:: 0.18.0
References
----------
.. [1] J. Kierzenka, L. F. Shampine, "A BVP Solver Based on Residual
Control and the Maltab PSE", ACM Trans. Math. Softw., Vol. 27,
Number 3, pp. 299-316, 2001.
.. [2] L.F. Shampine, P. H. Muir and H. Xu, "A User-Friendly Fortran BVP
Solver".
.. [3] U. Ascher, R. Mattheij and R. Russell "Numerical Solution of
Boundary Value Problems for Ordinary Differential Equations".
.. [4] `Cauchy-Riemann equations
<https://en.wikipedia.org/wiki/Cauchy-Riemann_equations>`_ on
Wikipedia.
Examples
--------
In the first example we solve Bratu's problem::
y'' + k * exp(y) = 0
y(0) = y(1) = 0
for k = 1.
We rewrite the equation as a first order system and implement its
right-hand side evaluation::
y1' = y2
y2' = -exp(y1)
>>> def fun(x, y):
... return np.vstack((y[1], -np.exp(y[0])))
Implement evaluation of the boundary condition residuals:
>>> def bc(ya, yb):
... return np.array([ya[0], yb[0]])
Define the initial mesh with 5 nodes:
>>> x = np.linspace(0, 1, 5)
This problem is known to have two solutions. To obtain both of them we
use two different initial guesses for y. We denote them by subscripts
a and b.
>>> y_a = np.zeros((2, x.size))
>>> y_b = np.zeros((2, x.size))
>>> y_b[0] = 3
Now we are ready to run the solver.
>>> from scipy.integrate import solve_bvp
>>> res_a = solve_bvp(fun, bc, x, y_a)
>>> res_b = solve_bvp(fun, bc, x, y_b)
Let's plot the two found solutions. We take an advantage of having the
solution in a spline form to produce a smooth plot.
>>> x_plot = np.linspace(0, 1, 100)
>>> y_plot_a = res_a.sol(x_plot)[0]
>>> y_plot_b = res_b.sol(x_plot)[0]
>>> import matplotlib.pyplot as plt
>>> plt.plot(x_plot, y_plot_a, label='y_a')
>>> plt.plot(x_plot, y_plot_b, label='y_b')
>>> plt.legend()
>>> plt.xlabel("x")
>>> plt.ylabel("y")
>>> plt.show()
We see that the two solutions have similar shape, but differ in scale
significantly.
In the second example we solve a simple Sturm-Liouville problem::
y'' + k**2 * y = 0
y(0) = y(1) = 0
It is known that a non-trivial solution y = A * sin(k * x) is possible for
k = pi * n, where n is an integer. To establish the normalization constant
A = 1 we add a boundary condition::
y'(0) = k
Again we rewrite our equation as a first order system and implement its
right-hand side evaluation::
y1' = y2
y2' = -k**2 * y1
>>> def fun(x, y, p):
... k = p[0]
... return np.vstack((y[1], -k**2 * y[0]))
Note that parameters p are passed as a vector (with one element in our
case).
Implement the boundary conditions:
>>> def bc(ya, yb, p):
... k = p[0]
... return np.array([ya[0], yb[0], ya[1] - k])
Setup the initial mesh and guess for y. We aim to find the solution for
k = 2 * pi, to achieve that we set values of y to approximately follow
sin(2 * pi * x):
>>> x = np.linspace(0, 1, 5)
>>> y = np.zeros((2, x.size))
>>> y[0, 1] = 1
>>> y[0, 3] = -1
Run the solver with 6 as an initial guess for k.
>>> sol = solve_bvp(fun, bc, x, y, p=[6])
We see that the found k is approximately correct:
>>> sol.p[0]
6.28329460046
And finally plot the solution to see the anticipated sinusoid:
>>> x_plot = np.linspace(0, 1, 100)
>>> y_plot = sol.sol(x_plot)[0]
>>> plt.plot(x_plot, y_plot)
>>> plt.xlabel("x")
>>> plt.ylabel("y")
>>> plt.show()
"""
x = np.asarray(x, dtype=float)
if x.ndim != 1:
raise ValueError("`x` must be 1 dimensional.")
h = np.diff(x)
if np.any(h <= 0):
raise ValueError("`x` must be strictly increasing.")
a = x[0]
y = np.asarray(y)
if np.issubdtype(y.dtype, np.complexfloating):
dtype = complex
else:
dtype = float
y = y.astype(dtype, copy=False)
if y.ndim != 2:
raise ValueError("`y` must be 2 dimensional.")
if y.shape[1] != x.shape[0]:
raise ValueError("`y` is expected to have {} columns, but actually "
"has {}.".format(x.shape[0], y.shape[1]))
if p is None:
p = np.array([])
else:
p = np.asarray(p, dtype=dtype)
if p.ndim != 1:
raise ValueError("`p` must be 1 dimensional.")
if tol < 100 * EPS:
warn("`tol` is too low, setting to {:.2e}".format(100 * EPS))
tol = 100 * EPS
if verbose not in [0, 1, 2]:
raise ValueError("`verbose` must be in [0, 1, 2].")
n = y.shape[0]
k = p.shape[0]
if S is not None:
S = np.asarray(S, dtype=dtype)
if S.shape != (n, n):
raise ValueError("`S` is expected to have shape {}, "
"but actually has {}".format((n, n), S.shape))
# Compute I - S^+ S to impose necessary boundary conditions.
B = np.identity(n) - np.dot(pinv(S), S)
y[:, 0] = np.dot(B, y[:, 0])
# Compute (I - S)^+ to correct derivatives at x=a.
D = pinv(np.identity(n) - S)
else:
B = None
D = None
fun_wrapped, bc_wrapped, fun_jac_wrapped, bc_jac_wrapped = wrap_functions(
fun, bc, fun_jac, bc_jac, k, a, S, D, dtype)
f = fun_wrapped(x, y, p)
if f.shape != y.shape:
raise ValueError("`fun` return is expected to have shape {}, "
"but actually has {}.".format(y.shape, f.shape))
bc_res = bc_wrapped(y[:, 0], y[:, -1], p)
if bc_res.shape != (n + k,):
raise ValueError("`bc` return is expected to have shape {}, "
"but actually has {}.".format((n + k,), bc_res.shape))
status = 0
iteration = 0
if verbose == 2:
print_iteration_header()
while True:
m = x.shape[0]
col_fun, jac_sys = prepare_sys(n, m, k, fun_wrapped, bc_wrapped,
fun_jac_wrapped, bc_jac_wrapped, x, h)
y, p, singular = solve_newton(n, m, h, col_fun, bc_wrapped, jac_sys,
y, p, B, tol)
iteration += 1
col_res, y_middle, f, f_middle = collocation_fun(fun_wrapped, y,
p, x, h)
# This relation is not trivial, but can be verified.
r_middle = 1.5 * col_res / h
sol = create_spline(y, f, x, h)
rms_res = estimate_rms_residuals(fun_wrapped, sol, x, h, p,
r_middle, f_middle)
max_rms_res = np.max(rms_res)
if singular:
status = 2
break
insert_1, = np.nonzero((rms_res > tol) & (rms_res < 100 * tol))
insert_2, = np.nonzero(rms_res >= 100 * tol)
nodes_added = insert_1.shape[0] + 2 * insert_2.shape[0]
if m + nodes_added > max_nodes:
status = 1
if verbose == 2:
nodes_added = "({})".format(nodes_added)
print_iteration_progress(iteration, max_rms_res, m,
nodes_added)
break
if verbose == 2:
print_iteration_progress(iteration, max_rms_res, m, nodes_added)
if nodes_added > 0:
x = modify_mesh(x, insert_1, insert_2)
h = np.diff(x)
y = sol(x)
else:
status = 0
break
if verbose > 0:
if status == 0:
print("Solved in {} iterations, number of nodes {}, "
"maximum relative residual {:.2e}."
.format(iteration, x.shape[0], max_rms_res))
elif status == 1:
print("Number of nodes is exceeded after iteration {}, "
"maximum relative residual {:.2e}."
.format(iteration, max_rms_res))
elif status == 2:
print("Singular Jacobian encountered when solving the collocation "
"system on iteration {}, maximum relative residual {:.2e}."
.format(iteration, max_rms_res))
if p.size == 0:
p = None
return BVPResult(sol=sol, p=p, x=x, y=y, yp=f, rms_residuals=rms_res,
niter=iteration, status=status,
message=TERMINATION_MESSAGES[status], success=status == 0)
0
Example 38
Project: rootpy Source File: plot_corrcoef_matrix.py
def plot_corrcoef_matrix(matrix, names=None,
cmap=None, cmap_text=None,
fontsize=12, grid=False,
axes=None):
"""
This function will draw a lower-triangular correlation matrix
Parameters
----------
matrix : 2-dimensional numpy array/matrix
A correlation coefficient matrix
names : list of strings, optional (default=None)
List of the parameter names corresponding to the rows in ``matrix``.
cmap : matplotlib color map, optional (default=None)
Color map used to color the matrix cells.
cmap_text : matplotlib color map, optional (default=None)
Color map used to color the cell value text. If None, then
all values will be black.
fontsize : int, optional (default=12)
Font size of parameter name and correlation value text.
grid : bool, optional (default=False)
If True, then draw dashed grid lines around the matrix elements.
axes : matplotlib Axes instance, optional (default=None)
The axes to plot on. If None then use the global current axes.
Notes
-----
NumPy and matplotlib are required
Examples
--------
>>> matrix = corrcoef(data.T, weights=weights)
>>> plot_corrcoef_matrix(matrix, names)
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
if axes is None:
axes = plt.gca()
matrix = np.asarray(matrix)
if matrix.ndim != 2:
raise ValueError("matrix is not a 2-dimensional array or matrix")
if matrix.shape[0] != matrix.shape[1]:
raise ValueError("matrix is not square")
if names is not None and len(names) != matrix.shape[0]:
raise ValueError("the number of names does not match the number of "
"rows/columns in the matrix")
# mask out the upper triangular matrix
matrix[np.triu_indices(matrix.shape[0])] = np.nan
if isinstance(cmap_text, string_types):
cmap_text = cm.get_cmap(cmap_text, 201)
if cmap is None:
cmap = cm.get_cmap('jet', 201)
elif isinstance(cmap, string_types):
cmap = cm.get_cmap(cmap, 201)
# make NaN pixels white
cmap.set_bad('w')
axes.imshow(matrix, interpolation='nearest',
cmap=cmap, origin='upper',
vmin=-1, vmax=1)
axes.set_frame_on(False)
plt.setp(axes.get_yticklabels(), visible=False)
plt.setp(axes.get_yticklines(), visible=False)
plt.setp(axes.get_xticklabels(), visible=False)
plt.setp(axes.get_xticklines(), visible=False)
if grid:
# draw grid lines
for slot in range(1, matrix.shape[0] - 1):
# vertical
axes.plot((slot - 0.5, slot - 0.5),
(slot - 0.5, matrix.shape[0] - 0.5), 'k:', linewidth=1)
# horizontal
axes.plot((-0.5, slot + 0.5),
(slot + 0.5, slot + 0.5), 'k:', linewidth=1)
if names is not None:
for slot in range(1, matrix.shape[0]):
# diagonal
axes.plot((slot - 0.5, slot + 1.5),
(slot - 0.5, slot - 2.5), 'k:', linewidth=1)
# label cell values
for row, col in zip(*np.tril_indices(matrix.shape[0], k=-1)):
value = matrix[row][col]
if cmap_text is not None:
color = cmap_text((value + 1.) / 2.)
else:
color = 'black'
axes.text(
col, row,
"{0:d}%".format(int(value * 100)),
color=color,
ha='center', va='center',
fontsize=fontsize)
if names is not None:
# write parameter names
for i, name in enumerate(names):
axes.annotate(
name, (i, i),
rotation=45,
ha='left', va='bottom',
transform=axes.transData,
fontsize=fontsize)
0
Example 39
def __init__(self, x, y, axis=0, bc_type='not-a-knot', extrapolate=None):
x, y = map(np.asarray, (x, y))
if np.issubdtype(x.dtype, np.complexfloating):
raise ValueError("`x` must contain real values.")
if np.issubdtype(y.dtype, np.complexfloating):
dtype = complex
else:
dtype = float
y = y.astype(dtype, copy=False)
axis = axis % y.ndim
if x.ndim != 1:
raise ValueError("`x` must be 1-dimensional.")
if x.shape[0] < 2:
raise ValueError("`x` must contain at least 2 elements.")
if x.shape[0] != y.shape[axis]:
raise ValueError("The length of `y` along `axis`={0} doesn't "
"match the length of `x`".format(axis))
if not np.all(np.isfinite(x)):
raise ValueError("`x` must contain only finite values.")
if not np.all(np.isfinite(y)):
raise ValueError("`y` must contain only finite values.")
dx = np.diff(x)
if np.any(dx <= 0):
raise ValueError("`x` must be strictly increasing sequence.")
n = x.shape[0]
y = np.rollaxis(y, axis)
bc, y = self._validate_bc(bc_type, y, y.shape[1:], axis)
if extrapolate is None:
if bc[0] == 'periodic':
extrapolate = 'periodic'
else:
extrapolate = True
dxr = dx.reshape([dx.shape[0]] + [1] * (y.ndim - 1))
slope = np.diff(y, axis=0) / dxr
# If bc is 'not-a-knot' this change is just a convention.
# If bc is 'periodic' then we already checked that y[0] == y[-1],
# and the spline is just a constant, we handle this case in the same
# way by setting the first derivatives to slope, which is 0.
if n == 2:
if bc[0] in ['not-a-knot', 'periodic']:
bc[0] = (1, slope[0])
if bc[1] in ['not-a-knot', 'periodic']:
bc[1] = (1, slope[0])
# This is a very special case, when both conditions are 'not-a-knot'
# and n == 3. In this case 'not-a-knot' can't be handled regularly
# as the both conditions are identical. We handle this case by
# constructing a parabola passing through given points.
if n == 3 and bc[0] == 'not-a-knot' and bc[1] == 'not-a-knot':
A = np.zeros((3, 3)) # This is a standard matrix.
b = np.empty((3,) + y.shape[1:], dtype=y.dtype)
A[0, 0] = 1
A[0, 1] = 1
A[1, 0] = dx[1]
A[1, 1] = 2 * (dx[0] + dx[1])
A[1, 2] = dx[0]
A[2, 1] = 1
A[2, 2] = 1
b[0] = 2 * slope[0]
b[1] = 3 * (dxr[0] * slope[1] + dxr[1] * slope[0])
b[2] = 2 * slope[1]
s = solve(A, b, overwrite_a=True, overwrite_b=True,
check_finite=False)
else:
# Find derivative values at each x[i] by solving a tridiagonal
# system.
A = np.zeros((3, n)) # This is a banded matrix representation.
b = np.empty((n,) + y.shape[1:], dtype=y.dtype)
# Filling the system for i=1..n-2
# (x[i-1] - x[i]) * s[i-1] +\
# 2 * ((x[i] - x[i-1]) + (x[i+1] - x[i])) * s[i] +\
# (x[i] - x[i-1]) * s[i+1] =\
# 3 * ((x[i+1] - x[i])*(y[i] - y[i-1])/(x[i] - x[i-1]) +\
# (x[i] - x[i-1])*(y[i+1] - y[i])/(x[i+1] - x[i]))
A[1, 1:-1] = 2 * (dx[:-1] + dx[1:]) # The diagonal
A[0, 2:] = dx[:-1] # The upper diagonal
A[-1, :-2] = dx[1:] # The lower diagonal
b[1:-1] = 3 * (dxr[1:] * slope[:-1] + dxr[:-1] * slope[1:])
bc_start, bc_end = bc
if bc_start == 'periodic':
# Due to the periodicity, and because y[-1] = y[0], the linear
# system has (n-1) unknowns/equations instead of n:
A = A[:, 0:-1]
A[1, 0] = 2 * (dx[-1] + dx[0])
A[0, 1] = dx[-1]
b = b[:-1]
# Also, due to the periodicity, the system is not tri-diagonal.
# We need to compute a "condensed" matrix of shape (n-2, n-2).
# See http://www.cfm.brown.edu/people/gk/chap6/node14.html for
# more explanations.
# The condensed matrix is obtained by removing the last column
# and last row of the (n-1, n-1) system matrix. The removed
# values are saved in scalar variables with the (n-1, n-1)
# system matrix indices forming their names:
a_m1_0 = dx[-2] # lower left corner value: A[-1, 0]
a_m1_m2 = dx[-1]
a_m1_m1 = 2 * (dx[-1] + dx[-2])
a_m2_m1 = dx[-2]
a_0_m1 = dx[0]
b[0] = 3 * (dxr[0] * slope[-1] + dxr[-1] * slope[0])
b[-1] = 3 * (dxr[-1] * slope[-2] + dxr[-2] * slope[-1])
Ac = A[:, :-1]
b1 = b[:-1]
b2 = np.zeros_like(b1)
b2[0] = -a_0_m1
b2[-1] = -a_m2_m1
# s1 and s2 are the solutions of (n-2, n-2) system
s1 = solve_banded((1, 1), Ac, b1, overwrite_ab=False,
overwrite_b=False, check_finite=False)
s2 = solve_banded((1, 1), Ac, b2, overwrite_ab=False,
overwrite_b=False, check_finite=False)
# computing the s[n-2] solution:
s_m1 = ((b[-1] - a_m1_0 * s1[0] - a_m1_m2 * s1[-1]) /
(a_m1_m1 + a_m1_0 * s2[0] + a_m1_m2 * s2[-1]))
# s is the solution of the (n, n) system:
s = np.empty((n,) + y.shape[1:], dtype=y.dtype)
s[:-2] = s1 + s_m1 * s2
s[-2] = s_m1
s[-1] = s[0]
else:
if bc_start == 'not-a-knot':
A[1, 0] = dx[1]
A[0, 1] = x[2] - x[0]
d = x[2] - x[0]
b[0] = ((dxr[0] + 2*d) * dxr[1] * slope[0] +
dxr[0]**2 * slope[1]) / d
elif bc_start[0] == 1:
A[1, 0] = 1
A[0, 1] = 0
b[0] = bc_start[1]
elif bc_start[0] == 2:
A[1, 0] = 2 * dx[0]
A[0, 1] = dx[0]
b[0] = -0.5 * bc_start[1] * dx[0]**2 + 3 * (y[1] - y[0])
if bc_end == 'not-a-knot':
A[1, -1] = dx[-2]
A[-1, -2] = x[-1] - x[-3]
d = x[-1] - x[-3]
b[-1] = ((dxr[-1]**2*slope[-2] +
(2*d + dxr[-1])*dxr[-2]*slope[-1]) / d)
elif bc_end[0] == 1:
A[1, -1] = 1
A[-1, -2] = 0
b[-1] = bc_end[1]
elif bc_end[0] == 2:
A[1, -1] = 2 * dx[-1]
A[-1, -2] = dx[-1]
b[-1] = 0.5 * bc_end[1] * dx[-1]**2 + 3 * (y[-1] - y[-2])
s = solve_banded((1, 1), A, b, overwrite_ab=True,
overwrite_b=True, check_finite=False)
# Compute coefficients in PPoly form.
t = (s[:-1] + s[1:] - 2 * slope) / dxr
c = np.empty((4, n - 1) + y.shape[1:], dtype=t.dtype)
c[0] = t / dxr
c[1] = (slope - s[:-1]) / dxr - t
c[2] = s[:-1]
c[3] = y[:-1]
super(CubicSpline, self).__init__(c, x, extrapolate=extrapolate)
self.axis = axis
0
Example 40
Project: scipy Source File: _minimize.py
def minimize(fun, x0, args=(), method=None, jac=None, hess=None,
hessp=None, bounds=None, constraints=(), tol=None,
callback=None, options=None):
"""Minimization of scalar function of one or more variables.
In general, the optimization problems are of the form::
minimize f(x) subject to
g_i(x) >= 0, i = 1,...,m
h_j(x) = 0, j = 1,...,p
where x is a vector of one or more variables.
``g_i(x)`` are the inequality constraints.
``h_j(x)`` are the equality constrains.
Optionally, the lower and upper bounds for each element in x can also be
specified using the `bounds` argument.
Parameters
----------
fun : callable
Objective function.
x0 : ndarray
Initial guess.
args : tuple, optional
Extra arguments passed to the objective function and its
derivatives (Jacobian, Hessian).
method : str or callable, optional
Type of solver. Should be one of
- 'Nelder-Mead' :ref:`(see here) <optimize.minimize-neldermead>`
- 'Powell' :ref:`(see here) <optimize.minimize-powell>`
- 'CG' :ref:`(see here) <optimize.minimize-cg>`
- 'BFGS' :ref:`(see here) <optimize.minimize-bfgs>`
- 'Newton-CG' :ref:`(see here) <optimize.minimize-newtoncg>`
- 'L-BFGS-B' :ref:`(see here) <optimize.minimize-lbfgsb>`
- 'TNC' :ref:`(see here) <optimize.minimize-tnc>`
- 'COBYLA' :ref:`(see here) <optimize.minimize-cobyla>`
- 'SLSQP' :ref:`(see here) <optimize.minimize-slsqp>`
- 'dogleg' :ref:`(see here) <optimize.minimize-dogleg>`
- 'trust-ncg' :ref:`(see here) <optimize.minimize-trustncg>`
- custom - a callable object (added in version 0.14.0),
see below for description.
If not given, chosen to be one of ``BFGS``, ``L-BFGS-B``, ``SLSQP``,
depending if the problem has constraints or bounds.
jac : bool or callable, optional
Jacobian (gradient) of objective function. Only for CG, BFGS,
Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg.
If `jac` is a Boolean and is True, `fun` is assumed to return the
gradient along with the objective function. If False, the
gradient will be estimated numerically.
`jac` can also be a callable returning the gradient of the
objective. In this case, it must accept the same arguments as `fun`.
hess, hessp : callable, optional
Hessian (matrix of second-order derivatives) of objective function or
Hessian of objective function times an arbitrary vector p. Only for
Newton-CG, dogleg, trust-ncg.
Only one of `hessp` or `hess` needs to be given. If `hess` is
provided, then `hessp` will be ignored. If neither `hess` nor
`hessp` is provided, then the Hessian product will be approximated
using finite differences on `jac`. `hessp` must compute the Hessian
times an arbitrary vector.
bounds : sequence, optional
Bounds for variables (only for L-BFGS-B, TNC and SLSQP).
``(min, max)`` pairs for each element in ``x``, defining
the bounds on that parameter. Use None for one of ``min`` or
``max`` when there is no bound in that direction.
constraints : dict or sequence of dict, optional
Constraints definition (only for COBYLA and SLSQP).
Each constraint is defined in a dictionary with fields:
type : str
Constraint type: 'eq' for equality, 'ineq' for inequality.
fun : callable
The function defining the constraint.
jac : callable, optional
The Jacobian of `fun` (only for SLSQP).
args : sequence, optional
Extra arguments to be passed to the function and Jacobian.
Equality constraint means that the constraint function result is to
be zero whereas inequality means that it is to be non-negative.
Note that COBYLA only supports inequality constraints.
tol : float, optional
Tolerance for termination. For detailed control, use solver-specific
options.
options : dict, optional
A dictionary of solver options. All methods accept the following
generic options:
maxiter : int
Maximum number of iterations to perform.
disp : bool
Set to True to print convergence messages.
For method-specific options, see :func:`show_options()`.
callback : callable, optional
Called after each iteration, as ``callback(xk)``, where ``xk`` is the
current parameter vector.
Returns
-------
res : OptimizeResult
The optimization result represented as a ``OptimizeResult`` object.
Important attributes are: ``x`` the solution array, ``success`` a
Boolean flag indicating if the optimizer exited successfully and
``message`` which describes the cause of the termination. See
`OptimizeResult` for a description of other attributes.
See also
--------
minimize_scalar : Interface to minimization algorithms for scalar
univariate functions
show_options : Additional options accepted by the solvers
Notes
-----
This section describes the available solvers that can be selected by the
'method' parameter. The default method is *BFGS*.
**Unconstrained minimization**
Method :ref:`Nelder-Mead <optimize.minimize-neldermead>` uses the
Simplex algorithm [1]_, [2]_. This algorithm is robust in many
applications. However, if numerical computation of derivative can be
trusted, other algorithms using the first and/or second derivatives
information might be preferred for their better performance in
general.
Method :ref:`Powell <optimize.minimize-powell>` is a modification
of Powell's method [3]_, [4]_ which is a conjugate direction
method. It performs sequential one-dimensional minimizations along
each vector of the directions set (`direc` field in `options` and
`info`), which is updated at each iteration of the main
minimization loop. The function need not be differentiable, and no
derivatives are taken.
Method :ref:`CG <optimize.minimize-cg>` uses a nonlinear conjugate
gradient algorithm by Polak and Ribiere, a variant of the
Fletcher-Reeves method described in [5]_ pp. 120-122. Only the
first derivatives are used.
Method :ref:`BFGS <optimize.minimize-bfgs>` uses the quasi-Newton
method of Broyden, Fletcher, Goldfarb, and Shanno (BFGS) [5]_
pp. 136. It uses the first derivatives only. BFGS has proven good
performance even for non-smooth optimizations. This method also
returns an approximation of the Hessian inverse, stored as
`hess_inv` in the OptimizeResult object.
Method :ref:`Newton-CG <optimize.minimize-newtoncg>` uses a
Newton-CG algorithm [5]_ pp. 168 (also known as the truncated
Newton method). It uses a CG method to the compute the search
direction. See also *TNC* method for a box-constrained
minimization with a similar algorithm.
Method :ref:`dogleg <optimize.minimize-dogleg>` uses the dog-leg
trust-region algorithm [5]_ for unconstrained minimization. This
algorithm requires the gradient and Hessian; furthermore the
Hessian is required to be positive definite.
Method :ref:`trust-ncg <optimize.minimize-trustncg>` uses the
Newton conjugate gradient trust-region algorithm [5]_ for
unconstrained minimization. This algorithm requires the gradient
and either the Hessian or a function that computes the product of
the Hessian with a given vector.
**Constrained minimization**
Method :ref:`L-BFGS-B <optimize.minimize-lbfgsb>` uses the L-BFGS-B
algorithm [6]_, [7]_ for bound constrained minimization.
Method :ref:`TNC <optimize.minimize-tnc>` uses a truncated Newton
algorithm [5]_, [8]_ to minimize a function with variables subject
to bounds. This algorithm uses gradient information; it is also
called Newton Conjugate-Gradient. It differs from the *Newton-CG*
method described above as it wraps a C implementation and allows
each variable to be given upper and lower bounds.
Method :ref:`COBYLA <optimize.minimize-cobyla>` uses the
Constrained Optimization BY Linear Approximation (COBYLA) method
[9]_, [10]_, [11]_. The algorithm is based on linear
approximations to the objective function and each constraint. The
method wraps a FORTRAN implementation of the algorithm. The
constraints functions 'fun' may return either a single number
or an array or list of numbers.
Method :ref:`SLSQP <optimize.minimize-slsqp>` uses Sequential
Least SQuares Programming to minimize a function of several
variables with any combination of bounds, equality and inequality
constraints. The method wraps the SLSQP Optimization subroutine
originally implemented by Dieter Kraft [12]_. Note that the
wrapper handles infinite values in bounds by converting them into
large floating values.
**Custom minimizers**
It may be useful to pass a custom minimization method, for example
when using a frontend to this method such as `scipy.optimize.basinhopping`
or a different library. You can simply pass a callable as the ``method``
parameter.
The callable is called as ``method(fun, x0, args, **kwargs, **options)``
where ``kwargs`` corresponds to any other parameters passed to `minimize`
(such as `callback`, `hess`, etc.), except the `options` dict, which has
its contents also passed as `method` parameters pair by pair. Also, if
`jac` has been passed as a bool type, `jac` and `fun` are mangled so that
`fun` returns just the function values and `jac` is converted to a function
returning the Jacobian. The method shall return an ``OptimizeResult``
object.
The provided `method` callable must be able to accept (and possibly ignore)
arbitrary parameters; the set of parameters accepted by `minimize` may
expand in future versions and then these parameters will be passed to
the method. You can find an example in the scipy.optimize tutorial.
.. versionadded:: 0.11.0
References
----------
.. [1] Nelder, J A, and R Mead. 1965. A Simplex Method for Function
Minimization. The Computer Journal 7: 308-13.
.. [2] Wright M H. 1996. Direct search methods: Once scorned, now
respectable, in Numerical Analysis 1995: Proceedings of the 1995
Dundee Biennial Conference in Numerical Analysis (Eds. D F
Griffiths and G A Watson). Addison Wesley Longman, Harlow, UK.
191-208.
.. [3] Powell, M J D. 1964. An efficient method for finding the minimum of
a function of several variables without calculating derivatives. The
Computer Journal 7: 155-162.
.. [4] Press W, S A Teukolsky, W T Vetterling and B P Flannery.
Numerical Recipes (any edition), Cambridge University Press.
.. [5] Nocedal, J, and S J Wright. 2006. Numerical Optimization.
Springer New York.
.. [6] Byrd, R H and P Lu and J. Nocedal. 1995. A Limited Memory
Algorithm for Bound Constrained Optimization. SIAM Journal on
Scientific and Statistical Computing 16 (5): 1190-1208.
.. [7] Zhu, C and R H Byrd and J Nocedal. 1997. L-BFGS-B: Algorithm
778: L-BFGS-B, FORTRAN routines for large scale bound constrained
optimization. ACM Transactions on Mathematical Software 23 (4):
550-560.
.. [8] Nash, S G. Newton-Type Minimization Via the Lanczos Method.
1984. SIAM Journal of Numerical Analysis 21: 770-778.
.. [9] Powell, M J D. A direct search optimization method that models
the objective and constraint functions by linear interpolation.
1994. Advances in Optimization and Numerical Analysis, eds. S. Gomez
and J-P Hennart, Kluwer Academic (Dordrecht), 51-67.
.. [10] Powell M J D. Direct search algorithms for optimization
calculations. 1998. Acta Numerica 7: 287-336.
.. [11] Powell M J D. A view of algorithms for optimization without
derivatives. 2007.Cambridge University Technical Report DAMTP
2007/NA03
.. [12] Kraft, D. A software package for sequential quadratic
programming. 1988. Tech. Rep. DFVLR-FB 88-28, DLR German Aerospace
Center -- Institute for Flight Mechanics, Koln, Germany.
Examples
--------
Let us consider the problem of minimizing the Rosenbrock function. This
function (and its respective derivatives) is implemented in `rosen`
(resp. `rosen_der`, `rosen_hess`) in the `scipy.optimize`.
>>> from scipy.optimize import minimize, rosen, rosen_der
A simple application of the *Nelder-Mead* method is:
>>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
>>> res = minimize(rosen, x0, method='Nelder-Mead', tol=1e-6)
>>> res.x
array([ 1., 1., 1., 1., 1.])
Now using the *BFGS* algorithm, using the first derivative and a few
options:
>>> res = minimize(rosen, x0, method='BFGS', jac=rosen_der,
... options={'gtol': 1e-6, 'disp': True})
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 26
Function evaluations: 31
Gradient evaluations: 31
>>> res.x
array([ 1., 1., 1., 1., 1.])
>>> print(res.message)
Optimization terminated successfully.
>>> res.hess_inv
array([[ 0.00749589, 0.01255155, 0.02396251, 0.04750988, 0.09495377], # may vary
[ 0.01255155, 0.02510441, 0.04794055, 0.09502834, 0.18996269],
[ 0.02396251, 0.04794055, 0.09631614, 0.19092151, 0.38165151],
[ 0.04750988, 0.09502834, 0.19092151, 0.38341252, 0.7664427 ],
[ 0.09495377, 0.18996269, 0.38165151, 0.7664427, 1.53713523]])
Next, consider a minimization problem with several constraints (namely
Example 16.4 from [5]_). The objective function is:
>>> fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2
There are three constraints defined as:
>>> cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},
... {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
... {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2})
And variables must be positive, hence the following bounds:
>>> bnds = ((0, None), (0, None))
The optimization problem is solved using the SLSQP method as:
>>> res = minimize(fun, (2, 0), method='SLSQP', bounds=bnds,
... constraints=cons)
It should converge to the theoretical solution (1.4 ,1.7).
"""
x0 = np.asarray(x0)
if x0.dtype.kind in np.typecodes["AllInteger"]:
x0 = np.asarray(x0, dtype=float)
if not isinstance(args, tuple):
args = (args,)
if method is None:
# Select automatically
if constraints:
method = 'SLSQP'
elif bounds is not None:
method = 'L-BFGS-B'
else:
method = 'BFGS'
if callable(method):
meth = "_custom"
else:
meth = method.lower()
if options is None:
options = {}
# check if optional parameters are supported by the selected method
# - jac
if meth in ['nelder-mead', 'powell', 'cobyla'] and bool(jac):
warn('Method %s does not use gradient information (jac).' % method,
RuntimeWarning)
# - hess
if meth not in ('newton-cg', 'dogleg', 'trust-ncg', '_custom') and hess is not None:
warn('Method %s does not use Hessian information (hess).' % method,
RuntimeWarning)
# - hessp
if meth not in ('newton-cg', 'dogleg', 'trust-ncg', '_custom') and hessp is not None:
warn('Method %s does not use Hessian-vector product '
'information (hessp).' % method, RuntimeWarning)
# - constraints or bounds
if (meth in ['nelder-mead', 'powell', 'cg', 'bfgs', 'newton-cg', 'dogleg',
'trust-ncg'] and (bounds is not None or np.any(constraints))):
warn('Method %s cannot handle constraints nor bounds.' % method,
RuntimeWarning)
if meth in ['l-bfgs-b', 'tnc'] and np.any(constraints):
warn('Method %s cannot handle constraints.' % method,
RuntimeWarning)
if meth == 'cobyla' and bounds is not None:
warn('Method %s cannot handle bounds.' % method,
RuntimeWarning)
# - callback
if (meth in ['cobyla'] and callback is not None):
warn('Method %s does not support callback.' % method, RuntimeWarning)
# - return_all
if (meth in ['l-bfgs-b', 'tnc', 'cobyla', 'slsqp'] and
options.get('return_all', False)):
warn('Method %s does not support the return_all option.' % method,
RuntimeWarning)
# fun also returns the jacobian
if not callable(jac):
if bool(jac):
fun = MemoizeJac(fun)
jac = fun.derivative
else:
jac = None
# set default tolerances
if tol is not None:
options = dict(options)
if meth == 'nelder-mead':
options.setdefault('xatol', tol)
options.setdefault('fatol', tol)
if meth in ['newton-cg', 'powell', 'tnc']:
options.setdefault('xtol', tol)
if meth in ['powell', 'l-bfgs-b', 'tnc', 'slsqp']:
options.setdefault('ftol', tol)
if meth in ['bfgs', 'cg', 'l-bfgs-b', 'tnc', 'dogleg', 'trust-ncg']:
options.setdefault('gtol', tol)
if meth in ['cobyla', '_custom']:
options.setdefault('tol', tol)
if meth == '_custom':
return method(fun, x0, args=args, jac=jac, hess=hess, hessp=hessp,
bounds=bounds, constraints=constraints,
callback=callback, **options)
elif meth == 'nelder-mead':
return _minimize_neldermead(fun, x0, args, callback, **options)
elif meth == 'powell':
return _minimize_powell(fun, x0, args, callback, **options)
elif meth == 'cg':
return _minimize_cg(fun, x0, args, jac, callback, **options)
elif meth == 'bfgs':
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
elif meth == 'newton-cg':
return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,
**options)
elif meth == 'l-bfgs-b':
return _minimize_lbfgsb(fun, x0, args, jac, bounds,
callback=callback, **options)
elif meth == 'tnc':
return _minimize_tnc(fun, x0, args, jac, bounds, callback=callback,
**options)
elif meth == 'cobyla':
return _minimize_cobyla(fun, x0, args, constraints, **options)
elif meth == 'slsqp':
return _minimize_slsqp(fun, x0, args, jac, bounds,
constraints, callback=callback, **options)
elif meth == 'dogleg':
return _minimize_dogleg(fun, x0, args, jac, hess,
callback=callback, **options)
elif meth == 'trust-ncg':
return _minimize_trust_ncg(fun, x0, args, jac, hess, hessp,
callback=callback, **options)
else:
raise ValueError('Unknown solver %s' % method)
0
Example 41
Project: pyspeckit Source File: readcol.py
def readcol(filename,skipline=0,skipafter=0,names=False,fsep=None,twod=True,
fixedformat=None,asdict=False,comment='#',verbose=True,nullval=None,
asStruct=False,namecomment=True,removeblanks=False):
"""
The default return is a two dimensional float array. If you want a list of
columns output instead of a 2D array, pass 'twod=False'. In this case,
each column's data type will be automatically detected.
Example usage:
CASE 1) a table has the format:
X Y Z
0.0 2.4 8.2
1.0 3.4 5.6
0.7 3.2 2.1
...
names,(x,y,z)=readcol("myfile.tbl",names=True,twod=False)
or
x,y,z=readcol("myfile.tbl",skipline=1,twod=False)
or
names,xx = readcol("myfile.tbl",names=True)
or
xxdict = readcol("myfile.tbl",asdict=True)
or
xxstruct = readcol("myfile.tbl",asStruct=True)
CASE 2) no title is contained into the table, then there is
no need to skipline:
x,y,z=readcol("myfile.tbl")
CASE 3) there is a names column and then more descriptive text:
X Y Z
(deg) (deg) (km/s)
0.0 2.4 8.2
1.0 3.4. 5.6
...
then use:
names,x,y,z=readcol("myfile.tbl",names=True,skipline=1,twod=False)
or
x,y,z=readcol("myfile.tbl",skipline=2,twod=False)
INPUTS:
fsep - field separator, e.g. for comma separated value (csv) files
skipline - number of lines to ignore at the start of the file
names - read / don't read in the first line as a list of column names
can specify an integer line number too, though it will be
the line number after skipping lines
twod - two dimensional or one dimensional output
nullval - if specified, all instances of this value will be replaced
with a floating NaN
asdict - zips names with data to create a dict with column headings
tied to column data. If asdict=True, names will be set to True
asStruct - same as asdict, but returns a structure instead of a dictionary
(i.e. you call struct.key instead of struct['key'])
fixedformat - if you have a fixed format file, this is a python list of
column lengths. e.g. the first table above would be [3,5,5]. Note
that if you specify the wrong fixed format, you will get junk; if your
format total is greater than the line length, the last entries will all
be blank but readcol will not report an error.
namecomment - assumed that "Name" row is on a comment line. If it is not -
e.g., it is the first non-comment line, change this to False
removeblanks - remove all blank entries from split lines. This can cause lost
data if you have blank entries on some lines.
If you get this error: "scipy could not be imported. Your table must have
full rows." it means readcol cannot automatically guess which columns
contain data. If you have scipy and columns of varying length, readcol will
read in all of the rows with length=mode(row lengths).
"""
with open(filename,'r') as ff:
f = ff.readlines()
null=[f.pop(0) for i in range(skipline)]
commentfilter = make_commentfilter(comment)
if namecomment is False and (names or asdict or asStruct):
while 1:
line = f.pop(0)
if line[0] != comment:
nameline = line
nms=nameline.split(fsep)
break
elif len(f) == 0:
raise Exception("No uncommented lines found.")
else:
if names or asdict or asStruct:
# can specify name line
if type(names) == type(1):
nameline = f.pop(names)
else:
nameline = f.pop(0)
if nameline[0]==comment:
nameline = nameline[1:]
nms=nameline.split(fsep)
null=[f.pop(0) for i in range(skipafter)]
if fixedformat:
myreadff = lambda x: readff(x,fixedformat)
splitarr = map(myreadff,f)
splitarr = filter(commentfilter,splitarr)
else:
fstrip = map(string.strip,f)
fseps = [ fsep for i in range(len(f)) ]
splitarr = map(string.split,fstrip,fseps)
if removeblanks:
for i in xrange(splitarr.count([''])):
splitarr.remove([''])
splitarr = filter(commentfilter,splitarr)
# check to make sure each line has the same number of columns to avoid
# "ValueError: setting an array element with a sequence."
nperline = map(len,splitarr)
if hasmode:
ncols,nrows = mode(nperline)
if nrows != len(splitarr):
if verbose:
print("Removing %i rows that don't match most common length %i. \
\n%i rows read into array." % (len(splitarr) - nrows,ncols,nrows))
for i in xrange(len(splitarr)-1,-1,-1): # need to go backwards
if nperline[i] != ncols:
splitarr.pop(i)
try:
x = numpy.asarray( splitarr , dtype='float')
except ValueError:
if verbose:
print("WARNING: reading as string array because %s array failed" % 'float')
try:
x = numpy.asarray( splitarr , dtype='S')
except ValueError:
if hasmode:
raise Exception( "ValueError when converting data to array." + \
" You have scipy.mode on your system, so this is " + \
"probably not an issue of differing row lengths." )
else:
raise Exception( "Conversion to array error. You probably " + \
"have different row lengths and scipy.mode was not " + \
"imported." )
if nullval is not None:
x[x==nullval] = numpy.nan
x = get_autotype(x)
if asdict or asStruct:
mydict = dict(zip(nms,x.T))
for k,v in iteritems(mydict):
mydict[k] = get_autotype(v)
if asdict:
return mydict
elif asStruct:
return Struct(mydict)
elif names and twod:
return nms,x
elif names:
# if not returning a twod array, try to return each vector as the spec. type
return nms,[ get_autotype(x.T[i]) for i in xrange(x.shape[1]) ]
else:
if twod:
return x
else:
return [ get_autotype(x.T[i]) for i in xrange(x.shape[1]) ]
0
Example 42
Project: scikit-beam Source File: recip.py
def process_to_q(setting_angles, detector_size, pixel_size,
calibrated_center, dist_sample, wavelength, ub,
frame_mode=None):
"""
This will compute the hkl values for all pixels in a shape specified by
detector_size.
Parameters
----------
setting_angles : ndarray
six angles of all the images - Required shape is [num_images][6] and
required type is something that can be cast to a 2D numpy array
Angle order: delta, theta, chi, phi, mu, gamma (degrees)
detector_size : tuple
2 element tuple defining the number of pixels in the detector. Order is
(num_columns, num_rows)
pixel_size : tuple
2 element tuple defining the size of each pixel in mm. Order is
(column_pixel_size, row_pixel_size). If not in mm, must be in the same
units as `dist_sample`
calibrated_center : tuple
2 element tuple defining the center of the detector in pixels. Order
is (column_center, row_center)(x y)
dist_sample : float
distance from the sample to the detector (mm). If not in mm, must be
in the same units as `pixel_size`
wavelength : float
wavelength of incident radiation (Angstroms)
ub : ndarray
UB matrix (orientation matrix) 3x3 matrix
frame_mode : str, optional
Frame mode defines the data collection mode and thus the desired
output from this function. Defaults to hkl mode (frame_mode=4)
'theta' : Theta axis frame.
'phi' : Phi axis frame.
'cart' : Crystal cartesian frame.
'hkl' : Reciprocal lattice units frame.
See the `process_to_q.frame_mode` attribute for an exact list of
valid options.
Returns
-------
hkl : ndarray
(Qx, Qy, Qz) - HKL values
shape is [num_images * num_rows * num_columns][3]
Notes
-----
Six angles of an image: (delta, theta, chi, phi, mu, gamma )
These axes are defined according to the following references.
References: text [1]_, text [2]_
.. [1] M. Lohmeier and E.Vlieg, "Angle calculations for a six-circle
surface x-ray diffractometer," J. Appl. Cryst., vol 26, pp 706-716,
1993.
.. [2] E. Vlieg, "A (2+3)-Type surface diffractometer: Mergence of the
z-axis and (2+2)-Type geometries," J. Appl. Cryst., vol 31, pp 198-203,
1998.
"""
try:
from ..ext import ctrans
except ImportError:
raise NotImplementedError(
"ctrans is not available on your platform. See"
"https://github.com/scikit-beam/scikit-beam/issues/418"
"to follow updates to this problem.")
# Set default threads
# set default frame_mode
if frame_mode is None:
frame_mode = 4
else:
str_to_int = verbosedict((k, j + 1) for j, k
in enumerate(process_to_q.frame_mode))
frame_mode = str_to_int[frame_mode]
# ensure the ub matrix is an array
ub = np.asarray(ub)
# ensure setting angles is a 2-D
setting_angles = np.atleast_2d(setting_angles)
if setting_angles.ndim != 2:
raise ValueError('setting_angles is expected to be a 2-D array with'
' dimensions [num_images][num_angles]. You provided '
'an array with dimensions {0}'
''.format(setting_angles.shape))
if setting_angles.shape[1] != 6:
raise ValueError('It is expected that there should be six angles in '
'the setting_angles parameter. You provided {0}'
' angles.'.format(setting_angles.shape[1]))
# cuem******* Converting to Q **************
# starting time for the process
t1 = time.time()
# ctrans - c routines for fast data analysis
hkl = ctrans.ccdToQ(angles=setting_angles * np.pi / 180.0,
mode=frame_mode,
ccd_size=(detector_size),
ccd_pixsize=(pixel_size),
ccd_cen=(calibrated_center),
dist=dist_sample,
wavelength=wavelength,
UBinv=np.matrix(ub).I)
# ending time for the process
t2 = time.time()
logger.info("Processing time for {0} {1} x {2} images took {3} seconds."
"".format(setting_angles.shape[0], detector_size[0],
detector_size[1], (t2 - t1)))
return hkl
0
Example 43
Project: scipy Source File: fir_filter_design.py
def minimum_phase(h, method='humomorphic', n_fft=None):
"""Convert a linear-phase FIR filter to minimum phase
Parameters
----------
h : array
Linear-phase FIR filter coefficients.
method : {'hilbert', 'humomorphic'}
The method to use:
'humomorphic' (default)
This method [4]_ [5]_ works best with filters with an
odd number of taps, and the resulting minimum phase filter
will have a magnitude response that approximates the square
root of the the original filter's magnitude response.
'hilbert'
This method [1]_ is designed to be used with equiripple
filters (e.g., from `remez`) with unity or zero gain
regions.
n_fft : int
The number of points to use for the FFT. Should be at least a
few times larger than the signal length (see Notes).
Returns
-------
h_minimum : array
The minimum-phase version of the filter, with length
``(length(h) + 1) // 2``.
See Also
--------
firwin
firwin2
remez
Notes
-----
Both the Hilbert [1]_ or humomorphic [4]_ [5]_ methods require selection
of an FFT length to estimate the complex cepstrum of the filter.
In the case of the Hilbert method, the deviation from the ideal
spectrum ``epsilon`` is related to the number of stopband zeros
``n_stop`` and FFT length ``n_fft`` as::
epsilon = 2. * n_stop / n_fft
For example, with 100 stopband zeros and a FFT length of 2048,
``epsilon = 0.0976``. If we conservatively assume that the number of
stopband zeros is one less than the filter length, we can take the FFT
length to be the next power of 2 that satisfies ``epsilon=0.01`` as::
n_fft = 2 ** int(np.ceil(np.log2(2 * (len(h) - 1) / 0.01)))
This gives reasonable results for both the Hilbert and humomorphic
methods, and gives the value used when ``n_fft=None``.
Alternative implementations exist for creating minimum-phase filters,
including zero inversion [2]_ and spectral factorization [3]_ [4]_.
For more information, see:
http://dspguru.com/dsp/howtos/how-to-design-minimum-phase-fir-filters
Examples
--------
Create an optimal linear-phase filter, then convert it to minimum phase:
>>> from scipy.signal import remez, minimum_phase, freqz, group_delay
>>> import matplotlib.pyplot as plt
>>> freq = [0, 0.2, 0.3, 1.0]
>>> desired = [1, 0]
>>> h_linear = remez(151, freq, desired, Hz=2.)
Convert it to minimum phase:
>>> h_min_hom = minimum_phase(h_linear, method='humomorphic')
>>> h_min_hil = minimum_phase(h_linear, method='hilbert')
Compare the three filters:
>>> fig, axs = plt.subplots(4, figsize=(4, 8))
>>> for h, style, color in zip((h_linear, h_min_hom, h_min_hil),
... ('-', '-', '--'), ('k', 'r', 'c')):
... w, H = freqz(h)
... w, gd = group_delay((h, 1))
... w /= np.pi
... axs[0].plot(h, color=color, linestyle=style)
... axs[1].plot(w, np.abs(H), color=color, linestyle=style)
... axs[2].plot(w, 20 * np.log10(np.abs(H)), color=color, linestyle=style)
... axs[3].plot(w, gd, color=color, linestyle=style)
>>> for ax in axs:
... ax.grid(True, color='0.5')
... ax.fill_between(freq[1:3], *ax.get_ylim(), color='#ffeeaa', zorder=1)
>>> axs[0].set(xlim=[0, len(h_linear) - 1], ylabel='Amplitude', xlabel='Samples')
>>> axs[1].legend(['Linear', 'Min-Hom', 'Min-Hil'], title='Phase')
>>> for ax, ylim in zip(axs[1:], ([0, 1.1], [-150, 10], [-60, 60])):
... ax.set(xlim=[0, 1], ylim=ylim, xlabel='Frequency')
>>> axs[1].set(ylabel='Magnitude')
>>> axs[2].set(ylabel='Magnitude (dB)')
>>> axs[3].set(ylabel='Group delay')
>>> plt.tight_layout()
References
----------
.. [1] N. Damera-Venkata and B. L. Evans, "Optimal design of real and
complex minimum phase digital FIR filters," Acoustics, Speech,
and Signal Processing, 1999. Proceedings., 1999 IEEE International
Conference on, Phoenix, AZ, 1999, pp. 1145-1148 vol.3.
doi: 10.1109/ICASSP.1999.756179
.. [2] X. Chen and T. W. Parks, "Design of optimal minimum phase FIR
filters by direct factorization," Signal Processing,
vol. 10, no. 4, pp. 369–383, Jun. 1986.
.. [3] T. Saramaki, "Finite Impulse Response Filter Design," in
Handbook for Digital Signal Processing, chapter 4,
New York: Wiley-Interscience, 1993.
.. [4] J. S. Lim, Advanced Topics in Signal Processing.
Englewood Cliffs, N.J.: Prentice Hall, 1988.
.. [5] A. V. Oppenheim, R. W. Schafer, and J. R. Buck,
"Discrete-Time Signal Processing," 2nd edition.
Upper Saddle River, N.J.: Prentice Hall, 1999.
""" # noqa
h = np.asarray(h)
if np.iscomplexobj(h):
raise ValueError('Complex filters not supported')
if h.ndim != 1 or h.size <= 2:
raise ValueError('h must be 1D and at least 2 samples long')
n_half = len(h) // 2
if not np.allclose(h[-n_half:][::-1], h[:n_half]):
warnings.warn('h does not appear to by symmetric, conversion may '
'fail', RuntimeWarning)
if not isinstance(method, string_types) or method not in \
('humomorphic', 'hilbert',):
raise ValueError('method must be "humomorphic" or "hilbert", got %r'
% (method,))
if n_fft is None:
n_fft = 2 ** int(np.ceil(np.log2(2 * (len(h) - 1) / 0.01)))
n_fft = int(n_fft)
if n_fft < len(h):
raise ValueError('n_fft must be at least len(h)==%s' % len(h))
if method == 'hilbert':
w = np.arange(n_fft) * (2 * np.pi / n_fft * n_half)
H = np.real(fft(h, n_fft) * np.exp(1j * w))
dp = max(H) - 1
ds = 0 - min(H)
S = 4. / (np.sqrt(1+dp+ds) + np.sqrt(1-dp+ds)) ** 2
H += ds
H *= S
H = np.sqrt(H, out=H)
H += 1e-10 # ensure that the log does not explode
h_minimum = _dhtm(H)
else: # method == 'humomorphic'
# zero-pad; calculate the DFT
h_temp = np.abs(fft(h, n_fft))
# take 0.25*log(|H|**2) = 0.5*log(|H|)
h_temp += 1e-7 * h_temp[h_temp > 0].min() # don't let log blow up
np.log(h_temp, out=h_temp)
h_temp *= 0.5
# IDFT
h_temp = ifft(h_temp).real
# multiply pointwise by the humomorphic filter
# lmin[n] = 2u[n] - d[n]
win = np.zeros(n_fft)
win[0] = 1
stop = (len(h) + 1) // 2
win[1:stop] = 2
if len(h) % 2:
win[stop] = 1
h_temp *= win
h_temp = ifft(np.exp(fft(h_temp)))
h_minimum = h_temp.real
n_out = n_half + len(h) % 2
return h_minimum[:n_out]
0
Example 44
def _spectral_helper(x, y, fs=1.0, window='hann', nperseg=256,
noverlap=None, nfft=None, detrend='constant',
return_onesided=True, scaling='spectrum', axis=-1,
mode='psd'):
"""
Calculate various forms of windowed FFTs for PSD, CSD, etc.
This is a helper function that implements the commonality between the
psd, csd, and spectrogram functions. It is not designed to be called
externally. The windows are not averaged over; the result from each window
is returned.
Parameters
---------
x : array_like
Array or sequence containing the data to be analyzed.
y : array_like
Array or sequence containing the data to be analyzed. If this is
the same object in memoery as x (i.e. _spectral_helper(x, x, ...)),
the extra computations are spared.
fs : float, optional
Sampling frequency of the time series. Defaults to 1.0.
window : str or tuple or array_like, optional
Desired window to use. See `get_window` for a list of windows and
required parameters. If `window` is array_like it will be used
directly as the window and its length will be used for nperseg.
Defaults to 'hann'.
nperseg : int, optional
Length of each segment. Defaults to 256.
noverlap : int, optional
Number of points to overlap between segments. If None,
``noverlap = nperseg // 2``. Defaults to None.
nfft : int, optional
Length of the FFT used, if a zero padded FFT is desired. If None,
the FFT length is `nperseg`. Defaults to None.
detrend : str or function or False, optional
Specifies how to detrend each segment. If `detrend` is a string,
it is passed as the ``type`` argument to `detrend`. If it is a
function, it takes a segment and returns a detrended segment.
If `detrend` is False, no detrending is done. Defaults to 'constant'.
return_onesided : bool, optional
If True, return a one-sided spectrum for real data. If False return
a two-sided spectrum. Note that for complex data, a two-sided
spectrum is always returned.
scaling : { 'density', 'spectrum' }, optional
Selects between computing the cross spectral density ('density')
where `Pxy` has units of V**2/Hz and computing the cross spectrum
('spectrum') where `Pxy` has units of V**2, if `x` and `y` are
measured in V and fs is measured in Hz. Defaults to 'density'
axis : int, optional
Axis along which the periodogram is computed; the default is over
the last axis (i.e. ``axis=-1``).
mode : str, optional
Defines what kind of return values are expected. Options are ['psd',
'complex', 'magnitude', 'angle', 'phase'].
Returns
-------
freqs : ndarray
Array of sample frequencies.
t : ndarray
Array of times corresponding to each data segment
result : ndarray
Array of output data, contents dependant on *mode* kwarg.
References
----------
.. [1] Stack Overflow, "Rolling window for 1D arrays in Numpy?",
http://stackoverflow.com/a/6811241
.. [2] Stack Overflow, "Using strides for an efficient moving average
filter", http://stackoverflow.com/a/4947453
Notes
-----
Adapted from matplotlib.mlab
.. versionadded:: 0.16.0
"""
if mode not in ['psd', 'complex', 'magnitude', 'angle', 'phase']:
raise ValueError("Unknown value for mode %s, must be one of: "
"'default', 'psd', 'complex', "
"'magnitude', 'angle', 'phase'" % mode)
# If x and y are the same object we can save ourselves some computation.
same_data = y is x
if not same_data and mode != 'psd':
raise ValueError("x and y must be equal if mode is not 'psd'")
axis = int(axis)
# Ensure we have np.arrays, get outdtype
x = np.asarray(x)
if not same_data:
y = np.asarray(y)
outdtype = np.result_type(x,y,np.complex64)
else:
outdtype = np.result_type(x,np.complex64)
if not same_data:
# Check if we can broadcast the outer axes together
xouter = list(x.shape)
youter = list(y.shape)
xouter.pop(axis)
youter.pop(axis)
try:
outershape = np.broadcast(np.empty(xouter), np.empty(youter)).shape
except ValueError:
raise ValueError('x and y cannot be broadcast together.')
if same_data:
if x.size == 0:
return np.empty(x.shape), np.empty(x.shape), np.empty(x.shape)
else:
if x.size == 0 or y.size == 0:
outshape = outershape + (min([x.shape[axis], y.shape[axis]]),)
emptyout = np.rollaxis(np.empty(outshape), -1, axis)
return emptyout, emptyout, emptyout
if x.ndim > 1:
if axis != -1:
x = np.rollaxis(x, axis, len(x.shape))
if not same_data and y.ndim > 1:
y = np.rollaxis(y, axis, len(y.shape))
# Check if x and y are the same length, zero-pad if neccesary
if not same_data:
if x.shape[-1] != y.shape[-1]:
if x.shape[-1] < y.shape[-1]:
pad_shape = list(x.shape)
pad_shape[-1] = y.shape[-1] - x.shape[-1]
x = np.concatenate((x, np.zeros(pad_shape)), -1)
else:
pad_shape = list(y.shape)
pad_shape[-1] = x.shape[-1] - y.shape[-1]
y = np.concatenate((y, np.zeros(pad_shape)), -1)
# X and Y are same length now, can test nperseg with either
if x.shape[-1] < nperseg:
warnings.warn('nperseg = {0:d}, is greater than input length = {1:d}, '
'using nperseg = {1:d}'.format(nperseg, x.shape[-1]))
nperseg = x.shape[-1]
nperseg = int(nperseg)
if nperseg < 1:
raise ValueError('nperseg must be a positive integer')
if nfft is None:
nfft = nperseg
elif nfft < nperseg:
raise ValueError('nfft must be greater than or equal to nperseg.')
else:
nfft = int(nfft)
if noverlap is None:
noverlap = nperseg//2
elif noverlap >= nperseg:
raise ValueError('noverlap must be less than nperseg.')
else:
noverlap = int(noverlap)
# Handle detrending and window functions
if not detrend:
def detrend_func(d):
return d
elif not hasattr(detrend, '__call__'):
def detrend_func(d):
return signaltools.detrend(d, type=detrend, axis=-1)
elif axis != -1:
# Wrap this function so that it receives a shape that it could
# reasonably expect to receive.
def detrend_func(d):
d = np.rollaxis(d, -1, axis)
d = detrend(d)
return np.rollaxis(d, axis, len(d.shape))
else:
detrend_func = detrend
if isinstance(window, string_types) or type(window) is tuple:
win = get_window(window, nperseg)
else:
win = np.asarray(window)
if len(win.shape) != 1:
raise ValueError('window must be 1-D')
if win.shape[0] != nperseg:
raise ValueError('window must have length of nperseg')
if np.result_type(win,np.complex64) != outdtype:
win = win.astype(outdtype)
if mode == 'psd':
if scaling == 'density':
scale = 1.0 / (fs * (win*win).sum())
elif scaling == 'spectrum':
scale = 1.0 / win.sum()**2
else:
raise ValueError('Unknown scaling: %r' % scaling)
else:
scale = 1
if return_onesided is True:
if np.iscomplexobj(x):
sides = 'twosided'
else:
sides = 'onesided'
if not same_data:
if np.iscomplexobj(y):
sides = 'twosided'
else:
sides = 'twosided'
if sides == 'twosided':
num_freqs = nfft
elif sides == 'onesided':
if nfft % 2:
num_freqs = (nfft + 1)//2
else:
num_freqs = nfft//2 + 1
# Perform the windowed FFTs
result = _fft_helper(x, win, detrend_func, nperseg, noverlap, nfft)
result = result[..., :num_freqs]
freqs = fftpack.fftfreq(nfft, 1/fs)[:num_freqs]
if not same_data:
# All the same operations on the y data
result_y = _fft_helper(y, win, detrend_func, nperseg, noverlap, nfft)
result_y = result_y[..., :num_freqs]
result = np.conjugate(result) * result_y
elif mode == 'psd':
result = np.conjugate(result) * result
elif mode == 'magnitude':
result = np.absolute(result)
elif mode == 'angle' or mode == 'phase':
result = np.angle(result)
elif mode == 'complex':
pass
result *= scale
if sides == 'onesided':
if nfft % 2:
result[...,1:] *= 2
else:
# Last point is unpaired Nyquist freq point, don't double
result[...,1:-1] *= 2
t = np.arange(nperseg/2, x.shape[-1] - nperseg/2 + 1, nperseg - noverlap)/float(fs)
if sides != 'twosided' and not nfft % 2:
# get the last value correctly, it is negative otherwise
freqs[-1] *= -1
# we unwrap the phase here to handle the onesided vs. twosided case
if mode == 'phase':
result = np.unwrap(result, axis=-1)
result = result.astype(outdtype)
# All imaginary parts are zero anyways
if same_data and mode != 'complex':
result = result.real
# Output is going to have new last axis for window index
if axis != -1:
# Specify as positive axis index
if axis < 0:
axis = len(result.shape)-1-axis
# Roll frequency axis back to axis where the data came from
result = np.rollaxis(result, -1, axis)
else:
# Make sure window/time index is last axis
result = np.rollaxis(result, -1, -2)
return freqs, t, result
0
Example 45
Project: scikit-beam Source File: spectroscopy.py
def integrate_ROI(x, y, x_min, x_max):
"""Integrate region(s) of input data.
If `x_min` and `x_max` are arrays/lists they must be equal in
length. The values contained in the 'x' must be monotonic (up or
down). The returned value is the sum of all the regions and a
single scalar value is returned. Each region is computed
independently, if regions overlap the overlapped area will be
included multiple times in the final sum.
This function assumes that `y` is a function of
`x` sampled at `x`.
Parameters
----------
x : array
Independent variable, any unit
y : array
Dependent variable, any units
x_min : float or array
The lower edge of the integration region(s)
in units of x.
x_max : float or array
The upper edge of the integration region(s)
in units of x.
Returns
-------
float
The totals integrated value in same units as `y`
"""
# make sure x (x-values) and y (y-values) are arrays
x = np.asarray(x)
y = np.asarray(y)
if x.shape != y.shape:
raise ValueError("Inputs (x and y) must be the same "
"size. x.shape = {0} and y.shape = "
"{1}".format(x.shape, y.shape))
# use np.sign() to obtain array which has evaluated sign changes in all
# diff in input x_value array. Checks and tests are then run on the
# evaluated sign change array.
eval_x_arr_sign = np.sign(np.diff(x))
# check to make sure no outliers exist which violate the monotonically
# increasing requirement, and if exceptions exist, then error points to the
# location within the source array where the exception occurs.
if not np.all(eval_x_arr_sign == eval_x_arr_sign[0]):
error_locations = np.where(eval_x_arr_sign != eval_x_arr_sign[0])[0]
raise ValueError("Independent variable must be monotonically "
"increasing. Erroneous values found at x-value "
"array index locations:\n" +
_formatter_array_regions(x, error_locations))
# check whether the sign of all diff measures are negative in the
# x. If so, then the input array for both x_values and
# count are reversed so that they are positive, and monotonically increase
# in value
if eval_x_arr_sign[0] == -1:
x = x[::-1]
y = y[::-1]
logging.debug("Input values for 'x' were found to be "
"monotonically decreasing. The 'x' and "
"'y' arrays have been reversed prior to "
"integration.")
# up-cast to 1d and make sure it is flat
x_min = np.atleast_1d(x_min).ravel()
x_max = np.atleast_1d(x_max).ravel()
# verify that the number of minimum and maximum boundary values are equal
if len(x_min) != len(x_max):
raise ValueError("integration bounds must have same lengths")
# verify that the specified minimum values are actually less than the
# sister maximum value, and raise error if any minimum value is actually
# greater than the sister maximum value.
if np.any(x_min >= x_max):
raise ValueError("All lower integration bounds must be less than "
"upper integration bounds.")
# check to make sure that all specified minimum and maximum values are
# actually contained within the extents of the independent variable array
if np.any(x_min < x[0]):
error_locations = np.where(x_min < x[0])[0]
raise ValueError("Specified lower integration boundary values are "
"outside the spectrum range. All minimum integration "
"boundaries must be greater than, or equal to the "
"lowest value in spectrum range. The erroneous x_min_"
"array indices are:\n" +
_formatter_array_regions(x_min,
error_locations, window=0))
if np.any(x_max > x[-1]):
error_locations = np.where(x_max > x[-1])[0]
raise ValueError("Specified upper integration boundary values "
"are outside the spectrum range. All maximum "
"integration boundary values must be less "
"than, or equal to the highest value in the spectrum "
"range. The erroneous x_max array indices are: "
"\n" +
_formatter_array_regions(x_max,
error_locations, window=0))
# find the bottom index of each integration bound
bottom_indx = x.searchsorted(x_min)
# find the top index of each integration bound
# NOTE: +1 required for correct slicing for integration function
top_indx = x.searchsorted(x_max) + 1
# set up temporary variables
accuem = 0
# integrate each region
for bot, top in zip(bottom_indx, top_indx):
# Note: If an odd number of intervals is specified, then the
# even='avg' setting calculates and averages first AND last
# N-2 intervals using trapezoidal rule.
# If calculation speed become an issue, then consider changing
# setting to 'first', or 'last' in which case trap rule is only
# applied to either first or last N-2 intervals.
accuem += simps(y[bot:top], x[bot:top], even='avg')
return accuem
0
Example 46
Project: scipy Source File: _binned_statistic.py
def binned_statistic(x, values, statistic='mean',
bins=10, range=None):
"""
Compute a binned statistic for one or more sets of data.
This is a generalization of a histogram function. A histogram divides
the space into bins, and returns the count of the number of points in
each bin. This function allows the computation of the sum, mean, median,
or other statistic of the values (or set of values) within each bin.
Parameters
----------
x : (N,) array_like
A sequence of values to be binned.
values : (N,) array_like or list of (N,) array_like
The data on which the statistic will be computed. This must be
the same shape as `x`, or a set of sequences - each the same shape as
`x`. If `values` is a set of sequences, the statistic will be computed
on each independently.
statistic : string or callable, optional
The statistic to compute (default is 'mean').
The following statistics are available:
* 'mean' : compute the mean of values for points within each bin.
Empty bins will be represented by NaN.
* 'median' : compute the median of values for points within each
bin. Empty bins will be represented by NaN.
* 'count' : compute the count of points within each bin. This is
identical to an unweighted histogram. `values` array is not
referenced.
* 'sum' : compute the sum of values for points within each bin.
This is identical to a weighted histogram.
* function : a user-defined function which takes a 1D array of
values, and outputs a single numerical statistic. This function
will be called on the values in each bin. Empty bins will be
represented by function([]), or NaN if this returns an error.
bins : int or sequence of scalars, optional
If `bins` is an int, it defines the number of equal-width bins in the
given range (10 by default). If `bins` is a sequence, it defines the
bin edges, including the rightmost edge, allowing for non-uniform bin
widths. Values in `x` that are smaller than lowest bin edge are
assigned to bin number 0, values beyond the highest bin are assigned to
``bins[-1]``. If the bin edges are specified, the number of bins will
be, (nx = len(bins)-1).
range : (float, float) or [(float, float)], optional
The lower and upper range of the bins. If not provided, range
is simply ``(x.min(), x.max())``. Values outside the range are
ignored.
Returns
-------
statistic : array
The values of the selected statistic in each bin.
bin_edges : array of dtype float
Return the bin edges ``(length(statistic)+1)``.
binnumber: 1-D ndarray of ints
Indices of the bins (corresponding to `bin_edges`) in which each value
of `x` belongs. Same length as `values`. A binnumber of `i` means the
corresponding value is between (bin_edges[i-1], bin_edges[i]).
See Also
--------
numpy.digitize, numpy.histogram, binned_statistic_2d, binned_statistic_dd
Notes
-----
All but the last (righthand-most) bin is half-open. In other words, if
`bins` is ``[1, 2, 3, 4]``, then the first bin is ``[1, 2)`` (including 1,
but excluding 2) and the second ``[2, 3)``. The last bin, however, is
``[3, 4]``, which *includes* 4.
.. versionadded:: 0.11.0
Examples
--------
>>> from scipy import stats
>>> import matplotlib.pyplot as plt
First some basic examples:
Create two evenly spaced bins in the range of the given sample, and sum the
corresponding values in each of those bins:
>>> values = [1.0, 1.0, 2.0, 1.5, 3.0]
>>> stats.binned_statistic([1, 1, 2, 5, 7], values, 'sum', bins=2)
(array([ 4. , 4.5]), array([ 1., 4., 7.]), array([1, 1, 1, 2, 2]))
Multiple arrays of values can also be passed. The statistic is calculated
on each set independently:
>>> values = [[1.0, 1.0, 2.0, 1.5, 3.0], [2.0, 2.0, 4.0, 3.0, 6.0]]
>>> stats.binned_statistic([1, 1, 2, 5, 7], values, 'sum', bins=2)
(array([[ 4. , 4.5], [ 8. , 9. ]]), array([ 1., 4., 7.]),
array([1, 1, 1, 2, 2]))
>>> stats.binned_statistic([1, 2, 1, 2, 4], np.arange(5), statistic='mean',
... bins=3)
(array([ 1., 2., 4.]), array([ 1., 2., 3., 4.]),
array([1, 2, 1, 2, 3]))
As a second example, we now generate some random data of sailing boat speed
as a function of wind speed, and then determine how fast our boat is for
certain wind speeds:
>>> windspeed = 8 * np.random.rand(500)
>>> boatspeed = .3 * windspeed**.5 + .2 * np.random.rand(500)
>>> bin_means, bin_edges, binnumber = stats.binned_statistic(windspeed,
... boatspeed, statistic='median', bins=[1,2,3,4,5,6,7])
>>> plt.figure()
>>> plt.plot(windspeed, boatspeed, 'b.', label='raw data')
>>> plt.hlines(bin_means, bin_edges[:-1], bin_edges[1:], colors='g', lw=5,
... label='binned statistic of data')
>>> plt.legend()
Now we can use ``binnumber`` to select all datapoints with a windspeed
below 1:
>>> low_boatspeed = boatspeed[binnumber == 0]
As a final example, we will use ``bin_edges`` and ``binnumber`` to make a
plot of a distribution that shows the mean and distribution around that
mean per bin, on top of a regular histogram and the probability
distribution function:
>>> x = np.linspace(0, 5, num=500)
>>> x_pdf = stats.maxwell.pdf(x)
>>> samples = stats.maxwell.rvs(size=10000)
>>> bin_means, bin_edges, binnumber = stats.binned_statistic(x, x_pdf,
... statistic='mean', bins=25)
>>> bin_width = (bin_edges[1] - bin_edges[0])
>>> bin_centers = bin_edges[1:] - bin_width/2
>>> plt.figure()
>>> plt.hist(samples, bins=50, normed=True, histtype='stepfilled',
... alpha=0.2, label='histogram of data')
>>> plt.plot(x, x_pdf, 'r-', label='analytical pdf')
>>> plt.hlines(bin_means, bin_edges[:-1], bin_edges[1:], colors='g', lw=2,
... label='binned statistic of data')
>>> plt.plot((binnumber - 0.5) * bin_width, x_pdf, 'g.', alpha=0.5)
>>> plt.legend(fontsize=10)
>>> plt.show()
"""
try:
N = len(bins)
except TypeError:
N = 1
if N != 1:
bins = [np.asarray(bins, float)]
if range is not None:
if len(range) == 2:
range = [range]
medians, edges, binnumbers = binned_statistic_dd(
[x], values, statistic, bins, range)
return BinnedStatisticResult(medians, edges[0], binnumbers)
0
Example 47
Project: quality-assessment-protocol Source File: qap_workflows_utils.py
def slice_head_mask(infile, transform, standard):
import os
import sys
import nibabel as nb
import numpy as np
import subprocess
import pkg_resources as p
# get file info
infile_img = nb.load(infile)
infile_header = infile_img.get_header()
infile_affine = infile_img.get_affine()
infile_dims = infile_header.get_data_shape()
# these are stored in the files listed below, just here for reference
inpoint_a = "78 -110 -72"
inpoint_b = "-78 -110 -72"
inpoint_c = "0 88 -72" # nose, apparently
# these each contain a set of coordinates for drawing the plane across
# the image (to "slice" it)
inpoint_files = [p.resource_filename("qap", "inpoint_a.txt"),
p.resource_filename("qap", "inpoint_b.txt"),
p.resource_filename("qap", "inpoint_c.txt")]
# let's convert the coordinates into voxel coordinates
coords = []
for inpoint in inpoint_files:
coord_cmd = "std2imgcoord -img %s -std %s -xfm %s -vox %s" \
% (infile, standard, transform, inpoint)
coord_out = subprocess.check_output(coord_cmd, shell=True)
if "Could not" in coord_out:
raise Exception(coord_out)
coords.append(coord_out)
# get the converted coordinates into a list format, and also check to make
# sure they are not "out of bounds"
new_coords = []
for coord in coords:
co_nums = coord.split(" ")
co_nums_newlist = []
for num in co_nums:
if num != "":
co_nums_newlist.append(int(num.split(".")[0]))
for ind in range(0, 3):
if co_nums_newlist[ind] > infile_dims[ind]:
co_nums_newlist[ind] = infile_dims[ind]
elif co_nums_newlist[ind] < 1:
co_nums_newlist[ind] = 1
new_coords.append(co_nums_newlist)
# get the vectors connecting the points
u = []
for a_pt, c_pt in zip(new_coords[0], new_coords[2]):
u.append(int(a_pt - c_pt))
v = []
for b_pt, c_pt in zip(new_coords[1], new_coords[2]):
v.append(int(b_pt - c_pt))
u_vector = np.asarray(u)
v_vector = np.asarray(v)
# vector cross product
n = np.cross(u, v)
# normalize the vector
n = n / np.linalg.norm(n, 2)
constant = np.dot(n, np.asarray(new_coords[0]))
# now determine the z-coordinate for each pair of x,y
plane_dict = {}
for yvox in range(0, infile_dims[1]):
for xvox in range(0, infile_dims[0]):
zvox = (constant - (n[0] * xvox + n[1] * yvox)) / n[2]
zvox = np.floor(zvox)
if zvox < 1:
zvox = 1
elif zvox > infile_dims[2]:
zvox = infile_dims[2]
plane_dict[(xvox, yvox)] = zvox
# create the mask
mask_array = np.zeros(infile_dims)
for x in range(0, infile_dims[0]):
for y in range(0, infile_dims[1]):
for z in range(0, infile_dims[2]):
if plane_dict[(x, y)] > z:
mask_array[x, y, z] = 1
new_mask_img = nb.Nifti1Image(mask_array, infile_affine, infile_header)
infile_filename = infile.split("/")[-1].split(".")[0]
outfile_name = infile_filename + "_slice_mask.nii.gz"
outfile_path = os.path.join(os.getcwd(), outfile_name)
nb.save(new_mask_img, outfile_path)
return outfile_path
0
Example 48
Project: pycalphad Source File: eqplot.py
def eqplot(eq, ax=None, x=None, y=None, z=None, **kwargs):
"""
Plot the result of an equilibrium calculation.
Parameters
----------
eq : xarray.Dataset
Result of equilibrium calculation.
ax : matplotlib.Axes
Default axes used if not specified.
x : StateVariable, optional
y : StateVariable, optional
z : StateVariable, optional
kwargs : kwargs
Passed to `matplotlib.pyplot.scatter`.
Returns
-------
matplotlib AxesSubplot
"""
ax = plt.gca() if ax is None else ax
conds = OrderedDict([(_map_coord_to_variable(key), unpack_condition(np.asarray(value)))
for key, value in sorted(eq.coords.items(), key=str)
if (key == 'T') or (key == 'P') or (key.startswith('X_'))])
indep_comp = [key for key, value in conds.items() if isinstance(key, v.Composition) and len(value) > 1]
indep_pot = [key for key, value in conds.items() if ((key == v.T) or (key == v.P)) and len(value) > 1]
if (len(indep_comp) != 1) or (len(indep_pot) != 1):
raise ValueError('Plot currently requires exactly one composition and one potential coordinate')
indep_comp = indep_comp[0]
indep_pot = indep_pot[0]
x = indep_comp if x is None else x
y = indep_pot if y is None else y
if z is not None:
raise NotImplementedError('3D plotting is not yet implemented')
# TODO: Temporary workaround to fix string encoding issue when loading netcdf files from disk
phases = map(str, sorted(set(np.array(eq.Phase.values.ravel(), dtype='U')) - {''}, key=str))
comps = map(str, sorted(np.array(eq.coords['component'].values, dtype='U'), key=str))
eq['component'] = np.array(eq['component'], dtype='U')
eq['Phase'].values = np.array(eq['Phase'].values, dtype='U')
# Select all two-phase regions
two_phase_indices = np.nonzero(np.sum(eq.Phase.values != '', axis=-1, dtype=np.int) == 2)
found_phases = eq.Phase.values[two_phase_indices][..., :2]
colorlist = {}
# colors from Junwei Huang, March 21 2013
# exclude green and red because of their special meaning on the diagram
colorvalues = ["0000FF", "FFFF00", "FF00FF", "00FFFF", "000000",
"800000", "008000", "000080", "808000", "800080", "008080",
"808080", "C00000", "00C000", "0000C0", "C0C000", "C000C0",
"00C0C0", "C0C0C0", "400000", "004000", "000040", "404000",
"400040", "004040", "404040", "200000", "002000", "000020",
"202000", "200020", "002020", "202020", "600000", "006000",
"000060", "606000", "600060", "006060", "606060", "A00000",
"00A000", "0000A0", "A0A000", "A000A0", "00A0A0", "A0A0A0",
"E00000", "00E000", "0000E0", "E0E000", "E000E0", "00E0E0",
"E0E0E0"]
phasecount = 0
legend_handles = []
for phase in phases:
phase = phase.upper()
colorlist[phase] = "#"+colorvalues[np.mod(phasecount, len(colorvalues))]
legend_handles.append(mpatches.Patch(color=colorlist[phase], label=phase))
phasecount += 1
# position the phase legend
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(handles=legend_handles, loc='center left', bbox_to_anchor=(1, 0.5))
ax.tick_params(axis='both', which='major', labelsize=14)
ax.grid(True)
plotcolors = np.array(list(map(lambda x: [colorlist[x[0]], colorlist[x[1]]], found_phases)), dtype='U')
if isinstance(x, v.Composition):
compositions = eq.X.sel(component=x.species).values[two_phase_indices][..., :2]
else:
raise NotImplementedError('Plotting {} is not yet implemented'.format(x))
# Have to do some extra work to get all potential values for the given tie lines
temps = np.take(eq[str(y)].values, two_phase_indices[list(str(i) for i in conds.keys()).index(str(y))])
if ax is None:
ax = plt.gca()
# Draw zero phase-fraction lines
ax.scatter(compositions[..., 0], temps, s=3, c=plotcolors[..., 0], edgecolors='None', zorder=2, **kwargs)
ax.scatter(compositions[..., 1], temps, s=3, c=plotcolors[..., 1], edgecolors='None', zorder=2, **kwargs)
# Draw tie-lines
tielines = np.array([np.concatenate((compositions[..., 0][..., np.newaxis], temps[..., np.newaxis]), axis=-1),
np.concatenate((compositions[..., 1][..., np.newaxis], temps[..., np.newaxis]), axis=-1)])
tielines = np.rollaxis(tielines, 1)
lc = mc.LineCollection(tielines, zorder=1, colors=[0, 1, 0, 1], linewidths=[0.5, 0.5])
ax.add_collection(lc)
plot_title = '-'.join([x.title() for x in sorted(comps) if x != 'VA'])
ax.set_title(plot_title, fontsize=20)
ax.set_xlim([np.min(conds[indep_comp])-1e-2, np.max(conds[indep_comp])+1e-2])
ax.set_ylim([np.min(conds[indep_pot]), np.max(conds[indep_pot])])
if isinstance(x, v.Composition):
ax.set_xlabel('X({})'.format(indep_comp.species), labelpad=15, fontsize=20)
else:
ax.set_xlabel(indep_comp, labelpad=15, fontsize=20)
ax.set_ylabel(_plot_labels[indep_pot], fontsize=20)
return ax
0
Example 49
Project: python-control Source File: timeresp.py
def forced_response(sys, T=None, U=0., X0=0., transpose=False):
"""Simulate the output of a linear system.
As a convenience for parameters `U`, `X0`:
Numbers (scalars) are converted to constant arrays with the correct shape.
The correct shape is inferred from arguments `sys` and `T`.
For information on the **shape** of parameters `U`, `T`, `X0` and
return values `T`, `yout`, `xout`, see :ref:`time-series-convention`.
Parameters
----------
sys: LTI (StateSpace, or TransferFunction)
LTI system to simulate
T: array-like
Time steps at which the input is defined; values must be evenly spaced.
U: array-like or number, optional
Input array giving input at each time `T` (default = 0).
If `U` is ``None`` or ``0``, a special algorithm is used. This special
algorithm is faster than the general algorithm, which is used otherwise.
X0: array-like or number, optional
Initial condition (default = 0).
transpose: bool
If True, transpose all input and output arrays (for backward
compatibility with MATLAB and scipy.signal.lsim)
Returns
-------
T: array
Time values of the output.
yout: array
Response of the system.
xout: array
Time evolution of the state vector.
See Also
--------
step_response, initial_response, impulse_response
Examples
--------
>>> T, yout, xout = forced_response(sys, T, u, X0)
See :ref:`time-series-convention`.
"""
if not isinstance(sys, LTI):
raise TypeError('Parameter ``sys``: must be a ``LTI`` object. '
'(For example ``StateSpace`` or ``TransferFunction``)')
sys = _convertToStateSpace(sys)
A, B, C, D = np.asarray(sys.A), np.asarray(sys.B), np.asarray(sys.C), \
np.asarray(sys.D)
# d_type = A.dtype
n_states = A.shape[0]
n_inputs = B.shape[1]
n_outputs = C.shape[0]
# Set and/or check time vector in discrete time case
if isdtime(sys, strict=True):
if T is None:
if U is None:
raise ValueError('Parameters ``T`` and ``U`` can\'t both be'
'zero for discrete-time simulation')
# Set T to integers with same length as U
T = range(len(U))
else:
# Make sure the input vector and time vector have same length
# TODO: allow interpolation of the input vector
if len(U) != len(T):
ValueError('Pamameter ``T`` must have same length as'
'input vector ``U``')
# Test if T has shape (n,) or (1, n);
# T must be array-like and values must be increasing.
# The length of T determines the length of the input vector.
if T is None:
raise ValueError('Parameter ``T``: must be array-like, and contain '
'(strictly monotonic) increasing numbers.')
T = _check_convert_array(T, [('any',), (1, 'any')],
'Parameter ``T``: ', squeeze=True,
transpose=transpose)
dt = T[1] - T[0]
if not np.allclose(T[1:] - T[:-1], dt):
raise ValueError('Parameter ``T``: time values must be equally spaced.')
n_steps = len(T) # number of simulation steps
# create X0 if not given, test if X0 has correct shape
X0 = _check_convert_array(X0, [(n_states,), (n_states, 1)],
'Parameter ``X0``: ', squeeze=True)
xout = np.zeros((n_states, n_steps))
xout[:, 0] = X0
yout = np.zeros((n_outputs, n_steps))
# Separate out the discrete and continuous time cases
if isctime(sys):
# Solve the differential equation, copied from scipy.signal.ltisys.
dot, squeeze, = np.dot, np.squeeze # Faster and shorter code
# Faster algorithm if U is zero
if U is None or (isinstance(U, (int, float)) and U == 0):
# Solve using matrix exponential
expAdt = sp.linalg.expm(A * dt)
for i in range(1, n_steps):
xout[:, i] = dot(expAdt, xout[:, i-1])
yout = dot(C, xout)
# General algorithm that interpolates U in between output points
else:
# Test if U has correct shape and type
legal_shapes = [(n_steps,), (1, n_steps)] if n_inputs == 1 else \
[(n_inputs, n_steps)]
U = _check_convert_array(U, legal_shapes,
'Parameter ``U``: ', squeeze=False,
transpose=transpose)
# convert 1D array to 2D array with only one row
if len(U.shape) == 1:
U = U.reshape(1, -1) # pylint: disable=E1103
# Algorithm: to integrate from time 0 to time dt, with linear
# interpolation between inputs u(0) = u0 and u(dt) = u1, we solve
# xdot = A x + B u, x(0) = x0
# udot = (u1 - u0) / dt, u(0) = u0.
#
# Solution is
# [ x(dt) ] [ A*dt B*dt 0 ] [ x0 ]
# [ u(dt) ] = exp [ 0 0 I ] [ u0 ]
# [u1 - u0] [ 0 0 0 ] [u1 - u0]
M = np.bmat([[A * dt, B * dt, np.zeros((n_states, n_inputs))],
[np.zeros((n_inputs, n_states + n_inputs)),
np.identity(n_inputs)],
[np.zeros((n_inputs, n_states + 2 * n_inputs))]])
expM = sp.linalg.expm(M)
Ad = expM[:n_states, :n_states]
Bd1 = expM[:n_states, n_states+n_inputs:]
Bd0 = expM[:n_states, n_states:n_states + n_inputs] - Bd1
for i in range(1, n_steps):
xout[:, i] = (dot(Ad, xout[:, i-1]) + dot(Bd0, U[:, i-1]) +
dot(Bd1, U[:, i]))
yout = dot(C, xout) + dot(D, U)
yout = squeeze(yout)
xout = squeeze(xout)
else:
# Discrete time simulation using signal processing toolbox
dsys = (A, B, C, D, sys.dt)
tout, yout, xout = sp.signal.dlsim(dsys, U, T, X0)
# See if we need to transpose the data back into MATLAB form
if (transpose):
T = np.transpose(T)
yout = np.transpose(yout)
xout = np.transpose(xout)
return T, yout, xout
0
Example 50
Project: scikit-beam Source File: utils.py
def grid3d(q, img_stack,
nx=None, ny=None, nz=None,
xmin=None, xmax=None, ymin=None,
ymax=None, zmin=None, zmax=None,
binary_mask=None):
"""Grid irregularly spaced data points onto a regular grid via histogramming
This function will process the set of reciprocal space values (q), the
image stack (img_stack) and grid the image data based on the bounds
provided, using defaults if none are provided.
Parameters
----------
q : ndarray
(Qx, Qy, Qz) - HKL values - Nx3 array
img_stack : ndarray
Intensity array of the images
dimensions are: [num_img][num_rows][num_cols]
nx : int, optional
Number of voxels along x
ny : int, optional
Number of voxels along y
nz : int, optional
Number of voxels along z
xmin : float, optional
Minimum value along x. Defaults to smallest x value in q
ymin : float, optional
Minimum value along y. Defaults to smallest y value in q
zmin : float, optional
Minimum value along z. Defaults to smallest z value in q
xmax : float, optional
Maximum value along x. Defaults to largest x value in q
ymax : float, optional
Maximum value along y. Defaults to largest y value in q
zmax : float, optional
Maximum value along z. Defaults to largest z value in q
binary_mask : ndarray, optional
The binary mask provides a mechanism to remove unwanted pixels
from the images.
Binary mask can be two different shapes.
- 1: 2-D with binary_mask.shape == np.asarray(img_stack[0]).shape
- 2: 3-D with binary_mask.shape == np.asarray(img_stack).shape
Returns
-------
mean : ndarray
intensity grid. The values in this grid are the
mean of the values that fill with in the grid.
occupancy : ndarray
The number of data points that fell in the grid.
std_err : ndarray
This is the standard error of the value in the
grid box.
bounds : list
tuple of (min, max, step) for x, y, z in order: [x_bounds,
y_bounds, z_bounds]
"""
try:
from ..ext import ctrans
except ImportError:
raise NotImplementedError(
"ctrans is not available on your platform. See "
"https://github.com/scikit-beam/scikit-beam/issues/418 "
"to follow updates to this problem.")
# validate input
img_stack = np.asarray(img_stack)
# todo determine if we're going to support masked arrays
# todo masked arrays seemed to have been punted to `process_to_q`
# check to see if the binary mask and the image stack are identical shapes
if binary_mask is None or binary_mask.shape == img_stack.shape:
# do a dance :)
pass
elif binary_mask.shape == img_stack[0].shape:
# this is still a valid mask, so make it the same dimensions
# as img_stack.
# should probably change this to use something similar to:
# todo http://stackoverflow.com/questions/5564098/
binary_mask = np.tile(np.ravel(binary_mask), img_stack.shape[0])
else:
raise ValueError("The binary mask must be the same shape as the"
"img_stack ({0}) or a single image in the image "
"stack ({1}). The input binary mask is shaped ({2})"
"".format(img_stack.shape, img_stack[0].shape,
binary_mask.shape))
q = np.atleast_2d(q)
if q.ndim != 2:
raise ValueError("q.ndim must be a 2-D array of shape Nx3 array. "
"You provided an array with {0} dimensions."
"".format(q.ndim))
if q.shape[1] != 3:
raise ValueError("The shape of q must be an Nx3 array, not {0}X{1}"
" which you provided.".format(*q.shape))
# set defaults for qmin, qmax, dq
qmin = np.min(q, axis=0)
qmax = np.max(q, axis=0)
dqn = [_defaults['nx'], _defaults['ny'], _defaults['nz']]
# pad the upper edge by just enough to ensure that all of the
# points are in-bounds with the binning rules: lo <= val < hi
qmax += np.spacing(qmax)
# check for non-default input
for target, input_vals in ((dqn, (nx, ny, nz)),
(qmin, (xmin, ymin, zmin)),
(qmax, (xmax, ymax, zmax))):
for j, in_val in enumerate(input_vals):
if in_val is not None:
target[j] = in_val
# format bounds
bounds = np.array([qmin, qmax, dqn]).T
# creating (Qx, Qy, Qz, I) Nx4 array - HKL values and Intensity
# getting the intensity value for each pixel
q = np.insert(q, 3, np.ravel(img_stack), axis=1)
if binary_mask is not None:
q = q[np.ravel(binary_mask)]
# 3D grid of the data set
# starting time for gridding
t1 = time.time()
# call the c library
total, total2, occupancy, std_err = ctrans.grid3d(q, qmin, qmax, dqn)
mean = total / occupancy
# ending time for the gridding
t2 = time.time()
logger.info("Done processed in {0} seconds".format(t2 - t1))
# No. of values zero in the grid
empt_nb = (occupancy == 0).sum()
# log some information about the grid at the debug level
logger.debug("There are %2e bins in the grid", mean.size)
if empt_nb:
logger.debug("There are %.2e values zero in the grid", empt_nb)
return mean, occupancy, std_err, bounds