Files
hpos-web/scripts/merge_csv.py

121 lines
4.3 KiB
Python
Raw Normal View History

2023-07-14 16:56:28 +05:30
import glob
import pandas as pd
import openpyxl
2023-07-14 17:01:37 +05:30
import os
2023-07-27 15:05:57 +05:30
from tqdm import tqdm
import platform
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog
2023-07-14 17:01:37 +05:30
2023-07-27 15:05:57 +05:30
def consolidate_and_perform_calculations(curdir, rootdir, path_delim, validation_file):
2023-07-14 17:01:37 +05:30
2023-07-17 17:17:01 +05:30
print(os.path.split(curdir)[1])
2023-07-14 17:01:37 +05:30
# list all csv files only
2023-07-27 15:05:57 +05:30
csv_files = glob.glob(curdir + path_delim + '/*.{}'.format('csv'))
if len(csv_files) == 0:
print("no csv files in the sub folder")
2023-07-14 17:01:37 +05:30
# 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)
2023-07-27 15:05:57 +05:30
colname = file.split('.')[0].split(path_delim)[-1] #file.split('.')[0]
2023-07-14 17:01:37 +05:30
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)
2023-07-27 15:05:57 +05:30
colname = file.split('.')[0].split(path_delim)[-1] #file.split('.')[0]
2023-07-14 17:01:37 +05:30
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)]
2023-07-27 15:05:57 +05:30
wavelength_col = "123_tv" # to make sorting columns simpler
df_csv_append.rename(columns={'tv': wavelength_col}, inplace = True)
2023-07-14 17:01:37 +05:30
df_csv_append = df_csv_append.reindex(sorted(df_csv_append.columns), axis=1)
2023-07-27 15:05:57 +05:30
outfile = rootdir + path_delim + os.path.split(curdir)[1] + "_analysis.xlsx"
2023-07-14 17:01:37 +05:30
2023-07-25 20:13:50 +05:30
# df_csv_append.to_excel(outfile, sheet_name="merged_data", index=False)
2023-07-14 17:01:37 +05:30
# df_csv_append.to_csv("D8 Merged.csv", index=False)
2023-07-27 15:05:57 +05:30
# 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)
2023-07-25 20:13:50 +05:30
writer.close()
2023-07-14 17:01:37 +05:30
2023-07-27 15:05:57 +05:30
sampleID = 1
allDF = pd.DataFrame()
procData = []
if __name__ == "__main__":
2023-07-27 15:05:57 +05:30
environment = "dev" # dev, QC
rootdir = os.getcwd()
validation_file = rootdir + '/validation.xlsx'
if environment != "dev":
app = QApplication([])
# window = QWidget()
# layout = QVBoxLayout(window)
rootdir = QFileDialog.getExistingDirectory(None, 'Select main folder')
choose_validation_file = QFileDialog.getOpenFileName(None, "Select validation excel")
validation_file = choose_validation_file[0]
path_delim = ''
if platform.system() == 'Darwin':
path_delim = '/'
else:
path_delim = '\\'
for file in tqdm(os.listdir(rootdir)):
curdir = os.path.join(rootdir, file)
if os.path.isdir(curdir):
# os.chdir(d)
consolidate_and_perform_calculations(curdir, rootdir, path_delim, validation_file)