Skip to content

Commit d71e3fa

Browse files
committed
Also store output of External C++
1 parent 675130f commit d71e3fa

File tree

1 file changed

+145
-12
lines changed

1 file changed

+145
-12
lines changed

docsrc/users-guide/examples/Using External C++.ipynb

Lines changed: 145 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": null,
21+
"execution_count": 1,
2222
"metadata": {
2323
"nbsphinx": "hidden"
2424
},
@@ -33,9 +33,35 @@
3333
},
3434
{
3535
"cell_type": "code",
36-
"execution_count": null,
36+
"execution_count": 2,
3737
"metadata": {},
38-
"outputs": [],
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"functions {\n",
44+
" real make_odds(real theta);\n",
45+
"}\n",
46+
"data {\n",
47+
" int<lower=0> N;\n",
48+
" array[N] int<lower=0, upper=1> y;\n",
49+
"}\n",
50+
"parameters {\n",
51+
" real<lower=0, upper=1> theta;\n",
52+
"}\n",
53+
"model {\n",
54+
" theta ~ beta(1, 1); // uniform prior on interval 0, 1\n",
55+
" y ~ bernoulli(theta);\n",
56+
"}\n",
57+
"generated quantities {\n",
58+
" real odds;\n",
59+
" odds = make_odds(theta);\n",
60+
"} \n",
61+
"\n"
62+
]
63+
}
64+
],
3965
"source": [
4066
"from cmdstanpy import CmdStanModel\n",
4167
"model_external = CmdStanModel(stan_file='bernoulli_external.stan', compile=False)\n",
@@ -51,13 +77,40 @@
5177
},
5278
{
5379
"cell_type": "code",
54-
"execution_count": null,
80+
"execution_count": 3,
5581
"metadata": {
5682
"tags": [
5783
"raises-exception"
5884
]
5985
},
60-
"outputs": [],
86+
"outputs": [
87+
{
88+
"name": "stderr",
89+
"output_type": "stream",
90+
"text": [
91+
"14:52:06 - cmdstanpy - INFO - compiling stan file /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.stan to exe file /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external\n",
92+
"14:52:06 - cmdstanpy - ERROR - Stan program failed to compile:\n",
93+
"14:52:06 - cmdstanpy - WARNING - \n",
94+
"--- Translating Stan model to C++ code ---\n",
95+
"bin/stanc --o=/home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.hpp /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.stan\n",
96+
"Semantic error in '/home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.stan', line 2, column 7 to column 16:\n",
97+
" -------------------------------------------------\n",
98+
" 1: functions {\n",
99+
" 2: real make_odds(real theta);\n",
100+
" ^\n",
101+
" 3: }\n",
102+
" 4: data {\n",
103+
" -------------------------------------------------\n",
104+
"\n",
105+
"Function is declared without specifying a definition.\n",
106+
"make: *** [make/program:50: /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.hpp] Error 1\n",
107+
"\n",
108+
"Command ['make', '/home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external']\n",
109+
"\terror during processing No such file or directory\n",
110+
"\n"
111+
]
112+
}
113+
],
61114
"source": [
62115
"model_external.compile()"
63116
]
@@ -71,13 +124,35 @@
71124
},
72125
{
73126
"cell_type": "code",
74-
"execution_count": null,
127+
"execution_count": 4,
75128
"metadata": {
76129
"tags": [
77130
"raises-exception"
78131
]
79132
},
80-
"outputs": [],
133+
"outputs": [
134+
{
135+
"name": "stderr",
136+
"output_type": "stream",
137+
"text": [
138+
"14:52:06 - cmdstanpy - INFO - compiling stan file /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.stan to exe file /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external\n",
139+
"14:52:07 - cmdstanpy - ERROR - Stan program failed to compile:\n",
140+
"14:52:07 - cmdstanpy - WARNING - \n",
141+
"--- Translating Stan model to C++ code ---\n",
142+
"bin/stanc --allow-undefined --o=/home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.hpp /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.stan\n",
143+
"\n",
144+
"--- Compiling, linking C++ code ---\n",
145+
"g++ -march=native -mtune=native -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/sundials_6.0.0/src/sundials -DBOOST_DISABLE_ASSERTS -c -Wno-ignored-attributes -include /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/user_header.hpp -x c++ -o /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.o /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.hpp\n",
146+
"cc1plus: fatal error: /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/user_header.hpp: No such file or directory\n",
147+
"compilation terminated.\n",
148+
"make: *** [make/program:58: /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external] Error 1\n",
149+
"\n",
150+
"Command ['make', 'STANCFLAGS+=--allow-undefined', '/home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external']\n",
151+
"\terror during processing No such file or directory\n",
152+
"\n"
153+
]
154+
}
155+
],
81156
"source": [
82157
"model_external.compile(stanc_options={'allow-undefined':True})"
83158
]
@@ -95,11 +170,21 @@
95170
},
96171
{
97172
"cell_type": "code",
98-
"execution_count": null,
173+
"execution_count": 5,
99174
"metadata": {},
100-
"outputs": [],
175+
"outputs": [
176+
{
177+
"name": "stderr",
178+
"output_type": "stream",
179+
"text": [
180+
"14:52:07 - cmdstanpy - INFO - compiling stan file /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external.stan to exe file /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external\n",
181+
"14:52:25 - cmdstanpy - INFO - compiled model executable: /home/brian/Dev/py/cmdstanpy/docsrc/users-guide/examples/bernoulli_external\n"
182+
]
183+
}
184+
],
101185
"source": [
102-
"model_external.compile(user_header='make_odds.hpp')"
186+
"model_external.compile(user_header='make_odds.hpp')\n",
187+
"assert model_external.exe_file is not None"
103188
]
104189
},
105190
{
@@ -111,9 +196,57 @@
111196
},
112197
{
113198
"cell_type": "code",
114-
"execution_count": null,
199+
"execution_count": 6,
115200
"metadata": {},
116-
"outputs": [],
201+
"outputs": [
202+
{
203+
"name": "stderr",
204+
"output_type": "stream",
205+
"text": [
206+
"14:52:25 - cmdstanpy - INFO - CmdStan start processing\n",
207+
"chain 1 |\u001b[33m \u001b[0m| 00:00 Status\n",
208+
"\u001b[A\n",
209+
"\n",
210+
"chain 1 |\u001b[34m██████████\u001b[0m| 00:00 Sampling completed\n",
211+
"chain 2 |\u001b[34m██████████\u001b[0m| 00:00 Sampling completed\n",
212+
"chain 3 |\u001b[34m██████████\u001b[0m| 00:00 Sampling completed\n",
213+
"chain 4 |\u001b[34m██████████\u001b[0m| 00:00 Sampling completed"
214+
]
215+
},
216+
{
217+
"name": "stdout",
218+
"output_type": "stream",
219+
"text": [
220+
" "
221+
]
222+
},
223+
{
224+
"name": "stderr",
225+
"output_type": "stream",
226+
"text": [
227+
"\n",
228+
"14:52:25 - cmdstanpy - INFO - CmdStan done processing.\n"
229+
]
230+
},
231+
{
232+
"name": "stdout",
233+
"output_type": "stream",
234+
"text": [
235+
"\n"
236+
]
237+
},
238+
{
239+
"data": {
240+
"text/plain": [
241+
"array([0.20418 , 0.27078 , 0.614841 , ..., 0.124882 , 0.0797855,\n",
242+
" 0.165672 ])"
243+
]
244+
},
245+
"execution_count": 6,
246+
"metadata": {},
247+
"output_type": "execute_result"
248+
}
249+
],
117250
"source": [
118251
"fit = model_external.sample(data={'N':10, 'y':[0,1,0,0,0,0,0,0,0,1]})\n",
119252
"fit.stan_variable('odds')"

0 commit comments

Comments
 (0)