def scaleTemplate(templatearray, scalefactor=1.0, boxsize=None):
        if(templatearray.shape[0] != templatearray.shape[1]):
                apDisplay.printWarning("template shape is NOT square, this may cause errors")

        if abs(scalefactor - 1.0) > 0.01:
                apDisplay.printMsg("scaling template by a factor of "+str(scalefactor))
                templatearray = apImage.scaleImage(templatearray, scalefactor)

        #make sure the box size is divisible by 16
        if boxsize is not None or (templatearray.shape[0] % 16 != 0):
                edgeavg = apImage.meanEdgeValue(templatearray)
                origsize = templatearray.shape[0]
                if boxsize is None:
                        # minimal padisize is 16
                        padsize  = max(int(math.floor(float(origsize)/16)*16),16)
                else:
                        padsize = boxsize
                padshape = numpy.array([padsize,padsize])
                apDisplay.printMsg("changing box size from "+str(origsize)+" to "+str(padsize))
                if origsize > padsize:
                        #shrink image
                        templatearray = apImage.frame_cut(templatearray, padshape)
                else:
                        #grow image
                        templatearray = apImage.frame_constant(templatearray, padshape, cval=edgeavg)

        if templatearray.shape[0] < 20 or templatearray.shape[1] < 20:
                apDisplay.printWarning("template is only "+str(templatearray.shape[0])+" pixels wide\n"+\
                  " and may only correlation noise in the image")

        return templatearray
Example #2
0
def projectFullZ(processdir,
                 runname,
                 seriesname,
                 bin=1,
                 rotx=True,
                 flipyz=False):
    """
#       Command for projecting full tomogram to z-axis
#
# Tomography reconstruction y and z are usually flipped
# full tomogram mrc file axes [x,z,y]
# clip average command need [x,y,z]
clip flipyz 09feb18c_002_full.rec temp.mrc
clip avg -2d -iz 0-199 temp.mrc projection.mrc
                """
    inputparams = {
        'recon': os.path.join(processdir, seriesname + "_full.rec"),
        'temp': os.path.join(processdir, "temp.rec"),
        'project': os.path.join(processdir, seriesname + "_zproject.mrc"),
    }
    fullshape = apFile.getMrcFileShape(inputparams['recon'])
    commands = []
    if rotx or flipyz:
        op = ''
        if rotx:
            op += ' rotx'
        if flipyz:
            op += ' flipyz'
        lookup = {'y': 0, 'z': 1}
        inputparams['3d'] = inputparams['temp']
        commands.append("$clip" + op + " %s %s" % (
            inputparams['recon'],
            inputparams['temp'],
        ))
    else:
        lookup = {'y': 1, 'z': 0}
        inputparams['3d'] = inputparams['recon']
    # limit slices for projection to 200 to save time.
    zcenter = int(fullshape[lookup['z']] / 2)
    zstart = max(0, zcenter - 100)
    zend = min(fullshape[lookup['z']] - 1, zcenter + 99)
    commands.append(
        "$clip avg -2d -iz %d-%d %s %s" % (
            zstart,
            zend,
            inputparams['3d'],
            inputparams['project'],
        ), )
    writeCommandAndRun(
        processdir, 'projectZ', commands,
        [inputparams['temp'], inputparams['project'], 'projectZ.log'])
    if bin > 1:
        # unbin the projection
        a = mrc.read(inputparams['project'])
        b = apImage.scaleImage(a, bin)
        mrc.write(b, inputparams['project'])
    return inputparams['project']
def array2jpg(pictpath,im,imin=None,imax=None,size=512):
        jpgpath = pictpath+'.jpg'
        imshape = im.shape
        scale = float(size)/imshape[1]
        im = apImage.scaleImage(im,scale)
        stats = arraystats.all(im)
        if imin is not None and imax is not None:
                range = (imin,imax)
        else:
                range = stats['mean']-3*stats['std'],stats['mean']+3*stats['std']
        numpil.write(im,jpgpath, format = 'JPEG', limits=range)
def array2jpg(pictpath, im, imin=None, imax=None, size=512):
    jpgpath = pictpath + '.jpg'
    imshape = im.shape
    scale = float(size) / imshape[1]
    im = apImage.scaleImage(im, scale)
    stats = arraystats.all(im)
    if imin is not None and imax is not None:
        range = (imin, imax)
    else:
        range = stats['mean'] - 3 * stats['std'], stats[
            'mean'] + 3 * stats['std']
    numpil.write(im, jpgpath, format='JPEG', limits=range)
def projectFullZ(processdir, runname, seriesname,bin=1,rotx=True,flipyz=False):
		"""
#	Command for projecting full tomogram to z-axis
#
# Tomography reconstruction y and z are usually flipped
# full tomogram mrc file axes [x,z,y]
# clip average command need [x,y,z]
clip flipyz 09feb18c_002_full.rec temp.mrc
clip avg -2d -iz 0-199 temp.mrc projection.mrc
		"""	
		inputparams = {
			'recon': os.path.join(processdir, seriesname+"_full.rec"),
			'temp': os.path.join(processdir, "temp.rec"),
			'project': os.path.join(processdir, seriesname+"_zproject.mrc"),
		}
		fullshape = apFile.getMrcFileShape(inputparams['recon'])
		commands = []
		if rotx or flipyz:
			op = ''
			if rotx:
				op += ' rotx'
			if flipyz:
				op += ' flipyz'
			lookup = {'y':0,'z':1}
			inputparams['3d'] = inputparams['temp']
			commands.append(
				"$clip"+op+" %s %s"
					% (inputparams['recon'],inputparams['temp'],
					)
				)
		else:
			lookup = {'y':1,'z':0}
			inputparams['3d'] = inputparams['recon']
		# limit slices for projection to 200 to save time.
		zcenter = int(fullshape[lookup['z']] / 2)
		zstart = max(0,zcenter - 100)
		zend = min(fullshape[lookup['z']]-1,zcenter + 99)
		commands.append(
				"$clip avg -2d -iz %d-%d %s %s"
					% (zstart,zend,
					inputparams['3d'],inputparams['project'],
					),
			)	
		writeCommandAndRun(processdir,'projectZ',commands,[inputparams['temp'],inputparams['project'],'projectZ.log'])
		if bin > 1:
			# unbin the projection
			a = mrc.read(inputparams['project'])
			b = apImage.scaleImage(a,bin)
			mrc.write(b,inputparams['project'])
		return inputparams['project']
def scaleTemplate(templatearray, scalefactor=1.0, boxsize=None):
    if (templatearray.shape[0] != templatearray.shape[1]):
        apDisplay.printWarning(
            "template shape is NOT square, this may cause errors")

    if abs(scalefactor - 1.0) > 0.01:
        apDisplay.printMsg("scaling template by a factor of " +
                           str(scalefactor))
        templatearray = apImage.scaleImage(templatearray, scalefactor)

    #make sure the box size is divisible by 16
    if boxsize is not None or (templatearray.shape[0] % 16 != 0):
        edgeavg = apImage.meanEdgeValue(templatearray)
        origsize = templatearray.shape[0]
        if boxsize is None:
            # minimal padisize is 16
            padsize = max(int(math.floor(float(origsize) / 16) * 16), 16)
        else:
            padsize = boxsize
        padshape = numpy.array([padsize, padsize])
        apDisplay.printMsg("changing box size from " + str(origsize) + " to " +
                           str(padsize))
        if origsize > padsize:
            #shrink image
            templatearray = apImage.frame_cut(templatearray, padshape)
        else:
            #grow image
            templatearray = apImage.frame_constant(templatearray,
                                                   padshape,
                                                   cval=edgeavg)

    if templatearray.shape[0] < 20 or templatearray.shape[1] < 20:
        apDisplay.printWarning("template is only "+str(templatearray.shape[0])+" pixels wide\n"+\
          " and may only correlation noise in the image")

    return templatearray