OBE_Server class¶
- class optbayesexpt.obe_server.OBE_Server(initial_args=(), ip_address='127.0.0.1', port=61981, **kwargs)[source]¶
Bases:
Socket
A TCP socket interface for OptBayesExpt using JSON strings
This class provides communication between an OptBayesExpt object and user software running in a separate process. Instrumentation software is not always written in python; the idea of this class is to allow experiments to use OptBayesExpt from their native languages by issuing command messages in JSON
object
format through TCP sockets. The available commands are documented below in therun()
method.- Parameters
initial_args (
tuple
) –Requires a tuple with the following structure:
Model_function (python function)
Settings (tuple of settings arrays)
Parameter samples (tuple of parameter sample arrays)
Constants (tuple of constants)
ip_address (
str
) – an IP address for TCP communications. Default ‘127.0.0.1’.port (
int
) – a TCP port number to use for communications. Default 61981.
- Keyword Arguments
function. (**kwargs are passed to the specified OBE_class's `` __init__``) –
- obe_engine¶
The disembodied brains of the experimental design scheme.
OBE_server
provides TCP communication betweenobe_engine
and a client program.- Type
OptBayesExpt
- initial_args¶
Stored samples from an initial parameter distribution. Initialized by
self.__init__()
andself.make_obe()
.- Type
tuple
- initial_kwargs¶
keyword arguments stored for future OBE_class intantiation.
- Type
dict
Notes
The messages sent between the client and this server are expected to be JSON strings, each prependend by the string length formatted as a 10-digit number. See the obe_socket module docs for details. Unless otherwise stated the server will reply with
'0000000004"OK"'
upon successful completion of the command.In pre-v1.0.0 version
OBE_Server
was a child class ofOptBayesExpt
. In versions 1.0.0 and later, theOBE_class
is an attribute ofOBE_Server
. The advantage of the new arrangement is that OBE_Server can manage a sequence of experimental runs with differently configuredOptBayesExpt
objects.
- make_obe(obe_class, class_args, **kwargs)[source]¶
Creates and attaches a new OptBayesExpt-like object
A server may need to handle several runs. This function allows OBE_Server to instantiate new OptBayesExpt objects from scratch. Enables a server to start a new experimental run with modified starting conditions.
- Parameters
obe_class (
OptBayesExpt
-like) – e.g.optbayesexpt.OptBayesExpt
without parenthesesclass_args (
tuple
) – the arguments to the obe_class. For example,(model_function, settings, parameters, cons)
.
- newrun(message)[source]¶
A stub to allow customized TCP commands
Invoked by the
run
method when a message with a'newrun'
command string is received. The idea is to provide flexible control from the experiment program. The original intent was to provide a way for the user program to start a fresh measurement, perhaps with different setting ranges or a different prior. However, with access to the OBE_Server and its OptBayesExpt through theself
argument, and with any information the user chooses to send from the client program, there are many more possibilities.- Parameters
self (
optbayesexpt.OBE_Server
) – provides access to the attributes ofself
and also its OptBayesExpt attribute.message (
tuple
) – User defined information passed from the client program.
Returns: User defined.
- run()[source]¶
Listens and responds to TCP messages
Enters a continuous loop, interpreting incoming messages as python
dict
objects and responding to command strings found inmessage[ "command"]
. Only one command string is allowed in each message. Valid command strings are listed below.- ‘done’
Stops the
OBE_server
and allows the server script to complete.{"command": "done"}
Warning
It may be important for the client to issue the
done
command. If the server is allowed to run, it will continue to use the TCP socket it was assigned during initialization. Later instances ofOBE_Server
may conflict.- ‘getcon’
Reports the
OptBayesExpt.cons
attribute. See also getset and getpar{"command": "getcon"}
Reply: a list of model constants.
- ‘getcov’
Reports the covariance matrix of the parameter distribution as a JSON array of arrays.
{"command": "getcov"}
Reply: JSON formatted array of arrays
- ‘getmean’
Reports the mean value of the parameter distribution as a JSON array. See also getstd and getcov
{"command": "getmean"}
Reply: JSON formatted array.
- ‘getpar’
Reports the
OptBayesExpt.parameters
arrays representing samples from the probability distribution.{"command": "getset"}
Reply: a list of parameter value lists.
- ‘getset’
Reports the
OptBayesExpt.sets
attribute{"command": "getset"}
Reply: a list of setting value lists.
- ‘getstd’
Reports the standard deviation of the parameter distribution as a JSON array.
{"command": "getstd"}
Reply: JSON formatted array
- ‘getwgt’
Reports the particle weights of the probability distribution as a JSON array.
{"command": "getwgt"}
Reply: particle weights as a JSON formatted array
- ‘goodset’
Generated measurement settings. Calculates the utility of possible settings and reports a random selection that is weighted by the utility. Invokes
OptBayesExpt.good_setting()
. See also optset.{"command": "goodset"[, "pickiness", <integer>]}
Reply: a list of setting values.
- ‘newdat’
Processes measurement results. Refines the parameter probability distribution based on the likelihood of measurement results reported in the command. Invokes
OptBayesExpt.pdf_update()
.{"command": "newdat", "x": <settings tuple>, "y": <measured values tuple>, "s": <uncertainty tuple>}
Required components include:
“x”: a tuple containing the measurement settings.
“y”: a tuple of measurement mean values.
“s”: uncertainty of the measurement as a standard deviation.
- ‘newrun’
A command string to run the user-defined
OBE_Server.newrun()
method. The message string must conform to the 10 digits + JSON object format, and it must avoid using other defined command strings. Except for those restrictions, any<keyword>: <string>
pairs are allowed.{"command": "newrun"[, ...]}
See the
newrun()
method documentation, above.- ‘optset’
Requests optimal measurement settings. Calculates the utility of possible settings and reports the settings having the maximum utility. Invokes
OptBayesExpt.opt_setting()
. See also goodset.{"command": "optset"}
Reply: a list of setting values.
- ‘ready’
Returns ‘OK’. Useful for checking communication.