HTTP-based interface to Stan, a package for Bayesian inference.
Project description
HTTP-based REST interface to Stan, a package for Bayesian inference.
An HTTP 1.1 interface to the Stan C++ package, httpstan is a shim that allows users to interact with the Stan C++ library using a REST API. The package is intended for use as a universal backend for frontends which know how to make HTTP requests. The primary audience for this package is developers.
In addition to providing the essential functionality of the command-line interface to Stan (CmdStan) over HTTP, httpstan provides the following features:
Automatic caching of compiled Stan models
Automatic caching of samples from Stan models
Parallel sampling
Documentation: https://httpstan.readthedocs.org.
Requirements
Linux or macOS
C++ compiler: gcc ≥9.0 or clang ≥10.0.
Background
httpstan is a shim allowing clients able to make HTTP-based requests to call functions in the Stan C++ library’s stan::services namespace. httpstan was originally developed as a “backend” for a Stan interface written in Python, PyStan.
Stability and maintainability are two overriding goals of this software package.
Install
$ python3 -m pip install httpstan
In order to install httpstan from PyPI make sure your system satisfies the requirements:
Linux or macOS
x86-64 CPU
C++ compiler: gcc ≥9.0 or clang ≥10.0.
If your system uses a different kind of CPU, you should be able to install from source using the following commands:
# Build shared libraries make # Build the httpstan wheel on your system python3 -m pip install poetry python3 -m poetry build # Install the wheel python3 -m pip install dist/*.whl
Usage
After installing httpstan, running the module will begin listening on localhost, port 8080:
python3 -m httpstan
In a different terminal, make a POST request to http://localhost:8080/v1/models with Stan program code to compile the program:
curl -H "Content-Type: application/json" \ --data '{"program_code":"parameters {real y;} model {y ~ normal(0,1);}"}' \ http://localhost:8080/v1/models
This request will return a model name along with all the compiler output:
{"compiler_output": "In file included from …", "stanc_warnings": "", "name": "models/xc2pdjb4"}
(The model name depends on the platform and the version of Stan.)
Drawing samples from this model using default settings requires two steps: (1) launching the sampling operation and (2) retrieving the output of the operation (once it has finished).
First we make a request to launch the sampling operation:
curl -H "Content-Type: application/json" \ --data '{"function":"stan::services::sample::hmc_nuts_diag_e_adapt"}' \ http://localhost:8080/v1/models/xc2pdjb4/fits
This request instructs httpstan to draw samples from the normal distribution described in the model. The function name picks out a specific function in the stan::services namespace found in the Stan C++ library (see the Stan C++ documentation for details). This request will return immediately with a reference to a long-running fit operation:
{"name": "operations/gkf54axb", "done": false, "metadata": {"fit": {"name": "models/xc2pdjb4/fits/gkf54axb"}}}
Once the operation is complete, the “fit” can be retrieved. The name of the fit, models/xc2pdjb4/fits/gkf54axb, is included in the metadata field of the operation. The fit is saved as sequence of JSON-encoded messages. These messages are strung together with newlines. To retrieve these messages, saving them locally in the file myfit.jsonlines, make the following request:
curl http://localhost:8080/v1/models/xc2pdjb4/fits/gkf54axb > myfit.jsonlines
The Stan “fit”, saved in myfit.jsonlines, aggregates all messages. By reading them one by one you can recover all messages sent by the Stan C++ library.
Citation
We appreciate citations as they let us discover what people have been doing with the software. Citations also provide evidence of use which can help in obtaining grant funding.
To cite httpstan in publications use:
Riddell, A., Hartikainen, A., & Carter, M. (2021). httpstan (4.4.0). https://pypi.org/project/httpstan
Or use the following BibTeX entry:
@misc{httpstan, title = {httpstan (4.4.0)}, author = {Riddell, Allen and Hartikainen, Ari and Carter, Matthew}, year = {2021}, month = mar, howpublished = {PyPI} }
Please also cite Stan.
License
ISC License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for httpstan-4.10.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 821469883091523fef2c6d806bbfdb43efa07ec0df662ba58ccda9e773224640 |
|
MD5 | f4705e7ff148c40141ac46d60e7099d8 |
|
BLAKE2b-256 | 215810d5bb827a623feb42afeebfe36fd9877d9d711cc970e1a46083d835d453 |
Hashes for httpstan-4.10.1-cp311-cp311-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 007e09c3f3e52d17f02a1a1546c89480209e25ccfd11c454b39a670fbc31fad2 |
|
MD5 | 103417851be877c0c98471f4749ff168 |
|
BLAKE2b-256 | 28f0ad5249ca2df71ad723afbf4c6c72b8808ea49574555118460d74f8131b09 |
Hashes for httpstan-4.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 440d2b3002863dbacbf87d64ebe5cead41880b53ef5736f624af4dc44d1e5161 |
|
MD5 | 30058f746416db5c2504786f31d375f6 |
|
BLAKE2b-256 | 95fe3861743c0ec4aed8b2fb9e5251344acba0aad13092f97c8db900b6616590 |
Hashes for httpstan-4.10.1-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16b81ae0f13b114cbbecc1514c1f6ab4f9f84afb622500533de933fcfd19f155 |
|
MD5 | 57ee55a7dbcc586009c83518c34d3a35 |
|
BLAKE2b-256 | 146bf56e6816383b55451d32bf796dd5ee19dc3d097a2d78379c6b7943ddce66 |
Hashes for httpstan-4.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e28f73b7be79befc843d86cf9d96938561503242ab0965695d6f99ce9b89539b |
|
MD5 | 89b619293cee802ae411532c52c1e070 |
|
BLAKE2b-256 | c55e8126cb3dd3940d7c04967e8460839b3d5d5e6319b1d75859fe65acf59b84 |
Hashes for httpstan-4.10.1-cp39-cp39-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c068eb80a323f40542ee46c208a67001efadfead319023b92842b1daddf60961 |
|
MD5 | 71348cc196a4982319250004a6314cac |
|
BLAKE2b-256 | 26e6cb1a3a7a3652b818b21f64a8fdd33c7f4e5ee09705c408ae3a8fb1d7d815 |
Hashes for httpstan-4.10.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2bea7c2f390cdacf5cf68130895489f3a901539d87eeaa40e06d95fba461864b |
|
MD5 | d6382840f3f8ed9b7766b64acd8f76cd |
|
BLAKE2b-256 | abceb023bbfe18c5c6dc877acfe020caccd0044022fb4fc1cf1171a8c6aa7904 |
Hashes for httpstan-4.10.1-cp38-cp38-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1051012dc56dfba5e809604e5eaacc42c9f322618a59df5b16552825f914e84a |
|
MD5 | 3b4a0049f7f5c7ca772a98017f43675e |
|
BLAKE2b-256 | 360f8c062c434730b356eed99893666f67dc18dead91758d770a6c27d420a87f |