示例#1
0
def setup_nn4(image_size, npad=2, sym='c1', weighting=1, **extra):
    ''' Initalize a reconstruction object
    
    :Parameters:
    
    image_size : int
                 Size of the input image and output volume
    npad : int, optional
           Number of times to pad the input image, default: 2
    sym : str, optional
          Type of symmetry, default: 'c1'
    weighting : int
                Amount of weight to give projections
    
    :Returns:
    
    recon : tuple
            Reconstructor, Fourier volume, Weight Volume, and numpy versions
    '''

    if EMAN2 is None:
        raise ImportError, "EMAN2/Sparx library not available, setup_nn4 requires EMAN2/Sparx"
    fftvol = EMAN2.EMData()
    weight = EMAN2.EMData()
    param = {
        "size": image_size,
        "npad": npad,
        "symmetry": sym,
        "weighting": weighting,
        "fftvol": fftvol,
        "weight": weight
    }
    r = EMAN2.Reconstructors.get("nn4", param)
    r.setup()
    return (r, fftvol, weight), em2numpy(fftvol), em2numpy(weight), image_size
示例#2
0
def localali(im1, im2, psref, maxdx):
    """Takes an fft.amplitude() psref to use as a basis for optimal alignment. Exhaustive search +-maxdx pixels.
	returns (dx,dy) required to bring im2 into alignment with im1"""

    nx = im1["nx"]
    mask = EMAN2.EMData(nx, nx, 1)
    mask.to_one()
    mask.process_inplace("mask.gaussian", {
        "inner_radius": nx / 4,
        "outer_radius": nx / 12
    })
    #	EMAN2.display(mask)

    maxdx = int(maxdx)
    out = EMAN2.EMData(2 * maxdx + 1, 2 * maxdx + 1, 1)
    for dx in xrange(-maxdx, maxdx + 1):
        for dy in xrange(-maxdx, maxdx + 1):
            av = im1 + im2.process("xform.translate.int",
                                   {"trans": (dx, dy, 0)})
            av.mult(
                mask
            )  # to prevent edge effects and slightly smooth the powspec
            avf = av.do_fft()
            avf.ri2ap()
            avf = avf.amplitude()
            out[dx + maxdx, dy + maxdx] = avf.cmp("ccc", psref)


#			print dx,dy,out[dx+20,dy+20]

    ret = out.calc_max_location()
    return (int(ret[0] - maxdx), int(ret[1] - maxdx)), out
示例#3
0
def do_dataset(names):
    '''
    Supposing we have marked data laying in folder data,
    in two subfolders 'good' and 'ice' and only this way.
    For now way of keeping data is hardcoded.
    #TODO:Make set creator more flexible.
    
    So we got list of full paths to mrc files lying in both direcories.
    Func assembles dataset,labels encoding dictionary
    matching classifier labels to words 'good' and 'ice' and
    returning it.
    
    Args:
         names - list of full paths to mrc files lying in a directories
                 as described above.
    
    returns: dataset(list of feature vectors)
             labels(list of labels)
             encoding_dict dictionary via label:label_name.
    '''
    labels = [name.split('/')[-2] for name in names]
    labels = np.array(labels)

    ice_mask = labels == 'ice'
    good_mask = labels == 'good'

    names = np.array(names)
    ice_names = names[ice_mask]
    good_names = names[good_mask]

    good_labels = [0] * len(good_names)
    ice_labels = [1] * len(good_names)

    encoding_dict = {0: 'good', 1: 'ice'}

    goods = []
    ices = []

    for name in good_names:
        img = EMAN2.EMData(name, 0)
        img_np = EMAN2.EMNumPy.em2numpy(img)
        sample = ft.preprocess_img(img_np)
        goods.append(sample)

    for name in ice_names:
        img = EMAN2.EMData(name, 0)
        img_np = EMAN2.EMNumPy.em2numpy(img)
        sample = ft.preprocess_img(img_np)
        ices.append(sample)

    dataset = goods + ices
    labels = good_labels + ice_labels

    return dataset, labels, encoding_dict
示例#4
0
文件: handlers.py 项目: cryoem/emen2
    def _build(self, index):
        '''Build a single image in the file.'''
        # print "...index:", index
        header = {}

        # Read the header
        img = EMAN2.EMData()
        img.read_image(self.workfile, 0, True)
        h = img.get_attr_dict()

        # Copy basic header information
        header['nx'] = h['nx']
        header['ny'] = h['ny']
        header['nz'] = h['nz']
        header['slices'] = []

        if header['nz'] == 1:
            # 2D Image
            img2 = EMAN2.EMData()
            img2.read_image(self.workfile, index, False)
            img2.process_inplace("normalize")
            if self.nimg > 1:
                # ... stack of 2D images.
                header['slices'].append(
                    self.build_slice(img2, index=index, fixed=[128, 512,
                                                               1024]))
            else:
                # regular old 2D image -- also generate power spectrum and tiles.
                header['slices'].append(
                    self.build_slice(img2,
                                     index=index,
                                     tile=True,
                                     pspec=True,
                                     fixed=[128, 512, 1024]))

        else:
            # 3D Image -- read region for each Z slice
            for i in range(header['nz']):
                region = EMAN2.Region(0, 0, i, header['nx'], header['ny'], 1)
                img2 = EMAN2.EMData()
                img2.read_image(self.workfile, 0, False, region)
                header['slices'].append(
                    self.build_slice(img2,
                                     index=index,
                                     nz=i,
                                     fixed=[128, 512, 1024]))

        return header
示例#5
0
    def convert(self, name, format, normalize=False):
        import EMAN2

        if format not in [
                'tif', 'tiff', 'tif8', 'mrc', 'hdf', 'jpg', 'jpeg', 'png'
        ]:
            raise ValueError, "Invalid format: %s" % format

        depth = None
        if format in ['tif8']:
            depth = 8
            format = 'tif'

        bdo = self.db.binary.get(name)
        img = EMAN2.EMData()
        img.read_image(str(bdo.filepath))

        if normalize:
            img.process_inplace("normalize")

        outfile = tempfile.NamedTemporaryFile(delete=False,
                                              suffix='.%s' % format)

        if depth == 8:
            img['render_min'] = -1
            img['render_max'] = 256
            img.write_image(str(outfile.name), -1,
                            EMAN2.EMUtil.ImageType.IMAGE_UNKNOWN, False, None,
                            EMAN2.EMUtil.EMDataType.EM_UCHAR, False)
        else:
            img.write_image(str(outfile.name))

        filename = os.path.splitext(bdo.filename)[0]
        filename = '%s.%s' % (filename, format)
        return filename, outfile.name
示例#6
0
def preload_images(micrographs, img_buffer):
    '''
    Preloads IMG_BUFFER_SIZE number of images into the memory.

    :param micrographs: list of all micrographs
    :param img_buffer: deque as buffer for images
    :return: None
    '''

    while True:
        index = GUI.fileList.row(GUI.fileList.currentItem())
        max_index = index + IMG_BUFFER_SIZE + 1
        if max_index > len(micrographs):
            max_index = len(micrographs)
        should_be_inside = set(range(index + 1, max_index))
        is_inside = set([tuble[1] for tuble in img_buffer])
        load = should_be_inside - is_inside
        if len(img_buffer) == 0:
            load.add(index)
        for index_to_load in load:
            filename = str(micrographs[index_to_load])
            image = EMAN2.EMData(filename)

            fft_img = image.do_fft()

            fft_img.set_value_at(0, 0, 0, 0)
            fft_img.set_value_at(1, 0, 0, 0)
            fft_img.process_inplace("xform.phaseorigin.tocorner")
            fft_img.process_inplace("xform.fourierorigin.tocenter")
            fft_img = fft_img.get_fft_amplitude()

            img_buffer.append((filename, index_to_load, image, fft_img))
            print("Put new image:", filename)
        time.sleep(1)
        '''
示例#7
0
def numpy2em(im, e=None):
    '''Convert NumPy array to an EMAN2 image object
    
    This convenience method converts a numpy.ndarray object into an EMAN2.EMData
    
    .. sourcecode:: py
    
        >>> from core.image.eman2_utility import *
        >>> ar = numpy.zeros((2,2))
        >>> numpy2em(ar)
        <libpyEMData2.EMData object at 0xdd61b0>
    
    :Parameters:

    img : numpy.ndarray
          A numpy array
        
    :Returns:
        
    return_val : EMAN2.EMData
                An EMAN2 image object
    '''

    if EMAN2 is None:
        raise ImportError, "EMAN2/Sparx library not available, numpy2em requires EMAN2/Sparx"
    try:
        im = numpy.require(im, numpy.float32)
        if e is None: e = EMAN2.EMData()
        EMAN2.EMNumPy.numpy2em(im, e)
        return e
    except:
        return EMAN2.EMNumPy.numpy2em(im)
示例#8
0
def create_particle_stack(particle_stack, output_dir, particle_data):
    """
	Create a mrcs particle stack based on the bdb/hdf input stack.

	Arguments:
	particle_stack - Particle stack name
	output_dir - Output directory
	particle_data - Particle_data array

	Returns:
	None
	"""
    print('|_Get particle ID and particle names')
    ptcl_ids = [
        int(entry.split('@')[0]) for entry in particle_data['_rlnImageName']
    ]
    ptcl_names = [
        entry.split('@')[1] for entry in particle_data['_rlnImageName']
    ]

    print('|_Write images')
    for particle_idx in range(particle_data.shape[0]):
        if particle_idx % 10000 == 0:
            print(particle_idx, ' of ', particle_data.shape[0])
        emdata = EMAN2.EMData(particle_stack, particle_idx)

        output_name = os.path.join(output_dir, ptcl_names[particle_idx])
        try:
            os.makedirs(os.path.dirname(output_name))
        except OSError:
            pass
        emdata.write_image(output_name, -1)
示例#9
0
def backproject_nn4_new(img, align=None, recon=None, **extra):
    ''' Add the given image and alignment or generator of image/alignment pairs
    to the current reconstruction
    
    :Parameters:
    
    img : array or EMData
          Image of projection to backproject into reconstruction
    align : array, optional
            Array of alignment parameters (not required if img is generator of images and alignments)
    recon : tuple
            Reconstructor, Fourier volume, Weight Volume, and numpy versions
    extra : dict
            Keyword arguments to be passed to setup_nn4 if recon is None
    
    :Returns:
    
    recon : tuple
            Reconstructor, Fourier volume, Weight Volume, and numpy versions
    '''

    if EMAN2 is None:
        raise ImportError, "EMAN2/Sparx library not available, backproject_nn4_new requires EMAN2/Sparx"
    npad, sym, weighting = extra.get('npad', 2), extra.get('sym',
                                                           'c1'), extra.get(
                                                               'weighting', 1)
    e = EMAN2.EMData()
    if not hasattr(img, 'ndim'):
        for i, val in img:
            a = align[i]
            '''
            if a[4] != (i+1):
                _logger.error("mismatch: %d != %d -- %s"%(a[4], i+1, str(align[:5])))
            assert(a[4]==(i+1))
            '''
            xform_proj = EMAN2.Transform({
                "type": "spider",
                "phi": a[2],
                "theta": a[1],
                "psi": a[0]
            })
            if not is_em(val): val = numpy2em(val, e)
            if recon is None:
                recon = setup_nn4(val.get_xsize(), npad, sym, weighting)
            recon[0][0].insert_slice(val, xform_proj)
    else:
        xform_proj = EMAN2.Transform({
            "type": "spider",
            "phi": align[2],
            "theta": align[1],
            "psi": align[0]
        })
        if not is_em(img): img = numpy2em(img)
        if recon is None:
            recon = setup_nn4(val.get_xsize(), npad, sym, weighting)
        recon[0][0].insert_slice(img, xform_proj)
    val1 = recon[1].copy()
    val2 = recon[2].copy()
    return val1, val2
示例#10
0
 def loadMicrographsFromItemList(self, image_index):
     """creates an EMImage-Widget"""
     if self.current_image is None:
         image_path = itemName[image_index]
         self.current_image = EMAN2.EMData(image_path)
     self.micrograph.set_data(
         self.current_image
     )  # in project descriptopn as 'aaa' instead of 'micrograph'
示例#11
0
 def createComponents(self):
     self.buttonLoad = QPushButton('Load')
     self.buttonSave = QPushButton('Save')
     self.buttonKeep = QPushButton('Keep (Right arrow key)')
     self.buttonKeep.setStyleSheet('QPushButton {color: green;}')
     self.buttonDiscard = QPushButton('Discard (Left arrow key)')
     self.buttonDiscard.setStyleSheet('QPushButton {color: red;}')
     self.fileList = QListWidget()
     self.micrograph = emimage2d.EMImage2DWidget(image=EMAN2.EMData(
         700, 700),
                                                 application=app,
                                                 parent=self)
     self.powerSpectrum = emimage2d.EMImage2DWidget(image=EMAN2.EMData(
         700, 700),
                                                    application=app,
                                                    parent=self)
     self.all_items = []
示例#12
0
def writeParticles():
    line = sys.stdin.readline()
    fnHdf = ""
    while line:
        objDict = json.loads(line)
        index = int(objDict['_index'])
        filename = str(objDict['_filename'])
        imageData = eman.EMData()
        imageData.read_image(filename, index)

        if '_ctfModel._defocusU' in objDict:
            ctf = eman.EMAN2Ctf()
            defU = objDict['_ctfModel._defocusU']
            defV = objDict['_ctfModel._defocusV']

            ctf.from_dict({"defocus": (defU + defV) / 20000.0,
                           "dfang": objDict['_ctfModel._defocusAngle'],
                           "dfdiff": (defU - defV) / 10000.0,
                           "voltage": objDict['_acquisition._voltage'],
                           "cs": max(objDict['_acquisition._sphericalAberration'], 0.0001),
                           "ampcont": objDict['_acquisition._amplitudeContrast'] * 100.0,
                           "apix": objDict['_samplingRate']})
            imageData.set_attr('ctf', ctf)

        imageData.set_attr('apix_x', objDict['_samplingRate'])
        imageData.set_attr('apix_y', objDict['_samplingRate'])

        transformation = None
        if '_angles' in objDict:
            # TODO: convert to vector not matrix
            angles = objDict['_angles']
            shifts = objDict['_shifts']
            transformation = eman.Transform({"type": "spider",
                                             "phi": angles[0],
                                             "theta": angles[1],
                                             "psi": angles[2],
                                             "tx": -shifts[0],
                                             "ty": -shifts[1],
                                             "tz": -shifts[2],
                                             "mirror": 0,  # TODO: test flip
                                             "scale": 1.0})

        if transformation is not None:
            imageData.set_attr('xform.projection', transformation)

        outputFile = str(objDict['hdfFn'])
        if outputFile != fnHdf:
            i = 0
            fnHdf = outputFile

        imageData.write_image(outputFile, i,
                              eman.EMUtil.ImageType.IMAGE_HDF, False)
        i += 1
        print("OK")  # it is necessary to add newline
        sys.stdout.flush()
        line = sys.stdin.readline()
示例#13
0
    def emdata_header(self):
        """Get header information from EMAN2."""
        if not EMAN2:
            return

        # EMAN2 only works with str filenames; no unicode.
        img = EMAN2.EMData()
        img.read_image(str(self.filename), 0, True)
        header = img.get_attr_dict()
        return header
示例#14
0
def backproject_nn4_queue(qin,
                          qout,
                          shmem,
                          shape,
                          process_limit,
                          process_number,
                          align=None,
                          **extra):
    ''' Add the given image and alignment or generator of image/alignment pairs
    to the current reconstruction
    
    :Parameters:
    
    img : array or EMData
          Image of projection to backproject into reconstruction
    align : array, optional
            Array of alignment parameters (not required if img is generator of images and alignments)
    recon : tuple
            Reconstructor, Fourier volume, Weight Volume, and numpy versions
    extra : dict
            Keyword arguments to be passed to setup_nn4 if recon is None
    
    :Returns:
    
    recon : tuple
            Reconstructor, Fourier volume, Weight Volume, and numpy versions
    '''

    if EMAN2 is None:
        raise ImportError, "EMAN2/Sparx library not available, backproject_nn4_queue requires EMAN2/Sparx"
    npad, sym, weighting = extra.get('npad', 2), extra.get('sym',
                                                           'c1'), extra.get(
                                                               'weighting', 1)
    e = EMAN2.EMData()
    recon = None
    while True:
        pos = qin.get()
        if pos is None or pos[0] == -1:
            if hasattr(qin, "task_done"): qin.task_done()
            break
        pos, idx = pos
        a = align[idx]
        img = shmem[pos].reshape(shape)
        xform_proj = EMAN2.Transform({
            "type": "spider",
            "phi": a[2],
            "theta": a[1],
            "psi": a[0]
        })
        if not is_em(img): img = numpy2em(img, e)
        if recon is None:
            recon = setup_nn4(img.get_xsize(), npad, sym, weighting)
        recon[0][0].insert_slice(img, xform_proj)
        qout.put((process_number, idx))
    return recon[0], recon[1], recon[2]
示例#15
0
def read_particle(path, index, header_only=False):
    """
	Reads single particle from stack.
	:param path: Stack from path
	:param index: Index of the particle in the stack
	:param header_only: If true, only the header information will be read
	:return: 2D particle image
	"""
    particle_image = EMAN2.EMData()
    particle_image.read_image(path, index, header_only)
    return particle_image
示例#16
0
def stackToHDF(infile, outfile, apix, pinfo=None):
    """
	convert stack to an hdf stack
	pinfo contains CTF information from apFrealign.getPerParticleCTF
	pinfo may also contain helical information
	"""
    from utilities import generate_ctf

    a = EMAN2.EMData()
    imn = EMAN2.EMUtil.get_image_count(infile)

    if pinfo is not None:
        if len(pinfo) != imn:
            apDisplay.printError("insufficient particle info for stack")

    # output must end with hdf
    outf, ext = os.path.splitext(outfile)
    if ext != '.hdf':
        outstack = outf + ".hdf"

    apDisplay.printMsg("Generating '%s' with %i particles" % (outstack, imn))

    for i in xrange(imn):
        a.read_image(infile, i)
        a.set_attr_dict({'active': 1})
        t2 = EMAN2.Transform({
            "type": "spider",
            "phi": 0,
            "theta": 0,
            "psi": 0
        })
        a.set_attr("xform.projection", t2)
        a.set_attr("apix_x", apix)

        if pinfo is not None:
            pdata = pinfo[i + 1]
            df1 = pdata['df1']
            df2 = pdata['df2']
            astig = pdata['angastig']
            kv = pdata['kev']
            cs = pdata['cs']
            ampc = pdata['ampc'] * 100

            # save CTF dict (bfactor is hard coded to 0.0)
            df = (float(df1) + float(df2)) / 2
            ctfgen = generate_ctf([df, cs, kv, apix, 0.0, ampc])
            a.set_attr("ctf", ctfgen)

            # if helical info is present
            if pdata['hnum'] is not None:
                a.set_attr("h_angle", pdata['hangle'])
        a.write_image(outstack, i)

    return outstack
示例#17
0
def readstack(n):
	"read the data for the nth particle. Return ref,ptclframes,psref"
	ref=EMAN2.EMData("tmp2.hdf",n*33)
	ptcls=EMAN2.EMData.read_images("tmp2.hdf",list(range(n*33+1,(n+1)*33)))
	ctfim=ref.do_fft()
	ctf=ref["ctf"]
	ctf.compute_2d_complex(ctfim,EMAN2.Ctf.CtfType.CTF_POWEVAL)
	ctfim.ri2ap()
	ctfim=ctfim.amplitude()
#	ctfim.process_inplace("normalize")

	return ref,ptcls,ctfim
示例#18
0
def check_output_format(input_var, filename):
    out_data_3d = EMAN2.EMData(10, 10, 10)
    out_data_3d += 1
    try:
        out_data_3d.write_image("test_" + filename)
    except Exception as e:
        msg = "The parameter '" + input_var + "' has a not valid extension or the file is not writable. Actual namefile is: '" + filename
        sp_global_def.ERROR(msg, action=1)
        return False

    # i cannot just remove it because in case of multiple cpu use it could crash
    try:
        os.remove("test_" + filename)
    except Exception as useless_e:
        pass
    return True
示例#19
0
    def preload_images(self, micrograph_list, img_buffer):
        """
        Preloads IMG_BUFFER_SIZE number of images into the memory.

        :param micrograph_list: list of all micrographs ######## mistake --> here it gets just a string not a list
        :param img_buffer: deque as buffer for images
        :return: None
        """
        print("preload_images micrograph_list", micrograph_list)
        offset = 0
        last_index = -1
        while True:
            index = self.fileList.row(self.fileList.currentItem())
            if last_index != -1:
                if index - last_index == 1:
                    offset = offset - 1
                elif index - last_index != 0:
                    offset = 0

            if len(img_buffer) < IMG_BUFFER_SIZE and (
                    index + offset) < len(micrograph_list):
                start = time.time()
                print("in", index + offset)
                print("micrograph_list", micrograph_list)
                filename = str(micrograph_list[index + offset])

                print("filename", filename)
                image = EMAN2.EMData(filename)

                fft_img = image.do_fft()

                fft_img.set_value_at(0, 0, 0, 0)
                fft_img.set_value_at(1, 0, 0, 0)
                fft_img.process_inplace("xform.phaseorigin.tocorner")
                fft_img.process_inplace("xform.fourierorigin.tocenter")
                fft_img = fft_img.get_fft_amplitude()

                img_buffer.append((filename, index + offset, image, fft_img))
                end = time.time()
                print("Put new image:", filename, "Current index", index,
                      "Image+offset:", index + offset, "offset", offset,
                      "time", end - start)
                offset = offset + 1
            else:
                time.sleep(1)

            last_index = index
示例#20
0
 def create_raw_hdf(self, outfile):
     # This is a very expensive process and may take a while.
     # This runs in the main thread :( Blocks the UI. Must fix.
     files = self.find('RawImage')
     if not files:
         return
     self.log("Creating raw HDF with %s frames: %s" % (len(files), outfile))
     outfile = str(outfile)
     # Sort the raw frames
     files = sorted(files,
                    key=lambda x: int(x.split("_")[-1].split(".")[0]))
     for f in files:
         f = str(f)
         self.log("Adding %s to raw HDF" % (os.path.basename(f)))
         e = EMAN2.EMData()
         e.read_image(f)
         e['render_min'] = -1
         e['render_max'] = 256 * 256
         e.write_image(outfile, -1, EMAN2.EMUtil.ImageType.IMAGE_HDF, False,
                       None, EMAN2.EMUtil.EMDataType.EM_USHORT, False)
示例#21
0
def import_particle_stack(particle_stack, output_dir):
    """
	Import the particle stack.

	Arguments:
	particle_stack - Path to the particle stack

	Returns:
	Particle array
	"""
    particle_header = EMAN2.EMData()
    particle_header.read_image(particle_stack, 0, True)

    dtype_list, name_list = create_stack_dtype(particle_header.get_attr_dict())

    header_dict = {}
    for name in name_list:
        header_dict[name] = np.array(
            EMAN2.EMUtil.get_all_attributes(particle_stack, name))

    stack_array, create_stack = create_stack_array(dtype_list, header_dict,
                                                   output_dir)

    return stack_array, create_stack
示例#22
0
def classify_by_names(names, model):
    '''
    With a given sklearn model and list of paths to mrc files
    predicts label for each name
    
    Args:
         names - list of paths to mrc files
         model - sklearn model instance.
                 (should predict classes not probabilities)
                 
    returns - dict like name:label
    '''
    samples = []
    for name in names:
        img = EMAN2.EMData(name, 0)
        img_np = EMAN2.EMNumPy.em2numpy(img)
        sample = ft.preprocess_img(img_np)
        samples.append(sample)

    preds = model.predict(samples)

    names_preds_dict = {name: label for name, label in zip(names, preds)}

    return names_preds_dict
示例#23
0
def writeParticles():
    line = sys.stdin.readline()
    fnHdf = ""
    while line:
    #for line in sys.stdin:
        objDict=json.loads(line)
        ###imgId, index, filename = line.split()
        if '_index' in objDict.keys():
            index = int(objDict['_index'])
            
        if '_filename' in objDict.keys():
            filename = str(objDict['_filename'])
        else:
            raise Exception('ERROR (e2converter): Cannot process a particle without filename')
        imageData = eman.EMData()
        imageData.read_image(filename, index)
        
        ctf = None
        if '_ctfModel._defocusU' in objDict.keys():
            ctf = eman.EMAN2Ctf()
            
            defU = objDict['_ctfModel._defocusU']
            defV = objDict['_ctfModel._defocusV']
            
            ctf.from_dict({"defocus": (defU + defV)/20000.0,
                           "dfang": objDict['_ctfModel._defocusAngle'],
                           "dfdiff": (defU - defV)/10000.0,
                           "voltage": objDict['_acquisition._voltage'],
                           "cs": objDict['_acquisition._sphericalAberration'],
                           "ampcont": objDict['_acquisition._amplitudeContrast'] * 100.0,
                           "apix": objDict['_samplingRate']})
            imageData.set_attr('ctf', ctf)
        
        transformation = None
        if '_angles' in objDict.keys():
            #TODO: convert to vector not matrix
            angles = objDict['_angles']
            shifts = objDict['_shifts']
            transformation = eman.Transform({"type": "spider",
                                             "phi": angles[0],
                                             "theta": angles[1],
                                             "psi": angles[2],
                                             "tx": shifts[0],
                                             "ty": shifts[1],
                                             "tz": shifts[2],
                                             "mirror": 0,  ####TODO: test flip
                                             "scale": 1.0})
        
        if transformation is not None:
            imageData.set_attr('xform.projection', transformation)
        
        outputFile = str(objDict['hdfFn'])
        if outputFile != fnHdf:
            i = 0
            fnHdf = outputFile

        imageData.write_image(outputFile, i, eman.EMUtil.ImageType.IMAGE_HDF, False)
        i += 1
        print "OK"
        sys.stdout.flush()
        line = sys.stdin.readline()
    print "DONE"
示例#24
0
def mode_meridien(reconfile,
                  classavgstack,
                  classdocs,
                  partangles,
                  selectdoc,
                  maxshift,
                  outerrad,
                  outanglesdoc,
                  outaligndoc,
                  interpolation_method=1,
                  outliers=None,
                  goodclassparttemplate=None,
                  alignopt='apsh',
                  ringstep=1,
                  log=None,
                  verbose=False):

    # Resample reference
    recondata = EMAN2.EMData(reconfile)
    idim = recondata['nx']
    reconprep = prep_vol(recondata,
                         npad=2,
                         interpolation_method=interpolation_method)

    # Initialize output angles
    outangleslist = []
    outalignlist = []

    # Read class lists
    classdoclist = glob.glob(classdocs)
    partangleslist = read_text_row(partangles)

    # Loop through class lists
    for classdoc in classdoclist:  # [classdoclist[32]]:  #
        # Strip out three-digit filenumber
        classexample = os.path.splitext(classdoc)
        classnum = int(classexample[0][-3:])

        # Initial average
        [avg_phi_init, avg_theta_init] = average_angles(partangleslist,
                                                        classdoc,
                                                        selectdoc=selectdoc)

        # Look for outliers
        if outliers:
            [avg_phi_final, avg_theta_final] = average_angles(
                partangleslist,
                classdoc,
                selectdoc=selectdoc,
                init_angles=[avg_phi_init, avg_theta_init],
                threshold=outliers,
                goodpartdoc=goodclassparttemplate.format(classnum),
                log=log,
                verbose=verbose)
        else:
            [avg_phi_final, avg_theta_final] = [avg_phi_init, avg_theta_init]

        # Compute re-projection
        refprjreal = prgl(reconprep, [avg_phi_final, avg_theta_final, 0, 0, 0],
                          interpolation_method=1,
                          return_real=True)

        # Align to class average
        classavg = get_im(classavgstack, classnum)

        # Alignment using self-correlation function
        if alignopt == 'scf':
            ang_align2d, sxs, sys, mirrorflag, peak = align2d_scf(classavg,
                                                                  refprjreal,
                                                                  maxshift,
                                                                  maxshift,
                                                                  ou=outerrad)

        # Weird results
        elif alignopt == 'align2d':
            # Set search range
            currshift = 0
            txrng = tyrng = search_range(idim, outerrad, currshift, maxshift)

            # Perform alignment
            ang_align2d, sxs, sys, mirrorflag, peak = align2d(
                classavg, refprjreal, txrng, tyrng, last_ring=outerrad)

        # Direct3 (angles seemed to be quantized)
        elif alignopt == 'direct3':
            [[ang_align2d, sxs, sys, mirrorflag,
              peak]] = align2d_direct3([classavg],
                                       refprjreal,
                                       maxshift,
                                       maxshift,
                                       ou=outerrad)

        # APSH-like alignment (default)
        else:
            [[ang_align2d, sxs, sys, mirrorflag,
              scale]] = apsh(refprjreal,
                             classavg,
                             outerradius=outerrad,
                             maxshift=maxshift,
                             ringstep=ringstep)

        outalignlist.append([ang_align2d, sxs, sys, mirrorflag, 1])
        msg = "Particle list %s: ang_align2d=%s sx=%s sy=%s mirror=%s\n" % (
            classdoc, ang_align2d, sxs, sys, mirrorflag)
        print_log_msg(msg, log, verbose)

        # Check for mirroring
        if mirrorflag == 1:
            tempeulers = list(
                compose_transform3(avg_phi_final, avg_theta_final, 0, 0, 0, 0,
                                   1, 0, 180, 0, 0, 0, 0, 1))
            combinedparams = list(
                compose_transform3(tempeulers[0], tempeulers[1], tempeulers[2],
                                   tempeulers[3], tempeulers[4], 0, 1, 0, 0,
                                   -ang_align2d, 0, 0, 0, 1))
        else:
            combinedparams = list(
                compose_transform3(avg_phi_final, avg_theta_final, 0, 0, 0, 0,
                                   1, 0, 0, -ang_align2d, 0, 0, 0, 1))
        # compose_transform3: returns phi,theta,psi, tx,ty,tz, scale

        outangleslist.append(combinedparams)
    # End class-loop

    write_text_row(outangleslist, outanglesdoc)
    write_text_row(outalignlist, outaligndoc)
    print_log_msg(
        'Wrote alignment parameters to %s and %s\n' %
        (outanglesdoc, outaligndoc), log, verbose)

    del recondata  # Clean up
示例#25
0
def main_proj_compare(classavgstack,
                      reconfile,
                      outdir,
                      options,
                      mode='viper',
                      prjmethod='trilinear',
                      classangles=None,
                      partangles=None,
                      selectdoc=None,
                      verbose=False,
                      displayYN=False):
    """
	Main function overseeing various projection-comparison modes.
	
	Arguments:
		classavgstack : Input image stack
		reconfile : Map of which to generate projections (an optionally perform alignment)
		outdir : Output directory
		mode : Mode, viper (pre-existing angles for each input image), projmatch (angles from internal projection-matching)
		verbose : (boolean) Whether to write additional information to screen
		options : (list) Command-line options, run 'sxproj_compare.py -h' for an exhaustive list
		classangles : Angles and shifts for each input class average
		partangles : Angles and shifts for each particle (mode meridien)
		selectdoc : Selection file for included images
		prjmethod : Interpolation method to use
		displayYN : (boolean) Whether to automatically open montage
	"""

    # Expand path for outputs
    refprojstack = os.path.join(outdir, 'refproj.hdf')
    refanglesdoc = os.path.join(outdir, 'refangles.txt')
    outaligndoc = os.path.join(outdir, 'docalign2d.txt')

    # If not an input, will create an output, in modes projmatch
    if classangles == None:
        classangles = os.path.join(outdir, 'docangles.txt')

        # You need either input angles (mode viper) or to calculate them on the fly (mode projmatch)
        if mode == 'viper':
            sp_global_def.ERROR(
                "\nERROR!! Input alignment parameters not specified.",
                __file__, 1)
            sxprint('Type %s --help to see available options\n' %
                    os.path.basename(__file__))
            exit()

    # Check if inputs exist
    check(classavgstack, verbose=verbose)
    check(reconfile, verbose=verbose)
    if verbose: sxprint('')

    # Check that dimensions of images and volume agree (maybe rescale volume)
    voldim = EMAN2.EMData(reconfile).get_xsize()
    imgdim = EMAN2.EMData(classavgstack, 0).get_xsize()
    if voldim != imgdim:
        sp_global_def.ERROR(
            "\nERROR!! Dimension of input volume doesn't match that of image stack: %s vs. %s"
            % (voldim, imgdim), __file__, 1)

        scale = float(
            imgdim
        ) / voldim  # only approximate, since full-sized particle radius is arbitrary
        msg = 'The command to resize the volume will be of the form:\n'
        msg += 'e2proc3d.py %s resized_vol.hdf --scale=%1.5f --clip=%s,%s,%s\n' % (
            reconfile, scale, imgdim, imgdim, imgdim)
        msg += 'Check the file in the ISAC directory named "README_shrink_ratio.txt" for confirmation.\n'
        sxprint(msg)
        exit()

    #  Here if you want to be fancy, there should be an option to chose the projection method,
    #  the mechanism can be copied from sxproject3d.py  PAP
    if prjmethod == 'trilinear':
        method_num = 1
    elif prjmethod == 'gridding':
        method_num = -1
    elif prjmethod == 'nn':
        method_num = 0
    else:
        sp_global_def.ERROR(
            "\nERROR!! Valid projection methods are: trilinear (default), gridding, and nn (nearest neighbor).",
            __file__, 1)
        sxprint('Usage:\n%s' % USAGE)
        exit()

    # Set output directory and log file name
    log, verbose = prepare_outdir_log(outdir, verbose)

    # In case class averages include discarded images, apply selection file
    if mode == 'viper':
        if selectdoc:
            goodavgs, extension = os.path.splitext(
                os.path.basename(classavgstack))
            newclasses = os.path.join(outdir, goodavgs + "_kept" + extension)

            # e2proc2d appends to existing files, so rename existing output
            if os.path.exists(newclasses):
                renamefile = newclasses + '.bak'
                print_log_msg(
                    "Selected-classes stack %s exists, renaming to %s" %
                    (newclasses, renamefile), log, verbose)
                print_log_msg("mv %s %s\n" % (newclasses, renamefile), log,
                              verbose)
                os.rename(newclasses, renamefile)

            print_log_msg(
                'Creating subset of %s to %s based on selection list %s' %
                (classavgstack, newclasses, selectdoc), log, verbose)
            cmd = "e2proc2d.py %s %s --list=%s" % (classavgstack, newclasses,
                                                   selectdoc)
            print_log_msg(cmd, log, verbose)
            os.system(cmd)
            sxprint('')

            # Update class-averages
            classavgstack = newclasses

    # align de novo to reference map
    if mode == 'projmatch':
        # Generate reference projections
        print_log_msg(
            'Projecting %s to output %s using an increment of %s degrees using %s symmetry'
            % (reconfile, refprojstack, options.delta, options.symmetry), log,
            verbose)
        cmd = 'sxproject3d.py %s %s --delta=%s --method=S --phiEqpsi=Minus --symmetry=%s' % (
            reconfile, refprojstack, options.delta, options.symmetry)
        if options.prjmethod == 'trilinear': cmd += ' --trilinear'
        cmd += '\n'
        print_log_msg(cmd, log, verbose)
        project3d(reconfile,
                  refprojstack,
                  delta=options.delta,
                  symmetry=options.symmetry)

        # Export projection angles
        print_log_msg(
            "Exporting projection angles from %s to %s" %
            (refprojstack, refanglesdoc), log, verbose)
        cmd = "sp_header.py %s --params=xform.projection --import=%s\n" % (
            refprojstack, refanglesdoc)
        print_log_msg(cmd, log, verbose)
        header(refprojstack, 'xform.projection', fexport=refanglesdoc)

        # Perform multi-reference alignment
        if options.align == 'ali2d':
            projdir = os.path.join(
                outdir, 'Projdir')  # used if input angles no provided
            if os.path.isdir(projdir):
                print_log_msg('Removing pre-existing directory %s' % projdir,
                              log, verbose)
                print_log_msg('rm -r %s\n' % projdir, log, verbose)
                shutil.rmtree(
                    projdir)  # os.rmdir only removes empty directories

            # Zero out alignment parameters in header
            print_log_msg(
                'Zeroing out alignment parameters in header of %s' %
                classavgstack, log, verbose)
            cmd = 'sxheader.py %s --params xform.align2d --zero\n' % classavgstack
            print_log_msg(cmd, log, verbose)
            header(classavgstack, 'xform.align2d', zero=True)

            # Perform multi-reference alignment
            msg = 'Aligning images in %s to projections %s with a radius of %s and a maximum allowed shift of %s' % (
                classavgstack, refprojstack, options.matchrad,
                options.matchshift)
            print_log_msg(msg, log, verbose)
            cmd = 'sxmref_ali2d.py %s %s %s --ou=%s --xr=%s --yr=%s\n' % (
                classavgstack, refprojstack, projdir, options.matchrad,
                options.matchshift, options.matchshift)
            print_log_msg(cmd, log, verbose)
            mref_ali2d(classavgstack,
                       refprojstack,
                       projdir,
                       ou=options.matchrad,
                       xrng=options.matchshift,
                       yrng=options.matchshift)

            # Export alignment parameters
            print_log_msg(
                'Exporting angles from %s into %s' %
                (classavgstack, classangles), log, verbose)
            cmd = "sp_header.py %s --params=xform.align2d --export=%s\n" % (
                classavgstack, classangles)
            print_log_msg(cmd, log, verbose)
            header(classavgstack, 'xform.align2d', fexport=classangles)

        # By default, use AP SH
        else:
            apsh(refprojstack,
                 classavgstack,
                 outangles=classangles,
                 refanglesdoc=refanglesdoc,
                 outaligndoc=outaligndoc,
                 outerradius=options.matchrad,
                 maxshift=options.matchshift,
                 ringstep=options.matchstep,
                 log=log,
                 verbose=verbose)

        # Diagnostic
        alignlist = read_text_row(
            classangles)  # contain 2D alignment parameters
        nimg1 = EMAN2.EMUtil.get_image_count(classavgstack)
        assert len(alignlist) == nimg1, "MRK_DEBUG"

    # Get alignment parameters from MERIDIEN
    if mode == 'meridien':
        continueTF = True  # Will proceed unless some information is missing

        if not partangles:
            sp_global_def.ERROR(
                "\nERROR!! Input alignment parameters not provided.", __file__,
                1)
            continueTF = False

        if not continueTF:
            sxprint('Type %s --help to see available options\n' %
                    os.path.basename(__file__))
            exit()

        if not options.classdocs or options.outliers:
            classdir = os.path.join(outdir, 'Byclass')
            if not os.path.isdir(classdir): os.makedirs(classdir)

            if options.outliers:
                goodclassparttemplate = os.path.join(
                    classdir, 'goodpartsclass{0:03d}.txt')
            else:
                goodclassparttemplate = None

            if not options.classdocs:
                classmap = os.path.join(classdir, 'classmap.txt')
                classdoc = os.path.join(classdir, 'docclass{0:03d}.txt')
                options.classdocs = os.path.join(classdir, 'docclass*.txt')

                # Separate particles by class
                vomq(classavgstack,
                     classmap,
                     classdoc,
                     log=log,
                     verbose=verbose)

        mode_meridien(reconfile,
                      classavgstack,
                      options.classdocs,
                      partangles,
                      selectdoc,
                      options.refineshift,
                      options.refinerad,
                      classangles,
                      outaligndoc,
                      interpolation_method=method_num,
                      outliers=options.outliers,
                      goodclassparttemplate=goodclassparttemplate,
                      alignopt=options.align,
                      ringstep=options.refinestep,
                      log=log,
                      verbose=verbose)

    # Import Euler angles
    print_log_msg(
        "Importing parameter information into %s from %s" %
        (classavgstack, classangles), log, verbose)
    cmd = "sp_header.py %s --params=xform.projection --import=%s\n" % (
        classavgstack, classangles)
    print_log_msg(cmd, log, verbose)
    header(classavgstack, 'xform.projection', fimport=classangles)

    # Make comparison stack between class averages (images 0,2,4,...) and re-projections (images 1,3,5,...)
    compstack = compare_projs(reconfile,
                              classavgstack,
                              classangles,
                              outdir,
                              interpolation_method=method_num,
                              log=log,
                              verbose=verbose)

    # Optionally pop up e2display
    if displayYN:
        sxprint('Opening montage')
        cmd = "e2display.py %s\n" % compstack
        sxprint(cmd)
        os.system(cmd)

    sxprint("Done!")
示例#26
0
def compare_projs(reconfile,
                  classavgstack,
                  inputanglesdoc,
                  outdir,
                  interpolation_method=1,
                  log=None,
                  verbose=False):
    """
	Make comparison stack between class averages (even-numbered (starts from 0)) and re-projections (odd-numbered).
	
	Arguments:
		reconfile : Input volume from which to generate re-projections
		classavgstack ; Input image stack
		inputanglesdoc : Input Euler angles doc
		outdir ; Output directory
		interpolation_method : Interpolation method: nearest neighbor (nn, 0), trilinear (1, default), gridding (-1)
		log : Logger object
		verbose : (boolean) Whether to write additional information to screen
	Returns:
		compstack : Stack of comparisons between input image stack (even-numbered (starts from 0)) and input volume (odd-numbered)
	"""

    recondata = EMAN2.EMData(reconfile)
    nx = recondata.get_xsize()

    # Resample reference
    reconprep = prep_vol(recondata,
                         npad=2,
                         interpolation_method=interpolation_method)

    ccclist = []

    #  Here you need actual radius to compute proper ccc's, but if you do, you have to deal with translations, PAP
    mask = model_circle(nx // 2 - 2, nx, nx)
    mask.write_image(os.path.join(outdir, 'maskalign.hdf'))
    compstack = os.path.join(outdir, 'comp-proj-reproj.hdf')

    # Number of images may have changed
    nimg1 = EMAN2.EMUtil.get_image_count(classavgstack)
    angleslist = read_text_row(inputanglesdoc)

    for imgnum in range(nimg1):
        # Get class average
        classimg = get_im(classavgstack, imgnum)

        # Compute re-projection
        prjimg = prgl(reconprep,
                      angleslist[imgnum],
                      interpolation_method=1,
                      return_real=False)

        # Calculate 1D power spectra
        rops_dst = rops_table(classimg * mask)
        rops_src = rops_table(prjimg)

        #  Set power spectrum of reprojection to the data.
        #  Since data has an envelope, it would make more sense to set data to reconstruction,
        #  but to do it one would have to know the actual resolution of the data.
        #  you can check sxprocess.py --adjpw to see how this is done properly  PAP
        table = [0.0] * len(rops_dst)  # initialize table
        for j in range(len(rops_dst)):
            table[j] = sqrt(rops_dst[j] / rops_src[j])
        prjimg = fft(filt_table(
            prjimg,
            table))  # match FFT amplitudes of re-projection and class average

        cccoeff = ccc(prjimg, classimg, mask)
        #print imgnum, cccoeff
        classimg.set_attr_dict({'cross-corr': cccoeff})
        prjimg.set_attr_dict({'cross-corr': cccoeff})

        montagestack = []
        montagestack.append(prjimg)
        montagestack.append(classimg)
        comparison_pair = montage2(montagestack, ncol=2, marginwidth=1)
        comparison_pair.write_image(compstack, imgnum)

        ccclist.append(cccoeff)
    del angleslist
    meanccc = sum(ccclist) / nimg1
    print_log_msg("Average CCC is %s\n" % meanccc, log, verbose)

    nimg2 = EMAN2.EMUtil.get_image_count(compstack)

    for imgnum in range(nimg2):  # xrange will be deprecated in Python3
        prjimg = get_im(compstack, imgnum)
        meanccc1 = prjimg.get_attr_default('mean-cross-corr', -1.0)
        prjimg.set_attr_dict({'mean-cross-corr': meanccc})
        write_header(compstack, prjimg, imgnum)

    return compstack
示例#27
0
def read_mrc_numpy_vol(path):
    ve = E.EMData(str(path))
    v = em2numpy(ve)
    v = v.astype(N.float32)
    del ve  # to prevent memory leak
    return v
示例#28
0
def read_hdf_numpy_vol(path):
    ve = E.EMData(str(path))
    return em2numpy(ve)
    for root, _, files in os.walk(stack_dir):
        files = [fi for fi in files if fi.endswith(".hdf")]
        print("Processing {} files in {}".format(len(files), root))
        for hdf_file in files:
            stack = os.path.join(root, hdf_file)
            output = os.path.join(
                output_dir,
                os.path.splitext(os.path.basename(hdf_file))[0] + ".csv")
            try:
                count = EMAN2.EMUtil.get_image_count(stack)
            except:
                print("Skipping: {}".format(stack))
                # Input is likely decimated coordinates, so try to undecimate
                fout = open(output, 'w')
                fin = open(stack, 'r')
                for line in fin:
                    coords = numpy.asarray([int(v) for v in line.split()])
                    fout.write("\t".join(["%d" % int(c * 8)
                                          for c in coords]) + "\n")
                fout.close()
                fin.close()
                continue
            fout = open(output, 'w')
            for i in xrange(count):
                aimg = EMAN2.EMData()
                aimg.read_image(stack, i)
                coords = numpy.asarray(aimg['ptcl_source_coord'])
                fout.write("\t".join(["%d" % int(c * 8)
                                      for c in coords]) + "\n")
            fout.close()
示例#30
0
    norm = False

    coords = numpy.asarray([[int(val) for val in line.split()]
                            for line in open(partcoords, 'r')])

    #get_trans
    #get_rotation_transform

    for i in xrange(len(coords)):
        print i + 1, len(coords)
        x, y, z = coords[i]
        x = round(x * cshrink)
        y = round(y * cshrink)
        z = round(z * cshrink)
        r = EMAN2.Region((2 * x - boxsize) / 2, (2 * y - boxsize) / 2,
                         (2 * z - boxsize) / 2, boxsize, boxsize, boxsize)
        e = EMAN2.EMData()
        e.read_image(tomogram, 0, False, r)
        if invert:
            e = e * -1
        if norm:
            e.process_inplace('normalize', {})
        e['origin_x'] = 0
        e['origin_y'] = 0
        e['origin_z'] = 0
        e.write_image(output, i)

        # norm, intervt, threshold

        #ptcl.process_inplace("xform",{"transform":ptcl_parms[0]["xform.align3d"]})