-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphs.py
113 lines (95 loc) · 4.79 KB
/
Graphs.py
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
import customtkinter as ctk
import matplotlib.pyplot as plt
from matplotlib.backend_bases import PickEvent
from matplotlib.figure import Figure
from matplotlib.widgets import Cursor
from os import sep
from customtkinter import filedialog
from tkinter import messagebox
from pandas import read_csv
class Graphs:
def __init__(self, master: ctk.CTkFrame) -> None:
"""Class which defined for FTIR data plotting and to define all UI elements which are releated
to |FTIR data plotting.
:param master: _description_
:type master: ctk.CTkFrame
"""
self.master = master
self.filepaths_str = ctk.StringVar()
# Define all UI elements used in Graps class. They are defined explicitly because with that, UI customization
# become much eassy.
self.filenames_entry = ctk.CTkEntry(self.master, width=400, state="disabled")
self.filesselect_btn = ctk.CTkButton(self.master, text="Select Data file(s)", command=self.__open_file_dialog)
self.display_graphs_btn = ctk.CTkButton(
self.master,
text="Display Graph(s)",
command=self.__display_graphs,
)
self.filenames_entry.grid(row=0, column=0, columnspan=2)
self.filesselect_btn.grid(row=1, column=0)
self.display_graphs_btn.grid(row=1, column=1)
def __open_file_dialog(self) -> None:
"""Open file picker and set the texts in filenames entry box"""
filepaths = filedialog.askopenfilenames(
initialdir=".", title="Select files", filetypes=[("Text files", "*.csv")]
)
self.filepaths_str.set(",".join(filepaths)) # save the path of all selected data files.
# format data file names to display in text box
txtbox_str = ",".join(map(lambda x: x.split(sep)[-1], filepaths))
self.filenames_entry.configure(state=ctk.NORMAL)
self.filenames_entry.delete(0, ctk.END)
self.filenames_entry.insert(ctk.END, txtbox_str)
self.filenames_entry.configure(state=ctk.DISABLED)
def __on_pick(self, event: PickEvent, graphs: dict, fig: Figure) -> None:
"""Fucntion which triggers when mouse button is pressed on a legend of a graph. When mouse button is pressed
on a legend, the graph related to that legend become hidden one. If again click one that same legend then that
graph is visible again. This feature is simply called as "show-hide".
:param event: parameter to store the mouse button press event
:type event: PickEvent
:param graphs: dictionary containing all graphs
:type graphs: dict
:param fig: container for all displaying plot elements
:type fig: Figure
"""
legend = event.artist
isVisible = legend.get_visible()
graphs[legend].set_visible(not isVisible)
legend.set_visible(not isVisible)
fig.canvas.draw()
def __display_graphs(self) -> None:
"""Displaying plots and other features activates after click on self.display_graphs_btn button."""
try:
file_paths = self.filepaths_str.get().split(",")
dataframes = [read_csv(filename, skiprows=1).dropna() for filename in file_paths]
txtbox_str = self.filenames_entry.get()
datafile_names = tuple(map(lambda x: x.split(".")[0], txtbox_str.strip().split(",")))
# Create a plot which displays all selected dataframes
fig, ax = plt.subplots()
fig.set_size_inches(w=10, h=6)
fig.set_tight_layout(True)
lines = []
for dataframe in dataframes:
lines.append(ax.plot(dataframe["cm-1"], dataframe["%T"], "-")[0])
ax.set_xlabel("cm-1")
ax.set_ylabel("%T")
ax.grid()
ax.set_title(f"Transmition vs wave number of {', '.join(datafile_names)}")
# Config plot for show-hide feature
legends = ax.legend(datafile_names, loc="lower right")
for legend in legends.get_lines():
legend.set_picker(True)
legend.set_pickradius(5)
graphs = dict(zip(legends.get_lines(), lines))
cursor = Cursor(ax, color="k", linewidth=1) # noqa: F841
plt.connect("pick_event", lambda event: self.__on_pick(event, graphs, fig)) # connect mouse click event
plt.show()
except FileNotFoundError as fnf:
print(fnf)
messagebox.showwarning("Warning", "Please select at least one valid FTIR data file.")
except KeyError as ke:
print(ke)
plt.close()
messagebox.showwarning("Warning", "Please check whether is the selected file valid FTIR data file.")
except Exception as exc:
print(exc)
messagebox.showerror("Warning", "Something went wrong. Try again.")