SimVascular_report_PostProcess/generatePDF.py
2020-07-19 21:58:01 -05:00

172 lines
6.9 KiB
Python

#!/usr/bin/env python
"""
Created on Fri Jul 17 05:34:59 2020
@author: Aloma Blanch
"""
from fpdf import FPDF
def generatePDF(save_path,project,DBP,MBP,SBP,PP,Q_avg,txt1,txt2,Rc_C_Rd,T_cyc,n_cyc,N_ts,dt):
class PDF(FPDF):
def header(self):
# Arial bold 15
self.set_font('Times', 'B', 15)
# Calculate width of title and position
w = self.get_string_width(title) + 6
self.set_x((210 - w) / 2)
# Colors of frame, background and text
self.set_draw_color(211, 84, 0 )
self.set_fill_color(249, 231, 159)
self.set_text_color(40, 116, 166)
# Thickness of frame (0.2 mm)
self.set_line_width(0.2)
# Title
self.cell(w, 9, title, 1, 1, 'C', 1)
# Line break
self.ln(10)
def footer(self):
# Position at 1.5 cm from bottom
self.set_y(-15)
# Arial italic 8
self.set_font('Times', 'I', 8)
# Text color in gray
self.set_text_color(128)
# Page number
self.cell(0, 10, 'Page ' + str(self.page_no()), 0, 0, 'C')
def chapter_title(self, num, label):
# Arial 12
self.set_font('Times', '', 12)
# Background color
self.set_fill_color(200, 220, 255)
# Title
self.cell(0, 6, 'Chapter %d : %s' % (num, label), 0, 1, 'L', 1)
# Line break
self.ln(4)
def section_title(self, num, label):
# Arial 12
self.set_font('Times', '', 12)
# Background color
self.set_fill_color(232, 248, 245)
# Title
self.cell(0, 6, 'Section %d : %s' % (num, label), 0, 1, 'L', 1)
# Line break
self.ln(4)
def chapter_body(self, name):
# Read text file
with open(name, 'rb') as fh:
txt = fh.read().decode('latin-1')
# Times 12
self.set_font('Times', '', 12)
# Output justified text
self.multi_cell(0, 5, txt)
# Line break
self.ln()
# Mention in italics
self.set_font('', 'I')
self.cell(0, 5, '(end of excerpt)')
def print_chapter(self, num, title, name):
self.add_page()
self.chapter_title(num, title)
self.chapter_body(name)
def print_section(self, num, title):
self.add_page()
self.section_title(num, title)
title = 'Simulation Job name: '+ project
pdf = PDF()
pdf.set_title(title)
pdf.set_author('Aloma Blanch Granada')
pdf.print_section(1, 'Checking convergency and periodicity')
pdf.image(save_path +'/Log_Last_nonlin_res_error.jpg', x = 40, y = None, w = 140, h = 100, type = '', link = '')
pdf.image(save_path +'/periodicity.jpg', x = 40, y = None, w = 140, h = 100, type = '', link = '')
pdf.set_font('Arial', '', 10)
pdf.cell(0, 10,' ', 0, 1)
pdf.cell(0, 5,txt1[0], 0, 1)
pdf.cell(0, 5,txt1[1], 0, 1)
pdf.cell(0, 5,txt1[2], 0, 1)
pdf.cell(0, 5,txt1[3], 0, 1)
pdf.cell(0, 5,txt1[4], 0, 1)
pdf.print_section(2, 'Checking Pressures at each outlet')
pdf.image(save_path +'/pressure.jpg', x = 40, y = None, w = 140, h = 100, type = '', link = '')
width_cell=[20,30,30,30,30,30,30];
# pfd.SetFillColor(193,229,252); # Background color of header
pdf.set_font('Times', '', 10)
# Header starts
pdf.cell(0, 25,' ', 0, 1)
pdf.set_x(35)
pdf.cell(width_cell[0],10,'ROI',1,0,'C') # First header column
pdf.cell(width_cell[1],10,'DBP [mmHg]',1,0,'C') # Second header column
pdf.cell(width_cell[2],10,'MBP [mmHg]',1,0,'C') # Third header column
pdf.cell(width_cell[3],10,'SBP [mmHg]',1,0,'C') # Fourth header column
pdf.cell(width_cell[4],10,'PP [mmHg]',1,1,'C') # Fourth header column
# Rows
for i in range(0,len(SBP)):
pdf.set_x(35)
pdf.cell(width_cell[0],10,'ROI-'+ str(i+2),1,0,'C') # First column of row 1
pdf.cell(width_cell[1],10,str(round(DBP[i],2)),1,0,'C') # Second column of row 1
pdf.cell(width_cell[2],10,str(round(MBP[i],2)),1,0,'C') # Third column of row 1
pdf.cell(width_cell[3],10,str(round(SBP[i],2)),1,0,'C') # Fourth column of row 1
pdf.cell(width_cell[4],10,str(round(PP[i],2)),1,1,'C') # Fourth column of row 1
pdf.print_section(3, 'Checking Flow Rate at each outlet')
pdf.image(save_path +'/flow.jpg', x = 40, y = None, w = 140, h = 100, type = '', link = '')
width_cell=[20,45];
# pfd.SetFillColor(193,229,252); # Background color of header
pdf.set_font('Times', '', 10)
# Header starts
pdf.cell(0, 25,' ', 0, 1)
pdf.set_x(80)
pdf.cell(width_cell[0],10,'ROI',1,0,'C') # First header column
pdf.cell(width_cell[1],10,'Average Flow rate [mL/s]',1,1,'C') # Second header column
# Rows
for i in range(0,len(Q_avg)):
pdf.set_x(80)
pdf.cell(width_cell[0],10,'ROI-'+ str(i+2),1,0,'C')
pdf.cell(width_cell[1],10,str(round(Q_avg[i],3)),1,1,'C')
pdf.print_section(4, 'Checking Inlet Flow Waveform and time steps saved')
pdf.image(save_path +'/inlet_waveform.jpg', x = 40, y = 50, w = 140, h = 100, type = '', link = '')
pdf.set_font('Arial', '', 10)
pdf.cell(0, 5,txt2, 0, 1)
width_cell=[30,20,110];
pdf.print_section(5, 'Outlet Boundary Condition Applied')
pdf.set_font('Arial', '', 10)
pdf.cell(0, 10,'3-Element Windkessel model outlet boundary condition applied in SimVascular', 0, 1)
pdf.cell(0, 5,' ', 0, 1)
pdf.set_x(50)
pdf.set_font('Times', '', 10)
pdf.cell(width_cell[2],10,'3-Element Windkessel',1,1,'C') # First header column
pdf.set_x(50)
pdf.cell(width_cell[1],10,'ROI',1,0,'C') # Second header column
pdf.cell(width_cell[0],10,'Rc [g/cm^4 s]',1,0,'C') # Second header column
pdf.cell(width_cell[0],10,'C [cm^4 s^2/g]',1,0,'C') # Second header column
pdf.cell(width_cell[0],10,'Rd [g/cm^4 s]',1,1,'C') # Second header
# Rows
for i in range(0,Rc_C_Rd.shape[0]):
pdf.set_x(50)
pdf.cell(width_cell[1],10,'ROI-'+ str(i+2),1,0,'C') # First column of row 1
pdf.cell(width_cell[0],10,str(Rc_C_Rd[i][0]),1,0,'C') # Second column of row 1
pdf.cell(width_cell[0],10,str(Rc_C_Rd[i][1]),1,0,'C') # Third column of row 1
pdf.cell(width_cell[0],10,str(Rc_C_Rd[i][2]),1,1,'C') # Third column of row 1
pdf.print_section(6, 'Solver Parameters')
pdf.set_font('Arial', '', 10)
pdf.cell(0, 10,'Cardiac cycle period - ' + str(T_cyc) + 's', 0, 1)
pdf.cell(0, 10,'Number of cycles - ' + str(n_cyc), 0, 1)
pdf.cell(0, 10,'Number of time steps - ' + str(N_ts), 0, 1)
pdf.cell(0, 10,'Time step size - ' + str(dt) + 's', 0, 1)
pdf.output(save_path + '/' + project + '-report.pdf', 'F')