# coding: utf-8
import pickle
from pprint import pformat
import numpy as np

np.set_printoptions(precision=4)
np.set_printoptions(edgeitems=1)

"""
Explanation: DEMO:
obj: Mini demo to inspect a studies with multiple parameters...
End of explanation
"""


with open("report.pkl", 'r') as pfile:
    report = pickle.load(pfile)

"""
Explanation: Load the report
End of explanation
"""


print("Unconstraint")
print("-" * 10)

print(" - total of {0} studies".format(len(report.recons_im)))
print(" - fixed parameters are : {0}".format(report.fixed_params))
print(" - floatting parameters are : {0}".format(report.floatting_params))
for param in report.floatting_params:
    print("   -- '{0}' takes values {1}".format(param,
                                                report.get_list_params(param)))

for metric in report.metrics_funcs:
    print(" - best score for {0} is {1}"
          "obtained with parameters #{2}".format(metric.func_name,
                                                 report.best_score(metric),
                                                 report.best_index(metric)))
    print(" - {0}".format(pformat(report.best_params(metric))))

"""
Explanation: Inspect the report on all the floatting parameters
End of explanation
"""


print("Constraint #1")
print("-" * 10)

print("Now we fix those floatting parameters:")
key_fixed = report.floatting_params[:-1]
values_fixed = [[report.get_list_params(param)[0]]
                                     for param in report.floatting_params[:-1]]
filter_ = dict(zip(key_fixed, values_fixed))
print("  -- {0}".format(filter_))
print("The remaning floatting parameters is:")
key_floatting = report.floatting_params[-1]
values_floatting = report.get_list_params(report.floatting_params[-1])
print("  -- {0}".format({key_floatting:values_floatting}))

for metric in report.metrics_funcs:
    print(" - best score for "
          "{0} is {1} obtained with:".format(metric.func_name,
                                             report.best_score(metric, filter_)))
    print(" - {0}".format(pformat(report.best_params(metric, filter_))))

"""
Explanation: Inspect the report on a subset of the floatting parameters
End of explanation
"""


print("Constraint #2")
print("-" * 10)

print("Now we fix those floatting parameters:")
key_fixed = report.floatting_params[1:]
values_fixed = [[report.get_list_params(param)[0]]
                                      for param in report.floatting_params[1:]]
filter_ = dict(zip(key_fixed, values_fixed))
print("  -- {0}".format(filter_))
print("The remaning floatting parameters is:")
key_floatting = report.floatting_params[0]
values_floatting = report.get_list_params(report.floatting_params[0])
print("  -- {0}".format({key_floatting:values_floatting}))

for metric in report.metrics_funcs:
    print(" - best score for "
          "{0} is {1} obtained with:".format(metric.func_name,
                                             report.best_score(metric, filter_)))
    print(" - {0}".format(pformat(report.best_params(metric, filter_))))

"""
Explanation: Inspect the report on a n other subset of floatting parameters
End of explanation
"""


