def appendEulerDoc(self, eulerfile, tiltpartnum, count):
         eulerf = open(eulerfile, "a")
         stackpartdata = apStack.getStackParticle(self.params['tiltstackid'], tiltpartnum)
         gamma, theta, phi, tiltangle = apTiltPair.getParticleTiltRotationAngles(stackpartdata)
         line = operations.spiderOutLine(count, [phi, tiltangle, -1.0*gamma])
         eulerf.write(line)
         eulerf.close()
 def makeEulerDoc(self, parttree):
     count = 0
     eulerfile = os.path.join(self.params['rundir'],
                              "eulersdoc" + self.timestamp + ".spi")
     eulerf = open(eulerfile, "w")
     apDisplay.printMsg("creating Euler doc file")
     starttime = time.time()
     for partdict in parttree:
         stackpartdata = apStack.getStackParticle(
             self.params['tiltstackid'], partdict['tilt'])
         count += 1
         if count % 100 == 0:
             sys.stderr.write(".")
             eulerf.flush()
         gamma, theta, phi, tiltangle = apTiltPair.getParticleTiltRotationAngles(
             stackpartdata)
         if gamma is None:
             apDisplay.printWarning("Skipping " + str(stackpartdata))
             continue
         line = operations.spiderOutLine(count, [phi, tiltangle, gamma])
         eulerf.write(line)
     eulerf.close()
     apDisplay.printColor(
         "finished Euler doc file in " +
         apDisplay.timeString(time.time() - starttime), "cyan")
     return eulerfile
	def appendEulerDoc(self, eulerfile, tiltpartnum, count):
		eulerf = open(eulerfile, "a")
		stackpartdata = apStack.getStackParticle(self.params['tiltstackid'], tiltpartnum)
		gamma, theta, phi, tiltangle = apTiltPair.getParticleTiltRotationAngles(stackpartdata)
		if gamma is None:
			apDisplay.printWarning("Skipping "+str(stackpartdata))
			return			
		line = operations.spiderOutLine(count, [phi, tiltangle, -1.0*gamma])
		eulerf.write(line)
		eulerf.close()
 def appendEulerDoc(self, eulerfile, tiltpartnum, count):
     eulerf = open(eulerfile, "a")
     stackpartdata = apStack.getStackParticle(self.params['tiltstackid'],
                                              tiltpartnum)
     gamma, theta, phi, tiltangle = apTiltPair.getParticleTiltRotationAngles(
         stackpartdata)
     if gamma is None:
         apDisplay.printWarning("Skipping " + str(stackpartdata))
         return
     line = operations.spiderOutLine(count, [phi, tiltangle, -1.0 * gamma])
     eulerf.write(line)
     eulerf.close()
 def makeEulerDoc(self, tiltParticlesData):
     count = 0
     eulerfile = os.path.join(self.params['rundir'],
                              "eulersdoc" + self.timestamp + ".spi")
     eulerf = open(eulerfile, "w")
     apDisplay.printMsg("Creating Euler angles doc file")
     starttime = time.time()
     tiltParticlesData.sort(self.sortTiltParticlesData)
     startmem = mem.active()
     for stackpartdata in tiltParticlesData:
         count += 1
         if count % 50 == 0:
             sys.stderr.write(".")
             eulerf.flush()
             memdiff = (mem.active() - startmem) / count / 1024.0
             if memdiff > 3:
                 apDisplay.printColor(
                     "Memory increase: %d MB/part" % (memdiff), "red")
         tiltrot, theta, notrot, tiltangle = apTiltPair.getParticleTiltRotationAngles(
             stackpartdata)
         if tiltrot is None:
             apDisplay.printError("BAD particle  " + str(stackpartdata))
         inplane, mirror = self.getParticleInPlaneRotation(stackpartdata)
         totrot = -1.0 * (notrot + inplane)
         if mirror is True:
             #theta flips to the back
             tiltangle = -1.0 * tiltangle + 180  #tiltangle = tiltangle + 180.0   #theta
             totrot = -1.0 * totrot - 180.0  #phi
             tiltrot = tiltrot + 180  #tiltrot = -1.0 * tiltrot + 180.0 #psi
         while totrot < 0:
             totrot += 360.0
         ### this is the original eman part num; count is new part num
         partnum = stackpartdata['particleNumber'] - 1
         line = operations.spiderOutLine(count,
                                         [tiltrot, tiltangle, totrot])
         eulerf.write(line)
     eulerf.close()
     apDisplay.printColor(
         "\nFinished Euler angle doc file in " +
         apDisplay.timeString(time.time() - starttime), "cyan")
     memdiff = (mem.active() - startmem) / count / 1024.0
     if memdiff > 0.1:
         apDisplay.printColor("Memory increase: %.2f MB/part" % (memdiff),
                              "red")
     return eulerfile
 def makeEulerDoc(self, parttree):
         count = 0
         eulerfile = os.path.join(self.params['rundir'], "eulersdoc"+self.timestamp+".spi")
         eulerf = open(eulerfile, "w")
         apDisplay.printMsg("creating Euler doc file")
         starttime = time.time()
         for partdict in parttree:
                 stackpartdata = apStack.getStackParticle(self.params['tiltstackid'], partdict['tilt'])
                 count += 1
                 if count%100 == 0:
                         sys.stderr.write(".")
                         eulerf.flush()
                 gamma, theta, phi, tiltangle = apTiltPair.getParticleTiltRotationAngles(stackpartdata)
                 line = operations.spiderOutLine(count, [phi, tiltangle, gamma])
                 eulerf.write(line)
         eulerf.close()
         apDisplay.printColor("finished Euler doc file in "+apDisplay.timeString(time.time()-starttime), "cyan")
         return eulerfile
	def makeEulerDoc(self, tiltParticlesData):
		count = 0
		eulerfile = os.path.join(self.params['rundir'], "eulersdoc"+self.timestamp+".spi")
		eulerf = open(eulerfile, "w")
		apDisplay.printMsg("Creating Euler angles doc file")
		starttime = time.time()
		tiltParticlesData.sort(self.sortTiltParticlesData)
		startmem = mem.active()
		for stackpartdata in tiltParticlesData:
			count += 1
			if count%50 == 0:
				sys.stderr.write(".")
				eulerf.flush()
				memdiff = (mem.active()-startmem)/count/1024.0
				if memdiff > 3:
					apDisplay.printColor("Memory increase: %d MB/part"%(memdiff), "red")
			tiltrot, theta, notrot, tiltangle = apTiltPair.getParticleTiltRotationAngles(stackpartdata)
			if tiltrot is None:
				apDisplay.printError("BAD particle  "+str(stackpartdata))
			inplane, mirror = self.getParticleInPlaneRotation(stackpartdata)
			totrot = -1.0*(notrot + inplane)
			if mirror is True:
				#theta flips to the back
				tiltangle = -1.0 * tiltangle + 180 #tiltangle = tiltangle + 180.0   #theta
				totrot = -1.0 * totrot - 180.0  #phi
				tiltrot = tiltrot + 180            #tiltrot = -1.0 * tiltrot + 180.0 #psi
			while totrot < 0:
				totrot += 360.0
			### this is the original eman part num; count is new part num
			partnum = stackpartdata['particleNumber']-1
			line = operations.spiderOutLine(count, [tiltrot, tiltangle, totrot])
			eulerf.write(line)
		eulerf.close()
		apDisplay.printColor("\nFinished Euler angle doc file in "+apDisplay.timeString(time.time()-starttime), "cyan")
		memdiff = (mem.active()-startmem)/count/1024.0
		if memdiff > 0.1:
			apDisplay.printColor("Memory increase: %.2f MB/part"%(memdiff), "red")
		return eulerfile
    def getGoodAlignParticles(self):
        includeParticle = []
        tiltParticlesData = []
        nopairParticle = 0
        excludeParticle = 0
        badmirror = 0
        badscore = 0
        apDisplay.printMsg("Sorting particles from classes at " +
                           time.asctime())
        count = 0
        startmem = mem.active()
        t0 = time.time()
        if self.params['clusterid'] is not None:
            ### method 1: get particles from clustering data
            clusterpartq = appiondata.ApClusteringParticleData()
            clusterpartq[
                'clusterstack'] = appiondata.ApClusteringStackData.direct_query(
                    self.params['clusterid'])
            clusterpartdatas = clusterpartq.query()
            apDisplay.printMsg("Sorting " + str(len(clusterpartdatas)) +
                               " clustered particles")

            for clustpart in clusterpartdatas:
                count += 1
                if count % 50 == 0:
                    sys.stderr.write(".")
                    memdiff = (mem.active() - startmem) / count / 1024.0
                    if memdiff > 3:
                        apDisplay.printColor(
                            "Memory increase: %d MB/part" % (memdiff), "red")
                #write to text file
                clustnum = clustpart['refnum'] - 1
                if self.params['minscore'] is not None:
                    if (clustpart['alignparticle']['score'] is not None
                            and clustpart['alignparticle']['score'] <
                            self.params['minscore']):
                        badscore += 1
                        continue
                    elif (clustpart['alignparticle']['spread'] is not None
                          and clustpart['alignparticle']['spread'] <
                          self.params['minscore']):
                        badscore += 1
                        continue
                if clustnum in self.classlist:
                    notstackpartnum = clustpart['alignparticle']['stackpart'][
                        'particleNumber']
                    tiltstackpartdata = apTiltPair.getStackParticleTiltPair(
                        self.params['notstackid'], notstackpartnum,
                        self.params['tiltstackid'])

                    if tiltstackpartdata is None:
                        nopairParticle += 1
                        continue
                    tiltrot, theta, notrot, tiltangle = apTiltPair.getParticleTiltRotationAngles(
                        tiltstackpartdata)
                    if tiltrot is None:
                        apDisplay.printWarning("BAD particle  " +
                                               str(tiltstackpartdata))
                        nopairParticle += 1
                        continue
                    else:
                        inplane, mirror = self.getParticleInPlaneRotation(
                            tiltstackpartdata)
                        if (self.params['mirror'] == "all" or
                            (self.params['mirror'] == "no" and mirror is False)
                                or (self.params['mirror'] == "yes"
                                    and mirror is True)):
                            emantiltstackpartnum = tiltstackpartdata[
                                'particleNumber'] - 1
                            includeParticle.append(emantiltstackpartnum)
                            tiltParticlesData.append(tiltstackpartdata)
                            if self.params['numpart'] is not None and len(
                                    includeParticle) > self.params['numpart']:
                                break
                        else:
                            badmirror += 1
                else:
                    excludeParticle += 1
        else:
            ### method 2: get particles from alignment data
            alignpartq = appiondata.ApAlignParticleData()
            alignpartq['alignstack'] = self.alignstackdata
            alignpartdatas = alignpartq.query()
            apDisplay.printMsg("Sorting " + str(len(alignpartdatas)) +
                               " aligned particles")

            for alignpart in alignpartdatas:
                count += 1
                if count % 50 == 0:
                    sys.stderr.write(".")
                    memdiff = (mem.active() - startmem) / count / 1024.0
                    if memdiff > 3:
                        apDisplay.printColor(
                            "Memory increase: %d MB/part" % (memdiff), "red")
                #write to text file
                alignnum = alignpart['ref']['refnum'] - 1
                if (self.params['minscore'] is not None
                        and alignpart['score'] is not None
                        and alignpart['score'] < self.params['minscore']):
                    badscore += 1
                    continue
                if alignnum in self.classlist:
                    notstackpartnum = alignpart['stackpart']['particleNumber']
                    tiltstackpartdata = apTiltPair.getStackParticleTiltPair(
                        self.params['notstackid'], notstackpartnum,
                        self.params['tiltstackid'])
                    if tiltstackpartdata is None:
                        nopairParticle += 1
                    else:
                        inplane, mirror = self.getParticleInPlaneRotation(
                            tiltstackpartdata)
                        if (self.params['mirror'] == "all" or
                            (self.params['mirror'] == "no" and mirror is False)
                                or (self.params['mirror'] == "yes"
                                    and mirror is True)):
                            emantiltstackpartnum = tiltstackpartdata[
                                'particleNumber'] - 1
                            includeParticle.append(emantiltstackpartnum)
                            tiltParticlesData.append(tiltstackpartdata)
                            if self.params['numpart'] is not None and len(
                                    includeParticle) > self.params['numpart']:
                                break
                        else:
                            badmirror += 1
                else:
                    excludeParticle += 1
        ### end methods

        includeParticle.sort()
        ### messages
        if time.time() - t0 > 1.0:
            apDisplay.printMsg("\nSorting time: " +
                               apDisplay.timeString(time.time() - t0))
        apDisplay.printMsg("Keeping " + str(len(includeParticle)) +
                           " and excluding \n\t" + str(excludeParticle) +
                           " particles with " + str(nopairParticle) +
                           " unpaired particles")
        if badmirror > 0:
            apDisplay.printMsg("Particles with bad mirrors: %d" % (badmirror))
        if badscore > 0:
            apDisplay.printColor("Particles with bad scores: %d" % (badscore),
                                 "cyan")
        if len(includeParticle) < 1:
            apDisplay.printError("No particles were kept")
        memdiff = (mem.active() - startmem) / count / 1024.0
        if memdiff > 0.1:
            apDisplay.printColor("Memory increase: %.2f MB/part" % (memdiff),
                                 "red")

        return includeParticle, tiltParticlesData
	def getGoodAlignParticles(self):
		includeParticle = []
		tiltParticlesData = []
		nopairParticle = 0
		excludeParticle = 0
		badmirror = 0
		badscore = 0
		apDisplay.printMsg("Sorting particles from classes at "+time.asctime())
		count = 0
		startmem = mem.active()
		t0 = time.time()
		if self.params['clusterid'] is not None:
			### method 1: get particles from clustering data
			clusterpartq = appiondata.ApClusteringParticleData()
			clusterpartq['clusterstack'] = appiondata.ApClusteringStackData.direct_query(self.params['clusterid'])
			clusterpartdatas = clusterpartq.query()
			apDisplay.printMsg("Sorting "+str(len(clusterpartdatas))+" clustered particles")

			for clustpart in clusterpartdatas:
				count += 1
				if count%50 == 0:
					sys.stderr.write(".")
					memdiff = (mem.active()-startmem)/count/1024.0
					if memdiff > 3:
						apDisplay.printColor("Memory increase: %d MB/part"%(memdiff), "red")
				#write to text file
				clustnum = clustpart['refnum']-1
				if self.params['minscore'] is not None:
					if ( clustpart['alignparticle']['score'] is not None
					 and clustpart['alignparticle']['score'] < self.params['minscore'] ):
						badscore += 1
						continue
					elif ( clustpart['alignparticle']['spread'] is not None
					 and clustpart['alignparticle']['spread'] < self.params['minscore'] ):
						badscore += 1
						continue
				if clustnum in self.classlist:
					notstackpartnum = clustpart['alignparticle']['stackpart']['particleNumber']
					tiltstackpartdata = apTiltPair.getStackParticleTiltPair(self.params['notstackid'],
						notstackpartnum, self.params['tiltstackid'])
						
				
					if tiltstackpartdata is None:
						nopairParticle += 1
						continue
					tiltrot, theta, notrot, tiltangle = apTiltPair.getParticleTiltRotationAngles(tiltstackpartdata)
					if tiltrot is None:
						apDisplay.printWarning("BAD particle  "+str(tiltstackpartdata))
						nopairParticle += 1
						continue
					else:
						inplane, mirror = self.getParticleInPlaneRotation(tiltstackpartdata)
						if ( self.params['mirror'] == "all"
						 or (self.params['mirror'] == "no" and mirror is False)
						 or (self.params['mirror'] == "yes" and mirror is True) ):
							emantiltstackpartnum = tiltstackpartdata['particleNumber']-1
							includeParticle.append(emantiltstackpartnum)
							tiltParticlesData.append(tiltstackpartdata)
							if self.params['numpart'] is not None and len(includeParticle) > self.params['numpart']:
								break
						else:
							badmirror += 1
				else:
					excludeParticle += 1
		else:
			### method 2: get particles from alignment data
			alignpartq = appiondata.ApAlignParticleData()
			alignpartq['alignstack'] = self.alignstackdata
			alignpartdatas = alignpartq.query()
			apDisplay.printMsg("Sorting "+str(len(alignpartdatas))+" aligned particles")

			for alignpart in alignpartdatas:
				count += 1
				if count%50 == 0:
					sys.stderr.write(".")
					memdiff = (mem.active()-startmem)/count/1024.0
					if memdiff > 3:
						apDisplay.printColor("Memory increase: %d MB/part"%(memdiff), "red")
				#write to text file
				alignnum = alignpart['ref']['refnum']-1
				if ( self.params['minscore'] is not None
				 and alignpart['score'] is not None
				 and alignpart['score'] < self.params['minscore'] ):
					badscore += 1
					continue
				if alignnum in self.classlist:
					notstackpartnum = alignpart['stackpart']['particleNumber']
					tiltstackpartdata = apTiltPair.getStackParticleTiltPair(self.params['notstackid'],
						notstackpartnum, self.params['tiltstackid'])
					if tiltstackpartdata is None:
						nopairParticle += 1
					else:
						inplane, mirror = self.getParticleInPlaneRotation(tiltstackpartdata)
						if ( self.params['mirror'] == "all"
						 or (self.params['mirror'] == "no" and mirror is False)
						 or (self.params['mirror'] == "yes" and mirror is True) ):
							emantiltstackpartnum = tiltstackpartdata['particleNumber']-1
							includeParticle.append(emantiltstackpartnum)
							tiltParticlesData.append(tiltstackpartdata)
							if self.params['numpart'] is not None and len(includeParticle) > self.params['numpart']:
								break
						else:
							badmirror += 1
				else:
					excludeParticle += 1
		### end methods

		includeParticle.sort()
		### messages
		if time.time()-t0 > 1.0:
			apDisplay.printMsg("\nSorting time: "+apDisplay.timeString(time.time()-t0))
		apDisplay.printMsg("Keeping "+str(len(includeParticle))+" and excluding \n\t"
			+str(excludeParticle)+" particles with "+str(nopairParticle)+" unpaired particles")
		if badmirror > 0:
			apDisplay.printMsg("Particles with bad mirrors: %d"%(badmirror))
		if badscore > 0:
			apDisplay.printColor("Particles with bad scores: %d"%(badscore), "cyan")
		if len(includeParticle) < 1:
			apDisplay.printError("No particles were kept")
		memdiff = (mem.active()-startmem)/count/1024.0
		if memdiff > 0.1:
			apDisplay.printColor("Memory increase: %.2f MB/part"%(memdiff), "red")

		return includeParticle, tiltParticlesData