Update app.py
Browse files
app.py
CHANGED
|
@@ -54,54 +54,66 @@ def compare_methods(data):
|
|
| 54 |
if len(data) < 2:
|
| 55 |
raise ValueError("Número insuficiente de observações para análise estatística.")
|
| 56 |
|
| 57 |
-
# Calculando a média das triplicatas
|
| 58 |
-
data['Method_1_Mean'] = data[['Method_1_1', 'Method_1_2', 'Method_1_3']].mean(axis=1)
|
| 59 |
-
data['Method_2_Mean'] = data[['Method_2_1', 'Method_2_2', 'Method_2_3']].mean(axis=1)
|
| 60 |
-
data['Difference'] = data['Method_1_Mean'] - data['Method_2_Mean']
|
| 61 |
-
|
| 62 |
# Teste t pareado
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
for conc, subset in data.groupby('Concentration')]
|
| 66 |
-
t_test_df = pd.DataFrame(t_results)
|
| 67 |
|
| 68 |
# Teste de Levene
|
| 69 |
-
levene_stat, levene_p = stats.levene(data['
|
| 70 |
levene_df = pd.DataFrame({'Levene Statistic': [levene_stat], 'p-value': [levene_p]})
|
| 71 |
|
| 72 |
# ANOVA
|
| 73 |
-
anova_stat, anova_p = stats.f_oneway(data['
|
| 74 |
anova_df = pd.DataFrame({'F-statistic': [anova_stat], 'p-value': [anova_p]})
|
| 75 |
|
| 76 |
# Regressão Linear
|
| 77 |
-
slope, intercept, r_value, p_value, std_err = stats.linregress(data['
|
| 78 |
regression_results = {'Slope': slope, 'Intercept': intercept, 'R²': r_value**2, 'p-value': p_value, 'Std Error': std_err}
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
# Interpretação automática
|
| 81 |
interpretation_text = interpret_results(t_test_df, levene_df, anova_df, regression_results)
|
| 82 |
|
| 83 |
-
return t_test_df, levene_df, anova_df, regression_results, interpretation_text
|
| 84 |
except Exception as e:
|
| 85 |
return f"Erro durante a análise: {str(e)}"
|
| 86 |
|
| 87 |
# Interface revisada e funcional
|
| 88 |
iface = gr.Interface(
|
| 89 |
fn=compare_methods,
|
| 90 |
-
inputs=gr.Dataframe(
|
| 91 |
-
headers=["Concentration", "Method_1_1", "Method_1_2", "Method_1_3", "Method_2_1", "Method_2_2", "Method_2_3"],
|
| 92 |
-
label="Insira os dados das concentrações e triplicatas para cada método",
|
| 93 |
-
row_count=3,
|
| 94 |
-
col_count=(7, "fixed")
|
| 95 |
-
),
|
| 96 |
outputs=[
|
| 97 |
gr.Dataframe(label="Teste t pareado"),
|
| 98 |
gr.Dataframe(label="Teste de Levene"),
|
| 99 |
gr.Dataframe(label="ANOVA"),
|
| 100 |
gr.JSON(label="Regressão Linear"),
|
|
|
|
|
|
|
| 101 |
gr.Textbox(label="Interpretação dos Resultados")
|
| 102 |
],
|
| 103 |
title="Comparação de Métodos de Medição",
|
| 104 |
-
description="Preencha a tabela com
|
| 105 |
)
|
| 106 |
|
| 107 |
iface.launch(share=True)
|
|
@@ -109,4 +121,3 @@ iface.launch(share=True)
|
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
-
|
|
|
|
| 54 |
if len(data) < 2:
|
| 55 |
raise ValueError("Número insuficiente de observações para análise estatística.")
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Teste t pareado
|
| 58 |
+
t_stat, t_p_value = stats.ttest_rel(data['Method_1'], data['Method_2'])
|
| 59 |
+
t_test_df = pd.DataFrame({'t-statistic': [t_stat], 'p-value': [t_p_value]})
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Teste de Levene
|
| 62 |
+
levene_stat, levene_p = stats.levene(data['Method_1'], data['Method_2'])
|
| 63 |
levene_df = pd.DataFrame({'Levene Statistic': [levene_stat], 'p-value': [levene_p]})
|
| 64 |
|
| 65 |
# ANOVA
|
| 66 |
+
anova_stat, anova_p = stats.f_oneway(data['Method_1'], data['Method_2'])
|
| 67 |
anova_df = pd.DataFrame({'F-statistic': [anova_stat], 'p-value': [anova_p]})
|
| 68 |
|
| 69 |
# Regressão Linear
|
| 70 |
+
slope, intercept, r_value, p_value, std_err = stats.linregress(data['Method_1'], data['Method_2'])
|
| 71 |
regression_results = {'Slope': slope, 'Intercept': intercept, 'R²': r_value**2, 'p-value': p_value, 'Std Error': std_err}
|
| 72 |
|
| 73 |
+
# Criando gráfico de regressão linear
|
| 74 |
+
fig_reg, ax_reg = plt.subplots(figsize=(6, 4))
|
| 75 |
+
sns.regplot(x=data['Method_1'], y=data['Method_2'], ax=ax_reg, scatter_kws={'color': 'blue'}, line_kws={'color': 'red'})
|
| 76 |
+
ax_reg.set_xlabel('Método 1')
|
| 77 |
+
ax_reg.set_ylabel('Método 2')
|
| 78 |
+
ax_reg.set_title('Gráfico de Regressão Linear')
|
| 79 |
+
|
| 80 |
+
# Criando gráfico de Bland-Altman
|
| 81 |
+
means = (data['Method_1'] + data['Method_2']) / 2
|
| 82 |
+
diffs = data['Method_1'] - data['Method_2']
|
| 83 |
+
mean_diff = np.mean(diffs)
|
| 84 |
+
std_diff = np.std(diffs, ddof=1)
|
| 85 |
+
fig_bland, ax_bland = plt.subplots(figsize=(6, 4))
|
| 86 |
+
ax_bland.scatter(means, diffs, color='blue', alpha=0.6)
|
| 87 |
+
ax_bland.axhline(mean_diff, color='red', linestyle='--', label=f'Média das Diferenças ({mean_diff:.2f})')
|
| 88 |
+
ax_bland.axhline(mean_diff + 1.96 * std_diff, color='gray', linestyle='--', label='Limite Superior (95%)')
|
| 89 |
+
ax_bland.axhline(mean_diff - 1.96 * std_diff, color='gray', linestyle='--', label='Limite Inferior (95%)')
|
| 90 |
+
ax_bland.set_xlabel('Média dos Métodos')
|
| 91 |
+
ax_bland.set_ylabel('Diferença Método 1 - Método 2')
|
| 92 |
+
ax_bland.set_title('Gráfico de Bland-Altman')
|
| 93 |
+
ax_bland.legend()
|
| 94 |
+
|
| 95 |
# Interpretação automática
|
| 96 |
interpretation_text = interpret_results(t_test_df, levene_df, anova_df, regression_results)
|
| 97 |
|
| 98 |
+
return t_test_df, levene_df, anova_df, regression_results, fig_reg, fig_bland, interpretation_text
|
| 99 |
except Exception as e:
|
| 100 |
return f"Erro durante a análise: {str(e)}"
|
| 101 |
|
| 102 |
# Interface revisada e funcional
|
| 103 |
iface = gr.Interface(
|
| 104 |
fn=compare_methods,
|
| 105 |
+
inputs=gr.Dataframe(headers=["Concentration", "Method_1", "Method_2"], label="Insira os dados das concentrações e valores dos métodos"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
outputs=[
|
| 107 |
gr.Dataframe(label="Teste t pareado"),
|
| 108 |
gr.Dataframe(label="Teste de Levene"),
|
| 109 |
gr.Dataframe(label="ANOVA"),
|
| 110 |
gr.JSON(label="Regressão Linear"),
|
| 111 |
+
gr.Plot(label="Gráfico de Regressão Linear"),
|
| 112 |
+
gr.Plot(label="Gráfico de Bland-Altman"),
|
| 113 |
gr.Textbox(label="Interpretação dos Resultados")
|
| 114 |
],
|
| 115 |
title="Comparação de Métodos de Medição",
|
| 116 |
+
description="Preencha a tabela com concentrações e os respectivos valores dos métodos. A ferramenta realizará a análise estatística automaticamente e apresentará os resultados com uma interpretação baseada na ISO 5725."
|
| 117 |
)
|
| 118 |
|
| 119 |
iface.launch(share=True)
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
|
|
|