예제 #1
0
def plot_IV(subject_id=454597, cellnum=1, ivnum=0, IVsweepstoplot=None):
    #%
    key = {'subject_id': subject_id, 'cell_number': cellnum}
    sweeps = pd.DataFrame(ephys_patch.Sweep() & key)
    protocolnames = sweeps['protocol_name'].unique()
    ivprotocolnames = [i for i in protocolnames if 'iv' in i.lower()]
    ivprotocolname = ivprotocolnames[ivnum]
    key['protocol_name'] = ivprotocolname
    #
    df_iv = pd.DataFrame()
    #%
    if IVsweepstoplot == None:
        #%
        ivsweepnums = (ephys_patch.Sweep()
                       & key).fetch('protocol_sweep_number')
        for iwsweepnum in ivsweepnums:
            key['protocol_sweep_number'] = iwsweepnum
            apnum = len(ephysanal.ActionPotential() * ephys_patch.Sweep()
                        & key)
            if apnum > 2:
                break
        IVsweepstoplot = [0, iwsweepnum]

    for sweepnum in IVsweepstoplot:
        key['protocol_sweep_number'] = sweepnum
        df_iv = pd.concat([
            df_iv,
            pd.DataFrame((ephys_patch.Sweep() & key) *
                         (ephys_patch.SweepResponse() & key) *
                         (ephys_patch.SweepStimulus() & key) *
                         (ephys_patch.SweepMetadata() & key))
        ])
    df_IV = pd.DataFrame()
    for line in df_iv.iterrows():
        linenow = line[1]
        time = np.arange(0, len(
            linenow['response_trace'])) / linenow['sample_rate']
        linenow['time'] = time
        df_IV = pd.concat([df_IV, linenow.to_frame().transpose()])
    fig = plt.figure()
    ax_IV = fig.add_subplot(211)  #add_axes([0,0,1,1])
    ax_stim = fig.add_subplot(212)  #.add_axes([0,-.4,1,.2])
    for line in df_IV.iterrows():
        ax_IV.plot(line[1]['time'], line[1]['response_trace'] * 1000, 'k-')
        ax_stim.plot(line[1]['time'], line[1]['stimulus_trace'] * 10**12, 'k-')
    ax_IV.set_xlabel('Time (s)')
    ax_IV.set_xlim([0, 1])

    ax_IV.set_ylabel('mV')
    ax_IV.set_title('subject: {} cell: {}'.format(subject_id, cellnum))

    ax_stim.set_xlabel('Time (s)')
    ax_stim.set_xlim([0, 1])
    ax_stim.set_ylabel('pA')
    #ax_stim.set_title('Stimulus')
    return fig
예제 #2
0
 def make(self, key):
     time_back = .0002
     time_capacitance = .0001
     time_forward = .0002
     df_squarepulse = pd.DataFrame((SquarePulse()&key)*ephys_patch.Sweep()*ephys_patch.SweepResponse()*ephys_patch.SweepMetadata())
     stimamplitude = df_squarepulse['square_pulse_amplitude'].values[0]
     if np.abs(stimamplitude)>=40*10**-12:
         trace = df_squarepulse['response_trace'].values[0]
         start_idx = df_squarepulse['square_pulse_start_idx'][0]
         end_idx = df_squarepulse['square_pulse_end_idx'][0]
         sr = df_squarepulse['sample_rate'][0]
         step_back = int(np.round(time_back*sr))
         step_capacitance = int(np.round(time_capacitance*sr))
         step_forward = int(np.round(time_forward*sr))
         
         
         v0_start = np.mean(trace[start_idx-step_back:start_idx])
         vrs_start = np.mean(trace[start_idx+step_capacitance:start_idx+step_capacitance+step_forward])
         v0_end = np.mean(trace[end_idx-step_back:end_idx])
         vrs_end = np.mean(trace[end_idx+step_capacitance:end_idx+step_capacitance+step_forward])
         
         dv_start = vrs_start-v0_start
         RS_start = dv_start/stimamplitude 
         dv_end = vrs_end-v0_end
         RS_end = dv_end/stimamplitude*-1
         
         RS = np.round(np.mean([RS_start,RS_end])/1000000,2)
         key['series_resistance_squarepulse'] = RS
         self.insert1(key,skip_duplicates=True)
예제 #3
0
    def make(self, key):

        #%%
        #key = {'subject_id': 454263, 'session': 1, 'cell_number': 1, 'sweep_number': 62}
        #print(key)
        keynow = key.copy()
        if len(ActionPotential() & keynow) == 0:
            #%%
            pd_sweep = pd.DataFrame((ephys_patch.Sweep() & key) *
                                    (SweepResponseCorrected() & key) *
                                    (ephys_patch.SweepStimulus() & key) *
                                    (ephys_patch.SweepMetadata() & key))
            if len(pd_sweep) > 0:
                trace = pd_sweep['response_trace_corrected'].values[0]
                sr = pd_sweep['sample_rate'][0]
                si = 1 / sr
                msstep = int(.0005 / si)
                sigma = .00005
                trace_f = ndimage.gaussian_filter(trace, sigma / si)
                d_trace_f = np.diff(trace_f) / si
                peaks = d_trace_f > 40
                sp_starts, sp_ends = (SquarePulse() & key).fetch(
                    'square_pulse_start_idx', 'square_pulse_end_idx')
                squarepulses = np.concatenate([sp_starts, sp_ends])
                for sqidx in squarepulses:
                    peaks[sqidx - msstep:sqidx + msstep] = False
                peaks = ndimage.morphology.binary_dilation(
                    peaks, np.ones(int(round(.002 / si))))
                spikemaxidxes = list()
                while np.any(peaks):
                    spikestart = np.argmax(peaks)
                    spikeend = np.argmin(peaks[spikestart:]) + spikestart
                    if spikestart == spikeend:
                        if sum(peaks[spikestart:]) == len(peaks[spikestart:]):
                            spikeend = len(trace)
                    try:
                        sipeidx = np.argmax(
                            trace[spikestart:spikeend]) + spikestart
                    except:
                        print(key)
                        sipeidx = np.argmax(
                            trace[spikestart:spikeend]) + spikestart
                    spikemaxidxes.append(sipeidx)
                    peaks[spikestart:spikeend] = False
                    #%
                if len(spikemaxidxes) > 0:
                    spikemaxtimes = spikemaxidxes / sr + float(
                        pd_sweep['sweep_start_time'].values[0])
                    spikenumbers = np.arange(len(spikemaxidxes)) + 1
                    keylist = list()
                    for spikenumber, spikemaxidx, spikemaxtime in zip(
                            spikenumbers, spikemaxidxes, spikemaxtimes):
                        keynow = key.copy()
                        keynow['ap_num'] = spikenumber
                        keynow['ap_max_index'] = spikemaxidx
                        keynow['ap_max_time'] = spikemaxtime
                        keylist.append(keynow)
                        #%%
                    self.insert(keylist, skip_duplicates=True)
예제 #4
0
 def make(self, key):
     #%%
     #key = {'subject_id':456462,'session':1,'cell_number':3,'sweep_number':24}
     exposure = (ephys_patch.SweepImagingExposure()&key).fetch('imaging_exposure_trace')
     if len(exposure)>0:
         si = 1/(ephys_patch.SweepMetadata()&key).fetch('sample_rate')[0]
         sweeptime = float((ephys_patch.Sweep()&key).fetch('sweep_start_time')[0])
         exposure = np.diff(exposure[0])
         peaks = signal.find_peaks(exposure)
         peaks_idx = peaks[0]
         key['frame_idx']= peaks_idx 
         key['frame_sweep_time']= peaks_idx*si
         key['frame_time']= peaks_idx*si + sweeptime
         self.insert1(key,skip_duplicates=True)
예제 #5
0
def get_sweep(key_sweep,junction_potential = 13.5, downsampled_rate = 10000):
    key_sweep['cell_number'] = (imaging_gt.CellMovieCorrespondance()&key_sweep).fetch1('cell_number')
    #%
# =============================================================================
#     key_sweep = {'subject_id': 456462,'session' : 1, 'movie_number' : 0, 'cell_number' : 3,'sweep_number':28} 
#     junction_potential = 13.5 #mV
#     downsampled_rate = 10000 #Hz
# =============================================================================

    neutralizationenable,e_sr= (ephys_patch.SweepMetadata()&key_sweep).fetch1('neutralizationenable','sample_rate')
    try:
        uncompensatedRS =  float((ephysanal.SweepSeriesResistance()&key_sweep).fetch1('series_resistance_residual'))
    except:
        uncompensatedRS = 0
    v = (ephys_patch.SweepResponse()&key_sweep).fetch1('response_trace')
    i = (ephys_patch.SweepStimulus()&key_sweep).fetch1('stimulus_trace')
    tau_1_on =.1/1000
    t = np.arange(0,.001,1/e_sr)
    f_on = np.exp(t/tau_1_on) 
    f_on = f_on/np.max(f_on)
    kernel = np.concatenate([f_on,np.zeros(len(t))])[::-1]
    kernel  = kernel /sum(kernel )  
    i_conv = np.convolve(i,kernel,'same')
    v_comp = (v - i_conv*uncompensatedRS*10**6)*1000 - junction_potential
    i = i * 10**12
    
    sweep_start_time  = float((ephys_patch.Sweep()&key_sweep).fetch('sweep_start_time')) 
    trace_t = np.arange(len(v))/e_sr + sweep_start_time
    
    downsample_factor = int(np.round(e_sr/downsampled_rate))
    #%downsampling
    v_out = moving_average(v_comp, n=downsample_factor)
    v_out = v_out[int(downsample_factor/2)::downsample_factor]
    i_out = moving_average(i, n=downsample_factor)
    i_out = i_out[int(downsample_factor/2)::downsample_factor]
    t_out = moving_average(trace_t, n=downsample_factor)
    t_out = t_out[int(downsample_factor/2)::downsample_factor]
    
    return v_out, i_out, t_out
예제 #6
0
    def make(self, key):
        counter = 0
        #%%
        sigma = .00003  # seconds for filering
        step_time = .0001  # seconds
        threshold_value = 5  # mV/ms
        baseline_length = .01  #s back from threshold
        #%%
        # =============================================================================
        #         key = {'subject_id': 454263, 'session': 1, 'cell_number': 1, 'sweep_number': 56, 'ap_num': 30}
        #         print(key)
        # =============================================================================
        keynow = key.copy()
        del keynow['ap_num']
        pd_sweep = pd.DataFrame(
            (ephys_patch.Sweep() & key) * (SweepResponseCorrected() & key) *
            (ephys_patch.SweepStimulus() & key) *
            (ephys_patch.SweepMetadata() & key))
        pd_ap = pd.DataFrame(ActionPotential() & keynow)
        #%%
        if len(pd_ap) > 0 and len(ActionPotentialDetails()
                                  & dict(pd_ap.loc[0])) == 0:
            #%%
            #print(key)
            trace = pd_sweep['response_trace_corrected'].values[0]
            sr = pd_sweep['sample_rate'][0]
            si = 1 / sr
            step_size = int(np.round(step_time / si))
            ms5_step = int(np.round(.005 / si))
            trace_f = ndimage.gaussian_filter(trace, sigma / si)
            d_trace_f = np.diff(trace_f) / si
            tracelength = len(trace)
            baseline_step = int(np.round(baseline_length / si))
            #%%
            keylist = list()
            for ap_now in pd_ap.iterrows():
                counter += 1
                if counter % 2 == 0:
                    dj.conn().ping()
                ap_now = dict(ap_now[1])
                ap_max_index = ap_now['ap_max_index']
                dvmax_index = ap_max_index
                while dvmax_index > step_size * 2 and trace_f[dvmax_index] > 0:
                    dvmax_index -= step_size
                while dvmax_index > step_size * 2 and dvmax_index < tracelength - step_size and np.max(
                        d_trace_f[dvmax_index - step_size:dvmax_index]
                ) > np.max(d_trace_f[dvmax_index:dvmax_index + step_size]):
                    dvmax_index -= step_size
                if dvmax_index < tracelength - 1:
                    dvmax_index = dvmax_index + np.argmax(
                        d_trace_f[dvmax_index:dvmax_index + step_size])
                else:
                    dvmax_index = tracelength - 2

                dvmin_index = ap_max_index
                #%
                while dvmin_index < tracelength - step_size and (
                        trace_f[dvmin_index] > 0 or
                        np.min(d_trace_f[np.max([dvmin_index -
                                                 step_size, 0]):dvmin_index]) >
                        np.min(
                            d_trace_f[dvmin_index:dvmin_index + step_size])):
                    dvmin_index += step_size
                    #%
                dvmin_index -= step_size
                dvmin_index = dvmin_index + np.argmin(
                    d_trace_f[dvmin_index:dvmin_index + step_size])

                thresh_index = dvmax_index
                while thresh_index > step_size * 2 and (np.min(
                        d_trace_f[thresh_index - step_size:thresh_index]) >
                                                        threshold_value):
                    thresh_index -= step_size
                thresh_index = thresh_index - np.argmax(
                    (d_trace_f[np.max([0, thresh_index - step_size]
                                      ):thresh_index] < threshold_value)[::-1])
                ap_threshold = trace_f[thresh_index]
                ap_amplitude = trace_f[ap_max_index] - ap_threshold
                hw_step_back = np.argmax(
                    trace_f[ap_max_index:np.max([ap_max_index -
                                                 ms5_step, 0]):-1] <
                    ap_threshold + ap_amplitude / 2)
                hw_step_forward = np.argmax(
                    trace_f[ap_max_index:ap_max_index +
                            ms5_step] < ap_threshold + ap_amplitude / 2)
                ap_halfwidth = (hw_step_back + hw_step_forward) * si

                if ap_amplitude > .01 and ap_halfwidth > .0001:
                    ap_now['ap_real'] = 1
                else:
                    ap_now['ap_real'] = 0
                ap_now['ap_threshold'] = ap_threshold * 1000
                ap_now['ap_threshold_index'] = thresh_index
                if thresh_index > 10:
                    ap_now['ap_baseline_value'] = np.mean(
                        trace_f[np.max([thresh_index - baseline_step, 0]
                                       ):thresh_index]) * 1000
                else:
                    ap_now['ap_baseline_value'] = ap_threshold * 1000
                ap_now['ap_halfwidth'] = ap_halfwidth * 1000
                ap_now['ap_amplitude'] = ap_amplitude * 1000
                ap_now['ap_dv_max'] = d_trace_f[dvmax_index]
                ap_now['ap_dv_max_voltage'] = trace_f[dvmax_index] * 1000
                ap_now['ap_dv_min'] = d_trace_f[dvmin_index]
                ap_now['ap_dv_min_voltage'] = trace_f[dvmin_index] * 1000
                del ap_now['ap_max_index']
                del ap_now['ap_max_time']
                keylist.append(ap_now)
                #%%
            self.insert(keylist, skip_duplicates=True)
예제 #7
0
def populateelphys():
    #%%
    df_subject_wr_sessions = pd.DataFrame(lab.WaterRestriction() *
                                          experiment.Session() *
                                          experiment.SessionDetails)
    df_subject_ids = pd.DataFrame(lab.Subject())
    if len(df_subject_wr_sessions) > 0:
        subject_names = df_subject_wr_sessions[
            'water_restriction_number'].unique()
        subject_names.sort()
    else:
        subject_names = list()
    subject_ids = df_subject_ids['subject_id'].unique()
    #%
    sumdata = list()
    basedir = Path(dj.config['locations.elphysdata_acq4'])
    for setup_dir in basedir.iterdir():
        setup_name = setup_dir.name
        sessions = np.sort(
            os.listdir(setup_dir)
        )  #configfile.readConfigFile(setup_dir.joinpath('.index'))
        for session_acq in sessions[::-1]:  #.keys():
            if session_acq != '.' and session_acq != 'log.txt':
                session_dir = setup_dir.joinpath(session_acq)
                try:
                    cells = configfile.readConfigFile(
                        session_dir.joinpath('.index'))
                except:  # if there is no file
                    cells = None
                if cells and 'WR_name/ID' in cells['.'].keys(
                ):  # it needs to have WRname
                    wrname_ephys = cells['.']['WR_name/ID']
                    wrname = None
                    for wrname_potential in subject_names:  # look for water restriction number
                        if wrname_potential.lower() in wrname_ephys.lower():
                            wrname = wrname_potential
                            subject_id = (df_subject_wr_sessions.loc[
                                df_subject_wr_sessions[
                                    'water_restriction_number'] == wrname,
                                'subject_id']).unique()[0]
                    if wrname == None:  # look for animal identifier:
                        for wrname_potential in subject_ids:  # look for water restriction number
                            if str(wrname_potential) in wrname_ephys.lower():
                                subject_id = wrname_potential
                                if len(df_subject_wr_sessions) > 0 and len(
                                    (df_subject_wr_sessions.loc[
                                        df_subject_wr_sessions['subject_id'] ==
                                        subject_id, 'water_restriction_number']
                                     ).unique()) > 0:
                                    wrname = (df_subject_wr_sessions.loc[
                                        df_subject_wr_sessions['subject_id'] ==
                                        subject_id, 'water_restriction_number']
                                              ).unique()[0]
                                else:
                                    wrname = 'no water restriction number for this mouse'

                    if wrname:
                        session_date = (
                            session_acq[0:session_acq.find('_')]).replace(
                                '.', '-')

                        print('animal: ' + str(subject_id) + '  -  ' +
                              wrname)  ##
                        if setup_name == 'Voltage_rig_1P':
                            setupname = 'Voltage-Imaging-1p'
                        else:
                            print('unkwnown setup, please add')
                            timer.wait(1000)
                        if 'experimenter' in cells['.'].keys():
                            username = cells['.']['experimenter']
                        else:
                            username = '******'
                            print(
                                'username not specified in acq4 file, assuming rozsam'
                            )
                        ### check if session already exists
                        sessiondata = {
                            'subject_id':
                            subject_id,  #(lab.WaterRestriction() & 'water_restriction_number = "'+df_behavior_session['subject'][0]+'"').fetch()[0]['subject_id'],
                            'session': np.nan,
                            'session_date': session_date,
                            'session_time':
                            np.nan,  #session_time.strftime('%H:%M:%S'),
                            'username': username,
                            'rig': setupname
                        }
                        for cell in cells.keys():
                            if cell != '.' and cell != 'log.txt':
                                ephisdata_cell = list()
                                sweepstarttimes = list()
                                cell_dir = session_dir.joinpath(cell)
                                serieses = configfile.readConfigFile(
                                    cell_dir.joinpath('.index'))
                                cellstarttime = datetime.datetime.fromtimestamp(
                                    serieses['.']['__timestamp__'])
                                for series in serieses.keys():
                                    if series != '.' and series != 'log.txt':
                                        series_dir = cell_dir.joinpath(series)
                                        sweeps = configfile.readConfigFile(
                                            series_dir.joinpath('.index'))
                                        if 'Clamp1.ma' in sweeps.keys():
                                            protocoltype = 'single sweep'
                                            sweepkeys = ['']
                                        else:
                                            protocoltype = 'multiple sweeps'
                                            sweepkeys = sweeps.keys()
                                        for sweep in sweepkeys:
                                            if sweep != '.' and '.txt' not in sweep and '.ma' not in sweep:
                                                sweep_dir = series_dir.joinpath(
                                                    sweep)
                                                sweepinfo = configfile.readConfigFile(
                                                    sweep_dir.joinpath(
                                                        '.index'))
                                                if sweep == '':
                                                    sweep = '0'
                                                for file in sweepinfo.keys():
                                                    if '.ma' in file:
                                                        try:  # old file version

                                                            #print('new file version')
                                                            #%
                                                            ephysfile = h5.File(
                                                                sweep_dir.
                                                                joinpath(file),
                                                                "r")
                                                            data = ephysfile[
                                                                'data'][()]
                                                            metadata_h5 = ephysfile[
                                                                'info']
                                                            metadata = read_h5f_metadata(
                                                                metadata_h5)
                                                            daqchannels = list(
                                                                metadata[2]
                                                                ['DAQ'].keys())
                                                            sweepstarttime = datetime.datetime.fromtimestamp(
                                                                metadata[2]
                                                                ['DAQ']
                                                                [daqchannels[
                                                                    0]]
                                                                ['startTime'])
                                                            relativetime = (
                                                                sweepstarttime
                                                                - cellstarttime
                                                            ).total_seconds()
                                                            if len(
                                                                    ephisdata_cell
                                                            ) > 0 and ephisdata_cell[
                                                                    -1]['sweepstarttime'] == sweepstarttime:
                                                                ephisdata = ephisdata_cell.pop(
                                                                )
                                                            else:
                                                                ephisdata = dict(
                                                                )
                                                            if 'primary' in daqchannels:  # ephys data
                                                                ephisdata[
                                                                    'V'] = data[
                                                                        1]
                                                                ephisdata[
                                                                    'stim'] = data[
                                                                        0]
                                                                ephisdata[
                                                                    'data'] = data
                                                                ephisdata[
                                                                    'metadata'] = metadata
                                                                ephisdata[
                                                                    'time'] = metadata[
                                                                        1]['values']
                                                                ephisdata[
                                                                    'relativetime'] = relativetime
                                                                ephisdata[
                                                                    'sweepstarttime'] = sweepstarttime
                                                                ephisdata[
                                                                    'series'] = series
                                                                ephisdata[
                                                                    'sweep'] = sweep
                                                                sweepstarttimes.append(
                                                                    sweepstarttime
                                                                )
                                                            else:  # other daq stuff
                                                                #%
                                                                for idx, channel in enumerate(
                                                                        metadata[
                                                                            0]
                                                                    ['cols']):
                                                                    channelname = channel[
                                                                        'name'].decode(
                                                                        )
                                                                    if channelname[
                                                                            0] == 'u':
                                                                        channelname = channelname[
                                                                            2:
                                                                            -1]
                                                                        if channelname in [
                                                                                'OrcaFlashExposure',
                                                                                'Temperature',
                                                                                'LED525',
                                                                                'FrameCommand',
                                                                                'NextFileTrigger'
                                                                        ]:
                                                                            ephisdata[
                                                                                channelname] = data[
                                                                                    idx]
                                                                            #print('{} added'.format(channelname))
                                                                        else:
                                                                            print(
                                                                                'waiting in the other daq'
                                                                            )
                                                                            timer.sleep(
                                                                                1000
                                                                            )
                                                            ephisdata_cell.append(
                                                                ephisdata)
                                                            #%

                                                        except:  # new file version
                                                            print(
                                                                'old version')
                                                            ephysfile = MetaArray(
                                                            )
                                                            ephysfile.readFile(
                                                                sweep_dir.
                                                                joinpath(file))
                                                            data = ephysfile.asarray(
                                                            )
                                                            metadata = ephysfile.infoCopy(
                                                            )
                                                            sweepstarttime = datetime.datetime.fromtimestamp(
                                                                metadata[2]
                                                                ['startTime'])
                                                            relativetime = (
                                                                sweepstarttime
                                                                - cellstarttime
                                                            ).total_seconds()
                                                            ephisdata = dict()
                                                            ephisdata[
                                                                'V'] = data[1]
                                                            ephisdata[
                                                                'stim'] = data[
                                                                    0]
                                                            ephisdata[
                                                                'data'] = data
                                                            ephisdata[
                                                                'metadata'] = metadata
                                                            ephisdata[
                                                                'time'] = metadata[
                                                                    1]['values']
                                                            ephisdata[
                                                                'relativetime'] = relativetime
                                                            ephisdata[
                                                                'sweepstarttime'] = sweepstarttime
                                                            ephisdata[
                                                                'series'] = series
                                                            ephisdata[
                                                                'sweep'] = sweep
                                                            sweepstarttimes.append(
                                                                sweepstarttime)
                                                            ephisdata_cell.append(
                                                                ephisdata)

    # ============================================================================
    #                             if wrname == 'FOR04':
    # =============================================================================
    # add session to DJ if not present
                                if len(ephisdata_cell) > 0:

                                    # =============================================================================
                                    #                                     print('waiting')
                                    #                                     timer.sleep(1000)
                                    # =============================================================================
                                    #%
                                    if len(experiment.Session()
                                           & 'subject_id = "' +
                                           str(sessiondata['subject_id']) + '"'
                                           & 'session_date = "' +
                                           str(sessiondata['session_date']) +
                                           '"') == 0:
                                        if len(experiment.Session()
                                               & 'subject_id = "' +
                                               str(sessiondata['subject_id']) +
                                               '"') == 0:
                                            sessiondata['session'] = 1
                                        else:
                                            sessiondata['session'] = len(
                                                (experiment.Session()
                                                 & 'subject_id = "' +
                                                 str(sessiondata['subject_id'])
                                                 + '"').fetch()['session']) + 1
                                        sessiondata['session_time'] = (
                                            sweepstarttimes[0]
                                        ).strftime(
                                            '%H:%M:%S'
                                        )  # the time of the first sweep will be the session time
                                        experiment.Session().insert1(
                                            sessiondata)
                                    #%
                                    session = (
                                        experiment.Session()
                                        & 'subject_id = "' +
                                        str(sessiondata['subject_id']) + '"'
                                        & 'session_date = "' +
                                        str(sessiondata['session_date']) +
                                        '"').fetch('session')[0]
                                    cell_number = int(cell[cell.find('_') +
                                                           1:])
                                    #add cell if not added already
                                    celldata = {
                                        'subject_id': subject_id,
                                        'session': session,
                                        'cell_number': cell_number,
                                    }
                                    #%
                                    if len(ephys_patch.Cell() & celldata
                                           ) == 0 or len(ephys_patch.Cell() *
                                                         ephys_patch.Sweep()
                                                         & celldata) < len(
                                                             ephisdata_cell):
                                        if len(ephys_patch.Cell() *
                                               ephys_patch.Sweep() & celldata
                                               ) < len(ephisdata_cell):
                                            print('finishing a recording:')
                                        else:
                                            print('adding new recording:')
                                        print(celldata)
                                        if 'type' in serieses['.'].keys():
                                            if serieses['.'][
                                                    'type'] == 'interneuron':
                                                celldata['cell_type'] = 'int'
                                            elif serieses['.'][
                                                    'type'] == 'unknown' or serieses[
                                                        '.']['type'] == 'fail':
                                                celldata[
                                                    'cell_type'] = 'unidentified'
                                            else:
                                                print('unhandled cell type!!')
                                                timer.sleep(1000)
                                        else:
                                            celldata[
                                                'cell_type'] = 'unidentified'
                                        celldata['cell_recording_start'] = (
                                            sweepstarttimes[0]
                                        ).strftime('%H:%M:%S')
                                        if 'depth' in serieses['.'].keys(
                                        ) and len(serieses['.']['depth']) > 0:
                                            celldata['depth'] = int(
                                                serieses['.']['depth'])
                                        else:
                                            celldata['depth'] = -1
                                        try:
                                            ephys_patch.Cell().insert1(
                                                celldata,
                                                allow_direct_insert=True)
                                        except dj.errors.DuplicateError:
                                            pass  #already uploaded
                                        if 'notes' in serieses['.'].keys():
                                            cellnotes = serieses['.']['notes']
                                        else:
                                            cellnotes = ''
                                        cellnotesdata = {
                                            'subject_id': subject_id,
                                            'session': session,
                                            'cell_number': cell_number,
                                            'notes': cellnotes
                                        }
                                        try:
                                            ephys_patch.CellNotes().insert1(
                                                cellnotesdata,
                                                allow_direct_insert=True)
                                        except dj.errors.DuplicateError:
                                            pass  #already uploaded

                                        #%
                                        for i, ephisdata in enumerate(
                                                ephisdata_cell):

                                            #%
                                            sweep_number = i
                                            print('sweep {}'.format(
                                                sweep_number))
                                            sweep_data = {
                                                'subject_id':
                                                subject_id,
                                                'session':
                                                session,
                                                'cell_number':
                                                cell_number,
                                                'sweep_number':
                                                sweep_number,
                                                'sweep_start_time':
                                                (ephisdata['sweepstarttime'] -
                                                 sweepstarttimes[0]
                                                 ).total_seconds(),
                                                'sweep_end_time':
                                                (ephisdata['sweepstarttime'] -
                                                 sweepstarttimes[0]
                                                 ).total_seconds() +
                                                ephisdata['time'][-1],
                                                'protocol_name':
                                                ephisdata[
                                                    'series'],  #[:ephisdata['series'].find('_')],
                                                'protocol_sweep_number':
                                                int(ephisdata['sweep'])
                                            }

                                            if 'mode' in ephisdata['metadata'][
                                                    2]['ClampState']:  # old file version
                                                recmode = ephisdata[
                                                    'metadata'][2][
                                                        'ClampState']['mode']
                                            else:
                                                recmode = ephisdata[
                                                    'metadata'][2]['Protocol'][
                                                        'mode']

                                            if 'IC' in str(recmode):
                                                recording_mode = 'current clamp'
                                            else:
                                                print(
                                                    'unhandled recording mode, please act..'
                                                )
                                                timer.sleep(10000)

                                            channelnames = list()
                                            channelunits = list()
                                            for line_now in ephisdata[
                                                    'metadata'][0]['cols']:
                                                if type(line_now['name']
                                                        ) == bytes:
                                                    channelnames.append(
                                                        line_now['name'].
                                                        decode().strip("'"))
                                                    channelunits.append(
                                                        line_now['units'].
                                                        decode().strip("'"))
                                                else:
                                                    channelnames.append(
                                                        line_now['name'])
                                                    channelunits.append(
                                                        line_now['units'])
                                            commandidx = np.where(
                                                np.array(channelnames) ==
                                                'command')[0][0]
                                            dataidx = np.where(
                                                np.array(channelnames) ==
                                                'primary')[0][0]
                                            #%
                                            clampparams_data = ephisdata[
                                                'metadata'][2]['ClampState'][
                                                    'ClampParams'].copy()
                                            clampparams_data_new = dict()
                                            for clampparamkey in clampparams_data.keys(
                                            ):  #6004 is true for some reason.. changing it back to 1
                                                if type(clampparams_data[
                                                        clampparamkey]
                                                        ) == np.int32:
                                                    if clampparams_data[
                                                            clampparamkey] > 0:
                                                        clampparams_data[
                                                            clampparamkey] = int(
                                                                1)
                                                    else:
                                                        clampparams_data[
                                                            clampparamkey] = int(
                                                                0)
                                                else:
                                                    clampparams_data[
                                                        clampparamkey] = float(
                                                            clampparams_data[
                                                                clampparamkey])
                                                clampparams_data_new[
                                                    clampparamkey.lower(
                                                    )] = clampparams_data[
                                                        clampparamkey]
                                                #%
                                            sweepmetadata_data = {
                                                'subject_id':
                                                subject_id,
                                                'session':
                                                session,
                                                'cell_number':
                                                cell_number,
                                                'sweep_number':
                                                sweep_number,
                                                'recording_mode':
                                                recording_mode,
                                                'sample_rate':
                                                np.round(1 / np.median(
                                                    np.diff(
                                                        ephisdata['metadata']
                                                        [1]['values'])))
                                            }
                                            sweepmetadata_data.update(
                                                clampparams_data_new)
                                            sweepdata_data = {
                                                'subject_id':
                                                subject_id,
                                                'session':
                                                session,
                                                'cell_number':
                                                cell_number,
                                                'sweep_number':
                                                sweep_number,
                                                'response_trace':
                                                ephisdata['data'][dataidx, :],
                                                'response_units':
                                                ephisdata['metadata'][0]
                                                ['cols'][dataidx]['units']
                                            }

                                            sweepstimulus_data = {
                                                'subject_id':
                                                subject_id,
                                                'session':
                                                session,
                                                'cell_number':
                                                cell_number,
                                                'sweep_number':
                                                sweep_number,
                                                'stimulus_trace':
                                                ephisdata['data'][
                                                    commandidx, :],
                                                'stimulus_units':
                                                ephisdata['metadata'][0]
                                                ['cols'][commandidx]['units']
                                            }
                                            #print('waiting')
                                            #timer.sleep(10000)
                                            try:
                                                ephys_patch.Sweep().insert1(
                                                    sweep_data,
                                                    allow_direct_insert=True)
                                            except dj.errors.DuplicateError:
                                                pass  #already uploaded
                                            try:  # maybe it's a duplicate..
                                                ephys_patch.ClampParams(
                                                ).insert1(
                                                    clampparams_data_new,
                                                    allow_direct_insert=True)
                                            except dj.errors.DuplicateError:
                                                pass  #already uploaded
                                            try:
                                                ephys_patch.SweepMetadata(
                                                ).insert1(
                                                    sweepmetadata_data,
                                                    allow_direct_insert=True)
                                            except dj.errors.DuplicateError:
                                                pass  #already uploaded
                                            try:
                                                ephys_patch.SweepResponse(
                                                ).insert1(
                                                    sweepdata_data,
                                                    allow_direct_insert=True)
                                            except dj.errors.DuplicateError:
                                                pass  #already uploaded
                                            try:
                                                ephys_patch.SweepStimulus(
                                                ).insert1(
                                                    sweepstimulus_data,
                                                    allow_direct_insert=True)
                                            except dj.errors.DuplicateError:
                                                pass  #already uploaded
                                            #%
                                            if 'OrcaFlashExposure' in ephisdata.keys(
                                            ):
                                                sweepimagingexposuredata = {
                                                    'subject_id':
                                                    subject_id,
                                                    'session':
                                                    session,
                                                    'cell_number':
                                                    cell_number,
                                                    'sweep_number':
                                                    sweep_number,
                                                    'imaging_exposure_trace':
                                                    ephisdata[
                                                        'OrcaFlashExposure']
                                                }
                                                try:
                                                    ephys_patch.SweepImagingExposure(
                                                    ).insert1(
                                                        sweepimagingexposuredata,
                                                        allow_direct_insert=True
                                                    )
                                                except dj.errors.DuplicateError:
                                                    pass  #already uploaded
                                            if 'Temperature' in ephisdata.keys(
                                            ):
                                                sweeptemperaturedata = {
                                                    'subject_id':
                                                    subject_id,
                                                    'session':
                                                    session,
                                                    'cell_number':
                                                    cell_number,
                                                    'sweep_number':
                                                    sweep_number,
                                                    'temperature_trace':
                                                    ephisdata['Temperature'] *
                                                    10,
                                                    'temperature_units':
                                                    'degC'
                                                }
                                                try:
                                                    ephys_patch.SweepTemperature(
                                                    ).insert1(
                                                        sweeptemperaturedata,
                                                        allow_direct_insert=True
                                                    )
                                                except dj.errors.DuplicateError:
                                                    pass  #already uploaded
                                            if 'LED525' in ephisdata.keys():
                                                sweepLEDdata = {
                                                    'subject_id':
                                                    subject_id,
                                                    'session':
                                                    session,
                                                    'cell_number':
                                                    cell_number,
                                                    'sweep_number':
                                                    sweep_number,
                                                    'imaging_led_trace':
                                                    ephisdata['LED525']
                                                }
                                                try:
                                                    ephys_patch.SweepLED(
                                                    ).insert1(
                                                        sweepLEDdata,
                                                        allow_direct_insert=True
                                                    )
                                                except dj.errors.DuplicateError:
                                                    pass  #already uploaded
예제 #8
0
     uncompensatedRS =  float((ephysanal.SweepSeriesResistance()&key_sweep).fetch1('series_resistance_residual'))
 except:
     uncompensatedRS = 0
 v = (ephys_patch.SweepResponse()&key_sweep).fetch1('response_trace')
 i = (ephys_patch.SweepStimulus()&key_sweep).fetch1('stimulus_trace')
 tau_1_on =.1/1000
 t = np.arange(0,.001,1/e_sr)
 f_on = np.exp(t/tau_1_on) 
 f_on = f_on/np.max(f_on)
 kernel = np.concatenate([f_on,np.zeros(len(t))])[::-1]
 kernel  = kernel /sum(kernel )  
 i_conv = np.convolve(i,kernel,'same')
 v_comp = (v - i*uncompensatedRS*10**6)*1000 - junction_potential
 i = i * 10**12
 
 sweep_start_time  = float((ephys_patch.Sweep()&key_sweep).fetch('sweep_start_time')) 
 trace_t = np.arange(len(v))/e_sr + sweep_start_time
 
 downsample_factor = int(np.round(e_sr/downsampled_rate))
 #%downsampling
 v_out = moving_average(v_comp, n=downsample_factor)
 v_out = v_out[int(downsample_factor/2)::downsample_factor]
 i_out = moving_average(i, n=downsample_factor)
 i_out = i_out[int(downsample_factor/2)::downsample_factor]
 t_out = moving_average(trace_t, n=downsample_factor)
 t_out = t_out[int(downsample_factor/2)::downsample_factor]
 
 sweepdata_out.append(np.asarray([t_out,v_out,i_out]))
 sweep_dict = dict((ephys_patch.SweepMetadata()*ephys_patch.Sweep()&key_sweep).fetch1())
 for keynow in sweep_dict.keys():
     if type(sweep_dict[keynow]) == decimal.Decimal:
예제 #9
0
                                                   'cell_number',
                                                   'movie_number',
                                                   'motion_correction_method',
                                                   'roi_type',
                                                   'roi_number',
                                                   as_dict=True)
for roi in gtrois:
    session_time = (experiment.Session() & roi).fetch('session_time')[0]
    cell_time = (ephys_patch.Cell() & roi).fetch('cell_recording_start')[0]
    movie_start_time = float(
        (imaging.Movie() & roi).fetch1('movie_start_time'))
    movie_start_time = session_time.total_seconds(
    ) + movie_start_time - cell_time.total_seconds()

    sweeps = (imaging_gt.ROIEphysCorrelation() & roi).fetch('sweep_number')
    sweep_now = ephys_patch.Sweep() & roi & 'sweep_number = ' + str(sweeps[0])
    trace, sr = ((ephys_patch.SweepResponse() * ephys_patch.SweepMetadata())
                 & sweep_now).fetch1('response_trace', 'sample_rate')
    sweep_start_time = float(sweep_now.fetch1('sweep_start_time'))
    trace_time = np.arange(len(trace)) / sr + sweep_start_time
    neededidx = (trace_time > movie_start_time - window) & (trace_time <
                                                            movie_start_time)
    fig = plt.figure()
    ax = fig.add_axes([0, 0, .8, .8])
    ax.plot(trace_time[neededidx], trace[neededidx])
    print(roi)

    #fig.show()

# =============================================================================
#         print(key_cell)