def main(): # scan the target folder and process each of them if len(sys.argv)!=3: print "provide the target folder, output folder and run!\n" sys.abort() USER = getpass.getuser() TARGET_FOLDER = sys.argv[1] STMT_FOLDER = sys.argv[2] try: os.mkdir(STMT_FOLDER) except: pass files = [] linear_total = 0 non_total = 0 for f in os.listdir(TARGET_FOLDER): if os.path.isfile(TARGET_FOLDER+'/'+f) and f[-3:] == 'dot': files.append(TARGET_FOLDER+'/'+f) for infilename in files: #print "now processing the dot file %s: \n" % infilename curlinear,curnonlinear = process_one(infilename,STMT_FOLDER) print "%s, %d, %d\n" %(infilename,curlinear,curnonlinear) linear_total += curlinear non_total += curnonlinear print "In total: %d / %d = %0.3f" % (non_total,non_total+linear_total,float(non_total)/(non_total+linear_total))
def OnCreateSessionResult(self, code): if 105000 == code: print "login ok" self.setReady(True) self.start() else: sys.abort()
def mpas_to_triangle(mpasfile, trifile): fin = NetCDFFile(options.mpasfile, 'r') if fin.on_a_sphere == "YES": sys.abort("ERROR: This script only works for planar meshes!") if len(fin.dimensions['vertexDegree']) != 3: sys.abort("ERROR: This script only works for vertexDegree of 3!") nCells = len(fin.dimensions['nCells']) nVertices = len(fin.dimensions['nVertices']) xCell = fin.variables['xCell'][:] yCell = fin.variables['yCell'][:] ConC = fin.variables['cellsOnCell'][:] nConC = fin.variables['nEdgesOnCell'][:] ConV = fin.variables['cellsOnVertex'][:] # create node file fnode = open(options.trifile + ".node", 'w') # write node file header: First line: <# of vertices> <dimension (must # be 2)> <# of attributes> <# of boundary markers (0 or 1)> fnode.write("{:d} 2 0 1\n".format(nCells)) # Remaining lines: <vertex #> <x> <y> [attributes] [boundary marker] for i in range(nCells): if ConC[i, 0:nConC[i]].min() == 0: isBdy = 1 else: isBdy = 0 fnode.write("{:d} {:f} {:f} {:d}\n".format(i + 1, xCell[i], yCell[i], isBdy)) fnode.write("# Generated from MPAS file: {}\n".format(options.mpasfile)) fnode.close() # create ele file fele = open(options.trifile + ".ele", "w") # calculate number of non-degenerate triangles numtri = 0 for i in range(nVertices): if ConV[i, :].min() > 0: numtri += 1 # write ele file header: First line: <# of triangles> <nodes per # triangle> <# of attributes> fele.write("{:d} 3 0\n".format(numtri)) # Remaining lines: <triangle #> <node> <node> <node> ... [attributes] cnt = 0 for i in range(nVertices): # write non-generate triangles only if ConV[i, :].min() > 0: cnt += 1 fele.write("{:d} {:d} {:d} {:d}\n".format(cnt, ConV[i, 0], ConV[i, 1], ConV[i, 2])) fele.write("# Generated from MPAS file: {}\n".format(options.mpasfile)) fele.close() fin.close() print("Conversion complete.")
def load_config(configfile): if not os.path.exists(configfile): print(f"Config file {configfile} not found.") sys.abort(-1) else: with open(configfile, 'r') as f: config = json.load(f) return config
def init(self): try: self.sock.bind((self.ip, self.port)) self.sock.listen(0) except OSError: print( f'[{datetime.datetime.now()}] [MAINSERVER] Failed to init server: address already in use' ) abort(1)
def OnConnectIS(self, bConn, isid, reason, p, islist): if bConn: print "1 Connect OK" self.getps().login(self.p) else: print "1 Error", self.getps().getError(reason) import sys sys.abort(0) pass
def attribute_check(D): assert isinstance(D, dict) for key in ['node_number', 'edge_number', 'type', 'complete']: assert key in D if D['complete'] is True: assert 'dfunction' in D else: assert 'edge_list' in D if D['type'] == 'matrix': assert 'matrix' in D elif D['type'] == 'features': assert 'features' in D elif D['type'] == 'graph': 'dlist' in D else: sys.abort('Attribute type incorrect')
def multigraph_setup(dissimilarity_list, verbose=0, **kwargs): """\ If the DISS object is not set up yet, it sets it up from a list of dissimilarity relations. If D0 is a an array or list of arrays, then it is assumed that it consists of square matrices containing pairwise distances. If D0 is a dictionary, it is assumed that it already contains the distance graphs. """ if isinstance(dissimilarity_list, DISS): return dissimilarity_list if isinstance(dissimilarity_list, list): K = len(dissimilarity_list) D0 = dissimilarity_list[0] if isinstance(D0, np.ndarray): node_number = len(D0) elif isinstance(D0, dict) and 'node_number' in D0: node_number = D0['node_number'] else: sys.abort('number of nodes not found') diss = DISS(node_number, verbose=verbose) for D in dissimilarity_list: if isinstance(D, np.ndarray): if len(D.shape) == 2 and D.shape[0] == D.shape[1]: diss.add_matrix(D, verbose=verbose) else: diss.add_feature(D, verbose=verbose) elif isinstance(D, dict): diss.add_graph(verbose=verbose, **D) return diss if isinstance(dissimilarity_list, dict): if 'node_number' in dissimilarity_list: node_number = dissimilarity_list['node_number'] else: node_number = dissimilarity_list['nodes'] diss = DISS(node_number) diss.add_projections(**dissimilarity_list) return diss
def read_players(): """Reads a player list and returns it.""" players = read_it('players') if players == -1: try: f = open('players' + BLACKJACK_FILE_EXTENSION, "wb") players = [] pickle.dump(players, f) f.close() print "Created an empty players file." return players except IOError: print "Could not create players file." print "Exiting." sys.abort() return players
def error(self, message, logger='main'): self.log(logging.ERROR, message, logger) sys.abort()
dest="trifile", help= "output file name template to be in triangle format (FILE.1.node, FILE.1.ele).", metavar="FILE") options, args = parser.parse_args() if not options.mpasfile: parser.error("An input MPAS file is required.") if not options.trifile: parser.error("A output Triangle format file name is required.") fin = NetCDFFile(options.mpasfile, 'r') if fin.on_a_sphere == "YES": sys.abort("ERROR: This script only works for planar meshes!") if len(fin.dimensions['vertexDegree']) != 3: sys.abort("ERROR: This script only works for vertexDegree of 3!") nCells = len(fin.dimensions['nCells']) nVertices = len(fin.dimensions['nVertices']) xCell = fin.variables['xCell'][:] yCell = fin.variables['yCell'][:] ConC = fin.variables['cellsOnCell'][:] nConC = fin.variables['nEdgesOnCell'][:] ConV = fin.variables['cellsOnVertex'][:] # create node file fnode = open(options.trifile + ".node", 'w')
ports = serial.tools.list_ports.comports() laserAddress = "" for port in ports: #print port typ = port[0] dev = port[2] if "USB VID:PID=483" in dev: print "range finder found." laserAddress = typ """if we still don't have a port, abort.""" if laserAddress == "": print "no ranger found" sys.abort() path = 'logfile.csv' if len(sys.argv) == 2: path = sys.argv[1] arcom = serial.Serial(laserAddress, 115200) """get the current system path and create a log file for the sensor data.""" log = open(path, 'w') startTime = time.time() lastTime = 0 measureDistance(arcom, MEAS_CONT_START, 2) while time.time() - startTime < sampleTime+1: if time.time() - lastTime > 0.035:
def render_slice(x,y,z,var,vol,xrange,yrange,zrange,plane,loc,resolution,kernel="gaussian",number=0, root=root, time=0.): dx = (xrange[1]-xrange[0])/(resolution) dy = (yrange[1]-yrange[0])/(resolution) dz = (zrange[1]-zrange[0])/(resolution) print('rendering %s slice at %s=%f using %s kernel'%(root,plane,loc,kernel)) #create new xp, yp coordinates corresponding to x, y of image, and a z for depth. xp=[] yp=[] zp=[] volp=[] varp=[] if plane == "x": if kernel == "gaussian": xmin = loc-gwidth*dx xmax = loc+gwidth*dx else: xmin = loc-dx xmax = loc+dx dxp=dy dyp=dz dzp=dx for i in range(len(x)): if x[i] > xmin and x[i] < xmax: xp.append(y[i]) yp.append(z[i]) zp.append(x[i]) volp.append(vol[i]) varp.append(var[i]) xg=np.arange(yrange[0],yrange[1]+dy,dy) yg=np.arange(zrange[0],zrange[1]+dz,dz) elif plane == "y": if kernel == "gaussian": ymin = loc-gwidth*dy ymax = loc+gwidth*dy else: ymin = loc-dy ymax = loc+dy dxp=dx dyp=dz dzp=dy for i in range(len(y)): if y[i] > ymin and y[i] < ymax: xp.append(x[i]) yp.append(z[i]) zp.append(y[i]) volp.append(vol[i]) varp.append(var[i]) xg=np.arange(xrange[0],xrange[1]+dx,dx) yg=np.arange(zrange[0],zrange[1]+dz,dz) elif plane == "z": if kernel == "gaussian": zmin = loc-gwidth*dz zmax = loc+gwidth*dz else: zmin = loc-dz zmax = loc+dz dxp=dx dyp=dy dzp=dz for i in range(len(z)): if z[i] > zmin and z[i] < zmax: xp.append(x[i]) yp.append(y[i]) zp.append(z[i]) volp.append(vol[i]) varp.append(var[i]) xg=np.arange(xrange[0],xrange[1]+dx,dx) yg=np.arange(yrange[0],yrange[1]+dy,dy) else: print("Invalid plane. Aborting") print(plane) sys.exit() print("Identified %d parcels in plane"%len(xp)) img=np.zeros((resolution+1,resolution+1)) wgt=np.zeros((resolution+1,resolution+1)) for num in range(len(xp)): #identify the cell we're in (index of lower left corner) i=int(math.floor(xp[num]/dxp)) j=int(math.floor(yp[num]/dyp)) if kernel == "linear": for ii in range(i,i+2): for jj in range(j,j+2): w = (1.-np.abs(xp[num]-xg[ii])/dxp)*(1.-np.abs(yp[num]-yg[jj])/dyp)*(1-np.abs(zp[num]-loc)/dzp)*volp[num] img[ii,jj] += varp[num]*w wgt[ii,jj] += w elif kernel == "gaussian": for ii in range(i-gwidth,i+gwidth): if ii >= 0 and ii <= resolution: for jj in range(j-gwidth,j+gwidth): if jj >= 0 and jj <= resolution: w = np.exp( -(xp[num] - xg[ii])**2/dxp**2 - (yp[num] - yg[jj])**2/dyp**2 -(zp[num] - loc)**2/dzp**2) * volp[num] img[ii,jj] += varp[num]*w wgt[ii,jj] += w else: print("Error, invalid kernel '%s'. Aborting."%kernel) print(sys.abort()) #Remove any zero weights counter=0 for i in range(resolution+1): for j in range(resolution+1): if wgt[i,j] == 0.: wgt[i,j]=1. counter+=1 print("Removed %d zeroed pixels"%counter) img=img/wgt img=img.T if root == "vort": plt.imshow(img,origin='lower',extent=[0,1,0,1],vmin=0.,vmax=max(10.,np.amax(img))) elif root == "hgliq": plt.imshow(img,origin='lower',extent=[0,1,0,1],vmin=0.,vmax=0.08) else: plt.imshow(img,origin='lower',extent=[0,1,0,1]) plt.colorbar() plt.title("Time = %f"%time) #plt.savefig("%s_%04d.png"%(root,number)) #plt.clf() plt.show()
str_res = key + ":1" else: str_res = str_res + "\t" + key + ":1" return str_res # load the vedio feature vid_value_dict = {} vid_pid_dict = {} for line in open("./rec_media_doc_info.txt"): try: vid, pid, value = line.strip().split("\t") except: sys.abort() continue vid_value_dict[vid] = value if pid and pid != "NULL": vid_pid_dict[vid] = pid #read the vid ctr information vid_ctr_dict = {} for line in open("./recvid_rate.txt"): try: vid, rate, fenzi, fenmu, rate_value, rate_log, rate_buckt, click_value, click_log, click_buckt, exp_value, exp_log, exp_buckt = line.strip( ).split(" ") except: continue
def mpas_to_triangle(mpasfile, trifile): """ Script to convert from MPAS netCDF format to the Triangle format: https://www.cs.cmu.edu/~quake/triangle.node.html https://www.cs.cmu.edu/~quake/triangle.ele.html Parameters ---------- mpasfile : str The path to an MPAS mesh in NetCDF format trifile : str The prefix for the Triangle output files. Files with extensions ``.node`` and ``.ele`` will be produced. Only works for planar meshes. """ fin = NetCDFFile(mpasfile, 'r') if fin.on_a_sphere == "YES": sys.abort("ERROR: This script only works for planar meshes!") if len(fin.dimensions['vertexDegree']) != 3: sys.abort("ERROR: This script only works for vertexDegree of 3!") nCells = len(fin.dimensions['nCells']) nVertices = len(fin.dimensions['nVertices']) xCell = fin.variables['xCell'][:] yCell = fin.variables['yCell'][:] ConC = fin.variables['cellsOnCell'][:] nConC = fin.variables['nEdgesOnCell'][:] ConV = fin.variables['cellsOnVertex'][:] # create node file fnode = open(trifile + ".node", 'w') # write node file header: First line: <# of vertices> <dimension (must # be 2)> <# of attributes> <# of boundary markers (0 or 1)> fnode.write("{:d} 2 0 1\n".format(nCells)) # Remaining lines: <vertex #> <x> <y> [attributes] [boundary marker] for i in range(nCells): if ConC[i, 0:nConC[i]].min() == 0: isBdy = 1 else: isBdy = 0 fnode.write( "{:d} {:f} {:f} {:d}\n".format( i + 1, xCell[i], yCell[i], isBdy)) fnode.write("# Generated from MPAS file: {}\n".format(mpasfile)) fnode.close() # create ele file fele = open(trifile + ".ele", "w") # calculate number of non-degenerate triangles numtri = 0 for i in range(nVertices): if ConV[i, :].min() > 0: numtri += 1 # write ele file header: First line: <# of triangles> <nodes per # triangle> <# of attributes> fele.write("{:d} 3 0\n".format(numtri)) # Remaining lines: <triangle #> <node> <node> <node> ... [attributes] cnt = 0 for i in range(nVertices): # write non-generate triangles only if ConV[i, :].min() > 0: cnt += 1 fele.write("{:d} {:d} {:d} {:d}\n".format( cnt, ConV[i, 0], ConV[i, 1], ConV[i, 2])) fele.write("# Generated from MPAS file: {}\n".format(mpasfile)) fele.close() fin.close() print("Conversion complete.")