Skip to content

Commit

Permalink
Merge pull request #104 from jacquelinegarrahan/doc-cleanup
Browse files Browse the repository at this point in the history
Create prod example and fix controller pvaccess bug
  • Loading branch information
jacquelinegarrahan authored Jun 3, 2022
2 parents caae98b + 8d6dd6b commit 36076c1
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 4 deletions.
Empty file added examples/lcls/README.md
Empty file.
64 changes: 64 additions & 0 deletions examples/lcls/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from bokeh.io import curdoc
from bokeh import palettes
from bokeh.layouts import column, row
from bokeh.models import LinearColorMapper, Div

from lume_epics.client.controller import Controller
from lume_model.utils import variables_from_yaml
from lume_epics.utils import config_from_yaml

from lume_epics.client.widgets.plots import ImagePlot, Striptool
from lume_epics.client.widgets.tables import ValueTable
from lume_epics.client.widgets.controls import build_sliders, EntryTable
from lume_epics.client.controller import Controller
from pathlib import Path

variable_path = Path(__file__).parent / "variables.yml"

with variable_path.open() as f:
input_variables, output_variables = variables_from_yaml(f)

# load epics configuration
epics_path = Path(__file__).parent / "epics_config.yml"

with epics_path.open() as f:
epics_config = config_from_yaml(f)

# create controller from epics config
controller = Controller(epics_config)

# prepare as list for rendering
input_variables = list(input_variables.values())
output_variables = list(output_variables.values())

input_value_table = ValueTable(input_variables, controller)
output_value_table = ValueTable(output_variables, controller)

title_div = Div(
text=f"<b>LCLS ampl sum: Last update {controller.last_update}</b>",
style={
"font-size": "150%",
"color": "#3881e8",
"text-align": "center",
"width": "100%",
},
)


def update_div_text():
global controller
title_div.text = f"<b>LCLS ampl sum: Last update {controller.last_update}</b>"


# render
curdoc().title = "LCLS ampl"
curdoc().add_root(
column(
row(column(title_div)),
row(input_value_table.table, output_value_table.table),
)
)

# must add refresh callbacks
curdoc().add_periodic_callback(input_value_table.update, 250)
curdoc().add_periodic_callback(output_value_table.update, 250)
30 changes: 30 additions & 0 deletions examples/lcls/epics_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
input_variables:
klys_li24_11:
pvname: KLYS:LI24:11:AMPL
protocol: ca
serve: false

klys_li24_21:
pvname: KLYS:LI24:21:AMPL
protocol: ca
serve: false

klys_li24_31:
pvname: KLYS:LI24:31:AMPL
protocol: ca
serve: false

klys_li24_41:
pvname: KLYS:LI24:41:AMPL
protocol: ca
serve: false

klys_li24_51:
pvname: KLYS:LI24:51:AMPL
protocol: ca
serve: false

output_variables:
summation:
pvname: sample:summation
protocol: pva
40 changes: 40 additions & 0 deletions examples/lcls/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
from lume_model.models import BaseModel
from lume_model.utils import variables_from_yaml
from lume_epics.utils import config_from_yaml
from lume_epics.epics_server import Server
from pathlib import Path


class AmplSummationModel(BaseModel):
def __init__(self):

variable_path = Path(__file__).parent / "variables.yml"

with variable_path.open() as f:
input_variables, output_variables = variables_from_yaml(f)

self.input_variables = input_variables
self.output_variables = output_variables

def evaluate(self, input_variables):

summation = sum([var.value for var in input_variables.values()])
self.output_variables["summation"].value = summation

return self.output_variables


if __name__ == "__main__":
# load epics configuration
epics_path = Path(__file__).parent / "epics_config.yml"

with epics_path.open() as f:
epics_config = config_from_yaml(f)

server = Server(
AmplSummationModel,
epics_config,
)
# monitor = False does not loop in main thread
server.start(monitor=True)
29 changes: 29 additions & 0 deletions examples/lcls/variables.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
input_variables:
klys_li24_11:
type: scalar
default: 1 # will be dropped in later lume-epics iter
range: [0, 100] # will be dropped in later lume-epics iter

klys_li24_21:
type: scalar
default: 1 # will be dropped in later lume-epics iter
range: [0, 100] # will be dropped in later lume-epics iter

klys_li24_31:
type: scalar
default: 1 # will be dropped in later lume-epics iter
range: [0, 100] # will be dropped in later lume-epics iter

klys_li24_41:
type: scalar
default: 1 # will be dropped in later lume-epics iter
range: [0, 100] # will be dropped in later lume-epics iter

klys_li24_51:
type: scalar
default: 1 # will be dropped in later lume-epics iter
range: [0, 100] # will be dropped in later lume-epics iter

output_variables:
summation:
type: scalar
10 changes: 6 additions & 4 deletions lume_epics/client/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ def _ca_value_callback(self, pvname, value, *args, **kwargs):
self._last_updates[pvname] = update_datetime

def _ca_connection_callback(self, *, pvname, conn, pv):
"""Callback used for monitoring connection and setting values to None on disconnect.
"""
"""Callback used for monitoring connection and setting values to None on disconnect."""
if not conn:
self._pv_registry[pvname]["value"] = None

Expand Down Expand Up @@ -208,7 +207,7 @@ def get(self, pvname: str, root: str = None) -> np.ndarray:
if pv:
val = pv["value"]
if val is None:
if protocol:
if protocol == "ca":
val = pv["pv"].get()

elif protocol == "pva":
Expand Down Expand Up @@ -438,7 +437,10 @@ def put_image(
logger.debug(f"No initial value set for {pvname}.")

def put_array(
self, varname, array: np.ndarray = None, timeout: float = 1.0,
self,
varname,
array: np.ndarray = None,
timeout: float = 1.0,
) -> None:
"""Assign the value of an array process variable. Allows updates to individual attributes.
Expand Down

0 comments on commit 36076c1

Please sign in to comment.