removed commandbuffer and made it possible to run multiple plots at the same time
This commit is contained in:
@ -7,27 +7,34 @@ import h5py
|
||||
import time
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
import random
|
||||
|
||||
|
||||
class BECAdapter:
|
||||
|
||||
def __init__(self, redis_connector) -> None:
|
||||
def __init__(self, redis_connector, recon = False, monitor=False, motor_pos = False) -> None:
|
||||
super().__init__()
|
||||
|
||||
self.redis_connector = redis_connector # this is incoming
|
||||
# self.file_reader = .... # add file reader here
|
||||
self.grum_client = RPCClient("localhost", 8000) # this is outgoing
|
||||
self.grum_client_monitor = RPCClient("localhost", 8000) # this is outgoing
|
||||
self.grum_client_motor_pos = RPCClient("localhost", 8000) # this is outgoing
|
||||
self.grum_client_recon_abs = RPCClient("localhost", 8000) # this is outgoing
|
||||
self.grum_client_recon_phase = RPCClient("localhost", 8000) # this is outgoing
|
||||
|
||||
self.last_plotted_scan_nr = 0
|
||||
self.latest_reconstructed =None
|
||||
self.scan_nr = None
|
||||
|
||||
#uncomment for monitor and motor pos start plots
|
||||
# self.grum_client.new_plot("monitor",{'xlabel':'time', 'ylabel':"sls_ring_current" })
|
||||
# self.grum_client.new_plot("Motor position", {'xlabel':'average_x_st_fzp', 'ylabel':"average_y_st_fzp" })
|
||||
|
||||
self.scan_running = False
|
||||
|
||||
# To decide what should be plotted
|
||||
self.monitor = monitor
|
||||
self.motor_pos = motor_pos
|
||||
self.recon = recon
|
||||
|
||||
if self.monitor:
|
||||
self.grum_client_monitor.new_plot( "monitor",{'xlabel':'time', 'ylabel':"sls_ring_current", "format": "time" })
|
||||
if self.motor_pos:
|
||||
self.grum_client_motor_pos.new_plot("Motor position", {'xlabel':'average_x_st_fzp', 'ylabel':"average_y_st_fzp" })
|
||||
|
||||
def start(self):
|
||||
self.start_scan_status_sub()
|
||||
self.start_monitor_sub()
|
||||
@ -48,17 +55,15 @@ class BECAdapter:
|
||||
ypos = [y for x,y in self.positions]
|
||||
if len(segment.content["info"]["primary"]) == 2:
|
||||
self.scan_running = True
|
||||
# uncomment for plot a new plot for every motor scan
|
||||
# self.grum_client.new_plot(f"Motor position for scan: {self.scan_nr}", {"xs": xpos, "ys":ypos, 'xlabel':'average_x_st_fzp', 'ylabel':"average_y_st_fzp" })
|
||||
# uncomment for plot in the same plot for motor pos, how to append array?
|
||||
# self.grum_client.new_plot(f"Motor position {self.scan_nr}", {"xs": xpos, "ys":ypos, 'xlabel':'average_x_st_fzp', 'ylabel':"average_y_st_fzp" })
|
||||
|
||||
# print("Plotting new positions")
|
||||
if self.motor_pos:
|
||||
self.grum_client_motor_pos.new_plot( "Motor position", {"xs": xpos, "ys":ypos, 'xlabel':'average_x_st_fzp', 'ylabel':"average_y_st_fzp", "format": "Dots"})
|
||||
else:
|
||||
self.scan_running = False
|
||||
|
||||
if len(segment.content["info"]["primary"]) == 1:
|
||||
print("umv")
|
||||
|
||||
|
||||
def start_scan_segment_sub(self):
|
||||
self._scan_segment_sub = self.redis_connector.consumer(
|
||||
MessageEndpoints.scan_segment(), cb=self.scan_segment_update
|
||||
@ -67,14 +72,14 @@ class BECAdapter:
|
||||
|
||||
|
||||
def scan_segment_update(self, segment):
|
||||
# every scan segment of a scan
|
||||
if self.scan_running:
|
||||
segment = BECMessage.ScanMessage.loads(segment.value)
|
||||
x_val = segment[0].content["data"]["average_x_st_fzp"]["value"]
|
||||
y_val = segment[0].content["data"]["average_y_st_fzp"]["value"]
|
||||
|
||||
point = [x_val, y_val]
|
||||
#self.grum_client.append_data(f"Motor position for scan: {self.scan_nr}", point)
|
||||
if self.motor_pos:
|
||||
self.grum_client_motor_pos.append_data( "Motor position", point)
|
||||
|
||||
def start_monitor_sub(self):
|
||||
self._monitor_sub = self.redis_connector.consumer(
|
||||
@ -91,60 +96,62 @@ class BECAdapter:
|
||||
x = msg.content["signals"][dev]['timestamp']
|
||||
y = msg.content["signals"][dev]['value']
|
||||
point = [x,y]
|
||||
#uncomment to plot monitor values
|
||||
# self.grum_client.append_data("monitor", point)
|
||||
|
||||
|
||||
if self.monitor:
|
||||
self.grum_client_monitor.append_data( "monitor", point)
|
||||
|
||||
def plot_img_from_h5(self):
|
||||
|
||||
while True:
|
||||
print('update for new scan_nr')
|
||||
self.update_scan_nr()
|
||||
print('scan_nr: ', self.scan_nr)
|
||||
print('Check for new reconstruction')
|
||||
self.update_latest_reconstructed()
|
||||
print('latest_reconstructed: ', self.latest_reconstructed)
|
||||
print("last plotted scan nr: ", self.last_plotted_scan_nr)
|
||||
if self.scan_nr > self.last_plotted_scan_nr:
|
||||
|
||||
# scan = "S0" + str(self.last_plotted_scan_nr + 1)
|
||||
ending_path = self._get_scan_dir(1000, self.scan_nr, leading_zeros = 5)
|
||||
mypath = "/sls/X12SA/data/e20632/Data10/analysis/" + ending_path
|
||||
|
||||
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
|
||||
recon_files = [
|
||||
file for file in files if file.endswith("recons.h5")]
|
||||
|
||||
hf = h5py.File(mypath + '/' + recon_files[0], 'r')
|
||||
recon = hf.get('reconstruction')
|
||||
recon_object = hf.get('reconstruction/object')
|
||||
recon_probes = hf.get('reconstruction/probes')
|
||||
|
||||
recon_probes_1 = recon_probes[0]
|
||||
recon_probes_2 = recon_probes[1]
|
||||
|
||||
recon_object =np.array(recon_object)
|
||||
recon_object_abs = np.abs(recon_object)
|
||||
recon_object_angle = np.angle(recon_object)
|
||||
if self.latest_reconstructed > self.last_plotted_scan_nr:
|
||||
|
||||
recon_img_abs = recon_object_abs.tolist()
|
||||
recon_img_angle = recon_object_angle.tolist()
|
||||
eaccount = "e20632" #TODO: change to e20642
|
||||
ending_path = self._get_scan_dir(1000, self.latest_reconstructed, leading_zeros = 5)
|
||||
mypath = "/sls/X12SA/data/" + eaccount + "/Data10/analysis/" + ending_path
|
||||
recon_file = self.get_recon_file(mypath)
|
||||
with h5py.File(mypath + '/' + recon_file, 'r') as hf:
|
||||
recon_img_abs, recon_img_angle = self.get_recon_imgs(hf)
|
||||
|
||||
# PLOT
|
||||
print('plotting new image with scan_nr: ', self.scan_nr )
|
||||
xlab = str(self.scan_nr)
|
||||
self.grum_client.new_image("Absorption", {'image': recon_img_abs, "xlabel": xlab})
|
||||
self.grum_client.new_image("Phase", {'image': recon_img_angle, "xlabel": xlab, "colormap":"CET-C1"})
|
||||
print("done with plotting")
|
||||
self.last_plotted_scan_nr = self.scan_nr
|
||||
print('plotting new image with scan_nr: ', self.latest_reconstructed)
|
||||
xlab = str(self.latest_reconstructed)
|
||||
if self.recon:
|
||||
self.grum_client_recon_abs.new_image( "Absorption", {'image': recon_img_abs, "xlabel": xlab})
|
||||
self.grum_client_recon_phase.new_image( f"Phase for scan_nr: {self.latest_reconstructed}", {'image': recon_img_angle, "xlabel": xlab}) #, "colormap":"CET-C1"})
|
||||
self.last_plotted_scan_nr = self.latest_reconstructed
|
||||
|
||||
time.sleep(10)
|
||||
def get_recon_file(self, mypath):
|
||||
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
|
||||
for file in files:
|
||||
if file.endswith("recons.h5"):
|
||||
return file
|
||||
return None
|
||||
|
||||
def get_recon_imgs(self, hf):
|
||||
recon = hf.get('reconstruction')
|
||||
recon_object = hf.get('reconstruction/object')
|
||||
recon_probes = hf.get('reconstruction/probes')
|
||||
|
||||
def update_scan_nr(self):
|
||||
mypath = '/sls/X12SA/Data10/e20632/analysis/online/ptycho/gallery' #TODO: change hardcoded eaccount
|
||||
recon_probes_1 = recon_probes[0]
|
||||
recon_probes_2 = recon_probes[1]
|
||||
|
||||
recon_object =np.array(recon_object)
|
||||
recon_object_abs = np.abs(recon_object)
|
||||
recon_object_angle = np.angle(recon_object)
|
||||
|
||||
recon_img_abs = recon_object_abs.tolist()
|
||||
recon_img_angle = recon_object_angle.tolist()
|
||||
return recon_img_abs, recon_img_angle
|
||||
|
||||
def update_latest_reconstructed(self):
|
||||
mypath = '/sls/X12SA/Data10/e20632/analysis/online/ptycho/gallery' #TODO: change hardcoded eaccount to e20642
|
||||
file_list = listdir(mypath)
|
||||
scans = [file_name.split('_')[0] for file_name in file_list]
|
||||
max_scan_nr = max([int(scan[1:6]) for scan in scans])
|
||||
print('Max scan nr: ', max_scan_nr)
|
||||
self.scan_nr = max_scan_nr
|
||||
self.latest_reconstructed = max([int(scan[1:6]) for scan in scans])
|
||||
print('Max scan nr: ', self.latest_reconstructed)
|
||||
|
||||
def _get_scan_dir(self, scan_bundle, scan_number, leading_zeros=None):
|
||||
if leading_zeros is None:
|
||||
@ -158,6 +165,6 @@ if __name__ == "__main__":
|
||||
print("initializing redis-connector")
|
||||
redis_url = "129.129.122.75:6379" # for LamNI
|
||||
redis_connector = RedisConnector(redis_url)
|
||||
ba = BECAdapter(redis_connector)
|
||||
ba = BECAdapter(redis_connector, recon=True, monitor = True)
|
||||
ba.start()
|
||||
ba.plot_img_from_h5()
|
||||
|
@ -1,34 +0,0 @@
|
||||
from queue import Queue
|
||||
from threading import Thread
|
||||
|
||||
|
||||
class CommandBuffer:
|
||||
|
||||
def __init__(self):
|
||||
self.cmds = Queue()
|
||||
self.thread = None
|
||||
self.running = False
|
||||
|
||||
def _run(self):
|
||||
self.running = True
|
||||
while self.running:
|
||||
c = self.cmds.get()
|
||||
func, args, kwargs = c
|
||||
print("run:", func, args, kwargs.keys())
|
||||
func(*args, **kwargs)
|
||||
|
||||
def start(self):
|
||||
self.thread = thread = Thread(target=self._run)
|
||||
thread.start()
|
||||
|
||||
def stop(self):
|
||||
self.running = False
|
||||
|
||||
def add(self, func, *args, **kwargs):
|
||||
print("add:", func, args, kwargs.keys())
|
||||
c = (func, args, kwargs)
|
||||
self.cmds.put(c)
|
||||
|
||||
|
||||
|
||||
|
@ -1,13 +1,15 @@
|
||||
from pyqtgraph import DateAxisItem
|
||||
|
||||
class PlotDescription:
|
||||
|
||||
def __init__(self, name, title=None, xlabel=None, ylabel=None, xs=None, ys=None):
|
||||
def __init__(self, name, title=None, xlabel=None, ylabel=None, xs=None, ys=None, format=None):
|
||||
self.name = name
|
||||
self.title = title
|
||||
self.xlabel = xlabel
|
||||
self.ylabel = ylabel
|
||||
self.xs = [] if xs is None else list(xs)
|
||||
self.ys = [] if ys is None else list(ys)
|
||||
self.format = format
|
||||
|
||||
|
||||
@property
|
||||
@ -31,7 +33,18 @@ class PlotDescription:
|
||||
|
||||
|
||||
def make_plot(self, plotwidget, style):
|
||||
# if self.format == "Line":
|
||||
# res = plotwidget.plot(self.xs, self.ys, name=self.name, pen="r") #, **style)
|
||||
# elif self.format == "Dots":
|
||||
# print('Dots for positions')
|
||||
# res = plotwidget.plot(self.xs, self.ys, name=self.name, pen=None, symbol="o") #, **style)
|
||||
# else:
|
||||
# res = plotwidget.plot(self.xs, self.ys, name=self.name, **style)
|
||||
|
||||
res = plotwidget.plot(self.xs, self.ys, name=self.name, **style)
|
||||
if format == "time":
|
||||
axis = DateAxisItem()
|
||||
plotwidget.setAxisItems({'bottom':axis})
|
||||
|
||||
if self.title:
|
||||
plotwidget.setTitle(self.title)
|
||||
|
Reference in New Issue
Block a user