Esempio n. 1
0
def assemblePngs(keys, tag, montagestack):
	apDisplay.printMsg("assembling pngs into montage")
	# get path from montagestack
	stackpath = os.path.dirname(os.path.abspath(montagestack))

	montagecmd = "montage -geometry +4+4 "
	montagestackdata = apImagicFile.readImagic(montagestack)
	for i,key in enumerate(keys):
		if i % 20 == 0:
			sys.stderr.write(".")
		pngfile = key+".png"
		array = montagestackdata['images'][i]
		apImage.arrayToPng(array, pngfile, normalize=True, msg=False)
		#proccmd = "proc2d "+montagestack+" "+pngfile+" first="+str(i)+" last="+str(i)
		#apEMAN.executeEmanCmd(proccmd, verbose=False, showcmd=False)
		montagecmd += pngfile+" "
	apDisplay.printMsg("montaging")
	montagefile = os.path.join(stackpath,"montage"+tag+".png")
	montagecmd += montagefile
	apEMAN.executeEmanCmd(montagecmd, verbose=True)
	#rotatecmd = "mogrify -rotate 180 -flop "+montagefile
	#apEMAN.executeEmanCmd(rotatecmd, verbose=False)
	for key in keys:
		apFile.removeFile(key+".png")
	if not os.path.isfile(montagefile):
		apDisplay.printWarning("failed to create montage file")
	def createMontageInMemory(self, apix):
		self.cluster_resolution = []
		apDisplay.printMsg("Converting files")

		### Set binning of images
		boxsize = apImagicFile.getBoxsize(self.instack)
		bin = 1
		while boxsize/bin > 200:
			bin+=1
		binboxsize = boxsize/bin

		### create averages
		files = glob.glob(self.timestamp+".[0-9]*")
		files.sort(self.sortFile)
		montage = []
		montagepngs = []
		i = 0
		for listname in files:
			i += 1
			apDisplay.printMsg("%d of %d classes"%(i,len(files)))
			pngfile = listname+".png"
			if not os.path.isfile(listname) or apFile.fileSize(listname) < 1:
				### create a ghost particle
				sys.stderr.write("skipping "+listname+"\n")
				blank = numpy.ones((binboxsize, binboxsize), dtype=numpy.float32)

				### add to montage stack
				montage.append(blank)
				self.cluster_resolution.append(None)

				### create png
				apImage.arrayToPng(blank, pngfile)

			else:
				### read particle list
				partlist = self.readListFile(listname)

				### average particles
				partdatalist = apImagicFile.readParticleListFromStack(self.instack, partlist, boxsize, msg=False)
				partdataarray = numpy.asarray(partdatalist)
				finaldata = partdataarray.mean(0)
				if bin > 1:
					finaldata = apImage.binImg(finaldata, bin)

				### add to montage stack
				montage.append(finaldata)
				res = apFourier.spectralSNR(partdatalist, apix)
				self.cluster_resolution.append(res)

				### create png
				apImage.arrayToPng(finaldata, pngfile)

			### check for png file
			if os.path.isfile(pngfile):
				montagepngs.append(pngfile)
			else:
				apDisplay.printError("failed to create montage")

		stackname = "kerdenstack"+self.timestamp+".hed"
		apImagicFile.writeImagic(montage, stackname)
		### create montage
		montagecmd = ("montage -geometry +4+4 -tile %dx%d "%(self.params['xdim'], self.params['ydim']))
		for monpng in montagepngs:
			montagecmd += monpng+" "
		montagecmd += "montage.png"
		apEMAN.executeEmanCmd(montagecmd, showcmd=True, verbose=False)
		time.sleep(1)
		apFile.removeFilePattern(self.timestamp+".*.png")
		return bin