def init(self, serial_number, settings=1):
        from numpy import zeros, nan
        print('')
        print('-------------INITIALIZING NEW CAMERA-----------')
        import PySpin
        from circular_buffer_numpy.queue import Queue
        from circular_buffer_numpy.circular_buffer import CircularBuffer
        self.acquiring = False

        self.cam = self.find_camera(serial_number=serial_number)
        self.nodes = self.get_nodes()

        self.height = self.get_height()
        self.width = self.get_width()
        self.queue = Queue((620, self.height + 1, self.width), dtype='uint16')
        self.analysis_buffer = CircularBuffer(shape=(100, 20), dtype='float64')
        self.last_image_time_buffer = CircularBuffer(shape=(6000, 20),
                                                     dtype='float64')
        self.last_image_time_buffer.buffer = self.last_image_time_buffer.buffer * nan
        self.timestamp_buffer = CircularBuffer(shape=(6000, 1),
                                               dtype='float64')
        self.last_image = zeros((self.height + 1, self.width), dtype='uint16')
        self.set_lut_enable(False)
        self.set_gamma_enable(False)
        #self.set_gamma(0.5)
        self.set_autoexposure('Off')
        self.set_autogain('Off')

        self.configure_transport()
        try:
            self.cam.AcquisitionStop()
            print("Acquisition stopped")
        except PySpin.SpinnakerException as ex:
            print("Acquisition was already stopped")

        print('setting pixel format Mono11Packed')
        self.cam.PixelFormat.SetValue(PixelFormat_Mono12p)

        self.set_exposure_mode('Timed')
        self.exposure_time = 63000  #53500
        self.gain = 0
        self.black_level = 15
        self.background = zeros((self.height + 1, self.width))
        self.background[:self.height, :] = 15
        self.background_flag = True

        self.conf_acq_and_trigger(settings=settings)
Example #2
0
    def init(self,
             pump_id=None,
             speed=None,
             backlash=None,
             orientation=None,
             volume=None):
        """
        initialize the device level code

        - initializes the driver
        - sets default valve position to 'o'
        - sets initial position to 0.0
        - sets initial speed to input speed value

        Parameters
        ----------
        pump_id: integer
            pump_id
        speed: float
            initial speed of the syringe pump, default is 25
        backlash: float
            the backlash of the syringe pump. The default value is 100
        orientation: string
            the orientation of the syringe pump valve: Y or Z
        volume: float
            the volume of the installed syringe

        Returns
        -------

        Examples
        --------
        >>> device.init(pump_id = 1, speed = 25, backlash = 100,orientation = 'Y', volume = 250)
        """
        from threading import RLock
        self.lock = RLock()

        from circular_buffer_numpy.circular_buffer import CircularBuffer
        self.buffers = {}
        self.buffers['position'] = CircularBuffer(shape=(1 * 3600 * 2, 2),
                                                  dtype='float64')

        from syringe_pump.driver import Driver
        self.pump_id = pump_id
        self.driver = Driver()
        self.name = 'NIH_syringe_pump_' + str(pump_id)
        self.prefix = 'NIH:SYRINGE' + str(pump_id)
        if pump_id is not None:
            self.driver.init(pump_id,
                             speed=speed,
                             backlash=backlash,
                             orientation=orientation,
                             volume=volume)
            self.speed = speed
            self.cmd_position = 0.0
            self.set_valve(b'o')
            self.set_speed(speed)
Example #3
0
 def configure_device(self):
     self.driver.stop_scan()
     from circular_buffer_numpy.circular_buffer import CircularBuffer
     self.buffer = CircularBuffer(shape=(self.buffer_size,
                                         len(self.scan_lst)),
                                  dtype='int16')  #4320000
     self.buffer.packet_length = self.packet_length
     print(self.scan_lst, self.phys_ch_lst, self.gain_lst)
     self.dev.config_channels(scan_lst=self.scan_lst,
                              phys_ch_lst=self.phys_ch_lst,
                              gain_lst=self.gain_lst)
Example #4
0
 def __init__(self, driver):
     """
     initialization of the instance and creation of all other instances and variables
     """
     from circular_buffer_numpy.circular_buffer import CircularBuffer
     self.buffer = CircularBuffer(shape=(1000, 4))
     self.dt = 1
     self.running = False
     self.header = ['time', 'cpu', 'memory', 'battery']
     self.io_push_queue = None
     self.io_put_queue = None
     self.driver = driver
     self.threads = {}
Example #5
0
def read_dict(filename):
    from ubcs_auxiliary.save_load_object import load_from_file
    return load_from_file(filename)


def func(i, flow, ratio):
    flow = flow
    ratio = ratio
    buffer.buffer.fill(0)
    buffer.reset()
    inject(pumpa=p3, pumpf=p1, tau=0.0, flow=flow, ratio=ratio, N=7, t=60)
    plot_2(buffer, 60, bck, comments=f'exp {i} flow {flow} ratio {ratio}')


from circular_buffer_numpy.circular_buffer import CircularBuffer
buffer = CircularBuffer(shape=(100, 3, 1360, 120))


def load_buffer(filename):
    from ubcs_auxiliary.save_load_object import load_from_file
    data = load_from_file(filename)


if __name__ == '__main__':
    from tempfile import gettempdir
    import logging
    logging.basicConfig(filename=gettempdir() + '/scripts.log',
                        level=logging.DEBUG,
                        format="%(asctime)s %(levelname)s: %(message)s")
    from sys import platform
    if platform == "linux" or platform == "linux2":
Example #6
0
from circular_buffer_numpy import __version__
from circular_buffer_numpy.circular_buffer import CircularBuffer

from numpy import random, asarray, where, savetxt
from matplotlib import pyplot as plt
import timeit

print('circular buffer numpy version: {}'.format(__version__))
data_dim = 1000
buffer = CircularBuffer(shape=(1000000, data_dim))
data = random.randint(2**16, size=(1000, data_dim))


def write():
    global buffer
    buffer.append(data)


def read():
    global buffer
    data = buffer.get_last_N(N)
    return data


header = ['N', 'data_dim', 'number', 'write time', 'read time']
number_lst = [1000000, 100000, 10000, 1000, 100]
N_lst = [1, 10, 100, 1000, 10000]
data_dim_lst = [10, 100, 1000]
t_lst = []
for data_dim in data_dim_lst:
    buffer = CircularBuffer(shape=(1000000, data_dim))
Example #7
0
    def init(self, serial_number, settings=1):
        from numpy import zeros, nan, ones
        info('')
        info('-------------INITIALIZING NEW CAMERA-----------')
        import PySpin
        from circular_buffer_numpy.queue import Queue

        self.cam = self.find_camera(serial_number=serial_number)

        for i in range(2):
            try:
                self.cam.BeginAcquisition()
                info("Acquisition Started")
            except PySpin.SpinnakerException as ex:
                info("Acquisition was already started")
            try:
                self.cam.AcquisitionStop()
                info("Acquisition ended")
            except PySpin.SpinnakerException as ex:
                info("Acquisition was already ended")

        self.nodes = self.get_nodes()

        #Transport Configuration
        self.configure_transport()

        #Configure Image Format
        self.configure_image()

        #Analog Configuration
        self.conf_acq_and_trigger(settings=settings)

        self.height = self.get_height()
        self.width = self.get_width()
        if (self.pixel_format == 'mono12p'):
            from lcp_video.analysis import get_mono12p_conversion_mask, get_mono12p_conversion_mask_8bit
            self.img_len = int(self.height * self.width * 1.5)
            self.images_dtype = 'uint8'
            self.conversion_mask = get_mono12p_conversion_mask(self.img_len)
            self.conversion_mask8 = get_mono12p_conversion_mask_8bit(
                self.img_len)
        elif (self.pixel_format == 'mono12packed'):
            from lcp_video.analysis import get_mono12packed_conversion_mask, get_mono12p_conversion_mask_8bit
            self.img_len = int(self.height * self.width * 1.5)
            self.images_dtype = 'uint8'
            self.conversion_mask = get_mono12packed_conversion_mask(
                self.img_len)
            self.conversion_mask8 = get_mono12p_conversion_mask_8bit(
                self.img_len)
        elif self.pixel_format == 'mono16' or self.pixel_format == 'mono12p_16':
            self.img_len = int(self.height * self.width)
            self.images_dtype = 'int16'
        self.queue = Queue(
            (self.queue_length, self.img_len + self.header_length),
            dtype=self.images_dtype)

        self.queue_frameID = Queue((self.queue_length, 2), dtype='float64')

        self.last_raw_image = zeros((self.img_len + self.header_length, ),
                                    dtype=self.images_dtype)

        from circular_buffer_numpy.circular_buffer import CircularBuffer
        self.hits_buffer = CircularBuffer((1350000, 2), dtype='float64')

        #Algorithms Configuration
        self.lut_enable = False
        self.gamma_enable = False
        self.autoexposure = 'Off'
        self.autogain = 'Off'

        try:
            self.cam.AcquisitionStop()
            info("Acquisition stopped")
        except PySpin.SpinnakerException as ex:
            info("Acquisition was already stopped")

        self.set_exposure_mode('Timed')
        self.exposure_time = 63000  #53500
        self.gain = 0
        self.black_level = 15

        self.image_threshold = zeros((self.height, self.width)) + 7

        self.image_median = zeros((self.height, self.width))
        self.image_median[:, :] = 15

        self.image_mean = zeros((self.height, self.width))
        self.image_mean[:, :] = 15
        self.image_mean_flag = True

        self.image_std = ones((self.height, self.width))
        self.image_std[:, :] = 0.8
        self.image_std_flag = True

        self.sigma_level = 6

        self.mask = zeros((self.height, self.width), dtype='bool')
        self.mask_flag = True

        self.last_frameID = -1
        self.num_of_missed_frames = 0
        "%(asctime)-15s|PID:%(process)-6s|%(levelname)-8s|%(name)s| module:%(module)s-%(funcName)s|message:%(message)s"
    )

    print("Circular buffer library")
    print("two classes: server and client")
    print("server = server() \nclient = client()")
    print("---------------------------")
    print("Server functions")
    print("server.append(data)")
    print("server.get_all()")
    print("server.get_N(N = integer)")
    print("---------------------------")

    from circular_buffer_numpy.circular_buffer import CircularBuffer
    from numpy import random
    buffer = CircularBuffer(shape=(100, 2, 4), packet_length=5)
    buffer.reset()
    j = 0
    for i in range(101):
        data = random.randint(1024, size=(5, 2, 4)) * 0 + i
        buffer.append(data)
        print(data.mean() == i)
        j = i % 5
        print('c normal', buffer.circular_pointer, (i % 20 + 1) * 5 - 1)
        print('l normal', buffer.linear_pointer, (i + 1) * 5 - 1)
        print('c packet', buffer.circular_packet_pointer, i % 20)
        print('l packet', buffer.linear_packet_pointer, i)
        print('01234567',
              buffer.get_packet_circular_i_j(i % 20, i % 20).mean() == i)
        print('01234567', buffer.get_packet_linear_i_j(i, i).mean(), i)
        if i > 20:
Example #9
0
    def init(self, config_filename):
        from numpy import zeros, nan, ones
        info('')
        info('-------------INITIALIZING NEW CAMERA-----------')
        import PySpin
        from circular_buffer_numpy.queue import Queue

        config, flag = self.read_config_file(config_filename)
        self.pixel_format = config['pixel_format']

        self.ROI_width = int(config['ROI_width'])
        self.ROI_height = int(config['ROI_height'])
        self.ROI_offset_x = int(config['ROI_offset_x'])
        self.ROI_offset_y = int(config['ROI_offset_y'])

        self.reverseX = int(config['ROI_offset_y'])
        self.reverseY = int(config['ROI_offset_y'])
        self.rotate = int(config['rotate'])
        self.trigger = config['trigger']
        if 'binning_selector' in config.keys():
            self.binning_selector = config['binning_selector']
        if 'binning_horizontal_mode' in config.keys():
            self.binning_horizontal_mode = config['binning_horizontal_mode']
        if 'binning_vertical_mode' in config.keys():
            self.binning_vertical_mode = config['binning_vertical_mode']
        if 'binning_vertical' in config.keys():
            self.binning_vertical = int(config['binning_vertical'])
        if 'binning_horizontal' in config.keys():
            self.binning_horizontal = int(config['binning_horizontal'])

        nice = int(config['nice'])
        self.nice = nice
        info(f'setting up nice {nice}')
        if nice != 0:
            os.nice(nice)
        sn = str(config['serial_number'])
        self.init(serial_number=sn)
        self.start_thread()

        self.set_exposure_time(config['exposure_time'])

        self.cam = self.find_camera(serial_number=serial_number)

        for i in range(2):
            try:
                self.cam.AcquisitionStart()
                info("Acquisition Started")
            except PySpin.SpinnakerException as ex:
                info("Acquisition was already started")
            try:
                self.cam.AcquisitionStop()
                info("Acquisition ended")
            except PySpin.SpinnakerException as ex:
                info("Acquisition was already ended")

        # reset camera to default settings

        self.nodes = self.get_nodes()

        #Transport Configuration
        self.configure_transport()

        #Configure Image Format
        self.configure_image()

        #Analog Configuration
        self.conf_acq_and_trigger(settings=settings)

        self.height = self.get_height()
        self.width = self.get_width()
        if (self.pixel_format == 'mono8'):
            from lcp_video.analysis import get_mono12p_conversion_mask, get_mono12p_conversion_mask_8bit
            self.img_len = int(self.height * self.width)
            self.images_dtype = 'uint8'
            self.conversion_mask = get_mono12p_conversion_mask(self.img_len)
            self.conversion_mask8 = get_mono12p_conversion_mask_8bit(
                self.img_len)
        elif (self.pixel_format == 'mono12p'):
            from lcp_video.analysis import get_mono12p_conversion_mask, get_mono12p_conversion_mask_8bit
            self.img_len = int(self.height * self.width * 1.5)
            self.images_dtype = 'uint8'
            self.conversion_mask = get_mono12p_conversion_mask(self.img_len)
            self.conversion_mask8 = get_mono12p_conversion_mask_8bit(
                self.img_len)
        elif (self.pixel_format == 'mono12packed'):
            from lcp_video.analysis import get_mono12packed_conversion_mask, get_mono12p_conversion_mask_8bit
            self.img_len = int(self.height * self.width * 1.5)
            self.images_dtype = 'uint8'
            self.conversion_mask = get_mono12packed_conversion_mask(
                self.img_len)
            self.conversion_mask8 = get_mono12p_conversion_mask_8bit(
                self.img_len)
        elif self.pixel_format == 'mono16' or self.pixel_format == 'mono12p_16':
            self.img_len = int(self.height * self.width)
            self.images_dtype = 'int16'
        self.queue = Queue(
            (self.queue_length, self.img_len + self.header_length),
            dtype=self.images_dtype)

        self.queue_frameID = Queue((self.queue_length, 2), dtype='float64')

        self.last_raw_image = zeros((self.img_len + self.header_length, ),
                                    dtype=self.images_dtype)

        from circular_buffer_numpy.circular_buffer import CircularBuffer
        self.hits_buffer = CircularBuffer((1350000, 2), dtype='float64')

        #Algorithms Configuration
        self.lut_enable = False
        self.gamma_enable = False
        self.autoexposure = 'Off'
        self.autogain = 'Off'

        try:
            self.cam.AcquisitionStop()
            info("Acquisition stopped")
        except PySpin.SpinnakerException as ex:
            info("Acquisition was already stopped")

        self.set_exposure_mode('Timed')
        self.exposure_time = 63000  #53500
        self.gain = 0
        self.black_level = 15

        self.image_threshold = zeros((self.height, self.width)) + 7

        self.image_median = zeros((self.height, self.width))
        self.image_median[:, :] = 15

        self.image_mean = zeros((self.height, self.width))
        self.image_mean[:, :] = 15
        self.image_mean_flag = True

        self.image_std = ones((self.height, self.width))
        self.image_std[:, :] = 0.8
        self.image_std_flag = True

        self.sigma_level = 6

        self.mask = zeros((self.height, self.width), dtype='bool')
        self.mask_flag = True

        self.last_frameID = -1
        self.num_of_missed_frames = 0