SimVascular_report_PostProcess/main.py
2020-07-17 01:16:04 -05:00

185 lines
6.3 KiB
Python

#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfile, askdirectory
import pandas as pd
import tkinter as tk
import os.path
from scipy.signal import find_peaks_cwt
from scipy import signal
import statistics
from fpdf import FPDF
from functions import error_plot, periodicity, pressure, flow, inlet_flow_waveform
# Selct dir
Tk().withdraw()
folder = askdirectory()
project_folder = os.path.dirname(folder)
project = os.path.basename(project_folder)
save_path = folder+'/'+project+'-report'
os.mkdir(save_path)
save_pdf = save_path + '/' + project + '-report.pdf'
# Input parameters
T_cyc = 0.477
n_cyc = 5
# Read important parameters from - solver.inp file
mylines = [] # Declare an empty list named mylines.
with open (project_folder + '/solver.inp', 'rt') as myfile: # Open lorem.txt for reading text data.
for myline in myfile: # For each line, stored as myline,
mylines.append(myline) # add its contents to mylines.
# Number of Timesteps - idx 3
# Idea: remove the text to extract the number, the text part will be the same no matter the simulation
N_ts = int(mylines[3][20:-1])
# Time Step Size - idx 4
dt = float(mylines[4][16:-1])
# Residual criteria - idx 4
rc = float(mylines[26][18:-1])
# Imesteps between Restarts - idx 6
t_btw_rst = int(mylines[6][37:-1])
# Cehcking convergency and periodicity
error_plot(folder,dt,rc,save_path)
txt1 = periodicity(project,folder,dt,T_cyc,n_cyc,save_path)
# Pressure - Outlets
(DBP,MBP,SBP,PP) = pressure(folder,N_ts,T_cyc,dt,n_cyc,save_path)
# Flow Rate - Outlets
(Q_avg) = flow(folder,N_ts,T_cyc,dt,n_cyc,save_path)
# Inlet Flow Rate + and t saved
txt2 = inlet_flow_waveform(project_folder,t_btw_rst,N_ts,dt,T_cyc,n_cyc,save_path)
# # Create PDF report
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 = 'Project name: '+ project
pdf = PDF()
pdf.set_title(title)
pdf.set_author('Aloma Blanch Granada')
pdf.print_section(1, 'Cehcking convergency and periodicity')
pdf.image(save_path +'/Log_Last_nonlin_res_error.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
pdf.image(save_path +'/periodicity.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
pdf.cell(0, 10,txt1[0], 0, 1)
pdf.cell(0, 10,txt1[1], 0, 1)
pdf.cell(0, 10,txt1[2], 0, 1)
pdf.print_section(2, 'Cehcking Pressures at each outlet')
pdf.image(save_path +'/pressure.jpg', x = None, 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
# Header starts
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.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, 'Cehcking Flow Rate at each outlet')
pdf.image(save_path +'/flow.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
width_cell=[20,60];
# pfd.SetFillColor(193,229,252); # Background color of header
# Header starts
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.cell(width_cell[0],10,'ROI-'+ str(i+2),1,0,'C')
pdf.cell(width_cell[1],10,str(round(Q_avg[i],2)),1,1,'C')
pdf.print_section(4, 'Cehcking Inlet Flow Waveform and time steps saved')
pdf.image(save_path +'/inlet_waveform.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
pdf.cell(0, 10,txt2, 0, 1)
pdf.output(save_pdf, 'F')