In [1]:
%matplotlib inline

In [2]:
from pylab import*
import numpy as np

In [3]:
#import sys
#sys.path.append('\CVFIT')

In [4]:
#from cvfit import fitting
from cvfit.fitting import SingleFitSession
from cvfit.fitting import MultipleFitSession
from cvfit import data
from cvfit import plots

In [5]:
fname = "Chimera3_tofit.xlsx"
sheet = 0
sets = data.read_sets_from_Excel(fname, 2, 0, sheet)

print('File {0} loaded'.format(fname))
print('{0:d} sets found.'.format(len(sets)))
    
#sets = fitting.set_weights(sets)
for i in range(len(sets)):
    print ('\nSet #{0:d}:'.format(i+1))
    print (sets[i])


File Chimera3_tofit.xlsx loaded
6 sets found.

Set #1:

X	Y	s(Y)	weight
0.1	189.446	0	1
0.2	480.966	0	1
0.5	540.123	0	1
1	532.233	0	1
2	637.359	0	1
5	772.081	0	1
10	611.283	0	1
10	734.65	0	1
10	625.649	0	1


Set #2:

X	Y	s(Y)	weight
0.05	94.5719	0	1
0.1	254.832	0	1
0.2	611.322	0	1
0.5	766.943	0	1
1	542.023	0	1
2	604.908	0	1
5	786.409	0	1
10	879.467	0	1
10	881.18	0	1
10	668.376	0	1


Set #3:

X	Y	s(Y)	weight
0.05	110.811	0	1
0.1	337.028	0	1
0.2	511.058	0	1
0.5	698.082	0	1
1	750.619	0	1
2	814.317	0	1
5	870.956	0	1
10	936.694	0	1
10	960.62	0	1
10	775.718	0	1


Set #4:

X	Y	s(Y)	weight
0.05	559.92	0	1
0.1	737.378	0	1
0.2	1353.63	0	1
0.5	1532.53	0	1
1	1827.33	0	1
2	1825.46	0	1
5	1955.43	0	1
10	2113.3	0	1
10	1800.11	0	1
10	1921.9	0	1
10	1978.67	0	1


Set #5:

X	Y	s(Y)	weight
0.05	236.069	0	1
0.1	798.895	0	1
0.2	1744.28	0	1
0.5	2028.85	0	1
1	2506.82	0	1
2	2356.55	0	1
5	2512.08	0	1
10	2985.96	0	1
10	2257.51	0	1
10	2529.57	0	1


Set #6:

X	Y	s(Y)	weight
0.05	166.683	0	1
0.1	528.987	0	1
0.2	984.164	0	1
0.5	1225.71	0	1
1	1297.1	0	1
1	1108.63	0	1
2	1409.53	0	1
5	1396.6	0	1
10	1560.17	0	1
10	1294.92	0	1
10	1169.92	0	1
10	1097.26	0	1


In [6]:
eqname = 'Hill'
if eqname == 'Hill' or eqname == 'Langmuir':
        from cvfit.equations import Hill as eqfit

Example of multiple fit session


In [7]:
# initiate fitting sessions, fit data and print results
fitsessions = MultipleFitSession()
for each in sets:
    equation = eqfit(eqname)
    fs = SingleFitSession(each, equation)
    fs.fit()
    fs.calculate_errors()
    print('\n*************************************************')
    print('\t' + fs.data.title + ' fit finished')
    print(fs.string_estimates())
    print(fs.string_liklimits())
    #plots.plot_hill_fit_result_single(fname, fsession.data, fsession.eq, plotguess=True)
    fitsessions.add(fs)
print('\n*************************************************')
print('\tAverage of all fits:')
print(fitsessions.string_average_estimates())


	Fitting session for Set 1 initialised!
C:\DCPROGS\CVFIT\cvfit\equations.py:167: RuntimeWarning: invalid value encountered in power
  return (coeff[0] + ((coeff[1] - coeff[0]) * (conc / coeff[2]) ** coeff[3]) /
C:\DCPROGS\CVFIT\cvfit\equations.py:168: RuntimeWarning: invalid value encountered in power
  (1 + (conc / coeff[2]) ** coeff[3]))
*************************************************
	Set 1 fit finished
Number of point fitted = 9
Number of parameters estimated = 3
Degrees of freedom = 6
Residual error SD = 87.537      (variance = 7662.811)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 725.475  	  Approx SD = 92.2475	  CV = 12.7
Parameter 3: EC50  	= 0.163949  	  Approx SD = 0.080449	  CV = 49.1
Parameter 4: nH    	= 0.741724  	  Approx SD = 0.419618	  CV = 56.6
Minimum SSD = 45976.863; 
Max log-likelihood = -51.194
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
2.99-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.45)
Lmax= -51.1945;   Lcrit= -54.1884
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 725.475	  LOWER = 560.146	  UPPER limit not found
Parameter 3:   EC50	= 0.163949	  LOWER limit not found	  UPPER limit not found
Parameter 4:   nH  	= 0.741724	  LOWER = 0.115889	  UPPER limit not found

	Fitting session for Set 2 initialised!
*************************************************
	Set 2 fit finished
Number of point fitted = 10
Number of parameters estimated = 3
Degrees of freedom = 7
Residual error SD = 125.805      (variance = 15826.939)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 744.024  	  Approx SD = 52.5471	  CV = 7.1
Parameter 3: EC50  	= 0.122063  	  Approx SD = 0.0313284	  CV = 25.7
Parameter 4: nH    	= 2.01223  	  Approx SD = 1.24638	  CV = 61.9
Minimum SSD = 110788.572; 
Max log-likelihood = -60.753
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
2.8-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.37)
Lmax= -60.7534;   Lcrit= -63.55
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 744.024	  LOWER = 623.634	  UPPER limit not found
Parameter 3:   EC50	= 0.122063	  LOWER = 0.0627321	  UPPER limit not found
Parameter 4:   nH  	= 2.01223	  LOWER = 0.3081	  UPPER limit not found

	Fitting session for Set 3 initialised!
*************************************************
	Set 3 fit finished
Number of point fitted = 10
Number of parameters estimated = 3
Degrees of freedom = 7
Residual error SD = 62.841      (variance = 3948.954)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 882.923  	  Approx SD = 36.1223	  CV = 4.1
Parameter 3: EC50  	= 0.168371  	  Approx SD = 0.0299462	  CV = 17.8
Parameter 4: nH    	= 1.20532  	  Approx SD = 0.258865	  CV = 21.5
Minimum SSD = 27642.680; 
Max log-likelihood = -53.812
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.8-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.37)
Lmax= -53.812;   Lcrit= -56.6087
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 882.923	  LOWER = 808.506	  UPPER = 989.709
Parameter 3:   EC50	= 0.168371	  LOWER = 0.114701	  UPPER = 0.27004
Parameter 4:   nH  	= 1.20532	  LOWER = 0.722241	  UPPER = 2.03934

	Fitting session for Set 4 initialised!
*************************************************
	Set 4 fit finished
Number of point fitted = 11
Number of parameters estimated = 3
Degrees of freedom = 8
Residual error SD = 108.471      (variance = 11765.946)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 1967.44  	  Approx SD = 55.6382	  CV = 2.8
Parameter 3: EC50  	= 0.126872  	  Approx SD = 0.0172014	  CV = 13.6
Parameter 4: nH    	= 1.09647  	  Approx SD = 0.18946	  CV = 17.3
Minimum SSD = 94127.570; 
Max log-likelihood = -65.408
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.66-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.31)
Lmax= -65.4081;   Lcrit= -68.067
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 1967.44	  LOWER = 1852.17	  UPPER = 2115.29
Parameter 3:   EC50	= 0.126872	  LOWER = 0.093713	  UPPER = 0.174905
Parameter 4:   nH  	= 1.09647	  LOWER = 0.734672	  UPPER = 1.63619

	Fitting session for Set 5 initialised!
*************************************************
	Set 5 fit finished
Number of point fitted = 10
Number of parameters estimated = 3
Degrees of freedom = 7
Residual error SD = 238.059      (variance = 56672.062)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 2524.43  	  Approx SD = 110.705	  CV = 4.4
Parameter 3: EC50  	= 0.149194  	  Approx SD = 0.0263891	  CV = 17.7
Parameter 4: nH    	= 1.75691  	  Approx SD = 0.559144	  CV = 31.8
Minimum SSD = 396704.437; 
Max log-likelihood = -67.131
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.8-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.37)
Lmax= -67.1312;   Lcrit= -69.9278
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 2524.43	  LOWER = 2286.13	  UPPER = 2815.9
Parameter 3:   EC50	= 0.149194	  LOWER = 0.10263	  UPPER = 0.237568
Parameter 4:   nH  	= 1.75691	  LOWER = 0.896854	  UPPER = 4.0814

	Fitting session for Set 6 initialised!
*************************************************
	Set 6 fit finished
Number of point fitted = 12
Number of parameters estimated = 3
Degrees of freedom = 9
Residual error SD = 169.586      (variance = 28759.406)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 1360.18  	  Approx SD = 78.2593	  CV = 5.8
Parameter 3: EC50  	= 0.125016  	  Approx SD = 0.034899	  CV = 27.9
Parameter 4: nH    	= 1.06464  	  Approx SD = 0.332431	  CV = 31.2
Minimum SSD = 258834.652; 
Max log-likelihood = -76.901
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.56-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.26)
Lmax= -76.9015;   Lcrit= -79.4598
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 1360.18	  LOWER = 1133.37	  UPPER = 1514.04
Parameter 3:   EC50	= 0.125016	  LOWER = 0.0651932	  UPPER = 0.233715
Parameter 4:   nH  	= 1.06464	  LOWER = 0.679849	  UPPER limit not found

*************************************************
	Average of all fits:
Average of estimates of 6 sets (mean +/- sdm):
Parameter 1: Ymin  	= 0 +/- 0
	(all: 0.0	0.0	0.0	0.0	0.0	0.0
Parameter 2: Ymax  	= 1367.41 +/- 275.59
	(all: 725.4749106309289	744.0240936856511	882.9225688690085	1967.4386720981079	2524.426331260671	1360.1842079409257
Parameter 3: EC50  	= 0.142578 +/- 0.00771315
	(all: 0.16394918932906583	0.12206329751214895	0.16837094388694054	0.12687170834268513	0.149194271593193	0.1250161256502485
Parameter 4: nH    	= 1.31288 +/- 0.177365
	(all: 0.741723915134877	2.012231318363066	1.2053179279688098	1.096474843757712	1.7569140875945175	1.0646411518238688

In [8]:
fplots = fitsessions.prepare_fplot('fit')
for each in sets:
    semilogx(each.X, each.Y, 'o', label=each.title)
for fp in fplots:
    semilogx(fp[0], fp[1], 'b-')



In [9]:
for fs in fitsessions.list:
    fs.eq.normalise(fs.data)
fplots = fitsessions.prepare_fplot('norm')
for each in sets:
    semilogx(each.X, each.normY, 'o', label=each.title)
for fp in fplots:
    semilogx(fp[0], fp[1], 'b-')



In [10]:
# pool data and fit pooled
fitsessions.pool(norm=True)
fitsessions.pooled.fit()
fitsessions.pooled.calculate_errors()
fitsessions.pooled.data.average_pooled()
print('\n***********************\n**************************')
print('\tNormalised and pooled data fit finished')
print(fitsessions.pooled.string_estimates())
print(fitsessions.pooled.string_liklimits())


data: WARNING : some SD equal to 0;cannot be used for weights; reverting to weightmode = 1

	Fitting session for Pooled data initialised!
***********************
**************************
	Normalised and pooled data fit finished
Number of point fitted = 62
Number of parameters estimated = 3
Degrees of freedom = 59
Residual error SD = 0.103      (variance = 0.011)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 0.994483  	  Approx SD = 0.0223169	  CV = 2.2
Parameter 3: EC50  	= 0.146212  	  Approx SD = 0.0142952	  CV = 9.8
Parameter 4: nH    	= 1.29848  	  Approx SD = 0.187044	  CV = 14.4
Minimum SSD = 0.629; 
Max log-likelihood = 54.357
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2-unit Likelihood Intervals  (equivalent SD for Gaussian- 2)
Lmax= 54.3575;   Lcrit= 52.3554
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 0.994483	  LOWER = 0.929765	  UPPER = 1.02523
Parameter 3:   EC50	= 0.146212	  LOWER = 0.112187	  UPPER = 0.1687
Parameter 4:   nH  	= 1.29848	  LOWER = 1.08429	  UPPER = 2.31826

In [11]:
# plot pooled data fit
fplots = fitsessions.prepare_fplot('pooled')
fig = plots.cvfit_plot([fitsessions.pooled.data], fig=None, fplotsets=fplots, 
    fplotline='b-', logX=True, logY=False, legend=False, pooled=True)



In [12]:
fout = open(fname[:-5] + '_points.txt', 'w')
for i in range(len(fitsessions.pooled.data.avX)):
    fout.write('{0:.6e}\t{1:.6e}\t{2:.6e}\n'.
        format(fitsessions.pooled.data.avX[i], 
        fitsessions.pooled.data.avY[i],
        fitsessions.pooled.data.avS[i]))
fout.close()
fout = open(fname[:-5] + '_fit.txt', 'w')
for i in range(len(fplots[0][0])):
    fout.write('{0:.6e}\t{1:.6e}\n'.
        format(fplots[0][0][i], fplots[0][1][i]))
fout.close()

In [ ]: