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 = "Chimera1_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 Chimera1_tofit.xlsx loaded
7 sets found.

Set #1:

X	Y	s(Y)	weight
0.05	319.964	0	1
0.1	1052.76	0	1
0.2	2254.67	0	1
0.5	3319.22	0	1
1	2616.93	0	1
2	3593.12	0	1
5	3352.19	0	1
10	3782.98	0	1
10	3515.37	0	1
10	3536.82	0	1
10	3366.5	0	1


Set #2:

X	Y	s(Y)	weight
0.05	397.973	0	1
0.1	1103.26	0	1
0.2	2264.53	0	1
0.5	3784.67	0	1
1	3599.15	0	1
2	3859.39	0	1
2	2848.39	0	1
5	3162.45	0	1
10	3832.87	0	1
10	3423.72	0	1
10	3100.23	0	1
10	2826.72	0	1


Set #3:

X	Y	s(Y)	weight
0.05	306.079	0	1
0.1	919.307	0	1
0.2	3028.15	0	1
0.5	5813.87	0	1
0.5	6791.47	0	1
1	6837.33	0	1
2	8897.22	0	1
5	7781.85	0	1
10	5340.77	0	1
10	7864.58	0	1
10	8860.95	0	1
10	9055.28	0	1


Set #4:

X	Y	s(Y)	weight
0.05	282.312	0	1
0.1	855.595	0	1
0.2	2562.56	0	1
0.5	3911.4	0	1
1	4492.65	0	1
2	4426.95	0	1
2	3999	0	1
5	4307.51	0	1
10	5588.11	0	1
10	3752.53	0	1
10	3003.99	0	1
10	4824.4	0	1
10	4606.37	0	1
10	4689.06	0	1


Set #5:

X	Y	s(Y)	weight
0.05	296.948	0	1
0.1	977.019	0	1
0.2	2275.37	0	1
0.5	4314.2	0	1
0.5	6128.02	0	1
1	3899.71	0	1
1	6217.41	0	1
2	7183.85	0	1
5	4733.49	0	1
10	6116.6	0	1
10	5075.93	0	1
10	7630.73	0	1
10	7622.14	0	1
10	7260.46	0	1


Set #6:

X	Y	s(Y)	weight
0.05	257.781	0	1
0.1	980.898	0	1
0.2	2751.72	0	1
0.5	3103.99	0	1
1	3712.55	0	1
2	3516.46	0	1
5	4374.71	0	1
10	3803.67	0	1
10	4328.67	0	1
10	5021.01	0	1


Set #7:

X	Y	s(Y)	weight
0.05	825.581	0	1
0.1	1235.18	0	1
0.2	3923.25	0	1
0.5	5244.38	0	1
1	6523.26	0	1
2	6159.81	0	1
5	5764.82	0	1
10	6834.3	0	1
10	5819.93	0	1
10	5970.89	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!
*************************************************
	Set 1 fit finished
Number of point fitted = 11
Number of parameters estimated = 3
Degrees of freedom = 8
Residual error SD = 308.702      (variance = 95296.779)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 3441.76  	  Approx SD = 126.855	  CV = 3.7
Parameter 3: EC50  	= 0.149401  	  Approx SD = 0.0227037	  CV = 15.2
Parameter 4: nH    	= 1.92299  	  Approx SD = 0.618571	  CV = 32.2
Minimum SSD = 762374.235; 
Max log-likelihood = -76.913
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.66-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.31)
Lmax= -76.913;   Lcrit= -79.5718
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 3441.76	  LOWER = 3172.08	  UPPER = 3761.71
Parameter 3:   EC50	= 0.149401	  LOWER = 0.108294	  UPPER = 0.223025
Parameter 4:   nH  	= 1.92299	  LOWER = 0.981329	  UPPER = 3.98462

	Fitting session for Set 2 initialised!
*************************************************
	Set 2 fit finished
Number of point fitted = 12
Number of parameters estimated = 3
Degrees of freedom = 9
Residual error SD = 490.375      (variance = 240467.725)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 3473.51  	  Approx SD = 184.372	  CV = 5.3
Parameter 3: EC50  	= 0.14148  	  Approx SD = 0.0358968	  CV = 25.4
Parameter 4: nH    	= 1.41338  	  Approx SD = 0.426992	  CV = 30.2
Minimum SSD = 2164209.526; 
Max log-likelihood = -89.643
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.56-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.26)
Lmax= -89.6432;   Lcrit= -92.2015
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 3473.51	  LOWER = 2932.65	  UPPER = 3848.21
Parameter 3:   EC50	= 0.14148	  LOWER = 0.0734449	  UPPER = 0.235842
Parameter 4:   nH  	= 1.41338	  LOWER = 0.907816	  UPPER limit not found

	Fitting session for Set 3 initialised!
*************************************************
	Set 3 fit finished
Number of point fitted = 12
Number of parameters estimated = 3
Degrees of freedom = 9
Residual error SD = 1174.920      (variance = 1380437.866)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 8284.15  	  Approx SD = 526.84	  CV = 6.4
Parameter 3: EC50  	= 0.270345  	  Approx SD = 0.0660932	  CV = 24.4
Parameter 4: nH    	= 1.45123  	  Approx SD = 0.44539	  CV = 30.7
Minimum SSD = 12423940.792; 
Max log-likelihood = -100.129
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.56-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.26)
Lmax= -100.129;   Lcrit= -102.687
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 8284.15	  LOWER = 6771.24	  UPPER = 9186.08
Parameter 3:   EC50	= 0.270345	  LOWER = 0.142742	  UPPER = 0.438974
Parameter 4:   nH  	= 1.45123	  LOWER = 0.872725	  UPPER limit not found

	Fitting session for Set 4 initialised!
*************************************************
	Set 4 fit finished
Number of point fitted = 14
Number of parameters estimated = 3
Degrees of freedom = 11
Residual error SD = 683.676      (variance = 467413.121)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 4490.56  	  Approx SD = 251.951	  CV = 5.6
Parameter 3: EC50  	= 0.180194  	  Approx SD = 0.051064	  CV = 28.3
Parameter 4: nH    	= 1.27711  	  Approx SD = 0.424834	  CV = 33.3
Minimum SSD = 5141544.336; 
Max log-likelihood = -109.562
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

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

LIKELIHOOD INTERVALS
2.42-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.2)
Lmax= -109.562;   Lcrit= -111.984
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 4490.56	  LOWER = 3827.71	  UPPER = 5026.69
Parameter 3:   EC50	= 0.180194	  LOWER = 0.0974363	  UPPER = 0.351416
Parameter 4:   nH  	= 1.27711	  LOWER = 0.758441	  UPPER limit not found

	Fitting session for Set 5 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 5 fit finished
Number of point fitted = 14
Number of parameters estimated = 3
Degrees of freedom = 11
Residual error SD = 1169.980      (variance = 1368853.563)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 6452.43  	  Approx SD = 494.246	  CV = 7.7
Parameter 3: EC50  	= 0.266666  	  Approx SD = 0.0801916	  CV = 30.1
Parameter 4: nH    	= 1.65824  	  Approx SD = 0.743895	  CV = 44.9
Minimum SSD = 15057389.196; 
Max log-likelihood = -117.083
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

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

LIKELIHOOD INTERVALS
2.42-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.2)
Lmax= -117.083;   Lcrit= -119.506
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 6452.43	  LOWER = 5483.58	  UPPER = 8088.43
Parameter 3:   EC50	= 0.266666	  LOWER = 0.133945	  UPPER = 0.576924
Parameter 4:   nH  	= 1.65824	  LOWER = 0.609339	  UPPER limit not found

	Fitting session for Set 6 initialised!
*************************************************
	Set 6 fit finished
Number of point fitted = 10
Number of parameters estimated = 3
Degrees of freedom = 7
Residual error SD = 497.867      (variance = 247871.501)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 4254.76  	  Approx SD = 299.232	  CV = 7.0
Parameter 3: EC50  	= 0.19692  	  Approx SD = 0.0596603	  CV = 30.3
Parameter 4: nH    	= 1.34591  	  Approx SD = 0.526259	  CV = 39.1
Minimum SSD = 1735100.507; 
Max log-likelihood = -74.509
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= -74.5093;   Lcrit= -77.306
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 4254.76	  LOWER = 3665.95	  UPPER = 5554.02
Parameter 3:   EC50	= 0.19692	  LOWER = 0.110113	  UPPER = 0.593755
Parameter 4:   nH  	= 1.34591	  LOWER = 0.543726	  UPPER = 5.33253

	Fitting session for Set 7 initialised!
*************************************************
	Set 7 fit finished
Number of point fitted = 10
Number of parameters estimated = 3
Degrees of freedom = 7
Residual error SD = 456.347      (variance = 208252.882)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 6165.73  	  Approx SD = 195.091	  CV = 3.2
Parameter 3: EC50  	= 0.166126  	  Approx SD = 0.0194908	  CV = 11.7
Parameter 4: nH    	= 2.09925  	  Approx SD = 0.509406	  CV = 24.3
Minimum SSD = 1457770.176; 
Max log-likelihood = -73.639
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.8-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.37)
Lmax= -73.6386;   Lcrit= -76.4352
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 6165.73	  LOWER = 5731.38	  UPPER = 6629.83
Parameter 3:   EC50	= 0.166126	  LOWER = 0.127683	  UPPER = 0.221495
Parameter 4:   nH  	= 2.09925	  LOWER = 1.28276	  UPPER = 4.16991

*************************************************
	Average of all fits:
Average of estimates of 7 sets (mean +/- sdm):
Parameter 1: Ymin  	= 0 +/- 0
	(all: 0.0	0.0	0.0	0.0	0.0	0.0	0.0
Parameter 2: Ymax  	= 5223.27 +/- 630.49
	(all: 3441.7550903107626	3473.5148421416375	8284.153479615938	4490.562059459366	6452.430835637782	4254.760522166977	6165.731335818786
Parameter 3: EC50  	= 0.195876 +/- 0.0185208
	(all: 0.14940067850526303	0.1414803540680004	0.2703454480476842	0.18019369554834683	0.2666664726771879	0.19692037426138928	0.16612630906267556
Parameter 4: nH    	= 1.59545 +/- 0.109041
	(all: 1.9229919384270482	1.413380317412475	1.4512345713316686	1.2771068390397633	1.6582426025500328	1.3459112632602692	2.0992539683289126

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!
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]))
***********************
**************************
	Normalised and pooled data fit finished
Number of point fitted = 83
Number of parameters estimated = 3
Degrees of freedom = 80
Residual error SD = 0.129      (variance = 0.017)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 0.986169  	  Approx SD = 0.0197886	  CV = 2.0
Parameter 3: EC50  	= 0.178344  	  Approx SD = 0.0153337	  CV = 8.6
Parameter 4: nH    	= 1.57795  	  Approx SD = 0.192939	  CV = 12.2
Minimum SSD = 1.326; 
Max log-likelihood = 53.894
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
1.99-unit Likelihood Intervals  (equivalent SD for Gaussian- 1.99)
Lmax= 53.8938;   Lcrit= 51.9071
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 0.986169	  LOWER = 0.933165	  UPPER = 1.02816
Parameter 3:   EC50	= 0.178344	  LOWER = 0.150286	  UPPER = 0.226401
Parameter 4:   nH  	= 1.57795	  LOWER = 1.3451	  UPPER = 2.78356

In [12]:
# 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 [13]:
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 [ ]: