def __init__(self, p_flip=.9, Nt=100, b0=.3, b1=.9, noise_level=.1, aov_typ=2, poissonify=False): """Initialize a new simulation and set parameters""" self.p_flip = p_flip self.Nt = int(my.rint(Nt)) # 0d array sometimes self.b0 = b0 self.b1 = b1 self.noise_level = noise_level self.aov_typ = aov_typ self.poissonify = poissonify
def take_equally_spaced(arr, n): """Take n equally spaced elements from arr We avoid the endpoints. So, divide the array into n+1 segments, and take the highest point of each segment, discarding the last. """ # e.g., we want 2 equally spaced, so they are at 1/3 and 2/3 arr = np.asarray(arr) first_element_relative = 1.0 / (n + 1) relative_pos = np.linspace( first_element_relative, 1 - first_element_relative, n) absolute_pos = my.rint((len(arr) - 1) * relative_pos) return arr[absolute_pos]
def generate_inputs(self): """Set x0 (normal), x1 (binary), and noise""" # Random normals self.x0 = np.random.standard_normal((self.Nt,)) # Discretify x1, starting with max possible corr self.x1 = (self.x0 > 0).astype(np.int) # Flip a random number of bits, to decrease correlation nflip = my.rint(self.p_flip * self.Nt) self.x1[:nflip] = np.random.binomial(1, .5, nflip) # Scale self.x1 = self.x1 - self.x1.mean() self.x1 = self.x1 / self.x1.std() # Generate noise self.noise = self.noise_level * np.random.standard_normal((self.Nt,))
def generate_inputs(self): """Set x0 (normal), x1 (binary), and noise""" # Random normals self.x0 = np.random.standard_normal((self.Nt, )) # Discretify x1, starting with max possible corr self.x1 = (self.x0 > 0).astype(np.int) # Flip a random number of bits, to decrease correlation nflip = my.rint(self.p_flip * self.Nt) self.x1[:nflip] = np.random.binomial(1, .5, nflip) # Scale self.x1 = self.x1 - self.x1.mean() self.x1 = self.x1 / self.x1.std() # Generate noise self.noise = self.noise_level * np.random.standard_normal((self.Nt, ))
def display_perf_by_servo_from_day(date=None): """Plot perf vs servo position from all sessions from date""" # Get bdf and its dates bdf = BeWatch.db.get_behavior_df() bdf_dates = bdf['dt_end'].apply(lambda dt: dt.date()) # Set to most recent date in database if None if date is None: date = bdf_dates.max() # Choose the ones to display display_dates = bdf.ix[bdf_dates == date] if len(display_dates) > 20: raise ValueError("too many dates") # Display each f, axa = plt.subplots(2, my.rint(np.ceil(len(display_dates) / 2.0)), figsize=(15, 5)) for nax, (idx, row) in enumerate(display_dates.iterrows()): ax = axa.flatten()[nax] display_perf_by_servo(session=row['session'], ax=ax) ax.set_title(row['session'], size='small') f.tight_layout()
def generate_colorbar(n_colors, mapname='jet', rounding=100, start=0., stop=1.): """Generate N evenly spaced colors from start to stop in map""" color_idxs = my.rint(rounding * np.linspace(start, stop, n_colors))[::-1] colors = plt.cm.get_cmap('jet', rounding)(color_idxs) return colors
# Create figure # Rows for N # 4 columns: narrow and broad, score1 and score2 f, axa = plt.subplots(1, 2, figsize=(9, 3)) f.subplots_adjust(left=.125, right=.95, wspace=.75) # Noise levels to plot n_noise_levels_to_plot = list(range(len(noise_level_l))) n_noise_levels_to_plot = np.where( (noise_level_l > 3) & (noise_level_l < 65))[0] # Which n_noise_levels to plot idxs = my.rint(100 * np.linspace(.4, 1.0, len(n_noise_levels_to_plot)))[::-1] colors = plt.cm.get_cmap('Blues', 100)(idxs) colors2= plt.cm.get_cmap('Reds', 100)(idxs) # Get the gains (relative to signal) using the indexes of data (ngain) gain_vs_sig = gains_l[data.index] # Iterate over noise_levels for nnnl, n_noise_level in enumerate(n_noise_levels_to_plot): #data['m'].columns: # Get the noise level, and gain_vs_noise noise_level = noise_level_l[n_noise_level] x_to_plot = gain_vs_sig / noise_level # Plot this trace y_to_plot = data[n_noise_level].copy() axa[0].plot(x_to_plot, y_to_plot,
# Use cross_point to index back into gains_l cross_point_val = cross_point.applymap( lambda idx: gains_l[int(idx)] if not np.isnan(idx) else np.nan) cross_point_val.index = cross_point.index cross_point_val.columns = cross_point.columns # Make a heatmap version of the above f, ax = plt.subplots(figsize=(5, 5)) f.subplots_adjust(right=.8) im = my.plot.imshow(np.log10(cross_point_val['m'].divide(noise_level_l, axis=1).T), cmap=plt.cm.hot, ax=ax) my.plot.harmonize_clim_in_subplots(fig=f, clim=(-1.0, 3)) ax.set_yticks(range(len(noise_level_l))) ax.set_yticklabels(['2^-%d' % my.rint(np.log2(val)) for val in noise_level_l]) ax.set_xticks(range(len(N_l))) ax.set_xticklabels([str(int(val)) for val in N_l[::-1]]) ax.set_xlabel('network size') ax.set_ylabel('sensory signal-to-noise ratio') # Colorbar cbar_ax = f.add_axes([.85, .15, .05, .7]) cb = f.colorbar(im, cax=cbar_ax) cb.set_ticks((-1, 0, 1, 2, 3)) cb.set_ticklabels((.1, 1, 10, 100, 1000)) f.savefig('parameter space 1.svg') plt.show()
data = medians['score1b'].unstack('n_noise_level') data2 = medians['score2b'].unstack('n_noise_level') # Create figure # Rows for N # 4 columns: narrow and broad, score1 and score2 f, axa = plt.subplots(1, 2, figsize=(9, 3)) f.subplots_adjust(left=.125, right=.95, wspace=.75) # Noise levels to plot n_noise_levels_to_plot = list(range(len(noise_level_l))) n_noise_levels_to_plot = np.where((noise_level_l > 3) & (noise_level_l < 65))[0] # Which n_noise_levels to plot idxs = my.rint(100 * np.linspace(.4, 1.0, len(n_noise_levels_to_plot)))[::-1] colors = plt.cm.get_cmap('Blues', 100)(idxs) colors2 = plt.cm.get_cmap('Reds', 100)(idxs) # Get the gains (relative to signal) using the indexes of data (ngain) gain_vs_sig = gains_l[data.index] # Iterate over noise_levels for nnnl, n_noise_level in enumerate( n_noise_levels_to_plot): #data['m'].columns: # Get the noise level, and gain_vs_noise noise_level = noise_level_l[n_noise_level] x_to_plot = gain_vs_sig / noise_level # Plot this trace y_to_plot = data[n_noise_level].copy()