-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add getting started for Python and R.
- Loading branch information
1 parent
9eaf53e
commit e6bcdb5
Showing
10 changed files
with
190 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
FROM python:3.10 | ||
# Install R and dependencies. | ||
RUN apt-get update && apt-get install -y \ | ||
r-base \ | ||
r-cran-devtools \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
WORKDIR /workdir | ||
COPY setup.R . | ||
RUN Rscript setup.R | ||
|
||
# Install Python dependencies and compile cmdstan. | ||
COPY requirements.txt . | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
RUN python -m cmdstanpy.install_cmdstan --verbose --version 2.33.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
getting_started |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Install the packages. We explicitly specify the repos here so we don't get asked for the mirror when | ||
rendering the Rmarkdown. | ||
|
||
```{r} | ||
install.packages( | ||
"cmdstanr", | ||
repos = c("https://mc-stan.org/r-packages/", "http://cran.us.r-project.org") | ||
) | ||
install.packages("gptoolsStan", repos=c("http://cran.us.r-project.org")) | ||
``` | ||
|
||
Compile and run the model. | ||
|
||
```{r} | ||
library(cmdstanr) | ||
library(gptoolsStan) | ||
model <- cmdstan_model( | ||
stan_file="getting_started.stan", | ||
include_paths=gptools_include_path(), | ||
) | ||
fit <- model$sample( | ||
data=list(n=100, sigma=1, length_scale=0.1, period=1), | ||
chains=1, | ||
iter_warmup=500, | ||
iter_sampling=50 | ||
) | ||
f <- fit$draws("f") | ||
dim(f) | ||
``` | ||
|
||
Expected output: `[1] 50 1 100` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d2b9dac2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
">>> import cmdstanpy\n", | ||
">>> from gptools.stan import get_include\n", | ||
">>>\n", | ||
">>> model = cmdstanpy.CmdStanModel(\n", | ||
"... stan_file=\"getting_started.stan\",\n", | ||
"... stanc_options={\"include-paths\": get_include()},\n", | ||
"... )\n", | ||
">>> fit = model.sample(\n", | ||
"... data = {\"n\": 100, \"sigma\": 1, \"length_scale\": 0.1, \"period\": 1},\n", | ||
"... chains=1,\n", | ||
"... iter_warmup=500,\n", | ||
"... iter_sampling=50,\n", | ||
"... )\n", | ||
">>> fit.f.shape" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "34ffcd17", | ||
"metadata": {}, | ||
"source": [ | ||
"Expected output: `(50, 100)`" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
jupytext: | ||
text_representation: | ||
extension: .md | ||
format_name: myst | ||
format_version: 0.13 | ||
jupytext_version: 1.15.1 | ||
kernelspec: | ||
display_name: Python 3 (ipykernel) | ||
language: python | ||
name: python3 | ||
--- | ||
|
||
```{code-cell} ipython3 | ||
>>> import cmdstanpy | ||
>>> from gptools.stan import get_include | ||
>>> | ||
>>> model = cmdstanpy.CmdStanModel( | ||
... stan_file="getting_started.stan", | ||
... stanc_options={"include-paths": get_include()}, | ||
... ) | ||
>>> fit = model.sample( | ||
... data = {"n": 100, "sigma": 1, "length_scale": 0.1, "period": 1}, | ||
... chains=1, | ||
... iter_warmup=500, | ||
... iter_sampling=50, | ||
... ) | ||
>>> fit.f.shape | ||
``` | ||
|
||
Expected output: `(50, 100)` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
functions { | ||
#include gptools/util.stan | ||
#include gptools/fft.stan | ||
} | ||
|
||
data { | ||
int n; | ||
real<lower=0> sigma, length_scale, period; | ||
} | ||
|
||
transformed data { | ||
vector [n %/% 2 + 1] cov_rfft = | ||
gp_periodic_exp_quad_cov_rfft(n, sigma, length_scale, period) + 1e-9; | ||
} | ||
|
||
parameters { | ||
vector [n] f; | ||
} | ||
|
||
model { | ||
f ~ gp_rfft(zeros_vector(n), cov_rfft); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
repos <- c("https://mc-stan.org/r-packages/", "http://cran.us.r-project.org") | ||
install.packages("cmdstanr", repos=repos) | ||
devtools::install_github("onnela-lab/gptoolsStan") |