#!/usr/bin/env python

"""
Python implementation of common model fitting operations to
analyse protein folding data. Simply automates some fitting
and value calculation. Will be extended to include phi-value
analysis and other common calculations.

Allows for quick model evaluation and plotting.

Also tried to make this somewhat abstract and modular to
enable more interesting calculations, such as Ising models
and such.

Requirements (recommended python 2.7+):
    - numpy
    - scipy
    - matplotlib

Lowe, A.R. 2015-2016

"""

import os
import sys
import time
import inspect
import numpy as np
import scipy as sp

import matplotlib.pyplot as plt

# use a global optimisation algorithm for the Ising model fitting
# from scipy.optimize import differential_evolution, minimize, leastsq, curve_fit

# import PyFolding specific libraries
from . import constants
# import models
from . import core
# from ising import IsingDomain

__author__ = "Alan R. Lowe"
__email__ = "a.lowe@ucl.ac.uk"


# set some global plotting params
plt.rc('xtick', labelsize=constants.LABEL_SIZE)
plt.rc('ytick', labelsize=constants.LABEL_SIZE)


"""
CORE PLOTTING FUNCTIONS
"""

def plot_figure(equilibrium, chevron, pth=None, display=False, save=False):
    """ Plot a figure with the data and fits combined.

    TODO: Clean this up. It is terrible.
    """
    if equilibrium.two_state:
        dfifty = equilibrium.midpoint
        dfive = equilibrium.point(0.05)
        dninetyfive = equilibrium.point(0.95)

    fity = np.exp(chevron.results.y_fit)
    res = chevron.results.residuals
    fiteq = equilibrium.results.y_fit

    #
    eq_y_range = (np.min(equilibrium.y)-.1, np.max(equilibrium.y)+.1)
    kin_x_range = (-.1, np.max(chevron.x)+.1)
    # NOTE (ergm) changed limits of the dfifity to make a prettier picture 1/9/2017
    kin_y_range = (np.min(chevron.y_raw), np.max(chevron.y_raw))


    # PLOT THE EQUILIBRIUM DATA
    plt.figure(figsize=(14,8))
    plt.subplot2grid((4,2),(0,0))
    if equilibrium.two_state:
        plt.plot([dfifty,dfifty],eq_y_range,'b-',[dfive,dfive],eq_y_range,'b:',
                [dninetyfive,dninetyfive],eq_y_range,'b:')
    plt.plot(equilibrium.x, equilibrium.y,'ko' , markersize=constants.MARKER_SIZE)