What are the features which would help you using cmdstan in Emacs?

Hi everyone,

I’ve asked about emacs and stan-mode before and I was wondering what kind of features would help you working with Stan? Such as compiling the model from emacs or setting the cmdstan arguments when running the model.

I’m trying to build a little interface for fun and need some ideas for what to put it in.

Thanks!

Here are some examples of what I’m talking about :

4 Likes

@grburgess You might be interested in this

This is something I am very interested in… but do not know enough elisp to figure it out. However, I’m willing to learn to help

I have started the project here, note that it is still very basic but I hope it’ll help

3 Likes

very cool!

1 Like

Thank you for working on this. Currently, I use emacs to edit Stan. However, I then use RStan and ess to build my models.

Would you compiler work on Windows? or require a *nix system?

The interface is strictly for cmdstan because it allows me to send the cmd arguments and paths straight to the cmdstan interface.

Also, I should add that it only works on Linux and Mac because Windows does not use paths the same way and has different executable names.

All in all I think RStan and PyStan are out of the scope of this package. Sorry 🙏

you could look at CmdStanPy - the CmdStanModel class compile function wraps a call to the CmdStan makefile - https://github.com/stan-dev/cmdstanpy/blob/develop/cmdstanpy/model.py#L211

the things you need to consider:

  • need a CmdStan install (again, CmdStanPy and CmdStanR have install_cmdstan functions implemented in Python and R respectively)
  • CmdStan makefile needs to be run from the CmdStan home directory
  • requires C++ toolhain
  • also requires CmdStan install - first implementation of function install_cmdstan was as a shell script - now it’s been implemented in both Python and R

back when I started working on CmdStanPy, install_cmdstan was a bash script, but then @ahartikainen implemented everything in Python and got it all working on Windows.

when I worked on Windows machines, I did everything in Cygwin and must have had access to Emacs somehow because that’s always been my editor of choice, so give Windows users some love and more power to you!

I really like this concept. I suppose the only drawback for me is that when I use cmstanpy at the moment, I immediately save the fit via arviz to a netcdf file… but this is just my common workflow and perhaps very specific.

One thing that the emacs interface lacks which is common to most stan highlighting is some form of checking. like what happens when one compiles the model. This is not a huge problem as you can get this information on compile, but it would really help to quickly build models.

I’ve no insight into how difficult this would be but I’m sure it would be very quickly one of the coolest things for editing stan code yet.

For now the ELisp code retrieves the cmdstan directory from the user, it’s a variable to be set once and then it dynamically switches to the directory at compile time. This is relatively easy on Linux & Mac but all the paths’ names and executables are different on Windows so I’ll have to push that further into the future I believe.

I have a Linux machine and a Windows one but I use WSL2 to spin up a Linux environment so I’ve only briefly used emacs on Windows proper.

The “end goal” for the coming months is to have a simple push button interface in Emacs without the need to go to the shell everytime.

I’ll see what I can do but my (e)lisp is still quite poor so it’ll take time.

Thanks for the comment! =)

I must say that code analysis is way out of my depth for the time being, that’s a very good problem though.

If you are only interested in checking syntax, you can run
bin/stanc model.stan --o=/dev/null (bin inside cmdstan)

That will output any syntax error and if there is no output on stderr, the syntax is ok. --o=/dev/null is there just so that the generated cpp is not saved to a file.

1 Like

Oh that’s very interesting, does it compile at all or does it check for errors like a linter?

This call transpiles the Stan model to C++. If you ignore the generated C++ its basically a syntax check.

This call is also the first step when you run
make model

make model then uses the generated C++ together with the rest of the Stan core to make an executable. A call to bin/stanc ends with a generated c++. This part is really fast and can be used in this context.

p.s.:

bin/stanc model.stan --auto-format formats your Stan model (the output is a formatted stan model) but it currently does not preserve comments unfortunately.

p.p.s.:
The compiler is also available in javascript, but that probably does not help you for emacs. I had an idea a while ago to make a vscode extension (vscode extensions are written in typescript) that would enable syntax highlighting, syntax checks, auto formatting, etc but that idea is waiting for a rainy day someday :)

1 Like

That’s very good news, i know absolutely zero C++ so it’ll be a great challenge. Thanks for taking the time to explain!

Well luckily, you do not have to know anything about C++.

Example: https://rok-cesnovar.github.io/stanc3js-demo/ (excuse the zero styling on this demo webpage).

Put this model in the upper input field:

parameters {
    real y;
}
model {
    z ~ std_normal();
}

You will see: Semantic error in ‘string’, line 5, column 4 to column 5: Identifier ‘z’ not in scope.

then change z to y and you will see some non-formated generated C++.

The compile button is a call to bin/stanc (in this case to a javascript version).

3 Likes

Good stuff, I should be able to hold the arguments in a list somewhere, have people run their checks and then compile the model. At least I think I can.

1 Like

Wow… this is very cool and useful on its own.

1 Like

to expand on what Rok said, the Stanc compiler is designed to generate correct C++ code - if a Stan program is syntactically correct, the generated C++ code will compile. (it might not do the right thing - that would require implementing the full Bayesian workflow in emacs ;-) )

Thanks. A bit more on the idea behind this here: https://github.com/stan-dev/stanc3/issues/577
Want to add syntax highlighting and c++ formatting first.

1 Like