-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add startup scripts for server
- Loading branch information
1 parent
baf856a
commit f19fa53
Showing
5 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Passive GSAS Agent that Publishes Reports""" | ||
|
||
import tiled.client.node # noqa: F401 | ||
from bluesky_adaptive.server import register_variable, shutdown_decorator, startup_decorator | ||
|
||
from pdf_agents.gsas import RefinementAgent | ||
|
||
param_dict_phase1 = { | ||
"set": { | ||
"Background": {"type": "chebyschev-1", "no. coeffs": 6, "refine": True}, | ||
"Limits": [1, 12], | ||
"Scale": True, | ||
"Cell": True, | ||
"Size": { | ||
"type": "isotropic", | ||
"refine": True, | ||
"value": 0.2, | ||
}, # crystallite size in microns | ||
"Mustrain": {"type": "isotropic", "refine": True, "value": 0.001}, # RMS strain | ||
} | ||
} | ||
|
||
agent = RefinementAgent( | ||
# GSAS Args | ||
cif_paths=["/src/pdf-agents/assets/fcc.cif"], | ||
refinement_params=[param_dict_phase1], | ||
inst_param_path="/src/pdf-agents/assets/LaB6_IPF.instprm", | ||
# BS Adaptive Args | ||
ask_on_tell=False, | ||
report_on_tell=True, | ||
) | ||
|
||
|
||
@startup_decorator | ||
def startup(): | ||
agent.start() | ||
|
||
|
||
@shutdown_decorator | ||
def shutdown_agent(): | ||
return agent.stop() | ||
|
||
|
||
register_variable("Agent Name", agent, "instance_name") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Kmeans Agent that consumes GSAS Refinement Agent Output""" | ||
|
||
import uuid | ||
|
||
import nslsii | ||
import numpy as np | ||
import tiled.client.node # noqa: F401 | ||
from bluesky_adaptive.agents.base import AgentConsumer | ||
from bluesky_adaptive.server import register_variable, shutdown_decorator, startup_decorator | ||
|
||
from pdf_agents.sklearn import ActiveKmeansAgent | ||
|
||
# Custom Kafka Consumer Needed since we are subscribing downstream from GSAS | ||
beamline_tla = "pdf" | ||
kafka_config = nslsii.kafka_utils._read_bluesky_kafka_config_file(config_file_path="/etc/bluesky/kafka.yml") | ||
kafka_consumer = AgentConsumer( | ||
topics=[ | ||
f"{beamline_tla}.mmm.bluesky.agents", | ||
], | ||
consumer_config=kafka_config["runengine_producer_config"], | ||
bootstrap_servers=",".join(kafka_config["bootstrap_servers"]), | ||
group_id=f"echo-{beamline_tla}-{str(uuid.uuid4())[:8]}", | ||
) | ||
|
||
|
||
class Agent(ActiveKmeansAgent): | ||
@property | ||
def name(self): | ||
return "GSAS-Based-Active-Kmeans" | ||
|
||
def trigger_condition(self, uid) -> bool: | ||
return self.exp_catalog.metadata["start"]["agent_name"].startswith("GSAS-Refinement-Agent") | ||
|
||
|
||
agent = Agent( | ||
# K means Args | ||
bounds=np.array([(-32, 32), (-32, 32)]), | ||
k_clusters=4, | ||
# PDF Args | ||
motor_names=["xstage", "ystage"], | ||
motor_origins=[-154.7682, 48.9615], | ||
# BS Adaptive Args | ||
kafka_consumer=kafka_consumer, | ||
ask_on_tell=False, | ||
report_on_tell=False, | ||
) | ||
|
||
|
||
@startup_decorator | ||
def startup(): | ||
agent.start() | ||
|
||
|
||
@shutdown_decorator | ||
def shutdown_agent(): | ||
return agent.stop() | ||
|
||
|
||
register_variable("Tell Cache", agent, "tell_cache") | ||
register_variable("Agent Name", agent, "instance_name") |
108 changes: 108 additions & 0 deletions
108
pdf_agents/startup_scripts/mmm5-tax-day/monarch-kmeans-gsas.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import time as ttime | ||
import uuid | ||
|
||
import nslsii | ||
import numpy as np | ||
import tiled.client.node # noqa: F401 | ||
from bluesky_adaptive.agents.base import AgentConsumer | ||
from bluesky_adaptive.server import register_variable, shutdown_decorator, startup_decorator | ||
from bmm_agents.base import BMMBaseAgent | ||
|
||
from pdf_agents.monarch_bmm_subject import KMeansMonarchSubject | ||
|
||
bmm_objects = BMMBaseAgent.get_beamline_objects() | ||
|
||
|
||
# Custom Kafka Consumer Needed since we are subscribing downstream from GSAS | ||
beamline_tla = "pdf" | ||
kafka_config = nslsii.kafka_utils._read_bluesky_kafka_config_file(config_file_path="/etc/bluesky/kafka.yml") | ||
kafka_consumer = AgentConsumer( | ||
topics=[ | ||
f"{beamline_tla}.mmm.bluesky.agents", | ||
], | ||
consumer_config=kafka_config["runengine_producer_config"], | ||
bootstrap_servers=",".join(kafka_config["bootstrap_servers"]), | ||
group_id=f"echo-{beamline_tla}-{str(uuid.uuid4())[:8]}", | ||
) | ||
|
||
|
||
class Agent(KMeansMonarchSubject): | ||
def __init__(self, *args, **kwargs): | ||
self.last_time = ttime.time() | ||
self._auto_asking = False | ||
super().__init__(self, *args, **kwargs) | ||
|
||
@property | ||
def name(self): | ||
return "GSAS-Based-KMeans-PDF-Monarch-BMM-Subject" | ||
|
||
def trigger_condition(self, uid) -> bool: | ||
return self.exp_catalog.metadata["start"]["agent_name"].startswith("GSAS-Refinement-Agent") | ||
|
||
def subject_ask_condition(self): | ||
if not self._auto_asking: | ||
return False | ||
elif ttime.time() - self.last_time > 60 * 90: # 1.5 hours | ||
self.last_time = ttime.time() | ||
return True | ||
|
||
@property | ||
def auto_asking(self): | ||
return self._auto_asking | ||
|
||
def enable_auto_subject_asking(self): | ||
self.last_time = ttime.time() | ||
self._auto_asking = True | ||
|
||
def disable_auto_subject_asking(self): | ||
self._auto_asking = False | ||
|
||
def server_registrations(self) -> None: | ||
self._register_method("enable_auto_subject_asking") | ||
self._register_method("disable_auto_subject_asking") | ||
return super().server_registrations() | ||
|
||
|
||
agent = Agent( | ||
# Monarch-Subject args | ||
filename="Pt-Zr-Multimodal", | ||
exp_mode="fluorescence", | ||
exp_data_type="mu", | ||
elements=["Pt", "Ni"], | ||
edges=["L3", "K"], | ||
element_origins=[[186.307, 89.276], [186.384, 89.305]], | ||
element_det_positions=[185, 160], | ||
sample="AlPtNi wafer pretend-binary PtNi", | ||
preparation="AlPtNi codeposited on a silica wafer", | ||
exp_bounds="-200 -30 -10 25 13k", | ||
exp_steps="10 2 0.5 0.05k", | ||
exp_times="1 1 1 1", | ||
subject_qserver=bmm_objects["qserver"], | ||
subject_kafka_producer=bmm_objects["kafka_producer"], | ||
subject_endstation_key="bmm", | ||
pdf_origin=[-154.7682, 48.9615], # This is apparently an unused value, redundant with motor_origins | ||
# K means Args | ||
bounds=np.array([(-32, 32), (-32, 32)]), | ||
k_clusters=4, | ||
# PDF Args | ||
motor_names=["xstage", "ystage"], | ||
motor_origins=[-154.7682, 48.9615], | ||
# BS Adaptive Args | ||
kafka_consumer=kafka_consumer, | ||
ask_on_tell=False, | ||
report_on_tell=False, | ||
) | ||
|
||
|
||
@startup_decorator | ||
def startup(): | ||
agent.start() | ||
|
||
|
||
@shutdown_decorator | ||
def shutdown_agent(): | ||
return agent.stop() | ||
|
||
|
||
register_variable("Tell Cache", agent, "tell_cache") | ||
register_variable("Agent Name", agent, "instance_name") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""SVA agent, that finds regions of scientific value, according to GSAS Refinement Agent Output""" | ||
|
||
import uuid | ||
|
||
import nslsii | ||
import numpy as np | ||
import tiled.client.node # noqa: F401 | ||
from bluesky_adaptive.agents.base import AgentConsumer | ||
from bluesky_adaptive.server import register_variable, shutdown_decorator, startup_decorator | ||
|
||
from pdf_agents.scientific_value import ScientificValueAgentBase | ||
|
||
# Custom Kafka Consumer Needed since we are subscribing downstream from GSAS | ||
beamline_tla = "pdf" | ||
kafka_config = nslsii.kafka_utils._read_bluesky_kafka_config_file(config_file_path="/etc/bluesky/kafka.yml") | ||
kafka_consumer = AgentConsumer( | ||
topics=[ | ||
f"{beamline_tla}.mmm.bluesky.agents", | ||
], | ||
consumer_config=kafka_config["runengine_producer_config"], | ||
bootstrap_servers=",".join(kafka_config["bootstrap_servers"]), | ||
group_id=f"echo-{beamline_tla}-{str(uuid.uuid4())[:8]}", | ||
) | ||
|
||
|
||
class Agent(ScientificValueAgentBase): | ||
@property | ||
def name(self): | ||
return "GSAS-Based-SVA" | ||
|
||
def trigger_condition(self, uid) -> bool: | ||
return self.exp_catalog.metadata["start"]["agent_name"].startswith("GSAS-Refinement-Agent") | ||
|
||
|
||
agent = Agent( | ||
# SVA Args | ||
bounds=np.array([(-32, 32), (-32, 32)]), | ||
# PDF Args | ||
motor_names=["xstage", "ystage"], | ||
motor_origins=[-154.7682, 48.9615], | ||
# BS Adaptive Args | ||
kafka_consumer=kafka_consumer, | ||
ask_on_tell=False, | ||
report_on_tell=False, | ||
) | ||
|
||
|
||
@startup_decorator | ||
def startup(): | ||
agent.start() | ||
|
||
|
||
@shutdown_decorator | ||
def shutdown_agent(): | ||
return agent.stop() | ||
|
||
|
||
register_variable("Tell Cache", agent, "tell_cache") | ||
register_variable("Agent Name", agent, "instance_name") |