コード例 #1
0
    def execute_query(self, query_map):
        """
        This method is called to execute a query
        """
        scores = {}
        query_map_copy = copy.deepcopy(query_map)
        for term in query_map:
            q_idf, term_offset = self.dictionary.term(term)

            # unknown term, skip everything, score 0
            if term_offset is None:
                continue

            # accumulate scores for postings list
            query_map[term] = q_wt = tf(query_map[term]) * q_idf
            postings_list = self._get_postings(term_offset)
            self._accumulate_scores(scores, postings_list, q_wt)

        # perform length normalization (query and document)
        q_len = math.sqrt(sum(x * x for x in query_map.values()))
        self._normalize(scores, q_len)

        # if havent done relevance feedback, do relevance feedback
        if not self.feedback:
            top_n_docs = self._get_top_n_docs(scores, Engine.NUM_RESULTS)
            stringout = self.relevance_feedback(query_map_copy, top_n_docs)

        # if here, calling from within relevance feedback
        else:
            # return the output of all the scores after relevance feedback
            stringout = " ".join(str(x) for x in scores.keys())

        return stringout
コード例 #2
0
    def execute_query(self, query_map):
        """
        This method is called to execute a query
        """
        scores = {}
        for term in query_map:
            q_idf, term_offset = self.dictionary.term(term)

            # unknown term, skip everything, score 0
            if term_offset is None:
                continue

            # accumulate scores for postings list
            query_map[term] = q_wt = tf(query_map[term]) * q_idf
            postings_list = self._get_postings(term_offset)
            self._accumulate_scores(scores, postings_list, q_wt)

        # perform length normalization (query and document)
        q_len = math.sqrt(sum(x * x for x in query_map.values()))
        self._normalize(scores, q_len)

        # find top n
        # top_n_docs = self._get_top_n_docs(scores, Engine.NUM_RESULTS)
        # return " ".join(str(x) for x in top_n_docs)

        return " ".join(str(x) for x in scores.keys())
コード例 #3
0
def index(directory: str, dict_file: str, post_file: str):
    """
    Core of the module. Index all the file of a specified directory into a couple
    of Dictionary and Postings. The Dictionary stores the amount of documents
    and of the entries (with their frequency, offset in the postings and size (in bytes))
    Whereas the Postings file stores the list of lists of tuple document, frequency.
    The postings are useless without the Dictonary.

    *params*:
        - directory: The directory containing the file to index
        - dict_file: The file that will contain the Dictionary
        - post_file: The file that will contain the Postings
    """

    dict_builder = defaultdict(dict)
    file_list = get_file_list(directory)  # [:10]
    # Generate Dict
    for in_file in file_list:
        tokens = generate_token(directory + str(in_file))
        weighted_tf = normalize([tf(y) for (x, y) in tokens.items()])
        for ((term, freq), w_tf) in zip(tokens.items(), weighted_tf):
            dict_builder[term][in_file] = Term(freq, w_tf)

    # Write Postings
    dict_term = defaultdict(Entry)
    with open(post_file, mode="wb") as postings_file:
        for key, value in dict_builder.items():
            offset = postings_file.tell()
            size = postings_file.write(pickle.dumps(value))
            dict_term[key] = Entry(len(value), offset, size)

    # Write Dictionary
    with open(dict_file, mode="wb") as dictionary_file:
        pickle.dump(len(file_list), dictionary_file)
        pickle.dump(dict_term, dictionary_file)
コード例 #4
0
ファイル: build_index.py プロジェクト: racheltanxueqi/CS3245
def add_vocab_to_index(doc_id, vocab, d, p):
    doc_len_squared = 0
    for term in vocab.iterkeys():
        tf_val = tf(vocab[term])
        if d.does_term_exist(term):
            frequecy, offset = d.term(term)
            d.add_term(term, offset)
            p.list_at_offset(offset).add((doc_id, tf_val))
        else:
            p_list, offset = p.new_list()
            d.add_term(term, offset, add_to_offset_index=True)
            p_list.add((doc_id, tf_val))
        doc_len_squared += tf_val * tf_val
    d.add_doc_id(doc_id, sqrt(doc_len_squared))
コード例 #5
0
def Gstable(G,poles):
    Gmul = 1
    for p in poles:
        Gmul *= (s - p)/(s + p)
    Gs = Gmul*G
    num = Gs.numerator.c
    den = Gs.denominator.c
    sym_num = symbolic_poly(num)
    sym_den = symbolic_poly(den)
    fraction = (sym_num/sym_den).simplify()
    numer, denom = fraction.as_numer_denom()
    if numer.find('s'):
        num_coeff  = ceoff_symbolic_poly(numer)
    else:
        num_coeff = as_int(numer)
    if denom.find('s'):
        den_coeff  = ceoff_symbolic_poly(denom)
    else:
        den_coeff = denom
    return tf(num_coeff,den_coeff)
コード例 #6
0
def Gstable(G, poles):
    Gmul = 1
    for p in poles:
        Gmul *= (s - p) / (s + p)
    Gs = Gmul * G
    num = Gs.numerator.c
    den = Gs.denominator.c
    sym_num = symbolic_poly(num)
    sym_den = symbolic_poly(den)
    fraction = (sym_num / sym_den).simplify()
    numer, denom = fraction.as_numer_denom()
    if numer.find('s'):
        num_coeff = ceoff_symbolic_poly(numer)
    else:
        num_coeff = as_int(numer)
    if denom.find('s'):
        den_coeff = ceoff_symbolic_poly(denom)
    else:
        den_coeff = denom
    return tf(num_coeff, den_coeff)
コード例 #7
0
from __future__ import print_function
import numpy as np

from utils import poles, zeros, tf, mimotf

s = tf([1, 0], [1])
G = 1 / (s + 2) * mimotf([[s - 1, 4], [4.5, 2 * (s - 1)]])
print('Poles: ', poles(G))
print('Zeros: ', zeros(G))
コード例 #8
0
def main():
    o_model_rf = 'randfor.sav'
    o_model_xgb = 'xgboost.sav'

    usage = "\n %(prog)s -r full_path_to_file -m action -f model -o optimize [options]"

    parser = argparse.ArgumentParser(usage=usage)
    parser.add_argument("-q",
                        action="store_true",
                        dest="quite",
                        default=False,
                        help="don't print status messages to stdout")

    parser.add_argument("-r",
                        dest="filename",
                        help="full path to read_map file")
    parser.add_argument("-m",
                        dest="action",
                        help="train, looptrain, or predict?")
    parser.add_argument(
        "-f",
        dest="fitmodel",
        help="model to train or to load, (randfor, xgboost, both)")
    parser.add_argument("-o",
                        dest="optimize",
                        help="optimize training paramters")

    args = parser.parse_args()
    if not args.action or not args.filename or not args.fitmodel or not args.optimize:
        print("Some input is missing!")
        parser.print_help()
        sys.exit(1)

    if not os.path.exists(args.filename):
        print("Sorry, file ", args.filename, "does not exists")
        sys.exit(1)

    # Train
    if args.action == 'train' or args.action == 'looptrain':
        if args.fitmodel != "randfor" and args.fitmodel != "xgboost" and args.fitmodel != "both":
            print(
                "\n Model ", args.fitmodel,
                " not recognized, please choose between \"randfor\" (Random Forest), \"xgboost\" (xgboost), or \"both\" "
            )
            sys.exit(1)

    # Predict
    elif args.action == 'predict':

        if args.fitmodel == "randfor" or args.fitmodel == "both":
            if not os.path.exists(o_model_rf):
                print("Sorry, Random Forest model to load not found")
                sys.exit(1)
        if args.fitmodel == "xgboost" or args.fitmodel == "both":
            if not os.path.exists(o_model_xgb):
                print("Sorry, XGBoost model to load not found")
                sys.exit(1)

        if args.fitmodel != "randfor" and args.fitmodel != "xgboost" and args.fitmodel != "both":
            if not os.path.exists(args.fitmodel):
                print("Sorry, model to load not found", args.fitmodel)
                sys.exit(1)

    # Wrong command
    else:
        print("I don't understand your command:", args.action,
              "\nPlease choose: train or predict")
        sys.exit(1)

    inputfile = args.filename
    action = args.action
    fitmodel = args.fitmodel
    optimize = int(args.optimize)

    # minimum scaffold size
    min_size = 100000

    ############################
    ########## Train ###########
    # loop over scaffold min_size
    if action == "looptrain":
        print("Loop Training on scaffold length")

        sizes = [
            50000, 100000, 500000, 700000, 800000, 900000, 1000000, 1500000,
            2000000
        ]
        si = [
            '50K', '100K', '500k', '700K', '800K', '900K', '1M', '1.5M', '2M'
        ]

        x = None
        y = None
        X_train = None
        X_test = None
        y_train = None
        y_test = None

        filename = fitmodel + 'loop_train.txt'
        fp = open(filename, 'w')
        for i, min_size in enumerate(sizes):
            del x, y, X_train, X_test, y_train, y_test

            x, y = utils.read_data(inputfile, min_size, 1)

            ### Split sample in Train and Test to estimate Accuracy ###
            test_perc = 0.2  # perc size of test sample
            X_train, X_test, y_train, y_test = utils.resize(x, y, test_perc)

            print(" Sample", si[i])

            if fitmodel == "randfor" or fitmodel == "both":

                n_est = 100  # same scores as 700 ??
                maxf = 'log2'
                crit = 'entropy'
                min_s_leaf = 3  #10

                if optimize == 1:
                    n_est, maxf, crit, min_s_leaf = utils.best_randforest_cl(
                        x, y)

                clf_rf = utils.randforest_cl(X_train, y_train, n_est, maxf,
                                             crit, min_s_leaf)
                #joblib.dump(clf_rf,o_model_rf)

            if fitmodel == "xgboost" or fitmodel == "both":
                booster = 'gbtree'
                eta = 0.1
                max_depth = 30
                subsample = 0.5

                if optimize == 1:
                    max_depth, subsample = utils.best_xgboost_cl(x, y)

                clf_xgb = utils.xgboost_cl(X_train, y_train, booster, eta,
                                           max_depth, subsample)
                #joblib.dump(clf_xgb,o_model_xgb)

            ######################################
            ### Test Predict with chosen Model ###

            print("\n Test sample: ")
            if fitmodel == "randfor":
                y_pred = clf_rf.predict(X_test)
                score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                    y_pred, y_test)
                rf_scores = str(tf(score)) + " " + str(pos_ok) + " " + str(
                    false_neg) + " " + str(of(err_pos)) + " " + str(
                        neg_ok) + " " + str(false_pos) + " " + str(of(err_neg))
                xgb_scores = ''
                comb_score = ''

            elif fitmodel == "xgboost":
                y_pred = clf_xgb.predict(X_test)
                score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                    y_pred, y_test)
                xgb_scores = str(tf(score)) + " " + str(pos_ok) + " " + str(
                    false_neg) + " " + str(of(err_pos)) + " " + str(
                        neg_ok) + " " + str(false_pos) + " " + str(of(err_neg))
                rf_scores = ''
                comb_score = ''

            elif fitmodel == "both":
                y_pred_rf = clf_rf.predict(X_test)
                score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                    y_pred_rf, y_test)
                rf_scores = str(tf(score)) + " " + str(pos_ok) + " " + str(
                    false_neg) + " " + str(of(err_pos)) + " " + str(
                        neg_ok) + " " + str(false_pos) + " " + str(of(err_neg))

                y_pred_xgb = clf_xgb.predict(X_test)
                score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                    y_pred_xgb, y_test)
                xgb_scores = str(tf(score)) + " " + str(pos_ok) + " " + str(
                    false_neg) + " " + str(of(err_pos)) + " " + str(
                        neg_ok) + " " + str(false_pos) + " " + str(of(err_neg))

                # Combine models
                y_pred = np.array(y_pred_rf) | np.array(y_pred_xgb)

                ### Estimate Accuracy and Confusion Matrix on Test sample
                score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                    y_pred, y_test)
                comb_score = str(tf(score)) + " " + str(pos_ok) + " " + str(
                    false_neg) + " " + str(of(err_pos)) + " " + str(
                        neg_ok) + " " + str(false_pos) + " " + str(of(err_neg))
                #print("  Score :", (tf(tf(score))),"\n  Pos_ok:", pos_ok, "False Neg:", false_neg, " Pos error:", of(of(err_pos)) ,
                #   "%\n  Neg_ok:", neg_ok, "False_pos:", false_pos, " Neg error:", of(of(err_neg)) ,"%")

            line = si[
                i] + " " + rf_scores + " " + xgb_scores + " " + comb_score + "\n"
            print(line)
            fp.write(line)

        fp.close()

        # Plot results for score and positive: how many we miss? how much contamination?
        col_names = [
            'sample', 'score_rf', 'pos_rf', 'fneg_rf', 'epos_rf', 'neg_rf',
            'fpos_rf', 'eneg_rf', 'score_xgb', 'pos_xgb', 'fneg_xgb',
            'epos_xgb', 'neg_xgb', 'fpos_xgb', 'eneg_xgb', 'score', 'pos',
            'fneg', 'epos', 'neg', 'fpos', 'eneg'
        ]
        df = pd.read_csv(filename, sep=" ", names=col_names, header=None)

        fig, axes = plt.subplots(nrows=2, ncols=2)
        i = 0
        j = 0
        df.score_rf.plot(ax=axes[i, j],
                         label='Random Forest',
                         legend=True,
                         title="Accuracy Score on Test")
        df.score_xgb.plot(ax=axes[i, j], label='XGBoost', legend=True)
        df.score.plot(ax=axes[i, j], label='Combined', legend=True)
        j = 1
        df.pos_rf.plot(ax=axes[i, j],
                       label='Random Forest',
                       legend=True,
                       title="Correct Positive")
        df.pos_xgb.plot(ax=axes[i, j], label='XGBoost', legend=True)
        df.pos.plot(ax=axes[i, j], label='Combined', legend=True)
        i = 1
        j = 0
        df.fneg_rf.plot(ax=axes[i, j],
                        label='Random Forest',
                        legend=True,
                        title="False Negative =  Missing")
        df.fneg_xgb.plot(ax=axes[i, j], label='XGBoost', legend=True)
        df.fneg.plot(ax=axes[i, j], label='Combined', legend=True)
        j = 1
        df.fpos_rf.plot(ax=axes[i, j],
                        label='Random Forest',
                        legend=True,
                        title="False Positive = Contamination")
        df.fpos_xgb.plot(ax=axes[i, j], label='XGBoost', legend=True)
        df.fpos.plot(ax=axes[i, j], label='Combined', legend=True)

        #plt.show()
        fig.savefig(filename + '.png')
        plt.close(fig)

    elif action == "train":
        print("Training with scaffold > ", min_size)

        x, y = utils.read_data(inputfile, min_size, 1)

        ### Split sample in Train and Test to estimate Accuracy ###
        test_perc = 0.2  # perc size of test sample
        X_train, X_test, y_train, y_test = utils.resize(x, y, test_perc)

        ### Pick Model, Fit and Dump to file ###
        if fitmodel == "randfor" or fitmodel == "both":

            n_est = 100  # same scores as 700 ??
            maxf = 'log2'
            crit = 'entropy'
            min_s_leaf = 3  #10

            if optimize == 1:
                n_est, maxf, crit, min_s_leaf = utils.best_randforest_cl(x, y)
                print(" Best xgboost")
                #{'criterion': 'entropy', 'max_features': 'log2',
                #     'min_samples_leaf': 10, 'n_estimators': 700}
            #for min_size=100K: {'criterion': 'gini', 'max_features': 'log2', 'min_samples_leaf': 1, 'n_estimators': 1000}

            clf_rf = utils.randforest_cl(X_train, y_train, n_est, maxf, crit,
                                         min_s_leaf)
            joblib.dump(clf_rf, o_model_rf)

        if fitmodel == "xgboost" or fitmodel == "both":
            booster = 'gbtree'
            eta = 0.1
            max_depth = 30
            subsample = 0.5

            if optimize == 1:
                print(" Best xgboost")
                max_depth, subsample = utils.best_xgboost_cl(x, y)
                # {'max_depth': 30, 'subsample': 0.5} #booster, eta, not in version?
                #for min_size=100K:
            clf_xgb = utils.xgboost_cl(X_train, y_train, booster, eta,
                                       max_depth, subsample)
            joblib.dump(clf_xgb, o_model_xgb)

        print("\n Train sample: ")
        ### Predict with chosen Model ###
        if fitmodel == "randfor":
            y_pred = clf_rf.predict(X_train)
        elif fitmodel == "xgboost":
            y_pred = clf_xgb.predict(X_train)
        elif fitmodel == "both":
            y_pred_rf = clf_rf.predict(X_train)
            print("Random Forest Accuracy")
            score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                y_pred_rf, y_train)
            print("  Score :", (tf(score)), "\n  Pos_ok:", pos_ok,
                  "False Neg:", false_neg, " Pos error:", of(err_pos),
                  "%\n  Neg_ok:", neg_ok, "False_pos:", false_pos,
                  " Neg error:", of(err_neg), "%")

            y_pred_xgb = clf_xgb.predict(X_train)
            print("Xgboost Accuracy")
            score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                y_pred_xgb, y_train)
            print("  Score :", (tf(score)), "\n  Pos_ok:", pos_ok,
                  "False Neg:", false_neg, " Pos error:", of(err_pos),
                  "%\n  Neg_ok:", neg_ok, "False_pos:", false_pos,
                  " Neg error:", of(err_neg), "%")

            #y_pred = y_pred_rf * y_pred_xgb
            y_pred = np.array(y_pred_rf) | np.array(y_pred_xgb)
            print("Xgboost|RandForest Accuracy")
            #[x or y for (x, y) in zip(a, b)]   wo numpy

        ### Estimate Accuracy and Confusion Matrix on Training sample ###
        score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
            y_pred, y_train)
        ### Print Score and Confusion Matrix ###
        print("  Score :", (tf(score)), " f1 score :", tf(f1score),
              "\n  Pos_ok:", pos_ok, "False Neg:", false_neg, " Pos error:",
              of(false_neg * 100 / (false_neg + pos_ok)), "%\n  Neg_ok:",
              neg_ok, "False_pos:", false_pos, " Neg error:",
              of(false_pos * 100 / (false_pos + neg_ok)), "%")

        print(len(y_pred), y_pred.sum())

        ######################################
        ### Test Predict with chosen Model ###

        print("\n Test sample: ")
        #print("  Tot Real Pos:", sum(y_test))
        if fitmodel == "randfor":
            y_pred = clf_rf.predict(X_test)
        elif fitmodel == "xgboost":
            y_pred = clf_xgb.predict(X_test)
        elif fitmodel == "both":
            y_pred_rf = clf_rf.predict(X_test)
            print("Random Forest Accuracy")
            #print("  Predicted pos: ", y_pred_rf.sum(), " Predicted Neg: ", len(y_pred_rf)-y_pred_rf.sum() )
            score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                y_pred_rf, y_test)
            print("  Score :", (tf(score)), "\n  Pos_ok:", pos_ok,
                  "False Neg:", false_neg, " Pos error:", of(err_pos),
                  "%\n  Neg_ok:", neg_ok, "False_pos:", false_pos,
                  " Neg error:", of(err_neg), "%")
            y_pred_xgb = clf_xgb.predict(X_test)
            print("Xgboost Accuracy")
            #print("  Predicted pos: ", y_pred_xgb.sum(), " Predicted Neg: ", len(y_pred_xgb)-y_pred_xgb.sum() )
            score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
                y_pred_xgb, y_test)
            print("  Score :", (tf(score)), "\n  Pos_ok:", pos_ok,
                  "False Neg:", false_neg, " Pos error:", of(err_pos),
                  "%\n  Neg_ok:", neg_ok, "False_pos:", false_pos,
                  " Neg error:", of(err_neg), "%")

            # Combine models?
            #y_pred = y_pred_rf * y_pred_xgb
            y_pred = np.array(y_pred_rf) | np.array(y_pred_xgb)
            print("Xgboost|RandForest Accuracy")

        ### Estimate Accuracy and Confusion Matrix on Test sample ###
        #print("  Predicted pos: ", y_pred.sum(), " Tot Neg: ", len(y_pred)-y_pred.sum() )
        score, pos_ok, false_neg, err_pos, neg_ok, false_pos, err_neg = utils.get_accuracy_cl(
            y_pred, y_test)
        print("  Score :", (tf(score)),
              "\n  Pos_ok:", pos_ok, "False Neg:", false_neg, " Pos error:",
              of(err_pos), "%\n  Neg_ok:", neg_ok, "False_pos:", false_pos,
              " Neg error:", of(err_neg), "%")

    ############################
    ######## Predict ###########

    elif action == "predict":
        print("Predicting with scaffold > ", min_size)

        x, pairs = utils.read_data(inputfile, min_size, 0)
        # Important: x and pairs must keep the same indexes: no shuffling, sorting or similar

        ### Pick and Load Model ###
        if fitmodel == "randfor":
            clf = joblib.load('randfor.sav')
            y_pred = clf.predict(x)
        elif fitmodel == "xgboost":
            clf = joblib.load('xgboost.sav')
            y_pred = clf.predict(x)

        elif fitmodel == "both":
            clf_rf = joblib.load('randfor.sav')
            y_pred_rf = clf_rf.predict(x)

            clf_xgb = joblib.load('xgboost.sav')
            y_pred_xgb = clf_xgb.predict(x)

            y_pred = np.array(y_pred_rf) | np.array(y_pred_xgb)

        else:
            clf = joblib.load(fitmodel)
            y_pred = clf.predict(x)
            fitmodel = os.path.split(fitmodel)[1]

        ### Dump prediction to file ###
        o_pairs = 'real_pairs_' + fitmodel + '.txt'
        fp = open(o_pairs, 'w')
        for i, j in enumerate(y_pred):
            ### Dump to File if it's predicted to be a Real Connection ###
            if j == 1:
                fp.write(pairs[i, 0] + " " + pairs[i, 1] + "\n")

        fp.close()
コード例 #9
0
def step(G,
         t_end=100,
         initial_val=0,
         input_label=None,
         output_label=None,
         points=1000):
    """
    This function is similar to the MatLab step function.

    Parameters
    ----------
    G : tf
        Plant transfer function.
    t_end : integer
        Time period which the step response should occur (optional).
    initial_val : integer
        Starting value to evaluate step response (optional).
    input_label : array
        List of input variable labels.
    output_label : array
        List of output variable labels.

    Returns
    -------
    Plot : matplotlib figure
    """

    plt.gcf().set_facecolor('white')

    rows = numpy.shape(G(0))[0]
    columns = numpy.shape(G(0))[1]

    s = utils.tf([1, 0])
    system = G(s)

    if (input_label is None) and (output_label is None):
        labels = False
    elif ((numpy.shape(input_label)[0] == columns)
          and (numpy.shape(output_label)[0] == rows)):
        labels = True
    else:
        raise ValueError('Label count is inconsistent to plant size')

    fig = adjust_spine('Time', 'Output magnitude', -0.05, 0.1, 0.8, 0.9)

    cnt = 0
    tspace = numpy.linspace(0, t_end, points)
    for i in range(rows):
        for j in range(columns):
            cnt += 1
            nulspace = numpy.zeros(points)
            ax = fig.add_subplot(rows + 1, columns, cnt)
            tf = system[i, j]
            if all(tf.numerator) != 0:
                realstep = numpy.real(tf.step(initial_val, tspace))
                ax.plot(realstep[0], realstep[1])
            else:
                ax.plot(tspace, nulspace)
            if labels:
                ax.set_title('Output ({}) vs. Input ({})'.format(
                    output_label[i], input_label[j]))
            else:
                ax.set_title('Output {} vs. Input {}'.format(i + 1, j + 1))

            if i == 0:
                xax = ax
            else:
                ax.sharex = xax

            if j == 0:
                yax = ax
                ax.set_ylabel = output_label[j]
            else:
                ax.sharey = yax

            plt.setp(ax.get_yticklabels(), fontsize=10)
            box = ax.get_position()
            ax.set_position([
                box.x0, box.y0 * 1.05 - 0.05, box.width * 0.8, box.height * 0.9
            ])
コード例 #10
0
ファイル: utilsplot.py プロジェクト: Mvuyiso/Skogestad-Python
def step(G, t_end=100, initial_val=0, input_label=None, output_label=None, points=1000):
    """
    This function is similar to the MatLab step function.

    Parameters
    ----------
    G : tf
        Plant transfer function.
    t_end : integer
        Time period which the step response should occur (optional).
    initial_val : integer
        Starting value to evaluate step response (optional).
    input_label : array
        List of input variable labels.
    output_label : array
        List of output variable labels.

    Returns
    -------
    Plot : matplotlib figure
    """

    plt.gcf().set_facecolor('white')

    rows = numpy.shape(G(0))[0]
    columns = numpy.shape(G(0))[1]

    s = utils.tf([1, 0])
    system = G(s)

    if (input_label is None) and (output_label is None):
        labels = False
    elif (numpy.shape(input_label)[0] == columns) and (numpy.shape(output_label)[0] == rows):
        labels = True
    else:
        raise ValueError('Label count is inconsistent to plant size')

    fig = adjust_spine('Time', 'Output magnitude', -0.05, 0.1, 0.8, 0.9)

    cnt = 0
    tspace = numpy.linspace(0, t_end, points)
    for i in range(rows):
        for j in range(columns):
            cnt += 1
            nulspace = numpy.zeros(points)
            ax = fig.add_subplot(rows + 1, columns, cnt)
            tf = system[i, j]
            if all(tf.numerator) != 0:
                realstep = numpy.real(tf.step(initial_val, tspace))
                ax.plot(realstep[0], realstep[1])
            else:
                ax.plot(tspace, nulspace)
            if labels:
                ax.set_title('Output ({}) vs. Input ({})'.format(output_label[i], input_label[j]))
            else:
                ax.set_title('Output {} vs. Input {}'.format(i + 1, j + 1))

            if i == 0:
                xax = ax
            else:
                ax.sharex = xax

            if j == 0:
                yax = ax
                ax.set_ylabel = output_label[j]
            else:
                ax.sharey =yax

            plt.setp(ax.get_yticklabels(), fontsize=10)
            box = ax.get_position()
            ax.set_position([box.x0, box.y0 * 1.05 - 0.05,
                             box.width * 0.8, box.height * 0.9])
コード例 #11
0
ファイル: connect.py プロジェクト: u27538355/Skogestad-Python
from __future__ import print_function
from utils import tf, feedback, tf_step
import matplotlib.pyplot as plt
import utilsplot
"""
This function aims to be the Python equivalent of the MatLab connect function
Reference:
http://www.mathworks.com/help/control/examples/connecting-models.html

The example used here is the same as in the reference
"""

# First example in reference to demonstrate working of tf object

# Define s as tf to enable its use as a variable
s = tf([1, 0])

# Define the transfer functions
F = tf(1, [1, 1])
G = tf(100, [1, 5, 100])
C = 20 * (s**2 + s + 60) / s / (s**2 + 40 * s + 400)
S = tf(10, [1, 10])

T = F * feedback(G * C, S)
# This is NOT the same figure as in the reference
t, y = tf_step(T, 6)
plt.plot(t, y)
plt.xlabel('Time')
plt.ylabel('y(t)')
plt.show()
#utilsplot.step(T, t_end = 6)
コード例 #12
0
    plt.figure('Combined SISO Controllability Rules')
    plt.loglog(w, np.abs(G(s)), label='|G|')
    plt.loglog(w, np.abs(Gd(s)), label='|Gd|')
    plt.loglog(w, np.abs(L(s)), label='|L|')
    plt.axvline(wd, ls='--', color='k', label='$\omega_d$')
    plt.axvline(wc, ls='--', color='k', label='$\omega_c$')
    plt.scatter(wp, 1, color='blue', label='$2p$')
    plt.scatter(wz, 1, color='lime', label='$z/2$')
    plt.scatter(wu, 1, color='magenta', label='$\omega_u$')
    plt.scatter(wtd, 1, color='red', label='$1/ \theta$')
    plt.legend()
    plt.xlim([10**-4, 10**4])
    plt.ylim([10**-1, 10**3])
    plt.show()


if __name__ == '__main__':  # only executed when called directly

    s = tf([1, 0], 1)

    # Example plant based on Example 2.9 and Example 2.16
    G = (s + 200) / ((10 * s + 1) * (0.05 * s + 1)**2)
    deadtime = 0.002
    Gd = 33 / (10 * s + 1)
    K = 0.4 * ((s + 2) / s) * (0.075 * s + 1)
    R = 3.0
    wr = 10
    Gm = 1  # Measurement model

    allSISOrules(G, deadtime, Gd, K, R, wr, Gm)
コード例 #13
0
ファイル: connect.py プロジェクト: Mvuyiso/Skogestad-Python
"""
from __future__ import print_function
from utils import tf, feedback, tf_step

"""
This function aims to be the Python equivalent of the MatLab connect function
Reference:
http://www.mathworks.com/help/control/examples/connecting-models.html

The example used here is the same as in the reference
"""

# First example in reference to demonstrate working of tf object

# Define s as tf to enable its use as a variable
s = tf([1, 0])

# Define the transfer functions
F = tf(1, [1, 1])
G = tf(100, [1, 5, 100])
C = 20*(s**2 + s + 60) / s / (s**2 + 40*s + 400)
S = tf(10, [1, 10])

T = F * feedback(G*C, S)

# This is the same figure as in the reference

tf_step(T, 6)


コード例 #14
0
from utils import tf, mimotf, poles, zeros


s = tf([1,0],[1])
T1 = (2*s + 1)/(s**2 + 0.8*s + 1)
T2 = (-2*s + 1)/(s**2 + 0.8*s + 1)

S1 = 1 - T1
S2 = 1 - T2

print("S1 contains RHP zero %.2f, therefore L1 contains a RHP pole at %.2f" %(S1.zeros()[0],S1.zeros()[0]))
print("T2 contains RHP zero %.2f, therefore L2 contains the same RHP zero %.2f" %(T2.zeros(),T2.zeros()))
コード例 #15
0
import numpy as np
import matplotlib.pyplot as plt

from utils import tf

WP = tf([0.25, 0.1], [1, 0])
WU = tf([1, 0], [1, 1])
GK1 = tf([0, 0.5], [1, 0])
GK2 = tf([0, -0.5, 0.5], [1, 1, 0])

w = np.logspace(-2, 2, 500)
s = 1j * w
ru = np.linspace(0, 1, 10)
"""Solution to part a,b is on pg 284, only part c is illustrateed here"""
plt.loglog(w, np.abs(1 / (1 + GK1(s))), label='$S_1$')
plt.loglog(w, np.abs(1 / (1 + GK2(s))), label='$S_2$')

for i in range(np.size(ru)):
    if i == 1:
        plt.loglog(w,
                   1 / (np.abs(WP(s)) + np.abs(ru[i] * WU(s))),
                   'k--',
                   label='varying $r_u$')
    else:
        plt.loglog(w, 1 / (np.abs(WP(s)) + np.abs(ru[i] * WU(s))), 'k--')

plt.loglog(w,
           1 / (np.abs(WP(s)) + np.abs(0.75 * WU(s))),
           'r',
           label='$r_u$ = 0.75')
plt.legend(loc=2)
コード例 #16
0
import matplotlib.pyplot as plt
from math import pi
from utils import tf

def LI(rk, tmax, w):
    L = np.zeros(np.size(w))
    for i in range(np.size(w)):
        if w[i-1] < pi/tmax:
            L[i-1] = np.sqrt(rk**2 + 2*(1+rk)*(1-np.cos(tmax*w[i-1])))
        else:
            L[i-1] = 2 + rk
    return L

rk = 0.2
tmax = 1

WI = tf([(1 + rk*0.5)*tmax, rk], [tmax*0.5, 1])
WI_improved = WI*tf([(tmax/2.363)**2, 2*0.838*(tmax/2.363), 1], [(tmax/2.363)**2, 2*0.685*(tmax/2.363), 1])

w = np.logspace(-2, 2, 500)
s = 1j*w

plt.loglog(w, LI(rk, tmax, w), 'r', label='$l_I$')
plt.loglog(w, np.abs(WI(s)), 'b--', label='$\omega_I$')
plt.loglog(w, np.abs(WI_improved(s)), color = 'lime', label='$improved$ $\omega_I$')
plt.legend()
plt.title(r'Figure 7.9')
plt.xlabel(r'Frequency [rad/s]', fontsize=12)
plt.ylabel(r'Magnitude', fontsize=12)
plt.show()
コード例 #17
0
def add_deadtime_SISO(G, deadtime):
    G = (tf(list(G.numerator.coeffs),
            list(G.denominator.coeffs),
            deadtime=deadtime))
    return G
コード例 #18
0
ファイル: BODE.py プロジェクト: ruanne/Skogestad-Python
import numpy as np
import scipy as sc
import scipy.signal as scs
import matplotlib.pyplot as plt
from utils import phase, tf

G = tf(8, [1, 8]) * tf(1, [1, 1])

# freq(G) returns a frequency response function given a Laplace function

def freq(G):
    def Gw(w):
        return G(1j*w)
    return Gw

def margins(G):
    """ Calculate the gain margin and phase margin of a system.

    Input: G - a function of s
    Outputs:
       GM    Gain margin
       PM    Phase margin
       wc    Gain crossover frequency
       w_180 Phase Crossover frequency
    """

    Gw = freq(G)

    def mod(x):
        """to give the function to calculate |G(jw)| = 1"""
        return np.abs(Gw(x)) - 1
コード例 #19
0
import matplotlib.pyplot as plt
import numpy as np
import sympy as sp

from utilsplot import dis_rejctn_plot, input_perfect_const_plot, input_acceptable_const_plot
from utils import distRHPZ, tf, mimotf

s = tf([1, 0])

G11 = (s - 1) / (s + 2)
G12 = 4 / (s + 2)
G21 = 4.5 / (s + 2)
G22 = 2 * (s - 1) / (s + 2)

G = mimotf([[G11, G12], [G21, G22]])


def Gdk(s, k):
    return 6 / (s + 2) * np.matrix([[k], [1]])


def Gd(s):
    k = 1
    return Gdk(s, k)


def Gdz(s):
    k = sp.Symbol('k')
    return Gdk(s, k)

コード例 #20
0
import numpy as np
import matplotlib.pyplot as plt

from utilsplot import step_response_plot
from utils import feedback, tf


s = tf([1, 0], 1)

Kd = 0.5

G = 5/((10*s + 1)*(s - 1))
Gd = Kd/((s + 1)*(0.2*s + 1))
K = 0.04/s * ((10*s + 1)**2)/((0.1*s + 1)**2)

L = G * K

# Transfer function between disturbance and output y
S = feedback(1, L)*Gd

# Transfer function between disturbance and controller input u
Gu = -S*K
    
plt.figure('Figure 5.16 (a)')

plt.subplot(1, 2, 1)
w = np.logspace(-2, 1, 1000)
wi = w*1j 
plt.loglog(w, np.abs(G(wi)))
plt.loglog(w, np.abs(Gd(wi)))
plt.axhline(1, color='black', linestyle=':')
コード例 #21
0
def add_deadtime_SISO(G, deadtime):
    G = (tf(list(G.numerator.coeffs),
            list(G.denominator.coeffs), deadtime=deadtime))
    return G
コード例 #22
0
ファイル: multi.py プロジェクト: dt1483/SnFFT
    split_transform('/local/hopan/cube/split/xaa.pkl', irrep_dict, (0, 1, 7),
                    ((), (1, ), (4, 3)))
    end = time.time()
    print('Elapsed: {:.2f}'.format(end - start))


if __name__ == '__main__':
    #test_partial()
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('--pkldir',
                        type=str,
                        default='/local/hopan/cube/pickles')
    parser.add_argument('--splitdir',
                        type=str,
                        default='/local/hopan/cube/split_unmod')
    parser.add_argument('--savedir',
                        type=str,
                        default='/local/hopan/cube/fourier')
    parser.add_argument('--alpha', type=str, default='(8, 0, 0)')
    parser.add_argument('--parts', type=str, default='((7,1),(),())')
    parser.add_argument('--par',
                        type=int,
                        default=1,
                        help='Amount of parallelism')
    parser.add_argument('--suffix',
                        type=str,
                        default='split',
                        help='special suffix for split files')
    args = parser.parse_args()
    tf(main, [args])
コード例 #23
0
# -*- coding: utf-8 -*-
"""
Created on Tue May 26 21:05:19 2015

@author: cronjej
"""

import utils
import utilsplot
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['figure.facecolor'] = 'white'

s = utils.tf([1, 0], 1)
A = np.asarray([[-10, 0], [0, -1]])
B = np.eye(A.shape[0])
C = np.asarray([[10., 1.1], [10., 0]])
D = np.asarray([[0., 0.], [0., 1.]])

G = utils.mimotf(C * utils.mimotf(s * np.eye(2) - A).inverse() * B + D)

# a) Controllability analysis

G_poles = G.poles()
G_rhp_poles = utils.RHPonly(G_poles)
# G has stable poles, -10 with multiplicity of 2 and -1
G_zeros = G.zeros()
G_rhp_zeros = utils.RHPonly(G_zeros)
# G has a LHP zero @ -10 and a RHP zero @ 0.1
コード例 #24
0
"""
Created on Tue May 26 21:05:19 2015

@author: cronjej
"""

import utils
import utilsplot
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['figure.facecolor'] = 'white'


s = utils.tf([1, 0], 1)
A = np.asarray([[-10, 0], [0, -1]])
B = np.eye(A.shape[0])
C = np.asarray([[10., 1.1], [10., 0]])
D = np.asarray([[0., 0.], [0., 1.]])

G = utils.mimotf(C*utils.mimotf(s*np.eye(2) - A).inverse()*B + D)

# a) Controllability analysis

G_poles = G.poles()
G_rhp_poles = utils.RHPonly(G_poles)
# G has stable poles, -10 with multiplicity of 2 and -1
G_zeros = G.zeros()
G_rhp_zeros = utils.RHPonly(G_zeros)
# G has a LHP zero @ -10 and a RHP zero @ 0.1
コード例 #25
0
import matplotlib.pyplot as plt
from utils import phase, tf, feedback

# Loop shaping is an iterative procedure where the designer
# 1. shapes and reshapes |L(jw)| after computing PM and GM,
# 2. the peaks of closed loop frequency responses (Mt and Ms),
# 3. selected closed-loop time responses,
# 4. the magnitude of the input signal
#
# 1 to 4 are the important frequency domain measures used to assess
#   perfomance and characterise speed of response

w = np.logspace(-2, 1, 1000)
wi = w*1j

s = tf([1, 0])

Kc = 0.05
#plant model
G = 3*(-2*s+1)/((10*s+1)*(5*s+1))
#Controller model
K = Kc*(10*s+1)*(5*s+1)/(s*(2*s+1)*(0.33*s+1))
#closed-loop transfer function
L = G*K

#magnitude and phase
plt.subplot(2, 1, 1)
plt.loglog(w, abs(L(wi)))
plt.loglog(w, np.ones_like(w))
plt.ylabel('Magnitude')
コード例 #26
0
import utils
import numpy as np
import sympy as sp
from scipy.optimize import fsolve

s = utils.tf([1,0],[1])
L = (-s + 2)/(s*(s + 2))

_,_,_,w180 = utils.margins(L)
GM = 1/np.abs(L(1j*w180))
print('w_180',w180)
print('GM = 1/|L(w180)|',GM)
print('From 7.55, kmax = GM = ',GM)

omega = np.logspace(-2,2,1000)

def rk(kmax):
    return (kmax - 1)/(kmax + 1)

def kbar(kmax):
    return (kmax + 1)/2

def RScondition(kmax):
    abs_ineq = [abs(rk(kmax) * kbar(kmax) * L(1j*s)/(1 + kbar(kmax)*L(1j*s))) for s in omega]
    max_ineq = max(abs_ineq) - 1
    return max_ineq

kcal = fsolve(RScondition,1)
print('From 7.58, kmax = ',np.round(kcal,2))
コード例 #27
0
import matplotlib.pyplot as plt

from utils import tf
from utilsplot import bode

G = tf([5], [10, 1], deadtime=2)

plt.figure('Figure 2.2')
plt.title('Bode plots of G(s)')
bode(G, -3, 1)
plt.show()
コード例 #28
0
import numpy as np
import matplotlib.pyplot as plt
from utils import tf

WP = tf([0.25, 0.1], [1, 0])
WU = tf([1, 0], [1, 1])
GK1 = tf([0, 0.5], [1, 0])
GK2 = tf([0, -0.5, 0.5], [1, 1, 0])

w = np.logspace(-2, 2, 500)
s = 1j*w
ru = np.linspace(0, 1, 10)

"""Solution to part a,b is on pg 284, only part c is illustrateed here"""
plt.loglog(w, np.abs(1/(1 + GK1(s))), label='$S_1$')
plt.loglog(w, np.abs(1/(1 + GK2(s))), label='$S_2$')

for i in range(np.size(ru)):
    if i == 1:
        plt.loglog(w, 1/(np.abs(WP(s)) + np.abs(ru[i]*WU(s))), 'k--', label='varying $r_u$')
    else:
        plt.loglog(w, 1/(np.abs(WP(s)) + np.abs(ru[i]*WU(s))), 'k--')

plt.loglog(w, 1/(np.abs(WP(s)) + np.abs(0.75*WU(s))), 'r', label='$r_u$ = 0.75')
plt.legend(loc=2)
plt.title(r'Figure 7.19')
plt.xlabel(r'Frequency [rad/s]', fontsize=14)
plt.ylabel(r'Magnitude', fontsize=15)
plt.show()