Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Hierarchy #13

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ae3022d
changed bayesmix submodule to forked
taguhiM Apr 9, 2022
d544420
test commit to check submodules - done
taguhiM Apr 9, 2022
c1ece8c
check submodule behaviour
taguhiM Apr 15, 2022
e6381e2
adding lib/math submodule
taguhiM Apr 15, 2022
09b080e
Python hierarchy in external main
EnricoPaglia Apr 21, 2022
09dd210
update .gitignore
taguhiM Apr 25, 2022
13296ec
migrate compute_posterior_hypers : running super slow
taguhiM Apr 25, 2022
2220011
migrate summary_statistics methods
taguhiM Apr 26, 2022
b4dd38d
change append to subscript (faster)
taguhiM Apr 30, 2022
9404c84
fixed numerical mistakes in compute_posterior_hypers, from append to …
taguhiM May 2, 2022
4b32c0a
declare as global vars all fun.attr methods
taguhiM May 2, 2022
b015840
submodule update
taguhiM May 3, 2022
8107d0c
added namespace for python global variables (attr)
taguhiM May 3, 2022
d674670
modified to use pybind11 built-in type conversions (stl, eigen)
taguhiM May 3, 2022
7b673b3
modify summary statistics to be stored in a vector
taguhiM May 9, 2022
59d2b6c
initial trials to run from python
taguhiM May 9, 2022
d7c91e5
update bayesmix - modified tbb in cmake
taguhiM May 12, 2022
e6ba3d3
Merge branch 'bayesmix-dev:master' into master
taguhiM May 13, 2022
8e7d10a
build with cmake instead of setup
taguhiM May 18, 2022
c377845
update submodules
taguhiM May 18, 2022
846a99b
Merge branch 'master' into build_cmake
taguhiM May 18, 2022
b2cd9ca
Merge branch 'py_hier' into build_cmake
taguhiM May 18, 2022
b73cc75
first working run from python, yay!
taguhiM May 18, 2022
0acfa82
add cmake in the bash script
taguhiM May 19, 2022
9b8247f
Implementing pythonhierarchy int Python
EnricoPaglia May 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ dmypy.json

# Pyre type checker
.pyre/

# PyCharm
.idea/
7 changes: 6 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
[submodule "pybmix/src/bayesmix"]
path = pybmix/core/pybmixcpp/bayesmix
url = https://github.com/bayesmix-dev/bayesmix.git
url = https://github.com/taguhiM/bayesmix.git
depth = 1
update = merge
[submodule "lib/pybind11"]
path = lib/pybind11
depth = 1
url = https://github.com/pybind/pybind11.git
[submodule "lib/math"]
path = lib/math
depth = 1
url = https://github.com/bayesmix-dev/math.git
ignore = dirty
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ target_compile_definitions(pybmixcpp PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO
target_link_libraries(pybmixcpp PUBLIC bayesmixlib ${BAYESMIX_LINK_LIBRARIES})
target_compile_options(pybmixcpp PUBLIC ${BAYESMIX_COMPILE_OPTIONS})

add_custom_target(genterate_protos ALL DEPENDS ${PROTO_PYS})
add_custom_target(generate_protos ALL DEPENDS ${PROTO_PYS})
add_custom_target(two_to_three ALL COMMAND ${CMAKE_CURRENT_LIST_DIR}/convert_proto.sh DEPENDS generate_protos)
6 changes: 6 additions & 0 deletions convert_proto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/..

# NEEDS TO BE SET MANUALLY
ENV_DIR=/home/taguhi/miniconda3/envs/pybmix3.9

$ENV_DIR/bin/2to3 --output-dir=$SCRIPT_DIR/pybmix/proto -W -n $SCRIPT_DIR/pybmix/proto
48 changes: 48 additions & 0 deletions docs/examples/test_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import sys
HERE = os.path.dirname(os.path.realpath(__file__))
BUILD_DIR = os.path.join(HERE, "../../")
sys.path.insert(0, os.path.realpath(BUILD_DIR))

import numpy as np
import matplotlib.pyplot as plt
from pybmix.core.mixing import DirichletProcessMixing
from pybmix.core.hierarchy import PythonHierarchy
from pybmix.core.mixture_model import MixtureModel
np.random.seed(2021)


def sample_from_mixture(weights, means, sds, n_data):
n_comp = len(weights)
clus_alloc = np.random.choice(np.arange(n_comp), p=[0.5, 0.5], size=n_data)
return np.random.normal(loc=means[clus_alloc], scale=sds[clus_alloc])


y = sample_from_mixture(np.array([0.5, 0.5]), np.array([-3, 3]), np.array([1, 1]), 200)


plt.hist(y)
plt.show()

mixing = DirichletProcessMixing(total_mass=5)
hierarchy = PythonHierarchy()
hierarchy.make_default_fixed_params(y, 2)
mixture = MixtureModel(mixing, hierarchy)

mixture.run_mcmc(y, algorithm="Neal2", niter=2000, nburn=1000)

from pybmix.estimators.density_estimator import DensityEstimator

grid = np.linspace(-6, 6, 500)
dens_est = DensityEstimator(mixture)
densities = dens_est.estimate_density(grid)

plt.hist(y, density=True)
plt.plot(grid, np.mean(densities, axis=0), lw=3, label="predictive density")
idxs = [5, 100, 300]

for idx in idxs:
plt.plot(grid, densities[idx, :], "--", label="iteration: {0}".format(idx))
plt.legend()
plt.show()

2 changes: 1 addition & 1 deletion lib/pybind11
Submodule pybind11 updated 213 files
7 changes: 7 additions & 0 deletions make_.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

cd build/;
cmake ..;
make pybmixcpp;
make generate_protos;
make two_to_three;
220 changes: 220 additions & 0 deletions pybmix/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
cmake_minimum_required(VERSION 3.14.0)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
message("CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}")

find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()

project(bayesmix)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release)

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -funroll-loops -ftree-vectorize")
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)

# Require PkgConfig
find_package(PkgConfig REQUIRED)
find_package(OpenMP REQUIRED)
find_package(pybind11 REQUIRED)

# TBB CMake integration
message(STATUS "Using math TBB")
# Define TBB_ROOT Folder
set(TBB_ROOT ${CMAKE_CURRENT_LIST_DIR}/pybmixcpp/bayesmix/lib/math/lib/tbb)
file(COPY ${CMAKE_CURRENT_LIST_DIR}/pybmixcpp/bayesmix/lib/math/lib/tbb_2019_U8/ DESTINATION ${TBB_ROOT})
# Build TBB Library with CMake Integration
include(${TBB_ROOT}/cmake/TBBBuild.cmake)
list(APPEND MAKE_ARGS "tbb_build_dir=${TBB_ROOT}")
list(APPEND MAKE_ARGS "tbb_build_prefix=tbb")
tbb_build(TBB_ROOT ${TBB_ROOT} CONFIG_DIR TBB_DIR MAKE_ARGS ${MAKE_ARGS})
# Require TBB library (for compile / link options)
find_package(TBB)

# Check if Protobuf is present in system
find_package(Protobuf)
if (NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND AND NOT TARGET protobuf::libprotobuf)
if (${CMAKE_VERSION} VERSION_LESS "3.20.0")
message(FATAL_ERROR
"Your cmake version is too old: either install a newer version (>=3.20)"
" or install google's protocol buffer (protobuf) library by hand.")
endif()

include(FetchContent)
set(FETCHCONTENT_QUIET OFF)
set(FETCHCONTENT_UPDATES_DISCONNECTED ON)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_TESTING OFF)

message(CHECK_START "Fetching Protobuf")
list(APPEND CMAKE_MESSAGE_INDENT " ")

option(protobuf_BUILD_TESTS "" OFF)
set(protobuf_BUILD_EXPORT OFF)
set(protobuf_MSVC_STATIC_RUNTIME OFF)
FetchContent_Declare(
protobuf
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
GIT_TAG "v3.14.0"
GIT_SUBMODULES ""
SOURCE_SUBDIR cmake)
FetchContent_MakeAvailable(protobuf)

list(POP_BACK CMAKE_MESSAGE_INDENT)
message(CHECK_PASS "fetched")
message("Protobuf_FOUND ${Protobuf_FOUND}")
message(" --> PROTOBUF LIB: ${PROTOBUF_LIBRARIES}")
message(" --> PROTOBUF INCLUDE: ${PROTOBUF_INCLUDE_DIRS}")
message(" --> PROTOBUF VERSION: ${Protobuf_VERSION}")
message(" --> PROTOBUF Found: ${Protobuf_FOUND}")

endif()

option(DISABLE_TESTS
"If tests should be compiled or no" OFF)
option(DISABLE_BENCHMARKS
"If benchmarks should be compiled or no" OFF)
option(DISABLE_DOCS
"If docs should be generated or no" OFF)
option(BUILD_RUN "" ON)

# set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(BASEPATH "${CMAKE_CURRENT_LIST_DIR}")
set(INCLUDE_PATHS
${BASEPATH}
${CMAKE_CURRENT_LIST_DIR}/pybmixcpp/bayesmix/lib/math
${CMAKE_CURRENT_LIST_DIR}/pybmixcpp/bayesmix/lib/math/lib/boost_1.72.0
${CMAKE_CURRENT_LIST_DIR}/pybmixcpp/bayesmix/lib/math/lib/eigen_3.3.9
# TBB already included
${CMAKE_CURRENT_BINARY_DIR}
${protobuf_SOURCE_DIR}/pybmixcpp/bayesmix/src
${protobuf_SOURCE_DIR}/src
)


set(LINK_LIBRARIES
pthread
protobuf::libprotobuf
TBB::tbb
OpenMP::OpenMP_CXX
pybind11::embed
pybind11::pybind11
)

set(COMPILE_OPTIONS -D_REENTRANT -fPIC)

file(GLOB ProtoFiles "${BASEPATH}/pybmixcpp/bayesmix/src/proto/*.proto")
set(PROTO_DIR proto)

foreach(PROTO_FILE IN LISTS ProtoFiles)
message(STATUS "protoc proto(cc): ${PROTO_FILE}")
get_filename_component(PROTO_DIR ${PROTO_FILE} DIRECTORY)
get_filename_component(PROTO_NAME ${PROTO_FILE} NAME_WE)
set(PROTO_HDR ${CMAKE_CURRENT_BINARY_DIR}/${PROTO_NAME}.pb.h)
set(PROTO_SRC ${CMAKE_CURRENT_BINARY_DIR}/${PROTO_NAME}.pb.cc)
message(STATUS "protoc hdr: ${PROTO_HDR}")
message(STATUS "protoc src: ${PROTO_SRC}")
add_custom_command(
OUTPUT ${PROTO_SRC} ${PROTO_HDR}
COMMAND protobuf::protoc "--proto_path=${BASEPATH}/pybmixcpp/bayesmix/src/proto"
${PROTO_DIRS} "--cpp_out=${PROJECT_BINARY_DIR}" ${PROTO_FILE}
DEPENDS ${PROTO_FILE} protobuf::protoc
COMMENT "Generate C++ protocol buffer for ${PROTO_FILE}"
VERBATIM)
list(APPEND PROTO_HDRS ${PROTO_HDR})
list(APPEND PROTO_SRCS ${PROTO_SRC})
endforeach()

SET_SOURCE_FILES_PROPERTIES(${PROTO_SRCS} ${PROTO_HDRS} PROPERTIES GENERATED TRUE)

get_directory_property(HAS_PARENT PARENT_DIRECTORY)
if(HAS_PARENT)
set(BAYESMIX_INCLUDE_PATHS ${INCLUDE_PATHS} PARENT_SCOPE)
set(BAYESMIX_LINK_LIBRARIES ${LINK_LIBRARIES} PARENT_SCOPE)
set(BAYESMIX_COMPILE_OPTIONS ${COMPILE_OPTIONS} PARENT_SCOPE)
set(PROTO_HEADERS ${PROTO_HDRS} PARENT_SCOPE)
set(PROTO_SOURCES ${PROTO_SRCS} PARENT_SCOPE)
set(ProtoFiles ${ProtoFiles} PARENT_SCOPE)
endif()

# Build library object
add_library(bayesmix OBJECT)
target_sources(bayesmix PUBLIC ${PROTO_SRCS} ${PROTO_HDRS})
add_subdirectory(pybmixcpp/bayesmix/src)
target_include_directories(bayesmix PUBLIC ${INCLUDE_PATHS})
target_include_directories(bayesmix PUBLIC pybmixcpp/bayesmix)
target_link_libraries(bayesmix PUBLIC ${LINK_LIBRARIES})
target_compile_options(bayesmix PUBLIC ${COMPILE_OPTIONS})

# Build static library
add_library(bayesmixlib $<TARGET_OBJECTS:bayesmix>)

if (BUILD_RUN)
# Build run executable
add_executable(run_mcmc $<TARGET_OBJECTS:bayesmix> pybmixcpp/run_mcmc.cc)
target_include_directories(run_mcmc PUBLIC ${INCLUDE_PATHS})
target_include_directories(run_mcmc PUBLIC pybmixcpp/bayesmix/)
target_include_directories(run_mcmc PUBLIC pybmixcpp/bayesmix/src)
target_include_directories(run_mcmc PUBLIC build)
target_link_libraries(run_mcmc PUBLIC ${LINK_LIBRARIES})
target_compile_options(run_mcmc PUBLIC ${COMPILE_OPTIONS})
endif()

add_subdirectory(pybmixcpp)

if (NOT DISABLE_TESTS)
add_subdirectory(test)
endif()

if (NOT DISABLE_BENCHMARKS)
add_subdirectory(benchmarks)
endif()

if (NOT DISABLE_DOCS)
add_subdirectory(docs)
endif()

if (NOT DISABLE_PLOTS)
include(FetchContent)

set(FETCHCONTENT_QUIET OFF)
set(FETCHCONTENT_UPDATES_DISCONNECTED ON)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_TESTING OFF)

message(CHECK_START "Fetching Matplotplusplus")
list(APPEND CMAKE_MESSAGE_INDENT " ")

FetchContent_Declare(matplotplusplus
GIT_REPOSITORY https://github.com/alandefreitas/matplotplusplus
GIT_TAG origin/master # or whatever tag you want
)

FetchContent_GetProperties(matplotplusplus)
if(NOT matplotplusplus_POPULATED)
FetchContent_Populate(matplotplusplus)
add_subdirectory(${matplotplusplus_SOURCE_DIR} ${matplotplusplus_BINARY_DIR}
EXCLUDE_FROM_ALL)
endif()
# find_package(Matplot++ REQUIRED)
message("matplot " ${matplotplusplus_SOURCE_DIR})

add_executable(plot_mcmc $<TARGET_OBJECTS:bayesmix> pybmixcpp/plot_mcmc.cc
pybmixcpp/bayesmix/src/plots/plot_utils.h
pybmixcpp/bayesmix/src/plots/plot_utils.cc)
target_include_directories(plot_mcmc PUBLIC
${INCLUDE_PATHS} ${matplotplusplus_SOURCE_DIR}/source)
target_include_directories(plot_mcmc PUBLIC pybmixcpp/bayesmix)
target_link_libraries(plot_mcmc PUBLIC ${LINK_LIBRARIES} matplot)
target_compile_options(plot_mcmc PUBLIC ${COMPILE_OPTIONS})
endif()

if (NOT DISABLE_EXAMPLES)
add_subdirectory(pybmixcpp/bayesmix/examples)
endif()
27 changes: 27 additions & 0 deletions pybmix/core/algo_python.asciipb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
##### GENERIC SETTINGS FOR ALL ALGORITHMS #####
# Algorithm ID string, e.g. "Neal2"
algo_id: "Neal3"

# RNG initial seed: any nonnegative integer
rng_seed: 20201124

# Number of iterations of the algorithm
iterations: 1100

# Number of initial iterations discarded by the algorithm
burnin: 100

# Number of clusters in which data will be first initialized
# (NOTE: If you wish to initialize one datum per cluster, please write 0.)
# (NOTE: This value is ONLY used for initialization, and it may be overwritten
# by certain mixing objects, such as LogSBMixing. Please check a mixing's
# initialize() function to know for sure whether or not it will override this
# value.)
init_num_clusters: 3


##### ALGORITHM-SPECIFIC SETTINGS #####
# Neal8 number of auxiliary blocks
# (NOTE: 3 is the recommended value in most cases, please change it only if you
# know what you're doing.)
neal8_n_aux: 3
Loading