Skip to content

Commit c2e0bf2

Browse files
committed
Now the csvplotter can detect any number of channels(between 1 to 6)
1 parent d8aa273 commit c2e0bf2

File tree

1 file changed

+15
-36
lines changed

1 file changed

+15
-36
lines changed

csvplotter.py

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,50 +45,29 @@ def load_csv(self):
4545
self.filename = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
4646
if self.filename:
4747
try:
48-
# Read file with pandas, skipping metadata if any
49-
self.data = self.load_csv_data(self.filename)
48+
# Read file with pandas
49+
self.data = pd.read_csv(self.filename)
5050

51-
if self.data is not None:
52-
self.setup_dropdown_menu()
53-
self.file_label.config(text=f"File: {self.filename.split('/')[-1]}")
54-
else:
55-
messagebox.showerror("Error", "CSV does not contain the expected columns.")
51+
# Ensure 'Counter' column is present
52+
if 'Counter' not in self.data.columns:
53+
messagebox.showerror("Error", "CSV file must contain a 'Counter' column.")
54+
return
55+
56+
# Setup dropdown based on available channels
57+
self.setup_dropdown_menu()
58+
self.file_label.config(text=f"File: {self.filename.split('/')[-1]}")
5659

5760
except Exception as e:
5861
messagebox.showerror("Error", f"Could not load CSV file: {e}")
5962

60-
def load_csv_data(self, file_path):
61-
# Open the CSV file and find the line with headers
62-
with open(file_path, 'r') as file:
63-
lines = file.readlines()
64-
65-
# Look for the line containing the headers
66-
headers = ['Counter', 'Channel1', 'Channel2', 'Channel3', 'Channel4', 'Channel5', 'Channel6']
67-
header_line_index = None
68-
for i, line in enumerate(lines):
69-
if all(header in line for header in headers):
70-
header_line_index = i
71-
break
72-
73-
if header_line_index is None:
74-
return None # No header found, return None to indicate error
75-
76-
# Load the CSV data starting from the header line
77-
data = pd.read_csv(file_path, skiprows=header_line_index)
78-
79-
# Ensure the required columns exist
80-
if all(col in data.columns for col in headers):
81-
return data
82-
else:
83-
return None # Return None if required columns are missing
84-
8563
def setup_dropdown_menu(self):
86-
# Populate dropdown menu with the CSV channel columns (ignore Timestamp and Counter)
87-
columns = list(self.data.columns)
88-
channel_columns = [col for col in columns if 'Channel' in col]
64+
# Get available channel columns (Channel1 to Channel6)
65+
channel_columns = [col for col in self.data.columns if 'Channel' in col]
66+
67+
# Populate dropdown menu with available channels
8968
self.dropdown_menu['values'] = channel_columns
9069
if channel_columns:
91-
self.channel_selection.set(channel_columns[0]) # Default selection
70+
self.channel_selection.set(channel_columns[0]) # Default selection to the first channel
9271

9372
def plot_data(self):
9473
selected_channel = self.channel_selection.get() # Get the selected channel

0 commit comments

Comments
 (0)