Skip to content

Jonghyun-Yun/LSA

Repository files navigation

LSA: Latent Space Accumulator model

LSA runs based on C++ for Gibbs samplers and R / Rcpp for data preprocessing and posterior sample analysis. The Tempest depends on the Stan math library. We should build the Stan math library, and then you can build the Tempest executable.

Building the Stan math library

First, you need to build Stan Math Library obtained from https://github.com/stan-dev/math. I am using develop commit fd1d8917ee5754810cfab4f7871760315e075135

The below is a shell script borrowed from https://github.com/stan-dev/math. It downloads and builds the Stan Math Library. You may want to change the target directory path/to/stan-math.

git clone https://github.com/stan-dev/math.git path/to/stan-math
make -f path/to/stan-math/make/standalone math-clean
make -j4 -f path/to/stan-math/make/standalone math-libs

The StanHeaders R package may be useful if you want to use the Stan Math Library in the Rcpp.

install.packages("StanHeaders")

For more inforamtion, please see https://cran.r-project.org/web/packages/StanHeaders/vignettes/stanmath.html I initialized an R package in an art directory using the STAN header.

CMake

The below is a CMake build script. You need to set MATH variable to indicate the Stan Math Library path (here, it is set to /Users/yunj/stan-dev/math). Then, save the script as CMakeLists.txt.

 cmake_minimum_required(VERSION 3.0)
 project(tempest)

 set(MATH /Users/yunj/stan-dev/math)
 set(PROJROOT .)
 set(Xcode_CLT /Library/Developer/CommandLineTools)

 set(CMAKE_CXX_FLAGS_DEBUG "-g")
 set(CMAKE_CXX_FLAGS_RELEASE "-O3")

 # set(CMAKE_CXX_STANDARD 11)

 # # STAN math lib
 include_directories(${MATH}/lib/tbb_2019_U8/include)
 include_directories(${MATH})
 include_directories(${MATH}/lib/eigen_3.3.7)
 include_directories(${MATH}/lib/boost_1.72.0)
 include_directories(${MATH}/lib/sundials_5.2.0/include)

 # xcode commandline toolchain
 include_directories(${Xcode_CLT}/usr/include/c++/v1)
 include_directories(${Xcode_CLT}/usr/lib/clang/11.0.0/include)
 include_directories(${Xcode_CLT}/usr/include)
 include_directories(${Xcode_CLT}/SDKs/MacOSX.sdk/usr/include)

 # set compiler
 # SET(CMAKE_CXX_COMPILER "/usr/bin/clang++")
 # SET(CMAKE_CC_COMPILER "/usr/bin/clnag")

 # c++ linker
 # SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -isystem/Library/Developer/CommandLineTools/usr/include/c++/v1")
 SET(CMAKE_CXX_FLAGS  "-std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT ${CMAKE_CXX_FLAGS}")
 SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -DSTAN_THREADS")
 SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -DBOOST_DISABLE_ASSERTS")
 SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wl,-L,${MATH}/lib/tbb -Wl,-rpath,${MATH}/lib/tbb")
 SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -isysroot ${Xcode_CLT}/SDKs/MacOSX.sdk/usr/include")
 SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk")
 # SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-unused-local-typedef -Wno-unused-function -Wno-ignored-qualifiers -Wno-unused-variable -Wno-sign-compare -Wno-infinite-recursion")

include_directories(${PROJROOT}/include)
file(GLOB SOURCES
    ${PROJROOT}/include/*.h
    ${PROJROOT}/include/*.hpp
    ${PROJROOT}/src/*.cpp
)

 add_executable(main main.cpp ${SOURCES})
 target_link_libraries(main ${CMAKE_CXX_FLAGS} ${MATH}/lib/sundials_5.2.0/lib/libsundials_nvecserial.a ${MATH}/lib/sundials_5.2.0/lib/libsundials_cvodes.a ${MATH}/lib/sundials_5.2.0/lib/libsundials_idas.a ${MATH}/lib/sundials_5.2.0/lib/libsundials_kinsol.a ${MATH}/lib/tbb/libtbb.dylib ${MATH}/lib/tbb/libtbbmalloc.dylib ${MATH}/lib/tbb/libtbbmalloc_proxy.dylib)

How to use

In the project root folder, use cmake and then make to build the executable program main.

cmake .
make -j8
  • See run.sh for an example to use the main executable.
  • The main executable needs arguments to run. Specify full if there is no missing; sparse otherwise. See run.sh for an example.
  • pisa-preprocess.R contains R scripts to preprocess the PISA data (cluster S01).
  • opusIII-preprocess.R contains R scripts to preprocess the opusIII data.
    • These two files use survival package to calculate $t - sj$ and the smallest $j$ such that $sj+1 > t$
    • You can specify hyperparameters and/or an MH jumping step size in these file.
  • art-functions.R contains R functions.
  • art_1.0.tar.gz is an R package source file built. You can also build it using sources in an art directory. This package contains functions to calculate a cumulative incidence function, an accuracy function, etc.
  • load-outputs.R contains R scripts to load MCMC sample from the main executable.
  • Other script files in an R directory is for visualization and summary for MCMC samples.
  • ljsm-code.org is what I use for the literate programming. All R functions and scripts are tangles from this file.

    You can safely ignore anything after this line. These are for my own reference.

Generate compile_commands.json using CMAKE

This is for compiling with the -g flag for debugging. If you are not using gdb, please ignore this part. The executable will be compiled in a Debug directory.

rm -r CMakeFiles/ Debug/ cmake_install.cmake CMakeCache.txt Makefile compile_commands.json
# export CXX="/usr/local/opt/llvm/bin/clang++"
# export CC="/usr/local/opt/llvm/bin/clang"
cmake -H. -BDebug -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=YES
ln -s Debug/compile_commands.json

Debug: LLDB

(dap-register-debug-template
   "LLDB::lsjm-main"
  (list :type "lldb"
        :request "launch"
        :name "LLDB::lsjm-main"
        :arguments "initialize parallel single_w single_z full latent gamma true single no_ars nonzero_theta zero_beta lambda_free 1 100 100 10"
        :target "/Users/yunj/workspace/lsjm-code/Debug/main"
        :cwd "/Users/yunj/workspace/lsjm-code/"
        ))