Skip to content

Commit

Permalink
Merge pull request #5 from NSLS-II-PDF/offline-testing
Browse files Browse the repository at this point in the history
Offline testing
  • Loading branch information
maffettone authored Dec 11, 2023
2 parents 9538455 + a7ec73c commit 33bdf32
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pdf_agents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from bluesky_queueserver_api.http import REManagerAPI
from numpy.typing import ArrayLike

from .utils import OfflineKafka
from .utils import OfflineKafka, OfflineRedis

logger = getLogger(__name__)

Expand All @@ -31,13 +31,10 @@ def __init__(
offline=False,
**kwargs,
):
self._rkvs = redis.Redis(host="info.pdf.nsls2.bnl.gov", port=6379, db=0) # redis key value store
self._motor_name = motor_name
self._motor_resolution = motor_resolution
self._data_key = data_key
self._roi_key = roi_key
self._roi = roi
# Attributes pulled in from Redis
if offline:
self._rkvs = OfflineRedis()
else:
self._rkvs = redis.Redis(host="info.pdf.nsls2.bnl.gov", port=6379, db=0) # redis key value store
self._exposure = float(self._rkvs.get("PDF:desired_exposure_time").decode("utf-8"))
self._sample_number = int(self._rkvs.get("PDF:xpdacq:sample_number").decode("utf-8"))
self._background = np.array(
Expand All @@ -46,6 +43,14 @@ def __init__(
ast.literal_eval(self._rkvs.get("PDF:bgd:y").decode("utf-8")),
]
)

self._motor_name = motor_name
self._motor_resolution = motor_resolution
self._data_key = data_key
self._roi_key = roi_key
self._roi = roi
# Attributes pulled in from Redis

if offline:
_default_kwargs = self.get_offline_objects()
else:
Expand Down Expand Up @@ -245,7 +250,7 @@ def get_offline_objects() -> dict:
offline_kafka = OfflineKafka()
try:
node = tiled.client.from_profile(f"{beamline_tla}_bluesky_sandbox")
except tiled.profile.ProfileNotFound:
except tiled.profiles.ProfileNotFound:
node = None

return dict(
Expand Down
150 changes: 150 additions & 0 deletions pdf_agents/offline_testing/ask_agent_from_df.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example agent invesitgation\n",
"This mimics the scripts in `pdf_agents/startup_scripts` with minimal and perhaps broken functionality due to a lack of redis, tiled, kafka, etc. \n",
"\n",
"It doesn't look at what an **exact** agent **did**, but what a type of agent **might do**.\n",
"\n",
"Recreations of other agents could be attempted by following a similar approach. As such multiple agents are shown in the cells below. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"sys.path.append(\"../../\")\n",
"import pandas as pd\n",
"import numpy as np\n",
"from pdf_agents.scientific_value import ScientificValueAgentBase\n",
"from pdf_agents.sklearn import ActiveKmeansAgent"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Choose your fighter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### GP driven Baysian optimization of a scientific value function ###\n",
"# agent = ScientificValueAgentBase(bounds=[-12.0, 46.0], ask_on_tell=False, report_on_tell=False, offline=True)\n",
"### GP driven Baysian optimization of a scientific value function ###\n",
"\n",
"### KMeans driven selection of interesting points for XAFS beamline. ###\n",
"## This skips over the MonarchSubject businsiness, and uses the same approach for driving a single beamline\n",
"agent = ActiveKmeansAgent(\n",
" k_clusters=4, bounds=[-12.0, 46.0], ask_on_tell=False, report_on_tell=False, offline=True\n",
")\n",
"### KMeans driven selection of interesting points for XAFS beamline. ###"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load and tell the agent about data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_pickle(\"~/Downloads/sim_pdf_df_020pts.pckl\")\n",
"independent_vars = df.columns.to_numpy()\n",
"observables = df.to_numpy().T"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"agent.tell_many(independent_vars, observables)\n",
"for x in independent_vars:\n",
" # This is done because the tell is happening without UIDs.\n",
" agent.tell_cache.append(x)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ask the agent what it would do next\n",
"\n",
"- Kmeans agents can suggest multiple points\n",
"- The GP Bayesopt agents are designed to suggest one at a time. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# docs, suggestions = agent.ask(1) # The argument here is batch size/number of suggestions\n",
"docs, suggestions = agent.ask(4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"docs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"suggestions"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
9 changes: 9 additions & 0 deletions pdf_agents/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from pydantic import parse_obj_as


class OfflineKafka:
Expand All @@ -18,6 +19,14 @@ def stop(self, *args, **kwargs):
pass


class OfflineRedis:
def get(self, *args, **kwargs):
return b"0"

def set(self, *args, **kwargs):
return b"0"


def discretize(value: np.typing.ArrayLike, resolution: np.typing.ArrayLike):
return np.floor(value / resolution)

Expand Down

0 comments on commit 33bdf32

Please sign in to comment.