Skip to content

Commit

Permalink
Merge pull request #5 from VlachosGroup/development
Browse files Browse the repository at this point in the history
Provided examples and updated documentation
  • Loading branch information
MaxRCohen authored Jul 1, 2020
2 parents 7475cf6 + b39cd50 commit c7caf34
Show file tree
Hide file tree
Showing 49 changed files with 5,865 additions and 57 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file not shown.
Binary file added Examples/Jupyter Notebook Examples/CSTR_Data.xlsx
Binary file not shown.
433 changes: 433 additions & 0 deletions Examples/Jupyter Notebook Examples/Examples of app_ea .ipynb

Large diffs are not rendered by default.

452 changes: 452 additions & 0 deletions Examples/Jupyter Notebook Examples/Examples of batch,pfr.ipynb

Large diffs are not rendered by default.

465 changes: 465 additions & 0 deletions Examples/Jupyter Notebook Examples/Examples of cstr.ipynb

Large diffs are not rendered by default.

432 changes: 432 additions & 0 deletions Examples/Jupyter Notebook Examples/Examples of rxn_ord.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Binary file added Examples/Jupyter Notebook Examples/RO_data.xlsx
Binary file not shown.
490 changes: 490 additions & 0 deletions Examples/Jupyter Notebook Examples/Test File - CSTR Example.ipynb

Large diffs are not rendered by default.

Binary file not shown.
38 changes: 38 additions & 0 deletions Examples/Python Script Examples/App_Ea-Example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 13:18:21 2020
@author: Jane
"""

import arviz
from ckbit import app_ea

#Import data
file = './App_Ea_Data.xlsx'

#Run MAP estimation with standard priors
map1 = app_ea.MAP(filename=file)

#Run MCMC estimation with standard priors
m1, m2 = app_ea.MCMC(filename=file,control={'adapt_delta':0.99999999, 'max_treedepth':100},
iters=1000, chains=2)

#Generate pairplot
arviz.plot_pair(m1)

#Run VI estimation with standard priors
v1, v2 = app_ea.VI(filename=file)

#Process data
data_dict={'intercept':v1['sampler_params'][0],
'Ea':v1['sampler_params'][1],
'sigma':v1['sampler_params'][2]}

#Generate pairplot
arviz.plot_pair(data_dict)

#Run MCMC estimation with specified priors
p1, p2 = app_ea.MCMC(filename=file,control={'adapt_delta':0.99999999,
'max_treedepth':100}, iters=1000,
priors = ['app_ea ~ normal(90,5)'])
Binary file added Examples/Python Script Examples/App_Ea_Data.xlsx
Binary file not shown.
28 changes: 28 additions & 0 deletions Examples/Python Script Examples/CSTR-Example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 16:15:20 2020
@author: Jane
"""

import arviz
from ckbit import cstr

#Import data
file = './CSTR_Data.xlsx'

#Run MAP estimation with standard priors
map1 = cstr.MAP(filename=file, pH=True, seed=7)

#Run MCMC estimation with standard priors
m1, m2 = cstr.MCMC(filename=file, pH=True)

#Generate pairplot
arviz.plot_pair(m1)

#Run VI estimation with standard priors
v1, v2 = cstr.VI(filename=file, pH=True,
priors = ['A0[1] ~ normal(11,4)',
'Ea[1] ~ normal(95,5)',
'A0[2] ~ normal(17,4)',
'Ea[2] ~ normal(140,5)'])
Binary file added Examples/Python Script Examples/CSTR_Data.xlsx
Binary file not shown.
26 changes: 26 additions & 0 deletions Examples/Python Script Examples/PFR_Batch-Example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 15:42:04 2020
@author: Jane
"""

import arviz
from ckbit import pfr

#Import data
file = './PFR_Data.xlsx'

#Run MAP estimation with standard priors
map1 = pfr.MAP(filename=file, pH=True, rel_tol=5E-6, abs_tol=5E-6, max_num_steps=1000)

#Run MCMC estimation with standard priors
m1, m2 = pfr.MCMC(filename=file, pH=True)

#Generate pairplot
arviz.plot_pair(m1)

#Run MCMC estimation with specified priors
p1, p2 = pfr.MCMC(filename=file, pH=True,
priors = ['A0[1] ~ normal(10,5)',
'Ea[1] ~ normal(100,5)'])
Binary file added Examples/Python Script Examples/PFR_Data.xlsx
Binary file not shown.
14 changes: 14 additions & 0 deletions Examples/Python Script Examples/README-for_test_files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
First, we should say we recommend using the Jupyter notebook named
Test File - CSTR Example to test CKBIT since it is better displayed
and all functions are within a single file. However, we recognize some
users will be more comfortable with standard Python scripts, so the
test files have been provided in this format as well:

Test_file-generate_data.py - File used to generate the simulated data used for testing
Test_file-MAP.py - file used to test the MAP functionality of CKBIT
Test_file-MCMC.py - file used to test the MCMC functionality of CKBIT
Test_file-VI.py - file used to test the VI functionality of CKBIT

Please reference the Word document in this folder named Test File - Outputs Comparison
to find the examples of the outputs this software should generate when running
properly.
Binary file added Examples/Python Script Examples/RO_data.xlsx
Binary file not shown.
38 changes: 38 additions & 0 deletions Examples/Python Script Examples/Rxn_Ord-Example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 14:23:57 2020
@author: Jane
"""

import arviz
from ckbit import rxn_ord

#Import data
file = './RO_data.xlsx'

#Run MAP estimation with standard priors
map1 = rxn_ord.MAP(filename=file)

#Run MCMC estimation with standard priors
m1, m2 = rxn_ord.MCMC(filename=file,control={'adapt_delta':0.99999999,
'max_treedepth':100}, iters=1000, chains=2)

#Generate pairplot
arviz.plot_pair(m1)

#Run VI estimation with standard priors
v1, v2 = rxn_ord.VI(filename=file)

#Process data
data_dict={'intercept':v1['sampler_params'][0],
'rxn_ord':v1['sampler_params'][1],
'sigma':v1['sampler_params'][2]}

#Generate pairplot
arviz.plot_pair(data_dict)

#Run MCMC estimation with specified priors
p1, p2 = rxn_ord.MCMC(filename=file,control={'adapt_delta':0.99999999,
'max_treedepth':100}, iters=1000,
priors = ['rxn_ord ~ normal(1,0.05)'])
Binary file not shown.
20 changes: 20 additions & 0 deletions Examples/Python Script Examples/Test_file-MAP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 12:07:40 2020
@author: Jane
"""

# The MAP estimation. This yields point estimates of the modes of the posterior.
# These estimates are the values that fit the model best given the data and priors.

#These output values can be compared against the values provided in the attached Word document
#named "Test File - Outputs Comparison" in the same folder as this script.

from ckbit import cstr

#Import data
file = './CSTR_Data.xlsx'

#Run MAP estimation with standard priors
map_vals = cstr.MAP(filename=file, pH=True, seed=3)
31 changes: 31 additions & 0 deletions Examples/Python Script Examples/Test_file-MCMC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 12:07:40 2020
@author: Jane
"""

# The MCMC estimation. This yields estimates of the posterior distributions of
# each parameter being estimated.

#These output values can be compared against the values provided in the attached Word document
#named "Test File - Outputs Comparison" in the same folder as this script.

from ckbit import cstr
import arviz

#Import data
file = './CSTR_Data.xlsx'

#Run MCMC estimation with standard priors
m1, m2 = cstr.MCMC(filename=file, pH=True, seed=3, iters=10000)

# There are convergence checks to ensure that these samples can be relied upon.
# These checks are discussed in detail in the published article. This run passes all
# those checks, and offers a successful inference we can trust.

# It is important to visualize the correlation that exists between the samples of
# the parameters, which we can accomplish with a pair plot.

#Generate pairplot
arviz.plot_pair(m1)
36 changes: 36 additions & 0 deletions Examples/Python Script Examples/Test_file-VI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 12:07:40 2020
@author: Jane
"""

# The VI estimation. This yields estimates of the posterior distributions of
# each parameter being estimated, but using the VI technique instead of the MCMC.
# VI is better than MCMC at generating a large number of samples, but is a less
# robust technique. It is still in its experimental implementation phase.
# We demonstrate VI estimation below.

# We can also specify prior distributions and run inference with them. The following
# example shows the implementation of the follow prior distributions:
# For the A0 term of rxn 1, a normal distribution with a mean of 11 and standard deviation of 4.
# For the Ea term of rxn 1, a normal distribution with a mean of 95 and standard deviation of 5.
# For the Ea term of rxn 1, a normal distribution with a mean of 0.5 and standard deviation of 0.1.
# For the Ea term of rxn 2, a normal distribution with a mean of 140 and standard deviation of 5.
# All prior distribution specification must follow Stan's implementation forms:
# https://mc-stan.org/docs/2_23/functions-reference/unbounded-continuous-distributions.html

#These output values can be compared against the values provided in the attached Word document
#named "Test File - Outputs Comparison" in the same folder as this script.

from ckbit import cstr

#Import data
file = './CSTR_Data.xlsx'

#Run VI estimation with standard priors
v1, v2 = cstr.VI(filename=file, pH=True, seed=3, output_samples=10000,
priors = ['A0[1] ~ normal(11,4)',
'Ea[1] ~ normal(95,5)',
'A0[2] ~ normal(17,4)',
'Ea[2] ~ normal(140,5)'])
105 changes: 105 additions & 0 deletions Examples/Python Script Examples/Test_file-generate_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 12:07:40 2020
@author: Jane
"""

#First, we generate the CSTR data with experimental noise added to the concentrations.
#The smooth lines are the unperturbed data, and the data points are the noisy
#measurements we use as our data points.

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(40)
HMFinit = 100
LFinit = 50
Huinit = 50
taus = np.linspace (0,20,11)
Hconc = 0.1
T = [423,448]
R = 8.31446261815324*(1/1000) #units of kJ/(mol*K)

#True params
sigma = 5
A0HLF = 11.31
A0HHu = 16.69
EaHLF = 94.72
EaHHu = 141.94

try:
del cHMF0,cHMF1,cLF0,cLF1,cHu0,cHu1
except:
pass

size0 = len(taus)
cHMF0 = np.linspace(10,20,size0)
cHMF1 = np.linspace(10,20,size0)
cLF0 = np.linspace(10,20,size0)
cLF1 = np.linspace(10,20,size0)
cHu0 = np.linspace(10,20,size0)
cHu1 = np.linspace(10,20,size0)
total0 = np.linspace(10,20,size0)
total1 = np.linspace(10,20,size0)
kHLF0 = (10**A0HLF)*np.exp(-EaHLF/(R*T[0]))
kHLF1 = (10**A0HLF)*np.exp(-EaHLF/(R*T[1]))
kHHu0 = (10**A0HHu)*np.exp(-EaHHu/(R*T[0]))
kHHu1 = (10**A0HHu)*np.exp(-EaHHu/(R*T[1]))
#clean data
for i in range(size0):
kHLF0 = (10**A0HLF)*np.exp(-EaHLF/(R*T[0]))
kHLF1 = (10**A0HLF)*np.exp(-EaHLF/(R*T[1]))
kHHu0 = (10**A0HHu)*np.exp(-EaHHu/(R*T[0]))
kHHu1 = (10**A0HHu)*np.exp(-EaHHu/(R*T[1]))
cHMF0[i] = HMFinit/(1+(Hconc*taus[i]*(kHLF0+kHHu0)))
cHMF1[i] = HMFinit/(1+(Hconc*taus[i]*(kHLF1+kHHu1)))
cLF0[i] = LFinit+(Hconc*taus[i]*kHLF0*cHMF0[i])
cLF1[i] = LFinit+(Hconc*taus[i]*kHLF1*cHMF1[i])
cHu0[i] = Huinit+(Hconc*taus[i]*kHHu0*cHMF0[i])
cHu1[i] = Huinit+(Hconc*taus[i]*kHHu1*cHMF1[i])

f, ax = plt.subplots(1)
ax.plot(taus,cHMF0, label='HMF, 150C')
ax.plot(taus,cHMF1, label='HMF, 175C')
ax.plot(taus,cLF0, label='LA+FA, 150C')
ax.plot(taus,cLF1, label='LA+FA, 175C')
ax.plot(taus,cHu0, label='Humins, 150C')
ax.plot(taus,cHu1, label='Humins, 175C')


for i in range(size0):
cHMF0[i] = cHMF0[i]+np.random.normal(0,sigma,1)
cHMF1[i] = cHMF1[i]+np.random.normal(0,sigma,1)
cLF0[i] = cLF0[i]+np.random.normal(0,sigma,1)
cLF1[i] = cLF1[i]+np.random.normal(0,sigma,1)
cHu0[i] = cHu0[i]+np.random.normal(0,sigma,1)
cHu1[i] = cHu1[i]+np.random.normal(0,sigma,1)

cHMF0[0] = HMFinit
cHMF1[0] = HMFinit
cLF0[0] = LFinit
cLF1[0] = LFinit
cHu0[0] = Huinit
cHu1[0] = Huinit

for i in range (len(cHMF0)):
cHMF0[i] = max(0,cHMF0[i])
cHMF1[i] = max(0,cHMF1[i])
cLF0[i] = max(0,cLF0[i])
cLF1[i] = max(0,cLF1[i])
cHu0[i] = max(0,cHu0[i])
cHu1[i] = max(0,cHu1[i])


ax.scatter(taus,cHMF0)
ax.scatter(taus,cHMF1)
ax.scatter(taus,cLF0)
ax.scatter(taus,cLF1)
ax.scatter(taus,cHu0)
ax.scatter(taus,cHu1)
ax.set_xlabel('Residence Time (min)')
ax.set_ylabel('Noisy CSTR Outlet Concs')
ax.set_title('Noisy CSTR Outlet Concs vs. Residence Time')
ax.legend()
plt.show()
Loading

0 comments on commit c7caf34

Please sign in to comment.