while l.get() < min(nLayers, currentWrittenLayer + nLayersAtATime + 1) : k = l.getAndIncrement() if k < min(nLayers, currentWrittenLayer + nLayersAtATime): IJ.log('Start montaging elastically layer ' + str(k)) if layerset.getLayers().get(k).getNDisplayables() > 1: # some EM projects have a single large tile AlignTask().montageLayers(params, layerset.getLayers(k, k)) namePlugin = 'montage_ElasticEM' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) MagCParams = fc.readMagCParameters(MagCFolder) nLayersAtATime = MagCParams[namePlugin]['nLayersAtATime'] nThreads = MagCParams[namePlugin]['nThreads'] projectPath = fc.cleanLinuxPath(fc.findFilesFromTags(MagCFolder,['EM', 'Project'])[0]) project, loader, layerset, nLayers = fc.openTrakemProject(projectPath) IJ.log('Sleeping in case the opening of the large project takes some time ...') time.sleep(20) # parameters for elastic montage params = ElasticMontage.Param().clone() params.bmScale = 0.5 params.bmSearchRadius = 50 params.bmBlockRadius = 50 params.bmMinR = 0.1 params.bmMaxCurvatureR = 100 params.bmRodR = 1
import sys sys.path.append(IJ.getDirectory('plugins')) import fijiCommon as fc from mpicbg.trakem2.align import RegularizedAffineLayerAlignment from java.awt.geom import AffineTransform from java.awt import Rectangle from java.util import HashSet from ini.trakem2.display import Patch from ini.trakem2.imaging import Blending from ini.trakem2 import Project, ControlWindow namePlugin = 'export_LMChannels' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) transformsPath = fc.findFilesFromTags(MagCFolder, ['LM', 'transforms'])[0] with (open(transformsPath, 'r')) as f: transforms = f.readlines() nLayers = max([int(transforms[i]) for i in range(1, len(transforms), 8)]) + 1 IJ.log('nLayers = ' + str(nLayers)) width, height, nChannels, xGrid, yGrid, scaleX, scaleY, channels = fc.readSessionMetadata( MagCFolder) MagCParameters = fc.readMagCParameters(MagCFolder) scaleFactors = MagCParameters[namePlugin][ 'scaleFactors'] # scale factor for export, typically 1 and 0.1 flipX = MagCParameters[namePlugin]['flipX'] IJ.log('Iterating over the LM channels') for channel in channels:
from java.util import HashSet from ini.trakem2 import Project, ControlWindow from ini.trakem2.display import Patch from register_virtual_stack import Register_Virtual_Stack_MT namePlugin = 'alignRigid_LM' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) # get mosaic size MagCParameters = fc.readMagCParameters(MagCFolder) executeAlignment = MagCParameters[namePlugin]['executeAlignment'] boxFactor = MagCParameters[namePlugin]['boxFactor'] # e.g. 0.5, use only the center part of the layer to compute alignment: 0.5 divides the x and y dimensions by 2 projectPath = fc.findFilesFromTags(MagCFolder,['LMProject'])[0] # alignment parameters regParams = Register_Virtual_Stack_MT.Param() regParams.minInlierRatio = 0 regParams.registrationModelIndex = 1 regParams.featuresModelIndex = 1 regParams.sift.fdBins = 8 regParams.sift.fdSize = 4 regParams.sift.initialSigma = 1.6 regParams.sift.maxOctaveSize = 1024 regParams.sift.minOctaveSize = 64 regParams.sift.steps = 6 regParams.interpolate = True
from distutils.dir_util import copy_tree namePlugin = 'export_TransformedCroppedLM' MagCFolder = fc.startPlugin(namePlugin) # MagCFolder = r'E:\Users\Thomas\MixturesClean\MinimalPipelineTest5Sections_OneMoreTest04_07_17' ControlWindow.setGUIEnabled(False) width, height, nChannels, xGrid, yGrid, scaleX, scaleY, channels = fc.readSessionMetadata( MagCFolder) # channels = ['Brightfield', 'GFP', 'DsRed', 'contrastedBrightfield'] LMFolder = os.path.join(MagCFolder, 'MagC_LM') projectPath = fc.findFilesFromTags( MagCFolder, ['EM', 'Project' ])[0] # should I make 2 projects ? One for rigid, one for warped ? exportedEMFolder = fc.findFoldersFromTags( MagCFolder, ['export_alignedEMForRegistration'])[0] nLayers = len(os.listdir(exportedEMFolder)) registrationFolder = os.path.join(os.path.dirname(projectPath), 'LMEMRegistration') f = open(os.path.join(registrationFolder, 'lowResEMBounds'), 'r') widthEM, heightEM = pickle.load(f) f.close() pZ, loaderZ, layersetZ, nLayersZ = fc.getProjectUtils( fc.initTrakem(registrationFolder, 1))
[l, distance, len(inliers)]) namePlugin = 'compute_RegistrationMovingLeastSquares' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) MagCParams = fc.readMagCParameters(MagCFolder) matchingThreshold = MagCParams[namePlugin][ 'matchingThreshold'] # rejection threshold for the mean displacement of the transforms for the low and high resolution steps, respectively nOctaves = MagCParams[namePlugin]['nOctaves'] width, height, nChannels, xGrid, yGrid, scaleX, scaleY, channels = fc.readSessionMetadata( MagCFolder) EMMetadataPath = fc.findFilesFromTags(MagCFolder, ['EM', 'Metadata'])[0] nSections = fc.readParameters(EMMetadataPath)['nSections'] # nSections = 20 BIB = False ordered_surface_ids_path = [ os.path.join(MagCFolder, n) for n in os.listdir(MagCFolder) if 'ordered_surface_ids' in n ] if len(ordered_surface_ids_path) != 0: ordered_surface_ids_path = ordered_surface_ids_path[0] ordered_surface_ids = readIds(ordered_surface_ids_path) BIB = True projectPath = fc.findFilesFromTags(MagCFolder, ['EM', 'Project'])[0]
if not os.path.isfile(resizedFilePath): im = fc.resize(im, scaleFactor) FileSaver(im).saveAsTiff(resizedFilePath) IJ.log('Image resized to ' + resizedFilePath) im.close() namePlugin = 'downsample_EM' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) MagCParameters = fc.readMagCParameters(MagCFolder) EMDataFolder = os.path.join(MagCFolder, 'EMData') MagCEMFolder = os.path.join(MagCFolder, 'MagC_EM') # read metadata EMMetadataPath = fc.findFilesFromTags(MagCFolder,['EM_Metadata'])[0] EMMetadata = fc.readParameters(EMMetadataPath) tileWidth = int(EMMetadata['tileWidth']) tileHeight = int(EMMetadata['tileHeight']) IJ.log('TileWidth ' + str(tileWidth)) IJ.log('TileHeight ' + str(tileHeight)) cropRoi = Roi(100, 20, tileWidth - 2*100, tileHeight-20) # remove first lines because the Zeiss API # read downsampling factor downsamplingFactor = MagCParameters[namePlugin]['downsamplingFactor'] scaleFactor = 1./downsamplingFactor factorString = str(int(1000000*downsamplingFactor)).zfill(8) filePathsPath = os.path.join(MagCEMFolder, 'imagePathsForDownsampling' + factorString + '.txt') nTilesAtATime = MagCParameters[namePlugin]['nTilesAtATime']
IJ.log('Start montaging elastically layer ' + str(k)) if layerset.getLayers().get(k).getNDisplayables( ) > 1: # some EM projects have a single large tile AlignTask().montageLayers(params, layerset.getLayers(k, k)) namePlugin = 'montage_ElasticEM' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) MagCParams = fc.readMagCParameters(MagCFolder) nLayersAtATime = MagCParams[namePlugin]['nLayersAtATime'] nThreads = MagCParams[namePlugin]['nThreads'] projectPath = fc.cleanLinuxPath( fc.findFilesFromTags(MagCFolder, ['EM', 'Project'])[0]) projectPath = os.sep + projectPath IJ.log('projectPath ----------' + str(projectPath)) IJ.log('os.path.isfile(projectPath)------- ' + str(os.path.isfile(projectPath))) project, loader, layerset, nLayers = fc.openTrakemProject(projectPath) IJ.log('Sleeping in case the opening of the large project takes some time ...') time.sleep(20) # parameters for elastic montage params = ElasticMontage.Param().clone() params.bmScale = 0.5 params.bmSearchRadius = 50 params.bmBlockRadius = 50
resultRigidAlignmentFolder, resultRigidAlignmentFolder, referenceName, regParams, use_shrinking_constraint) time.sleep(2) # IJ.getImage().close() WindowManager.closeAllWindows() IJ.log('Rigid Alignment done') else: IJ.log('Rigid alignment already performed - skipping') ################################################ ########################################### IJ.log('Aligning the lowEM with the new rigid transforms') projectPath = fc.cleanLinuxPath( fc.findFilesFromTags(MagCFolder, ['EMProject_'])[0]) # this is the low res EM project, loader, layerset, nLayers = fc.openTrakemProject( projectPath) # the low res EM for l, layer in enumerate(layerset.getLayers()): transformPath = os.path.join( resultRigidAlignmentFolder, 'stitchedDownsampledEM_' + str(l).zfill(4) + '.xml') theList = ArrayList(HashSet()) aff = fc.getAffFromRVSTransformPath(transformPath) for patch in layer.getDisplayables(Patch): patch.setLocation( patch.getX(), patch.getY()) # compensate for the extracted bounding box currentAff = patch.getAffineTransform() currentAff.preConcatenate(aff)
if os.path.isfile(orderPath): newOrder = fc.readOrder(orderPath) else: newOrder = range(10000) # Reorder both the low EM and the EM projects ## low EM # downsamplingFactor downsamplingFactor = MagCParameters['downsample_EM']['downsamplingFactor'] factorString = str(int(1000000 * downsamplingFactor)).zfill(8) MagCEMFolder = os.path.dirname( fc.cleanLinuxPath( fc.findFilesFromTags(MagCFolder, ['montage_ElasticEM_Transforms.txt'])[0])) projectPath = fc.cleanLinuxPath( os.path.join(MagCEMFolder, 'EMProject_' + factorString + '.xml')) # this is the low res EM project unorderedProjectPath = fc.cleanLinuxPath( os.path.join(MagCEMFolder, 'LowEMProjectUnordered.xml')) # if a reordering had already been made, reinitialize the unordered project if os.path.isfile(unorderedProjectPath): if os.path.isfile(projectPath): os.remove(projectPath) shutil.copyfile(unorderedProjectPath, projectPath) project, loader, layerset, nLayers = fc.openTrakemProject(projectPath) project.saveAs(projectPath, True) fc.closeProject(project)
################################################ # rigid alignment outside trakem2 with register virtual stack plugin because bug in trakem2 transformsPath = os.path.join(MagC_EM_Folder, 'rigidAlignmentTransforms_' + namePlugin + '.txt') referenceName = fc.naturalSort(os.listdir(exportForRigidAlignmentFolder))[0] use_shrinking_constraint = 0 IJ.log('Rigid alignment with register virtual stack') Register_Virtual_Stack_MT.exec(exportForRigidAlignmentFolder, resultRigidAlignmentFolder, resultRigidAlignmentFolder, referenceName, regParams, use_shrinking_constraint) time.sleep(2) # IJ.getImage().close() WindowManager.closeAllWindows() IJ.log('Rigid Alignment done') ################################################ ########################################### IJ.log('Aligning the lowEM with the new rigid transforms') projectPath = fc.cleanLinuxPath(fc.findFilesFromTags(MagCFolder,['EMProject_'])[0]) # this is the low res EM project, loader, layerset, nLayers = fc.openTrakemProject(projectPath) # the low res EM for l, layer in enumerate(layerset.getLayers()): transformPath = os.path.join(resultRigidAlignmentFolder, 'stitchedDownsampledEM_' + str(l).zfill(4) + '.xml') theList = ArrayList(HashSet()) aff = fc.getAffFromRVSTransformPath(transformPath) for patch in layer.getDisplayables(Patch): patch.setLocation(patch.getX(), patch.getY()) # compensate for the extracted bounding box currentAff = patch.getAffineTransform() currentAff.preConcatenate(aff) patch.setAffineTransform(currentAff) fc.resizeDisplay(layerset) project.save() fc.closeProject(project)