# Set the parameters by cross-validation
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                     'C': [1, 10, 100, 1000]},
                    {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]

scores = [
    ('precision', ),
    ('recall', recall_score),
]

# for score_name, score_func in scores:
# print "# Tuning hyper-parameters for %s" % score_name
print

clf = IPythonGridSearchCV(SVC(C=1), tuned_parameters, score_func=precision_score, view=v)
clf.fit(X_train, y_train, cv=5)
print "fit submitted"
while v.outstanding:
    v.wait(timeout=0.1)
    grid_scores = clf.collect_results()
    
    # import IPython
    # IPython.embed()
    # 
    # print "Best parameters set found on development set:"
    # print
    # print clf.best_estimator_
    print
    print "Grid scores on development set:"
    print
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
X = digits.images.reshape((n_samples, -1))
y = digits.target

# Split the dataset in two equal parts
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_fraction=0.5, random_state=0)

# Set the parameters by cross-validation
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                     'C': [1, 10, 100, 1000]},
                    {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]

search = IPythonGridSearchCV(SVC(C=1), tuned_parameters,
                             score_func=precision_score, view=v, cv=5,
                             local_store='/tmp')
search.fit_async(X_train, y_train)
print "Launched asynchronous fit on a cluster."


def print_scores(scores):
    for params, mean_score, scores, mean_duration, durations in scores:
        print "%0.3f (+/-%0.03f) [%i] for %r (%0.3fs)" % (
            mean_score, scores.std() / 2, len(scores), params, mean_duration)

while v.outstanding:
    v.wait(timeout=0.5)
    completed_scores, n_remaining = search.collect_results()
    top_scores = completed_scores[:3]