193 lines
7.2 KiB
Python
193 lines
7.2 KiB
Python
"adxvSocketh5.py" 190L, 7371C
|
|
#!/usr/bin/env python
|
|
"""
|
|
Library for adxv-socketh5
|
|
"""
|
|
__author__ = "JieNan"
|
|
__date__ = "2022/05/12"
|
|
__version__ = "0.1"
|
|
|
|
|
|
import argparse
|
|
import socket
|
|
import os
|
|
#import h5py
|
|
import time
|
|
import numpy as np
|
|
|
|
class adxvSocketh5:
|
|
def __init__(self, host="localhost", port = 8100, spot_type_file = None):
|
|
#def __init__(self, host="localhost", spot_type_file = None):
|
|
self.adxv_sock = None
|
|
self.host = host
|
|
self.port = 8100
|
|
self.cont_size = None
|
|
self.master_file = None
|
|
self.img_num = None
|
|
self.num_spots_groups = 0 # number of sets of spots
|
|
self.spot_type_file = spot_type_file
|
|
|
|
def __del__(self):
|
|
try:
|
|
if self.adxv_sock is not None:
|
|
self.adxv_sock.close()
|
|
except:
|
|
pass
|
|
|
|
def init(self):
|
|
try:
|
|
self.adxv_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
|
print("init adxv socket...... {} {}".format(self.host,self.port))
|
|
self.adxv_sock.connect((self.host,self.port))
|
|
self.run_adxv_cmd("rings on")
|
|
self.run_adxv_cmd("colors Heat")
|
|
self.run_adxv_cmd("raise_window Magnify")
|
|
self.clear_spot()
|
|
self.set_spot_type()
|
|
except:
|
|
raise Exception("Init Error: Could not connect to adxv host")
|
|
self.adxv_sock.close()
|
|
def set_spot_type(self):
|
|
if self.spot_type_file:
|
|
with open(self.spot_type_file) as fp:
|
|
lines = fp.read().splitlines()
|
|
for line in lines:
|
|
tmp= line.split()
|
|
color = tmp[1]
|
|
radius = tmp[2]
|
|
print("define_type {} color {} radius {}\n".format(tmp[0],color,radius))
|
|
self.run_adxv_cmd("define_type {} color {} radius {}\n".format(tmp[0],color,radius))
|
|
|
|
def run_adxv_cmd(self, cmd=""):
|
|
self.adxv_sock.sendall("{}\n".format(cmd).encode())
|
|
|
|
def get_h5_num(self, img_num):
|
|
h5_img_num = (img_num -1) % self.cont_size + 1
|
|
h5_cont_num = int((img_num -1) / self.cont_size) + 1
|
|
h5_cont = self.master_file.replace("_master.h5","_data_%06d.h5" % h5_cont_num)
|
|
return h5_cont, h5_img_num
|
|
|
|
|
|
def load_tiff(self, tiff_path):
|
|
weak_data = 1
|
|
self.run_adxv_cmd("weak_data {}".format(weak_data))
|
|
self.run_adxv_cmd("load_image {}".format(tiff_path))
|
|
|
|
def load_image(self, slabs=1):
|
|
h5_cont, h5_img_num = self.get_h5_num(self.img_num)
|
|
print("will try to load file {}".format(h5_cont))
|
|
if not os.path.exists(h5_cont):
|
|
raise Exception("Error: data file {} doesn't exist".format(h5_cont))
|
|
weak_data = 1
|
|
self.run_adxv_cmd("set_slab {}".format(h5_img_num))
|
|
self.run_adxv_cmd("weak_data {}".format(weak_data))
|
|
self.run_adxv_cmd("set_slabs {}".format(slabs))
|
|
self.run_adxv_cmd("load_image {}".format(h5_cont)
|
|
|
|
def add_spots(self, spots_sets, total_num_spots):
|
|
# spots_sets is a list of numpy array, each array is a set/group of spots
|
|
# spots_sets[i][j][0] is pos_x, spots[i][j][0] is pos_y
|
|
self.num_spots_groups = len(spots_sets)
|
|
self.run_adxv_cmd("load_spots {}\n".format(total_num_spots))
|
|
spots_group = 1
|
|
for spots in spots_sets:
|
|
for spot in spots:
|
|
self.run_adxv_cmd("{} {} {}\n".format(spot[0],spot[1], spots_group))
|
|
spots_group +=1
|
|
self.run_adxv_cmd("end_of_pack\n")
|
|
|
|
def clear_spot(self):
|
|
#self.
|
|
self.num_spots_groups = 0
|
|
self.run_adxv_cmd("load_spots 0\n")
|
|
self.run_adxv_cmd("end_of_pack\n")
|
|
|
|
|
|
def save_img(self, img_file="", overwrite=False):
|
|
if overwrite and os.path.exists(img_file):
|
|
try:
|
|
os.remove(img_file)
|
|
except:
|
|
raise Exception("Error: File {} already exists and cannot overwrite it, please check the permission".format(img_file))
|
|
self.run_adxv_cmd("save_image {}".format(img_file))
|
|
|
|
def set_color(self, color="Grey"):
|
|
self.run_adxv_cmd("colors {}".format(color))
|
|
|
|
def update_slabs(self, slabs = 1):
|
|
self.run_adxv_cmd("slabs {}".format(slabs))
|
|
|
|
def update_meta(self, master_file=""):
|
|
# make sure the first data container is available before calling
|
|
data_h5 = master_file.replace("_master.h5", "_data_000001.h5")
|
|
if not os.path.exists(data_h5):
|
|
raise Exception ("Error: {} doesn't exist...".format(data_h5) )
|
|
self.master_file = master_file
|
|
with h5py.File(master_file, 'r') as f:
|
|
self.cont_size = f['/entry/data/data_000001'].shape[0]
|
|
# update the meta info in adxv
|
|
self.run_adxv_cmd("load_image {}".format(self.master_file))
|
|
|
|
def load_image_from_h5(self, h5_cont, img_num):
|
|
self.run_adxv_cmd("set_slab {}".format(img_num))
|
|
self.run_adxv_cmd("load_image {}".format(h5_cont))
|
|
|
|
def load_image_from_master(self, master_file, img_num =1):
|
|
if not os.access(master_file, os.R_OK):
|
|
raise Exception("Error: No access to master file {}".format(master_file))
|
|
self.update_meta(master_file=master_file)
|
|
self.img_num = img_num
|
|
self.load_image()
|
|
|
|
|
|
def read_spots_from_file(self, spots_file):
|
|
try:
|
|
with open(spots_file) as fp:
|
|
lines = fp.read().splitlines()
|
|
spots = np.loadtxt(lines)
|
|
except Exception as ex:
|
|
raise Exception("Error when reading spots from file {}".format(spots_file))
|
|
return spots.astype(int)
|
|
|
|
def save_sptxt(tiff_path):
|
|
|
|
return
|
|
|
|
def parseArgs():
|
|
"""
|
|
parse user input and return arguments
|
|
"""
|
|
parser = argparse.ArgumentParser(description = "test adxv lib")
|
|
parser.add_argument("-m", "--master_file", help="master file", type=str, default=None)
|
|
parser.add_argument("-n", "--img_num", help="image number", type=int, default=None)
|
|
parser.add_argument("-sp1", "--spot_file1", help="spot file1", type=str, default=None)
|
|
parser.add_argument("-sp2", "--spot_file2", help="spot file2", type=str, default=None)
|
|
parser.add_argument("-s", "--socket", help="socket number", type=int, default=8100)
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
args = parseArgs()
|
|
adxv = adxvSocketh5(port = args.socket, spot_type_file = "spot_type.txt")
|
|
# adxv = adxvSocketh5(spot_type_file = "spot_type.txt")
|
|
adxv.init()
|
|
# spots_file = "t1.adx"
|
|
# spots_file2 = "t3.adx"
|
|
# h5_cont = "/data/visitors/micromax/20231830/tests/ref_data/j4m_lys_2k/Lyso-2kHz_1_data_000001.h5"
|
|
spots_file = "/data/visitors/micromax/20231830/tests/dawn/preview/noindexed.adx"
|
|
spots_file2 = "/data/visitors/micromax/20231830/tests/dawn/preview/indexed.adx"
|
|
# adxv.load_image_from_h5(h5_cont,1)
|
|
tiff_path = "/data/visitors/micromax/20231830/tests/dawn/preview/test.tif"
|
|
adxv.load_tiff(tiff_path)
|
|
spots_sets_list=[]
|
|
total_spots_num =0
|
|
spots = adxv.read_spots_from_file(spots_file)
|
|
spots_sets_list.append(spots)
|
|
total_spots_num += spots.shape[0]
|
|
spots = adxv.read_spots_from_file(spots_file2)
|
|
spots_sets_list.append(spots)
|
|
total_spots_num += spots.shape[0]
|
|
adxv.add_spots(spots_sets_list, total_spots_num)
|
|
print(len(spots_sets_list), total_spots_num)
|