def hello(self, im): " filename is the classavg image " if self.docfile == "" or self.serfile == "": self.callTemplates() filename = im.info['filename'] #print "hello from " + filename tmp = self.doctemplate.get() fn = Spiderutils.template2filename(tmp, os.path.basename(filename)) if len(fn) < 1: return if not os.path.exists(fn): print "Unable to find " + fn return D = Spiderutils.readdoc(fn, keys='all') plist = [] files = D.keys() for f in files: num = int(D[f][0]) # each element is a list (of column data) imgfilename = Spiderutils.template2filename(self.serfile, num) plist.append(imgfilename) title = "images for class: " + os.path.basename(filename) newtop = Toplevel(self.top) m = montage.montage(newtop, imagelist=plist, title=title, useLabels=0) m.makeMenus() m.createMontage()
def GetIconName(self): if not self.IsExpandable(): name, ext = os.path.splitext(self.path) if ext in ['.py']: #, '.pyc','.pyo']: return "python" if ext.lower() in self.imagetypes: return "image" typ = Spiderutils.istextfile(self.path) if typ == 1: if Spiderutils.isSpiderDocfile(self.path): return "docfile" elif Spiderutils.isSpiderBatchfile(self.path): return "procfile" elif Spiderutils.isSpiderProcedurefile(self.path): return "procfile" else: return "text" elif typ == -1: return "unknown" spi = Spiderutils.isSpiderBin(self.path) if spi == "image" or spi == "Fourier" or spi == "stack": return "spider" elif spi == "volume": return "volume" return "binary" # i.e. a non-spider binary file
def getdata(self, filename=None, xcol=None, ycol=None): if filename == None or filename == "": filename = self.filename if self.filename == None or self.filename == "": return try: #D = spiderutils.readSpiderDocFile(filename, [xcol,ycol]) D = Spiderutils.readdoc(filename, keys='all') except: print "unable to get data from %s" % filename return keys = D.keys() if len(keys) < 1: print "%s contains no data" % filename return f = D[keys[0]] xmin = f[0] xmax = f[0] ymin = f[1] ymax = f[1] for k in keys: x = D[k][0] y = D[k][1] if x < xmin: xmin = x elif x > xmax: xmax = x if y < ymin: ymin = y elif y > ymax: ymax = y vector_x = (xmin, xmax) vector_y = (ymin, ymax) return (D, vector_x, vector_y)
def callTemplates(self): gtwin = Toplevel(self.top) self.getTemplates(gtwin) self.top.wait_window(gtwin) # wait til template window gone # if they typed in w/o asterisks.. doc = self.doctemplate.get() if len(doc) > 0: self.docfile = doc if string.find(doc, "*") < 0: tmp = Spiderutils.name2template(doc) self.doctemplate.set(tmp) ser = self.sertemplate.get() if len(ser) > 0: self.serfile = ser if string.find(ser, "*") < 0: tmp = Spiderutils.name2template(ser) self.sertemplate.set(tmp)
def writedoc(filename, column1=1, column2=0): column1 = integer(column1) column2 = integer(column2) # if file already exists if os.path.exists(filename): # if it's a doc file, try to get the last key if Spiderutils.isSpiderDocfile(filename): lastline = getoutput("tail -1 %s" % filename) if len(lastline) > 0: key = 1 + int(string.split(lastline)[0]) else: key = 1 else: # if it's not a doc file... os.remove(filename) key = 1 data = "%5d 2 %11d% 11d\n" % (key, column1, column2) try: fp = open(filename, 'a') # append fp.write(data) fp.close() except: print "unable to write to %s" % filename return 0 # if it's a new file else: try: fp = open(filename, 'w') fname = os.path.basename(filename) ext = os.path.splitext(filename)[1] ext = ext[1:] # remove dot date, time, id = Spiderutils.nowisthetime() h = " ;ctf/%s %s AT %s %s\n" % (ext, date, time, fname) fp.write(h) fp.write(" ; / MICROGRAPH DEFOCUS\n") key = 1 data = "%5d 2 %11d %11d\n" % (key, column1, column2) fp.write(data) fp.close() except: print "unable to create %s" % filename return 0 return 1
def setTemplates(self, parent, which='doc'): filename = askopenfilename(parent=parent, filetypes=self.filetypes) if len(filename) < 1: return tmp = Spiderutils.name2template(filename) if which == 'doc': self.doctemplate.set(tmp) else: self.sertemplate.set(tmp)
def saveSelectedMarkers(self, filename=None): if self.selectedPolygon == "": self.statusMessage("no polygon selected") return if filename == None: filename = asksaveasfilename(title="Save points into a doc file") if len(filename) == 0: return print filename if type(filename) == type((1, 2)): print 'filename is a tuple!' poly = self.Polygons[self.selectedPolygon] e = poly.elements e.sort() E = [] # make header for doc file and column headings E.append(Spiderutils.makeDocfileHeader(filename)) xaxis = self.xaxisVar.get() if xaxis == "": xaxis = 'x axis' if len(xaxis) > 11: xaxis = xaxis[:11] # just use first 11 chars else: xaxis = string.center(xaxis, 11) yaxis = self.yaxisVar.get() if yaxis == "": yaxis = 'y axis' if len(yaxis) > 11: yaxis = yaxis[:11] # just use first 11 chars else: yaxis = string.center(yaxis, 11) keyhdr = string.center('keys', 11) E.append(" ; / %s %s %s\n" % (keyhdr, xaxis, yaxis)) n = len(e) for i in range(n): key = i + 1 val = e[i] x, y = self.D[val][0], self.D[val][1] line = "%5d %2d %11.1f %11f %11f\n" % (key, 3, float(e[i]), x, y) E.append(line) try: fp = open(filename, 'w') fp.writelines(E) fp.close() self.statusMessage('Data written to %s' % filename) except: self.statusMessage('ERROR: unable to write to %s' % filename, timeout=0)
def getSpiderInfo(self, filename): info = Spiderutils.spiderInfo(filename) if info == 0: return "" type = info[0] dims = info[1] infotxt = os.path.basename(filename) + "\n" infotxt += "Spider %s file %s\n" % (type, str(dims)) if len(info) > 2: stats = info[2] infotxt += "max %5.4f, min %5.4f, avg %5.4f, stdev %5.4f\n" % ( stats[0], stats[1], stats[2], stats[3]) return infotxt
def readDefocus(filename): " returns list of (mic#, defocus) pairs (mic=int defocus=string)" # F = spiderutils.readSpiderDocFile(filename, col_list=(1,2)) F = Spiderutils.readdoc(filename, keys='all') if F == None: return [] keys = F.keys() keys.sort() M = [] for key in keys: mic = int(F[key][0]) defocus = str(int(F[key][1])) M.append((mic, defocus)) return M
def readdoc(filename, factor=1.0, squared=1): F = Spiderutils.readdoc(filename, keys='all') if F == None: return [] roofile = 0 A = [] B = [] C = [] D = [] E = [] keys = F.keys() keys.sort() # get the 1st line of data, test if roo (cols 3 & 4 = 1) k = keys[0] vals = F[k] vlen = len(vals) if vlen < 4: roofile = 1 else: # if vals[2] == 1 and vals[3] == 1: roofile = 1 # get the data for key in keys: if roofile: a = factor * F[key][0] D.append(a) else: freq = F[key][0] bgd = factor * F[key][1] sub = factor * F[key][2] env = factor * F[key][3] roo = bgd + sub # get original spectrum if squared == 1: k = env + bgd roo = roo * roo # square the spectrum bgd = bgd * bgd # square the background sub = roo - bgd # new subtracted curve env = k * k - bgd # new envelope A.append(freq) B.append(bgd) C.append(sub) D.append(env) E.append(roo) if roofile: return [D] else: return [A, B, C, D, E]
def saveDefocus(self, filename=None): "save current defocus and file number to a doc file" if filename != None: self.savefile = filename if self.savefile == "": filename = asksaveasfilename() if filename == "": return self.savefile = filename self.setFileLabels() if self.tfedfile == "": print "defocus data will be saved to %s" % self.savefile return micnum = self.filenumber(os.path.basename(self.tfedfile)) defocus = int(float(self.defocus.get())) outfile = self.savefile headers = Spiderutils.getDocfileHeaders(outfile) if os.path.exists(outfile): # try to replace the line d = Spiderutils.readdoc(outfile, keys='all') keys = d.keys() found = 0 for k in keys: mic = d[k][0] if mic == micnum: d[k][1] = defocus found = 1 break if found: Spiderutils.writedoc(outfile, columns=d) else: Spiderutils.writedoc(outfile, columns=[[micnum], [defocus]], mode='a') else: Spiderutils.writedoc(outfile, columns=[[micnum], [defocus]], headers=headers) #if writedoc(self.savefile, column1=micnum, column2=defocus): print "defocus %s saved to %s" % (defocus, outfile)
def processargs(args): " returns (serverflag, filelist, directory) " serverflag = 0 readall = 0 filenames = [] cwd = os.getcwd() for item in args: if item == '-s': serverflag = 1 elif item == '-h' or item == '-help': helpmessage() elif os.path.isdir(item): # qview dir : reads all files in directory, but # qview * : should not read subdirectories if len(filenames) > 0: continue cwd = os.path.abspath(item) os.chdir(cwd) filenames = os.listdir(os.getcwd()) break else: filenames.append(item) if filenames == []: helpmessage() if readall: files = os.listdir(os.getcwd()) filenames = [] for file in files: if not os.path.exists(file): # why is this needed? continue # only include binary files if not os.path.isdir(file) \ and not Spiderutils.istextfile(file) \ and file[0] != '.': filenames.append(file) if len(filenames) == 0: print "no images found in %s" % os.getcwd() sys.exit() return (serverflag, filenames, cwd)
def OnDoubleClick(self): """Called on a double-click on the icon.""" self.status.set(self.path) # Directories: list them in window if os.path.isdir(self.path): dirmax = int(self.textbox.limits[3].get()) dirsize = os.path.getsize(self.path) if dirsize > dirmax: basename = os.path.basename(self.path) txt = "%s may contain a very large number of files.\nDisplay anyway?" % ( basename) if not askyesno("Warning", txt): return cmd = "ls -lF %s " % self.path out = getoutput(cmd) filelisting = self.path + "\n" + out self.textbox.clear() self.textbox.settext(filelisting) # view text files in window (sometimes pdfs slip thru) elif self.ext != ".pdf" and Spiderutils.istextfile(self.path): # check if html files are sent to browser if self.ext == ".html" or self.ext == ".htm": browser = self.textbox.limits[4].get() if browser: webbrowser.open(self.path) return textmax = self.textbox.limits[2] tmax = int(textmax.get()) textsize = os.path.getsize(self.path) if textsize > tmax: basename = os.path.basename(self.path) txt = "%s is %d bytes.\nDisplay anyway?" % (basename, textsize) if not askyesno("Warning", txt): return try: fp = open(self.path, 'r') B = fp.readlines() except: pass fp.close() self.textbox.clear() for line in B: self.textbox.component('text').insert(END, str(line)) # binaries else: spidertype = Spiderutils.isSpiderBin(self.path) if spidertype != 0: #== "image": infotxt = self.getSpiderInfo(self.path) self.textbox.clear() self.textbox.component('text').insert(END, infotxt) if spidertype == "image": self.putImage(self.path, clear=0) elif self.ext.lower() in self.imagetypes: self.putImage(self.path)