#!/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

"""

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

import core
import constants

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

def list_models():
	""" List the kinetic of equilibrium models defined in this module.

	Returns a list of the names of the models, whose parent class is
	FitModel.
	"""
	clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
	verif = lambda cls: 'Verified: {0}'.format(cls[1]().verified)
	fit_models = [ (cls[0], verif(cls)) for cls in clsmembers if cls[1].__bases__[0] == core.FitModel ]
	return fit_models

class TemplateModel(core.FitModel):
	""" A template model for expansion
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([])

	def fit_func(self, x):
		raise NotImplementedError

	@property
	def equation(self):
		return r'F=f(x)'

"""
==========================================================
EQUILIBRIUM FOLDING models
==========================================================
"""

class TwoStateEquilibrium(core.FitModel):
	""" Two state equilbrium denaturation curve.

	F = \frac{\exp( m(x-d_{50})) / RT} { 1+\exp(m(x-d_{50}))/RT}

	Notes:
		Clarke and Fersht. Engineered disulfide bonds as probes of
		the folding pathway of barnase: Increasing the stability
		of proteins against the rate of denaturation.
		Biochemistry (1993) vol. 32 (16) pp. 4322-4329
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([1.5, 5.])
		self.verified = True

	def fit_func(self, x, m, d50):
		F = ( np.exp((m*(x-d50))/core.temperature.RT)) / (1.+np.exp((m*(x-d50))/core.temperature.RT))
		return F

	@property
	def equation(self):
		return r'F = \frac{\exp( m(x-d_{50})) / RT} { 1+\exp(m(x-d_{50}))/RT}'

class TwoStateEquilibriumSloping(core.FitModel):
	""" Two state equilbrium denaturation curve.

	F = (\alpha_f+\beta_f x) + (\alpha_u+\beta_u x) \cdot \frac{\exp( m(x-d_{50})) / RT} { 1+\exp(m(x-d_{50}))/RT}

	Notes:
		Clarke and Fersht. Engineered disulfide bonds as probes of
		the folding pathway of barnase: Increasing the stability
		of proteins against the rate of denaturation.
		Biochemistry (1993) vol. 32 (16) pp. 4322-4329
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([1., 0.1, 0.0, 0.1, 1.5, 5.])
		self.verified = True

	def fit_func(self, x, alpha_f, beta_f, alpha_u, beta_u, m, d50):
		F = (alpha_f+beta_f*x) + (alpha_u+beta_u*x) * (\
		( np.exp((m*(x-d50))/core.temperature.RT)) / (1.+np.exp((m*(x-d50))/core.temperature.RT)))
		return F

	@property
	def equation(self):
		return r'F = (\alpha_f+\beta_f x) + (\alpha_u+\beta_u x) \cdot \frac{\exp( m(x-d_{50})) / RT} { 1+\exp(m(x-d_{50}))/RT}'

class TwoStateDimerEquilibrium(core.FitModel):
	""" Two State model for a dimer denaturation Equilibrium.
	i.e.  N2 = 2D

	Y_0 = Y_N * (1 - F_D) + Y_D * F_D
	Y_N = alpha_N + beta_N * x
	Y_D = alpha_D + beta_D * x
	F_D = (((K_U)^2 + (8 * K_U * P_t - K_U)^0.5)/(4 * P_t)
	K_U = exp((RT * ln(P_t)-m(d_{50}-x)))/RT)

	Notes:
		Mallam and Jackson. Folding studies on a knotted protein.
		Journal of Molecular Biology (2005) vol. 346 (5) pp. 1409-1421

	Comments:
		Pt is a variable that needs to be set, So it like Ising model when you need
		to define a specific value to a curve.
		Could this be the self-constants code line ?

	"""

	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([1., 0.1, 0.0, 0.1, 1.5, 5.])
		self.constants = (('Pt',7),)

	def fit_func(self, x, alpha_N, beta_N, alpha_D, beta_D, m, d50):
		K_U = np.exp(((core.temperature.RT * np.ln(Pt))-(m*d50)-x) / core.temperature.RT)
		F_D = ((K_U^2 + (8 * K_U * Pt))^0.5 - K_U) / (4*Pt)
		Y_0 = ((alpha_N + beta_N*x)*(1-F_D)) +  ((alpha_D + beta_D*x)*(F_D))
		return Y_0

	"""
	@property
	def equation(self):
		return r'Y_0 = (\alpha_N+\beta_N x) \cdot (1-F_D) + Y_D \cdot F_D \\
				\text{where}\\
				F_D = \frac{(K_U^2 + (8 * K_U * Pt))^0.5 - K_U / 4*Pt} (\alpha_u+\beta_u x) \\
				K_U = \frac{\exp(((constants.RT * np.ln(Pt))-(m*d50)-x) / constants.RT}'
	"""

# NOTE(ergm) added this on 30/8/2017
class ThreeStateEquilibrium (core.FitModel):
	""" Three state equilbrium denaturation curve.

	i.e.  N = I = D

	Y_obs = Y_N*F_N + Y_I*F_I + Y_D*F_D

	where:
	F_N = 1/(1 + Kni + Kni*Kid)
	F_I = Kni / (1 + Kni + Kni*Kid)
	F_D = Kni*Kid/(1 + Kni + Kni*Kid)
	Kni = exp((-DGni - m_ni * x)/RT)
	Kid = exp((-DGid - m_id * x)/RT)

	expanded:

	Y_obs = Y_N + Y_I*exp((DGni_H20 + m_ni*x)/RT) + Y_D*exp((DGni_H20 + m_ni*x)/RT) * exp((DGid_H20 + m_id*x)/RT) /
			1 + exp((DGni_H20 + m_ni*x)/RT) + exp((DGni_H20 + m_ni*x)/RT) * exp((DGid_H20 + m_id*x)/RT)

	Notes:
		Hecky J, Muller KM Structural Perturbation and Compensation by Directed
		Evolution at Physiological Temperature Leads to Thermostabilization of
		beta-Lactamase. (2005) Biochemistry 44. pp. 12640 –12654

	Comments:
		Y_obs = The spectroscopic signal maximum as a function of denaturant concentration
		YN and YD are the spectroscopic signals for native and denatured states
		K = Equilibrium Constants
		F_D = fraction of denatured

	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([1., 0.5, 0.0, 5., 1.5, 5., 1])

	def fit_func(self, x, Y_N, Y_I, Y_D, DGni, m_ni, DGid, m_id):
		F = (Y_N + Y_I*np.exp((-DGni + m_ni*x)/core.temperature.RT) + Y_D*np.exp((-DGni + m_ni*x)/core.temperature.RT) * np.exp((-DGid + m_id*x)/core.temperature.RT)) \
		/ (1 + np.exp((-DGni + m_ni*x)/core.temperature.RT) + np.exp((-DGni + m_ni*x)/core.temperature.RT) * np.exp((-DGid + m_id*x)/core.temperature.RT))
		return F

	@property
	def equation(self):
		return r'\Upsilon_{obs} = \Upsilon_N F_N + \Upsilon_I F_I + \Upsilon_D F_D \\ \
				\text{expanded} \\ \
				\Upsilon_{obs} = \frac{ \Upsilon_N + \Upsilon_I \exp \frac {\Delta G_{NI}^{H_2O} + m_{NI} x} {RT} + \
				\Upsilon_D \exp \frac{\Delta G_{NI}^{H_2O} + m_{NI} x} {RT} \cdot \exp \frac{\Delta G_{ID}^{H_2O} + m_{ID} x} {RT}} {1 + \exp \
				\frac{\Delta G_{NI}^{H_2O} + m_{NI} x} {RT} + \exp \frac{\Delta G_{NI}^{H_2O} + m_{NI} x} {RT} \cdot  \
 			    \exp \frac{\Delta G_{ID}^{H_2O} + m_{ID} x} {RT}}'

class ThreeStateMonoIEquilibrium(core.FitModel):
	""" Three State model for a dimer denaturation Equilibrium.
	i.e.  N2 = I2 = 2D

	Y_rel = (Y_N * ((2*Pt*F_D^2)/(K1*K2))) + (Y_I * ((2*Pt*F_D^2)/K2)) + (Y_D * F_D)
	F_D = -((K1*K2) + ((K1*K2)^2 + (8*(1+K1)*(K1*K2)*Pt))^0.5) / (4*Pt*(1+K1))
	K1 = exp((DG1 + m1*x)/RT)
	K2 = exp((DG2 + m2*x)/RT)

	Notes:
		Mallam and Jackson. Folding studies on a knotted protein.
		Journal of Molecular Biology (2005) vol. 346 (5) pp. 1409-1421

	Comments:
		Pt is a variable that needs to be set, So it like Ising model when you need
		to define a specific value to a curve.
		also needs to fit to multiple datasets

	"""

	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([1., 0.1, 0.0, 0.1, 1.5, 5., 2.])
		self.constants = (('Pt',1e-6),)

	def fit_func(self, x, DG1, m1, DG2, m2, Y_N, Y_I, Y_D, Pt):
		K1 = np.exp((-DG1 + (m1*x)) / core.temperature.RT)
		K2 = np.exp((-DG2 + (m2*x)) / core.temperature.RT)
		F_D = -((K1*K2) + ((K1*K2)^2 + (8*(1+K1)*(K1*K2)*Pt))^0.5) / (4*Pt*(1+K1))
		Y_rel = (Y_N * ((2*Pt*F_D^2)/(K1*K2))) + (Y_I * ((2*Pt*F_D^2)/K2)) + (Y_D * F_D)
		return Y_rel

	"""
	@property
	def equation(self):
		return r'Y_rel = (Y_N \cdot \frac{(2PtF_D^2/K1K2)} + (Y_I \cdot \frac{(2PtF_D^2)/K2)} + (Y_D * F_D) \\
			\text{where}\\ F_D = \frac {- K1K2 + ((K1K2)^2 + (8(1+K1)(K1K2)Pt))^0.5) / 4Pt(1+K1)} \\
			K1 = \frac {\exp((-DG1 + (m1 x)) / RT)} \\ K2 = \frac {\exp((-DG2 + (m2 x)) / RT)} '
	"""

class ThreeStateDimericIEquilibrium(core.FitModel):
	""" Three State model for a dimer denaturation Equilibrium.
	i.e.  N2 = 2I = 2D

	Y_rel = (Y_N * ((2*Pt*F_D^2)/(K1*K2))) + (Y_I * ((2*Pt*F_D^2)/K2)) + (Y_D * F_D)
	F_D = -((K1*K2) + ((K1*K2)^2 + (8*(1+K1)*(K1*K2)*Pt))^0.5) / (4*Pt*(1+K1))
	K1 = exp((DG1 + m1*x)/RT)
	K2 = exp((DG2 + m2*x)/RT)

	Notes:
		Mallam and Jackson. Folding studies on a knotted protein.
		Journal of Molecular Biology (2005) vol. 346 (5) pp. 1409-1421

	Comments:
		Pt is a variable that needs to be set, So it like Ising model when you need
		to define a specific value to a curve.
		also needs to fit to multiple datasets.

	"""

	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([1., 0.1, 0.0, 0.1, 1.5, 5., 3.])
		self.constants = (('Pt',1e-6),)

	def fit_func(self, x, DG1, m1, DG2, m2, Y_N, Y_I, Y_D, Pt):
		K1 = np.exp((-DG1 + (m1*x)) / core.temperature.RT)
		K2 = np.exp((-DG2 + (m2*x)) / core.temperature.RT)
		F_I = -(K1*(1+K2) + ((K1^2*(1+K2)^2 +(8*Pt*K1))^0.5)) / (4*Pt)
		Y_rel = (Y_N * ((2*Pt*F_I^2)/K1)) + (Y_I * F_I) + (Y_D * (K2*F_I))
		return Y_rel

	"""
	@property
	def equation(self):
		return r'Y_rel = (Y_N \cdot \frac{(2PtF_I^2/K1)} + (Y_I F_I} + (Y_D * (K2F_I)) \\
			\text{where} \\
			F_I = \frac {- K1(1+K2) + (K1^2 \cdot(1+K2)^2 + (8 Pt K1))^0.5) / 4Pt} \\
			K1 = \frac {\exp((-DG1 + (m1 x)) / RT)} \\
			K2 = \frac {\exp((-DG2 + (m2 x)) / RT)}'
	"""

class HomozipperIsingEquilibrium(core.FitModel):
	""" Homopolymer Zipper Ising model

	Notes:
		Aksel and Barrick. Analysis of repeat-protein folding using
		nearest-neighbor statistical mechanical models.
		Methods in enzymology (2009) vol. 455 pp. 95-125
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([7, 0.1, -.53, -4.6])
		self.constants = (('n',7),)
		self.verified = True

	def fit_func(self, x, n, DG_intrinsic, m_intrinsic, DG_interface):
		# , m_interface , -0.6

		# clamp to prevent instability
		if DG_intrinsic<0. or DG_interface>0.:
			return core.FIT_ERROR(x)

		k = np.exp(-(DG_intrinsic - m_intrinsic*x) / core.temperature.RT )
		#t = np.exp(-(DG_interface - m_interface*x) / core.temperature.RT )
		t = np.exp(-(DG_interface) / core.temperature.RT )
		pre_factor = (k/(n*(k*t-1)))
		numerator = n*(k*t)**(n+2) - (n+2)*(k*t)**(n+1) + (n+2)*k*t-n
		denominator = (k*t-1)**2 + k*((k*t)**(n+1) - (n+1)*k*t+n )
		theta = pre_factor * (numerator / denominator)
		return 1.-theta

"""
==========================================================
KINETIC FOLDING models
==========================================================
"""

class TwoStateChevron(core.FitModel):
	""" Two state chevron plot.

	k_{obs} = k_u^{H_2O}\exp(m_{ku}x) + k_f^{H_2O}\exp(m_{kf}x)

	Notes:
		Jackson SE and Fersht AR.  Folding of chymotrypsin inhibitor 2.
		1. Evidence for a two-state transition.
		Biochemistry (1991) 30(43):10428-10435.
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([50., 1.3480, 5e-4, 1.])
		#self.constants = (('mf',1.76408),('mu',1.13725))
		self.verified = True

	def fit_func(self, x, kf, mf, ku, mu):
		k_obs = kf*np.exp(-mf*x) + ku*np.exp(mu*x)
		return k_obs

	def error_func(self, y):
		return np.log(y)

	@property
	def equation(self):
		return r'k_{obs} = k_f^{H_2O}\exp(-m_{kf}x) + k_u^{H_2O}\exp(m_{ku}x)'

class ThreeStateChevron(core.FitModel):
	""" Three state chevron with single intermediate.

	k_{obs} = k_{fi}^{H_2O} * exp(-m_{if}*x) +
				k_{if}^{H_2O} * exp((m_i - m_{if})*x) /
				(1 + 1 / K_{iu})

	where:

	K_{iu} = K_{iu}^{H_2O} * exp((m_u-m_i)*x)

	Notes:
		Parker et al. An integrated kinetic analysis of
		intermediates and transition states in protein folding
		reactions.
		Journal of molecular biology (1995) vol. 253 (5) pp. 771-86
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([4.5e-4, -9.5e-1, 1.3e9, -6.9,  1.4e-8, -1.6])
		#self.constants = (('mif',-0.97996),('mi',-6.00355),('mu',-1.66154))
		self.verified = True

	def fit_func(self, x, kfi, mif, kif, mi, Kiu, mu):
		k_fi = kfi*np.exp(-mif*x)
		k_if = kif*np.exp((mi - mif)*x)
		K_iu = Kiu*np.exp((mu - mi)*x)
		k_obs = k_fi + k_if / (1.+1./K_iu)
		return k_obs

	def error_func(self, y):
		return np.log(y)

	def components(self, x, kfi, mif, kif, mi, Kiu, mu):
		k_fi = kfi*np.exp(-mif*x)
		k_if = kif*np.exp((mi - mif)*x)
		k_obs_I = k_fi + k_if
		return {'kobs_I':k_obs_I}

	@property
	def equation(self):
		return r'k_{obs} = k_{fi}^{H_2O}\exp(-m_{if}x) + k_{if}^{H_2O}\exp((m_i - m_{if})x) /(1 + 1 / (K_{iu}^{H_2O}\exp((m_u-m_i)x)))'

class ThreeStateFastPhaseChevron(core.FitModel):
	""" Three state chevron with single intermediate.

	k_{obs} = k_{fi}^{H_2O} * exp(-m_{if}*x) +
				k_{if}^{H_2O} * exp((m_i - m_{if})*x) /
				(1 + 1 / K_{iu})

	where:

	K_{iu} = K_{iu}^{H_2O} * exp((m_u-m_i)*x)

	Notes:
		Parker et al. An integrated kinetic analysis of
		intermediates and transition states in protein folding
		reactions.
		Journal of molecular biology (1995) vol. 253 (5) pp. 771-86
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([172., 1.42, .445, .641, 9.41e3, 2.71313, 1.83e-4, 1.06])
		#self.constants = (('kui',172.), ('mui',1.42), ('kiu',.445), ('miu',.641), ('mif',-2.71313),('mfi',1.06534))
		self.verified = True

	def fit_func(self, x, kui, mui, kiu, miu, kif, mif, kfi, mfi):
		k_iu = kiu*np.exp(miu*x)
		k_ui = kui*np.exp(-mui*x)
		k_if = kif*np.exp(-mif*x)
		k_fi = kfi*np.exp(mfi*x)
		K_iu = k_iu / (k_iu+k_ui)
		k_obs = k_fi + k_if / (1.+1./K_iu)
		return k_obs

	def error_func(self, y):
		return np.log(y)

	def components(self, x, kui, mui, kiu, miu, kif, mif, kfi, mfi):
		k_iu = kiu*np.exp(miu*x)
		k_ui = kui*np.exp(-mui*x)
		k_if = kif*np.exp(-mif*x)
		k_fi = kfi*np.exp(mfi*x)
		k_obs_I = k_iu + k_ui
		k_obs_N = k_fi + k_if
		return {'kobs_I':k_obs_I} #, 'kobs_N':k_obs_N}

	# NOTE (ergm) EDIT 23/8/2017
	@property
	def equation(self):
		return r'k_{obs} = k_{fi}^{H_2O}\exp(-m_{if}x) + k_{if}^{H_2O}\exp((m_i - m_{if})x) / (1 + 1 /K_{iu}^{H_2O}\exp((m_u-m_i)x))  \\ \
        where  \\ \
        k_{ui} = k_{ui}^{H2O}  exp(-m_{ui}x) \\ \
        k_{iu} = k_{iu}^{H2O}  exp(m_{iu}x) \\ \
        K_{iu} = k_{iu}/(k_{iu} + k_{ui})'

class ThreeStateSequentialChevron(core.FitModel):
	""" Three state metastable intermediate chevron plot.

	A_1 = -(k_{ui}+k_{iu}+k_{if}+k_{fi})
	A_2 = k_{ui}*(k_{if}+k_{fi}) + k_{iu}*k_{uf}

	k_{obs} = 0.5 * (-A_2 - sqrt(A_2^2 - 4*A_1))

	Notes:
		Bachmann and Kiefhaber. Apparent two-state tendamistat
		folding is a sequential process along a defined route.
		J Mol Biol (2001) vol. 306 (2) pp. 375-386
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([2e4, 0.3480, 20.163, 1.327, 0.3033, 0.2431])
		# self.constants = (('mui',4.34965),('mif',0.68348),('mfi',0.97966))
		self.verified = True

	def fit_func(self, x, kui, mui, kif, mif, kfi, mfi):
		kiu = 1.e4
		miu = 0.
		k_ui = kui*np.exp(-mui*x)
		k_iu = kiu*np.exp(miu*x)
		k_if = kif*np.exp(-mif*x)
		k_fi = kfi*np.exp(mfi*x)
		lam_1 = -(k_ui + k_iu + k_if + k_fi)
		lam_2 = k_ui * (k_if+k_fi) + k_iu*k_fi
		k_obs = 0.5 * (-lam_1 - np.sqrt(lam_1**2 - 4*lam_2))
		return k_obs

	def error_func(self, y):
		return np.log(y)

	def components(self, x, kui, mui, kif, mif, kfi, mfi):
		kiu = 1.e4
		miu = 0.
		k_ui = kui*np.exp(-mui*x)
		k_iu = kiu*np.exp(miu*x)
		k_if = kif*np.exp(-mif*x)
		k_fi = kfi*np.exp(mfi*x)
		k_TS1 = k_ui + (k_fi/kif)*k_iu
		k_TS2 = (k_ui/k_iu)*k_if + k_fi
		return {'kTS1':k_TS1, 'kTS2':k_TS2}

	@property
	def equation(self):
		return r'k_{obs} = 0.5(-A_2 \pm \sqrt{A_2^2 - 4A_1}) \\ \text{where}\\ A_1 = -(k_{ui} + k_{iu} + k_{if} + k_{fi}) \\A_2 = k_{ui}(k_{if} + k_{fi}) + k_{iu}k_{if} \\ \text{and} \\k_{ui} = k_{ui}^{H_2O}\exp(-m_{ui}x) \\k_{iu} = k_{iu}^{H_2O}\exp(-m_{iu}x) \\ etc...'

class ParallelTwoStateChevron(core.FitModel):
	""" Two state chevron plot.

	k_{obs} = k_u^{H_2O}\exp(m_ku*x) + k_u^{H_2O}\exp(m_kf*x)

	Notes:
		[Reference]
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([50., 1.3480, 5e-4, 1., 150., 3.5])

	def fit_func(self, x, kf_A, mf_A, ku_A, mu_A, kf_B, mf_B):

		if mf_A < 0. or mf_B < 0. or mu_A < 0.:
			return core.FIT_ERROR(x)

		if kf_A <0. or ku_A <0. or kf_B < 0.:
			return core.FIT_ERROR(x)

		deltaG_A = kf_A / ku_A
		ku_B = kf_B / deltaG_A
		mu_B = np.abs(mf_A + mu_A) - np.abs(mf_B)
		k_obs_A = kf_A*np.exp(-mf_A*x) + ku_A*np.exp(mu_A*x)
		k_obs_B = kf_B*np.exp(-mf_B*x) + ku_B*np.exp(mu_B*x)
		k_obs = k_obs_A + k_obs_B
		return k_obs

	def error_func(self, y):
		return np.log(y)

	def components(self, x, kf_A, mf_A, ku_A, mu_A, kf_B, mf_B):
		deltaG_A = kf_A / ku_A
		ku_B = kf_B / deltaG_A
		mu_B = np.abs(mf_A + mu_A) - np.abs(mf_B)
		k_obs_A = kf_A*np.exp(-mf_A*x) + ku_A*np.exp(mu_A*x)
		k_obs_B = kf_B*np.exp(-mf_B*x) + ku_B*np.exp(mu_B*x)
		k_obs = k_obs_A + k_obs_B
		return {'kobs_A':k_obs_A, 'kobs_B':k_obs_B}

	@property
	def equation(self):
		return r'k_{obs} = k_u^{H_2O}\exp(m_{ku}x) + k_u^{H_2O}\exp(m_{kf}x)'

class ParallelTwoStateUnfoldingChevron(core.FitModel):
	""" Two state chevron plot.

	k_{obs} = k_u^{H_2O}\exp(m_ku*x) + k_u^{H_2O}\exp(m_kf*x)

	Notes:
		[Reference]
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([5e-4, 1., 1e-5, 1.5])

	def fit_func(self, x, ku_A, mu_A, ku_B, mu_B):

		if mu_A < 0. or mu_B < 0.:
			return core.FIT_ERROR(x)

		k_obs_A = ku_A*np.exp(mu_A*x)
		k_obs_B = ku_B*np.exp(mu_B*x)
		k_obs = k_obs_A + k_obs_B
		return k_obs

	def error_func(self, y):
		return np.log(y)

	def components(self, x, ku_A, mu_A, ku_B, mu_B):
		k_obs_A = ku_A*np.exp(mu_A*x)
		k_obs_B = ku_B*np.exp(mu_B*x)
		k_obs = k_obs_A + k_obs_B
		return {'kobs_A':k_obs_A, 'kobs_B':k_obs_B}

class TwoStateChevronMovingTransition(core.FitModel):
	""" Two state chevron with moving transition state.
	Second order polynomial.

	k_u = k_u^{H_2O} * \exp(m_{ku}*x) * \exp(m_{ku}^' * x^2)
	k_f = k_f^{H_2O} * \exp(m_{kf}*x) * \exp(m_{kf}^' * x^2)

	k_{obs} = k_u + k_f

	Notes:
		Ternstrom et al. From snapshot to movie: phi analysis
		of protein folding transition states taken one step
		further.
		PNAS (1999) vol. 96 (26) pp. 14854-9
	"""
	def __init__(self):
		core.FitModel.__init__(self)
		fit_args = self.fit_func_args
		self.params = tuple( [(fit_args[i],i) for i in xrange(len(fit_args))] )
		self.default_params = np.array([50., 1.3480, 5e-4, 1., 1.])
		self.verified = False

	def fit_func(self, x, ku, mu, kf, mf, m_prime):
		k_obs = ku*(np.exp(mu*x)+np.exp(m_prime*x*x)) + kf*(np.exp(mf*x)+np.exp(m_prime*x*x))
		return k_obs

	def error_func(self, y):
		return np.log(y)

	@property
	def equation(self):
		return r'k_u = k_u^{H_2O} \cdot \exp(m_{ku}*x) \cdot \exp(m^{\'} x^2) \\ \
				k_f = k_f^{H_2O} \cdot \exp(m_{kf}*x) \cdot \exp(m^{\'} x^2) \\ \
				k_{obs} = k_u + k_f'

if __name__ == "__main__":
	get_models()


# %%
import numpy as np
import pandas as pd
import os
from sklearn.preprocessing import StandardScaler

# %%
dg = pd.read_csv('files/match_player_formatted_with_rating.txt', '|')

ds = dg.sort_values(by = ['DATE', 'COUNTRIES', 'GROUND', 'PLAYER_RATING'], ascending = [1, 1, 1, 0])
ds = ds.reindex(columns = ['DATE', 'COUNTRIES', 'GROUND', 'TEAM', 'PLAYER_NAME', 'PLAYER_ID', 'PLAYER_RATING'])

# %%
def pivot(df, dq, attr, j, index):
    total = 0

    for i in range(0, 11):
        total += dq.iloc[i][attr]

    df.at[index, f'{attr}.MEAN.{j}'] = total / 11

# %%
#T1 = 'New Zealand'
#T2 = 'India'
#IS_HOST_1 = 0
#IS_HOST_2 = 0
#RANK_1 = 1
#RANK_2 = 3
#TEMP = 13.6

#%%
wc = pd.read_csv('data/Cricket_World_cup_fixtures.csv', encoding='cp1252')
dm = pd.read_csv('files/2019_lineup.csv', ',')
dm['PLAYER_RATING'] = dg['PLAYER_RATING'].min()

testset = pd.DataFrame()
for index, r0 in wc.iterrows():
    T1 = r0['TEAM.1']
    T2 = r0['TEAM.2']
    IS_HOST_1 = r0['IS_HOST.1']
    IS_HOST_2 = r0['IS_HOST.2']
    RANK_1 = r0['RANK.1']
    RANK_2 = r0['RANK.2']
    TEMP = r0['TEMP.HOST']
    TEMP_1 = r0['TEMP.1']
    TEMP_2 = r0['TEMP.2']
    print(T1, T2, IS_HOST_1, IS_HOST_2, RANK_1, RANK_2)
    #T1 = 'India'
    #T2 = 'South Africa'
    #IS_HOST_1 = 0
    #IS_HOST_2 = 0
    #RANK_1 = 2
    #RANK_2 = 3
    #TEMP = 21
    #TEMP_1 = 32
    #TEMP_2 = 18

    dn = dm[(dm['TEAM'] == T1) | (dm['TEAM'] == T2)].copy()

    for row in dn.itertuples():
        dd = dg[dg['PLAYER_ID'] == row.PLAYER]
        dn.at[row.Index, 'TEAM'] = 0 if dn.at[row.Index, 'TEAM'] == T1 else 1
        if dd.index.size == 0:
            print('error', row.PLAYER)
            continue
        dn.at[row.Index, 'PLAYER_RATING'] = dd.iloc[-1]['PLAYER_RATING']
        dn.at[row.Index, 'INNINGS'] = dd.iloc[-1]['INNINGS']
        dn.at[row.Index, 'R_T5'] = dd.iloc[-1]['R_T5']
        dn.at[row.Index, 'R_T10'] = dd.iloc[-1]['R_T10']
        dn.at[row.Index, 'BAT_AVG_T5'] = dd.iloc[-1]['BAT_AVG_T5']
        dn.at[row.Index, 'BAT_AVG_T10'] = dd.iloc[-1]['BAT_AVG_T10']
        dn.at[row.Index, 'BAT_SR_T5'] = dd.iloc[-1]['BAT_SR_T5']
        dn.at[row.Index, 'BAT_SR_T10'] = dd.iloc[-1]['BAT_SR_T10']
        dn.at[row.Index, 'WIN_T5'] = dd.iloc[-1]['WIN_T5']
        dn.at[row.Index, 'WIN_T10'] = dd.iloc[-1]['WIN_T10']
        dn.at[row.Index, 'O_T5'] = dd.iloc[-1]['O_T5']
        dn.at[row.Index, 'O_T10'] = dd.iloc[-1]['O_T10']
        dn.at[row.Index, 'W_T5'] = dd.iloc[-1]['W_T5']
        dn.at[row.Index, 'W_T10'] = dd.iloc[-1]['W_T10']
        dn.at[row.Index, 'BOWL_ER_T5'] = dd.iloc[-1]['BOWL_ER_T5']
        dn.at[row.Index, 'BOWL_ER_T10'] = dd.iloc[-1]['BOWL_ER_T10']
        dn.at[row.Index, 'BOWL_AVG_T5'] = dd.iloc[-1]['BOWL_AVG_T5']
        dn.at[row.Index, 'BOWL_AVG_T10'] = dd.iloc[-1]['BOWL_AVG_T10']
        dn.at[row.Index, 'BOWL_SR_T5'] = dd.iloc[-1]['BOWL_SR_T5']
        dn.at[row.Index, 'BOWL_SR_T10'] = dd.iloc[-1]['BOWL_SR_T10']
        dn.at[row.Index, 'TEMP_T5'] = abs(dd.iloc[-1]['TEMP_M_T5'] - TEMP)
        dn.at[row.Index, 'TEMP_T10'] = abs(dd.iloc[-1]['TEMP_M_T10'] - TEMP)

    dn.fillna(0, inplace = True)

    dh = dn.sort_values(by = ['TEAM', 'PLAYER_RATING'], ascending = [1, 0])
    dh = dh[dh['TEAM'] == 0][:11].append(dh[dh['TEAM'] == 1][:11])

    ds = dh.sort_values(by = ['PLAYER_RATING'], ascending = [0])

    df = pd.DataFrame([[T1, T2, IS_HOST_1, IS_HOST_2,
                            RANK_1, RANK_2, TEMP, TEMP_1, TEMP_2]],
                columns = ['TEAM.1','TEAM.2','IS_HOST.1','IS_HOST.2',
                           'RANK.1','RANK.2', 'TEMP.HOST', 'TEMP.1', 'TEMP.2'])

    for r1 in df.itertuples():
        for i in range(0, 2):
            dq = dh[dh['TEAM'] == i]

            pivot(df, dq, 'PLAYER_RATING', i + 1, r1[0])
            pivot(df, dq, 'INNINGS', i + 1, r1[0])
            pivot(df, dq, 'R_T5', i + 1, r1[0])
            pivot(df, dq, 'R_T10', i + 1, r1[0])
            pivot(df, dq, 'BAT_AVG_T5', i + 1, r1[0])
            pivot(df, dq, 'BAT_AVG_T10', i + 1, r1[0])
            pivot(df, dq, 'BAT_SR_T5', i + 1, r1[0])
            pivot(df, dq, 'BAT_SR_T10', i + 1, r1[0])
            pivot(df, dq, 'WIN_T5', i + 1, r1[0])
            pivot(df, dq, 'WIN_T10', i + 1, r1[0])
            pivot(df, dq, 'O_T5', i + 1, r1[0])
            pivot(df, dq, 'O_T10', i + 1, r1[0])
            pivot(df, dq, 'W_T5', i + 1, r1[0])
            pivot(df, dq, 'W_T10', i + 1, r1[0])
            pivot(df, dq, 'BOWL_AVG_T5', i + 1, r1[0])
            pivot(df, dq, 'BOWL_AVG_T10', i + 1, r1[0])
            pivot(df, dq, 'BOWL_ER_T5', i + 1, r1[0])
            pivot(df, dq, 'BOWL_ER_T10', i + 1, r1[0])
            pivot(df, dq, 'BOWL_SR_T5', i + 1, r1[0])
            pivot(df, dq, 'BOWL_SR_T10', i + 1, r1[0])
            pivot(df, dq, 'TEMP_T5', i + 1, r1[0])
            pivot(df, dq, 'TEMP_T10', i + 1, r1[0])
    testset = testset.append(df)

#%%
df = testset.copy()
df['diff_rank_t1t2'] = df['RANK.1'] - df['RANK.2']
df['diff_rank_t2t1'] = df['RANK.2'] - df['RANK.1']

df['diff_temp_tHt1'] = df['TEMP.HOST'] - df['TEMP.1']
df['diff_temp_tHt2'] = df['TEMP.HOST'] - df['TEMP.2']

df['diff_PR_t1t2'] = df['PLAYER_RATING.MEAN.1'] - df['PLAYER_RATING.MEAN.2']
df['diff_PR_t2t1'] = df['PLAYER_RATING.MEAN.2'] - df['PLAYER_RATING.MEAN.1']

df['diff_BA_t1t2'] = df['BAT_AVG_T10.MEAN.1'] - df['BAT_AVG_T10.MEAN.2']
df['diff_BA_t2t1'] = df['BAT_AVG_T10.MEAN.2'] - df['BAT_AVG_T10.MEAN.1']

df['diff_BWLA_t1t2'] = df['BOWL_AVG_T10.MEAN.1'] - df['BOWL_AVG_T10.MEAN.2']
df['diff_BWLA_t2t1'] = df['BOWL_AVG_T10.MEAN.2'] - df['BOWL_AVG_T10.MEAN.1']

# %%
X = df[cols]
X = StandardScaler().fit_transform(X)

# %%
y = clf.predict(X)
pred = list(zip(testset['TEAM.1'], testset['TEAM.2'], y))
pred = pd.DataFrame(pred, columns = ['TEAM.1', 'TEAM.2', 'WIN'])
pred['WIN_R'] = 1 - pred['WIN']

#%%
a = pred.groupby('TEAM.1')['WIN'].sum()
b = pred.groupby('TEAM.2')['WIN_R'].sum()

#%%
df.to_csv('errr.csv')


# Numpy是表达N维数组的最基础库
# Python接口使用，C语言实现，计算速度优异
# Python数据分析及科学计算的基础库，支撑Pandas等
# 提供直接的矩阵运算、广播函数、线性代数等功能

# Pandas库提供了简单易用的数据结构和数据分析工具
# 理解数据类型与索引的关系，操作索引即操作数据
# Python最主要的数据分析功能库，基于Numpy开发

# SciPy库是数学、科学和工程计算功能库
# 提供了一批数学算法及工程数据运算功能
# 类似Matlab，可用于如傅里叶变换、信号处理等应用
# Python最主要的科学计算功能库，基于Numpy开发

import numpy
import Pandas
import SciPy

def npSum(l1, l2):
    a = numpy.array(l1)
    b = numpy.array(l2)
    c = a**2 + b**3
    return c

a = [0, 1, 2, 3, 4]
b = [9, 8, 7, 6, 5]
print(npSum(a, b))

Python计算生态概览/数据分析常用库.py


from astropy.table import Table
from astropy.io import fits
import numpy
from time import strftime

import re

def get_file_data(filename):
    """
    Reads a file from the ASAS and returns a list of dictionaries containing all the pairs key-value
    from it. For each dictionary, for the tables, each column is represented as a list on the
    dictionary's key which name matchs the column's name.
    """
    document= open(filename,'r')
    data=[]
    pairs={'ORIGIN':filename.replace('/','\\'),'FRST_MAG':'None'}
    actuald=None #Will be used to save the magnitude's table
    for line in document:
        if line[0]=='#':
            key= re.match(r"#.*=",line)
            if key:
                #It's a key
                key= key.group()[1:-1]
                values= (line.strip()[len(key)+2:]).split(' ')
                values= [x for x in values if x!='']
                values= tuple(values)
                if len(values)==1:
                    pairs[key]= values[0]
                else:
                    pairs[key]= values
                #If a data block has been completely read, add it to the data.
                if actuald!=None:
                    pairs.update(actuald)
                    data.append(pairs)
                    actuald=None
                    pairs={'ORIGIN':filename.replace('/','\\'),'FRST_MAG':'None'}
            else:
                #It's a multiple key description
                keys= re.findall(r"\s\S+",line)
                keys= [x[1:] for x in keys]
                #Check if this are MAGnitudes, if so, add the first one to the dictionary.
                for k in keys:
                    if k[:4]=="MAG_":
                        pairs['FRST_MAG']= k
                        break
                #Restart the current dictionary
                actuald={}
                for k in keys:
                    actuald[k]=[]
        else:
            #Extract the values and append them to the dictionary of multiple keys.
            values= re.findall(r"\S+[\s$]",line)
            values= [x[:-1] for x in values]
            #Try convertion to float, and if it doesn't work, append values as they are is.
            for i in range(len(values)):
                actuald[keys[i]].append(values[i])
    pairs.update(actuald)
    data.append(pairs)
    for dictionary in data:
        for key in dictionary:
            val= dictionary[key]
            #Try the conversion of columns to float
            if type(val)==list:
                try: dictionary[key]= [float(x) for x in val]
                except ValueError as er: pass
    return data

def get_files_data(filenames):
    """
    The same as get_file_data but receibes multiple files.
    """
    dict_list=[]
    for fip in filenames:
        dict_list= dict_list+ get_file_data(fip)
    return dict_list

def save_to_fits(dictionary_list,filename,type_to_format={float:"E",str:"20A"}):
    """
    Saves all the dictionaries of the list onto a FITS file.
    """
    hdus= fits.HDUList()
    #Creating the primary hdu to contain the meta
    primary_hdu = fits.PrimaryHDU()
    primary_hdu.header['history']= "From the ASAS records, parsed by ACALIB ASAS parser the "+str(strftime("%d/%m/%Y")+" (dd/mm/yyyy)")
    hdus.append(primary_hdu)
    #Creating the bintables HDUS for each dictionary
    for dictionary in dictionary_list:
        columns= []
        for key in dictionary:
            val= dictionary[key]
            if type(val)==list:
                #Goes to the table.
                columns.append( fits.Column(name=key, array=val, format=type_to_format[type(val[0])] ))
        column_definitions = fits.ColDefs(columns)
        tbhdu = fits.BinTableHDU.from_columns(column_definitions)
        for key in dictionary:
            val= dictionary[key]
            if not type(val)==list:
                #Goes to the HDU's header
                if type(val)!=tuple:
                    tbhdu.header[key]= val
                else:
                    val= tuple([''.join(x) for x in ' '.join(val).split(';')])
                    tbhdu.header[key]= val
        hdus.append(tbhdu)

    output_file= open(filename,"w")
    hdus.writeto(output_file)
    output_file.close()


import sys
print(sys.version)

def isPrime(number):
    foundFactor = False
    endRange = 1
    if number % 2 == 0:
        endRange = int(number / 2) + 1
    else:
        endRange = int((number + 1) / 2) + 1

    for x in range(2,endRange,1):
        if number % x == 0:
            foundFactor = True
            break

    if foundFactor == True:
        return False
    else:
        return True

def isVoodooPrime(number):
    foundNumber = False
    if isPrime(number) == True:
        primeString = str(1 / number)
        print(primeString)
        if primeString.find(str(number)) >= 0:
            return True
        else:
            return False
    else:
        return False

for i in range(1,30,1):
    print(str(i) + ":")
    print(isVoodooPrime(i))


# coding: UTF-8
"""
Script: Prog1/damier
"""

# Imports
import turtle

# Fonctions
def carre(x, y, taille, couleur):
    turtle.speed(10)
    turtle.up()
    turtle.goto(x, y)
    turtle.down()
    turtle.pencolor(couleur)
    turtle.fillcolor(couleur)
    turtle.begin_fill()
    for i in range(4):
        turtle.forward(taille)
        turtle.right(90)
        i += 1
    turtle.end_fill()
    turtle.up()
    return None

def ligne(x, y, taille, couleurs):
    turtle.speed(10)
    turtle.up()
    turtle.goto(x, y)
    turtle.down()
    for i in range(5):
        carre(x, y, taille, couleurs[0])
        turtle.forward(taille)
        x += taille
        carre(x, y, taille, couleurs[1])
        x += taille
    return None

def deux_lignes(x, y, taille, couleurs):
    turtle.speed(10)
    turtle.up()
    turtle.goto(x, y)
    turtle.down()
    x2 = x
    y2 = y - taille
    ligne(x, y, taille, couleurs)
    for i in range(5):
        carre(x2, y2, taille, couleurs[1])
        turtle.forward(taille)
        x2 += taille
        carre(x2, y2, taille, couleurs[0])
        x2 += taille
    return None

def damier(x, y, taille, couleurs):
    turtle.speed(10)
    for i in range(5):
        deux_lignes(x, y, taille, couleurs)
        y = y - taille * 2
    return None

# Programme principal
def main():
    damier(-200, 100, 25, ['pink', 'green'])
    turtle.exitonclick()

if __name__ == '__main__':
    main()
# Fin


## Classification Tree on Balanced Dataset
## CART Model on IRIS Data
## Three species of iris flower setosa, versicolor, virginica
from sklearn.datasets import load_iris

iris = load_iris()

import pydotplus

## Get the Features and Labels
print(iris.feature_names)
print(iris.target_names)
print(iris.target)

type(iris.data)
print(iris.data)

X = iris.data
y = iris.target

type(X)
type(y)
# create training and testing vars
from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(
        X,y,test_size=50,
        random_state = 42)

print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)

import numpy as np
y_freq = np.bincount(y_train)
y_val = np.nonzero(y_freq)[0]
np.vstack((y_val,y_freq[y_val])).T

## Building the Classifier
## Importing the Classification Tree Package
from sklearn import tree

## Creating the Classifier Object
clf = tree.DecisionTreeClassifier()

## Fitting the Classification Tree on Training Dataset
clf = clf.fit(X_train, y_train)

## Visualize the tree

import io
from sklearn.tree import export_graphviz
from scipy import misc
from matplotlib import pyplot as plt
f = io.StringIO()
dot_data = export_graphviz(clf, out_file = f,
                feature_names=iris.feature_names)
pydot_graph = pydotplus.graph_from_dot_data(f.getvalue())
import graphviz
## The below command requires graphviz to be installed on your machine
## Path setting be done to graphviz bin folder
## Link to download graphviz
## https://graphviz.gitlab.io/_pages/Download/Download_windows.html
pydot_graph.write_png("decision_tree.png")
img = misc.imread("decision_tree.png")
plt.rcParams["figure.figsize"]=(20,20)
plt.imshow(img)

##f.getvalue()
#dot_data = tree.export_graphviz(clf,out_file = None)
#graph = graphviz.Source(dot_data)
#graph.render("iris")

## Predict based on the Classifier
pred_y_test = clf.predict(X_test)
pred_y_test

## Let us see the classification accuracy of our model
from sklearn.metrics import accuracy_score
score = accuracy_score(y_test, pred_y_test)
score

from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, pred_y_test)

"""
Created on Sun Aug  6 23:42:04 2017
@author: Rajesh Jakhotia
@Company: K2 Analytics Finishing School Pvt. Ltd
@Email: ar.jakhotia@k2analytics.co.in
@Website: k2analytics.co.in
"""
## Classification Tree on Unbalanced Dataset
import os
import pandas as pd

#Set the working directory
#os.chdir("D:\K2Analytics\Datafile")

#Load the Dataset
CTDF_dev = pd.read_csv("DEV_SAMPLE.csv")
CTDF_holdout = pd.read_csv("HOLDOUT_SAMPLE.csv")

print( len(CTDF_dev),  len(CTDF_holdout))
CTDF_dev.head()

import numpy as np
import matplotlib.pyplot as plt

#Data Preprocessing
#Splitting into features and response variables
X =  CTDF_dev[['Age', 'Gender', 'Balance', 'Occupation',
               'No_OF_CR_TXNS', 'AGE_BKT', 'SCR', 'Holding_Period']]

#Categorical Variable to Numerical Variables
X_train = pd.get_dummies(X)
X_train.columns

y_train = CTDF_dev["Target"]

print (type(X_train) , type(y_train))

#Decision Tree
#Loading the library
from sklearn.tree import DecisionTreeClassifier

#Setting the parameter
clf = DecisionTreeClassifier(criterion = "gini" ,
                             min_samples_split = 100,
                             min_samples_leaf = 10,
                             max_depth = 50)

#Calling the fit function to built the tree
clf.fit(X_train,y_train)

import pydot
from sklearn.tree import tree
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
dot_data = StringIO()
feature_list = list(X_train.columns.values)
export_graphviz(clf,
                out_file = dot_data,
                feature_names = feature_list)
graph=pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf(".\classification_tree_output.pdf")

Nodes = pd.DataFrame(clf.tree_.__getstate__()["nodes"])
Nodes

feature_importance = pd.DataFrame([X_train.columns,
                               clf.tree_.compute_feature_importances()])
feature_importance.T

## Let us see how good is the model
pred_y_train = clf.predict(X_train )
pred_y_train

## Let us see the classification accuracy of our model
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
score = accuracy_score(y_train, pred_y_train)
score

y_train_prob = clf.predict_proba(X_train)
## AUC
fpr, tpr, thresholds = roc_curve(y_train, y_train_prob[:,1])
auc(fpr, tpr)

## Let us see how good is the model
X_holdout =  CTDF_holdout[['Age', 'Gender', 'Balance', 'Occupation',
               'No_OF_CR_TXNS', 'AGE_BKT', 'SCR', 'Holding_Period']]
X_test = pd.get_dummies(X_holdout)
y_test = CTDF_holdout["Target"]

pred_y_test = clf.predict(X_test)
score_h = accuracy_score(y_test, pred_y_test)
score_h

y_test_prob = clf.predict_proba(X_test)
fpr, tpr, thresholds = roc_curve(y_test, y_test_prob[:,1])
auc(fpr, tpr)

y_freq = np.bincount(y_train)
y_val = np.nonzero(y_freq)[0]
np.vstack((y_val,y_freq[y_val])).T

#Cross validation function
from sklearn.cross_validation import cross_val_score
scores = cross_val_score(clf, X_train , y_train, cv = 10, scoring='roc_auc')
scores.mean()
scores.std()

y_train_prob = clf.predict_proba(X_train)
fpr, tpr, thresholds = roc_curve(y_train, y_train_prob[:,1])
auc(fpr, tpr)

y_test_prob = clf.predict_proba(X_test)
fpr, tpr, thresholds = roc_curve(y_test, y_test_prob[:,1])
auc(fpr, tpr)

## Tuning the Classifier using GridSearchCV
from sklearn.grid_search import GridSearchCV
help(GridSearchCV)

param_dist = {"criterion": ["gini","entropy"],
              "max_depth": np.arange(3,10),
              }

tree = DecisionTreeClassifier(min_samples_split = 100,
                             min_samples_leaf = 10)

tree_cv  = GridSearchCV(tree, param_dist, cv = 10,
                        scoring = 'roc_auc', verbose = 100)

tree_cv.fit(X_train,y_train)

## Building the model using best combination of parameters
print("Tuned Decision Tree parameter : {}".format(tree_cv.best_params_))

classifier = tree_cv.best_estimator_

classifier.fit(X_train,y_train)

#predicting probabilities
y_train_prob = classifier.predict_proba(X_train)
fpr, tpr, thresholds = roc_curve(y_train, y_train_prob[:,1])
auc_d = auc(fpr, tpr)
auc_d
y_test_prob = classifier.predict_proba(X_test)
fpr, tpr, thresholds = roc_curve(y_test, y_test_prob[:,1])
auc_h = auc(fpr, tpr)
auc_h

## Rank Ordering
Prediction = classifier.predict_proba(X_train)
CTDF_dev["prob_score"] = Prediction[:,1]

#scoring step
#decile code
def deciles(x):
    decile = pd.Series(index=[0,1,2,3,4,5,6,7,8,9])
    for i in np.arange(0.1,1.1,0.1):
        decile[int(i*10)]=x.quantile(i)
    def z(x):
        if x<decile[1]: return(1)
        elif x<decile[2]: return(2)
        elif x<decile[3]: return(3)
        elif x<decile[4]: return(4)
        elif x<decile[5]: return(5)
        elif x<decile[6]: return(6)
        elif x<decile[7]: return(7)
        elif x<decile[8]: return(8)
        elif x<decile[9]: return(9)
        elif x<=decile[10]: return(10)
        else:return(np.NaN)
    s=x.map(z)
    return(s)

def Rank_Ordering(X,y,Target):
    X['decile']=deciles(X[y])
    Rank=X.groupby('decile').apply(lambda x: pd.Series([
        np.min(x[y]),
        np.max(x[y]),
        np.mean(x[y]),
        np.size(x[y]),
        np.sum(x[Target]),
        np.size(x[Target][x[Target]==0]),
        ],
        index=(["min_resp","max_resp","avg_resp",
                "cnt","cnt_resp","cnt_non_resp"])
        )).reset_index()
    Rank = Rank.sort_values(by='decile',ascending=False)
    Rank["rrate"] = Rank["cnt_resp"]*100/Rank["cnt"]
    Rank["cum_resp"] = np.cumsum(Rank["cnt_resp"])
    Rank["cum_non_resp"] = np.cumsum(Rank["cnt_non_resp"])
    Rank["cum_resp_pct"] = Rank["cum_resp"]/np.sum(Rank["cnt_resp"])
    Rank["cum_non_resp_pct"]=Rank["cum_non_resp"]/np.sum(Rank["cnt_non_resp"])
    Rank["KS"] = Rank["cum_resp_pct"] - Rank["cum_non_resp_pct"]
    Rank
    return(Rank)

Rank = Rank_Ordering(CTDF_dev["prob_score"],"Target")
Rank

## Let us see the Rank Ordering on Hold-Out Dataset
Prediction_h = classifier.predict_proba(X_test)
CTDF_holdout["prob_score"] = Prediction_h[:,1]

Rank_h = Rank_Ordering(CTDF_holdout["prob_score"],"Target")
Rank_h

y_freq = np.bincount(y_test)
y_val = np.nonzero(y_freq)[0]
np.vstack((y_val,y_freq[y_val])).T


import logging
import random
import time
from concurrent import futures

import grpc
import pandas as pd

import Assignment1_pb2
import Assignment1_pb2_grpc

class RedditServer(Assignment1_pb2_grpc.RedditServicer):

    def __init__(self):
        self.reddit_posts = pd.read_csv("r_dataisbeautiful_posts.csv", encoding="latin")

    def RequestRedditPosts(self, request, context):
        print("Received a request for %d reddit posts" % request.number_of_reddit_posts, flush=True)

        for _ in range(request.number_of_reddit_posts):
            random_post = self.reddit_posts.iloc[random.randint(1, self.reddit_posts.shape[0])]

            response = Assignment1_pb2.RedditPostReply(
                id=str(random_post[0]),
                title=str(random_post[1]),
                score=str(random_post[2]),
                author=str(random_post[3]),
                original_date=str(random_post[8]),
                full_link=str(random_post[9]),
                over_18=str(random_post[11])
            )

            time.sleep(1 / random.uniform(0.7, 3))  # Sleep for random amount, so number of reddit posts is anywhere between 1 and 3 per second, with mostly 2 per second

            yield response

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    Assignment1_pb2_grpc.add_RedditServicer_to_server(RedditServer(), server)
    server.add_insecure_port('[::]:50052')
    server.start()
    server.wait_for_termination()

if __name__ == '__main__':
    logging.basicConfig()
    serve()


import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tokenizer.settings")

import django
django.setup()

from django.contrib.auth.models import User, Group
from editor import models

import random

# create groups
annotatorGroup = Group.objects.get(name='annotator')
adjudicatorGroup = Group.objects.get(name='adjudicator')

# # create users
annotators = {'zhou':'zhou', 'shaodian':'shaodian', 'yue':'yue', 'handong':'handong', 'nanfang':'nanfang',
                 'junyan':'junyan', 'nina':'nina', 'jinsen':'jinsen', 'ying':'ying'}
adjudicators = {'nanfang':'nanfang', 'shaodian':'shaodian'}

# for username in annotators:
#     user = User.objects.create_user(username, '', annotators[username])
#     user.save()
#     annotatorGroup.user_set.add(user)
# annotatorGroup.save()

# for username in adjudicators:
#     user = User.objects.get(username=username)
#     adjudicatorGroup.user_set.add(user)
# adjudicatorGroup.save()

# assign tasks
statusNA = models.Status.objects.get(pk=1)
statusIN = models.Status.objects.get(pk=2)
defaultUser = User.objects.get(pk=1)

numAssigned = {}
userSet = annotatorGroup.user_set

tempSet = set()
for user in userSet.all():
    tempSet.add(user)

for i in range(6,51):
    filename = "A 00 ("+str(i)+").txt"
    firstWorker = ""
    secondWorker = ""
    print(i)
    if len(tempSet) <= 2:
        print(tempSet)
        break
    while True:
        samples = random.sample(tempSet,2)
        firstWorker = samples[0]
        secondWorker = samples[1]
        print(firstWorker.id, secondWorker.id)
        if (adjudicatorGroup.user_set.count() == 2):
            if (firstWorker.groups.filter(name='adjudicator').exists() and secondWorker.groups.filter(name='adjudicator').exists()):
                continue
        if (firstWorker.id != secondWorker.id):
            break
    #f = models.TextFile()
    #f.file_name = filename
    f = models.TextFile.objects.get(file_name=filename)
    f.first_worker = firstWorker
    f.second_worker = secondWorker

    if firstWorker.id in numAssigned:
        numAssigned[firstWorker.id] += 1
    else:
        numAssigned[firstWorker.id] = 1

    if secondWorker.id in numAssigned:
        numAssigned[secondWorker.id] += 1
    else:
        numAssigned[secondWorker.id] = 1

    if (firstWorker.id in numAssigned) and (numAssigned[firstWorker.id] == 10):
        tempSet.remove(firstWorker)
    if (secondWorker.id in numAssigned) and (numAssigned[secondWorker.id] == 10):
        tempSet.remove(secondWorker)

    f.first_worker_comment = "请在此处留下评论。有任何不确定的疑问也请在此处留下。".encode('UTF-8')
    f.second_worker_comment = "请在此处留下评论。有任何不确定的疑问也请在此处留下。".encode('UTF-8')
    f.first_worker_state = statusIN
    f.second_worker_state = statusIN
    f.judge = defaultUser
    f.jurisdiction_state = statusNA
    f.save()

if i==50:
    print('initialization success.')
else:
    print('failed on ' + str(i) + ' See above user set for details in the database.')


#!/usr/bin/env python3

#The following iterative sequence is defined for the set of positive integers:

#n → n/2 (n is even)
#n → 3n + 1 (n is odd)

#Using the rule above and starting with 13, we generate the following sequence:

#13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
#It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

#Which starting number, under one million, produces the longest chain?

#NOTE: Once the chain starts the terms are allowed to go above one million.

cache = {}
def collatz(n):
    if n in cache:
        return cache[n]

    tmp = n
    counter = 1
    while tmp % 2 == 0:
        tmp //= 2
        counter += 1
    if tmp > 1:
        counter += collatz(3*tmp + 1)
    cache[n] = counter
    return counter

ans = 0
max_len = 0
for i in range(1, 1000000):
    tmp = collatz(i)
    if tmp > max_len:
        (ans, max_len) = (i, tmp)

print(ans)


import os

import pygame as pg
from pygame.locals import *

import agent, event

color = {
    'gray': (100, 100, 100),
    'white': (255, 255, 255),
    'light_green': (20, 60, 20),
    'danger': (150, 50, 50)
}

help = """
    CMPSC360 Project GoldMiner Demo
    *** I: Intelligent agent mode
    *** V: View what state it is in each cell
    *** R: Reset the board
    *** H: Display help information
    *** Q: Quit the game
    *** Enter: Next step
    *** ->: turn left
    *** <-: turn right
   Press H to hide the help information

   Have fun \(^o^)/\(^o^)/\(^o^)/
"""

status_font = (os.path.join('helpInfoFont', 'Pacifico.ttf'), 22)

def load_image(name):
    fullname = os.path.join('images', name)
    image = pg.image.load(fullname)
    image = image.convert()
    image.set_colorkey(image.get_at((0, 0)), RLEACCEL)
    return image, image.get_rect()

class MainFrame:

    def __init__(self, event_controller):
        self.event_controller = event_controller
        self.event_controller.register_listener(self)

        pg.init()
        self.screen = pg.display.set_mode((769, 920), pg.RESIZABLE)
        pg.display.set_caption('GoldMiner (360 project game)')
        self.background = pg.Surface(self.screen.get_size())
        self.background.convert()
        self.background.fill(color["white"])

        self.screen.blit(self.background, (0, 0))
        pg.display.update()

        self.back_sprites = pg.sprite.RenderUpdates()
        self.front_sprites = pg.sprite.RenderUpdates()

        self.sectors = {}
        self.player_sector = None
        self.view_all = False

        self.help_display = HelpDisplay()
        self.help_display.rect.center = self.background.get_rect().center

    def app_start(self):
        dx, dy = (192, 192)
        x, y = (3, 4)
        rect = pg.Rect(2 + 192 * 3, -192 + 2, 188, 188)
        for count in xrange(16):
            if count % 4 == 0:
                x -= 3
                y -= 1
                rect = rect.move(-3 * dx, dy)
            else:
                x += 1
                rect = rect.move(dx, 0)
            new_sector = Sector(self.back_sprites)
            new_sector.index = (x, y)
            new_sector.rect = rect
            self.sectors[(x, y)] = new_sector

        self.player = Player()

        ev = event.GenerateRequest()
        self.event_controller.post(ev)

    def player_moveto(self, pos):
        self.player_sector = self.sectors[pos]
        self.player.moveto = self.player_sector.rect.center

    def help(self):
        if not self.front_sprites.has(self.help_display):
            self.help_display.add(self.front_sprites)
        else:
            self.help_display.remove(self.front_sprites)

    def player_forward(self, ev):
        if not self.front_sprites.has(self.player):
            self.player.add(self.front_sprites)
            self.player.update_facing(agent.facing_list['right'])
        self.player_moveto(ev.pos)
        self.sectors[ev.pos].visit()
        self.front_sprites.update()
        self.redraw()

    def player_turn(self, ev):
        self.player.update_facing(ev.facing)

    def found_danger(self, ev):
        self.sectors[ev.pos].set_danger()

    def view(self, ev):
        if self.view_all:
            self.view_all = False
        else:
            self.view_all = True

        for s in self.sectors.values():
            if not s.visited:
                s.view(self.view_all)

    def world_built(self, ev):
        for key, sector in self.sectors.items():
            item = ev.world[key]
            for x in range(5):
                if item[x] == 1:
                    thing = agent.map_list[x].lower()
                    sector.things.append(thing)

    def reset_world(self, ev):
        for sector in self.sectors.values():
            sector.visited = False
            sector.danger = False
            sector.image.fill(color['gray'])
            sector.things = []
        self.player.remove(self.front_sprites)

        ev = event.GenerateRequest()
        self.event_controller.post(ev)

    def redraw(self):
        # Draw everything
        self.back_sprites.clear(self.screen, self.background)
        self.front_sprites.clear(self.screen, self.background)

        self.back_sprites.update()
        self.front_sprites.update()

        dirty_rects1 = self.back_sprites.draw(self.screen)
        dirty_rects2 = self.front_sprites.draw(self.screen)

        dirty_rects = dirty_rects1 + dirty_rects2
        pg.display.update(dirty_rects)

    def notify(self, ev):
        if isinstance(ev, event.Tick):
            self.redraw()
        elif isinstance(ev, event.AppStart):
            self.app_start()
        elif isinstance(ev, event.Reset):
            self.reset_world(ev)
        elif isinstance(ev, event.WorldBuilt):
            self.world_built(ev)
        elif isinstance(ev, event.PlayerForward):
            self.player_forward(ev)
        elif isinstance(ev, event.PlayerTurn):
            self.player_turn(ev)

        elif isinstance(ev, event.View):
            self.view(ev)
        elif isinstance(ev, event.Help):
            self.help(ev)
        elif isinstance(ev, event.FoundDanger):
            self.found_danger(ev)

class HelpDisplay(pg.sprite.Sprite):
    """Help information"""

    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((500, 580))
        self.image.set_alpha(255 * 0.6)
        self.image.fill(color['white'])
        self.rect = self.image.get_rect()
        self.text = help

        self.draw_text()

    def draw_text(self):
        fo = pg.font.Font(*status_font)

        prevpos = None
        for line in self.text.split('\n'):
            textr = fo.render(line, 1, color['danger'])
            textrpos = textr.get_rect()
            textrpos.left = self.image.get_rect().left
            if prevpos:
                textrpos.top = prevpos.bottom
            else:
                textrpos.top = self.image.get_rect().top
            prevpos = textrpos
            self.image.blit(textr, textrpos)

class Sector(pg.sprite.Sprite):

    def __init__(self, group=None):
        pg.sprite.Sprite.__init__(self, group)
        self.image = pg.Surface((180, 180))
        self.image.fill(color['gray'])

        self.index = None
        self.visited = False
        self.danger = False
        self.view = False
        self.things = []

    def draw_things(self):
        if self.danger:
            self.image.fill(color['danger'])
        for t in self.things:
            self.draw_img(t)

    def clear_things(self):
        if self.danger:
            self.image.fill(color['danger'])
        else:
            self.image.fill(color['gray'])

    def view(self, view_flag):
        self.view = view_flag
        if view_flag:
            self.draw_things()
        else:
            self.clear_things()

    def set_danger(self):
        if not self.danger:
            self.danger = True
            self.image.fill(color['danger'])
            if self.view:
                self.draw_things()

    def draw_img(self, s):
        image, rect = load_image('%s.png' % s)
        rect.center = self.image.get_rect().center

        self.image.blit(image, rect)

    def visit(self):
        if not self.visited:
            self.visited = True
            self.image.fill((180, 180, 180))
            self.draw_things()

    def update(self):
        pass

class Player(pg.sprite.Sprite):

    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((30, 30))
        self.image.fill((180, 180, 180))
        self.rect = self.image.get_rect()

        self.moveto = None
        self.facing = None

    def update_facing(self, facing=None):

        def draw_facing(image, rect):
            if self.facing == 0:
                rect.midtop = self.image.get_rect().midtop
            elif self.facing == 1:
                rect.midright = self.image.get_rect().midright
            elif self.facing == 2:
                rect.midbottom = self.image.get_rect().midbottom
            elif self.facing == 3:
                rect.midleft = self.image.get_rect().midleft
            self.image.blit(image, rect)

        # clear the old facing line
        if self.facing is not None:
            image = pg.Surface((30, 30))
            image.fill((180, 180, 180))
            rect = image.get_rect()
            draw_facing(image, rect)
        self.facing = facing
        image, rect = load_image('facing_%s.png' % self.facing)
        draw_facing(image, rect)

    def update(self):
        if self.moveto:
            self.rect.center = self.moveto
            self.moveto = None


from pymongo import *
import data.Pipelines as pl
from matplotlib import pyplot as plt
import re as r
import operator
import numpy as np
import pandas as pd
import seaborn as sns
import data.constants as con
import os

# sns.set(rc={'figure.figsize': (21.7, 11.00)})

def ensure_dir(file_path):
    directory = os.path.dirname(file_path)
    if not os.path.exists(directory):
        os.makedirs(directory)

pd.set_option('display.width', 1000)
db = MongoClient('mongodb://localhost:27017/').LeagueCrawler
threshold = 0.1

for _id in con.ADC_CHAMPIONS_DICT.keys():
    itemsets = db.itemsets_adc_pro.find({"championId": _id})
    champion = con.ADC_CHAMPIONS_DICT[_id]
    path = "Viz_Pro/" + champion + "/"
    PATCHES = ["7.1-7.3", "7.4-7.8", "7.9-7.13", "7.14-7.18"]

    ensure_dir(path)

    itemsets: pd.DataFrame = pd.DataFrame(list(itemsets))

    # itemsets["patch"] = itemsets["gameVersion"].apply(lambda row: r.match("\d.\d+", row).group(0))
    itemsets["patch_grp"] = np.where(itemsets['patch'].isin(["7.1", "7.2", "7.3"]), PATCHES[0],
                                     np.where(itemsets['patch'].isin(["7.5", "7.6", "7.7", "7.8"]), PATCHES[1],
                                              np.where(itemsets['patch'].isin(["7.9", "7,10", "7.11", "7.12", "7.13"]),
                                                       PATCHES[2],
                                                       np.where(itemsets['patch'].isin(["7.14", "7.15", "7.16", "7.17",
                                                                                "7.18"]), PATCHES[3], "none")
                                                       )
                                              )
                                     )
    itemsets["core_items"] = \
        itemsets.apply(lambda row:
                       tuple(map(
                           lambda x: con.ADC_TIER_3_ITEMS_DICT[x],
                           filter(lambda x: x in con.ADC_TIER_3_ITEMS_LIST, row["items"])
                       )),
                       axis=1)
    itemsets["num_core_items"] = itemsets.apply(lambda row: len(row['core_items']), axis=1)
    itemsets["two_core_items"] = itemsets["core_items"].apply(lambda x: True if len(x) >= 2 else False)
    itemsets["core_items_2"] = itemsets["core_items"].apply(lambda x: x[:2] if len(x) >= 2 else None)
    itemsets["three_core_items"] = itemsets["core_items"].apply(lambda x: True if len(x) >= 3 else False)
    itemsets["core_items_3"] = itemsets["core_items"].apply(lambda x: x[:3] if len(x) >= 3 else None)
    itemsets["four_core_items"] = itemsets["core_items"].apply(lambda x: True if len(x) >= 4 else False)
    itemsets["core_items_4"] = itemsets["core_items"].apply(lambda x: x[:4] if len(x) >= 4 else None)

    df = itemsets

    gamesCount_2_items = df[df['two_core_items']].groupby(['championId', 'patch_grp']).size().reset_index(
        name='gamesPlayed_2')
    gamesCount_3_items = df[df['three_core_items']].groupby(['championId', 'patch_grp']).size().reset_index(
        name='gamesPlayed_3')
    gamesCount_4_items = df[df['four_core_items']].groupby(['championId', 'patch_grp']).size().reset_index(
        name='gamesPlayed_4')

    # First two items
    result_2_items = df[df["num_core_items"] >= 2] \
        .groupby(["championId", "patch_grp", "core_items_2"]).core_items_2 \
        .count() \
        .reset_index(name="2_items_count")

    result_2 = pd.merge(result_2_items, gamesCount_2_items, on=['championId', 'patch_grp'])
    result_2['freq'] = result_2['2_items_count'] / result_2['gamesPlayed_2']
    num_games_2 = sum(itemsets["two_core_items"])

    # sns.set(font_scale=0.6)
    df2 = result_2[(result_2['freq'] > threshold)][['patch_grp', 'core_items_2', 'freq']]
    g2, ax = plt.subplots(figsize=(22, 10))
    g2 = sns.FacetGrid(df2,
                       col='patch_grp', col_wrap=5, col_order=PATCHES)
    g2 = (g2.map(sns.barplot, 'freq', 'core_items_2',  # 'platformId', hue_order=["EUW1", "NA1", "BR1", "KR"],
                 # palette=sns.color_palette("pastel"),
                 order=sorted(df2.core_items_2.unique(), key=operator.itemgetter(0))).add_legend())
    # g2.fig.subplots_adjust(top=0.9)
    g2.fig.suptitle(champion + "'s Itemsets - Games: " + str(num_games_2)+", Threshold:" + str(threshold), fontsize=20)
    g2.fig.tight_layout()
    g2.savefig(path + "2_items.svg")

    # alternatively:
    # g2 = sb.factorplot('freq', 'core_items_2', 'platformId',
    #                    data=result_2[(result_2['freq'] > 0.02)][['platformId', 'patch_grp', 'core_items_2', 'freq']],
    #                    kind='bar',
    #                    col='patch_grp',
    #                    col_wrap=5,
    #                    col_order=PATCHES,
    #                    palette=sb.color_palette("pastel"))

    # First three items
    result_3_items = df[df["num_core_items"] >= 3] \
        .groupby(["championId", "patch_grp", "core_items_3"]).core_items_3 \
        .count() \
        .reset_index(name="2_items_count")

    result_3 = pd.merge(result_3_items, gamesCount_3_items, on=['championId', 'patch_grp'])
    result_3['freq'] = result_3['2_items_count'] / result_3['gamesPlayed_3']
    num_games_3 = sum(itemsets["three_core_items"])

    # sns.set(font_scale=1.3)
    df3 = result_3[(result_3['freq'] > threshold)][['patch_grp', 'core_items_3', 'freq']]
    g3, ax = plt.subplots(figsize=(22, 10))
    g3 = sns.FacetGrid(df3,
                       col='patch_grp', col_wrap=5, col_order=PATCHES)
    g3 = (g3.map(sns.barplot, 'freq', 'core_items_3',  # 'platformId', hue_order=["EUW1", "NA1", "BR1", "KR"],
                 # palette=sns.color_palette("pastel"),
                 order=sorted(df3.core_items_3.unique(), key=operator.itemgetter(0))
                 ).add_legend())
    # g3.fig.subplots_adjust(top=0.9)
    g3.fig.suptitle(champion + " Itemsets - Games: " + str(num_games_3)+", Threshold:" + str(threshold), fontsize=20)
    g3.fig.tight_layout()
    g3.savefig(path + "3_items.svg")

    # First four items
    result_4_items = df[df["num_core_items"] >= 4] \
        .groupby(["championId", "patch_grp", "core_items_4"]).core_items_4 \
        .count() \
        .reset_index(name="4_items_count")

    result_4 = pd.merge(result_4_items, gamesCount_4_items, on=['championId', 'patch_grp'])
    result_4['freq'] = result_4['4_items_count'] / result_4['gamesPlayed_4']
    num_games_4 = sum(itemsets["four_core_items"])

    sns.set(font_scale=0.6)
    df4 = result_4[(result_4['freq'] > threshold)][['patch_grp', 'core_items_4', 'freq']]
    g4, ax = plt.subplots(figsize=(21.7, 11.00))
    g4 = sns.FacetGrid(df4,
                       col='patch_grp', col_wrap=5, col_order=PATCHES)
    g4 = (g4.map(sns.barplot, 'freq', 'core_items_4',  # 'platformId', hue_order=["EUW1", "NA1", "BR1", "KR"],
                 # palette=sns.color_palette("pastel"),
                 order=sorted(df4.core_items_4.unique(), key=operator.itemgetter(0))).add_legend())
    g4.fig.subplots_adjust(top=0.9)
    # g4.fig.suptitle(champion + "'s Itemsets - Games: " + str(num_games_4)+", Threshold:" + str(threshold), fontsize=20)
    # g4.tight_layout()
    g4.savefig(path + "4_items.svg")


#!/usr/bin/env python

#create a 3 line file
#Write a python program that reads this file and prints out the lines of t    he file
#write a python program that writes a different file
#close this new file and re-open it and append to it.
#Check your code into GitHub

myfile = open("readme.txt", "r")
contents = myfile.readlines()
print (contents)
myfile.close()

myfile2 = open("create2.txt", "w")
myfile2.write("Sienna Rocks\n")
myfile2.close()

append_file = open("create2.txt", "a")
append_file.write("appended")
append_file.close()


# topics = ["动态规划"]

from typing import List

class Solution:
    """
    0-1 背包(二维)
    """

    def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
        # dp[i][j] 表示不超过 i 个 0 和 j 个 1 的最长子集
        dp: List[List[int]] = [[0] * (n + 1) for _ in range(m + 1)]

        for k in range(len(strs)):
            c0 = strs[k].count('0')
            c1 = strs[k].count('1')
            for i in range(m + 1)[::-1]:
                for j in range(n + 1)[::-1]:
                    if k == 0:
                        dp[i][j] = 0 if (c0 > i or c1 > j) else 1
                    elif i >= c0 and j >= c1:
                        dp[i][j] = max(dp[i][j], dp[i - c0][j - c1] + 1)

        return dp[m][n]


"""
Given an array of size N containing only 0s, 1s, and 2s;
 sort the array in ascending order.
Example 1:
Input:
N = 5
arr[]= {0 2 1 2 0}
Output:
0 0 1 2 2
Explanation:
0s 1s and 2s are segregated
into ascending order.
"""

def sort012(arr, n):
    c0 = 0
    c1 = 0
    c2 = 0
    for ele in arr:
        if ele == 0:
            c0 += 1
        elif ele == 1:
            c1 += 1
        else:
            c2 += 1
    i = 0
    while c0 != 0:
        arr[i] = 0
        i += 1
        c0 -= 1
    while c1 != 0:
        arr[i] = 1
        i += 1
        c1 -= 1
    while c2 != 0:
        arr[i] = 2
        i += 1
        c2 -= 1
    print(arr)

def sort012_opti(arr, n):
    start = 0
    mid = 0
    end = n - 1

    while mid <= end:
        if arr[mid] == 0:
            arr[start], arr[mid] = arr[mid], arr[start]
            start = start + 1
            mid = mid + 1
        elif arr[mid] == 1:
            mid = mid + 1
        else:
            arr[mid], arr[end] = arr[end], arr[mid]
            end = end - 1
    print(arr)

sort012_opti([0, 1, 1, 2, 0, 1, 1], 7)


# -*- coding: utf-8 -*-
"""
Created on Sun Dec  1 17:28:04 2019

@author: YUSS
"""

# -*- coding: utf-8 -*-
"""
Created on 27/5/2019
Interceptor V2
@author: I.
This ML challenge was created by SAMLA (National Electronic Warfare, Cyber & Intelligence Research Center) - a national research lab at Rafael http://portal.rafael.co.il/mlchallenge2019/Documents/samla.html

The goal of the game:
    Getting highest score in 100 games each running for 1000 steps.
    The player have access to 3 functions:

        Init(): This function initializes the game. It should be called before each game.

        Game_step(action_button): This function performs an action as described:
            action_button = 0: Change turret angle one step left
            action_button = 1: Do nothing in the current game step
            action_button = 2: Change turret angle one step right
            action_button = 3: Fire

            This function returns several variables:
                r_locs: Location of each rocket in the game (x,y)
                i_locs: Location of each interceptor in the game (x,y)
                c_locs: Location of each city in the game (x, width)
                ang: Turret angle
                score: Current player score

        Draw(): This function displays current game state (slows down your program. Not a must)

    Score is as follows:
        Rocket hits city: -15 points
        Rocket hits open field: -1 point
        Firing an interceptor: -1 point
        Intercepting a rocket: +4 points

In order to play, do the following:
***********************************

from Interceptor_V2 import Init, Draw, Game_step

Init()
for stp in range(1000):
    action_button = *** Insert your logic here: 0,1,2 or 3 ***
    r_locs, i_locs, c_locs, ang, score = Game_step(action_button)
    Draw()

*************************************

Don't forget to play by the rules described in the website.
"""

import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
from mrcnn.config import Config
from mrcnn.model import MaskRCNN

class RocketConfig(Config):
    NAME = "rocket"
    IMAGES_PER_GPU = 1
    NUM_CLASSES = 2
#    STEPS_PER_EPOCH = 1000

config = RocketConfig()
config.IMAGE_SHAPE = [192, 384, 3]
config.IMAGE_MIN_DIM = 192
config.IMAGE_MAX_DIM = 384
config.IMAGE_RESIZE_MODE = 'none'
config.USE_MINI_MASK = False
config.display()

model = MaskRCNN(mode='inference', model_dir='./', config=config)
model.load_weights('./mask_rcnn_rocket_0070.h5', by_name=True)

class World():
    width = 10000 # [m]
    height = 4000 # [m]
    dt = 0.2 # [sec]
    time = 0 # [sec]
    score = 0
    reward_city = -15
    reward_open = -1
    reward_fire = -1
    reward_intercept = 4
    g = 9.8 # Gravity [m/sec**2]
    fric = 5e-7 # Air friction [Units of Science]
    rocket_prob = 1 # expected rockets per sec

class Turret():
    x = -2000 # [m]
    y = 0 # [m]
    x_hostile = 4800
    y_hostile = 0
    ang_vel = 30 # Turret angular speed [deg/sec]
    ang = 0 # Turret angle [deg]
    v0 = 800 # Initial speed [m/sec]
    prox_radius = 150 # detonation proximity radius [m]
    reload_time = 1.5 # [sec]
    last_shot_time = -3 # [sec]

    def update(self, action_button):
        if action_button == 0:
            self.ang = self.ang - self.ang_vel * world.dt
            if self.ang < -90: self.ang = -90

        if action_button == 1:
            pass

        if action_button == 2:
            self.ang = self.ang + self.ang_vel * world.dt
            if self.ang > 90: self.ang = 90

        if action_button == 3:
            if world.time - self.last_shot_time > self.reload_time:
                Interceptor()
                self.last_shot_time = world.time # [sec]

class Interceptor():
    def __init__(self):
        self.x = turret.x
        self.y = turret.y
        self.vx = turret.v0 * np.sin(np.deg2rad(turret.ang))
        self.vy = turret.v0 * np.cos(np.deg2rad(turret.ang))
        world.score = world.score + world.reward_fire
        interceptor_list.append(self)

    def update(self):
        self.v_loss = (self.vx ** 2 + self.vy ** 2) * world.fric * world.dt
        self.vx = self.vx * (1 - self.v_loss)
        self.vy = self.vy * (1 - self.v_loss) - world.g * world.dt
        self.x = self.x + self.vx * world.dt
        self.y = self.y + self.vy * world.dt
        if self.y < 0:
            Explosion(self.x, self.y)
            interceptor_list.remove(self)
        if np.abs(self.x) > world.width / 2:
            interceptor_list.remove(self)

class Rocket():
    def __init__(self, world):
        self.x = turret.x_hostile # [m]
        self.y = turret.y_hostile # [m]
        self.v0 = 700 + np.random.rand() * 300 # [m/sec]
        self.ang = -88 + np.random.rand() * 68 # [deg]
        self.vx = self.v0 * np.sin(np.deg2rad(self.ang))
        self.vy = self.v0 * np.cos(np.deg2rad(self.ang))
        rocket_list.append(self)

    def update(self):
        self.v_loss = (self.vx ** 2 + self.vy ** 2) * world.fric * world.dt
        self.vx = self.vx * (1 - self.v_loss)
        self.vy = self.vy * (1 - self.v_loss) - world.g * world.dt
        self.x = self.x + self.vx * world.dt
        self.y = self.y + self.vy * world.dt

class City():
    def __init__(self, x1, x2, width):
        self.x = np.random.randint(x1, x2) # [m]
        self.width = width # [m]
        city_list.append(self)
        self.img = np.zeros((200, 800))
        for b in range(60):
            h = np.random.randint(30, 180)
            w = np.random.randint(30, 80)
            x = np.random.randint(1, 700)
            self.img[0:h, x:x+w] = np.random.rand()
        self.img = np.flipud(self.img)

class Explosion():
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.size = 500
        self.duration = 0.4 # [sec]
        self.verts1 = (np.random.rand(30 ,2)- 0.5) * self.size
        self.verts2 = (np.random.rand(20 ,2)- 0.5) * self.size / 2
        self.verts1[:,0] = self.verts1[:,0] + x
        self.verts1[:,1] = self.verts1[:,1] + y
        self.verts2[:,0] = self.verts2[:,0] + x
        self.verts2[:,1] = self.verts2[:,1] + y
        self.hit_time = world.time
        explosion_list.append(self)

    def update(self):
        if world.time - self.hit_time > self.duration:
            explosion_list.remove(self)

def Check_interception():
    for intr in interceptor_list:
        for r in rocket_list:
            if ((r.x - intr.x)**2 + (r.y - intr.y)**2)**0.5 < turret.prox_radius:
                rocket_list.remove(r)
                Explosion(intr.x, intr.y)
                if intr in interceptor_list: interceptor_list.remove(intr)
                world.score = world.score + world.reward_intercept

def Check_ground_hit():
    for r in rocket_list:
        if r.y < 0:
            city_hit = False
            for c in city_list:
                if np.abs(r.x - c.x) < c.width:
                    city_hit = True
            if city_hit == True:
                world.score = world.score + world.reward_city
            else:
                world.score = world.score + world.reward_open
            Explosion(r.x, r.y)
            rocket_list.remove(r)

def Draw():
    plt.cla()
    plt.rcParams['axes.facecolor'] = 'black'
    for r in rocket_list:
        plt.plot(r.x, r.y,'.y')
    for intr in interceptor_list:
        plt.plot(intr.x, intr.y,'or')
        C1 = plt.Circle((intr.x, intr.y), radius=turret.prox_radius, linestyle='--', color='gray', fill=False)
        ax = plt.gca()
        ax.add_artist(C1)
    for c in city_list:
        plt.imshow(c.img, extent=[c.x-c.width/2, c.x+c.width/2, 0, c.img.shape[0]])
        plt.set_cmap('bone')
    for e in explosion_list:
        P1 = plt.Polygon(e.verts1, True, color='yellow')
        P2 = plt.Polygon(e.verts2, True, color='red')
        ax = plt.gca()
        ax.add_artist(P1)
        ax.add_artist(P2)
    plt.plot(turret.x, turret.y,'oc', markersize=12)
    plt.plot([turret.x, turret.x + 100*np.sin(np.deg2rad(turret.ang))],
             [turret.y, turret.y + 100*np.cos(np.deg2rad(turret.ang))],'c', linewidth = 3)
    plt.plot(turret.x_hostile, turret.y_hostile,'or', markersize=12)
    plt.axes().set_aspect('equal')
    plt.axis([-world.width / 2, world.width / 2, 0, world.height])
    #plt.title('Score: ' + str(world.score))

    plt.xticks([])
    plt.yticks([])

    plt.draw()

    plt.gcf().savefig('./im.jpg', bbox_inches = 'tight', pad_inches = 0)
    n_img = cv2.imread('./im.jpg')
    os.remove('./im.jpg')

    plt.pause(0.001)
    return n_img

def Init():
    global world, turret, rocket_list, interceptor_list, city_list, explosion_list
    world = World()
    rocket_list = []
    interceptor_list = []
    turret = Turret()
    city_list = []
    explosion_list = []
    City(-world.width * 0.5 + 400, -world.width * 0.25 - 400, 800)
    City(-world.width * 0.25 + 400, -400, 800)
    plt.rcParams['axes.facecolor'] = 'black'

def Game_step(action_button):

    world.time = world.time + world.dt
    rand_in = np.random.rand()
    if rand_in < world.rocket_prob * world.dt:
        Rocket(world)

    for r in rocket_list:
        r.update()

    for intr in interceptor_list:
        intr.update()

    for e in explosion_list:
        e.update()

    turret.update(action_button)
    Check_interception()
    Check_ground_hit()

    r_locs = np.zeros(shape=(len(rocket_list), 2))
    for ind in range(len(rocket_list)):
        r_locs[ind, :] = [rocket_list[ind].x, rocket_list[ind].y]

    i_locs = np.zeros(shape=(len(interceptor_list), 2))
    for ind in range(len(interceptor_list)):
        i_locs[ind, :] = [interceptor_list[ind].x, interceptor_list[ind].y]

    c_locs = np.zeros(shape=(len(city_list), 2))
    for ind in range(len(city_list)):
        c_locs[ind, :] = [city_list[ind].x, city_list[ind].width]

    return r_locs, i_locs, c_locs, turret.ang, world.score

def Create_state(n_img):

    #prepare image for prediction
    gray_im = cv2.cvtColor(n_img, cv2.COLOR_BGR2GRAY)
    gray_im = gray_im[:135, 4:]
    pad = np.zeros((192, 384)).astype('uint8')
    pad[29:-28, 25:-25] = gray_im
    color_im = cv2.cvtColor(pad, cv2.COLOR_GRAY2BGR)
    #state
    state=np.zeros((192,384))
    #predict
    y_h = model.detect([color_im])[0]
    #rois (bounding boxes)
    for y_min,x_min,y_max,x_max in y_h["rois"]:
    #take the center of each rocket (calculate middle of two points)
#        center_y,center_x=(y_min+y_max)/2,(x_min+x_max)/2
    #add one to rocket's center in state mat
        state[y_min:y_max+1, x_min:x_max+1] = 1
#        state[center_y,center_x]=1

    return state

#def start(iters):
#    Init()
#    np.random.seed(1)
#    table=list()
##    state = env.observation_space
#    for stp in range(iters):
#        action_button = np.random.randint(0,4)
##        action_button = RockeAgent.act(state)
##        --> agent(state) --> a
#        r_locs, i_locs, c_locs, ang, score = Game_step(action_button)
#        n_img=Draw()
#        state=Create_state(n_img)
#        table.append([state,action_button,score])

#start(15)

"""
TESTING CONVERSATION
im = cv2.imread('./train/85.jpg')
gray_im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

gray_im = gray_im[:135, 4:]
pad = np.zeros((192, 384)).astype('uint8')
pad[29:-28, 25:-25] = gray_im

color = cv2.cvtColor(pad, cv2.COLOR_GRAY2BGR)
plt.imshow(color)

im = plt.imread('./train/crop/85.jpg')
plt.imshow(im)
"""


from django.core.management.base import BaseCommand, CommandError
from menuFinder.models import DiningHall

class Command(BaseCommand):

  def handle(self, *args, **options):
    dining_hall_info = [['104West!', 'west'], ['Rose House Dining Room', 'west'], ['Keeton House Dining Room', 'west'],
                ['Jansen\'s Dining Room at Bethe House', 'west'], ['Cook House Dining Room', 'west'],
                ['Becker House Dining Room', 'west'], ['Okenshields Dining Room', 'central'],
                ['Robert Purcell Marketplace Eatery', 'north'], ['Risley Dining Room', 'north'],
                ['North Star Dining Room', 'north']]

    DiningHall.objects.all().delete()

    for arr in dining_hall_info:
      DiningHall.objects.create(hall_name=arr[0], campus_location=arr[1])


# -*- coding: utf-8 -*-
"""
Created on Wed Jun  1 10:34:46 2016

@author: Admin_1
"""

import numpy as np
import matplotlib.pyplot as plt

def pca(x):
    print(x.shape)
    mu = np.mean(x, axis=1)
    print(mu)
    xmf = np.vstack(mu) - x
    xcov = 1/x.shape[1]*np.dot(xmf, np.transpose(xmf))
    w, v = np.linalg.eigh(xcov)
    w_sorted = np.sort(w)[::-1]
    v_sorted = v[:, w_sorted.argsort()]

    return mu, xmf, xcov, w_sorted, v_sorted

num1 = np.array([0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,1,1,1,0,0,1,1,1,1,1,0,0,
             1,1,1,1,1,1,0,0,1,0,1,1,0,0,1,1,1,1,1,0,1,1,0,1,1,1,1,
             0,0,1,0,0,1,1,1,1,1,0,0,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,
             1,1,1,1,0,1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1,0,0,1,1,1,1,
             1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,
             1,1,1,1,0,1,1,0,1,1,0,1,1,1,1]).reshape((10 ,15))

plt.figure(1)
for i in range(num1.shape[0]):
    plt.subplot(2, 5, i+1)
    # b1 = np.flipud(np.transpose(num[i,:].reshape((5,3))))
    b1 = np.flipud(np.transpose(num1[i, ::-1].reshape((5, 3))))
    plt.imshow(b1.T, origin='lower', cmap=plt.cm.gray_r,
               interpolation='nearest')
    plt.xticks([])
    plt.yticks([])

plt.subplot(2, 5, 3)
plt.title('Ziffernmuster')

mu, xmf, xcov, w, v = pca(np.transpose(num1))

plt.figure(2)
plt.imshow(np.flipud(np.transpose(mu[::-1].reshape((5, 3)))).T, origin='lower',
           cmap=plt.cm.gray_r, interpolation='nearest')
plt.xticks([])
plt.yticks([])
plt.title('Mittelwertbild')

plt.figure(3)
for i in range(v.shape[1]):
    plt.subplot(3, 5, i+1)
    b1 = np.flipud(np.transpose((-1)*v[::-1, i].reshape((5, 3))))
    plt.imshow(b1.T, origin='lower', cmap=plt.cm.gray_r,
               interpolation='nearest')
    plt.xticks([])
    plt.yticks([])
V = v[:, 0:14]
X1 = np.dot(np.transpose(V), xmf)
rec = np.vstack(mu)+np.dot(V, X1)

plt.figure(4)
for i in range(rec.shape[1]):
    plt.subplot(2, 5, i+1)
    # b1 = np.flipud(np.transpose(num[i,:].reshape((5,3))))
    b1 = np.flipud(np.transpose(rec[::-1, i].reshape((5, 3))))
    plt.imshow(b1.T, origin='lower', cmap=plt.cm.gray_r,
               interpolation='nearest')
    plt.xticks([])
    plt.yticks([])


from argparse import ArgumentParser
import sys
import re
import requests

parser = ArgumentParser(description="Run a SQL command to create a table")
parser.add_argument('sql_file', type=str, help='Path to the .sql file')
parser.add_argument('--database-name', type=str, default="", help='name of the database')

args = parser.parse_args()

db_name = args.database_name

if db_name:
    result = requests.post(
        "http://localhost:8123",
        params={"query": f"CREATE DATABASE IF NOT EXISTS {db_name}"},
    )
    try:
        result.raise_for_status()
    except requests.exceptions.HTTPError as exc:
        raise Exception(result.text) from exc

with open(args.sql_file) as fp:
    query = fp.read()

m = re.search(r"table (\S*)", query)
table_name = m.group(1)

if db_name:
    query = query.replace(f"create table {table_name}", f"create table {db_name}.{table_name}")

    from_db = f"FROM {db_name}"
else:
    from_db = ""

tables = requests.post("http://localhost:8123", params={"query": f"SHOW TABLES {from_db}"}).text.split("\n")
if table_name in tables:
    print(f"Table `{table_name}` already exists, not continuing")
    sys.exit()

result = requests.post(
    "http://localhost:8123",
    params={"query": query},
)
try:
    result.raise_for_status()
except requests.exceptions.HTTPError as exc:
    raise Exception(result.text) from exc


def orientation(a, b, c):
    det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)
    return sign(det)

def sign(a):
    if a == 0:
        return 0
    if a > 0:
        return 1
    if a < 0:
        return -1

def do_line_segments_without_endpoints_intersect(a, b, c, d):
    o1 = orientation(a, b, c)
    o2 = orientation(a, b, d)
    o3 = orientation(c, d, a)
    o4 = orientation(c, d, b)

    if o1 == 0 and o2 == 0 and o3 == 0 and o4 == 0:  # all colinear
        if a.x == b.x == c.x == d.x:
            sorted_a = min(a.x, b.x)
            sorted_b = max(a.x, b.x)
            sorted_c = min(c.x, d.x)
            sorted_d = max(c.x, d.x)
        elif a.y == b.y == c.y == d.y:
            return False
        else:
            sorted_a = min(a.y, b.y)
            sorted_b = max(a.y, b.y)
            sorted_c = min(c.y, d.y)
            sorted_d = max(c.y, d.y)

        return sorted_c < sorted_a < sorted_d \
            or sorted_c < sorted_b < sorted_d \
            or sorted_a < sorted_c < sorted_b \
            or sorted_a < sorted_d < sorted_b \
            or sorted_a == sorted_c \
            or sorted_b == sorted_d

    if o1 == 0 or o2 == 0 or o3 == 0 or o4 == 0:
        return False

    return o1 != o2 and o3 != o4


nums = list(map(int, input().split()))
y_size = nums[0]  # кол-во строк, т.е. длина вертикали
x_size = nums[1]  # кол-во столбцов, т.е. длина горизонтали
tab = [[0] * x_size for _ in range(y_size)]

index = 0; x = 1; y = -1

while True:
	while x > 0 and y < y_size -1:
		y += 1; x -= 1
		index += 1; tab[y][x] = index
	if x == 0 and y < y_size - 1:
		y += 1  # вправо если дошли до верхней границы
	else:
		if y == y_size - 1 and x == x_size - 1:
			break
		else:
			x += 1  #вниз если дошли до правой границы
	index += 1; tab[y][x] = index

	if x == x_size -1 and y == y_size -1:
		break

	while y > 0 and x < x_size - 1:
		y -= 1; x += 1
		index += 1; tab[y][x] = index
	if y == 0 and x < x_size - 1:
		x += 1
	else:
		if y == y_size - 1 and x == x_size - 1:
			break
		else:
			y += 1
	index += 1; tab[y][x] = index

for line in tab:
	print(' '.join(map(str, line)))


from pdbfixer import PDBFixer
from simtk.openmm.app import PDBFile

def fix_pdb(input_file, output_file):
    """

    .. note::
        Set ``export OPENMM_CPU_THREADS=1`` if you want this function
        to use only one thead.
    """
    fixer = PDBFixer(filename=input_file)
    fixer.findMissingResidues()
    # Replace non-standard residues with their standard equiv.
    # (But if atoms have to be added, they get added using the `.addMissingAtoms()` command)
    fixer.findNonstandardResidues()
    fixer.replaceNonstandardResidues()
    # Find missing heavy atoms
    fixer.findMissingAtoms()
    fixer.addMissingAtoms()
    # fixer.removeHeterogens(True)
    fixer.addMissingHydrogens(7.0)
    with open(output_file, 'w') as ofh:
        PDBFile.writeFile(fixer.topology, fixer.positions, ofh, keepIds=True)


import argparse

# ================================================
# Parse data input and output directories
# ================================================
def parse_args():

    # Parse arguments
    parser = argparse.ArgumentParser(description='Run CNN Segmenter for 4D flow MRIs.')
    parser.add_argument('--train', dest='train', action='store_true', help='run training')
    parser.add_argument('--inference', dest='train', action='store_false', help='run inference')
    parser.add_argument('--config', type=str, default='config/cnn_segmenter_neerav.json', help='Directory containing MRI data set') # default='system/cnn_segmenter_neerav.json'
    parser.add_argument('--model', type=str, default='experiments/unet_neerav.json', help='Directory containing model configuration')

    # training arguments
    parser.add_argument('--training-input', type=str, help='Training input directory (for training only)')
    parser.add_argument('--training-output', type=str, help='Training output directory (for training only)')

    # inference arguments
    parser.add_argument('--inference-input', type=str, help='Input FlowMRI (for inference only)')
    parser.add_argument('--inference-output', type=str, help='Output SegmentedFlowMRI (for inference only)')

    # debug arguments
    parser.add_argument('--debug_server', type=str, help='Socket address (hostname:port) of Pycharm debug server')

    return parser.parse_args()

args = parse_args()


# Import libraries needed
from configparser import ConfigParser
from pathlib import Path

import tweepy

# Path to the config file with the keys make sure not to commit this file
CONFIG_FILE = Path.cwd() / "config.cfg"

config = ConfigParser()
config.read(CONFIG_FILE)

# Authenticate to Twitter
auth = tweepy.OAuthHandler(
    config.get("twitter", "consumer_key"), config.get("twitter", "consumer_secret")
)
auth.set_access_token(
    config.get("twitter", "access_token"), config.get("twitter", "access_token_secret")
)

# Create Twitter API object
twitter = tweepy.API(auth)

# let's collect some of the tweets in your public timeline
public_tweets = twitter.home_timeline()

for tweet in public_tweets:
    print()
    print(tweet.text)


from bs4 import BeautifulSoup
import urllib.request
import os
import argparse
import sys
import json
import csv

# adapted from https://gist.github.com/genekogan/ebd77196e4bf0705db51f86431099e57

# CHANGES MADE:
# changed to accommodate python 3 (instead of 2)
# cleaned up
# create directory if not exist
# download multiple search terms into subdirectories - intended of TF training purposes

def get_soup(url,header):
    return BeautifulSoup(urllib.request.urlopen(urllib.request.Request(url,headers=header)),'html.parser')

def main(args):
    parser = argparse.ArgumentParser(description='Scrape Google images')
    # parser.add_argument('-s', '--search', default='eye', type=str, help='search term')
    parser.add_argument('-i', '--search_file', default=os.path.join(os.getcwd(), 'additional', 'search_file.csv'), type=str, help='image search term file')
    parser.add_argument('-n', '--num_images', default=10, type=int, help='number of images to save')
    parser.add_argument('-d', '--directory', default=os.path.join(os.getcwd(), 'images'), type=str, help='save directory')

    args = parser.parse_args()
    # query = args.search
    query = args.search_file
    max_images = args.num_images
    save_directory = args.directory

    if not os.path.exists(save_directory):
        os.makedirs(save_directory)

    if not os.path.isfile(query):
        raise ValueError('Image search term file does not exist. Please specify an existing file.')
    else:
        with open(query) as csvfile:
            queryreader = csv.DictReader(csvfile)
            for row in queryreader:
                img_class = row['class']
                img_phrase = row['search']
                row_search = img_phrase.split()
                row_search = '_'.join(row_search)
                img_phrase = img_phrase.split()
                img_phrase = '+'.join(img_phrase)
                url = "https://www.google.co.in/search?q="+img_phrase+"&source=lnms&tbm=isch"
                header = {'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"}
                soup = get_soup(url, header)

                class_save_dir = os.path.join(save_directory, img_class)

                if not os.path.isdir(class_save_dir):
                    os.makedirs(class_save_dir)

                img_links = []

                for a in soup.find_all("div", {"class":"rg_meta"}):
                    img_link = json.loads(a.text)["ou"]
                    img_format = json.loads(a.text)["ity"]
                    img_links.append((img_link, img_format))

                for i, (img_link, img_format) in enumerate(img_links[0:max_images]):
                    try:
                        with urllib.request.urlopen(img_link) as response:
                            raw_img = response.read()

                        if len(img_format) == 0:
                            f = open(os.path.join(class_save_dir, row_search + "_" + str(i) + ".jpg"), 'wb')
                        else :
                            f = open(os.path.join(class_save_dir, row_search + "_" + str(i) + "." + img_format), 'wb')

                        f.write(raw_img)
                        f.close()

                    except Exception as e:
                        print("could not load : {}".format(img_link))
                        print(e)

if __name__ == '__main__':
    from sys import argv
    try:
        main(argv)
    except KeyboardInterrupt:
        pass
    sys.exit()


from sys import stdin, stdout
# import math
# import random
# def func(N,x,co):
for T in range(int(input())):
    N,x = list(map(int,stdin.readline().split()))
    co = list(map(int,stdin.readline().split()))
    co = sorted(co)
    co_val = [1] * N
    # print(co)
    days = 0
    position = 0
    n = 0
    # Find the initial Position
    for i in range(len(co)):
        if co[i] == x:
            position = i+1
            days += i+1
            x += x
            break
        if co[i] > x:
            if i > 0 and 2*co[i-1] >= x:
                x = 2 * co[i-1]
                position = i-1
                days += i - 1
            else :
                days += i
                position = i
            break
    # print(position)
    for pos in range(position,N):
        while co_val[pos]:
            print(f'value: {co[pos]}, x: {x}, days: {days}')
            if co[pos] <= x:
                co_val[pos] = 0
            days += 1
            x = 2 * min(x,co[pos])

    # print(x)
    print(days)

# if __name__ == '__main__':
#     for i in range(1):
#         N = 20
#         l1 = [random.choice(range(1, 50)) for _ in range(N)]
#         x  = random.choice(range(1,50))
#         print(f'x:{x}, N : {N}')
#         # print(l1)
#         func(N,x,l1)


import os
from math import ceil
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

def load_image(file_name, dtype='float32', gray=False):
    """
    Loads an image as an numpy array
    :param file_name:
    :param trans:
    :return:
    """
    img = Image.open(file_name)
    img.load()
    if gray:
        img = img.convert('L')
    data = np.asarray(img, dtype=dtype)

    return data

def show_histogram(img):
    plt.hist(img.ravel(), 256, [0, 256]);
    plt.show()

def divide_img(img, bound, min=0, max=1):
    less = img < bound
    greater = img >= bound
    new_img = img.copy()
    new_img[less] = min
    new_img[greater] = max
    return new_img


from impl.matrices import (
    positive_definite_matrix
)
from impl.cholesky import cholesky_decomposition
from impl.lineq_solver import solve_cholesky
import numpy as np
import visual as vs

def impl_vs_numpy(m_size):
    M = positive_definite_matrix(size=m_size)
    L = cholesky_decomposition(M)
    L_NP = np.linalg.cholesky(M)

    mv = vs.MatrixVisualizer(inputs=((M, L, L.T, np.dot(L, L.T)),
                                     (M, L_NP, L_NP.T, np.dot(L_NP, L_NP.T))),
                             labels=(("Initial Matrix",
                                      "Lower triangular - L",
                                      "Transpose - L'",
                                      "Recomposed matrix (L*L')"),
                                     ("Initial Matrix",
                                      "Numpy - Lower triangular - L",
                                      "Numpy - Transpose - L'",
                                      "Recomposed matrix (L*L')")),
                             map_type="flag")
    mv.show(rows=2, cols=4)

def impl_vs_numpy_timeit(m_size):
    matrix = positive_definite_matrix(size=m_size)
    funcs = [cholesky_decomposition, np.linalg.cholesky]
    labels = ("Implemented Cholesky", "Numpy Cholesky")
    tv = vs.TimeVisualizer(funcs=funcs,
                           data=matrix,
                           labels=labels,
                           iterations=200)
    tv.show()

def cholesky_fail(m_size):
    matrix = rand_matrix(m_size)
    try:
        cholesky_decomposition(matrix)
    except Exception as e:
        print(e)

    mv = vs.MatrixVisualizer(inputs=matrix,
                             labels="Random matrix",
                             map_type="plasma")
    mv.show()

def cholesky_solve(m_size):
    matrix = [[4, 12, -16], [12, 37, -43], [-16, -43, 98]]
    coef = np.array([2, 5, 3]).reshape((3, 1))
    L = cholesky_decomposition(matrix)
    x = solve_cholesky(matrix, coef)
    mv = vs.MatrixVisualizer(inputs=(matrix, L, L.T, x),
                             labels=("Matrix", "Lower triangular",
                                     "Transpose - L'", "X solution"),
                             map_type="plasma")
    mv.show(rows=1, cols=4)
    print("Matrix A")
    print(matrix)
    print("B coefficient: ")
    print(coef)
    print("X solution: ")
    print(x)


from django.test import override_settings

from ozpcenter.scripts import sample_data_generator as data_gen
from tests.ozp.cases import APITestCase
from tests.ozpcenter.helper import APITestHelper

@override_settings(ES_ENABLED=False)
class IntentApiTest(APITestCase):

    @classmethod
    def setUpTestData(cls):
        data_gen.run()

    def setUp(self):
        pass

    def test_get_intent_list(self):
        url = '/api/intent/'
        response = APITestHelper.request(self, url, 'GET', username='wsmith', status_code=200)

        actions = [i['action'] for i in response.data]
        self.assertTrue('/application/json/view' in actions)
        self.assertTrue(response.data[0]['icon'] is not None)
        self.assertTrue(response.data[0]['media_type'] is not None)
        self.assertTrue(response.data[0]['label'] is not None)

    def test_get_intent(self):
        url = '/api/intent/1/'
        response = APITestHelper.request(self, url, 'GET', username='wsmith', status_code=200)

        self.assertEqual(response.data['action'], '/application/json/view')

    def test_create_intent(self):
        url = '/api/intent/'
        data = {'action': '/application/test',
                'media_type': 'vnd.ozp-intent-v1+json.json', 'label': 'test',
                'icon': {'id': 1, 'security_marking': 'UNCLASSIFIED'}}

        response = APITestHelper.request(self, url, 'POST', data=data, username='bigbrother', status_code=201)
        self.assertEqual(response.data['action'], '/application/test')

    def test_update_intent(self):
        url = '/api/intent/1/'
        action = '/application/json/viewtest'
        media_type = 'vnd.ozp-intent-v2+json.json'
        label = 'mylabel'
        data = {'action': action,
                'media_type': media_type, 'label': label,
                'icon': {'id': 1, 'security_marking': 'UNCLASSIFIED'}}

        response = APITestHelper.request(self, url, 'PUT', data=data, username='bigbrother', status_code=200)

        self.assertEqual(response.data['action'], action)
        self.assertEqual(response.data['label'], label)
        self.assertEqual(response.data['media_type'], media_type)

    def test_delete_intent(self):
        url = '/api/intent/1/'
        APITestHelper.request(self, url, 'DELETE', username='bigbrother', status_code=204)


from django.urls import path,include
from .views import *
app_name='user'

urlpatterns = [
    # path('frame/',frame),
    path('login/',login),
    path('home1/',home1),
    path('register/',register),
    path('options/',options),
    path('pay/',pay),

]