@@ -45,50 +45,29 @@ def load_csv(self):
45
45
self .filename = filedialog .askopenfilename (filetypes = [("CSV files" , "*.csv" )])
46
46
if self .filename :
47
47
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 )
50
50
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 ]} " )
56
59
57
60
except Exception as e :
58
61
messagebox .showerror ("Error" , f"Could not load CSV file: { e } " )
59
62
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
-
85
63
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
89
68
self .dropdown_menu ['values' ] = channel_columns
90
69
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
92
71
93
72
def plot_data (self ):
94
73
selected_channel = self .channel_selection .get () # Get the selected channel
0 commit comments