<jupyter_start><jupyter_text>Importando bibliotecas<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.optimize import curve_fit   # para ajuste das curvas dos modelos
# inclui funções para fitar curvas:
import sys
sys.path.insert(0,'/dados/github/projetos/recrutabilidade')
import modelos as m
%load_ext autoreload
%autoreload 2
pasta_figuras_texto = '/mnt/hdsda3/arquivos/Recrutamento/figuras/texto/'<jupyter_output><empty_output><jupyter_text>Lendo dados<jupyter_code>dados_ct_df = pd.read_pickle('./dados_ct.pkl')
dados_ct_df<jupyter_output><empty_output><jupyter_text>Gerando gráficos  Comparando volume de ar (CT x Luva)<jupyter_code>plt.subplots(figsize=(30, 10))
for idx in range(len(dados_ct_df)):
    p = dados_ct_df.iloc[idx]['Pressure']
    v = dados_ct_df.iloc[idx]['CT Volume']
    v_luva = dados_ct_df.iloc[idx]['Volume (Luva)']
    plt.subplot(2,4,idx+1)
    plt.scatter(p, v_luva,label='Luva')
    plt.scatter(p, v,label='CT')
    #plt.scatter([0], [0],label='(0,0)')
    plt.legend()
    plt.xlabel('Pressure [$cmH_2O$]')
    plt.ylabel('Volume [$mL$]')
    plt.title(dados_ct_df.iloc[idx]['Animal'])
    plt.suptitle('Air volume estimated from volumetric CTs (B1, B3, C4, D5, Rec3)', fontsize=16, y=0.95)
    plt.subplots_adjust(hspace=0.3, wspace=0.2) # plt.subplots_adjust(hspace=0.4, wspace=0.4)<jupyter_output><empty_output><jupyter_text>CT vol x press<jupyter_code>plt.subplots(figsize=(30, 10))
for idx in range(len(dados_ct_df)):
    p = dados_ct_df.iloc[idx]['Pressure']
    v = dados_ct_df.iloc[idx]['CT Volume']
    plt.subplot(2,4,idx+1)
    plt.scatter(p, v,label='CT')
    #plt.scatter([0], [0],label='(0,0)')
    #plt.legend()
    #plt.ylim((-50,max(v)*1.1))
    plt.xlabel('Pressure [$cmH_2O$]')
    plt.ylabel('Volume [$mL$]')
    plt.title(dados_ct_df.iloc[idx]['Animal'])
    plt.suptitle('Air volume estimated from volumetric CTs (B1, B3, C4, D5, Rec3)', fontsize=16, y=0.95)
    #plt.tight_layout()
    plt.subplots_adjust(hspace=0.3, wspace=0.2) # plt.subplots_adjust(hspace=0.4, wspace=0.4)
plt.savefig(pasta_figuras_texto+'volumes_cts_passos.pdf',bbox_inches='tight')
plt.subplots(figsize=(30, 10))
for idx in range(len(dados_ct_df)):
    fator_K = 1.0
    p = dados_ct_df.iloc[idx]['Pressure']
    v = dados_ct_df.iloc[idx]['CT Volume']
    
    p_fit = range(0,100)
    v_fit = m.expsalazar(p_fit, dados_ct_df.iloc[idx]['Estimated TLC'],dados_ct_df.iloc[idx]['Estimated K']*fator_K)
    
    
    plt.subplot(2,4,idx+1)
    plt.scatter(p, v,label='CT')
    plt.plot(p_fit, v_fit, 'k:', label='Fit')
    
    K = dados_ct_df.iloc[idx]['Estimated K']*fator_K
    for ponto in range(len(p)):
        pressoes = [0, p[ponto]]
        volumes = [0, v[ponto]]
        popt,pcov = curve_fit(lambda x, volumes: m.expsalazar(x, volumes, K), pressoes, volumes) # fit fixando K
        v_fit = m.expsalazar(p_fit, popt[0] ,K)
        plt.plot(p_fit, v_fit, 'k:')
    
    plt.legend()
    plt.xlabel('Pressure [$cmH_2O$]')
    plt.ylabel('Volume [$mL$]')
    plt.title(dados_ct_df.iloc[idx]['Animal'])
    plt.suptitle('Air volume estimated from volumetric CTs and fitted exponencial curves (B1, B3, C4, D5, Rec3)', fontsize=16, y=0.95)
    plt.subplots_adjust(hspace=0.3, wspace=0.2) # plt.subplots_adjust(hspace=0.4, wspace=0.4)
plt.savefig(pasta_figuras_texto+'volumes_cts_passos_fit.pdf',bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>Estimando TLC<jupyter_code># TLC já estimada anteriormente... apenas plotando o resultado...
plt.subplots(figsize=(30, 10))
for idx in range(len(dados_ct_df)):
    p = dados_ct_df.iloc[idx]['Pressure (rec)']
    v = dados_ct_df.iloc[idx]['CT_Volume (rec)']
    
    p_fit = range(0,100)
    v_fit = m.expsalazar(p_fit, dados_ct_df.iloc[idx]['Estimated TLC'],dados_ct_df.iloc[idx]['Estimated K'])
    
    
    plt.subplot(2,4,idx+1)
    plt.scatter(p, v,label='CT air volume')
    plt.plot(p_fit, v_fit, 'k:', label='Fit')
    plt.legend()
    plt.xlabel('Pressure [$cmH_2O$]')
    plt.ylabel('Volume [$mL$]')
    unid = r'$cmH_2O^{-1}$'
    plt.title(f'{dados_ct_df.iloc[idx]["Animal"]}: K = {dados_ct_df.iloc[idx]["Estimated K"]:.4f} {unid}; TLC = {dados_ct_df.iloc[idx]["Estimated TLC"]:.1f} $mL$')
    plt.suptitle('TLC estimated from volumetric CTs after recruitment', fontsize=16, y=0.95)
    plt.subplots_adjust(hspace=0.3, wspace=0.2) # plt.subplots_adjust(hspace=0.4, wspace=0.4)
plt.savefig(pasta_figuras_texto+'tlc_estimado_cts.pdf',bbox_inches='tight')
dados_ct_df.head(2)
def recruit_percentage(ser):
    TLC = ser['Estimated TLC']
    K = ser['Estimated K']
    
    p = ser['Pressure']
    v = ser['CT Volume']
    
    v_recruited = []
    
    for ponto in range(len(p)):
        pressoes = [0, p[ponto]]
        volumes = [0, v[ponto]]
        popt,pcov = curve_fit(lambda x, volumes: m.expsalazar(x, volumes, K), pressoes, volumes) # fit fixando K
        v_recruited.append(popt[0])
        
    rec_percentage = np.array(v_recruited)/TLC
    rec_potential = 1-rec_percentage
        
    return rec_percentage, rec_potential
        
dados_ct_df[['Recruited percentage', 'Recruitment potential']] = dados_ct_df.apply(lambda ser: recruit_percentage(ser), axis=1, result_type='expand')
dados_ct_df.head(2)
plt.subplots(figsize=(30, 10))
for idx in range(len(dados_ct_df)):
    p = dados_ct_df.iloc[idx]['Pressure']
    r = dados_ct_df.iloc[idx]['Recruited percentage']
    
    
    plt.subplot(2,4,idx+1)
    plt.scatter(p, r*100,label='Recruited percentage')
    #plt.legend()
    plt.xlabel('Pressure [$cmH_2O$]')
    plt.ylabel('Recruited percentage [$\%$]')
    plt.title(f'{dados_ct_df.iloc[idx]["Animal"]}; TLC = {dados_ct_df.iloc[idx]["Estimated TLC"]:.1f} $mL$')
    plt.suptitle('Estimated recruited percentage [$\%$]', fontsize=16, y=0.95)
    plt.subplots_adjust(hspace=0.3, wspace=0.2) # plt.subplots_adjust(hspace=0.4, wspace=0.4)
plt.savefig(pasta_figuras_texto+'cts_recruit_percent.pdf',bbox_inches='tight')
plt.subplots(figsize=(30, 10))
for idx in range(len(dados_ct_df)):
    p = dados_ct_df.iloc[idx]['Pressure']
    v = dados_ct_df.iloc[idx]['CT Volume']
    TLC = dados_ct_df.iloc[idx]["Estimated TLC"]
    
    vperc = np.array(v)/TLC
    
    
    plt.subplot(2,4,idx+1)
    plt.scatter(p, vperc*100,label='air volume percentage')
    #plt.legend()
    plt.xlabel('Pressure [$cmH_2O$]')
    plt.ylabel('Percentage of TLC [$\%$]')
    plt.title(f'{dados_ct_df.iloc[idx]["Animal"]}; TLC = {TLC:.1f} $mL$')
    plt.suptitle('Air Volume estimated from CT as a percentage of TLC [$\%$] (B1, B3, C4, D5, Rec3)', fontsize=16, y=0.95)
    plt.subplots_adjust(hspace=0.3, wspace=0.2) # plt.subplots_adjust(hspace=0.4, wspace=0.4)
plt.savefig(pasta_figuras_texto+'volumes_cts_percentuais_tlc.pdf',bbox_inches='tight')
plt.subplots(figsize=(30, 10))
for idx in range(len(dados_ct_df)):
    p = dados_ct_df.iloc[idx]['Pressure']
    r = dados_ct_df.iloc[idx]['Recruitment potential']
    
    
    plt.subplot(2,4,idx+1)
    plt.scatter(p, r*100,label='Recruitment potential')
    #plt.legend()
    plt.xlabel('Pressure [$cmH_2O$]')
    plt.ylabel('Recruitment potential [$\%$]')
    plt.title(f'{dados_ct_df.iloc[idx]["Animal"]}; TLC = {dados_ct_df.iloc[idx]["Estimated TLC"]:.1f} $mL$')
    plt.suptitle('Estimated recruitment potential [$\%$]', fontsize=16, y=0.95)
    plt.subplots_adjust(hspace=0.3, wspace=0.2) # plt.subplots_adjust(hspace=0.4, wspace=0.4)
plt.savefig(pasta_figuras_texto+'cts_recruit_potential.pdf',bbox_inches='tight')
# gravando resultados
dados_ct_df.to_pickle('./dados_ct_completo.pkl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17389589905362776, 'fraction_numerical': 0.028654048370136697, 'mean_word_length': 4.703898050974512, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '214192', 'license': 'MIT', 'n_tokens_mistral': 3401, 'n_tokens_neox': 3250, 'n_words': 474, 'path': 'volume_referencia/gera_figuras_relatorio.ipynb', 'repo_name': 'edlbcamargo/recrutabilidade', 'size': '7608'}