if k < min(nLayers, currentWrittenLayer + nLayersAtATime): IJ.log('Start exporting layer ' + str(k) + ' currentWrittenLayer - ' + str(currentWrittenLayer)) fc.exportFlat(project, exportFolder, 1/float(LMEMFactor), baseName = 'alignedDownsampledEM', bitDepth = 8, layers = [k]) namePlugin = 'export_alignedEMForRegistration' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) MagCParams = fc.readMagCParameters(MagCFolder) nLayersAtATime = MagCParams[namePlugin]['nLayersAtATime'] nThreads = MagCParams[namePlugin]['nThreads'] LMEMFactor = fc.getLMEMFactor(MagCFolder) IJ.log('Exporting with LMEMFactor = ' + str(LMEMFactor)) projectPath = fc.findFilesFromTags(MagCFolder,['EM', 'Project'])[0] exportFolder = fc.mkdir_p(os.path.join(os.path.dirname(projectPath), namePlugin)) project, loader, layerset, nLayers = fc.openTrakemProject(projectPath) temporaryFolder = fc.mkdir_p(os.path.join(os.path.dirname(projectPath), 'temporary_LMEMRegistration')) # to save contrasted images # currentLayerPath stores in a file the current layer being processed by the script which is run several times currentLayerPath = os.path.join(os.path.dirname(projectPath), 'currentLayer_' + namePlugin + '.txt') currentWrittenLayer = fc.incrementCounter(currentLayerPath, increment = nLayersAtATime) atom = AtomicInteger(currentWrittenLayer) fc.startThreads(exportLayer, wait = 0, nThreads = nThreads) # project.save() # why do I save the project here ? time.sleep(3) fc.shouldRunAgain(namePlugin, currentWrittenLayer, nLayers, MagCFolder, project, increment = nLayersAtATime)
channels.append(contrastedChannel) toContrastPaths = [] for (dirpath, dirnames, filenames) in os.walk(LMDataFolder): for filename in filenames: IJ.log('ToContrast: ' + str(filename)) if (os.path.splitext(filename)[1] == '.tif') and (refChannel in filename): imagePath = os.path.join(dirpath, filename) contrastedPath = os.path.join( dirpath, filename.replace(refChannel, contrastedChannel)) toContrastPaths.append([imagePath, contrastedPath]) IJ.log('toContrastPaths : ' + str(toContrastPaths)) nPaths = len(toContrastPaths) atomicI = AtomicInteger(0) fc.startThreads(contrastImage) # Update metadata with the new contrasted channel f = open(LMMetadataPath, 'r') lines = f.readlines() for idLine, line in enumerate(lines): if 'nChannels' in line: lines[idLine] = 'nChannels = ' + str(nChannels + 1) if 'channels' in line: lines[idLine] = 'channels = [' + ','.join( map(lambda x: "'" + x + "'", channels)) + ']' f.close() f = open(LMMetadataPath, 'w') for line in lines: f.write(line + '\n') f.close()
widthEM = imEM0.width heightEM = imEM0.height imEM0.close() f = open(os.path.join(registrationFolder, 'lowResEMBounds'), 'w') pickle.dump([widthEM, heightEM], f) f.close() registrationStatsPath = os.path.join(registrationFolder, 'registrationStats') registrationStats = [] # create dummy trakem for applying affine and cropping LM in the first registration step pZ, loaderZ, layersetZ, nLayersZ = fc.getProjectUtils( fc.initTrakem(temporaryFolder, 1)) layersetZ.setDimensions(0, 0, widthEM * 5, heightEM * 5) layerZ = layersetZ.getLayers().get(0) lock = threading.Lock() # Setting up the parallel threads and starting them atomicI = AtomicInteger(0) fc.startThreads(computeRegistration, fractionCores=1, wait=0.5) fc.closeProject(pZ) # close dummy trakem # save some stats on the registration with open(registrationStatsPath, 'w') as f: pickle.dump(registrationStats, f) fc.terminatePlugin(namePlugin, MagCFolder) # fc.shouldRunAgain(namePlugin, currentLayer, nSections, MagCFolder, '', increment = nLayersAtATime)
filePaths.append(imPath) with open(filePathsPath,'w') as f: for path in filePaths: f.write(path + '\n') # pickle.dump(filePaths,f) else: filePaths = [] with open(filePathsPath,'r') as f: lines = f.readlines() for line in lines: filePaths.append(line.replace('\n', '')) # filePaths = pickle.load(f) #Create all the subfolders downSampledEMFolder = fc.mkdir_p(os.path.join(MagCEMFolder, 'MagC_EM_' + factorString, '')) for sectionFolderName in os.walk(EMDataFolder).next()[1]: fc.mkdir_p(os.path.join(downSampledEMFolder, sectionFolderName)) normLocalContrastSize = MagCParameters[namePlugin]['normLocalContrastSize'] # downsample in parallel threads = [] currentLayerPath = os.path.join(MagCEMFolder, 'currentLayer_' + namePlugin + '.txt') currentWrittenLayer = fc.incrementCounter(currentLayerPath, increment = nTilesAtATime) IJ.log(namePlugin + ' layer ' + str(currentWrittenLayer)) atomicI = AtomicInteger(currentWrittenLayer) fc.startThreads(resizeAndSave, fractionCores = 0.9, wait = 0, arguments = (filePaths, atomicI)) # terminate or rerun if more tiles to be processed time.sleep(1) fc.shouldRunAgain(namePlugin, atomicI.get(), len(filePaths), MagCFolder, '')
allPatchCoordinates = [] downsampledFolder = os.path.join(MagC_EM_Folder, 'MagC_EM_' + factorString) IJ.log('downsampledFolder ' + downsampledFolder) foldersToStitch = [ os.path.join(downsampledFolder, folderName) for folderName in os.walk(downsampledFolder).next()[1] ] foldersToStitch = fc.naturalSort(foldersToStitch) IJ.log('foldersToStitch ------------- ' + str(foldersToStitch)) # stitching should be done in parallel but the stitching plugin does not seem to run in parallel, so fractionCores=0 -> only one core used ... atom = AtomicInteger(0) fc.startThreads(parallelStitch, fractionCores=0, wait=0, arguments=(atom, foldersToStitch, allPatchCoordinates)) #for k in range(len(foldersToStitch)): # sectionFolder = foldersToStitch[k] # tileConfigurationPath = os.path.join(sectionFolder, 'TileConfiguration_' + str(k).zfill(4) + '.registered.txt') # stitchCommand = 'type=[Filename defined position] order=[Defined by filename ] grid_size_x=' + str(numTilesX) + ' grid_size_y=' + str(numTilesY) + ' tile_overlap=' + str(100 * (tileOverlapX + tileOverlapY)/2.) + ' first_file_index_x=0 first_file_index_y=0 directory=' + sectionFolder + ' file_names=Tile_{xx}-{yy}_resized_' + factorString + '.tif output_textfile_name=TileConfiguration_' + str(k).zfill(4) +'.txt fusion_method=[Do not fuse images (only write TileConfiguration)] regression_threshold=0.70 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 compute_overlap subpixel_accuracy computation_parameters=[Save computation time (but use more RAM)] image_output=[Write to disk] output_directory=' + sectionFolder # IJ.log('stitchCommand ---- ' + stitchCommand) # IJ.run('Grid/Collection stitching', stitchCommand) # f = open(tileConfigurationPath, 'r') # lines = f.readlines()[4:] # trimm the heading # f.close()
l = atomicI.getAndIncrement() if l < nLayers: IJ.log('Stitching layer ' + str(l)) AlignTask().montageLayers(params, layerset.getLayers(l, l), tilesAreInPlaceIn, largestGraphOnlyIn, hideDisconnectedTilesIn, deleteDisconnectedTilesIn) IJ.log('Blending layer ' + str(l)) Blending.blendLayerWise(layerset.getLayers(l, l), True, None) if l%10 == 0: # save project every 5 layers project.save() namePlugin = 'montage_LM' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) # get mosaic size MagCParameters = fc.readMagCParameters(MagCFolder) mosaic = MagCParameters[namePlugin]['mosaic'] # e.g. [2,2] if mosaic !=[1,1]: projectPath = fc.findFilesFromTags(MagCFolder,['LMProject'])[0] projectName = os.path.basename(projectPath) project, loader, layerset, nLayers = fc.openTrakemProject(projectPath) nLayers = len(layerset.getLayers()) atomicI = AtomicInteger(0) fc.startThreads(stitchLayers, fractionCores = 0.1) fc.resizeDisplay(layerset) project.save() fc.closeProject(project) fc.terminatePlugin(namePlugin, MagCFolder)