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()
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()
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(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()
        if line.startswith('ID:'):

            script_id = line[3:].strip()
            desc_dict[script_id] = ['', '']

        elif line.startswith('TYPE'):

            script_type = line[5:].strip()
            desc_dict[script_id][0] = script_type

        else:

            desc_dict[script_id][1] += (line + '\n')

# write everything to database
for script_id, info in desc_dict.items():

    script = Script(id=script_id, type=info[0], description=info[1])

    try:

        session.add(script)
        session.commit()

    except Exception, e:

        session.rollback()

        print e

        continue
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()