def labelmap(bmap, mask, opts): # Local Variables: nearest, dist, thetaBin, V, locs, ind, Theta, nOrients, bmap, Dx, Dy, theta, nDists, gtWidth, C, labels, nLabels, I, J, step, R, U, T, dx, dy, Nearest, Dist, h, k, Labels, mask, edges, w, opts # Function calls: binangles360, angleimage, cosd, distBin, all, double, cross, sign, labelmap, meshgrid, ind2sub, bwdist, sind, repmat, find, size #% [Labels,Dist,Theta] = labelmap( bmap, mask, opts ) #% It is assumed that bmap was already cleaned (cleanbmap.m) gtWidth = opts.gtWidth nOrients = opts.nOrients nDists = opts.nDists nLabels = np.dot(nOrients, nDists) #% calculate distances and angles [h, w] = matcompat.size(bmap) [Dist, Nearest] = bwdist(bmap) Nearest = np.double(Nearest) Theta = angleimage(bmap, (opts.angleRad)) [R, C] = ind2sub(np.array(np.hstack((h, w))), Nearest) [J, I] = matcompat.meshgrid(np.arange(1., (w)+1), np.arange(1., (h)+1)) Dx = C-J Dy = -(R-I) #% focus on locations indicated by mask locs = nonzero(mask) dx = Dx[int(locs)-1] dy = Dy[int(locs)-1] nearest = Nearest[int(locs)-1] theta = Theta[int(nearest)-1] thetaBin = binangles360(theta, (nOrients*2.)) T = np.array(np.hstack((sind(theta), cosd(theta), 0.*theta))) V = np.array(np.hstack((dy, dx, 0.*dx))) U = np.cross(T, V) dist = Dist[int(locs)-1]*np.sign((-U[:,2])) #% standardize ind = thetaBin > nOrients dist[int(ind)-1] = -dist[int(ind)-1] thetaBin[int(ind)-1] = thetaBin[int(ind)-1]-nOrients #% bin distance k = (nDists-1.)/2. step = matdiv(gtWidth-2., nDists-1.) edges = np.array(np.hstack((np.dot(step, np.array(np.hstack((np.arange(1., (k)+1)))))-step/2., gtWidth/2.))) labels = np.dot(thetaBin-1., nDists)+distBin.conj().T #% IMPORTANT assertion that has caught MANY bugs #% write result images Labels = matcompat.repmat((nLabels+1.), np.array(np.hstack((h, w)))) Dist = Labels Theta = Labels Labels[int(locs)-1] = labels Dist[int(locs)-1] = distBin Theta[int(locs)-1] = thetaBin return [Labels, Dist, Theta]
def myfun(beta, X): # Local Variables: D, F, J, N, beta, X # Function calls: myfun, double, nargout, exp, repmat, size #% size( beta ) = [D 1] #% size( X ) = [N D] #% size( X*beta ) = [N 1] [N, D] = matcompat.size(X) F = 1.-np.exp(np.dot(-X, beta)) F = np.double(F) if nargout > 1.: J = matcompat.repmat((1.-F), np.array(np.hstack((1., D))))*X J = np.double(J) return [F, J]
#% current pattern generator xCollector = np.zeros(Netsize, learnLength) xOldCollector = np.zeros(Netsize, learnLength) pCollector = np.zeros(1., learnLength) x = np.zeros(Netsize, 1.) for n in np.arange(1., (washoutLength + learnLength) + 1): u = patt[int(n) - 1] #% pattern input xOld = x x = np.tanh((np.dot(Wstar, x) + np.dot(Win, u) + Wbias)) if n > washoutLength: xCollector[:, int((n - washoutLength)) - 1] = x xOldCollector[:, int((n - washoutLength)) - 1] = xOld pCollector[0, int((n - washoutLength)) - 1] = u xCollectorCentered = xCollector - matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0, int(p) - 1] = xCollectorCentered xCollectors.cell[0, int(p) - 1] = xCollector R = matdiv(np.dot(xCollector, xCollector.conj().T), learnLength) [Ux, Sx, Vx] = plt.svd(R) SRCollectors.cell[0, int(p) - 1] = Sx URCollectors.cell[0, int(p) - 1] = Ux patternRs.cell[int(p) - 1] = R startXs[:, int(p) - 1] = x train_xPL.cell[0, int(p) - 1] = xCollector[0:5., 0:signalPlotLength] train_pPL.cell[0, int(p) - 1] = pCollector[0, 0:signalPlotLength] patternCollectors.cell[0, int(p) - 1] = pCollector allTrainArgs[:, int(np.dot(p - 1., learnLength) + 1.) - 1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:,
def kntrefine(knots, n_sub, degree, regularity): # Local Variables: knots, old_mult, degree, insk, nz, varargout, regularity, aux_knots, n_sub, idim, new_knots, ik, min_mult, zeta, z, rknots, mult, deg # Function calls: repmat, max, sum, kntrefine, nargout, ones, linspace, numel, error, iscell, unique, vec if iscell(knots): if numel(n_sub) != numel(degree) or numel(n_sub) != numel(regularity) or numel(n_sub) != numel(knots): matcompat.error('kntrefine: n_sub, degree and regularity must have the same length as the number of knot vectors') aux_knots = knots else: if numel(n_sub) != numel(degree) or numel(n_sub) != numel(regularity) or numel(n_sub) != 1.: matcompat.error('kntrefine: n_sub, degree and regularity must have the same length as the number of knot vectors') aux_knots = cellarray(np.hstack((knots))) if nargout == 3.: for idim in np.arange(1., (numel(n_sub))+1): if degree[int(idim)-1]+1. != np.sum((aux_knots.cell[int(idim)-1] == aux_knots.cell[int(idim)-1,0]())): matcompat.error('kntrefine: new_knots is only computed when the degree is maintained') for idim in np.arange(1., (numel(n_sub))+1): min_mult = degree[int(idim)-1]-regularity[int(idim)-1] z = np.unique(aux_knots.cell[int(idim)-1]) nz = numel(z) deg = np.sum((aux_knots.cell[int(idim)-1] == z[0]))-1. rknots.cell[int(idim)-1] = z[int(np.ones(1., (degree[int(idim)-1]+1.)))-1] new_knots.cell[int(idim)-1] = np.array([]) for ik in np.arange(2., (nz)+1): insk = np.linspace(z[int((ik-1.))-1], z[int(ik)-1], (n_sub[int(idim)-1]+2.)) insk = vec(matcompat.repmat(insk[1:0-1.], min_mult, 1.)).conj().T old_mult = np.sum((aux_knots.cell[int(idim)-1] == z[int(ik)-1])) mult = matcompat.max(min_mult, (degree[int(idim)-1]-deg+old_mult)) rknots.cell[int(idim)-1] = np.array(np.hstack((rknots.cell[int(idim)-1], insk, z[int(np.dot(ik, np.ones(1., mult)))-1]))) new_knots.cell[int(idim)-1] = np.array(np.hstack((new_knots.cell[int(idim)-1], insk, z[int(np.dot(ik, np.ones(1., (mult-old_mult))))-1]))) zeta.cell[int(idim)-1] = np.unique(rknots.cell[int(idim)-1]) if not iscell(knots): rknots = rknots.cell[0] zeta = zeta.cell[0] new_knots = new_knots.cell[0] varargout.cell[0] = rknots varargout.cell[1] = zeta varargout.cell[2] = new_knots else: for idim in np.arange(1., (numel(n_sub))+1): min_mult = degree[int(idim)-1]-regularity[int(idim)-1] z = np.unique(aux_knots.cell[int(idim)-1]) nz = numel(z) deg = np.sum((aux_knots.cell[int(idim)-1] == z[0]))-1. rknots.cell[int(idim)-1] = z[int(np.ones(1., (degree[int(idim)-1]+1.)))-1] for ik in np.arange(2., (nz)+1): insk = np.linspace(z[int((ik-1.))-1], z[int(ik)-1], (n_sub[int(idim)-1]+2.)) insk = vec(matcompat.repmat(insk[1:0-1.], min_mult, 1.)).conj().T old_mult = np.sum((aux_knots.cell[int(idim)-1] == z[int(ik)-1])) mult = matcompat.max(min_mult, (degree[int(idim)-1]-deg+old_mult)) rknots.cell[int(idim)-1] = np.array(np.hstack((rknots.cell[int(idim)-1], insk, z[int(np.dot(ik, np.ones(1., mult)))-1]))) zeta.cell[int(idim)-1] = np.unique(rknots.cell[int(idim)-1]) if not iscell(knots): rknots = rknots.cell[0] zeta = zeta.cell[0] varargout.cell[0] = rknots if nargout == 2.: varargout.cell[1] = zeta return [varargout]
xOldCollector = np.zeros(Netsize, learnLength) pCollector = np.zeros(1., learnLength) x = np.zeros(Netsize, 1.) for n in np.arange(1., (washoutLength+learnLength)+1): u = patt[int(n)-1] #% pattern input xOld = x x = np.tanh((np.dot(Wstar, x)+np.dot(Win, u)+Wbias)) if n > washoutLength: xCollector[:,int((n-washoutLength))-1] = x xOldCollector[:,int((n-washoutLength))-1] = xOld pCollector[0,int((n-washoutLength))-1] = u xCollectorCentered = xCollector-matcompat.repmat(np.mean(xCollector, 2.), 1., learnLength) xCollectorsCentered.cell[0,int(p)-1] = xCollectorCentered xCollectors.cell[0,int(p)-1] = xCollector R = matdiv(np.dot(xCollector, xCollector.conj().T), learnLength) [Ux, Sx, Vx] = plt.svd(R) SRCollectors.cell[0,int(p)-1] = Sx URCollectors.cell[0,int(p)-1] = Ux patternRs.cell[int(p)-1] = R startXs[:,int(p)-1] = x train_xPL.cell[0,int(p)-1] = xCollector[:,0:signalPlotLength] train_pPL.cell[0,int(p)-1] = pCollector[0,0:signalPlotLength] patternCollectors.cell[0,int(p)-1] = pCollector allTrainArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = pCollector allTrainTargs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = np.dot(Win, pCollector)
def aveknt(varargin): # Local Variables: ndim, nrb, onedim, idim, varargin, knt_aux, knt, pts, order # Function calls: false, nargin, reshape, sum, isfield, cell, aveknt, zeros, numel, error, iscell, repmat, true #% AVEKNT: compute the knot averages (Greville points) of a knot vector #% #% Calling Sequence: #% #% pts = aveknt (knt, p) #% pts = aveknt (nrb) #% #% INPUT: #% #% knt - knot sequence #% p - spline order (degree + 1) #% nrb - NURBS structure (see nrbmak) #% #% OUTPUT: #% #% pts - average knots. If the input is a NURBS, it gives a cell-array, #% with the average knots in each direction #% #% See also: #% #% Copyright (C) 2016 Rafael Vazquez #% #% This program is free software: you can redistribute it and/or modify #% it under the terms of the GNU General Public License as published by #% the Free Software Foundation, either version 3 of the License, or #% (at your option) any later version. #% This program is distributed in the hope that it will be useful, #% but WITHOUT ANY WARRANTY; without even the implied warranty of #% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #% GNU General Public License for more details. #% #% You should have received a copy of the GNU General Public License #% along with this program. If not, see <http://www.gnu.org/licenses/>. if nargin == 1.: if isfield(varargin.cell[0], 'form'): nrb = varargin.cell[0] knt = nrb.knots order = nrb.order else: matcompat.error( 'The input should be a NURBS structure, or a knot vector and the order. See the help for details' ) elif nargin == 2.: knt = varargin.cell[0] order = varargin.cell[1] else: matcompat.error( 'The input should be a NURBS structure, or a knot vector and the order. See the help for details' ) onedim = false if not iscell(knt): knt = cellarray(np.hstack((knt))) onedim = true ndim = numel(knt) pts = cell(ndim, 1.) for idim in np.arange(1., (ndim) + 1): if numel(knt.cell[int(idim) - 1]) < order[int(idim) - 1] + 1.: matcompat.error( 'The knot vector must contain at least p+2 knots, with p the degree' ) knt_aux = matcompat.repmat(knt.cell[int(idim) - 1, 1:0 - 1.](), 1., (order[int(idim) - 1] - 1.)) knt_aux = np.array( np.vstack((np.hstack((knt_aux.flatten(1))), np.hstack((np.zeros((order[int(idim) - 1] - 1.), 1.)))))) knt_aux = np.reshape(knt_aux, np.array([]), (order[int(idim) - 1] - 1.)) pts.cell[int(idim) - 1] = matdiv(np.sum(knt_aux.T, 1.), order[int(idim) - 1] - 1.) pts.cell[int(idim) - 1] = pts.cell[int(idim) - 1, 0:0 - order[int(idim) - 1] + 1.]() if onedim: pts = pts.cell[0] #%!test #%! knt = [0 0 0 0.5 1 1 1]; #%! pts = aveknt (knt, 3); #%! assert (pts - [0 1/4 3/4 1] < 1e-14) #%! #%!test #%! knt = {[0 0 0 0.5 1 1 1] [0 0 0 0 1/3 2/3 1 1 1 1]}; #%! pts = aveknt (knt, [3 4]); #%! assert (pts{1} - [0 1/4 3/4 1] < 1e-14); #%! assert (pts{2} - [0 1/9 1/3 2/3 8/9 1] < 1e-14); #%! #%!test #%! nrb = nrb4surf([0 0], [1 0], [0 1], [1 1]); #%! nrb = nrbkntins (nrbdegelev (nrb, [1 2]), {[1/2] [1/3 2/3]}); #%! pts = aveknt (nrb); #%! assert (pts{1} - [0 1/4 3/4 1] < 1e-14); #%! assert (pts{2} - [0 1/9 1/3 2/3 8/9 1] < 1e-14); return [pts]
def cohbias(v, cb): # Local Variables: A, c, f, i1, cb, i3, i2, M, ec, n, ct, pl, expect, v, z, k, cu, qans # Function calls: disp, isnan, repmat, cohbias, interp1, sum, sqrt, nargin, length, strncmpi, input, exist, pi, find, gamma if nargin<2.: help(cohbias) return [] if v<2.: np.disp('Warning: degress of freedom must be greater or equal to two.') return [] if np.logical_or(cb<0., cb > 1.): np.disp('Warning: biased coherence should be between zero and one, inclusive.') return [] if v > 50.: np.disp('using 50 degrees of freedom') v = 50. #%if nargin==0; help cohbias.m; return; end; if exist('cohbias.mat') == 0.: #%Cohbias.mat file should be down-loaded with cmtm.m and cohbias.m #%The routine is included primarily to show how it was created. n = np.arange(2., 51.0, 1.) c = np.arange(.1, 1.0001, .0001) np.disp('-- The file cohbias.mat does not exist within the path.') qans = input('-- To create this file now enter \'y\' or to skip \'n\'. \n--> ', 's') qans if strncmpi(qans, 'y', 1.): z = np.arange(0., 1.1, .1) for i3 in np.arange(1., (length(n))+1): np.disp(n[int(i3)-1]) for i2 in np.arange(1., (length(c)-1.)+1): for i1 in np.arange(1., (length(z))+1): A[0] = 1. #%Calculated according to: Amos and Koopmans, "Tables of the distribution of the #%coefficient of coherence for stationary bivariate Gaussian processes", Sandia #%Corporation, 1963 #% #%Also see the manuscript of Wunsch, C. "Time-Series Analysis. A Heuristic Primer". for k in np.arange(1., (n[int(i3)-1]-1.)+1): A[int((k+1.))-1] = np.dot(matdiv(np.dot(np.dot(A[int(k)-1], n[int(i3)-1]-k), 2.*k-1.), np.dot(2.*n[int(i3)-1]-2.*k+1., k)), matdiv(1.-np.dot(c[int(i2)-1], z[int(i1)-1]), 1.+np.dot(c[int(i2)-1], z[int(i1)-1]))**2.) f[int(i1)-1] = np.dot(matdiv(np.dot(matdiv(np.dot(np.dot(np.dot(2.*(n[int(i3)-1]-1.), matixpower(1.-c[int(i2)-1]**2., n[int(i3)-1])), z[int(i1)-1]), matixpower(1.-z[int(i1)-1]**2., n[int(i3)-1]-2.)), np.dot(1.+np.dot(c[int(i2)-1], z[int(i1)-1]), matixpower(1.-np.dot(c[int(i2)-1], z[int(i1)-1]), 2.*n[int(i3)-1]-1.))), plt.gamma((n[int(i3)-1]-.5))), np.dot(np.sqrt(np.pi), plt.gamma(n[int(i3)-1]))), np.sum(A)) #%Use a quadratic Newton-Cotes methods to determine the cumulative sum for i1 in np.arange(2., (length(f)/2.)+1): M[int(i1)-1] = np.dot(np.array(np.hstack((f[int((2.*(i0.)+1.))-1], np.dot(+4., f[int((2.*i1))-1]), +f[int((2.*i1+1.))-1]))), z[int((2.*i1))-1]) expect[int(i3)-1,int(i2)-1] = matdiv(np.sum(np.array(np.hstack((M, 1.)))), 6.*length(M)) expect[int(i3)-1,int((i2+1.))-1] = 1. #% save cohbias.mat expect n c; else: #%if skip cohbias.mat calculation expect = matcompat.repmat(c, length(n), 1.) #%stop qans condition else: #%if cohbias.mat already exists #%load cohbias.mat expect n c; #%stop cohbias calculation cb = cb.flatten(1) c = c.flatten(1) n = n.flatten(1) v = v.flatten(1) for ct in np.arange(1., (length(c))+1): ec[int(ct)-1] = interp1(n, expect[:,int(ct)-1], v) for ct in np.arange(1., (length(cb))+1): cu[int(ct)-1] = interp1(ec, c, cb[int(ct)-1]) cu = cu.flatten(1) pl = nonzero(np.logical_and(np.logical_and(np.isnan(cu) == 1., cb<1.), cb >= 0.)) #%If cu is NaN while cb is between (0,1) cu[int(pl)-1] = 0. return [cu]
preview(vid) start(vid) frames = getdata(vid) np.delete(vid) clear(vid) nFrames = matcompat.size(frames, 4.) first_frame = frames[:,:,:,0] rect = np.array(np.hstack((910., 420., 90., 50.))) first_region = imcrop(first_frame, rect) frame_regions = matcompat.repmat(np.uint8(0.), np.array(np.hstack((matcompat.size(first_region), nFrames)))) for count in np.arange(1., (nFrames)+1): frame_regions[:,:,:,int(count)-1] = imcrop(frames[:,:,:,int(count)-1], rect) seg_pend = false(np.array(np.hstack((matcompat.size(first_region, 1.), matcompat.size(first_region, 2.), nFrames)))) centroids = np.zeros(nFrames, 2.) se_disk = strel('disk', 3.) for count in np.arange(1., (nFrames)+1): fr = frame_regions[:,:,:,int(count)-1] gfr = rgb2gray(fr) gfr = imcomplement(gfr) bw = im2bw(gfr, .65) #% threshold is determined experimentally