Commit 3deb96ac authored by Martin Schorb's avatar Martin Schorb

support idoc for refmap more generically -> updated map_header

parent 002eb213
...@@ -37,9 +37,7 @@ nnf.write("%s\n" % navlines[1]) ...@@ -37,9 +37,7 @@ nnf.write("%s\n" % navlines[1])
navfile=flow_variables['navfile'] navfile=flow_variables['navfile']
os.chdir(os.path.dirname(navfile)) os.chdir(os.path.dirname(navfile))
targetfile = em.map_file(targetitem) targetheader = em.map_header(targetitem)
target_mrc = mrc.open(targetfile, permissive = 'True')
targetheader = em.map_header(target_mrc)
tx = list(map(float,targetitem['PtsX'])) tx = list(map(float,targetitem['PtsX']))
ty = list(map(float,targetitem['PtsY'])) ty = list(map(float,targetitem['PtsY']))
......
...@@ -43,6 +43,9 @@ import fnmatch ...@@ -43,6 +43,9 @@ import fnmatch
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
mrcext = ('.st','.mrc','.map','.rec','.ali','.preali')
# get python version # get python version
py_ver = sys.version_info py_ver = sys.version_info
...@@ -51,8 +54,13 @@ p1 = Popen("imodinfo", shell=True, stdout=PIPE) ...@@ -51,8 +54,13 @@ p1 = Popen("imodinfo", shell=True, stdout=PIPE)
o=list() o=list()
for line in p1.stdout: for line in p1.stdout:
o.append(line) o.append(line)
o1=str(o[0]).split(' ')
imod_ver = list(map(int,o1[o1.index('Version')+1].split('.'))) if o == []:
print('Warning! - No IMOD installation found')
imod_ver = [0,0,0]
else:
o1=str(o[0]).split(' ')
imod_ver = list(map(int,o1[o1.index('Version')+1].split('.')))
# define functions # define functions
...@@ -230,19 +238,28 @@ def findfile(searchstr, searchdir): ...@@ -230,19 +238,28 @@ def findfile(searchstr, searchdir):
# ------------------------------- # -------------------------------
#%% #%%
def map_header(m): def map_header(m):
# extracts MRC header information for a given mrc.object (legacy from reading mrc headers)
header={} header={}
header['xsize'] = numpy.int(m.header.nx) if (type(m) is mrc.mrcfile.MrcFile) | (type(m) is mrc.mrcmemmap.MrcMemmap):
header['ysize'] = numpy.int(m.header.ny) # extracts MRC header information for a given mrc.object (legacy from reading mrc headers)
header['stacksize'] = numpy.int(m.header.nz) header['xsize'] = numpy.int(m.header.nx)
header['ysize'] = numpy.int(m.header.ny)
# determine the scale
header['stacksize'] = numpy.int(m.header.nz)
header['pixelsize'] = m.voxel_size.x / 10000 # in um
# determine the scale
header['pixelsize'] = m.voxel_size.x / 10000 # in um
elif (type(m) is dict):
if ("MapFile" in m.keys()):
merge = mergemap(m,blendmont=False)
header = merge['mapheader']
elif(type(m) is str):
if os.path.exists(m):
if os.path.splitext(m)[1] in mrcext:
header = map_header(mrc.mmap(m, permissive = 'True'))
return header return header
# ------------------------------- # -------------------------------
...@@ -519,11 +536,11 @@ def mergemap(mapitem,crop=False,black=False,blendmont=True): ...@@ -519,11 +536,11 @@ def mergemap(mapitem,crop=False,black=False,blendmont=True):
tileidx_offset = 0 tileidx_offset = 0
mbase = os.path.splitext(os.path.basename(mapfile))[0] mbase = os.path.splitext(os.path.basename(mapfile))[0]
if mapfile.find('.st')<0 and mapfile.find('.map')<0 and mapfile.find('.mrc')<0: if os.path.splitext(mapfile)[1] not in mrcext:
#not an mrc file #not an mrc file
mergeheader = {} mergeheader = {}
mergeheader['stacksize'] = 1 # mergeheader['stacksize'] = 1
if '.idoc' in mapfile: if '.idoc' in mapfile:
# List of tif files with additional metadata # List of tif files with additional metadata
...@@ -551,28 +568,46 @@ def mergemap(mapitem,crop=False,black=False,blendmont=True): ...@@ -551,28 +568,46 @@ def mergemap(mapitem,crop=False,black=False,blendmont=True):
maphead0 = mdoc_item(idoctxt,[],header=True) maphead0 = mdoc_item(idoctxt,[],header=True)
im = list() im = list()
for i in range(0,numpy.min([montage_tiles,stacksize])):
tilefile = mapfile[:mapfile.find('.idoc')]+'{:04d}'.format(mapsection + i)+'.tif' if montage_tiles == 0:
im.append(tilefile) # stack of single images
tile = mdoc_item(idoctxt,'Image = '+os.path.basename(tilefile)) mergefile = mapfile[:mapfile.find('.idoc')]+'{:04d}'.format(mapsection)+'.tif'
tilepos.append(tile['StagePosition']) mergeheader['stacksize'] = 0
tilepx1.append(tile['PieceCoordinates']) tilepos = mapitem['StageXYZ'][0:2]
tilepx = '0'
if 'AlignedPieceCoordsVS' in tile: tilepx=numpy.array([[tilepx,tilepx,tilepx],[tilepx,tilepx,tilepx]])
m['Sloppy'] = True tilepx1 = tilepx
tilepx.append(tile['AlignedPieceCoordsVS']) m['Sloppy'] = 'NoMont'
else: im = io.imread(mergefile)
tilepx.append(tile['AlignedPieceCoords']) mergeheader['xsize'] = numpy.array(im.shape)[0]
mergeheader['ysize'] = numpy.array(im.shape)[1]
if mdoc_item(idoctxt,'MontSection = 0') == []: #older mdoc file format, created before SerialEM 3.7x mapheader = mergeheader.copy()
print('Warning - item'+mapitem['# Item']+': Series of tif images without montage information. Assume pixel size is consistent for all sections.') pixelsize = float(maphead0['PixelSpacing'][0])/ 10000
pixelsize = float(maphead0['PixelSpacing'][0])/ 10000 # in um
else: else:
mont_item = mdoc_item(idoctxt,'MontSection = '+str(mapsection)) if mdoc_item(idoctxt,'MontSection = 0') == []: #older mdoc file format, created before SerialEM 3.7x
pixelsize = float(mont_item['PixelSpacing'][0])/ 10000 # in um print('Warning - item'+mapitem['# Item']+': Series of tif images without montage information. Assume pixel size is consistent for all sections.')
pixelsize = float(maphead0['PixelSpacing'][0])/ 10000 # in um
else:
mont_item = mdoc_item(idoctxt,'MontSection = '+str(mapsection))
pixelsize = float(mont_item['PixelSpacing'][0])/ 10000 # in um
for i in range(0,numpy.min([montage_tiles,stacksize])):
tilefile = mapfile[:mapfile.find('.idoc')]+'{:04d}'.format(mapsection + i)+'.tif'
im.append(tilefile)
tile = mdoc_item(idoctxt,'Image = '+os.path.basename(tilefile))
tilepos.append(tile['StagePosition'])
tilepx1.append(tile['PieceCoordinates'])
if 'AlignedPieceCoordsVS' in tile:
m['Sloppy'] = True
tilepx.append(tile['AlignedPieceCoordsVS'])
else:
tilepx.append(tile['AlignedPieceCoords'])
mergeheader['pixelsize'] = pixelsize mergeheader['pixelsize'] = pixelsize
mapheader = mergeheader.copy() mapheader = mergeheader.copy()
mapheader['ysize'] = int(maphead0['ImageSize'][1]) mapheader['ysize'] = int(maphead0['ImageSize'][1])
...@@ -803,21 +838,21 @@ def mergemap(mapitem,crop=False,black=False,blendmont=True): ...@@ -803,21 +838,21 @@ def mergemap(mapitem,crop=False,black=False,blendmont=True):
tileloc = numpy.array([tpx / xstep,tpy/ystep]).T tileloc = numpy.array([tpx / xstep,tpy/ystep]).T
if not blendmont: # if not blendmont:
#prepare coordinate list for Big Stitcher # #prepare coordinate list for Big Stitcher
print('preparing coordinate list for BigStitcher for map item '+mapitem['# Item']+'.') # print('preparing coordinate list for BigStitcher for map item '+mapitem['# Item']+'.')
outpx = tilepx.copy() # outpx = tilepx.copy()
stitchname = mapfile+'.stitch.csv' # stitchname = mapfile+'.stitch.csv'
stitchfile = open(stitchname,'w') # stitchfile = open(stitchname,'w')
stitchfile.write('dim=2\n') # stitchfile.write('dim=2\n')
#stitchfile.write('ViewSetupID;TimePointID;(position_x, position_y, position_z)\n') # #stitchfile.write('ViewSetupID;TimePointID;(position_x, position_y, position_z)\n')
for j,item in enumerate(outpx): # for j,item in enumerate(outpx):
stitchfile.write(str(j)+";;"+"(%s, %s" % (int(item[0]),-int(item[1]))+")\n") # stitchfile.write(str(j)+";;"+"(%s, %s" % (int(item[0]),-int(item[1]))+")\n")
stitchfile.close() # stitchfile.close()
m['sections'] = numpy.array(list(map(int,tileloc[:,0]*m['frames'][1]+tileloc[:,1]))) m['sections'] = numpy.array(list(map(int,tileloc[:,0]*m['frames'][1]+tileloc[:,1])))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment