OBE_Server class

class optbayesexpt.obe_server.OBE_Server(initial_args=(), ip_address='', 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 the run() method.

  • initial_args (tuple) –

    Requires a tuple with the following structure:

    1. Model_function (python function)

    2. Settings (tuple of settings arrays)

    3. Parameter samples (tuple of parameter sample arrays)

    4. Constants (tuple of constants)

  • ip_address (str) – an IP address for TCP communications. Default ‘’.

  • 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__``) –


The disembodied brains of the experimental design scheme. OBE_server provides TCP communication between obe_engine and a client program.




Stored samples from an initial parameter distribution. Initialized by self.__init__() and self.make_obe().




keyword arguments stored for future OBE_class intantiation.




  1. 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.

  2. In pre-v1.0.0 version OBE_Server was a child class of OptBayesExpt. In versions 1.0.0 and later, the OBE_class is an attribute of OBE_Server. The advantage of the new arrangement is that OBE_Server can manage a sequence of experimental runs with differently configured OptBayesExpt 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.

  • obe_class (OptBayesExpt-like) – e.g. optbayesexpt.OptBayesExpt without parentheses

  • class_args (tuple) – the arguments to the obe_class. For example, (model_function, settings, parameters, cons).


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 the self argument, and with any information the user chooses to send from the client program, there are many more possibilities.

  • self (optbayesexpt.OBE_Server) – provides access to the attributes of self and also its OptBayesExpt attribute.

  • message (tuple) – User defined information passed from the client program.

Returns: User defined.


Listens and responds to TCP messages

Enters a continuous loop, interpreting incoming messages as python dict objects and responding to command strings found in message[ "command"]. Only one command string is allowed in each message. Valid command strings are listed below.


Stops the OBE_server and allows the server script to complete.

{"command": "done"}


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 of OBE_Server may conflict.


Reports the OptBayesExpt.cons attribute. See also getset and getpar

{"command": "getcon"}

Reply: a list of model constants.


Reports the covariance matrix of the parameter distribution as a JSON array of arrays.

{"command": "getcov"}

Reply: JSON formatted array of arrays


Reports the mean value of the parameter distribution as a JSON array. See also getstd and getcov

{"command": "getmean"}

Reply: JSON formatted array.


Reports the OptBayesExpt.parameters arrays representing samples from the probability distribution.

{"command": "getset"}

Reply: a list of parameter value lists.


Reports the OptBayesExpt.sets attribute

{"command": "getset"}

Reply: a list of setting value lists.


Reports the standard deviation of the parameter distribution as a JSON array.

{"command": "getstd"}

Reply: JSON formatted array


Reports the particle weights of the probability distribution as a JSON array.

{"command": "getwgt"}

Reply: particle weights as a JSON formatted array


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.


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.


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.


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.


Returns ‘OK’. Useful for checking communication.