|
1 |
| -import tkinter as tk |
| 1 | +import tkinter as tk |
2 | 2 | from tkinter import filedialog, messagebox, ttk
|
3 | 3 | import pandas as pd
|
4 | 4 | import plotly.graph_objects as go
|
5 |
| -from plotly.subplots import make_subplots |
6 | 5 |
|
7 | 6 | class CSVPlotterApp:
|
8 | 7 | def __init__(self, root):
|
@@ -46,26 +45,58 @@ def load_csv(self):
|
46 | 45 | self.filename = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
|
47 | 46 | if self.filename:
|
48 | 47 | try:
|
49 |
| - self.data = pd.read_csv(self.filename) # Load the CSV file |
50 |
| - self.setup_dropdown_menu() |
51 |
| - self.file_label.config(text=f"File: {self.filename.split('/')[-1]}") |
| 48 | + # Read file with pandas, skipping metadata if any |
| 49 | + self.data = self.load_csv_data(self.filename) |
| 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.") |
| 56 | + |
52 | 57 | except Exception as e:
|
53 | 58 | messagebox.showerror("Error", f"Could not load CSV file: {e}")
|
54 | 59 |
|
| 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 | + |
55 | 85 | def setup_dropdown_menu(self):
|
56 |
| - # Populate dropdown menu with the CSV column names (channels) |
| 86 | + # Populate dropdown menu with the CSV channel columns (ignore Timestamp and Counter) |
57 | 87 | columns = list(self.data.columns)
|
58 |
| - self.dropdown_menu['values'] = columns |
59 |
| - if columns: |
60 |
| - self.channel_selection.set(columns[0]) # Default selection |
| 88 | + channel_columns = [col for col in columns if 'Channel' in col] |
| 89 | + self.dropdown_menu['values'] = channel_columns |
| 90 | + if channel_columns: |
| 91 | + self.channel_selection.set(channel_columns[0]) # Default selection |
61 | 92 |
|
62 | 93 | def plot_data(self):
|
63 | 94 | selected_channel = self.channel_selection.get() # Get the selected channel
|
64 | 95 | if not selected_channel:
|
65 | 96 | messagebox.showerror("Error", "No channel selected for plotting")
|
66 | 97 | return
|
67 | 98 |
|
68 |
| - fig = go.Figure() # Plot the selected channel using Plotly |
| 99 | + fig = go.Figure() # Plot the selected channel using Plotly |
69 | 100 | fig.add_trace(go.Scatter(x=self.data.index, y=self.data[selected_channel], mode='lines', name=selected_channel))
|
70 | 101 |
|
71 | 102 | fig.update_layout(
|
|
0 commit comments