Esempio n. 1
0
def resample( prjfile, outdir, bufprefix, nbufvol, nvol, seedbase,\
  delta, d, snr, CTF, npad,\
  MPI, myid, ncpu, verbose = 0 ):
    from utilities import even_angles
    from random import seed, jumpahead, shuffle
    import os
    from sys import exit

    nprj = EMUtil.get_image_count(prjfile)

    if MPI:
        from mpi import mpi_barrier, MPI_COMM_WORLD

        if myid == 0:
            if os.path.exists(outdir): nx = 1
            else: nx = 0
        else: nx = 0
        ny = bcast_number_to_all(nx, source_node=0)
        if ny == 1:
            ERROR(
                'Output directory exists, please change the name and restart the program',
                "resample", 1, myid)
        mpi_barrier(MPI_COMM_WORLD)

        if myid == 0:
            os.mkdir(outdir)
        mpi_barrier(MPI_COMM_WORLD)
    else:
        if os.path.exists(outdir):
            ERROR(
                'Output directory exists, please change the name and restart the program',
                "resample", 1, 0)
        os.mkdir(outdir)

    if (verbose == 1):
        finfo = open(os.path.join(outdir, "progress%04d.txt" % myid), "w")
    else:
        finfo = None
    #print  " before evenangles",myid
    from utilities import getvec
    from numpy import array, reshape
    refa = even_angles(delta)
    nrefa = len(refa)
    refnormal = zeros((nrefa, 3), 'float32')

    tetref = [0.0] * nrefa
    for i in range(nrefa):
        tr = getvec(refa[i][0], refa[i][1])
        for j in range(3):
            refnormal[i][j] = tr[j]
        tetref[i] = refa[i][1]
    del refa
    vct = array([0.0] * (3 * nprj), 'float32')
    if myid == 0:
        print(" will read ", myid)
        tr = EMUtil.get_all_attributes(prjfile, 'xform.projection')
        tetprj = [0.0] * nprj
        for i in range(nprj):
            temp = tr[i].get_params("spider")
            tetprj[i] = temp["theta"]
            if (tetprj[i] > 90.0): tetprj[i] = 180.0 - tetprj[i]
            vct[3 * i + 0] = tr[i].at(2, 0)
            vct[3 * i + 1] = tr[i].at(2, 1)
            vct[3 * i + 2] = tr[i].at(2, 2)
        del tr
    else:
        tetprj = [0.0] * nprj
    #print "  READ ",myid
    if MPI:
        #print " will bcast",myid
        from mpi import mpi_bcast, MPI_FLOAT, MPI_COMM_WORLD
        vct = mpi_bcast(vct, len(vct), MPI_FLOAT, 0, MPI_COMM_WORLD)
        from utilities import bcast_list_to_all
        tetprj = bcast_list_to_all(tetprj, myid, 0)
    #print  "  reshape  ",myid
    vct = reshape(vct, (nprj, 3))
    assignments = [[] for i in range(nrefa)]
    dspn = 1.25 * delta
    for k in range(nprj):
        best_s = -1.0
        best_i = -1
        for i in range(nrefa):
            if (abs(tetprj[k] - tetref[i]) <= dspn):
                s = abs(refnormal[i][0] * vct[k][0] +
                        refnormal[i][1] * vct[k][1] +
                        refnormal[i][2] * vct[k][2])
                if s > best_s:
                    best_s = s
                    best_i = i
            assignments[best_i].append(k)
    am = len(assignments[0])
    mufur = 1.0 / am
    for i in range(1, len(assignments)):
        ti = len(assignments[i])
        am = min(am, ti)
        if (ti > 0): mufur += 1.0 / ti

    del tetprj, tetref

    dp = 1.0 - d  # keep that many in each direction
    keep = int(am * dp + 0.5)
    mufur = keep * nrefa / (1.0 - mufur * keep / float(nrefa))
    if myid == 0:
        print(" Number of projections ", nprj,
              ".  Number of reference directions ", nrefa,
              ",  multiplicative factor for the variance ", mufur)
        print(" Minimum number of assignments ", am,
              "  Number of projections used per stratum ", keep,
              " Number of projections in resampled structure ",
              int(am * dp + 0.5) * nrefa)
        if am < 2 or am == keep:
            print("incorrect settings")
            exit()  #                                         FIX

    if (seedbase < 1):
        seed()
        jumpahead(17 * myid + 123)
    else:
        seed(seedbase)
        jumpahead(17 * myid + 123)

    volfile = os.path.join(outdir, "bsvol%04d.hdf" % myid)
    from random import randint
    niter = nvol / ncpu / nbufvol
    for kiter in range(niter):
        if (verbose == 1):
            finfo.write("Iteration %d: \n" % kiter)
            finfo.flush()

        iter_start = time()
        #  the following has to be converted to resample  mults=1 means take given projection., mults=0 means omit

        mults = [[0] * nprj for i in range(nbufvol)]
        for i in range(nbufvol):
            for l in range(nrefa):
                mass = assignments[l][:]
                shuffle(mass)
                mass = mass[:keep]
                mass.sort()
                #print  l, "  *  ",mass
                for k in range(keep):
                    mults[i][mass[k]] = 1
            '''
			lout = []
			for l in xrange(len(mults[i])):
				if mults[i][l] == 1:  lout.append(l)
			write_text_file(lout, os.path.join(outdir, "list%04d_%03d.txt" %(i, myid)))
			del lout
			'''

        del mass

        rectors, fftvols, wgtvols = resample_prepare(prjfile, nbufvol, snr,
                                                     CTF, npad)
        resample_insert(bufprefix, fftvols, wgtvols, mults, CTF, npad, finfo)
        del mults
        resample_finish(rectors, fftvols, wgtvols, volfile, kiter, nprj, finfo)
        rectors = None
        fftvols = None
        wgtvols = None
        if (verbose == 1):
            finfo.write("time for iteration: %10.3f\n" % (time() - iter_start))
            finfo.flush()
Esempio n. 2
0
def resample( prjfile, outdir, bufprefix, nbufvol, nvol, seedbase,\
		delta, d, snr, CTF, npad,\
		MPI, myid, ncpu, verbose = 0 ):
	from   utilities import even_angles
	from   random import seed, jumpahead, shuffle
	import os
	from   sys import exit

	nprj = EMUtil.get_image_count( prjfile )

	if MPI:
		from mpi import mpi_barrier, MPI_COMM_WORLD

		if myid == 0:
			if os.path.exists(outdir):  nx = 1
			else:  nx = 0
		else:  nx = 0
		ny = bcast_number_to_all(nx, source_node = 0)
		if ny == 1:  ERROR('Output directory exists, please change the name and restart the program', "resample", 1,myid)
		mpi_barrier(MPI_COMM_WORLD)

		if myid == 0:
			os.mkdir(outdir)
		mpi_barrier(MPI_COMM_WORLD)
	else:
		if os.path.exists(outdir):
			ERROR('Output directory exists, please change the name and restart the program', "resample", 1,0)
		os.mkdir(outdir)

	if(verbose == 1):  finfo=open( os.path.join(outdir, "progress%04d.txt" % myid), "w" )
	else:              finfo = None
	#print  " before evenangles",myid
	from utilities import getvec
	from numpy import array, reshape
	refa = even_angles(delta)
	nrefa = len(refa)
	refnormal = zeros((nrefa,3),'float32')

	tetref = [0.0]*nrefa
	for i in xrange(nrefa):
	        tr = getvec( refa[i][0], refa[i][1] )
	        for j in xrange(3):  refnormal[i][j] = tr[j]
		tetref[i] = refa[i][1]
	del refa
	vct = array([0.0]*(3*nprj),'float32')
	if myid == 0:
		print  " will read ",myid
	        tr = EMUtil.get_all_attributes(prjfile,'xform.projection')
		tetprj = [0.0]*nprj
	        for i in xrange(nprj):
			temp = tr[i].get_params("spider")
			tetprj[i] = temp["theta"]
			if(tetprj[i] > 90.0): tetprj[i]  = 180.0 - tetprj[i] 
	        	vct[3*i+0] = tr[i].at(2,0)
	        	vct[3*i+1] = tr[i].at(2,1)
	        	vct[3*i+2] = tr[i].at(2,2)
	        del tr
	else:
		tetprj = [0.0]*nprj
	#print "  READ ",myid
	if  MPI:
		#print " will bcast",myid
		from mpi import mpi_bcast, MPI_FLOAT, MPI_COMM_WORLD
		vct = mpi_bcast(vct,len(vct),MPI_FLOAT,0,MPI_COMM_WORLD)
		from utilities import  bcast_list_to_all
		tetprj = bcast_list_to_all(tetprj, myid, 0)
	#print  "  reshape  ",myid
	vct = reshape(vct,(nprj,3))
	assignments = [[] for i in xrange(nrefa)]
	dspn = 1.25*delta
	for k in xrange(nprj):
	        best_s = -1.0
	        best_i = -1
	        for i in xrange( nrefa ):
			if(abs(tetprj[k] - tetref[i]) <= dspn):
				s = abs(refnormal[i][0]*vct[k][0] + refnormal[i][1]*vct[k][1] + refnormal[i][2]*vct[k][2])
				if s > best_s:
					best_s = s
					best_i = i
	        assignments[best_i].append(k)
	am = len(assignments[0])
	mufur = 1.0/am
	for i in xrange(1,len(assignments)):
		ti = len(assignments[i])
		am = min(am, ti)
		if(ti>0):  mufur += 1.0/ti

	del tetprj,tetref

	dp = 1.0 - d  # keep that many in each direction
	keep = int(am*dp +0.5)
	mufur = keep*nrefa/(1.0 - mufur*keep/float(nrefa))
	if myid == 0:
		print  " Number of projections ",nprj,".  Number of reference directions ",nrefa,",  multiplicative factor for the variance ",mufur
		print  " Minimum number of assignments ",am,"  Number of projections used per stratum ", keep," Number of projections in resampled structure ",int(am*dp +0.5)*nrefa
		if am <2 or am == keep:
			print "incorrect settings"
			exit()  #                                         FIX

	if(seedbase < 1):
		seed()
		jumpahead(17*myid+123)
	else:
		seed(seedbase)
		jumpahead(17*myid+123)

	volfile = os.path.join(outdir, "bsvol%04d.hdf" % myid)
	from random import randint
	niter = nvol/ncpu/nbufvol
	for kiter in xrange(niter):
		if(verbose == 1):
			finfo.write( "Iteration %d: \n" % kiter )
			finfo.flush()

		iter_start = time()
		#  the following has to be converted to resample  mults=1 means take given projection., mults=0 means omit

		mults = [ [0]*nprj for i in xrange(nbufvol) ]
		for i in xrange(nbufvol):
			for l in xrange(nrefa):
				mass = assignments[l][:]
				shuffle(mass)
				mass = mass[:keep]
				mass.sort()
				#print  l, "  *  ",mass
				for k in xrange(keep):
					mults[i][mass[k]] = 1
			'''
			lout = []
			for l in xrange(len(mults[i])):
				if mults[i][l] == 1:  lout.append(l)
			write_text_file(lout, os.path.join(outdir, "list%04d_%03d.txt" %(i, myid)))
			del lout
			'''

		del mass

		rectors, fftvols, wgtvols = resample_prepare( prjfile, nbufvol, snr, CTF, npad )
		resample_insert( bufprefix, fftvols, wgtvols, mults, CTF, npad, finfo )
		del mults
		resample_finish( rectors, fftvols, wgtvols, volfile, kiter, nprj, finfo )
		rectors = None
		fftvols = None
		wgtvols = None
		if(verbose == 1):
			finfo.write( "time for iteration: %10.3f\n" % (time() - iter_start) )
			finfo.flush()