diff --git a/cloud-functions/python/functions/performance/main.py b/cloud-functions/python/functions/performance/main.py index 00ec514..e1cbfd7 100644 --- a/cloud-functions/python/functions/performance/main.py +++ b/cloud-functions/python/functions/performance/main.py @@ -15,6 +15,7 @@ import re import xlsxwriter import math import matplotlib.pyplot as plt +from scipy.stats import linregress initialize_app() @@ -59,27 +60,27 @@ def performance(request): if 'file' not in request.files: print('No file part') + else: + file = request.files['file'] - file = request.files['file'] + if file.filename == '': + print('No selected file') + + # Save the file to a temporary location + temp_filepath = '/tmp/temp_file.csv' + file.save(temp_filepath) - if file.filename == '': - print('No selected file') - - # Save the file to a temporary location - temp_filepath = '/tmp/temp_file.csv' - file.save(temp_filepath) - - # Load the file into a Pandas DataFrame - try: - df_reference_device_raw = pd.read_csv(temp_filepath) # Adjust the read method based on your file type (e.g., read_excel for Excel files) - # Now you can work with the DataFrame (e.g., perform analysis or display it) - print(df_reference_device_raw.head()) - print('File uploaded and loaded into DataFrame successfully') - except Exception as e: - print(f'Error loading the file: {str(e)}') - finally: - # Remove the temporary file - os.remove(temp_filepath) + # Load the file into a Pandas DataFrame + try: + df_reference_device_raw = pd.read_csv(temp_filepath) # Adjust the read method based on your file type (e.g., read_excel for Excel files) + # Now you can work with the DataFrame (e.g., perform analysis or display it) + print(df_reference_device_raw.head()) + print('File uploaded and loaded into DataFrame successfully') + except Exception as e: + print(f'Error loading the file: {str(e)}') + finally: + # Remove the temporary file + os.remove(temp_filepath) path_delim = "/" @@ -283,6 +284,7 @@ def performance(request): wks1.write(0, 0, 'Solution') wks1.write(0, 1, 'Slope') wks1.write(0, 2, 'Intercept') + wks1.write(0, 3, 'R^2') row = 1 fig, ax = plt.subplots() @@ -295,33 +297,38 @@ def performance(request): concentration_means = solution_data.groupby('concentration')['absorbance'].mean() + # Fit a linear regression model coefficients = np.polyfit(concentration_means.index, concentration_means.values, 1) + + # Use linregress to get additional statistics including R-squared + slope, intercept, r_value, p_value, std_err = linregress(concentration_means.index, concentration_means.values) - print(f'Coefficients for {solution}: Slope={coefficients[0]}, Intercept={coefficients[1]}') + print(f'For {solution}: Slope={slope:.4f}, Intercept={intercept:.4f}, R^2={r_value**2:.4f}') ax.plot(concentration_means.index, concentration_means.values, marker='o', linestyle='-', label=f'Mean Absorbance for {solution}') ax.plot(concentration_means.index, np.polyval(coefficients, concentration_means.index), linestyle='--', label=f'Linear Fit for {solution}') - annotation_text = f'Slope: {coefficients[0]:.4f}\nIntercept: {coefficients[1]:.4f}' + annotation_text = f'Slope: {slope:.4f}\nIntercept: {intercept:.4f}\nR^2: {r_value**2:.4f}' ax.text(concentration_means.index[-1] + 5, np.polyval(coefficients, concentration_means.index[-1]), annotation_text, fontsize=10, verticalalignment='center') wks1.write(row, 0, solution) - wks1.write(row, 1, str(coefficients[0])) - wks1.write(row, 2, str(coefficients[1])) + wks1.write(row, 1, str(slope)) + wks1.write(row, 2, str(intercept)) + wks1.write(row, 3, str(r_value**2)) row += 1 - # writer.save() - ax.set_title('Mean Absorbance for Each Solution') ax.set_xlabel('Concentration') ax.set_ylabel('Mean Absorbance') ax.legend() + # plt.show() imgdata=io.BytesIO() fig.savefig(imgdata, format='png') - wks1.insert_image(2,2, '', {'image_data': imgdata}) + wks1.insert_image(6,0, '', {'image_data': imgdata}) + ### debug each row