Пример #1
0
def grid_search_run(train_dfs, label_dfs, test_dfs, sax_words_as_svec, window_list, nbin_list, alphabet_list, verbose):
    dask_output = []
    i = 1
    for train_df, label_df, test_df in zip(train_dfs, label_dfs, test_dfs):
        for window in window_list:
            for nbins in nbin_list:
                for alphabet_size in alphabet_list:
                    s_time = gt.time()
                    if verbose:
                        print('dset: {}\tstime: {}\tw: {}\tnb: {}\ta: {}'.
                              format(i, gt.time(), window, nbins, alphabet_size))
                    alphabet = string.ascii_uppercase[:alphabet_size]
                    sax = ToSAX(window, nbins=nbins, alphabet=alphabet)
                    train_sax_df = run_sax_along_df(train_df, sax, sax_words_as_svec, verbose)
                    test_sax_df = run_sax_along_df(test_df, sax, sax_words_as_svec, verbose)
                    print_toSAX_csv(train_sax_df, i, window, nbins, alphabet_size, 'train')
                    print_toSAX_csv(test_sax_df, i, window, nbins, alphabet_size, 'test')
                    # train_sax_df = delayed(run_sax_along_df)(train_df, sax, sax_words_as_svec, verbose)
                    # test_sax_df = delayed(run_sax_along_df)(test_df, sax, sax_words_as_svec, verbose)
                    # dask_output.append(delayed(print_SAX_to_csv)(train_sax_df, window, nbins, alphabet_size, 'train'))
                    # dask_output.append(delayed(print_SAX_to_csv)(test_sax_df, window, nbins, alphabet_size, 'test'))

                    if verbose:
                        print('SAXed dataset: {}\tstart time: {}\tend time: {}'.format(i, s_time, gt.time()))
        i += 1
Пример #2
0
def main():
    print(os.listdir())
    time = gt.GetTime()
    verbose = True
    full_run = True
    grid_search = True
    sax_words_as_string_vec = True  # scikit learn requires string vectors, does not accept 2d list
    #----------SAX--------------#
    window = 4
    stride = window
    nbins = 5
    alphabet_size = 5
    alphabet = string.ascii_uppercase[:alphabet_size]
    sax = ToSAX(window, stride, nbins, alphabet)
    #-----------GRID------------#
    window_list = range(5, 30, 5)
    nbin_list = range(5, 12, 2)
    alphabet_list = range(4, 9, 2)

    if verbose:
        print(time.initial_time)

    if full_run:
        test_fns, train_fns, labels_fns = get_fns(verbose)
        # dfs below are generators
        train_dfs = get_dataframes(verbose, train_fns)
        label_dfs = get_dataframes(verbose, labels_fns)
        test_dfs = get_dataframes(verbose, test_fns)

        if grid_search:
            if verbose: print('doing grid search')
            grid_search_run(train_dfs, label_dfs, test_dfs, sax_words_as_string_vec, window_list, nbin_list, alphabet_list, verbose)
        else:
            regular_run(train_dfs, label_dfs, test_dfs, sax, sax_words_as_string_vec, window, nbins, alphabet_size, verbose)
    else:
        if verbose: print('in else')
        i = 1
        dataset1_test = 'hw1_datasets/dataset4/test_normalized.csv'
        test1_labels = 'hw1_datasets/dataset4/test_labels.csv'
        dataset1_train = 'hw1_datasets/dataset4/train_normalized.csv'
        dataset1_train_labels = 'hw1_datasets/dataset4/train_labels.csv'
        train1 = pd.read_csv(dataset1_train, index_col=0)
        test1 = pd.read_csv(dataset1_test, index_col=0)
        train1_labels = pd.read_csv(dataset1_train_labels, index_col=0)

        # ts_sax_df = run_sax_along_df(train1.head(n=10), sax, sax_words_as_string_vec, verbose)
        train_sax_df = run_sax_along_df(train1, sax, sax_words_as_string_vec, verbose)
        test_sax_df = run_sax_along_df(test1, sax, sax_words_as_string_vec, verbose)
        if verbose:
            print('Results Found')
            print('sax df shape: {}'.format(train_sax_df.shape))
            # print(train_sax_df)
        print_toSAX_csv(train_sax_df, i, window, nbins, alphabet_size, 'train', True)
        print_toSAX_csv(test_sax_df, i, window, nbins, alphabet_size, 'test', True)

    if verbose:
        print('-------Completed!-------')
        print('Started at: {}\tFinished at: {}'.format(time.initial_time, gt.time()))
Пример #3
0
 def search_time(self):
     print(u"请确保将查询的人员名单写入name_total.txt,并保证其在运行目录下")
     print(u"若人员较多则所需时间较长,请耐心等待...")
     GetID.get_id()
     GetTime.get_time()
     print(u"详细时长已保存在time_total.txt")
     print(u"按任意键返回主菜单..."),
     raw_input()
     self.__init__()
def print_results_to_csv(predictions, dataset_num, sax, s_time, clf='kNN'):
    print('Printing Results')
    test_output_fn = 'test_output/results_dataset{}_{}_w{}_nb{}_a{}_s{}_e{}.csv'.format(
        dataset_num, clf, sax.window, sax.nbins, sax.alphabet_cardinality, s_time, gt.time())

    with open(test_output_fn, 'w') as results:
        for y in predictions:
            results.write('{0}\n'.format(y))
Пример #5
0
def main(fname):
    '''主程序'''
    print('提示:如果直接回车,要转换的文字将为“要转换的文字.txt”中的文字')
    ms = input('输入要转换成图片的文字: \n ')
    print('开始时间:', GetTime.getTime())
    if ms == '':
        try:
            f = open('要转换的文字.txt', 'r', encoding='utf-8')
            ms = str(f.readlines())
            f.close()
        except:
            print('缺少默认文件')
    print('len(ms)=', len(ms))
    print('start', GetTime.getTime())
    saveimg(fname, str_to_arr(ms))
    print('结束时间:', GetTime.getTime())
    input('按任意键继续')
Пример #6
0
def update_results():
    full_filename = os.path.join(app.config['UPLOAD_FOLDER'],
                                 'WashWatch_blau.png')
    print(full_filename)
    #The next line needs to be uncommented, when a camera is adapted!
    #pic.takePicture()

    time_left = time.getTime()

    return render_template('index.html',
                           user_image=full_filename,
                           time=time_left)
Пример #7
0
def regular_run(train_dfs, label_dfs, test_dfs, sax, sax_words_as_string_vec, window, nbins, alphabet_size, verbose):
    i=1
    for train_df, label_df, test_df in zip(train_dfs, label_dfs, test_dfs):
        s_time = gt.time()
        print(label_df.shape)
        # print(label_df)
        train_sax_df = run_sax_along_df(train_df, sax, sax_words_as_string_vec, verbose)
        test_sax_df = run_sax_along_df(test_df, sax, sax_words_as_string_vec, verbose)
        print_toSAX_csv(train_sax_df, i, window, nbins, alphabet_size, 'train')
        print_toSAX_csv(test_sax_df, i, window, nbins, alphabet_size, 'test')

        if verbose:
            print('SAXed dataset: {}\tstart time: {}\tend time: {}'.format(i, s_time, gt.time()))
            print('sax df shape: {}'.format(train_sax_df.shape))
        i += 1
Пример #8
0
def CreateAbnormalErrorSet(userName, apiToken):
    minutesOfConsistentErrors = random.randint(15, 90)

    overallErrorClass = random.randint(1, 4)
    elementInErrorClassArray = random.randint(0, 4)

    for i in range(minutesOfConsistentErrors):

        createdAt = GetTime.getTime()
        avgNumberOfErrorsPerMinute = random.randint(10, 25)

        for j in range(avgNumberOfErrorsPerMinute):
            eventDict = se.postEvent(userName, apiToken, overallErrorClass, elementInErrorClassArray, createdAt)
            sm.sendMeasurements(userName, apiToken, eventDict, avgNumberOfErrorsPerMinute, createdAt)

# Commented out all the multi-threading during inital stages to make sure everything is working
#
#
#
#
#

    # while numberOfEventsSent <= minutesOfConsistentErrors:
    #
    #
    #
    #     timeofLastAbnormalError = time.time()
    #     abnormalErrorSetInterval = random.uniform(1,3)
    #     numberOfEventsSent += 1


        # if time.time() >= (timeofLastAbnormalError + abnormalErrorSetInterval):
        #     createdAt = GetTime.getTime()
        #     timeofLastAbnormalError = time.time()
        #     abnormalErrorSetInterval = random.uniform(1,3)
        #     eventDict = se.postEvent(userName, apiToken, overallErrorClass, elementInErrorClassArray, createdAt)
        #     numberOfEventsSent += 1
        #
        #     sm.sendMeasurements(userName, apiToken, eventDict, avgNumberOfErrorsPerMinute, createdAt)

    # _thread.exit()
def main():

    #----------MAIN-------------#
    print(os.listdir())
    time = gt.GetTime()
    verbose = True
    full_run = True
    #----------SAX--------------#
    window = 4
    stride = window
    nbins = 5
    alphabet_size = 5
    alphabet = string.ascii_uppercase[:alphabet_size]
    sax = ToSAX(window, stride, nbins, alphabet)
    #-----------CLF------------#
    # grid_search = True
    k = 1
    njobs = cpu_count() - 1

    if verbose:
        print(time.initial_time)

    if full_run:
        if verbose: print('in full run')
        train_fns, test_fns, train_label_fns, test_label_fns = get_fns(sax)

        # dfs below are generators
        train_dfs = get_dataframes(train_fns)
        train_label_dfs = get_dataframes(train_label_fns, True)
        test_label_dfs = get_dataframes(test_label_fns, True)
        test_dfs = get_dataframes(test_fns)

        results_array = []

        i = 1
        for train_sax, train_labels, test_sax, test_labels in zip(
                train_dfs, train_label_dfs, test_dfs, test_label_dfs):

            s_time = gt.time()
            print(test_labels)
            # create a Bag of patterns and transform sax representations to a tfidf matrix
            BoP = BagOfPatterns(train_sax, train_labels, test_sax, test_labels,
                                sax)
            BoP.fit(sax_letter_cardinality=sax.alphabet_cardinality,
                    do_tfidf=True,
                    ngram_len=2,
                    min_term_freq=1,
                    max_term_freq=1)
            train_tfidf, test_tfidf = BoP.transform()

            # classify tfidfs using kNN
            kNN = KNeighborsClassifier(n_neighbors=k,
                                       weights='uniform',
                                       algorithm='auto',
                                       metric='minkowski',
                                       p=2,
                                       n_jobs=njobs)
            kNN.fit(train_tfidf, train_labels)
            test_predicted = kNN.predict(test_tfidf)
            print_results_to_csv(test_predicted, i, sax, time.initial_time,
                                 'kNN')

            if verbose:
                print('Results Found for dataset: {}\ttime: {}'.format(
                    i, gt.time()))
            i += 1

    else:
        print('in else')
        i = 1
        data_test_fn = 'hw1_datasets/dataset{}/test_sax_w{}_nb{}_v{}.csv'.format(
            i, window, nbins, alphabet_size)
        data_train_fn = 'hw1_datasets/dataset{}/train_sax_w{}_nb{}_v{}.csv'.format(
            i, window, nbins, alphabet_size)
        test_labels = 'hw1_datasets/dataset{}/test_labels.csv'.format(i)
        dataset_train_labels = 'hw1_datasets/dataset{}/train_labels.csv'.format(
            i)
        dataset_test_labels = 'hw1_datasets/dataset{}/test_labels.csv'.format(
            i)
        x_sax = pd.read_csv(data_train_fn, index_col=0)
        test_sax = pd.read_csv(data_test_fn, index_col=0)

        # converting from pandas to list for scikit learn
        train_labels = pd.read_csv(dataset_train_labels,
                                   index_col=0).transpose().values.tolist()[0]
        test_labels = pd.read_csv(dataset_test_labels,
                                  index_col=0).transpose().values.tolist()[0]

        # create a Bag of patterns and transform sax representations to a tfidf matrix
        BoP = BagOfPatterns(x_sax, train_labels, test_sax, test_labels, sax)
        BoP.fit(sax_letter_cardinality=sax.alphabet_cardinality,
                do_tfidf=True,
                ngram_len=2,
                min_term_freq=1,
                max_term_freq=1)
        train_tfidf, test_tfidf = BoP.transform()

        # classify tfidfs using kNN
        kNN = KNeighborsClassifier(n_neighbors=k,
                                   weights='uniform',
                                   algorithm='auto',
                                   metric='minkowski',
                                   p=2,
                                   n_jobs=njobs)
        kNN.fit(train_tfidf, train_labels)
        test_predicted = kNN.predict(test_tfidf)

        print_results_to_csv(test_predicted, i, sax, time.initial_time, 'kNN')

        if verbose: print('Results Found')

    if verbose:
        print('-------Completed!{}-------')
        print('Started at: {}\tFinished at: {}'.format(time.initial_time,
                                                       gt.time()))
Пример #10
0
apiToken = args.api
pollingFrequency = args.freq

# Metadata definition for the metrics, this will create the metric definitions
# if they do not exist
# dm.defineAllMetrics(userName, apiToken)

NON_CONSISTENT_ERROR_TYPE = 0

# Live action - push random errors in an ongoing basis
# at random intervals, also start sending consistent error messages (still with the random errors at the same speed)
counter = 0
total_error_counter = 0

while True:
    createdAt = GetTime.getTime()
    numberOfErrors = random.randint(1, 4)
    for i in range(numberOfErrors):
        eventDict = se.postEvent(userName, apiToken, NON_CONSISTENT_ERROR_TYPE,
                                 NON_CONSISTENT_ERROR_TYPE, createdAt)
        sm.sendMeasurements(userName, apiToken, eventDict, numberOfErrors,
                            createdAt)
        print(str(counter))
        counter += 1

# Commented out the abnormal issues section of the code during testing to make sure normal events hit the API
#
#
#
#
#
Пример #11
0
def get_dataframes(verbose, fn_arr):
    if verbose:
        print('In get_dataframes: {}'.format(gt.time()))
    file_generator = (pd.read_csv(fn, index_col=0) for fn in fn_arr)
    return file_generator
Пример #12
0
def find_prediction_metrics(dtw_list, k_list, dist='DTW', verbose=True):
    # Grid search crashed ONLY GOT W=5 PREDICTIONS for w in w_list:
    if dist == 'DTW':
        print('in DTW')
        for dtw_w in dtw_list:
            for k in k_list:
                for i in range(1, 6):  #each dataset
                    try:
                        if verbose:
                            print('TRY - dset: {}\ttime: {}\tw: {}\tk: {}'.
                                  format(i, gt.time(), dtw_w, k))
                        y = get_y(i)
                        yhat = get_class_predictions(i, dist, dtw_w, k)
                        f1 = f1_score(
                            y, yhat, average=None
                        )  # returns list score [pos neg], can use weighted
                        f1_avg = f1_score(
                            y, yhat, average='weighted'
                        )  # returns list score [pos neg], can use weighted
                        acc = accuracy_score(y, yhat)
                        class_counts = yhat[0].value_counts(sort=False)
                        save_kNN_metrics(i, dist, dtw_w, k, acc, f1, f1_avg,
                                         class_counts)
                        i += 1
                    except:
                        print('--------------')
                        print('These parameters were skipped:')
                        print(
                            'EXCEPT - dset: {}\ttime: {}\tw: {}\tk: {}'.format(
                                i, gt.time(), dtw_w, k))
                        print('test_output/test_results_dataset{}_{}_k{}.csv'.
                              format(i, dist, k))
                        print('--------------')
                        continue

    else:
        print('in EUC')
        for k in k_list:
            for i in range(1, 6):  #each dataset
                try:
                    if verbose:
                        print('TRY - dset: {}\ttime: {}\tk: {}'.format(
                            i, gt.time(), k))
                    y = get_y(i)
                    yhat = get_class_predictions(i, dist, None, k)
                    f1 = f1_score(
                        y, yhat, average=None
                    )  # returns list score [pos neg], can use weighted
                    f1_avg = f1_score(
                        y, yhat, average='weighted'
                    )  # returns list score [pos neg], can use weighted
                    acc = accuracy_score(y, yhat)
                    class_counts = yhat[0].value_counts(sort=False)
                    save_kNN_metrics(i, dist, np.nan, k, acc, f1, f1_avg,
                                     class_counts)
                    i += 1
                except:
                    print('--------------')
                    print('These parameters were skipped:')
                    print('EXCEPT - dset: {}\ttime: {}\tk: {}'.format(
                        i, gt.time(), k))
                    print(
                        'test_output/test_results_dataset{}_{}_k{}.csv'.format(
                            i, dist, k))
                    print('--------------')
                    continue
Пример #13
0
    #print(len(图片数组), type(图片数组))
    #print(图片数组)
    mpimg.imsave(fname, 图片数组)


def main(fname):
    '''主程序'''
    print('提示:如果直接回车,要转换的文字将为“要转换的文字.txt”中的文字')
    ms = input('输入要转换成图片的文字: \n ')
    print('开始时间:', GetTime.getTime())
    if ms == '':
        try:
            f = open('要转换的文字.txt', 'r', encoding='utf-8')
            ms = str(f.readlines())
            f.close()
        except:
            print('缺少默认文件')
    print('len(ms)=', len(ms))
    print('start', GetTime.getTime())
    saveimg(fname, str_to_arr(ms))
    print('结束时间:', GetTime.getTime())
    input('按任意键继续')


if __name__ == '__main__':
    #for i in range(9):
    filename = GetTime.getTime(2) + '.png'
    main(filename)
    print(filename, 'is ok')
    print('end', '--' * 10)
Пример #14
0
@LastModifiedBy: ywyz
@Github: https://github.com/ywyz
@LastEditors: ywyz
@LastEditTime: 2020-07-22 22:02:53
'''

import openpyxl
import GetTime
import datetime
import ProcessingForm
import CompareTime
import CopyCell

current_time = datetime.datetime.now()
# 获取当前时间
time = GetTime.GetTime(current_time)
time_strings = time.ReturnTimeStrings()
# 获取当前时间字符串
print("今日时间:", time_strings)
namestrings = time.TimeStrings()

names = input("输入文件名字或者拖动文件到这儿来:")
wb = openpyxl.load_workbook(names)  # 打开文件
wb_sheet = wb["Sheet1"]  # 打开工作簿1

# 获取表格最多行数
processForm = ProcessingForm.ProcessingForm(wb_sheet)
max_row = processForm.GetMaxROW()

# 比较时间,删除过时的行
time = CompareTime.CompareTime(wb_sheet, time_strings, max_row)
Пример #15
0
oled.text('Well Connected', 5, 20)  #显示连接是否成功及IP地址
oled.text("IP:" + wlan.ifconfig()[0], 0, 35)
oled.show()
utime.sleep(1)

rtc = RTC()
update_time = utime.ticks_ms() - First_time
print(update_time)

while True:
    if not wlan.isconnected():
        machine.reset()

    elif utime.ticks_ms() - update_time >= First_time:
        Year, Month, Day, Hour, Week, Minute, Second = GetTime.Get_Time(
        )  #通过GetTime文件爬取时间
        rtc.datetime(
            (Year, Month, Day, Week, Hour, Minute, Second, 0))  #使用RTC实时时钟记录时间
        #date = "{:02}-{:02}-{:02}".format(rtc.datetime()[0], rtc.datetime()[1], rtc.datetime()[2])
        #time = "{:02}:{:02}:{:02}".format(rtc.datetime()[4], rtc.datetime()[5], rtc.datetime()[6])
        Week = Chance.Week_English(rtc.datetime()[3])  #将数字星期变换成英文缩写

        Temperature_Now, Temperature_Min, Temperature_Max, Weather = GetWeather.Get_Weather(
        )
        Weather = Chance.Weather_English(Weather)

        update_time = utime.ticks_ms()

    else:
        update_time = utime.ticks_ms() - First_time + Second_time