Skip to content

Commit

Permalink
Update ML agent
Browse files Browse the repository at this point in the history
  • Loading branch information
XPD Operator committed Feb 3, 2024
1 parent ec0972e commit 7e4efba
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 37 deletions.
58 changes: 54 additions & 4 deletions scripts/kafka_consumer_iterate_qserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')

Expand Down
89 changes: 56 additions & 33 deletions scripts/prepare_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,89 @@

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}.')
else:
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))
# print(agent.ask("qei", n=1))
# print(agent.ask("qr", n=36))

0 comments on commit 7e4efba

Please sign in to comment.