<commit_before>#!/usr/bin/env python-sirius
"""."""

import time as _time
from threading import Thread as _Thread, Event as _Event
import numpy as np

import pyaccel
from siriuspy.namesys import SiriusPVName as _PVName

from pymodels.middlelayer.devices import SOFB, LiLLRF
from apsuite.optimization import SimulAnneal
from apsuite.commissioning_scripts.base import BaseClass


class ParamsDisp:
    """."""

    def __init__(self):
        """."""
        self.klystron_delta = -2
        self.wait_time = 40
        self.timeout_orb = 10
        self.num_points = 10
        self.klystron_excit_coefs = [1.098, 66.669]


class MeasureDispTBBO(BaseClass):
    """."""

    def __init__(self):
        """."""
        super().__init__(ParamsDisp())
        self.devices = {
            'bo_sofb': SOFB('BO'),
            'tb_sofb': SOFB('TB'),
            'kly': LiLLRF('Klystron2'),
            }

    @property
    def energy(self):
        """."""
        return np.polyval(
            self.params.klystron_excit_coefs, self.devices['kly'].amplitude)

    @property
    def trajx(self):
        """."""
        return np.hstack(
            [self.devices['tb_sofb'].trajx, self.devices['bo_sofb'].trajx_idx])

    @property
    def trajy(self):
        """."""
        return np.hstack(
            [self.devices['tb_sofb'].trajy, self.devices['bo_sofb'].trajy_idx])

    @property
    def nr_points(self):
        """."""
        return min(
            self.devices['tb_sofb'].nr_points,
            self.devices['bo_sofb'].nr_points)

    @nr_points.setter
    def nr_points(self, value):
        self.devices['tb_sofb'].nr_points = int(value)
        self.devices['bo_sofb'].nr_points = int(value)

    def wait(self, timeout=10):
        """."""
        self.devices['tb_sofb'].wait(timeout=timeout)
        self.devices['bo_sofb'].wait(timeout=timeout)

    def reset(self, wait=0):
        """."""
        _time.sleep(wait)
        self.devices['tb_sofb'].reset()
        self.devices['bo_sofb'].reset()
        _time.sleep(1)

    def measure_dispersion(self):
        """."""
        self.nr_points = self.params.num_points
        delta = self.params.klystron_delta

        self.reset(self.params.wait_time)
        self.wait(self.params.timeout_orb)
        orb = [-np.hstack([self.trajx, self.trajy]), ]
        ene0 = self.energy

        origamp = self.devices['kly'].amplitude
        self.devices['kly'].amplitude = origamp + delta

        self.reset(self.params.wait_time)
        self.wait(self.params.timeout_orb)
        orb.append(np.hstack([self.trajx, self.trajy]))
        ene1 = self.energy<commit_msg>API: Change klystron2 coefficients in TBBO disp. meas.<commit_after>#!/usr/bin/env python-sirius
"""."""

import time as _time
from threading import Thread as _Thread, Event as _Event
import numpy as np

import pyaccel
from siriuspy.namesys import SiriusPVName as _PVName

from pymodels.middlelayer.devices import SOFB, LiLLRF
from apsuite.optimization import SimulAnneal
from apsuite.commissioning_scripts.base import BaseClass


class ParamsDisp:
    """."""

    def __init__(self):
        """."""
        self.klystron_delta = -2
        self.wait_time = 40
        self.timeout_orb = 10
        self.num_points = 10
        # self.klystron_excit_coefs = [1.098, 66.669]  # old
        # self.klystron_excit_coefs = [1.01026423, 71.90322743]  # > 2.5nC
        self.klystron_excit_coefs = [0.80518365, 87.56545895]  # < 2.5nC


class MeasureDispTBBO(BaseClass):
    """."""

    def __init__(self):
        """."""
        super().__init__(ParamsDisp())
        self.devices = {
            'bo_sofb': SOFB('BO'),
            'tb_sofb': SOFB('TB'),
            'kly': LiLLRF('Klystron2'),
            }

    @property
    def energy(self):
        """."""
        return np.polyval(
            self.params.klystron_excit_coefs, self.devices['kly'].amplitude)

    @property
    def trajx(self):
        """."""
        return np.hstack(
            [self.devices['tb_sofb'].trajx, self.devices['bo_sofb'].trajx_idx])

    @property
    def trajy(self):
        """."""
        return np.hstack(
            [self.devices['tb_sofb'].trajy, self.devices['bo_sofb'].trajy_idx])

    @property
    def nr_points(self):
        """."""
        return min(
            self.devices['tb_sofb'].nr_points,
            self.devices['bo_sofb'].nr_points)

    @nr_points.setter
    def nr_points(self, value):
        self.devices['tb_sofb'].nr_points = int(value)
        self.devices['bo_sofb'].nr_points = int(value)

    def wait(self, timeout=10):
        """."""
        self.devices['tb_sofb'].wait(timeout=timeout)
        self.devices['bo_sofb'].wait(timeout=timeout)

    def reset(self, wait=0):
        """."""
        _time.sleep(wait)
        self.devices['tb_sofb'].reset()
        self.devices['bo_sofb'].reset()
        _time.sleep(1)

    def measure_dispersion(self):
        """."""
        self.nr_points = self.params.num_points
        delta = self.params.klystron_delta

        self.reset(3)
        self.wait(self.params.timeout_orb)
        orb = [-np.hstack([self.trajx, self.trajy]), ]
        ene0 = self.energy

        origamp = self.devices['kly'].amplitude
        self.devices['kly'].amplitude = origamp + delta

        self.reset(self.params.wait_time)
        self.wait(self.params.timeout_orb)
        orb.append(np.hstack([self.trajx, self.trajy]))
        ene1 = self.energy
{'fraction_non_alphanumeric': 0.14695077149155034, 'fraction_numerical': 0.01653196179279941, 'mean_word_length': 2.36734693877551, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5930101', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1833, 'n_words': 293}