Esempio n. 1
0
    def setval(self, key, val, deferupdate=False):
        '''Alternative to assignment operator. Permits deferred writing to improve performance.'''
        # ok, decided to permit non-string keys to pass through and get converted to strings
        #		if not isinstance(key,str) : raise Exception,"JSONDB keys must be strings"
        key = str(key)
        if key in self.delkeys: self.delkeys.remove(key)
        # for EMData objects we need to figure out what file they will get stored in
        if isinstance(val, EMData):
            # Changing an image triggers an actual read of the old image
            try:
                if isinstance(self.data[key], tmpimg):
                    self.data[key] = self.data[key].image()
            except:
                pass
            try:
                val["json_path"] = self.changes[key]["json_path"]
                val["json_n"] = self.changes[key]["json_n"]
            except:
                try:
                    val["json_path"] = self.data[key]["json_path"]
                    val["json_n"] = self.data[key]["json_n"]
                except:
                    val["json_path"] = self.path.replace(
                        ".json", "_jsonimg.hdf")
                    try:
                        val["json_n"] = EMUtil.get_image_count(
                            val["json_path"])
                    except:
                        val["json_n"] = 0


#			print key,val["json_path"],val["json_n"]

        self.changes[key] = val
        if not deferupdate: self.sync()
Esempio n. 2
0
	def setval(self,key,val,deferupdate=False):
		'''Alternative to assignment operator. Permits deferred writing to improve performance.'''
		# ok, decided to permit non-string keys to pass through and get converted to strings
#		if not isinstance(key,str) : raise Exception,"JSONDB keys must be strings"
		key=str(key)
		if key in self.delkeys : self.delkeys.remove(key)
		# for EMData objects we need to figure out what file they will get stored in
		if isinstance(val,EMData) :
			# Changing an image triggers an actual read of the old image
			try:
				if isinstance(self.data[key],tmpimg):
					self.data[key]=self.data[key].image()
			except: pass
			try: 
				val["json_path"]=self.changes[key]["json_path"]
				val["json_n"]=self.changes[key]["json_n"]
			except: 
				try: 
					val["json_path"]=self.data[key]["json_path"]
					val["json_n"]=self.data[key]["json_n"]
				except: 
					val["json_path"] = self.path.replace(".json","_jsonimg.hdf")
					try: 
						val["json_n"] = EMUtil.get_image_count(val["json_path"])
					except: val["json_n"] = 0
#			print key,val["json_path"],val["json_n"]

		self.changes[key]=val
		if not deferupdate : self.sync()
Esempio n. 3
0
def db_get_image_info(fsp):
	"""get_image_info(path)

Takes a bdb: specifier and returns the number of images and image dimensions."""
	if fsp[:4].lower()=="bdb:" :
		path,dictname,keys=db_parse_path(fsp)
		if keys==None :
			# This dictionary contains image counts for the others in this directory
			db2=db_open_dict("bdb:%s#%s"%(path,"00image_counts"))

			# If this is true, we need to update the dictionary
			if (db2.has_key(dictname) and os.path.getmtime("%s/EMAN2DB/%s.bdb"%(path,dictname))>db2[dictname][0]) or not db2.has_key(dictname) or db2[dictname][2][0]==0:
#				print "update ",dictname,os.path.getmtime("%s/EMAN2DB/%s.bdb"%(path,dictname)),db2[dictname][0],db2.has_key(dictname)
				db=db_open_dict(fsp,True)
				try:
					im=db[0]
					sz=(im["nx"],im["ny"],im["nz"])
				except :
					sz=(0,0,0)
				db2[dictname]=(time.time(),len(db),sz)

			return db2[dictname][1:]

		else :			# if the user specifies the key in fsp, we ignore parms
			db=db_open_dict(fsp,True)
			n=0
			for i in keys:
				if i in db : n+=1
			sz=db[keys[0]]
			sz=(sz["nx"],sz["ny"],sz["nz"])
			return (n,sz)
	img=EMData(fsp,0,True)
	return (EMUtil.get_image_count_c(fsp),(img["nx"],img["ny"],img["nz"]))
Esempio n. 4
0
def db_get_image_count(fsp):
	"""get_image_count(path)

Takes a path or bdb: specifier and returns the number of images in the referenced stack."""
	if fsp[:4].lower()=="bdb:" :
		path,dictname,keys=db_parse_path(fsp)
		if keys==None :
			# This dictionary contains image counts for the others in this directory
			db2=db_open_dict("bdb:%s#%s"%(path,"00image_counts"))

			# If this is true, we need to update the dictionary
			if (db2.has_key(dictname) and os.path.getmtime("%s/EMAN2DB/%s.bdb"%(path,dictname))>db2[dictname][0]) or not db2.has_key(dictname) :
				db=db_open_dict(fsp,True)
				try:
					im=db[0]
					sz=(im["nx"],im["ny"],im["nz"])
				except : sz=(0,0,0)
				db2[dictname]=(time.time(),len(db),sz)

			return db2[dictname][1]

		else :			# if the user specifies the key in fsp, we ignore parms
			db=db_open_dict(fsp,True)
			n=0
			for i in keys:
				if i in db : n+=1
			return n

	return EMUtil.get_image_count_c(fsp)
Esempio n. 5
0
def db_get_all_attributes(fsp,*parms):
	if fsp[:4].lower()=="bdb:" :
		db=db_open_dict(fsp,True)
		attr_name = parms[-1]
		if "?" in fsp:
			keys=fsp[fsp.rfind("?")+1:].split(",")
			for i in range(len(keys)):
				try: keys[i]=int(keys[i])
				except: pass
			return [db.get_attr(i, attr_name) for i in keys]
		else :
			if len(parms)==1 : keys=range(0,len(db))
			else : keys=parms[0]
		return [db.get_attr(i, attr_name) for i in keys]
	return EMUtil.get_all_attributes_c(fsp,*parms)