Exemplo n.º 1
0
def run_behaving(
    rs=None,
    output_dir="/media/dendrite",
    all_channels_file="../DO1_ALL_CHANNELS",
    channel_groups_file="../DO1_CHANNEL_GROUPS",
    analog_channels_file="../ANALOG_7_8",
    ns5_filename=None,
    remove_from_TC=None,
    soft_time_limits=(-2.0, 2.0),
    hard_time_limits=(-0.04, 0.15),
    do_add_bcontrol=True,
    bcontrol_folder="/media/hippocampus/TRWINRIG_DATA/Data/chris/CR20B",
    do_timestamps=True,
    do_add_btrial_numbers=True,
    force_put_neural_data=False,
    do_avg_plots=True,
    do_extract_spikes=True,
    CAR=True,
    save_to_klusters=True,
    do_MUA_grand_plot=True,
    do_MUA_plot_by_stimulus=True,
    do_run_klustakwik=True,
    do_SUA_plot_by_stimulus=True,
    group_multiplier=100,
    psth_time_limits=(None, None),
    lfp_time_limits="hard",
    **kwargs
):
    if len(kwargs) > 0:
        print "unexpected kwargs"
        print kwargs

    # Make session
    if rs is None:
        printnow("creating recording session %s" % ns5_filename)
        rsm = rswrap.RecordingSessionMaker(
            data_analysis_dir=output_dir,
            all_channels_file=all_channels_file,
            channel_groups_file=channel_groups_file,
            analog_channels_file=analog_channels_file,
        )

        if remove_from_TC is None:
            remove_from_TC = []

        rs = rsm.make_session(ns5_filename=ns5_filename, remove_from_TC=remove_from_TC)

        rs.write_time_limits(soft_time_limits, hard_time_limits)
    rs.group_multiplier = group_multiplier
    printnow("RS %s" % rs.full_path)

    # add bcontrol
    if do_add_bcontrol:
        printnow("adding bcontrol")
        rswrap.add_bcontrol_data_to_session(bcontrol_folder, rs, verbose=True)

    # add timestamps
    if do_timestamps:
        printnow("adding timestamps")
        # write timestamps to directory
        times, numbers = rswrap.add_timestamps_to_session(rs, verbose=True, meth="digital_trial_number")

    # put in neural db (does nothing if exists unless forced)
    printnow("putting neural data")
    rs.put_neural_data_into_db(verbose=True, force=force_put_neural_data)

    # adding btrial numbers
    if do_add_btrial_numbers:
        printnow("adding btrial numbers")
        if len(numbers) == 0:
            print "warning: no numbers?"
            numbers = None  # will auto-determine if all is well
        rswrap.add_behavioral_trial_numbers2(rs, known_trial_numbers=numbers, trial_number_channel=16, verbose=True)

    # plot averages
    if do_avg_plots:
        printnow("avg plots")
        if lfp_time_limits == "hard":
            lfp_time_limits = rs.read_time_limits()[1]

        rswrap.plot_avg_lfp(rs, savefig=True, t_start=lfp_time_limits[0], t_stop=lfp_time_limits[1])
        rswrap.plot_avg_audio(rs, savefig=True)

    # spike extract
    if do_extract_spikes:
        printnow("extracting spikes")
        rs.generate_spike_block(CAR=CAR, smooth_spikes=False, verbose=True)
        rs.run_spikesorter(save_to_db=True, save_to_klusters=save_to_klusters)
        rs.spiketime_dump()

    # plot MUA stuff
    if do_MUA_grand_plot:
        printnow("mua grand psths")
        rswrap.plot_all_spike_psths(rs, savefig=True)

    # plot MUA by stim
    if do_MUA_plot_by_stimulus:
        printnow("mua by stimulus")
        t_start, t_stop = psth_time_limits
        rswrap.plot_MUA_by_stim(rs, savefig=True, t_start=t_start, t_stop=t_stop)

    # spike sort
    if do_run_klustakwik:
        printnow("running klustakwik")
        rs.group_multiplier = group_multiplier
        rs.run_klustakwik()
        rs.prep_for_klusters(verbose=True)

    # SUA by stim
    if do_SUA_plot_by_stimulus:
        printnow("SUA plot by stimulus")
        t_start, t_stop = psth_time_limits
        rswrap.plot_all_spike_psths_by_stim(
            rs, savefig=True, t_start=t_start, t_stop=t_stop, skipNoScore=False, override_path=rs.last_klusters_dir()
        )

    return rs