def _quantize(self, vec): """ Given a point in space, partition space into little cubes so that when the time comes, it will be quick to locate and search the cubes right around a point. """ maxradius = self._maxradius return (int(floor(vec[0] / maxradius)), int(floor(vec[1] / maxradius)), int(floor(vec[2] / maxradius)))
def __init__(self, shp, ptlist, origin, selSense, **opts): """ ptlist is a list of 3d points describing a selection. origin is the center of view, and normal gives the direction of the line of light. Form a structure for telling whether arbitrary points fall inside the curve from the point of view. """ # bruce 041214 rewrote some of this method simple_shape_2d.__init__(self, shp, ptlist, origin, selSense, opts) # bounding rectangle, in integers (scaled 8 to the angstrom) ibbhi = array(map(int, ceil(8 * self.bboxhi) + 2)) ibblo = array(map(int, floor(8 * self.bboxlo) - 2)) bboxlo = self.bboxlo # draw the curve in these matrices and fill it # [bruce 041214 adds this comment: this might be correct but it's very # inefficient -- we should do it geometrically someday. #e] mat = zeros(ibbhi - ibblo) mat1 = zeros(ibbhi - ibblo) mat1[0, :] = 1 mat1[-1, :] = 1 mat1[:, 0] = 1 mat1[:, -1] = 1 pt2d = self.pt2d pt0 = pt2d[0] for pt in pt2d[1:]: l = ceil(vlen(pt - pt0) * 8) if l < 0.01: continue v = (pt - pt0) / l for i in range(1 + int(l)): ij = 2 + array(map(int, floor((pt0 + v * i - bboxlo) * 8))) mat[ij] = 1 pt0 = pt mat1 += mat fill(mat1, array([1, 1]), 1) mat1 -= mat # Which means boundary line is counted as inside the shape. # boolean raster of filled-in shape self.matrix = mat1 ## For any element inside the matrix, if it is 0, then it's inside. # where matrix[0, 0] is in x, y space self.matbase = ibblo # axes of the plane; only used for debugging self.x = self.right self.y = self.up self.z = self.normal
def __init__(self, shp, ptlist, origin, selSense, **opts): """ ptlist is a list of 3d points describing a selection. origin is the center of view, and normal gives the direction of the line of light. Form a structure for telling whether arbitrary points fall inside the curve from the point of view. """ # bruce 041214 rewrote some of this method simple_shape_2d.__init__( self, shp, ptlist, origin, selSense, opts) # bounding rectangle, in integers (scaled 8 to the angstrom) ibbhi = array(map(int, ceil(8 * self.bboxhi)+2)) ibblo = array(map(int, floor(8 * self.bboxlo)-2)) bboxlo = self.bboxlo # draw the curve in these matrices and fill it # [bruce 041214 adds this comment: this might be correct but it's very # inefficient -- we should do it geometrically someday. #e] mat = zeros(ibbhi - ibblo) mat1 = zeros(ibbhi - ibblo) mat1[0,:] = 1 mat1[-1,:] = 1 mat1[:,0] = 1 mat1[:,-1] = 1 pt2d = self.pt2d pt0 = pt2d[0] for pt in pt2d[1:]: l = ceil(vlen(pt - pt0)*8) if l<0.01: continue v=(pt - pt0)/l for i in range(1 + int(l)): ij = 2 + array(map(int, floor((pt0 + v * i - bboxlo)*8))) mat[ij]=1 pt0 = pt mat1 += mat fill(mat1, array([1, 1]),1) mat1 -= mat #Which means boundary line is counted as inside the shape. # boolean raster of filled-in shape self.matrix = mat1 ## For any element inside the matrix, if it is 0, then it's inside. # where matrix[0, 0] is in x, y space self.matbase = ibblo # axes of the plane; only used for debugging self.x = self.right self.y = self.up self.z = self.normal
def gen_wav(seconds, frequency, amplitudePercentOfMax, samplesPerSecond): # calculate frequency as radians/sample radiansPerSample = (2.0 * pi * frequency) / samplesPerSecond numberOfSamples = int(seconds*samplesPerSecond) maxAmplitude = (amplitudePercentOfMax / 100.0) * 32767 samples = sin(arange(numberOfSamples) * radiansPerSample) * maxAmplitude # round and convert to array of 16-bit shorts return floor(samples + 0.5).astype('s')
def _hashAtomPos(self, pos): return int(dot(V(1000000, 1000, 1), floor(pos * 1.2)))
def _baumWelsh(self,obsIndices,maxiter,scale_every=50): """Uses Baum-Welsh algorithm to learn the probabilities Scaling on the forward and backward values is automatically performed when numerical problems (underflow) are encountered. Each iteration prints a dot on stderr, or a star if scaling was applied""" B = self.B A = self.A pi= self.pi apply_scaling=0 for iter in xrange(1,maxiter+1): print '(%s/%s),'% (iter, maxiter) if not (iter%scale_every): apply_scaling=1 try: if apply_scaling: stderr.write('*') alpha,scale_factors = self._alpha_scaled(obsIndices) apply_scaling = 0 else: stderr.write('.') alpha = self._alpha(obsIndices) scale_factors = None stderr.flush() beta = self._beta(obsIndices,scale_factors) ksi = self._ksi(obsIndices,alpha,beta) except OverflowError: if apply_scaling: # we have overflown even though scaling was applied # There's nothing much we can do, so we abort. :o( from traceback import print_exc print_exc() print "There's nothing much we can do, so we abort. :o(" raise else: # we have overflown: we process the exception by # decreasing scale_every, setting apply_scaling to TRUE # and restarting the loop scale_every = int(floor(scale_every/1.5)) apply_scaling = 1 continue gamma = self._gamma(ksi) sigma_gamma = reduce(add,gamma) pi_bar = gamma[0] # (40a) A_bar = reduce(add,ksi)/sigma_gamma # (40b) B_bar = zeros((self.M,self.N),Float) # sort things out sorter = {} for i in range(len(obsIndices)-1): partial = sorter.setdefault(obsIndices[i],B_bar[obsIndices[i]]) partial += gamma[i] B_bar /= sigma_gamma if allclose(A, A_bar) and allclose(B,B_bar) and \ allclose(pi,pi_bar): print 'Converged in %d iterations'%iter break else: self.A = A = A_bar self.B = B = B_bar self.pi = pi = pi_bar else: print "The Baum-Welsh algorithm had not converged in %d iterations"%maxiter
def multiple_learn(self, m_observations, states=None, maxiter=10000, scale_every=50): """Uses Baum-Welsh algorithm to learn the probabilities on multiple observations sequences """ K=len(m_observations) apply_scaling = [0]*K scale_every = [scale_every]*K all_scaling = 0 B = self.B A = self.A pi= self.pi epsilon_v = EPSILON*self.N for iter in xrange(1,maxiter+1): print '(%s/%s),'% (iter, maxiter) A_bar = zeros((self.N,self.N),Float) B_bar = zeros((self.M,self.N),Float) sigma_gamma = zeros((self.N,),Float) ok = 0 for k in range(K): observations = m_observations[k] # FIXME cache obsIndices ? obsIndices = self._getObservationIndices(observations) if not (iter%scale_every[k]): apply_scaling[k] = 1 try: if apply_scaling[k]: stderr.write('*') alpha,scale_factors = self._alpha_scaled(obsIndices) apply_scaling[k] = 0 else: stderr.write('.') alpha = self._alpha(obsIndices) scale_factors = None stderr.flush() beta = self._beta(obsIndices,scale_factors) ksi = self._ksi(obsIndices,alpha,beta) except OverflowError: from traceback import print_exc if apply_scaling[k]: # we have overflown even though scaling was applied, # hope the next is better stderr.write('f') continue else: # we have overflown: we process the exception by # decreasing scale_every, setting apply_scaling to TRUE # and restarting the loop if scale_every[k] >= 1.5: scale_every[k] = int(floor(scale_every[k]/1.5)) apply_scaling[k] = 1 stderr.write('o') #print_exc() continue stderr.write('+') gamma = self._gamma(ksi) sigma_gamma_k = reduce(add,gamma) pi_bar = gamma[0] A_bar_k = reduce(add, ksi) B_bar_k = zeros((self.M,self.N),Float) # sort things out sorter = {} for i in range(len(obsIndices)-1): partial = sorter.setdefault(obsIndices[i],B_bar_k[obsIndices[i]]) partial += gamma[i] # add temp results sigma_gamma = add(sigma_gamma, sigma_gamma_k) # (109) et (110) denominateur A_bar = add(A_bar, A_bar_k) # (109) numerateur B_bar = add(B_bar, B_bar_k) # (110) numerateur ok = 1 stderr.write('\n') if not ok: # we have overflown in all sequences if not all_scaling: all_scaling = 1 continue # and this is the second time else: raise Exception("There's nothing much we can do, so we abort. :o(") all_scaling = 0 # replace 0 with epsilon ? # sigma_gamma = where(sigma_gamma, sigma_gamma, epsilon_v) A_bar /= sigma_gamma # (109) B_bar /= sigma_gamma # (110) #print A_bar #print B_bar if allclose(A, A_bar) and allclose(B,B_bar) and \ allclose(pi,pi_bar): print 'Converged in %d iterations'%iter break else: self.A = A = A_bar self.B = B = B_bar self.pi = pi = pi_bar else: print "The Baum-Welsh algorithm had not converged in %d iterations"%maxiter