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 = 1
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
10	90.5874	0	1
10	103.059	0	1
20	169.206	0	1
50	304.107	0	1
100	332.893	0	1
200	563.583	0	1
300	633.724	0	1
300	628.074	0	1
300	628.341	0	1


Set #2:

X	Y	s(Y)	weight
10	44.6445	0	1
20	96.1439	0	1
50	182.292	0	1
100	203.971	0	1
200	394.794	0	1
300	369.152	0	1
300	458.34	0	1
300	497.025	0	1


Set #3:

X	Y	s(Y)	weight
10	144.962	0	1
20	228.072	0	1
20	325.843	0	1
50	359.941	0	1
50	571.488	0	1
100	477.414	0	1
200	661.291	0	1
200	822.78	0	1
300	804.874	0	1
300	978	0	1
300	971.521	0	1
300	849.972	0	1


Set #4:

X	Y	s(Y)	weight
10	74.9393	0	1
20	168.546	0	1
50	365.788	0	1
100	473.351	0	1
200	714.229	0	1
300	689.361	0	1
300	893.892	0	1
300	907.393	0	1


Set #5:

X	Y	s(Y)	weight
20	62.6688	0	1
50	115	0	1
100	95.4777	0	1
200	223.862	0	1
300	236.678	0	1
300	220.554	0	1
300	274.139	0	1
300	259.1	0	1
300	250.555	0	1


Set #6:

X	Y	s(Y)	weight
10	59.1356	0	1
20	137.873	0	1
50	252.149	0	1
100	332.55	0	1
200	600.011	0	1
300	691.69	0	1
300	637.672	0	1
300	769.058	0	1
300	820.244	0	1
300	648.444	0	1


Set #7:

X	Y	s(Y)	weight
20	144.435	0	1
50	284.816	0	1
100	380.031	0	1
200	517.36	0	1
300	651.637	0	1
300	601.588	0	1
300	461.818	0	1
300	617.457	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 = 9
Number of parameters estimated = 3
Degrees of freedom = 6
Residual error SD = 38.907      (variance = 1513.774)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 833.059  	  Approx SD = 110.314	  CV = 13.2
Parameter 3: EC50  	= 98.1281  	  Approx SD = 34.5103	  CV = 35.2
Parameter 4: nH    	= 0.942302  	  Approx SD = 0.141313	  CV = 15.0
Minimum SSD = 9082.642; 
Max log-likelihood = -43.896
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS ARE STRONGLY CORRELATED (coeff > 0.9); try different guesses
WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
2.99-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.45)
Lmax= -43.8965;   Lcrit= -46.8904
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 833.059	  LOWER = 681.219	  UPPER limit not found
Parameter 3:   EC50	= 98.1281	  LOWER = 57.0593	  UPPER limit not found
Parameter 4:   nH  	= 0.942302	  LOWER = 0.391533	  UPPER = 1.27324

	Fitting session for Set 2 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 2 fit finished
Number of point fitted = 8
Number of parameters estimated = 3
Degrees of freedom = 5
Residual error SD = 58.008      (variance = 3364.882)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 514.603  	  Approx SD = 122.044	  CV = 23.7
Parameter 3: EC50  	= 89.4214  	  Approx SD = 43.0199	  CV = 48.1
Parameter 4: nH    	= 1.40091  	  Approx SD = 0.648799	  CV = 46.3
Minimum SSD = 16824.408; 
Max log-likelihood = -41.956
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS ARE STRONGLY CORRELATED (coeff > 0.9); try different guesses
WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
3.31-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.57)
Lmax= -41.9561;   Lcrit= -45.2611
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 514.603	  LOWER = 378.859	  UPPER limit not found
Parameter 3:   EC50	= 89.4214	  LOWER limit not found	  UPPER limit not found
Parameter 4:   nH  	= 1.40091	  LOWER = 0.314389	  UPPER = 3.27422

	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 = 98.537      (variance = 9709.618)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 1304.88  	  Approx SD = 416.122	  CV = 31.9
Parameter 3: EC50  	= 121.948  	  Approx SD = 111.454	  CV = 91.4
Parameter 4: nH    	= 0.79129  	  Approx SD = 0.229856	  CV = 29.0
Minimum SSD = 87386.563; 
Max log-likelihood = -70.386
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS ARE STRONGLY CORRELATED (coeff > 0.9); try different guesses
WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
2.56-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.26)
Lmax= -70.3864;   Lcrit= -72.9447
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 1304.88	  LOWER = 875.612	  UPPER limit not found
Parameter 3:   EC50	= 121.948	  LOWER limit not found	  UPPER limit not found
Parameter 4:   nH  	= 0.79129	  LOWER = 0.307406	  UPPER = 1.41836

	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 = 82.477      (variance = 6802.416)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 1102.03  	  Approx SD = 290.549	  CV = 26.4
Parameter 3: EC50  	= 108.998  	  Approx SD = 68.5652	  CV = 62.9
Parameter 4: nH    	= 1.05008  	  Approx SD = 0.322607	  CV = 30.7
Minimum SSD = 34012.082; 
Max log-likelihood = -44.772
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS ARE STRONGLY CORRELATED (coeff > 0.9); try different guesses
WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
3.31-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.57)
Lmax= -44.7716;   Lcrit= -48.0766
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 1102.03	  LOWER = 757.711	  UPPER limit not found
Parameter 3:   EC50	= 108.998	  LOWER limit not found	  UPPER limit not found
Parameter 4:   nH  	= 1.05008	  LOWER = 0.371317	  UPPER = 2.37846

	Fitting session for Set 5 initialised!
*************************************************
	Set 5 fit finished
Number of point fitted = 9
Number of parameters estimated = 3
Degrees of freedom = 6
Residual error SD = 30.623      (variance = 937.764)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 354.506  	  Approx SD = 118.221	  CV = 33.3
Parameter 3: EC50  	= 138.43  	  Approx SD = 106.154	  CV = 76.7
Parameter 4: nH    	= 1.02995  	  Approx SD = 0.385117	  CV = 37.4
Minimum SSD = 5626.586; 
Max log-likelihood = -41.742
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS ARE STRONGLY CORRELATED (coeff > 0.9); try different guesses
WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
2.99-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.45)
Lmax= -41.7416;   Lcrit= -44.7355
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 354.506	  LOWER = 240.091	  UPPER limit not found
Parameter 3:   EC50	= 138.43	  LOWER limit not found	  UPPER limit not found
Parameter 4:   nH  	= 1.02995	  LOWER = 0.295555	  UPPER = 2.27235

	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 = 83.957      (variance = 7048.786)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 790.022  	  Approx SD = 91.0806	  CV = 11.5
Parameter 3: EC50  	= 76.5754  	  Approx SD = 21.2811	  CV = 27.8
Parameter 4: nH    	= 1.43717  	  Approx SD = 0.417534	  CV = 29.1
Minimum SSD = 49341.499; 
Max log-likelihood = -56.709
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.8-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.37)
Lmax= -56.7091;   Lcrit= -59.5057
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 790.022	  LOWER = 657.077	  UPPER limit not found
Parameter 3:   EC50	= 76.5754	  LOWER = 49.4445	  UPPER limit not found
Parameter 4:   nH  	= 1.43717	  LOWER = 0.370631	  UPPER = 3.38511

	Fitting session for Set 7 initialised!
*************************************************
	Set 7 fit finished
Number of point fitted = 8
Number of parameters estimated = 3
Degrees of freedom = 5
Residual error SD = 65.191      (variance = 4249.931)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 857.849  	  Approx SD = 583.943	  CV = 68.1
Parameter 3: EC50  	= 123.706  	  Approx SD = 226.924	  CV = 183.4
Parameter 4: nH    	= 0.84864  	  Approx SD = 0.588843	  CV = 69.4
Minimum SSD = 21249.654; 
Max log-likelihood = -42.890
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS ARE STRONGLY CORRELATED (coeff > 0.9); try different guesses
WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
3.31-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.57)
Lmax= -42.8901;   Lcrit= -46.1951
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 857.849	  LOWER = 521.372	  UPPER limit not found
Parameter 3:   EC50	= 123.706	  LOWER limit not found	  UPPER limit not found
Parameter 4:   nH  	= 0.84864	  LOWER = 0.257637	  UPPER = 2.87409

*************************************************
	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  	= 822.422 +/- 113.125
	(all: 833.0587839032667	514.6031900390151	1304.8813992463793	1102.031990971152	354.5064053066929	790.0222253853202	857.8494942021994
Parameter 3: EC50  	= 108.172 +/- 7.55672
	(all: 98.12807965988637	89.42136150390533	121.94773360357628	108.998236660094	138.42969384568494	76.57544369010924	123.70612709424957
Parameter 4: nH    	= 1.07148 +/- 0.089133
	(all: 0.9423021137465801	1.400910439169317	0.7912902082093515	1.0500828134566087	1.0299493581183632	1.4371743015963652	0.848640070311413

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 = 64
Number of parameters estimated = 3
Degrees of freedom = 61
Residual error SD = 0.097      (variance = 0.009)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 0.90282  	  Approx SD = 0.064434	  CV = 7.1
Parameter 3: EC50  	= 78.5831  	  Approx SD = 13.6349	  CV = 17.4
Parameter 4: nH    	= 1.16172  	  Approx SD = 0.144831	  CV = 12.5
Minimum SSD = 0.573; 
Max log-likelihood = 60.070
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS ARE STRONGLY CORRELATED (coeff > 0.9); try different guesses

LIKELIHOOD INTERVALS
2-unit Likelihood Intervals  (equivalent SD for Gaussian- 2)
Lmax= 60.0696;   Lcrit= 58.0702
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 0.90282	  LOWER = 0.838396	  UPPER limit not found
Parameter 3:   EC50	= 78.5831	  LOWER = 66.761	  UPPER limit not found
Parameter 4:   nH  	= 1.16172	  LOWER limit not found	  UPPER = 1.37384

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