diff --git a/Makefile b/Makefile index 19e3be7e..f931f6c7 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,6 @@ READLINE_VERSION = 6.3 TERMCAP_VERSION = 1.3.1 GSL_VERSION = 2.0 - - ifeq ($(OS),Windows_NT) CCFLAGS += -D WIN32 ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) @@ -36,17 +34,15 @@ else endif endif - all: cli r_package -cli: libnet readline +cli: pgmlab readline make -C cli +r_package: pgmlab + make -C R/pgmlabR -r_package: libnet - make -C r_package/libnetR - -libnet: sha gsl +pgmlab: sha gsl make -C net sha: @@ -70,8 +66,16 @@ termcap: make; \ make install; +make install: + make -C net install; \ + make -C cli install + +make uninstall: + make -C net uninstall; \ + make -C cli uninstall + clean: make -C net clean; \ make -C cli clean; \ - make -C r_package/libnetR clean; \ + make -C R/pgmlabR clean; \ rm bin/* diff --git a/r_package/libnetR/DESCRIPTION b/R/pgmlabR/DESCRIPTION similarity index 80% rename from r_package/libnetR/DESCRIPTION rename to R/pgmlabR/DESCRIPTION index edb9e4b0..add1a127 100644 --- a/r_package/libnetR/DESCRIPTION +++ b/R/pgmlabR/DESCRIPTION @@ -1,4 +1,4 @@ -Package: libnetR +Package: pgmlabR Type: Package Version: 1.0 Date: 2015-05-01 @@ -10,8 +10,8 @@ Authors@R: c(person("Adam", "Wright", role = c("aut", "cre"), Author: Adam Wright [aut, cre], Hossein Radfar [aut] Maintainer: Adam Wright Depends: R (>= 3.0.2) -Description: This is the R interface to LibNet. LibNet Consists of a c library that can be used for learning and inference on binary baysean probabilistic networks. +Description: This is the R interface to PGMLAB. PGMLAB Consists of a c library that can be used for learning and inference on binary baysean probabilistic networks. License: GPL (>= 2) LazyLoad: yes -URL: http://www.r-project.org, http://github.com/a8wright/libnet +URL: http://www.r-project.org, http://github.com/a8wright/PGMLAB NeedsCompilation: yes diff --git a/r_package/libnetR/Makefile b/R/pgmlabR/Makefile similarity index 83% rename from r_package/libnetR/Makefile rename to R/pgmlabR/Makefile index 92cecc77..1bf4c7c4 100644 --- a/r_package/libnetR/Makefile +++ b/R/pgmlabR/Makefile @@ -3,8 +3,8 @@ CC = gcc R_VERSION = 3.2.2_1 -TARGET = libnetR -LIBNET_LIB = ../../net/lib +TARGET = pgmlabR +PGMLAB_LIB = ../../net/lib ifeq ($(OS),Windows_NT) @@ -24,7 +24,7 @@ else R_LIB = -L/usr/lib/R/lib -lR INCLUDE_R = -I/usr/share/R/include R_FLAGS = -Wl,Bsymbolic-functions -Wl,-z,relro -std=gnu99 - SOFLAGS += -Wl,-rpath=$(LIBNET_LIB),-rpath=../external_lib/gsl/lib -fPIC + SOFLAGS += -Wl,-rpath=$(PGMLAB_LIB),-rpath=../external_lib/gsl/lib -fPIC OFLAGS = -std=gnu99 -DNDEBUG -fpic -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g endif ifeq ($(UNAME_S),Darwin) @@ -36,7 +36,7 @@ else R_FLAGS = -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation SOFLAGS += -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress OFLAGS = -Wall -mtune=core2 -g -O2 -w - XLINKER = -Xlinker $(LIBNET_LIB) + XLINKER = -Xlinker $(PGMLAB_LIB) endif UNAME_P := $(shell uname -p) ifeq ($(UNAME_P),x86_64) @@ -51,15 +51,15 @@ else endif INCLUDE_GSL = -L../../external_lib/gsl/lib -I../../external_lib/gsl/include -lgsl -lgslcblas -INCLUDE_LIBNET = -L../../net/lib -lnet -I../../net/include -I../../resources/make_hash_table/include +INCLUDE_PGMLAB = -L../../net/lib -lpgmlab -I../../net/include -I../../resources/make_hash_table/include all: object mkdir -p lib; \ - $(CC) $(LDFLAGS) $(SOFLAGS) $(R_LIB) -o lib/$(TARGET).$(SHARED_OBJECT_EXTENSION) obj/$(TARGET).o $(INCLUDE_LIBNET) $(RFLAGS) + $(CC) $(LDFLAGS) $(SOFLAGS) $(R_LIB) -o lib/$(TARGET).$(SHARED_OBJECT_EXTENSION) obj/$(TARGET).o $(INCLUDE_PGMLAB) $(RFLAGS) object: mkdir -p obj; \ - $(CC) -DNDEBUG $(INCLUDE_R) $(OFLAGS) -c src/$(TARGET).c -o obj/$(TARGET).o $(INCLUDE_LIBNET) $(INCLUDE_GSL) $(XLINKER) + $(CC) -DNDEBUG $(INCLUDE_R) $(OFLAGS) -c src/$(TARGET).c -o obj/$(TARGET).o $(INCLUDE_PGMLAB) $(INCLUDE_GSL) $(XLINKER) clean: rm lib/* obj/* diff --git a/r_package/libnetR/NAMESPACE b/R/pgmlabR/NAMESPACE similarity index 100% rename from r_package/libnetR/NAMESPACE rename to R/pgmlabR/NAMESPACE diff --git a/r_package/libnetR/src/libnetR.c b/R/pgmlabR/src/pgmlabR.c similarity index 95% rename from r_package/libnetR/src/libnetR.c rename to R/pgmlabR/src/pgmlabR.c index 0bbc8e66..c8de4ca9 100644 --- a/r_package/libnetR/src/libnetR.c +++ b/R/pgmlabR/src/pgmlabR.c @@ -12,7 +12,7 @@ SEXP r_reaction_logic_to_factorgraph(SEXP reaction_logic_pathway_filepath_, SEXP int exit_code = reaction_logic_to_factorgraph(reaction_logic_pathway_filepath, pathway_filepath, number_of_states); if (exit_code != 0) { - char * strerr = strerror_libnet(exit_code); + char * strerr = strerror_pgmlab(exit_code); Rprintf("ERROR: %s\n", strerr); } @@ -37,7 +37,7 @@ SEXP r_learning_discrete_BayNet(SEXP reaction_logic_pathway_filepath_, SEXP path int exit_code = learning_discrete_BayNet(reaction_logic_pathway_filepath, pathway_filepath, observed_data_filepath, estimated_parameters_filepath, number_of_states, em_max_iterations, em_log_likelihood_change_limit, em_parameters_change_limit, map_flag, logging); if (exit_code != 0) { - char * strerr = strerror_libnet(exit_code); + char * strerr = strerror_pgmlab(exit_code); Rprintf("ERROR: %s\n", strerr); } @@ -55,7 +55,7 @@ SEXP r_doLBPinference(SEXP reaction_logic_pathway_filepath_, SEXP pathway_filepa int exit_code = doLBPinference(reaction_logic_pathway_filepath, pathway_filepath, observed_data_filepath, posterior_probabilities_filepath, number_of_states); if (exit_code != 0) { - char * strerr = strerror_libnet(exit_code); + char * strerr = strerror_pgmlab(exit_code); Rprintf("ERROR: %s\n", strerr); } diff --git a/README.md b/README.md index 88d6a86f..17badf0f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -#LibNet +#PGMLAB (Probablistic Graphical Model Laboratory) -Libnet performs learning and inference in large discrete baysian networks. Libnet is a standalone C library, which has command line and R interfaces. +PGMLAB performs learning and inference in large discrete baysian networks. PGMLAB is a standalone C library, which has command line and R interfaces. -LibNet developed to fulfill three goals: +PGMLAB developed to fulfill three goals: - To perform learning and inference in extremely large graphs. - To be used by both expert and non expert in the field of machine learning @@ -12,19 +12,21 @@ LibNet developed to fulfill three goals: - Hossein Radfar - Adam Wright +##Web Site + - Visit the PGMLAB website [here](http://oicr.github.io/PGMLAB) ##Wiki - - Please visit the LibNet wiki [here](https://github.com/OICR/LibNet/wiki) for details and theory. - - Make sure to follow the input file formats specifications [here](https://github.com/OICR/LibNet/wiki/File-Formats). + - Please visit the PGMLAB wiki [here](https://github.com/OICR/PGMLAB/wiki) for details and theory. + - Make sure to follow the input file formats specifications [here](https://github.com/OICR/PGMLAB/wiki/File-Formats). ##System requirements -LibNet has been tested on OS X and Ubuntu 14.04. +PGMLAB has been tested on OS X and Ubuntu 14.04. -###How to download, install and Run LibNet +###How to download, install and Run PGMLAB ####1. Download - - Download the latest version of LibNet from [here](https://github.com/OICR/LibNet/zipball/master) + - Download the latest version of PGMLAB from [here](https://github.com/OICR/PGMLAB/zipball/master) ####2. Installation @@ -33,42 +35,33 @@ LibNet has been tested on OS X and Ubuntu 14.04. Linux: sudo apt-get install texinfo Mac OS X: follow the following instructions http://macappstore.org/texinfo/ -#####2.2 Install LibNet Package +#####2.2 Install PGMLAB Package - Type the following commands in a terminal: - cd .../your-download-directory/LibNet - make + cd .../your-download-directory/PGMLAB + make + make install (this will link PGMLAB to system paths, making PGMLAB accessible from anywhere on the host) -####3. Running LibNet -#####3.1 Generate hash for particular network with generateHash +####3 User Interfaces -######Library Path +There are two interaces to the shared object that come with this package: a command line interaface and a C interface. To make either of the interfaces you are required to have already compiled the PGMLAB shared object (3.2). - - Run the following command if you would like to access this library system wide (not necessary for basic installation) +#####3.1Command line interface - export LD_LIBRARY_PATH=$LD_LIBRARY_PATH: - -####4 User Interfaces - -There are two interaces to the shared object that come with this package: a command line interaface and a C interface. To make either of the interfaces you are required to have already compiled the LibNet shared object (3.2). - -#####4.1Command line interface - -######4.1.2 Interacting with the command line interface +######3.1.2 Interacting with the command line interface This command line interface can be used in two distinct ways. The first way is to supply the paths of the files in a config file, in the same way as the example config files in the config folder and flags to customize the parameters to be use and the second way is to input the information through an interactive interface. Further infromation on how to use these interface can be found in the wiki. - - Run the following commands for a description of the LibNet command line interface. + - Run the following commands for a description of the PGMLAB command line interface. - cd bin - ./libnet --help + pgmlab --help - The following will be outputed from the previous command ``` -libnet [-gil] [--interactive] [--file-paths=file-paths] [--inference-use-logical-factorgraph] [--number-of-states=] [--em-max-iterations=] [--training-samples=] [--log-likelihood-change-limit=] [--parameters-change-limit=] [--logging-on] [--maximum-a-posteriori-estimation] [--help] [--version] +pgmlab [-gil] [--interactive] [--file-paths=file-paths] [--inference-use-logical-factorgraph] [--number-of-states=] [--em-max-iterations=] [--training-samples=] [--log-likelihood-change-limit=] [--parameters-change-limit=] [--logging-on] [--maximum-a-posteriori-estimation] [--help] [--version] ###Flag descriptions -g, --generate-factorgraph Generate factor graph from reaction logic @@ -90,33 +83,30 @@ libnet [-gil] [--interactive] [--file-paths=file-paths] [--inference-use-logical *If you would like to use the interactive interface select the following flag: "--interactive". -*If you would like to run a new pathway you will need to rerun the generateHash program and then recompile the libnet program. +#####3.2 R interface +In order to call pgmlab from the R Console you will need to load the PGMLAB R shared object. -#####4.2 R interface - -In order to call libnet from the R Console you will need to load the R LibNet shared object. - -######4.2.1 Running R in order to be able to access the R LibNet shared object +######3.2.1 Running R in order to be able to access the PGMLAB R shared object - Run on of the first two commands, depending on your OS, and then run one of the two options in the last line. - cd r_package/libnetR/ (for Linux) - cd r_package/ (for OS X) + cd R/pgmlabR/ (for Linux) + cd R/ (for OS X) type r or rstudio - + - You should now be in a R prompt *The current working directory needs to be correct to have the shared obejects link to one another correctly -######4.2.2 Loading the LibNet shared ojbect within R or Rstudio +######3.2.2 Loading the PGMLAB shared ojbect within R or Rstudio - Run the following command in order to load the shared object - dyn.load("/libnet/r_package/libnetR/lib/libnetR.so") (for Linux) - dyn.load("libnetR/lib/libnetR.so") (for OS X) + dyn.load("/PGMLAB/R/pgmlabR/lib/pgmlabR.so") (for Linux) + dyn.load("pgmlabR/lib/pgmlabR.so") (for OS X) -######4.2.3 Description of functions available from the R LibNet library +######3.2.3 Description of functions available from the PGMLAB R library r_reaction_logic_to_factorgraph(SEXP reaction_logic_pathway_filepath_, SEXP pathway_filepath_, SEXP number_of_states_) @@ -126,7 +116,7 @@ In order to call libnet from the R Console you will need to load the R LibNet sh *All filepaths can be either full or abolute paths and the rest of the variables should be supplied as integer values. -######4.2.4 Example R function calls +######3.2.4 Example R function calls *These relative paths are for linux and should be changed of OS X. In OS X the change should be to remove one of the '../' from the beginning of the each filepath. @@ -144,16 +134,16 @@ In order to call libnet from the R Console you will need to load the R LibNet sh *Functions will return 0 upon success and error codes otherwise. -####5 Libnet dependencies +####4 PGMLAB dependencies -All resources are included in the LibNet package. +All resources are included in the PGMLAB package. -#####5.1 Resources +#####4.1 Resources | Name | Description | Link | |---------------------------|------------------------|------| -| Minimal Perfect Hashing | Minimal Perfect Hashing wass Created by Bob Jenkins and is used to hashing the names of the nodes. This allows LibNet to very quickely query nodes by their unique hash | http://burtleburtle.net/bob/hash/perfect.html | +| Minimal Perfect Hashing | Minimal Perfect Hashing wass Created by Bob Jenkins and is used to hashing the names of the nodes. This allows PGMLAB to very quickely query nodes by their unique hash | http://burtleburtle.net/bob/hash/perfect.html | -#####5.2 External Libraries +#####4.2 External Libraries | Name | Description | Link | |---------------------------|------------------------|------| | GNU Scientific Library (GSL) | GSL is a numerical library for C and C++ that provides a wirde range of mathematical routines | http://www.gnu.org/software/gsl/ | diff --git a/cli/Makefile b/cli/Makefile index ecb748b9..dc8dc895 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -2,7 +2,8 @@ # # other external libraries/tools # -# +# +CWD = $(shell pwd) LIBNET_LIB = ../net/lib SOFLAGS = ################################ @@ -49,13 +50,13 @@ DEBUGFLAGS = -O0 -D _DEBUG LIBDIRS = -L$(LIBNET_LIB) RPATH = -Wl,-rpath=$(LIBNET_LIB) -TARGET = libnet +TARGET = pgmlab TARGET_PATH = ../bin/$(TARGET) SOURCES = $(shell echo src/*.c) $(shell echo ../resources/make_hash_table/src/*.c) ../resources/minini/minIni.c HEADERS = $(shell echo include/*.h) $(shell echo ../resources/make_hash_table/src/*.h) ../resources/minini/minIni.h ../resources/minini/wxMinIni.h ../resources/minini/minGlue.h OBJECTS = $(SOURCES:.c=.o) -INCLUDE_NET = -L../net/lib -I../net/include -lnet +INCLUDE_NET = -L../net/lib -I../net/include -l$(TARGET) INCLUDE_GSL = -L../external_lib/gsl/lib -I../external_lib/gsl/include -lgsl -lgslcblas INCLUDE_READLINE = -L../external_lib/readline/lib -I../external_lib/readline/include -lreadline INCLUDE_TERMCAP = -L../external_lib/termcap/lib -I../external_lib/termcap/include -ltermcap @@ -68,12 +69,11 @@ $(TARGET): folders folders: mkdir -p ../bin -readline: - cd ..; \ - make readline +make install: + ln -s $(CWD)/../bin/$(TARGET) /usr/local/bin/$(TARGET) + +make uninstall: + rm /usr/local/bin/$(TARGET) clean: rm -f $(TARGET) $(OBJECTS) - - - diff --git a/cli/src/main.c b/cli/src/main.c index 1b01bab3..4dbbdd52 100644 --- a/cli/src/main.c +++ b/cli/src/main.c @@ -1,7 +1,7 @@ /******************************************************************************* -* C interface to libnet +* C interface to PGMLAB * USAGE: -* libnet [--help] [--learning] [--inference] +* pgmlab [--help] [--learning] [--inference] * *******************************************************************************/ //for inimini @@ -60,12 +60,12 @@ int non_interactive_command(int em_max_iterations, int em_number_of_training_sam if (g_count > 0) { printf("Generating factorgraph with:\n\t\tnumber of states\t%d\n", number_of_states); - if ( strcmp(logical_factorgraph_filepath, "dummy") == 0 ) { - printf("Pathway filepath not specified in config\n"); + if ( strcmp(logical_factorgraph_filepath, "dummy") == 0 ) { + printf("Pathway filepath not specified correctly in config\n"); return 1; } - if ( strcmp(pairwise_interactions_filepath, "dummy") == 0 ) { - printf("Pairwise interactions filepath not specified in config\n"); + if (( strcmp(pairwise_interactions_filepath, "dummy") == 0 ) || access(pairwise_interactions_filepath, R_OK) ) { + printf("Pairwise interactions filepath not specified correctly in config\n"); return 1; } @@ -95,26 +95,26 @@ int non_interactive_command(int em_max_iterations, int em_number_of_training_sam else { printf("\t\tMAP\t\t\t\toff\n"); } - if ( strcmp(logical_factorgraph_filepath, "dummy") == 0 ) { - printf("Logic factorgraph filepath not specified in config\n"); + if (( strcmp(logical_factorgraph_filepath, "dummy") == 0 ) || access(logical_factorgraph_filepath, R_OK)){ + printf("Logic factorgraph filepath not specified correctly in config\n"); return 1; } - if ( strcmp(learning_observed_data_filepath, "dummy") == 0 ) { - printf("Learning observed data filepath not specified in config\n"); + if (( strcmp(learning_observed_data_filepath, "dummy") == 0 ) || access(learning_observed_data_filepath, R_OK) ) { + printf("Learning observed data filepath not specified correctly in config\n"); return 1; } if ( strcmp(estimated_parameters_filepath, "dummy") == 0 ) { - printf("Estimated Parameters filepath not specified in config\n"); + printf("Estimated Parameters filepath not specified correctly in config\n"); return 1; } - if ( strcmp(pairwise_interactions_filepath, "dummy") == 0 ) { - printf("Pairwise interactions filepath not specified in config\n"); + if (( strcmp(pairwise_interactions_filepath, "dummy") == 0 ) || access(pairwise_interactions_filepath, R_OK) ) { + printf("Pairwise interactions filepath not specified correctly in config\n"); return 1; } int exit_code = learning_discrete_BayNet(pairwise_interactions_filepath, logical_factorgraph_filepath, learning_observed_data_filepath, estimated_parameters_filepath, number_of_states, em_max_iterations, em_log_likelihood_change_limit, em_parameters_change_limit, MAP_flag, logging); if (exit_code != 0) { - char * strerr = strerror_libnet(exit_code); + char * strerr = strerror_pgmlab(exit_code); printf("Learning failed (error code: %d): %s\n", exit_code, *strerr); return 0; } @@ -125,43 +125,39 @@ int non_interactive_command(int em_max_iterations, int em_number_of_training_sam } if (i_count > 0) { printf("Running Inference with:\n\t\tnumber of states\t%d\n", number_of_states); - if ( strcmp(learning_observed_data_filepath, "dummy") == 0 ) { - printf("Observed data filepath not specified in config\n"); + if (( strcmp(inference_observed_data_filepath, "dummy") == 0 ) || access(inference_observed_data_filepath, R_OK) ){ + printf("Observed data filepath not specified correctly in config\n"); return 1; } if ( strcmp(inference_posterior_probabilities_filepath, "dummy") == 0 ) { - printf("Posterior probabilities filepath not specified in config\n"); + printf("Posterior probabilities filepath not specified correctly in config\n"); return 1; } - if ( strcmp(logical_factorgraph_filepath, "dummy") == 0 ) { - printf("Logic factorgraph filepath not specified in config\n"); - return 1; - } - if ( strcmp(pairwise_interactions_filepath, "dummy") == 0 ) { - printf("Pairwise interactions filepath not specified in config\n"); + if (( strcmp(pairwise_interactions_filepath, "dummy") == 0 ) || access(pairwise_interactions_filepath, R_OK) ) { + printf("Pairwise interactions filepath not specified correctly in config\n"); return 1; } int exit_code; if (inference_use_learnt_factorgraph) { - if ( strcmp(estimated_parameters_filepath, "dummy") == 0 ) { - printf("Estimated parameters (learnt factorgraph) filepath not specified in config\n"); + if (( strcmp(estimated_parameters_filepath, "dummy") == 0 ) || access(estimated_parameters_filepath, R_OK) ) { + printf("Estimated parameters (learnt factorgraph) filepath not specified correctly in config\n"); return 1; } exit_code = doLBPinference(pairwise_interactions_filepath, estimated_parameters_filepath, inference_observed_data_filepath, inference_posterior_probabilities_filepath, number_of_states); } else { - if ( strcmp(logical_factorgraph_filepath, "dummy") == 0 ) { - printf("Logic factorgraph filepath not specified in config\n"); + if (( strcmp(logical_factorgraph_filepath, "dummy") == 0 ) || access(logical_factorgraph_filepath, R_OK) ) { + printf("Logic factorgraph filepath not specified correctly in config\n"); return 1; } exit_code = doLBPinference(pairwise_interactions_filepath, logical_factorgraph_filepath, inference_observed_data_filepath, inference_posterior_probabilities_filepath, number_of_states); } if (exit_code != 0) { - char * strerr = strerror_libnet(exit_code); + char * strerr = strerror_pgmlab(exit_code); printf("Inference failed with (error code: %d): %s\n", exit_code, *strerr); return exit_code; } @@ -447,7 +443,7 @@ int interactive_pairwise_to_factorgraph(char **pairwise_interactions_filepath, c int exit_code = reaction_logic_to_factorgraph(*pairwise_interactions_filepath, *factorgraph_filepath, *number_of_states); if (exit_code != 0) { - char * strerr = strerror_libnet(exit_code); + char * strerr = strerror_pgmlab(exit_code); printf("Failed to generate factorgraph (error code: %d): %s\n", exit_code, strerr); return exit_code; } @@ -476,7 +472,7 @@ int interactive_learning(char **pairwise_interactions_filepath, char **logical_f printf("Running Learning\n"); int exit_code = learning_discrete_BayNet(*pairwise_interactions_filepath, *logical_factorgraph_filepath, *observed_data_filepath, *estimated_parameters_filepath, *number_of_states, *em_max_iterations, *em_log_likelihood_change_limit, *em_parameters_change_limit, *MAP_flag, *logging); if (exit_code != 0) { - char * strerr = strerror_libnet(exit_code); + char * strerr = strerror_pgmlab(exit_code); printf("Learning failed (error code: %d): %s\n", exit_code, *strerr); return 0; } @@ -503,7 +499,7 @@ int interactive_inference(char** pairwise_interactions_filepath, char **factorgr printf("Running Inference\n"); int exit_code = doLBPinference(*pairwise_interactions_filepath, *factorgraph_filepath, *observed_data_filepath, *posterior_probabilities_filepath, *number_of_states); if (exit_code != 0) { - char * strerr = strerror_libnet(exit_code); + char * strerr = strerror_pgmlab(exit_code); printf("Inference failed with (error code: %d): %s\n", exit_code, *strerr); return exit_code; } @@ -602,7 +598,7 @@ int main(int argc, char *argv[]) { end = arg_end(20) }; - const char *program_name = "libnet"; + const char *program_name = "pgmlab"; const char *program_version = "1.0.0"; int exitcode = 0; diff --git a/net/Makefile b/net/Makefile index e024dda3..470acb0c 100644 --- a/net/Makefile +++ b/net/Makefile @@ -3,6 +3,7 @@ CC = gcc CWD = $(shell pwd) ODIR = obj +NAME = pgmlab GSL_VERSION = 1.9 SHARED_OBJECT_EXTENSION = @@ -29,7 +30,7 @@ else CCFLAGS += -D OSX LDFLAGS = -dynamiclib SHARED_OBJECT_EXTENSION = dylib - RPATH = -install_name ../net/lib/libnet.$(SHARED_OBJECT_EXTENSION) + RPATH = -install_name ../net/lib/lib$(NAME).$(SHARED_OBJECT_EXTENSION) endif UNAME_P := $(shell uname -p) ifeq ($(UNAME_P),x86_64) @@ -43,7 +44,7 @@ else endif endif -TARGET = libnet.$(SHARED_OBJECT_EXTENSION) +TARGET = lib$(NAME).$(SHARED_OBJECT_EXTENSION) TARGET_PATH = $(shell pwd)/lib/$(TARGET) SOURCES = src/net.c \ @@ -69,7 +70,8 @@ $(TARGET_PATH): folder net.o perfect.o lookupa.o recycle.o perfhex.o hash_graph_ $(OBJECTS_SHA) \ $(INCLUDE_GSL) \ -lm \ - -ldl + -ldl + # -w; \ @@ -77,7 +79,7 @@ folder: mkdir -p obj lib hash_obj; net.o: src/net.c - $(CC) $(CCFLAGS) $(CFLAGS) -w -c src/net.c -o obj/net.o -Iinclude $(INCLUDE_GSL) -I../resources/make_hash_table/include $(INCLUDE_SHA) + $(CC) $(CCFLAGS) $(CFLAGS) -w -c src/net.c -o obj/net.o -Iinclude $(INCLUDE_GSL) -I../resources/make_hash_table/include $(INCLUDE_SHA) -DPGMLAB_SO_DIR=\"$(CWD)\" perfect.o: ../resources/make_hash_table/src/perfect.c $(CC) $(CCFLAGS) $(CFLAGS) -c ../resources/make_hash_table/src/perfect.c -o obj/perfect.o -I../resources/make_hash_table/include -w @@ -94,6 +96,12 @@ perfhex.o: ../resources/make_hash_table/src/perfhex.c hash_graph_node_ids.o: src/hash_graph_node_ids.c $(CC) $(CCFLAGS) $(CFLAGS) -c src/hash_graph_node_ids.c -o obj/hash_graph_node_ids.o -Iinclude -I../resources/make_hash_table/include -w +make install: + ln -s $(CWD)/lib/$(TARGET) /usr/lib/ + +make uninstall: + rm /usr/lib/$(TARGET) + clean: rm -f $(TARGET_PATH) ; \ rm -f $(OBJECTS); diff --git a/net/include/net.h b/net/include/net.h index 15741e50..fdc9c480 100644 --- a/net/include/net.h +++ b/net/include/net.h @@ -24,10 +24,10 @@ #endif #ifndef EPS -#define EPS 1e-300 ; +#define EPS 1e-300; #endif #ifndef logEPS -#define logEPS -30 ; +#define logEPS -30; #endif #ifndef LargeNumber #define LargeNumber 1e300; @@ -89,7 +89,7 @@ int ReadMultipleVisibleSets(lib_function *phash, char *filename, double **obs_v void hash_graph_node_IDs(char *readreactionlogicpathways); int learning_discrete_BayNet(char* readreactionlogic, char * logical_factorgraph, char *obs_data,char *estimated_parameters, int num_state, int max_num_repeat, double LLchangeLimit, double parChangeLimit, int MAPflag, int logging); -char *strerror_libnet(int error_code); +char *strerror_pgmlab(int error_code); int compile_shared_object(char* path); int create_hash_folder (char* hash_folder_path); @@ -98,5 +98,5 @@ int streamFile(char*filename, char**string, long *length); int hashFile(char *filename, unsigned char *sum); int create_hash_object(char* readreactionlogic, char* hash_folder_path ); -int load_hash_library(char* readreactionlogic, lib_function *phash); +int load_hash_library(char* readreactionlogic, void **hash_library, lib_function *phash); #endif diff --git a/net/src/net.c b/net/src/net.c index 0b048ed4..a09d4d27 100644 --- a/net/src/net.c +++ b/net/src/net.c @@ -360,7 +360,7 @@ void normalizeCPD(double * f, double *CPD,int numComb,int num_state,int flag) { if (CPD[k] == 0) { - f[k] =logEPS; + f[k] = logEPS; } else { @@ -382,8 +382,9 @@ void normalizeCPD(double * f, double *CPD,int numComb,int num_state,int flag) int doLBPinference(char* readreactionlogic, char *factorgraph, char * obs_data, char *posterior_probabilities, int num_state) { + void *hash_library; lib_function phash; - int exit_code = load_hash_library(readreactionlogic, &phash); + int exit_code = load_hash_library(readreactionlogic, &hash_library, &phash); if (exit_code != 0) return exit_code; int *obs_values = NULL; @@ -430,7 +431,6 @@ int doLBPinference(char* readreactionlogic, char *factorgraph, char * obs_data, for(i=0;i nn pGraph[nn].iIdx[adjCount[nn]] = num_state * (*lenMsgVec); // message nn --> ii - (*lenMsgVec)++; - pGraph[ii+Nv].iIdx[k] = num_state * (*lenMsgVec); // message nn --> ii pGraph[nn].oIdx[adjCount[nn]] = num_state * (*lenMsgVec); // message ii --> nn - - (*lenMsgVec)++; - adjCount[nn]++; - k++; - temp = strtok(NULL, delims); - // printf("%s\n",temp); } } @@ -1179,18 +1113,14 @@ int FactorgraphFile_To_NodeStructures(lib_function *phash, char *factorgraph, No { parZ = 0; // the probabilities should sum up to 1; temp = strtok(buf, delims); - pGraph[ii+Nv].numComb =atoi(temp); - pGraph[ii+Nv].beliefs = malloc(pGraph[ii+Nv].numComb *sizeof(double)); - for(h=0;h maxLog[i]) - { - maxLog[i] = f[k]; + if ((i==array[v-Nv-1][k+j*L]-1) && (f[k] > maxLog[i])) + maxLog[i] = f[k]; - } - } - } - } - for (k=0;kmaxLogVar) maxLogVar = temp[k]; - } for (k = 0; k < num_state; k++) { @@ -1427,9 +1323,7 @@ int LogRoundRobinSplashLBP(double **factorarray,int **array,Node *AlarmNet,doubl normLog = log(normLog); for (k = 0; k < num_state; k++) - { u[AlarmNet[v-1].oIdx[j]+k] = temp[k]-normLog; - } } } } @@ -1452,21 +1346,15 @@ int LogRoundRobinSplashLBP(double **factorarray,int **array,Node *AlarmNet,doubl for(k=0;kmaxLogVar) maxLogVar = temp[k]; - } for (k = 0; k < num_state; k++) { @@ -1480,11 +1368,7 @@ int LogRoundRobinSplashLBP(double **factorarray,int **array,Node *AlarmNet,doubl temp[k] -= normLog; for (k = 0; k < num_state; k++) - { AlarmNet[v-1].beliefs[k] = exp(temp[k]); - //if(!isfinite(temp[k])) - //printf("%f %f\n", exp(temp[k]),log(exp(temp[k]))); - } } /*compute the joint probabilty of set of variables belonging to a factor (see Eq 8.72 Bishop book)*/ @@ -1501,29 +1385,21 @@ int LogRoundRobinSplashLBP(double **factorarray,int **array,Node *AlarmNet,doubl ff[k] = factorarray[v][k]; for (j = 0;j < noN;j++) - { for (k=0;kmaxLogVar) maxLogVar = ff[k]; - } for (k = 0; k < L; k++) { ff[k] -= maxLogVar; normLog += exp(ff[k]); - // printf("%f %f\n",ff[k], normLog); } normLog = log(normLog); @@ -1532,10 +1408,8 @@ int LogRoundRobinSplashLBP(double **factorarray,int **array,Node *AlarmNet,doubl ff[k] -= normLog; for (k = 0; k < L; k++) - { AlarmNet[v+Nv].beliefs[k] = exp(ff[k]); - // printf("%d %f\n",k, AlarmNet[v+Nv].beliefs[k]); - } + free(ff); } @@ -1556,6 +1430,7 @@ void unsigned_char_to_char(char *dst,unsigned char *src,size_t src_len) { while (src_len--) dst += sprintf(dst,"%02x",*src++); + *dst = '\0'; } @@ -1593,7 +1468,7 @@ int create_hash_object(char* readreactionlogic, char* hash_folder_path) return 0; } -int load_hash_library(char* readreactionlogic, lib_function * phash) +int load_hash_library(char* readreactionlogic, void **hash_library, lib_function * phash) { unsigned char sum[16]; @@ -1603,22 +1478,26 @@ int load_hash_library(char* readreactionlogic, lib_function * phash) char folder_name[sizeof(sum)*2+1]; unsigned_char_to_char(folder_name, sum, sizeof(sum)); - const char* base_dir = "../net/hash_obj/"; + const char* hash_dir = "/hash_obj/"; const char* filename = "/libphash.so"; + char * pgmlab_so_dir = PGMLAB_SO_DIR; + char* hash_object_path; - int hash_object_path_size = strlen(folder_name)+1+strlen(base_dir)+1+strlen(filename); + int hash_object_path_size = strlen(pgmlab_so_dir)+1+strlen(hash_dir)+1+strlen(folder_name)+1+strlen(filename); hash_object_path = malloc(hash_object_path_size); - strcpy(hash_object_path, base_dir); + strcpy(hash_object_path, pgmlab_so_dir); + strcat(hash_object_path, hash_dir); strcat(hash_object_path, folder_name); strcat(hash_object_path, filename); //Creates the shared object if it does not exist if ( access( hash_object_path, X_OK ) == -1 ) { char* hash_folder_path; - int hash_folder_path_size = strlen(folder_name)+1+strlen(base_dir); + int hash_folder_path_size = strlen(pgmlab_so_dir)+1+strlen(hash_dir)+1+strlen(folder_name); hash_folder_path = malloc(hash_folder_path_size); - strcpy(hash_folder_path, base_dir); + strcpy(hash_folder_path, pgmlab_so_dir); + strcat(hash_folder_path, hash_dir); strcat(hash_folder_path, folder_name); error_code = create_hash_object(readreactionlogic, hash_folder_path); @@ -1627,14 +1506,12 @@ int load_hash_library(char* readreactionlogic, lib_function * phash) } char* error; - void *hash_library; - hash_library = dlopen(hash_object_path, RTLD_NOW); + + *hash_library = dlopen(hash_object_path, RTLD_NOW); if(!hash_library) return 110; - *phash = dlsym(hash_library, "phash"); + *phash = dlsym(*hash_library, "phash"); if((error = dlerror()) != NULL) return 111; - - /* dlclose(hash_library); */ return 0; } @@ -1645,8 +1522,9 @@ int load_hash_library(char* readreactionlogic, lib_function * phash) int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgraphfilename,int nstate) { + void *hash_library; lib_function phash; - int exit_code = load_hash_library(readreactionlogic, &phash); + int exit_code = load_hash_library(readreactionlogic, &hash_library, &phash); if (exit_code != 0) return exit_code; int i,k,h,kk,Ne,Nv; @@ -1668,7 +1546,7 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap // read the node name and store in an array FILE *file = fopen(readreactionlogic, "r"); if (file == NULL) return 101; - + fgets(buf,maxLen,file); /* read first line to get number of edges and store it in Ne*/ buf[strlen(buf)-1] = '\0'; /*remove \n placed at the end of each line */ @@ -1693,7 +1571,7 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap { targetsource[kk] = malloc((strlen(temp)+1)*sizeof(char)); - strcpy(targetsource[kk],temp) ; + strcpy(targetsource[kk],temp); kk++; } temp = strtok(NULL, delims); @@ -1750,7 +1628,6 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap varcount[i] = 1 ;/* initialize it*/ } - /*--------------------------------------------------------------------------------*/ /* find the variable parent nodes and the child node connected to each factor */ /*--------------------------------------------------------------------------------*/ @@ -1761,39 +1638,24 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap while ((fgets(buf,maxLen,file)) != NULL) { buf[strlen(buf)-1] = '\0'; /*remove \n placed at the end of each line */ - source = strtok(buf, delims); /* get the source node */ - //printf("%s \n",source); - target = strtok(NULL, delims); /* get the target node */ - //printf("%s \n",target); - pos_neg = strtok(NULL, delims); /* get flag indicating the interaction is positive or negative*/ - //printf("%s \n",type); - type_interaction = strtok(NULL, delims); /* get the type of interaction and or or*/ - //printf("%s %s \n",pos_neg,type_interaction); - k = (*phash)(target,strlen(target)); + /* add the target node to the factor*/ fGraph[k].variablenode[0] = malloc((strlen(target)+1)*sizeof(char)); strcpy(fGraph[k].variablenode[0],target); - fGraph[k].pos_neg[0] = 1; /*this is a target source*/ - fGraph[k].type_interaction[0] = atoi(type_interaction); /* read the forth column */ /* add the source nodes to the factor*/ fGraph[k].variablenode[varcount[k]] = malloc((strlen(source)+1)*sizeof(char)); - strcpy(fGraph[k].variablenode[varcount[k]],source); - fGraph[k].pos_neg[varcount[k]] = atoi(pos_neg); /* read the third column */ - //printf("%d %d %d \n",k,fGraph[k].pos_neg[varcount[k]],atoi(pos_neg)); - fGraph[k].type_interaction[varcount[k]] = atoi(type_interaction); /* read the forth column */ - varcount[k]++; /* one node added to factor k */ } @@ -1809,18 +1671,9 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap buf[strlen(buf)-1] = '\0'; /*remove \n placed at the end of each line */ source = strtok(buf, delims); /* get the source node */ - //printf("%s \n",source); - target = strtok(NULL, delims); /* get the target node */ - //printf("%s \n",target); - pos_neg = strtok(NULL, delims); /* get flag indicating the interaction is positive or negative*/ - //printf("%s \n",type); - type_interaction = strtok(NULL, delims); /* get the type of interaction and or or*/ - //printf("%s \n",type); - //printf("%s %s \n",pos_neg,type_interaction); - k = (*phash)(source, strlen(source)); if (varcount[k] == 1) @@ -1845,9 +1698,6 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap // build conditional probability table indexes Ncpt = (int *)malloc(Nv*sizeof(int)); - - //cpt = malloc(Nf*sizeof(*cpt)); - for (i = 0;i < Nv; i++) { h = 1; @@ -1855,13 +1705,9 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap h *= nstate; Ncpt[i] = h; - cpt[i] = malloc(Ncpt[i]*fGraph[i].numVariables*sizeof(int)); -// cpt[i] = malloc(Ncpt[i]*fGraph[i].numVariables*sizeof(**cpt)); - Nrep = Ncpt[i]; - //for(ii =0 ; ii < pGraph[Nv+i].numAdj;ii++) //if you use alramnet format for(ii = fGraph[i].numVariables-1 ; ii >= 0 ;ii--) { kk = 1; @@ -1869,7 +1715,6 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap Nrep /= (double)nstate; for(j =0; j< Ncpt[i];j++) { - cpt[i][ii*Ncpt[i]+j] = kk; if(((j+1) % Nrep) == 0) @@ -1898,51 +1743,21 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap vote[i] = (double *)malloc(pow(nstate,fGraph[i].numVariables)*sizeof(**vote)); factorarray[i] = (double *)malloc(pow(nstate,fGraph[i].numVariables)*sizeof(**factorarray)); } - + for(i = 0;i < Nv;i++) - { for (k=0;k nstate/2) - { - cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)] = (cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)]-floor(nstate/2))*fGraph[i].pos_neg[h] ; - - } - - else - { - - cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)] = (cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)]-floor(nstate/2)-1)*fGraph[i].pos_neg[h] ; - - } - - - } - } - - - - } - } + cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)] = (nstate %2 !=0)? + (cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)]-floor(nstate/2)-1)*fGraph[i].pos_neg[h] : + (cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)] > nstate/2) ? + (cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)]-floor(nstate/2))*fGraph[i].pos_neg[h] : + (cpt[i][k+h*(int)pow(nstate,fGraph[i].numVariables)]-floor(nstate/2)-1)*fGraph[i].pos_neg[h]; + /* OR resembles max and AND resembles min */ int Small_num_for_OR ; /* to find max*/ @@ -2023,9 +1838,7 @@ int reaction_logic_to_factorgraph(char *readreactionlogic,char * writefactorgrap for(i=0;i p(x1|x2); it is a local normalization*/ for(i = 0;i < Nf;i++) - { normalizeCPD(factorarray0[i],sumFactorProbability[i],pGraph[i+Nv].numComb,num_state,1);/*flag =1 mean convert to log */ - // for(k=0;k