示例#1
0
def build_stimulus_table(stimulus_pkl_path, sync_h5_path, frame_time_strategy,
                         minimum_spontaneous_activity_duration,
                         extract_const_params_from_repr, drop_const_params,
                         maximum_expected_spontanous_activity_duration,
                         stimulus_name_map, column_name_map,
                         output_stimulus_table_path, output_frame_times_path,
                         fail_on_negative_duration, **kwargs):
    stim_file = CamStimOnePickleStimFile.factory(stimulus_pkl_path)

    sync_dataset = EcephysSyncDataset.factory(sync_h5_path)
    frame_times = sync_dataset.extract_frame_times(
        strategy=frame_time_strategy)

    def seconds_to_frames(seconds):
        return  \
            (np.array(seconds) + stim_file.pre_blank_sec) * \
            stim_file.frames_per_second

    minimum_spontaneous_activity_duration = (
        minimum_spontaneous_activity_duration / stim_file.frames_per_second)

    stimulus_tabler = functools.partial(
        ephys_pre_spikes.build_stimuluswise_table,
        seconds_to_frames=seconds_to_frames,
        extract_const_params_from_repr=extract_const_params_from_repr,
        drop_const_params=drop_const_params,
    )
    spon_tabler = functools.partial(
        ephys_pre_spikes.make_spontaneous_activity_tables,
        duration_threshold=minimum_spontaneous_activity_duration,
    )

    stim_table_full = ephys_pre_spikes.create_stim_table(
        stim_file.stimuli, stimulus_tabler, spon_tabler)
    stim_table_full = ephys_pre_spikes.apply_frame_times(
        stim_table_full, frame_times, stim_file.frames_per_second, True)

    output_validation.validate_epoch_durations(
        stim_table_full, fail_on_negative_durations=fail_on_negative_duration)
    output_validation.validate_max_spontaneous_epoch_duration(
        stim_table_full, maximum_expected_spontanous_activity_duration)

    stim_table_full = naming_utilities.collapse_columns(stim_table_full)
    stim_table_full = naming_utilities.drop_empty_columns(stim_table_full)
    stim_table_full = naming_utilities.standardize_movie_numbers(
        stim_table_full)
    stim_table_full = naming_utilities.add_number_to_shuffled_movie(
        stim_table_full)
    stim_table_full = naming_utilities.map_stimulus_names(
        stim_table_full, stimulus_name_map)
    stim_table_full = naming_utilities.map_column_names(
        stim_table_full, column_name_map)

    stim_table_full.to_csv(output_stimulus_table_path, index=False)
    np.save(output_frame_times_path, frame_times, allow_pickle=False)
    return {
        "output_path": output_stimulus_table_path,
        "output_frame_times_path": output_frame_times_path,
    }
示例#2
0
def build_opto_table(args):

    opto_file = pd.read_pickle(args['opto_pickle_path'])
    sync_file = EcephysSyncDataset.factory(args['sync_h5_path'])

    start_times = sync_file.extract_led_times()
    conditions = [str(item) for item in opto_file['opto_conditions']]
    levels = opto_file['opto_levels']

    assert len(conditions) == len(levels)
    if len(start_times) > len(conditions):
        raise ValueError(
            f"there are {len(start_times) - len(conditions)} extra optotagging sync times!"
        )

    optotagging_table = pd.DataFrame({
        'start_time': start_times,
        'condition': conditions,
        'level': levels
    })
    optotagging_table = optotagging_table.sort_values(by='start_time', axis=0)

    stop_times = []
    names = []
    conditions = []
    for ii, row in optotagging_table.iterrows():
        condition = args["conditions"][row["condition"]]
        stop_times.append(row["start_time"] + condition["duration"])
        names.append(condition["name"])
        conditions.append(condition["condition"])

    optotagging_table["stop_time"] = stop_times
    optotagging_table["stimulus_name"] = names
    optotagging_table["condition"] = conditions
    optotagging_table["duration"] = optotagging_table[
        "stop_time"] - optotagging_table["start_time"]

    optotagging_table.to_csv(args['output_opto_table_path'], index=False)
    return {'output_opto_table_path': args['output_opto_table_path']}
def test_factory():

    with mock.patch('allensdk.brain_observatory.sync_dataset.Dataset.load') as p:
        dataset = EcephysSyncDataset.factory('foo')
        p.assert_called_with('foo')