Beispiel #1
0
def radial(evt, type, key, mask=None, cx=None, cy=None):
    """Compute the radial average of a detector image given the center position (and a mask) and saves it to ``evt["analysis"]["radial average - " + key]`` and the radial distances are saved to``evt["analysis"]["radial distance - " + key]``

    Args:
        :evt:        The event variable
        :type(str):  The event type (e.g. photonPixelDetectors)
        :key(str):   The event key (e.g. CCD)

    Kwargs:
        :mask:    Binary mask, pixels that are masked out are not counted into the radial average.
        :cx(float):  X-coordinate of the center position. If None the center will be in the middle.
        :cy(float):  Y-coordinate of the center position. If None the center will be in the middle.

    :Authors:
        Max F. Hantke ([email protected])
    """
    import spimage
    image = evt[type][key].data
    r, img_r = spimage.radialMeanImage(image, msk=mask, cx=cx, cy=cy, output_r=True)
    valid = np.isfinite(img_r)
    if valid.sum() > 0:
        r = r[valid]
        img_r = img_r[valid]
    add_record(evt["analysis"], "analysis", "radial distance - "+key, r)
    add_record(evt["analysis"], "analysis", "radial average - "+key, img_r)
Beispiel #2
0
def q_factor(images,full_output=False,axis=0,mask=None):
    """
    Calcualtes the q factor for a set of images. [Hstau Y. Liao et al. Definition and estimation of resolution in single-particle reconstructions, Structure, 2010]

    Args:
        :images(float ndarray):        3d ndarray, which contains the images
        
    Kwargs:
        :full_output(bool):            gives the full output, default=False
        :axis(int):                    axis on which the images are located, default=0
    """

    # apply mask if neccessary
    if mask:
        images = images * mask
    else:
        mask = np.ones_like(images[0,:,:],dtype=np.int)

    # calculating the q value
    q_map = np.abs(np.sum(images,axis=axis)) / (np.abs(images)).sum(axis=axis)
    
    q_function = spimage.radialMeanImage(q_map)

    q_pure_noise = 1 / np.sqrt(images.shape[axis])

    if full_output:
        out={'q_map':q_map,
             'q_function':q_function,
             'noise_convergence':q_pure_noise}
        return out

    else:
        return q_map
Beispiel #3
0
def prtf_radial_average(prtf_dir, downsampling):
    prtf = spimage.sp_image_read(os.path.join(prtf_dir, 'PRTF-prtf.h5'), 0)
    s = np.shape(prtf.image)
    r, prtf_radavg = spimage.radialMeanImage(prtf.image,
                                             cx=0.,
                                             cy=0.,
                                             cz=0.,
                                             output_r=True)
    q = pix_to_q(r, 1.035e-9, 0.7317, 0.000075 * downsampling)
    q /= 1e09  #reciprocal nanometres
    q_edge = pix_to_q(s[0] / 2, 1.035e-9, 0.7317,
                      0.000075 * downsampling) / 1e09
    q_short = q[q <= q_edge]  #Truncate prtf at detector edge
    return prtf_radavg[:len(q_short)], q_short
Beispiel #4
0
def radial_average_q(file_name, downsampling):
    img = spimage.sp_image_read(file_name, 0)
    s = np.shape(img.image)
    r, radavg = spimage.radialMeanImage(img.image,
                                        cx=0.,
                                        cy=0.,
                                        cz=0.,
                                        output_r=True)
    q = pix_to_q(r, 1.035e-9, 0.7317, 0.000075 * downsampling)
    q /= 1e09  #reciprocal nanometres
    q_edge = pix_to_q(s[0] / 2, 1.035e-9, 0.7317,
                      0.000075 * downsampling) / 1e09
    q_short = q[q <= q_edge]  #Truncate prtf at detector edge
    return radavg[:len(q_short)], q_short
Beispiel #5
0
def phase_retieval_transfer_function(images,
                                     support,
                                     full_output=False,
                                     mask=None):
    """
    Calculates the phase retrieval transfer function by using the libspimage functions.

    Args:
        :images(float ndarray):        4d ndarray of real space images
        :support(bool ndarray):        4d ndarray of the support

    Kwargs:
        :full_output(bool):            gives the full output as a dictionary
        :mask(int ndarray):            provide a mask to be used for the radial mean function, default = None
    """

    # calulating the PRTF
    output_prtf = spimage.prtf(images, support, enantio=True, translate=True)
    prtf_3d = output_prtf['prtf']

    if mask is None:
        # preparing the mask for the radial average
        nx, ny, nz = prtf_3d.shape[2], prtf_3d.shape[1], prtf_3d.shape[0]
        xx, yy, zz = np.meshgrid(np.arange(nx), np.arange(ny), np.arange(nz))
        mask = np.sqrt((xx - nx / 2)**2 + (yy - ny / 2)**2 +
                       (zz - nz / 2)**2) < nx / 2

    prtf_centers, prtf_radial = spimage.radialMeanImage(prtf_3d,
                                                        msk=mask,
                                                        output_r=True)

    if full_output:
        out = {
            'prtf_3D_volume': prtf_3d,
            'prtf_radial': prtf_radial,
            'prtf_centers': prtf_centers
        }
        return out
    else:
        return prtf_3d
Beispiel #6
0
def radavg_from_file(file_name,
                     mode='absolute',
                     mask=False,
                     log_scale=False,
                     shift=False,
                     s='x',
                     plot=False,
                     do_return=False):
    im = spimage.sp_image_read(file_name, 0)
    if shift:
        im = spimage.sp_image_shift(im)
    dim = len(np.shape(im.image))
    if dim == 3 and s == 'x':
        sl = (np.shape(im.image)[0] / 2, slice(None), slice(None))
    elif dim == 3 and s == 'y':
        sl = (slice(None), np.shape(im.image)[1] / 2, slice(None))
    elif dim == 3 and s == 'z':
        sl = (slice(None), slice(None), np.shape(im.image)[2] / 2)
    elif dim == 2:
        sl = (slice(None), slice(None))

    if mask:
        msk = im.mask
    if not mask:
        msk = np.ones_like(im.image)

    if mode == 'absolute': f = np.absolute
    elif mode == 'angle': f = np.angle
    elif mode == 'real': f = np.real
    elif mode == 'imag': f = np.imag

    radavg = spimage.radialMeanImage(
        f(im.image[sl]), msk=msk[sl]
    )  #cx=im.detector.image_center[0], cy=im.detector.image_center[1])
    if log_scale:
        radavg = log10(radavg)
    if plot:
        plt.plot(radavg)
    if do_return:
        return radavg
                                   blur_radius=centering_blur)
        #print("Step 1: Found center position (%.2f, %2.f)" %(x,y))

        # Add spherical mask in the center (restricting the sizing to low q)
        mask_sizing = mask & ~rmask(maskradius, mask.shape, mask.shape[1] / 2 +
                                    x, mask.shape[0] / 2 + y)

        #plt.figure(figsize=(5,5), dpi=100)
        #plt.axis('off')
        #plt.imshow(assembled_sorted[j]*cmask*mask_sizing, norm=colors.LogNorm(), cmap='magma')
        #plt.show()

        # Step 2. Radial average
        centers, radial = spimage.radialMeanImage(assembled,
                                                  msk=mask,
                                                  cx=mask.shape[1] / 2 + x,
                                                  cy=mask.shape[0] / 2 + y,
                                                  output_r=True)
        data_r = radial[:sh[0] // 2]
        data_qr = spimage.x_to_qx(centers, pixelsize, distance)[:sh[0] // 2]
        mask_qr = data_qr > 35

        # Step 3. Fitting size/intensity
        output = optimize.brute(costfunc1, [(1e-9, 301e-9)],
                                args=(data_r, mask_qr, data_qr),
                                Ns=300,
                                full_output=True)
        diameter = output[0]
        pearson = output[1]
        res = optimize.minimize(costfunc2, [intensity_start],
                                args=(data_r, mask_qr, diameter, data_qr),
Beispiel #8
0
for n in range(N):
    for m in range(M):
        if n == 0:
            x = 0
            y = m
            mosaic[y * 32:(y + 1) * 32 - 1, x * 31:x * 31 + 31] = refsubimage.T
        x = n + 1
        y = m
        subimage = images[n][m, 128 - 15:128 + 16, 128 - 15:128 + 16]
        mosaic[y * 32:(y + 1) * 32 - 1, x * 31:x * 31 + 31] = subimage.T
        pattern = np.fft.ifftshift(
            abs(
                np.fft.fft2(images[n][m]) *
                (np.sqrt(f2) if args.f2[n] == 1 else 1)))
        centers, nom_radial = spimage.radialMeanImage(abs(pattern -
                                                          strucfactors),
                                                      output_r=True)
        centers, denom_radial = spimage.radialMeanImage(strucfactors,
                                                        output_r=True)
        r_radial = nom_radial / (denom_radial + 1e-9)

        MSEs[n, m] = np.linalg.norm(abs(subimage) -
                                    abs(refsubimage)) / np.linalg.norm(
                                        abs(refsubimage))
        Rs[n, m] = np.sum(abs(pattern - strucfactors)) / np.sum(strucfactors)
        if n == 0:
            vspattern = np.fft.fftshift(
                vs[m * 256:(m + 1) * 256, 0:256] *
                (np.sqrt(f2) if args.f2[n] == 1 else 1))
            centers, nom_radial_vs = spimage.radialMeanImage(abs(vspattern -
                                                                 strucfactors),