-
Notifications
You must be signed in to change notification settings - Fork 2
/
assembly_EM.py
136 lines (115 loc) · 5.95 KB
/
assembly_EM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#this script puts the acquired EM tiles into Trakem at the right positions
from __future__ import with_statement
import os, re, errno, string, shutil, time
from java.awt.event import MouseAdapter, KeyEvent, KeyAdapter
from jarray import zeros, array
from java.util import HashSet, ArrayList
from java.awt.geom import AffineTransform
from java.awt import Color
import ij
from ij import IJ, Macro
from fiji.tool import AbstractTool
from ini.trakem2 import Project, ControlWindow
from ini.trakem2.display import Patch, Display
from ini.trakem2.utils import Utils
from mpicbg.trakem2.align import Align, AlignTask
import sys
sys.path.append(IJ.getDirectory('plugins'))
import fijiCommon as fc
import java
from java.lang import System
System.setProperty('javax.xml.parsers.SAXParserFactory', 'com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl')
def xyToN(x, y, nX):
return y * nX + (nX - x - 1)
namePlugin = 'assembly_EM'
MagCFolder = fc.startPlugin(namePlugin)
MagCParameters = fc.readMagCParameters(MagCFolder)
ControlWindow.setGUIEnabled(False)
MagC_EM_Folder = os.path.join(MagCFolder, 'MagC_EM','')
# read metadata
EMMetadataPath = fc.findFilesFromTags(MagCFolder,['EM_Metadata'])[0]
EMMetadata = fc.readParameters(EMMetadataPath)
numTilesX = EMMetadata['numTilesX']
# reading transforms from the low-resolution montage
IJ.log('reading the affine transform parameters from the affine montage')
transformsPath = os.path.join(MagC_EM_Folder, 'assembly_lowEM_Transforms.txt')
with (open(transformsPath, 'r')) as f:
transforms = f.readlines()
nLayers = max([int(transforms[i]) for i in range(1, len(transforms),8) ]) + 1 # why not, could simply read the EM_Metadata parameters ...
# getting downsamplingFactor
downsamplingFactor = MagCParameters['downsample_EM']['downsamplingFactor']
factorString = str(int(1000000*downsamplingFactor)).zfill(8)
# create the normal resolution project
IJ.log('Creating project with the full size EM images')
projectPath = fc.cleanLinuxPath(os.path.join(MagC_EM_Folder,'EMProject.xml'))
projectFolder = fc.cleanLinuxPath(os.path.dirname(projectPath))
project, loader, layerset, _ = fc.getProjectUtils(fc.initTrakem(projectFolder, 1))
project.saveAs(projectPath, True)
if 'g0000' in os.listdir(os.path.join(MagCFolder, 'EMData')):
sbemimage = True
else:
sbemimage = False
# Assembling all images in the project with the transforms computed on the low res and adjusted with the scale factor
IJ.log('Assembling all images in the project with the transforms computed on the low res and adjusted with the scale factor')
paths = []
locations = []
layers = []
for i in range(0, len(transforms), 8):
alignedPatchPath = os.path.normpath(transforms[i])
alignedPatchName = os.path.basename(alignedPatchPath)
if sbemimage:
x, y = alignedPatchName.split('Tile_')[1].split('_resized')[0].split('-')
x, y = int(x), int(y)
nSection = int(os.path.dirname(alignedPatchPath).split('section_')[1])
tileNumber = xyToN(x, y, numTilesX)
tileFolder = os.path.join(MagCFolder, 'EMData', 'g' + str(nSection).zfill(4), 't' + str(tileNumber).zfill(4))
tileName = os.listdir(tileFolder)[0]
toAlignPatchPath = os.path.join(tileFolder, tileName)
else:
toAlignPatchName = alignedPatchName.replace('_' + factorString, '').replace('_resized', '')
toAlignPatchPath = os.path.join(MagCFolder, 'EMData', os.path.basename(os.path.dirname(alignedPatchPath)), toAlignPatchName)
toAlignPatchPath = fc.cleanLinuxPath(toAlignPatchPath[:-1]) # mysterious trailing character ...
toAlignPatchPath = fc.cleanLinuxPath(toAlignPatchPath)
IJ.log('toAlignPatchPath ' + toAlignPatchPath)
l = int(transforms[i+1])
paths.append(toAlignPatchPath)
locations.append([0,0])
layers.append(l)
importFilePath = fc.createImportFile(MagC_EM_Folder, paths, locations, layers = layers, name = namePlugin)
# insert the tiles in the project
IJ.log('I am going to insert many files at factor ' + str(downsamplingFactor) + ' ...')
task = loader.importImages(layerset.getLayers().get(0), importFilePath, '\t', 1, 1, False, 1, 0)
task.join()
# apply the transforms
for i in range(0, len(transforms), 8):
alignedPatchPath = transforms[i]
alignedPatchName = os.path.basename(alignedPatchPath)
if sbemimage:
x, y = alignedPatchName.split('Tile_')[1].split('_resized')[0].split('-')
x, y = int(x), int(y)
nSection = int(os.path.dirname(alignedPatchPath).split('section_')[1])
tileNumber = xyToN(x, y, numTilesX)
tileFolder = os.path.join(MagCFolder, 'EMData', 'g' + str(nSection).zfill(4), 't' + str(tileNumber).zfill(4))
tileName = os.listdir(tileFolder)[0]
toAlignPatchPath = os.path.join(tileFolder, tileName)
else:
toAlignPatchName = alignedPatchName.replace('_' + factorString, '').replace('_resized', '')
toAlignPatchPath = os.path.join(MagCFolder, 'EMData', os.path.basename(os.path.dirname(alignedPatchPath)), toAlignPatchName)
toAlignPatchPath = fc.cleanLinuxPath(toAlignPatchPath[:-1]) # mysterious trailing character ...
toAlignPatchPath = fc.cleanLinuxPath(os.path.normpath(toAlignPatchPath))
IJ.log('--- toAlignPatchPath --- ' + toAlignPatchPath)
l = int(transforms[i+1])
aff = AffineTransform([float(transforms[i+2]), float(transforms[i+3]), float(transforms[i+4]), float(transforms[i+5]), float(transforms[i+6])*float(downsamplingFactor), float(transforms[i+7])*float(downsamplingFactor) ])
layer = layerset.getLayers().get(l)
patches = layer.getDisplayables(Patch)
patchPaths = [os.path.normpath(loader.getAbsolutePath(patch))for patch in patches]
IJ.log('ALL PATCH PATHS ------------ ' + str(patchPaths))
thePatch = filter(lambda x: os.path.normpath(loader.getAbsolutePath(x)) == toAlignPatchPath, patches)[0]
thePatch.setAffineTransform(aff)
thePatch.updateBucket()
time.sleep(2)
fc.resizeDisplay(layerset)
time.sleep(2)
project.save()
fc.closeProject(project)
fc.terminatePlugin(namePlugin, MagCFolder)