generate_report.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import numpy
  2. import pandas
  3. from jinja2 import Environment, FileSystemLoader
  4. from weasyprint import HTML
  5. from tools.helper import get_filters
  6. from tools.definitions import OUTPUT_FOLDER, HELPER_TABLE_FOLDER, STIMULATION_METADATA_FILTERED
  7. def yes(bool_array):
  8. if numpy.isnan(bool_array.values[0]):
  9. return numpy.NaN
  10. else:
  11. return numpy.sum(bool_array.values.astype(numpy.int))
  12. def no(bool_array):
  13. no_yes = yes(bool_array)
  14. if numpy.isnan(no_yes):
  15. return numpy.NaN
  16. else:
  17. return len(bool_array) - no_yes
  18. # Prepare template
  19. env = Environment(loader=FileSystemLoader('.'))
  20. REPORT_HTML_TEMPLATE = "scripts/tools/report.html"
  21. template = env.get_template(REPORT_HTML_TEMPLATE)
  22. path_to_filtered_stimulus_file = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+STIMULATION_METADATA_FILTERED
  23. all_protocols = pandas.read_csv(path_to_filtered_stimulus_file, index_col="stimulation_id")
  24. protocols_for_persistent_activity = all_protocols[get_filters(all_protocols) & (all_protocols["is_control"] == False)]
  25. protocols_for_control = all_protocols[get_filters(all_protocols) & (all_protocols["is_control"] == True)]
  26. protocols_filtered = all_protocols[get_filters(all_protocols)==False]
  27. overview_protocols = pandas.DataFrame(protocols_for_persistent_activity.groupby(["protocol_type", "pulse_number"]).agg({
  28. "was_successful": [yes, no]}))
  29. protocols = []
  30. overview_plots=[]
  31. for name, df in protocols_for_persistent_activity.groupby(["protocol_type", "pulse_number"]):
  32. protocol_dict = {
  33. "type": "{}-{}-success".format(name[0], name[1]),
  34. "stim_ids": df[df["was_successful"] == True].index.values,
  35. }
  36. protocols.append(protocol_dict)
  37. protocol_dict = {
  38. "type": "{}-{}-fail".format(name[0], name[1]),
  39. "stim_ids": df[df["was_successful"] == False].index.values,
  40. }
  41. protocols.append(protocol_dict)
  42. overview_plots.append("{}-{}".format(name[0], name[1]))
  43. for name, df in protocols_for_control.groupby(["protocol_type", "pulse_number"]):
  44. protocol_dict = {
  45. "type": "{}-{}-control".format(name[0], name[1]),
  46. "stim_ids": df.index.values,
  47. }
  48. protocols.append(protocol_dict)
  49. for name, df in protocols_filtered.groupby(["protocol_type", "pulse_number"]):
  50. protocol_dict = {
  51. "type": "{}-{}-reject".format(name[0], name[1]),
  52. "stim_ids": df.index.values,
  53. }
  54. protocols.append(protocol_dict)
  55. template_vars = {
  56. "total_number_of_cells": len(all_protocols["cell_id"].unique()),
  57. "total_number_of_protocols": len(all_protocols),
  58. "number_of_analysed_protocols": len(protocols_for_persistent_activity)+len(protocols_for_control),
  59. "number_cooperative": len(protocols_for_persistent_activity),
  60. "number_independent": len(protocols_for_control),
  61. "number_of_filtered_protocols": len(protocols_filtered),
  62. "overview_table": overview_protocols.to_html(),
  63. "protocols": protocols,
  64. "overview_plots": overview_plots
  65. }
  66. html_out = template.render(template_vars)
  67. path_to_report = OUTPUT_FOLDER+"report.pdf"
  68. HTML(string=html_out, base_url='.').write_pdf(path_to_report, stylesheets=["scripts/tools/style.css"])