Beispiel #1
0
def subscriber(func, topic, event, *args, **kwargs):
    port = 5555
    if 'port' in kwargs:
        port = kwargs['port']
        del kwargs['port']

    logger = get_logger(name=__name__)
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.connect("tcp://localhost:%s" % port)
    sleep(1)  # Takes a while for TCP connections to propagate
    topic_filter = topic.encode('ascii')
    socket.setsockopt(zmq.SUBSCRIBE, topic_filter)
    logger.info('Subscribing {} to {}'.format(func.__name__, topic))
    while not event.is_set():
        topic = socket.recv_string()
        data = socket.recv_pyobj()  # flags=0, copy=True, track=False)
        logger.debug('Got data of type {} on topic: {}'.format(
            type(data), topic))
        if isinstance(data, str):
            logger.debug('Data: {}'.format(data))
            if data == 'stop':
                logger.debug('Stopping subscriber on method {}'.format(
                    func.__name__))
                break

        func(data, *args, **kwargs)
    sleep(
        1
    )  # Gives enough time for the publishers to finish sending data before closing the socket
    socket.close()
    logger.debug('Stopped subscriber {}'.format(func.__name__))
Beispiel #2
0
    def __init__(self, filename=None):
        super().__init__()  # Initialize base class
        self.logger = get_logger(name=__name__)

        self.worker_saver_queue = Queue()

        self.load_configuration(filename)

        self.dropped_frames = 0
        self.keep_acquiring = True
        self.acquiring = False  # Status of the acquisition
        self.camera = None  # This will hold the model for the camera
        self.current_height = None
        self.current_width = None
        self.max_width = None
        self.max_height = None
        self.background = None
        self.temp_image = None  # Temporary image, used to quickly have access to 'some' data and display it to the user
        self.movie_buffer = None  # Holds few frames of the movie in order to be able to do some analysis, save later, etc.
        self.last_index = 0  # Last index used for storing to the movie buffer
        self.stream_saving_running = False
        self.async_threads = []  # List holding all the threads spawn
        self.stream_saving_process = None
        self.do_background_correction = False
        self.background_method = self.BACKGROUND_SINGLE_SNAP
        self._stop_evet = Event()
        self.waterfall_index = 0

        self.locations_queue = Queue()
Beispiel #3
0
def link_queue(locations_queue, publisher_queue, links_queue, **kwargs):
    """ Links the locations of particles from a location queue.
    It is a reimplementation of the link_iter of trackpy.
    """
    logger = get_logger(name=__name__)
    logger.info('Starting to create trajectory links from queue')
    t = 0  # First frame
    search_range = kwargs['search_range']
    del kwargs['search_range']
    linker = Linker(search_range, **kwargs)
    while True:
        if not locations_queue.empty() or locations_queue.qsize() > 0:
            locations = locations_queue.get()
            if isinstance(locations, str):
                logger.debug('Got string on coordinates')
                break

            coords = np.vstack((locations['x'], locations['y'])).T
            if t == 0:
                logger.debug('First set of coordinates')
                linker.init_level(coords, t)
            else:
                logger.debug('Processing frame {}'.format(t))
                linker.next_level(coords, t)
            logger.debug("Frame {0}: {1} trajectories present.".format(t, len(linker.particle_ids)))
            t += 1
            locations['particle'] = linker.particle_ids
            locations['frame'] = t
            publisher_queue.put({'topic': 'particle_links', 'data': [locations, linker.particle_ids]})
            links_queue.put(locations)
    logger.info('Stopping link queue trajectories')
 def __init__(self):
     self.config = {
     }  # Dictionary storing the configuration of the experiment
     self.logger = get_logger(name=__name__)
     self._threads = []
     self.publisher = Publisher()
     self.publisher.start()
     self._connections = []
     self.subscriber_events = []
Beispiel #5
0
    def __init__(self, filename=None):
        super().__init__(filename)
        self.free_run_running = False
        self.saving_location = False
        self.logger = get_logger(name=__name__)

        self.dropped_frames = 0
        self.keep_acquiring = True
        self.acquiring = False  # Status of the acquisition
        self.tracking = False

        self.monitoring_pixels = False
        self.monitor_callback = self.callback_pixel
        self.monitor_callback = self.callback_local_max_and_lock
        self.temp_monitor_values = {}

        self.camera = None  # This will hold the model for the camera
        self.current_height = None
        self.current_width = None
        self.max_width = None
        self.max_height = None
        self.background = np.array(())
        self.temp_image = None  # Temporary image, used to quickly have access to 'some' data and display it to the user
        # self.temp_locations = None
        self.movie_buffer = None  # Holds few frames of the movie in order to be able to do some analysis, save later, etc.
        self.last_index = 0  # Last index used for storing to the movie buffer
        self.stream_saving_running = False
        self.async_threads = []  # List holding all the threads spawn
        self.stream_saving_process = None
        self.link_particles_process = None
        self.calculate_histogram_process = None
        self.do_background_correction = False
        self.background_method = self.BACKGROUND_SINGLE_SNAP
        self.last_locations = None

        self.waterfall_index = 0

        self.locations_queue = Queue()
        self.tracks_queue = Queue()
        self.size_distribution_queue = Queue()
        self.saver_queue = Queue()
        self.keep_locating = True
        self._threads = []
        self._processes = []
        self._stop_free_run = Event()

        #self.location = LocateParticles(self.publisher, self.config['tracking'])
        self.fps = 0  # Calculates frames per second based on the number of frames received in a period of time
Beispiel #6
0
def calculate_locations_image(image, publisher_queue, locations_queue, **kwargs):
    """ Calculates the positions of the particles on an image. It used the trackpy package, which may not be
    installed by default.
    """
    if not 'diameter' in kwargs:
        raise DiameterNotDefined('A diameter is mandatory for locating particles')

    diameter = kwargs['diameter']
    del kwargs['diameter']
    image = image[1]  # image[0] is the timestamp of the frame
    logger = get_logger(name=__name__)
    logger.debug('Calculating positions on image')

    logger.debug('Calculating positions with trackpy')
    locations = tp.locate(image, diameter, **kwargs)
    logger.debug('Got {} locations'.format(len(locations)))
    publisher_queue.put({'topic': 'trackpy_locations', 'data': locations})
    locations_queue.put(locations)
Beispiel #7
0
    def __init__(self, publisher, config):
        self._accumulate_links_event = Event()
        self.publisher = publisher
        self._tracking_process = None
        self._tracking_event = Event()
        self._linking_process = None
        self._linking_event = Event()
        self._saving_process = None
        self._saving_event = Event()
        self.config = config

        self.locations = DataFrame()
        self.particle_ids = None

        self._threads = []

        self.logger = get_logger(name=__name__)
        self.logger.info('Initialized locate particles')
Beispiel #8
0
import importlib

from pynta.util import get_logger

logger = get_logger(name=__name__)


def load_camera_module(name):
    try:
        logger.info('Importing camera model {}'.format(name))
        logger.debug('pynta.model.cameras.' + name)
        camera_model_to_import = 'pynta.model.cameras.' + name
        cam_module = importlib.import_module(camera_model_to_import)
    except ModuleNotFoundError:
        logger.error('The model {} for the camera was not found'.format(name))
        raise
    return cam_module


def instantiate_camera(config: dict):
    cam_module = load_camera_module(config['model'])
    cam_init_arguments = config['init']
    if 'extra_args' in config:
        logger.info('Initializing camera with extra arguments')
        logger.debug('cam_module.camera({}, {})'.format(cam_init_arguments, config['extra_args']))
        camera = cam_module.Camera(cam_init_arguments, *config['extra_args'])
    else:
        logger.info('Initializing camera without extra arguments')
        logger.debug('cam_module.camera({})'.format(cam_init_arguments))
        camera = cam_module.Camera(cam_init_arguments)
Beispiel #9
0
def test_func(data, queue):
    logger = get_logger(name=__name__)
    logger.info('Putting data to queue')
    queue.put(data)
Beispiel #10
0
import logging
from time import sleep, time

import numpy as np
from multiprocessing import Process, Queue

from pynta.model.experiment.publisher import publisher
from pynta.model.experiment.subscriber import subscriber
from pynta.util import get_logger

logger = get_logger(name='pynta.model.experiment.subscriber'
                    )  # 'pynta.model.experiment.nano_cet.saver'
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

num_data_frames = 1000

data = np.random.randint(0, high=2**8, size=(1000, 1000), dtype=np.uint8)
recv_q = Queue()  # Queue where data will be received
send_q = Queue()
for i in range(num_data_frames):
    send_q.put({'topic': 'test', 'data': data})
send_q.put({'topic': 'test', 'data': 'stop'})
send_q.put({'topic': '', 'stop_pub': 'stop_pub'})

Beispiel #11
0
import logging
from multiprocessing import freeze_support
from time import sleep

from pynta.model.experiment.base_experiment import BaseExperiment
from pynta.util import get_logger

logger = get_logger(
)  #'nanoparticle_tracking.model.experiment.nanoparticle_tracking.worker_saver')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

if __name__ == "__main__":
    freeze_support()
    with BaseExperiment() as exp:

        sleep(5)
Beispiel #12
0
def add_linking_queue(data, queue):
    logger = get_logger(__name__)
    logger.debug('Adding data frame to linking queue')
    queue.put(data)
Beispiel #13
0
# -*- coding: utf-8 -*-
"""
    Dummy DAQ
    =========
    DAQ model for testing GUI and other functionalities. Based on the skeleton. It does not interact with any real
    device, it just generates random data and accepts inputs which have no effect.

    :copyright:  Aquiles Carattino <*****@*****.**>
    :license: GPLv3, see LICENSE for more details
"""
from pynta.util import get_logger
from .skeleton import DaqBase

logger = get_logger(__name__)


class DAQDummy(DaqBase):
    def __init__(self, dev_number=None):
        super().__init__(dev_number)
        logger.info('Initialized device with number: %s' % dev_number)
        self.test_value = 0

    def triggerAnalog(self, conditions):
        """Triggers an analog measurement. It does not read the value.
        conditions -- a dictionary with the needed parameters for an analog acquisition.
        """
        pass

    def getAnalog(self, conditions):
        """Gets the analog values acquired with the triggerAnalog function.
        conditions -- dictionary with the number of points ot be read
Beispiel #14
0
import logging
from multiprocessing import freeze_support
from time import sleep

from pynta.model.experiment.base_experiment import BaseExperiment
from pynta.util import get_logger

logger = get_logger()#'pynta.model.experiment.nano_cet.worker_saver')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)


if __name__ == "__main__":
    freeze_support()
    with BaseExperiment() as exp:

        sleep(5)