Beispiel #1
0
 def run_simulation(self,
                    path,
                    sim_type,
                    network_state,
                    debug=False,
                    drop_list=None):
     """
     Uses a model identified by path to run a naive and a trained and optionally an ideal and unit dropped simulation
     :param path: The model path
     :param sim_type: The simulation type to run ("r"adial or "l"inear)
     :param network_state: The state of the network ("naive", "trained", "ideal", "bfevolve")
     :param debug: If true, also return debug dictionary
     :param drop_list: If not none should be a list that will be fed to det_drop to determine which units are kept
             (1) or dropped (0)
     :return:
         [0]: Array of x,y,angle positions
         [1]: If debug=True dictionary of simulation debug information
     """
     with SimulationStore(self.sim_store_name, self.std,
                          self.mo) as sim_store:
         if debug:
             return sim_store.get_sim_debug(path, sim_type, network_state,
                                            drop_list)
         else:
             return sim_store.get_sim_pos(path, sim_type, network_state,
                                          drop_list)
Beispiel #2
0
def compute_da_modulation(model_path, drop_list=None):
    with SimulationStore("sim_store.hdf5", std, MoTypes(False)) as sim_store:
        pos_ev = sim_store.get_sim_pos(model_path, "r", "bfevolve", drop_list)
    pos_flt = run_flat_gradient(model_path, drop_list)
    bs_ev = get_bout_starts(pos_ev)
    bs_flt = get_bout_starts(pos_flt)
    # get delta angle of each bout
    da_ev = get_bout_da(pos_ev, bs_ev)
    da_flt = get_bout_da(pos_flt, bs_flt)
    # get temperature at each bout start
    temp_ev = a.temp_convert(np.sqrt(np.sum(pos_ev[bs_ev.astype(bool), :2]**2, 1)), 'r')
    temp_flt = a.temp_convert(np.sqrt(np.sum(pos_flt[bs_flt.astype(bool), :2] ** 2, 1)), 'r')
    # get delta-temperature effected by each previous bout
    dt_ev = np.r_[0, np.diff(temp_ev)]
    dt_flt = np.r_[0, np.diff(temp_flt)]
    # only consider data above T_Preferred and away from the edge
    valid_ev = np.logical_and(temp_ev > GlobalDefs.tPreferred, temp_ev < GlobalDefs.circle_sim_params["t_max"]-1)
    valid_flt = np.logical_and(temp_flt > GlobalDefs.tPreferred, temp_flt < GlobalDefs.circle_sim_params["t_max"] - 1)
    da_ev = da_ev[valid_ev]
    da_flt = da_flt[valid_flt]
    dt_ev = dt_ev[valid_ev]
    dt_flt = dt_flt[valid_flt]
    # get turn magnitude for up and down gradient
    up_grad_ev = np.mean(np.abs(da_ev[dt_ev > 0.5]))
    dn_grad_ev = np.mean(np.abs(da_ev[dt_ev < -0.5]))
    up_grad_flt = np.mean(np.abs(da_flt[dt_flt > 0.5]))
    dn_grad_flt = np.mean(np.abs(da_flt[dt_flt < -0.5]))
    up_change = up_grad_ev / up_grad_flt
    dn_change = dn_grad_ev / dn_grad_flt
    return dn_change, up_change
Beispiel #3
0
def compute_da_coherence(model_path, drop_list=None):
    with SimulationStore("sim_store.hdf5", std_zf, MoTypes(False)) as sim_store:
        pos_ev = sim_store.get_sim_pos(model_path, "r", "bfevolve", drop_list)
    bs_ev = get_bout_starts(pos_ev)
    # get delta angle of each bout
    da_ev = np.rad2deg(get_bout_da(pos_ev, bs_ev))
    # convert into appproximation of S, L and R behaviors
    bhv_ev = np.ones_like(da_ev)
    bhv_ev[da_ev < -10] = 2
    bhv_ev[da_ev > 10] = 3
    return a.turn_coherence(bhv_ev, 10), da_ev
Beispiel #4
0
def compute_gradient_bout_frequency(model_path, drop_list=None):
    def bout_freq(pos: np.ndarray):
        r = np.sqrt(np.sum(pos[:, :2]**2, 1))  # radial position
        spd = np.r_[0, np.sqrt(np.sum(np.diff(pos[:, :2], axis=0) ** 2, 1))]  # speed
        bs = np.r_[0, np.diff(spd) > 0.00098]  # bout starts
        bins = np.linspace(0, 100, 6)
        bcenters = bins[:-1] + np.diff(bins)/2
        cnt_r = np.histogram(r, bins)[0]
        cnt_r_bs = np.histogram(r[bs > 0.1], bins)[0]
        bfreq = cnt_r_bs / cnt_r * GlobalDefs.frame_rate
        return bfreq, bcenters

    with SimulationStore("sim_store.hdf5", std, MoTypes(False)) as sim_store:
        pos_fixed = sim_store.get_sim_pos(model_path, "r", "trained", drop_list)
        pos_var = sim_store.get_sim_pos(model_path, "r", "bfevolve", drop_list)
    bf_fixed, bc = bout_freq(pos_fixed)
    bf_var, bc = bout_freq(pos_var)
    return bc, bf_fixed, bf_var
Beispiel #5
0
def do_simulation(path, sim_type, run_ideal, drop_list=None):
    """
    Uses a model identified by path to run a naive and a trained and optionally an ideal and unit dropped simulation
    :param path: The model path
    :param sim_type: The simulation type to run
    :param run_ideal: If true, an ideal choice simulation will be run as well
    :param drop_list: If not none should be a list that will be fed to det_drop to determine which units are kept (1)
        or dropped (0)
    :return:
        [0]: The occupancy bin centers in degrees C
        [1]: The occupancy of the naive model
        [2]: The occupancy of the trained model
        [3]: The occupancy of the ideal choice model if run_ideal=True, None otherwise
        [4]: The occupancy of a unit dropped model if drop_list is provided, None otherwise
    """
    bins = np.linspace(0, GlobalDefs.circle_sim_params["radius"], 100)
    # bin-centers in degress
    bcenters = bins[:-1]+np.diff(bins)/2
    bcenters = temp_convert(bcenters, sim_type)
    if sim_type == "l":
        simdir = "x"
    else:
        simdir = "r"
    with SimulationStore("sim_store.hdf5", std, MoTypes(False)) as sim_store:
        pos_naive = sim_store.get_sim_pos(path, sim_type, "naive")
        h_naive = bin_simulation(pos_naive, bins, simdir)
        pos_trained = sim_store.get_sim_pos(path, sim_type, "trained")
        h_trained = bin_simulation(pos_trained, bins, simdir)
        if run_ideal:
            pos_ideal = sim_store.get_sim_pos(path, sim_type, "ideal")
            h_ideal = bin_simulation(pos_ideal, bins, simdir)
        else:
            h_ideal = None
        if drop_list is not None:
            pos_drop = sim_store.get_sim_pos(path, sim_type, "trained", drop_list)
            h_drop = bin_simulation(pos_drop, bins, simdir)
        else:
            h_drop = None
    return bcenters, h_naive, h_trained, h_ideal, h_drop
Beispiel #6
0
    def sim_info(net_id):
        def bin_pos(all_pos):
            nonlocal sim_type
            nonlocal bins
            bin_centers = bins[:-1] + np.diff(bins) / 2
            if sim_type == "r":
                quantpos = np.sqrt(np.sum(all_pos[:, :2] ** 2, 1))
            else:
                quantpos = all_pos[:, 0]
            h = np.histogram(quantpos, bins)[0].astype(float)
            # normalize for radius if applicable
            if sim_type == "r":
                h /= bin_centers
            h /= h.sum()
            # convert bin_centers to temperature
            bin_centers = temp_convert(bin_centers, sim_type)
            return bin_centers, h

        def temp_error(all_pos):
            nonlocal sim_type
            if sim_type == "r":
                quantpos = np.sqrt(np.sum(all_pos[:, :2] ** 2, 1))
            else:
                quantpos = all_pos[:, 0]
            temp_pos = temp_convert(quantpos, sim_type)
            if sim_type == "r":
                # form a weighted average, considering points of larger radius less since just by
                # chance they will be visited more often
                weights = 1 / np.sqrt(np.sum(all_pos[:, :2]**2, 1))
                weights[np.isinf(weights)] = 0  # occurs when 0,0 was picked as starting point only
                sum_of_weights = np.nansum(weights)
                weighted_sum = np.nansum(np.sqrt((temp_pos - GlobalDefs.tPreferred)**2) * weights)
                return weighted_sum / sum_of_weights
            return np.mean(np.sqrt((temp_pos - GlobalDefs.tPreferred)**2))

        nonlocal sim_type
        nonlocal fish
        nonlocal non_fish
        fish_remove = create_det_drop_list(net_id, clust_ids, all_ids, fish)
        nonfish_remove = create_det_drop_list(net_id, clust_ids, all_ids, non_fish)
        shuff_remove = create_det_drop_list(net_id, clust_ids, all_ids, fish, True)
        with SimulationStore("sim_store.hdf5", std, MoTypes(False)) as sim_store:
            pos_naive, db_naive = sim_store.get_sim_debug(mpath(paths_512[net_id]), sim_type, "naive")
            pos_trained, db_trained = sim_store.get_sim_debug(mpath(paths_512[net_id]), sim_type, "bfevolve")
            pos_fish, db_fish = sim_store.get_sim_debug(mpath(paths_512[net_id]), sim_type, "bfevolve", fish_remove)
            pos_nonfish, db_nonfish = sim_store.get_sim_debug(mpath(paths_512[net_id]), sim_type, "bfevolve",
                                                              nonfish_remove)
        with SimulationStore(None, std, MoTypes(False)) as sim_store:  # don't store shuffle
            pos_shuff, db_shuff = sim_store.get_sim_debug(mpath(paths_512[net_id]), sim_type, "bfevolve", shuff_remove)
        bins = np.linspace(0, GlobalDefs.circle_sim_params["radius"], 100)
        bc, h_naive = bin_pos(pos_naive)
        e_naive = temp_error(pos_naive)
        h_trained = bin_pos(pos_trained)[1]
        e_trained = temp_error(pos_trained)
        h_fish = bin_pos(pos_fish)[1]
        e_fish = temp_error(pos_fish)
        h_nonfish = bin_pos(pos_nonfish)[1]
        e_nonfish = temp_error(pos_nonfish)
        h_shuff = bin_pos(pos_shuff)[1]
        e_shuff = temp_error(pos_shuff)
        return bc, {"naive": (h_naive, db_naive, e_naive), "trained": (h_trained, db_trained, e_trained),
                    "fish": (h_fish, db_fish, e_fish), "nonfish": (h_nonfish, db_nonfish, e_nonfish),
                    "shuffle": (h_shuff, db_shuff, e_shuff)}
Beispiel #7
0
def plot_sim_debug(path, sim_type, drop_list=None):
    """
    Runs indicated simulation on fully trained network, retrieves debug information and plots parameter correlations
    :param path: The model path
    :param sim_type: Either "r"adial or "l"inear
    :param drop_list: Optional list of vectors that indicate which units should be kept (1) or dropped (0)
    :return:
        [0]: The simulation positions
        [1]: The debug dict
    """
    with SimulationStore("sim_store.hdf5", std, MoTypes(False)) as sim_store:
        all_pos, db_dict = sim_store.get_sim_debug(path, sim_type, "trained", drop_list)
    ct = db_dict["curr_temp"]
    val = np.logical_not(np.isnan(ct))
    ct = ct[val]
    pred = db_dict["pred_temp"][val, :]
    selb = db_dict["sel_behav"][val]
    tru = db_dict["true_temp"][val, :]
    btypes = ["N", "S", "L", "R"]
    # plot counts of different behavior types
    fig, ax = pl.subplots()
    sns.countplot(selb, order=btypes)
    sns.despine(fig, ax)
    # for each behavior type, plot scatter of prediction vs. current temperature
    fig, axes = pl.subplots(2, 2)
    axes = axes.ravel()
    for i in range(4):
        axes[i].scatter(ct, pred[:, i], s=2)
        axes[i].set_xlabel("Current temperature")
        axes[i].set_ylabel("{0} prediction".format(btypes[i]))
        axes[i].set_title("r = {0:.2g}".format(np.corrcoef(ct, pred[:, i])[0, 1]))
    sns.despine(fig)
    fig.tight_layout()
    # for each behavior type, plot scatter of prediction vs.true outcome
    fig, axes = pl.subplots(2, 2)
    axes = axes.ravel()
    for i in range(4):
        axes[i].scatter(tru[:, i], pred[:, i], s=2)
        axes[i].set_xlabel("{0} tru outcome".format(btypes[i]))
        axes[i].set_ylabel("{0} prediction".format(btypes[i]))
        axes[i].set_title("r = {0:.2g}".format(np.corrcoef(tru[:, i], pred[:, i])[0, 1]))
    sns.despine(fig)
    fig.tight_layout()
    # Plot average rank errors binned by current temperature
    rerbins = 10
    avg_rank_errors = np.zeros(rerbins)
    ctb = np.linspace(ct.min(), ct.max(), rerbins+1)
    bincents = ctb[:-1] + np.diff(ctb)/2
    for i in range(rerbins):
        in_bin = np.logical_and(ct >= ctb[i], ct < ctb[i+1])
        pib = pred[in_bin, :]
        tib = tru[in_bin, :]
        errsum = 0
        for j in range(pib.shape[0]):
            p_ranks = np.unique(pib[j, :], return_inverse=True)[1]
            t_ranks = np.unique(tib[j, :], return_inverse=True)[1]
            errsum += np.sum(np.abs(p_ranks - t_ranks))
        avg_rank_errors[i] = errsum / pib.shape[0]
    fig, ax = pl.subplots()
    ax.plot(bincents, avg_rank_errors, 'o')
    ax.set_title("Avg. rank errors by temperature")
    ax.set_xlabel("Binned start temperature")
    ax.set_ylabel("Average rank error")
    sns.despine(fig, ax)
    return all_pos, db_dict
Beispiel #8
0
    sns.despine(fig, ax)
    ax.set_ylabel("Fraction within +/- 1C")
    ax.set_yticks([0, 0.25, 0.5])
    fig.savefig(save_folder + "zf_type_ablations.pdf", type="pdf")

    # for each removal and retrain compute gradient distributions
    trained = np.empty((len(paths_512_zf), centers.size))
    fl_ablated = np.empty_like(trained)
    fl_retrained_t = np.empty_like(trained)
    fl_retrained_nont = np.empty_like(trained)
    nfl_ablated = np.empty_like(trained)
    for i, p in enumerate(paths_512_zf):
        mp = mpath(base_path_zf, p)
        rt_path_nont = mp + "/fl_nontbranch_retrain"
        rt_path_t = mp + "/fl_tbranch_retrain"
        with SimulationStore("zf_retrain_simStore.hdf5", std_zf,
                             MoTypes(False)) as sim_store:
            pos = sim_store.get_sim_pos(mp, 'r', "trained")
            trained[i, :] = a.bin_simulation(pos, bns, 'r')
            dlist = a.create_det_drop_list(i, clust_ids_zf, all_ids_zf,
                                           [1, 2, 3, 4, 5])
            n_fish_rem = 512 - dlist['t'][0].sum() + 512 - dlist['t'][1].sum()
            pos = sim_store.get_sim_pos(mp, 'r', "trained", dlist)
            fl_ablated[i, :] = a.bin_simulation(pos, bns, 'r')
            pos = sim_store.get_sim_pos(rt_path_t, 'r', "trained", dlist)
            fl_retrained_t[i, :] = a.bin_simulation(pos, bns, 'r')
            pos = sim_store.get_sim_pos(rt_path_nont, 'r', "trained", dlist)
            fl_retrained_nont[i, :] = a.bin_simulation(pos, bns, 'r')
            dlist = a.create_det_drop_list(i, clust_ids_zf, all_ids_zf,
                                           [0, 6, 7])
            # add random removals to remove the same number of units as in fish-like
            n_nfish_rem = 512 - dlist['t'][0].sum() + 512 - dlist['t'][1].sum()