-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashboard.py
161 lines (130 loc) · 6.71 KB
/
Dashboard.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import csv
import xarray as xr
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import os
class SatelliteDashboard:
def __init__(self, data_dict, saveloc):
self.data_dict = data_dict
self.saveloc = saveloc
self.root = tk.Tk()
self.root.title('Satellite Data Analysis')
self.main_frame = ttk.Frame(self.root)
self.main_frame.pack(fill='both', expand=True)
self.frames = []
for _ in range(3):
frame_ = ttk.Frame(self.main_frame)
frame_.pack(side='left', fill='both', expand=True)
self.frames.append(frame_)
# Add buttons to each frame
plume_button = ttk.Button(frame_, text="Plume", command=lambda frame=frame_: self.set_data_label(frame, 'plume'))
plume_button.pack(side='top', padx=10, pady=5)
artefact_button = ttk.Button(frame_, text="Artefact", command=lambda frame=frame_: self.set_data_label(frame, 'artefact'))
artefact_button.pack(side='top', padx=10, pady=5)
empty_button = ttk.Button(frame_, text="Empty", command=lambda frame=frame_: self.set_data_label(frame, 'empty'))
empty_button.pack(side='top', padx=10, pady=5)
save_button = ttk.Button(self.main_frame, text="Save Data", command=self.save_data)
save_button.pack(side='bottom', padx=10, pady=10)
self.location_dropdowns = []
self.variable_dropdowns = []
self.time_sliders = []
self.canvases = []
for frame in self.frames:
location_dropdown = ttk.Combobox(frame, values=list(data_dict.keys()))
location_dropdown.pack(side='top', padx=10, pady=5)
location_dropdown.bind("<<ComboboxSelected>>", self.location_selected)
self.location_dropdowns.append(location_dropdown)
variable_dropdown = ttk.Combobox(frame, values=[])
variable_dropdown.pack(side='top', padx=10, pady=5)
self.variable_dropdowns.append(variable_dropdown)
time_slider = ttk.Scale(frame, orient='horizontal', length=400) # Adjust length here
time_slider.pack(side='top', padx=10, pady=5)
time_slider.bind("<ButtonRelease-1>", self.update_plot)
self.time_sliders.append(time_slider)
canvas = FigureCanvasTkAgg(plt.figure(), master=frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.canvases.append(canvas)
# Preselect the first item in dropdown menus
for location_dropdown, variable_dropdown in zip(self.location_dropdowns, self.variable_dropdowns):
location_dropdown.set(list(data_dict.keys())[0])
variable_dropdown.set(list(data_dict[list(data_dict.keys())[0]].keys())[0])
def location_selected(self, event):
location_dropdown = event.widget
location_id = location_dropdown.get()
index = self.location_dropdowns.index(location_dropdown)
variables = list(self.data_dict[location_id].keys())
self.variable_dropdowns[index]['values'] = variables
self.update_slider_range(index, variables)
self.update_plot()
def update_slider_range(self, index, variables):
location_id = self.location_dropdowns[index].get()
max_time = max(len(self.data_dict[location_id][variable]) for variable in variables)
self.time_sliders[index].config(to=max_time - 1)
def update_plot(self, event=None):
for i, (location_dropdown, variable_dropdown, time_slider, canvas) in enumerate(
zip(self.location_dropdowns, self.variable_dropdowns, self.time_sliders, self.canvases)):
location_id = location_dropdown.get()
variable = variable_dropdown.get()
time_index = int(round(time_slider.get())) # Round to nearest integer
sensor_data = self.data_dict[location_id][variable][time_index]
time_value_display = str(self.data_dict[location_id]['time'][time_index])[36:46]
plt.figure(canvas.figure.number)
plt.clf() # Clear existing plot
# plt.imshow(normalized_data, cmap='viridis', origin='lower', vmin=0, vmax=1) # normalized
plt.imshow(sensor_data, cmap='viridis', origin='lower') # actual data
plt.title(f'{variable} at time {time_value_display}')
plt.colorbar(label='Value') # Add colorbar legend
plt.axis('off')
canvas.draw()
def set_data_label(self, frame, label):
location_dropdown = self.location_dropdowns[self.frames.index(frame)]
location_id = location_dropdown.get()
print(location_id)
time_slider = self.time_sliders[self.frames.index(frame)]
time_index = int(round((time_slider.get())))
time = self.data_dict[location_id]['time'].isel(time=time_index).values
print(time)
# Set the label in the data dictionary
print(location_id, 'label before:', self.data_dict[location_id]['classification_label'].isel(
time=time_index).values)
self.data_dict[location_id]['classification_label'][{'time': time_index}] = label
print(location_id, 'changed to:', label)
print()
def save_data(self):
for key, timeserie in self.data_dict.items():
savepath = f'{os.path.join(self.saveloc, "labelled_data")}_{key}.nc'
timeserie.to_netcdf(savepath)
print("labels:", timeserie['classification_label'].values)
print('file saved as ', savepath)
def run(self):
print("Dashboard Running")
self.root.mainloop()
print("Dashboard terminated")
self.save_data()
def load_dashboard_data(labelled_imagesdirectory, csvpath, ignore_id_list):
# load in data
loaded_data = dict({})
counter = 0
with open(csvpath, 'r') as file:
reader = csv.DictReader(file)
rows = list(reader)
counter += 1
for file in os.listdir(labelled_imagesdirectory):
if file.split('_')[2][:2] in ignore_id_list:
print(f"{file} ignored")
continue
print(file.split('_')[2][:2])
fileinfo = file.split('_')
loaded_data[fileinfo[2][:2]] = xr.open_dataset(
os.path.join(labelled_imagesdirectory, file))
counter += 1
total_scenes = 0
"================================================================="
for key in loaded_data.keys():
total_scenes += len(loaded_data[key]['time'].values)
print(key, 'scenes:', len(loaded_data[key]['time'].values))
print("total scenes opened:", total_scenes)
print("==========================================================================\n")
return loaded_data