Beispiel #1
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()