Package: pyserialem
PySerialEMA small Python library to read and writeSerialEMnavigator files (.nav), and process grid maps.Install usingpip install pyserialem.UsageThe main use ofpyserialemis to manipulate coordinates in a.navfile written by SerialEM (specification).Reading a.navfile:importpyserialemfrompathlibimportPathp=Path('C:/path/to/data/')/'nav.nav'items=pyserialem.read_nav_file(p)# listYou can set theacquire_onlytoggle to return only the items with theAcquiretag set:items=pyserialem.read_nav_file(p,acquire_only=True)# listThis returns alistofMapItemandNavItem. AMapItemis associated with an image in the corresponding.mrcfile, and aNavItemis a marker or point on that image.map_items=[item.kind=='Map'foriteminitems]nav_items=[item.kind=='Marker'foriteminitems]All of the tags associated with theMapItemorNavItemcan be accessed as an attribute using the same name as in the.navfile, i.e. with the key definedhere. This is also how the values should be updated:nav_item=nav_items[0]stage_position=nav_item.StageXYZ# tuplemap_item.StageXYZ=(100,200,0)# overwrite valuesAlternatively, the stage position can be accessed directly through:x=map_item.stage_xy=map_item.stage_yz=map_item.stage_zxy=map_item.stage_xyMap itemsAMapItemhas all the functions of aNavItem, and then some. EachMapItemcan have a list of markers associated with it:map_item=map_items[0]markers=map_item.markers# listTo visualize them, call:map_item.plot()To just load the image associated with theMapItem:img=map_item.load()# np.arrayThey can be extracted as a dictionary:d=map_item.to_dict()# dict...and restored:new_map_item=pysem.from_dict(d,tag='new_mapitem')This is also the easiest way of constructing a newMapItem, because some keys can be autogenerated. Otherwise, all the required keys have to be specified to theMapItemconstructor. Thetagspecifies the name of the item when displayed inSerialEM. If omitted, one will be generated.It is easy to add new markers to aMapItem. As a pixel coordinate (i.e. from segmentation) is the default.PySerialEMcalculates the corresponding stage position. Theacquireargument sets the acquire flag (default=True):pixel_position=(0,0)new_nav_item=map_item.add_marker(pixel_position,tag='pixel_item',acquire=True)# NavItemYou can also add a marker as a stage coordinate (although this is a bit more tricky to calculate the corresponding pixel coordinate):stage_positionion=(1000,1000)new_nav_item=map_item.add_marker(pixel_position,kind='stage',tag='stage_item',acquire=False)# NavItemTo add many markers:pixel_coordinates=((0,0),(100,100),(200,200))nav_item_group=map_item.add_marker_group(pixel_coordinates)# tupleSpecifyreplace=Trueto replace the current list of markers associated with theMapItem.If theMapItemhas a set of markers associated with itmap_item.markers, the coordinates be retrieved as followed:map_item.markers_as_pixel_coordinates()# np.array (Nx2)map_item.markers_as_stage_coordinates()# np.array (Nx2)To just convert between stage and pixel coordinates:pixel_coord=(1024,1024)stage_coord=map_item.pixel_to_stagecoords(pixel_coord)# tuplenew_pixel_coord=map_item.stage_to_pixelcoords(stagecoord)# tupleassertnew_pixel_coord==pixel_coordTo write a new file:pyserialem.write_nav_file('out.nav',map_item,*nav_item_group)Note the*. This function captures arguments in a list (*args, so they must be unpacked when supplied.StitchingA basic stitching algorithm is available to get an overview of the location of all map items:map_items=[itemforiteminitemsifitem.kind=='Map']pyserialem.stitch_map_items(map_items)For more advanced stitching and montaging, use thepyserialem.montagemodule. Ademo notebookis available to demonstrate its usage.Mdoc filesThere is also a simple function to read.mdocfiles (link). This returns a list of python objects where each key can be accessed as an attribute.p=Path('C:/path/to/data')/'gm.mrc.mdoc'mdoc=pyserialem.read_mdoc_file(p)