Files
hpos-web/scripts/single_csv.py

125 lines
4.7 KiB
Python
Raw Normal View History

import glob
import pandas as pd
import openpyxl
import os
from tqdm import tqdm
import platform
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog
def consolidate_and_perform_calculations(curdir, rootdir, path_delim, validation_file):
print(os.path.split(curdir)[1])
# list all csv files only
csv_files = glob.glob(curdir + path_delim + '/*.{}'.format('csv'))
if len(csv_files) == 0:
print("no csv files in the sub folder")
# print(csv_files)
df_csv_append = pd.DataFrame()
first = True
# merge the CSV files
for file in csv_files:
if first:
df_csv_append = pd.read_csv(file)
colname = file.split('.')[0].split(path_delim)[-1] #file.split('.')[0]
df_csv_append.rename(columns={'ca': colname}, inplace = True)
df_csv_append = df_csv_append.drop(['1'], axis=1)
first = False
else:
df = pd.read_csv(file)
colname = file.split('.')[0].split(path_delim)[-1] #file.split('.')[0]
df.rename(columns={'ca': colname}, inplace = True)
df = df.drop(['1'], axis=1)
df_csv_append = df_csv_append.merge(df, on='tv')
df_csv_append = df_csv_append[df_csv_append['tv'].between(300, 700)]
wavelength_col = "123_tv" # to make sorting columns simpler
df_csv_append.rename(columns={'tv': wavelength_col}, inplace = True)
df_csv_append = df_csv_append.reindex(sorted(df_csv_append.columns), axis=1)
outfile = rootdir + path_delim + os.path.split(curdir)[1] + "_analysis.xlsx"
# df_csv_append.to_excel(outfile, sheet_name="merged_data", index=False)
# df_csv_append.to_csv("D8 Merged.csv", index=False)
# calculations
df_427 = df_csv_append.loc[(df_csv_append[wavelength_col] >= 427) & (df_csv_append[wavelength_col] < 428)]
df_555 = df_csv_append.loc[(df_csv_append[wavelength_col] >= 555) & (df_csv_append[wavelength_col] < 556)]
df_validation = pd.read_excel(validation_file)
df_analysis = df_427.iloc[0] + df_555.iloc[0]
# print(df_analysis)
# print(min(df_csv_append[0:5]))
midpoint1 = 427
midpoint2 = 555
bandwidth1 = 25
bandwidth2 = 10
df = df_analysis.rename(columns = {"NM":"Wavelength","CA":"Absorbance"}, inplace = True)
# 427 nm range
df1 = df[ (df['Wavelength'] > (midpoint1-bandwidth1)) & (df['Wavelength'] < (midpoint1+bandwidth1)) ]
# 555 nm range
df2 = df[ (df['Wavelength'] > (midpoint2-bandwidth2)) & (df['Wavelength'] < (midpoint2+bandwidth2)) ]
procData.append({"Sample ID": sampleID,
"max_427": round(df1["Absorbance"].max() , 3),
"wvmax_427": round(df1.at[df1["Absorbance"].idxmax(),"Wavelength"], 3),
"avg_427": round(df1["Absorbance"].mean(), 3),
"max_555": round(df2["Absorbance"].max(), 3),
"wvmax_555": round(df2.at[df2["Absorbance"].idxmax(),"Wavelength"], 3),
"avg_555": round(df2["Absorbance"].mean(), 3),
"ratio_max": round(df2["Absorbance"].max()/df1["Absorbance"].max(), 3),
"ratio_avg": round(df2["Absorbance"].mean()/df1["Absorbance"].mean(), 3)
})
writer = pd.ExcelWriter(outfile, engine = 'openpyxl')
df_analysis.to_excel(writer, sheet_name = 'analysis', index=False)
df_csv_append.to_excel(writer, sheet_name = 'merged_data', index=False)
writer.close()
sampleID = 1
allDF = pd.DataFrame()
procData = []
if __name__ == "__main__":
file = os.getcwd() + "/D26-100umol-1.csv"
df = pd.read_csv(file)
midpoint1 = 427
midpoint2 = 555
bandwidth1 = 25
bandwidth2 = 10
df.rename(columns = {"tv":"Wavelength","ca":"Absorbance"}, inplace = True)
# 427 nm range
df1 = df[ (df['Wavelength'] > (midpoint1-bandwidth1)) & (df['Wavelength'] < (midpoint1+bandwidth1)) ]
# 555 nm range
df2 = df[ (df['Wavelength'] > (midpoint2-bandwidth2)) & (df['Wavelength'] < (midpoint2+bandwidth2)) ]
procData.append({"Sample ID": sampleID,
"max_427": round(df1["Absorbance"].max() , 3),
"wvmax_427": round(df1.at[df1["Absorbance"].idxmax(),"Wavelength"], 3),
"avg_427": round(df1["Absorbance"].mean(), 3),
"max_555": round(df2["Absorbance"].max(), 3),
"wvmax_555": round(df2.at[df2["Absorbance"].idxmax(),"Wavelength"], 3),
"avg_555": round(df2["Absorbance"].mean(), 3),
"ratio_max": round(df2["Absorbance"].max()/df1["Absorbance"].max(), 3),
"ratio_avg": round(df2["Absorbance"].mean()/df1["Absorbance"].mean(), 3)
})
print(procData)