Skip to content
Adrian Quintana edited this page Dec 11, 2017 · 1 revision

EMX python module

Introduction

Emx Python module provides basic support for reading and writing EMX files using Python. At present this module contains two main python classes: micrograph and particle that store all the information related with micrographs and particles respectively. There is a third class called EmxData that groups EMX objects, that is, stores a list of particles and another list of micrographs. Reading and writing emx files is done from/to this class. A full description of the EMX format is available here and a dictionary can be accessed here

Examples

The basic use of this module is best illustrated with examples.

Validate an emx file against a schema

At the moment schema validation is made through a subprocess call to the program xmllint. This could use a Python-based library at some point in the future, if pure python validator could be found

from emx.emxmapper import * xmlFile 'EMXread.emx' (code,_out,_err)validateSchema(xmlFile) if code==0: print "%s is validated" else: print "Error:", _err

#### Micrograph Class

Create a micrograph with_fileName='mic001.mrc'_ , set some of the micrograph properties and print the resulting micrograph

from emx import * #create micrograph m1 =[EmxMicrograph] m1.set('acceleratingVoltage',100) m1.set('defocusU',1000.) m1.set('pixelSpacing__X',5.6) m1.set('pixelSpacing__Y',5.7) #print micrograph print m1

Particle Class

Create a particle with_fileName='part.mrc'_ and_index=1_ , set some of the particle properties, assign to the particle the micrograph from which it has been extracted and print the resulting particle

from emx import * #create micrograph m1 =[EmxMicrograph] m1.set('acceleratingVoltage',100) m1.set('defocusU',1000.) m1.set('pixelSpacing__X',5.6) m1.set('pixelSpacing__Y',5.7) #create particle p1 =[EmxParticle] p1.set('defocusU',1000.10) p1.set('pixelSpacing__X',5.66) p1.set('pixelSpacing__Y',5.77) #assign micrograph to particle p1.setMicrograph(m1) #print particle print p1

emxData Class

Store a set of micrographs and particles in a emxData object (it is used for writting/reading to/from emx files)

from emx import * emxData=EmxData()

#add 3 micrographs for i in range (1,4): m1 =[EmxMicrograph] m1.set('acceleratingVoltage',100) m1.set('defocusU',1000.) m1.set('pixelSpacing__X',5.6) m1.set('pixelSpacing__Y',5.7) emxData.addObject(m1) #add 2 particles for i in range (1,3): p1 =EmxParticle('part.stk', i)

emxData.addObject(p1)

#print all emx objects for _object in emxData: print _object #print only the name and acelerating voltage of the micrographsmicrographs for _object in emxData.iterClasses(MICROGRAPH): print _object.get('fileName'), _object.get('acceleratingVoltage')

#### Read an emx file

Read an EMX file and print its contents

from emx import * from emx.emxmapper import * #input file name fileName 'EMXread.emx' #Class to group EMX objects emxData[EmxData] #mapper class that relates the micrograph/particle properties to the xml file xmlMapper =[XmlMapper] xmlMapper.readEMXFile(fileName) #iterate through all the micrographs/particles and print them for _object in emxData: print _object

example file available/resources/test/EMX/EMXread.emx

Write an emx file

Create some micrographs, assign then to emxData and save them to a file

from emx import * from emx.emxmapper import * fileName 'EMXwrite.emx' #Class to group EMX objects emxData[EmxData] #create some data and assign it to emxData #create several micrograph xmlMapper =[XmlMapper] for i in range (1,10): m1 =[EmxMicrograph] m1.set('acceleratingVoltage',100) m1.set('defocusU',1000.) m1.set('pixelSpacing__X',5.6) m1.set('pixelSpacing__Y',5.7) emxData.addObject(m1) xmlMapper.writeEMXFile(fileName)

#### More examples

Xmipp package uses this module for importing and exporting EMX files. Please check the code of the programs/applications/scripts/import_emx/batch_import_emx.py and/applications/scripts/export_emx/batch_export_emx.py for further examples. Both use the xmipp python module emxLib.py

Clone this wiki locally