changes at beamline

This commit is contained in:
Stalberg Jonathan
2023-04-14 18:15:06 +02:00
parent 61e732e941
commit 830879cbe6
3 changed files with 61 additions and 35 deletions

View File

@ -5,6 +5,8 @@ from os import listdir
from os.path import isfile, join from os.path import isfile, join
import h5py import h5py
import time import time
import numpy as np
from PIL import Image
class BECAdapter: class BECAdapter:
@ -16,8 +18,8 @@ class BECAdapter:
# self.file_reader = .... # add file reader here # self.file_reader = .... # add file reader here
self.grum_client = RPCClient("localhost", 8000) # this is outgoing self.grum_client = RPCClient("localhost", 8000) # this is outgoing
self.last_plotted_scan_nr = 6755 self.last_plotted_scan_nr = 6760
self.scan_nr = 6757 self.scan_nr = None
def start(self): def start(self):
self.start_scan_status_sub() self.start_scan_status_sub()
@ -52,47 +54,59 @@ class BECAdapter:
# point = [2, 3] # point = [2, 3]
# self.grum_client.append_data(name, point) # self.grum_client.append_data(name, point)
def get_data_from_file(self): def plot_img_from_h5(self):
while True: while True:
print('checking for new scan_nr') print('update for new scan_nr')
self.check_for_new_scan_nr() self.update_scan_nr()
if self.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) # scan = "S0" + str(self.last_plotted_scan_nr + 1)
mypath = "/sls/X12SA/data/e20632/Data10/analysis/S06000-06999/" + scan ending_path = self._get_scan_dir(1000, self.last_plotted_scan_nr + 1, leading_zeros = 5)
# mypath = "/home/stalbe_j/Documents" # for test on own computer mypath = "/sls/X12SA/data/e20632/Data10/analysis/" + ending_path
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
print(files)
recon_files = []
while not recon_files:
recon_files = [
file for file in files if file.endswith("recons.h5")]
print('this are recon_files: ', recon_files)
if recon_files:
break
print('sleeping 1')
time.sleep(10)
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') hf = h5py.File(mypath + '/' + recon_files[0], 'r')
print(hf.keys())
recon = hf.get('reconstruction') recon = hf.get('reconstruction')
print(recon.keys())
recon_object = hf.get('reconstruction/object') recon_object = hf.get('reconstruction/object')
recon_probes = hf.get('reconstruction/probes') recon_probes = hf.get('reconstruction/probes')
print(recon_object)
print(recon_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)
# PLOT self.grum_client.new_image(self.last_plotted_scan_nr+1, {'img': recon_object}) recon_img_abs = recon_object_abs.tolist()
self.last_plotted_scan_nr +=1 recon_img_angle = recon_object_angle.tolist()
print('sleeping 2') # PLOT
time.sleep(5) self.grum_client.new_image("Absorption", {'image': recon_img_abs, "xlabel": str(self.scan_nr)})
self.grum_client.new_image("Phase", {'image': recon_img_angle, "xlabel": str(self.scan_nr), "colormap":"CET-C1"})
def check_for_new_scan_nr(self): self.last_plotted_scan_nr = self.scan_nr
print('From checking: ')
print('scan_nr: ', self.scan_nr) time.sleep(10)
print('last_plotted_scan_nr: ', self.last_plotted_scan_nr)
def update_scan_nr(self):
# return (self.last_plotted_scan_nr == self.scan_nr)
mypath = '/sls/X12SA/Data10/e20632/analysis/online/ptycho/gallery'
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)
self.scan_nr = max_scan_nr
def _get_scan_dir(self, scan_bundle, scan_number, leading_zeros=None):
if leading_zeros is None:
leading_zeros = len(str(scan_bundle))
floor_dir = scan_number // scan_bundle * scan_bundle
return f"S{floor_dir:0{leading_zeros}d}-{floor_dir+scan_bundle-1:0{leading_zeros}d}/S{scan_number:0{leading_zeros}d}"
if __name__ == "__main__": if __name__ == "__main__":
@ -102,4 +116,4 @@ if __name__ == "__main__":
redis_connector = RedisConnector(redis_url) redis_connector = RedisConnector(redis_url)
ba = BECAdapter(redis_connector) ba = BECAdapter(redis_connector)
ba.start() ba.start()
ba.get_data_from_file() ba.plot_img_from_h5()

View File

@ -1,14 +1,16 @@
import numpy as np import numpy as np
import pyqtgraph as pg
class ImageDescription: class ImageDescription:
def __init__(self, name, title=None, xlabel=None, ylabel=None, image=None): def __init__(self, name, title=None, xlabel=None, ylabel=None, image=None, colormap=None):
self.name = name self.name = name
self.title = title self.title = title
self.xlabel = xlabel self.xlabel = xlabel
self.ylabel = ylabel self.ylabel = ylabel
self.image = image self.image = image
self.colormap = colormap
@property @property
def data(self): def data(self):
@ -28,16 +30,23 @@ class ImageDescription:
def make_plot(self, plotwidget, style): def make_plot(self, plotwidget, style):
res = plotwidget.setImage(self.data) res = plotwidget.setImage(self.data)
# plotwidget.colormap()
if self.title: if self.title:
plotwidget.setTitle(self.title) plotwidget.setTitle(self.title)
if self.xlabel: if self.xlabel:
plotwidget.setLabel("bottom", self.xlabel) # vbox = plotwidget.getView()
# vbox.addItem(pg.LabelItem(self.xlabel))
plotwidget.setHistogramLabel(self.xlabel)
# plotwidget.setLabel("bottom", self.xlabel)
if self.ylabel: if self.ylabel:
plotwidget.setLabel("left", self.ylabel) plotwidget.setLabel("left", self.ylabel)
if self.colormap:
cm = pg.colormap.get(self.colormap)
plotwidget.setColorMap(cm)
return res return res

View File

@ -15,6 +15,9 @@ class MDISubImage(MDISubWindow):
style = pg_plot_style() style = pg_plot_style()
# cm = pg.colormap.get('CET-C1')
# pw.setColorMap(cm)
plot = desc.make_plot(self.pw, style) plot = desc.make_plot(self.pw, style)
self.plots = {name: plot} self.plots = {name: plot}