SimVascular_report_PostProcess/report_main.py
2020-10-20 09:00:19 -05:00

100 lines
3.5 KiB
Python

#!/usr/bin/env python
"""
Created on Thu Jul 16 15:08:27 2020
@author: Aloma Blanch
"""
# 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
from tkinter.filedialog import 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, rcr, cycle, barPlot
from generatePDF import generatePDF
# 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'
# 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])
# Extracting Outlet Boundary Conditions from - rcrt.dat fle
Rc_C_Rd = rcr(project_folder)
# Extracting number of cycles and period of cardiac cycle
(T_cyc,n_cyc) = cycle(folder,dt,N_ts,save_path)
T_cyc=0.4769
n_cyc=4
# 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)
# Extract bar plots CFD vs. aimed
barPlot(project_folder,DBP,MBP,SBP,PP,Q_avg,save_path)
# # Flow comparison specific for patient 120.
# # Only ROI-5,6,8 because that is the PC-MRA data that we have.
# def flow_comparison(folder,N_ts,T_cyc,dt,n_cyc,save_path):
# flow_sim = np.loadtxt(folder+'/QHistRCR.dat',skiprows=2,)
# flow_doc = np.loadtxt(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(folder)))))+'/Data/flow_waveforms.txt',skiprows=2,)
# Nc = round(T_cyc/dt)
# time_sim = np.linspace(0,T_cyc,Nc)
# time_doc = np.linspace(0,T_cyc,flow_doc.shape[0])
# Q = np.empty(flow_sim.shape[1])
# for i in range(0,flow_sim.shape[1]):
# fig, ax = plt.subplots()
# ax.plot(time_sim,flow_sim[N_ts-Nc:N_ts,i],label='sim ROI-'+str(i+2))
# ax.plot(time_doc,flow_doc[:,1+i],label='doc ROI-'+str(i+2))
# ax.set(xlabel='time [s]', ylabel='Flow [mL/s]',
# title='Flow at each outlet')
# ax.spines['right'].set_visible(False)
# ax.spines['top'].set_visible(False)
# ax.legend(loc=0)
# # plt.show()
# plt.savefig(save_path + '/flow_comparison.pdf')
# fig.savefig(save_path + '/flow_comparison.jpg',dpi=150)
# Create PDF report
generatePDF(save_path,project,DBP,MBP,SBP,PP,Q_avg,txt1,txt2,Rc_C_Rd,T_cyc,n_cyc,N_ts,dt)