Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def create_all_reports(data_dir,
                       num_groups,
                       trial_duration,
                       p_b_e_range,
                       p_x_e_range,
                       p_e_e_range,
                       p_e_i_range,
                       p_i_i_range,
                       p_i_e_range,
                       contrast_range,
                       num_trials,
                       e_desc,
                       base_report_dir,
                       regenerate_network_plots=True,
                       regenerate_trial_plots=True,
                       smooth_missing_params=False,
                       summary_filename='wta_network_summary.h5'):

    make_report_dirs(base_report_dir)

    summary_data = SummaryData(num_groups=num_groups,
                               num_trials=num_trials,
                               trial_duration=trial_duration,
                               p_b_e_range=p_b_e_range,
                               p_x_e_range=p_x_e_range,
                               p_e_e_range=p_e_e_range,
                               p_e_i_range=p_e_i_range,
                               p_i_i_range=p_i_i_range,
                               p_i_e_range=p_i_e_range)

    bc_slope_dict = {}
    bc_intercept_dict = {}
    bc_r_sqr_dict = {}
    auc_dict = {}
    bfr_slope_dict = {}
    bfr_intercept_dict = {}
    bfr_r_sqr_dict = {}

    param_combos = get_tested_param_combos(data_dir, num_groups,
                                           trial_duration, contrast_range,
                                           num_trials, e_desc)

    report_info = Struct()
    report_info.edesc = e_desc
    report_info.roc_auc = {}
    report_info.bc_slope = {}
    report_info.bc_intercept = {}
    report_info.bc_r_sqr = {}
    report_info.bfr_slope = {}
    report_info.bfr_intercept = {}
    report_info.bfr_r_sqr = {}

    for (p_b_e, p_x_e, p_e_e, p_e_i, p_i_i, p_i_e) in param_combos:
        if p_b_e in p_b_e_range and p_x_e in p_x_e_range and p_e_e in p_e_e_range and p_e_i in p_e_i_range and p_i_i in p_i_i_range and p_i_e in p_i_e_range:
            i = p_b_e_range.index(round(p_b_e, 3))
            j = p_x_e_range.index(round(p_x_e, 3))
            k = p_e_e_range.index(round(p_e_e, 3))
            l = p_e_i_range.index(round(p_e_i, 3))
            m = p_i_i_range.index(round(p_i_i, 3))
            n = p_i_e_range.index(round(p_i_e, 3))

            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.%s' %\
                      (num_groups, trial_duration, p_b_e, p_x_e, p_e_e, p_e_i, p_i_i, p_i_e, e_desc)
            file_prefix = os.path.join(data_dir, file_desc)
            reports_dir = os.path.join(base_report_dir, file_desc)
            if all_trials_exist(file_prefix, contrast_range, num_trials):
                print('Creating report for %s' % file_desc)
                wta_report = create_wta_network_report(
                    file_prefix,
                    contrast_range,
                    num_trials,
                    reports_dir,
                    e_desc,
                    regenerate_network_plots=regenerate_network_plots,
                    regenerate_trial_plots=regenerate_trial_plots)

                if not (i, j, k, l, m, n) in bc_slope_dict:
                    bc_slope_dict[(i, j, k, l, m, n)] = []
                bc_slope_dict[(i, j, k, l, m,
                               n)].append(wta_report.bold.bold_contrast_slope)
                if not (i, j, k, l, m, n) in bc_intercept_dict:
                    bc_intercept_dict[(i, j, k, l, m, n)] = []
                bc_intercept_dict[(i, j, k, l, m, n)].append(
                    wta_report.bold.bold_contrast_intercept)
                if not (i, j, k, l, m, n) in bc_r_sqr_dict:
                    bc_r_sqr_dict[(i, j, k, l, m, n)] = []
                bc_r_sqr_dict[(i, j, k, l, m,
                               n)].append(wta_report.bold.bold_contrast_r_sqr)

                if not (i, j, k, l, m, n) in auc_dict:
                    auc_dict[(i, j, k, l, m, n)] = []
                auc_dict[(i, j, k, l, m, n)].append(wta_report.roc.auc)

                if not (i, j, k, l, m, n) in bfr_slope_dict:
                    bfr_slope_dict[(i, j, k, l, m, n)] = []
                bfr_slope_dict[(i, j, k, l, m, n)].append(
                    wta_report.bold.bold_firing_rate_slope)
                if not (i, j, k, l, m, n) in bfr_intercept_dict:
                    bfr_intercept_dict[(i, j, k, l, m, n)] = []
                bfr_intercept_dict[(i, j, k, l, m, n)].append(
                    wta_report.bold.bold_firing_rate_intercept)
                if not (i, j, k, l, m, n) in bfr_r_sqr_dict:
                    bfr_r_sqr_dict[(i, j, k, l, m, n)] = []
                bfr_r_sqr_dict[(i, j, k, l, m, n)].append(
                    wta_report.bold.bold_firing_rate_r_sqr)

                report_info.roc_auc[(p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                     p_i_e)] = wta_report.roc.auc
                report_info.bc_slope[(
                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                    p_i_e)] = wta_report.bold.bold_contrast_slope
                report_info.bc_intercept[(
                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                    p_i_e)] = wta_report.bold.bold_contrast_intercept
                report_info.bc_r_sqr[(
                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                    p_i_e)] = wta_report.bold.bold_contrast_r_sqr
                report_info.bfr_slope[(
                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                    p_i_e)] = wta_report.bold.bold_firing_rate_slope
                report_info.bfr_intercept[(
                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                    p_i_e)] = wta_report.bold.bold_firing_rate_intercept
                report_info.bfr_r_sqr[(
                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                    p_i_e)] = wta_report.bold.bold_firing_rate_r_sqr
            else:
                report_info.roc_auc[(p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                     p_i_e)] = 0
                report_info.bc_slope[(p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                      p_i_e)] = 0
                report_info.bc_intercept[(p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                          p_i_e)] = 0
                report_info.bc_r_sqr[(p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                      p_i_e)] = 0
                report_info.bfr_slope[(p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                       p_i_e)] = 0
                report_info.bfr_intercept[(p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                           p_i_e)] = 0
                report_info.brf_r_sqr[(p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                       p_i_e)] = 0

    report_info.num_groups = num_groups
    report_info.trial_duration = trial_duration
    report_info.num_trials = num_trials
    report_info.p_b_e_range = p_b_e_range
    report_info.p_x_e_range = p_x_e_range
    report_info.p_e_e_range = p_e_e_range
    report_info.p_e_i_range = p_e_i_range
    report_info.p_i_i_range = p_i_i_range
    report_info.p_i_e_range = p_i_e_range

    summary_data.fill(auc_dict,
                      bc_slope_dict,
                      bc_intercept_dict,
                      bc_r_sqr_dict,
                      bfr_slope_dict,
                      bfr_intercept_dict,
                      bfr_r_sqr_dict,
                      smooth_missing_params=smooth_missing_params)

    summary_data.write_to_file(os.path.join(base_report_dir, summary_filename))

    bc_bayes_analysis = run_bayesian_analysis(
        summary_data.auc, summary_data.bc_slope, summary_data.bc_intercept,
        summary_data.bc_r_sqr, num_trials, p_b_e_range, p_e_e_range,
        p_e_i_range, p_i_e_range, p_i_i_range, p_x_e_range)

    bfr_bayes_analysis = run_bayesian_analysis(
        summary_data.auc, summary_data.bfr_slope, summary_data.bfr_intercept,
        summary_data.bfr_r_sqr, num_trials, p_b_e_range, p_e_e_range,
        p_e_i_range, p_i_e_range, p_i_i_range, p_x_e_range)

    bc_base_dir = os.path.join(base_report_dir, 'bold-contrast')
    make_report_dirs(bc_base_dir)
    render_summary_report(bc_base_dir, bc_bayes_analysis, p_b_e_range,
                          p_e_e_range, p_e_i_range, p_i_e_range, p_i_i_range,
                          p_x_e_range, report_info)

    bfr_base_dir = os.path.join(base_report_dir, 'bold-firing_rate')
    make_report_dirs(bfr_base_dir)
    render_summary_report(bfr_base_dir, bfr_bayes_analysis, p_b_e_range,
                          p_e_e_range, p_e_i_range, p_i_e_range, p_i_i_range,
                          p_x_e_range, report_info)
Ejemplo n.º 3
0
def create_summary_report(summary_file_name, base_report_dir, e_desc):
    make_report_dirs(base_report_dir)

    summary_data=SummaryData()
    summary_data.read_from_file(summary_file_name)

    report_info=Struct()
    report_info.edesc=e_desc
    report_info.roc_auc={}
    report_info.io_slope={}
    report_info.io_intercept={}
    report_info.io_r_sqr={}
    report_info.bc_slope={}
    report_info.bc_intercept={}
    report_info.bc_r_sqr={}
    report_info.bfr_slope={}
    report_info.bfr_intercept={}
    report_info.bfr_r_sqr={}
    for i,p_b_e in enumerate(summary_data.p_b_e_range):
        for j,p_x_e in enumerate(summary_data.p_x_e_range):
            for k,p_e_e in enumerate(summary_data.p_e_e_range):
                for l,p_e_i in enumerate(summary_data.p_e_i_range):
                    for m,p_i_i in enumerate(summary_data.p_i_i_range):
                        for n,p_i_e in enumerate(summary_data.p_i_e_range):
                            if summary_data.auc[i,j,k,l,m,n]>0:
                                report_info.roc_auc[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=summary_data.auc[i,j,k,l,m,n]
                                report_info.bc_slope[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=summary_data.bc_slope[i,j,k,l,m,n]
                                report_info.bc_intercept[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=summary_data.bc_intercept[i,j,k,l,m,n]
                                report_info.bc_r_sqr[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=summary_data.bc_r_sqr[i,j,k,l,m,n]
                                report_info.bfr_slope[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=summary_data.bfr_slope[i,j,k,l,m,n]
                                report_info.bfr_intercept[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=summary_data.bfr_intercept[i,j,k,l,m,n]
                                report_info.bfr_r_sqr[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=summary_data.bfr_r_sqr[i,j,k,l,m,n]
                            else:
                                report_info.roc_auc[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                                report_info.bc_slope[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                                report_info.bc_intercept[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                                report_info.bc_r_sqr[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                                report_info.bfr_slope[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                                report_info.bfr_intercept[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                                report_info.bfr_r_sqr[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0

    report_info.num_groups=summary_data.num_groups
    report_info.trial_duration=summary_data.trial_duration
    report_info.num_trials=summary_data.num_trials
    report_info.p_b_e_range=summary_data.p_b_e_range
    report_info.p_x_e_range=summary_data.p_x_e_range
    report_info.p_e_e_range=summary_data.p_e_e_range[:-1]
    report_info.p_e_i_range=summary_data.p_e_i_range
    report_info.p_i_i_range=summary_data.p_i_i_range
    report_info.p_i_e_range=summary_data.p_i_e_range

    bc_bayes_analysis=run_bayesian_analysis(summary_data.auc, summary_data.bc_slope, summary_data.bc_intercept,
        summary_data.bc_r_sqr, summary_data.num_trials, summary_data.p_b_e_range, summary_data.p_e_e_range,
        summary_data.p_e_i_range, summary_data.p_i_e_range, summary_data.p_i_i_range, summary_data.p_x_e_range)

    bfr_bayes_analysis=run_bayesian_analysis(summary_data.auc, summary_data.bfr_slope, summary_data.bfr_intercept,
        summary_data.bfr_r_sqr, summary_data.num_trials, summary_data.p_b_e_range, summary_data.p_e_e_range,
        summary_data.p_e_i_range, summary_data.p_i_e_range, summary_data.p_i_i_range, summary_data.p_x_e_range)

    bc_base_dir=os.path.join(base_report_dir, 'bold-contrast')
    make_report_dirs(bc_base_dir)
    render_summary_report(bc_base_dir, bc_bayes_analysis, summary_data.p_b_e_range, summary_data.p_e_e_range,
        summary_data.p_e_i_range, summary_data.p_i_e_range, summary_data.p_i_i_range, summary_data.p_x_e_range,
        report_info)

    bfr_base_dir=os.path.join(base_report_dir, 'bold-firing_rate')
    make_report_dirs(bfr_base_dir)
    render_summary_report(bfr_base_dir, bfr_bayes_analysis, summary_data.p_b_e_range, summary_data.p_e_e_range,
        summary_data.p_e_i_range, summary_data.p_i_e_range, summary_data.p_i_i_range, summary_data.p_x_e_range,
        report_info)
Ejemplo n.º 4
0
def create_all_reports(data_dir, num_groups, trial_duration, p_b_e_range, p_x_e_range, p_e_e_range, p_e_i_range,
                       p_i_i_range, p_i_e_range, contrast_range, num_trials, e_desc, base_report_dir, regenerate_network_plots=True,
                       regenerate_trial_plots=True, smooth_missing_params=False,
                       summary_filename='wta_network_summary.h5'):

    make_report_dirs(base_report_dir)

    summary_data=SummaryData(num_groups=num_groups, num_trials=num_trials, trial_duration=trial_duration,
        p_b_e_range=p_b_e_range, p_x_e_range=p_x_e_range, p_e_e_range=p_e_e_range, p_e_i_range=p_e_i_range,
        p_i_i_range=p_i_i_range, p_i_e_range=p_i_e_range)

    bc_slope_dict={}
    bc_intercept_dict={}
    bc_r_sqr_dict={}
    auc_dict={}
    bfr_slope_dict={}
    bfr_intercept_dict={}
    bfr_r_sqr_dict={}

    param_combos=get_tested_param_combos(data_dir, num_groups, trial_duration, contrast_range, num_trials, e_desc)

    report_info=Struct()
    report_info.edesc=e_desc
    report_info.roc_auc={}
    report_info.bc_slope={}
    report_info.bc_intercept={}
    report_info.bc_r_sqr={}
    report_info.bfr_slope={}
    report_info.bfr_intercept={}
    report_info.bfr_r_sqr={}


    for (p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e) in param_combos:
        if p_b_e in p_b_e_range and p_x_e in p_x_e_range and p_e_e in p_e_e_range and p_e_i in p_e_i_range and p_i_i in p_i_i_range and p_i_e in p_i_e_range:
            i=p_b_e_range.index(round(p_b_e,3))
            j=p_x_e_range.index(round(p_x_e,3))
            k=p_e_e_range.index(round(p_e_e,3))
            l=p_e_i_range.index(round(p_e_i,3))
            m=p_i_i_range.index(round(p_i_i,3))
            n=p_i_e_range.index(round(p_i_e,3))

            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.%s' %\
                      (num_groups, trial_duration, p_b_e, p_x_e, p_e_e, p_e_i, p_i_i, p_i_e, e_desc)
            file_prefix=os.path.join(data_dir,file_desc)
            reports_dir=os.path.join(base_report_dir,file_desc)
            if all_trials_exist(file_prefix, contrast_range, num_trials):
                print('Creating report for %s' % file_desc)
                wta_report=create_wta_network_report(file_prefix, contrast_range, num_trials, reports_dir,
                    e_desc, regenerate_network_plots=regenerate_network_plots, regenerate_trial_plots=regenerate_trial_plots)

                if not (i,j,k,l,m,n) in bc_slope_dict:
                    bc_slope_dict[(i,j,k,l,m,n)]=[]
                bc_slope_dict[(i,j,k,l,m,n)].append(wta_report.bold.bold_contrast_slope)
                if not (i,j,k,l,m,n) in bc_intercept_dict:
                    bc_intercept_dict[(i,j,k,l,m,n)]=[]
                bc_intercept_dict[(i,j,k,l,m,n)].append(wta_report.bold.bold_contrast_intercept)
                if not (i,j,k,l,m,n) in bc_r_sqr_dict:
                    bc_r_sqr_dict[(i,j,k,l,m,n)]=[]
                bc_r_sqr_dict[(i,j,k,l,m,n)].append(wta_report.bold.bold_contrast_r_sqr)

                if not (i,j,k,l,m,n) in auc_dict:
                    auc_dict[(i,j,k,l,m,n)]=[]
                auc_dict[(i,j,k,l,m,n)].append(wta_report.roc.auc)

                if not (i,j,k,l,m,n) in bfr_slope_dict:
                    bfr_slope_dict[(i,j,k,l,m,n)]=[]
                bfr_slope_dict[(i,j,k,l,m,n)].append(wta_report.bold.bold_firing_rate_slope)
                if not (i,j,k,l,m,n) in bfr_intercept_dict:
                    bfr_intercept_dict[(i,j,k,l,m,n)]=[]
                bfr_intercept_dict[(i,j,k,l,m,n)].append(wta_report.bold.bold_firing_rate_intercept)
                if not (i,j,k,l,m,n) in bfr_r_sqr_dict:
                    bfr_r_sqr_dict[(i,j,k,l,m,n)]=[]
                bfr_r_sqr_dict[(i,j,k,l,m,n)].append(wta_report.bold.bold_firing_rate_r_sqr)

                report_info.roc_auc[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=wta_report.roc.auc
                report_info.bc_slope[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=wta_report.bold.bold_contrast_slope
                report_info.bc_intercept[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=wta_report.bold.bold_contrast_intercept
                report_info.bc_r_sqr[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=wta_report.bold.bold_contrast_r_sqr
                report_info.bfr_slope[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=wta_report.bold.bold_firing_rate_slope
                report_info.bfr_intercept[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=wta_report.bold.bold_firing_rate_intercept
                report_info.bfr_r_sqr[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=wta_report.bold.bold_firing_rate_r_sqr
            else:
                report_info.roc_auc[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                report_info.bc_slope[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                report_info.bc_intercept[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                report_info.bc_r_sqr[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                report_info.bfr_slope[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                report_info.bfr_intercept[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0
                report_info.brf_r_sqr[(p_b_e,p_x_e,p_e_e,p_e_i,p_i_i,p_i_e)]=0

    report_info.num_groups=num_groups
    report_info.trial_duration=trial_duration
    report_info.num_trials=num_trials
    report_info.p_b_e_range=p_b_e_range
    report_info.p_x_e_range=p_x_e_range
    report_info.p_e_e_range=p_e_e_range
    report_info.p_e_i_range=p_e_i_range
    report_info.p_i_i_range=p_i_i_range
    report_info.p_i_e_range=p_i_e_range

    summary_data.fill(auc_dict, bc_slope_dict, bc_intercept_dict, bc_r_sqr_dict, bfr_slope_dict, bfr_intercept_dict,
        bfr_r_sqr_dict, smooth_missing_params=smooth_missing_params)

    summary_data.write_to_file(os.path.join(base_report_dir,summary_filename))

    bc_bayes_analysis=run_bayesian_analysis(summary_data.auc, summary_data.bc_slope, summary_data.bc_intercept,
        summary_data.bc_r_sqr, num_trials, p_b_e_range, p_e_e_range, p_e_i_range, p_i_e_range, p_i_i_range, p_x_e_range)

    bfr_bayes_analysis=run_bayesian_analysis(summary_data.auc, summary_data.bfr_slope, summary_data.bfr_intercept,
            summary_data.bfr_r_sqr, num_trials, p_b_e_range, p_e_e_range, p_e_i_range, p_i_e_range, p_i_i_range, p_x_e_range)

    bc_base_dir=os.path.join(base_report_dir, 'bold-contrast')
    make_report_dirs(bc_base_dir)
    render_summary_report(bc_base_dir, bc_bayes_analysis, p_b_e_range, p_e_e_range, p_e_i_range, p_i_e_range,
        p_i_i_range, p_x_e_range, report_info)

    bfr_base_dir=os.path.join(base_report_dir, 'bold-firing_rate')
    make_report_dirs(bfr_base_dir)
    render_summary_report(bfr_base_dir, bfr_bayes_analysis, p_b_e_range, p_e_e_range, p_e_i_range, p_i_e_range,
        p_i_i_range, p_x_e_range, report_info)
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def create_summary_report(summary_file_name, base_report_dir, e_desc):
    make_report_dirs(base_report_dir)

    summary_data = SummaryData()
    summary_data.read_from_file(summary_file_name)

    report_info = Struct()
    report_info.edesc = e_desc
    report_info.roc_auc = {}
    report_info.io_slope = {}
    report_info.io_intercept = {}
    report_info.io_r_sqr = {}
    report_info.bc_slope = {}
    report_info.bc_intercept = {}
    report_info.bc_r_sqr = {}
    report_info.bfr_slope = {}
    report_info.bfr_intercept = {}
    report_info.bfr_r_sqr = {}
    for i, p_b_e in enumerate(summary_data.p_b_e_range):
        for j, p_x_e in enumerate(summary_data.p_x_e_range):
            for k, p_e_e in enumerate(summary_data.p_e_e_range):
                for l, p_e_i in enumerate(summary_data.p_e_i_range):
                    for m, p_i_i in enumerate(summary_data.p_i_i_range):
                        for n, p_i_e in enumerate(summary_data.p_i_e_range):
                            if summary_data.auc[i, j, k, l, m, n] > 0:
                                report_info.roc_auc[(
                                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                    p_i_e)] = summary_data.auc[i, j, k, l, m,
                                                               n]
                                report_info.bc_slope[(
                                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                    p_i_e)] = summary_data.bc_slope[i, j, k, l,
                                                                    m, n]
                                report_info.bc_intercept[(
                                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                    p_i_e)] = summary_data.bc_intercept[i, j,
                                                                        k, l,
                                                                        m, n]
                                report_info.bc_r_sqr[(
                                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                    p_i_e)] = summary_data.bc_r_sqr[i, j, k, l,
                                                                    m, n]
                                report_info.bfr_slope[(
                                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                    p_i_e)] = summary_data.bfr_slope[i, j, k,
                                                                     l, m, n]
                                report_info.bfr_intercept[(
                                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                    p_i_e)] = summary_data.bfr_intercept[i, j,
                                                                         k, l,
                                                                         m, n]
                                report_info.bfr_r_sqr[(
                                    p_b_e, p_x_e, p_e_e, p_e_i, p_i_i,
                                    p_i_e)] = summary_data.bfr_r_sqr[i, j, k,
                                                                     l, m, n]
                            else:
                                report_info.roc_auc[(p_b_e, p_x_e, p_e_e,
                                                     p_e_i, p_i_i, p_i_e)] = 0
                                report_info.bc_slope[(p_b_e, p_x_e, p_e_e,
                                                      p_e_i, p_i_i, p_i_e)] = 0
                                report_info.bc_intercept[(p_b_e, p_x_e, p_e_e,
                                                          p_e_i, p_i_i,
                                                          p_i_e)] = 0
                                report_info.bc_r_sqr[(p_b_e, p_x_e, p_e_e,
                                                      p_e_i, p_i_i, p_i_e)] = 0
                                report_info.bfr_slope[(p_b_e, p_x_e, p_e_e,
                                                       p_e_i, p_i_i,
                                                       p_i_e)] = 0
                                report_info.bfr_intercept[(p_b_e, p_x_e, p_e_e,
                                                           p_e_i, p_i_i,
                                                           p_i_e)] = 0
                                report_info.bfr_r_sqr[(p_b_e, p_x_e, p_e_e,
                                                       p_e_i, p_i_i,
                                                       p_i_e)] = 0

    report_info.num_groups = summary_data.num_groups
    report_info.trial_duration = summary_data.trial_duration
    report_info.num_trials = summary_data.num_trials
    report_info.p_b_e_range = summary_data.p_b_e_range
    report_info.p_x_e_range = summary_data.p_x_e_range
    report_info.p_e_e_range = summary_data.p_e_e_range[:-1]
    report_info.p_e_i_range = summary_data.p_e_i_range
    report_info.p_i_i_range = summary_data.p_i_i_range
    report_info.p_i_e_range = summary_data.p_i_e_range

    bc_bayes_analysis = run_bayesian_analysis(
        summary_data.auc, summary_data.bc_slope, summary_data.bc_intercept,
        summary_data.bc_r_sqr, summary_data.num_trials,
        summary_data.p_b_e_range, summary_data.p_e_e_range,
        summary_data.p_e_i_range, summary_data.p_i_e_range,
        summary_data.p_i_i_range, summary_data.p_x_e_range)

    bfr_bayes_analysis = run_bayesian_analysis(
        summary_data.auc, summary_data.bfr_slope, summary_data.bfr_intercept,
        summary_data.bfr_r_sqr, summary_data.num_trials,
        summary_data.p_b_e_range, summary_data.p_e_e_range,
        summary_data.p_e_i_range, summary_data.p_i_e_range,
        summary_data.p_i_i_range, summary_data.p_x_e_range)

    bc_base_dir = os.path.join(base_report_dir, 'bold-contrast')
    make_report_dirs(bc_base_dir)
    render_summary_report(bc_base_dir, bc_bayes_analysis,
                          summary_data.p_b_e_range, summary_data.p_e_e_range,
                          summary_data.p_e_i_range, summary_data.p_i_e_range,
                          summary_data.p_i_i_range, summary_data.p_x_e_range,
                          report_info)

    bfr_base_dir = os.path.join(base_report_dir, 'bold-firing_rate')
    make_report_dirs(bfr_base_dir)
    render_summary_report(bfr_base_dir, bfr_bayes_analysis,
                          summary_data.p_b_e_range, summary_data.p_e_e_range,
                          summary_data.p_e_i_range, summary_data.p_i_e_range,
                          summary_data.p_i_i_range, summary_data.p_x_e_range,
                          report_info)