<commit_before>### import ####################################################################


import os
import collections
import time

from PyQt4 import QtGui, QtCore

import WrightTools as wt

import project
import project.classes as pc
import project.widgets as pw
import project.project_globals as g
main_dir = g.main_dir.read()
ini = project.ini_handler.Ini(os.path.join(main_dir, 'hardware', 'delays',
                                                     'pico',
                                                     'pico_delay.ini'))

if g.offline.read():
    import project.precision_micro_motors.v_precision_motors as motors
else:
    import project.precision_micro_motors.precision_motors as motors

ps_per_mm = 6.671281903963041  # a mm on the delay stage (factor of 2)


### delay object ##############################################################


class Delay(QtCore.QObject):

    def __init__(self):
        QtCore.QObject.__init__(self)
        # list of objects to be exposed to PyCMDS
        self.native_units = 'ps'
        self.limits = pc.NumberLimits(min_value=-100, max_value=100, units='ps')
        self.current_position = pc.Number(name='Delay', initial_value=0.,
                                          limits=self.limits,
                                          units='ps', display=True,
                                          set_method='set_position')
        self.offset = pc.Number(initial_value=0, units=self.native_units, display=True)
        self.motor_limits = pc.NumberLimits(min_value=0, max_value=50, units='mm')
        self.exposed = [self.current_position]
        self.recorded = collections.OrderedDict()
        self.gui = gui(self)
        self.initialized = pc.Bool()

    def close(self):
        self.motor.close()

    def get_position(self):
        position = self.motor.current_position_mm
        self.current_position_mm.write(position, 'mm')
        delay = (position - self.zero_position.read()) * ps_per_mm
        self.current_position.write(delay, 'ps')
        return delay

    def initialize(self, inputs, address):
        self.address = address
        self.index = inputs[0]
        motor_identity = motors.identity['D{}'.format(self.index)]
        self.motor = motors.Motor(motor_identity)
        self.current_position_mm = pc.Number(units='mm', display=True, decimals=5)
        # zero position
        self.zero_position = pc.Number(name='Zero', initial_value=25.,
                                       ini=ini, section='D{}'.format(self.index),
                                       option='zero position (mm)', import_from_ini=True,
                                       save_to_ini_at_shutdown=False,
                                       limits=self.motor_limits,
                                       decimals=5,
                                       units='mm', display=True)
        self.set_zero(self.zero_position.read())
        # recorded
        labels = ['13', '23']
        self.recorded['d%d'%self.index] = [self.current_position, 'ps', 0.01, labels[self.index-1], False]
        self.recorded['d%d_zero'%self.index] = [self.zero_position, 'mm', 0.001, str(self.index), True]
        self.initialized.write(True)
        self.address.initialized_signal.emit()

    def is_busy(self):
        return not self.motor.is_stopped()
        
    def set_offset(self, offset):
        # update zero
        offset_from_here = offset - self.offset.read('ps')
        offset_mm = offset_from_here/ps_per_mm
        new_zero = self.zero_position.read('mm') + offset_mm
        self.set_zero(new_zero)
        self.offset.write(offset)
        # return to old position
        destination = self.address.hardware.destination.read('ps')
        self.set_position(destination)

    def set_position(self, destination):
        destination_mm = self.zero_position.read() + destination/ps_per_mm
        self.motor.move_absolute(destination_mm, 'mm')
        self.motor.wait_until_still(method=self.get_position)
        self.get_position()

    def set_zero(self, zero):
        '''
        float zero mm
        '''
        self.zero_position.write(zero)
        min_value = -self.zero_position.read() * ps_per_mm
        max_value = (50. - self.zero_position.read()) * ps_per_mm
        self.limits.write(min_value, max_value, 'ps')
        self.get_position()
        # write new position to ini
        section = 'D{}'.format(self.index)
        option = 'zero position (mm)'
        ini.write(section, option, zero)


### advanced gui ##############################################################


class gui(QtCore.QObject):

    def __init__(self, delay):
        QtCore.QObject.__init__(self)
        self.delay = delay

    def create_frame(self, layout):
        layout.setMargin(5)
        self.layout = layout
        self.advanced_frame = QtGui.QWidget()
        self.advanced_frame.setLayout(self.layout)
        if self.delay.initialized.read():<commit_msg>pico delays factor<commit_after>### import ####################################################################


import os
import collections
import time

from PyQt4 import QtGui, QtCore

import WrightTools as wt

import project
import project.classes as pc
import project.widgets as pw
import project.project_globals as g
main_dir = g.main_dir.read()
ini = project.ini_handler.Ini(os.path.join(main_dir, 'hardware', 'delays',
                                                     'pico',
                                                     'pico_delay.ini'))

if g.offline.read():
    import project.precision_micro_motors.v_precision_motors as motors
else:
    import project.precision_micro_motors.precision_motors as motors

ps_per_mm = 6.671281903963041  # a mm on the delay stage (factor of 2)


### delay object ##############################################################


class Delay(QtCore.QObject):

    def __init__(self):
        QtCore.QObject.__init__(self)
        # list of objects to be exposed to PyCMDS
        self.native_units = 'ps'
        self.limits = pc.NumberLimits(min_value=-100, max_value=100, units='ps')
        self.current_position = pc.Number(name='Delay', initial_value=0.,
                                          limits=self.limits,
                                          units='ps', display=True,
                                          set_method='set_position')
        self.offset = pc.Number(initial_value=0, units=self.native_units, display=True)
        self.motor_limits = pc.NumberLimits(min_value=0, max_value=50, units='mm')
        self.exposed = [self.current_position]
        self.recorded = collections.OrderedDict()
        self.gui = gui(self)
        self.initialized = pc.Bool()

    def close(self):
        self.motor.close()

    def get_position(self):
        position = self.motor.current_position_mm
        self.current_position_mm.write(position, 'mm')
        delay = (position - self.zero_position.read()) * ps_per_mm * self.factor.read()
        self.current_position.write(delay, 'ps')
        return delay

    def initialize(self, inputs, address):
        self.address = address
        self.index = inputs[0]
        motor_identity = motors.identity['D{}'.format(self.index)]
        self.motor = motors.Motor(motor_identity)
        self.current_position_mm = pc.Number(units='mm', display=True, decimals=5)
        # factor
        self.factor = pc.Number(name='Factor', ini=ini, section='D%s'%self.index, option='factor', decimals=0)
        # zero position
        self.zero_position = pc.Number(name='Zero', initial_value=25.,
                                       ini=ini, section='D{}'.format(self.index),
                                       option='zero position (mm)', import_from_ini=True,
                                       save_to_ini_at_shutdown=False,
                                       limits=self.motor_limits,
                                       decimals=5,
                                       units='mm', display=True)
        self.set_zero(self.zero_position.read())
        # recorded
        labels = ['13', '23']
        self.recorded['d%d'%self.index] = [self.current_position, 'ps', 0.01, labels[self.index-1], False]
        self.recorded['d%d_zero'%self.index] = [self.zero_position, 'mm', 0.001, str(self.index), True]
        self.initialized.write(True)
        self.address.initialized_signal.emit()

    def is_busy(self):
        return not self.motor.is_stopped()
        
    def set_offset(self, offset):
        # update zero
        offset_from_here = offset - self.offset.read('ps')
        offset_mm = offset_from_here/(ps_per_mm*self.factor.read())
        new_zero = self.zero_position.read('mm') + offset_mm
        self.set_zero(new_zero)
        self.offset.write(offset)
        # return to old position
        destination = self.address.hardware.destination.read('ps')
        self.set_position(destination)

    def set_position(self, destination):
        destination_mm = self.zero_position.read() + destination/(ps_per_mm * self.factor.read())
        self.motor.move_absolute(destination_mm, 'mm')
        self.motor.wait_until_still(method=self.get_position)
        self.get_position()

    def set_zero(self, zero):
        '''
        float zero mm
        '''
        self.zero_position.write(zero)
        min_value = -self.zero_position.read() * ps_per_mm * self.factor.read()
        max_value = (50. - self.zero_position.read()) * ps_per_mm * self.factor.read()
        self.limits.write(min_value, max_value, 'ps')
        self.get_position()
        # write new position to ini
        section = 'D{}'.format(self.index)
        option = 'zero position (mm)'
        ini.write(section, option, zero)


### advanced gui ##############################################################


class gui(QtCore.QObject):

    def __init__(self, delay):
        QtCore.QObject.__init__(self)
        self.delay = delay

    def create_frame(self, layout):
        layout.setMargin(5)
        self.layout = layout
        self.advanced_frame = QtGui.QWidget()
        self.advanced_frame.setLayout(self.layout)
        if self.delay.initialized.read():
{'fraction_non_alphanumeric': 0.1455671319048555, 'fraction_numerical': 0.009730686062512286, 'mean_word_length': 2.298217179902755, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6940426', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2844, 'n_words': 538}