def test_ridge_vs_lstsq(): """On alpha=0., Ridge and OLS yield the same solution.""" # we need more samples than features n_samples, n_features = 5, 4 np.random.seed(0) y = np.random.randn(n_samples) X = np.random.randn(n_samples, n_features) ridge = Ridge(alpha=0.) ols = LinearRegression() ridge.fit(X, y) ols.fit (X, y) assert_almost_equal(ridge.coef_, ols.coef_) ridge.fit(X, y, fit_intercept=False) ols.fit (X, y, fit_intercept=False) assert_almost_equal(ridge.coef_, ols.coef_)
def plot_bold_contrast(data_dir, num_groups, trial_duration, num_trials, p_b_e, p_x_e, p_e_e, p_e_i, p_i_i, p_i_e): file_desc='wta.groups.%d.duration.%0.3f.p_b_e.%0.3f.p_x_e.%0.3f.p_e_e.%0.3f.p_e_i.%0.3f.p_i_i.%0.3f.p_i_e.%0.3f' %\ (num_groups, trial_duration, p_b_e, p_x_e, p_e_e, p_e_i, p_i_i, p_i_e) file_prefix = os.path.join(data_dir, file_desc) trial_contrast = np.zeros([num_trials, 1]) trial_max_bold = np.zeros(num_trials) trial_max_bold_exc = np.zeros(num_trials) for i in range(num_trials): file_name = '%s.trial.%d.h5' % (file_prefix, i) print('opening %s' % file_name) data = FileInfo(file_name) trial_contrast[i] = abs(data.input_freq[0] - data.input_freq[1]) / sum( data.input_freq) trial_max_bold[i] = np.max(data.voxel_rec['y'][0]) trial_max_bold_exc[i] = np.max(data.voxel_exc_rec['y'][0]) clf = LinearRegression() clf.fit(trial_contrast, trial_max_bold) a = clf.coef_[0] b = clf.intercept_ fig = plt.figure() plt.plot(trial_contrast, trial_max_bold, 'x') x_min = np.min(trial_contrast) x_max = np.max(trial_contrast) plt.plot([x_min, x_max], [a * x_min + b, a * x_max + b], '--') plt.xlabel('Input Contrast') plt.ylabel('Max BOLD') plt.show() clf = LinearRegression() clf.fit(trial_contrast, trial_max_bold_exc) a = clf.coef_[0] b = clf.intercept_ fig = plt.figure() plt.plot(trial_contrast, trial_max_bold_exc, 'x') x_min = np.min(trial_contrast) x_max = np.max(trial_contrast) plt.plot([x_min, x_max], [a * x_min + b, a * x_max + b], '--') plt.xlabel('Input Contrast') plt.ylabel('Max BOLD') plt.title('Exc only') plt.show()
def test_ridge_vs_lstsq(): """On alpha=0., Ridge and OLS yield the same solution.""" # we need more samples than features n_samples, n_features = 5, 4 np.random.seed(0) y = np.random.randn(n_samples) X = np.random.randn(n_samples, n_features) ridge = Ridge(alpha=0.) ols = LinearRegression() ridge.fit(X, y) ols.fit(X, y) assert_almost_equal(ridge.coef_, ols.coef_) ridge.fit(X, y, fit_intercept=False) ols.fit(X, y, fit_intercept=False) assert_almost_equal(ridge.coef_, ols.coef_)
def plot_bold_contrast(data_dir, num_groups, trial_duration, num_trials, p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e): file_desc='wta.groups.%d.duration.%0.3f.p_b_e.%0.3f.p_x_e.%0.3f.p_e_e.%0.3f.p_e_i.%0.3f.p_i_i.%0.3f.p_i_e.%0.3f' %\ (num_groups, trial_duration, p_b_e, p_x_e, p_e_e, p_e_i, p_i_i, p_i_e) file_prefix=os.path.join(data_dir,file_desc) trial_contrast=np.zeros([num_trials,1]) trial_max_bold=np.zeros(num_trials) trial_max_bold_exc=np.zeros(num_trials) for i in range(num_trials): file_name='%s.trial.%d.h5' % (file_prefix, i) print('opening %s' % file_name) data=FileInfo(file_name) trial_contrast[i]=abs(data.input_freq[0]-data.input_freq[1])/sum(data.input_freq) trial_max_bold[i]=np.max(data.voxel_rec['y'][0]) trial_max_bold_exc[i]=np.max(data.voxel_exc_rec['y'][0]) clf=LinearRegression() clf.fit(trial_contrast,trial_max_bold) a=clf.coef_[0] b=clf.intercept_ fig=plt.figure() plt.plot(trial_contrast, trial_max_bold, 'x') x_min=np.min(trial_contrast) x_max=np.max(trial_contrast) plt.plot([x_min,x_max],[a*x_min+b,a*x_max+b],'--') plt.xlabel('Input Contrast') plt.ylabel('Max BOLD') plt.show() clf=LinearRegression() clf.fit(trial_contrast,trial_max_bold_exc) a=clf.coef_[0] b=clf.intercept_ fig=plt.figure() plt.plot(trial_contrast, trial_max_bold_exc, 'x') x_min=np.min(trial_contrast) x_max=np.max(trial_contrast) plt.plot([x_min,x_max],[a*x_min+b,a*x_max+b],'--') plt.xlabel('Input Contrast') plt.ylabel('Max BOLD') plt.title('Exc only') plt.show()
def create_bold_report(reports_dir, trial_contrast, trial_max_bold, trial_max_rate, trial_rt, regenerate_plot=True): report_info = Struct() clf = LinearRegression() clf.fit(trial_contrast, trial_max_bold) a = clf.coef_[0] b = clf.intercept_ report_info.bold_contrast_slope = a report_info.bold_contrast_intercept = b report_info.bold_contrast_r_sqr = clf.score(trial_contrast, trial_max_bold) furl = 'img/contrast_bold.png' fname = os.path.join(reports_dir, furl) report_info.contrast_bold_url = furl if regenerate_plot or not os.path.exists(fname): fig = plt.figure() plt.plot(trial_contrast, trial_max_bold, 'x') x_min = np.min(trial_contrast) x_max = np.max(trial_contrast) plt.plot([x_min, x_max], [a * x_min + b, a * x_max + b], '--') plt.xlabel('Input Contrast') plt.ylabel('Max BOLD') save_to_png(fig, fname) save_to_eps(fig, os.path.join(reports_dir, 'img/contrast_bold.eps')) plt.close() clf = LinearRegression() clf.fit(trial_max_rate, trial_max_bold) a = clf.coef_[0] b = clf.intercept_ report_info.bold_firing_rate_slope = a report_info.bold_firing_rate_intercept = b report_info.bold_firing_rate_r_sqr = clf.score(trial_max_rate, trial_max_bold) furl = 'img/firing_rate_bold.png' fname = os.path.join(reports_dir, furl) report_info.firing_rate_bold_url = furl if regenerate_plot or not os.path.exists(fname): fig = plt.figure() plt.plot(trial_max_rate, trial_max_bold, 'x') x_min = np.min(trial_max_rate) x_max = np.max(trial_max_rate) plt.plot([x_min, x_max], [a * x_min + b, a * x_max + b], '--') plt.xlabel('Max Firing Rate') plt.ylabel('Max BOLD') save_to_png(fig, fname) save_to_eps(fig, os.path.join(reports_dir, 'img/firing_rate_bold.eps')) plt.close() clf = LinearRegression() clf.fit(trial_rt, trial_max_bold) a = clf.coef_[0] b = clf.intercept_ report_info.bold_rt_slope = a report_info.bold_rt_intercept = b report_info.bold_rt_r_sqr = clf.score(trial_rt, trial_max_bold) furl = 'img/response_time_bold.png' fname = os.path.join(reports_dir, furl) report_info.response_time_bold_url = furl if regenerate_plot or not os.path.exists(fname): fig = plt.figure() plt.plot(trial_rt, trial_max_bold, 'x') x_min = np.min(trial_rt) x_max = np.max(trial_rt) plt.plot([x_min, x_max], [a * x_min + b, a * x_max + b], '--') plt.xlabel('Response Time') plt.ylabel('Max BOLD') save_to_png(fig, fname) save_to_eps(fig, os.path.join(reports_dir, 'img/response_time_bold.eps')) plt.close() return report_info
def create_wta_network_report(file_prefix, contrast_range, num_trials, reports_dir, edesc, regenerate_network_plots=True, regenerate_trial_plots=True): make_report_dirs(reports_dir) report_info = Struct() report_info.edesc = edesc report_info.trials = [] (data_dir, data_file_prefix) = os.path.split(file_prefix) total_trials = num_trials * len(contrast_range) trial_contrast = np.zeros([total_trials, 1]) trial_max_bold = np.zeros(total_trials) trial_max_input = np.zeros([total_trials, 1]) trial_max_rate = np.zeros([total_trials, 1]) trial_rt = np.zeros([total_trials, 1]) report_info.contrast_accuracy = np.zeros([len(contrast_range), 1]) max_bold = [] for j, contrast in enumerate(contrast_range): contrast_max_bold = [] report_info.contrast_accuracy[j] = 0.0 for i in range(num_trials): file_name = '%s.contrast.%0.4f.trial.%d.h5' % (file_prefix, contrast, i) print('opening %s' % file_name) data = FileInfo(file_name) if not i: report_info.wta_params = data.wta_params report_info.voxel_params = data.voxel_params report_info.num_groups = data.num_groups report_info.trial_duration = data.trial_duration report_info.background_rate = data.background_rate report_info.stim_start_time = data.stim_start_time report_info.stim_end_time = data.stim_end_time report_info.network_group_size = data.network_group_size report_info.background_input_size = data.background_input_size report_info.task_input_size = data.task_input_size trial_idx = j * num_trials + i trial = create_trial_report( data, reports_dir, contrast, i, regenerate_plots=regenerate_trial_plots) trial_contrast[trial_idx] = trial.input_contrast if not math.isnan(trial.max_bold): trial_max_bold[trial_idx] = trial.max_bold else: if j > 1 and max_bold[j - 1] < 1.0 and max_bold[j - 2] < 1.0: trial_max_bold[trial_idx] = max_bold[j - 1] + ( max_bold[j - 1] - max_bold[j - 2]) elif j > 0 and max_bold[j - 1] < 1.0: trial_max_bold[trial_idx] = max_bold[j - 1] * 2.0 else: trial_max_bold[trial_idx] = 1.0 report_info.contrast_accuracy[j] += trial.correct trial_max_input[trial_idx] = trial.max_input trial_max_rate[trial_idx] = trial.max_rate trial_rt[trial_idx] = trial.rt report_info.trials.append(trial) contrast_max_bold.append(trial_max_bold[trial_idx]) report_info.contrast_accuracy[j] /= float(num_trials) mean_contrast_bold = np.mean(np.array(contrast_max_bold)) max_bold.append(mean_contrast_bold) clf = LinearRegression() clf.fit(trial_max_input, trial_max_rate) a = clf.coef_[0] b = clf.intercept_ report_info.io_slope = a report_info.io_intercept = b report_info.io_r_sqr = clf.score(trial_max_input, trial_max_rate) furl = 'img/input_output_rate.png' fname = os.path.join(reports_dir, furl) report_info.input_output_rate_url = furl if regenerate_network_plots or not os.path.exists(fname): fig = plt.figure() plt.plot(trial_max_input, trial_max_rate, 'x') x_min = np.min(trial_max_input) x_max = np.max(trial_max_input) plt.plot([x_min, x_max], [x_min, x_max], '--') plt.plot([x_min, x_max], [a * x_min + b, a * x_max + b], '--') plt.xlabel('Max Input Rate') plt.ylabel('Max Population Rate') save_to_png(fig, fname) save_to_eps(fig, os.path.join(reports_dir, 'img/input_output_rate.eps')) plt.close() report_info.bold = create_bold_report( reports_dir, trial_contrast, trial_max_bold, trial_max_rate, trial_rt, regenerate_plot=regenerate_network_plots) report_info.roc = create_roc_report( file_prefix, report_info.num_groups, contrast_range, num_trials, reports_dir, regenerate_plot=regenerate_network_plots) #create report template_file = 'wta_network_instance.html' env = Environment(loader=FileSystemLoader(TEMPLATE_DIR)) template = env.get_template(template_file) output_file = 'wta_network.%s.html' % data_file_prefix fname = os.path.join(reports_dir, output_file) stream = template.stream(rinfo=report_info) stream.dump(fname) return report_info
def create_bold_report(reports_dir, trial_contrast, trial_max_bold, trial_max_rate, trial_rt, regenerate_plot=True): report_info=Struct() clf=LinearRegression() clf.fit(trial_contrast,trial_max_bold) a=clf.coef_[0] b=clf.intercept_ report_info.bold_contrast_slope=a report_info.bold_contrast_intercept=b report_info.bold_contrast_r_sqr=clf.score(trial_contrast,trial_max_bold) furl='img/contrast_bold.png' fname=os.path.join(reports_dir, furl) report_info.contrast_bold_url=furl if regenerate_plot or not os.path.exists(fname): fig=plt.figure() plt.plot(trial_contrast, trial_max_bold, 'x') x_min=np.min(trial_contrast) x_max=np.max(trial_contrast) plt.plot([x_min,x_max],[a*x_min+b,a*x_max+b],'--') plt.xlabel('Input Contrast') plt.ylabel('Max BOLD') save_to_png(fig, fname) save_to_eps(fig, os.path.join(reports_dir, 'img/contrast_bold.eps')) plt.close() clf=LinearRegression() clf.fit(trial_max_rate,trial_max_bold) a=clf.coef_[0] b=clf.intercept_ report_info.bold_firing_rate_slope=a report_info.bold_firing_rate_intercept=b report_info.bold_firing_rate_r_sqr=clf.score(trial_max_rate,trial_max_bold) furl='img/firing_rate_bold.png' fname=os.path.join(reports_dir, furl) report_info.firing_rate_bold_url=furl if regenerate_plot or not os.path.exists(fname): fig=plt.figure() plt.plot(trial_max_rate, trial_max_bold, 'x') x_min=np.min(trial_max_rate) x_max=np.max(trial_max_rate) plt.plot([x_min,x_max],[a*x_min+b,a*x_max+b],'--') plt.xlabel('Max Firing Rate') plt.ylabel('Max BOLD') save_to_png(fig, fname) save_to_eps(fig, os.path.join(reports_dir, 'img/firing_rate_bold.eps')) plt.close() clf=LinearRegression() clf.fit(trial_rt,trial_max_bold) a=clf.coef_[0] b=clf.intercept_ report_info.bold_rt_slope=a report_info.bold_rt_intercept=b report_info.bold_rt_r_sqr=clf.score(trial_rt,trial_max_bold) furl='img/response_time_bold.png' fname=os.path.join(reports_dir, furl) report_info.response_time_bold_url=furl if regenerate_plot or not os.path.exists(fname): fig=plt.figure() plt.plot(trial_rt, trial_max_bold, 'x') x_min=np.min(trial_rt) x_max=np.max(trial_rt) plt.plot([x_min,x_max],[a*x_min+b,a*x_max+b],'--') plt.xlabel('Response Time') plt.ylabel('Max BOLD') save_to_png(fig, fname) save_to_eps(fig, os.path.join(reports_dir, 'img/response_time_bold.eps')) plt.close() return report_info
def create_wta_network_report(file_prefix, contrast_range, num_trials, reports_dir, edesc, regenerate_network_plots=True, regenerate_trial_plots=True): make_report_dirs(reports_dir) report_info=Struct() report_info.edesc=edesc report_info.trials=[] (data_dir, data_file_prefix) = os.path.split(file_prefix) total_trials=num_trials*len(contrast_range) trial_contrast=np.zeros([total_trials,1]) trial_max_bold=np.zeros(total_trials) trial_max_input=np.zeros([total_trials,1]) trial_max_rate=np.zeros([total_trials,1]) trial_rt=np.zeros([total_trials,1]) report_info.contrast_accuracy=np.zeros([len(contrast_range),1]) max_bold=[] for j,contrast in enumerate(contrast_range): contrast_max_bold=[] report_info.contrast_accuracy[j]=0.0 for i in range(num_trials): file_name='%s.contrast.%0.4f.trial.%d.h5' % (file_prefix, contrast, i) print('opening %s' % file_name) data=FileInfo(file_name) if not i: report_info.wta_params=data.wta_params report_info.voxel_params=data.voxel_params report_info.num_groups=data.num_groups report_info.trial_duration=data.trial_duration report_info.background_rate=data.background_rate report_info.stim_start_time=data.stim_start_time report_info.stim_end_time=data.stim_end_time report_info.network_group_size=data.network_group_size report_info.background_input_size=data.background_input_size report_info.task_input_size=data.task_input_size trial_idx=j*num_trials+i trial = create_trial_report(data, reports_dir, contrast, i, regenerate_plots=regenerate_trial_plots) trial_contrast[trial_idx]=trial.input_contrast if not math.isnan(trial.max_bold): trial_max_bold[trial_idx]=trial.max_bold else: if j>1 and max_bold[j-1]<1.0 and max_bold[j-2]<1.0: trial_max_bold[trial_idx]=max_bold[j-1]+(max_bold[j-1]-max_bold[j-2]) elif j>0 and max_bold[j-1]<1.0: trial_max_bold[trial_idx]=max_bold[j-1]*2.0 else: trial_max_bold[trial_idx]=1.0 report_info.contrast_accuracy[j]+=trial.correct trial_max_input[trial_idx]=trial.max_input trial_max_rate[trial_idx]=trial.max_rate trial_rt[trial_idx]=trial.rt report_info.trials.append(trial) contrast_max_bold.append(trial_max_bold[trial_idx]) report_info.contrast_accuracy[j]/=float(num_trials) mean_contrast_bold=np.mean(np.array(contrast_max_bold)) max_bold.append(mean_contrast_bold) clf=LinearRegression() clf.fit(trial_max_input,trial_max_rate) a=clf.coef_[0] b=clf.intercept_ report_info.io_slope=a report_info.io_intercept=b report_info.io_r_sqr=clf.score(trial_max_input,trial_max_rate) furl='img/input_output_rate.png' fname=os.path.join(reports_dir, furl) report_info.input_output_rate_url=furl if regenerate_network_plots or not os.path.exists(fname): fig=plt.figure() plt.plot(trial_max_input, trial_max_rate, 'x') x_min=np.min(trial_max_input) x_max=np.max(trial_max_input) plt.plot([x_min,x_max],[x_min,x_max],'--') plt.plot([x_min,x_max],[a*x_min+b,a*x_max+b],'--') plt.xlabel('Max Input Rate') plt.ylabel('Max Population Rate') save_to_png(fig, fname) save_to_eps(fig, os.path.join(reports_dir, 'img/input_output_rate.eps')) plt.close() report_info.bold=create_bold_report(reports_dir, trial_contrast, trial_max_bold, trial_max_rate, trial_rt, regenerate_plot=regenerate_network_plots) report_info.roc=create_roc_report(file_prefix, report_info.num_groups, contrast_range, num_trials, reports_dir, regenerate_plot=regenerate_network_plots) #create report template_file='wta_network_instance.html' env = Environment(loader=FileSystemLoader(TEMPLATE_DIR)) template=env.get_template(template_file) output_file='wta_network.%s.html' % data_file_prefix fname=os.path.join(reports_dir,output_file) stream=template.stream(rinfo=report_info) stream.dump(fname) return report_info