Skip to content

Commit 006bdf0

Browse files
committedAug 1, 2017
Merged in PhyNerd/lyse/UpdateDataframeNoRead (pull request labscript-suite#17)
Update Dataframe without Readoperations Approved-by: Chris Billington <chrisjbillington@gmail.com> Approved-by: Shaun Johnstone <shaun.johnstone@monash.edu>
2 parents a60feb6 + d934484 commit 006bdf0

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed
 

‎__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
# will be as follows. Otherwise lyse will override them with spinning_top =
4444
# True and path <name of hdf5 file being analysed>:
4545
spinning_top = False
46+
# data to be sent back to the lyse GUI if running within lyse
47+
_updated_data = {}
4648

4749
if len(sys.argv) > 1:
4850
path = sys.argv[1]
@@ -159,7 +161,12 @@ def save_result(self, name, value, group=None, overwrite=True):
159161
raise Exception('Attribute %s exists in group %s. ' \
160162
'Use overwrite=True to overwrite.' % (name, group))
161163
h5_file[group].attrs.modify(name, value)
162-
164+
165+
if spinning_top:
166+
if self.h5_path not in _updated_data:
167+
_updated_data[self.h5_path] = {}
168+
_updated_data[self.h5_path][str(self.group), name] = value
169+
163170
def save_result_array(self, name, data, group=None, overwrite=True, keep_attrs=False):
164171
if self.no_write:
165172
raise Exception('This run is read-only. '

‎__main__.py

+30-22
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ def do_analysis(self, filepath):
242242
self.to_worker.put(['analyse', filepath])
243243
signal, data = self.from_worker.get()
244244
if signal == 'error':
245-
return False
245+
return False, data
246246
elif signal == 'done':
247-
return True
247+
return True, data
248248
else:
249249
raise ValueError('invalid signal %s'%str(signal))
250250

@@ -684,6 +684,7 @@ def do_analysis(self, filepath):
684684
routine.set_status('clear')
685685
remaining = self.todo()
686686
error = False
687+
updated_data = {}
687688
while remaining:
688689
self.logger.debug('%d routines left to do'%remaining)
689690
for routine in self.routines:
@@ -694,7 +695,7 @@ def do_analysis(self, filepath):
694695
if routine is not None:
695696
self.logger.info('running analysis routine %s'%routine.shortname)
696697
routine.set_status('working')
697-
success = routine.do_analysis(filepath)
698+
success, updated_data = routine.do_analysis(filepath)
698699
if success:
699700
routine.set_status('done')
700701
self.logger.debug('success')
@@ -713,11 +714,11 @@ def do_analysis(self, filepath):
713714
except ZeroDivisionError:
714715
# All routines got deleted mid-analysis, we're done here:
715716
status_percent = 100.0
716-
self.to_filebox.put(['progress', status_percent])
717+
self.to_filebox.put(['progress', status_percent, updated_data])
717718
if error:
718-
self.to_filebox.put(['error', None])
719+
self.to_filebox.put(['error', None, updated_data])
719720
else:
720-
self.to_filebox.put(['done', 100.0])
721+
self.to_filebox.put(['done', 100.0, {}])
721722
self.logger.debug('completed analysis of %s'%filepath)
722723

723724
def reorder(self, order):
@@ -1285,16 +1286,25 @@ def mark_as_deleted_off_disk(self, filepath):
12851286
app.output_box.output('Warning: Shot deleted from disk or no longer readable %s\n' % filepath, red=True)
12861287

12871288
@inmain_decorator()
1288-
def update_row(self, filepath, dataframe_already_updated=False, status_percent=None, new_row_data=None):
1289+
def update_row(self, filepath, dataframe_already_updated=False, status_percent=None, new_row_data=None, updated_row_data=None):
12891290
""""Updates a row in the dataframe and Qt model
12901291
to the data in the HDF5 file for that shot. Also sets the percent done, if specified"""
12911292
# Update the row in the dataframe first:
1293+
if (new_row_data is None) == (updated_row_data is None) and not dataframe_already_updated:
1294+
raise ValueError('Exactly one of new_row_data or updated_row_data must be provided')
1295+
12921296
df_row_index = np.where(self.dataframe['filepath'].values == filepath)
12931297
try:
12941298
df_row_index = df_row_index[0][0]
12951299
except IndexError:
12961300
# Row has been deleted, nothing to do here:
12971301
return
1302+
1303+
if updated_row_data is not None and not dataframe_already_updated:
1304+
for group, name in updated_row_data:
1305+
self.dataframe.loc[df_row_index, (group, name) + ('',) * (self.nlevels - 2)] = updated_row_data[group, name]
1306+
dataframe_already_updated = True
1307+
12981308
if not dataframe_already_updated:
12991309
if new_row_data is None:
13001310
raise ValueError("If dataframe_already_updated is False, then new_row_data, as returned "
@@ -1357,6 +1367,8 @@ def update_row(self, filepath, dataframe_already_updated=False, status_percent=N
13571367
if not isinstance(column_name, tuple):
13581368
# One of our special columns, does not correspond to a column in the dataframe:
13591369
continue
1370+
if updated_row_data is not None and column_name not in updated_row_data:
1371+
continue
13601372
item = self._model.item(model_row_number, column_number)
13611373
if item is None:
13621374
# This is the first time we've written a value to this part of the model:
@@ -1704,33 +1716,29 @@ def do_singleshot_analysis(self, filepath):
17041716
return
17051717
self.to_singleshot.put(filepath)
17061718
while True:
1707-
signal, status_percent = self.from_singleshot.get()
1719+
signal, status_percent, updated_data = self.from_singleshot.get()
17081720
if signal in ['error', 'progress']:
1709-
# Do the file reading here outside the GUI thread so as not to hang the GUI:
1710-
try:
1711-
new_row_data = get_dataframe_from_shot(filepath)
1712-
except IOError:
1713-
self.shots_model.mark_as_deleted_off_disk(filepath)
1714-
new_row_data = None
1715-
else:
1716-
self.shots_model.update_row(filepath, status_percent=status_percent, new_row_data=new_row_data)
1721+
for file in updated_data:
1722+
self.shots_model.update_row(file, status_percent=status_percent, updated_row_data=updated_data[file])
17171723
if signal == 'done':
17181724
# No need to update the dataframe again, that should have been done with the last 'progress' signal:
17191725
self.shots_model.update_row(filepath, status_percent=status_percent, dataframe_already_updated=True)
17201726
return
17211727
if signal == 'error':
1722-
# If new_row_data is None, that indicates that we got an
1723-
# IOError error above. Do not pause analysis in this
1724-
# case, as an error is expected given the shot file doesn't
1725-
# exist.
1726-
if new_row_data is not None:
1728+
if not os.path.exists(filepath):
1729+
# Do not pause if the file has been deleted. An error is
1730+
# no surprise there:
1731+
self.shots_model.mark_as_deleted_off_disk(filepath)
1732+
else:
17271733
self.pause_analysis()
17281734
return
17291735

17301736
def do_multishot_analysis(self):
17311737
self.to_multishot.put(None)
17321738
while True:
1733-
signal, _ = self.from_multishot.get()
1739+
signal, _, updated_data = self.from_multishot.get()
1740+
for file in updated_data:
1741+
self.shots_model.update_row(file, updated_row_data=updated_data[file])
17341742
if signal == 'done':
17351743
self.multishot_required = False
17361744
return

‎analysis_subprocess.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ def mainloop(self):
253253
path = data
254254
success = self.do_analysis(path)
255255
if success:
256-
self.to_parent.put(['done', None])
256+
self.to_parent.put(['done', lyse._updated_data])
257257
else:
258-
self.to_parent.put(['error', None])
258+
self.to_parent.put(['error', lyse._updated_data])
259259
else:
260260
self.to_parent.put(['error','invalid task %s'%str(task)])
261261

@@ -280,7 +280,7 @@ def do_analysis(self, path):
280280
sandbox.deprecation_messages['path'] = deprecation_message
281281
# Use lyse.path instead:
282282
lyse.path = path
283-
283+
lyse._updated_data = {}
284284
# Do not let the modulewatcher unload any modules whilst we're working:
285285
try:
286286
with self.modulewatcher.lock:

0 commit comments

Comments
 (0)
Failed to load comments.