<commit_before>from __future__ import division

import numpy as np
import logging

logger = logging.getLogger(__name__)

class DataIterator(object):
    def __init__(self, seed, data_dim, latent_dim):
        np.random.seed(seed)
        self.data_dim = data_dim
        self.latent_dim = latent_dim

    @staticmethod
    def adjust_data_size(data, batch_size, fill_to=None):
        if isinstance(data, dict):
            # interpret the data as dict with keys `data` and `target` and apply augmentation operations equally on both
            data_size = data['data'].shape[0]
        else:
            data_size = data.shape[0]
        if fill_to is not None:
            additional_samples = max(fill_to - data_size, 0)
            data_size = fill_to
        else:
            additional_samples = 0
        n_batches = data_size / batch_size

        if n_batches - int(n_batches) > 0:
            additional_samples += int(np.ceil(n_batches) * batch_size - data_size)
            logger.warning("The dataset cannot be iterated in epochs completely with the current batch size "
                           "and it will be automatically augmented with {} more samples.".format(additional_samples))
            additional_samples_indices = np.random.choice(data_size, size=additional_samples)
            if isinstance(data, dict):
                altered_data = {'data': np.concatenate([data['data'],
                                                        data['data'][additional_samples_indices]], axis=0),
                                'target': np.concatenate([data['target'],
                                                          data['target'][additional_samples_indices]], axis=0)}
            else:
                altered_data = np.concatenate([data, data[additional_samples_indices]], axis=0)
        else:
            altered_data = data
        n_batches = altered_data['data'].shape[0] // batch_size
        return altered_data, n_batches

    def iter_data_inference(self, data, n_batches, **kwargs):
        raise NotImplementedError

    def iter_data_generation(self, data, n_batches,  **kwargs):
        raise NotImplementedError

    def iter_data_training(self, data, n_batches,  **kwargs):
        raise NotImplementedError

    def iter(self, data, batch_size, mode='training', **kwargs):
        if isinstance(data, tuple):
            logger.debug("Iterating over {} datasets.".format(len(data)))
            altered_data, n_batches = [], []
            max_data_size = max([len(d['data']) for d in data])
            for dataset in data:
                altered_i, n_batches_i = self.adjust_data_size(dataset, batch_size=batch_size, fill_to=max_data_size)
                altered_data.append(altered_i)
                n_batches.append(n_batches_i)
            altered_data = tuple(altered_data)
            n_batches = min(n_batches)
        else:
            altered_data, n_batches = self.adjust_data_size(data, batch_size=batch_size)
        iterator = getattr(self, 'iter_data_{}'.format(mode))
        return iterator(altered_data, n_batches, **kwargs), n_batches

class AVBDataIterator(DataIterator):
    def __init__(self, data_dim, latent_dim, seed=7):
        super(AVBDataIterator, self).__init__(seed=seed, data_dim=data_dim, latent_dim=latent_dim)

    def iter_data_training(self, data, n_batches, **kwargs):
        shuffle = kwargs.get('shuffle', True)
        data_size = data.shape[0]
        while True:
            indices_new_order = np.arange(data_size)
            if shuffle:
                np.random.shuffle(indices_new_order)
            batches_indices = np.split(indices_new_order, n_batches)
            # run for 1 epoch
            for batch_indices in batches_indices:
                yield data[batch_indices].astype(np.float32)

    def iter_data_inference(self, data, n_batches, **kwargs):
        data_size = data.shape[0]
        while True:
            for batch_indices in np.split(np.arange(data_size), n_batches):
                yield data[batch_indices].astype(np.float32)

    def iter_data_generation(self, data, n_batches, **kwargs):
        data_size = data.shape[0]
        while True:
            for batch_indices in np.split(np.arange(data_size), n_batches):
                yield data[batch_indices].astype(np.float32)

VAEDataIterator = AVBDataIterator

class ConjointVAEDataIterator(DataIterator):
    def __init__(self, data_dim, latent_dim, seed=7):
        super(ConjointVAEDataIterator, self).__init__(seed=seed, data_dim=data_dim, latent_dim=latent_dim)

    def iter_data_training(self, data, n_batches, **kwargs):
        group_by_target = kwargs.get('group_by_target', True)

        unique_labels = np.unique(data[0]['target'])
        if not (all([len(d['data']) == len(d['target']) for d in data])
                and all([len(d['data']) == len(data[0]['data']) for d in data[1:]])
                and np.all([np.unique(d['target']) == unique_labels for d in data[1:]])):
            raise ValueError("Datasets are not equal in size or not well formed or not homogeneous in labels.")

        data_size = len(data[0]['data'])
        batch_size = data_size // n_batches

        if group_by_target:
            indices_groups = []
            for d in data:
                # group the targets of each dataset by label
                sorted_indices = np.argsort(d['target'])
                sorted_labels = d['target'][sorted_indices]
                first_uniques = np.concatenate(([True], sorted_labels[1:] != sorted_labels[:-1]))
                count_uniques = np.diff(np.nonzero(first_uniques)[0])
                indices_groups.append(np.split(sorted_indices, np.cumsum(count_uniques)))
            while True:
                for _ in range(n_batches):
                    # sample batch_size many samples from each dataset such that the labels are the same
                    random_groups = np.random.randint(low=0, high=unique_labels.size, size=batch_size)
                    sample_ids = np.squeeze([[np.random.choice(dataset_groups[random_group_id], size=1)
                                             for dataset_groups in indices_groups]
                                             for random_group_id in random_groups]).T
                    batch = [data[i]['data'][ids].astype(np.float32) for i, ids in enumerate(sample_ids)]
                    yield batch
        else:
            raise NotImplementedError

    def iter_data_inference(self, data, n_batches, **kwargs):
        return self.iter_data_training(data, n_batches, **kwargs)

    def iter_data_generation(self, data, n_batches, **kwargs):
        raise NotImplementedError
<commit_msg>fix iterator for inference<commit_after>from __future__ import division

import numpy as np
import logging

logger = logging.getLogger(__name__)

class DataIterator(object):
    def __init__(self, seed, data_dim, latent_dim):
        np.random.seed(seed)
        self.data_dim = data_dim
        self.latent_dim = latent_dim

    @staticmethod
    def adjust_data_size(data, batch_size, fill_to=None):
        if isinstance(data, dict):
            # interpret the data as dict with keys `data` and `target` and apply augmentation operations equally on both
            data_size = data['data'].shape[0]
        else:
            data_size = data.shape[0]
        if fill_to is not None:
            additional_samples = max(fill_to - data_size, 0)
            data_size = fill_to
        else:
            additional_samples = 0
        n_batches = data_size / batch_size

        if n_batches - int(n_batches) > 0:
            additional_samples += int(np.ceil(n_batches) * batch_size - data_size)
            logger.warning("The dataset cannot be iterated in epochs completely with the current batch size "
                           "and it will be automatically augmented with {} more samples.".format(additional_samples))
            additional_samples_indices = np.random.choice(data_size, size=additional_samples)
            if isinstance(data, dict):
                altered_data = {'data': np.concatenate([data['data'],
                                                        data['data'][additional_samples_indices]], axis=0),
                                'target': np.concatenate([data['target'],
                                                          data['target'][additional_samples_indices]], axis=0)}
            else:
                altered_data = np.concatenate([data, data[additional_samples_indices]], axis=0)
        else:
            altered_data = data
        n_batches = altered_data['data'].shape[0] // batch_size
        return altered_data, n_batches

    def iter_data_inference(self, data, n_batches, **kwargs):
        raise NotImplementedError

    def iter_data_generation(self, data, n_batches,  **kwargs):
        raise NotImplementedError

    def iter_data_training(self, data, n_batches,  **kwargs):
        raise NotImplementedError

    def iter(self, data, batch_size, mode='training', **kwargs):
        if isinstance(data, tuple):
            logger.debug("Iterating over {} datasets.".format(len(data)))
            altered_data, n_batches = [], []
            max_data_size = max([len(d['data']) for d in data])
            for dataset in data:
                altered_i, n_batches_i = self.adjust_data_size(dataset, batch_size=batch_size, fill_to=max_data_size)
                altered_data.append(altered_i)
                n_batches.append(n_batches_i)
            altered_data = tuple(altered_data)
            n_batches = min(n_batches)
        else:
            altered_data, n_batches = self.adjust_data_size(data, batch_size=batch_size)
        iterator = getattr(self, 'iter_data_{}'.format(mode))
        return iterator(altered_data, n_batches, **kwargs), n_batches

class AVBDataIterator(DataIterator):
    def __init__(self, data_dim, latent_dim, seed=7):
        super(AVBDataIterator, self).__init__(seed=seed, data_dim=data_dim, latent_dim=latent_dim)

    def iter_data_training(self, data, n_batches, **kwargs):
        shuffle = kwargs.get('shuffle', True)
        data_size = data.shape[0]
        while True:
            indices_new_order = np.arange(data_size)
            if shuffle:
                np.random.shuffle(indices_new_order)
            batches_indices = np.split(indices_new_order, n_batches)
            # run for 1 epoch
            for batch_indices in batches_indices:
                yield data[batch_indices].astype(np.float32)

    def iter_data_inference(self, data, n_batches, **kwargs):
        data_size = data.shape[0]
        while True:
            for batch_indices in np.split(np.arange(data_size), n_batches):
                yield data[batch_indices].astype(np.float32)

    def iter_data_generation(self, data, n_batches, **kwargs):
        data_size = data.shape[0]
        while True:
            for batch_indices in np.split(np.arange(data_size), n_batches):
                yield data[batch_indices].astype(np.float32)

VAEDataIterator = AVBDataIterator

class ConjointVAEDataIterator(DataIterator):
    def __init__(self, data_dim, latent_dim, seed=7):
        super(ConjointVAEDataIterator, self).__init__(seed=seed, data_dim=data_dim, latent_dim=latent_dim)

    def iter_data_training(self, data, n_batches, **kwargs):
        group_by_target = kwargs.get('group_by_target', True)
        shuffle = kwargs.get('shuffle', True)
        unique_labels = np.unique(data[0]['target'])
        if not (all([len(d['data']) == len(d['target']) for d in data])
                and all([len(d['data']) == len(data[0]['data']) for d in data[1:]])
                and np.all([np.unique(d['target']) == unique_labels for d in data[1:]])):
            raise ValueError("Datasets are not equal in size or not well formed or not homogeneous in labels.")

        data_size = len(data[0]['data'])
        batch_size = data_size // n_batches

        if group_by_target:
            indices_groups = []
            for d in data:
                # group the targets of each dataset by label
                sorted_indices = np.argsort(d['target'])
                sorted_labels = d['target'][sorted_indices]
                first_uniques = np.concatenate(([True], sorted_labels[1:] != sorted_labels[:-1]))
                count_uniques = np.diff(np.nonzero(first_uniques)[0])
                indices_groups.append(np.split(sorted_indices, np.cumsum(count_uniques)))
            while True:
                for i in range(n_batches):
                    if shuffle:
                        # sample batch_size many samples from each dataset such that the labels are the same
                        random_groups = np.random.randint(low=0, high=unique_labels.size, size=batch_size)
                        sample_ids = np.squeeze([[np.random.choice(dataset_groups[random_group_id], size=1)
                                                 for dataset_groups in indices_groups]
                                                 for random_group_id in random_groups]).T
                    else:
                        sample_ids = np.squeeze([np.random.choice(dataset_groups[i % len(unique_labels)],
                                                                  size=batch_size)
                                                 for dataset_groups in indices_groups])
                    batch = [data[i]['data'][ids].astype(np.float32) for i, ids in enumerate(sample_ids)]
                    yield batch
        else:
            raise NotImplementedError

    def iter_data_inference(self, data, n_batches, **kwargs):
        kwargs['shuffle'] = False
        return self.iter_data_training(data, n_batches, **kwargs)

    def iter_data_generation(self, data, n_batches, **kwargs):
        raise NotImplementedError
<|endoftext|>


<commit_before>#!/usr/bin/pyhton

import csv
import time
import numpy as np
from sklearn import svm
from sklearn import tree
from sklearn import neighbors
from sklearn import preprocessing
from sklearn import multiclass
from sklearn import cross_validation
from sklearn import metrics
from sklearn import ensemble
from sklearn.pipeline import Pipeline
from math import sqrt
import argparse
import exceptions

# Stores the parsed command line args
args = None

# use labelEncoder.transform() and .inverse_transform() to convert from
# letters to numbers and vice versa.
# The label encoder will be instantiated during data parsing.
labelEncoder = None

# This label will be applied by the RadiusNeighborsClassifier to detect outliers
# in the data set.
OUTLIER_LABEL = '?'

def _parse_csv(filename, typeCast=int):
    '''
    Parse data from CSV and return (y,X) with y a array of labels and X array
    of observation vectors. X entries are casted using typeCast.
    Global variable labelEncoder will be instantiated
    '''
    data = csv.reader(open(filename))
    X = []
    y = []
    for row in data:
        y.append(row[0])
        X.append([typeCast(r) for r in row[1:len(row)]])

    # sklearn classifiers expect classes to be integers.
    # use preprocessing to encode the labels accordingly
    global labelEncoder
    labelEncoder = preprocessing.LabelEncoder()
    # Add an extra outlier label '?' to the list of labels so that methods like
    # RadiusNeighborsClassifier that detect outliers can give them a
    # indistinguishable label.
    labelEncoder.fit(y + [OUTLIER_LABEL]);
    y = labelEncoder.transform(y)

    return (y, X)

def __print_confusion_matrix(cm, labels):
    '''
    Print the confusion matrix in a nice way
    This will leave out 0 entries and label the columns and rows
    '''
    print ' ',
    for label in labels:
        print '   %s' % label,

    for i in range(cm.size):
        if i % len(labels) == 0:
            print ''
            print labels[i / len(labels)],
        if not cm.item(i) == 0:
            print '%4d' % cm.item(i),
        else:
            print 4 * ' ',

def _evaluate_calssifier(clf, trainingSet, validationSet):
    '''
    Trains the given classifier clf with the training set and compares the
    predicted results with the validation set.
    Returns the trained classifier
    '''
    (tLabels, tVectors) = trainingSet
    (vLabels, vVectors) = validationSet

    t = time.time()
    clf.fit(tVectors, tLabels)
    trainingTime = time.time() - t

    t = time.time()
    pLabels = clf.predict(vVectors)
    validationTime = time.time() - t

    score = clf.score(vVectors, vLabels)

    if (args.verbose > 1):
        print '        Training %d elements in %d Seconds, Prediction in %d ' \
              'Seconds' % (len(tVectors), trainingTime, validationTime)

    if (args.verbose > 2):
        errCount = sum([1 for (p, v) in zip(pLabels, vLabels) if p != v])
        print '        %s errors out of %s validation vectors' % (errCount,
                                                                  len(
                                                                      vVectors))
        label_names = labelEncoder.classes_
        print '        Classification Report:'
        print metrics.classification_report(vLabels, pLabels,
                                            target_names=label_names)
        print '        Confusion Matrix:'
        cm = metrics.confusion_matrix(vLabels, pLabels,
                                      labelEncoder.transform(label_names))
        __print_confusion_matrix(cm, label_names)
    elif (args.verbose > 1):
        print '        %.2f%% Accuracy' % (score * 100)

    return (score, trainingTime)

def _evaluate_classifiers(classifiers, datasets):
    '''
    classifiers is expected to be a dictionary of classifiers and sets a dict
    of (training,validation) data sets, each made up of (Labels, Vectors)
    '''
    quality = {}
    for setKey in datasets.keys():
        if (args.verbose > 1):
            print 'Using Dataset %s :' % setKey
        (trainingSet, validationSet) = datasets[setKey]
        for clfKey in classifiers.keys():
            if (args.verbose > 1):
                print '    %s :' % clfKey
            res = _evaluate_calssifier(classifiers[clfKey],
                                       trainingSet,
                                       validationSet)
            quality[clfKey] = res
            if (args.verbose > 1):
                print ' '
    return quality

def __get_as_int_or_float(num):
    # TODO Document
    try:
        if int(num) > 1:
            return int(num)
    except exceptions.ValueError:
        return float(num)

def _prepare_data_set(trainingSetSize=16000, test_size=None, type=int,
                      scale=False, featureSelection=None):
    '''
    Load training and validation data in desired format
    '''

    train_size = __get_as_int_or_float(trainingSetSize)
    if not test_size is None:
        test_size = __get_as_int_or_float(test_size)

    (labels, vectors) = _parse_csv('res/letter-recognition.data', type)

    if (scale):
        vectors = preprocessing.scale(np.array(vectors))

    x_train, x_test, y_train, y_test = cross_validation.train_test_split(
        vectors, labels, test_size=test_size, train_size=train_size)
    return ((y_train, x_train), (y_test, x_test))

def _prepare_classifiers(cmd_class=['all']):
    classifiers = {}
    select = args.select_features
    has_all = 'all' in cmd_class
    if has_all or 'svm' in cmd_class:
        # see http://scikit-learn.org/stable/modules/svm.html#classification
        kernels = args.svm_kernels
        if 'all' in kernels:
            kernels = ['linear-svc', 'linear-ovr', 'linear', 'poly', 'rbf',
                       'sigmoid']
        for k in kernels:
            if k == 'linear-ovr':
                classifiers[
                    'SVC kernel=linear OvR'] = multiclass.OneVsRestClassifier(
                    svm.SVC(kernel='linear'))
            elif k == 'linear-svc':
                classifiers['Linear SVC'] = svm.LinearSVC()
            else:
                g = 0.0
                if k == 'sigmoid':
                    # TODO: Document this magic number
                    # Maximum dot product of the vectors in our data set
                    g = 1.0 / 962.0
                classifiers['SVC kernel=%s' % k] = svm.SVC(kernel=k, gamma=g)
    if has_all or 'tree' in cmd_class:
        # see http://scikit-learn.org/stable/modules/tree.html
        if select == 'off' or select == 'both':
            classifiers['Default Decision Trees'] = tree.DecisionTreeClassifier()
        if select == 'on' or select == 'both':
            classifiers['Default Decision Trees Feature selection Pipeline'] = \
            Pipeline([('selection', ensemble.ExtraTreesClassifier()),
                             ('classification',tree.DecisionTreeClassifier())])

        for maxf in frange(0.85, 0.90, 0.024):
            for c in ['entropy', 'gini']:
                clf = tree.DecisionTreeClassifier(max_features=maxf, criterion=c)

                if select == 'on' or select == 'both':
                    pipe = Pipeline([('selection', ensemble
                    .ExtraTreesClassifier()),
                                     ('classification', clf)])
                    classifiers['Max Features %.2f with %s Decision Trees ' \
                                'Feature selection Pipeline' % (
                                    maxf, c)] = pipe
                if select == 'off' or select == 'both':
                    classifiers['Max Features %.2f with %s Decision Trees' %
                                (maxf, c)] = clf

    if has_all or 'random' in cmd_class:
        for i in range(10):
            classifiers['Random Tree #%d' % i] = tree \
                .ExtraTreeClassifier()

    if has_all or 'ensemble' in cmd_class:
        min_trees = args.min_trees
        max_trees = args.max_trees
        step = 0
        divisor = 10

        while step < 1:
            step = int((max_trees - min_trees)/divisor)
            divisor -= 1

        for trees in range(min_trees, max_trees+1, step):
            clf1 = ensemble.RandomForestClassifier(bootstrap=False,
                                                   n_estimators=trees)
            clf2 = ensemble.ExtraTreesClassifier(bootstrap=False,
                                                 n_estimators=trees)

            if select == 'on' or select == 'both':
                pipe1 = Pipeline([('selection', ensemble
                .ExtraTreesClassifier()),
                                  ('classification', clf1)])
                pipe2 = Pipeline([('selection', ensemble
                .ExtraTreesClassifier()),
                                  ('classification', clf2)])
                classifiers['%d Random Forest ' \
                            'Feature selection Pipeline' % trees] = pipe1
                classifiers['%d Extra Random Trees ' \
                            'Feature selection Pipeline' % trees] = pipe2

            if select == 'off' or select == 'both':
                classifiers['%d Random Forest' % trees] = clf1
                classifiers['%d Extra Random Trees' % trees] = clf2

    if has_all or 'kNN' in cmd_class or 'rNN' in cmd_class:
        # see http://scikit-learn.org/stable/auto_examples/neighbors/plot_classification.html#example-neighbors-plot-classification-py
        selected_weights = args.NN_weights
	if 'all' in args.NN_weights:
            selected_weights = ['uniform', 'distance']
	for weight in selected_weights:
            if 'kNN' in cmd_class:
                show_knn_warnings = args.verbose > 3
                for k in args.kNN_neighbors:
                    classifiers['kNN %s k=%d' % (weight, int(k))] = \
                        neighbors.KNeighborsClassifier(k, weights = weight,
                            warn_on_equidistant=show_knn_warnings)
            if 'rNN' in cmd_class:
                # XXX: Buggy scikit does not handle the distance weight
                #
                # The following error message is being thrown by scikit when
                # the distance weight is being used. This could not be fixed so
                # it is being ignored instead.
                #
                # Traceback (most recent call last):
                #   File "lird.py", line 329, in <module>
                #     main()
                #   File "lird.py", line 316, in main
                #     quality = _evaluate_classifiers(classifiers, datasets)
                #   File "lird.py", line 130, in _evaluate_classifiers
                #     validationSet)
                #   File "lird.py", line 90, in _evaluate_calssifier
                #     pLabels = clf.predict(vVectors)
                #   File "/usr/lib/pymodules/python2.7/sklearn/neighbors/classification.py", line 307, in predict
                #     for (pl, w) in zip(pred_labels[inliers], weights)],
                #   File "/usr/lib/pymodules/python2.7/sklearn/utils/extmath.py", line 305, in weighted_mode
                #     w = np.zeros(a.shape, dtype=w.dtype) + w
                # ValueError: operands could not be broadcast together with shapes (47) (194)
                if weight == 'distance':
                    continue

                # Assign the outlier class to outliers.
                outlier_class = labelEncoder.transform([OUTLIER_LABEL])
                for r in args.rNN_radius:
                    classifiers['rNN %s r=%f' % (weight, float(r))] = \
                        neighbors.RadiusNeighborsClassifier(radius = r,
                            weights = weight, outlier_label = outlier_class)

    # Disabled because of horrible performance
    # classifiers['NuSVC'] = svm.NuSVC()
    return classifiers

def _prepare_data_sets(train_size, test_size, sets):
    '''
    Loads and names all desired datasets into a dirctionary
    TODO proper cross validation see
    http://scikit-learn.org/stable/modules/cross_validation.html#cross-validation
    '''
    datasets = {}
    if 'all' in sets or 'orig' in sets:
        datasets['Out of the Box Integer Data'] = \
            _prepare_data_set(train_size, test_size)
    if 'all' in sets or 'scaled' in sets:
        datasets['Normalizerd Float Data'] = \
            _prepare_data_set(train_size, test_size, float, True)

    return datasets

def _write_dot_file(dtClf, filename='dtClf.dot'):
    '''
    Creates dot file from Decision Tree classifier
    Use graphviz dot tool to create decision tree via
    $ dot -Tpdf out/dtCls.dot -o out/dtCls.pdf
    for more info see http://graphviz.org/
    '''
    try:
        import pydot

        f = open('out/' + filename, 'w')
        tree.export_graphviz(dtClf, out_file=f)
        print 'Written %s file' % ('out/' + filename)
        return
    except ImportError:
        return

def _parse_args():
    '''
    Parse the command line arguments to select test at runtime
    '''
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', type=int, action='store', default=1,
                        help='verbose level, default = 1', choices=range(1, 5))
    parser.add_argument('--select-features', action='store',
                        default='on',
                        help='dis/enable feature selection before '
                             'training', choices=['on', 'off', 'both'])
    parser.add_argument('--min-trees', action='store',default=10,type=int,
                        help='minimum number of trees used in ensemble '
                             'methods')
    parser.add_argument('--max-trees', action='store',default=50,type=int,
                        help='maximum number of trees used in ensemble '
                             'methods')
    parser.add_argument('--train-size', default=16000, action='store',
                        help='amount of data used for training. Can be either \
                              an int representing the absolute number of \
                              samples or a float between 0 and 1.0 \
                              representing the ratio of train samples')
    parser.add_argument('--test-size', default=None, action='store',
                        help='amount of data used for testing, if not \
                              specified the rest of the data set will be used \
                              Like the train-size this can either be a int or \
                              a float')
    parser.add_argument('--data', action='store', default=['all'],
                        choices=['all', 'orig', 'scaled'])
    parser.add_argument('--svm-kernels', action='store', default=['all'],
                        choices=['all', 'rbf', 'linear', 'linear-ovr', 'poly',
                                 'sigmoid'],
                        nargs='*',
                        help='select the kernels that should be trained for the\
                              SVM. by default all will be trained')
    parser.add_argument('classifiers', nargs='*', default='all',
                        choices=['all', 'svm', 'kNN', 'rNN', 'tree', 'random',
                                 'ensemble'])
    parser.add_argument('--NN-weights', action='store', default=['all'],
                        choices=['all', 'uniform', 'distance'], nargs='*')
    parser.add_argument('--kNN-neighbors', action='store', default=[5],
                        nargs='*', type=int)
    parser.add_argument('--rNN-radius', action='store', default=[3.5], \
                        nargs='*', type=float)
    global args
    args = parser.parse_args()

def frange(start, stop, step):
    '''
    helper function to get a range generator for float values.
    '''
    while start < stop:
        yield start
        start += step

def main():
    secs = time.time()
    _parse_args()
    datasets = _prepare_data_sets(args.train_size, args.test_size, args.data)
    classifiers = _prepare_classifiers(args.classifiers)
    quality = _evaluate_classifiers(classifiers, datasets)

    # if 'all' in args.classifiers or 'tree' in args.classifiers:
    # _write_dot_file(classifiers['Decision Tree'])

    #rank classifiers by score and print highscore list
    for clf, (score, trainingTime) in sorted(quality.iteritems(), key=lambda
            (k, v): (v[0], k)):
        print "%.2f %% in %d secs: %s" % (100 * score, trainingTime,clf)

    secs = time.time() - secs;
    print 'Total Time: %d seconds' % secs

    return

if __name__ == "__main__":
    main()
<commit_msg>style(lird.py): Whitespace fixes<commit_after>#!/usr/bin/pyhton

import csv
import time
import numpy as np
from sklearn import svm
from sklearn import tree
from sklearn import neighbors
from sklearn import preprocessing
from sklearn import multiclass
from sklearn import cross_validation
from sklearn import metrics
from sklearn import ensemble
from sklearn.pipeline import Pipeline
from math import sqrt
import argparse
import exceptions

# Stores the parsed command line args
args = None

# use labelEncoder.transform() and .inverse_transform() to convert from
# letters to numbers and vice versa.
# The label encoder will be instantiated during data parsing.
labelEncoder = None

# This label will be applied by the RadiusNeighborsClassifier to detect outliers
# in the data set.
OUTLIER_LABEL = '?'

def _parse_csv(filename, typeCast=int):
    '''
    Parse data from CSV and return (y,X) with y a array of labels and X array
    of observation vectors. X entries are casted using typeCast.
    Global variable labelEncoder will be instantiated
    '''
    data = csv.reader(open(filename))
    X = []
    y = []
    for row in data:
        y.append(row[0])
        X.append([typeCast(r) for r in row[1:len(row)]])

    # sklearn classifiers expect classes to be integers.
    # use preprocessing to encode the labels accordingly
    global labelEncoder
    labelEncoder = preprocessing.LabelEncoder()
    # Add an extra outlier label '?' to the list of labels so that methods like
    # RadiusNeighborsClassifier that detect outliers can give them a
    # indistinguishable label.
    labelEncoder.fit(y + [OUTLIER_LABEL]);
    y = labelEncoder.transform(y)

    return (y, X)

def __print_confusion_matrix(cm, labels):
    '''
    Print the confusion matrix in a nice way
    This will leave out 0 entries and label the columns and rows
    '''
    print ' ',
    for label in labels:
        print '   %s' % label,

    for i in range(cm.size):
        if i % len(labels) == 0:
            print ''
            print labels[i / len(labels)],
        if not cm.item(i) == 0:
            print '%4d' % cm.item(i),
        else:
            print 4 * ' ',

def _evaluate_calssifier(clf, trainingSet, validationSet):
    '''
    Trains the given classifier clf with the training set and compares the
    predicted results with the validation set.
    Returns the trained classifier
    '''
    (tLabels, tVectors) = trainingSet
    (vLabels, vVectors) = validationSet

    t = time.time()
    clf.fit(tVectors, tLabels)
    trainingTime = time.time() - t

    t = time.time()
    pLabels = clf.predict(vVectors)
    validationTime = time.time() - t

    score = clf.score(vVectors, vLabels)

    if (args.verbose > 1):
        print '        Training %d elements in %d Seconds, Prediction in %d ' \
              'Seconds' % (len(tVectors), trainingTime, validationTime)

    if (args.verbose > 2):
        errCount = sum([1 for (p, v) in zip(pLabels, vLabels) if p != v])
        print '        %s errors out of %s validation vectors' % (errCount,
                                                                  len(
                                                                      vVectors))
        label_names = labelEncoder.classes_
        print '        Classification Report:'
        print metrics.classification_report(vLabels, pLabels,
                                            target_names=label_names)
        print '        Confusion Matrix:'
        cm = metrics.confusion_matrix(vLabels, pLabels,
                                      labelEncoder.transform(label_names))
        __print_confusion_matrix(cm, label_names)
    elif (args.verbose > 1):
        print '        %.2f%% Accuracy' % (score * 100)

    return (score, trainingTime)

def _evaluate_classifiers(classifiers, datasets):
    '''
    classifiers is expected to be a dictionary of classifiers and sets a dict
    of (training,validation) data sets, each made up of (Labels, Vectors)
    '''
    quality = {}
    for setKey in datasets.keys():
        if (args.verbose > 1):
            print 'Using Dataset %s :' % setKey
        (trainingSet, validationSet) = datasets[setKey]
        for clfKey in classifiers.keys():
            if (args.verbose > 1):
                print '    %s :' % clfKey
            res = _evaluate_calssifier(classifiers[clfKey],
                                       trainingSet,
                                       validationSet)
            quality[clfKey] = res
            if (args.verbose > 1):
                print ' '
    return quality

def __get_as_int_or_float(num):
    # TODO Document
    try:
        if int(num) > 1:
            return int(num)
    except exceptions.ValueError:
        return float(num)

def _prepare_data_set(trainingSetSize=16000, test_size=None, type=int,
                      scale=False, featureSelection=None):
    '''
    Load training and validation data in desired format
    '''

    train_size = __get_as_int_or_float(trainingSetSize)
    if not test_size is None:
        test_size = __get_as_int_or_float(test_size)

    (labels, vectors) = _parse_csv('res/letter-recognition.data', type)

    if (scale):
        vectors = preprocessing.scale(np.array(vectors))

    x_train, x_test, y_train, y_test = cross_validation.train_test_split(
        vectors, labels, test_size=test_size, train_size=train_size)
    return ((y_train, x_train), (y_test, x_test))

def _prepare_classifiers(cmd_class=['all']):
    classifiers = {}
    select = args.select_features
    has_all = 'all' in cmd_class
    if has_all or 'svm' in cmd_class:
        # see http://scikit-learn.org/stable/modules/svm.html#classification
        kernels = args.svm_kernels
        if 'all' in kernels:
            kernels = ['linear-svc', 'linear-ovr', 'linear', 'poly', 'rbf',
                       'sigmoid']
        for k in kernels:
            if k == 'linear-ovr':
                classifiers[
                    'SVC kernel=linear OvR'] = multiclass.OneVsRestClassifier(
                    svm.SVC(kernel='linear'))
            elif k == 'linear-svc':
                classifiers['Linear SVC'] = svm.LinearSVC()
            else:
                g = 0.0
                if k == 'sigmoid':
                    # TODO: Document this magic number
                    # Maximum dot product of the vectors in our data set
                    g = 1.0 / 962.0
                classifiers['SVC kernel=%s' % k] = svm.SVC(kernel=k, gamma=g)
    if has_all or 'tree' in cmd_class:
        # see http://scikit-learn.org/stable/modules/tree.html
        if select == 'off' or select == 'both':
            classifiers['Default Decision Trees'] = tree.DecisionTreeClassifier()
        if select == 'on' or select == 'both':
            classifiers['Default Decision Trees Feature selection Pipeline'] = \
            Pipeline([('selection', ensemble.ExtraTreesClassifier()),
                             ('classification',tree.DecisionTreeClassifier())])

        for maxf in frange(0.85, 0.90, 0.024):
            for c in ['entropy', 'gini']:
                clf = tree.DecisionTreeClassifier(max_features=maxf, criterion=c)

                if select == 'on' or select == 'both':
                    pipe = Pipeline([('selection', ensemble
                    .ExtraTreesClassifier()),
                                     ('classification', clf)])
                    classifiers['Max Features %.2f with %s Decision Trees ' \
                                'Feature selection Pipeline' % (
                                    maxf, c)] = pipe
                if select == 'off' or select == 'both':
                    classifiers['Max Features %.2f with %s Decision Trees' %
                                (maxf, c)] = clf

    if has_all or 'random' in cmd_class:
        for i in range(10):
            classifiers['Random Tree #%d' % i] = tree \
                .ExtraTreeClassifier()

    if has_all or 'ensemble' in cmd_class:
        min_trees = args.min_trees
        max_trees = args.max_trees
        step = 0
        divisor = 10

        while step < 1:
            step = int((max_trees - min_trees)/divisor)
            divisor -= 1

        for trees in range(min_trees, max_trees+1, step):
            clf1 = ensemble.RandomForestClassifier(bootstrap=False,
                                                   n_estimators=trees)
            clf2 = ensemble.ExtraTreesClassifier(bootstrap=False,
                                                 n_estimators=trees)

            if select == 'on' or select == 'both':
                pipe1 = Pipeline([('selection', ensemble
                .ExtraTreesClassifier()),
                                  ('classification', clf1)])
                pipe2 = Pipeline([('selection', ensemble
                .ExtraTreesClassifier()),
                                  ('classification', clf2)])
                classifiers['%d Random Forest ' \
                            'Feature selection Pipeline' % trees] = pipe1
                classifiers['%d Extra Random Trees ' \
                            'Feature selection Pipeline' % trees] = pipe2

            if select == 'off' or select == 'both':
                classifiers['%d Random Forest' % trees] = clf1
                classifiers['%d Extra Random Trees' % trees] = clf2

    if has_all or 'kNN' in cmd_class or 'rNN' in cmd_class:
        # see http://scikit-learn.org/stable/auto_examples/neighbors/plot_classification.html#example-neighbors-plot-classification-py
        selected_weights = args.NN_weights
	if 'all' in args.NN_weights:
            selected_weights = ['uniform', 'distance']
	for weight in selected_weights:
            if 'kNN' in cmd_class:
                show_knn_warnings = args.verbose > 3
                for k in args.kNN_neighbors:
                    classifiers['kNN %s k=%d' % (weight, int(k))] = \
                        neighbors.KNeighborsClassifier(k, weights=weight,
                            warn_on_equidistant=show_knn_warnings)
            if 'rNN' in cmd_class:
                # XXX: Buggy scikit does not handle the distance weight
                #
                # The following error message is being thrown by scikit when
                # the distance weight is being used. This could not be fixed so
                # it is being ignored instead.
                #
                # Traceback (most recent call last):
                #   File "lird.py", line 329, in <module>
                #     main()
                #   File "lird.py", line 316, in main
                #     quality = _evaluate_classifiers(classifiers, datasets)
                #   File "lird.py", line 130, in _evaluate_classifiers
                #     validationSet)
                #   File "lird.py", line 90, in _evaluate_calssifier
                #     pLabels = clf.predict(vVectors)
                #   File "/usr/lib/pymodules/python2.7/sklearn/neighbors/classification.py", line 307, in predict
                #     for (pl, w) in zip(pred_labels[inliers], weights)],
                #   File "/usr/lib/pymodules/python2.7/sklearn/utils/extmath.py", line 305, in weighted_mode
                #     w = np.zeros(a.shape, dtype=w.dtype) + w
                # ValueError: operands could not be broadcast together with shapes (47) (194)
                if weight == 'distance':
                    continue

                # Assign the outlier class to outliers.
                outlier_class = labelEncoder.transform([OUTLIER_LABEL])
                for r in args.rNN_radius:
                    classifiers['rNN %s r=%f' % (weight, float(r))] = \
                        neighbors.RadiusNeighborsClassifier(radius=r,
                            weights=weight, outlier_label=outlier_class)

    # Disabled because of horrible performance
    # classifiers['NuSVC'] = svm.NuSVC()
    return classifiers

def _prepare_data_sets(train_size, test_size, sets):
    '''
    Loads and names all desired datasets into a dirctionary
    TODO proper cross validation see
    http://scikit-learn.org/stable/modules/cross_validation.html#cross-validation
    '''
    datasets = {}
    if 'all' in sets or 'orig' in sets:
        datasets['Out of the Box Integer Data'] = \
            _prepare_data_set(train_size, test_size)
    if 'all' in sets or 'scaled' in sets:
        datasets['Normalizerd Float Data'] = \
            _prepare_data_set(train_size, test_size, float, True)

    return datasets

def _write_dot_file(dtClf, filename='dtClf.dot'):
    '''
    Creates dot file from Decision Tree classifier
    Use graphviz dot tool to create decision tree via
    $ dot -Tpdf out/dtCls.dot -o out/dtCls.pdf
    for more info see http://graphviz.org/
    '''
    try:
        import pydot

        f = open('out/' + filename, 'w')
        tree.export_graphviz(dtClf, out_file=f)
        print 'Written %s file' % ('out/' + filename)
        return
    except ImportError:
        return

def _parse_args():
    '''
    Parse the command line arguments to select test at runtime
    '''
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', type=int, action='store', default=1,
                        help='verbose level, default = 1', choices=range(1, 5))
    parser.add_argument('--select-features', action='store',
                        default='on',
                        help='dis/enable feature selection before '
                             'training', choices=['on', 'off', 'both'])
    parser.add_argument('--min-trees', action='store',default=10,type=int,
                        help='minimum number of trees used in ensemble '
                             'methods')
    parser.add_argument('--max-trees', action='store',default=50,type=int,
                        help='maximum number of trees used in ensemble '
                             'methods')
    parser.add_argument('--train-size', default=16000, action='store',
                        help='amount of data used for training. Can be either \
                              an int representing the absolute number of \
                              samples or a float between 0 and 1.0 \
                              representing the ratio of train samples')
    parser.add_argument('--test-size', default=None, action='store',
                        help='amount of data used for testing, if not \
                              specified the rest of the data set will be used \
                              Like the train-size this can either be a int or \
                              a float')
    parser.add_argument('--data', action='store', default=['all'],
                        choices=['all', 'orig', 'scaled'])
    parser.add_argument('--svm-kernels', action='store', default=['all'],
                        choices=['all', 'rbf', 'linear', 'linear-ovr', 'poly',
                                 'sigmoid'],
                        nargs='*',
                        help='select the kernels that should be trained for the\
                              SVM. by default all will be trained')
    parser.add_argument('classifiers', nargs='*', default='all',
                        choices=['all', 'svm', 'kNN', 'rNN', 'tree', 'random',
                                 'ensemble'])
    parser.add_argument('--NN-weights', action='store', default=['all'],
                        choices=['all', 'uniform', 'distance'], nargs='*')
    parser.add_argument('--kNN-neighbors', action='store', default=[5],
                        nargs='*', type=int)
    parser.add_argument('--rNN-radius', action='store', default=[3.5], \
                        nargs='*', type=float)
    global args
    args = parser.parse_args()

def frange(start, stop, step):
    '''
    helper function to get a range generator for float values.
    '''
    while start < stop:
        yield start
        start += step

def main():
    secs = time.time()
    _parse_args()
    datasets = _prepare_data_sets(args.train_size, args.test_size, args.data)
    classifiers = _prepare_classifiers(args.classifiers)
    quality = _evaluate_classifiers(classifiers, datasets)

    # if 'all' in args.classifiers or 'tree' in args.classifiers:
    # _write_dot_file(classifiers['Decision Tree'])

    #rank classifiers by score and print highscore list
    for clf, (score, trainingTime) in sorted(quality.iteritems(), key=lambda
            (k, v): (v[0], k)):
        print "%.2f %% in %d secs: %s" % (100 * score, trainingTime,clf)

    secs = time.time() - secs;
    print 'Total Time: %d seconds' % secs

    return

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>########
# Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    * See the License for the specific language governing permissions and
#    * limitations under the License.

# Built-in Imports
import requests
from requests import Request,Session,Response
import json
import constants
import sys
import os
import auth
import utils
from cloudify.exceptions import NonRecoverableError
from cloudify import ctx
from cloudify.decorators import operation

@operation
def creation_validation(**_):
    for property_key in constants.NIC_REQUIRED_PROPERTIES:
        _validate_node_properties(property_key, ctx.node.properties)

    nic_name = _get_nic_name(utils.get_nic_name())

    if ctx.node.properties['use_external_resource'] and not nic_name:
        raise NonRecoverableError(
        'External resource, but the supplied '
        'nic does not exist in the account.')

    if not ctx.node.properties['use_external_resource'] and nic_name:
        raise NonRecoverableError(
        'Not external resource, but the supplied '
        'nic exists in the account.')

@operation
#nic:
 def create_nic(**_):
    if ctx.node.properties['use_external_resource']
        if not nic_name:
             raise NonRecoverableError(
             'External nic, but the supplied '
             'nic does not exist in the account.')
             sys.exit(1)
         else
             ctx.instance.runtime_properties['constants.NIC_KEY']=ctx.node.properties['existing_nic_name']
    else

        RANDOM_SUFFIX_VALUE = utils.random_suffix_generator()
        nic_name = ctx.node.properties['nic_name']+RANDOM_SUFFIX_VALUE
        public_ip_name=ctx.node.properties['public_ip_name']
        resource_group_name = ctx.node.properties['resource_group_name']
        location = ctx.node.properties['location']
        subscription_id = ctx.node.properties['subscription_id']
        vnet_name = ctx.node.properties['vnet_name']
        credentials= 'Bearer ' + auth.get_token_from_client_credentials()
        headers = {"Content-Type": "application/json", "Authorization": credentials}

        ctx.logger.info("Checking availability of network interface card: " + nic_name)

        if 1:
            try:
              ctx.logger.info("Creating new network interface card: " + nic_name)
              nic_params=json.dumps({
                                "location":location,
                                "properties":{
                                    "ipConfigurations":[
                                        {
                                            "name":constants.ip_config_name,
                                            "properties":{
                                                "subnet":{
                                                    "id":"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/Microsoft.Network/virtualNetworks/"+vnet_name+"/subnets/"+constants.subnet_name
                                                },
                                                "privateIPAllocationMethod":"Dynamic",
                                                "publicIPAddress":{
                                                        "id":"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/Microsoft.Network/publicIPAddresses/"+public_ip_name
                                            }
                                            }
                                        }
                                    ],
                                }
                            })
              nic_url=constants.azure_url+"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/microsoft.network/networkInterfaces/"+nic_name+"?api-version="+constants.api_version
              response_nic = requests.put(url=nic_url, data=nic_params, headers=headers)

              print(response_nic.text)
              ctx.instance.runtime_properties['nic_name']=nic_name
              ctx.logger.info("response_nic : " + response_nic.text)
              response_nic_json = response_nic.json()
              nic_root_properties = response_nic_json[u'properties']
              ctx.logger.info("nic_root_properties : " + str(nic_root_properties))
              ip_configurations = nic_root_properties[u'ipConfigurations'][0]
              ctx.logger.info("nic ip_configurations0 : " + str(ip_configurations))
              curr_properties = ip_configurations[u'properties']
              ctx.logger.info("nic curr_properties : " + str(curr_properties))
              private_ip_address = curr_properties[u'privateIPAddress']
              ctx.logger.info("nic private_ip_address : " + str(private_ip_address))
              ctx.instance.runtime_properties['private_ip']= str(private_ip_address)
            except:
              ctx.logger.info("network interface card " + nic_name + "could not be created.")
              sys.exit(1)
        else:
         ctx.logger.info("network interface card" + nic_name + "has already been provisioned by another user.")

@operation
def delete_nic(**_):
    vm_name=ctx.node.properties['vm_name']
    nic_name = vm_name+'_nic'
    subscription_id = ctx.node.properties['subscription_id']

    credentials='Bearer '+ auth.get_token_from_client_credentials()

    headers = {"Content-Type": "application/json", "Authorization": credentials}

    resource_group_name = vm_name+'_resource_group'
    if 1:

        try:
           ctx.logger.info("Deleting NIC")
           nic_url=constants.azure_url+"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/microsoft.network/networkInterfaces/"+nic_name+"?api-version="+constants.api_version
           response_nic = requests.delete(url=nic_url,headers=headers)
           print(response_nic.text)
        except:
           ctx.logger.info("Network Interface Card " + nic_name + " could not be deleted.")
           sys.exit(1)
    else:
        ctx.logger.info("Network Interface Card " + nic_name + " does not exist.")

def _validate_node_properties(key, ctx_node_properties):
    if key not in ctx_node_properties:
        raise NonRecoverableError('{0} is a required input. Unable to create.'.format(key))

<commit_msg>Update nic.py<commit_after>########
# Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    * See the License for the specific language governing permissions and
#    * limitations under the License.

# Built-in Imports
import requests
from requests import Request,Session,Response
import json
import constants
import sys
import os
import auth
import utils
from resourcegroup import *
from cloudify.exceptions import NonRecoverableError
from cloudify import ctx
from cloudify.decorators import operation

 RANDOM_SUFFIX_VALUE = utils.random_suffix_generator()
 nic_name = ctx.node.properties['nic_name']+RANDOM_SUFFIX_VALUE

@operation
def creation_validation(**_):
    for property_key in constants.NIC_REQUIRED_PROPERTIES:
        _validate_node_properties(property_key, ctx.node.properties)

    nic_name = _get_nic_name(utils.get_nic_name())

    if ctx.node.properties['use_external_resource'] and not nic_name:
        raise NonRecoverableError(
        'External resource, but the supplied '
        'nic does not exist in the account.')

    if not ctx.node.properties['use_external_resource'] and nic_name:
        raise NonRecoverableError(
        'Not external resource, but the supplied '
        'nic exists in the account.')

@operation
#nic:
 def create_nic(**_):
    if ctx.node.properties['use_external_resource']
        if not nic_name:
             raise NonRecoverableError(
             'External nic, but the supplied '
             'nic does not exist in the account.')
             sys.exit(1)
         else
             ctx.instance.runtime_properties['constants.NIC_KEY']=ctx.node.properties['existing_nic_name']
    else

        RANDOM_SUFFIX_VALUE = utils.random_suffix_generator()
        nic_name = ctx.node.properties['nic_name']+RANDOM_SUFFIX_VALUE
        public_ip_name=ctx.node.properties['public_ip_name']
        resource_group_name = ctx.node.properties['resource_group_name']
        location = ctx.node.properties['location']
        subscription_id = ctx.node.properties['subscription_id']
        vnet_name = ctx.node.properties['vnet_name']
        credentials= 'Bearer ' + auth.get_token_from_client_credentials()
        headers = {"Content-Type": "application/json", "Authorization": credentials}

        ctx.logger.info("Checking availability of network interface card: " + nic_name)

        if 1:
            try:
              ctx.logger.info("Creating new network interface card: " + nic_name)
              nic_params=json.dumps({
                                "location":location,
                                "properties":{
                                    "ipConfigurations":[
                                        {
                                            "name":constants.ip_config_name,
                                            "properties":{
                                                "subnet":{
                                                    "id":"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/Microsoft.Network/virtualNetworks/"+vnet_name+"/subnets/"+constants.subnet_name
                                                },
                                                "privateIPAllocationMethod":"Dynamic",
                                                "publicIPAddress":{
                                                        "id":"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/Microsoft.Network/publicIPAddresses/"+public_ip_name
                                            }
                                            }
                                        }
                                    ],
                                }
                            })
              nic_url=constants.azure_url+"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/microsoft.network/networkInterfaces/"+nic_name+"?api-version="+constants.api_version
              response_nic = requests.put(url=nic_url, data=nic_params, headers=headers)

              print(response_nic.text)
              ctx.instance.runtime_properties['nic_name']=nic_name
              ctx.logger.info("response_nic : " + response_nic.text)
              response_nic_json = response_nic.json()
              nic_root_properties = response_nic_json[u'properties']
              ctx.logger.info("nic_root_properties : " + str(nic_root_properties))
              ip_configurations = nic_root_properties[u'ipConfigurations'][0]
              ctx.logger.info("nic ip_configurations0 : " + str(ip_configurations))
              curr_properties = ip_configurations[u'properties']
              ctx.logger.info("nic curr_properties : " + str(curr_properties))
              private_ip_address = curr_properties[u'privateIPAddress']
              ctx.logger.info("nic private_ip_address : " + str(private_ip_address))
              ctx.instance.runtime_properties['private_ip']= str(private_ip_address)
            except:
              ctx.logger.info("network interface card " + nic_name + "could not be created.")
              sys.exit(1)
        else:
         ctx.logger.info("network interface card" + nic_name + "has already been provisioned by another user.")

@operation
def delete_nic(**_):
    vm_name=ctx.node.properties['vm_name']
    nic_name = vm_name+'_nic'
    subscription_id = ctx.node.properties['subscription_id']

    credentials='Bearer '+ auth.get_token_from_client_credentials()

    headers = {"Content-Type": "application/json", "Authorization": credentials}

    resource_group_name = vm_name+'_resource_group'
    if 1:

        try:
           ctx.logger.info("Deleting NIC")
           nic_url=constants.azure_url+"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/microsoft.network/networkInterfaces/"+nic_name+"?api-version="+constants.api_version
           response_nic = requests.delete(url=nic_url,headers=headers)
           print(response_nic.text)
        except:
           ctx.logger.info("Network Interface Card " + nic_name + " could not be deleted.")
           sys.exit(1)
    else:
        ctx.logger.info("Network Interface Card " + nic_name + " does not exist.")

def _validate_node_properties(key, ctx_node_properties):
    if key not in ctx_node_properties:
        raise NonRecoverableError('{0} is a required input. Unable to create.'.format(key))

<|endoftext|>


<commit_before># Copyright (c) 2015 SUSE Linux GmbH.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# project
from azurectl_exceptions import AzureDomainLookupError

class Defaults(object):
    """
        Default values and static information
    """
    @classmethod
    def get_azure_domain(self, region):
        """
            return domain name according to the specified Azure region
        """
        azure_domain = {
            'Australia East': 'cloudapp.net',
            'Australia Southeast': 'cloudapp.net',
            'Brazil South': 'cloudapp.net',
            'Central India': 'cloudapp.net',
            'Central US': 'cloudapp.net',
            'East Asia': 'cloudapp.net',
            'East US 2': 'cloudapp.net',
            'East US': 'cloudapp.net',
            'Japan East': 'cloudapp.net',
            'Japan West': 'cloudapp.net',
            'North Central US': 'cloudapp.net',
            'North Europe': 'cloudapp.net',
            'South Central US': 'cloudapp.net',
            'Southeast Asia': 'cloudapp.net',
            'South India': 'cloudapp.net',
            'West Europe': 'cloudapp.net',
            'West India': 'cloudapp.net',
            'West US': 'cloudapp.net',
            'China North': 'chinacloudapp.cn',
            'China East': 'chinacloudapp.cn',
            'Germany Central': 'azurecloudapp.de',
            'Germany Northeast': 'azurecloudapp.de'
        }
        if region not in azure_domain:
            raise AzureDomainLookupError(
                'Specified region %s not in lookup table'
            )
        return azure_domain[region]

    @classmethod
    def get_attribute(self, instance, name):
        return getattr(instance, name)

    @classmethod
    def set_attribute(self, instance, name, value):
        return setattr(instance, name, value)
<commit_msg>Bug in errormsg for VM domains<commit_after># Copyright (c) 2015 SUSE Linux GmbH.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# project
from azurectl_exceptions import AzureDomainLookupError

class Defaults(object):
    """
        Default values and static information
    """
    @classmethod
    def get_azure_domain(self, region):
        """
            return domain name according to the specified Azure region
        """
        azure_domain = {
            'Australia East': 'cloudapp.net',
            'Australia Southeast': 'cloudapp.net',
            'Brazil South': 'cloudapp.net',
            'Central India': 'cloudapp.net',
            'Central US': 'cloudapp.net',
            'East Asia': 'cloudapp.net',
            'East US 2': 'cloudapp.net',
            'East US': 'cloudapp.net',
            'Japan East': 'cloudapp.net',
            'Japan West': 'cloudapp.net',
            'North Central US': 'cloudapp.net',
            'North Europe': 'cloudapp.net',
            'South Central US': 'cloudapp.net',
            'Southeast Asia': 'cloudapp.net',
            'South India': 'cloudapp.net',
            'West Europe': 'cloudapp.net',
            'West India': 'cloudapp.net',
            'West US': 'cloudapp.net',
            'China North': 'chinacloudapp.cn',
            'China East': 'chinacloudapp.cn',
            'Germany Central': 'azurecloudapp.de',
            'Germany Northeast': 'azurecloudapp.de'
        }
        if region not in azure_domain:
            raise AzureDomainLookupError(
                'Specified region %s not in lookup table' % region
            )
        return azure_domain[region]

    @classmethod
    def get_attribute(self, instance, name):
        return getattr(instance, name)

    @classmethod
    def set_attribute(self, instance, name, value):
        return setattr(instance, name, value)
<|endoftext|>


<commit_before>import numpy as np

def optimize_chain(chain, unary_cost, pairwise_cost,
                   return_energy=True):
    n_states = unary_cost.shape[1]
    n_nodes = chain.shape[0]

    p = np.zeros((n_states, n_nodes))
    track = np.zeros((n_states, n_nodes), dtype=np.int32)
    p[:,0] = unary_cost[0,:]
    track[:,0] = -1

    for i in xrange(1, n_nodes):
        p[:,i] = unary_cost[i,:]
        p_cost = pairwise_cost[(chain[i - 1], chain[i])]
        for k in xrange(n_states):
            p[k,i] += np.min(p[:,i - 1] + p_cost[:,k])
            track[k,i] = np.argmin(p[:,i - 1] + p_cost[:,k])

    x = np.zeros(n_nodes, dtype=np.int32)
    current = np.argmin(p[:,n_nodes - 1])
    for i in xrange(n_nodes - 1, -1, -1):
        x[i] = current
        current = track[current,i]

    return x, np.min(p[:,n_nodes - 1])

def get_labelling(relaxed_x):
    n_nodes = relaxed_x.shape[0]
    x = np.zeros(n_nodes)
    for i in xrange(n_nodes):
        x[i] = np.where(relaxed_x[i,:])[0][0]
    return x

def trw(node_weights, edges, edge_weights,
        max_iter=100, relaxed=False, verbose=0,
        smart_step=False, return_energy_history=False):
    n_nodes, n_states = node_weights.shape
    n_edges = edges.shape[0]

    pairwise_cost = {}
    for i in xrange(n_edges):
        pairwise_cost[(edges[i,0], edges[i,1])] = edge_weights[i,:]

    x_opt = []
    chains = []
    unaries = []
    lambdas = []
    trees = {k : [] for k in xrange(n_nodes)}
    decode = {}
    for i in xrange(1, 400, 20):
        chains.append(np.arange(i, i + 20) - 1)
        x_opt.append(np.zeros(20))
        lambdas.append(np.zeros((20, n_states)))
        unaries.append(node_weights[chains[-1],:] * 0.5)
        tree_number = len(chains) - 1
        for j, k in enumerate(chains[-1]):
            decode[(k,tree_number)] = j
            trees[k].append(tree_number)
    for i in xrange(1, 21):
        chains.append(np.arange(i, 401, 20) - 1)
        x_opt.append(np.zeros(20))
        lambdas.append(np.zeros((20, n_states)))
        unaries.append(node_weights[chains[-1],:] * 0.5)
        tree_number = len(chains) - 1
        for j, k in enumerate(chains[-1]):
            decode[(k,tree_number)] = j
            trees[k].append(tree_number)

    energy_history = []
    alpha = 1.
    gamma = 1.
    delta = 1.
    rho0 = 1.5
    rho1 = 0.1
    eps = 0.001
    best_dual = -np.inf
    for iteration in xrange(max_iter):
        energy = 0.
        for i, chain in enumerate(chains):
            x_opt[i], e = optimize_chain(chain, unaries[i]+lambdas[i], pairwise_cost)
            energy += e

        mean_x = np.zeros((n_nodes, n_states), dtype=np.float64)
        for i in xrange(n_nodes):
            for k in trees[i]:
                mean_x[i][x_opt[k][decode[(i,k)]]] += 1
        mean_x *= 0.5

        for i, chain in enumerate(chains):
            x = np.zeros((20, n_states), dtype=np.float64)
            x[np.arange(20), x_opt[i]] = 1
            lambdas[i] += alpha * (x - mean_x[chain,:])

        if verbose:
            print 'iteration {}: energy = {}'.format(iteration, energy)

        if energy > best_dual:
            best_dual = energy
        if iteration:
            if smart_step:
                grad_norm = np.sum((mean_x * 2) ** 2)
                if energy > prev_energy:
                    delta *= rho0
                else:
                    delta = max(rho1 * delta, eps)
                alpha = (best_dual + delta) - energy
                alpha /= grad_norm
                alpha *= gamma
            else:
                alpha = 2 / np.sqrt(iteration)

        prev_energy = energy
        energy_history.append(energy)

    if verbose:
        print 'number of unconsistent labels: {}'.format(np.sum(mean_x == 0.5)/2)

    if relaxed:
        return mean_x
    else:
        if return_energy_history:
            return get_labelling(mean_x), energy_history
        else:
            return get_labelling(mean_x)

class TRW(object):
    def __init__(self, n_nodes, n_states, max_iter=1, relaxed=False, verbose=0,
                 smart_step=False):
        self.max_iter = max_iter
        self.iteration = 0
        self.relaxed = relaxed
        self.verbose = verbose
        self.smart_step = smart_step

        self.x_opt = []
        self.chains = []
        self.unaries = []
        self.lambdas = []
        self.trees = {k : [] for k in xrange(n_nodes)}
        self.decode = {}
        for i in xrange(1, 400, 20):
            self.chains.append(np.arange(i, i + 20) - 1)
            self.x_opt.append(np.zeros(20))
            self.lambdas.append(np.zeros((20, n_states)))
            tree_number = len(self.chains) - 1
            for j, k in enumerate(self.chains[-1]):
                self.decode[(k,tree_number)] = j
                self.trees[k].append(tree_number)
        for i in xrange(1, 21):
            self.chains.append(np.arange(i, 401, 20) - 1)
            self.x_opt.append(np.zeros(20))
            self.lambdas.append(np.zeros((20, n_states)))
            tree_number = len(self.chains) - 1
            for j, k in enumerate(self.chains[-1]):
                self.decode[(k,tree_number)] = j
                self.trees[k].append(tree_number)

        self.energy_history = []
        self.alpha = 1.
        self.gamma = 1.
        self.delta = 1.
        self.rho0 = 1.5
        self.rho1 = 0.1
        self.eps = 0.001
        self.best_dual = -np.inf

    def do_step(self, node_weights, edges, pairwise_cost):
        n_nodes, n_states = node_weights.shape
        for iteration in xrange(self.max_iter):
            energy = 0.
            for i, chain in enumerate(self.chains):
                self.x_opt[i], e = optimize_chain(chain, 0.5 * node_weights[chain] + self.lambdas[i], pairwise_cost)
                energy += e

            mean_x = np.zeros((n_nodes, n_states), dtype=np.float64)
            for i in xrange(n_nodes):
                for k in self.trees[i]:
                    mean_x[i][self.x_opt[k][self.decode[(i,k)]]] += 1
            mean_x *= 0.5

            for i, chain in enumerate(self.chains):
                x = np.zeros((20, n_states), dtype=np.float64)
                x[np.arange(20), self.x_opt[i]] = 1
                self.lambdas[i] += self.alpha * (x - mean_x[chain,:])

            if self.verbose:
                print 'iteration {}: energy = {}'.format(iteration, energy)

            if energy > self.best_dual:
                self.best_dual = energy
            if iteration:
                if self.smart_step:
                    grad_norm = np.sum((mean_x * 2) ** 2)
                    if energy > self.prev_energy:
                        self.delta *= self.rho0
                    else:
                        self.delta = max(self.rho1 * self.delta, self.eps)
                    self.alpha = (self.best_dual + self.delta) - energy
                    self.alpha /= grad_norm
                    self.alpha *= self.gamma
                else:
                    self.alpha = 2 / np.sqrt(iteration)

            self.prev_energy = energy
            self.energy_history.append(energy)

            self.iteration += 1

        if self.verbose:
            print 'number of unconsistent labels: {}'.format(np.sum(mean_x == 0.5)/2)
<commit_msg>save count of inconsistent labellings<commit_after>import numpy as np

def optimize_chain(chain, unary_cost, pairwise_cost,
                   return_energy=True):
    n_states = unary_cost.shape[1]
    n_nodes = chain.shape[0]

    p = np.zeros((n_states, n_nodes))
    track = np.zeros((n_states, n_nodes), dtype=np.int32)
    p[:,0] = unary_cost[0,:]
    track[:,0] = -1

    for i in xrange(1, n_nodes):
        p[:,i] = unary_cost[i,:]
        p_cost = pairwise_cost[(chain[i - 1], chain[i])]
        for k in xrange(n_states):
            p[k,i] += np.min(p[:,i - 1] + p_cost[:,k])
            track[k,i] = np.argmin(p[:,i - 1] + p_cost[:,k])

    x = np.zeros(n_nodes, dtype=np.int32)
    current = np.argmin(p[:,n_nodes - 1])
    for i in xrange(n_nodes - 1, -1, -1):
        x[i] = current
        current = track[current,i]

    return x, np.min(p[:,n_nodes - 1])

def get_labelling(relaxed_x):
    n_nodes = relaxed_x.shape[0]
    x = np.zeros(n_nodes)
    for i in xrange(n_nodes):
        x[i] = np.where(relaxed_x[i,:])[0][0]
    return x

def trw(node_weights, edges, edge_weights,
        max_iter=100, relaxed=False, verbose=0,
        smart_step=False, return_energy_history=False):
    n_nodes, n_states = node_weights.shape
    n_edges = edges.shape[0]

    pairwise_cost = {}
    for i in xrange(n_edges):
        pairwise_cost[(edges[i,0], edges[i,1])] = edge_weights[i,:]

    x_opt = []
    chains = []
    unaries = []
    lambdas = []
    trees = {k : [] for k in xrange(n_nodes)}
    decode = {}
    for i in xrange(1, 400, 20):
        chains.append(np.arange(i, i + 20) - 1)
        x_opt.append(np.zeros(20))
        lambdas.append(np.zeros((20, n_states)))
        unaries.append(node_weights[chains[-1],:] * 0.5)
        tree_number = len(chains) - 1
        for j, k in enumerate(chains[-1]):
            decode[(k,tree_number)] = j
            trees[k].append(tree_number)
    for i in xrange(1, 21):
        chains.append(np.arange(i, 401, 20) - 1)
        x_opt.append(np.zeros(20))
        lambdas.append(np.zeros((20, n_states)))
        unaries.append(node_weights[chains[-1],:] * 0.5)
        tree_number = len(chains) - 1
        for j, k in enumerate(chains[-1]):
            decode[(k,tree_number)] = j
            trees[k].append(tree_number)

    energy_history = []
    alpha = 1.
    gamma = 1.
    delta = 1.
    rho0 = 1.5
    rho1 = 0.1
    eps = 0.001
    best_dual = -np.inf
    for iteration in xrange(max_iter):
        energy = 0.
        for i, chain in enumerate(chains):
            x_opt[i], e = optimize_chain(chain, unaries[i]+lambdas[i], pairwise_cost)
            energy += e

        mean_x = np.zeros((n_nodes, n_states), dtype=np.float64)
        for i in xrange(n_nodes):
            for k in trees[i]:
                mean_x[i][x_opt[k][decode[(i,k)]]] += 1
        mean_x *= 0.5

        for i, chain in enumerate(chains):
            x = np.zeros((20, n_states), dtype=np.float64)
            x[np.arange(20), x_opt[i]] = 1
            lambdas[i] += alpha * (x - mean_x[chain,:])

        if verbose > 2:
            print 'iteration {}: energy = {}'.format(iteration, energy)

        if energy > best_dual:
            best_dual = energy
        if iteration:
            if smart_step:
                grad_norm = np.sum((mean_x * 2) ** 2)
                if energy > prev_energy:
                    delta *= rho0
                else:
                    delta = max(rho1 * delta, eps)
                alpha = (best_dual + delta) - energy
                alpha /= grad_norm
                alpha *= gamma
            else:
                alpha = 2 / np.sqrt(iteration)

        prev_energy = energy
        energy_history.append(energy)

    if verbose > 0:
        print 'number of unconsistent labels: {}'.format(np.sum(mean_x == 0.5)/2)

    if relaxed:
        return mean_x
    else:
        if return_energy_history:
            return get_labelling(mean_x), energy_history
        else:
            return get_labelling(mean_x)

class TRW(object):
    def __init__(self, n_nodes, n_states, max_iter=1, relaxed=False, verbose=0,
                 smart_step=False):
        self.max_iter = max_iter
        self.iteration = 0
        self.relaxed = relaxed
        self.verbose = verbose
        self.smart_step = smart_step

        self.x_opt = []
        self.chains = []
        self.unaries = []
        self.lambdas = []
        self.trees = {k : [] for k in xrange(n_nodes)}
        self.decode = {}
        for i in xrange(1, 400, 20):
            self.chains.append(np.arange(i, i + 20) - 1)
            self.x_opt.append(np.zeros(20))
            self.lambdas.append(np.zeros((20, n_states)))
            tree_number = len(self.chains) - 1
            for j, k in enumerate(self.chains[-1]):
                self.decode[(k,tree_number)] = j
                self.trees[k].append(tree_number)
        for i in xrange(1, 21):
            self.chains.append(np.arange(i, 401, 20) - 1)
            self.x_opt.append(np.zeros(20))
            self.lambdas.append(np.zeros((20, n_states)))
            tree_number = len(self.chains) - 1
            for j, k in enumerate(self.chains[-1]):
                self.decode[(k,tree_number)] = j
                self.trees[k].append(tree_number)

        self.energy_history = []
        self.alpha = 1.
        self.gamma = 1.
        self.delta = 1.
        self.rho0 = 1.5
        self.rho1 = 0.1
        self.eps = 0.001
        self.best_dual = -np.inf
        self.inconsistent = []

    def do_step(self, node_weights, edges, pairwise_cost):
        n_nodes, n_states = node_weights.shape
        for iteration in xrange(self.max_iter):
            energy = 0.
            for i, chain in enumerate(self.chains):
                self.x_opt[i], e = optimize_chain(chain, 0.5 * node_weights[chain] + self.lambdas[i], pairwise_cost)
                energy += e

            mean_x = np.zeros((n_nodes, n_states), dtype=np.float64)
            for i in xrange(n_nodes):
                for k in self.trees[i]:
                    mean_x[i][self.x_opt[k][self.decode[(i,k)]]] += 1
            mean_x *= 0.5

            for i, chain in enumerate(self.chains):
                x = np.zeros((20, n_states), dtype=np.float64)
                x[np.arange(20), self.x_opt[i]] = 1
                self.lambdas[i] += self.alpha * (x - mean_x[chain,:])

            if self.verbose:
                print 'iteration {}: energy = {}'.format(iteration, energy)

            if energy > self.best_dual:
                self.best_dual = energy
            if iteration:
                if self.smart_step:
                    grad_norm = np.sum((mean_x * 2) ** 2)
                    if energy > self.prev_energy:
                        self.delta *= self.rho0
                    else:
                        self.delta = max(self.rho1 * self.delta, self.eps)
                    self.alpha = (self.best_dual + self.delta) - energy
                    self.alpha /= grad_norm
                    self.alpha *= self.gamma
                else:
                    self.alpha = 2 / np.sqrt(iteration)

            self.prev_energy = energy
            self.energy_history.append(energy)

            self.iteration += 1

        self.inconsistent.append(np.sum(mean_x == 0.5)/2)

        if self.verbose:
            print 'number of unconsistent labels: {}'.format(self.inconsistent[-1])
<|endoftext|>


<commit_before>import numpy as np

from scipy.optimize import fmin_l_bfgs_b
from graph_utils import decompose_graph, decompose_grid_graph
from trw_utils import *

def trw(node_weights, edges, edge_weights,
        max_iter=100, verbose=0, tol=1e-3,
        strategy='sqrt',
        r0=1.5, r1=0.5, gamma=0.1):

    assert strategy in ['best-dual', 'best-primal', 'sqrt', 'linear']

    result = decompose_grid_graph([(node_weights, edges, edge_weights)])
    contains_node, chains, edge_index = result[0][0], result[1][0], result[2][0]

    n_nodes, n_states = node_weights.shape

    y_hat = []
    lambdas = []
    multiplier = []

    for p in xrange(n_nodes):
        multiplier.append(1.0 / len(contains_node[p]))
    for chain in chains:
        lambdas.append(np.zeros((len(chain), n_states)))
        y_hat.append(np.zeros(len(chain)))

    multiplier = np.array(multiplier)
    multiplier.shape = (n_nodes, 1)

    delta = 1.
    learning_rate = 0.1
    dual_history = []
    primal_history = []

    best_dual = np.inf
    best_primal = -np.inf

    for iteration in xrange(max_iter):
        dual = 0.0
        unaries = node_weights * multiplier

        for i, chain in enumerate(chains):
            y_hat[i], e = optimize_chain(chain,
                                         lambdas[i] + unaries[chain,:],
                                         edge_weights,
                                         edge_index)

            dual += e

        lambda_sum = np.zeros((n_nodes, n_states), dtype=np.float64)
        for p in xrange(n_nodes):
            for i in contains_node[p]:
                pos = np.where(chains[i] == p)[0][0]
                lambda_sum[p, y_hat[i][pos]] += multiplier[p]

        p_norm = 0.0
        for i in xrange(len(chains)):
            N = lambdas[i].shape[0]

            dlambda = lambda_sum[chains[i],:].copy()
            dlambda[np.ogrid[:N], y_hat[i]] -= 1

            p_norm += np.sum(dlambda ** 2)

            lambdas[i] += learning_rate * dlambda

        primal = compute_energy(get_labelling(lambda_sum), unaries, edge_weights, edges)
        primal_history.append(primal)
        dual_history.append(dual)

        if iteration and (np.abs(dual - dual_history[-2]) < tol or p_norm < tol):
            if verbose:
                print 'Converged'
            break

        if iteration:
            if strategy == 'sqrt':
                learning_rate = 1. / np.sqrt(iteration)
            elif strategy == 'linear':
                learning_rate = 1. / iteration
            elif strategy == 'best-dual':
                best_dual = min(best_dual, dual)
                approx = best_dual - delta
                if dual <= dual_history[-2]:
                    delta *= r0
                else:
                    delta = max(r1 * delta, 1e-4)
                learning_rate = gamma * (dual - approx) / p_norm
            elif strategy == 'best-primal':
                best_primal = max(best_primal, primal)
                learning_rate = gamma * (dual - best_primal) / p_norm

        if verbose:
            print 'iteration {}: dual energy = {}'.format(iteration, dual)

    info = {}
    info['dual_energy'] = dual_history
    info['primal_energy'] = primal_history

    return lambda_sum, info

def trw_unconstr(node_weights, edges, edge_weights,
                 max_iter=100, verbose=0, tol=1e-3,
                 strategy='sqrt',
                 r0=1.5, r1=0.5, gamma=0.1):

    assert strategy in ['best-dual', 'best-primal', 'sqrt', 'linear']

    result = decompose_grid_graph([(node_weights, edges, edge_weights)], get_sign=True)
    contains_node, chains, edge_index, sign = result[0][0], result[1][0], result[2][0], result[3][0]

    n_nodes, n_states = node_weights.shape

    y_hat = []
    lambdas = np.zeros((n_nodes, n_states))
    multiplier = []

    for p in xrange(n_nodes):
        multiplier.append(1.0 / len(contains_node[p]))
        assert len(contains_node[p]) == 2
    for chain in chains:
        y_hat.append(np.zeros(len(chain)))

    multiplier = np.array(multiplier)
    multiplier.shape = (n_nodes, 1)

    delta = 1.
    learning_rate = 0.1
    dual_history = []
    primal_history = []

    best_dual = np.inf
    best_primal = -np.inf

    for iteration in xrange(max_iter):
        dual = 0.0
        unaries = node_weights * multiplier

        for i, chain in enumerate(chains):
            y_hat[i], e = optimize_chain(chain,
                                         sign[i] * lambdas[chain,:] + unaries[chain,:],
                                         edge_weights,
                                         edge_index)

            dual += e

        p_norm = 0.0
        lambda_sum = np.zeros((n_nodes, n_states), dtype=np.float64)
        for p in xrange(n_nodes):
            dlambda = np.zeros(n_states)
            for i in contains_node[p]:
                pos = np.where(chains[i] == p)[0][0]
                lambda_sum[p, y_hat[i][pos]] += multiplier[p]
                dlambda[y_hat[i][pos]] += sign[i]
            p_norm += np.sum(dlambda ** 2)
            lambdas[p] -= learning_rate * dlambda

        primal = compute_energy(get_labelling(lambda_sum), unaries, edge_weights, edges)
        primal_history.append(primal)
        dual_history.append(dual)

        if iteration and (np.abs(dual - dual_history[-2]) < tol or p_norm < tol):
            if verbose:
                print 'Converged'
            break

        if iteration:
            if strategy == 'sqrt':
                learning_rate = 1. / np.sqrt(iteration)
            elif strategy == 'linear':
                learning_rate = 1. / iteration
            elif strategy == 'best-dual':
                best_dual = min(best_dual, dual)
                approx = best_dual - delta
                if dual <= dual_history[-2]:
                    delta *= r0
                else:
                    delta = max(r1 * delta, 1e-4)
                learning_rate = gamma * (dual - approx) / p_norm
            elif strategy == 'best-primal':
                best_primal = max(best_primal, primal)
                learning_rate = gamma * (dual - best_primal) / p_norm

        if verbose:
            print 'iteration {}: dual energy = {}'.format(iteration, dual)

    info = {}
    info['dual_energy'] = dual_history
    info['primal_energy'] = primal_history

    return lambda_sum, info

def f(x, node_weights, multiplier, chains, edge_weights, edge_index, sign, y_hat, contains_node, history):
    dual = 0
    n_nodes, n_states = node_weights.shape
    unaries = node_weights * multiplier
    lambdas = x.reshape((400, 10))

    for i, chain in enumerate(chains):
        y_hat[i], e = optimize_chain(chain,
                                     sign[i] * lambdas[chain,:] + unaries[chain,:],
                                     edge_weights,
                                     edge_index)

        dual += e

    dlambda = np.zeros((n_nodes, n_states), dtype=np.float64)
    for p in xrange(n_nodes):
        for i in contains_node[p]:
            pos = np.where(chains[i] == p)[0][0]
            dlambda[p][y_hat[i][pos]] += sign[i]

    history.append(dual)
    print dual

    return dual, dlambda.reshape(4000)

def trw_lbfgs(node_weights, edges, edge_weights,
              max_iter=100, verbose=1, tol=1e-3):

    result = decompose_grid_graph([(node_weights, edges, edge_weights)], get_sign=True)
    contains_node, chains, edge_index, sign = result[0][0], result[1][0], result[2][0], result[3][0]

    n_nodes, n_states = node_weights.shape

    y_hat = []
    lambdas = np.zeros((n_nodes, n_states))
    multiplier = []

    for p in xrange(n_nodes):
        multiplier.append(1.0 / len(contains_node[p]))
        assert len(contains_node[p]) == 2
    for chain in chains:
        y_hat.append(np.zeros(len(chain)))

    multiplier = np.array(multiplier)
    multiplier.shape = (n_nodes, 1)

    history = []
    x, f_val, d = fmin_l_bfgs_b(f, np.zeros((n_nodes, n_states)),
                                args=(node_weights, multiplier, chains, edge_weights, edge_index, sign, y_hat, contains_node, history),
                                maxiter=max_iter,
                                disp=verbose,
                                pgtol=tol)

    lambdas = x.reshape((400, 10))
    unaries = node_weights * multiplier
    for i, chain in enumerate(chains):
        y_hat[i], e = optimize_chain(chain,
                                     sign[i] * lambdas[chain,:] + unaries[chain,:],
                                     edge_weights,
                                     edge_index)

    lambda_sum = np.zeros((n_nodes, n_states), dtype=np.float64)
    for p in xrange(n_nodes):
        for i in contains_node[p]:
            pos = np.where(chains[i] == p)[0][0]
            lambda_sum[p, y_hat[i][pos]] += multiplier[p]

    info = {}
    info['x'] = x
    info['f'] = f_val
    info['d'] = d
    info['history'] = history

    return lambda_sum, info

<commit_msg>minor<commit_after>import numpy as np

from scipy.optimize import fmin_l_bfgs_b
from graph_utils import decompose_graph, decompose_grid_graph
from trw_utils import *

def trw(node_weights, edges, edge_weights,
        max_iter=100, verbose=0, tol=1e-3,
        strategy='sqrt',
        r0=1.5, r1=0.5, gamma=0.1):

    assert strategy in ['best-dual', 'best-primal', 'sqrt', 'linear']

    result = decompose_grid_graph([(node_weights, edges, edge_weights)])
    contains_node, chains, edge_index = result[0][0], result[1][0], result[2][0]

    n_nodes, n_states = node_weights.shape

    y_hat = []
    lambdas = []
    multiplier = []

    for p in xrange(n_nodes):
        multiplier.append(1.0 / len(contains_node[p]))
    for chain in chains:
        lambdas.append(np.zeros((len(chain), n_states)))
        y_hat.append(np.zeros(len(chain)))

    multiplier = np.array(multiplier)
    multiplier.shape = (n_nodes, 1)

    delta = 1.
    learning_rate = 0.1
    dual_history = []
    primal_history = []

    best_dual = np.inf
    best_primal = -np.inf

    for iteration in xrange(max_iter):
        dual = 0.0
        unaries = node_weights * multiplier

        for i, chain in enumerate(chains):
            y_hat[i], e = optimize_chain(chain,
                                         lambdas[i] + unaries[chain,:],
                                         edge_weights,
                                         edge_index)

            dual += e

        lambda_sum = np.zeros((n_nodes, n_states), dtype=np.float64)
        for p in xrange(n_nodes):
            for i in contains_node[p]:
                pos = np.where(chains[i] == p)[0][0]
                lambda_sum[p, y_hat[i][pos]] += multiplier[p]

        p_norm = 0.0
        for i in xrange(len(chains)):
            N = lambdas[i].shape[0]

            dlambda = lambda_sum[chains[i],:].copy()
            dlambda[np.ogrid[:N], y_hat[i]] -= 1

            p_norm += np.sum(dlambda ** 2)

            lambdas[i] += learning_rate * dlambda

        primal = compute_energy(get_labelling(lambda_sum), unaries, edge_weights, edges)
        primal_history.append(primal)
        dual_history.append(dual)

        if iteration and (np.abs(dual - dual_history[-2]) < tol or p_norm < tol):
            if verbose:
                print 'Converged'
            break

        if iteration:
            if strategy == 'sqrt':
                learning_rate = 1. / np.sqrt(iteration)
            elif strategy == 'linear':
                learning_rate = 1. / iteration
            elif strategy == 'best-dual':
                best_dual = min(best_dual, dual)
                approx = best_dual - delta
                if dual <= dual_history[-2]:
                    delta *= r0
                else:
                    delta = max(r1 * delta, 1e-4)
                learning_rate = gamma * (dual - approx) / p_norm
            elif strategy == 'best-primal':
                best_primal = max(best_primal, primal)
                learning_rate = gamma * (dual - best_primal) / p_norm

        if verbose:
            print 'iteration {}: dual energy = {}'.format(iteration, dual)

    info = {}
    info['dual_energy'] = dual_history
    info['primal_energy'] = primal_history

    return lambda_sum, info

def trw_unconstr(node_weights, edges, edge_weights,
                 max_iter=100, verbose=0, tol=1e-3,
                 strategy='sqrt',
                 r0=1.5, r1=0.5, gamma=0.1):

    assert strategy in ['best-dual', 'best-primal', 'sqrt', 'linear']

    result = decompose_grid_graph([(node_weights, edges, edge_weights)], get_sign=True)
    contains_node, chains, edge_index, sign = result[0][0], result[1][0], result[2][0], result[3][0]

    n_nodes, n_states = node_weights.shape

    y_hat = []
    lambdas = np.zeros((n_nodes, n_states))
    multiplier = []

    for p in xrange(n_nodes):
        multiplier.append(1.0 / len(contains_node[p]))
        assert len(contains_node[p]) == 2
    for chain in chains:
        y_hat.append(np.zeros(len(chain)))

    multiplier = np.array(multiplier)
    multiplier.shape = (n_nodes, 1)

    delta = 1.
    learning_rate = 0.1
    dual_history = []
    primal_history = []

    best_dual = np.inf
    best_primal = -np.inf

    for iteration in xrange(max_iter):
        dual = 0.0
        unaries = node_weights * multiplier

        for i, chain in enumerate(chains):
            y_hat[i], e = optimize_chain(chain,
                                         sign[i] * lambdas[chain,:] + unaries[chain,:],
                                         edge_weights,
                                         edge_index)

            dual += e

        p_norm = 0.0
        lambda_sum = np.zeros((n_nodes, n_states), dtype=np.float64)
        for p in xrange(n_nodes):
            dlambda = np.zeros(n_states)
            for i in contains_node[p]:
                pos = np.where(chains[i] == p)[0][0]
                lambda_sum[p, y_hat[i][pos]] += multiplier[p]
                dlambda[y_hat[i][pos]] += sign[i]
            p_norm += np.sum(dlambda ** 2)
            lambdas[p] -= learning_rate * dlambda

        primal = compute_energy(get_labelling(lambda_sum), unaries, edge_weights, edges)
        primal_history.append(primal)
        dual_history.append(dual)

        if iteration and (np.abs(dual - dual_history[-2]) < tol or p_norm < tol):
            if verbose:
                print 'Converged'
            break

        if iteration:
            if strategy == 'sqrt':
                learning_rate = 1. / np.sqrt(iteration)
            elif strategy == 'linear':
                learning_rate = 1. / iteration
            elif strategy == 'best-dual':
                best_dual = min(best_dual, dual)
                approx = best_dual - delta
                if dual <= dual_history[-2]:
                    delta *= r0
                else:
                    delta = max(r1 * delta, 1e-4)
                learning_rate = gamma * (dual - approx) / p_norm
            elif strategy == 'best-primal':
                best_primal = max(best_primal, primal)
                learning_rate = gamma * (dual - best_primal) / p_norm

        if verbose:
            print 'iteration {}: dual energy = {}'.format(iteration, dual)

    info = {}
    info['dual_energy'] = dual_history
    info['primal_energy'] = primal_history

    return lambda_sum, info

def f(x, node_weights, multiplier, chains, edge_weights, edge_index, sign, y_hat, contains_node, history):
    dual = 0
    n_nodes, n_states = node_weights.shape
    unaries = node_weights * multiplier
    lambdas = x.reshape((400, 10))

    for i, chain in enumerate(chains):
        y_hat[i], e = optimize_chain(chain,
                                     sign[i] * lambdas[chain,:] + unaries[chain,:],
                                     edge_weights,
                                     edge_index)

        dual += e

    dlambda = np.zeros((n_nodes, n_states), dtype=np.float64)
    for p in xrange(n_nodes):
        for i in contains_node[p]:
            pos = np.where(chains[i] == p)[0][0]
            dlambda[p][y_hat[i][pos]] += sign[i]

    history.append(dual)

    return dual, dlambda.reshape(4000)

def trw_lbfgs(node_weights, edges, edge_weights,
              max_iter=100, verbose=1, tol=1e-3):

    result = decompose_grid_graph([(node_weights, edges, edge_weights)], get_sign=True)
    contains_node, chains, edge_index, sign = result[0][0], result[1][0], result[2][0], result[3][0]

    n_nodes, n_states = node_weights.shape

    y_hat = []
    lambdas = np.zeros((n_nodes, n_states))
    multiplier = []

    for p in xrange(n_nodes):
        multiplier.append(1.0 / len(contains_node[p]))
        assert len(contains_node[p]) == 2
    for chain in chains:
        y_hat.append(np.zeros(len(chain)))

    multiplier = np.array(multiplier)
    multiplier.shape = (n_nodes, 1)

    history = []
    x, f_val, d = fmin_l_bfgs_b(f, np.zeros((n_nodes, n_states)),
                                args=(node_weights, multiplier, chains, edge_weights, edge_index, sign, y_hat, contains_node, history),
                                maxiter=max_iter,
                                disp=verbose,
                                pgtol=tol)

    lambdas = x.reshape((400, 10))
    unaries = node_weights * multiplier
    for i, chain in enumerate(chains):
        y_hat[i], e = optimize_chain(chain,
                                     sign[i] * lambdas[chain,:] + unaries[chain,:],
                                     edge_weights,
                                     edge_index)

    lambda_sum = np.zeros((n_nodes, n_states), dtype=np.float64)
    for p in xrange(n_nodes):
        for i in contains_node[p]:
            pos = np.where(chains[i] == p)[0][0]
            lambda_sum[p, y_hat[i][pos]] += multiplier[p]

    info = {}
    info['x'] = x
    info['f'] = f_val
    info['d'] = d
    info['history'] = history

    return lambda_sum, info

<|endoftext|>