import numpy import pandas from jinja2 import Environment, FileSystemLoader from weasyprint import HTML from tools.helper import get_filters from tools.definitions import OUTPUT_FOLDER, HELPER_TABLE_FOLDER, STIMULATION_METADATA_FILTERED def yes(bool_array): if numpy.isnan(bool_array.values[0]): return numpy.NaN else: return numpy.sum(bool_array.values.astype(numpy.int)) def no(bool_array): no_yes = yes(bool_array) if numpy.isnan(no_yes): return numpy.NaN else: return len(bool_array) - no_yes # Prepare template env = Environment(loader=FileSystemLoader('.')) REPORT_HTML_TEMPLATE = "scripts/tools/report.html" template = env.get_template(REPORT_HTML_TEMPLATE) path_to_filtered_stimulus_file = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+STIMULATION_METADATA_FILTERED all_protocols = pandas.read_csv(path_to_filtered_stimulus_file, index_col="stimulation_id") protocols_for_persistent_activity = all_protocols[get_filters(all_protocols) & (all_protocols["is_control"] == False)] protocols_for_control = all_protocols[get_filters(all_protocols) & (all_protocols["is_control"] == True)] protocols_filtered = all_protocols[get_filters(all_protocols)==False] overview_protocols = pandas.DataFrame(protocols_for_persistent_activity.groupby(["protocol_type", "pulse_number"]).agg({ "was_successful": [yes, no]})) protocols = [] overview_plots=[] for name, df in protocols_for_persistent_activity.groupby(["protocol_type", "pulse_number"]): protocol_dict = { "type": "{}-{}-success".format(name[0], name[1]), "stim_ids": df[df["was_successful"] == True].index.values, } protocols.append(protocol_dict) protocol_dict = { "type": "{}-{}-fail".format(name[0], name[1]), "stim_ids": df[df["was_successful"] == False].index.values, } protocols.append(protocol_dict) overview_plots.append("{}-{}".format(name[0], name[1])) for name, df in protocols_for_control.groupby(["protocol_type", "pulse_number"]): protocol_dict = { "type": "{}-{}-control".format(name[0], name[1]), "stim_ids": df.index.values, } protocols.append(protocol_dict) for name, df in protocols_filtered.groupby(["protocol_type", "pulse_number"]): protocol_dict = { "type": "{}-{}-reject".format(name[0], name[1]), "stim_ids": df.index.values, } protocols.append(protocol_dict) template_vars = { "total_number_of_cells": len(all_protocols["cell_id"].unique()), "total_number_of_protocols": len(all_protocols), "number_of_analysed_protocols": len(protocols_for_persistent_activity)+len(protocols_for_control), "number_cooperative": len(protocols_for_persistent_activity), "number_independent": len(protocols_for_control), "number_of_filtered_protocols": len(protocols_filtered), "overview_table": overview_protocols.to_html(), "protocols": protocols, "overview_plots": overview_plots } html_out = template.render(template_vars) path_to_report = OUTPUT_FOLDER+"report.pdf" HTML(string=html_out, base_url='.').write_pdf(path_to_report, stylesheets=["scripts/tools/style.css"])