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 = "Chimera2_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 Chimera2_tofit.xlsx loaded
5 sets found.

Set #1:

X	Y	s(Y)	weight
0.05	80.5292	0	1
0.1	196.356	0	1
0.2	392.3	0	1
0.5	554.575	0	1
1	636.655	0	1
2	678.653	0	1
5	663.276	0	1
10	822.958	0	1
10	749.732	0	1
10	760.57	0	1
10	682.586	0	1


Set #2:

X	Y	s(Y)	weight
0.05	164.894	0	1
0.1	571.998	0	1
0.2	1029.76	0	1
0.5	1546.73	0	1
1	1482.64	0	1
2	1855.57	0	1
5	1630.35	0	1
10	2262.67	0	1
10	2040.5	0	1
10	1873.88	0	1


Set #3:

X	Y	s(Y)	weight
0.05	147.659	0	1
0.1	706.546	0	1
0.2	905.092	0	1
0.5	1401.23	0	1
1	1377.06	0	1
2	1310.95	0	1
5	1403.92	0	1
10	1802.18	0	1
10	1616.49	0	1
10	1490.96	0	1
10	1173.59	0	1


Set #4:

X	Y	s(Y)	weight
0.1	373.289	0	1
0.2	631.866	0	1
0.5	749.976	0	1
1	893.586	0	1
2	947.779	0	1
5	1019.15	0	1
10	1124.52	0	1
10	963.123	0	1


Set #5:

X	Y	s(Y)	weight
0.05	97	0	1
0.1	269.188	0	1
0.2	661.784	0	1
0.5	763.887	0	1
1	598.949	0	1
2	899.933	0	1
5	655.408	0	1
10	1212.77	0	1
10	1000.7	0	1
10	799.666	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 = 47.279      (variance = 2235.302)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 738.716  	  Approx SD = 24.4496	  CV = 3.3
Parameter 3: EC50  	= 0.208819  	  Approx SD = 0.0316229	  CV = 15.1
Parameter 4: nH    	= 1.26545  	  Approx SD = 0.230313	  CV = 18.2
Minimum SSD = 17882.417; 
Max log-likelihood = -56.274
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.66-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.31)
Lmax= -56.2736;   Lcrit= -58.9324
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 738.716	  LOWER = 688.501	  UPPER = 804.236
Parameter 3:   EC50	= 0.208819	  LOWER = 0.151421	  UPPER = 0.303533
Parameter 4:   nH  	= 1.26545	  LOWER = 0.833932	  UPPER = 1.95007

	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 = 200.031      (variance = 40012.205)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 1968.33  	  Approx SD = 130.193	  CV = 6.6
Parameter 3: EC50  	= 0.215201  	  Approx SD = 0.0605154	  CV = 28.1
Parameter 4: nH    	= 1.17061  	  Approx SD = 0.37115	  CV = 31.7
Minimum SSD = 280085.437; 
Max log-likelihood = -65.391
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.8-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.37)
Lmax= -65.3907;   Lcrit= -68.1873
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 1968.33	  LOWER = 1719.36	  UPPER = 2587.76
Parameter 3:   EC50	= 0.215201	  LOWER = 0.121838	  UPPER = 0.673629
Parameter 4:   nH  	= 1.17061	  LOWER = 0.51571	  UPPER = 2.73928

	Fitting session for Set 3 initialised!
*************************************************
	Set 3 fit finished
Number of point fitted = 11
Number of parameters estimated = 3
Degrees of freedom = 8
Residual error SD = 190.413      (variance = 36256.925)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 1479.68  	  Approx SD = 81.2206	  CV = 5.5
Parameter 3: EC50  	= 0.133481  	  Approx SD = 0.0337463	  CV = 25.3
Parameter 4: nH    	= 1.46511  	  Approx SD = 0.534703	  CV = 36.5
Minimum SSD = 290055.400; 
Max log-likelihood = -71.598
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

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

LIKELIHOOD INTERVALS
2.66-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.31)
Lmax= -71.5979;   Lcrit= -74.2568
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 1479.68	  LOWER = 1302.62	  UPPER = 1713.79
Parameter 3:   EC50	= 0.133481	  LOWER = 0.0754206	  UPPER = 0.247474
Parameter 4:   nH  	= 1.46511	  LOWER = 0.631852	  UPPER = 4.47542

	Fitting session for Set 4 initialised!
*************************************************
	Set 4 fit finished
Number of point fitted = 8
Number of parameters estimated = 3
Degrees of freedom = 5
Residual error SD = 61.134      (variance = 3737.335)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 1061.76  	  Approx SD = 54.8617	  CV = 5.2
Parameter 3: EC50  	= 0.165052  	  Approx SD = 0.0329177	  CV = 19.9
Parameter 4: nH    	= 0.897359  	  Approx SD = 0.215464	  CV = 24.0
Minimum SSD = 18686.673; 
Max log-likelihood = -42.376
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
3.31-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.57)
Lmax= -42.376;   Lcrit= -45.681
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 1061.76	  LOWER = 950.466	  UPPER = 1392.35
Parameter 3:   EC50	= 0.165052	  LOWER = 0.0976113	  UPPER = 0.442749
Parameter 4:   nH  	= 0.897359	  LOWER = 0.40399	  UPPER = 1.70882

	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 = 10
Number of parameters estimated = 3
Degrees of freedom = 7
Residual error SD = 193.165      (variance = 37312.858)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 960.718  	  Approx SD = 230.154	  CV = 24.0
Parameter 3: EC50  	= 0.210207  	  Approx SD = 0.217681	  CV = 103.6
Parameter 4: nH    	= 0.869484  	  Approx SD = 0.787747	  CV = 90.6
Minimum SSD = 261190.003; 
Max log-likelihood = -65.041
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= -65.0415;   Lcrit= -67.8381
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 960.718	  LOWER = 685.372	  UPPER limit not found
Parameter 3:   EC50	= 0.210207	  LOWER limit not found	  UPPER limit not found
Parameter 4:   nH  	= 0.869484	  LOWER = 0.10979	  UPPER limit not found

*************************************************
	Average of all fits:
Average of estimates of 5 sets (mean +/- sdm):
Parameter 1: Ymin  	= 0 +/- 0
	(all: 0.0	0.0	0.0	0.0	0.0
Parameter 2: Ymax  	= 1241.84 +/- 194.828
	(all: 738.715659005842	1968.3344454946823	1479.6765307307255	1061.7626887734205	960.7182737200407
Parameter 3: EC50  	= 0.186552 +/- 0.0143596
	(all: 0.20881885720019586	0.2152010388707697	0.13348085757434	0.16505199676498156	0.2102067196086906
Parameter 4: nH    	= 1.1336 +/- 0.10084
	(all: 1.2654515083988938	1.170608872982809	1.4651066329515376	0.8973588025074605	0.8694844079750131

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 = 50
Number of parameters estimated = 3
Degrees of freedom = 47
Residual error SD = 0.114      (variance = 0.013)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 0.983145  	  Approx SD = 0.0298633	  CV = 3.0
Parameter 3: EC50  	= 0.172678  	  Approx SD = 0.0224336	  CV = 13.0
Parameter 4: nH    	= 1.24258  	  Approx SD = 0.219657	  CV = 17.7
Minimum SSD = 0.607; 
Max log-likelihood = 39.351
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.03-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.01)
Lmax= 39.3515;   Lcrit= 37.3241
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 0.983145	  LOWER = 0.928648	  UPPER = 1.05303
Parameter 3:   EC50	= 0.172678	  LOWER = 0.135622	  UPPER = 0.230468
Parameter 4:   nH  	= 1.24258	  LOWER = 0.871103	  UPPER = 1.79331

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 [ ]: