Skip to content

Commit b721f21

Browse files
Tried Fixing blitting Error in pyroelectric
1 parent 95e8357 commit b721f21

1 file changed

Lines changed: 33 additions & 35 deletions

File tree

pica/keithley/k6517b/Pyroelectricity/Pyroelectric_K6517B_L350_GUI.py

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def __init__(self, root):
227227
self.data_storage = {'time': [], 'temperature': [], 'current': []}
228228
self.data_queue = queue.Queue()
229229
self.measurement_thread = None
230-
self.plot_backgrounds = None # For blitting
230+
# self.plot_backgrounds = None # For blitting - REMOVED
231231

232232
self.setup_styles()
233233
self.create_widgets()
@@ -757,27 +757,25 @@ def _update_data_storage_and_plots(self, elapsed_time, current_temp, current_val
757757
self.data_storage['temperature'].append(current_temp)
758758
self.data_storage['current'].append(current_val)
759759

760-
if self.plot_backgrounds:
761-
for bg in self.plot_backgrounds:
762-
self.canvas.restore_region(bg)
763-
self.line_main.set_data(
764-
self.data_storage['temperature'],
765-
self.data_storage['current'])
766-
self.line_sub1.set_data(
767-
self.data_storage['time'],
768-
self.data_storage['temperature'])
769-
self.line_sub2.set_data(
770-
self.data_storage['time'], self.data_storage['current'])
771-
for ax, line in zip(
772-
self.axes, [
773-
self.line_main, self.line_sub1, self.line_sub2]):
774-
ax.relim()
775-
ax.autoscale_view()
776-
ax.draw_artist(line)
777-
self.canvas.blit(self.figure.bbox)
778-
else:
779-
self.figure.tight_layout(pad=3.0)
780-
self.canvas.draw_idle()
760+
# Skip plotting points where the instrument returned NaN so the
761+
# autoscaler is not fed invalid limits.
762+
temp = self.data_storage['temperature']
763+
curr = self.data_storage['current']
764+
t = self.data_storage['time']
765+
766+
self.line_main.set_data(temp, curr)
767+
self.line_sub1.set_data(t, temp)
768+
self.line_sub2.set_data(t, curr)
769+
770+
# Recompute data limits and rescale. Because this is a FULL redraw,
771+
# the ticks, labels, gridlines and sci-notation offset update too.
772+
for ax in self.axes:
773+
ax.relim()
774+
ax.autoscale_view()
775+
776+
# draw_idle() coalesces multiple requests and is the correct call from
777+
# the Tk main loop. At a 2 s sample rate this is plenty fast.
778+
self.canvas.draw_idle()
781779

782780
def _check_ramping_completion_conditions(self, current_temp, params):
783781
if current_temp >= params['safety_cutoff']:
@@ -843,15 +841,13 @@ def start_measurement(self):
843841
f"I vs T | Sample: {params['sample_name']}",
844842
fontweight='bold')
845843

846-
# --- Performance Improvement: Capture static background for blitting ---
847-
for line in [self.line_main, self.line_sub1, self.line_sub2]:
848-
line.set_animated(True)
849-
self.canvas.draw()
850-
self.plot_backgrounds = [
851-
self.canvas.copy_from_bbox(
852-
ax.bbox) for ax in self.axes]
853-
self.log("Blitting enabled for fast graph updates.")
854-
# --- End of performance improvement ---
844+
# Reset axes and do a single clean full redraw before the run.
845+
for ax in self.axes:
846+
ax.relim()
847+
ax.autoscale_view()
848+
self.figure.tight_layout(pad=3.0)
849+
self.canvas.draw_idle()
850+
self.log("Live graphs initialized.")
855851

856852
self.log("Moving to start temperature for stabilization...")
857853
self.experiment_state = 'stabilizing'
@@ -876,10 +872,12 @@ def stop_measurement(self, reason="stopped by user"):
876872
self.log(f"Measurement loop {reason}.")
877873
self.start_button.config(state='normal')
878874
self.stop_button.config(state='disabled')
879-
# Turn off animation for any final redraws
880-
for line in [self.line_main, self.line_sub1, self.line_sub2]:
881-
line.set_animated(False)
882-
self.plot_backgrounds = None
875+
# Let the worker thread finish its current iteration before we
876+
# tear down the VISA sessions, to avoid a read-during-close race.
877+
if (self.measurement_thread is not None
878+
and self.measurement_thread.is_alive()
879+
and threading.current_thread() is not self.measurement_thread):
880+
self.measurement_thread.join(timeout=3.0)
883881
self.backend.close_instruments()
884882
self.log("Instrument connections closed.")
885883
messagebox.showinfo(

0 commit comments

Comments
 (0)