# Path: utils/test_time_augmentation.py
# class TestTimeAutgmentationPredictor(BaseEstimator):
#     def __init__(self, net, n_jobs=2,
#                  scale_choices=[1], shear_choices=[0],
#                  rotation_choices=[0], translation_choices=[0]):
#         self.net = net
#         self.pool = multiprocessing.Pool(processes=n_jobs)
#         self.scale_choices = scale_choices
#         self.shear_choices = shear_choices
#         self.rotation_choices = rotation_choices
#         self.translation_choices = translation_choices
# 
#     def predict_proba(self, X):
#         tf_args = list(itertools.product(
#             self.scale_choices, self.shear_choices,
#             self.rotation_choices, self.translation_choices
#         ))
# 
#         print 'Number of combinations', len(tf_args)
# 
#         X_probas = []
# 
#         for x in tqdm(X, total=len(X)):
#             tta_transform_x = functools.partial(tta_transform, x)
#             x_tf = np.asarray(self.pool.map(tta_transform_x, tf_args))
# 
#             x_tf_pred_probas = self.net.predict_proba(x_tf)
# 
#             x_tf_pred_proba = x_tf_pred_probas.sum(axis=0)
#             x_tf_pred_proba /= x_tf_pred_proba.sum()
# 
#             X_probas.append(x_tf_pred_proba)
# 
#         return np.vstack(X_probas)


import argparse
import importlib
import os
import sys
import cPickle as pickle
import numpy as np
import pandas as pd
from time import strftime
from utils.test_time_augmentation import TestTimeAutgmentationPredictor
    model = load_model(args.model)
    net = model.net
    net.initialize()
    print 'Loading model weights from %s' % model.model_fname
    net.load_params_from(model.model_fname)
    print

    if args.mean_fname is not None:
        print 'Loading mean image'
        X_mean = load_mean(args.mean_fname, args.as_grey)
        if X_mean is None:
            print 'Failed to load mean file from', args.mean_fname
            sys.exit(1)
        net.batch_iterator_train.mean = X_mean
        net.batch_iterator_test.mean = X_mean
        print 'Injected mean image'
    print

    print 'Predicting...'
    if args.tta:
        print 'Using test time augmentation'
        scale_choices = [0.9, 1, 1.1]
        # shear_choices = [-0.25, 0, 0.25]
        rotation_choices = [0, 90, 180, 270]
        translation_choices = [-12, 0, 12]
        # scale_choices = [1]
        # shear_choices = [0]
        # rotation_choices = [0]
        # translation_choices = [0]


# Path: utils/test_time_augmentation.py
# class TestTimeAutgmentationPredictor(BaseEstimator):
#     def __init__(self, net, n_jobs=2,
#                  scale_choices=[1], shear_choices=[0],
#                  rotation_choices=[0], translation_choices=[0]):
#         self.net = net
#         self.pool = multiprocessing.Pool(processes=n_jobs)
#         self.scale_choices = scale_choices
#         self.shear_choices = shear_choices
#         self.rotation_choices = rotation_choices
#         self.translation_choices = translation_choices
# 
#     def predict_proba(self, X):
#         tf_args = list(itertools.product(
#             self.scale_choices, self.shear_choices,
#             self.rotation_choices, self.translation_choices
#         ))
# 
#         print 'Number of combinations', len(tf_args)
# 
#         X_probas = []
# 
#         for x in tqdm(X, total=len(X)):
#             tta_transform_x = functools.partial(tta_transform, x)
#             x_tf = np.asarray(self.pool.map(tta_transform_x, tf_args))
# 
#             x_tf_pred_probas = self.net.predict_proba(x_tf)
# 
#             x_tf_pred_proba = x_tf_pred_probas.sum(axis=0)
#             x_tf_pred_proba /= x_tf_pred_proba.sum()
# 
#             X_probas.append(x_tf_pred_proba)
# 
#         return np.vstack(X_probas)


# Path: scripts/predict_from_head_crop.py
import argparse
import importlib
import os
import sys
import cPickle as pickle
import numpy as np
import pandas as pd
from time import strftime
from utils.test_time_augmentation import TestTimeAutgmentationPredictor

    model = load_model(args.model)
    net = model.net
    net.initialize()
    print 'Loading model weights from %s' % model.model_fname
    net.load_params_from(model.model_fname)
    print

    if args.mean_fname is not None:
        print 'Loading mean image'
        X_mean = load_mean(args.mean_fname, args.as_grey)
        if X_mean is None:
            print 'Failed to load mean file from', args.mean_fname
            sys.exit(1)
        net.batch_iterator_train.mean = X_mean
        net.batch_iterator_test.mean = X_mean
        print 'Injected mean image'
    print

    print 'Predicting...'
    if args.tta:
        print 'Using test time augmentation'
        scale_choices = [0.9, 1, 1.1]
        # shear_choices = [-0.25, 0, 0.25]
        rotation_choices = [0, 90, 180, 270]
        translation_choices = [-12, 0, 12]
        # scale_choices = [1]
        # shear_choices = [0]
        # rotation_choices = [0]
        # translation_choices = [0]


        tta = TestTimeAutgmentationPredictor(