def create_snake(): segments = [ Segment(SEG_SIZE, SEG_SIZE), Segment(SEG_SIZE * 2, SEG_SIZE), Segment(SEG_SIZE * 3, SEG_SIZE) ] return Snake(segments)
def get_form(self): from random import randint #simulated morphological alternations #chance of a feature being "left off" due to context/use (inflectional/derivational) #morphable_features = ["stop", "voiced", "fricative", "spread"] onset = self.onset nuc = self.nucleus coda = self.coda morph_chance = 75 # 75% chance for each feature to be included ofl = [ f for f in onset.features if ((randint(0, 99) < morph_chance) or f is "null") ] cfl = [ f for f in coda.features if ((randint(0, 99) < morph_chance) or f is "null") ] onset_cc = Segment.Segment(onset.name, ofl, onset.symbol) coda_cc = Segment.Segment(coda.name, cfl, coda.symbol) form = Word(onset_cc, nuc, coda_cc, self.percept, self.noise) return form
def SegmentImage(image, filledImage, contours): #Create a list for all the segments found in the image segments = list() #Create a collective image that has all segments imagecollective = np.zeros((image.shape[0], image.shape[1])) imagecollective[:, :] = 0.5 #Segment every contour for contour in contours: #Create new segment segment = Segment() #Get aspect ratio segment.BoundingBox = BoundingBox(np.min(contour[:, 1]), np.max(contour[:, 1]), np.min(contour[:, 0]), np.max(contour[:, 0])) segment.BoundingBox.ToInt() #Create the segment image (will be replaced with segment class) segment.CreateImage() #Copy the segment from the contour we have to the segment rr, cc = polygon(contour[:, 0], contour[:, 1], image.shape) rr = rr.astype(int) cc = cc.astype(int) segment.Image[rr - segment.BoundingBox.miny, cc - segment.BoundingBox.minx] = image[rr, cc] segment.FilledImage[rr - segment.BoundingBox.miny, cc - segment.BoundingBox.minx] = filledImage[rr, cc] imagecollective[rr, cc] = image[rr, cc] segments.append(segment) return imagecollective, segments
def __init__(self, segments, zeroConfig): if len(segments) != len(zeroConfig): raise ValueError( 'length of segment list and zero configuration list is not equal' ) self.numSegs = len(segments) #The number of segments in this robot self.focusedJoint = 0 #The joint in focus for manipulation self.segmentList = segments #The list of segments that make up the robot self.p0T = np.matrix( [[0], [0], [0]]) #the position of the end effector of the robot self.r0T = sa.eye(3) #The orientation of the end effector of the robot self.Q = [] #The array of angles of the robot self.P = [] #The array containing the positions of each position self.numJoints = -1 #The number of joints tha can be manipulated #Dictionary that carries a key and matches it to the appropriate joint self.keyToJoint = {} #Add origin to the position list self.P.append(self.p0T) for i in range( 0, len(segments) ): #loop performs foward kinematics to find the p0T and r0T if segments[i].getSegmentType( ) == 1: #if segment is prismatic perform translational motion self.p0T = self.p0T + np.dot( self.r0T, zeroConfig[i] * segments[i].getLength()) else: self.p0T = self.p0T + np.dot(self.r0T, segments[i].getLength()) self.r0T = np.dot( self.r0T, sa.rot(segments[i].getUnitVector(), zeroConfig[i])) self.P.append(self.p0T) #add new position to positions list self.Q.append(zeroConfig[i]) #add joint angle to angles list #If the joint is not the first or last joint, add the joint types of it's adjacent #joints to the segment object if i != 0 and i != len(segments) - 1: segments[i].adjacentJoints(segments[i - 1].getSegmentType(), segments[i + 1].getSegmentType()) #Other wise, mark that the joint is the last or first joint with a -1 elif i == 0: segments[i].adjacentJoints(-1, segments[i + 1].getSegmentType()) elif i == len(segments) - 1: segments[i].adjacentJoints(segments[i - 1].getSegmentType(), -1) if segments[i].getSegmentType() == 0 or segments[i].getSegmentType( ) == 1: self.numJoints += 1 self.keyToJoint[self.numJoints] = i ####################### Draw Plot #Creates figure for program to put things on self.fig = plt.figure() self.drawArm() return
def data_process(original_filepath, start_time, end_time): # 1. 过滤广告 # original_filepath (str) 为加载的全部微博数据文件路径 # filtered_blog_file (str) 为过滤后的微博数据文件路径 RemoveAd.remove_advertising(original_filepath, filtered_blog_file) print("过滤广告完毕") # 2. 将过滤后的文件分割为2个文件: 历史数据文件, 当前查询时间段的数据文件 # filtered_blog_file (str) 为过滤后的微博数据文件路径 # start_time (str) 为开始时间, 格式为: YYYYMMDD # end_time (str) 为结束时间, 格式为YYYYMMDD # history_blog_file (str) 历史数据文件路径 # current_blog_file (str) 当前查询时间段的数据文件路径 cutAndTransform.cut_original_file(filtered_blog_file, start_time, end_time, history_blog_file, current_blog_file) print("切割文件完毕") # 3. 分词: 将历史时间微博和当前时间微博进行分词及去除表情词、URL链接、停顿词等 # history_blog_file (str) 历史数据文件路径 # current_blog_file (str) 当前查询时间段的数据文件路径 # segment_history_blog_file (str) 历史时间分词后的微博文件路径 # segment_current_blog_file (str) 当前时间分词后的微博文件路径 Segment.word_segmentation(history_blog_file, segment_history_blog_file) Segment.word_segmentation(current_blog_file, segment_current_blog_file) print("微博数据分词完毕") # 4. 将当前查询时间段的数据文件微博按窗口读入内存 origin_blogdata (用于热点事件的原始微博查询) # current_blog_file (str) 当前查询时间段的数据文件路径 # origin_blogdata (二维列表) 当前时间段原始微博数据 (每一维是一个窗口时间内的原始微博) [['微博1','微博2',...],...] origin_blogdata = cutAndTransform.transform_data(current_blog_file) # 5. 分词后的历史时间微博和当前时间微博加载进内存 (用于敏感词发现) # segment_history_blog_file (str) 历史时间分词后的微博文件路径 # segment_current_blog_file (str) 当前时间分词后的微博文件路径 # history_blogdata (二维列表): 历史微博数据 (每一维是一个窗口时间内已分词的全部微博) [['微博1','微博2',...],...] # current_blogdata (二维列表): 当前时间段微博数据 (每一维是一个窗口时间内已分词的全部微博) [['微博1','微博2',...],...] history_blogdata = cutAndTransform.transform_data( segment_history_blog_file) current_blogdata = cutAndTransform.transform_data( segment_current_blog_file) print("微博数据加载进内存") # 6. 删除中间过程文件 ''' if os.path.exists(filtered_blog_file): os.remove(filtered_blog_file) if os.path.exists(history_blog_file): os.remove(history_blog_file) if os.path.exists(current_blog_file): os.remove(current_blog_file) if os.path.exists(segment_history_blog_file): os.remove(segment_history_blog_file) if os.path.exists(segment_current_blog_file): os.remove(segment_current_blog_file) ''' return history_blogdata, current_blogdata, origin_blogdata
def findNoisesegments(self, dirName): ''' dirName got manually annotated GT.data Generates auto segments by running wavelet detection Find noise segments by diff of auto segments and GT.data :returns noise segments [[filename, seg, label], ...] ''' manSegNum = 0 noiseSegments = [] # Generate GT files from annotations in dir1 print('Generating GT...') for root, dirs, files in os.walk(dirName): for file in files: wavFile = os.path.join(root, file) if file.lower().endswith('.wav') and os.stat( wavFile).st_size != 0 and file + '.data' in files: segments = Segment.SegmentList() segments.parseJSON(wavFile + '.data') sppSegments = segments.getSpecies(self.species) manSegNum += len(sppSegments) # Currently, we ignore call types here and just # look for all calls for the target species. segments.exportGT(wavFile, self.species, resolution=1.0) if manSegNum == 0: print("ERROR: no segments for species %s found" % self.species) return ws = WaveletSegment.WaveletSegment(self.filter, 'dmey2') autoSegments = ws.waveletSegment_cnn( dirName, self.filter) # [(filename, [segments]), ...] # now the diff between segment and autoSegments print("autoSeg", autoSegments) for item in autoSegments: print(item[0]) wavFile = item[0] if os.stat(wavFile).st_size != 0: sppSegments = [] if os.path.isfile(wavFile + '.data'): segments = Segment.SegmentList() segments.parseJSON(wavFile + '.data') sppSegments = [ segments[i] for i in segments.getSpecies(self.species) ] for segAuto in item[1]: overlappedwithGT = False for segGT in sppSegments: if self.Overlap(segGT, segAuto): overlappedwithGT = True break if not overlappedwithGT: noiseSegments.append( [wavFile, segAuto, len(self.calltypes)]) return noiseSegments
def MergeHalfCircles(circles): newCirclesList = list() merged = np.zeros(len(circles)) for i in range(len(circles)): if (merged[i] == 1): continue c1 = circles[i] c2 = None for j in range(len(circles)): if (i == j or merged[j]): continue if (abs(c1.BoundingBox.miny - circles[j].BoundingBox.miny) < circlesMergeThreshold and abs(c1.BoundingBox.maxy - circles[j].BoundingBox.maxy) < circlesMergeThreshold and (abs(c1.BoundingBox.minx - circles[j].BoundingBox.maxx) < circlesMergeThreshold * 10 or abs(c1.BoundingBox.maxx - circles[j].BoundingBox.minx) < circlesMergeThreshold * 10)): c2 = circles[j] merged[j] = 1 break if (c2 != None and c1.BoundingBox.minx > c2.BoundingBox.minx): c1, c2 = c2, c1 width = c1.Image.shape[1] height = c1.Image.shape[0] if (c2 != None): width = max(width, c2.Image.shape[1]) height = max(height, c2.Image.shape[0]) #Resize img1 tmp = np.copy(c1.Image) c1.Image = np.zeros((height, width)) c1.Image[:tmp.shape[0], :tmp.shape[1]] = tmp[:, :] if (c2 != None): #Resize img2 tmp = np.copy(c2.Image) c2.Image = np.zeros((height, width)) c2.Image[:tmp.shape[0], :tmp.shape[1]] = tmp[:, :] newCircle = np.concatenate((c1.Image, c2.Image), axis=1) segment = Segment() segment.BoundingBox = c1.BoundingBox segment.Image = newCircle newCirclesList.append(segment) merged[i] = 1 else: newCirclesList.append(c1) connectCircles(newCirclesList) for circle in newCirclesList: contours = find_contours(circle.Image, 0.9) circle.FilledImage = FillPolygon(circle.Image, contours) return newCirclesList
def getRandomPoint(poly: 'Polygon', lower: int) -> 'Point': mostLeftPoint = poly.listOfEdges[0].start for edge in poly.listOfEdges: if mostLeftPoint > edge.start: mostLeftPoint = edge.start if mostLeftPoint > edge.end: mostLeftPoint = edge.end mostRightPoint = poly.listOfEdges[0].start for edge in poly.listOfEdges: if mostRightPoint < edge.start: mostRightPoint = edge.start if mostRightPoint < edge.end: mostRightPoint = edge.end mostUpPoint = poly.listOfEdges[0].start for edge in poly.listOfEdges: if mostUpPoint.y > edge.start.y: mostUpPoint = edge.start if mostUpPoint.y > edge.end.y: mostUpPoint = edge.end mostDownPoint = poly.listOfEdges[0].start for edge in poly.listOfEdges: if mostDownPoint.y < edge.start.y: mostDownPoint = edge.start if mostDownPoint.y < edge.end.y: mostDownPoint = edge.end randomx = random.uniform(mostLeftPoint.x, mostRightPoint.x) randomy = random.uniform(mostDownPoint.y, mostUpPoint.y) randomPoint = Point(randomx, randomy, lower) segment = Segment(Point(mostLeftPoint.x - 1, mostUpPoint.y), randomPoint) counter = 0 for edge in poly.listOfEdges: if Segment.doIntersect(segment, edge): counter += 1 while not counter % 2: randomx = random.uniform(mostLeftPoint.x, mostRightPoint.x) randomy = random.uniform(mostDownPoint.y, mostUpPoint.y) randomPoint = Point(randomx, randomy, lower) segment = Segment(Point(mostLeftPoint.x - 1, mostUpPoint.y), randomPoint) counter = 0 for edge in poly.listOfEdges: if Segment.doIntersect(segment, edge): counter += 1 return randomPoint
def __init__(self,name,x,y,direction,playerType): head = pyglet.image.load('head.png') head.width = 36 head.height = 36 head.anchor_x = 18 head.anchor_y = 18 self.playerType = playerType self.playerName = name self.length = 7 self.direction = direction self.positionX = x self.positionY = y self.squaresoccupied = list() self.score = 0 self.moveQueue = list() self.segments = list() self.segBatch = pyglet.graphics.Batch() self.snakeSprite = pyglet.sprite.Sprite(head) for i in range(self.length): self.squaresoccupied.append(tuple((x,y+(i*self.direction)))) self.segments.append(Seg.Segment(340+(x*40),60+(y*40)+(i*40*self.direction),self.direction,self.playerType)) for element in self.segments: element.segmentSprite.batch = self.segBatch
def waveletSegment(self, filtnum, wpmode="new"): """ Main analysis wrapper (segmentation in batch mode). Also do species-specific post-processing. Reads data pre-loaded onto self.WF.tree by self.readBatch. Args: 1. filtnum: index of the current filter in self.spInfo (which is a list of filters...) 2. wpmode: old/new/aa to indicate no/partial/full antialias Returns: list of lists of segments found (over each subfilter)-->[[sub-filter1 segments], [sub-filter2 segments]] """ opst = time.time() # No resampling here. Will read nodes from self.spInfo, which may already be adjusted ### find segments with each subfilter separately detected_allsubf = [] for subfilter in self.spInfo[filtnum]["Filters"]: print("Identifying calls using subfilter", subfilter["calltype"]) goodnodes = subfilter['WaveletParams']["nodes"] detected = self.detectCalls(self.WF, nodelist=goodnodes, subfilter=subfilter, rf=True, aa=wpmode!="old") # merge neighbours in order to convert the detections into segments # note: detected np[0 1 1 1] becomes [[1,3]] segmenter = Segment.Segmenter() detected = segmenter.convert01(detected) detected = segmenter.joinGaps(detected, maxgap=0) detected_allsubf.append(detected) print("Wavelet segmenting completed in", time.time() - opst) return detected_allsubf
def deserialize(self, data): # TODO Add validation self.x_0 = data.get('x_0') self.y_0 = data.get('y_0') self.vx = data.get('vx') self.vy = data.get('vy') self.count = data.get('count') m_l = data.get('mirrors') if m_l: mrs = [] for m_data in m_l: mir = Mirror.Mirror(0, 0, 0, 0, 'flat', 0) mir.deserialize(m_data) mrs.append(mir) self.mirrors = mrs m_data = data.get('last_mirror') if m_data is not None: mir = Mirror.Mirror(0, 0, 0, 0, 'flat', 0) mir.deserialize(m_data) self.last_mirror = mir else: self.last_mirror = None s_l = data.get('segment_list') segments = [] for s in s_l: seg = Segment.Segment(0, 0, 0, 0) seg.deserialize(s) segments.append(seg) self.segment_list = segments
def findCTsegments(self, dirName, calltypei): ''' dirName got reviewed.data or manual.data Find calltype segments :returns ct segments [[filename, seg, label], ...] ''' calltypeSegments = [] for root, dirs, files in os.walk(dirName): for file in files: wavFile = os.path.join(root, file) if file.lower().endswith('.wav') and file + '.data' in files: segments = Segment.SegmentList() segments.parseJSON(wavFile + '.data') if len(self.calltypes) == 1: ctSegments = segments.getSpecies(self.species) else: ctSegments = segments.getCalltype( self.species, self.calltypes[calltypei]) for indx in ctSegments: seg = segments[indx] # skip uncertain segments cert = [ lab["certainty"] if lab["species"] == self.species else 100 for lab in seg[4] ] if cert: mincert = min(cert) if mincert == 100: calltypeSegments.append( [wavFile, seg[:2], calltypei]) return calltypeSegments
def getInput(): segArr = Segment.listSegments() inp = int( input("\n Lider Tablosunu Görmek İstediğiniz Segmenti Seçiniz: ")) print("\n", segArr[inp].name, " Lider Tablosu") atheleteArr = getBoard(str(segArr[inp].id), segArr[inp].distance) athName = input(" \n Atlet ismi giriniz: ") print("\n Bu tabloda ", counterAthelete(athName, atheleteArr), " kez var.")
def recalculate(self): self.p0T = np.matrix([[0],[0],[0]]) #The orientation of the end effector of the robot self.r0T = sa.eye(3) #The array containing the positions of each position self.P = [] #Add origin to the position list self.P.append(self.p0T) for i in range(0,len(self.segmentList)):#loop performs foward kinematics to find the p0T and r0T if self.segmentList[i].getSegmentType() == 1: self.p0T = self.p0T + np.dot(self.r0T,self.Q[i]*self.segmentList[i].getLength()) self.r0T = np.dot(self.r0T, sa.eye(3)) else: self.p0T = self.p0T + np.dot(self.r0T,self.segmentList[i].getLength()) self.r0T = np.dot(self.r0T, sa.rot(self.segmentList[i].getUnitVector(),self.Q[i])) self.P.append(self.p0T) #add new position to positions list
def findAllsegments(self, dirName): ''' dirName got manually annotated GT.data Generates noise segments as the complement to GT segments (i.e. every not marked second is used as noise) :returns noise segments [[filename, seg, label], ...] ''' manSegNum = 0 noiseSegments = [] segmenter = Segment.Segmenter() print('Generating GT...') for root, dirs, files in os.walk(dirName): for file in files: wavFile = os.path.join(root, file) if file.lower().endswith('.wav') and os.stat( wavFile).st_size != 0 and file + '.data' in files: # Generate GT files from annotations in dir1 segments = Segment.SegmentList() segments.parseJSON(wavFile + '.data') sppSegments = segments.getSpecies(self.species) manSegNum += len(sppSegments) # Currently, we ignore call types here and just # look for all calls for the target species. segments.exportGT(wavFile, self.species, resolution=1.0) print('Determining noise...') autoseg = Segment.SegmentList() for sec in range( math.floor(segments.metadata["Duration"]) - 1): autoseg.addSegment([sec, sec + 1, 0, 0, []]) autoSegments = segmenter.joinGaps(autoseg, maxgap=0) print("autoSeg, file", wavFile, autoSegments) for segAuto in autoSegments: noiseSegments.append( [wavFile, segAuto, len(self.calltypes)]) if manSegNum == 0: print("ERROR: no segments for species %s found" % self.species) return return noiseSegments
def imagecheck(path): Segment.segment(path) result = imageprepare("/home/sam/Downloads/imageprepare/tmp") x = tf.placeholder(tf.float32, [None, 784]) xs = tf.reshape(x, [len(result), 28, 28, 1]) y = LeNet_inference.inference(xs, False, None) variable_averages = tf.train.ExponentialMovingAverage( LeNet_Train.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) with tf.Session() as sess: saver.restore( sess, '/home/sam/Documents/Python_work/LearningPython/tensorflow/MODEL_SAVE4/model.ckpt-49001' ) prediction = tf.argmax(y, 1) predint = prediction.eval(feed_dict={x: result}, session=sess) result = connect(predint) Clear.clear("/home/sam/Downloads/imageprepare/tmp") return result
def setSegments(self): ''' See the documentation for setSquares(). It works similarly, except handles the segments of the snake for drawing in pyglet. Also specifies the batches of the snake. ''' self.segments.insert(0,Seg.Segment(340+(self.positionX*40),(60+(self.positionY*40)),self.direction,self.playerType)) if len(self.segments) > self.length: self.segments.pop() for element in self.segments: element.segmentSprite.batch = self.segBatch
def DetectNoteheads(notesAndConnectedSegments): newList = list() newSegmentsList = list() for seg in notesAndConnectedSegments: image = np.copy(seg.Image) tmpImg = np.copy(image[1:-2, 1:-2]) ones = np.sum(image[5:-5, 5:-5] == 1) zeros = np.sum(image[5:-5, 5:-5] == 0) if (ones > 0 and zeros / ones > 1.2): image = ndimage.binary_fill_holes(image).astype(int) image = binary_erosion(image, selement) image = binary_erosion(image, selement) image = binary_erosion(image, selement2) newSeg = Segment(seg) newSeg.Image = image newSeg.BoundingBox = seg.BoundingBox newList.append(image) newSegmentsList.append(newSeg) return newSegmentsList, newList
def calculateRectangle(coor, h): w = 0.5 #width of rectangle t = 0.15 #height/2 c1 = h * w #vector of rectangular prism and negative vector c2 = -h * w #The following calculates the 8 corners of the rectangular prism p0 = perpendicularVector(h) * t + c1 p2 = sa.rot(h, np.pi) * p0 p1 = sa.rot(h, np.pi / 2) * p0 p3 = sa.rot(h, -np.pi / 2) * p0 #Move points relative to coordinate of joint p0 += coor p1 += coor p2 += coor p3 += coor #repeats for other side of rectangular prism p4 = perpendicularVector(h) * t + c2 p6 = sa.rot(h, np.pi) * p4 p5 = sa.rot(h, np.pi / 2) * p4 p7 = sa.rot(h, -np.pi / 2) * p4 p4 += coor p5 += coor p6 += coor p7 += coor #returns array of 3D points. points = np.array([p0, p1, p2, p3, p4, p5, p6, p7]) return points
def findCTsegments(self, datafile, calltypei): calltypeSegments = [] species = self.currfilt["species"] segments = Segment.SegmentList() segments.parseJSON(datafile) if len(self.calltypes) == 1: ctSegments = segments.getSpecies(species) else: ctSegments = segments.getCalltype(species, self.calltypes[calltypei]) calltypeSegments = [segments[indx][:2] for indx in ctSegments] return calltypeSegments
def prepare_song(self, song_name): with open(song_name.replace(".wav", "") + '.json') as f: data = json.load(f) song_useable_segments = [] for segment in data: if segment['useable']: song_useable_segments.append( Segment.Segment().setJSON(segment)) for segment in song_useable_segments: print(segment.toString()) song_segment = song_useable_segments[0] song_time_per_chord = song_segment.average_time_per_beat return song_segment, song_time_per_chord
def makeSegments(self, segmentsNew, filtName=None, species=None, subfilter=None): """ Adds segments to self.segments """ if self.method == "Click": # Batmode: segmentsNew should be already prepared as: [x1, x2, labels] y1 = 0 y2 = 0 if len(segmentsNew) != 3: print("Warning: segment format does not match bat mode") segment = Segment.Segment( [segmentsNew[0], segmentsNew[1], y1, y2, segmentsNew[2]]) self.segments.addSegment(segment) elif subfilter is not None: # for wavelet segments: (same as self.species!="Any sound") y1 = subfilter["FreqRange"][0] y2 = min(subfilter["FreqRange"][1], self.sampleRate // 2) for s in segmentsNew: segment = Segment.Segment([ s[0][0], s[0][1], y1, y2, [{ "species": species, "certainty": s[1], "filter": filtName, "calltype": subfilter["calltype"] }] ]) self.segments.addSegment(segment) else: # for generic all-species segments: y1 = 0 y2 = 0 species = "Don't Know" cert = 0.0 self.segments.addBasicSegments(segmentsNew, [y1, y2], species=species, certainty=cert)
def findCTsegments(self, file, calltypei): calltypeSegments = [] if file.lower().endswith('.wav') and os.path.isfile(file + '.tmpdata'): segments = Segment.SegmentList() segments.parseJSON(file + '.tmpdata') if len(self.calltypes) == 1: ctSegments = segments.getSpecies(self.species) else: ctSegments = segments.getCalltype(self.species, self.calltypes[calltypei]) for indx in ctSegments: seg = segments[indx] calltypeSegments.append(seg[:2]) return calltypeSegments
def main(): ################# Robot Stuff ##################### #Declare the segments the robot arm will contain #Can have more than this many segments. s1 = sg.Segment(1,0) s2 = sg.Segment(1,0) s3 = sg.Segment(1,0) #Place segments into a list, this list is used to initialize the robot arm segments = [s1,s2,s3] #Declare the angle configurations of the arm. angleConfig = [0,0,0] ################ Canvas Stuff #################### #configure height and width for canvas wid = 640 hei = 480 scale = 50 master = Tk() master.title = "ArmSim" vas = Canvas(master,width = wid, height = hei) vas.configure(background='white') #Canvas does not respond to keyboard commands if it is not focused on #So this important vas.focus_set() vas.pack() drawGrid(vas,hei,wid,scale) r1 = ra.RobotArm(segments,angleConfig,vas) r1.drawArm() master.mainloop() return
def calc_ray_step(self, mirrors): inters_mirrors = [] for mirror in mirrors: if mirror is not self.last_mirror: prs, x, y = intersect(self.x_0, self.y_0, self.x_0 + self.vx * 1000, self.y_0 + self.vy * 1000, mirror.x1, mirror.y1, mirror.x2, mirror.y2) if prs: # ищем ближайшее зеркало distance = math.sqrt((self.x_0 - x)**2 + (self.y_0 - y)**2) if distance >= EPS: inters_mirrors.append((distance, mirror, x, y)) inters_mirrors.sort(key=lambda s: s[0]) curr_mirror = inters_mirrors[0] if inters_mirrors and inters_mirrors[ 0][1] is not self.last_mirror else None if curr_mirror and self.count > 0: x, y = curr_mirror[2], curr_mirror[3] ray = Segment.Segment(self.x_0, self.y_0, x, y) self.segment_list.append(ray) ai_x, ai_y = reflect(self.x_0, self.y_0, x, y, curr_mirror[1]) if self.win_circle: # нужно посчитать, попадает ли луч в круг. a = (y - self.y_0) b = (self.x_0 - x) ln_w = a * a + b * b c_new = self.y_0 * x - self.x_0 * y + a * self.win_circle[ 0] + b * self.win_circle[1] x_w = -(a * c_new) / ln_w y_w = -(b * c_new) / ln_w ln_r = math.sqrt(x_w * x_w + y_w * y_w) if ln_r < self.win_circle[2]: print('Вы победили за ' + str(self.base_count - self.count) + ' шагов(-а)') return True self.vx = ai_x self.vy = ai_y self.x_0 = x self.y_0 = y self.last_mirror = curr_mirror[1] else: print("Ой") print(self) print(inters_mirrors) self.count -= 1 return False
def analyse(request): model = word2vec.Word2Vec.load('../others/弹幕.model') danmus = request.POST.get("danmus", 0) print(danmus) w = '' d = Segment.Chinese_Word_Segmentation( time.strftime("%Y-%m-%d", time.localtime()), danmus) for word in d: w += word[0] + ' ' with open('danmuall.txt', 'w') as f: f.write(' '.join(jieba.cut(danmus, cut_all=False))) f.close() sentences = word2vec.Text8Corpus(r'danmuall.txt') # model = word2vec.Word2Vec(sentences, size=10, hs=1, min_count=2, window=5) return HttpResponse(w + '!@##$$$%%%^^^&&&***(((*&)^' + model.doesnt_match(w.split()))
def drawSingle(self): xs = self.ui.spin_box_xs.value() ys = self.ui.spin_box_ys.value() xe = self.ui.spin_box_xe.value() ye = self.ui.spin_box_ye.value() self.singleAlgorithm = self.getAlg( self.ui.combobox_alg_single.currentText()) if QPoint(xs, ys) != QPoint(xe, ye): drawableObject = Segment(xs, ys, xe, ye, self.singleAlgorithm, self.singleColor) self.newWindow = Drawing(drawableObject) self.newWindow.show() else: self.__showErrorMessage("Начало и конец отрезка совпадают!")
def generateFlight(poly: 'Polygon', lower: int, upper: int, flightType: str, closeRange: int) -> 'Flight': if flightType == "external": randomEdgeEnter = random.randint(0, len(poly.listOfEdges) - 1) randomEdgeExit = random.randint(0, len(poly.listOfEdges) - 1) enterPoint = poly.listOfEdges[randomEdgeEnter].getRandomPoint(poly.lowerLimit, poly.upperLimit, flightType) exitPoint = poly.listOfEdges[randomEdgeExit].getRandomPoint(poly.lowerLimit, poly.upperLimit, flightType) v = random.randint(lower, upper) return Flight(enterPoint, exitPoint, v, flightType, [Segment(enterPoint, exitPoint)], closeRange) elif flightType == "internal": randomEnterPoint = Polygon.getRandomPoint(poly, poly.lowerLimit) randomExitPoint = Polygon.getRandomPoint(poly, poly.lowerLimit) randomPoint1 = Polygon.getRandomPointHigher(poly, poly.lowerLimit, poly.upperLimit) randomPoint2 = Polygon.getRandomPointSameHeight(poly, randomPoint1) segment1 = Segment(randomEnterPoint, randomPoint1) segment2 = Segment(randomPoint1, randomPoint2) segment3 = Segment(randomPoint2, randomExitPoint) v = random.randint(lower, upper) return Flight(randomEnterPoint, randomPoint1, v, "internal", [segment1, segment2, segment3], closeRange) elif flightType == "half-internal": randNum = random.randint(1, 2) if randNum == 1: # ulazi i silazi randomEdgeEnter = random.randint(0, len(poly.listOfEdges) - 1) enterPoint = poly.listOfEdges[randomEdgeEnter].getRandomPoint(poly.lowerLimit, poly.upperLimit, flightType) randomHeight = random.uniform(poly.lowerLimit, poly.upperLimit) randomPoint = Polygon.getRandomPoint(poly, randomHeight) exitPoint = Polygon.getRandomPoint(poly, poly.lowerLimit) segment1 = Segment(enterPoint, randomPoint) segment2 = Segment(randomPoint, exitPoint) v = random.randint(lower, upper) return Flight(enterPoint, randomPoint, v, "half-internal", [segment1, segment2], closeRange) else: # penje se i izlazi enterPoint = Polygon.getRandomPoint(poly, poly.lowerLimit) randomHeight = random.uniform(poly.lowerLimit, poly.upperLimit) randomPoint = Polygon.getRandomPoint(poly, randomHeight) exitPoint = Polygon.getRandomPoint(poly, poly.lowerLimit) segment1 = Segment(enterPoint, randomPoint) segment2 = Segment(randomPoint, exitPoint) v = random.randint(lower, upper) return Flight(enterPoint, randomPoint, v, "half-internal", [segment1, segment2], closeRange)
def handleLine(self, line, fileName="", index=0): """This function sends a line to translation to assembly based on if its a pop, push or arithmetic command""" lineEdit = line.replace("\n", "").rstrip() lineSplit = lineEdit.split(" ") if len(lineSplit) == 3: if lineSplit[0] == "function": lineObject = FD.FunctionDecl(lineSplit[1], lineSplit[2]) self.funcName = lineSplit[1] + "$" elif lineSplit[0] == "call": lineObject = FC.FunctionCall(index, self.funcName, lineSplit[1], lineSplit[2]) else: lineObject = S.Segment(line, lineSplit[0], lineSplit[1], lineSplit[2], fileName) elif self.isArithmetic(line): # Arithmetic lineObject = A.Arithmetic(line, lineSplit[0], index, self.funcName) elif ("goto" in line) or ("label" in line): lineObject = C.Conditionals(line, self.funcName) elif "return" in line: lineObject = FD.FunctionDecl() return "//" + line + "\n" + lineObject.writeLine()
def isInPoly(self, point: 'Point') -> bool: mostLeftPoint = self.listOfEdges[0].start for edge in self.listOfEdges: if mostLeftPoint > edge.start: mostLeftPoint = edge.start if mostLeftPoint > edge.end: mostLeftPoint = edge.end mostUpPoint = self.listOfEdges[0].start for edge in self.listOfEdges: if mostUpPoint.y > edge.start.y: mostUpPoint = edge.start if mostUpPoint.y > edge.end.y: mostUpPoint = edge.end segment = Segment(Point(mostLeftPoint.x - 1, mostUpPoint.y), point) counter = 0 for edge in self.listOfEdges: if Segment.doIntersect(segment, edge): counter += 1 return counter % 2
def interpolate(self, m, xform, **kw): """Interpolate to new conformation 'm'.""" # # Get all the options # if kw.has_key("method"): self.method = kw["method"] if kw.has_key("rate"): self.rate = kw["rate"] if kw.has_key("frames"): self.frames = kw["frames"] if kw.has_key("cartesian"): self.cartesian = kw["cartesian"] if kw.has_key("minimize"): self.minimize = kw["minimize"] if kw.has_key("steps"): self.steps = kw["steps"] if self.minimize: self.callback = self.minimizeCallback else: self.callback = self.interpolateCallback # # Find matching set of residues. First try for # one-to-one residue match, and, if that fails, # then finding a common set of residues. # import Segment sm = self.mol try: results = Segment.segmentHingeExact(sm, m) except ValueError: results = Segment.segmentHingeApproximate(sm, m) segments, atomMap, unusedResidues, unusedAtoms = results for r in unusedResidues: sm.deleteResidue(r) for a in unusedAtoms: sm.deleteAtom(a) if unusedResidues or unusedAtoms: from chimera import Sequence Sequence.invalidate(sm) if self.minimize: from chimera.baseDialog import AskYesNoDialog from chimera.tkgui import app d = AskYesNoDialog( "Cannot minimize with non-identical models.\n" "Continue without minimization?", default="Yes") if d.run(app) != "yes": raise ValueError("terminated at user request") self.minimize = False self.callback = self.interpolateCallback # # Interpolate between last conformation in trajectory # and new conformations # sm.activeCoordSet = sm.coordSets[max(sm.coordSets.keys())] from Interpolate import interpolate import chimera combinedXform = chimera.Xform(self.inverseXform) combinedXform.multiply(xform) interpolate(sm, combinedXform, segments, atomMap, self.method, self.rate, self.frames, self.cartesian, self.callback)
correct += 1.0 else: wrong += 1.0 print "Correctness:" + str(100*correct/(correct+wrong)) + "%" print "Wrong number:" + str(wrong) return else: pred = [] for sli in slices: if cf == "SVM": pred.append(SVM_Predict(clf,sli[0])[0]) else: pred.append(KNN_Predict(knn,sli[0])[0]) if __name__ == '__main__': #im = Image.open(os.curdir + os.sep + "43192.png") LoadData() #clf = SVC() #knn = KNN() #cf = raw_input("Choose SVM or KNN as classifier:") im = Image.open("Test.png") #ans = Segment.shotgun(im,True) Segment.shotgun(im,False) Correctness("SVM",False) # (cf,True,ans)
''' Main Process ''' import os from PIL import Image import imtools import Denoise,Segment,Recognition if __name__ == '__main__': isRecog = True s_dir = os.curdir + os.sep + "Sample" img_list = imtools.get_imlist(s_dir) Recognition.LoadData() cf = raw_input("Choose SVM or KNN as classifier:") for pic in img_list: pre = Image.open(pic) pre.show() pre = Denoise.refine(pre) seg = Segment.shotgun(pre,True) Recognition.Correctness(cf,isRecog,seg) # (cf,True,ans)
def __init__(self, scid, seglist, all_data, fixation_data, event_data = None, Segments = None, aoilist = None, prune_length= None, require_valid = True, auto_partition = False, rest_pupil_size = 0, export_pupilinfo = False): """ Args: scid: A string containing the id of the Scene. seglist: a list of tuples of the form (segid, start, end) defining the segments *Note: this method of defining segments is implemented to make batch processing of files defining segments easier all_data: a list of "Datapoint"s which make up this Scene. fixation_data: a list of "Fixation"s which make up this Scene. Segments: a list of "Segment"s which belong to this Scene. aoilist: If not None, a list of "AOI"s. prune_length: If not None, an integer that specifies the time interval (in ms) from the begining of each Segment of this Scene which samples are considered in calculations. This can be used if, for example, you only wish to consider data in the first 1000 ms of each Segment. In this case (prune_length = 1000), all data beyond the first 1000ms of the start of the "Segment"s will be disregarded. require_valid: a boolean determining whether invalid "Segment"s will be ignored when calculating the features or not. default = True auto_partition_low_quality_segments: a boolean flag determining whether EMDAT should automatically split the "Segment"s which have low sample quality into two new ssub "Segment"s discarding the largest invalid sample gap in the "Segment". default = False rest_pupil_size: rest pupil size for the current scene Yields: a Scene object """ ######################################## def partition_segment(new_seg, seg_start, seg_end, rest_pupil_size, export_pupilinfo): """ A helper method for splitting a Segment object into new Segments and removing gaps of invalid samples One way to deal with a low quality Segment is to find the gaps of invalid samples within its "Datapoint"s and splitting the Segment into two Segments one from the beginnning of the Segment to the gap and another from after the gap to the end of the Segment. This can be done multiple times resulting multiple "Segment"s with higher quality. For example if a Segment S1 started at s1 and ended at e1 and had two invalid gaps between gs1-ge1 and gs2-ge2 milliseconds, this method will generate the following three segments SS1: starting at s1 and ending at gs1 SS2: starting at ge1 and ending at gs2 SS3: starting at ge2 and ending at e1 Args: new_seg: The Segment that is being split seg_start: An integer showing the start time of the segment in milliseconds seg_end: An integer showing the end time of the segment in milliseconds rest_pupil_size: rest pupil size for the current scene Returns: subsegments: a list of newly generated "Segment"s samp_inds: a list of tuples of the form (start, end) that detrmines the index of the start and end of each new Segment in the old Segment's all_data field fix_inds: a list of tuples of the form (start, end) that detrmines the index of the start and end of each new Segment in the old Segment's fixation_data field """ timegaps = new_seg.getgaps() subsegments = [] sub_segid=0 samp_inds = [] fix_inds = [] event_inds = [] last_samp_idx = 0 last_fix_idx = 0 last_event_idx = 0 sub_seg_time_start = seg_start for timebounds in timegaps: sub_seg_time_end = timebounds[0] #end of this sub_seg is start of this gap last_samp_idx, all_start,all_end = get_chunk(all_data, last_samp_idx, sub_seg_time_start, sub_seg_time_end) last_fix_idx, fix_start, fix_end = get_chunk(fixation_data, last_fix_idx, sub_seg_time_start, sub_seg_time_end) if event_data != None: last_event_idx, event_start, event_end = get_chunk(event_data, last_event_idx, sub_seg_time_start, sub_seg_time_end) sub_seg_time_start = timebounds[1] #beginning of the next sub_seg is end of this gap if fix_end - fix_start>0: try: if event_data != None: new_sub_seg = Segment(segid+"_"+str(sub_segid), all_data[all_start:all_end], fixation_data[fix_start:fix_end], event_data=event_data[event_start:event_end], aois=aoilist, prune_length=prune_length, rest_pupil_size = rest_pupil_size, export_pupilinfo = export_pupilinfo) else: new_sub_seg = Segment(segid+"_"+str(sub_segid), all_data[all_start:all_end], fixation_data[fix_start:fix_end], event_data=None, aois=aoilist, prune_length=prune_length, rest_pupil_size = rest_pupil_size, export_pupilinfo = export_pupilinfo) except Exception as e: warn(str(e)) if params.DEBUG: raise else: continue else: continue subsegments.append(new_sub_seg) samp_inds.append((all_start,all_end)) fix_inds.append((fix_start, fix_end)) if event_data != None: event_inds.append((event_start, event_end)) sub_segid +=1 # handling the last sub_seg sub_seg_time_end = seg_end #end of last sub_seg is the end of seg last_samp_idx, all_start,all_end = get_chunk(all_data, last_samp_idx, sub_seg_time_start, sub_seg_time_end) last_fix_idx, fix_start, fix_end = get_chunk(fixation_data, last_fix_idx, sub_seg_time_start, sub_seg_time_end) if event_data != None: last_event_idx, event_start, event_end = get_chunk(event_data, last_event_idx, sub_seg_time_start, sub_seg_time_end) if fix_end - fix_start>0: #add the last sub_seg try: if event_data != None: new_sub_seg = Segment(segid+"_"+str(sub_segid), all_data[all_start:all_end], fixation_data[fix_start:fix_end], event_data=event_data[event_start:event_end], aois=aoilist, prune_length=prune_length, rest_pupil_size = rest_pupil_size, export_pupilinfo = export_pupilinfo) else: new_sub_seg = Segment(segid+"_"+str(sub_segid), all_data[all_start:all_end], fixation_data[fix_start:fix_end], event_data=None, aois=aoilist, prune_length=prune_length, rest_pupil_size = rest_pupil_size, export_pupilinfo = export_pupilinfo) except Exception as e: warn(str(e)) if params.DEBUG: raise else: new_sub_seg = None if new_sub_seg != None: subsegments.append(new_sub_seg) samp_inds.append((all_start,all_end)) fix_inds.append((fix_start, fix_end)) if event_data != None: event_inds.append((event_start, event_end)) #end of handling the last sub_seg return subsegments, samp_inds, fix_inds, event_inds ######################################## if len(all_data)<=0: raise Exception('A scene with no sample data!') if Segments == None: self.segments = [] # print "seglist",seglist for (segid, start, end) in seglist: print "segid, start, end:",segid, start, end if prune_length != None: end = min(end, start+prune_length) _, all_start, all_end = get_chunk(all_data, 0, start, end) _, fix_start, fix_end = get_chunk(fixation_data, 0, start, end) if event_data != None: _, event_start, event_end = get_chunk(event_data, 0, start, end) if fix_end - fix_start>0: try: if event_data != None: new_seg = Segment(segid, all_data[all_start:all_end], fixation_data[fix_start:fix_end], event_data=event_data[event_start:event_end], aois=aoilist, prune_length=prune_length, rest_pupil_size = rest_pupil_size, export_pupilinfo = export_pupilinfo) else: new_seg = Segment(segid, all_data[all_start:all_end], fixation_data[fix_start:fix_end], event_data=None, aois=aoilist, prune_length=prune_length, rest_pupil_size = rest_pupil_size, export_pupilinfo = export_pupilinfo) except Exception as e: warn(str(e)) if params.DEBUG: raise else: continue else: continue if (new_seg.largest_data_gap > params.MAX_SEG_TIMEGAP) and auto_partition: #low quality segment that needs to be partitioned! new_segs, samp_inds, fix_inds, event_inds = partition_segment(new_seg, start, end, rest_pupil_size, export_pupilinfo=export_pupilinfo) if event_data != None: for nseg,samp,fix,eve in zip(new_segs, samp_inds, fix_inds, event_inds): if nseg.length > params.MINSEGSIZE: nseg.set_indices(samp[0],samp[1],fix[0],fix[1],eve[0],eve[1]) self.segments.append(nseg) else: for nseg,samp,fix in zip(new_segs, samp_inds, fix_inds): if nseg.length > params.MINSEGSIZE: nseg.set_indices(samp[0],samp[1],fix[0],fix[1]) self.segments.append(nseg) else: #good quality segment OR no auto_partition if event_data != None: new_seg.set_indices(all_start,all_end,fix_start,fix_end,event_start,event_end) else: new_seg.set_indices(all_start,all_end,fix_start,fix_end) self.segments.append(new_seg) else: self.segments = Segments #segments are already generated self.require_valid_Segments = require_valid if require_valid: #filter out the invalid Segments segments = filter(lambda x:x.is_valid,self.segments) else: segments = self.segments if len(segments)==0: raise Exception('no segments in scene %s!' %(scid)) fixationlist = [] eventlist = [] sample_list = [] totalfixations = 0 firstsegtime = float('infinity') firstseg = None for seg in segments: sample_st,sample_end,fix_start,fix_end,event_st,event_end = seg.get_indices() if params.DEBUG: print "sample_st,sample_end,fix_start,fix_end",sample_st,sample_end,fix_start,fix_end,event_st,event_end sample_list.append(all_data[sample_st:sample_end]) fixationlist.append(fixation_data[fix_start:fix_end]) totalfixations += len(fixationlist[-1]) if event_data != None: eventlist.append(event_data[event_st:event_end]) totalevents = len(eventlist[-1]) if seg.start < firstsegtime: firstsegtime = seg.start firstseg = seg self.firstseg = firstseg self.scid = scid self.features = {} self.largest_data_gap = maxfeat(self.segments,'largest_data_gap') #self.segments is used to calculate validity of the scenes instead of segments which is only valid segments self.proportion_valid = weightedmeanfeat(self.segments,'numsamples','proportion_valid') #self.segments is used to calculate validity of the scenes instead of segments which is only valid segments self.proportion_valid_fix = weightedmeanfeat(self.segments,'numsamples','proportion_valid_fix') #self.segments is used to calculate validity of the scenes instead of segments which is only valid segments self.validity1 = self.calc_validity1() self.validity2 = self.calc_validity2() self.validity3 = self.calc_validity3() self.is_valid = self.get_validity() self.length = sumfeat(segments,'length') if self.length == 0: raise Exception('Zero length segments!') self.features['numsegments'] = len(segments) self.features['length'] = self.length self.start = minfeat(segments,'start') self.numfixations = sumfeat(segments,'numfixations') self.end = maxfeat(segments,'end') self.numsamples = sumfeat(segments, 'numsamples') self.features['numsamples'] = self.numsamples self.numfixations = sumfeat(segments, 'numfixations') self.features['numfixations'] = self.numfixations if prune_length == None: if self.numfixations != totalfixations: raise Exception('error in fixation count for scene:'+self.scid) #warn ('error in fixation count for scene:'+self.scid) self.features['fixationrate'] = float(self.numfixations) / self.length if self.numfixations > 0: self.features['meanfixationduration'] = weightedmeanfeat(segments,'numfixations',"features['meanfixationduration']") self.features['stddevfixationduration'] = stddev(map(lambda x: float(x.fixationduration), reduce(lambda x,y: x+y ,fixationlist)))## self.features['sumfixationduration'] = sumfeat(segments, "features['sumfixationduration']") self.features['fixationrate'] = float(self.numfixations)/self.length distances = self.calc_distances(fixationlist) abs_angles = self.calc_abs_angles(fixationlist) rel_angles = self.calc_rel_angles(fixationlist) else: self.features['meanfixationduration'] = 0 self.features['stddevfixationduration'] = 0 self.features['sumfixationduration'] = 0 self.features['fixationrate'] = 0 distances = [] if len(distances) > 0: self.features['meanpathdistance'] = mean(distances) self.features['sumpathdistance'] = sum(distances) self.features['stddevpathdistance'] = stddev(distances) self.features['eyemovementvelocity'] = self.features['sumpathdistance']/self.length self.features['sumabspathangles'] = sum(abs_angles) self.features['meanabspathangles'] = mean(abs_angles) self.features['abspathanglesrate'] = sum(abs_angles)/self.length self.features['stddevabspathangles'] = stddev(abs_angles) self.features['sumrelpathangles'] = sum(rel_angles) self.features['relpathanglesrate'] = sum(rel_angles)/self.length self.features['meanrelpathangles'] = mean(rel_angles) self.features['stddevrelpathangles'] = stddev(rel_angles) else: self.features['meanpathdistance'] = 0 self.features['sumpathdistance'] = 0 self.features['stddevpathdistance'] = 0 self.features['eyemovementvelocity'] = 0 self.features['sumabspathangles'] = 0 self.features['abspathanglesrate'] = 0 self.features['meanabspathangles']= 0 self.features['stddevabspathangles']= 0 self.features['sumrelpathangles'] = 0 self.features['relpathanglesrate'] = 0 self.features['meanrelpathangles']= 0 self.features['stddevrelpathangles'] = 0 """ calculate pupil dilation features (no rest pupil size adjustments yet)""" self.numpupilsizes = sumfeat(segments,'numpupilsizes') self.adjvalidpupilsizes = mergevalues(segments, 'adjvalidpupilsizes') if self.numpupilsizes > 0: # check if scene has any pupil data if export_pupilinfo: self.pupilinfo_for_export = mergevalues(segments, 'pupilinfo_for_export') self.features['meanpupilsize'] = weightedmeanfeat(segments, 'numpupilsizes', "features['meanpupilsize']") self.features['stddevpupilsize'] = stddev(self.adjvalidpupilsizes) self.features['maxpupilsize'] = maxfeat(segments, "features['maxpupilsize']") self.features['minpupilsize'] = minfeat(segments, "features['minpupilsize']") self.features['startpupilsize'] = segments[0].features['startpupilsize'] self.features['endpupilsize'] = segments[-1].features['endpupilsize'] else: self.pupilinfo_for_export = [] self.features['meanpupilsize'] = 0 self.features['stddevpupilsize'] = 0 self.features['maxpupilsize'] = 0 self.features['minpupilsize'] = 0 self.features['startpupilsize'] = 0 self.features['endpupilsize'] = 0 """end """ self.numdistances = sumfeat(segments,'numdistances') #Distance self.distances_from_screen = mergevalues(segments, 'distances_from_screen') if self.numdistances > 0: # check if scene has any pupil data self.features['meandistance'] = weightedmeanfeat(segments, 'numdistances', "features['meandistance']") self.features['stddevdistance'] = stddev(self.distances_from_screen) self.features['maxdistance'] = maxfeat(segments, "features['maxdistance']") self.features['mindistance'] = minfeat(segments, "features['mindistance']") self.features['startdistance'] = segments[0].features['startdistance'] self.features['enddistance'] = segments[-1].features['enddistance'] else: self.features['meandistance'] = 0 self.features['stddevdistance'] = 0 self.features['maxdistance'] = 0 self.features['mindistance'] = 0 self.features['startdistance'] = 0 self.features['enddistance'] = 0 """end """ if event_data != None: self.features['numevents'] = sumfeat(segments,'numevents') self.features['numleftclic'] = sumfeat(segments,"features['numleftclic']") self.features['numrightclic'] = sumfeat(segments, "features['numrightclic']") self.features['numdoubleclic'] = sumfeat(segments, "features['numdoubleclic']") self.features['numkeypressed'] = sumfeat(segments, "features['numkeypressed']") self.features['leftclicrate'] = float(self.features['numleftclic'])/self.length self.features['rightclicrate'] = float(self.features['numrightclic'])/self.length self.features['doubleclicrate'] = float(self.features['numdoubleclic'])/self.length self.features['keypressedrate'] = float(self.features['numkeypressed'])/self.length self.features['timetofirstleftclic'] = segments[0].features['timetofirstleftclic'] self.features['timetofirstrightclic'] = segments[0].features['timetofirstrightclic'] self.features['timetofirstdoubleclic'] = segments[0].features['timetofirstdoubleclic'] self.features['timetofirstkeypressed'] = segments[0].features['timetofirstkeypressed'] else: self.features['numevents'] = 0 self.features['numleftclic'] = 0 self.features['numrightclic'] = 0 self.features['numdoubleclic'] = 0 self.features['numkeypressed'] = 0 self.features['leftclicrate'] = 0 self.features['rightclicrate'] = 0 self.features['doubleclicrate'] = 0 self.features['keypressedrate'] = 0 self.features['timetofirstleftclic'] = -1 self.features['timetofirstrightclic'] = -1 self.features['timetofirstdoubleclic'] = -1 self.features['timetofirstkeypressed'] = -1 """end """ self.has_aois = False if aoilist: self.set_aois(segments, aoilist) self.features['aoisequence'] = self.merge_aoisequences(segments)
def drawChildren(self): for c in self.children: s = Segment(self.window, self.state.copy(), c.state.copy()) s.draw("green")
def __init__(self, scid, seglist, all_data, fixation_data, Segments = None, aoilist = None, prune_length= None, require_valid = True, auto_partition = False): ''' @type scid: str @param scid: The id of the scene. @type segements: List of Segment.Segement @param scid: The segments belonging to this scene @type all_data: array of L{Datapoints<Datapoint.Datapoint>} @param all_data: The datapoints which make up this Trial. @type fixation_data: array of L{Fixations<Datapoint.Fixation>} @param fixation_data: The fixations which make up this Trial. @type aois: array of L{AOIs<AOI.AOI>} @param aois: The AOIs relevant to this trial @type prune_length: int ''' def partition_segement(new_seg, seg_start,seg_end): timegaps = new_seg.getgaps() subsegments = [] sub_segid=0 samp_inds = [] fix_inds = [] last_samp_idx = 0 last_fix_idx = 0 sub_seg_time_start = seg_start for timebounds in timegaps: sub_seg_time_end = timebounds[0] #end of this sub_seg is start of this gap last_samp_idx, all_start,all_end = get_chunk(all_data, last_samp_idx, sub_seg_time_start, sub_seg_time_end) last_fix_idx, fix_start, fix_end = get_chunk(fixation_data, last_fix_idx, sub_seg_time_start, sub_seg_time_end) sub_seg_time_start = timebounds[1] #beginning of the next sub_seg is end of this gap if fix_end - fix_start>0: new_sub_seg = Segment(segid, all_data[all_start:all_end], fixation_data[fix_start:fix_end], aois=aoilist, prune_length=prune_length) else: continue subsegments.append(new_sub_seg) samp_inds.append((all_start,all_end)) fix_inds.append((fix_start, fix_end)) sub_segid +=1 # handling the last sub_seg sub_seg_time_end = seg_end #end of last sub_seg is the end of seg last_samp_idx, all_start,all_end = get_chunk(all_data, last_samp_idx, sub_seg_time_start, sub_seg_time_end) last_fix_idx, fix_start, fix_end = get_chunk(fixation_data, last_fix_idx, sub_seg_time_start, sub_seg_time_end) if fix_end - fix_start>0: #add the last sub_seg new_sub_seg = Segment(segid, all_data[all_start:all_end], fixation_data[fix_start:fix_end], aois=aoilist, prune_length=prune_length) subsegments.append(new_sub_seg) samp_inds.append((all_start,all_end)) fix_inds.append((fix_start, fix_end)) #end of handling the last sub_seg return subsegments, samp_inds, fix_inds if len(all_data)<=0: raise Exception('A scene with no sample data!') if Segments == None: self.segments = [] # print "seglist",seglist for (segid, start, end) in seglist: print "segid, start, end:",segid, start, end _, all_start, all_end = get_chunk(all_data, 0, start, end) _, fix_start, fix_end = get_chunk(fixation_data, 0, start, end) if fix_end - fix_start>0: new_seg = Segment(segid, all_data[all_start:all_end], fixation_data[fix_start:fix_end], aois=aoilist, prune_length=prune_length) else: continue if (new_seg.largest_data_gap > params.MAX_SEG_TIMEGAP) and auto_partition: #low quality segment that needs to be partitioned! new_segs, samp_inds, fix_inds = partition_segement(new_seg, start, end) for nseg,samp,fix in zip(new_segs, samp_inds, fix_inds): nseg.set_indices(samp[0],samp[1],fix[0],fix[1]) self.segments.append(nseg) else: #good quality segment OR no auto_partition new_seg.set_indices(all_start,all_end,fix_start,fix_end) self.segments.append(new_seg) else: self.segments = Segments #segments are already generated if require_valid: segments = filter(lambda x:x.is_valid,self.segments) else: segments = self.segments if len(segments)==0: raise Exception('no segments in scene %s!' %(scid)) fixationlist = [] sample_list = [] totalfixations = 0 firstsegtime = float('infinity') firstseg = None for seg in segments: sample_st,sample_end,fix_start,fix_end = seg.get_indices() if params.DEBUG: print "sample_st,sample_end,fix_start,fix_end",sample_st,sample_end,fix_start,fix_end sample_list.append(all_data[sample_st:sample_end]) fixationlist.append(fixation_data[fix_start:fix_end]) totalfixations += len(fixationlist[-1]) if seg.start < firstsegtime: firstsegtime = seg.start firstseg = seg self.firstseg = firstseg self.scid = scid self.features = {} self.largest_data_gap = maxfeat(self.segments,'largest_data_gap') #self.segments is used to calculate validity of the scenes instead of segments which is only valid segments self.proportion_valid = weightedmeanfeat(self.segments,'numsamples','proportion_valid') #self.segments is used to calculate validity of the scenes instead of segments which is only valid segments self.proportion_valid_fix = weightedmeanfeat(self.segments,'numsamples','proportion_valid_fix') #self.segments is used to calculate validity of the scenes instead of segments which is only valid segments self.validity1 = self.calc_validity1() self.validity2 = self.calc_validity2() self.validity3 = self.calc_validity3() self.is_valid = self.get_validity() self.length = sumfeat(segments,'length') if self.length == 0: raise Exception('Zero length segments!') self.features['numsegments'] = len(segments) self.features['length'] = self.length self.start = minfeat(segments,'start') self.numfixations = sumfeat(segments,'numfixations') self.end = maxfeat(segments,'end') self.numsamples = sumfeat(segments, 'numsamples') self.features['numsamples'] = self.numsamples self.numfixations = sumfeat(segments, 'numfixations') self.features['numfixations'] = self.numfixations if self.numfixations != totalfixations: raise Exception('error in fixation count for scene:'+self.scid) #warn ('error in fixation count for scene:'+self.scid) self.features['fixationrate'] = float(self.numfixations) / self.length if self.numfixations > 0: self.features['meanfixationduration'] = weightedmeanfeat(segments,'numfixations',"features['meanfixationduration']") self.features['stddevfixationduration'] = stddev(map(lambda x: float(x.fixationduration), reduce(lambda x,y: x+y ,fixationlist)))## self.features['sumfixationduration'] = sumfeat(segments, "features['sumfixationduration']") self.features['fixationrate'] = float(self.numfixations)/self.length distances = self.calc_distances(fixationlist) abs_angles = self.calc_abs_angles(fixationlist) rel_angles = self.calc_rel_angles(fixationlist) else: self.features['meanfixationduration'] = 0 self.features['stddevfixationduration'] = 0 self.features['sumfixationduration'] = 0 self.features['fixationrate'] = 0 distances = [] if len(distances) > 0: self.features['meanpathdistance'] = mean(distances) self.features['sumpathdistance'] = sum(distances) self.features['stddevpathdistance'] = stddev(distances) self.features['sumabspathangles'] = sum(abs_angles) self.features['meanabspathangles'] = mean(abs_angles) self.features['stddevabspathangles'] = stddev(abs_angles) self.features['sumrelpathangles'] = sum(rel_angles) self.features['meanrelpathangles'] = mean(rel_angles) self.features['stddevrelpathangles'] = stddev(rel_angles) else: self.features['meanpathdistance'] = 0 self.features['sumpathdistance'] = 0 self.features['stddevpathdistance'] = 0 self.features['sumabspathangles'] = 0 self.features['meanabspathangles']= 0 self.features['stddevabspathangles']= 0 self.features['sumrelpathangles'] = 0 self.features['meanrelpathangles']= 0 self.features['stddevrelpathangles'] = 0 self.has_aois = False if aoilist: self.set_aois(segments, aoilist,fixationlist)