import pandas as pd
import numpy as np
import sqlite3
from _decorators import timeit
import matplotlib.pyplot as plt

class EventAnalysis(object):

    def __init__(self):

        pass

    def get_event_value_and_time(self, db_con, trigid, channel, tester):

        query = "select sample_time, sample_value from eventdetail where tester = ? and trigid = ? and channel =?"

        temp_df = pd.read_sql(query, db_con, params=[tester, str(trigid), str(channel)])

        times = temp_df.iloc[:, 0].values

        yvalues = temp_df.iloc[:, 1].values

        return yvalues, times

    def calculate_event_amplitude(self, db_con, trigid, tester, channel, scaling_idx,
                                  normal_samples=50):

        yvalues, times = self.get_event_value_and_time(db_con, trigid, channel, tester)

        # toujours lié a un trig qui a des infos liées à une seuele voie
        if len(yvalues) == 0:
            return np.nan

        normal_amplitude = yvalues[-normal_samples:].mean()

        amplitude_bas = normal_amplitude - min(yvalues)

        amplitude_haut = normal_amplitude - max(yvalues)

        event_amplitude = max(abs(amplitude_bas), abs(amplitude_haut))

        return event_amplitude

    def calculate_run_amplitudes(self, db_con):

        my_df = self.get_plotable_trigs_df(db_con=db_con)

        amplitudes = {}

        # row is a tuple (index, Series)
        for row in my_df.iterrows():

            trigid = row[1]["TrigId"]
            tester = "DIGITALIZER"
            channel = row[1]["Channel"]

            amplitude = self.calculate_event_amplitude(db_con,trigid, tester, channel, 0)

            amplitudes[trigid] = amplitude

        return amplitudes

    def get_plotable_trigs_df(self, db_con):

        query = "select tester, clusterid, trigid, time, trigsrc, channel " \
                "from eventnumber" \
                " where eventoccur !=0 and trigsrc != 'manual'" \
                " and trigid in (select distinct trigid from eventdetail)"

        try:
            the_df = pd.read_sql(query, db_con)
            return the_df

        except :  # pd.io.sql.DataBaseError
            print("Probably no EventDetail or Eventnumber in database")
            return None

    def median_filter(self, values, n_samples=5):

        filtered = []

        for index, value in enumerate(values[:-n_samples]):

            filtered.append(np.median(values[index:index+n_samples]))

        return filtered

    # marche pas bien pour l'AOP
    def calculate_event_duration(self, db_con, trigid, tester, channel, scaling_idx, min_thresh, max_thresh,
                                 normal_samples=50, filtered_samples=10):
        """

        :param db_con:
        :param trigid:
        :param tester:
        :param channel:
        :param scaling_idx:
        :param normal_samples:
        :return:
        """

        yvalues, times = self.get_event_value_and_time(db_con, trigid, channel, tester)

        pass
        print("coucou")

        # filtered = self.median_filter(yvalues, filtered_samples)

        # normal_amplitude = yvalues[-normal_samples:].mean()
        normal_amplitude = yvalues[-normal_samples:].min()

    def calculate_run_durations(self, db_con):

        my_df = self.get_plotable_trigs_df(db_con=db_con)

        durations = {}

        # row is a tuple (index, Series)
        for row in my_df.iterrows():

            trigid = row[1]["TrigId"]
            tester = "DIGITALIZER"
            channel = row[1]["Channel"]

            amplitude = self.calculate_event_duration(db_con,trigid, tester, channel, 0)

            durations[trigid] = amplitude

        return durations

    def signal_substraction(self):

        fig, axes = plt.subplots(3, 1)

        y1, t1 = Analyst.get_event_values_and_times(db_con, test_trigid, channel, tester)

        # signal_period = 1.6666e-6

        n_samples = len(y1)

        decalage_samples = n_samples - len(np.where(t1 > t1[0] + signal_period)[0])

        new_n_samples = n_samples + decalage_samples

        t2 = np.zeros(new_n_samples)

        extra_times = (t1[1] - t1[0]) * np.array(list(range(int(decalage_samples)))) + t1[-1]

        t2[n_samples:] = extra_times
        t2[:n_samples] = t1

        y2 = np.zeros(new_n_samples)
        y2[decalage_samples:] = y1

        y3 = np.append(y2[:-decalage_samples] - y1, y2[-decalage_samples:])

        axes[0].scatter(t1, y1)
        axes[1].scatter(t2, y2)
        axes[2].scatter(t2, y3)

        for ax in axes:
            ax.xaxis.set_major_formatter(EngFormatter(unit="s"))

        error_zone = np.where(t2 > temps_avant)
        error_zone_2 = np.where(t2[error_zone] < temps_apres) + error_zone[0][0] - 1

        error_y = y3[error_zone_2[0]]
        error_t = t2[error_zone_2[0]]

        for index, ax in enumerate(axes):
            if index in [0, 1]:
                ax.set_xlim(left=t1.min(), right=t1.max())

            else:
                ax.set_xlim(left=temps_avant, right=temps_apres)

        plt.plot(error_t, error_y, c='r')

        my_min = error_y.min()
        my_max = error_y.max()

        print("min: " + str(my_min) + "; max: " + str(my_max) + "; amplitude: " + str(my_max - my_min))

        fig.suptitle("Trigid: " + str(test_trigid))

        plt.show()


# -*- coding: utf-8 -*-
'''
Created on 07.12.2017

@author: yu03
'''
import visa
import numpy as np
# import time

class RigolScope():
    '''

    '''
    def __init__(self,info):
        '''
            needs the string from Ultra Sigma
        'USB0::0x1AB1::0x04B1::DS4A140800046::INSTR'
        '''
        self.rm = visa.ResourceManager()
        self.dev = self.rm.open_resource(info)
        return

#     def Read_ID(self):
#         return self.dev.query('*IDN?')

    def Read_ID(self):
        self.dev.write('*IDN?')
        self.r = self.dev.read()
        return self.r

    def Write_AUToscale(self):
        self.dev.write(':AUToscale')
        return

    def Write_Run(self):
        self.dev.write(':RUN')
        return

    def Write_Stop(self):
        self.dev.write(':STOP')
        return

    def Write_Single(self):
        self.dev.write(':SINGLE')
        return

    def Read_WavFormat(self):
        '''
            Queries the return format of the waveform data
        '''
        self.dev.write(':WAVeform:FORMat?')
#         return self.dev.query(':WAVeform:FORMat?')
        return self.dev.read()

    def Write_WavFormat_Word(self):
        '''
            Sets the return format of the waveform data to be 'WORD'
        '''
        self.dev.write(':WAVeform:FORMat WORD')
        return

    def Write_WavFormat_Byte(self):
        '''
            Sets the return format of the waveform data to be 'BYTE'
        '''
        self.dev.write(':WAVeform:FORMat BYTE')
        return

    def Write_WavFormat_Ascii(self):
        '''
            Sets the return format of the waveform data to be 'ASCII'
        '''
        self.dev.write(':WAVeform:FORMat ASCii')
#         return

    def Read_WavMode(self):
        '''
            Queries the reading mode of the waveform data
        '''
        return self.dev.query(':WAVeform:MODE?',delay=0.1)

    def Write_WavMode_Norm(self):
        '''
            Sets the reading mode of the waveform data to be 'NORMAL'
        '''
        self.dev.write(':WAVeform:MODE NORMal')
        return

    def Write_WavMode_Raw(self):
        '''
            Sets the reading mode of the waveform data to be 'RAW'
        '''
        self.dev.write(':WAVeform:MODE RAW')
        return

    def Write_WavMode_Max(self):
        '''
            Sets the reading mode of the waveform data to be 'MAXIMUM'
        '''
        self.dev.write(':WAVeform:MODE MAXimum')
        return

    def Read_WavPoints(self):
        '''
            Queries the number of the waveform points to be read
        '''
        return self.dev.query(':WAVeform:POINts?',delay=0.1)

    def Write_WavPoints(self,Setvalue):
        '''
            Sets the number of the waveform points to be read
            NORMal mode: 1 to 1400
            RAW mode: 1 to the current maximum memory depth
            MAXimum mode: 1 to the number of effective points currently on the screen
        '''
        self.dev.write(':WAVeform:POINts %i'%Setvalue)
        return

    def Read_WavSource(self):
        '''
            Queries the source channel of waveform data reading
        '''
        return self.dev.query(':WAVeform:SOURce?',delay=0.1)

    def Write_WavSource_CHAN1(self):
        '''
            Sets the source channel of waveform data reading to be 'CHANNEL 1'
        '''
        self.dev.write('WAVeform:SOURce CHANnel1')
        return

    def Write_WavSource_CHAN2(self):
        '''
            Sets the source channel of waveform data reading to be 'CHANNEL 2'
        '''
        self.dev.write('WAVeform:SOURce CHANnel2')
        return

    def Write_WavSource_CHAN3(self):
        '''
            Sets the source channel of waveform data reading to be 'CHANNEL 3'
        '''
        self.dev.write('WAVeform:SOURce CHANnel3')
        return

    def Write_WavSource_CHAN4(self):
        '''
            Sets the source channel of waveform data reading to be 'CHANNEL 4'
        '''
        self.dev.write('WAVeform:SOURce CHANnel4')
        return

    def Write_WavSource_Math(self):
        '''
            Sets the source channel of waveform data reading to be 'MATH'
        '''
        self.dev.write('WAVeform:SOURceMATH')
        return

    def Write_WavSource_FFT(self):
        '''
            Sets the source channel of waveform data reading to be 'FFT'
        '''
        self.dev.write('WAVeform:SOURce FFT')
        return

    def Read_WavData(self):
        '''
            Queries the waveform data in the buffer
        '''
        return self.dev.query(':WAVeform:DATA?',delay=0.1)

    def Read_WavPreSet(self):
        '''
            Queries the waveform parameter settings related to the waveform data reading
        '''
        return self.dev.query(':WAVeform:PREamble?',delay=0.1)

    def Get_WavData_ASC(self):
        '''
            Get Waveform Data Reading (ASCII) into self.Result[] (float)
        '''
        self.DATA = self.Read_WavData()

        self.DataLen = self.DATA.count(',')
        self.Result=list(range(self.DataLen))
        self.k = 0
        for self.i in range(self.DataLen):
            if self.DATA[self.k] == '-':
                if self.DATA[self.k+7] == '-':
                    self.Result[self.i] = float(self.DATA[self.k:self.k+11])
                    self.k = self.k+12
                    self.i += 1
#                     print self.Result[1]
                else:
                    self.Result[self.i] = float(self.DATA[self.k:self.k+10])
                    self.k = self.k+11
                    self.i += 1
            else:
                if self.DATA[self.k+6] == '-':
                    self.Result[self.i] = float(self.DATA[self.k:self.k+10])
                    self.k = self.k+11
                    self.i += 1
                else:
                    self.Result[self.i] = float(self.DATA[self.k:self.k+9])
                    self.k = self.k+10
                    self.i += 1
        self.Y_Increment = float(self.Read_Y_Increment())
        self.Y_Origin = float(self.Read_Y_Origin())
        self.Y_Ref = self.Read_Y_Reference()
        return self.Result

    def Read_Channel_1_Scale(self):
        '''
            Queries the vertical scale of the specified analog channel.
            The unit is related to the current amplitude unit of the specified analog channel.
            The default unit is V/div.
        '''
        return float(self.dev.query(':CHANnel1:SCALe?',delay=0.1))

    def Read_Y_Increment(self):
        '''
            Queries the unit voltage value of the current source channel (refer to the :WAVeform:SOURce command) in the Y direction.
            The unit (V) is the same as the current amplitude unit of the source channel.
            YINCrement=Vertical Scale/32
        '''
        return float(self.dev.query(':WAVeform:YINCrement?',delay=0.1))

    def Read_Y_Origin(self):
        '''
            Queries the vertical position relative to the vertical reference position (refer to the :WAVeform:YREFerence?command)
            of the current source channel (refer to the :WAVeform:SOURce command) in the Y direction.
        '''
        return float(self.dev.query(':WAVeform:YORigin?',delay=0.1))

    def Read_Y_Reference(self):
        '''
            Queries the vertical reference position of the current source channel (refer to the :WAVeform:SOURcecommand) in the Y direction.
        '''
        return float(self.dev.query(':WAVeform:YREFerence?',delay=0.1))

    def Read_Channel_1_Offset(self):
        '''
            Queries the vertical position of channel_1
        '''
        return float(self.dev.query(':CHANnel1:OFFSet?',delay=0.1))

    def Get_WavData_AllChannel(self):
        '''
            Get Waveform Data Reading (ASCII) for each Channel into self.Result_1-4[] (float)
        '''

        self.Write_WavSource_CHAN1()
        self.Result_Ch1 = self.Get_WavData_ASC()
        self.Y_Scale_1 = self.Read_Y_Increment()*32
        self.Y_Origin_1 = self.Read_Y_Origin()
        self.Result_test = self.Result_Ch1

        self.Write_WavSource_CHAN2()
        self.Result_Ch2 = self.Get_WavData_ASC()
        self.Y_Scale_2 = self.Read_Y_Increment()*32
        self.Y_Origin_2 = self.Read_Y_Origin()

        self.Write_WavSource_CHAN3()
        self.Result_Ch3 = self.Get_WavData_ASC()
        self.Y_Scale_3 = self.Read_Y_Increment()*32
        self.Y_Origin_3 = self.Read_Y_Origin()

#         self.Write_WavSource_CHAN4()
#         self.Result_Ch4 = self.Get_WavData_ASC()
#         self.Y_Scale_4 = self.Read_Y_Increment()*32
#         self.Y_Origin_4 = self.Read_Y_Origin()

# #         self.Result_Ch1 = [(x+self.Y_Scale_2*(self.Y_Origin_2/self.Y_Scale_2-self.Y_Origin_1/self.Y_Scale_1))  for x in self.Result_Ch1]
#         self.Result_Ch1 = [(x)  for x in self.Result_Ch1]
#         self.Result_Ch2 = [(x)  for x in self.Result_Ch2]
#         self.Result_Ch3 = [(x+self.Y_Scale_4*(self.Y_Origin_4/self.Y_Scale_4-self.Y_Origin_3/self.Y_Scale_3))  for x in self.Result_Ch3]
#         self.Result_Ch4 = [(x+self.Y_Scale_1*(self.Y_Origin_1/self.Y_Scale_1-self.Y_Origin_4/self.Y_Scale_4))  for x in self.Result_Ch4]

        return self.Result_Ch1, self.Result_Ch2, self.Result_Ch3

    def Read_X_Increment(self):
        '''
            Queries the time difference (the default unit is s) between two neighboring points
            of the current source channel in the X direction
            XINCrement = TimeScale/100
            !!!Wrong Return!!!
        '''
        return float(self.dev.query(':WAVeform:XINCrement?',delay=0.1))

    def Read_Sample_Rate(self):
        '''
            Queries the current sample rate of the oscilloscope. The default unit is Sa/s.
        '''
        return float(self.dev.query(':ACQuire:SRATe?',delay=0.1))

    def Read_Memory_Depth(self):
        '''
            Queries the current sample rate of the oscilloscope. The default unit is Sa/s.
        '''
        return int(self.dev.query(':ACQuire:MDEPth?',delay=0.1))

    def Write_WavReading_Reset(self):
        '''
            Resets the waveform data reading.
            This command is only applicable to reading waveform data in the internal memory
        '''
        self.dev.write(':WAVeform:RESet')
        return

    def Write_WavReading_Begin(self):
        '''
            Starts the waveform data reading.
            This command is only applicable to reading waveform data in the internal memory
        '''
        self.dev.write(':WAVeform:BEGin')
        return

    def Read_WavReading_Status(self):
        '''
            Queries the current status of waveform data reading.
            IDLE,<n>: indicates that the waveform data reading thread finishes; <n> is the number of waveform points that have been read.
            READ,<n>: indicates that the waveform data reading thread is running; <n> is the number of waveform points that have currently been read
        '''
        return self.dev.query(':WAVeform:STATus?',delay=0.1)

    def Write_WavReading_End(self):
        '''
            Stops the waveform data reading.
            This command is only applicable to reading waveform data in the internal memory
        '''
        self.dev.write(':WAVeform:END')
        return

    def Get_Buffer_Ch1(self):
        '''
            Queries waveform data (and points number) of Channel 1 from the internal memory
        '''
        self.Write_Stop()

        self.Scale_1 = float(self.dev.query(':CHANnel1:SCALe?'))
#         print self.Scale_1
        self.Offset_1 = float(self.dev.query(':CHANnel1:OFFSet?'))
        self.Scale_2 = float(self.dev.query(':CHANnel2:SCALe?'))
        self.Offset_2 = float(self.dev.query(':CHANnel2:OFFSet?'))
        self.dev.write(':CHANnel1:SCALe %f'%self.Scale_1)

        self.Write_WavSource_CHAN1()
        self.Write_WavMode_Raw()
        self.MemDep = self.Read_Memory_Depth()
        self.Write_WavPoints(self.MemDep)
#         self.Write_WavPoints(100)
        self.Write_WavReading_Reset()
        self.Write_WavReading_Begin()
        self.BufData_Ch1 = list()
        self.BufLen_Ch1 = 0
        self.count = 0

        status,remain_cnt = self.Read_WavReading_Status().split(',')

#         while status[0:4]!='READ':
        while remain_cnt[0] == '0':
            status,remain_cnt = self.Read_WavReading_Status().split(',')
        print ('CHANNEL_1 start reading')
        while status[0:4] == 'READ':
            tmp = [float(i) for i in self.Read_WavData().strip().split(',')[:-1]]
            self.BufData_Ch1 = self.BufData_Ch1 + tmp
            self.count += 1
            print (self.count)
            status,remain_cnt = self.Read_WavReading_Status().split(',')

        if (int(remain_cnt)>0):
            tmp = [float(i) for i in self.Read_WavData().strip().split(',')[:-1]]
            self.BufData_Ch1 = self.BufData_Ch1 + tmp
            self.count += 1
            print (self.count)

        if status!='IDLE':
            print ('Unexpected status %s'%status)

        self.Write_WavReading_End()
        self.BufData_Ch1_RealV = [(x + self.Offset_2)/self.Scale_2*self.Scale_1 - self.Offset_1 for x in self.BufData_Ch1] #####Function from test, differ from expected
        print ('finish reading')
        print ('Data Size:', len(self.BufData_Ch1))
        return self.BufData_Ch1_RealV

    def Get_Buffer_Ch2(self):
        '''
            Queries waveform data (and points number) of Channel 2 from the internal memory
        '''
        self.Write_Stop()

        self.Scale_2 = float(self.dev.query(':CHANnel2:SCALe?'))
        self.Offset_2 = float(self.dev.query(':CHANnel2:OFFSet?'))
        self.Scale_3 = float(self.dev.query(':CHANnel3:SCALe?'))
        self.Offset_3 = float(self.dev.query(':CHANnel3:OFFSet?'))
        self.dev.write(':CHANnel2:SCALe %f'%self.Scale_2)

        self.Write_WavSource_CHAN2()
        self.Write_WavMode_Raw()
        self.MemDep = self.Read_Memory_Depth()
        self.Write_WavPoints(self.MemDep)
#         self.Write_WavPoints(100)
        self.Write_WavReading_Reset()
        self.Write_WavReading_Begin()

        self.BufData_Ch2 = list()
        self.BufLen_Ch2 = 0
        self.count = 0

        status,remain_cnt = self.Read_WavReading_Status().split(',')

#         while status[0:4]!='READ':
        while remain_cnt[0] == '0':
            status,remain_cnt = self.Read_WavReading_Status().split(',')

        print ('CHANNEL_2 start reading')

        while status[0:4] == 'READ':
            tmp = [float(i) for i in self.Read_WavData().strip().split(',')[:-1]]
            self.BufData_Ch2 = self.BufData_Ch2 + tmp
            self.count += 1
            print (self.count)
            status,remain_cnt = self.Read_WavReading_Status().split(',')

        if (int(remain_cnt)>0):
            tmp = [float(i) for i in self.Read_WavData().strip().split(',')[:-1]]
            self.BufData_Ch2 = self.BufData_Ch2 + tmp
            self.count += 1
            print (self.count)

        if status!='IDLE':
            print ('Unexpected status %s'%status)

        self.Write_WavReading_End()
        self.BufData_Ch2_RealV = [(x + self.Offset_3)/self.Scale_3*self.Scale_2 - self.Offset_2 for x in self.BufData_Ch2] #####Function from test, differ from expected
        print ('finish reading')
        print ('Data Size:', len(self.BufData_Ch2))
        return self.BufData_Ch2_RealV

#     def Get_Buffer_Ch2(self):
#         '''
#             Queries waveform data (and points number) of Channel 2 from the internal memory
#         '''
#         self.Write_Stop()
#         self.Write_WavSource_CHAN2()
#         self.Write_WavMode_Raw()
#         self.MemDep = self.Read_Memory_Depth()
#         self.Write_WavPoints(self.MemDep)
# #         self.Write_WavPoints(100)
#         self.Write_WavReading_Reset()
#         self.Write_WavReading_Begin()
#         time.sleep(2)
#         self.BufData_Ch2 = list()
#         self.BufLen_Ch2 = 0
#         self.count = 0
#         while self.Read_WavReading_Status()[0:4] == 'READ':
# #             time.sleep(0.1)
#             self.BufData_Ch2 = self.BufData_Ch2 + self.Get_WavData_ASC()
#             self.BufLen_Ch2 = self.BufLen_Ch2 + self.DataLen
#             self.count += 1
# #             time.sleep(1)
#             print self.count
#         else:
# #             time.sleep(0.1)
#             self.BufData_Ch2 = self.BufData_Ch2 + self.Get_WavData_ASC()
#             self.BufLen_Ch2 = self.BufLen_Ch2 + self.DataLen
#             self.count += 1
#             print self.count
#         print self.Read_WavReading_Status()
#         self.Write_WavReading_End()
#         return self.BufData_Ch2, self.BufLen_Ch2

    def Get_Buffer_Ch3(self):
        '''
            Queries waveform data (and points number) of Channel 3 from the internal memory
        '''
        self.Write_Stop()

        self.Scale_3 = float(self.dev.query(':CHANnel3:SCALe?'))
        self.Offset_3 = float(self.dev.query(':CHANnel3:OFFSet?'))
        self.Scale_4 = float(self.dev.query(':CHANnel4:SCALe?'))
        self.Offset_4 = float(self.dev.query(':CHANnel4:OFFSet?'))
        self.dev.write(':CHANnel3:SCALe %f'%self.Scale_3)

        self.Write_WavSource_CHAN3()
        self.Write_WavMode_Raw()
        self.MemDep = self.Read_Memory_Depth()
        self.Write_WavPoints(self.MemDep)
#         self.Write_WavPoints(100)
        self.Write_WavReading_Reset()
        self.Write_WavReading_Begin()
#         time.sleep(2)
        self.BufData_Ch3 = list()
        self.BufLen_Ch3 = 0
        self.count = 0

        status,remain_cnt = self.Read_WavReading_Status().split(',')

#         while status[0:4]!='READ':
        while remain_cnt[0] == '0':
            status,remain_cnt = self.Read_WavReading_Status().split(',')
        print ('CHANNEL_3 start reading')
        while status[0:4] == 'READ':
            tmp = [float(i) for i in self.Read_WavData().strip().split(',')[:-1]]
            self.BufData_Ch3 = self.BufData_Ch3 + tmp
            self.count += 1
            print (self.count)
            status,remain_cnt = self.Read_WavReading_Status().split(',')

        if (int(remain_cnt)>0):
            tmp = [float(i) for i in self.Read_WavData().strip().split(',')[:-1]]
            self.BufData_Ch3 = self.BufData_Ch3 + tmp
            self.count += 1
            print (self.count)

        if status!='IDLE':
            print ('Unexpected status %s'%status)

        self.Write_WavReading_End()
        self.BufData_Ch3_RealV = [((x+self.Offset_4)/self.Scale_4)*self.Scale_3-self.Offset_3 for x in self.BufData_Ch3]#####Function from test, differ from expected
        print ('finish reading')
        print ('Data Size:', len(self.BufData_Ch3))
        return self.BufData_Ch3_RealV

    def Get_Buffer_Ch4(self):
        '''
            Queries waveform data (and points number) of Channel 4 from the internal memory
        '''
        self.Write_Stop()

        self.Scale_4 = float(self.dev.query(':CHANnel4:SCALe?'))
        self.Offset_4 = float(self.dev.query(':CHANnel4:OFFSet?'))
        self.Scale_1 = float(self.dev.query(':CHANnel1:SCALe?'))
        self.Offset_1 = float(self.dev.query(':CHANnel1:OFFSet?'))
        self.dev.write(':CHANnel4:SCALe %f'%self.Scale_4)

        self.Write_WavSource_CHAN4()
        self.Write_WavMode_Raw()
        self.MemDep = self.Read_Memory_Depth()
        self.Write_WavPoints(self.MemDep)
#         self.Write_WavPoints(100)
        self.Write_WavReading_Reset()
        self.Write_WavReading_Begin()
#         time.sleep(2)
        self.BufData_Ch4 = list()
        self.BufLen_Ch4 = 0
        self.count = 0

        status,remain_cnt = self.Read_WavReading_Status().split(',')

#         while status[0:4]!='READ':
        while remain_cnt[0] == '0':
            status,remain_cnt = self.Read_WavReading_Status().split(',')
        print ('CHANNEL_4 start reading')
        while status[0:4] == 'READ':
            tmp = [float(i) for i in self.Read_WavData().strip().split(',')[:-1]]
            self.BufData_Ch4 = self.BufData_Ch4 + tmp
            self.count += 1
            print (self.count)
            status,remain_cnt = self.Read_WavReading_Status().split(',')

        if (int(remain_cnt)>0):
            tmp = [float(i) for i in self.Read_WavData().strip().split(',')[:-1]]
            self.BufData_Ch4 = self.BufData_Ch4 + tmp
            self.count += 1
            print (self.count)

        if status!='IDLE':
            print ('Unexpected status %s'%status)

        self.Write_WavReading_End()
        self.BufData_Ch4_RealV = [((x+self.Offset_1)/self.Scale_1)*self.Scale_4-self.Offset_4 for x in self.BufData_Ch4]#####Function from test, differ from expected
        print ('finish reading')
        print ('Data Size:', len(self.BufData_Ch4))
        return self.BufData_Ch4_RealV

    def Read(self):
        '''
            scope.Read_Ch1, scope.Read_Ch2, scope.Read_Ch3, scope.Read_Ch4
        '''
        print(self.Read_ID())
        self.Write_WavFormat_Ascii()
        print('Start Reading')
        self.Read_Ch1 = np.array(self.Get_Buffer_Ch1())
        self.Read_Ch2 = np.array(self.Get_Buffer_Ch2())
#         self.Read_Ch3 = np.array(self.Get_Buffer_Ch3())
#         self.Read_Ch4 = np.array(self.Get_Buffer_Ch4())
        self.Memory_Depth = self.Read_Memory_Depth()
        self.Sample_Rate = self.Read_Sample_Rate()
        print('Finish Reading')
        return self.Read_Ch1, self.Read_Ch2, self.Memory_Depth, self.Sample_Rate
#         return self.Read_Ch1, self.Read_Ch2, self.Read_Ch3, self.Read_Ch4, self.Memory_Depth, self.Sample_Rate


# Copyright 2017-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

# Intended to compatible with both Python 2.7 and Python 3.x.
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from virtualcat import ImageFile, MemoryTechnologyDevice, VirtualCat
from partition import (
    DeviceTreePartition,
    EnvironmentPartition,
    ExternalChecksumPartition,
    LegacyUBootPartition,
    Partition
)

import argparse
import json
import logging
import logging.handlers
import os
import re
import socket
import subprocess
import sys

from glob import glob

if False:
    from typing import List, Optional, Tuple, Union
    from virtualcat import ImageSourcesType
    LogHandlerType = Union[
        logging.StreamHandler,
        logging.FileHandler,
        logging.handlers.SysLogHandler
    ]
    LogDetailsType = List[Tuple[LogHandlerType, logging.Formatter]]
    MTDListType = List[MemoryTechnologyDevice]

def is_openbmc():
    # type: () -> bool
    if os.path.exists('/etc/issue'):
        magics = [b'Open BMC', b'OpenBMC']
        with open('/etc/issue', 'rb') as etc_issue:
            first_line = etc_issue.readline()
        return any(first_line.startswith(magic) for magic in magics)
    return False

standalone = logging.Formatter(
    '%(levelname)s:%(asctime)-15s %(message)s'
)

def get_logger():
    # type: () -> logging.Logger
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    loggers = [(logging.StreamHandler(), standalone)]  # type: LogDetailsType
    if is_openbmc():
        try:
            loggers.append((logging.FileHandler('/mnt/data/pypartition.log'),
                            standalone))
        except IOError:
            print('Error initializing log file in /mnt/data; using /tmp.',
                  file=sys.stderr)
            loggers.append((logging.FileHandler('/tmp/pypartition.log'),
                            standalone))
        try:
            loggers.append((logging.handlers.SysLogHandler('/dev/log'),
                            logging.Formatter('pypartition: %(message)s')))
        except socket.error:
            print('Error initializing syslog; skipping.', file=sys.stderr)
    for (handler, formatter) in loggers:
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    return logger

def get_checksums_args(description):
    # type: (str) -> Tuple[List[str], argparse.Namespace]
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('image', nargs='?')
    checksum_help = 'currently required path to JSON file with dict mapping '
    checksum_help += 'md5sums to versions'
    parser.add_argument('--checksums', help=checksum_help, required=True,
                        type=argparse.FileType('r'))
    if is_openbmc():
        parser.add_argument('--dry-run', action='store_true')
    else:
        parser.add_argument('--serve', action='store_true')
        parser.add_argument('--port', type=int, default=2876)

    args = parser.parse_args()

    checksums = json.load(args.checksums).keys()

    return (checksums, args)

def free_kibibytes():
    # type: () -> int
    proc_meminfo_regex = re.compile(
        '^MemFree: +(?P<free_kb>[0-9]+) kB$', re.MULTILINE
    )
    with open('/proc/meminfo', 'r') as proc_meminfo:
        return int(proc_meminfo_regex.findall(proc_meminfo.read())[0])

def get_mtds():
    # type () -> Tuple[MTDListType, MTDListType]
    proc_mtd_regex = re.compile(
        '^(?P<dev>mtd[0-9]+): (?P<size>[0-9a-f]+) [0-9a-f]+ '
        '"(?P<name>[^"]+)"$', re.MULTILINE
    )
    mtd_info = []
    with open('/proc/mtd', 'r') as proc_mtd:
        mtd_info = proc_mtd_regex.findall(proc_mtd.read())
    vboot_support = 'none'
    if any(name == 'romx' for (_, _, name) in mtd_info):
        vboot_support = 'software-enforce'
        vboot_util_output = subprocess.check_output(
            ['/usr/local/bin/vboot-util']
        ).decode()
        if 'Flags hardware_enforce:  0x01' in vboot_util_output:
            vboot_support = 'hardware-enforce'
    all_mtds = []
    full_flash_mtds = []
    for (device, size_in_hex, name) in mtd_info:
        if name.startswith('flash') or name == 'Partition_000':
            if vboot_support == 'none' or \
               (vboot_support == 'software-enforce' and (name in ['flash0', 'flash1'])):
                    full_flash_mtds.append(MemoryTechnologyDevice(
                        device, int(size_in_hex, 16), name
                    ))
            elif vboot_support == 'hardware-enforce' and name in ['flash1']:
                    full_flash_mtds.append(MemoryTechnologyDevice(
                        device, int(size_in_hex, 16), name, 384
                    ))
        all_mtds.append(MemoryTechnologyDevice(
            device, int(size_in_hex, 16), name
        ))
    full_flash_mtds.sort(key=lambda mtd: mtd.device_name, reverse=True)
    return (full_flash_mtds, all_mtds)

def get_kernel_parameters():
    # type: () -> str
    # As far as cov knows, kernel parameters we use are backwards compatible,
    # so it's safe to use the following output even when there are CRC32
    # mismatch warnings.
    return subprocess.check_output(['fw_printenv', '-n', 'bootargs']).strip()

def append_to_kernel_parameters(dry_run, addition, logger):
    # type: (bool, str, logging.Logger) -> None
    before = get_kernel_parameters()
    logger.info('Kernel parameters before changes: {}'.format(before))
    if 'mtdparts' in before:
        logger.info('mtdparts already set in firmware environment.')
    if dry_run:
        logger.info('This is a dry run. Not changing kernel parameters.')
        return
    subprocess.call([
        'fw_setenv', 'bootargs', ' '.join([before, addition])
    ])
    logger.info('Kernel parameters after changes: {}'.format(
        get_kernel_parameters()
    ))

def get_partitions(images, checksums, logger):
    # type: (VirtualCat, List[str], logging.Logger) -> List[Partition]
    partitions = []  # type: List[Partition]
    next_magic = images.peek()
    # First 384K is u-boot for legacy or regular-fit images OR
    # the combination of SPL + recovery u-boot. Treat them as the same.
    if next_magic in ExternalChecksumPartition.UBootMagics:
        partitions.append(ExternalChecksumPartition(
            0x060000, 0x000000, 'u-boot', images, checksums, logger
        ))
    else:
        logger.error('Unrecognized magic 0x{:x} at offset 0x{:x}.'.format(
            next_magic, 0
        ))
        sys.exit(1)

    # Env is always in the same location for both legacy and FIT images.
    partitions.append(EnvironmentPartition(
        0x020000, 0x060000, 'env', images, logger
    ))

    # Either we are using the legacy image format or the FIT format.
    next_magic = images.peek()
    if next_magic == LegacyUBootPartition.magic:
        partitions.append(LegacyUBootPartition(
            [0x280000, 0x0400000], 0x080000, 'kernel', images, logger,
            LegacyUBootPartition.magic
        ))
        partitions.append(LegacyUBootPartition(
            [0xc00000, 0x1780000],
            partitions[-1].end(),
            'rootfs',
            images,
            logger,
        ))
    elif next_magic == DeviceTreePartition.magic:
        # The FIT image at 0x80000 could be a u-boot image (size 0x60000)
        # or the kernel+rootfs FIT which is much larger.
        # DeviceTreePartition() will pick the smallest which fits.
        part = DeviceTreePartition(
            [0x60000, 0x1B200000], 0x80000, "fit1", images, logger
        )
        partitions.append(part)

        # If the end of the above partition is 0xE0000 then we need to
        # check a second FIT image. This is definitely the larger one.
        if (part.end() == 0xE0000):
            partitions.append(DeviceTreePartition(
                [0x1B200000], 0xE0000, "fit2", images, logger
            ))
    else:
        logging.error('Unrecognized magic 0x{:x} at offset 0x{:x}.'.format(
            next_magic, 0x80000
        ))
        sys.exit(1)
    if images.images != []:
        # TODO data0 missing is only okay for ImageFiles, not
        # MemoryTechnologyDevices.  Also, this omits data0 from mtdparts=
        # message.
        partitions.append(Partition(
            0x2000000 - partitions[-1].end(),
            partitions[-1].end(),
            'data0',
            images,
            logger,
        ))
    return partitions

def get_valid_partitions(images_or_mtds, checksums, logger):
    # type: (ImageSourcesType, List[str], logging.Logger) -> List[Partition]
    if images_or_mtds == []:
        return []
    logger.info('Validating partitions in {}.'.format(
        ', '.join(map(str, images_or_mtds))
    ))
    with VirtualCat(images_or_mtds) as vc:
        partitions = get_partitions(vc, checksums, logger)
    last = None  # type: Optional[Partition]
    for current in partitions:
        # TODO populate valid env partition at build time
        # TODO learn to validate data0 partition
        if not current.valid and current.name not in ['env', 'data0']:
            message = 'Exiting due to invalid {} partition (details above).'
            logger.error(message.format(current))
            sys.exit(1)
        if last is not None and last.end() != current.partition_offset:
            logger.error('{} does not begin at 0x{:x}'.format(current,
                                                              last.end()))
            sys.exit(1)
        last = current
    return partitions

def run_verbosely(command, logger):
    # type: (List[str], logging.Logger) -> None
    command_string = ' '.join(command)
    logger.info('Starting to run `{}`.'.format(command_string))
    subprocess.check_call(command)
    logger.info('Finished running `{}`.'.format(command_string))

def other_flasher_running(logger):
    # type: (logging.Logger) -> bool
    basenames = [
        b'autodump.sh', b'cpld_upgrade.sh', b'dd', b'flash_eraseall', b'flashcp',
        b'flashrom', b'fw-util', b'fw_setenv', b'improve_system.py', b'jbi',
        b'psu-update-bel.py', b'psu-update-delta.py',
    ]
    running_flashers = {}

    our_cmdline = ['/proc/self/cmdline', '/proc/thread-self/cmdline',
        '/proc/{}/cmdline'.format(os.getpid())]

    for cmdline_file in glob('/proc/*/cmdline'):
        if cmdline_file in our_cmdline:
            continue
        try:
            with open(cmdline_file, 'rb') as cmdline:
                # Consider all command line parameters so `python foo.py` and
                # `foo.py` are both detected.
                for parameter in cmdline.read().split(b'\x00'):
                    basename = os.path.basename(parameter)
                    if basename in basenames:
                        if basename in running_flashers.keys():
                            running_flashers[basename] += 1
                        else:
                            running_flashers[basename] = 1
        # Processes and their respective files in procfs naturally come and go.
        except IOError:
            pass

    if running_flashers == {}:
        return False

    message = '{} running.'.format(b','.join(running_flashers.keys()).decode())
    logger.error(message)
    return True

def flash(attempts, image_file, mtd, logger):
    # type: (int, ImageFile, MemoryTechnologyDevice, logging.Logger) -> None
    if image_file.size > mtd.size:
        logger.error('{} is too big for {}.'.format(image_file, mtd))
        sys.exit(1)

    if other_flasher_running(logger):
        sys.exit(1)

    image_name = image_file.file_name
    # If MTD has a read-only offset, create a new image file with the
    # readonly offset from the device and the remaining from the image
    # file and use that for flashcp.
    if mtd.offset > 0:
        image_name = image_name + '.tmp'
        with open(image_name, 'wb') as out_f:
            with open(mtd.file_name, 'rb') as in_f:
                out_f.write(in_f.read(mtd.offset * 1024))
            with open(image_file.file_name, 'rb') as in_f:
                in_f.seek(mtd.offset * 1024)
                out_f.write(in_f.read())

    # TODO only write bytes that have changed
    flash_command = ['flashcp', image_name, mtd.file_name]
    if attempts < 1:
        flash_command = ['dd', 'if={}'.format(image_file.file_name),
                         'of=/dev/null']
        attempts = 1

    for attempt in range(attempts):
        try:
            run_verbosely(flash_command, logger)
            break
        except subprocess.CalledProcessError as result:
            # Retry the specified amount but even on consecutive failures don't
            # exit yet. Instead let the verification stage after this determine
            # the next steps.
            logger.warning('flashcp attempt {} returned {}.'.format(
                attempt, result.returncode
            ))
    # Remove temp file.
    if (image_file.file_name != image_name):
        os.remove(image_name)

# Unfortunately there is no mutual exclusion between flashing and rebooting.
# So for now minimize the window of opportunity for those operations to occur
# concurrently by rebooting as soon as possible after verification.
def reboot(dry_run, reason, logger):
    # type: (bool, str, logging.Logger) -> None

    if other_flasher_running(logger):
        sys.exit(1)

    reboot_command = ['shutdown', '-r', 'now',
                      'pypartition is {}.'.format(reason)]
    if dry_run:
        reboot_command = [
            'wall',
            'pypartition would be {} if this were not a dry run.'.format(
                reason
            )
        ]
        logger.info('This is a dry run. Not rebooting.')
    else:
        logger.info('Proceeding with reboot.')

    logging.shutdown()
    # Trying to run anything after the `shutdown -r` command is issued would be
    # racing against shutdown killing this process.
    if subprocess.call(reboot_command) == 0:
        sys.exit(0)
    else:
        logger.error('Unable to reboot')
        sys.exit(1)


# -*- coding: utf-8 -*-
'''
Getting hip and trying the Keras Functional API implementation of my Cifar10 code.
'''
from __future__ import print_function
from __future__ import absolute_import

import warnings
import keras
from keras.models import Model
from keras.layers import Input, Dense, Dropout, Activation, Flatten, Convolution2D, MaxPooling2D, SpatialDropout2D
from keras import backend as K
from keras.utils import np_utils

def cifar(): #maybe change border mode, idk; also maybe add BatchNormalization(axis=1)

    # Determine proper input shape
    K.set_image_dim_ordering('th')
    input_shape = (1, 256, 192)
    img_input = Input(shape=input_shape)

    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_1')(img_input)
    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_2')(x)
    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_3')(x)
    x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_4')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)
    x = Dropout(0.25)(x)

    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_1')(x)
    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_2')(x)
    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_3')(x)
    x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_4')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(x)
    x = Dropout(0.25)(x)

    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_1')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_2')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_3')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_4')(x)
    x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_5')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(x)
    x = Dropout(0.25)(x)

    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_1')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_2')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_3')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_4')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_5')(x)
    x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_6')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(x)
    x = Dropout(0.25)(x)

    x = Flatten(name='flatten')(x)
    x = Dense(1000, activation='relu', name='fc1')(x)
    x = Dropout(0.5)(x)
    x = Dense(1000, activation='relu', name='fc2')(x)
    x = Dropout(0.5)(x)
    x = Dense(2, activation='softmax', name='pred')(x)

    # Create model.
    model = Model(img_input, x)

    #weights=''
    #model.load_weights(weights)

    return model


#!/usr/bin/env python
# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np

# ### 图1：世界地图可视化
#
# 使用plotly 画出。
#
# reference:https://plotly.com/python/plotly-express/

# In[2]:

df1 = pd.read_csv('https://raw.githubusercontent.com/yitingpeng/datavisualization/master/data_plot1.csv')

# In[3]:

df1.columns

# In[4]:

df1.教育投入占GDP比例.describe()

# In[5]:

import plotly.express as px
fig1_1 = px.choropleth(df1, locations="Country Code",
                    color="教育投入占GDP比例",
                    hover_name="Country Name",
                    #title = '地图可视化: %s' %('教育投入占GDP比例'),
                    range_color=[0,max(df1.教育投入占GDP比例)])
fig1_1.show()

# In[6]:

import plotly.express as px
fig1_2 = px.choropleth(df1, locations="Country Code",
                    color="初等教育投入比例",
                    hover_name="Country Name",
                    # title = '地图可视化: %s' %("初等教育投入比例"),
                    range_color=[0,max(df1.初等教育投入比例)])
fig1_2.update_layout()
fig1_2.show()

# In[7]:

import plotly.express as px
fig1_3 = px.choropleth(df1, locations="Country Code",
                    color="中等教育投入比例",
                    hover_name="Country Name",
                    # title = '地图可视化: %s' %('中等教育投入比例'),
                    range_color=[0,max(df1.中等教育投入比例)])

fig1_3.show()

# In[8]:

fig1_4 = px.choropleth(df1, locations="Country Code",
                    color="高等教育投入比例",
                    hover_name="Country Name",
                    # title = '地图可视化: %s' %('高等教育投入比例'),
                    range_color=[0,max(df1.高等教育投入比例)])

fig1_4.show()

# ### 图2：气泡图
#
# 使用 plotly 套件画出。

# In[9]:

df2 = pd.read_csv("https://raw.githubusercontent.com/yitingpeng/datavisualization/master/data_plot2.csv")

# In[10]:

df2.columns

# In[11]:

# bubble chart
# https://plotly.com/python/bubble-charts/?utm_source=mailchimp-jan-2015&utm_medium=email&utm_campaign=generalemail-jan2015&utm_term=bubble-chart
## hover text
# https://plotly.com/r/hover-text-and-formatting/
# https://plotly.com/python/hover-text-and-formatting/
import plotly.express as px
fig2 = px.scatter(df2, x='人均GDP（美元)',y='高中及以上受教育人口比例',
                size='人口',color='Region',
                hover_name="Country Name",log_x =True,size_max=60,)
fig2.show()

# ### 图3：变化条形图
#
# 使用 matplotlib 画出。

# In[12]:

# 导入库文件
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.animation as animation
from IPython.display import HTML
import matplotlib
#防止动漫内存太大，报错
matplotlib.rcParams['animation.embed_limit'] = 2**128

#pandas读取数据，且去列名分别为name,group,year和value的值；,usecols=['countryname', 'group', 'year', 'value']
df = pd.read_csv('https://raw.githubusercontent.com/yitingpeng/datavisualization/master/data_plot3.csv')
df.head()

#导入random函数，randomcolor用于生成颜色代码
# randomcolor生成颜色代码原理，
# 【1-9/A-F】15个数字随机组合成6位字符串前面再加上一个“#”号键

'''import random
def randomcolor():
    colorlist = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
    color =''
    for i in range(6):
        color += random.choice(colorlist)
    return '#'+ color

#对地区列表进行去重，分类；
area_list1 = set(df['Country Name'])

# color_list用于存放随机生成颜色代码个数
# 因为后面区域个数 要与颜色个数保持一致，这里用了len函数；
color_list =[]
for i in range(len(area_list1)):
    str_1 = randomcolor()

    color_list.append(str_1)
    str_1 = randomcolor()

print(color_list)
#area_list转化为列表
area_list_1 = [i for i in area_list1]
print(area_list_1)

#colors表示 所在城市：颜色 一一对应字典形式；
colors =dict(zip(area_list_1,color_list))
print(colors)'''

#group_lk为 城市：所在区域 --对应字典形式；
group_lk = df.set_index('name')['group'].to_dict()
print(group_lk)

colors = dict(zip(
     ["East Asia & Pacific", "Europe & Central Asia", "Latin America & Caribbean", "Middle East & North Africa", "North America", "South Asia", "Sub-Saharan Africa"],
     ["#adb0ff", "#ffb3ff", "#90d595", "#e48381", "#aafbff", "#f7bb5f", "#eafb50","#edab10"]
 ))
# group_lk = df.set_index('name')['group'].to_dict()

# 用plt加理图表，figsize表示图标长宽，ax表示标签
fig, ax = plt.subplots(figsize=(15, 8))

#dras_barchart生成current_year这一年各城市人口基本情况；
def draw_barchart(current_year):

    #dff对year==current_year的行，以value从升序方式排序，取后十名也就是最大值；
    dff = df[df['year'].eq(current_year)].sort_values(by='value',ascending = True).tail(12)
    # 所有坐标、标签清除
    ax.clear()
    #显示颜色、城市名字
    ax.barh(dff['name'],dff['value'],color = [colors[x] for x in dff['group']])

    dx = dff['value'].max()/200

    #ax.text(x,y,name,font,va,ha)
    # x,y表示位置；
    # name表示显示文本；
    # va,ba分别表示水平位置，垂直放置位置；
    for i ,(value,name) in enumerate(zip(dff['value'], dff['name'])):
        ax.text(value-dx,i,name,size=14,weight=600,ha ='right',va = 'bottom')
        ax.text(value-dx,i-.25,group_lk[name],size = 10,color ='#444444',ha ='right',va = 'baseline')
        ax.text(value+dx,i ,f'{value:,.0f}',size = 14,ha = 'left',va ='center')

    #ax.transAxes表示轴坐标系，(1,0.4)表示放置位置
    ax.text(1,0.4,current_year,transform = ax.transAxes,color ='#777777',size = 46,ha ='right',weight=800)
    ax.text(0,1.06,'Per capita investment in education(dollars)',transform = ax.transAxes,size=12,color='#777777')

    #set_major_formatter表示刻度尺格式；
    ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
    ax.xaxis.set_ticks_position('top')
    ax.tick_params(axis='x',colors='#777777',labelsize=12)
    ax.set_yticks([])
    #margins表示自动缩放余额；
    ax.margins(0,0.01)
    # 设置后面的网格
    ax.grid(which='major',axis='x',linestyle='-')
    #刻度线和网格线是在图标上方还是下方，True为下方
    ax.set_axisbelow(True)
    ax.text(0,1.15,'Per capita investment in education from 1970 to 2018',
           transform=ax.transAxes,size=24,weight=600,ha='left',va='top')
    #取消图表周围的方框显示
    plt.box(False)

#绘制2018年各城市人口情况
draw_barchart(2018)

#将原来的静态图拼接成动画
fig, ax = plt.subplots(figsize=(15, 8))
animator = animation.FuncAnimation(fig, draw_barchart, frames=range(1970, 2018))

# In[13]:

df4 = pd.read_csv('https://raw.githubusercontent.com/yitingpeng/datavisualization/master/data_plot3.csv')
fig4 = px.bar(df4,x="name",y="value",color='group',
             animation_frame='year',range_y=[0,350000])
fig4.show()

# In[14]:

df4.columns

# ### 利用Dash 形成网页

# ### Reference
#
# Dash App
# - https://github.com/plotly/dash-sample-apps/blob/master/apps/dash-opioid-epidemic/app.py#L107
#
# Dash Core Components
# - https://dash.plotly.com/dash-core-components
#
# Video Addition
# - https://community.plotly.com/t/adding-video-player/5303
# - https://dash.plotly.com/dash-html-components/video

# In[16]:

import os
import pathlib
import re
import flask

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output, State

app = dash.Dash(
    __name__,
    meta_tags=[
        {"name": "viewport", "content": "width=device-width, initial-scale=1.0"}
    ],)

colors = {
    'background':'#3d5a80',
    'text':'#355070'
}

app.layout = html.Div(
    id = "root",
    style = {},
    children = [
        html.Div(
            id="header",
            children = [
                html.H1(
                    children="各国教育可视化",
                    style = {'color':colors["text"],
                             'textAlign':'center',
                             'font-family':'Microsoft JhengHei',
                            },
                       ),
                html.P(
                    id="description",
                    children = "|| 利用世界银行的世界发展指标，来观察各国教育发展情形。 ||",
                    style = {'color':'#495057',
                             'font-family':'Microsoft JhengHei',
                             'font-size': "2",
                             'textAlign':'center',
                            },
                       ),
                    ],
                ),
        html.Div(
            id = "app-container",
            children = [
                html.Div(
                    id = "left-column",
                    style={'width': '49%','display': 'inline-block'},
                    children = [
                        html.P(
                          id = "plot-selector",
                          children = "Select:"),
                        dcc.Dropdown(
                          options = [
                              {"label":"教育投入占GDP比例",
                               "value": "教育投入占GDP比例",},
                              {"label":"初等教育投入比例",
                               "value": "初等教育投入比例",},
                              {"label":"中等教育投入比例",
                               "value": "中等教育投入比例",},
                              {"label":"高等教育投入比例",
                               "value": "高等教育投入比例",},
                          ],
                          value = "教育投入占GDP比例",
                          id="chart-dropdown",
                        ),
                        dcc.Graph(
                            id = "selected-data",
                            figure = fig1_1,

                        ),
                    ],
                ),
                html.Div(
                    id = "right-column",
                    style={'width': '49%',
                           'float':'right',
                           'display': 'inline-block'},
                    children=[
                        html.H4(
                            children = "气泡图",
                            style = {
                            'font-family':'Microsoft JhengHei'
                                    },
                        ),
                        dcc.Graph(
                            id = "bubble-plot",
                            figure = fig2,
                        ),
                    ],
                ),
                html.Div(
                    id = "bottom",
                     style={'width': '49%',
                           'float':'center',
                           'display': 'inline-block'},
                    children=[
                        html.H4(
                            children = "条形变化图",
                            style = {
                            'font-family':'Microsoft JhengHei'
                                    },
                                ),
                        html.Iframe(
                            style = {
                              'Align':'center',
                              "margin-left": "100px",
                            },
                            height = "400",
                            width = "600",
                            #src = "https://www.youtube.com/embed/lHNApiji9-M",
                            src = "//player.bilibili.com/player.html?aid=841044569&bvid=BV1a54y1B7pv&cid=202580784&page=1",
                        ),
                    ],
                ),
                html.Div(
                    id = "right-bottom",
                     style={'width': '49%',
                           'float':'right',
                           'display': 'inline-block'},
                    children=[
                        dcc.Graph(
                            id = "bar-plot",
                            figure = fig4,
                        ),
                    ],
                ),
            ],
        ),
    ],
)

@app.callback(
    Output("selected-data","figure"),
    [
      Input("chart-dropdown","value")
    ],
)
def display_selected_data(chart_dropdown):
    if chart_dropdown == "教育投入占GDP比例":
        return fig1_1
    if chart_dropdown == "初等教育投入比例":
        return fig1_2
    if chart_dropdown == "中等教育投入比例":
        return fig1_3
    if chart_dropdown == "高等教育投入比例":
        return fig1_4

if __name__ == "__main__":
    app.run_server(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter

# In[19]:

np.random.randint(0, 1000000)


# -*- coding: utf-8 -*-
from itertools import product

import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_equal

from bounded_rand_walkers.cpp import bound_map
from bounded_rand_walkers.utils import (
    DelaunayArray,
    approx_edges,
    cluster_indices,
    get_centres,
    in_bounds,
    label,
    match_ref,
    normalise,
)

def test_get_centres():
    assert_allclose(get_centres([1, 2, 3, 4]), [1.5, 2.5, 3.5])
    assert_allclose(get_centres([-1, 1, 3, 4]), [0, 2, 3.5])

@pytest.mark.parametrize(
    "x,exp_indices,exp_labelled",
    [
        (
            np.array([1, 1, 0, 0, 1, 0]),
            np.array([[0, 2], [4, 5]]),
            np.array([1, 1, 0, 0, 2, 0]),
        ),
        (
            np.array([0, 1, 0, 0, 1, 1]),
            np.array([[1, 2], [4, 6]]),
            np.array([0, 1, 0, 0, 2, 2]),
        ),
        (
            np.array([0, 1, 0, 0, 1, 0]),
            np.array([[1, 2], [4, 5]]),
            np.array([0, 1, 0, 0, 2, 0]),
        ),
        (
            np.array([0, 1, 0, 0, 0, 0]),
            np.array([[1, 2]]),
            np.array([0, 1, 0, 0, 0, 0]),
        ),
    ],
)
def test_labelling(x, exp_indices, exp_labelled):
    assert_equal(cluster_indices(x), exp_indices)
    labelled, n_cluster = label(x)
    assert_equal(labelled, exp_labelled)
    assert n_cluster == np.max(exp_labelled)

def test_approx_edges():
    assert_allclose(approx_edges([1, 2, 3]), [0.5, 1.5, 2.5, 3.5])
    assert_allclose(approx_edges([1, 2, 4]), [0.5, 1.5, 3, 5])
    assert_allclose(approx_edges([-1, 1, 2, 3]), [-2, 0, 1.5, 2.5, 3.5])

def test_normalise():
    centres = [0.5, 1.5]
    y = [1, 1]
    assert_allclose(normalise(centres, y, return_factor=True), 0.5)
    assert_allclose(normalise(centres, y), [0.5, 0.5])

    edges = [0, 1, 2]
    y = [1, 1]
    assert_allclose(normalise(edges, y, return_factor=True), 0.5)
    assert_allclose(normalise(edges, y), [0.5, 0.5])

    edges = [0, 1 / 3, 1]
    y = [1, 1]
    assert_allclose(normalise(edges, y, return_factor=True), 1)
    assert_allclose(normalise(edges, y), y)

    edges = [0, 1, 5]
    y = [1, 1 / 2]
    assert_allclose(normalise(edges, y, return_factor=True), 1 / 3)
    assert_allclose(normalise(edges, y), [1 / 3, 1 / 6])

def test_match_ref():
    x = np.linspace(0, 1, 10)
    y = np.random.RandomState(0).random(10)

    ref_x = x[:8]
    # Scale the original data and add random noise.
    ref_y = 2 * y[:8] + np.random.RandomState(1).random(8) * 0.1

    diff_before = np.mean(np.abs(y[:8] - ref_y))

    scaled = match_ref(x, y, ref_x, ref_y, interpolate=False)
    assert scaled.shape == y.shape

    diff_after_match = np.mean(np.abs(scaled[:8] - ref_y))

    assert diff_after_match / diff_before < 0.05

    scaled_interp = match_ref(x + 0.01, y, ref_x, ref_y, interpolate=True)
    assert scaled_interp.shape == y.shape

    diff_after_interp_match = np.mean(np.abs(scaled_interp[:8] - ref_y))

    assert diff_after_interp_match / diff_before < 0.05

    scaled_factor = match_ref(x, y, ref_x, ref_y, interpolate=False, return_factor=True)
    scaled_interp_factor = match_ref(
        x + 0.01, y, ref_x, ref_y, interpolate=True, return_factor=True
    )

    for factor in (scaled_factor, scaled_interp_factor):
        assert_allclose(factor, 2, rtol=0, atol=0.05)

def test_delaunay_bounds():
    for bound_name, bound_func in bound_map.items():
        bounds = DelaunayArray(bound_func())
        if bound_name == "irregular":
            pos = np.array([0.5, 0.5])
        else:
            pos = np.array([0, 0])
        assert in_bounds(pos, bounds)

        for index, coord in product(range(2), [-3, 3]):
            pos = np.array([0, 0])
            pos[index] = coord
            assert not in_bounds(pos, bounds)


import logging

import chromalog
import wuphf
from test_config import MAIL_01

chromalog.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)

log.debug('start create client')
w = wuphf.Wuphf()
log.debug('end create client')

log.debug('start create message')
msg = wuphf.Message('Test Title', 'Test Body')
log.debug('end create message')

log.debug('start send')
w.smtp(msg, (MAIL_01))
log.debug('end send')


import scrapy
from scrapy.loader import ItemLoader
from criminals.items import CriminalsItem

class Site15Spider(scrapy.Spider):
    name = 'site15'
    allowed_domains = ['treasury.gov']
    start_urls = ['https://www.treasury.gov/ofac/downloads/consolidated/consolidated.xml']

    def parse(self, response):
        response.selector.register_namespace('data','http://tempuri.org/sdnList.xsd')
        items = response.xpath('//data:sdnEntry')
        for item in items :
            loader = ItemLoader(CriminalsItem(),item)
            loader.add_xpath('first_name','.//data:aka[1]//data:firstName[1]/text()')
            loader.add_xpath('last_name','.//data:aka[1]//data:lastName[1]/text()')
            loader.add_xpath('date_of_birth','.//data:dateOfBirth[1]/text()')
            loader.add_xpath('country','.//data:country//text()')
            yield loader.load_item()


# -*- coding: utf8 -*-
import pytest

from raiden.tests.utils.transfer import direct_transfer, mediated_transfer

@pytest.mark.parametrize('privatekey_seed', ['fullnetwork:{}'])
@pytest.mark.parametrize('number_of_nodes', [3])
@pytest.mark.parametrize('channels_per_node', [2])
@pytest.mark.parametrize('deposit', [2 ** 20])
def test_fullnetwork(raiden_chain):
    app0, app1, app2 = raiden_chain  # pylint: disable=unbalanced-tuple-unpacking

    asset_address = app0.raiden.chain.default_registry.asset_addresses()[0]

    amount = 80
    direct_transfer(app0, app1, asset_address, amount)

    amount = 50
    direct_transfer(app1, app2, asset_address, amount)

    amount = 30
    mediated_transfer(app1, app2, asset_address, amount)


import gym
import numpy as np
import torch
from policy_backward import PolicyBackward
from vcritic import VCritic
from config import Config
import random
import time

# Logging stuff
from tensorboardX import SummaryWriter

env = gym.make('InvertedPendulum-v1')
seed = random.randint(1,10000)
env.seed(seed); torch.manual_seed(seed);

config = Config()

writer = SummaryWriter(f'score/backward-{config.critic_lr}-{config.policy_lr}-{config.normalize_advantages}-BS{config.batch_size}-learnStd={config.learn_std}-{seed}-{int(time.time())}')

vcritic = VCritic(env, config)
policy = PolicyBackward(env, config, writer)

policy.train()

for batch in range(4000):

    states = []
    actions = []
    rewards = []

    total_steps = 0

    # while total_steps < config.batch_size:

    observation = env.reset()
    done = False
    ep_length = 0

    ep_states = [observation]
    ep_actions = []
    ep_rewards = []

    while not done:
        # env.render()
        action = policy.get_action(observation)
        observation, reward, done, info = env.step(action)
        ep_states.append(observation)
        ep_actions.append(action)
        ep_rewards.append(reward)
        ep_length += 1
        if(len(ep_actions) >= 2):
            vcritic.apply_gradient(ep_states[-3], ep_actions[-2], ep_rewards[-2], ep_states[-2])

    states.append(ep_states)
    actions.append(ep_actions)
    rewards.append(ep_rewards)
    total_steps += ep_length

    policy.apply_gradient_batch(states, actions, rewards, batch, vcritic)

    policy_std = np.exp(policy.log_std.detach().numpy())
    print(f"Policy std: {policy_std}")

    # Compute evaluation reward (last episode of batch).
    # total_reward = np.sum(rewards[-1])
    total_reward = np.mean([np.sum(ep) for ep in rewards])
    print(f"Average score in batch: {total_reward}")

    writer.add_scalar("total_reward", total_reward, batch)
    writer.add_scalar("policy_std", policy_std, batch)
    writer.add_scalar("ep_length", ep_length, batch)


from styx_msgs.msg import TrafficLight
import cv2
import numpy as np

class TLCVClassifier(object):

    def __init__(self):

        self.debug = False

        # define color ranges
        self.hsv_red_low_low = (0, 50, 0)
        self.hsv_red_low_high = (10, 255, 255)
        self.hsv_red_high_low = (160, 50, 80)
        self.hsv_red_high_high = (179, 255, 255)
        self.hsv_yellow_low = (28, 100, 100)
        self.hsv_yellow_high = (32, 255, 255)
        self.hsv_green_low = (50, 90, 50)
        self.hsv_green_high = (70, 255, 255)
        self.distance_min = 15
        self.red_canny_low = 10
        self.red_canny_high = 100
        self.yellow_canny_low = 7
        self.yellow_canny_high = 100
        self.green_canny_low = 7
        self.green_canny_high = 50
        self.radious_min = 1
        self.radious_max = 20

    def get_classification(self, image):
        """Determines the color of the traffic light in the image

        Args:
            image (cv::Mat): image containing the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """

        # convert bgr to hsv
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

        # detect circles of a color
        red_circles = self.detect_traffic_light_circles(hsv, self.hsv_red_low_low, self.hsv_red_low_high, TrafficLight.RED,
            color_range_2_low=self.hsv_red_high_low, color_range_2_high=self.hsv_red_high_high)
        yellow_circles = self.detect_traffic_light_circles(hsv, self.hsv_yellow_low, self.hsv_yellow_high, TrafficLight.YELLOW)
        green_circles = self.detect_traffic_light_circles(hsv, self.hsv_green_low, self.hsv_green_high, TrafficLight.GREEN)

        print('red: {}, yellow: {}, green: {}'.format(
            0 if red_circles is None else len(red_circles),
            0 if yellow_circles is None else len(yellow_circles),
            0 if green_circles is None else len(green_circles)))

        if self.debug == True:
            self.save_debug_images(red_circles, yellow_circles, green_circles)

        if red_circles is not None:
            return TrafficLight.RED
        elif green_circles is not None:
            return TrafficLight.GREEN
        elif yellow_circles is not None:
            return TrafficLight.YELLOW
        else:
            return TrafficLight.UNKNOWN

    def detect_traffic_light_circles(self, hsv_image, color_range_1_low, color_range_1_high, color,
        color_range_2_low=None, color_range_2_high=None):

        if color_range_2_low is not None and color_range_2_high is not None:
            mask_1 = cv2.inRange(hsv_image,
                np.array(color_range_1_low, dtype = "uint8"),
                np.array(color_range_1_high, dtype = "uint8"))
            mask_2 = cv2.inRange(hsv_image,
                np.array(color_range_2_low, dtype = "uint8"),
                np.array(color_range_2_high, dtype = "uint8"))
            mask = cv2.addWeighted(mask_1, 1.2, mask_2, 1.0, 0.0)
            mask = cv2.GaussianBlur(mask,(1,1),0)
        else:
            mask = cv2.inRange(hsv_image, color_range_1_low, color_range_1_high)

        if color == TrafficLight.RED:
            canny_low = self.red_canny_low
            canny_high = self.red_canny_high
        elif color == TrafficLight.YELLOW:
            canny_low = self.yellow_canny_low
            canny_high = self.yellow_canny_high
        elif color == TrafficLight.GREEN:
            canny_low = self.green_canny_low
            canny_high = self.green_canny_high

        circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, self.distance_min,
                     param1=canny_high, param2=canny_low, minRadius=self.radious_min, maxRadius=self.radious_max)

        return circles

    def save_debug_images(self, image, red_circles, yellow_circles, green_circles):
        debug_image = image.copy()

        if red_circles is not None:
            for circ in range(0,len(red_circles[0,:])):
                cv2.circle(debug_image,
                    (red_circles[0,:][circ][0], red_circles[0,:][circ][1]),
                    int(red_circles[0,:][circ][2]+5), (0,0,155), 2)
        if yellow_circles is not None:
            for circ in range(0,len(yellow_circles[0,:])):
                cv2.circle(debug_image,
                    (yellow_circles[0,:][circ][0], yellow_circles[0,:][circ][1]),
                    int(yellow_circles[0,:][circ][2]+5), (0,0,155), 2)
        if green_circles is not None:
            for circ in range(0,len(green_circles[0,:])):
                cv2.circle(debug_image,
                    (green_circles[0,:][circ][0], green_circles[0,:][circ][1]),
                    int(green_circles[0,:][circ][2]+5), (0,0,155), 2)

        cv2.imwrite("/home/student/output/tl_cv_classified_" + str(rospy.Time.now()) + ".png", debug_image)


from termcolor import colored, cprint
import os
from shell import sh
import json

sh('yes | pacman -Sy --needed python-pip')
sh('pip install termcolor')

def input_opt():
    return input(colored('>>> ', 'blue'))

def print_no_such_opt():
    print(colored('\\\\\ There is no such option!', 'red'))

def p_green(string: str):
    cprint(string, 'green')

def p_blue(string: str):
    cprint(string, 'blue')

def p_red(string: str):
    cprint(string, 'red')

class Locale:
    def __init__(self, j: str):
        data = json.load(j)
        self.text_choose_language = data['text_choose_language']
        self.text_select_system_language = data['text_select_system_language']

def load_locale(filename_json: str):
    with open(filename_json, "r") as read_file:
        return Locale(read_file)

lc = load_locale('en_US.json')
# =====================================================================

def set_ru_lan():
    print(colored('Installing russian language...', 'green'))
    lc_gen = open('/etc/locale.gen', 'a')
    lc_gen.write('\nru_RU.UTF-8 UTF-8')
    lc_gen.close()

    lc_conf = open('/etc/locale.conf', 'w')
    lc_conf.write('LANG=ru_RU.UTF-8')
    lc_conf.close()

    vconsole = open('/etc/vconsole.conf', 'w')
    vconsole.write(
        '\nLOCALE=ru_RU.UTF-8' +
        '\nKEYMAP=ru' +
        '\nFONT=cyr-sun16')
    vconsole.close()

    sh("locale-gen")
    sh("loadkeys ru")
    sh("setfont cyr-sun16")
    print(colored('Русский язык установлен!', 'green'))

def check_ping():
    print(colored('Проверка соединения ...', 'green'))
    response = sh('ping -c 2 archlinux.org').code
    if response == 0:
        print(colored('Соединенение установлено!', 'green'))
    else:
        print(colored('Соединение не установлено!' +
                      '\nДля работы установщика нужно интернет соединение.', 'red'))
    press_enter()

def press_enter():
    input("Press Enter to continue...")

def change_language():
    global lc
    languages = {
        '1': 'English:en_US.UTF-8 UTF-8:en_US.json',
        '2': 'Russian:ru_RU.UTF-8 UTF-8:ru_RU.json'
    }

    print(lc.text_choose_language)
    for lang in languages.items():
        value = lang[1].split(':')
        print(f'[{lang[0]}] {value[0]}')

    while True:
        choice = input(lc.text_select_system_language)
        selected_lang = languages.get(choice, 'invalid_choice')
        if not selected_lang == "invalid_choice":
            value = selected_lang.split(':')
            lc = load_locale(value[2])
            set_language(value[1])
            break

def set_language(lang):
    f = open('/etc/locale.gen', 'rt')
    data = f.readlines()
    for line in data:
        index = data.index(line)
        if lang in line and not line == '##  en_US.UTF-8 UTF-8\n':
            data.remove(line)
            new_line = line.replace('#', '', 1)
            data.insert(index, new_line)
        if not line.startswith('#') and not 'en_US.UTF-8 UTF-8' in line:
            data.remove(line)
            new_line = line.replace(line, '#' + line)
            data.insert(index, new_line)
    f.close()

    f = open('/etc/locale.gen', 'wt')
    f.writelines(data)
    f.close()
    sh("locale-gen", True)

# print('Arch Linux Installer (01.12.2021)')
# print()
# print('Link: https://github.com/dot-pulsar/archi')
# change_language()

# =======================================================================

class Device:
    def __init__(self, path: str, is_efi: bool):
        self.path = path
        self.is_efi = is_efi
        if '/dev/nvm' in path:
            self.boot = path + 'p1'
            self.swap = path + 'p2'
            self.home = path + 'p3'
        else:
            self.boot = path + '1'
            self.swap = path + '2'
            self.home = path + '3'

def select_device():
    devices = {}
    i = 0
    sh.clear()
    p_green('===================================')
    #print(colored('NAME             TYPE FSTYPE SYZE', 'green'))
    sh('lsblk -n -p -o NAME,TYPE,FSTYPE,SIZE -e 7,11', True)
    p_green('===================================')
    temp_devices = sh('lsblk -d -p -n -l -o NAME -e 7,11').value
    p_green('\n(Installation devices)')
    for temp_device in temp_devices.split('\n'):
        if temp_device == '':
            break
        i += 1
        devices.update({str(i): temp_device})

    for device in devices.items():
        print(f'{colored(f"[{device[0]}]","yellow")} {device[1]}')

    while True:
        selected_devices = devices.get(input_opt(), 'invalid_choice')
        if not selected_devices == 'invalid_choice':
            return str(selected_devices)
        else:
            print_no_such_opt()

def swap_size():
    mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
    return str(int(mem_bytes/(1024.**2))) + 'M'

def auto_partition_device_gpt():
    device = select_device()
    sh(f'parted -s {device} mklabel gpt', True)
    sh(f'sgdisk {device} -n=4:0:+31M -t=1:ef02', True)
    sh(f'sgdisk {device} -n=1:0:+512M', True)
    sh(f'sgdisk {device} -n=2:0:+{swap_size()} -t=2:8200', True)
    sh(f'sgdisk {device} -n=3:0:0', True)
    return Device(device, False)

def auto_partition_device_gpt_efi():
    device = select_device()
    sh(f'parted -s {device} mklabel gpt', True)
    sh(f'sgdisk {device} -n=1:0:+1024M -t=1:ef00', True)
    sh(f'sgdisk {device} -n=2:0:+{swap_size()} -t=2:8200', True)
    sh(f'sgdisk {device} -n=3:0:0', True)
    return Device(device, True)

def auto_partition_device_dos():
    device = select_device()
    sh(f'parted -s {device} mklabel msdos')
    sh(f'echo -e "n\np\n1\n\n+512M\na\nw" | fdisk {device}')
    sh(f'echo -e "n\np\n2\n\n+{swap_size()}\nt\n\n82\nw" | fdisk {device}')
    sh(f'echo -e "n\np\n3\n\n\nw" | fdisk {device}')
    return Device(device, False)

def format_boot_partition(device: Device):
    print(f'Formatting {colored(device.boot,"red")} partition')
    if device.is_efi:
        sh(f'yes | mkfs.fat {device.boot}')
    else:
        sh(f'yes | mkfs.ext4 {device.boot}')

def format_swap_partition(device: Device):
    print(f'Formatting {colored(device.swap,"red")} partition')
    sh(f'yes | mkswap {device.swap}')

def format_home_partition(device: Device):
    print(f'Formatting {colored(device.home,"red")} partition')
    sh(f'yes | mkfs.ext4 {device.home}')

def mount_partitions(device: Device):
    print(
        f'Mounting {colored(device.home,"red")} {colored("<home>","blue")} partition in {colored("/mnt","red")}')
    sh(f'mount {device.home} /mnt', True)
    sh('mkdir /mnt/{boot,home}')
    if device.is_efi:
        sh('mkdir /mnt/boot/efi')
        print(
            f'Mounting {colored(device.boot,"red")} {colored("<boot>","blue")} partition in {colored("/mnt/boot/efi","red")}')
        sh(f'mount {device.boot} /mnt/boot/efi', True)
    else:
        print(
            f'Mounting {colored(device.boot,"red")} {colored("<boot>","blue")} partition in {colored("/mnt/boot","red")}')
        sh(f'mount {device.boot} /mnt/boot', True)
    print(
        f'Enabling {colored(device.swap,"red")} {colored("<swap>","blue")} partition')
    sh(f'swapon {device.swap}', True)

def umount_partitions(device: Device):
    p_green('Unmounting all partitions')
    sh('umount -R /mnt', True)
    sh(f'swapoff {device.swap}', True)

def install_opts(index: int):
    sh.clear()
    p_green('(Installation steps)')
    opts = {
        1: 'pacstrap',
        2: 'genfstab',
        3: 'mkinitcpio',
        4: 'grub-install',
        5: 'grub-mkconfig'
    }
    for opt in opts.items():
        if opt[0] >= index:
            print(f'{colored(f"[{opt[0]}]","yellow")} {opt[1]}')
        else:
            print(
                f'{colored(f"[{opt[0]}]","yellow")} {colored(opt[1],"blue")}')

def install():
    p_green('(Device partitioning modes)')
    print(
        f'{colored("[1]","yellow")} Automatic partitioning DOS\n' +
        f'{colored("[2]","yellow")} Automatic partitioning GPT (EFI)')
    while True:
        opt = input_opt()
        if opt == '1':
            device = auto_partition_device_dos()
            break
        elif opt == '2':
            device = auto_partition_device_gpt_efi()
            break
        else:
            print_no_such_opt()

    sh.clear()

    format_boot_partition(device)
    format_swap_partition(device)
    format_home_partition(device)
    umount_partitions(device)
    mount_partitions(device)
    chroot = 'arch-chroot /mnt'

    install_opts(1)
    sh('pacstrap /mnt base base-devel linux linux-firmware grub dhcpcd')
    if device.is_efi:
        sh('pacstrap /mnt efibootmgr')

    install_opts(2)
    sh('genfstab -p /mnt >> /mnt/etc/fstab')

    install_opts(3)
    sh(f'{chroot} mkinitcpio -p linux')

    install_opts(4)
    if device.is_efi:
        sh(f'{chroot} grub-install')
    else:
        sh(f'{chroot} grub-install {device.path}')

    install_opts(5)
    sh(f'{chroot} grub-mkconfig -o /boot/grub/grub.cfg')

    install_opts(6)
    p_green('Set a password for ROOT')
    sh(f'{chroot} passwd root', True)
    umount_partitions(device)

#install()


import pyodbc
import pandas as pd
# Windows Authentication
cnxn = pyodbc.connect(
    'Driver={SQL Server};'
    'Server=DESKTOP-8RMHNPU\MSSQLSERVER03;'
    'Database=Proyecto;'
    'Trusted_Connection=yes;')

cursor = cnxn.cursor()

sql_query = pd.read_sql_query('SELECT * FROM Proyecto.oficina.temperatura',cnxn)
print(sql_query)
print(type(sql_query))
cursor.close()