text:
# This is a virtual instrument abstracting a homodyne
# source which controls RF and LO sources

import logging
import numpy as np
from time import time
from qcodes.instrument.base import Instrument
from qcodes.utils import validators as vals
from qcodes.instrument.parameter import ManualParameter
# Used for uploading the right AWG sequences
from pycqed.measurement.pulse_sequences import standard_sequences as st_seqs
import time

class HeterodyneInstrument(Instrument):

    """
    This is a virtual instrument for a homodyne source

    Instrument is CBox, UHFQC, ATS and DDM compatible

    """
    shared_kwargs = ['RF', 'LO', 'AWG']

    def __init__(self, name, RF, LO, AWG=None, acquisition_instr=None,
                 acquisition_instr_controller=None,
                 single_sideband_demod=False, **kw):

        self.RF = RF

        self.common_init(name, LO, AWG, acquisition_instr,
                         single_sideband_demod, **kw)

        self.add_parameter('RF_power', label='RF power',
                           unit='dBm', vals=vals.Numbers(),
                           set_cmd=self._set_RF_power,
                           get_cmd=self._get_RF_power)
        self.add_parameter('acquisition_instr_controller',
                           set_cmd=self._set_acquisition_instr_controller,
                           get_cmd=self._get_acquisition_instr_controller,
                           vals=vals.Anything())
        self.acquisition_instr_controller(acquisition_instr_controller)
        self._RF_power = None

    def common_init(self, name, LO, AWG, acquisition_instr='CBox',
                    single_sideband_demod=False, **kw):
        logging.info(__name__ + ' : Initializing instrument')
        Instrument.__init__(self, name, **kw)

        self.LO = LO
        self.AWG = AWG
        self.add_parameter('frequency', label='Heterodyne frequency',
                           unit='Hz', vals=vals.Numbers(9e3, 40e9),
                           get_cmd=self._get_frequency,
                           set_cmd=self._set_frequency)
        self.add_parameter('f_RO_mod', label='Intermodulation frequency',
                           unit='Hz', vals=vals.Numbers(-600e6, 600e6),
                           set_cmd=self._set_f_RO_mod,
                           get_cmd=self._get_f_RO_mod)
        self.add_parameter('single_sideband_demod', vals=vals.Bool(),
                           label='Single sideband demodulation',
                           parameter_class=ManualParameter,
                           initial_value=single_sideband_demod)
        self.add_parameter('external_trigger', vals=vals.Bool(),
                           label='wait for external trigger',
                           parameter_class=ManualParameter,
                           initial_value=False)
        self.add_parameter('acquisition_instr', vals=vals.Strings(),
                           label='Acquisition instrument',
                           set_cmd=self._set_acquisition_instr,
                           get_cmd=self._get_acquisition_instr)
        self.add_parameter('nr_averages', label='Number of averages',
                           vals=vals.Numbers(min_value=0, max_value=1e6),
                           parameter_class=ManualParameter,
                           initial_value=1024)
        self.add_parameter('status', vals=vals.Enum('On', 'Off'),
                           set_cmd=self._set_status,
                           get_cmd=self._get_status)
        self.add_parameter('trigger_separation', label='Trigger separation',
                           unit='s', vals=vals.Numbers(0),
                           set_cmd=self._set_trigger_separation,
                           get_cmd=self._get_trigger_separation)
        self.add_parameter('RO_length', label='Readout length',
                           unit='s', vals=vals.Numbers(0),
                           set_cmd=self._set_RO_length,
                           get_cmd=self._get_RO_length)
        self.add_parameter('auto_seq_loading', vals=vals.Bool(),
                           label='Automatic AWG sequence loading',
                           parameter_class=ManualParameter,
                           initial_value=True)
        self.add_parameter('acq_marker_channels', vals=vals.Strings(),
                           label='Acquisition trigger channels',
                           docstring='comma (,) separated string of marker channels',
                           set_cmd=self._set_acq_marker_channels,
                           get_cmd=self._get_acq_marker_channels)

        self._trigger_separation = 10e-6
        self._RO_length = 2274e-9
        self._awg_seq_filename = ''
        self._awg_seq_parameters_changed = True
        self._UHFQC_awg_parameters_changed = True
        self.acquisition_instr(acquisition_instr)
        self.status('Off')
        self._f_RO_mod = 10e6
        self._frequency = 5e9
        self.frequency(5e9)
        self.f_RO_mod(10e6)
        self._eps = 0.01  # Hz slack for comparing frequencies
        self._acq_marker_channels = ('ch4_marker1,ch4_marker2,' +
                                     'ch3_marker1,ch3_marker2')

    def prepare(self, get_t_base=True):
        # Uploading the AWG sequence
        if self.AWG!=None:
            if (self._awg_seq_filename not in self.AWG.setup_filename() or
                    self._awg_seq_parameters_changed) and self.auto_seq_loading():
                self._awg_seq_filename = \
                    st_seqs.generate_and_upload_marker_sequence(
                        5e-9, self.trigger_separation(), RF_mod=False,
                        acq_marker_channels=self.acq_marker_channels(), trigger_wait=self.external_trigger())
                self._awg_seq_parameters_changed = False

        # Preparing the acquisition instruments
        if 'CBox' in self.acquisition_instr():
            self.prepare_CBox(get_t_base)
        elif 'UHFQC' in self.acquisition_instr():
            self.prepare_UHFQC()
        elif 'ATS' in self.acquisition_instr():
            self.prepare_ATS(get_t_base)
        elif 'DDM' in self.acquisition_instr():
            self.prepare_DDM()

        else:
            raise ValueError("Invalid acquisition instrument {} in {}".format(
                self.acquisition_instr(), self.__class__.__name__))

        # turn on the AWG and the MWGs
        if self.AWG!=None:
            self.AWG.run()
        self.on()

    def prepare_CBox(self, get_t_base=True):
        if get_t_base:
            trace_length = 512
            tbase = np.arange(0, 5*trace_length, 5)*1e-9
            self.cosI = np.floor(
                127.*np.cos(2*np.pi*self.f_RO_mod()*tbase))
            self.sinI = np.floor(
                127.*np.sin(2*np.pi*self.f_RO_mod()*tbase))
            self._acquisition_instr.sig0_integration_weights(self.cosI)
            self._acquisition_instr.sig1_integration_weights(self.sinI)
            # because using integrated avg
            self._acquisition_instr.set('nr_samples', 1)
            self._acquisition_instr.nr_averages(int(self.nr_averages()))

    def prepare_UHFQC(self):
        # Upload the correct integration weigths
        if self.single_sideband_demod():
            self._acquisition_instr.prepare_SSB_weight_and_rotation(
                IF=self.f_RO_mod(), weight_function_I=0, weight_function_Q=1)
        else:
            self._acquisition_instr.prepare_DSB_weight_and_rotation(
                IF=self.f_RO_mod(), weight_function_I=0, weight_function_Q=1)

        if self._UHFQC_awg_parameters_changed and self.auto_seq_loading():
            # self._acquisition_instr.awg_sequence_acquisition() # Dirty hack to get UHFLI to cooperate
            self._UHFQC_awg_parameters_changed = False

        # this sets the result to integration and rotation outcome
        self._acquisition_instr.qas_0_result_source(2)
        # only one sample to average over
        self._acquisition_instr.qas_0_result_length(1)
        self._acquisition_instr.qas_0_result_averages(self.nr_averages())
        self._acquisition_instr.qas_0_integration_length(
            int(self.RO_length()*1.8e9))
        # The AWG program uses userregs/0 to define the number of
        # iterations in the loop, but this is configured using the acquisition_initialize function
        self._acquisition_instr.acquisition_initialize(samples=1, averages=self.nr_averages(), channels=[0, 1], mode='rl')
        self.scale_factor_UHFQC = 1/(1.8e9*self.RO_length() *
                                     int(self.nr_averages()))

    def prepare_ATS(self, get_t_base=True):
        if self.AWG != None:
            if (self._awg_seq_filename not in self.AWG.setup_filename() or
                    self._awg_seq_parameters_changed) and \
                    self.auto_seq_loading():
                self._awg_seq_filename = \
                    st_seqs.generate_and_upload_marker_sequence(
                        self.RO_length(), self.trigger_separation(),
                        RF_mod=False,
                        acq_marker_channels=self.acq_marker_channels())
                self._awg_seq_parameters_changed = False

        if get_t_base:
            self._acquisition_instr_controller.demodulation_frequency = \
                self.f_RO_mod()
            buffers_per_acquisition = 8
            self._acquisition_instr_controller.update_acquisitionkwargs(
                # mode='NPT',
                samples_per_record=64*1000,  # 4992,
                records_per_buffer=int(
                    self.nr_averages()/buffers_per_acquisition),  # 70 segments
                buffers_per_acquisition=buffers_per_acquisition,
                channel_selection='AB',
                transfer_offset=0,
                external_startcapture='ENABLED',
                enable_record_headers='DISABLED',
                alloc_buffers='DISABLED',
                fifo_only_streaming='DISABLED',
                interleave_samples='DISABLED',
                get_processed_data='DISABLED',
                allocated_buffers=buffers_per_acquisition,
                buffer_timeout=1000)

    def probe(self):
        if 'CBox' in self.acquisition_instr():
            return self.probe_CBox()
        elif 'UHFQC' in self.acquisition_instr():
            return self.probe_UHFQC()
        elif 'ATS' in self.acquisition_instr():
            return self.probe_ATS()
        elif 'DDM' in self.acquisition_instr():
            return self.probe_DDM()
        else:
            raise ValueError("Invalid acquisition instrument {} in {}".format(
                self.acquisition_instr(), self.__class__.__name__))

    def probe_CBox(self):
        if self.single_sideband_demod():
            demodulation_mode = 'single'
        else:
            demodulation_mode = 'double'
        self._acquisition_instr.acquisition_mode('idle')
        self._acquisition_instr.acquisition_mode('integration averaging')
        self._acquisition_instr.demodulation_mode(demodulation_mode)
        # d = self.CBox.get_integrated_avg_results()
        # quick fix for spec units. Need to properrly implement it later
        # after this, output is in V
        scale_factor_dacV = 1.*0.75/128.
        # scale_factor_integration = 1./float(self.f_RO_mod() *
        #     self.CBox.nr_samples()*5e-9)
        scale_factor_integration = \
            1 / (64.*self._acquisition_instr.integration_length())
        factor = scale_factor_dacV*scale_factor_integration
        d = np.double(self._acquisition_instr.get_integrated_avg_results()) \
            * np.double(factor)
        # print(np.size(d))
        return d[0][0]+1j*d[1][0]

    def probe_UHFQC(self):
        if self._awg_seq_parameters_changed or \
           self._UHFQC_awg_parameters_changed:
            self.prepare()

        dataset = self._acquisition_instr.acquisition_poll(
            samples=1, acquisition_time=0.001)
        dat = (self.scale_factor_UHFQC*dataset[0][0] +
               self.scale_factor_UHFQC*1j*dataset[1][0])
        return dat

    def probe_ATS(self):
        dat = self._acquisition_instr_controller.acquisition()

        return dat

    def finish(self):
        self.off()
        if self.AWG is not None:
            self.AWG.stop()
        if 'UHFQC' in self.acquisition_instr():
            self._acquisition_instr.acquisition_finalize()

    def _set_frequency(self, val):
        self._frequency = val
        # this is the definition agreed upon in issue 131
        self.RF.frequency(val)
        self.LO.frequency(val-self._f_RO_mod)

    def _get_frequency(self):
        freq = self.RF.frequency()
        LO_freq = self.LO.frequency()
        if abs(LO_freq - freq + self._f_RO_mod) > self._eps:
            logging.warning('f_RO_mod between RF and LO is not set correctly')
            logging.warning('\tf_RO_mod = {}, LO_freq = {}, RF_freq = {}'
                            .format(self._f_RO_mod, LO_freq, freq))
        if abs(self._frequency - freq) > self._eps:
            logging.warning('Heterodyne frequency does not match RF frequency')
        return self._frequency

    def _set_f_RO_mod(self, val):
        self._f_RO_mod = val
        self.LO.frequency(self._frequency - val)

    def _get_f_RO_mod(self):
        freq = self.RF.frequency()
        LO_freq = self.LO.frequency()
        if abs(LO_freq - freq + self._f_RO_mod) > self._eps:
            logging.warning('f_RO_mod between RF and LO is not set correctly')
            logging.warning('\tf_RO_mod = {}, LO_freq = {}, RF_freq = {}'
                            .format(self._f_RO_mod, LO_freq, freq))
        return self._f_RO_mod

    def _set_RF_power(self, val):
        self.RF.power(val)
        self._RF_power = val
        # internally stored to allow setting RF from stored setting

    def _get_RF_power(self):
        return self._RF_power

    def _set_status(self, val):
        if val == 'On':
            self.on()
        else:
            self.off()

    def _get_status(self):
        if (self.LO.status().startswith('On') and
                self.RF.status().startswith('On')):
            return 'On'
        elif (self.LO.status().startswith('Off') and
              self.RF.status().startswith('Off')):
            return 'Off'
        else:
            return 'LO: {}, RF: {}'.format(self.LO.status(), self.RF.status())

    def on(self):
        if self.LO.status().startswith('Off') or \
           self.RF.status().startswith('Off'):
            wait = True
        else:
            wait = False
        self.LO.on()
        self.RF.on()
        if wait:
            # The R&S MWG-s take some time to stabilize their outputs
            time.sleep(1.0)

    def off(self):
        self.LO.off()
        self.RF.off()

    def _get_acquisition_instr(self):
        # Specifying the int_avg det here should allow replacing it with ATS
        # or potential digitizer acquisition easily
        if self._acquisition_instr is None:
            return None
        else:
            return self._acquisition_instr.name

    def _set_acquisition_instr(self, acquisition_instr):
        # Specifying the int_avg det here should allow replacing it with ATS
        # or potential digitizer acquisition easily
        if acquisition_instr is None:
            self._acquisition_instr = None
        else:
            self._acquisition_instr = self.find_instrument(acquisition_instr)
        self._awg_seq_parameters_changed = True
        self._UHFQC_awg_parameters_changed = True

    def _get_acquisition_instr_controller(self):
        # Specifying the int_avg det here should allow replacing it with ATS
        # or potential digitizer acquisition easily
        if self._acquisition_instr_controller == None:
            return None
        else:
            return self._acquisition_instr_controller.name

    def _set_acquisition_instr_controller(self, acquisition_instr_controller):
        # Specifying the int_avg det here should allow replacing it with ATS
        # or potential digitizer acquisition easily
        if acquisition_instr_controller == None:
            self._acquisition_instr_controller = None
        else:
            self._acquisition_instr_controller = \
                self.find_instrument(acquisition_instr_controller)
            print("controller initialized")

    def _set_trigger_separation(self, val):
        if val != self._trigger_separation:
            self._awg_seq_parameters_changed = True
        self._trigger_separation = val

    def _get_trigger_separation(self):
        return self._trigger_separation

    def _set_RO_length(self, val):
        if val != self._RO_length and 'UHFQC' not in self.acquisition_instr():
            self._awg_seq_parameters_changed = True
        self._RO_length = val

    def _get_RO_length(self):
        return self._RO_length

    def _set_acq_marker_channels(self, channels):
        if channels != self._acq_marker_channels:
            self._awg_seq_parameters_changed = True
        self._acq_marker_channels = channels

    def _get_acq_marker_channels(self):
        return self._acq_marker_channels

    def finish(self):
        if 'UHFQC' in self.acquisition_instr():
            self._acquisition_instr.acquisition_finalize()
        self.off()

    def get_demod_array(self):
        return self.cosI, self.sinI

    def demodulate_data(self, dat):
        """
        Returns a complex point in the IQ plane by integrating and demodulating
        the data. Demodulation is done based on the 'f_RO_mod' and
        'single_sideband_demod' parameters of the Homodyne instrument.
        """
        if self._f_RO_mod != 0:
            # self.cosI is based on the time_base and created in self.init()
            if self._single_sideband_demod is True:
                # this definition for demodulation is consistent with
                # issue #131
                I = np.average(self.cosI * dat[0] + self.sinI * dat[1])
                Q = np.average(-self.sinI * dat[0] + self.cosI * dat[1])
            else:  # Single channel demodulation, defaults to using channel 1
                I = 2*np.average(dat[0]*self.cosI)
                Q = 2*np.average(dat[0]*self.sinI)
        else:
            I = np.average(dat[0])
            Q = np.average(dat[1])
        return I+1.j*Q

class LO_modulated_Heterodyne(HeterodyneInstrument):

    """
    Homodyne instrument for pulse modulated LO.
    Inherits functionality from the HeterodyneInstrument

    AWG is used for modulating signal and triggering the CBox for acquisition
    or AWG is used for triggering and UHFQC for modulation and acquisition
    """
    shared_kwargs = ['RF', 'LO', 'AWG']

    def __init__(self, name, LO, AWG, acquisition_instr='CBox',
                 single_sideband_demod=False, **kw):

        self.common_init(name, LO, AWG, acquisition_instr,
                         single_sideband_demod, **kw)

        self.add_parameter('mod_amp', label='Modulation amplitude',
                           unit='V', vals=vals.Numbers(0, 1),
                           set_cmd=self._set_mod_amp,
                           get_cmd=self._get_mod_amp)
        self.add_parameter('acquisition_delay', label='Acquisition delay',
                           unit='s', vals=vals.Numbers(0, 1e-3),
                           set_cmd=self._set_acquisition_delay,
                           get_cmd=self._get_acquisition_delay)
        self.add_parameter('I_channel', vals=vals.Strings(),
                           label='I channel',
                           set_cmd=self._set_I_channel,
                           get_cmd=self._get_I_channel)
        self.add_parameter('Q_channel', vals=vals.Strings(),
                           label='Q channel',
                           set_cmd=self._set_Q_channel,
                           get_cmd=self._get_Q_channel)

        self._f_RO_mod = 10e6
        self._frequency = 5e9
        self.f_RO_mod(10e6)
        self.frequency(5e9)
        self._mod_amp = 0
        self.mod_amp(.5)
        self._acquisition_delay = 0
        self.acquisition_delay(200e-9)
        self._I_channel = 'ch3'
        self._Q_channel = 'ch4'

    def prepare(self, get_t_base=True):
        # Preparing the acquisition instruments
        if 'CBox' in self.acquisition_instr():
            self.prepare_CBox(get_t_base)
        elif 'UHFQC' in self.acquisition_instr():
            self.prepare_UHFQC()
        elif 'ATS' in self.acquisition_instr():
            self.prepare_ATS(get_t_base)
        elif 'DDM' in self.acquisition_instr():
            self.prepare_DDM()

        else:
            raise ValueError("Invalid acquisition instrument {} in {}".format(
                self.acquisition_instr(), self.__class__.__name__))

        # turn on the AWG and the MWGs
        self.AWG.run()
        self.on()

    def prepare_DDM(self):
        for i, channel in enumerate([1, 2]):
            eval("self._acquisition_instr.ch_pair1_weight{}_wint_intlength({})".format(
                channel, self.RO_length*500e6))
        self._acquisition_instr.ch_pair1_tvmode_naverages(self.nr_averages())
        self._acquisition_instr.ch_pair1_tvmode_nsegments(1)
        self.scale_factor = 1/(500e6*self.RO_length)/127

    def prepare_CBox(self, get_t_base=True):
        """
        uses the AWG to generate the modulating signal and CBox for readout
        """
        # only uploads a seq to AWG if something changed
        if (self._awg_seq_filename not in self.AWG.setup_filename() or
                self._awg_seq_parameters_changed) and self.auto_seq_loading():
            self._awg_seq_filename = \
                st_seqs.generate_and_upload_marker_sequence(
                    self.RO_length(), self.trigger_separation(), RF_mod=True,
                    IF=self.f_RO_mod(), mod_amp=0.5,
                    acq_marker_channels=self.acq_marker_channels(),
                    I_channel=self.I_channel(), Q_channel=self.Q_channel())
            self.AWG.ch3_amp(self.mod_amp())
            self.AWG.ch4_amp(self.mod_amp())
            self._awg_seq_parameters_changed = False

        self.AWG.ch3_amp(self.mod_amp())
        self.AWG.ch4_amp(self.mod_amp())

        if get_t_base is True:
            trace_length = self.CBox.nr_samples()
            tbase = np.arange(0, 5*trace_length, 5)*1e-9
            self.cosI = np.cos(2*np.pi*self.f_RO_mod()*tbase)
            self.sinI = np.sin(2*np.pi*self.f_RO_mod()*tbase)

        self.CBox.nr_samples(1)  # because using integrated avg

    def prepare_UHFQC(self):
        """
        uses the UHFQC to generate the modulating signal and readout
        """
        # only uploads a seq to AWG if something changed
        if (self._awg_seq_filename not in self.AWG.setup_filename() or
                self._awg_seq_parameters_changed) and self.auto_seq_loading():
            self._awg_seq_filename = \
                st_seqs.generate_and_upload_marker_sequence(
                    5e-9, self.trigger_separation(), RF_mod=False,
                    acq_marker_channels=self.acq_marker_channels())
            self._awg_seq_parameters_changed = False

        # reupload the UHFQC pulse generation only if something changed
        if self._UHFQC_awg_parameters_changed and self.auto_seq_loading():
            self._acquisition_instr.awg_sequence_acquisition_and_pulse_SSB(
                self.f_RO_mod.get(), self.mod_amp(), RO_pulse_length=800e-9,
                acquisition_delay=self.acquisition_delay())
            self._UHFQC_awg_parameters_changed = False

        # prepare weights and rotation
        if self.single_sideband_demod():
            self._acquisition_instr.prepare_SSB_weight_and_rotation(
                IF=self.f_RO_mod(), weight_function_I=0, weight_function_Q=1)
        else:
            self._acquisition_instr.prepare_DSB_weight_and_rotation(
                IF=self.f_RO_mod(), weight_function_I=0, weight_function_Q=1)

        # this sets the result to integration and rotation outcome
        self._acquisition_instr.qas_0_result_source(2)
        self._acquisition_instr.qas_0_result_length(1)
        self._acquisition_instr.qas_0_result_averages(self.nr_averages())
        self._acquisition_instr.qas_0_integration_length(int(self.RO_length()*1.8e9))
        self._acquisition_instr.awgs_0_single(1)
        self._acquisition_instr.wait_dly(int(self.acquisition_delay()*1.8e9/8))
        self._acquisition_instr.acquisition_initialize(samples=1, averages=self.nr_averages(), channels=[0, 1], mode='rl')

    def probe_CBox(self):
        if self._awg_seq_parameters_changed:
            self.prepare()
        self.CBox.acquisition_mode(0)
        self.CBox.acquisition_mode(4)
        d = self.CBox.get_integrated_avg_results()
        return d[0][0]+1j*d[1][0]

    def probe_DDM(self):
        # t0 = time.time()
        self._acquisition_instr.ch_pair1_tvmode_enable.set(1)
        self._acquisition_instr.ch_pair1_run.set(1)
        dataI = eval(
            "self._acquisition_instr.ch_pair1_weight{}_tvmode_data()".format(1))
        dataQ = eval(
            "self._acquisition_instr.ch_pair1_weight{}_tvmode_data()".format(2))
        dat = (self.scale_factor*dataI+self.scale_factor*1j*dataQ)
        # t1 = time.time()
        # print("time for DDM polling", t1-t0)
        return dat

    def _set_frequency(self, val):
        self._frequency = val
        # this is the definition agreed upon in issue 131
        # AWG modulation ensures that signal ends up at RF-frequency
        self.LO.frequency(val-self._f_RO_mod)

    def _get_frequency(self):
        freq = self.LO.frequency() + self._f_RO_mod
        if abs(self._frequency - freq) > self._eps:
            logging.warning('Homodyne frequency does not match LO frequency'
                            ' + RO_mod frequency')
        return self._frequency

    def _set_f_RO_mod(self, val):
        if val != self._f_RO_mod:
            if 'CBox' in self.acquisition_instr():
                self._awg_seq_parameters_changed = True
            elif 'UHFQC' in self.acquisition_instr():
                self._UHFQC_awg_parameters_changed = True
            self.frequency(self._frequency)
        self._f_RO_mod = val

    def _get_f_RO_mod(self):
        return self._f_RO_mod

    def _set_mod_amp(self, val):
        if val != self._mod_amp:
            if 'UHFQC' in self.acquisition_instr():
                self._UHFQC_awg_parameters_changed = True
        self._mod_amp = val

    def _get_mod_amp(self):
        return self._mod_amp

    def _set_acquisition_delay(self, val):
        if 'UHFQC' in self.acquisition_instr():
            self._acquisition_instr.awgs_0_userregs_2(int(val*1.8e9/8))
        else:
            raise NotImplementedError("CBox heterodyne driver does not "
                                      "implement acquisition delay")
        self._acquisition_delay = val

    def _get_acquisition_delay(self):
        return self._acquisition_delay

    def on(self):
        if self.LO.status().startswith('Off'):
            self.LO.on()
            time.sleep(1.0)

    def off(self):
        self.LO.off()

    def _get_status(self):
        return self.LO.get('status')

    def _set_I_channel(self, channel):
        if channel != self._I_channel and \
                not 'UHFQC' in self.acquisition_instr():
            self._awg_seq_parameters_changed = True
        self._I_channel = channel

    def _get_I_channel(self):
        return self._I_channel

    def _set_Q_channel(self, channel):
        if channel != self._Q_channel and \
                not 'UHFQC' in self.acquisition_instr():
            self._awg_seq_parameters_changed = True
        self._Q_channel = channel

    def _get_Q_channel(self):
        return self._Q_channel


text:
import theano
from theano.ifelse import ifelse
import numpy as np

from neupy.core.properties import (BoundedProperty,
                                   ProperFractionProperty)
from .base import SingleStepConfigurable

__all__ = ('ErrDiffStepUpdate',)

class ErrDiffStepUpdate(SingleStepConfigurable):
    """
    This algorithm make step update base on error difference between
    epochs.

    Parameters
    ----------
    update_for_smaller_error : float
        Multiplies this option to ``step`` in if the error
        was less than in previous epochs. Defaults to ``1.05``.
        Value can't be less than ``1``.

    update_for_bigger_error : float
        Multiplies this option to ``step`` in if the error
        was more than in previous epochs. Defaults to ``0.7``.

    error_difference : float
        The value indicates how many had to increase the
        error from the previous epochs that would produce
        reduction step. Defaults to ``1.04``.
        Value can't be less than ``1``.

    Warns
    -----
    {SingleStepConfigurable.Warns}

    Examples
    --------
    >>> from neupy import algorithms
    >>>
    >>> bpnet = algorithms.GradientDescent(
    ...     (2, 4, 1),
    ...     step=0.1,
    ...     verbose=False,
    ...     addons=[algorithms.ErrDiffStepUpdate]
    ... )
    """
    update_for_smaller_error = BoundedProperty(default=1.05, minval=1)
    update_for_bigger_error = ProperFractionProperty(default=0.7)
    error_difference = BoundedProperty(default=1.04, minval=1)

    def init_variables(self):
        self.variables.update(
            last_error=theano.shared(
                name='err-diff-step-update/last-error',
                value=np.nan
            ),
            previous_error=theano.shared(
                name='err-diff-step-update/previous-error',
                value=np.nan
            ),
        )
        super(ErrDiffStepUpdate, self).init_variables()

    def init_train_updates(self):
        updates = super(ErrDiffStepUpdate, self).init_train_updates()

        step = self.variables.step
        last_error = self.variables.last_error
        previous_error = self.variables.previous_error

        step_update_condition = ifelse(
            last_error < previous_error,
            self.update_for_smaller_error * step,
            ifelse(
                last_error > self.update_for_bigger_error * previous_error,
                self.update_for_bigger_error * step,
                step
            )

        )
        updates.append((step, step_update_condition))
        return updates

    def on_epoch_start_update(self, epoch):
        super(ErrDiffStepUpdate, self).on_epoch_start_update(epoch)

        previous_error = self.errors.previous()
        if previous_error:
            last_error = self.errors.last()
            self.variables.last_error.set_value(last_error)
            self.variables.previous_error.set_value(previous_error)

ext: py

max_stars_repo_name: vishalbelsare/neupy

max_issues_repo_name: vishalbelsare/neupy

max_forks_repo_name: vishalbelsare/neupy

reason: import numpy,import theano,from theano


text:
import torch

torch.manual_seed(0)
torch.cuda.manual_seed(0)
print(torch.__version__)

x = torch.empty(5, 3)
print(x)

x = torch.rand(5, 3)
print(x)

x = torch.zeros(5, 3, dtype=torch.long)
print(x)

x = torch.tensor([5.5, 3])
print(x)

x = x.new_ones(5, 3, dtype=torch.float64)
print(x)

x = torch.randn_like(x, dtype=torch.float)
print(x)

print(x.size())
print(x.shape)

y = torch.rand(5, 3)
print(x + y)

print(torch.add(x, y))
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

y.add_(x)
print(y)

y = x[0, :]
y += 1
print(y)
print(x[0, :])

y = x.view(15)
z = x.view(-1, 5)

print(x.size(), y.size(), z.size())

x += 1
print(x)
print(y)

x_cp = x.clone().view(15)
x -= 1
print(x)
print(x_cp)

x = torch.randn(1)
print(x)
print(x.item())

x = torch.arange(1, 3).view(1, 2)
print(x)
y = torch.arange(1, 4).view(3, 1)
print(y)
print(x + y)

x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
y = y + x
print(id(y) == id_before)

x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
y[:] = y + x
print(id(y) == id_before)

x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
torch.add(x, y, out=y)
print(id(y) == id_before) # y += x, y.add_(x)

a = torch.ones(5)
b = a.numpy()
print(a, b)

a += 1
print(a, b)
b += 1
print(a, b)

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(a, b)

a += 1
print(a, b)

b += 1
print(a, b)

c = torch.tensor(a)
a += 1
print(a, c)

if torch.cuda.is_available():
    device = torch.device("cuda")
    y = torch.ones_like(x, device=device)
    x = x.to(device)
    z = x + y
    print(z.to("cpu", torch.double))

reason: import numpy


text:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

from warmUpExercise import warmUpExercise
from plotData import plotData, plotData2
from gradientDescent import gradientDescent
from computeCost import computeCost

# ==================== Part 1: Basic Function ====================
print('Running warmup exercise ...')
print('5x5 Identity Matrix:')
print(warmUpExercise(), '\n')

# ======================= Part 2: Plotting =======================
print('Plotting Data ...\n')
data = np.loadtxt('ex1data1.txt', delimiter=',')
X, y = data[:, 0], data[:, 1]
plotData(X, y)

# =================== Part 3: Gradient descent ===================
print('Running Gradient Descent ...')
m, n = data.shape
X = np.hstack((np.ones((m, 1)), data[:, [0]]))
y = data[:, [1]]
theta = np.zeros((2, 1))
iterations = 1500
alpha = 0.01

theta = gradientDescent(X, y, theta, alpha, iterations)
print('Theta found by gradient descent: %f %f' % (theta[0, 0], theta[1, 0]))

plotData2(theta, X, y)

predict1 = np.dot([1, 3.5], theta) * 10000
predict2 = np.dot([1, 7], theta) * 10000
print('For population = 35,000, we predict a profit of %f' % predict1)
print('For population = 70,000, we predict a profit of %f' % predict2)

# ============= Part 4: Visualizing J(theta_0, theta_1) =============
print('\nVisualizing J(theta_0, theta_1) ...')
theta0_vals = np.linspace(-10, 10, 100)
theta1_vals = np.linspace(-1, 4, 100)
J_vals = np.zeros((theta0_vals.size, theta1_vals.size))

for i in range(theta0_vals.size):

    for j in range(theta1_vals.size):

        t = [theta0_vals[i], theta1_vals[j]]
        J_vals[i, j] = computeCost(X, y, t)

theta0_vals, theta1_vals = np.meshgrid(theta0_vals, theta1_vals)

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(theta0_vals, theta1_vals, J_vals,
                rstride=1, cstride=1, cmap=cm.coolwarm,
                linewidth=0, antialiased=False)
plt.xlabel('theta 0')
plt.ylabel('theta 1')
plt.show()

plt.contour(theta0_vals, theta1_vals, J_vals, 20)
plt.plot(theta[0, 0], theta[1, 0], 'b+')
plt.xlabel('theta 0')
plt.ylabel('theta 1')
plt.show()


text:
# -*- coding: utf-8 -*-
"""fine_pruning.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1C-kEXJhAAVX0XudHUF64IkHs3kaCMUfP
"""

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
import keras.backend as K
from keras import initializers
import sys
import os
# os.chdir("/content/drive/MyDrive/fine_pruning")
# os.chdir("CSAW-HackML-2020")
from architecture import Net
from eval import data_loader, data_preprocess

def eval_loaded_model(bd_model, x_test,y_test):
    clean_label_p = np.argmax(bd_model.predict(x_test), axis=1)
    class_accu = np.mean(np.equal(clean_label_p, y_test))*100
    print('Classification accuracy:', class_accu)
    return class_accu

def eval_repaired_model(model_final, test_x, test_y):
  return np.mean(np.equal(model_final(test_x), test_y))*100

def eval(model_filename, clean_data_filename):
    x_test, y_test = data_loader(clean_data_filename)
    x_test = data_preprocess(x_test)

    bd_model = keras.models.load_model(model_filename)
    class_accu = eval_loaded_model(bd_model, x_test,y_test)

    return class_accu
def data_preprocess_and_load(datapath):
 test_x,test_y = data_loader(datapath)
 return data_preprocess(test_x), test_y

def create_output_functions(model, layer_name):
  inp = model.input                                           # input placeholder
  out= model.get_layer(layer_name).output          # all layer outputs
  functors = K.function(inp, out)    # evaluation functions
  return functors

def get_pruned_weights(bd_model, test_x,\
                       layer_name_acts = 'conv_3',\
                       layer_name_weights = 'conv_3',\
                       sparsity_level = 0.1):
  conv_out_func = create_output_functions(bd_model, layer_name_acts)
  activations = conv_out_func(test_x)
  act_indices_sorted = np.argsort(np.mean(activations, axis=(0,1,2)))
  num_nodes_pruned = int((len(act_indices_sorted))*(1- sparsity_level))
  weights, biases =bd_model.get_layer(layer_name_weights).get_weights()
  # biases = bd_model.get_layer(layer_name).trainable_weights[1]
  for j in range(num_nodes_pruned):
    j_pruned_node = act_indices_sorted[j]
    weights[...,j_pruned_node] = 0.
    biases[...,j_pruned_node] = 0.

  return weights, biases

def prune_model(model_path, sparsity_level, test_x, ):
  bd_model= keras.models.load_model(model_path)
  ww4, b4 = get_pruned_weights(bd_model, test_x,\
                               layer_name_acts='conv_4',\
                               layer_name_weights='conv_4',\
                               sparsity_level= sparsity_level)
  ww3, b3 = get_pruned_weights(bd_model, test_x, \
                             layer_name_acts='pool_3',\
                             layer_name_weights='conv_3',\
                             sparsity_level= sparsity_level)
  model_2 = keras.models.load_model(model_path)
  model_2.get_layer('conv_4').set_weights([ww4,b4])
  model_2.get_layer('conv_3').set_weights([ww3,b3])
  return model_2, bd_model

def repair_model(model_path, validation_data_path,\
                 sparsity_level = 0.1,\
                 lr= 3e-4,\
                 epochs = 2):
  test_x, test_y = data_preprocess_and_load(validation_data_path)
  model_2, bd_model = prune_model(model_path, sparsity_level, test_x)
  opt = tf.optimizers.Adam(learning_rate= lr )
  model_2.compile(opt,tf.losses.sparse_categorical_crossentropy)
  model_2.fit( test_x, test_y,epochs=epochs)
  return  model_2, bd_model

def final_model_constructor(repaired_model, bd_model, num_classes =1282):
  def inference_function(test_x,):
    y_repaired = np.argmax(repaired_model.predict(test_x), axis= -1)
    y_badnet = np.argmax(bd_model.predict(test_x), axis= -1)
    backdoor_indicator = np.equal(y_repaired,y_badnet)
    return y_repaired*backdoor_indicator + (num_classes + 1) * (1- backdoor_indicator)

  return inference_function

def build_parser():
  parser = argparse.ArgumentParser()
  parser.add_argument('--clean_valid_data_path', default='./data/clean_validation_data.h5')
  parser.add_argument('--clean_test_data_path', default="./data/clean_test_data.h5")
  parser.add_argument('--sunglasses_data_path', default="./data/sunglasses_poisoned_data.h5")
  parser.add_argument('--sunglasses_model_path', default= "./models/sunglasses_bd_net.h5")
  parser.add_argument('--eyebrows_data_path', default="./data/Multi-trigger Multi-target/eyebrows_poisoned_data.h5")
  parser.add_argument('--multi_trigger_model_path', default="./models/multi_trigger_multi_target_bd_net.h5")
  parser.add_argument('--anonymous_model_path', default="./models/anonymous_bd_net.h5")
  return parser

if __name__ == '__main__':
  parser = build_parser()
  args = parser.parse_args()

  test_x, test_y = data_preprocess_and_load(args.clean_valid_data_path)
  test_x_unseen, test_y_unseen = data_preprocess_and_load(args.clean_test_data_path)
  test_x_sunglasses, test_y_sunglasses = data_preprocess_and_load(args.sunglasses_data_path)
  test_x_eyebrows, test_y_eyebrows = data_preprocess_and_load(args.eyebrows_data_path)

  repaired_model, bd_model = repair_model(args.sunglasses_model_path, args.clean_valid_data_path, sparsity_level=0.1)
  model_final = final_model_constructor(repaired_model, bd_model)

  clean_acc = eval_repaired_model(model_final, test_x_unseen, test_y_unseen)
  poisoned_acc = eval_repaired_model(model_final, test_x_sunglasses, test_y_sunglasses)
  print('testing on the sunglasses badnet')
  print("clean accuracy on heldout set is", clean_acc)
  print("attack success rate is", poisoned_acc)

  test_x, test_y = data_preprocess_and_load(args.clean_valid_data_path)
  test_x_unseen, test_y_unseen = data_preprocess_and_load(args.clean_test_data_path)
  test_x_sunglasses, test_y_sunglasses = data_preprocess_and_load(args.sunglasses_data_path)
  test_x_eyebrows, test_y_eyebrows = data_preprocess_and_load(args.eyebrows_data_path)

  repaired_model, bd_model = repair_model(args.multi_trigger_model_path, args.clean_valid_data_path, sparsity_level=0.1)
  model_final = final_model_constructor(repaired_model, bd_model)

  clean_acc = eval_repaired_model(model_final, test_x_unseen, test_y_unseen)
  poisoned_acc = eval_repaired_model(model_final, test_x_eyebrows, test_y_eyebrows)
  print("testing on the eyebrows badnet")
  print("clean accuracy on heldout set is", clean_acc)
  print("attack success rate is", poisoned_acc)


text:
import unittest
import logging

import numpy as np

from smac.scenario.scenario import Scenario
from smac.tae.execute_ta_run import StatusType
from smac.runhistory.runhistory import RunHistory
from smac.stats.stats import Stats
from smac.optimizer.objective import average_cost
from smac.utils.io.traj_logging import TrajLogger
from smac.utils.validate import Validator

class ValidationTest(unittest.TestCase):

    def setUp(self):
        logging.basicConfig()
        self.logger = logging.getLogger(self.__module__ + "." + self.__class__.__name__)
        self.logger.setLevel(logging.DEBUG)
        self.rng = np.random.RandomState(seed=42)
        self.scen = Scenario({'run_obj': 'quality', 'param_file':
                              'test/test_files/validation/test_validation_pcs.pcs',
                              'algo': 'python -u test/test_files/example_ta.py'})
        self.train_insts = ['0', '1', '2']
        self.test_insts = ['3', '4', '5']
        self.scen.instance_specific = {'0': 'null', '1': 'one', '2': 'two',
                                       '3': 'three', '4': 'four', '5': 'five'}
        self.output_rh = 'test/test_files/validation/test_validation_rh.json'
        self.trajectory = TrajLogger.read_traj_aclib_format(
            fn='test/test_files/validation/test_validation_traj.json', cs=self.scen.cs)

    def test_get_configs(self):
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        self.assertEqual(1, len(validator._get_configs("def")))
        self.assertEqual(1, len(validator._get_configs("inc")))
        self.assertEqual(2, len(validator._get_configs("def+inc")))
        self.assertEqual(9, len(validator._get_configs("time")))
        self.assertEqual(9, len(validator._get_configs("all")))

    def test_get_runs(self):
        ''' test if the runs are generated as expected '''
        self.scen.train_insts = self.train_insts
        self.scen.test_insts = self.test_insts
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        # Get multiple configs
        expected = [{'inst_specs': 'three', 'seed': 1608637542, 'inst': '3', 'config': 'config1'},
                    {'inst_specs': 'three', 'seed': 1608637542, 'inst': '3', 'config': 'config2'},
                    {'inst_specs': 'three', 'seed': 1273642419, 'inst': '3', 'config': 'config1'},
                    {'inst_specs': 'three', 'seed': 1273642419, 'inst': '3', 'config': 'config2'},
                    {'inst_specs': 'four',  'seed': 1935803228, 'inst': '4', 'config': 'config1'},
                    {'inst_specs': 'four',  'seed': 1935803228, 'inst': '4', 'config': 'config2'},
                    {'inst_specs': 'four',  'seed': 787846414,  'inst': '4', 'config': 'config1'},
                    {'inst_specs': 'four',  'seed': 787846414,  'inst': '4', 'config': 'config2'},
                    {'inst_specs': 'five',  'seed': 996406378,  'inst': '5', 'config': 'config1'},
                    {'inst_specs': 'five',  'seed': 996406378,  'inst': '5', 'config': 'config2'},
                    {'inst_specs': 'five',  'seed': 1201263687, 'inst': '5', 'config': 'config1'},
                    {'inst_specs': 'five',  'seed': 1201263687, 'inst': '5', 'config': 'config2'}]

        runs = validator.get_runs(['config1', 'config2'], self.scen.test_insts, repetitions=2)
        self.assertEqual(runs, expected)

        # Only train
        expected = [{'inst_specs': 'null', 'seed': 423734972,  'inst': '0', 'config': 'config1'},
                    {'inst_specs': 'null', 'seed': 415968276,  'inst': '0', 'config': 'config1'},
                    {'inst_specs': 'one',  'seed': 670094950,  'inst': '1', 'config': 'config1'},
                    {'inst_specs': 'one',  'seed': 1914837113, 'inst': '1', 'config': 'config1'},
                    {'inst_specs': 'two',  'seed': 669991378,  'inst': '2', 'config': 'config1'},
                    {'inst_specs': 'two',  'seed': 429389014,  'inst': '2', 'config': 'config1'}]

        runs = validator.get_runs(['config1'], self.scen.train_insts, repetitions=2)
        self.assertEqual(runs, expected)

        # Test and train
        expected = [{'inst': '0', 'seed': 249467210,  'config': 'config1', 'inst_specs': 'null'},
                    {'inst': '1', 'seed': 1972458954, 'config': 'config1', 'inst_specs': 'one'},
                    {'inst': '2', 'seed': 1572714583, 'config': 'config1', 'inst_specs': 'two'},
                    {'inst': '3', 'seed': 1433267572, 'config': 'config1', 'inst_specs': 'three'},
                    {'inst': '4', 'seed': 434285667,  'config': 'config1', 'inst_specs': 'four'},
                    {'inst': '5', 'seed': 613608295,  'config': 'config1', 'inst_specs': 'five'}]
        insts = self.train_insts
        insts.extend(self.test_insts)
        runs = validator.get_runs(['config1'], insts, repetitions=1)
        self.assertEqual(runs, expected)

    def test_validate(self):
        ''' test validation '''
        self.scen.train_insts = self.train_insts
        self.scen.test_insts = self.test_insts
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        # Test basic usage
        rh = validator.validate(config_mode='def', instance_mode='test',
                                repetitions=3)
        self.assertEqual(len(rh.get_all_configs()), 1)
        self.assertEqual(len(rh.get_runs_for_config(rh.get_all_configs()[0])), 9)

        rh = validator.validate(config_mode='inc', instance_mode='train+test')
        self.assertEqual(len(rh.get_all_configs()), 1)
        self.assertEqual(len(rh.get_runs_for_config(rh.get_all_configs()[0])), 6)

        rh = validator.validate(config_mode='time', instance_mode='train')
        self.assertEqual(len(rh.get_all_configs()), 9)
        self.assertEqual(sum([len(rh.get_runs_for_config(c)) for c in
                              rh.get_all_configs()]), 27)

        # Test with backend multiprocessing
        rh = validator.validate(config_mode='def', instance_mode='test',
                                repetitions=3, backend='multiprocessing')
        self.assertEqual(len(rh.get_all_configs()), 1)
        self.assertEqual(len(rh.get_runs_for_config(rh.get_all_configs()[0])), 9)

    def test_validate_no_insts(self):
        ''' no instances '''
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        rh = validator.validate(config_mode='def+inc',
                                instance_mode='train', repetitions=3)
        self.assertEqual(len(rh.get_all_configs()), 2)
        self.assertEqual(sum([len(rh.get_runs_for_config(c)) for c in
                              rh.get_all_configs()]), 6)

    def test_validate_deterministic(self):
        ''' deterministic ta '''
        self.scen.deterministic = True
        self.scen.train_insts = self.train_insts
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        rh = validator.validate(config_mode='def+inc',
                                instance_mode='train', repetitions=3)
        self.assertEqual(len(rh.get_all_configs()), 2)
        self.assertEqual(sum([len(rh.get_runs_for_config(c)) for c in
                              rh.get_all_configs()]), 6)

    def test_parallel(self):
        ''' test parallel '''
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        validator.validate(config_mode='all', instance_mode='train+test', n_jobs=-1)

    def test_passed_runhistory(self):
        ''' test if passed runhistory is in resulting runhistory '''
        self.scen.train_insts = self.train_insts
        self.scen.test_insts = self.test_insts
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        # Add a few runs and check, if they are correctly processed
        old_configs = [entry["incumbent"] for entry in self.trajectory]
        old_rh = RunHistory(average_cost)
        for config in old_configs[:int(len(old_configs)/2)]:
            old_rh.add(config, 1, 1, StatusType.SUCCESS, instance_id='0',
                       seed=127)

        configs = validator._get_configs('all')
        insts = validator._get_instances('train')
        runs_w_rh = validator.get_runs(configs, insts, repetitions=2,
                                       runhistory=old_rh)
        runs_wo_rh = validator.get_runs(configs, insts, repetitions=2)
        self.assertEqual(len(runs_w_rh), len(runs_wo_rh) - 4)

    def test_passed_runhistory_deterministic(self):
        ''' test if passed runhistory is in resulting runhistory '''
        self.scen.deterministic = True
        self.scen.train_insts = self.train_insts
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        # Add a few runs and check, if they are correctly processed
        old_configs = [entry["incumbent"] for entry in self.trajectory]
        old_rh = RunHistory(average_cost)
        for config in old_configs[:int(len(old_configs)/2)]:
            old_rh.add(config, 1, 1, StatusType.SUCCESS, instance_id='0')

        configs = validator._get_configs('all')
        insts = validator._get_instances('train')
        runs_w_rh = validator.get_runs(configs, insts, repetitions=2,
                                       runhistory=old_rh)
        runs_wo_rh = validator.get_runs(configs, insts, repetitions=2)
        self.assertEqual(len(runs_w_rh), len(runs_wo_rh) - 4)

    def test_passed_runhistory_no_insts(self):
        ''' test passed runhistory, without instances '''
        validator = Validator(self.scen, self.trajectory,
                              self.output_rh, self.rng)
        # Add a few runs and check, if they are correctly processed
        old_configs = [entry["incumbent"] for entry in self.trajectory]
        old_rh = RunHistory(average_cost)
        for config in old_configs[:int(len(old_configs)/2)]:
            old_rh.add(config, 1, 1, StatusType.SUCCESS, seed=127)

        configs = validator._get_configs('all')
        insts = validator._get_instances('train')
        runs_w_rh = validator.get_runs(configs, insts, repetitions=2,
                                       runhistory=old_rh)
        runs_wo_rh = validator.get_runs(configs, insts, repetitions=2)
        self.assertEqual(len(runs_w_rh), len(runs_wo_rh) - 4)


text:
"""
Measures the taskspace tracking error for aslip policies individually for each speed the policy accepts
"""

import os, sys, argparse
sys.path.append("../..")

from cassie.trajectory import get_ref_aslip_global_state_no_drift_correct
from cassie import CassieEnv, CassiePlayground
from rl.policies.actor import GaussianMLP_Actor

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import pickle
import numpy as np
import torch
import time

def set_axes_equal(ax):
    '''Make axes of 3D plot have equal scale so that spheres appear as spheres,
    cubes as cubes, etc..  This is one possible solution to Matplotlib's
    ax.set_aspect('equal') and ax.axis('equal') not working for 3D.

    Input
      ax: a matplotlib axis, e.g., as output from plt.gca().
    '''

    x_limits = ax.get_xlim3d()
    y_limits = ax.get_ylim3d()
    z_limits = ax.get_zlim3d()

    x_range = abs(x_limits[1] - x_limits[0])
    x_middle = np.mean(x_limits)
    y_range = abs(y_limits[1] - y_limits[0])
    y_middle = np.mean(y_limits)
    z_range = abs(z_limits[1] - z_limits[0])
    z_middle = np.mean(z_limits)

    # The plot bounding box is a sphere in the sense of the infinity
    # norm, hence I call half the max range the plot radius.
    plot_radius = 0.5*max([x_range, y_range, z_range])

    ax.set_xlim3d([x_middle - plot_radius, x_middle + plot_radius])
    ax.set_ylim3d([y_middle - plot_radius, y_middle + plot_radius])
    ax.set_zlim3d([z_middle - plot_radius, z_middle + plot_radius])

def eval_policy(policies, args, run_args, testing_speed, num_steps=10):

    all_l_foot_poses_actual = []
    all_l_foot_poses_ideal = []
    all_r_foot_poses_actual = []
    all_r_foot_poses_ideal = []
    all_footplace_err = []

    for policy in policies:

        l_foot_poses_actual = []
        l_foot_poses_ideal = []
        r_foot_poses_actual = []
        r_foot_poses_ideal = []
        footplace_err = []

        # run_args.dyn_random = True
        env = CassieEnv(traj="aslip", state_est=run_args.state_est, no_delta=run_args.no_delta, learn_gains=run_args.learn_gains, ik_baseline=run_args.ik_baseline, dynamics_randomization=run_args.dyn_random, clock_based=run_args.clock_based, reward="aslip_old", history=run_args.history)

        if args.debug:
            env.debug = True

        max_traj_len = args.traj_len
        visualize = not args.no_vis

        if visualize:
            env.render()
        print(env.reward_func)

        if hasattr(policy, 'init_hidden_state'):
            policy.init_hidden_state()

        orient_add = 0

        render_state = True

        for trial in range(1):

            state = env.reset_for_test()
            done = False
            timesteps = 0
            eval_reward = 0

            # Data to track
            left_swing, right_swing, l_ideal_land_pos, r_ideal_land_pos = False, False, None, None
            footstep_count = 0

            env.update_speed(testing_speed)
            print(env.speed)

            ## Get the fixed delta for each footstep position change
            right_delta = get_ref_aslip_global_state_no_drift_correct(env, phase=7)

            while footstep_count-1 < num_steps:

                if hasattr(env, 'simrate'):
                    start = time.time()

                # if (not env.vis.ispaused()):
                # Update Orientation
                env.orient_add = orient_add
                # quaternion = euler2quat(z=orient_add, y=0, x=0)
                # iquaternion = inverse_quaternion(quaternion)

                # # TODO: Should probably not assume these indices. Should make them not hard coded
                # if env.state_est:
                #     curr_orient = state[1:5]
                #     curr_transvel = state[15:18]
                # else:
                #     curr_orient = state[2:6]
                #     curr_transvel = state[20:23]

                # new_orient = quaternion_product(iquaternion, curr_orient)

                # if new_orient[0] < 0:
                #     new_orient = -new_orient

                # new_translationalVelocity = rotate_by_quaternion(curr_transvel, iquaternion)

                # if env.state_est:
                #     state[1:5] = torch.FloatTensor(new_orient)
                #     state[15:18] = torch.FloatTensor(new_translationalVelocity)
                #     # state[0] = 1      # For use with StateEst. Replicate hack that height is always set to one on hardware.
                # else:
                #     state[2:6] = torch.FloatTensor(new_orient)
                #     state[20:23] = torch.FloatTensor(new_translationalVelocity)

                action = policy.forward(torch.Tensor(state), deterministic=True).detach().numpy()

                state, reward, done, _ = env.step(action)

                # get the delta for right and left by looking at phases where both feet are in stance
                right_to_left = get_ref_aslip_global_state_no_drift_correct(env, phase=16)[2][:2] - get_ref_aslip_global_state_no_drift_correct(env, phase=16)[0][:2]
                left_to_right = get_ref_aslip_global_state_no_drift_correct(env, phase=29)[0][:2] - get_ref_aslip_global_state_no_drift_correct(env, phase=29)[2][:2]
                delta = right_to_left + left_to_right
                # print(right_to_left)
                # print(left_to_right)
                # print(delta)
                # print(get_ref_aslip_global_state_no_drift_correct(env, phase=16)[2][:2])
                # print(get_ref_aslip_global_state_no_drift_correct(env, phase=16)[2][:2] + delta)
                # exit()

                # print("{} : {} : {}".format(env.phase, env.l_foot_pos[2], env.r_foot_pos[2]))
                # allow some ramp up time
                if timesteps > env.phaselen * 2:
                    # fill r_last_ideal_pos and l_last_ideal_pos with values before collecting data

                    # check if we are in left swing phase
                    # left foot swing, right foot stance
                    if env.phase == 7:
                        left_swing = True
                        right_swing = False
                        r_actual_land_pos = env.r_foot_pos[:2]
                        # if we have data from last step, we can calculate error from where right foot landed to where it should have landed, BEFORE updating r_ideal_land_pos
                        if footstep_count >= 1:
                            r_foot_poses_actual.append(r_actual_land_pos)
                            footplace_err.append(np.linalg.norm(r_ideal_land_pos - r_actual_land_pos))
                            # print("right landed at\t\t\t{}".format(r_actual_land_pos))
                        l_ideal_land_pos = r_actual_land_pos + right_to_left
                        l_foot_poses_ideal.append(l_ideal_land_pos)
                        # print("next, left should land at\t{}\t{}\n".format(l_ideal_land_pos, r_actual_land_pos))
                        # print("left-swing : right should land at {}".format(r_ideal_land_pos))
                    # left foot stance, right foot swing
                    elif env.phase == 23:
                        left_swing = False
                        right_swing = True
                        l_actual_land_pos = env.l_foot_pos[:2]
                        # if we have data from last step, we can calculate error from where right foot landed to where it should have landed, BEFORE updating r_ideal_land_pos
                        if footstep_count >= 1:
                            l_foot_poses_actual.append(l_actual_land_pos)
                            footplace_err.append(np.linalg.norm(l_ideal_land_pos - l_actual_land_pos))
                            # print("left landed at\t\t\t{}".format(l_actual_land_pos))
                        r_ideal_land_pos = l_actual_land_pos + left_to_right
                        r_foot_poses_ideal.append(r_ideal_land_pos)
                        # print("next, right should land at\t{}\t{}\n".format(r_ideal_land_pos, l_actual_land_pos))
                        footstep_count += 1
                        # print("right-swing : left should land at {}".format(l_ideal_land_pos))

                eval_reward += reward
                timesteps += 1
                qvel = env.sim.qvel()
                # print("actual speed: ", np.linalg.norm(qvel[0:2]))
                # print("commanded speed: ", env.speed)

                if visualize:
                    render_state = env.render()
                # if hasattr(env, 'simrate'):
                #     # assume 40hz
                #     end = time.time()
                #     delaytime = max(0, 1000 / 40000 - (end-start))
                #     time.sleep(delaytime)

        # trim the ideal footplace poses
        l_foot_poses_ideal = l_foot_poses_ideal[1:]
        r_foot_poses_ideal = r_foot_poses_ideal[:-1]

        all_l_foot_poses_actual.extend(l_foot_poses_actual)
        all_l_foot_poses_ideal.extend(l_foot_poses_ideal)
        all_r_foot_poses_actual.extend(r_foot_poses_actual)
        all_r_foot_poses_ideal.extend(r_foot_poses_ideal)
        all_footplace_err.append(np.array(footplace_err))

    print("Avg Foot Placement Error: ", np.mean(all_footplace_err, axis=1))
    print("Stddev Foot Placement Error: ", np.std(all_footplace_err, axis=1))
    print("Max/Min Foot Placement Error:  {} / {}".format(np.max(all_footplace_err, axis=1), np.min(all_footplace_err, axis=1)))
    print("Num pairs: ", len(all_footplace_err) * len(all_footplace_err[0]))

    all_l_foot_poses_ideal = np.array(all_l_foot_poses_ideal)
    all_r_foot_poses_ideal = np.array(all_r_foot_poses_ideal)
    all_l_foot_poses_actual = np.array(all_l_foot_poses_actual)
    all_r_foot_poses_actual = np.array(all_r_foot_poses_actual)
    all_footplace_err = np.array(all_footplace_err)
    # print(FULL_all_footplace_err.shape)
    # exit()

    fig = plt.figure(figsize=(10,10))
    ax = fig.add_subplot(111)

    ax.scatter(all_l_foot_poses_ideal[:, 0], all_l_foot_poses_ideal[:, 1], c='tab:green', label='desired', alpha=0.5)
    ax.scatter(all_r_foot_poses_ideal[:, 0], all_r_foot_poses_ideal[:, 1], c='tab:green', alpha=0.5)
    ax.scatter(all_l_foot_poses_actual[:, 0], all_l_foot_poses_actual[:, 1], c='tab:red', label='actual', alpha=0.5)
    ax.scatter(all_r_foot_poses_actual[:, 0], all_r_foot_poses_actual[:, 1], c='tab:red', alpha=0.5)
    ax.axis('equal')
    ax.set_ylabel('y (m)')
    ax.set_xlabel('x (m)')
    ax.set_title('Desired vs Actual Foot Placements ({} m/s)'.format(testing_speed))
    plt.savefig('./plots/footplace{}.png'.format(testing_speed))
    # plt.show()

    return np.mean(all_footplace_err, axis=1), np.std(all_footplace_err, axis=1)

def get_data():

    parser = argparse.ArgumentParser()
    parser.add_argument("--path", type=str, default="../../trained_models/ppo/Cassie-v0/IK_traj-aslip_aslip_old_2048_12288_seed-10/", help="path to folder containing policy and run details")
    parser.add_argument("--traj_len", default=400, type=str)
    parser.add_argument("--num_steps", default=10, type=str)
    parser.add_argument("--debug", default=False, action='store_true')
    parser.add_argument("--no_vis", default=False, action='store_true')
    parser.add_argument("--eval", default=True, action="store_false", help="Whether to call policy.eval() or not")

    args = parser.parse_args()

    paths = ["../../trained_models/ppo/Cassie-v0/traj-aslip_aslip_old_2048_12288_seed-0/",
            "../../trained_models/ppo/Cassie-v0/traj-aslip_aslip_old_2048_12288_seed-10/",
            "../../trained_models/ppo/Cassie-v0/traj-aslip_aslip_old_2048_12288_seed-20/",
            "../../trained_models/ppo/Cassie-v0/traj-aslip_aslip_old_2048_12288_seed-30/"]

    run_args = pickle.load(open(paths[0] + "experiment.pkl", "rb"))

    policies = [torch.load(paths[i] + "actor.pt") for i in range(len(paths))]

    if args.eval:
        [policy.eval() for policy in policies]  # NOTE: for some reason the saved nodelta_neutral_stateest_symmetry policy needs this but it breaks all new policies...

    data = []
    stddevs = []
    speeds = [i/10 for i in range(0,21)] # 0.0 to 2.0 m/s
    # speeds = [i/10 for i in range(3, 5)] # 0.0 to 2.0 m/s

    for speed in speeds:
        d, s = eval_policy(policies, args, run_args, speed, num_steps=args.num_steps)
        data.append(d)
        stddevs.append(s)
    data = np.array(data)
    stddevs = np.array(stddevs)

    with open('data.pkl', 'wb') as f:
        pickle.dump(data, f)
    with open('stddevs.pkl', 'wb') as f:
        pickle.dump(stddevs, f)

get_data()

## Plot foot placement
speeds = [i/10 for i in range(0,21)] # 0.0 to 2.0 m/s
# speeds = [i/10 for i in range(3, 5)]

with open('data.pkl', 'rb') as f:
    data = pickle.load(f)
with open('stddevs.pkl', 'rb') as f:
    stddevs = pickle.load(f)

print(data.shape)

# Foot Placement tracking error
fig, axs = plt.subplots(data.shape[1], 1, sharex=False, figsize=(5,10))
for i, ax in enumerate(axs):
    # iterate over speed now
    for j in range(data.shape[0]):
        error, err_range = data[j,i]*100, stddevs[j,i]*100
        ax.bar(j, error, yerr=err_range, capsize=3, color='dodgerblue')
    plt.xticks(np.arange(len(speeds)), [str(speed) for speed in speeds], rotation=45)
    for index, label in enumerate(ax.xaxis.get_ticklabels()):
        if index % 2 != 0:
            label.set_visible(False)

# ax.set_title('Average Foot Placement Error')
ax.set_ylabel('Avg. Error (cm)')
ax.set_xlabel('Commanded Speed (m/s)')
plt.savefig("./plots/footpos_err.png")
# plt.show()

max_stars_repo_name: osudrl/ASLIP-RL

max_issues_repo_name: osudrl/ASLIP-RL

max_forks_repo_name: osudrl/ASLIP-RL


text:
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

import os
print(os.listdir("../input"))

# Any results you write to the current directory are saved as output.

import sys

sys.path.insert(0, '../input/wordbatch-133/wordbatch/')
sys.path.insert(0, '../input/randomstate/randomstate/')
import wordbatch
from sklearn.metrics import mean_squared_error

from wordbatch.extractors import WordHash
from wordbatch.models import FM_FTRL
from wordbatch.data_utils import *
import threading
import pandas as pd
from sklearn.metrics import roc_auc_score
import time
import numpy as np
import gc
from contextlib import contextmanager
from sklearn import preprocessing
from sklearn.feature_extraction.text import TfidfVectorizer,CountVectorizer
import regex as re
from scipy.sparse import hstack
from nltk.corpus import stopwords
from sklearn.model_selection import KFold
stopWords = stopwords.words('russian')

@contextmanager
def timer(name):
    t0 = time.time()
    yield
    print('[{}] done in {:.0f} s'.format(name,(time.time() - t0)))

import os, psutil

start_time = time.time()

def df2csr(df, traindex, testdex, len_train):
    df.reset_index(drop=True, inplace=True)
    with timer("Adding Time Variables"):
        df["Year"] = df["activation_date"].dt.year
        df["Date of Year"] = df['activation_date'].dt.dayofyear # Day of Year
        df["Weekday"] = df['activation_date'].dt.weekday
        df["Weekd of Year"] = df['activation_date'].dt.week
        df["Day of Month"] = df['activation_date'].dt.day
        df["Quarter"] = df['activation_date'].dt.quarter
        df.drop(["activation_date","image"],axis=1,inplace=True)

    with timer("Encode Variables"):
        categorical = ["user_id","region","city","parent_category_name","category_name","item_seq_number","user_type"]
        messy_categorical = ["param_1","param_2","param_3"] # Need to find better technique for these
        # Encoder:
        lbl = preprocessing.LabelEncoder()
        for col in categorical + messy_categorical:
            df[col] = lbl.fit_transform(df[col].astype(str))
        del lbl
        gc.collect()

    with timer("Log features"):
        for fea in ['price','image_top_1']:
            df[fea]= np.log2(1 + df[fea].values).astype(int)

    with timer("splitting to train and test"):
        trn = df[:len_train].copy()
        print("Training Set shape",trn.shape)
        tst = df[len_train:].copy()
        print("Submission Set Shape: {} Rows, {} Columns".format(*tst.shape))
        del df
        gc.collect()

    with timer("Generating str_array"):
        trn_str_array = ("Y" + trn['Year'].astype(str) \
            + " DY" + trn['Date of Year'].astype(str) \
            + " WD" + trn['Weekday'].astype(str) \
            + " WDY" + trn['Weekd of Year'].astype(str) \
            + " DM" + trn['Day of Month'].astype(str) \
            + " Q" + trn['Quarter'].astype(str) \
            + " UI" + trn['user_id'].astype(str) \
            + " R" + trn['region'].astype(str) \
            + " C" + trn['city'].astype(str) \
            + " PCN" + trn['parent_category_name'].astype(str) \
            + " CN" + trn['category_name'].astype(str) \
            + " ISN" + trn['item_seq_number'].astype(str) \
            + " UT" + trn['user_type'].astype(str) \
            + " PO" + trn['param_1'].astype(str) \
            + " PT" + trn['param_2'].astype(str) \
            + " PTT" + trn['param_3'].astype(str) \
            + " T" + trn['title'].astype(str) \

            + " UXC" + trn['user_id'].astype(str)+"_"+trn['city'].astype(str) \
            + " RXC" + trn['region'].astype(str)+"_"+trn['city'].astype(str) \
            + " CXI" + trn['category_name'].astype(str)+"_"+trn['item_seq_number'].astype(str) \
            + " UXU" + trn['user_type'].astype(str)+"_"+trn['user_id'].astype(str) \
            + " PXI" + trn['parent_category_name'].astype(str)+"_"+trn['item_seq_number'].astype(str) \

            + " P" + trn['price'].astype(str) \
            + " IT" + trn['image_top_1'].astype(str)).values
        tst_str_array = ("Y" + tst['Year'].astype(str) \
            + " DY" + tst['Date of Year'].astype(str) \
            + " WD" + tst['Weekday'].astype(str) \
            + " WDY" + tst['Weekd of Year'].astype(str) \
            + " DM" + tst['Day of Month'].astype(str) \
            + " Q" + tst['Quarter'].astype(str) \
            + " UI" + tst['user_id'].astype(str) \
            + " R" + tst['region'].astype(str) \
            + " C" + tst['city'].astype(str) \
            + " PCN" + tst['parent_category_name'].astype(str) \
            + " CN" + tst['category_name'].astype(str) \
            + " ISN" + tst['item_seq_number'].astype(str) \
            + " UT" + tst['user_type'].astype(str) \
            + " PO" + tst['param_1'].astype(str) \
            + " PT" + tst['param_2'].astype(str) \
            + " PTT" + tst['param_3'].astype(str) \
            + " T" + tst['title'].astype(str) \

            + " UXC" + tst['user_id'].astype(str)+"_"+tst['city'].astype(str) \
            + " RXC" + tst['region'].astype(str)+"_"+tst['city'].astype(str) \
            + " CXI" + tst['category_name'].astype(str)+"_"+tst['item_seq_number'].astype(str) \
            + " UXU" + tst['user_type'].astype(str)+"_"+tst['user_id'].astype(str) \
            + " PXI" + tst['parent_category_name'].astype(str)+"_"+tst['item_seq_number'].astype(str) \

            + " P" + tst['price'].astype(str) \
            + " IT" + tst['image_top_1'].astype(str)).values
    del trn
    gc.collect()
    del tst
    gc.collect()
    return trn_str_array , tst_str_array

def char_analyzer(text):
    """
    This is used to split strings in small lots
    anttip saw this in an article
    so <talk> and <talking> would have <Tal> <alk> in common
    should be similar to russian I guess
    """
    tokens = text.split()
    return [token[i: i + 3] for token in tokens for i in range(len(token) - 2)]

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

if __name__ == '__main__':
    print("\nData Load Stage")
    training = pd.read_csv('../input/train.csv', index_col="item_id", parse_dates=["activation_date"])
    len_train = len(training)
    traindex = training.index
    testing = pd.read_csv('../input/test.csv', index_col="item_id", parse_dates=["activation_date"])
    testdex = testing.index
    labels = training['deal_probability'].values
    training.drop("deal_probability", axis=1, inplace=True)
    print('Train shape: {} Rows, {} Columns'.format(*training.shape))
    print('Test shape: {} Rows, {} Columns'.format(*testing.shape))
    # Combine Train and Test
    df_all = pd.concat([training, testing], axis=0)
    trnshape = training.shape[0]
    del training, testing
    gc.collect()
    print('\nAll Data shape: {} Rows, {} Columns'.format(*df_all.shape))

    df_all['description'] = df_all['description'].fillna("")
    df_all["text_feature"] = df_all.apply(lambda row: " ".join([str(row["param_1"]),
                                                                str(row["param_2"]), str(row["param_3"])]), axis=1)

    with timer("Tfidf on word"):
        word_vectorizer = TfidfVectorizer(
            sublinear_tf=True,
            strip_accents='unicode',
            tokenizer=lambda x: re.findall(r'[^\p{P}\W]+', x),
            analyzer='word',
            token_pattern=None,
            stop_words=stopWords,
            ngram_range=(1, 1),
            max_features=40000)
        X = word_vectorizer.fit_transform(df_all['description'])
        train_word_features = X[:trnshape]
        test_word_features = X[trnshape:]
        del (X)

    with timer("Tfidf on word"):
        word_vectorizer = TfidfVectorizer(
            sublinear_tf=True,
            strip_accents='unicode',
            tokenizer=lambda x: re.findall(r'[^\p{P}\W]+', x),
            analyzer='word',
            token_pattern=None,
            stop_words=stopWords,
            ngram_range=(1, 2),
            max_features=16000)
        X = word_vectorizer.fit_transform(df_all['title'])
        train_title_word_features = X[:trnshape]
        test_title_word_features = X[trnshape:]
        del (X)
    with timer("CountVectorizer on word"):
        word_vectorizer = CountVectorizer(
            ngram_range=(1, 1),)
        X = word_vectorizer.fit_transform(df_all['text_feature'])
        train_text_feature_word_features = X[:trnshape]
        test_text_feature_word_features = X[trnshape:]
        del (X)

    with timer("Tfidf on char n_gram"):
        char_vectorizer = TfidfVectorizer(
            sublinear_tf=True,
            strip_accents='unicode',
            tokenizer=char_analyzer,
            analyzer='word',
            ngram_range=(1, 4),
            max_features=16000)
        X = char_vectorizer.fit_transform(df_all['title'])
        train_char_features = X[:trnshape]
        test_char_features = X[trnshape:]
        del (X)

    with timer("Tfidf on char n_gram 1,1"):
        char_vectorizer = TfidfVectorizer(
            sublinear_tf=True,
            strip_accents='unicode',
            tokenizer=char_analyzer,
            analyzer='word',
            ngram_range=(1, 1),
            max_features=16000)
        X = char_vectorizer.fit_transform(df_all['description'])
        train_char6_features = X[:trnshape]
        test_char6_features = X[trnshape:]
        del (X)

    with timer("Transforming other features"):
        trn_str_arrays, tst_str_arrays = df2csr(df_all, traindex, testdex, len_train)
        del df_all
        gc.collect

        batchsize = 10000000
        D = 2 ** 10

        wb = wordbatch.WordBatch(None, extractor=(WordHash, {"ngram_range": (1, 1), "analyzer": "word",
                                                             "lowercase": False, "n_features": D,
                                                             "norm": None, "binary": True})
                                 , minibatch_size=batchsize // 80, procs=8, freeze=True, timeout=1800, verbose=0)

        trn_str_array = wb.transform(trn_str_arrays)
        del trn_str_arrays
        gc.collect()
        tst_str_array = wb.transform(tst_str_arrays)
        del tst_str_arrays
        gc.collect()
        del wb
        gc.collect()

    print("DONE")
    with timer("Stacking Features"):
        train_features = hstack(
            [
                train_char_features,
                train_word_features,
                train_title_word_features,
                train_char6_features,
                train_text_feature_word_features
            ]
        )
        del train_word_features, train_char_features,train_title_word_features,train_char6_features,train_text_feature_word_features

        train_features = hstack(
            [
                train_features,
                trn_str_array

            ]
        ).tocsr()
        del trn_str_array
        gc.collect()
        print("Fin Stack Train")
        test_features = hstack(
            [
                test_char_features,
                test_word_features,
                tst_str_array,
                test_title_word_features,
                test_char6_features,
                test_text_feature_word_features

            ]
        ).tocsr()
        del test_word_features, tst_str_array, test_char_features,test_title_word_features,test_char6_features,test_text_feature_word_features
        gc.collect()
        print("Fin Stack test")

    with timer("Scoring FM FTRL"):
        kf = KFold(n_splits=5, random_state=42, shuffle=True)

        model = FM_FTRL(
            alpha=0.02, beta=0.01, L1=0.00001, L2=30.0,
            D=train_features.shape[1], alpha_fm=0.1,
            L2_fm=0.5, init_fm=0.01, weight_fm=50.0,
            D_fm=200, e_noise=0.0, iters=7,#5
            inv_link="identity", e_clip=1.0, threads=6, use_avx=1, verbose=1 #inv_link="identity", e_clip=1.0, threads=1, use_avx=1, verbose=1
        )

        fold_id = -1
        X = train_features
        y = labels
        x_test = test_features

        val_predict= np.zeros(y.shape)

        for train_index, val_index in kf.split(X):
            fold_id += 1
            x_train, x_val = X[train_index], X[val_index]
            y_train, y_val = y[train_index], y[val_index]

            model.fit(x_train, y_train)

            train_pred = model.predict(x_train)
            y_pred = model.predict(x_val)
            val_predict[val_index] = y_pred
            rmse = mean_squared_error(y_val, y_pred) ** 0.5
            print('valid score: {}'.format(rmse))
            sub = pd.read_csv('../input/sample_submission.csv')
            pred = model.predict(x_test)
            sub['deal_probability'] = pred
            sub['deal_probability'].clip(0.0, 1.0, inplace=True)
            print("Output Prediction CSV")
            sub.to_csv('subm/wordbatch_fmtrl_submissionV3_{}.csv'.format(fold_id), index=False)

        # val_predicts = pd.DataFrame(data=val_predict, columns= labels)
        # val_predicts['user_id'] = train_user_ids
        # val_predicts['item_id'] = train_item_ids
        # val_predicts.to_csv('subm/wordbatch_fmtrl_submission_train.csv', index=False)
'''
valid score: 0.22830226415753876

FINAL RMSE 0.22179138519107305, char n_gram 1,1 description

FINAL RMSE 0.22265192127859398 , char n_gram 1,6
FINAL RMSE 0.22265821448778575, char n_gram 1,4
[Scoring FM FTRL] done in 347 s

FINAL RMSE 0.22547715723618647
[Scoring FM FTRL] done in 692 s

FINAL RMSE 0.22721282218651886
[Scoring FM FTRL] done in 641 s

average valid acc:0.943010665961
Save epoch9 at ../data/MTCNN_model/PNet_landmark/PNet

增加一个text_feature
DONE
Fin Stack Train
Fin Stack test
[Stacking Features] done in 22 s
Total e: 189046.20930299623
Total e: 183864.5931029993
Total e: 182518.3256617243
Total e: 181750.69846973865
Total e: 181233.67554650828
Total e: 180853.83779048597
Total e: 180559.64456537634
valid score: 0.2275048093223973
Output Prediction CSV
Total e: 188873.4560661744
Total e: 183712.39039747955
Total e: 182368.29046199698
Total e: 181600.4972747812
Total e: 181083.63226694267
Total e: 180704.6173825646
Total e: 180411.32691318056
valid score: 0.2277848426611388
Output Prediction CSV
Total e: 188844.9140664788
Total e: 183665.8783948139
Total e: 182323.13800638154
Total e: 181559.40195599885
Total e: 181046.14236964888
Total e: 180669.61394944967
Total e: 180378.27942137406
valid score: 0.22792221605280613
Output Prediction CSV
Total e: 188964.6510297849
Total e: 183787.39461554418
Total e: 182442.7112902973
Total e: 181675.55625697025
Total e: 181158.00582514986
Total e: 180777.75450087097
Total e: 180483.69289019858
valid score: 0.2276856289512003
Output Prediction CSV
Total e: 188953.84776714086
Total e: 183750.32084681623
Total e: 182401.5191464039
Total e: 181635.09029368704
Total e: 181120.39854659705
Total e: 180743.25542031662
Total e: 180451.25567295877
valid score: 0.2278115387203652
Output Prediction CSV
[Scoring FM FTRL] done in 4325 s

Process finished with exit code 0

[Stacking Features] done in 21 s
Total e: 190332.13532013315
Total e: 185176.36752191
Total e: 183803.4162622352
Total e: 183022.48111174765
Total e: 182497.66910659662
valid score: 0.22830226415753876
Output Prediction CSV
Total e: 190175.97780981776
Total e: 185029.47584343897
Total e: 183657.40182914195
Total e: 182876.1451740742
Total e: 182350.14800472654
valid score: 0.22858791598445452
Output Prediction CSV
Total e: 190163.9991700796
Total e: 185020.63139812616
Total e: 183652.56760049515
Total e: 182875.41300158898
Total e: 182352.74030912982
valid score: 0.22867367692104767
Output Prediction CSV
Total e: 190237.01160260584
Total e: 185088.29752398795
Total e: 183713.19926152908
Total e: 182928.18302477512
Total e: 182400.8309299546
valid score: 0.22857778377101393
Output Prediction CSV
Total e: 190249.99448752435
Total e: 185079.2242154467
Total e: 183707.0861143455
Total e: 182926.85280229314
Total e: 182401.34665621707
valid score: 0.2285867373708063
Output Prediction CSV
'''

reason: import numpy,from scipy