How to mask 2D particles using EMAN2 python?

53 views
Skip to first unread message

Steve Chou

unread,
Sep 1, 2021, 9:11:27 PM9/1/21
to em...@googlegroups.com
Dear Prof. Steve Ludtke and all others,

I'm trying to mask particles manually using EMAN2 python, but I cannot find a reference online, so I'm asking for help here.

from EMAN2 import *
particle_stack = EMData("particle_stack.hdf")
for count,value in enumerate (particle_stack):
     # dimension: 128 pixels x 128 pixels; I want to create a soft mask with a radius of 60 pixels (diameter: 120 pixels)
   
     particle.write_image("particle_stack_masked.hdf", count)

Many thanks in advance,
Steven

--
Steven Chou


Ludtke, Steven J.

unread,
Sep 1, 2021, 10:14:58 PM9/1/21
to em...@googlegroups.com
Hi Steven,
You're on the right track. 

An EMData object is a representation of a single image/volume, not a set of images. While you could read a set of 2-D images into a single EMData object by literally "stacking" them in 3-D, that isn't a very productive way of working. To process a stack, you can either:
- read all of the images in at once as a list of EMData objects (assuming not too much RAM is involved, this is much more efficient)
or 
- you can find the number of images in the stack, and read/process them one at a time.

To generate the mask, assuming you want to make it programmatically rather than reading it from a file, you would generate an empty volume, then use a Processor to modify it in an appropriate fashion.

Writing images, again, could be handled one at a time, or all at once (again, this is much faster, but requires all of the images to be in RAM)

Example 1:
from EMAN2 import *

particle_stack=EMData.read_images("particle_stack.hdf") # reads the entire stack as a list of EMData objects
mask=EMData(particle_stack[0]["nx"],particle_stack[0]["ny"],1) # creates an empty image the same size
mask.to_one() # all pixels to 1.0
mask.process_inplace("mask.soft",{"outer_radius":60,"width":3}) # applies a soft mask to the 1.0 image, 3 is the Gaussian 1/2 width of the soft edge in pixels
for image in particle_stack:
image.mult(mask) # in place pixel-wise multiplication
EMData.write_images("output.hdf",particle_stack) # write the entire stack at once
# or, now preferred
EMData.write_compresed(particle_stack,"output.hdf",0,bits=10,erase=True) # write the entire stack at once with bit + lossless compression. erase the file first if it exists

Example 2:

from EMAN2 import *

n=EMUtil.get_image_count("particle_stack.hdf")
mask=EMData(particle_stack[0]["nx"],particle_stack[0]["ny"],1) # creates an empty image the same size
mask.to_one() # all pixels to 1.0
mask.process_inplace("mask.soft",{"outer_radius":60,"width":3}) # applies a soft mask to the 1.0 image, 3 is the Gaussian 1/2 width of the soft edge in pixels
for i in range(n):
image=EMData("particle_stack.hdf",i)
image.mult(mask) # in place pixel-wise multiplication
image.write_image("output.hdf",i)
# or
image.write_compressed("output.hdf",i,bits=10,erase=True) # note only erases when i=0


You can get a list of available processors and parameters with:
e2help.py processors

or a specific type:
e2help.py processors mask

or details:
e2help.py processors mask.soft -v 2

Within interactive python:
from EMAN2 import *
help(EMData)
help(EMUtil.get_image_count)
etc.

--------------------------------------------------------------------------------------
Steven Ludtke, Ph.D. <slu...@bcm.edu>                      Baylor College of Medicine 
Charles C. Bell Jr., Professor of Structural Biology
Dept. of Biochemistry and Molecular Biology                      (www.bcm.edu/biochem)
Academic Director, CryoEM Core                                        (cryoem.bcm.edu)
Co-Director CIBR Center                                    (www.bcm.edu/research/cibr)




--
--
----------------------------------------------------------------------------------------------
You received this message because you are subscribed to the Google
Groups "EMAN2" group.
To post to this group, send email to em...@googlegroups.com
To unsubscribe from this group, send email to eman2+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/eman2

---
You received this message because you are subscribed to the Google Groups "EMAN2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to eman2+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/eman2/CAA7-AsbNY00rZsZhye-e3P-wQ18wqyjYpa%2BJzjWfWQaONKN9OQ%40mail.gmail.com.

Ludtke, Steven J.

unread,
Sep 1, 2021, 10:20:54 PM9/1/21
to em...@googlegroups.com
Hmm... It looks like the tabs in my examples (for the loops) somehow got stripped out of the outgoing message and the comments got moved to a new line. Very odd... Let's try those again:

Example 1:
from EMAN2 import *

particle_stack=EMData.read_images("particle_stack.hdf")          # reads the entire stack as a list of EMData objects
mask=EMData(particle_stack[0]["nx"],particle_stack[0]["ny"],1)   # creates an empty image the same size
mask.to_one()      # all pixels to 1.0
mask.process_inplace("mask.soft",{"outer_radius":60,"width":3})  # applies a soft mask to the 1.0 image, 3 is the Gaussian 1/2 width of the soft edge in pixels
for image in particle_stack:
  image.mult(mask)            # in place pixel-wise multiplication

EMData.write_images("output.hdf",particle_stack)     # write the entire stack at once
# or (recommended)
EMData.write_compresed(particle_stack,"output.hdf",0,bits=10,erase=True)   # write the entire stack at once with bit + lossless cmpr. erase the file first if it exists


Example 2:

from EMAN2 import *

n=EMUtil.get_image_count("particle_stack.hdf")
mask=EMData(particle_stack[0]["nx"],particle_stack[0]["ny"],1)   # creates an empty image the same size
mask.to_one()   # all pixels to 1.0
mask.process_inplace("mask.soft",{"outer_radius":60,"width":3})  # applies a soft mask to the 1.0 image, 3 is the Gaussian 1/2 width of the soft edge in pixels
for i in range(n):
  image=EMData("particle_stack.hdf",i)
  image.mult(mask)                      # in place pixel-wise multiplication
  image.write_image("output.hdf",i)
  # or
  image.write_compressed("output.hdf",i,bits=10,erase=True)      # note only erases when i=0



--------------------------------------------------------------------------------------
Steven Ludtke, Ph.D. <slu...@bcm.edu>                      Baylor College of Medicine 
Charles C. Bell Jr., Professor of Structural Biology
Dept. of Biochemistry and Molecular Biology                      (www.bcm.edu/biochem)
Academic Director, CryoEM Core                                        (cryoem.bcm.edu)
Co-Director CIBR Center                                    (www.bcm.edu/research/cibr)



Reply all
Reply to author
Forward
0 new messages