Skip to content

Commit

Permalink
working on tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
daneschi committed Jul 15, 2023
1 parent 6fcdd20 commit 41a532e
Show file tree
Hide file tree
Showing 31 changed files with 1,097 additions and 2,623 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where `NAME` need to be replaced by
* `rc` for the RC model (Ex 3).
* `rcr` for the RCR model (Ex 4).
* `adaann` for the Friedman model example (Ex 5).
* `rcr_nofas_adaann_example` for the RCR model, combining NoFAS with adaptive annealing (AdaAnn)
* `rcr_nofas_adaann` for the RCR model, combining NoFAS with adaptive annealing (AdaAnn)

At regular intervals set by the parameter `experiment.save_interval` LINFA writes a few results files. The sub-string `NAME` refers to the experiment name specified in the `experiment.name` variable, and `IT` indicates the iteration at which the file is written. The results files are

Expand All @@ -73,7 +73,7 @@ At regular intervals set by the parameter `experiment.save_interval` LINFA write
A post processing script is also available to plot all results. To run it type

```sh
python linfa.plot_res -n NAME -i IT -f FOLDER
python -m linfa.plot_res -n NAME -i IT -f FOLDER
```
where `NAME` and `IT` are again the experiment name and iteration number corresponding to the result file of interest, while `FOLDER` is the name of the folder with the results of the inference task are kept.

Expand Down
Binary file removed docs/content/imgs/rcr/data_plot_rcr_25000_0_1-1.png
Binary file not shown.
Binary file removed docs/content/imgs/rcr/data_plot_rcr_25000_0_2-1.png
Binary file not shown.
Binary file removed docs/content/imgs/rcr/data_plot_rcr_25000_1_2-1.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/content/imgs/rcr/log_plot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/content/imgs/rcr/log_plot_rcr-1.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions docs/content/rcr.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Three-element Wndkessel Model
=============================

The three-parameter Windkessel or **RCR** model is characterized by proximal and distal resistance parameters :math:`R_{p}, R_{d} \in [100, 1500]`$ Barye :math:`\cdot` s/ml and one capacitance parameter :math:`C \in [1\times 10^{-5}, 1\times 10^{-2}]` ml/Barye.
The three-parameter Windkessel or **RCR** model is characterized by proximal and distal resistance parameters :math:`R_{p}, R_{d} \in [100, 1500]` Barye :math:`\cdot` s/ml and one capacitance parameter :math:`C \in [1\times 10^{-5}, 1\times 10^{-2}]` ml/Barye.

This model is not identifiable. The average distal pressure is only affected by the total system resistance, i.e. the sum :math:`R_{p}+R_{d}`, leading to a negative correlation between these two parameters. Thus, an increment in the proximal resistance is compensated by a reduction in the distal resistance (so the average distal pressure remains the same) which, in turn, reduces the friction encountered by the flow exiting the capacitor. An increase in the value of :math:`C` is finally needed to restore the average, minimum and maximum pressure. This leads to a positive correlation between :math:`C` and :math:`R_{d}`.

Expand All @@ -14,14 +14,14 @@ where the distal pressure is set to :math:`P_{d}=55` mmHg.

Synthetic observations are generated from :math:`N(\boldsymbol\mu, \boldsymbol\Sigma)`, where :math:`\mu=(f_{1}(\boldsymbol{z}^{*}),f_{2}(\boldsymbol{z}^{*}),f_{3}(\boldsymbol{z}^{*}))^T = (P_{p,\text{min}}, P_{p,\text{max}}, P_{p,\text{ave}})^T = (100.96, 148.02,116.50)^T` and :math:`\boldsymbol\Sigma`` is a diagonal matrix with entries :math:`(5.05, 7.40, 5.83)^T`. The budgeted number of true model solutions is 216; the fixed surrogate model is evaluated on a :math:`6\times 6\times 6 = 216` pre-grid while the adaptive surrogate is evaluated with a pre-grid of size :math:`4\times 4\times 4 = 64` and the other 152 evaluations are adaptively selected.

The results are presented in :numref:`fig_rcr_res`. The posterior samples obtained through NoFAS capture well the non-linear correlation among the parameters and generate a fairly accurate posterior predictive distribution that overlaps with the observations. Additional details can be found in :cite:p:`wang2022variational`.
This example also demonstrates how NoFAS can be combined with annealing for improved convergence. The results in :numref:`fig_rcr_res` are generated using the AdaAnn adaptive annealing scheduler with intial inverse temperature :math:`t_{0}=0.05`, KL tolerance :math:`\tau=0.01` and a batch size of 100 samples. The number of parameter updates is set to 500, 5000 and 5 for :math:`t_{0}`, :math:`t_{1}` and :math:`t_{0}<t<t_{1}`, respectively and 1000 Monte Carlo realizations are used to evaluate the denominator in :eq:`eqn:friedman1`. The posterior samples capture well the nonlinear correlation among the parameters and generate a fairly accurate posterior predictive distribution that overlaps with the observations. Additional details can be found in :cite:p:`wang2022variational,cobian2023adaann`.

.. figure:: imgs/rcr/log_plot_rcr-1.png
.. figure:: imgs/rcr/data_plot_rcr_25000_0_1-1.png
.. figure:: imgs/rcr/data_plot_rcr_25000_0_2-1.png
.. figure:: imgs/rcr/params_plot_rcr_25000_0_1-1.png
.. figure:: imgs/rcr/params_plot_rcr_25000_0_2-1.png
.. figure:: imgs/rcr/params_plot_rcr_25000_1_2-1.png
.. figure:: imgs/rcr/log_plot-1.png
.. figure:: imgs/rcr/data_plot_rcr_nofas_adaann_8400_0_1-1.png
.. figure:: imgs/rcr/data_plot_rcr_nofas_adaann_8400_0_2-1.png
.. figure:: imgs/rcr/params_plot_rcr_nofas_adaann_8400_0_1-1.png
.. figure:: imgs/rcr/params_plot_rcr_nofas_adaann_8400_0_2-1.png
.. figure:: imgs/rcr/params_plot_rcr_nofas_adaann_8400_1_2-1.png
:name: fig_rcr_res

Results from the RCR model. Loss profile (top), posterior predictive distribution (center) and posterior samples (bottom).
10 changes: 6 additions & 4 deletions linfa/plot_res.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def plot_outputs(sample_file,obs_file,idx1,idx2,out_dir,out_info):
action=None,
# nargs='+',
const=None,
default='./tests/results/',
default='./',
type=str,
required=False,
help='Folder with experiment results',
Expand Down Expand Up @@ -132,7 +132,7 @@ def plot_outputs(sample_file,obs_file,idx1,idx2,out_dir,out_info):
print('Plotting log...')
plot_log(log_file,out_dir)
else:
print('Log file not found...')
print('Log file not found: '+log_file)

# Plot 2D slice of posterior samples
if(os.path.isfile(param_file) and os.path.isfile(LL_file)):
Expand All @@ -142,7 +142,8 @@ def plot_outputs(sample_file,obs_file,idx1,idx2,out_dir,out_info):
for loopB in range(loopA+1, tot_params):
plot_params(param_file,LL_file,loopA,loopB,out_dir,out_info)
else:
print('File with posterior samples not found...')
print('File with posterior samples not found: '+param_file)
print('File with log-density not found: '+LL_file)

# Plot 2D slice of outputs and observations
if(os.path.isfile(output_file) and os.path.isfile(obs_file)):
Expand All @@ -152,4 +153,5 @@ def plot_outputs(sample_file,obs_file,idx1,idx2,out_dir,out_info):
for loopB in range(loopA+1, tot_outputs):
plot_outputs(output_file,obs_file,loopA,loopB,out_dir,out_info)
else:
print('File with posterior predictive samples not found...')
print('File with posterior predictive samples not found: '+output_file)
print('File with observations not found: '+obs_file)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "linfa_vi"
version = "1.1.5"
version = "1.1.6"
description = "A Python library for inference with normalizing flow and annealing"
readme = "README.md"
authors = [{ name = "resDesLab ", email = "daniele.schiavazzi@gmail.com" }]
Expand Down
58 changes: 0 additions & 58 deletions tutorial/README.md

This file was deleted.

Loading

0 comments on commit 41a532e

Please sign in to comment.