From 7e4efba5c3593a39339314dade3a05c34c5f6a46 Mon Sep 17 00:00:00 2001 From: XPD Operator Date: Fri, 2 Feb 2024 19:47:38 -0500 Subject: [PATCH] Update ML agent --- scripts/kafka_consumer_iterate_qserver.py | 58 ++++++++++++++- scripts/prepare_agent.py | 89 ++++++++++++++--------- 2 files changed, 110 insertions(+), 37 deletions(-) diff --git a/scripts/kafka_consumer_iterate_qserver.py b/scripts/kafka_consumer_iterate_qserver.py index 1587860..9ee373a 100644 --- a/scripts/kafka_consumer_iterate_qserver.py +++ b/scripts/kafka_consumer_iterate_qserver.py @@ -95,7 +95,7 @@ from blop import Agent, DOF, Objective # agent_data_path = '/home/xf28id2/data_ZnCl2' -# agent_data_path = '/home/xf28id2/data' +# agent_data_path = '/home/xf28id2/data_ZnI2_60mM' agent_data_path = '/home/xf28id2/data_halide' dofs = [ @@ -111,7 +111,7 @@ Objective(description="Quantum yield", name="PLQY", target="max", log=True, weight=1., max_noise=0.25), ] -USE_AGENT = True +USE_AGENT = False agent_iterate = False if USE_AGENT: @@ -293,7 +293,7 @@ def print_message(consumer, doctype, doc, label_uid = f'{uid[0:8]}_{metadata_dic["sample_type"]}' # u.plot_average_good(x0, y0, color=cmap(color_idx[sub_idx]), label=label_uid) # sub_idx = sample.index(metadata_dic['sample_type']) - u.plot_average_good(x0, y0, label=label_uid) + u.plot_average_good(x0, y0, label=label_uid, clf_limit=9) ## Skip peak fitting if qepro type is absorbance if qepro_dic['QEPro_spectrum_type'][0] == 3: @@ -334,7 +334,7 @@ def print_message(consumer, doctype, doc, if (stream_name == 'fluorescence') and (PLQY[0]==1): PL_integral_s = integrate.simpson(y,x) label_uid = f'{uid[0:8]}_{metadata_dic["sample_type"]}' - u.plot_CsPbX3(x, y, peak_emission, label=label_uid, clf_limit=10) + u.plot_CsPbX3(x, y, peak_emission, label=label_uid, clf_limit=9) ## Find absorbance at 365 nm from absorbance stream # q_dic, m_dic = de.read_qepro_by_stream(uid, stream_name='absorbance', data_agent='tiled') @@ -456,6 +456,56 @@ def print_message(consumer, doctype, doc, if stream_name == 'primary': if len(bad_data) > 3: print('*** qsever aborted due to too many bad scans, please check setup ***\n') + + ### Stop all infusing pumps + zmq_single_request( + method='queue_item_add', + params={ + 'item':{ + "name":"stop_group", + "args": [pump_list], + "item_type":"plan"}, + 'pos': 'front', + 'user_group':'primary', + 'user':'chlin'}) + + ### Set up washing tube/loop + zmq_single_request( + method='queue_item_add', + params={ + 'item':{ + "name":"start_group_infuse", + "args": [[wash_tube[1]], [wash_tube[2]]], + "item_type":"plan"}, + 'pos': pos, + 'user_group':'primary', + 'user':'chlin'}) + + ### Wash loop/tube for xxx seconds + zmq_single_request( + method='queue_item_add', + params={ + 'item':{ + "name":"sleep_sec_q", + "args":[wash_tube[3]], + "item_type":"plan"}, + 'pos': pos, + 'user_group':'primary', + 'user':'chlin'}) + + + ### Stop washing + zmq_single_request( + method='queue_item_add', + params={ + 'item':{ + "name":"stop_group", + "args": [[wash_tube[1]]], + "item_type":"plan"}, + 'pos': pos, + 'user_group':'primary', + 'user':'chlin'}) + zmq_single_request(method='queue_stop') # zmq_single_request(method='re_abort') diff --git a/scripts/prepare_agent.py b/scripts/prepare_agent.py index 952ddc7..986d53e 100644 --- a/scripts/prepare_agent.py +++ b/scripts/prepare_agent.py @@ -9,51 +9,72 @@ from blop import Agent, DOF, Objective -# data_path = '/home/xf28id2/data_ZnCl2' -#data_path = '/home/xf28id2/data' -agent_data_path = '/home/xf28id2/data_halide' -dofs = [ - DOF(description="CsPb(oleate)3", name="infusion_rate_CsPb", units="uL/min", search_bounds=(10, 110)), - DOF(description="TOABr", name="infusion_rate_Br", units="uL/min", search_bounds=(70, 170)), - DOF(description="ZnCl2", name="infusion_rate_Cl", units="uL/min", search_bounds=(0, 150)), - DOF(description="ZnI2", name="infusion_rate_I2", units="uL/min", search_bounds=(0, 150)), -] +def build_agen(peak_target=660, peak_tolerance=5): + # data_path = '/home/xf28id2/data_ZnCl2' + #data_path = '/home/xf28id2/data' + agent_data_path = '/home/xf28id2/data_halide' -objectives = [ - Objective(description="Peak emission", name="Peak", target=660, weight=10, max_noise=0.25), - Objective(description="Peak width", name="FWHM", target="min", log=True, weight=2., max_noise=0.25), - Objective(description="Quantum yield", name="PLQY", target="max", log=True, weight=1., max_noise=0.25), -] + if peak_target > 525: + dofs = [ + DOF(description="CsPb(oleate)3", name="infusion_rate_CsPb", units="uL/min", search_bounds=(10, 110)), + DOF(description="TOABr", name="infusion_rate_Br", units="uL/min", search_bounds=(70, 170)), + DOF(description="ZnCl2", name="infusion_rate_Cl", units="uL/min", search_bounds=(0, 0)), + DOF(description="ZnI2", name="infusion_rate_I2", units="uL/min", search_bounds=(0, 170)), + ] + elif peak_target < 515: + dofs = [ + DOF(description="CsPb(oleate)3", name="infusion_rate_CsPb", units="uL/min", search_bounds=(10, 110)), + DOF(description="TOABr", name="infusion_rate_Br", units="uL/min", search_bounds=(70, 170)), + DOF(description="ZnCl2", name="infusion_rate_Cl", units="uL/min", search_bounds=(0, 170)), + DOF(description="ZnI2", name="infusion_rate_I2", units="uL/min", search_bounds=(0, 0)), + ] + else: + dofs = [ + DOF(description="CsPb(oleate)3", name="infusion_rate_CsPb", units="uL/min", search_bounds=(10, 110)), + DOF(description="TOABr", name="infusion_rate_Br", units="uL/min", search_bounds=(70, 170)), + DOF(description="ZnCl2", name="infusion_rate_Cl", units="uL/min", search_bounds=(0, 0)), + DOF(description="ZnI2", name="infusion_rate_I2", units="uL/min", search_bounds=(0, 0)), + ] -# objectives = [ -# Objective(name="Peak emission", key="peak_emission", target=525, units="nm"), -# Objective(name="Peak width", key="peak_fwhm", minimize=True, units="nm"), -# Objective(name="Quantum yield", key="plqy"), -# ] -USE_AGENT = False + objectives = [ + Objective(description="Peak emission", name="Peak", target=(peak_target-peak_tolerance, peak_target+peak_tolerance), max_noise=0.25), + Objective(description="Peak width", name="FWHM", target="min", log=True, weight=1., max_noise=0.25), + Objective(description="Quantum yield", name="PLQY", target="max", log=True, weight=1., max_noise=0.25), + ] -agent = Agent(dofs=dofs, objectives=objectives, db=None, verbose=True) -#agent.load_data("~/blop/data/init.h5") -metadata_keys = ["time", "uid", "r_2"] -init_file = "/home/xf28id2/data_halide/init_240122_01.h5" -if os.path.exists(init_file): - agent.load_data(init_file) + # objectives = [ + # Objective(name="Peak emission", key="peak_emission", target=525, units="nm"), + # Objective(name="Peak width", key="peak_fwhm", minimize=True, units="nm"), + # Objective(name="Quantum yield", key="plqy"), + # ] -else: + USE_AGENT = False + + agent = Agent(dofs=dofs, objectives=objectives, db=None, verbose=True) + #agent.load_data("~/blop/data/init.h5") + + metadata_keys = ["time", "uid", "r_2"] + + init_file = "/home/xf28id2/data_halide/init_240122_01.h5" + + # if os.path.exists(init_file): + # agent.load_data(init_file) + + # else: filepaths = glob.glob(f"{agent_data_path}/*.json") for fp in tqdm(filepaths): with open(fp, "r") as f: data = json.load(f) - r_2_min = 0.6 + r_2_min = 0.85 try: if data['r_2'] < r_2_min: print(f'Skip because "r_2" of {os.path.basename(fp)} is {data["r_2"]:.2f} < {r_2_min}.') @@ -61,14 +82,16 @@ x = {k:[data[k]] for k in agent.dofs.names} y = {k:[data[k]] for k in agent.objectives.names} metadata = {k:[data.get(k, None)] for k in metadata_keys} - agent.tell(x=x, y=y, metadata=metadata, train=False) + agent.tell(x=x, y=y, metadata=metadata, train=False, update_models=False) except (KeyError): print(f'{os.path.basename(fp)} has no "r_2".') -agent._construct_all_models() -agent._train_all_models() + agent._construct_all_models() + agent._train_all_models() + + return agent -# print(agent.ask("qei", n=1)) -# print(agent.ask("qr", n=36)) \ No newline at end of file + # print(agent.ask("qei", n=1)) + # print(agent.ask("qr", n=36)) \ No newline at end of file