def _getNext(self): '''just return function of time, same every time''' ident = 'file__%s__%d' % (self.filtFile, self.counter) self.counter += 1 print 'returning', lambda time: matInterp(time, self.times, self.positions), ident return lambda time: matInterp(time, self.times, self.positions), ident
def main(): if len(sys.argv) < 2: print 'need name of file' exit(1) # load original positions ff = open(sys.argv[1], 'r') for ii, line in enumerate(ff): #print 'line', ii, 'is', line nums = [float(xx) for xx in line.split()] if ii == 0: positions = array(nums) else: positions = vstack((positions, array(nums))) ff.close() # assume 1 sec interp on beginning, 2 sec at end interpBegin = 1 runSeconds = 9 interpEnd = 2 totalTime = interpBegin + runSeconds + interpEnd # 12 for normal runs times = linspace(0, totalTime, positions.shape[0]) origFn = lambda time: matInterp(time, times, positions) # interpolate the starting positions for ii in flatnonzero(times < interpBegin): # for each index of one of these times, interpolate the function posOrig = positions[ii, :] posNew = lInterp(times[ii], [0, interpBegin], POS_READY, posOrig) positions[ii, :] = posNew # interpolate the ending positions for ii in flatnonzero(times > (totalTime - interpEnd)): # for each index of one of these times, interpolate the function posOrig = positions[ii, :] posNew = lInterp(times[ii], [totalTime - interpEnd, totalTime], posOrig, POS_READY) positions[ii, :] = posNew timeAndPos = vstack((times, positions.T)).T # print out positions writeArray(sys.stdout, timeAndPos)
def main(): if len(sys.argv) < 2: print 'need name of file' exit(1) # load original positions ff = open(sys.argv[1], 'r') for ii,line in enumerate(ff): #print 'line', ii, 'is', line nums = [float(xx) for xx in line.split()] if ii == 0: positions = array(nums) else: positions = vstack((positions, array(nums))) ff.close() # assume 1 sec interp on beginning, 2 sec at end interpBegin = 1 runSeconds = 9 interpEnd = 2 totalTime = interpBegin + runSeconds + interpEnd # 12 for normal runs times = linspace(0,totalTime,positions.shape[0]) origFn = lambda time: matInterp(time, times, positions) # interpolate the starting positions for ii in flatnonzero(times < interpBegin): # for each index of one of these times, interpolate the function posOrig = positions[ii,:] posNew = lInterp(times[ii], [0, interpBegin], POS_READY, posOrig) positions[ii,:] = posNew # interpolate the ending positions for ii in flatnonzero(times > (totalTime - interpEnd)): # for each index of one of these times, interpolate the function posOrig = positions[ii,:] posNew = lInterp(times[ii], [totalTime - interpEnd, totalTime], posOrig, POS_READY) positions[ii,:] = posNew timeAndPos = vstack((times, positions.T)).T # print out positions writeArray(sys.stdout, timeAndPos)
def _getNext(self): '''Get the next point to try. This reads from the file self.motionFile''' #print 'TRYING READ STDOUT' #stdout = self.proc.stdout.read() #print 'TRYING READ STDERR' #stderr = self.proc.stderr.read() #print 'STDOUT:' #print stdout #print 'STDERR:' #print stderr if self.orgId == self.generationSize: # print 'Restarting process after %d runs, push enter when ready...' % self.generationSize if self.restartHN: print 'Restarting process after %d runs' % self.generationSize print 'waiting for exit code...' code = self.proc.wait() print 'got exit code: %d' % code # raw_input() #sleep(10) if self.spawnProc: print 'Continuing with neatfile', self.nextNeatFile self.proc = Process((self.executable, '-O', self.prefix, '-R', '102', '-I', self.datFile, '-X', self.nextNeatFile, '-XG', '1')) self.genId += 1 self.orgId = 0 #print 'On iteration', self.orgId+1 while True: if self.spawnProc: out = self.proc.read() if out != '': #print 'Got stdout:' #print out pass out = self.proc.readerr() if out != '': #print 'Got stderr:' #print out pass try: ff = open(self.motionFile, 'r') except IOError: print 'File does not exist yet' sleep(1) if self.proc.wait(os.WNOHANG) is None: continue else: raise Exception('HN died') lines = ff.readlines() nLines = len(lines) if nLines < self.expectedLines: print ' only %d of %d lines, waiting...' % (nLines, self.expectedLines) ff.close() sleep(.5) continue break self.orgId += 1 for ii,line in enumerate(lines[self.junkPoints:]): #print 'line', ii, 'is', line nums = [float(xx) for xx in line.split()] if ii == 0: rawPositions = array(nums) else: rawPositions = vstack((rawPositions, array(nums))) # swap and scale rawPositions appropriately rawPositions = rawPositions.T[ix_(self.motorColumns)].T # remap to right columns #print 'First few lines of rawPositions are:' #for ii in range(10): # print prettyVec(rawPositions[ii,:], prec=2) # Average over every self.avgPoints for ii in range((self.expectedLines-self.junkPoints)/self.avgPoints): temp = mean(rawPositions[ii*self.avgPoints:(ii+1)*self.avgPoints,:], 0) if ii == 0: positions = temp else: positions = vstack((positions, temp)) #print 'First few lines of positions are:' #for ii in range(10): # print prettyVec(positions[ii,:], prec=2) # scale from [-1,1] to [0,1] positions += 1 positions *= .5 # scale from [0,1] to appropriate ranges innerIdx = [0, 2, 4, 6] outerIdx = [1, 3, 5, 7] centerIdx = [8] for ii in innerIdx: positions[:,ii] *= (MAX_INNER - MIN_INNER) positions[:,ii] += MIN_INNER for ii in outerIdx: positions[:,ii] *= (MAX_OUTER - MIN_OUTER) positions[:,ii] += MIN_OUTER for ii in centerIdx: positions[:,ii] *= (MAX_CENTER - MIN_CENTER) positions[:,ii] += MIN_CENTER # append a column of 512s for center #positions = hstack((positions, # NORM_CENTER * ones((positions.shape[0],1)))) times = linspace(0,12,positions.shape[0]) # Dump both raw positions and positions to file thisIdentifier = '%s_%05d_%03d' % (self.identifier, self.genId, self.orgId) ff = open('%s_raw' % thisIdentifier, 'w') writeArray(ff, rawPositions) ff.close() ff = open('%s_filt' % thisIdentifier, 'w') writeArray(ff, positions) ff.close() # return function of time return lambda time: matInterp(time, times, positions), thisIdentifier
def _getNext(self): '''Get the next point to try. This reads from the file self.motionFile''' #print 'TRYING READ STDOUT' #stdout = self.proc.stdout.read() #print 'TRYING READ STDERR' #stderr = self.proc.stderr.read() #print 'STDOUT:' #print stdout #print 'STDERR:' #print stderr if self.orgId == self.generationSize: print 'Restarting process after %d runs, push enter when ready...' % self.generationSize raw_input() #sleep(10) if self.spawnProc: print 'Continuing with neatfile', self.nextNeatFile self.proc = Process( (self.executable, '-O', self.prefix, '-R', '102', '-I', self.datFile, '-X', self.nextNeatFile, '-XG', '1')) self.genId += 1 self.orgId = 0 #print 'On iteration', self.orgId+1 while True: if self.spawnProc: out = self.proc.read() if out != '': #print 'Got stdout:' #print out pass out = self.proc.readerr() if out != '': #print 'Got stderr:' #print out pass try: ff = open(self.motionFile, 'r') except IOError: print 'File does not exist yet' sleep(1) continue lines = ff.readlines() nLines = len(lines) if nLines < self.expectedLines: print ' only %d of %d lines, waiting...' % ( nLines, self.expectedLines) ff.close() sleep(.5) continue break self.orgId += 1 for ii, line in enumerate(lines[self.junkPoints:]): #print 'line', ii, 'is', line nums = [float(xx) for xx in line.split()] if ii == 0: rawPositions = array(nums) else: rawPositions = vstack((rawPositions, array(nums))) # swap and scale rawPositions appropriately rawPositions = rawPositions.T[ix_( self.motorColumns)].T # remap to right columns #print 'First few lines of rawPositions are:' #for ii in range(10): # print prettyVec(rawPositions[ii,:], prec=2) # Average over every self.avgPoints for ii in range(self.expectedLines / self.avgPoints): temp = mean(rawPositions[ii:(ii + self.avgPoints), :], 0) if ii == 0: positions = temp else: positions = vstack((positions, temp)) #print 'First few lines of positions are:' #for ii in range(10): # print prettyVec(positions[ii,:], prec=2) # scale from [-1,1] to [0,1] positions += 1 positions *= .5 # scale from [0,1] to appropriate ranges innerIdx = [0, 2, 4, 6] outerIdx = [1, 3, 5, 7] centerIdx = [8] for ii in innerIdx: positions[:, ii] *= (MAX_INNER - MIN_INNER) positions[:, ii] += MIN_INNER for ii in outerIdx: positions[:, ii] *= (MAX_OUTER - MIN_OUTER) positions[:, ii] += MIN_OUTER for ii in centerIdx: positions[:, ii] *= (MAX_CENTER - MIN_CENTER) positions[:, ii] += MIN_CENTER # append a column of 512s for center #positions = hstack((positions, # NORM_CENTER * ones((positions.shape[0],1)))) times = linspace(0, 12, positions.shape[0]) # Dump both raw positions and positions to file thisIdentifier = '%s_%05d_%03d' % (self.identifier, self.genId, self.orgId) ff = open('%s_raw' % thisIdentifier, 'w') writeArray(ff, rawPositions) ff.close() ff = open('%s_filt' % thisIdentifier, 'w') writeArray(ff, positions) ff.close() # return function of time return lambda time: matInterp(time, times, positions), thisIdentifier
def _getNext(self): """Get the next point to try. This reads from the file self.motionFile""" # print 'TRYING READ STDOUT' # stdout = self.proc.stdout.read() # print 'TRYING READ STDERR' # stderr = self.proc.stderr.read() # print 'STDOUT:' # print stdout # print 'STDERR:' # print stderr if self.orgId == self.generationSize: print "Restarting process after %d runs, push enter when ready..." % self.generationSize raw_input() # sleep(10) if self.spawnProc: print "Continuing with neatfile", self.nextNeatFile self.proc = Process( ( self.executable, "-O", self.prefix, "-R", "102", "-I", self.datFile, "-X", self.nextNeatFile, "-XG", "1", ) ) self.genId += 1 self.orgId = 0 # print 'On iteration', self.orgId+1 while True: if self.spawnProc: out = self.proc.read() if out != "": # print 'Got stdout:' # print out pass out = self.proc.readerr() if out != "": # print 'Got stderr:' # print out pass try: ff = open(self.motionFile, "r") except IOError: print "File does not exist yet" sleep(1) continue lines = ff.readlines() nLines = len(lines) if nLines < self.expectedLines: print " only %d of %d lines, waiting..." % (nLines, self.expectedLines) ff.close() sleep(0.5) continue break self.orgId += 1 for ii, line in enumerate(lines[self.junkPoints :]): # print 'line', ii, 'is', line nums = [float(xx) for xx in line.split()] if ii == 0: rawPositions = array(nums) else: rawPositions = vstack((rawPositions, array(nums))) # swap and scale rawPositions appropriately rawPositions = rawPositions.T[ix_(self.motorColumns)].T # remap to right columns # print 'First few lines of rawPositions are:' # for ii in range(10): # print prettyVec(rawPositions[ii,:], prec=2) # Average over every self.avgPoints for ii in range(self.expectedLines / self.avgPoints): temp = mean(rawPositions[ii : (ii + self.avgPoints), :], 0) if ii == 0: positions = temp else: positions = vstack((positions, temp)) # print 'First few lines of positions are:' # for ii in range(10): # print prettyVec(positions[ii,:], prec=2) # scale from [-1,1] to [0,1] positions += 1 positions *= 0.5 # scale from [0,1] to appropriate ranges innerIdx = [0, 2, 4, 6] outerIdx = [1, 3, 5, 7] centerIdx = [8] for ii in innerIdx: positions[:, ii] *= MAX_INNER - MIN_INNER positions[:, ii] += MIN_INNER for ii in outerIdx: positions[:, ii] *= MAX_OUTER - MIN_OUTER positions[:, ii] += MIN_OUTER for ii in centerIdx: positions[:, ii] *= MAX_CENTER - MIN_CENTER positions[:, ii] += MIN_CENTER # append a column of 512s for center # positions = hstack((positions, # NORM_CENTER * ones((positions.shape[0],1)))) times = linspace(0, 12, positions.shape[0]) # Dump both raw positions and positions to file thisIdentifier = "%s_%05d_%03d" % (self.identifier, self.genId, self.orgId) ff = open("%s_raw" % thisIdentifier, "w") writeArray(ff, rawPositions) ff.close() ff = open("%s_filt" % thisIdentifier, "w") writeArray(ff, positions) ff.close() # return function of time return lambda time: matInterp(time, times, positions), thisIdentifier