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 = "Chimera4_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 Chimera4_tofit.xlsx loaded
4 sets found.

Set #1:

X	Y	s(Y)	weight
0.02	118.351	0	1
0.05	890.994	0	1
0.1	2510.78	0	1
0.2	3095.68	0	1
0.2	2772.12	0	1
0.5	3484.96	0	1
0.5	3438.25	0	1
0.5	3113.9	0	1
1	3437.85	0	1
2	3457.35	0	1
5	3530.14	0	1
5	3282.31	0	1
10	3399.36	0	1
10	3409.8	0	1
10	3535.51	0	1
10	3361.34	0	1


Set #2:

X	Y	s(Y)	weight
0.02	302.201	0	1
0.05	1512.84	0	1
0.1	3333.47	0	1
0.2	4191.42	0	1
0.5	5045.35	0	1
1	3844.88	0	1
2	4774.56	0	1
5	4563.49	0	1
10	5376.51	0	1
10	5081.76	0	1
10	3382.57	0	1


Set #3:

X	Y	s(Y)	weight
0.02	108.542	0	1
0.05	208.954	0	1
0.1	1832.79	0	1
0.2	2959.32	0	1
0.5	2516.08	0	1
1	2764.1	0	1
2	3629.56	0	1
5	2336.38	0	1
10	4164.85	0	1
10	3711.85	0	1
10	2731.67	0	1
10	2654.25	0	1


Set #4:

X	Y	s(Y)	weight
0.02	99.9507	0	1
0.05	775.642	0	1
0.1	3164.76	0	1
0.2	3852.6	0	1
0.5	3661.87	0	1
1	4503.69	0	1
2	3558.85	0	1
5	3861.64	0	1
10	4435.38	0	1
10	4320.84	0	1
10	3268.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!
*************************************************
	Set 1 fit finished
Number of point fitted = 16
Number of parameters estimated = 3
Degrees of freedom = 13
Residual error SD = 178.932      (variance = 32016.503)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 3440.06  	  Approx SD = 58.7157	  CV = 1.7
Parameter 3: EC50  	= 0.0726803  	  Approx SD = 0.00580097	  CV = 8.0
Parameter 4: nH    	= 1.7497  	  Approx SD = 0.203995	  CV = 11.7
Minimum SSD = 416214.534; 
Max log-likelihood = -104.034
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.33-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.16)
Lmax= -104.034;   Lcrit= -106.367
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 3440.06	  LOWER = 3242.88	  UPPER = 3568.86
Parameter 3:   EC50	= 0.0726803	  LOWER = 0.0595139	  UPPER = 0.0901172
Parameter 4:   nH  	= 1.7497	  LOWER = 1.53971	  UPPER = 3.8926

	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 = 11
Number of parameters estimated = 3
Degrees of freedom = 8
Residual error SD = 773.697      (variance = 598606.302)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 4786.79  	  Approx SD = 342.74	  CV = 7.2
Parameter 3: EC50  	= 0.0649141  	  Approx SD = 0.0227063	  CV = 35.0
Parameter 4: nH    	= 1.06088  	  Approx SD = 0.424244	  CV = 40.0
Minimum SSD = 4788850.418; 
Max log-likelihood = -87.020
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= -87.0198;   Lcrit= -89.6786
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 4786.79	  LOWER = 3788.03	  UPPER = 5496.87
Parameter 3:   EC50	= 0.0649141	  LOWER = 0.0279956	  UPPER = 0.144683
Parameter 4:   nH  	= 1.06088	  LOWER = 0.56217	  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 = 616.718      (variance = 380340.603)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 3130.26  	  Approx SD = 228.916	  CV = 7.3
Parameter 3: EC50  	= 0.0956602  	  Approx SD = 0.0271153	  CV = 28.3
Parameter 4: nH    	= 2.12243  	  Approx SD = 1.3629	  CV = 64.2
Minimum SSD = 3423065.426; 
Max log-likelihood = -92.394
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

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

LIKELIHOOD INTERVALS
2.56-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.26)
Lmax= -92.3941;   Lcrit= -94.9524
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 3130.26	  LOWER = 2574.02	  UPPER = 3666.27
Parameter 3:   EC50	= 0.0956602	  LOWER = 0.0496591	  UPPER = 0.207069
Parameter 4:   nH  	= 2.12243	  LOWER = 0.66529	  UPPER limit not found

	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 = 714.238      (variance = 510135.393)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 4166.72  	  Approx SD = 309.277	  CV = 7.4
Parameter 3: EC50  	= 0.0659331  	  Approx SD = 0.0231896	  CV = 35.2
Parameter 4: nH    	= 1.04822  	  Approx SD = 0.430925	  CV = 41.1
Minimum SSD = 4081083.144; 
Max log-likelihood = -86.140
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= -86.1402;   Lcrit= -88.799
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 4166.72	  LOWER = 3126.18	  UPPER = 4814.44
Parameter 3:   EC50	= 0.0659331	  LOWER = 0.0290645	  UPPER = 0.162543
Parameter 4:   nH  	= 1.04822	  LOWER = 0.586301	  UPPER limit not found

*************************************************
	Average of all fits:
Average of estimates of 4 sets (mean +/- sdm):
Parameter 1: Ymin  	= 0 +/- 0
	(all: 0.0	0.0	0.0	0.0
Parameter 2: Ymax  	= 3880.95 +/- 322.115
	(all: 3440.0552435032914	4786.785007629682	3130.25509133731	4166.7202811881625
Parameter 3: EC50  	= 0.0747969 +/- 0.00620482
	(all: 0.07268029233903743	0.06491406086099694	0.0956602380984511	0.06593307239719108
Parameter 4: nH    	= 1.49531 +/- 0.230029
	(all: 1.7496956999636917	1.0608778072762703	2.122430700606885	1.0482222050716117

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 = 50
Number of parameters estimated = 3
Degrees of freedom = 47
Residual error SD = 0.157      (variance = 0.025)
Parameter 1: Ymin  	= 0  	  (fixed)
Parameter 2: Ymax  	= 1.05593  	  Approx SD = 0.0405656	  CV = 3.8
Parameter 3: EC50  	= 0.0868828  	  Approx SD = 0.0159353	  CV = 18.3
Parameter 4: nH    	= 0.943217  	  Approx SD = 0.188362	  CV = 20.0
Minimum SSD = 1.159; 
Max log-likelihood = 23.169
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.


LIKELIHOOD INTERVALS
2.03-unit Likelihood Intervals  (equivalent SD for Gaussian- 2.01)
Lmax= 23.1692;   Lcrit= 21.1418
Parameter 1:   Ymin	= 0	  (fixed)
Parameter 2:   Ymax	= 1.05593	  LOWER limit not found	  UPPER = 1.11785
Parameter 3:   EC50	= 0.0868828	  LOWER = 0.0413986	  UPPER = 0.134122
Parameter 4:   nH  	= 0.943217	  LOWER = 0.838763	  UPPER limit not found

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