pytweezer.analysis.analysis_base module

Base classes for streaming analysis processes.

An analysis process is a small, long-lived program that subscribes to one or more pub/sub streams, receives (message, head, data) messages in a loop, transforms each one, and republishes the result on its own name. The existing scripts in this package (data_average.py, imageselector.py, subtract_bg.py, rotate_image.py) all hand-roll the same skeleton:

  • hold a Properties connection named after the process,

  • declare the input streams as a PropertyAttribute,

  • create a client, subscribe, then loop recv -> transform -> send.

This module factors that skeleton into AnalysisProcess, and captures the only real differences between image and data analyses in two subclasses:

  • ImageAnalysis – uses an ImageClient, subscribes to the imagestreams property, and republishes on channel '_'.

  • DataAnalysis – uses a DataClient, subscribes to the datastreams property, and republishes on the default channel.

Both clients publish with the same client.send(header, data, channel=...) order, so AnalysisProcess.send_result() is shared by both subclasses.

To write a new analysis, subclass one of the two and override process():

class ImageSlice(ImageAnalysis):
    _index = PropertyAttribute('SelectedImage', 1)

    def process(self, head, data):
        if head['_imgindex'] == self._index:
            return head, data      # publish this one
        return None                # drop everything else

if __name__ == '__main__':
    run_analysis(ImageSlice)
class pytweezer.analysis.analysis_base.AnalysisProcess(name)[source]

Bases: object

Common skeleton for a streaming analysis process.

Subclasses supply the transport-specific pieces via the three hooks make_client(), send_result() and the _streams property, and the analysis-specific behaviour by overriding process().

Attributes set up by __init__:

name – the process name (also the Properties namespace / title). _props – the Properties connection. client – the pub/sub client (an ImageClient or DataClient).

make_client(name)[source]

Return the pub/sub client for this analysis (subscribing publisher).

process(head, data)[source]

Transform one received message.

Parameters:
  • head – the message header dict.

  • data – the payload (numpy array for images/data).

Returns:

(head, data) to republish, or None to drop the message. The default is a pass-through.

run()[source]
send_result(head, data)[source]

Publish one transformed (head, data) result on self.client.

class pytweezer.analysis.analysis_base.DataAnalysis(name)[source]

Bases: AnalysisProcess

Analysis process that consumes and republishes a data stream.

make_client(name)[source]

Return the pub/sub client for this analysis (subscribing publisher).

class pytweezer.analysis.analysis_base.ImageAnalysis(name)[source]

Bases: AnalysisProcess

Analysis process that consumes and republishes an image stream.

make_client(name)[source]

Return the pub/sub client for this analysis (subscribing publisher).

pytweezer.analysis.analysis_base.run_analysis(analysis_cls, argv=None)[source]

Standard main for an analysis script.

Parses the single name argument the process launcher passes and runs an instance of analysis_cls:

if __name__ == '__main__':
    run_analysis(MyAnalysis)