-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVNMR_To_MATLABFile
More file actions
123 lines (97 loc) · 4.14 KB
/
CSVNMR_To_MATLABFile
File metadata and controls
123 lines (97 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
import numpy as np
import scipy.io as sio
import os
class NMRDataProcessor:
def __init__(self, root):
self.root = root
self.root.title("MRTools - NMR Data Processor")
self.root.geometry("500x300")
self.file_path = tk.StringVar()
self.save_path = tk.StringVar()
# Interface
self.create_widgets()
def create_widgets(self):
main_frame = tk.Frame(self.root, padx=20, pady=20)
main_frame.pack(expand=True, fill=tk.BOTH)
# Botão para selecionar arquivo
tk.Label(main_frame, text="Select your data files (Excel/CSV):").pack(anchor=tk.W)
file_frame = tk.Frame(main_frame)
file_frame.pack(fill=tk.X, pady=5)
tk.Entry(file_frame, textvariable=self.file_path, width=40).pack(side=tk.LEFT, fill=tk.X, expand=True)
tk.Button(file_frame, text="Search", command=self.select_file).pack(side=tk.LEFT, padx=5)
# Botão para selecionar local de salvamento
tk.Label(main_frame, text="Save your file .mat as:").pack(anchor=tk.W, pady=(15,0))
save_frame = tk.Frame(main_frame)
save_frame.pack(fill=tk.X, pady=5)
tk.Entry(save_frame, textvariable=self.save_path, width=40).pack(side=tk.LEFT, fill=tk.X, expand=True)
tk.Button(save_frame, text="Search", command=self.select_save_location).pack(side=tk.LEFT, padx=5)
tk.Button(main_frame, text="Process and Save", command=self.process_data,
bg='#4CAF50', fg='white').pack(pady=20)
# Status
self.status_label = tk.Label(main_frame, text="", fg='gray')
self.status_label.pack()
def select_file(self):
filetypes = (
('Excel/CSV files', '*.xlsx *.csv'),
('All files', '*.*')
)
filename = filedialog.askopenfilename(
title='Select you data file',
filetypes=filetypes
)
if filename:
self.file_path.set(filename)
base_name = os.path.splitext(os.path.basename(filename))[0] + '_nmrdata.mat'
self.save_path.set(os.path.join(os.path.dirname(filename), base_name))
def select_save_location(self):
filename = filedialog.asksaveasfilename(
title='Save your file .mat as',
defaultextension='.mat',
filetypes=(('MATLAB Data', '*.mat'),)
)
if filename:
self.save_path.set(filename)
def process_data(self):
if not self.file_path.get():
messagebox.showerror("Error", "No file selected!")
return
if not self.save_path.get():
messagebox.showerror("Error", "You must select a save location!")
return
try:
self.status_label.config(text="Processing...", fg='blue')
self.root.update()
file_path = self.file_path.get()
ext = os.path.splitext(file_path)[1].lower()
if ext == '.xlsx':
df = pd.read_excel(file_path, header=0)
headers = df.columns.tolist()
data = df.values
elif ext == '.csv':
# Lê CSV
df = pd.read_csv(file_path, header=0)
headers = df.columns.tolist()
data = df.values
else:
raise ValueError("Wrong file type!")
ppm_mean = data[:, 0].T
sample_data = data[:, 1:].T
sample_names = headers[1:]
nmrdata = {
'data': sample_data,
'ppm_mean': ppm_mean,
'subjects': np.array(sample_names, dtype='object')
}
sio.savemat(self.save_path.get(), {'nmrdata': nmrdata})
self.status_label.config(text="File saved!", fg='green')
messagebox.showinfo("Sucess", "Processed data and saved!")
except Exception as e:
self.status_label.config(text="Process failure", fg='red')
messagebox.showerror("Error", f"Something is wrong!:\n{str(e)}")
if __name__ == "__main__":
root = tk.Tk()
app = NMRDataProcessor(root)
root.mainloop()