Exemplo n.º 1
0
def readAndFlattenImageStack(filename):

    #***********************************************
    #* read imageStack and return Nx(nX*nY) array **
    #***********************************************

    imageStack = EMData()
    imageStack.read_image(filename)
    nx, ny, numImages = imageStack.get_xsize(), imageStack.get_ysize(
    ), imageStack.get_zsize()
    imageStackData = EMNumPy.em2numpy(imageStack)
    imageStack = []
    #free memory

    #flatten each image and append to array of NxD, with N the number of images and D the size of the flat image
    imageStackData = np.reshape(imageStackData, (nx * ny, numImages),
                                order='F')
    imageStackData = np.transpose(imageStackData)
    print(imageStackData.shape)

    return imageStackData
Exemplo n.º 2
0
def main(args):
    star = parse_star(args.input, keep_index=False)

    if args.class_2d is not None:
        refs = glob.glob(args.class_2d)
    elif args.class_3d is not None:
        refs = glob.glob(args.class_3d)
    else:
        refs = []
    
    shifts = []
    for r in refs:
        if args.class_3d is not None:
            refmap = EMData(r)
            com = Vec3f(*refmap.phase_cog()[:3])
            shifts.append(com)
        else:
            stack = EMData.read_images(r)
            for im in stack:
                com = Vec2f(*im.phase_cog()[:2])
                shifts.append(com)

    if args.class_2d is None and args.class_3d is None:
        for ptcl in star.rows:
            im = EMData.read_image(ptcl)
            com = im.phase_cog()
            ptcl["rlnOriginX"] += com[0]
            ptcl["rlnOriginY"] += com[1]
    else:
        for ptcl in star.rows:
            com = shifts[ptcl["rlnClassNumber"]]
            xshift, yshift = transform_com(com, ptcl)
            ptcl["rlnOriginX"] += xshift
            ptcl["rlnOriginY"] += yshift

    if args.zero_origin:
        star["rlnCoordinateX"] = star["rlnCoordinateX"] - star["rlnOriginX"]
        star["rlnCoordinateY"] = star["rlnCoordinateY"] - star["rlnOriginY"]
        star["rlnOriginX"] = 0
        star["rlnOriginY"] = 0

    write_star(args.output, star, reindex=True)

    return 0
Exemplo n.º 3
0
	def __new__(cls,filename,application,force_plot=False,force_2d=False,old=None):
		
		file_type = Util.get_filename_ext(filename)
		em_file_type = EMUtil.get_image_ext_type(file_type)
		if not file_exists(filename): return None
		
		if force_plot and force_2d:
			# ok this sucks but it suffices for the time being
			print "Error, the force_plot and force_2d options are mutually exclusive"
			return None
		
		if force_plot:
			from emplot2d import EMPlot2DWidget
			if isinstance(old,EMPlot2DWidget): widget = old
			else: widget = EMPlot2DWidget(application=application)
			widget.set_data_from_file(filename)
			return widget
		
		if em_file_type != IMAGE_UNKNOWN or filename[:4] == "bdb:":
			n = EMUtil.get_image_count(filename)
			nx,ny,nz = gimme_image_dimensions3D(filename)
			if n > 1 and nz == 1: 
				if force_2d:
					a = EMData()
					data=a.read_images(filename)
				else:
					data = None # This is like a flag - the ImageMXWidget only needs the file name
			elif nz == 1:
				data = [EMData(filename,0)]
			else:
				data = EMData()
				data.read_image(filename,0,not force_2d)		# This should be 3-D. We read the header-only here
				data = [data]
				
			if data != None and len(data) == 1: data = data[0]
			
			if force_2d or isinstance(data,EMData) and data.get_zsize()==1:
				if isinstance(data,list) or data.get_ysize() != 1:
					from emimage2d import EMImage2DWidget
					if isinstance(old,EMImage2DWidget): widget = old
					else: widget= EMImage2DWidget(application=application)
				else:
					from emplot2d import EMPlot2DWidget
					if isinstance(old,EMPlot2DWidget): widget = old
					else: widget = EMPlot2DWidget(application=application)
					widget.set_data_from_file(filename)
					return widget
			elif isinstance(data,EMData):
				if isinstance(old,EMScene3D): widget = old
				else: widget = EMScene3D()
#				print n,data
				for ii in xrange(n):
					data=EMData(filename,ii)
					datai = EMDataItem3D(data, transform=Transform())
					widget.insertNewNode(os.path.basename(filename), datai, parentnode=widget)
					isosurface = EMIsosurface(datai, transform=Transform())
					widget.insertNewNode("Iso", isosurface, parentnode=datai)
				return widget
				
			elif data == None or isinstance(data,list):
				from emimagemx import EMImageMXWidget
				if isinstance(old,EMImageMXWidget): widget = old
				else: widget = EMImageMXWidget(application=application)
				data = filename
			else: 
				print filename
				raise # weirdness, this should never happen
			widget.set_data(data,filename)
			return widget
		else:
			from emplot2d import EMPlot2DWidget
			if isinstance(old,EMPlot2DWidget): widget = old
			else: widget = EMPlot2DWidget(application=application)
			widget.set_data_from_file(filename)
			return widget
Exemplo n.º 4
0
	def __new__(cls,filename,application,force_plot=False,force_2d=False,old=None):
		
		file_type = Util.get_filename_ext(filename)
		em_file_type = EMUtil.get_image_ext_type(file_type)
		if not file_exists(filename): return None
		
		if force_plot and force_2d:
			# ok this sucks but it suffices for the time being
			print("Error, the force_plot and force_2d options are mutually exclusive")
			return None
		
		if force_plot:
			from .emplot2d import EMPlot2DWidget
			if isinstance(old,EMPlot2DWidget): widget = old
			else: widget = EMPlot2DWidget(application=application)
			widget.set_data_from_file(filename)
			return widget
		
		if em_file_type != IMAGE_UNKNOWN or filename[:4] == "bdb:":
			n = EMUtil.get_image_count(filename)
			nx,ny,nz = gimme_image_dimensions3D(filename)
			if n > 1 and nz == 1: 
				if force_2d:
					a = EMData()
					data=a.read_images(filename)
				else:
					data = None # This is like a flag - the ImageMXWidget only needs the file name
			elif nz == 1:
				data = [EMData(filename,0)]
			else:
				data = EMData()
				data.read_image(filename,0,not force_2d)		# This should be 3-D. We read the header-only here
				data = [data]
				
			if data != None and len(data) == 1: data = data[0]
			
			if force_2d or isinstance(data,EMData) and data.get_zsize()==1:
				if isinstance(data,list) or data.get_ysize() != 1:
					from .emimage2d import EMImage2DWidget
					if isinstance(old,EMImage2DWidget): widget = old
					else: widget= EMImage2DWidget(application=application)
				else:
					from .emplot2d import EMPlot2DWidget
					if isinstance(old,EMPlot2DWidget): widget = old
					else: widget = EMPlot2DWidget(application=application)
					widget.set_data_from_file(filename)
					return widget
			elif isinstance(data,EMData):
				if isinstance(old,EMScene3D): widget = old
				else: widget = EMScene3D()
#				print n,data
				for ii in range(n):
					data=EMData(filename,ii)
					datai = EMDataItem3D(data, transform=Transform())
					widget.insertNewNode(os.path.basename(filename), datai, parentnode=widget)
					isosurface = EMIsosurface(datai, transform=Transform())
					widget.insertNewNode("Iso", isosurface, parentnode=datai)
				return widget
				
			elif data == None or isinstance(data,list):
				from .emimagemx import EMImageMXWidget
				if isinstance(old,EMImageMXWidget): widget = old
				else: widget = EMImageMXWidget(application=application)
				data = filename
			else: 
				print(filename)
				raise # weirdness, this should never happen
			widget.set_data(data,filename)
			return widget
		else:
			from .emplot2d import EMPlot2DWidget
			if isinstance(old,EMPlot2DWidget): widget = old
			else: widget = EMPlot2DWidget(application=application)
			widget.set_data_from_file(filename)
			return widget
Exemplo n.º 5
0
	def get_attr_dict(self):
		a = EMData()
		a.read_image(self.file_name,self.idx,True)
		return a.get_attr_dict()
Exemplo n.º 6
0
	def write_image(self,*args):
		a = EMData()
		a.read_image(self.file_name,self.idx)
		a.write_image(*args)
Exemplo n.º 7
0
 def get_attr_dict(self):
     a = EMData()
     a.read_image(self.file_name, self.idx, True)
     return a.get_attr_dict()
Exemplo n.º 8
0
 def write_image(self, *args):
     a = EMData()
     a.read_image(self.file_name, self.idx)
     a.write_image(*args)