def __getitem__(self,key): self.realopen(self.rohint) try: r=loads(self.bdb.get(dumps(key,-1),txn=self.txn)) except: return None if isinstance(r,dict) and r.has_key("is_complex_x") : pkey="%s/%s_"%(self.path,self.name) fkey="%dx%dx%d"%(r["nx"],r["ny"],r["nz"]) # print "r",fkey ret=EMData(r["nx"],r["ny"],r["nz"]) if r.has_key("data_path"): p,l=r["data_path"].split("*") # print "read ",os.getcwd(),self.path,p,l if p[0]=='/' : ret.read_data(p,int(l)) else : ret.read_data(self.path+"/"+p,int(l)) else: try: n=loads(self.bdb.get(fkey+dumps(key,-1))) # this is the index for this binary data item in the image-dimensions-specific binary data file except: raise KeyError,"Undefined data location key for : %s"%key ret.read_data(pkey+fkey,n*4*r["nx"]*r["ny"]*r["nz"]) k=set(r.keys()) k-=DBDict.fixedkeys for i in k: if i not in ('nx', 'ny', 'nz'): ret.set_attr(i,r[i]) ret["source_path"]=pkey[:-1] ret["source_n"]=key return ret return r
def emdata_from_jsondict(dct): """This returns a new EMData object reconstituted from a JSON file""" fixedkeys=frozenset(("nx","ny","nz","minimum","maximum","mean","sigma","square_sum","mean_nonzero","sigma_nonzero","__class__","~bindata~")) ret=EMData(dct["nx"],dct["ny"],dct["nz"]) ret.set_data_string(zlib.decompress(base64.decodestring(dct["~bindata~"]))) for k in fixedkeys: try: del dct[k] except: pass for k in dct.keys(): ret[k]=dct[k] ret.update() return ret
def json_to_obj(jsdata): """converts a javascript object representation back to the original python object""" if jsdata.has_key("__pickle__") : try: return cPickle.loads(str(jsdata["__pickle__"])) except: return str(jsdata["__pickle__"]) # This shouldn't happen. Means a module hasn't been loaded. This is an emergency stopgap to avoid crashing elif jsdata.has_key("__image__") : # images now stored in a separate HDF file try: ret= EMData(str(jsdata["__image__"][0]),int(jsdata["__image__"][1])) except: print "Error reading image from JSON: ",jsdata["__image__"] ret= None return ret elif jsdata.has_key("__class__") : return jsonclasses[jsdata["__class__"]](jsdata) else: return jsdata
def db_read_images(fsp,*parms): """EMData.read_images(filespec,[image # list],[header only]) This function can be used to read a set of images from a file or bdb: database. Pass in the filename, or bdb: specification, optionally a list of image numbers to read (or None), and a flag indicating that only the image headers should be read in. If only the headers are read, accesses to the image data in the resulting EMData objects will be invalid""" if fsp[:4].lower()=="bdb:" : db,keys=db_open_dict(fsp,True,True) if len(parms)>1: nodata=parms[1] else: nodata=0 if keys: if len(parms)>0 : if not parms[0] or len(parms[0])==0 : parms[0]=keys return [db.get(keys[i]) for i in parms[0]] return [db.get(i,nodata=nodata) for i in keys] else : if len(parms)==0 : keys=range(0,len(db)) else : keys=parms[0] if not keys or len(keys)==0 : keys=range(len(db)) return [db.get(i,nodata=nodata) for i in keys] return EMData.read_images_c(fsp,*parms)
def image(self): return EMData(self.fsp, self.n)
def get(self,key,dfl=None,txn=None,target=None,nodata=0,region=None,idx=0): """Alternate method for retrieving records. Permits specification of an EMData 'target' object in which to place the read object""" self.realopen(self.rohint) try: r=loads(self.bdb.get(dumps(key,-1),txn=txn)) except: return dfl if isinstance(r,dict) and r.has_key("is_complex_x") : pkey="%s/%s_"%(self.path,self.name) rnx,rny,rnz = r["nx"],r["ny"],r["nz"] fkey="%dx%dx%d"%(rnx,rny,rnz) # print "r",fkey if region != None: size = region.get_size() # zeros are annoyingly necessary for i in range(len(size)): if size[i] == 0: size[i] = 1 nx,ny,nz = int(size[0]),int(size[1]),int(size[2]) else: nx,ny,nz = rnx,rny,rnz if target : ret = target else: ret = EMData() # metadata k=set(r.keys()) # k-=DBDict.fixedkeys for i in k: if i not in ('nx', 'ny', 'nz'): ret.set_attr(i,r[i]) ret["source_path"]=pkey[:-1] ret["source_n"]=key # binary data ret.set_size(nx,ny,nz,nodata) if not nodata: if region != None: ret.to_zero() # this has to occur in situations where the clip region goes outside the image if r.has_key("data_path"): p,l=r["data_path"].split("*") if p[0]=='/' or p[0]=='\\' or p[1]==':': ret.read_data(p,int(l),region,rnx,rny,rnz) # absolute path else : ret.read_data(self.path+"/"+p,int(l),region,rnx,rny,rnz) # relative path else: try: n=loads(self.bdb.get(fkey+dumps(key,-1))) # this is the index for this binary data item in the image-dimensions-specific binary data file except: raise KeyError,"Undefined data location key %s for %s"%(key,pkey+fkey) try: ret.read_data(pkey+fkey,n*4*rnx*rny*rnz,region,rnx,rny,rnz) # note that this uses n, NOT 'key'. Images cannot be located in the binary file based on their numerical key except : import socket print "Data read error (%s) on %s (%d)"%(socket.gethostname(),pkey+fkey,key*4*rnx*rny*rnz) traceback.print_exc() sys.stderr.flush() sys.stdout.flush() os._exit(1) return ret return r