Ejemplo n.º 1
0
	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
Ejemplo n.º 2
0
	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