-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog_editor.py
247 lines (194 loc) · 8.15 KB
/
log_editor.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from utility_functions import load_breadboard_client
bc = load_breadboard_client()
import os
import ipywidgets as widgets
from measurement_directory import *
from ipyfilechooser import FileChooser
from IPython.display import Image
import qgrid
import pandas as pd
from numpy import nan
import json
import numpy as np
import warnings
import matplotlib.cbook
from utility_functions import get_newest_df
warnings.filterwarnings("ignore", category=matplotlib.cbook.mplDeprecation)
filechooser_widget = FileChooser(os.getcwd())
filechooser_widget.show_only_dirs = True
table_viewer = widgets.Output(layout={'border': '1px solid black'})
def save_image_log(event, qgrid_widget):
row_idx = int(event['index'])
df = qgrid_widget.get_changed_df()
df_updated_row = df.loc[row_idx]
updated_row_dict = {}
for key in df_updated_row.index:
if key == 'notes':
continue
elif key == 'badshot':
updated_row_dict[key] = bool(df_updated_row.loc[key])
else:
if np.isnan(df_updated_row.loc[key]):
continue # don't upload nan's to breadboard
elif isinstance(df_updated_row.loc[key], np.integer):
updated_row_dict[key] = int(df_updated_row.loc[key])
elif isinstance(df_updated_row.loc[key], np.float):
updated_row_dict[key] = float(df_updated_row.loc[key])
else:
updated_row_dict[key] = df_updated_row.loc[key]
run_id = df_updated_row['run_id']
run_dict = bc._send_message(
'get', '/runs/' + str(run_id) + '/').json()
run_dict['notes'] = df_updated_row['notes']
run_dict['parameters'].update(updated_row_dict)
payload = json.dumps(run_dict)
# print(payload)
resp = bc._send_message('put', '/runs/' + str(run_id) + '/', data=payload)
def load_image_log(watchfolder, optional_column_names=[]):
try:
existing_df = load_qgrid.loaded_qgrid.get_changed_df().dropna()
except:
existing_df = None
try:
if watchfolder != load_image_log.old_watchfolder:
existing_df = None
load_qgrid.loaded_qgrid.close()
except:
pass
df = get_newest_df(
watchfolder, optional_column_names=optional_column_names, existing_df=existing_df)
load_image_log.old_watchfolder = watchfolder
for column in optional_column_names:
if column not in df.columns:
df[column] = nan
df.index = range(len(df.index))
col_opts = {'editable': False}
col_defs = {}
for col_name in optional_column_names + ['badshot', 'notes']:
col_defs[col_name] = {'editable': True}
col_defs['badshot']['ColumnWidth'] = 50
qgrid_widget = qgrid.show_grid(df, grid_options={'forceFitColumns': False, 'defaultColumnWidth': 100},
column_options=col_opts, column_definitions=col_defs)
qgrid_widget.on('cell_edited', save_image_log)
display(qgrid_widget)
return qgrid_widget
def load_qgrid(b):
load_qgrid.loaded_qgrid = load_image_log(filechooser_widget.selected_path,
list(optional_columns_widget.options))
xvars_menu.options = sorted(
load_qgrid.loaded_qgrid.get_changed_df().columns, key=str.casefold)
yvars_menu.options = sorted(
load_qgrid.loaded_qgrid.get_changed_df().columns, key=str.casefold)
load_button = widgets.Button(description='load')
load_button.on_click(load_qgrid)
def refresh_qgrid(b):
load_qgrid.loaded_qgrid.close()
load_qgrid(b)
refresh_button = widgets.Button(description='refresh')
refresh_button.on_click(refresh_qgrid)
# def close_qgrid(b):
# load_qgrid.loaded_qgrid.close()
# close_button = widgets.Button(description='close')
# close_button.on_click(close_qgrid)
def export_qgrid(b):
df = load_qgrid.loaded_qgrid.get_changed_df()
df.to_csv(os.path.join(filechooser_widget.selected_path,
os.path.basename(filechooser_widget.selected_path) + '_params.csv'))
export_button = widgets.Button(description='export')
export_button.on_click(export_qgrid)
optional_columns_widget = widgets.Select(
options=[],
description='manual columns',
disabled=False,
style={'description_width': 'initial'}
)
textbox = widgets.Text(
value='',
description='editable column name',
disabled=False,
style={'description_width': 'initial'}
)
add_column_button = widgets.Button(description='add name')
def add_column(button):
optional_columns = list(optional_columns_widget.options)
if len(textbox.value) > 0:
optional_columns = list(set().union(
['manual_' + textbox.value], optional_columns))
optional_columns_widget.options = optional_columns
add_column_button.on_click(add_column)
clear_column_button = widgets.Button(description='clear name')
def clear_column(button):
optional_columns = list(optional_columns_widget.options)
if len(optional_columns) > 0:
optional_columns.pop(0)
optional_columns_widget.options = optional_columns
clear_column_button.on_click(clear_column)
# live plotting
xvars_display = widgets.Select(
options=[],
disabled=False,
)
yvars_display = widgets.Select(
options=[],
disabled=False,
)
seqMode_display = widgets.Select(
options=[],
disabled=False,
)
xvars_menu = widgets.Dropdown(
options=[], description='xvar')
yvars_menu = widgets.Dropdown(
options=[], description='yvar')
MAX_DISTINCT_SEQMODES = 10
seqMode_menu = widgets.Dropdown(
options= [None] + list(range(MAX_DISTINCT_SEQMODES)), description='seqMode')
add_plot_button = widgets.Button(description='add (xvar, yvar)')
def add_plot(button):
xvars, yvars, seqModes = (list(xvars_display.options), list(yvars_display.options),
list(seqMode_display.options))
xvars.append(xvars_menu.value), yvars.append(yvars_menu.value)
seqModes.append(seqMode_menu.value)
xvars_display.options, yvars_display.options, seqMode_display.options = xvars, yvars, seqModes
add_plot_button.on_click(add_plot)
clear_plot_button = widgets.Button(description='clear')
def clear_plot(button):
xvars, yvars, seqModes = (list(xvars_display.options), list(yvars_display.options),
list(seqMode_display.options))
xvars.pop(), yvars.pop(), seqModes.pop()
xvars_display.options, yvars_display.options, seqMode_display.options = xvars, yvars, seqModes
clear_plot_button.on_click(clear_plot)
lastshotBad_button = widgets.Button(description='yeet last shot')
def mark_lastshotBad(button):
load_qgrid.loaded_qgrid.close()
load_qgrid(load_button)
df = load_qgrid.loaded_qgrid.get_changed_df()
bc.append_analysis_to_run(int(df['run_id'][0]), {'badshot': True})
load_qgrid.loaded_qgrid.close()
load_qgrid(load_button)
lastshotBad_button.on_click(mark_lastshotBad)
VBox_x = widgets.VBox([xvars_menu, xvars_display])
VBox_y = widgets.VBox([yvars_menu, yvars_display])
VBox_seq = widgets.VBox([seqMode_menu, seqMode_display])
VBox_buttons = widgets.VBox([add_plot_button, clear_plot_button, lastshotBad_button])
live_plot_HBox = widgets.HBox(
[VBox_x, VBox_y, VBox_seq, VBox_buttons])
# TODO create image preview widget
# image_viewer = widgets.Output(layout={'border': '1px solid black'})
# def show_selected_image(event, qgrid_widget):
# image_viewer.clear_output()
# row_idx = int(event['new'][0])
# df = pd.read_csv(filename)
# raw_image_filename = df.loc[row_idx, 'filename0']
# filepath = measurement_dir + '\\' + raw_image_filename.replace('.spe', '.jpg')
# with image_viewer:
# if os.path.exists(filepath) and '.jpg' in filepath:
# pil_img = Image(filepath)
# display(pil_img)
# else:
# display('no jpg preview at ' + filepath)
def display_qgrid_widgets():
display(filechooser_widget)
display(widgets.HBox([load_button, refresh_button, export_button]))
display(widgets.HBox([optional_columns_widget, textbox,
add_column_button, clear_column_button]))