コード例 #1
0
ファイル: abadura_demo.py プロジェクト: Rack95/Caiman_edit
def tile_and_correct_wrapper(params):

    from skimage.external.tifffile import imread
    import numpy as np
    import cv2
    try:
        cv2.setNumThreads(1)
    except:
        1  # 'Open CV is naturally single threaded'

    from caiman.motion_correction import tile_and_correct

    img_name,  out_fname, idxs, shape_mov, template, strides, overlaps, max_shifts,\
        add_to_movie, max_deviation_rigid, upsample_factor_grid, newoverlaps, newstrides = params

    imgs = imread(img_name, key=idxs)
    mc = np.zeros(imgs.shape, dtype=np.float32)
    shift_info = []
    for count, img in enumerate(imgs):
        if count % 10 == 0:
            print(count)
        mc[count], total_shift, start_step, xy_grid = tile_and_correct(img, template, strides, overlaps, max_shifts, add_to_movie=add_to_movie, newoverlaps=newoverlaps, newstrides=newstrides,
                                                                       upsample_factor_grid=upsample_factor_grid, upsample_factor_fft=10, show_movie=False, max_deviation_rigid=max_deviation_rigid)
        shift_info.append([total_shift, start_step, xy_grid])
    if out_fname is not None:
        outv = np.memmap(out_fname, mode='r+', dtype=np.float32,
                         shape=shape_mov, order='F')
        outv[:, idxs] = np.reshape(
            mc.astype(np.float32), (len(imgs), -1), order='F').T

    return shift_info, idxs, np.nanmean(mc, 0)
コード例 #2
0
    def _processFrame(self, frame, frame_number):
        ''' Do some basic processing on a single frame
            Raises NaNFrameException if a frame contains NaN
            Returns the normalized/etc modified frame
        '''
        t = time.time()
        if frame is None:
            raise ObjectNotFoundError
        if np.isnan(np.sum(frame)):
            raise NaNFrameException
        frame = frame.astype(
            np.float32)  #or require float32 from image acquistion
        if self.onAc.params.get('online', 'ds_factor') > 1:
            frame = cv2.resize(frame, self.onAc.img_norm.shape[::-1])
            # TODO check for params, onAc componenets before calling, or except
        if self.onAc.params.get('online', 'normalize'):
            frame -= self.onAc.img_min
        if self.onAc.params.get('online', 'motion_correct'):
            try:
                templ = self.onAc.estimates.Ab.dot(
                    self.onAc.estimates.C_on[:self.onAc.M, (frame_number - 1)]
                ).reshape(
                    # self.onAc.estimates.C_on[:self.onAc.M, (frame_number-1)%self.onAc.window]).reshape(
                    self.onAc.params.get('data', 'dims'),
                    order='F') * self.onAc.img_norm
            except Exception as e:
                logger.error('Unknown exception {0}'.format(e))
                raise Exception

            if self.onAc.params.get('motion', 'pw_rigid'):
                frame_cor, shift = tile_and_correct(
                    frame,
                    templ,
                    self.onAc.params.motion['strides'],
                    self.onAc.params.motion['overlaps'],
                    self.onAc.params.motion['max_shifts'],
                    newoverlaps=None,
                    newstrides=None,
                    upsample_factor_grid=4,
                    upsample_factor_fft=10,
                    show_movie=False,
                    max_deviation_rigid=self.onAc.params.
                    motion['max_deviation_rigid'],
                    add_to_movie=0,
                    shifts_opencv=True,
                    gSig_filt=None,
                    use_cuda=False,
                    border_nan='copy')[:2]
            else:
                frame_cor, shift = motion_correct_iteration_fast(
                    frame, templ, self.max_shifts_online,
                    self.max_shifts_online)
            self.onAc.estimates.shifts.append(shift)
        else:
            frame_cor = frame
        if self.onAc.params.get('online', 'normalize'):
            frame_cor = frame_cor / self.onAc.img_norm
        self.procFrame_time.append([time.time() - t])
        return frame_cor
コード例 #3
0
#newshapes = (32,32)
newstrides = None
##Sue Ann
#overlaps = (32,32)
#strides = (128,128)

#%
total_shifts = []
start_steps = []
xy_grids = []
mc = np.zeros(m.shape)
for count, img in enumerate(np.array(m)):
    if count % 10 == 0:
        print(count)
    mc[count],total_shift,start_step,xy_grid = tile_and_correct(img, template, strides, overlaps,(12,12), newoverlaps = None, \
                newstrides = newstrides, upsample_factor_grid=4,\
                upsample_factor_fft=10,show_movie=False,max_deviation_rigid=2,add_to_movie=add_to_movie)

    total_shifts.append(total_shift)
    start_steps.append(start_step)
    xy_grids.append(xy_grid)

#%%
pl.plot(np.reshape(np.array(total_shifts), (len(total_shifts), -1)))
#%%
m_raw = np.nanmean(m, 0)
m_rig = np.nanmean(mr, 0)
m_el = np.nanmean(mc, 0)
#%%
import scipy
r_raw = []
コード例 #4
0
    def __fit_next_frame(self, frame, t, model_LN=None, out=None):
        ssub_B = self.params.get('init', 'ssub_B') * self.params.get(
            'init', 'ssub')
        d1, d2 = self.params.get('data', 'dims')
        max_shifts_online = self.params.get('online', 'max_shifts_online')

        if model_LN is not None:
            if self.params.get('ring_CNN', 'remove_activity'):
                activity = self.estimates.Ab[:, :self.N].dot(
                    self.estimates.C_on[:self.N,
                                        t - 1]).reshape(self.params.get(
                                            'data', 'dims'),
                                                        order='F')
                if self.params.get('online', 'normalize'):
                    activity *= self.img_norm
            else:
                activity = 0.
                # frame = frame.astype(np.float32) - activity
            frame = frame - np.squeeze(
                model_LN.predict(
                    np.expand_dims(
                        np.expand_dims(frame.astype(np.float32) - activity, 0),
                        -1)))
            frame = np.maximum(frame, 0)

        t_frame_start = time.time()
        if np.isnan(np.sum(frame)):
            raise Exception('Current frame contains NaN')

        frame_ = frame.copy().astype(np.float32)
        if self.params.get('online', 'ds_factor') > 1:
            frame_ = cv2.resize(frame_, self.img_norm.shape[::-1])

        if self.params.get('online', 'normalize'):
            frame_ -= self.img_min  # make data non-negative

        if self.params.get('online', 'motion_correct'):
            templ = self.estimates.Ab.dot(
                np.median(self.estimates.C_on[:self.M, t - 51:t - 1],
                          1)).reshape(self.params.get('data', 'dims'),
                                      order='F')  #*self.img_norm
            if self.is1p and self.estimates.W is not None:
                if ssub_B == 1:
                    B = self.estimates.W.dot((frame_ - templ).flatten(
                        order='F') - self.estimates.b0) + self.estimates.b0
                    B = B.reshape(self.params.get('data', 'dims'), order='F')
                else:
                    b0 = self.estimates.b0.reshape((d1, d2),
                                                   order='F')  #*self.img_norm
                    bc2 = initialization.downscale(
                        frame_ - templ - b0,
                        (ssub_B, ssub_B)).flatten(order='F')
                    Wb = self.estimates.W.dot(bc2).reshape(
                        ((d1 - 1) // ssub_B + 1, (d2 - 1) // ssub_B + 1),
                        order='F')
                    B = b0 + np.repeat(np.repeat(Wb, ssub_B, 0), ssub_B,
                                       1)[:d1, :d2]
                templ += B
            if self.params.get('online', 'normalize'):
                templ *= self.img_norm
            if self.is1p:
                templ = high_pass_filter_space(templ,
                                               self.params.motion['gSig_filt'])
            if self.params.get('motion', 'pw_rigid'):
                frame_cor, shift, _, xy_grid = tile_and_correct(
                    frame_,
                    templ,
                    self.params.motion['strides'],
                    self.params.motion['overlaps'],
                    self.params.motion['max_shifts'],
                    newoverlaps=None,
                    newstrides=None,
                    upsample_factor_grid=4,
                    upsample_factor_fft=10,
                    show_movie=False,
                    max_deviation_rigid=self.params.
                    motion['max_deviation_rigid'],
                    add_to_movie=0,
                    shifts_opencv=True,
                    gSig_filt=None,
                    use_cuda=False,
                    border_nan='copy')
            else:
                if self.is1p:
                    frame_orig = frame_.copy()
                    frame_ = high_pass_filter_space(
                        frame_, self.params.motion['gSig_filt'])
                frame_cor, shift = motion_correct_iteration_fast(
                    frame_, templ, max_shifts_online, max_shifts_online)
                if self.is1p:
                    M = np.float32([[1, 0, shift[1]], [0, 1, shift[0]]])
                    frame_cor = cv2.warpAffine(frame_orig,
                                               M,
                                               frame_.shape[::-1],
                                               flags=cv2.INTER_CUBIC,
                                               borderMode=cv2.BORDER_REFLECT)

            self.estimates.shifts.append(shift)
        else:
            templ = None
            frame_cor = frame_

        self.current_frame_cor = frame_cor

        if self.params.get('online', 'normalize'):
            frame_cor = frame_cor / self.img_norm
        self.fit_next(t, frame_cor.reshape(-1, order='F'))

        if self.fp_detector.method is not None:
            self.__reject_fp_comps((d1, d2), max_bright=frame_.max())
コード例 #5
0
#newshapes = (32,32)
newstrides = None
##Sue Ann
#overlaps = (32,32)
#strides = (128,128)

#%
total_shifts = []
start_steps = []
xy_grids = []
mc = np.zeros(m.shape)
for count,img in enumerate(np.array(m)):
    if count % 10  == 0:
        print(count)
    mc[count],total_shift,start_step,xy_grid = tile_and_correct(img, template, strides, overlaps,(12,12), newoverlaps = None, \
                newstrides = newstrides, upsample_factor_grid=4,\
                upsample_factor_fft=10,show_movie=False,max_deviation_rigid=2,add_to_movie=add_to_movie)

    total_shifts.append(total_shift)
    start_steps.append(start_step)
    xy_grids.append(xy_grid)

#%%
pl.plot(np.reshape(np.array(total_shifts),(len(total_shifts),-1))) 
#%%
m_raw = np.nanmean(m,0)
m_rig = np.nanmean(mr,0)
m_el = np.nanmean(mc,0)
#%%
import scipy
r_raw = []