cmdStan - direct read and write from/to MATLAB's .mat files

Hello,

I would like to implement an interface for cmdStan to read and write to MATLAB’s .mat files. I have fully integrated and working Stan with the MATLAB but in my application direct read and write to .mat (rather than JSON or RDump) would offer some benefits.

I’ve already started to read though the source code and consulted Wiki for developers but I am struggling a bit to find relevant bits that I need to patch to add this functionality. I am not a s/w engineer but I can read C++ code and I am keen to explore relevant documentation. Any chance somebody could point me into the right direction. Where I can find relevant documentation or what part of the code I should focus on to add the functionality to open, read, parse and pass to Stan data from .mat file?

BTW: I am aware I need to do my homework first and I am not looking for easy answers with code samples etc. I already have a prototype for reading and extracting data from .mat file generated by my model. I just need a bit of help to get through complexity of Stan implantation and how the I/O interfaces are managed.

Regards,
Pawel

Hi Pawel,

For reading data, the abstract class you will want to implement is called var_contextstan/var_context.hpp at develop · stan-dev/stan · GitHub

The RDump reader can be found here: stan/dump.hpp at 94233d9c81eaa447a1385da4761880ccc9cce95b · stan-dev/stan · GitHub

The JSON reader uses the RapidJSON library and can be found here: stan/src/stan/io/json at develop · stan-dev/stan · GitHub

Writing files requires implementing the callbacks::writer class, but this is not really well suited for formats other than CSV at the moment (We’re working on it)

@WardBrian Thank you for valuable tips. I looked into it and despite my poor C++ skills I have a good idea now how to proceed.

I came across a few hurdles with using MATLAB API. Its new release has a very good C++ integration. The exception is access of .mat which has not been updated yet. I hoped to use nice integration with fstream that Stan’s using and minimise the carve up but, I guess, I have to wait for API update.

I will keep going, modify the code and try to access .mat but in meantime I realised I may be able to quicker connect Stan to running MATLAB instance and get access to its workspace with all the variables in vectors ready to go on Stan’s stack.

I am more of C and Python person with zero C++ experience so it is steep learning curve for me. However, I enjoy it a lot and thanks again for taking some time to help me!

BTW: If anybody is interested in the progress do let me know. I can share the code when done but it won’t be as pretty as Stan’s I/O implementation :-)