Esempio n. 1
0
def shiftImage(image, scale, angle, tvec):
    returnResult = np.zeros(image.shape)
    if len(image.shape) == 3:
        for index in range(0, image.shape[2]):
            x = np.arange(0, image.shape[1])
            y = np.arange(0, image.shape[0])
            #mask invalid values
            array = np.ma.masked_invalid(image[:, :, index])
            xx, yy = np.meshgrid(x, y)
            #get only the valid values
            x1 = xx[~array.mask]
            y1 = yy[~array.mask]
            temp = array[~array.mask]

            result = interpolate.griddata((x1, y1),
                                          temp.ravel(), (xx, yy),
                                          method='nearest')
            im = ird.transform_img(result.data,
                                   scale=scale,
                                   angle=angle,
                                   tvec=tvec,
                                   order=3)

            if image.dtype == np.dtype('uint16'):
                returnResult[:, :, index] = np.uint16(im)
            elif image.dtype == np.dtype('uint8'):
                returnResult[:, :, index] = np.uint8(im)
            else:
                returnResult[:, :, index] = im
    else:
        x = np.arange(0, image.shape[1])
        y = np.arange(0, image.shape[0])
        #mask invalid values
        array = np.ma.masked_invalid(image)
        xx, yy = np.meshgrid(x, y)
        #get only the valid values
        x1 = xx[~array.mask]
        y1 = yy[~array.mask]
        temp = array[~array.mask]

        result = interpolate.griddata((x1, y1),
                                      temp.ravel(), (xx, yy),
                                      method='nearest')
        im = ird.transform_img(result.data,
                               scale=scale,
                               angle=angle,
                               tvec=tvec,
                               order=3)
        if image.dtype == np.dtype('uint16'):
            returnResult[:, :] = np.uint16(im)
        elif image.dtype == np.dtype('uint8'):
            returnResult[:, :] = np.uint8(im)
        else:
            returnResult[:, :] = im

    return returnResult
Esempio n. 2
0
def imreg(im0, imk):
    import imreg_dft as ird
    from image_registration import chi2_shift
    from image_registration.fft_tools import shift
    xoff, yoff, exoff, eyoff = chi2_shift(im0, imk)
    timg = ird.transform_img(imk, tvec=np.array([-yoff, -xoff]))
    return timg
 def apply_shift(self, frames, shift, progress_callback):
     shifted_frames = []
     for frame_no, frame in enumerate(frames):
         progress_callback(frame_no / float(len(frames)))
         shifted_frames.append(ird.transform_img(frame, tvec=shift))
     progress_callback(1)
     return np.array(shifted_frames)
Esempio n. 4
0
 def definecontrols(self):
     dntpnames = ["dCTP","dATP","dGTP","dTTP"]
     dntps = [[],[],[],[]]
     zpro = [[],[],[],[]]
     
     dntpdirec = r'C:/Users/Meni/Robert/'
     fn = 'dntps.h5'
     hfile = h5py.File(dntpdirec + fn)
     for i,x in enumerate(dntpnames):
         fn = 'dntps.h5'
         zpro[i] = np.array(hfile[x]).astype(float)
         zpro[i] -=zpro[i].mean()
         zpro[i] /=zpro[i].std()
     
     composite = zpro[0] + zpro[1] + zpro[2] + zpro[3]
     ly, lx = composite.shape
     
     p0 = [composite.mean(1).max(), (ly/2)-4, (ly/2)+4, 1.]
     coeff1, var_matrix1 = curve_fit(dubgauss,np.linspace(0,ly-1, ly), composite.mean(1), p0=p0)
     p0 = [self.zpro.mean(1).max(), (ly/2)-4, (ly/2)+4, 1.]
     coeff2, var_matrix2 = curve_fit(dubgauss,np.linspace(0,ly-1, ly), self.zpro.mean(1), p0=p0)
     shifty = np.mean((coeff2[1], coeff2[2]))-np.mean((coeff1[1], coeff1[2]))
     
     p0 = [composite.mean(1).max(), lx/2, 1.]
     coeff1, var_matrix1 = curve_fit(gauss,np.linspace(0,lx-1, lx), composite.mean(0), p0=p0)
     p0 = [self.zpro.mean(1).max(), lx/2, 1.]
     coeff2, var_matrix2 = curve_fit(gauss,np.linspace(0,lx-1, lx), self.zpro.mean(0), p0=p0)
     shiftx = coeff2[1]-coeff1[1]
     
     self.czpro = [[],[],[],[]]
     for i,x in enumerate(dntps):
         self.czpro[i] = zpro[i]
         self.czpro[i] = ird.transform_img(self.czpro[i], tvec=[shifty,shiftx])
def RegisterToDummy(start, start_ref=None):
    #scale dummy mask with busbar oriented upper horizontal.

    dummy = np.zeros(start.shape)
    border = [int(np.round(x * 2 / 53)) for x in start.shape]
    busbar = int(np.round(start.shape[0] * 23 / 53))
    dummy[border[0]:-border[0], border[1]:-border[1]] = 0.05
    dummy[busbar:busbar + border[0], :] = 1

    if start_ref is None:
        start_ref = start

    start_gauss = gaussian(start_ref, sigma=0.5)
    result = ird.similarity(dummy,
                            start_gauss,
                            numiter=30,
                            constraints={
                                'angle': [0, 360],
                                'tx': [0, 2],
                                'ty': [0, 2],
                                'scale': [1, 0.02]
                            })

    start_reg = ird.transform_img(start,
                                  tvec=result['tvec'].round(4),
                                  angle=result['angle'],
                                  scale=result['scale'])

    return start_reg
Esempio n. 6
0
def tran():
    # basedir = os.path.join('.', 'examples/banklogo')
    # the TEMPLATE
    # im0 = sp.misc.imread(os.path.join(basedir, "moban.jpg"), True)  # float32
    # the image to be transformed
    # im1 = sp.misc.imread(os.path.join(basedir, "bxz1.jpg"), True)

    # im3 = cv2.imread(os.path.join(basedir, "moban.jpg"))
    # im3 = cv2.cvtColor(im3,cv2.COLOR_BGR2GRAY)  # unit8
    #
    # im4 = im0.astype(dtype=np.uint8)
    # cv2.imshow('im0', im4)
    # cv2.waitKey(0)

    result = ird.translation(im0, im1)

    tvec = result["tvec"].round(4)
    # the Transformed IMaGe.
    timg = ird.transform_img(im1, tvec=tvec)

    # Maybe we don't want to show plots all the time
    ird.imshow(im0, im1, timg)
    plt.show()

    print("Translation is {}, success rate {:.4g}".format(
        tuple(tvec), result["success"]))
    def analyze(self, blueThresh,redThresh,bluePeakThresh,redPeakThresh):
        self.blueThresh = blueThresh
        self.redThresh = redThresh
        self.bluePeakThresh = bluePeakThresh
        self.redPeakThresh = redPeakThresh
        if self.seqplotlist != [[]]  :
            for x in self.seqplotlist:
                self.p1.getRoiPlot().removeItem(x)
                self.p1.getRoiPlot().autoRange()
                
        if self.firingplotlist != []  :
            for x in self.firingplotlist:
                self.p1.getRoiPlot().removeItem(x)
                self.p1.getRoiPlot().autoRange()           

        self.seqplotlist = [[]]
        self.firingplotlist = []
        
        dntpnames = ["dCTP","dATP","dGTP","dTTP"]
        cdf = pd.DataFrame({'a':[],'t':[],'g':[],'c':[]})
        dntps = [[],[],[],[]]
        zpro = [[],[],[],[]]
        n = len(dntps)
        
        dntpdirec = r'C:/Users/Meni/Desktop/Fatemeh_dNTPs'
        fn = 'dntps.h5'
        hfile = h5py.File(dntpdirec + fn)
        for i,x in enumerate(dntpnames):
            fn = 'dntps.h5'
            zpro[i] = np.array(hfile[x]).astype(float)
            zpro[i] -=zpro[i].mean()
            zpro[i] /=zpro[i].std()
        
        composite = zpro[0] + zpro[1] + zpro[2] + zpro[3]
        ly, lx = composite.shape
        
        p0 = [composite.mean(1).max(), (ly/2)-4, (ly/2)+4, 1.]
        coeff1, var_matrix1 = curve_fit(dubgauss,np.linspace(0,ly-1, ly), composite.mean(1), p0=p0)
        p0 = [self.zpro.mean(1).max(), (ly/2)-4, (ly/2)+4, 1.]
        coeff2, var_matrix2 = curve_fit(dubgauss,np.linspace(0,ly-1, ly), self.zpro.mean(1), p0=p0)
        shifty = np.mean((coeff2[1], coeff2[2]))-np.mean((coeff1[1], coeff1[2]))
        
        p0 = [composite.mean(1).max(), lx/2, 1.]
        coeff1, var_matrix1 = curve_fit(gauss,np.linspace(0,lx-1, lx), composite.mean(0), p0=p0)
        p0 = [self.zpro.mean(1).max(), lx/2, 1.]
        coeff2, var_matrix2 = curve_fit(gauss,np.linspace(0,lx-1, lx), self.zpro.mean(0), p0=p0)
        shiftx = coeff2[1]-coeff1[1]
        
        self.czpro = [[],[],[],[]]
        for i,x in enumerate(dntps):
            self.czpro[i] = zpro[i]
            self.czpro[i] = ird.transform_img(self.czpro[i], tvec=[shifty,shiftx])
    
        seqdf = self.peakdetection(blueThresh,redThresh,bluePeakThresh,redPeakThresh)
        predictedseq = seqdf.base.str.cat()

        fn = self.datafilename[:-3] + '_seq.fasta'
        predictedseq = Seq.Seq(predictedseq, generic_dna)
        predictedseq = SeqRecord(predictedseq, id = os.path.split(fn)[1])
        SeqIO.write(predictedseq, fn, "fasta")
Esempio n. 8
0
def similarity(template_array, sub_array, scale_exponent=0.4):
    """
    Aligns array 'sub_array' to the template 'template_array' using rotational transformation.
    Args:
        template_array  : Template array which will be used as a reference for rotational aligning
        sub_array       : Subject array (already corrected for translation)
        scale_exponent  : Exponent to which the array will be scaled
    Returns:
        mod_array       : Modified subject array aligned using rotational & translational transformation
    """
    list_array = modify_array([template_array, sub_array], scale_exponent)
    template_arrayslice = list_array[0]
    sub_arrayslice = list_array[1]

    dict_constraint = {'scale': [1.0, 0], 'tx': [0, 0], 'ty': [0, 0]}

    dict_rot = ird.similarity(template_arrayslice,
                              sub_arrayslice,
                              numiter=3,
                              order=2,
                              constraints=dict_constraint)
    mod_array = ird.transform_img(sub_array,
                                  scale=1.0,
                                  angle=dict_rot['angle'],
                                  tvec=dict_rot['tvec'],
                                  mode='nearest')

    print("Rotation = {0}, Translation = {1}".format(dict_rot['angle'],
                                                     dict_rot['tvec']))

    return mod_array
Esempio n. 9
0
def shapeAlignImage(image, shapeImage):
    constraints = {}
    constraints['angle'] = (0, .1)
    constraints['scale'] = (1, .001)
    constraints['tx'] = (0, 5)
    constraints['ty'] = (0, 5)
    if image.shape[2] > 1:
        result = ird.similarity(rgb2gray(shapeImage),
                                rgb2gray(image[:, :, [4, 2, 1]] / 3),
                                numiter=10,
                                constraints=constraints)
    else:
        result = ird.similarity(rgb2gray(shapeImage),
                                np.squeeze(image / 3),
                                numiter=10,
                                constraints=constraints)
        #ird.translation(rgb2gray(shapeImage),rgb2gray(image[:,:,[4,2,1]]/3))
    #tvec = result["tvec"].round(4)
    returnResult = np.zeros(image.shape)
    if len(image.shape) == 3:
        for index in range(0, image.shape[2]):
            im = ird.transform_img(image[:, :, index],
                                   scale=result['scale'],
                                   angle=result['angle'],
                                   tvec=(result['tvec'][0], result['tvec'][1]),
                                   order=3)
            if image.dtype == np.dtype('uint16'):
                returnResult[:, :, index] = np.uint16(im)
            elif image.dtype == np.dtype('uint8'):
                returnResult[:, :, index] = np.uint16(im)
            else:
                returnResult[:, :, index] = im
    else:
        im = ird.transform_img(image[:, :],
                               scale=result['scale'],
                               angle=result['angle'],
                               tvec=(result['tvec'][0], result['tvec'][1]),
                               order=3)
        if image.dtype == np.dtype('uint16'):
            returnResult[:, :] = np.uint16(im)
        elif image.dtype == np.dtype('uint8'):
            returnResult[:, :] = np.uint16(im)
        else:
            returnResult[:, :] = im
    return returnResult, result['scale'], result['angle'], result['tvec']
Esempio n. 10
0
 def apply_shifts(self, frames, shifts, progress_callback):
     shifted_frames = []
     for frame_no, shift in enumerate(shifts):
         tvec = shift["tvec"]
         progress_callback(frame_no / float(len(shifts)))
         frame = frames[frame_no]
         shifted_frames.append(ird.transform_img(frame, tvec=tvec))
     progress_callback(1)
     return shifted_frames
Esempio n. 11
0
def motion_correction(arr, buffers=7):
    '''Simple, quick 2D translation for motion correction.'''
    a4d = np.copy(arr)
    a4d[a4d < THRESH_MC] = 0.0
    shape = a4d.shape
    outmat = np.zeros(shape)

    mean_intensities = [np.mean(a4d[:, :, :, i]) for i in range(shape[3])]
    m = np.argmin(
        mean_intensities)  #volume where the average intensity is the lowest
    refvol = np.sum(a4d[:, :, :, m - buffers:m + buffers], axis=3)
    refvol_rs = ndi.interpolation.zoom(refvol, (3.0, 3.0, 1))
    #refvol_rs = ndi.filters.gaussian_filter(refvol_rs, (1,1,0))
    for i in range(shape[2]):
        refvol_rs[:, :, i] = get_edges(refvol_rs[:, :, i])
    refvol_mean = np.mean(refvol_rs)
    pl.figure()
    pl.imshow(refvol_rs[:, :, 0])

    for sl in range(shape[2]):
        for v in range(shape[3]):
            print "Motion correction in progress. Volume %s of %s." % (
                v + 1, shape[3])
            if v > m - buffers and v < m + buffers:
                print "Reference slice. Skipping."
                outmat[:, :, sl, v] = a4d[:, :, sl, v]
            else:
                csl = a4d[:, :, sl, v]
                csl_rs = ndi.interpolation.zoom(csl, (3.0, 3.0))
                #csl_rs = ndi.gaussian_filter(csl_rs, sigma=1.0)
                csl_rs = get_edges(csl_rs)
                csl_rs = (csl_rs / np.mean(csl_rs)
                          ) * refvol_mean  #intensity normalization
                pl.figure()
                pl.imshow(csl_rs)
                tl = ird.imreg.similarity(refvol_rs[:, :, sl],
                                          csl_rs,
                                          constraints={
                                              "angle": [0.0, 45.0],
                                              "tx": [0.0, 20.0],
                                              "ty": [0.0, 20.0],
                                              "scale": [1.0, 0.0]
                                          })
                tvec = tl["tvec"].round(4)
                angle = tl["angle"].round(4)
                print tvec
                print angle
                print tl["scale"]
                outmat[:, :, sl, v] = ird.transform_img(csl,
                                                        angle=angle,
                                                        tvec=tvec / 3.0)
    return outmat
Esempio n. 12
0
    def cache(self, path):
        Bs = [os.path.join(path, p) for p in os.listdir(path) if p == 'Histograms.tif']
        LRs = [os.path.join(path, p) for p in os.listdir(path) if p == 'WF_TMR_calibrated.tif']
        ImgBs, PathBs, ImgLRs, PathLRs= [], [], [], []
        for p in Bs:
            img = np.array(Image.open(p))
            img = np.expand_dims(img, axis=2) if img.ndim == 2 else img
            ImgBs.append(img)
            PathBs.append(p)

        for p in LRs:
            try:
                imgStack = Image.open(p)
                indexes = [i for i in range(imgStack.n_frames)]
                random.shuffle(indexes)
                c = min(len(indexes), 20)
                for i in indexes[:c]:
                    imgStack.seek(i)
                    img = np.array(imgStack)
                    dtype = img.dtype
                    assert img.ndim == 2
                    if self.drift_correction:
                        import imreg_dft as ird
                        from skimage import exposure
                        b = ImgBs[0][:, :, 0]
                        b = exposure.equalize_hist(b)
                        b = scipy.ndimage.filters.gaussian_filter(b, sigma=(6, 6))
                        b = scipy.misc.imresize(b, img.shape[:2])
                        ts = ird.translation(b, img)
                        tvec = ts["tvec"]
                        # the Transformed IMaGe.
                        img = ird.transform_img(img, tvec=tvec)
                    if self.scale_LR == True:
                        img = scipy.misc.imresize(img, ImgBs[0].shape[:2])
                    elif type(self.scale_LR) is list:
                        img = scipy.misc.imresize(img, self.scale_LR)
                    img = np.expand_dims(img, axis=2)
                    img = img.astype(dtype)
                    ImgLRs.append(img)
                    PathLRs.append(p)
            except KeyboardInterrupt:
                raise
            except Exception as e:
                print('error when reading file ', p)
                import traceback, sys
                traceback.print_exc(file=sys.stdout)

        self.__cache[path] = { 'B': ImgBs, 'A':ImgLRs, 'path': path, 'pathB': PathBs, 'pathA': PathLRs}
        return True
Esempio n. 13
0
 def apply_shifts(self, frames, shift, progress_callback):
     # shifted_frames = []
     tvec = shift["tvec"]
     angle = shift["angle"]
     if "scale" in shift.keys():
         scale = shift["scale"]
     else:
         scale = 1.0
     for frame_no, frame in enumerate(frames):
         progress_callback(frame_no / float(len(frames)))
         # frame = frames[frame_no]
         # shifted_frames.append(ird.transform_img(frame, tvec=tvec, angle=angle, scale=scale))
         frames[frame_no] = ird.transform_img(frame, tvec=tvec, angle=angle, scale=scale)
     progress_callback(1)
     return frames
Esempio n. 14
0
 def apply_shifts(self, frames, shift, progress_callback):
     # shifted_frames = []
     tvec = shift["tvec"]
     angle = shift["angle"]
     if "scale" in shift.keys():
         scale = shift["scale"]
     else:
         scale = 1.0
     for frame_no, frame in enumerate(frames):
         progress_callback(frame_no / float(len(frames)))
         # frame = frames[frame_no]
         # shifted_frames.append(ird.transform_img(frame, tvec=tvec, angle=angle, scale=scale))
         frames[frame_no] = ird.transform_img(frame, tvec=tvec, angle=angle, scale=scale)
     progress_callback(1)
     return frames
Esempio n. 15
0
    def register(self, T, reg_ch):

        if T.ndim == 3:
            reg_ch = None

        def _reg(x):
            return x if reg_ch is None else x[reg_ch]

        R = [T[0]]

        print('Running drift correction...')

        for frame in tqdm(T[1:]):
            result = ird.translation(_reg(R[-1]), _reg(frame))
            if reg_ch is None:
                freg = ird.transform_img(frame, tvec=result["tvec"])
            else:
                freg = np.stack(
                    [ird.transform_img(c, tvec=result["tvec"]) for c in frame])
            R.append(freg)

        reg = np.stack(R)

        return reg
Esempio n. 16
0
def analyze(file, czpro, data_file_name):

    seq_plot_list = [[]]
    firing_plot_list = []

    dntp_names = ["dCTP", "dATP", "dGTP", "dTTP"]
    cdf = pd.DataFrame({'a': [], 't': [], 'g': [], 'c': []})
    dntps = [[], [], [], []]
    zpro = [[], [], [], []]

    # TODO: No reason to open another file; use data already loaded
    for idx, name in enumerate(dntp_names):
        zpro[idx] = np.array(file[name]).astype(float)
        zpro[idx] -= zpro[idx].mean()
        zpro[idx] /= zpro[idx].std()

    composite = zpro[0] + zpro[1] + zpro[2] + zpro[3]
    if len(composite.shape) == 2:
        # Single image
        ly, lx = composite.shape
    elif len(composite.shape) == 3:  # Should only be 3
        # Series of frames with time index being first
        lz, ly, lx = composite.shape
    else:
        raise ValueError("Composite dimensions are not 2D or 3D")

    shift_x, shift_y = calculate_xy_shift(lx, ly, composite, zpro)
    czpro = []  # Color zpro?
    for idx, name in enumerate(dntps):
        czpro.append(zpro[idx])
        czpro[idx] = ird.transform_img(zpro[idx], tvec=[shift_y, shift_x])

    # ------------
    seqdf, _, _ = peak_detection(blue_thresh, red_thresh, blue_peak_thresh,
                                 red_peak_thresh)
    predicted_seq = seqdf.base.str.cat()

    fn = data_file_name[:-3] + '_seq.fasta'
    predicted_seq = Seq.Seq(predicted_seq, generic_dna)
    predicted_seq = SeqRecord(predicted_seq, id=os.path.split(fn)[1])
    # TODO save as DATA_FILE_NAME + _analyzed.txt
    analyzed_name = data_file_name[:-3] + '_analyzed.txt'
    with open(analyzed_name, 'w') as data_file:
        data_file.write(str(predicted_seq))
    # SeqIO.write(predicted_seq, fn, "fasta")

    return zpro, czpro
Esempio n. 17
0
    def image(self) -> ndarray:
        from imreg_dft import transform_img

        if self.cache_image and self._image is not None:
            image = self._image.copy()
        else:
            image = ImageFile._load_image(self.file_path)
        if self.align_image and self.alignment_transform is not None:
            scale = self.alignment_transform.scale if hasattr(
                self.alignment_transform, "scale") else 1
            image = transform_img(image,
                                  scale,
                                  self.alignment_transform.rotation,
                                  self.alignment_transform.translation,
                                  bgval=0)

        return image
Esempio n. 18
0
def transformation(moving, trans_mat, trans_im):
    print(moving)
    print(trans_mat)
    print(trans_im)
    mov = plt.imread(moving)
    print(mov.shape)
    trans = np.load(trans_mat)
    scale = trans[0]
    angle = trans[1]
    t1 = trans[2]
    t2 = trans[3]
    tvec = np.array([t1, t2])
    trans_image = ird.transform_img(mov, scale, angle, tvec)

    plt.imsave(trans_im, trans_image, cmap='gray')

    return (trans_image)
Esempio n. 19
0
    def align(self, image: ndarray, precise: bool = True, **kwargs) -> ndarray:
        """
        Align an image with the current reference image

        :param image: an image to align
        :param precise: peform a second alignment pass, more accurate but much slower
        :param kwargs: keyword arguments passed to imreg_dft.imreg.similarity
        :returns: an image aligned with image_ref
        """
        from imreg_dft import transform_img

        iterations = 2 if precise else 1
        _, transform = self._align_transform(self.image_ref,
                                             image,
                                             numiter=iterations,
                                             **kwargs)

        return transform_img(image,
                             transform.scale,
                             transform.rotation,
                             transform.translation,
                             bgval=0)
Esempio n. 20
0
def check_controls(directory, roi_image_plot, roip2, background):
    dntp_names = ["dCTP", "dATP", "dGTP", "dTTP"]
    dntps = [[], [], [], []]
    zpro = [[], [], [], []]
    czpro = [[], [], [], []]

    for idx, name in enumerate(dntp_names):
        file_name = [
            filename for filename in os.listdir(directory)
            if filename.startswith(name) and filename.endswith('.h5')
        ]
        file_location = os.path.join(directory, file_name[-1])
        hfile = h5py.File(file_location)
        #            print self.direc + os.sep+ fn[-1]
        dntps[idx] = np.array(hfile["images"]).astype(float)
        if roi_image_plot:
            dntps[idx] = dntps[idx][:, roi_image_plot[1]:roip2[1],
                                    roi_image_plot[0]:roip2[0]]
        #            dntps[i] -= self.background

        #            if self.filtertype != []:

        dntps[idx] = dntps[idx][-1500:]
        #            dntps[i] = (dntps[i]/dntps[i].std((1,2))[0])
        #            dntps[i] -= dntps[i].mean()
        #            dntps[i] += self.background.mean()
        zpro[idx] = np.median(dntps[idx], 0)
    #            zpro[i] = np.ma.masked_where(zpro[i] < 0, zpro[i])

    #        zpro[0] -= np.mean(zpro[0])
    #        zpro[1] -= np.mean(zpro[1])
    #        zpro[2] -= np.mean(zpro[2])
    #        zpro[3] -= np.mean(zpro[3])

    fig = plt.figure(zpro)
    plt.subplot(1, 3, 1)

    def create_color_plot(data, color):
        color_map = plt.cm.get_cmap(color)
        my_color_map = color_map(np.arange(color_map.N))
        my_color_map[:, -1] = np.linspace(0, 1, color_map.N)
        my_color_map = ListedColormap(my_color_map)
        image = plt.imshow(data, cmap=my_color_map)
        return image

    im1 = create_color_plot(zpro[0], 'Reds')
    im2 = create_color_plot(zpro[1], 'Greens')
    im3 = create_color_plot(zpro[2], 'Blues')
    im4 = create_color_plot(zpro[3], 'PuRd')

    fig.show()

    plt.subplot(1, 3, 2)
    plt.imshow(zpro)

    composite = zpro[0] + zpro[1] + zpro[2] + zpro[3]
    ly, lx = composite.shape

    shiftx, shifty = calculate_xy_shift(lx, ly, composite, zpro)

    for idx, name in enumerate(dntps):
        czpro[idx] = np.median(dntps[idx], 0)
        czpro[idx] = ird.transform_img(czpro[idx], tvec=[shifty, shiftx])
        czpro[idx] -= background
        czpro[idx] = czpro[idx] / czpro[idx].std()
        czpro[idx] -= czpro[idx].mean()

    composite = czpro[0] + czpro[1] + czpro[2] + czpro[3]
    plt.subplot(1, 3, 3)
    plt.imshow(composite)
Esempio n. 21
0
import os

import scipy as sp
import scipy.misc

import imreg_dft as ird

basedir = os.path.join('..', 'examples')
# the TEMPLATE
im0 = sp.misc.imread(os.path.join(basedir, "sample1.png"), True)
# the image to be transformed
im1 = sp.misc.imread(os.path.join(basedir, "sample2.png"), True)
result = ird.translation(im0, im1)
tvec = result["tvec"].round(4)
# the Transformed IMaGe.
timg = ird.transform_img(im1, tvec=tvec)

# Maybe we don't want to show plots all the time
if os.environ.get("IMSHOW", "yes") == "yes":
    import matplotlib.pyplot as plt
    ird.imshow(im0, im1, timg)
    plt.show()

print("Translation is {}, success rate {:.4g}".format(tuple(tvec),
                                                      result["success"]))
Esempio n. 22
0
import os

import scipy as sp
import scipy.misc

import imreg_dft as ird


basedir = os.path.join('..', 'examples')
# the TEMPLATE
im0 = sp.misc.imread(os.path.join(basedir, "sample1.png"), True)
# the image to be transformed
im1 = sp.misc.imread(os.path.join(basedir, "sample2.png"), True)
result = ird.translation(im0, im1)
tvec = result["tvec"].round(4)
# the Transformed IMaGe.
timg = ird.transform_img(im1, tvec=tvec)

# Maybe we don't want to show plots all the time
if os.environ.get("IMSHOW", "yes") == "yes":
    import matplotlib.pyplot as plt
    ird.imshow(im0, im1, timg)
    plt.show()

print("Translation is {}, success rate {:.4g}"
      .format(tuple(tvec), result["success"]))
Esempio n. 23
0
    def analyze(self):
        #        if self.plotlist != [[],[],[],[],[]]  :
        #            for x in self.plotlist:
        #                self.p1.getRoiPlot().removeItem(x)
        #                self.plotlist = [[],[],[],[],[]]

        if self.seqplotlist != [[]]:
            for x in self.seqplotlist:
                self.p1.getRoiPlot().removeItem(x)
                self.p1.getRoiPlot().autoRange()
                self.seqplotlist = [[]]

        dntpnames = ["dCTP", "dATP", "dGTP", "dTTP"]
        dntps = [[], [], [], []]
        zpro = [[], [], [], []]
        n = len(dntps)

        lz, lx, ly = self.stack.shape
        seq = self.stack.reshape(lz, lx * ly)

        for i, x in enumerate(dntpnames):
            fn = [
                filename for filename in os.listdir(self.direc)
                if filename.startswith(x) and filename.endswith('.h5')
            ]
            hfile = h5py.File(self.direc + os.sep + fn[-1])
            dntps[i] = np.array(hfile["images"]).astype(float)
            if self.roip1 != []:
                dntps[i] = dntps[i][:, self.roip1[1]:self.roip2[1],
                                    self.roip1[0]:self.roip2[0]]

#            if self.filtertype != []:

            dntps[i] = dntps[i][-1500:]
            zpro[i] = np.median(dntps[i], 0)

        composite = zpro[0] + zpro[1] + zpro[2] + zpro[3]
        ly, lx = composite.shape

        p0 = [composite.mean(1).max(), (ly / 2) - 4, (ly / 2) + 4, 1.]
        coeff1, var_matrix1 = curve_fit(dubgauss,
                                        np.linspace(0, ly - 1, ly),
                                        composite.mean(1),
                                        p0=p0)
        p0 = [self.zpro.mean(1).max(), (ly / 2) - 4, (ly / 2) + 4, 1.]
        coeff2, var_matrix2 = curve_fit(dubgauss,
                                        np.linspace(0, ly - 1, ly),
                                        self.zpro.mean(1),
                                        p0=p0)
        shifty = np.mean((coeff2[1], coeff2[2])) - np.mean(
            (coeff1[1], coeff1[2]))

        p0 = [composite.mean(1).max(), lx / 2, 1.]
        coeff1, var_matrix1 = curve_fit(gauss,
                                        np.linspace(0, lx - 1, lx),
                                        composite.mean(0),
                                        p0=p0)
        p0 = [self.zpro.mean(1).max(), lx / 2, 1.]
        coeff2, var_matrix2 = curve_fit(gauss,
                                        np.linspace(0, lx - 1, lx),
                                        self.zpro.mean(0),
                                        p0=p0)
        shiftx = coeff2[1] - coeff1[1]

        self.czpro = [[], [], [], []]
        for i, x in enumerate(dntps):
            self.czpro[i] = np.median(dntps[i], 0)
            self.czpro[i] = ird.transform_img(self.czpro[i],
                                              tvec=[shifty, shiftx])
            #            czpro[i] -= self.background
            self.czpro[i] = self.czpro[i] / self.czpro[i].std()
            self.czpro[i] -= self.czpro[i].mean()

        predictedseq = self.peakdetection()
        predictedseq = predictedseq.str.cat()
        #        cscore = ascore = gscore = tscore = np.array(())
        #        scores = [cscore,ascore,gscore,tscore]
        #        total =[]
        #
        #        for i,x in enumerate(scores):
        #            x = np.dot(seq,czpro[i].ravel())
        #            x = (x - x.mean())/x.std()
        #            if i==0:
        #                total = x**2
        #            else:
        #                total = total+x**2
        #
        #            color = pg.intColor(i, hues = 4)
        #            scores[i] = x
        #
        #            self.plotlist[i] = self.p1.getRoiPlot().plot(x, pen = color)
        #        self.p1.getRoiPlot().setRange(yRange=[-4,10])
        #
        #        scores = pd.DataFrame(np.transpose(scores))
        ##        tot = scores[0]+scores[1] + scores[2] + scores[3]
        ##        tot = self.stack.mean((1,2))
        #
        #        predictedseq = np.zeros(len(scores[0]))
        #        predictedseq[np.array(scores.idxmax(axis=1)==0)] = 1
        #        predictedseq[np.array(scores.idxmax(axis=1)==1)] = 2
        #        predictedseq[np.array(scores.idxmax(axis=1)==2)] = 3
        #        predictedseq[np.array(scores.idxmax(axis=1)==3)] = 4
        ##
        ##        baseline = tot[tot < tot.mean()]
        ##        baseline = np.median(baseline)
        ##        predictedseq[np.array(tot < baseline)] = 0
        ##        predictedseq[scores[0]<1 and scores[1]<1 and scores[2]<1 and scores[3]<1] = 0
        #
        #        scores = scores.T
        #        predictedseq[np.array((scores<1).all())] = 0
        #
        #        for i,x in enumerate(predictedseq):
        #            if i != len(predictedseq)-1 and i !=0:
        #                if x != 0 and (x != predictedseq[i-1] or x != predictedseq[i+1]):
        #                    if x == 0:
        #                        predictedseq[i] = 0
        #                    elif predictedseq[i-1] ==predictedseq[i+1]:
        #                        predictedseq[i] = predictedseq[i-1]
        #
        #        self.plotlist[4] = self.p1.getRoiPlot().plot(predictedseq, pen = pg.mkPen('w', width = 3))
        #
        #        deletearray = np.zeros(len(predictedseq))
        #
        #        for i,x in enumerate(predictedseq):
        #            if i != len(predictedseq)-1:
        #                if x != 0 and (x != predictedseq[i-1] or x != predictedseq[i+1]):
        #                    deletearray[i] = 1
        #
        #        predictedseq = predictedseq[deletearray!=1]
        #        deletearray = np.zeros(len(predictedseq))
        #
        #        for i,x in enumerate(predictedseq):
        #            if i != len(predictedseq)-1:
        #                if predictedseq[i] == predictedseq[i+1]:
        #                    deletearray[i] = 1
        #
        #        predictedseq = predictedseq[deletearray!=1]
        #        predictedseq = predictedseq[predictedseq!=0]
        #
        #        predictedseq = predictedseq.astype('str')
        #
        #        for i,x in enumerate(predictedseq):
        #            if x == '1.0':
        #                predictedseq[i] = 'C'
        #            if x == '2.0':
        #                predictedseq[i] = 'A'
        #            if x == '3.0':
        #                predictedseq[i] = 'G'
        #            if x == '4.0':
        #                predictedseq[i] = 'T'
        #
        #        predictedseq = "".join(predictedseq)
        #        print(predictedseq)
        #
        fn = self.datafilename[:-3] + '_seq.fasta'
        predictedseq = Seq.Seq(predictedseq, generic_dna)
        predictedseq = SeqRecord(predictedseq, id=os.path.split(fn)[1])
        SeqIO.write(predictedseq, fn, "fasta")
    def analyze(self):
#        if self.plotlist != [[],[],[],[],[]]  :
#            for x in self.plotlist:
#                self.p1.getRoiPlot().removeItem(x)
#                self.plotlist = [[],[],[],[],[]] 

        if self.seqplotlist != [[]]  :
            for x in self.seqplotlist:
                self.p1.getRoiPlot().removeItem(x)
                self.p1.getRoiPlot().autoRange()
                self.seqplotlist = [[]]
                
        if self.firingplotlist != []  :
            for x in self.firingplotlist:
                self.p1.getRoiPlot().removeItem(x)
                self.p1.getRoiPlot().autoRange()
                self.firingplotlist = [] 

        dntpnames = ["dCTP","dATP","dGTP","dTTP"]
        dntps = [[],[],[],[]]
        zpro = [[],[],[],[]]
        n = len(dntps)
        
        lz,lx,ly =  self.stack.shape
        seq = self.stack.reshape(lz,lx*ly)

        for i,x in enumerate(dntpnames):
            fn = [filename for filename in os.listdir(self.direc) if filename.startswith(x)
                    and filename.endswith('.h5')]
            hfile = h5py.File(self.direc + os.sep + fn[-1])
            dntps[i] = np.array(hfile["images"]).astype(float)
            if self.roip1 != []:
                dntps[i] = dntps[i][:,self.roip1[1]:self.roip2[1],
                                    self.roip1[0]:self.roip2[0]]
                                    
                                    
#            if self.filtertype != []:
    
            dntps[i] = dntps[i][-1500:]
            zpro[i] = np.median(dntps[i],0)
 
        
        composite = zpro[0] + zpro[1] + zpro[2] + zpro[3]
        ly, lx = composite.shape
        
        p0 = [composite.mean(1).max(), (ly/2)-4, (ly/2)+4, 1.]
        coeff1, var_matrix1 = curve_fit(dubgauss,np.linspace(0,ly-1, ly), composite.mean(1), p0=p0)
        p0 = [self.zpro.mean(1).max(), (ly/2)-4, (ly/2)+4, 1.]
        coeff2, var_matrix2 = curve_fit(dubgauss,np.linspace(0,ly-1, ly), self.zpro.mean(1), p0=p0)
        shifty = np.mean((coeff2[1], coeff2[2]))-np.mean((coeff1[1], coeff1[2]))
        
        p0 = [composite.mean(1).max(), lx/2, 1.]
        coeff1, var_matrix1 = curve_fit(gauss,np.linspace(0,lx-1, lx), composite.mean(0), p0=p0)
        p0 = [self.zpro.mean(1).max(), lx/2, 1.]
        coeff2, var_matrix2 = curve_fit(gauss,np.linspace(0,lx-1, lx), self.zpro.mean(0), p0=p0)
        shiftx = coeff2[1]-coeff1[1]
        
        self.czpro = [[],[],[],[]]
        for i,x in enumerate(dntps):
            self.czpro[i] = np.median(dntps[i],0)
            self.czpro[i] = ird.transform_img(self.czpro[i], tvec=[shifty,shiftx])
#            czpro[i] -= self.background
            self.czpro[i] = self.czpro[i]/self.czpro[i].std()
            self.czpro[i] -= self.czpro[i].mean()
    
        seqdf = self.peakdetection()
        predictedseq = seqdf.base.str.cat()

        fn = self.datafilename[:-3] + '_seq.fasta'
        predictedseq = Seq.Seq(predictedseq, generic_dna)
        predictedseq = SeqRecord(predictedseq, id = os.path.split(fn)[1])
        SeqIO.write(predictedseq, fn, "fasta")
Esempio n. 25
0
def ratio(verbose, logger, work_out_path, crop, res, register, union, h5_save,
          tiff_save, frange):
    # Start time
    time_start = timer()

    # Input background subtracted image stack
    try:
        f = h5py.File(work_out_path + '_back.h5', 'r')
    except:
        raise ImportError(work_out_path + "_back.h5 not found")

    # Input acceptor stack
    try:
        acceptor = np.array(f['acceptor'])
        acceptor_frange = np.array(f.attrs['acceptor_frange'])
    except:
        raise ImportError("Acceptor stack background not processed")

    # Input donor stack
    try:
        donor = np.array(f['donor'])
        donor_frange = np.array(f.attrs['donor_frange'])
    except:
        raise ImportError("Donor stack background not processed")

    f.close()

    # Find frame dimensions and intersection between processed frames and input frames
    Ydim, Xdim = acceptor.shape[1:]
    brange = np.intersect1d(frange, acceptor_frange, return_indices=True)[2]

    # Set default values for crop
    if (crop[2] == 0):
        crop[2] = Xdim
    if (crop[3] == 0):
        crop[3] = Ydim

    # Testing input values
    test.assert_array_equal(
        acceptor_frange,
        donor_frange), "Acceptor and Donor stacks have different frame numbers"
    assert (
        sum(~np.isin(frange, acceptor_frange)) == 0
    ), "background subtracted stacks have not been processed for all frame values"
    assert (crop[2] >= crop[0]), "crop[2] must be greater than crop[0]"
    assert (crop[3] >= crop[1]), "crop[3] must be greater than crop[1]"
    assert (crop[0] >= 0), "crop[0] must be >= 0"
    assert (crop[2] <= Xdim), "crop[2] must be <= than the width of the image"
    assert (crop[1] >= 0), "crop[1] must be >= 0"
    assert (crop[3] <= Ydim), "crop[3] must be <= than the height of the image"

    # Image crop
    acceptorc = acceptor[:, crop[1]:crop[3], crop[0]:crop[2]]
    donorc = donor[:, crop[1]:crop[3], crop[0]:crop[2]]

    # Search for saved ratio images
    try:
        # Input files into dictionaries
        f2 = h5py.File(work_out_path + '_ratio_back.h5', 'r')
        ratio_frange = np.array(f2.attrs['ratio_frange'])

        acceptori = dict(list(zip(ratio_frange, np.array(f2['acceptori']))))
        donori = dict(list(zip(ratio_frange, np.array(f2['donori']))))
        f2.close()
    except:
        # Initialize empty dictionaries for intensities
        acceptori, donori = {}, {}

    # Initialize empty dictionaries for pixel counts
    acceptornz, donornz = {}, {}

    # Set up constants for loop
    mult = np.float32(255) / np.float32(res)
    ires = 100 / np.float32(res)
    ipix = 100 / (Xdim * Ydim)

    # Loop through frames
    for count, frame in list(zip(frange, brange)):
        if (verbose):
            print("(Ratio Processing) Frame Number: " + str(count + 1))

        # Image registration for donor channel
        if (register):
            trans = ird.translation(acceptorc[frame, :, :],
                                    donorc[frame, :, :])
            tvec = trans["tvec"].round(4)
            donorc[frame, :, :] = np.round(
                ird.transform_img(donorc[frame, :, :], tvec=tvec))

        # Thresholding
        acceptors = np.uint8(np.float32(acceptorc[frame, :, :]) * mult)
        donors = np.uint8(np.float32(donorc[frame, :, :]) * mult)

        # Check for max image intensity
        if np.uint32(np.amax(acceptors)) + np.uint32(np.amax(donors)) > 70:
            # Otsu thresholding for normal intensity images
            _, A_thresh = cv2.threshold(acceptors, 0, 255,
                                        cv2.THRESH_BINARY + cv2.THRESH_OTSU)
            _, B_thresh = cv2.threshold(donors, 0, 255,
                                        cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        else:
            # Simple thresholding for low intensity images
            _, A_thresh = cv2.threshold(acceptors, 3, 255, cv2.THRESH_BINARY)
            _, B_thresh = cv2.threshold(donors, 3, 255, cv2.THRESH_BINARY)

        # Setting values below threshold to zero
        acceptorc[frame, :, :] *= np.uint16(A_thresh / 255)
        donorc[frame, :, :] *= np.uint16(B_thresh / 255)

        # Consider only foreground pixel intensity overlapping between donor and acceptor channels to ensure channels overlap perfectly
        if (union):
            # Create mask for overlapping pixels
            C = np.multiply(A_thresh, B_thresh)
            C[C > 0] = 1

            # Set non-overlapping pixels to zero
            acceptorc[frame, :, :] *= C
            donorc[frame, :, :] *= C

        # Count number of non-zero pixels by total pixels per frame
        acceptornz[count] = np.count_nonzero(A_thresh) * ipix
        donornz[count] = np.count_nonzero(B_thresh) * ipix

        # Find the ratio of the median non-zero intensity pixels and the bit depth per frame for the acceptor stack
        if (np.amax(acceptorc[frame, :, :]) > 0.0):
            acceptori[count] = ndimage.median(acceptorc[frame, :, :],
                                              labels=A_thresh / 255) * ires
        else:
            acceptori[count] = 0

        # Find the ratio of the median non-zero intensity pixels and the bit depth per frame for the donor stack
        if (np.amax(donorc[frame, :, :]) > 0.0):
            donori[count] = ndimage.median(donorc[frame, :, :],
                                           labels=B_thresh / 255) * ires
        else:
            donori[count] = 0

    # End time
    time_end = timer()
    time_elapsed = str(int(time_end - time_start) + 1)
    if (verbose):
        print(("(Ratio Processing) Time: " + time_elapsed + " second(s)"))

    # Update log file to save stack metrics
    print_range = [x + 1 for x in frange]
    if (max(np.ediff1d(frange, to_begin=frange[0])) > 1):
        logger.info('(Ratio Processing) ' + 'frames: ' +
                    ",".join(list(map(str, print_range))) + ', time: ' +
                    time_elapsed + ' sec, save: ' + str(h5_save))
    else:
        logger.info('(Ratio Processing) ' + 'frames: ' + str(print_range[0]) +
                    '-' + str(print_range[-1]) + ', time: ' + time_elapsed +
                    ' sec, save: ' + str(h5_save))

    # Create plot to showcase median intensity over frame number and the number of foreground pixels per channel (NON-bleach corrected)
    time_evolution(acceptori, donori, work_out_path,
                   '_intensity_nonbleach.png', 'Median Intensity/Bit Depth',
                   h5_save)
    time_evolution(acceptornz, donornz, work_out_path, '_pixelcount.png',
                   'Foreground/Total Image Pixels', h5_save)

    # Calculate 8-bit ratio image with NON-bleach corrected donor and acceptor channels
    if (h5_save or tiff_save):
        # Calculate ratio stack
        ratio = ratio_calc(acceptorc, donorc)

        # Save processed images, non-zero pixel count, median intensity and ratio processed images in HDF5 format
        if (h5_save):
            acceptori_brange = np.array([acceptori[a] for a in brange])
            donori_brange = np.array([donori[a] for a in brange])

            h5_time_start = timer()
            h5(acceptorc[brange, :, :], 'acceptor',
               work_out_path + '_ratio_back.h5', frange)
            h5(donorc[brange, :, :], 'donor', work_out_path + '_ratio_back.h5',
               frange)
            h5(acceptori_brange, 'acceptori', work_out_path + '_ratio_back.h5',
               frange)
            h5(donori_brange, 'donori', work_out_path + '_ratio_back.h5',
               frange)
            h5(ratio[brange, :, :], 'ratio', work_out_path + '_ratio_back.h5',
               frange)
            h5_time_end = timer()

            if (verbose):
                print(("Saving Acceptor, Donor and Ratio stacks in " +
                       work_out_path + '_ratio_back.h5' + ' [Time: ' +
                       str(int(h5_time_end - h5_time_start) + 1) +
                       " second(s)]"))

        # Save NON-bleach corrected ratio image as TIFF
        if (tiff_save):
            tiff_time_start = timer()
            tiff(ratio, work_out_path + '_ratio_back.tif')
            tiff_time_end = timer()

            if (verbose):
                print(("Saving unbleached Ratio TIFF stack in " +
                       work_out_path + '_ratio_back.tif' + ' [Time: ' +
                       str(int(tiff_time_end - tiff_time_start) + 1) +
                       " second(s)]"))
Esempio n. 26
0
import os

import scipy as sp
import scipy.misc
import matplotlib.pyplot as plt

import imreg_dft as ird

basedir = os.path.join('..', 'examples')
# the TEMPLATE
im0 = sp.misc.imread(os.path.join(basedir, "sample1.png"), True)
# the image to be transformed
im1 = sp.misc.imread(os.path.join(basedir, "sample2.png"), True)
t0, t1 = ird.translation(im0, im1)
# the Transformed IMaGe.
timg = ird.transform_img(im1, tvec=(t0, t1))
ird.imshow(im0, im1, timg)
plt.show()
print(t0, t1)
Esempio n. 27
0
import os

import scipy as sp
import scipy.misc
import matplotlib.pyplot as plt

import imreg_dft as ird


basedir = os.path.join('..', 'examples')
# the TEMPLATE
im0 = sp.misc.imread(os.path.join(basedir, "sample1.png"), True)
# the image to be transformed
im1 = sp.misc.imread(os.path.join(basedir, "sample2.png"), True)
t0, t1 = ird.translation(im0, im1)
# the Transformed IMaGe.
timg = ird.transform_img(im1, tvec=(t0, t1))
ird.imshow(im0, im1, timg)
plt.show()
print(t0, t1)
Esempio n. 28
0
    def checkcontrols(self):
        dntpnames = ["dCTP","dATP","dGTP","dTTP"]
        dntps = [[],[],[],[]]
        zpro = [[],[],[],[]]
        czpro = [[],[],[],[]]
        n = len(dntps)
        
        for i,x in enumerate(dntpnames):
            fn = [filename for filename in os.listdir(self.direc) if filename.startswith(x)
                    and filename.endswith('.h5')]
            hfile = h5py.File(self.direc + os.sep + fn[-1])
#            print self.direc + os.sep+ fn[-1]
            dntps[i] = np.array(hfile["images"]).astype(float)
            if self.roip1 != []:
                dntps[i] = dntps[i][:,self.roip1[1]:self.roip2[1],
                                    self.roip1[0]:self.roip2[0]]
#            dntps[i] -= self.background

#            if self.filtertype != []:
    
            dntps[i] = dntps[i][-1500:]
#            dntps[i] = (dntps[i]/dntps[i].std((1,2))[0])
#            dntps[i] -= dntps[i].mean()
#            dntps[i] += self.background.mean()
            zpro[i] = np.median(dntps[i],0)
#            zpro[i] = np.ma.masked_where(zpro[i] < 0, zpro[i])
 
#        zpro[0] -= np.mean(zpro[0])
#        zpro[1] -= np.mean(zpro[1])
#        zpro[2] -= np.mean(zpro[2])
#        zpro[3] -= np.mean(zpro[3])

        
        fig = plt.figure()
        plt.subplot(1,3,1)   
        cmap = plt.cm.get_cmap("Reds")
        my_cmap = cmap(np.arange(cmap.N))
        my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
        my_cmap = ListedColormap(my_cmap)
        im1 = plt.imshow(zpro[0], cmap = my_cmap)
        
        cmap = plt.cm.get_cmap("Greens")
        my_cmap = cmap(np.arange(cmap.N))
        my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
        my_cmap = ListedColormap(my_cmap)
        im2 = plt.imshow(zpro[1], cmap = my_cmap)
        
        cmap = plt.cm.get_cmap("Blues")
        my_cmap = cmap(np.arange(cmap.N))
        my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
        my_cmap = ListedColormap(my_cmap)
        plt.imshow(zpro[2], cmap = my_cmap)
        
        cmap = plt.cm.get_cmap("PuRd")
        my_cmap = cmap(np.arange(cmap.N))
        my_cmap[:,-1] = np.linspace(0, 1, cmap.N)
        my_cmap = ListedColormap(my_cmap)
        im4 = plt.imshow(zpro[3], cmap = my_cmap)
        fig.show()
        
        plt.subplot(1,3,2)   
        plt.imshow(self.zpro)
        
        composite = zpro[0] + zpro[1] + zpro[2] + zpro[3]
        ly, lx = composite.shape
        
        p0 = [composite.mean(1).max(), (ly/2)-4, (ly/2)+4, 1.]
        coeff1, var_matrix1 = curve_fit(dubgauss,np.linspace(0,ly-1, ly), composite.mean(1), p0=p0)
        p0 = [self.zpro.mean(1).max(), (ly/2)-4, (ly/2)+4, 1.]
        coeff2, var_matrix2 = curve_fit(dubgauss,np.linspace(0,ly-1, ly), self.zpro.mean(1), p0=p0)
        shifty = np.mean((coeff2[1], coeff2[2]))-np.mean((coeff1[1], coeff1[2]))
        
        p0 = [composite.mean(1).max(), lx/2, 1.]
        coeff1, var_matrix1 = curve_fit(gauss,np.linspace(0,lx-1, lx), composite.mean(0), p0=p0)
        p0 = [self.zpro.mean(1).max(), lx/2, 1.]
        coeff2, var_matrix2 = curve_fit(gauss,np.linspace(0,lx-1, lx), self.zpro.mean(0), p0=p0)
        shiftx = coeff2[1]-coeff1[1]
        
        for i,x in enumerate(dntps):
            czpro[i] = np.median(dntps[i],0)
            czpro[i] = ird.transform_img(czpro[i], tvec=[shifty,shiftx])
            czpro[i] -= self.background
            czpro[i] = czpro[i]/czpro[i].std()
            czpro[i] -= czpro[i].mean()
        
        ccomposite = czpro[0] + czpro[1] + czpro[2] + czpro[3]
        plt.subplot(1,3,3)   
        plt.imshow(ccomposite)
Esempio n. 29
0
    def cache(self, path):
        As = [
            os.path.join(path, p) for p in os.listdir(path)
            if p.startswith('A') and p.endswith(self.ext)
        ]
        Bs = [
            os.path.join(path, p) for p in os.listdir(path)
            if p.startswith('B') and p.endswith(self.ext)
        ]
        LRs = [
            os.path.join(path, p) for p in os.listdir(path)
            if p.startswith('LR') and p.endswith(self.ext)
        ]
        if os.path.exists(os.path.join('mask_A' + self.ext)):
            m = np.array(Image.open(os.path.join('mask_A' + self.ext)))
            m = np.expand_dims(m, axis=2) if m.ndim == 2 else m
            maskA = m < m.max() / 2
        else:
            maskA = None
        if os.path.exists(os.path.join('mask_B' + self.ext)):
            m = np.array(Image.open(os.path.join('mask_B' + self.ext)))
            m = np.expand_dims(m, axis=2) if m.ndim == 2 else m
            maskB = m < m.max() / 2
        else:
            maskB = None
        ImgAs, PathAs, ImgBs, PathBs, ImgLRs, PathLRs = [], [], [], [], [], []
        for p in As:
            try:
                img = np.array(Image.open(p))
                img = np.expand_dims(img, axis=2) if img.ndim == 2 else img
                if maskA:
                    img[maskA] = img.min()
                ImgAs.append(img)
                PathAs.append(p)
            except KeyboardInterrupt:
                raise
            except Exception as e:
                print('error when reading file ', p)
        assert len(ImgAs) > 0, 'no file found for "A"'
        for p in Bs:
            try:
                img = np.array(Image.open(p))
                img = np.expand_dims(img, axis=2) if img.ndim == 2 else img
                if maskB:
                    img[maskB] = img.min()
                ImgBs.append(img)
                PathBs.append(p)
            except KeyboardInterrupt:
                raise
            except Exception as e:
                print('error when reading file ', p)

        for p in LRs:
            try:
                img = np.array(Image.open(p))
                assert img.ndim == 2
                if self.drift_correction:
                    import imreg_dft as ird
                    from skimage import exposure
                    b = ImgBs[0][:, :, 0]
                    b = exposure.equalize_hist(b)
                    b = scipy.ndimage.filters.gaussian_filter(b, sigma=(6, 6))
                    b = scipy.misc.imresize(b, img.shape[:2])
                    ts = ird.translation(b, img)
                    tvec = ts["tvec"].round(4)
                    # the Transformed IMaGe.
                    img = ird.transform_img(img, tvec=tvec)
                img = scipy.misc.imresize(img, ImgBs[0].shape[:2])
                img = np.expand_dims(img, axis=2)
                ImgLRs.append(img)
                PathLRs.append(p)
            except KeyboardInterrupt:
                raise
            except Exception as e:
                print('error when reading file ', p)
                import traceback, sys
                traceback.print_exc(file=sys.stdout)

        self.__cache[path] = {
            'A': ImgAs,
            'B': ImgBs,
            'LR': ImgLRs,
            'path': path,
            'pathA': PathAs,
            'pathB': PathBs,
            'pathLR': PathLRs
        }
        return True