Skip to content

Commit

Permalink
feat: add SVA off peakfit service
Browse files Browse the repository at this point in the history
  • Loading branch information
maffettone committed Apr 12, 2024
1 parent 94d0740 commit db83812
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
42 changes: 42 additions & 0 deletions containers/mmm5-tax-day-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,44 @@ services:
interval: 20s
timeout: 10s
retries: 5

sva-peakfit-agent:
image: gsas:conda
command: conda run -n GSASII --no-capture-output uvicorn bluesky_adaptive.server:app --host 0.0.0.0 --root-path /sva-peakfit-agent
environment:
- TILED_API_KEY=$TILED_API_KEY
- HTTPSERVER_API_KEY=$HTTPSERVER_API_KEY
- BS_AGENT_STARTUP_SCRIPT_PATH=/src/pdf-agents/pdf_agents/startup_scripts/mmm5-tax-day/sva-peakfit.py
volumes:
- type: bind
source: ../
target: /src/pdf-agents/
read_only: true
- type: bind
source: /etc/bluesky/kafka.yml
target: /etc/bluesky/kafka.yml
read_only: true

sva-peakfit-ui:
image: ghcr.io/maffettone/bluesky-adaptive-ui:latest
entrypoint: /modify_dns.sh
command: python3 /src/bluesky-adaptive-ui/bluesky_adaptive_ui/default_dash_app/app.py --agent-address sva-peakfit-agent --agent-port 8000
environment:
- DASH_REQUEST_PATHNAME_PREFIX=/sva-peakfit/
volumes:
- ./modify_dns.sh:/modify_dns.sh
- type: bind
source: ../../bluesky-adaptive-ui
target: /src/bluesky-adaptive-ui
read_only: true
depends_on:
sva-peakfit-agent:
condition: service_started
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8050"]
interval: 20s
timeout: 10s
retries: 5

proxy:
image: docker.io/nginx
Expand Down Expand Up @@ -196,6 +234,10 @@ services:
condition: service_started
kmeans-peakfit-ui:
condition: service_healthy
sva-peakfit-agent:
condition: service_started
sva-peakfit-ui:
condition: service_healthy
restart: always


2 changes: 2 additions & 0 deletions containers/nginx/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ <h2>Available Agents</h2>
<!-- Link to the Peak Fit service -->
<li><a href="/peakfit-agent/docs">Peak Fit Service (FastAPI)</a></li>
<li><a href="/kmeans-peakfit-agent/docs">Kmeans Peakfit Service (FastAPI)</a></li>
<li><a href="/sva-peakfit-agent/docs">Scientific Value Agent (SVA) Peakfit Service (FastAPI)</a></li>
<!-- Add more services here -->
<!-- Example: <li><a href="/your-service/">Your Service Name</a></li> -->
</ul>
Expand All @@ -83,6 +84,7 @@ <h2>Available Agent User Interfaces</h2>
<!-- Link to the Peakfit service -->
<li><a href="/peakfit/">Peakfit UI</a></li>
<li><a href="/kmeans-peakfit/">Kmeans Peakfit UI</a></li>
<li><a href="/sva-peakfit/">Scientific Value Agent (SVA) Peakfit UI</a></li>
<!-- Add more services here -->
<!-- Example: <li><a href="/your-service/">Your Service Name</a></li> -->
</ul>
Expand Down
16 changes: 16 additions & 0 deletions containers/nginx/locs.d/sva-peakfinding.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
location /sva-peakfit/ {
proxy_pass http://sva-peakfit-ui:8050/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 60s;
}

location /sva-peakfit-agent/ {
proxy_pass http://sva-peakfit-agent:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 60s;
}

74 changes: 74 additions & 0 deletions pdf_agents/startup_scripts/mmm5-tax-day/sva-peakfit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""SVA agent, that finds regions of scientific value, according to Peak Fit 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 "Peakfit-Based-SVA"

def trigger_condition(self, uid) -> bool:
return self.exp_catalog[uid].metadata["start"]["agent_name"].startswith("Peak-Fit-Agent")

def unpack_run(self, run):
data = run.report.data
x = data["raw_independent_variable"].read().flatten()
y = np.concatenate(
[
data[key].read().flatten()
for key in [
"peak_amplitudes",
"peak_positions",
"peak_fwhms",
]
]
)
return x, y


agent = Agent(
# SVA Args
bounds=np.array([(-30, 30), (-30, 30)]),
# PDF Args
motor_names=["xstage", "ystage"],
motor_origins=[-128.85, 49.91],
# BS Adaptive Args
kafka_consumer=kafka_consumer,
ask_on_tell=True,
report_on_tell=True,
)


@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")

0 comments on commit db83812

Please sign in to comment.