def main(traj_limit=None):
    # add script execution to database
    add_script_execution(SCRIPTID, session=session, multi_use=False, notes=SCRIPTNOTES)

    for sim_id_template in SIMULATION_IDS:
        for expt in EXPERIMENTS:
            for odor_state in ODOR_STATES:

                sim_id = sim_id_template.format(expt, odor_state)

                print(sim_id)

                sim = session.query(models.Simulation).get(sim_id)

                # get the position indexes for all time points for all trials
                pos_idxs = []
                for trial in sim.trials[:traj_limit]:
                    tps = trial.get_timepoints(session)
                    pos_idxs += [np.array([(tp.xidx, tp.yidx, tp.zidx) for tp in tps])]

                pos_idxs = np.concatenate(pos_idxs, axis=0)
                pos = np.array([sim.env.pos_from_idx(pos_idx) for pos_idx in pos_idxs])

                # build the histogram
                bins = (sim.env.xbins, sim.env.ybins, sim.env.zbins)
                pos_histogram, _ = np.histogramdd(pos, bins=bins)

                # create the data model and store it
                pos_hist_data_model = models.SimulationAnalysisPositionHistogram()
                pos_hist_data_model.simulation = sim

                pos_hist_data_model.store_data(session, pos_histogram.astype(int))
                session.add(pos_hist_data_model)

                session.commit()
def main():

    add_script_execution(SCRIPT_ID, session=session, notes=SCRIPT_NOTES)

    for expt in EXPERIMENTS:
        for odor_state in ODOR_STATES:

            sim_id = SIMULATION_ID.format(expt, odor_state)
            sim = session.query(models.Simulation).get(sim_id)

            print(sim_id)

            pos_idxs_start = []

            for trial in sim.trials:
                tp_id_start = trial.start_timepoint_id
                tp = session.query(models.Timepoint).get(tp_id_start)
                pos_idxs_start += [(tp.xidx, tp.yidx, tp.zidx)]

            pos_start = [sim.env.pos_from_idx(idx) for idx in pos_idxs_start]

            # build the histogram
            bins = (sim.env.xbins, sim.env.ybins, sim.env.zbins)
            hist, _ = np.histogramdd(np.array(pos_start), bins=bins)

            # create the data model and store it
            hist_data_model = models.SimulationAnalysisTakeOffPositionHistogram()
            hist_data_model.simulation = sim
            hist_data_model.store_data(session, hist.astype(int))

            session.add(hist_data_model)

    session.commit()
Пример #3
0
def main():

    add_script_execution(SCRIPT_ID, session=session, notes=SCRIPT_NOTES)

    for expt in EXPERIMENTS:
        for odor_state in ODOR_STATES:

            sim_id = SIMULATION_ID.format(expt, odor_state)
            sim = session.query(models.Simulation).get(sim_id)

            print(sim_id)

            pos_idxs_start = []

            for trial in sim.trials:
                tp_id_start = trial.start_timepoint_id
                tp = session.query(models.Timepoint).get(tp_id_start)
                pos_idxs_start += [(tp.xidx, tp.yidx, tp.zidx)]

            pos_start = [sim.env.pos_from_idx(idx) for idx in pos_idxs_start]

            # build the histogram
            bins = (sim.env.xbins, sim.env.ybins, sim.env.zbins)
            hist, _ = np.histogramdd(np.array(pos_start), bins=bins)

            # create the data model and store it
            hist_data_model = models.SimulationAnalysisTakeOffPositionHistogram(
            )
            hist_data_model.simulation = sim
            hist_data_model.store_data(session, hist.astype(int))

            session.add(hist_data_model)

    session.commit()
def main(traj_limit=None):
    # add script execution to database
    add_script_execution(SCRIPTID, session=session, multi_use=False, notes=SCRIPTNOTES)

    for sim_id_template in SIMULATION_IDS:
        for expt in EXPERIMENTS:
            for odor_state in ODOR_STATES:

                sim_id = sim_id_template.format(expt, odor_state)

                print(sim_id)

                sim = session.query(models.Simulation).get(sim_id)

                for n_timesteps in N_TIMESTEPSS:
                    # get the displacements for all trials
                    displacements = []
                    for trial in sim.trials[:traj_limit]:
                        tps = trial.get_timepoints(session).all()
                        pos_idx_start = np.array((tps[0].xidx, tps[0].yidx, tps[0].zidx))
                        if n_timesteps > len(tps) - 1:
                            # skip if the trajectory has ended by n_timesteps
                            continue

                        pos_idx_end = np.array((tps[n_timesteps].xidx,
                                                tps[n_timesteps].yidx,
                                                tps[n_timesteps].zidx))
                        displacements += [(pos_idx_end - pos_idx_start).astype(int)]

                    displacements = np.array(displacements)

                    # build the histogram
                    x_ub = min(n_timesteps + 1, sim.env.nx)
                    x_lb = -x_ub
                    y_ub = min(n_timesteps + 1, sim.env.ny)
                    y_lb = -y_ub
                    z_ub = min(n_timesteps + 1, sim.env.nz)
                    z_lb = -z_ub

                    x_bins = np.arange(x_lb, x_ub) + 0.5
                    y_bins = np.arange(y_lb, y_ub) + 0.5
                    z_bins = np.arange(z_lb, z_ub) + 0.5

                    displacement_histogram, _ = \
                        np.histogramdd(displacements, bins=(x_bins, y_bins, z_bins))

                    # create the data model and store it
                    displacement_hist_data_model = \
                        models.SimulationAnalysisDisplacementAfterNTimestepsHistogram()
                    displacement_hist_data_model.n_timesteps = n_timesteps
                    displacement_hist_data_model.simulation = sim
                    displacement_hist_data_model.shape = displacement_histogram.shape
                    displacement_hist_data_model. \
                        store_data(session, displacement_histogram.astype(int))
                    session.add(displacement_hist_data_model)

                    session.commit()
Пример #5
0
def main(traj_limit=None):

    # add script execution to infotaxis database
    add_script_execution(script_id=SCRIPT_ID,
                         notes=SCRIPT_NOTES,
                         session=session)
    session.commit()

    for experiment_id in EXPERIMENT_IDS:
        print experiment_id
        for odor_state in ODOR_STATES:

            # make geom_config_group
            geom_config_group_id = '{}_{}_odor_{}'.format(
                GEOM_CONFIG_GROUP_ID, experiment_id, odor_state)
            geom_config_group_desc = GEOM_CONFIG_GROUP_DESC.format(
                experiment_id, odor_state)
            geom_config_group = models.GeomConfigGroup(
                id=geom_config_group_id, description=geom_config_group_desc)

            # get all wind tunnel trajectories of interest
            trajs = wt_session.query(wt_models.Trajectory).\
                filter_by(
                    experiment_id=experiment_id,
                    odor_state=odor_state,
                    clean=True
                )

            for tctr, traj in enumerate(trajs):

                positions = traj.positions(wt_session)

                discrete_trajectory = ENV.discretize_position_sequence(
                    positions)
                discrete_duration = len(discrete_trajectory)
                avg_dt = .01 * len(positions) / discrete_duration
                geom_config = models.GeomConfig(duration=discrete_duration)
                geom_config.start_idx = discrete_trajectory[0]
                geom_config.geom_config_group = geom_config_group

                # add extension containing extra data about this geom_config
                ext = models.GeomConfigExtensionRealTrajectory(
                    real_trajectory_id=traj.id, avg_dt=avg_dt)
                geom_config.extension_real_trajectory = ext

                if traj_limit and (tctr == traj_limit - 1):

                    break

            session.add(geom_config_group)
            session.commit()
def main(traj_limit=None):

    # add script execution to infotaxis database
    add_script_execution(script_id=SCRIPT_ID, notes=SCRIPT_NOTES, session=session)
    session.commit()

    for experiment_id in EXPERIMENT_IDS:
        print experiment_id
        for odor_state in ODOR_STATES:

            # make geom_config_group
            geom_config_group_id = '{}_{}_odor_{}'.format(GEOM_CONFIG_GROUP_ID, experiment_id, odor_state)
            geom_config_group_desc = GEOM_CONFIG_GROUP_DESC.format(experiment_id, odor_state)
            geom_config_group = models.GeomConfigGroup(id=geom_config_group_id,
                                                       description=geom_config_group_desc)

            # get all wind tunnel trajectories of interest
            trajs = wt_session.query(wt_models.Trajectory).\
                filter_by(
                    experiment_id=experiment_id,
                    odor_state=odor_state,
                    clean=True
                )

            for tctr, traj in enumerate(trajs):

                positions = traj.positions(wt_session)

                discrete_trajectory = ENV.discretize_position_sequence(positions)
                discrete_duration = len(discrete_trajectory)
                avg_dt = .01 * len(positions) / discrete_duration
                geom_config = models.GeomConfig(duration=discrete_duration)
                geom_config.start_idx = discrete_trajectory[0]
                geom_config.geom_config_group = geom_config_group

                # add extension containing extra data about this geom_config
                ext = models.GeomConfigExtensionRealTrajectory(real_trajectory_id=traj.id,
                                                               avg_dt=avg_dt)
                geom_config.extension_real_trajectory = ext

                if traj_limit and (tctr == traj_limit - 1):

                    break

            session.add(geom_config_group)
            session.commit()
Пример #7
0
def main(traj_limit=None):
    # add script execution to database
    add_script_execution(SCRIPTID,
                         session=session,
                         multi_use=False,
                         notes=SCRIPTNOTES)

    for sim_id_template in SIMULATION_IDS:
        for expt in EXPERIMENTS:
            for odor_state in ODOR_STATES:

                sim_id = sim_id_template.format(expt, odor_state)

                print(sim_id)

                sim = session.query(models.Simulation).get(sim_id)

                # get the position indexes for all time points for all trials
                pos_idxs = []
                for trial in sim.trials[:traj_limit]:
                    tps = trial.get_timepoints(session)
                    pos_idxs += [
                        np.array([(tp.xidx, tp.yidx, tp.zidx) for tp in tps])
                    ]

                pos_idxs = np.concatenate(pos_idxs, axis=0)
                pos = np.array(
                    [sim.env.pos_from_idx(pos_idx) for pos_idx in pos_idxs])

                # build the histogram
                bins = (sim.env.xbins, sim.env.ybins, sim.env.zbins)
                pos_histogram, _ = np.histogramdd(pos, bins=bins)

                # create the data model and store it
                pos_hist_data_model = models.SimulationAnalysisPositionHistogram(
                )
                pos_hist_data_model.simulation = sim

                pos_hist_data_model.store_data(session,
                                               pos_histogram.astype(int))
                session.add(pos_hist_data_model)

                session.commit()
Пример #8
0
def main(SIM_PREFIX=None, sim_ids=None, thresholds=None, trial_limit=None):
    
    if thresholds is None:
        thresholds = THRESHOLDS
        
    SCRIPTNOTES = ('Identify plume crossings for simulations with prefix "{}" '
        'using heading smoothing "{}" and thresholds "{}"'.format(
        SIM_PREFIX, HEADING_SMOOTHING, thresholds))

    if sim_ids is None:
        SIM_SUFFIXES = [
            'fruitfly_0.3mps_checkerboard_floor_odor_on',
            'fruitfly_0.3mps_checkerboard_floor_odor_none',
            'fruitfly_0.3mps_checkerboard_floor_odor_afterodor',
            'fruitfly_0.4mps_checkerboard_floor_odor_on',
            'fruitfly_0.4mps_checkerboard_floor_odor_none',
            'fruitfly_0.4mps_checkerboard_floor_odor_afterodor',
            'fruitfly_0.6mps_checkerboard_floor_odor_on',
            'fruitfly_0.6mps_checkerboard_floor_odor_none',
            'fruitfly_0.6mps_checkerboard_floor_odor_afterodor',
            'mosquito_0.4mps_checkerboard_floor_odor_on',
            'mosquito_0.4mps_checkerboard_floor_odor_none',
            'mosquito_0.4mps_checkerboard_floor_odor_afterodor',]

        sim_ids = [
            '{}_{}'.format(SIM_PREFIX, sim_suffix)
            for sim_suffix in SIM_SUFFIXES
        ]

    # add script execution to database
    add_script_execution(
        SCRIPTID, session=session, multi_use=True, notes=SCRIPTNOTES)

    for sim_id in sim_ids:

        print('Identifying crossings from simulation: "{}"'.format(sim_id))

        # get simulation

        sim = session.query(models.Simulation).filter_by(id=sim_id).first()

        # get all trials from this simulation

        trials = session.query(models.Trial).filter_by(simulation=sim).all()

        # make crossing group

        if 'fly' in sim_id:

            threshold = thresholds['fly']

        elif 'mosq' in sim_id:

            threshold = thresholds['mosq']

        cg_id = '{}_th_{}_hsmoothing_{}'.format(
            sim_id, threshold, HEADING_SMOOTHING)
        
        print('Storing in crossing group:')
        print(cg_id)

        cg = models.CrossingGroup(
            id=cg_id,
            simulation=sim,
            threshold=threshold,
            heading_smoothing=HEADING_SMOOTHING)

        session.add(cg)

        # loop through trials and identify crossings

        trial_ctr = 0

        for trial in trials:

            if trial_limit and trial_ctr >= trial_limit:

                break

            # get relevant time-series

            odors = trial.timepoint_field(session, 'odor')

            xs = trial.timepoint_field(session, 'xidx')
            ys = trial.timepoint_field(session, 'yidx')
            zs = trial.timepoint_field(session, 'zidx')

            # get smoothed headings

            hs = smooth(trial.timepoint_field(session, 'hxyz'), HEADING_SMOOTHING)

            # identify crossings

            crossing_lists, peaks = time_series.segment_by_threshold(
                odors, threshold)

            tr_start = trial.start_timepoint_id

            # add crossings

            for c_ctr, (crossing_list, peak) in enumerate(zip(crossing_lists, peaks)):

                crossing = models.Crossing(
                    trial=trial,
                    crossing_number=c_ctr+1,
                    crossing_group=cg,
                    start_timepoint_id=crossing_list[0] + tr_start,
                    entry_timepoint_id=crossing_list[1] + tr_start,
                    peak_timepoint_id=crossing_list[2] + tr_start,
                    exit_timepoint_id=crossing_list[3] + tr_start - 1,
                    end_timepoint_id=crossing_list[4] + tr_start - 1,
                    max_odor=peak,)

                session.add(crossing)

                # create this crossing's basic feature set

                crossing.feature_set_basic = models.CrossingFeatureSetBasic(
                    position_x_entry=xs[crossing_list[1]],
                    position_y_entry=ys[crossing_list[1]],
                    position_z_entry=zs[crossing_list[1]],
                    heading_xyz_entry=hs[crossing_list[1]],
                    position_x_peak=xs[crossing_list[2]],
                    position_y_peak=ys[crossing_list[2]],
                    position_z_peak=zs[crossing_list[2]],
                    heading_xyz_peak=hs[crossing_list[2]],
                    position_x_exit=xs[crossing_list[3] - 1],
                    position_y_exit=ys[crossing_list[3] - 1],
                    position_z_exit=zs[crossing_list[3] - 1],
                    heading_xyz_exit=hs[crossing_list[3] - 1],
                )

                session.add(crossing)

            trial_ctr += 1

        # commit after all crossings from all trials from a simulation have been added

        session.commit()
def main(trial_limit=None):

    # add script execution to database

    add_script_execution(SCRIPTID, session=session, multi_use=True, notes=SCRIPTNOTES)

    for sim_id in SIM_IDS:

        print('Identifying crossings from simulation: "{}"'.format(sim_id))

        # get simulation

        sim = session.query(models.Simulation).filter_by(id=sim_id).first()

        # get all trials from this simulation

        trials = session.query(models.Trial).filter_by(simulation=sim).all()

        # make crossing group

        if 'fly' in sim_id:

            threshold = THRESHOLDS['fly']

        elif 'mosq' in sim_id:

            threshold = THRESHOLDS['mosq']

        cg_id = '{}_th_{}_hsmoothing_{}'.format(sim_id, threshold, HEADING_SMOOTHING)

        cg = models.CrossingGroup(
            id=cg_id,
            simulation=sim,
            threshold=threshold,
            heading_smoothing=HEADING_SMOOTHING)

        session.add(cg)

        # loop through trials and identify crossings

        trial_ctr = 0

        for trial in trials:

            if trial_limit and trial_ctr >= trial_limit:

                break

            # get relevant time-series

            odors = trial.timepoint_field(session, 'odor')

            xs = trial.timepoint_field(session, 'xidx')
            ys = trial.timepoint_field(session, 'yidx')
            zs = trial.timepoint_field(session, 'zidx')

            # get smoothed headings

            hs = smooth(trial.timepoint_field(session, 'hxyz'), HEADING_SMOOTHING)

            # identify crossings

            crossing_lists, peaks = time_series.segment_by_threshold(
                odors, threshold)

            tr_start = trial.start_timepoint_id

            # add crossings

            for c_ctr, (crossing_list, peak) in enumerate(zip(crossing_lists, peaks)):

                crossing = models.Crossing(
                    trial=trial,
                    crossing_number=c_ctr+1,
                    crossing_group=cg,
                    start_timepoint_id=crossing_list[0] + tr_start,
                    entry_timepoint_id=crossing_list[1] + tr_start,
                    peak_timepoint_id=crossing_list[2] + tr_start,
                    exit_timepoint_id=crossing_list[3] + tr_start - 1,
                    end_timepoint_id=crossing_list[4] + tr_start - 1,
                    max_odor=peak,)

                session.add(crossing)

                # create this crossing's basic feature set

                crossing.feature_set_basic = models.CrossingFeatureSetBasic(
                    position_x_entry=xs[crossing_list[1]],
                    position_y_entry=ys[crossing_list[1]],
                    position_z_entry=zs[crossing_list[1]],
                    heading_xyz_entry=hs[crossing_list[1]],
                    position_x_peak=xs[crossing_list[2]],
                    position_y_peak=ys[crossing_list[2]],
                    position_z_peak=zs[crossing_list[2]],
                    heading_xyz_peak=hs[crossing_list[2]],
                    position_x_exit=xs[crossing_list[3] - 1],
                    position_y_exit=ys[crossing_list[3] - 1],
                    position_z_exit=zs[crossing_list[3] - 1],
                    heading_xyz_exit=hs[crossing_list[3] - 1],
                )

                session.add(crossing)

            trial_ctr += 1

        # commit after all crossings from all trials from a simulation have been added

        session.commit()
Пример #10
0
def main(INSECT_PARAMS,
         SCRIPTNOTES,
         threshold=None,
         sim_ids=None,
         sim_descs=None,
         expts=None,
         odor_states=None,
         traj_limit=None):

    # add script execution to database
    add_script_execution(SCRIPTID,
                         session=session,
                         multi_use=True,
                         notes=SCRIPTNOTES)

    if expts is None:
        expts = EXPERIMENTS

    if odor_states is None:
        odor_states = ODOR_STATES

    for expt in expts:
        if '0.3mps' in expt:
            w = 0.3
        elif '0.4mps' in expt:
            w = 0.4
        elif '0.6mps' in expt:
            w = 0.6

        insect_params = INSECT_PARAMS.copy()
        insect_params['w'] = w

        for odor_state in odor_states:

            print('Running simulation for expt "{}" with odor "{}"...'.format(
                expt, odor_state))

            # get geom_config_group for this experiment and odor state
            geom_config_group_id = GEOM_CONFIG_GROUP_ID.format(
                expt, odor_state)
            geom_config_group = session.query(
                models.GeomConfigGroup).get(geom_config_group_id)

            # get wind tunnel copy simulation so we can match plume and insect
            # note we select the first simulation that is of this type and
            # corresponds to the right geom_config_group, since we only use the
            # plume from it, which is independent of insect parameters used
            #
            # for instance, the plume bound to a simulation in which the insect
            # had D = 0.6 and that bound to a simulation where D = 0.4 will be
            # the same, since it is only the insect's
            # internal model that has changed
            wt_copy_sims = session.query(models.Simulation).\
                filter(models.Simulation.geom_config_group == geom_config_group).\
                filter(models.Simulation.id.like(
                    WIND_TUNNEL_DISCRETIZED_SIMULATION_ID_PATTERN))

            # get plume from corresponding discretized real wind tunnel trajectory
            if 'fruitfly' in expt:
                pl = CollimatedPlume(env=ENV,
                                     dt=-1,
                                     orm=wt_copy_sims.first().plume)
            elif 'mosquito' in expt:
                pl = SpreadingGaussianPlume(env=ENV,
                                            dt=-1,
                                            orm=wt_copy_sims.first().plume)

            if threshold is not None:
                print('Setting plume detectability threshold to '
                      '{}'.format(threshold))
                pl.set_params(threshold=threshold)

            # create insect
            # note: we will actually make a new insect for each trial,
            # since the dt's vary;
            # here we set dt=-1, since this doesn't get stored in the db anyhow
            ins = Insect(env=ENV, dt=-1)
            ins.set_params(**insect_params)
            ins.generate_orm(models)

            # create simulation
            if sim_ids is None:
                sim_id = SIMULATION_ID.format(insect_params['r'],
                                              insect_params['d'], expt,
                                              odor_state)
            else:
                sim_id = sim_ids[(expt, odor_state)]

            if sim_descs is None:
                sim_desc = SIMULATION_DESCRIPTION.format(expt, odor_state)
            else:
                sim_desc = sim_descs[(expt, odor_state)]

            sim = models.Simulation(id=sim_id, description=sim_desc)
            sim.env = ENV
            sim.dt = -1
            sim.total_trials = len(geom_config_group.geom_configs)
            sim.heading_smoothing = 0
            sim.geom_config_group = geom_config_group

            sim.plume = pl.orm
            sim.insect = ins.orm

            session.add(sim)

            # create ongoing run
            ongoing_run = models.OngoingRun(trials_completed=0,
                                            simulations=[sim])
            session.add(ongoing_run)

            session.commit()

            # generate trials
            for gctr, geom_config in enumerate(geom_config_group.geom_configs):

                if gctr == traj_limit:
                    break

                # make new plume and insect with proper dts
                ins = Insect(env=ENV,
                             dt=geom_config.extension_real_trajectory.avg_dt)
                ins.set_params(**insect_params)
                ins.loglike_function = LOGLIKE

                # set insect starting position
                ins.set_pos(geom_config.start_idx, is_idx=True)

                # initialize plume and insect and create trial
                pl.initialize()
                ins.initialize()

                trial = Trial(pl=pl, ins=ins, nsteps=geom_config.duration)

                # run trial
                for step in xrange(geom_config.duration - 1):
                    trial.step()

                # save trial
                trial.add_timepoints(models,
                                     session=session,
                                     heading_smoothing=sim.heading_smoothing)
                trial.generate_orm(models)
                trial.orm.geom_config = geom_config
                trial.orm.simulation = sim
                session.add(trial.orm)

                # update ongoing_run
                ongoing_run.trials_completed = gctr + 1
                session.add(ongoing_run)

                session.commit()
def main(traj_limit=None):

    # add script execution to infotaxis database
    add_script_execution(script_id=SCRIPT_ID,
                         session=session,
                         multi_use=True,
                         notes=SCRIPT_NOTES)
    session.commit()

    # get wind tunnel connection and models
    wt_session = imp.load_source('connect',
                                 os.path.join(WT_REPO, 'db_api',
                                              'connect.py')).session
    wt_models = imp.load_source('models',
                                os.path.join(WT_REPO, 'db_api', 'models.py'))

    for experiment_id in EXPERIMENT_IDS:

        for odor_state in ODOR_STATES:

            # make geom_config_group
            geom_config_group_id = '{}_{}_odor_{}'.format(
                GEOM_CONFIG_GROUP_ID, experiment_id, odor_state)
            geom_config_group = session.query(
                models.GeomConfigGroup).get(geom_config_group_id)

            # make simulation
            r = INSECT_PARAMS_DICT[experiment_id]['r']
            d = INSECT_PARAMS_DICT[experiment_id]['d']
            sim_id = SIMULATION_ID.format(r, d, experiment_id, odor_state)
            sim_description = SIMULATION_DESCRIPTION.format(
                experiment_id, odor_state)
            sim = models.Simulation(id=sim_id, description=sim_description)
            sim.env, sim.dt = ENV, DT
            sim.heading_smoothing = 0
            sim.geom_config_group = geom_config_group

            # make plume
            if 'fruitfly' in experiment_id:
                pl = CollimatedPlume(env=ENV, dt=DT)
            elif 'mosquito' in experiment_id:
                pl = SpreadingGaussianPlume(env=ENV, dt=DT)
            pl.set_params(**PLUME_PARAMS_DICT[experiment_id])
            if odor_state in ('none', 'afterodor'):
                pl.set_params(threshold=-1)
            pl.initialize()
            pl.generate_orm(models, sim=sim)

            # make insect
            ins = Insect(env=ENV, dt=DT)
            ins.set_params(**INSECT_PARAMS_DICT[experiment_id])
            ins.loglike_function = LOGLIKE
            ins.initialize()
            ins.generate_orm(models, sim=sim)

            # add simulation and ongoing run
            sim.ongoing_run = models.OngoingRun(trials_completed=0)
            session.add(sim)
            session.commit()

            # loop through all geom_configs in group, look up corresponding trajectory,
            # and discretize it
            for gctr, geom_config in enumerate(geom_config_group.geom_configs):

                # get trajectory id from geom_config and trajectory from wind tunnel database
                traj_id = geom_config.extension_real_trajectory.real_trajectory_id
                traj = wt_session.query(wt_models.Trajectory).get(traj_id)

                # get positions from traj
                positions = traj.positions(wt_session)

                # create discretized version of trajectory
                trial = TrialFromPositionSequence(positions, pl, ins)
                # add timepoints to trial and generate data model
                trial.add_timepoints(models,
                                     session=session,
                                     heading_smoothing=sim.heading_smoothing)
                trial.generate_orm(models)

                # bind simulation, geom_config
                trial.orm.simulation = sim
                trial.orm.geom_config = geom_config

                # update ongoing run
                sim.ongoing_run.trials_completed += 1
                session.add(sim)
                session.commit()

                if traj_limit and (gctr == traj_limit - 1):
                    break

            # update total number of trials
            sim.total_trials = gctr + 1
            session.add(sim)
            session.commit()
def main(traj_limit=None):
    # add script execution to database
    add_script_execution(SCRIPTID, session=session, multi_use=True, notes=SCRIPTNOTES)

    for expt in EXPERIMENTS:
        if '0.3mps' in expt:
            w = 0.3
        elif '0.4mps' in expt:
            w = 0.4
        elif '0.6mps' in expt:
            w = 0.6

        insect_params = INSECT_PARAMS.copy()
        insect_params['w'] = w

        for odor_state in ODOR_STATES:

            print('Running simulation for expt "{}" with odor "{}"...'.
                  format(expt, odor_state))

            # get geom_config_group for this experiment and odor state
            geom_config_group_id = GEOM_CONFIG_GROUP_ID.format(expt, odor_state)
            geom_config_group = session.query(models.GeomConfigGroup).get(geom_config_group_id)

            # get wind tunnel copy simulation so we can match plume and insect
            # note we select the first simulation that is of this type and corresponds to the
            # right geom_config_group, since we only use the plume from it, which is independent
            # of what insect parameters were used
            #
            # for instance, the plume bound to a simulation in which the insect had D = 0.6 and that
            # bound to a simulation where D = 0.4 will be the same, since it is only the insect's
            # internal model that has changed
            wt_copy_sims = session.query(models.Simulation).\
                filter(models.Simulation.geom_config_group == geom_config_group).\
                filter(models.Simulation.id.like(WIND_TUNNEL_DISCRETIZED_SIMULATION_ID_PATTERN))

            # get plume from corresponding discretized real wind tunnel trajectory
            if 'fruitfly' in expt:
                pl = CollimatedPlume(env=ENV, dt=-1, orm=wt_copy_sims.first().plume)
            elif 'mosquito' in expt:
                pl = SpreadingGaussianPlume(env=ENV, dt=-1, orm=wt_copy_sims.first().plume)

            # create insect
            # note: we will actually make a new insect for each trial, since the dt's vary;
            # here we just set dt=-1, since this doesn't get stored in the db anyhow
            ins = Insect(env=ENV, dt=-1)
            ins.set_params(**insect_params)
            ins.generate_orm(models)

            # create simulation
            sim_id = SIMULATION_ID.format(insect_params['r'],
                                          insect_params['d'],
                                          expt, odor_state)
            sim_desc = SIMULATION_DESCRIPTION.format(expt, odor_state)

            sim = models.Simulation(id=sim_id, description=sim_desc)
            sim.env = ENV
            sim.dt = -1
            sim.total_trials = len(geom_config_group.geom_configs)
            sim.heading_smoothing = 0
            sim.geom_config_group = geom_config_group

            sim.plume = pl.orm
            sim.insect = ins.orm

            session.add(sim)

            # create ongoing run
            ongoing_run = models.OngoingRun(trials_completed=0, simulations=[sim])
            session.add(ongoing_run)

            session.commit()

            # generate trials
            for gctr, geom_config in enumerate(geom_config_group.geom_configs):

                if gctr == traj_limit:
                    break

                # make new plume and insect with proper dts
                ins = Insect(env=ENV, dt=geom_config.extension_real_trajectory.avg_dt)
                ins.set_params(**insect_params)
                ins.loglike_function = LOGLIKE

                # set insect starting position
                ins.set_pos(geom_config.start_idx, is_idx=True)

                # initialize plume and insect and create trial
                pl.initialize()
                ins.initialize()

                trial = Trial(pl=pl, ins=ins, nsteps=geom_config.duration)

                # run trial
                for step in xrange(geom_config.duration - 1):
                    trial.step()

                # save trial
                trial.add_timepoints(models, session=session, heading_smoothing=sim.heading_smoothing)
                trial.generate_orm(models)
                trial.orm.geom_config = geom_config
                trial.orm.simulation = sim
                session.add(trial.orm)

                # update ongoing_run
                ongoing_run.trials_completed = gctr + 1
                session.add(ongoing_run)

                session.commit()
def main(traj_limit=None):
    # add script execution to database
    add_script_execution(SCRIPTID,
                         session=session,
                         multi_use=False,
                         notes=SCRIPTNOTES)

    for sim_id_template in SIMULATION_IDS:
        for expt in EXPERIMENTS:
            for odor_state in ODOR_STATES:

                sim_id = sim_id_template.format(expt, odor_state)

                print(sim_id)

                sim = session.query(models.Simulation).get(sim_id)

                for n_timesteps in N_TIMESTEPSS:
                    # get the displacements for all trials
                    displacements = []
                    for trial in sim.trials[:traj_limit]:
                        tps = trial.get_timepoints(session).all()
                        pos_idx_start = np.array(
                            (tps[0].xidx, tps[0].yidx, tps[0].zidx))
                        if n_timesteps > len(tps) - 1:
                            # skip if the trajectory has ended by n_timesteps
                            continue

                        pos_idx_end = np.array(
                            (tps[n_timesteps].xidx, tps[n_timesteps].yidx,
                             tps[n_timesteps].zidx))
                        displacements += [
                            (pos_idx_end - pos_idx_start).astype(int)
                        ]

                    displacements = np.array(displacements)

                    # build the histogram
                    x_ub = min(n_timesteps + 1, sim.env.nx)
                    x_lb = -x_ub
                    y_ub = min(n_timesteps + 1, sim.env.ny)
                    y_lb = -y_ub
                    z_ub = min(n_timesteps + 1, sim.env.nz)
                    z_lb = -z_ub

                    x_bins = np.arange(x_lb, x_ub) + 0.5
                    y_bins = np.arange(y_lb, y_ub) + 0.5
                    z_bins = np.arange(z_lb, z_ub) + 0.5

                    displacement_histogram, _ = \
                        np.histogramdd(displacements, bins=(x_bins, y_bins, z_bins))

                    # create the data model and store it
                    displacement_hist_data_model = \
                        models.SimulationAnalysisDisplacementAfterNTimestepsHistogram()
                    displacement_hist_data_model.n_timesteps = n_timesteps
                    displacement_hist_data_model.simulation = sim
                    displacement_hist_data_model.shape = displacement_histogram.shape
                    displacement_hist_data_model. \
                        store_data(session, displacement_histogram.astype(int))
                    session.add(displacement_hist_data_model)

                    session.commit()