Пример #1
0
def Main(edgeFile=None,outEdgeFile=None,nodeFile=None,nodeList=None,comp=None,subType=None,sep=None,log=None):
    """ Main program """
    i0 = time.clock()
    inext = i0
    ### Argument processing ========================================
    ## Filename definitions ======================================
    i0 = time.clock()
    ## Lecture des fichiers ========================================  
    if nodeFile:
        subnodes = ut.file2list(nodeFile)
    elif nodeList:
        subnodes = nodeList.strip().split(",")
    elif comp:
        compFile,compID = comp.strip().split(",")
        subnodes = ut.getNodes(compFile,compID)
    else:
        subnodes = None
    ## Corps du programme =========================================== The dictionary old_nodes --> new_nodes obtained as the completion of the clustering file
    ut.inducedSubgraph(edgeFile,subnodes=subnodes,nodeType=subType,outFile=outEdgeFile,sep=sep)
    ## Sortie ======================================================
    inext = myTimer(inext,handle=log)
    ## Ending ==============================================================
    prog = myModule()
    if prog == "__main__.py":
        prog = sys.argv[0].split("/")[-1]
    inext = myTimer(i0,"Total computing time for %s" % prog,handle=log)
    return
Пример #2
0
def test_str2tmp():
    txt = "This is a test string"
    filename = utils.str2tmp(txt)
    assert os.path.isfile(filename)
    assert open(filename).read() == txt + "\n"
    assert utils.file2str(filename) == txt
    # TODO: should this really return
    # an empty line at the end?
    assert utils.file2list(filename) == [txt, '']
    os.unlink(filename)
Пример #3
0
def grammarLoader():
    gram_in = [
        x.rstrip() for x in utils.file2list('grammar.txt')
        if not re.match('\\n|#', x)
    ]
    terS.extend(gram_in[0].split(','))
    i_nTerS.extend(gram_in[1].split(','))
    relations.update({
        k.rstrip(): (v.strip()).split('|')
        for k, v in map(lambda x: x.split('->'), gram_in[2:])
    })
    o_relations.update({f'{x}_f': set(x) for x in terS})
Пример #4
0
def random_sample(segment_per_class, sample_range):
    """ Returns the list of the random sample
               (new_indexes, time_series, label)

        Argument
        ---------
        segment_per_class
               The list of time_series
               The list of the corresponding indexes
    """
    dataset = {}
    poses = []
    if load_history_matrix:
        poses = file2list(POS_FILE)
    if poses:
        for k in segment_per_class.keys():
            dataset[k] = ([], [])
            for item in segment_per_class[k]:
                if item[1] in poses:
                    dataset[k][0].append(item[0])
                    dataset[k][1].append(item[1])
    else:
        for k in segment_per_class.keys():
            array = list2array(segment_per_class[k])
            if sample_range > 0:
                if len(segment_per_class[k]) > sample_range:
                    list_a = random.sample(segment_per_class[k], sample_range)
                    array = list2array(list_a)
            dataset[k] = (array[:, 0].tolist(), array[:, 1].tolist())
    dataset = intercept_part_dataSet(dataset)
    ts = []
    for label in dataset:
        (list_time_series, list_indexes) = dataset[label]
        for time_series, indexes in zip(list_time_series, list_indexes):
            # time_series = preprocessing(time_series)
            time_series.insert(0, data_index[label])
            ts.append(time_series)
    return ts
Пример #5
0
def model_test(model_path,
               clf=None,
               filename='out.txt',
               tcName=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']):
    path = sys.path[0]
    tbasePath = os.path.join(path, "mnist/test/")
    #tcName = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    tst = time.time()
    f = open(filename, "w")
    feature_cnt = len(tcName)
    allErrCount = 0
    allErrorRate = 0.0
    allScore = 0.0
    ErrCount = np.zeros(feature_cnt, int)
    TrueCount = np.zeros(feature_cnt, int)
    predict_list = []
    true_list = []

    if clf == None:
        clf = joblib.load(model_path)

    for tcn in tcName:
        testPath = tbasePath + tcn
        tflist = utils.file2list(testPath)
        tdataMat, tdataLabel = utils.read2convert(tflist)
        print("test dataMat shape: {0}, test dataLabel len: {1} ".format(
            tdataMat.shape, len(tdataLabel)),
              file=f)
        pre_st = time.time()
        preResult = clf.predict(tdataMat)
        pre_et = time.time()
        predict_list.append(preResult)
        true_list.append([tcn] * len(preResult))
        print("Recognition  " + tcn + " spent {:.4f}s.".format(
            (pre_et - pre_st)),
              file=f)
        print("predict result: {}".format(len(preResult)))
        errCount = len([x for x in preResult if x != tcn])
        ErrCount[int(tcn)] = errCount
        TrueCount[int(tcn)] = len(tdataLabel) - errCount
        print("errorCount: {}.".format(errCount), file=f)
        allErrCount += errCount
        score_st = time.time()
        score = clf.score(tdataMat, tdataLabel)
        score_et = time.time()
        print("computing score spent {:.6f}s.".format(score_et - score_st),
              file=f)
        allScore += score
        print("score: {:.6f}.".format(score), file=f)
        print("error rate is {:.6f}.".format((1 - score)), file=f)

    tet = time.time()
    print("Testing All class total spent {:.6f}s.".format(tet - tst), file=f)
    print("All error Count is: {}.".format(allErrCount), file=f)
    avgAccuracy = allScore / (feature_cnt * 1.0)
    print("Average accuracy is: {:.6f}.".format(avgAccuracy), file=f)
    print("Average error rate is: {:.6f}.".format(1 - avgAccuracy), file=f)
    print("number", " TrueCount", " ErrCount", file=f)
    for tcn in tcName:
        tcn = int(tcn)
        print(tcn, "     ", TrueCount[tcn], "      ", ErrCount[tcn], file=f)
    plt.figure(figsize=(12, 6))
    x = list(range(feature_cnt))
    plt.plot(x, TrueCount, color='blue', label="TrueCount")  # 将正确的数量设置为蓝色
    plt.plot(x, ErrCount, color='red', label="ErrCount")  # 将错误的数量为红色
    plt.legend(loc='best')  # 显示图例的位置,这里为右下方
    plt.title('Projects')
    plt.xlabel('number')  # x轴标签
    plt.ylabel('count')  # y轴标签
    plt.xticks(np.arange(feature_cnt), tcName)
    plt.savefig('./accuracy_%s.jpg' % filename.split('_')[1])
    plt.show()
    f.close()

    true_list = np.array(true_list).flatten()
    predict_list = np.array(predict_list).flatten()
    return [predict_list, true_list]