Compare commits
315 Commits
cf1611539d
...
producer_c
Author | SHA1 | Date | |
---|---|---|---|
65a96851ef
|
|||
722da7f781
|
|||
b1833a5feb
|
|||
41a0bd4ddd
|
|||
9ab4fc7d76
|
|||
beadb7465f
|
|||
652e5f623f
|
|||
b7fef9a99d
|
|||
343269d48c
|
|||
21c4c6df51
|
|||
702f086706
|
|||
981bc8f98b
|
|||
e0b7b2d316
|
|||
9b9e91e856 | |||
18e8e84284
|
|||
7de11b0e6d
|
|||
9b8db37a4b
|
|||
49b26bd04b
|
|||
b5b5b48864
|
|||
19586a3a5a
|
|||
ffe6d37436
|
|||
b73f4be146
|
|||
dbf2f35502
|
|||
db9e80a70e
|
|||
40ae4ad7f9
|
|||
234342f2de
|
|||
aa0936abd1
|
|||
f0d6f0cc38
|
|||
cc316bb8d3
|
|||
0723564e66
|
|||
2e95e8999d
|
|||
fb9b395748
|
|||
03e4437fea
|
|||
33cd32c639
|
|||
c460ef46ed
|
|||
dee9c674da
|
|||
e3f6dc1e0b
|
|||
460d20a402
|
|||
8dbbb65a2f
|
|||
d06bf187b2
|
|||
4addaefb47
|
|||
82964190f6
|
|||
4fefe9a1d2
|
|||
7c12dd25e5
|
|||
c713c0b1df
|
|||
64069a6cb7
|
|||
ba2a3f9523 | |||
f94e2d6a27
|
|||
2121ba9b98
|
|||
8b7b59d42b
|
|||
bbe5302ab1
|
|||
c2eb727fc7
|
|||
fb347ed5b9
|
|||
b657762c0c
|
|||
495d8a8528
|
|||
4628e48d3c
|
|||
5876be4b24
|
|||
dc3400197f
|
|||
26d3a57782
|
|||
4f3a04058f
|
|||
89c4613591
|
|||
28f3d87e32 | |||
e8d2c9fc0b
|
|||
d3cb580387
|
|||
f088df14fd
|
|||
e2249eace7
|
|||
64f5a7f14a
|
|||
408db2aad5
|
|||
e03efb5f63
|
|||
f617886133
|
|||
69ad660040
|
|||
431b3a3aa5
|
|||
6a23e2cc26
|
|||
f6e00530be
|
|||
f9258e43b9
|
|||
92820555da
|
|||
5a3af51826
|
|||
a8f9800631
|
|||
84cec0c1e0
|
|||
130139f644
|
|||
651f84b562
|
|||
553ab0fa22
|
|||
4975feabff
|
|||
32293af69f
|
|||
858664be2d
|
|||
1f705f6018
|
|||
7bcd2eed06
|
|||
833acefbb3
|
|||
26b649ebae
|
|||
080eddf9cd
|
|||
04e754b2f5
|
|||
38423048bd
|
|||
64fc97b892
|
|||
2c2159f192
|
|||
6765552a7c
|
|||
f72aa5b9a6 | |||
fa7fe081ad
|
|||
660e783517
|
|||
b35532dd9e
|
|||
6ef49385ea
|
|||
6d5a25cdc8
|
|||
d00b08cbe8
|
|||
977ff6fddb
|
|||
54b8939f35
|
|||
5022a4dc90
|
|||
40d1dad5d8
|
|||
47e2b138c5
|
|||
e7ded68267
|
|||
ca833a34f5
|
|||
df9b4c48d2
|
|||
f288bbd6fa
|
|||
7d8aca4f59
|
|||
8fdad78a8c
|
|||
e3ae073333
|
|||
4b732e76c2
|
|||
fe5fead27e
|
|||
8c3864f3c8
|
|||
1287160c47
|
|||
2f58807322
|
|||
17e079edd5
|
|||
b9e0028e9d
|
|||
e0d39fe631
|
|||
36b0277576
|
|||
da8d018ec4
|
|||
5f0676691c
|
|||
3448fb1299
|
|||
5e938d5cca
|
|||
55e742438f
|
|||
c4ae3fe429
|
|||
93e4ff94db
|
|||
57c27f739c
|
|||
a434d7f1ae
|
|||
294666c516
|
|||
fd04e78ad9
|
|||
66ec1b343b
|
|||
bb423da42f
|
|||
db17c14042
|
|||
a4401cb78f
|
|||
9d3d9cc6c6
|
|||
cfcf3c16df
|
|||
85202260f3
|
|||
82acb3cab5
|
|||
623ceed396 | |||
926de2bebd
|
|||
71704e3547
|
|||
3b06534327
|
|||
ac89a451e3
|
|||
00c6cf663b
|
|||
5043c12be8
|
|||
11320e2cc7
|
|||
ce66483b65
|
|||
cab8e14b2d
|
|||
f0d0abe891
|
|||
dcba146e12
|
|||
3ea0285119
|
|||
e3888e1503 | |||
06de13df98
|
|||
de4fa6a04f
|
|||
3a7bf4e672
|
|||
cd0bc02a74
|
|||
c8597a794e
|
|||
b30416364d
|
|||
3a16589220
|
|||
c4f9187e2a
|
|||
c4d0a5b4e6
|
|||
7bfafe555f
|
|||
337b6f7e79
|
|||
5fa0b957dd
|
|||
67252fc41d
|
|||
94ae9456a0
|
|||
781993e326
|
|||
8257a6ae39
|
|||
fc81730dfc | |||
d8734ff082
|
|||
03533461c8
|
|||
68f22a673d
|
|||
b9bc0088f3
|
|||
c280e254ca
|
|||
3d0f29fda3
|
|||
20a6ebab7c
|
|||
925f71166c
|
|||
f69f415b92
|
|||
1bdfbd1620
|
|||
06fb135526
|
|||
501ea0ab4e
|
|||
847c6761d7
|
|||
6030885fc3
|
|||
89df7f4db0
|
|||
41257ed566
|
|||
506369e46b
|
|||
d908f389f5
|
|||
5a7c8f1818
|
|||
64fc7bd9dd
|
|||
0b7beda78c
|
|||
05b670dfc0
|
|||
de62d42b74
|
|||
edb957d22e
|
|||
4de5cb4c6c | |||
c35030f137
|
|||
182b07ed90
|
|||
7806f961e2
|
|||
7c3e315ae7
|
|||
284ef6dfd1
|
|||
1c6af619b5
|
|||
86ffdfd6f3
|
|||
d82148079d
|
|||
067430fd1b
|
|||
f5d0d16365 | |||
97ca8ac084
|
|||
1c1385b768
|
|||
35432b6294
|
|||
c59dd30e53
|
|||
d2da0ddb88
|
|||
8066701c3c
|
|||
0f66ac73d0
|
|||
4370bf51d7
|
|||
2b7353b9e0
|
|||
b686b3c9c3
|
|||
2dd04a6c44
|
|||
1da83662d0
|
|||
3ac9593c65
|
|||
6b317accf1
|
|||
4964aab722
|
|||
7a6ec73d63 | |||
1a534888d6
|
|||
59ffd179f4
|
|||
9972738deb
|
|||
bafcb26bb6
|
|||
2d7999d5f2
|
|||
a6bb22dfb5
|
|||
704dc937be
|
|||
a3e665eed6
|
|||
918a7b4180
|
|||
80b20f35b4
|
|||
4d4780c1d5
|
|||
fa612c531e
|
|||
24b68f9ae2
|
|||
a062ebf445 | |||
2a3fc9aa45
|
|||
55d21294d5
|
|||
3691cb4a61
|
|||
054567c65a
|
|||
2729b92f06
|
|||
f26ea1f0ac
|
|||
af0419c9da
|
|||
90c92e5c56 | |||
182b52a887
|
|||
6679b90a82 | |||
405887f833
|
|||
3a85481a5a
|
|||
0ad5505c16
|
|||
323444b74a
|
|||
ef1bffcac3
|
|||
06db8f51ce
|
|||
e74565ba01
|
|||
2da0fb5d8f
|
|||
14ea51648a
|
|||
9e94f4e140
|
|||
1d0fd629c9
|
|||
506ef34c6f
|
|||
7f45495837
|
|||
1a09ccca4c
|
|||
a1c6ab18f3
|
|||
64ac8fb4f2
|
|||
c568ba111d
|
|||
45c1d052ac
|
|||
eb1cec58a3
|
|||
f520b40016
|
|||
cdfb45d2cb
|
|||
f63a9a64f9
|
|||
285f0938a6
|
|||
8f8f9773ce
|
|||
a9ba21560d
|
|||
a18fbe5594
|
|||
adf650d257
|
|||
43bb017d5d
|
|||
53697648e7
|
|||
4ebc9c2013
|
|||
b882569169
|
|||
8b2ed26ab7
|
|||
5efa3beaee
|
|||
9a0449c12d
|
|||
7222119dfb
|
|||
cb54f61a69
|
|||
07d572a98c
|
|||
c4f3e6f19a
|
|||
adc0ca238f
|
|||
b9e76becce
|
|||
85cb447283
|
|||
b03e84044a
|
|||
7f7ddad36a
|
|||
3d8fea7a37
|
|||
bc214a496c
|
|||
3e954ba841
|
|||
6f7fb290b0
|
|||
49a49a9dcd
|
|||
af7a1d2b40
|
|||
4a54bd42a2
|
|||
099b4bea09
|
|||
be06e475f0
|
|||
c10ebca0e0
|
|||
0c226371cc
|
|||
644b6c9be0
|
|||
9981ad1811
|
|||
41cceece20
|
|||
f6e154bc6e
|
|||
a2622a4fb6
|
|||
d8218f9713
|
|||
48bfa02e1d
|
|||
f519003766
|
|||
8ddfd58a50
|
|||
5f70449091
|
|||
2f5bd0ea7e
|
|||
1a21015492
|
|||
57dab6d709
|
16
.clang-tidy
Normal file
16
.clang-tidy
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
---
|
||||||
|
Checks: '-*,
|
||||||
|
clang-*,
|
||||||
|
bugprone-*,
|
||||||
|
cppcoreguidelines-*,
|
||||||
|
modernize-*,
|
||||||
|
performance-*,
|
||||||
|
-cppcoreguidelines-pro-type-vararg,
|
||||||
|
-modernize-use-trailing-return-type,
|
||||||
|
-bugprone-exception-escape'
|
||||||
|
|
||||||
|
HeaderFilterRegex: 'src/*'
|
||||||
|
AnalyzeTemporaryDtors: false
|
||||||
|
WarningsAsErrors: ''
|
||||||
|
FormatStyle: file
|
||||||
|
...
|
31
.clang-uml
Normal file
31
.clang-uml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
compilation_database_dir: build
|
||||||
|
output_directory: puml
|
||||||
|
diagrams:
|
||||||
|
BayesNet:
|
||||||
|
type: class
|
||||||
|
glob:
|
||||||
|
- src/BayesNet/*.cc
|
||||||
|
- src/Platform/*.cc
|
||||||
|
using_namespace: bayesnet
|
||||||
|
include:
|
||||||
|
namespaces:
|
||||||
|
- bayesnet
|
||||||
|
- platform
|
||||||
|
plantuml:
|
||||||
|
after:
|
||||||
|
- "note left of {{ alias(\"MyProjectMain\") }}: Main class of myproject library."
|
||||||
|
sequence:
|
||||||
|
type: sequence
|
||||||
|
glob:
|
||||||
|
- src/Platform/main.cc
|
||||||
|
combine_free_functions_into_file_participants: true
|
||||||
|
using_namespace:
|
||||||
|
- std
|
||||||
|
- bayesnet
|
||||||
|
- platform
|
||||||
|
include:
|
||||||
|
paths:
|
||||||
|
- src/BayesNet
|
||||||
|
- src/Platform
|
||||||
|
start_from:
|
||||||
|
- function: main(int,const char **)
|
5
.gitignore
vendored
5
.gitignore
vendored
@@ -31,7 +31,10 @@
|
|||||||
*.exe
|
*.exe
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
build/
|
build/**
|
||||||
|
build_*/**
|
||||||
*.dSYM/**
|
*.dSYM/**
|
||||||
cmake-build*/**
|
cmake-build*/**
|
||||||
.idea
|
.idea
|
||||||
|
puml/**
|
||||||
|
.vscode/settings.json
|
||||||
|
25
.gitmodules
vendored
Normal file
25
.gitmodules
vendored
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
[submodule "lib/mdlp"]
|
||||||
|
path = lib/mdlp
|
||||||
|
url = https://github.com/rmontanana/mdlp
|
||||||
|
main = main
|
||||||
|
update = merge
|
||||||
|
[submodule "lib/catch2"]
|
||||||
|
path = lib/catch2
|
||||||
|
main = v2.x
|
||||||
|
update = merge
|
||||||
|
url = https://github.com/catchorg/Catch2.git
|
||||||
|
[submodule "lib/argparse"]
|
||||||
|
path = lib/argparse
|
||||||
|
url = https://github.com/p-ranav/argparse
|
||||||
|
master = master
|
||||||
|
update = merge
|
||||||
|
[submodule "lib/json"]
|
||||||
|
path = lib/json
|
||||||
|
url = https://github.com/nlohmann/json.git
|
||||||
|
master = master
|
||||||
|
update = merge
|
||||||
|
[submodule "lib/libxlsxwriter"]
|
||||||
|
path = lib/libxlsxwriter
|
||||||
|
url = https://github.com/jmcnamara/libxlsxwriter.git
|
||||||
|
main = main
|
||||||
|
update = merge
|
18
.vscode/c_cpp_properties.json
vendored
Normal file
18
.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Mac",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"macFrameworkPath": [
|
||||||
|
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
|
||||||
|
],
|
||||||
|
"cStandard": "c17",
|
||||||
|
"cppStandard": "c++17",
|
||||||
|
"compileCommands": "${workspaceFolder}/cmake-build-release/compile_commands.json"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
112
.vscode/launch.json
vendored
112
.vscode/launch.json
vendored
@@ -4,28 +4,120 @@
|
|||||||
{
|
{
|
||||||
"type": "lldb",
|
"type": "lldb",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "bayesnet",
|
"name": "sample",
|
||||||
"program": "${workspaceFolder}/build/sample/main",
|
"program": "${workspaceFolder}/build_debug/sample/BayesNetSample",
|
||||||
"args": [
|
"args": [
|
||||||
"-f",
|
"-d",
|
||||||
"iris"
|
"iris",
|
||||||
|
"-m",
|
||||||
|
"TANLd",
|
||||||
|
"-s",
|
||||||
|
"271",
|
||||||
|
"-p",
|
||||||
|
"/Users/rmontanana/Code/discretizbench/datasets/",
|
||||||
],
|
],
|
||||||
"cwd": "${workspaceFolder}",
|
//"cwd": "${workspaceFolder}/build/sample/",
|
||||||
"preLaunchTask": "CMake: build"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "lldb",
|
"type": "lldb",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "aout",
|
"name": "experimentPy",
|
||||||
"program": "${workspaceFolder}/a.out",
|
"program": "${workspaceFolder}/build_debug/src/Platform/b_main",
|
||||||
|
"args": [
|
||||||
|
"-m",
|
||||||
|
"STree",
|
||||||
|
"--stratified",
|
||||||
|
"-d",
|
||||||
|
"iris",
|
||||||
|
//"--discretize"
|
||||||
|
// "--hyperparameters",
|
||||||
|
// "{\"repeatSparent\": true, \"maxModels\": 12}"
|
||||||
|
],
|
||||||
|
"cwd": "${workspaceFolder}/../discretizbench",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "gridsearch",
|
||||||
|
"program": "${workspaceFolder}/build_debug/src/Platform/b_grid",
|
||||||
|
"args": [
|
||||||
|
"-m",
|
||||||
|
"KDB",
|
||||||
|
"--discretize",
|
||||||
|
"--continue",
|
||||||
|
"glass",
|
||||||
|
"--only",
|
||||||
|
"--compute"
|
||||||
|
],
|
||||||
|
"cwd": "${workspaceFolder}/../discretizbench",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "experimentBayes",
|
||||||
|
"program": "${workspaceFolder}/build_debug/src/Platform/b_main",
|
||||||
|
"args": [
|
||||||
|
"-m",
|
||||||
|
"TAN",
|
||||||
|
"--stratified",
|
||||||
|
"--discretize",
|
||||||
|
"-d",
|
||||||
|
"iris",
|
||||||
|
"--hyperparameters",
|
||||||
|
"{\"repeatSparent\": true, \"maxModels\": 12}"
|
||||||
|
],
|
||||||
|
"cwd": "/home/rmontanana/Code/discretizbench",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "best",
|
||||||
|
"program": "${workspaceFolder}/build_debug/src/Platform/b_best",
|
||||||
|
"args": [
|
||||||
|
"-m",
|
||||||
|
"BoostAODE",
|
||||||
|
"-s",
|
||||||
|
"accuracy",
|
||||||
|
"--build",
|
||||||
|
],
|
||||||
|
"cwd": "${workspaceFolder}/../discretizbench",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "manage",
|
||||||
|
"program": "${workspaceFolder}/build_debug/src/Platform/b_manage",
|
||||||
|
"args": [
|
||||||
|
"-n",
|
||||||
|
"20"
|
||||||
|
],
|
||||||
|
"cwd": "${workspaceFolder}/../discretizbench",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "list",
|
||||||
|
"program": "${workspaceFolder}/build_debug/src/Platform/b_list",
|
||||||
"args": [],
|
"args": [],
|
||||||
"cwd": "${workspaceFolder}"
|
//"cwd": "/Users/rmontanana/Code/discretizbench",
|
||||||
|
"cwd": "${workspaceFolder}/../discretizbench",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "test",
|
||||||
|
"program": "${workspaceFolder}/build_debug/tests/unit_tests",
|
||||||
|
"args": [
|
||||||
|
"-c=\"Metrics Test\"",
|
||||||
|
// "-s",
|
||||||
|
],
|
||||||
|
"cwd": "${workspaceFolder}/build/tests",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Build & debug active file",
|
"name": "Build & debug active file",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/build/bayesnet",
|
"program": "${workspaceFolder}/build_debug/bayesnet",
|
||||||
"args": [],
|
"args": [],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
|
104
.vscode/settings.json
vendored
104
.vscode/settings.json
vendored
@@ -1,104 +0,0 @@
|
|||||||
{
|
|
||||||
"files.associations": {
|
|
||||||
"*.rmd": "markdown",
|
|
||||||
"*.py": "python",
|
|
||||||
"vector": "cpp",
|
|
||||||
"__bit_reference": "cpp",
|
|
||||||
"__bits": "cpp",
|
|
||||||
"__config": "cpp",
|
|
||||||
"__debug": "cpp",
|
|
||||||
"__errc": "cpp",
|
|
||||||
"__hash_table": "cpp",
|
|
||||||
"__locale": "cpp",
|
|
||||||
"__mutex_base": "cpp",
|
|
||||||
"__node_handle": "cpp",
|
|
||||||
"__nullptr": "cpp",
|
|
||||||
"__split_buffer": "cpp",
|
|
||||||
"__string": "cpp",
|
|
||||||
"__threading_support": "cpp",
|
|
||||||
"__tuple": "cpp",
|
|
||||||
"array": "cpp",
|
|
||||||
"atomic": "cpp",
|
|
||||||
"bitset": "cpp",
|
|
||||||
"cctype": "cpp",
|
|
||||||
"chrono": "cpp",
|
|
||||||
"clocale": "cpp",
|
|
||||||
"cmath": "cpp",
|
|
||||||
"compare": "cpp",
|
|
||||||
"complex": "cpp",
|
|
||||||
"concepts": "cpp",
|
|
||||||
"cstdarg": "cpp",
|
|
||||||
"cstddef": "cpp",
|
|
||||||
"cstdint": "cpp",
|
|
||||||
"cstdio": "cpp",
|
|
||||||
"cstdlib": "cpp",
|
|
||||||
"cstring": "cpp",
|
|
||||||
"ctime": "cpp",
|
|
||||||
"cwchar": "cpp",
|
|
||||||
"cwctype": "cpp",
|
|
||||||
"exception": "cpp",
|
|
||||||
"initializer_list": "cpp",
|
|
||||||
"ios": "cpp",
|
|
||||||
"iosfwd": "cpp",
|
|
||||||
"istream": "cpp",
|
|
||||||
"limits": "cpp",
|
|
||||||
"locale": "cpp",
|
|
||||||
"memory": "cpp",
|
|
||||||
"mutex": "cpp",
|
|
||||||
"new": "cpp",
|
|
||||||
"optional": "cpp",
|
|
||||||
"ostream": "cpp",
|
|
||||||
"ratio": "cpp",
|
|
||||||
"sstream": "cpp",
|
|
||||||
"stdexcept": "cpp",
|
|
||||||
"streambuf": "cpp",
|
|
||||||
"string": "cpp",
|
|
||||||
"string_view": "cpp",
|
|
||||||
"system_error": "cpp",
|
|
||||||
"tuple": "cpp",
|
|
||||||
"type_traits": "cpp",
|
|
||||||
"typeinfo": "cpp",
|
|
||||||
"unordered_map": "cpp",
|
|
||||||
"variant": "cpp",
|
|
||||||
"algorithm": "cpp",
|
|
||||||
"iostream": "cpp",
|
|
||||||
"iomanip": "cpp",
|
|
||||||
"numeric": "cpp",
|
|
||||||
"set": "cpp",
|
|
||||||
"__tree": "cpp",
|
|
||||||
"deque": "cpp",
|
|
||||||
"list": "cpp",
|
|
||||||
"map": "cpp",
|
|
||||||
"unordered_set": "cpp",
|
|
||||||
"any": "cpp",
|
|
||||||
"condition_variable": "cpp",
|
|
||||||
"forward_list": "cpp",
|
|
||||||
"fstream": "cpp",
|
|
||||||
"stack": "cpp",
|
|
||||||
"thread": "cpp",
|
|
||||||
"__memory": "cpp",
|
|
||||||
"filesystem": "cpp",
|
|
||||||
"*.toml": "toml",
|
|
||||||
"utility": "cpp",
|
|
||||||
"__verbose_abort": "cpp",
|
|
||||||
"bit": "cpp",
|
|
||||||
"random": "cpp",
|
|
||||||
"*.tcc": "cpp",
|
|
||||||
"functional": "cpp",
|
|
||||||
"iterator": "cpp",
|
|
||||||
"memory_resource": "cpp",
|
|
||||||
"format": "cpp",
|
|
||||||
"valarray": "cpp",
|
|
||||||
"regex": "cpp",
|
|
||||||
"span": "cpp",
|
|
||||||
"cfenv": "cpp",
|
|
||||||
"cinttypes": "cpp",
|
|
||||||
"csetjmp": "cpp",
|
|
||||||
"future": "cpp",
|
|
||||||
"queue": "cpp",
|
|
||||||
"typeindex": "cpp",
|
|
||||||
"shared_mutex": "cpp"
|
|
||||||
},
|
|
||||||
"cmake.configureOnOpen": false,
|
|
||||||
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
|
|
||||||
}
|
|
23
.vscode/tasks.json
vendored
23
.vscode/tasks.json
vendored
@@ -32,6 +32,29 @@
|
|||||||
],
|
],
|
||||||
"group": "build",
|
"group": "build",
|
||||||
"detail": "Task generated by Debugger."
|
"detail": "Task generated by Debugger."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "cppbuild",
|
||||||
|
"label": "C/C++: g++ build active file",
|
||||||
|
"command": "/usr/bin/g++",
|
||||||
|
"args": [
|
||||||
|
"-fdiagnostics-color=always",
|
||||||
|
"-g",
|
||||||
|
"${file}",
|
||||||
|
"-o",
|
||||||
|
"${fileDirname}/${fileBasenameNoExtension}"
|
||||||
|
],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${fileDirname}"
|
||||||
|
},
|
||||||
|
"problemMatcher": [
|
||||||
|
"$gcc"
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
},
|
||||||
|
"detail": "Task generated by Debugger."
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
@@ -1,16 +1,20 @@
|
|||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
project(BayesNet
|
project(BayesNet
|
||||||
VERSION 0.1.0
|
VERSION 0.2.0
|
||||||
DESCRIPTION "Bayesian Network and basic classifiers Library."
|
DESCRIPTION "Bayesian Network and basic classifiers Library."
|
||||||
HOMEPAGE_URL "https://github.com/rmontanana/bayesnet"
|
HOMEPAGE_URL "https://github.com/rmontanana/bayesnet"
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (CODE_COVERAGE AND NOT ENABLE_TESTING)
|
||||||
|
MESSAGE(FATAL_ERROR "Code coverage requires testing enabled")
|
||||||
|
endif (CODE_COVERAGE AND NOT ENABLE_TESTING)
|
||||||
|
|
||||||
find_package(Torch REQUIRED)
|
find_package(Torch REQUIRED)
|
||||||
|
|
||||||
if (POLICY CMP0135)
|
if (POLICY CMP0135)
|
||||||
cmake_policy(SET CMP0135 NEW)
|
cmake_policy(SET CMP0135 NEW)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# Global CMake variables
|
# Global CMake variables
|
||||||
@@ -20,40 +24,82 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
|
||||||
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
||||||
# Options
|
# Options
|
||||||
# -------
|
# -------
|
||||||
option(ENABLE_CLANG_TIDY "Enable to add clang tidy." OFF)
|
option(ENABLE_CLANG_TIDY "Enable to add clang tidy." OFF)
|
||||||
option(ENABLE_TESTING "Unit testing build" ON)
|
option(ENABLE_TESTING "Unit testing build" OFF)
|
||||||
option(CODE_COVERAGE "Collect coverage from test library" ON)
|
option(CODE_COVERAGE "Collect coverage from test library" OFF)
|
||||||
|
option(MPI_ENABLED "Enable MPI options" ON)
|
||||||
|
|
||||||
set(CMAKE_BUILD_TYPE "Debug")
|
if (MPI_ENABLED)
|
||||||
|
find_package(MPI REQUIRED)
|
||||||
|
message("MPI_CXX_LIBRARIES=${MPI_CXX_LIBRARIES}")
|
||||||
|
message("MPI_CXX_INCLUDE_DIRS=${MPI_CXX_INCLUDE_DIRS}")
|
||||||
|
endif (MPI_ENABLED)
|
||||||
|
|
||||||
|
# Boost Library
|
||||||
|
set(Boost_USE_STATIC_LIBS OFF)
|
||||||
|
set(Boost_USE_MULTITHREADED ON)
|
||||||
|
set(Boost_USE_STATIC_RUNTIME OFF)
|
||||||
|
find_package(Boost 1.66.0 REQUIRED COMPONENTS python3 numpy3)
|
||||||
|
if(Boost_FOUND)
|
||||||
|
message("Boost_INCLUDE_DIRS=${Boost_INCLUDE_DIRS}")
|
||||||
|
include_directories(${Boost_INCLUDE_DIRS})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Python
|
||||||
|
find_package(Python3 3.11...3.11.9 COMPONENTS Interpreter Development REQUIRED)
|
||||||
|
message("Python3_LIBRARIES=${Python3_LIBRARIES}")
|
||||||
|
|
||||||
# CMakes modules
|
# CMakes modules
|
||||||
# --------------
|
# --------------
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
|
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
|
||||||
|
include(AddGitSubmodule)
|
||||||
|
|
||||||
|
if (CODE_COVERAGE)
|
||||||
|
enable_testing()
|
||||||
|
include(CodeCoverage)
|
||||||
|
MESSAGE("Code coverage enabled")
|
||||||
|
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -O0 -g")
|
||||||
|
SET(GCC_COVERAGE_LINK_FLAGS " ${GCC_COVERAGE_LINK_FLAGS} -lgcov --coverage")
|
||||||
|
endif (CODE_COVERAGE)
|
||||||
|
|
||||||
|
if (ENABLE_CLANG_TIDY)
|
||||||
|
include(StaticAnalyzers) # clang-tidy
|
||||||
|
endif (ENABLE_CLANG_TIDY)
|
||||||
|
|
||||||
|
# External libraries - dependencies of BayesNet
|
||||||
|
# ---------------------------------------------
|
||||||
|
# include(FetchContent)
|
||||||
|
add_git_submodule("lib/mdlp")
|
||||||
|
add_git_submodule("lib/argparse")
|
||||||
|
add_git_submodule("lib/json")
|
||||||
|
|
||||||
|
|
||||||
|
find_library(XLSXWRITER_LIB NAMES libxlsxwriter.dylib libxlsxwriter.so PATHS ${BayesNet_SOURCE_DIR}/lib/libxlsxwriter/lib)
|
||||||
|
message("XLSXWRITER_LIB=${XLSXWRITER_LIB}")
|
||||||
|
|
||||||
|
|
||||||
# Subdirectories
|
# Subdirectories
|
||||||
# --------------
|
# --------------
|
||||||
add_subdirectory(config)
|
add_subdirectory(config)
|
||||||
add_subdirectory(src)
|
add_subdirectory(lib/Files)
|
||||||
|
add_subdirectory(src/BayesNet)
|
||||||
|
add_subdirectory(src/Platform)
|
||||||
|
add_subdirectory(src/PyClassifiers)
|
||||||
add_subdirectory(sample)
|
add_subdirectory(sample)
|
||||||
|
|
||||||
|
file(GLOB BayesNet_HEADERS CONFIGURE_DEPENDS ${BayesNet_SOURCE_DIR}/src/BayesNet/*.h ${BayesNet_SOURCE_DIR}/BayesNet/*.h)
|
||||||
|
file(GLOB BayesNet_SOURCES CONFIGURE_DEPENDS ${BayesNet_SOURCE_DIR}/src/BayesNet/*.cc ${BayesNet_SOURCE_DIR}/src/BayesNet/*.cpp)
|
||||||
|
file(GLOB Platform_SOURCES CONFIGURE_DEPENDS ${BayesNet_SOURCE_DIR}/src/Platform/*.cc ${BayesNet_SOURCE_DIR}/src/Platform/*.cpp)
|
||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
# -------
|
# -------
|
||||||
|
|
||||||
if (ENABLE_TESTING)
|
if (ENABLE_TESTING)
|
||||||
MESSAGE("Testing enabled")
|
MESSAGE("Testing enabled")
|
||||||
enable_testing()
|
add_git_submodule("lib/catch2")
|
||||||
if (CODE_COVERAGE)
|
|
||||||
include(CodeCoverage)
|
|
||||||
MESSAGE("Code coverage enabled")
|
|
||||||
set(CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
|
|
||||||
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
|
|
||||||
SET(GCC_COVERAGE_LINK_FLAGS " ${GCC_COVERAGE_LINK_FLAGS} -lgcov --coverage")
|
|
||||||
endif (CODE_COVERAGE)
|
|
||||||
find_package(Catch2 3 REQUIRED)
|
|
||||||
include(CTest)
|
include(CTest)
|
||||||
include(Catch)
|
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
endif (ENABLE_TESTING)
|
endif (ENABLE_TESTING)
|
||||||
|
122
Makefile
122
Makefile
@@ -1,6 +1,26 @@
|
|||||||
SHELL := /bin/bash
|
SHELL := /bin/bash
|
||||||
.DEFAULT_GOAL := help
|
.DEFAULT_GOAL := help
|
||||||
.PHONY: coverage setup help build test
|
.PHONY: coverage setup help build test clean debug release
|
||||||
|
|
||||||
|
f_release = build_release
|
||||||
|
f_debug = build_debug
|
||||||
|
app_targets = b_best b_list b_main b_manage b_grid
|
||||||
|
test_targets = unit_tests_bayesnet unit_tests_platform
|
||||||
|
n_procs = -j 16
|
||||||
|
|
||||||
|
define ClearTests
|
||||||
|
@for t in $(test_targets); do \
|
||||||
|
if [ -f $(f_debug)/tests/$$t ]; then \
|
||||||
|
echo ">>> Cleaning $$t..." ; \
|
||||||
|
rm -f $(f_debug)/tests/$$t ; \
|
||||||
|
fi ; \
|
||||||
|
done
|
||||||
|
@nfiles="$(find . -name "*.gcda" -print0)" ; \
|
||||||
|
if test "${nfiles}" != "" ; then \
|
||||||
|
find . -name "*.gcda" -print0 | xargs -0 rm 2>/dev/null ;\
|
||||||
|
fi ;
|
||||||
|
endef
|
||||||
|
|
||||||
|
|
||||||
setup: ## Install dependencies for tests and coverage
|
setup: ## Install dependencies for tests and coverage
|
||||||
@if [ "$(shell uname)" = "Darwin" ]; then \
|
@if [ "$(shell uname)" = "Darwin" ]; then \
|
||||||
@@ -11,35 +31,87 @@ setup: ## Install dependencies for tests and coverage
|
|||||||
pip install gcovr; \
|
pip install gcovr; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
dependency: ## Create a dependency graph diagram of the project (build/dependency.png)
|
dest ?= ${HOME}/bin
|
||||||
cd build && cmake .. --graphviz=dependency.dot && dot -Tpng dependency.dot -o dependency.png
|
install: ## Copy binary files to bin folder
|
||||||
|
@echo "Destination folder: $(dest)"
|
||||||
|
make buildr
|
||||||
|
@echo "*******************************************"
|
||||||
|
@echo ">>> Copying files to $(dest)"
|
||||||
|
@echo "*******************************************"
|
||||||
|
@for item in $(app_targets); do \
|
||||||
|
echo ">>> Copying $$item" ; \
|
||||||
|
cp $(f_release)/src/Platform/$$item $(dest) ; \
|
||||||
|
done
|
||||||
|
|
||||||
build: ## Build the project
|
dependency: ## Create a dependency graph diagram of the project (build/dependency.png)
|
||||||
@echo ">>> Building BayesNet ...";
|
@echo ">>> Creating dependency graph diagram of the project...";
|
||||||
@if [ -d ./build ]; then rm -rf ./build; fi
|
$(MAKE) debug
|
||||||
@mkdir build;
|
cd $(f_debug) && cmake .. --graphviz=dependency.dot && dot -Tpng dependency.dot -o dependency.png
|
||||||
cmake -S . -B build; \
|
|
||||||
cd build; \
|
buildd: ## Build the debug targets
|
||||||
make; \
|
cmake --build $(f_debug) -t $(app_targets) BayesNetSample $(n_procs)
|
||||||
|
|
||||||
|
buildr: ## Build the release targets
|
||||||
|
cmake --build $(f_release) -t $(app_targets) BayesNetSample $(n_procs)
|
||||||
|
|
||||||
|
clean: ## Clean the tests info
|
||||||
|
@echo ">>> Cleaning Debug BayesNet tests...";
|
||||||
|
$(call ClearTests)
|
||||||
@echo ">>> Done";
|
@echo ">>> Done";
|
||||||
|
|
||||||
test: ## Run tests
|
clang-uml: ## Create uml class and sequence diagrams
|
||||||
@echo "* Running tests...";
|
clang-uml -p --add-compile-flag -I /usr/lib/gcc/x86_64-redhat-linux/8/include/
|
||||||
find . -name "*.gcda" -print0 | xargs -0 rm
|
|
||||||
@cd build; \
|
debug: ## Build a debug version of the project
|
||||||
cmake --build . --target unit_tests ;
|
@echo ">>> Building Debug BayesNet...";
|
||||||
@cd build/tests; \
|
@if [ -d ./$(f_debug) ]; then rm -rf ./$(f_debug); fi
|
||||||
./unit_tests;
|
@mkdir $(f_debug);
|
||||||
|
@cmake -S . -B $(f_debug) -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CODE_COVERAGE=ON
|
||||||
|
@echo ">>> Done";
|
||||||
|
|
||||||
|
release: ## Build a Release version of the project
|
||||||
|
@echo ">>> Building Release BayesNet...";
|
||||||
|
@if [ -d ./$(f_release) ]; then rm -rf ./$(f_release); fi
|
||||||
|
@mkdir $(f_release);
|
||||||
|
@cmake -S . -B $(f_release) -D CMAKE_BUILD_TYPE=Release
|
||||||
|
@echo ">>> Done";
|
||||||
|
|
||||||
|
opt = ""
|
||||||
|
test: ## Run tests (opt="-s") to verbose output the tests, (opt="-c='Test Maximum Spanning Tree'") to run only that section
|
||||||
|
@echo ">>> Running BayesNet & Platform tests...";
|
||||||
|
@$(MAKE) clean
|
||||||
|
@cmake --build $(f_debug) -t $(test_targets) $(n_procs)
|
||||||
|
@for t in $(test_targets); do \
|
||||||
|
if [ -f $(f_debug)/tests/$$t ]; then \
|
||||||
|
cd $(f_debug)/tests ; \
|
||||||
|
./$$t $(opt) ; \
|
||||||
|
fi ; \
|
||||||
|
done
|
||||||
|
@echo ">>> Done";
|
||||||
|
|
||||||
|
opt = ""
|
||||||
|
testp: ## Run platform tests (opt="-s") to verbose output the tests, (opt="-c='Stratified Fold Test'") to run only that section
|
||||||
|
@echo ">>> Running Platform tests...";
|
||||||
|
@$(MAKE) clean
|
||||||
|
@cmake --build $(f_debug) --target unit_tests_platform $(n_procs)
|
||||||
|
@if [ -f $(f_debug)/tests/unit_tests_platform ]; then cd $(f_debug)/tests ; ./unit_tests_platform $(opt) ; fi ;
|
||||||
|
@echo ">>> Done";
|
||||||
|
|
||||||
|
opt = ""
|
||||||
|
testb: ## Run BayesNet tests (opt="-s") to verbose output the tests, (opt="-c='Test Maximum Spanning Tree'") to run only that section
|
||||||
|
@echo ">>> Running BayesNet tests...";
|
||||||
|
@$(MAKE) clean
|
||||||
|
@cmake --build $(f_debug) --target unit_tests_bayesnet $(n_procs)
|
||||||
|
@if [ -f $(f_debug)/tests/unit_tests_bayesnet ]; then cd $(f_debug)/tests ; ./unit_tests_bayesnet $(opt) ; fi ;
|
||||||
|
@echo ">>> Done";
|
||||||
|
|
||||||
coverage: ## Run tests and generate coverage report (build/index.html)
|
coverage: ## Run tests and generate coverage report (build/index.html)
|
||||||
@echo "*Building tests...";
|
@echo ">>> Building tests with coverage...";
|
||||||
find . -name "*.gcda" -print0 | xargs -0 rm
|
@$(MAKE) test
|
||||||
@cd build; \
|
@cd $(f_debug) ; \
|
||||||
cmake --build . --target unit_tests ;
|
gcovr --config ../gcovr.cfg tests ;
|
||||||
@cd build/tests; \
|
@echo ">>> Done";
|
||||||
./unit_tests;
|
|
||||||
gcovr ;
|
|
||||||
|
|
||||||
help: ## Show help message
|
help: ## Show help message
|
||||||
@IFS=$$'\n' ; \
|
@IFS=$$'\n' ; \
|
||||||
|
86
README.md
86
README.md
@@ -1,5 +1,91 @@
|
|||||||
# BayesNet
|
# BayesNet
|
||||||
|
|
||||||
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
Bayesian Network Classifier with libtorch from scratch
|
Bayesian Network Classifier with libtorch from scratch
|
||||||
|
|
||||||
|
## 0. Setup
|
||||||
|
|
||||||
|
Before compiling BayesNet.
|
||||||
|
|
||||||
|
### Miniconda
|
||||||
|
|
||||||
|
To be able to run Python Classifiers such as STree, ODTE, SVC, etc. it is needed to install Miniconda. To do so, download the installer from [Miniconda](https://docs.conda.io/en/latest/miniconda.html) and run it. It is recommended to install it in the home folder.
|
||||||
|
|
||||||
|
In Linux sometimes the library libstdc++ is mistaken from the miniconda installation and produces the next message when running the b_xxxx executables:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
libstdc++.so.6: version `GLIBCXX_3.4.32' not found (required by b_xxxx)
|
||||||
|
```
|
||||||
|
|
||||||
|
The solution is to erase the libstdc++ library from the miniconda installation:
|
||||||
|
|
||||||
|
### MPI
|
||||||
|
|
||||||
|
In Linux just install openmpi & openmpi-devel packages. Only if cmake can't find openmpi installation (like in Oracle Linux) set the following variable:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export MPI_HOME="/usr/lib64/openmpi"
|
||||||
|
```
|
||||||
|
|
||||||
|
In Mac OS X, install mpich with brew and if cmake doesn't find it, edit mpicxx wrapper to remove the ",-commons,use_dylibs" from final_ldflags
|
||||||
|
|
||||||
|
```bash
|
||||||
|
vi /opt/homebrew/bin/mpicx
|
||||||
|
```
|
||||||
|
|
||||||
|
### boost library
|
||||||
|
|
||||||
|
[Getting Started](<https://www.boost.org/doc/libs/1_83_0/more/getting_started/index.html>)
|
||||||
|
|
||||||
|
The best option is install the packages that the Linux distribution have in its repository. If this is the case:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo dnf install boost-devel
|
||||||
|
```
|
||||||
|
|
||||||
|
If this is not possible and the compressed packaged is installed, the following environment variable has to be set pointing to the folder where it was unzipped to:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export BOOST_ROOT=/path/to/library/
|
||||||
|
```
|
||||||
|
|
||||||
|
In some cases, it is needed to build the library, to do so:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd /path/to/library
|
||||||
|
mkdir own
|
||||||
|
./bootstrap.sh --prefix=/path/to/library/own
|
||||||
|
./b2 install
|
||||||
|
export BOOST_ROOT=/path/to/library/own/
|
||||||
|
```
|
||||||
|
|
||||||
|
Don't forget to add the export BOOST_ROOT statement to .bashrc or wherever it is meant to be.
|
||||||
|
|
||||||
|
### libxlswriter
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd lib/libxlsxwriter
|
||||||
|
make
|
||||||
|
make install DESTDIR=/home/rmontanana/Code PREFIX=
|
||||||
|
```
|
||||||
|
|
||||||
|
Environment variable has to be set:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export LD_LIBRARY_PATH=/usr/local/lib
|
||||||
|
```
|
||||||
|
|
||||||
|
### Release
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make release
|
||||||
|
```
|
||||||
|
|
||||||
|
### Debug & Tests
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make debug
|
||||||
|
```
|
||||||
|
|
||||||
## 1. Introduction
|
## 1. Introduction
|
||||||
|
12
cmake/modules/AddGitSubmodule.cmake
Normal file
12
cmake/modules/AddGitSubmodule.cmake
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
function(add_git_submodule dir)
|
||||||
|
find_package(Git REQUIRED)
|
||||||
|
|
||||||
|
if(NOT EXISTS ${dir}/CMakeLists.txt)
|
||||||
|
message(STATUS "🚨 Adding git submodule => ${dir}")
|
||||||
|
execute_process(COMMAND ${GIT_EXECUTABLE}
|
||||||
|
submodule update --init --recursive -- ${dir}
|
||||||
|
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
|
||||||
|
endif()
|
||||||
|
add_subdirectory(${dir})
|
||||||
|
endfunction(add_git_submodule)
|
22
cmake/modules/StaticAnalyzers.cmake
Normal file
22
cmake/modules/StaticAnalyzers.cmake
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
if(ENABLE_CLANG_TIDY)
|
||||||
|
find_program(CLANG_TIDY_COMMAND NAMES clang-tidy)
|
||||||
|
|
||||||
|
if(NOT CLANG_TIDY_COMMAND)
|
||||||
|
message(WARNING "🔴 CMake_RUN_CLANG_TIDY is ON but clang-tidy is not found!")
|
||||||
|
set(CMAKE_CXX_CLANG_TIDY "" CACHE STRING "" FORCE)
|
||||||
|
else()
|
||||||
|
|
||||||
|
message(STATUS "🟢 CMake_RUN_CLANG_TIDY is ON")
|
||||||
|
set(CLANGTIDY_EXTRA_ARGS
|
||||||
|
"-extra-arg=-Wno-unknown-warning-option"
|
||||||
|
)
|
||||||
|
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND};-p=${CMAKE_BINARY_DIR};${CLANGTIDY_EXTRA_ARGS}" CACHE STRING "" FORCE)
|
||||||
|
|
||||||
|
add_custom_target(clang-tidy
|
||||||
|
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target ${CMAKE_PROJECT_NAME}
|
||||||
|
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target clang-tidy
|
||||||
|
COMMENT "Running clang-tidy..."
|
||||||
|
)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
endif()
|
||||||
|
endif(ENABLE_CLANG_TIDY)
|
BIN
diagrams/BayesNet.pdf
Executable file
BIN
diagrams/BayesNet.pdf
Executable file
Binary file not shown.
@@ -1,5 +1,4 @@
|
|||||||
filter = src/
|
filter = src/
|
||||||
exclude = external/
|
exclude-directories = build/lib/
|
||||||
exclude = tests/
|
|
||||||
print-summary = yes
|
print-summary = yes
|
||||||
sort-percentage = yes
|
sort-percentage = yes
|
||||||
|
162
grid_stree.json
Normal file
162
grid_stree.json
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
{
|
||||||
|
"balance-scale": {
|
||||||
|
"C": 10000.0,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "rbf",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"balloons": {
|
||||||
|
"C": 7,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "rbf",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"breast-cancer-wisc-diag": {
|
||||||
|
"C": 0.2,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"breast-cancer-wisc-prog": {
|
||||||
|
"C": 0.2,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"breast-cancer-wisc": {},
|
||||||
|
"breast-cancer": {},
|
||||||
|
"cardiotocography-10clases": {},
|
||||||
|
"cardiotocography-3clases": {},
|
||||||
|
"conn-bench-sonar-mines-rocks": {},
|
||||||
|
"cylinder-bands": {},
|
||||||
|
"dermatology": {
|
||||||
|
"C": 55,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"echocardiogram": {
|
||||||
|
"C": 7,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "poly",
|
||||||
|
"max_features": "auto",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"fertility": {
|
||||||
|
"C": 0.05,
|
||||||
|
"max_features": "auto",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"haberman-survival": {},
|
||||||
|
"heart-hungarian": {
|
||||||
|
"C": 0.05,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"hepatitis": {
|
||||||
|
"C": 7,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "rbf",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"ilpd-indian-liver": {},
|
||||||
|
"ionosphere": {
|
||||||
|
"C": 7,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "rbf",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"iris": {},
|
||||||
|
"led-display": {},
|
||||||
|
"libras": {
|
||||||
|
"C": 0.08,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"low-res-spect": {
|
||||||
|
"C": 0.05,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"lymphography": {
|
||||||
|
"C": 0.05,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"mammographic": {},
|
||||||
|
"molec-biol-promoter": {
|
||||||
|
"C": 0.05,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "poly",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"musk-1": {
|
||||||
|
"C": 0.05,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "poly",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"oocytes_merluccius_nucleus_4d": {
|
||||||
|
"C": 8.25,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "poly"
|
||||||
|
},
|
||||||
|
"oocytes_merluccius_states_2f": {},
|
||||||
|
"oocytes_trisopterus_nucleus_2f": {},
|
||||||
|
"oocytes_trisopterus_states_5b": {
|
||||||
|
"C": 0.11,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"parkinsons": {},
|
||||||
|
"pima": {},
|
||||||
|
"pittsburg-bridges-MATERIAL": {
|
||||||
|
"C": 7,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "rbf",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"pittsburg-bridges-REL-L": {},
|
||||||
|
"pittsburg-bridges-SPAN": {
|
||||||
|
"C": 0.05,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"pittsburg-bridges-T-OR-D": {},
|
||||||
|
"planning": {
|
||||||
|
"C": 7,
|
||||||
|
"gamma": 10.0,
|
||||||
|
"kernel": "rbf",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"post-operative": {
|
||||||
|
"C": 55,
|
||||||
|
"degree": 5,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "poly",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"seeds": {
|
||||||
|
"C": 10000.0,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"statlog-australian-credit": {
|
||||||
|
"C": 0.05,
|
||||||
|
"max_features": "auto",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"statlog-german-credit": {},
|
||||||
|
"statlog-heart": {},
|
||||||
|
"statlog-image": {
|
||||||
|
"C": 7,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"statlog-vehicle": {},
|
||||||
|
"synthetic-control": {
|
||||||
|
"C": 0.55,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"tic-tac-toe": {
|
||||||
|
"C": 0.2,
|
||||||
|
"gamma": 0.1,
|
||||||
|
"kernel": "poly",
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"vertebral-column-2clases": {},
|
||||||
|
"wine": {
|
||||||
|
"C": 0.55,
|
||||||
|
"max_iter": 10000
|
||||||
|
},
|
||||||
|
"zoo": {
|
||||||
|
"C": 0.1,
|
||||||
|
"max_iter": 10000
|
||||||
|
}
|
||||||
|
}
|
168
lib/Files/ArffFiles.cc
Normal file
168
lib/Files/ArffFiles.cc
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
#include "ArffFiles.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <map>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
ArffFiles::ArffFiles() = default;
|
||||||
|
|
||||||
|
std::vector<std::string> ArffFiles::getLines() const
|
||||||
|
{
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long int ArffFiles::getSize() const
|
||||||
|
{
|
||||||
|
return lines.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::pair<std::string, std::string>> ArffFiles::getAttributes() const
|
||||||
|
{
|
||||||
|
return attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ArffFiles::getClassName() const
|
||||||
|
{
|
||||||
|
return className;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ArffFiles::getClassType() const
|
||||||
|
{
|
||||||
|
return classType;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::vector<float>>& ArffFiles::getX()
|
||||||
|
{
|
||||||
|
return X;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int>& ArffFiles::getY()
|
||||||
|
{
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArffFiles::loadCommon(std::string fileName)
|
||||||
|
{
|
||||||
|
std::ifstream file(fileName);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
throw std::invalid_argument("Unable to open file");
|
||||||
|
}
|
||||||
|
std::string line;
|
||||||
|
std::string keyword;
|
||||||
|
std::string attribute;
|
||||||
|
std::string type;
|
||||||
|
std::string type_w;
|
||||||
|
while (getline(file, line)) {
|
||||||
|
if (line.empty() || line[0] == '%' || line == "\r" || line == " ") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (line.find("@attribute") != std::string::npos || line.find("@ATTRIBUTE") != std::string::npos) {
|
||||||
|
std::stringstream ss(line);
|
||||||
|
ss >> keyword >> attribute;
|
||||||
|
type = "";
|
||||||
|
while (ss >> type_w)
|
||||||
|
type += type_w + " ";
|
||||||
|
attributes.emplace_back(trim(attribute), trim(type));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (line[0] == '@') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
lines.push_back(line);
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
if (attributes.empty())
|
||||||
|
throw std::invalid_argument("No attributes found");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArffFiles::load(const std::string& fileName, bool classLast)
|
||||||
|
{
|
||||||
|
int labelIndex;
|
||||||
|
loadCommon(fileName);
|
||||||
|
if (classLast) {
|
||||||
|
className = std::get<0>(attributes.back());
|
||||||
|
classType = std::get<1>(attributes.back());
|
||||||
|
attributes.pop_back();
|
||||||
|
labelIndex = static_cast<int>(attributes.size());
|
||||||
|
} else {
|
||||||
|
className = std::get<0>(attributes.front());
|
||||||
|
classType = std::get<1>(attributes.front());
|
||||||
|
attributes.erase(attributes.begin());
|
||||||
|
labelIndex = 0;
|
||||||
|
}
|
||||||
|
generateDataset(labelIndex);
|
||||||
|
}
|
||||||
|
void ArffFiles::load(const std::string& fileName, const std::string& name)
|
||||||
|
{
|
||||||
|
int labelIndex;
|
||||||
|
loadCommon(fileName);
|
||||||
|
bool found = false;
|
||||||
|
for (int i = 0; i < attributes.size(); ++i) {
|
||||||
|
if (attributes[i].first == name) {
|
||||||
|
className = std::get<0>(attributes[i]);
|
||||||
|
classType = std::get<1>(attributes[i]);
|
||||||
|
attributes.erase(attributes.begin() + i);
|
||||||
|
labelIndex = i;
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
throw std::invalid_argument("Class name not found");
|
||||||
|
}
|
||||||
|
generateDataset(labelIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArffFiles::generateDataset(int labelIndex)
|
||||||
|
{
|
||||||
|
X = std::vector<std::vector<float>>(attributes.size(), std::vector<float>(lines.size()));
|
||||||
|
auto yy = std::vector<std::string>(lines.size(), "");
|
||||||
|
auto removeLines = std::vector<int>(); // Lines with missing values
|
||||||
|
for (size_t i = 0; i < lines.size(); i++) {
|
||||||
|
std::stringstream ss(lines[i]);
|
||||||
|
std::string value;
|
||||||
|
int pos = 0;
|
||||||
|
int xIndex = 0;
|
||||||
|
while (getline(ss, value, ',')) {
|
||||||
|
if (pos++ == labelIndex) {
|
||||||
|
yy[i] = value;
|
||||||
|
} else {
|
||||||
|
if (value == "?") {
|
||||||
|
X[xIndex++][i] = -1;
|
||||||
|
removeLines.push_back(i);
|
||||||
|
} else
|
||||||
|
X[xIndex++][i] = stof(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto i : removeLines) {
|
||||||
|
yy.erase(yy.begin() + i);
|
||||||
|
for (auto& x : X) {
|
||||||
|
x.erase(x.begin() + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
y = factorize(yy);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ArffFiles::trim(const std::string& source)
|
||||||
|
{
|
||||||
|
std::string s(source);
|
||||||
|
s.erase(0, s.find_first_not_of(" '\n\r\t"));
|
||||||
|
s.erase(s.find_last_not_of(" '\n\r\t") + 1);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> ArffFiles::factorize(const std::vector<std::string>& labels_t)
|
||||||
|
{
|
||||||
|
std::vector<int> yy;
|
||||||
|
yy.reserve(labels_t.size());
|
||||||
|
std::map<std::string, int> labelMap;
|
||||||
|
int i = 0;
|
||||||
|
for (const std::string& label : labels_t) {
|
||||||
|
if (labelMap.find(label) == labelMap.end()) {
|
||||||
|
labelMap[label] = i++;
|
||||||
|
}
|
||||||
|
yy.push_back(labelMap[label]);
|
||||||
|
}
|
||||||
|
return yy;
|
||||||
|
}
|
32
lib/Files/ArffFiles.h
Normal file
32
lib/Files/ArffFiles.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef ARFFFILES_H
|
||||||
|
#define ARFFFILES_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class ArffFiles {
|
||||||
|
private:
|
||||||
|
std::vector<std::string> lines;
|
||||||
|
std::vector<std::pair<std::string, std::string>> attributes;
|
||||||
|
std::string className;
|
||||||
|
std::string classType;
|
||||||
|
std::vector<std::vector<float>> X;
|
||||||
|
std::vector<int> y;
|
||||||
|
void generateDataset(int);
|
||||||
|
void loadCommon(std::string);
|
||||||
|
public:
|
||||||
|
ArffFiles();
|
||||||
|
void load(const std::string&, bool = true);
|
||||||
|
void load(const std::string&, const std::string&);
|
||||||
|
std::vector<std::string> getLines() const;
|
||||||
|
unsigned long int getSize() const;
|
||||||
|
std::string getClassName() const;
|
||||||
|
std::string getClassType() const;
|
||||||
|
static std::string trim(const std::string&);
|
||||||
|
std::vector<std::vector<float>>& getX();
|
||||||
|
std::vector<int>& getY();
|
||||||
|
std::vector<std::pair<std::string, std::string>> getAttributes() const;
|
||||||
|
static std::vector<int> factorize(const std::vector<std::string>& labels_t);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
1
lib/Files/CMakeLists.txt
Normal file
1
lib/Files/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
add_library(ArffFiles ArffFiles.cc)
|
1
lib/argparse
Submodule
1
lib/argparse
Submodule
Submodule lib/argparse added at 69dabd88a8
1
lib/catch2
Submodule
1
lib/catch2
Submodule
Submodule lib/catch2 added at 766541d12d
1
lib/json
Submodule
1
lib/json
Submodule
Submodule lib/json added at edffad036d
1
lib/libxlsxwriter
Submodule
1
lib/libxlsxwriter
Submodule
Submodule lib/libxlsxwriter added at 29355a0887
1
lib/mdlp
Submodule
1
lib/mdlp
Submodule
Submodule lib/mdlp added at 5708dc3de9
@@ -1,132 +0,0 @@
|
|||||||
#include "ArffFiles.h"
|
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
ArffFiles::ArffFiles() = default;
|
|
||||||
|
|
||||||
vector<string> ArffFiles::getLines() const
|
|
||||||
{
|
|
||||||
return lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long int ArffFiles::getSize() const
|
|
||||||
{
|
|
||||||
return lines.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<pair<string, string>> ArffFiles::getAttributes() const
|
|
||||||
{
|
|
||||||
return attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
string ArffFiles::getClassName() const
|
|
||||||
{
|
|
||||||
return className;
|
|
||||||
}
|
|
||||||
|
|
||||||
string ArffFiles::getClassType() const
|
|
||||||
{
|
|
||||||
return classType;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<vector<float>>& ArffFiles::getX()
|
|
||||||
{
|
|
||||||
return X;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<int>& ArffFiles::getY()
|
|
||||||
{
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ArffFiles::load(const string& fileName, bool classLast)
|
|
||||||
{
|
|
||||||
ifstream file(fileName);
|
|
||||||
if (!file.is_open()) {
|
|
||||||
throw invalid_argument("Unable to open file");
|
|
||||||
}
|
|
||||||
string line;
|
|
||||||
string keyword;
|
|
||||||
string attribute;
|
|
||||||
string type;
|
|
||||||
string type_w;
|
|
||||||
while (getline(file, line)) {
|
|
||||||
if (line.empty() || line[0] == '%' || line == "\r" || line == " ") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (line.find("@attribute") != string::npos || line.find("@ATTRIBUTE") != string::npos) {
|
|
||||||
stringstream ss(line);
|
|
||||||
ss >> keyword >> attribute;
|
|
||||||
type = "";
|
|
||||||
while (ss >> type_w)
|
|
||||||
type += type_w + " ";
|
|
||||||
attributes.emplace_back(trim(attribute), trim(type));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (line[0] == '@') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
lines.push_back(line);
|
|
||||||
}
|
|
||||||
file.close();
|
|
||||||
if (attributes.empty())
|
|
||||||
throw invalid_argument("No attributes found");
|
|
||||||
if (classLast) {
|
|
||||||
className = get<0>(attributes.back());
|
|
||||||
classType = get<1>(attributes.back());
|
|
||||||
attributes.pop_back();
|
|
||||||
} else {
|
|
||||||
className = get<0>(attributes.front());
|
|
||||||
classType = get<1>(attributes.front());
|
|
||||||
attributes.erase(attributes.begin());
|
|
||||||
}
|
|
||||||
generateDataset(classLast);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void ArffFiles::generateDataset(bool classLast)
|
|
||||||
{
|
|
||||||
X = vector<vector<float>>(attributes.size(), vector<float>(lines.size()));
|
|
||||||
auto yy = vector<string>(lines.size(), "");
|
|
||||||
int labelIndex = classLast ? static_cast<int>(attributes.size()) : 0;
|
|
||||||
for (size_t i = 0; i < lines.size(); i++) {
|
|
||||||
stringstream ss(lines[i]);
|
|
||||||
string value;
|
|
||||||
int pos = 0;
|
|
||||||
int xIndex = 0;
|
|
||||||
while (getline(ss, value, ',')) {
|
|
||||||
if (pos++ == labelIndex) {
|
|
||||||
yy[i] = value;
|
|
||||||
} else {
|
|
||||||
X[xIndex++][i] = stof(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
y = factorize(yy);
|
|
||||||
}
|
|
||||||
|
|
||||||
string ArffFiles::trim(const string& source)
|
|
||||||
{
|
|
||||||
string s(source);
|
|
||||||
s.erase(0, s.find_first_not_of(" '\n\r\t"));
|
|
||||||
s.erase(s.find_last_not_of(" '\n\r\t") + 1);
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<int> ArffFiles::factorize(const vector<string>& labels_t)
|
|
||||||
{
|
|
||||||
vector<int> yy;
|
|
||||||
yy.reserve(labels_t.size());
|
|
||||||
map<string, int> labelMap;
|
|
||||||
int i = 0;
|
|
||||||
for (const string& label : labels_t) {
|
|
||||||
if (labelMap.find(label) == labelMap.end()) {
|
|
||||||
labelMap[label] = i++;
|
|
||||||
}
|
|
||||||
yy.push_back(labelMap[label]);
|
|
||||||
}
|
|
||||||
return yy;
|
|
||||||
}
|
|
@@ -1,34 +0,0 @@
|
|||||||
#ifndef ARFFFILES_H
|
|
||||||
#define ARFFFILES_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class ArffFiles {
|
|
||||||
private:
|
|
||||||
vector<string> lines;
|
|
||||||
vector<pair<string, string>> attributes;
|
|
||||||
string className;
|
|
||||||
string classType;
|
|
||||||
vector<vector<float>> X;
|
|
||||||
vector<int> y;
|
|
||||||
|
|
||||||
void generateDataset(bool);
|
|
||||||
|
|
||||||
public:
|
|
||||||
ArffFiles();
|
|
||||||
void load(const string&, bool = true);
|
|
||||||
vector<string> getLines() const;
|
|
||||||
unsigned long int getSize() const;
|
|
||||||
string getClassName() const;
|
|
||||||
string getClassType() const;
|
|
||||||
static string trim(const string&);
|
|
||||||
vector<vector<float>>& getX();
|
|
||||||
vector<int>& getY();
|
|
||||||
vector<pair<string, string>> getAttributes() const;
|
|
||||||
static vector<int> factorize(const vector<string>& labels_t);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@@ -1,4 +1,10 @@
|
|||||||
include_directories(${BayesNet_SOURCE_DIR}/src)
|
include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
|
||||||
link_directories(${MyProject_SOURCE_DIR}/src)
|
include_directories(${BayesNet_SOURCE_DIR}/src/BayesNet)
|
||||||
add_executable(main main.cc ArffFiles.cc CPPFImdlp.cpp Metrics.cpp)
|
include_directories(${BayesNet_SOURCE_DIR}/src/PyClassifiers)
|
||||||
target_link_libraries(main BayesNet "${TORCH_LIBRARIES}")
|
include_directories(${Python3_INCLUDE_DIRS})
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
|
||||||
|
add_executable(BayesNetSample sample.cc ${BayesNet_SOURCE_DIR}/src/Platform/Folding.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc)
|
||||||
|
target_link_libraries(BayesNetSample BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}" PyWrap)
|
@@ -1,221 +0,0 @@
|
|||||||
#include <numeric>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <set>
|
|
||||||
#include <cmath>
|
|
||||||
#include "CPPFImdlp.h"
|
|
||||||
#include "Metrics.h"
|
|
||||||
|
|
||||||
namespace mdlp {
|
|
||||||
|
|
||||||
CPPFImdlp::CPPFImdlp(size_t min_length_, int max_depth_, float proposed) : min_length(min_length_),
|
|
||||||
max_depth(max_depth_),
|
|
||||||
proposed_cuts(proposed)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
CPPFImdlp::CPPFImdlp() = default;
|
|
||||||
|
|
||||||
CPPFImdlp::~CPPFImdlp() = default;
|
|
||||||
|
|
||||||
size_t CPPFImdlp::compute_max_num_cut_points() const
|
|
||||||
{
|
|
||||||
// Set the actual maximum number of cut points as a number or as a percentage of the number of samples
|
|
||||||
if (proposed_cuts == 0) {
|
|
||||||
return numeric_limits<size_t>::max();
|
|
||||||
}
|
|
||||||
if (proposed_cuts < 0 || proposed_cuts > static_cast<float>(X.size())) {
|
|
||||||
throw invalid_argument("wrong proposed num_cuts value");
|
|
||||||
}
|
|
||||||
if (proposed_cuts < 1)
|
|
||||||
return static_cast<size_t>(round(static_cast<float>(X.size()) * proposed_cuts));
|
|
||||||
return static_cast<size_t>(proposed_cuts);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CPPFImdlp::fit(samples_t& X_, labels_t& y_)
|
|
||||||
{
|
|
||||||
X = X_;
|
|
||||||
y = y_;
|
|
||||||
num_cut_points = compute_max_num_cut_points();
|
|
||||||
depth = 0;
|
|
||||||
discretizedData.clear();
|
|
||||||
cutPoints.clear();
|
|
||||||
if (X.size() != y.size()) {
|
|
||||||
throw invalid_argument("X and y must have the same size");
|
|
||||||
}
|
|
||||||
if (X.empty() || y.empty()) {
|
|
||||||
throw invalid_argument("X and y must have at least one element");
|
|
||||||
}
|
|
||||||
if (min_length < 3) {
|
|
||||||
throw invalid_argument("min_length must be greater than 2");
|
|
||||||
}
|
|
||||||
if (max_depth < 1) {
|
|
||||||
throw invalid_argument("max_depth must be greater than 0");
|
|
||||||
}
|
|
||||||
indices = sortIndices(X_, y_);
|
|
||||||
metrics.setData(y, indices);
|
|
||||||
computeCutPoints(0, X.size(), 1);
|
|
||||||
sort(cutPoints.begin(), cutPoints.end());
|
|
||||||
if (num_cut_points > 0) {
|
|
||||||
// Select the best (with lower entropy) cut points
|
|
||||||
while (cutPoints.size() > num_cut_points) {
|
|
||||||
resizeCutPoints();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pair<precision_t, size_t> CPPFImdlp::valueCutPoint(size_t start, size_t cut, size_t end)
|
|
||||||
{
|
|
||||||
size_t n;
|
|
||||||
size_t m;
|
|
||||||
size_t idxPrev = cut - 1 >= start ? cut - 1 : cut;
|
|
||||||
size_t idxNext = cut + 1 < end ? cut + 1 : cut;
|
|
||||||
bool backWall; // true if duplicates reach beginning of the interval
|
|
||||||
precision_t previous;
|
|
||||||
precision_t actual;
|
|
||||||
precision_t next;
|
|
||||||
previous = X[indices[idxPrev]];
|
|
||||||
actual = X[indices[cut]];
|
|
||||||
next = X[indices[idxNext]];
|
|
||||||
// definition 2 of the paper => X[t-1] < X[t]
|
|
||||||
// get the first equal value of X in the interval
|
|
||||||
while (idxPrev > start && actual == previous) {
|
|
||||||
previous = X[indices[--idxPrev]];
|
|
||||||
}
|
|
||||||
backWall = idxPrev == start && actual == previous;
|
|
||||||
// get the last equal value of X in the interval
|
|
||||||
while (idxNext < end - 1 && actual == next) {
|
|
||||||
next = X[indices[++idxNext]];
|
|
||||||
}
|
|
||||||
// # of duplicates before cutpoint
|
|
||||||
n = cut - 1 - idxPrev;
|
|
||||||
// # of duplicates after cutpoint
|
|
||||||
m = idxNext - cut - 1;
|
|
||||||
// Decide which values to use
|
|
||||||
cut = cut + (backWall ? m + 1 : -n);
|
|
||||||
actual = X[indices[cut]];
|
|
||||||
return { (actual + previous) / 2, cut };
|
|
||||||
}
|
|
||||||
|
|
||||||
void CPPFImdlp::computeCutPoints(size_t start, size_t end, int depth_)
|
|
||||||
{
|
|
||||||
size_t cut;
|
|
||||||
pair<precision_t, size_t> result;
|
|
||||||
// Check if the interval length and the depth are Ok
|
|
||||||
if (end - start < min_length || depth_ > max_depth)
|
|
||||||
return;
|
|
||||||
depth = depth_ > depth ? depth_ : depth;
|
|
||||||
cut = getCandidate(start, end);
|
|
||||||
if (cut == numeric_limits<size_t>::max())
|
|
||||||
return;
|
|
||||||
if (mdlp(start, cut, end)) {
|
|
||||||
result = valueCutPoint(start, cut, end);
|
|
||||||
cut = result.second;
|
|
||||||
cutPoints.push_back(result.first);
|
|
||||||
computeCutPoints(start, cut, depth_ + 1);
|
|
||||||
computeCutPoints(cut, end, depth_ + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t CPPFImdlp::getCandidate(size_t start, size_t end)
|
|
||||||
{
|
|
||||||
/* Definition 1: A binary discretization for A is determined by selecting the cut point TA for which
|
|
||||||
E(A, TA; S) is minimal amongst all the candidate cut points. */
|
|
||||||
size_t candidate = numeric_limits<size_t>::max();
|
|
||||||
size_t elements = end - start;
|
|
||||||
bool sameValues = true;
|
|
||||||
precision_t entropy_left;
|
|
||||||
precision_t entropy_right;
|
|
||||||
precision_t minEntropy;
|
|
||||||
// Check if all the values of the variable in the interval are the same
|
|
||||||
for (size_t idx = start + 1; idx < end; idx++) {
|
|
||||||
if (X[indices[idx]] != X[indices[start]]) {
|
|
||||||
sameValues = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (sameValues)
|
|
||||||
return candidate;
|
|
||||||
minEntropy = metrics.entropy(start, end);
|
|
||||||
for (size_t idx = start + 1; idx < end; idx++) {
|
|
||||||
// Cutpoints are always on boundaries (definition 2)
|
|
||||||
if (y[indices[idx]] == y[indices[idx - 1]])
|
|
||||||
continue;
|
|
||||||
entropy_left = precision_t(idx - start) / static_cast<precision_t>(elements) * metrics.entropy(start, idx);
|
|
||||||
entropy_right = precision_t(end - idx) / static_cast<precision_t>(elements) * metrics.entropy(idx, end);
|
|
||||||
if (entropy_left + entropy_right < minEntropy) {
|
|
||||||
minEntropy = entropy_left + entropy_right;
|
|
||||||
candidate = idx;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return candidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CPPFImdlp::mdlp(size_t start, size_t cut, size_t end)
|
|
||||||
{
|
|
||||||
int k;
|
|
||||||
int k1;
|
|
||||||
int k2;
|
|
||||||
precision_t ig;
|
|
||||||
precision_t delta;
|
|
||||||
precision_t ent;
|
|
||||||
precision_t ent1;
|
|
||||||
precision_t ent2;
|
|
||||||
auto N = precision_t(end - start);
|
|
||||||
k = metrics.computeNumClasses(start, end);
|
|
||||||
k1 = metrics.computeNumClasses(start, cut);
|
|
||||||
k2 = metrics.computeNumClasses(cut, end);
|
|
||||||
ent = metrics.entropy(start, end);
|
|
||||||
ent1 = metrics.entropy(start, cut);
|
|
||||||
ent2 = metrics.entropy(cut, end);
|
|
||||||
ig = metrics.informationGain(start, cut, end);
|
|
||||||
delta = static_cast<precision_t>(log2(pow(3, precision_t(k)) - 2) -
|
|
||||||
(precision_t(k) * ent - precision_t(k1) * ent1 - precision_t(k2) * ent2));
|
|
||||||
precision_t term = 1 / N * (log2(N - 1) + delta);
|
|
||||||
return ig > term;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Argsort from https://stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes
|
|
||||||
indices_t CPPFImdlp::sortIndices(samples_t& X_, labels_t& y_)
|
|
||||||
{
|
|
||||||
indices_t idx(X_.size());
|
|
||||||
iota(idx.begin(), idx.end(), 0);
|
|
||||||
stable_sort(idx.begin(), idx.end(), [&X_, &y_](size_t i1, size_t i2) {
|
|
||||||
if (X_[i1] == X_[i2])
|
|
||||||
return y_[i1] < y_[i2];
|
|
||||||
else
|
|
||||||
return X_[i1] < X_[i2];
|
|
||||||
});
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CPPFImdlp::resizeCutPoints()
|
|
||||||
{
|
|
||||||
//Compute entropy of each of the whole cutpoint set and discards the biggest value
|
|
||||||
precision_t maxEntropy = 0;
|
|
||||||
precision_t entropy;
|
|
||||||
size_t maxEntropyIdx = 0;
|
|
||||||
size_t begin = 0;
|
|
||||||
size_t end;
|
|
||||||
for (size_t idx = 0; idx < cutPoints.size(); idx++) {
|
|
||||||
end = begin;
|
|
||||||
while (X[indices[end]] < cutPoints[idx] && end < X.size())
|
|
||||||
end++;
|
|
||||||
entropy = metrics.entropy(begin, end);
|
|
||||||
if (entropy > maxEntropy) {
|
|
||||||
maxEntropy = entropy;
|
|
||||||
maxEntropyIdx = idx;
|
|
||||||
}
|
|
||||||
begin = end;
|
|
||||||
}
|
|
||||||
cutPoints.erase(cutPoints.begin() + static_cast<long>(maxEntropyIdx));
|
|
||||||
}
|
|
||||||
labels_t& CPPFImdlp::transform(const samples_t& data)
|
|
||||||
{
|
|
||||||
discretizedData.reserve(data.size());
|
|
||||||
for (const precision_t& item : data) {
|
|
||||||
auto upper = upper_bound(cutPoints.begin(), cutPoints.end(), item);
|
|
||||||
discretizedData.push_back(upper - cutPoints.begin());
|
|
||||||
}
|
|
||||||
return discretizedData;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,45 +0,0 @@
|
|||||||
#ifndef CPPFIMDLP_H
|
|
||||||
#define CPPFIMDLP_H
|
|
||||||
|
|
||||||
#include "typesFImdlp.h"
|
|
||||||
#include "Metrics.h"
|
|
||||||
#include <limits>
|
|
||||||
#include <utility>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace mdlp {
|
|
||||||
class CPPFImdlp {
|
|
||||||
protected:
|
|
||||||
size_t min_length = 3;
|
|
||||||
int depth = 0;
|
|
||||||
int max_depth = numeric_limits<int>::max();
|
|
||||||
float proposed_cuts = 0;
|
|
||||||
indices_t indices = indices_t();
|
|
||||||
samples_t X = samples_t();
|
|
||||||
labels_t y = labels_t();
|
|
||||||
Metrics metrics = Metrics(y, indices);
|
|
||||||
cutPoints_t cutPoints;
|
|
||||||
size_t num_cut_points = numeric_limits<size_t>::max();
|
|
||||||
labels_t discretizedData = labels_t();
|
|
||||||
|
|
||||||
static indices_t sortIndices(samples_t&, labels_t&);
|
|
||||||
|
|
||||||
void computeCutPoints(size_t, size_t, int);
|
|
||||||
void resizeCutPoints();
|
|
||||||
bool mdlp(size_t, size_t, size_t);
|
|
||||||
size_t getCandidate(size_t, size_t);
|
|
||||||
size_t compute_max_num_cut_points() const;
|
|
||||||
pair<precision_t, size_t> valueCutPoint(size_t, size_t, size_t);
|
|
||||||
|
|
||||||
public:
|
|
||||||
CPPFImdlp();
|
|
||||||
CPPFImdlp(size_t, int, float);
|
|
||||||
~CPPFImdlp();
|
|
||||||
void fit(samples_t&, labels_t&);
|
|
||||||
inline cutPoints_t getCutPoints() const { return cutPoints; };
|
|
||||||
labels_t& transform(const samples_t&);
|
|
||||||
inline int get_depth() const { return depth; };
|
|
||||||
static inline string version() { return "1.1.2"; };
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
@@ -1,78 +0,0 @@
|
|||||||
#include "Metrics.h"
|
|
||||||
#include <set>
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
namespace mdlp {
|
|
||||||
Metrics::Metrics(labels_t& y_, indices_t& indices_): y(y_), indices(indices_),
|
|
||||||
numClasses(computeNumClasses(0, indices.size()))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
int Metrics::computeNumClasses(size_t start, size_t end)
|
|
||||||
{
|
|
||||||
set<int> nClasses;
|
|
||||||
for (auto i = start; i < end; ++i) {
|
|
||||||
nClasses.insert(y[indices[i]]);
|
|
||||||
}
|
|
||||||
return static_cast<int>(nClasses.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
void Metrics::setData(const labels_t& y_, const indices_t& indices_)
|
|
||||||
{
|
|
||||||
indices = indices_;
|
|
||||||
y = y_;
|
|
||||||
numClasses = computeNumClasses(0, indices.size());
|
|
||||||
entropyCache.clear();
|
|
||||||
igCache.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
precision_t Metrics::entropy(size_t start, size_t end)
|
|
||||||
{
|
|
||||||
precision_t p;
|
|
||||||
precision_t ventropy = 0;
|
|
||||||
int nElements = 0;
|
|
||||||
labels_t counts(numClasses + 1, 0);
|
|
||||||
if (end - start < 2)
|
|
||||||
return 0;
|
|
||||||
if (entropyCache.find({ start, end }) != entropyCache.end()) {
|
|
||||||
return entropyCache[{start, end}];
|
|
||||||
}
|
|
||||||
for (auto i = &indices[start]; i != &indices[end]; ++i) {
|
|
||||||
counts[y[*i]]++;
|
|
||||||
nElements++;
|
|
||||||
}
|
|
||||||
for (auto count : counts) {
|
|
||||||
if (count > 0) {
|
|
||||||
p = static_cast<precision_t>(count) / static_cast<precision_t>(nElements);
|
|
||||||
ventropy -= p * log2(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entropyCache[{start, end}] = ventropy;
|
|
||||||
return ventropy;
|
|
||||||
}
|
|
||||||
|
|
||||||
precision_t Metrics::informationGain(size_t start, size_t cut, size_t end)
|
|
||||||
{
|
|
||||||
precision_t iGain;
|
|
||||||
precision_t entropyInterval;
|
|
||||||
precision_t entropyLeft;
|
|
||||||
precision_t entropyRight;
|
|
||||||
size_t nElementsLeft = cut - start;
|
|
||||||
size_t nElementsRight = end - cut;
|
|
||||||
size_t nElements = end - start;
|
|
||||||
if (igCache.find(make_tuple(start, cut, end)) != igCache.end()) {
|
|
||||||
return igCache[make_tuple(start, cut, end)];
|
|
||||||
}
|
|
||||||
entropyInterval = entropy(start, end);
|
|
||||||
entropyLeft = entropy(start, cut);
|
|
||||||
entropyRight = entropy(cut, end);
|
|
||||||
iGain = entropyInterval -
|
|
||||||
(static_cast<precision_t>(nElementsLeft) * entropyLeft +
|
|
||||||
static_cast<precision_t>(nElementsRight) * entropyRight) /
|
|
||||||
static_cast<precision_t>(nElements);
|
|
||||||
igCache[make_tuple(start, cut, end)] = iGain;
|
|
||||||
return iGain;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,22 +0,0 @@
|
|||||||
#ifndef CCMETRICS_H
|
|
||||||
#define CCMETRICS_H
|
|
||||||
|
|
||||||
#include "typesFImdlp.h"
|
|
||||||
|
|
||||||
namespace mdlp {
|
|
||||||
class Metrics {
|
|
||||||
protected:
|
|
||||||
labels_t& y;
|
|
||||||
indices_t& indices;
|
|
||||||
int numClasses;
|
|
||||||
cacheEnt_t entropyCache = cacheEnt_t();
|
|
||||||
cacheIg_t igCache = cacheIg_t();
|
|
||||||
public:
|
|
||||||
Metrics(labels_t&, indices_t&);
|
|
||||||
void setData(const labels_t&, const indices_t&);
|
|
||||||
int computeNumClasses(size_t, size_t);
|
|
||||||
precision_t entropy(size_t, size_t);
|
|
||||||
precision_t informationGain(size_t, size_t, size_t);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
225
sample/main.cc
225
sample/main.cc
@@ -1,225 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
#include <torch/torch.h>
|
|
||||||
#include <thread>
|
|
||||||
#include <getopt.h>
|
|
||||||
#include "ArffFiles.h"
|
|
||||||
#include "Network.h"
|
|
||||||
#include "Metrics.hpp"
|
|
||||||
#include "CPPFImdlp.h"
|
|
||||||
#include "KDB.h"
|
|
||||||
#include "SPODE.h"
|
|
||||||
#include "AODE.h"
|
|
||||||
#include "TAN.h"
|
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
const string PATH = "data/";
|
|
||||||
|
|
||||||
/* print a description of all supported options */
|
|
||||||
void usage(const char* path)
|
|
||||||
{
|
|
||||||
/* take only the last portion of the path */
|
|
||||||
const char* basename = strrchr(path, '/');
|
|
||||||
basename = basename ? basename + 1 : path;
|
|
||||||
|
|
||||||
cout << "usage: " << basename << "[OPTION]" << endl;
|
|
||||||
cout << " -h, --help\t\t Print this help and exit." << endl;
|
|
||||||
cout
|
|
||||||
<< " -f, --file[=FILENAME]\t {diabetes, glass, iris, kdd_JapaneseVowels, letter, liver-disorders, mfeat-factors}."
|
|
||||||
<< endl;
|
|
||||||
cout << " -p, --path[=FILENAME]\t folder where the data files are located, default " << PATH << endl;
|
|
||||||
cout << " -m, --model={AODE, KDB, SPODE, TAN}\t " << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
tuple<string, string, string> parse_arguments(int argc, char** argv)
|
|
||||||
{
|
|
||||||
string file_name;
|
|
||||||
string model_name;
|
|
||||||
string path = PATH;
|
|
||||||
const vector<struct option> long_options = {
|
|
||||||
{"help", no_argument, nullptr, 'h'},
|
|
||||||
{"file", required_argument, nullptr, 'f'},
|
|
||||||
{"path", required_argument, nullptr, 'p'},
|
|
||||||
{"model", required_argument, nullptr, 'm'},
|
|
||||||
{nullptr, no_argument, nullptr, 0}
|
|
||||||
};
|
|
||||||
while (true) {
|
|
||||||
const auto c = getopt_long(argc, argv, "hf:p:m:", long_options.data(), nullptr);
|
|
||||||
if (c == -1)
|
|
||||||
break;
|
|
||||||
switch (c) {
|
|
||||||
case 'h':
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(0);
|
|
||||||
case 'f':
|
|
||||||
file_name = string(optarg);
|
|
||||||
break;
|
|
||||||
case 'm':
|
|
||||||
model_name = string(optarg);
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
path = optarg;
|
|
||||||
if (path.back() != '/')
|
|
||||||
path += '/';
|
|
||||||
break;
|
|
||||||
case '?':
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
default:
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (file_name.empty()) {
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
return make_tuple(file_name, path, model_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline constexpr auto hash_conv(const std::string_view sv)
|
|
||||||
{
|
|
||||||
unsigned long hash{ 5381 };
|
|
||||||
for (unsigned char c : sv) {
|
|
||||||
hash = ((hash << 5) + hash) ^ c;
|
|
||||||
}
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline constexpr auto operator"" _sh(const char* str, size_t len)
|
|
||||||
{
|
|
||||||
return hash_conv(std::string_view{ str, len });
|
|
||||||
}
|
|
||||||
|
|
||||||
pair<vector<mdlp::labels_t>, map<string, int>> discretize(vector<mdlp::samples_t>& X, mdlp::labels_t& y, vector<string> features)
|
|
||||||
{
|
|
||||||
vector<mdlp::labels_t>Xd;
|
|
||||||
map<string, int> maxes;
|
|
||||||
|
|
||||||
auto fimdlp = mdlp::CPPFImdlp();
|
|
||||||
for (int i = 0; i < X.size(); i++) {
|
|
||||||
fimdlp.fit(X[i], y);
|
|
||||||
mdlp::labels_t& xd = fimdlp.transform(X[i]);
|
|
||||||
maxes[features[i]] = *max_element(xd.begin(), xd.end()) + 1;
|
|
||||||
Xd.push_back(xd);
|
|
||||||
}
|
|
||||||
return { Xd, maxes };
|
|
||||||
}
|
|
||||||
|
|
||||||
bool file_exists(const std::string& name)
|
|
||||||
{
|
|
||||||
if (FILE* file = fopen(name.c_str(), "r")) {
|
|
||||||
fclose(file);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tuple<string, string, string> get_options(int argc, char** argv)
|
|
||||||
{
|
|
||||||
map<string, bool> datasets = {
|
|
||||||
{"diabetes", true},
|
|
||||||
{"ecoli", true},
|
|
||||||
{"glass", true},
|
|
||||||
{"iris", true},
|
|
||||||
{"kdd_JapaneseVowels", false},
|
|
||||||
{"letter", true},
|
|
||||||
{"liver-disorders", true},
|
|
||||||
{"mfeat-factors", true},
|
|
||||||
};
|
|
||||||
vector <string> models = { "AODE", "KDB", "SPODE", "TAN" };
|
|
||||||
string file_name;
|
|
||||||
string path;
|
|
||||||
string model_name;
|
|
||||||
tie(file_name, path, model_name) = parse_arguments(argc, argv);
|
|
||||||
if (datasets.find(file_name) == datasets.end()) {
|
|
||||||
cout << "Invalid file name: " << file_name << endl;
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (!file_exists(path + file_name + ".arff")) {
|
|
||||||
cout << "Data File " << path + file_name + ".arff" << " does not exist" << endl;
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (find(models.begin(), models.end(), model_name) == models.end()) {
|
|
||||||
cout << "Invalid model name: " << model_name << endl;
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
return { file_name, path, model_name };
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
|
||||||
{
|
|
||||||
string file_name, path, model_name;
|
|
||||||
tie(file_name, path, model_name) = get_options(argc, argv);
|
|
||||||
auto handler = ArffFiles();
|
|
||||||
handler.load(path + file_name + ".arff");
|
|
||||||
// Get Dataset X, y
|
|
||||||
vector<mdlp::samples_t>& X = handler.getX();
|
|
||||||
mdlp::labels_t& y = handler.getY();
|
|
||||||
// Get className & Features
|
|
||||||
auto className = handler.getClassName();
|
|
||||||
vector<string> features;
|
|
||||||
for (auto feature : handler.getAttributes()) {
|
|
||||||
features.push_back(feature.first);
|
|
||||||
}
|
|
||||||
// Discretize Dataset
|
|
||||||
vector<mdlp::labels_t> Xd;
|
|
||||||
map<string, int> maxes;
|
|
||||||
tie(Xd, maxes) = discretize(X, y, features);
|
|
||||||
maxes[className] = *max_element(y.begin(), y.end()) + 1;
|
|
||||||
map<string, vector<int>> states;
|
|
||||||
for (auto feature : features) {
|
|
||||||
states[feature] = vector<int>(maxes[feature]);
|
|
||||||
}
|
|
||||||
states[className] = vector<int>(
|
|
||||||
maxes[className]);
|
|
||||||
double score;
|
|
||||||
vector<string> lines;
|
|
||||||
vector<string> graph;
|
|
||||||
auto kdb = bayesnet::KDB(2);
|
|
||||||
auto aode = bayesnet::AODE();
|
|
||||||
auto spode = bayesnet::SPODE(2);
|
|
||||||
auto tan = bayesnet::TAN();
|
|
||||||
switch (hash_conv(model_name)) {
|
|
||||||
case "AODE"_sh:
|
|
||||||
aode.fit(Xd, y, features, className, states);
|
|
||||||
lines = aode.show();
|
|
||||||
score = aode.score(Xd, y);
|
|
||||||
graph = aode.graph();
|
|
||||||
break;
|
|
||||||
case "KDB"_sh:
|
|
||||||
kdb.fit(Xd, y, features, className, states);
|
|
||||||
lines = kdb.show();
|
|
||||||
score = kdb.score(Xd, y);
|
|
||||||
graph = kdb.graph();
|
|
||||||
break;
|
|
||||||
case "SPODE"_sh:
|
|
||||||
spode.fit(Xd, y, features, className, states);
|
|
||||||
lines = spode.show();
|
|
||||||
score = spode.score(Xd, y);
|
|
||||||
graph = spode.graph();
|
|
||||||
break;
|
|
||||||
case "TAN"_sh:
|
|
||||||
tan.fit(Xd, y, features, className, states);
|
|
||||||
lines = tan.show();
|
|
||||||
score = tan.score(Xd, y);
|
|
||||||
graph = tan.graph();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
for (auto line : lines) {
|
|
||||||
cout << line << endl;
|
|
||||||
}
|
|
||||||
cout << "Score: " << score << endl;
|
|
||||||
auto dot_file = model_name + "_" + file_name;
|
|
||||||
ofstream file(dot_file + ".dot");
|
|
||||||
file << graph;
|
|
||||||
file.close();
|
|
||||||
cout << "Graph saved in " << model_name << "_" << file_name << ".dot" << endl;
|
|
||||||
cout << "dot -Tpng -o " + dot_file + ".png " + dot_file + ".dot " << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
235
sample/sample.cc
Normal file
235
sample/sample.cc
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include "ArffFiles.h"
|
||||||
|
#include "BayesMetrics.h"
|
||||||
|
#include "CPPFImdlp.h"
|
||||||
|
#include "Folding.h"
|
||||||
|
#include "Models.h"
|
||||||
|
#include "modelRegister.h"
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
const std::string PATH = "../../data/";
|
||||||
|
|
||||||
|
pair<std::vector<mdlp::labels_t>, map<std::string, int>> discretize(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y, std::vector<std::string> features)
|
||||||
|
{
|
||||||
|
std::vector<mdlp::labels_t>Xd;
|
||||||
|
map<std::string, int> maxes;
|
||||||
|
|
||||||
|
auto fimdlp = mdlp::CPPFImdlp();
|
||||||
|
for (int i = 0; i < X.size(); i++) {
|
||||||
|
fimdlp.fit(X[i], y);
|
||||||
|
mdlp::labels_t& xd = fimdlp.transform(X[i]);
|
||||||
|
maxes[features[i]] = *max_element(xd.begin(), xd.end()) + 1;
|
||||||
|
Xd.push_back(xd);
|
||||||
|
}
|
||||||
|
return { Xd, maxes };
|
||||||
|
}
|
||||||
|
|
||||||
|
bool file_exists(const std::string& name)
|
||||||
|
{
|
||||||
|
if (FILE* file = fopen(name.c_str(), "r")) {
|
||||||
|
fclose(file);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pair<std::vector<std::vector<int>>, std::vector<int>> extract_indices(std::vector<int> indices, std::vector<std::vector<int>> X, std::vector<int> y)
|
||||||
|
{
|
||||||
|
std::vector<std::vector<int>> Xr; // nxm
|
||||||
|
std::vector<int> yr;
|
||||||
|
for (int col = 0; col < X.size(); ++col) {
|
||||||
|
Xr.push_back(std::vector<int>());
|
||||||
|
}
|
||||||
|
for (auto index : indices) {
|
||||||
|
for (int col = 0; col < X.size(); ++col) {
|
||||||
|
Xr[col].push_back(X[col][index]);
|
||||||
|
}
|
||||||
|
yr.push_back(y[index]);
|
||||||
|
}
|
||||||
|
return { Xr, yr };
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
map<std::string, bool> datasets = {
|
||||||
|
{"diabetes", true},
|
||||||
|
{"ecoli", true},
|
||||||
|
{"glass", true},
|
||||||
|
{"iris", true},
|
||||||
|
{"kdd_JapaneseVowels", false},
|
||||||
|
{"letter", true},
|
||||||
|
{"liver-disorders", true},
|
||||||
|
{"mfeat-factors", true},
|
||||||
|
};
|
||||||
|
auto valid_datasets = std::vector<std::string>();
|
||||||
|
transform(datasets.begin(), datasets.end(), back_inserter(valid_datasets),
|
||||||
|
[](const pair<std::string, bool>& pair) { return pair.first; });
|
||||||
|
argparse::ArgumentParser program("BayesNetSample");
|
||||||
|
program.add_argument("-d", "--dataset")
|
||||||
|
.help("Dataset file name")
|
||||||
|
.action([valid_datasets](const std::string& value) {
|
||||||
|
if (find(valid_datasets.begin(), valid_datasets.end(), value) != valid_datasets.end()) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
throw runtime_error("file must be one of {diabetes, ecoli, glass, iris, kdd_JapaneseVowels, letter, liver-disorders, mfeat-factors}");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
program.add_argument("-p", "--path")
|
||||||
|
.help(" folder where the data files are located, default")
|
||||||
|
.default_value(std::string{ PATH }
|
||||||
|
);
|
||||||
|
program.add_argument("-m", "--model")
|
||||||
|
.help("Model to use " + platform::Models::instance()->tostring())
|
||||||
|
.action([](const std::string& value) {
|
||||||
|
static const std::vector<std::string> choices = platform::Models::instance()->getNames();
|
||||||
|
if (find(choices.begin(), choices.end(), value) != choices.end()) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
throw runtime_error("Model must be one of " + platform::Models::instance()->tostring());
|
||||||
|
}
|
||||||
|
);
|
||||||
|
program.add_argument("--discretize").help("Discretize input dataset").default_value(false).implicit_value(true);
|
||||||
|
program.add_argument("--dumpcpt").help("Dump CPT Tables").default_value(false).implicit_value(true);
|
||||||
|
program.add_argument("--stratified").help("If Stratified KFold is to be done").default_value(false).implicit_value(true);
|
||||||
|
program.add_argument("--tensors").help("Use tensors to store samples").default_value(false).implicit_value(true);
|
||||||
|
program.add_argument("-f", "--folds").help("Number of folds").default_value(5).scan<'i', int>().action([](const std::string& value) {
|
||||||
|
try {
|
||||||
|
auto k = stoi(value);
|
||||||
|
if (k < 2) {
|
||||||
|
throw runtime_error("Number of folds must be greater than 1");
|
||||||
|
}
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
catch (const runtime_error& err) {
|
||||||
|
throw runtime_error(err.what());
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
throw runtime_error("Number of folds must be an integer");
|
||||||
|
}});
|
||||||
|
program.add_argument("-s", "--seed").help("Random seed").default_value(-1).scan<'i', int>();
|
||||||
|
bool class_last, stratified, tensors, dump_cpt;
|
||||||
|
std::string model_name, file_name, path, complete_file_name;
|
||||||
|
int nFolds, seed;
|
||||||
|
try {
|
||||||
|
program.parse_args(argc, argv);
|
||||||
|
file_name = program.get<std::string>("dataset");
|
||||||
|
path = program.get<std::string>("path");
|
||||||
|
model_name = program.get<std::string>("model");
|
||||||
|
complete_file_name = path + file_name + ".arff";
|
||||||
|
stratified = program.get<bool>("stratified");
|
||||||
|
tensors = program.get<bool>("tensors");
|
||||||
|
nFolds = program.get<int>("folds");
|
||||||
|
seed = program.get<int>("seed");
|
||||||
|
dump_cpt = program.get<bool>("dumpcpt");
|
||||||
|
class_last = datasets[file_name];
|
||||||
|
if (!file_exists(complete_file_name)) {
|
||||||
|
throw runtime_error("Data File " + path + file_name + ".arff" + " does not exist");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const exception& err) {
|
||||||
|
cerr << err.what() << std::endl;
|
||||||
|
cerr << program;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Begin Processing
|
||||||
|
*/
|
||||||
|
auto handler = ArffFiles();
|
||||||
|
handler.load(complete_file_name, class_last);
|
||||||
|
// Get Dataset X, y
|
||||||
|
std::vector<mdlp::samples_t>& X = handler.getX();
|
||||||
|
mdlp::labels_t& y = handler.getY();
|
||||||
|
// Get className & Features
|
||||||
|
auto className = handler.getClassName();
|
||||||
|
std::vector<std::string> features;
|
||||||
|
auto attributes = handler.getAttributes();
|
||||||
|
transform(attributes.begin(), attributes.end(), back_inserter(features),
|
||||||
|
[](const pair<std::string, std::string>& item) { return item.first; });
|
||||||
|
// Discretize Dataset
|
||||||
|
auto [Xd, maxes] = discretize(X, y, features);
|
||||||
|
maxes[className] = *max_element(y.begin(), y.end()) + 1;
|
||||||
|
map<std::string, std::vector<int>> states;
|
||||||
|
for (auto feature : features) {
|
||||||
|
states[feature] = std::vector<int>(maxes[feature]);
|
||||||
|
}
|
||||||
|
states[className] = std::vector<int>(maxes[className]);
|
||||||
|
auto clf = platform::Models::instance()->create(model_name);
|
||||||
|
clf->fit(Xd, y, features, className, states);
|
||||||
|
if (dump_cpt) {
|
||||||
|
std::cout << "--- CPT Tables ---" << std::endl;
|
||||||
|
clf->dump_cpt();
|
||||||
|
}
|
||||||
|
auto lines = clf->show();
|
||||||
|
for (auto line : lines) {
|
||||||
|
std::cout << line << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << "--- Topological Order ---" << std::endl;
|
||||||
|
auto order = clf->topological_order();
|
||||||
|
for (auto name : order) {
|
||||||
|
std::cout << name << ", ";
|
||||||
|
}
|
||||||
|
std::cout << "end." << std::endl;
|
||||||
|
auto score = clf->score(Xd, y);
|
||||||
|
std::cout << "Score: " << score << std::endl;
|
||||||
|
auto graph = clf->graph();
|
||||||
|
auto dot_file = model_name + "_" + file_name;
|
||||||
|
ofstream file(dot_file + ".dot");
|
||||||
|
file << graph;
|
||||||
|
file.close();
|
||||||
|
std::cout << "Graph saved in " << model_name << "_" << file_name << ".dot" << std::endl;
|
||||||
|
std::cout << "dot -Tpng -o " + dot_file + ".png " + dot_file + ".dot " << std::endl;
|
||||||
|
std::string stratified_string = stratified ? " Stratified" : "";
|
||||||
|
std::cout << nFolds << " Folds" << stratified_string << " Cross validation" << std::endl;
|
||||||
|
std::cout << "==========================================" << std::endl;
|
||||||
|
torch::Tensor Xt = torch::zeros({ static_cast<int>(Xd.size()), static_cast<int>(Xd[0].size()) }, torch::kInt32);
|
||||||
|
torch::Tensor yt = torch::tensor(y, torch::kInt32);
|
||||||
|
for (int i = 0; i < features.size(); ++i) {
|
||||||
|
Xt.index_put_({ i, "..." }, torch::tensor(Xd[i], torch::kInt32));
|
||||||
|
}
|
||||||
|
float total_score = 0, total_score_train = 0, score_train, score_test;
|
||||||
|
platform::Fold* fold;
|
||||||
|
if (stratified)
|
||||||
|
fold = new platform::StratifiedKFold(nFolds, y, seed);
|
||||||
|
else
|
||||||
|
fold = new platform::KFold(nFolds, y.size(), seed);
|
||||||
|
for (auto i = 0; i < nFolds; ++i) {
|
||||||
|
auto [train, test] = fold->getFold(i);
|
||||||
|
std::cout << "Fold: " << i + 1 << std::endl;
|
||||||
|
if (tensors) {
|
||||||
|
auto ttrain = torch::tensor(train, torch::kInt64);
|
||||||
|
auto ttest = torch::tensor(test, torch::kInt64);
|
||||||
|
torch::Tensor Xtraint = torch::index_select(Xt, 1, ttrain);
|
||||||
|
torch::Tensor ytraint = yt.index({ ttrain });
|
||||||
|
torch::Tensor Xtestt = torch::index_select(Xt, 1, ttest);
|
||||||
|
torch::Tensor ytestt = yt.index({ ttest });
|
||||||
|
clf->fit(Xtraint, ytraint, features, className, states);
|
||||||
|
auto temp = clf->predict(Xtraint);
|
||||||
|
score_train = clf->score(Xtraint, ytraint);
|
||||||
|
score_test = clf->score(Xtestt, ytestt);
|
||||||
|
} else {
|
||||||
|
auto [Xtrain, ytrain] = extract_indices(train, Xd, y);
|
||||||
|
auto [Xtest, ytest] = extract_indices(test, Xd, y);
|
||||||
|
clf->fit(Xtrain, ytrain, features, className, states);
|
||||||
|
score_train = clf->score(Xtrain, ytrain);
|
||||||
|
score_test = clf->score(Xtest, ytest);
|
||||||
|
}
|
||||||
|
if (dump_cpt) {
|
||||||
|
std::cout << "--- CPT Tables ---" << std::endl;
|
||||||
|
clf->dump_cpt();
|
||||||
|
}
|
||||||
|
total_score_train += score_train;
|
||||||
|
total_score += score_test;
|
||||||
|
std::cout << "Score Train: " << score_train << std::endl;
|
||||||
|
std::cout << "Score Test : " << score_test << std::endl;
|
||||||
|
std::cout << "-------------------------------------------------------------------------------" << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << "**********************************************************************************" << std::endl;
|
||||||
|
std::cout << "Average Score Train: " << total_score_train / nFolds << std::endl;
|
||||||
|
std::cout << "Average Score Test : " << total_score / nFolds << std::endl;return 0;
|
||||||
|
}
|
@@ -1,18 +0,0 @@
|
|||||||
#ifndef TYPES_H
|
|
||||||
#define TYPES_H
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <map>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
namespace mdlp {
|
|
||||||
typedef float precision_t;
|
|
||||||
typedef vector<precision_t> samples_t;
|
|
||||||
typedef vector<int> labels_t;
|
|
||||||
typedef vector<size_t> indices_t;
|
|
||||||
typedef vector<precision_t> cutPoints_t;
|
|
||||||
typedef map<pair<int, int>, precision_t> cacheEnt_t;
|
|
||||||
typedef map<tuple<int, int, int>, precision_t> cacheIg_t;
|
|
||||||
}
|
|
||||||
#endif
|
|
@@ -1,119 +0,0 @@
|
|||||||
#include "BaseClassifier.h"
|
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
namespace bayesnet {
|
|
||||||
using namespace std;
|
|
||||||
using namespace torch;
|
|
||||||
|
|
||||||
BaseClassifier::BaseClassifier(Network model) : model(model), m(0), n(0), metrics(Metrics()), fitted(false) {}
|
|
||||||
BaseClassifier& BaseClassifier::build(vector<string>& features, string className, map<string, vector<int>>& states)
|
|
||||||
{
|
|
||||||
|
|
||||||
dataset = torch::cat({ X, y.view({y.size(0), 1}) }, 1);
|
|
||||||
this->features = features;
|
|
||||||
this->className = className;
|
|
||||||
this->states = states;
|
|
||||||
checkFitParameters();
|
|
||||||
auto n_classes = states[className].size();
|
|
||||||
metrics = Metrics(dataset, features, className, n_classes);
|
|
||||||
train();
|
|
||||||
model.fit(Xv, yv, features, className);
|
|
||||||
fitted = true;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
BaseClassifier& BaseClassifier::fit(vector<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states)
|
|
||||||
{
|
|
||||||
this->X = torch::zeros({ static_cast<int64_t>(X[0].size()), static_cast<int64_t>(X.size()) }, kInt64);
|
|
||||||
Xv = X;
|
|
||||||
for (int i = 0; i < X.size(); ++i) {
|
|
||||||
this->X.index_put_({ "...", i }, torch::tensor(X[i], kInt64));
|
|
||||||
}
|
|
||||||
this->y = torch::tensor(y, kInt64);
|
|
||||||
yv = y;
|
|
||||||
return build(features, className, states);
|
|
||||||
}
|
|
||||||
void BaseClassifier::checkFitParameters()
|
|
||||||
{
|
|
||||||
auto sizes = X.sizes();
|
|
||||||
m = sizes[0];
|
|
||||||
n = sizes[1];
|
|
||||||
if (m != y.size(0)) {
|
|
||||||
throw invalid_argument("X and y must have the same number of samples");
|
|
||||||
}
|
|
||||||
if (n != features.size()) {
|
|
||||||
throw invalid_argument("X and features must have the same number of features");
|
|
||||||
}
|
|
||||||
if (states.find(className) == states.end()) {
|
|
||||||
throw invalid_argument("className not found in states");
|
|
||||||
}
|
|
||||||
for (auto feature : features) {
|
|
||||||
if (states.find(feature) == states.end()) {
|
|
||||||
throw invalid_argument("feature [" + feature + "] not found in states");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Tensor BaseClassifier::predict(Tensor& X)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("Classifier has not been fitted");
|
|
||||||
}
|
|
||||||
auto m_ = X.size(0);
|
|
||||||
auto n_ = X.size(1);
|
|
||||||
vector<vector<int>> Xd(n_, vector<int>(m_, 0));
|
|
||||||
for (auto i = 0; i < n_; i++) {
|
|
||||||
auto temp = X.index({ "...", i });
|
|
||||||
Xd[i] = vector<int>(temp.data_ptr<int>(), temp.data_ptr<int>() + m_);
|
|
||||||
}
|
|
||||||
auto yp = model.predict(Xd);
|
|
||||||
auto ypred = torch::tensor(yp, torch::kInt64);
|
|
||||||
return ypred;
|
|
||||||
}
|
|
||||||
vector<int> BaseClassifier::predict(vector<vector<int>>& X)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("Classifier has not been fitted");
|
|
||||||
}
|
|
||||||
auto m_ = X[0].size();
|
|
||||||
auto n_ = X.size();
|
|
||||||
vector<vector<int>> Xd(n_, vector<int>(m_, 0));
|
|
||||||
for (auto i = 0; i < n_; i++) {
|
|
||||||
Xd[i] = vector<int>(X[i].begin(), X[i].end());
|
|
||||||
}
|
|
||||||
auto yp = model.predict(Xd);
|
|
||||||
return yp;
|
|
||||||
}
|
|
||||||
float BaseClassifier::score(Tensor& X, Tensor& y)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("Classifier has not been fitted");
|
|
||||||
}
|
|
||||||
Tensor y_pred = predict(X);
|
|
||||||
return (y_pred == y).sum().item<float>() / y.size(0);
|
|
||||||
}
|
|
||||||
float BaseClassifier::score(vector<vector<int>>& X, vector<int>& y)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("Classifier has not been fitted");
|
|
||||||
}
|
|
||||||
auto m_ = X[0].size();
|
|
||||||
auto n_ = X.size();
|
|
||||||
vector<vector<int>> Xd(n_, vector<int>(m_, 0));
|
|
||||||
for (auto i = 0; i < n_; i++) {
|
|
||||||
Xd[i] = vector<int>(X[i].begin(), X[i].end());
|
|
||||||
}
|
|
||||||
return model.score(Xd, y);
|
|
||||||
}
|
|
||||||
vector<string> BaseClassifier::show()
|
|
||||||
{
|
|
||||||
return model.show();
|
|
||||||
}
|
|
||||||
void BaseClassifier::addNodes()
|
|
||||||
{
|
|
||||||
// Add all nodes to the network
|
|
||||||
for (auto feature : features) {
|
|
||||||
model.addNode(feature, states[feature].size());
|
|
||||||
}
|
|
||||||
model.addNode(className, states[className].size());
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,46 +0,0 @@
|
|||||||
#ifndef CLASSIFIERS_H
|
|
||||||
#define CLASSIFIERS_H
|
|
||||||
#include <torch/torch.h>
|
|
||||||
#include "Network.h"
|
|
||||||
#include "Metrics.hpp"
|
|
||||||
using namespace std;
|
|
||||||
using namespace torch;
|
|
||||||
|
|
||||||
namespace bayesnet {
|
|
||||||
class BaseClassifier {
|
|
||||||
private:
|
|
||||||
bool fitted;
|
|
||||||
BaseClassifier& build(vector<string>& features, string className, map<string, vector<int>>& states);
|
|
||||||
protected:
|
|
||||||
Network model;
|
|
||||||
int m, n; // m: number of samples, n: number of features
|
|
||||||
Tensor X;
|
|
||||||
vector<vector<int>> Xv;
|
|
||||||
Tensor y;
|
|
||||||
vector<int> yv;
|
|
||||||
Tensor dataset;
|
|
||||||
Metrics metrics;
|
|
||||||
vector<string> features;
|
|
||||||
string className;
|
|
||||||
map<string, vector<int>> states;
|
|
||||||
void checkFitParameters();
|
|
||||||
virtual void train() = 0;
|
|
||||||
public:
|
|
||||||
BaseClassifier(Network model);
|
|
||||||
virtual ~BaseClassifier() = default;
|
|
||||||
BaseClassifier& fit(vector<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states);
|
|
||||||
void addNodes();
|
|
||||||
Tensor predict(Tensor& X);
|
|
||||||
vector<int> predict(vector<vector<int>>& X);
|
|
||||||
float score(Tensor& X, Tensor& y);
|
|
||||||
float score(vector<vector<int>>& X, vector<int>& y);
|
|
||||||
vector<string> show();
|
|
||||||
virtual vector<string> graph(string title) = 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@@ -2,14 +2,16 @@
|
|||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
AODE::AODE() : Ensemble() {}
|
AODE::AODE() : Ensemble() {}
|
||||||
void AODE::train()
|
void AODE::buildModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
models.clear();
|
models.clear();
|
||||||
for (int i = 0; i < features.size(); ++i) {
|
for (int i = 0; i < features.size(); ++i) {
|
||||||
models.push_back(std::make_unique<SPODE>(i));
|
models.push_back(std::make_unique<SPODE>(i));
|
||||||
}
|
}
|
||||||
|
n_models = models.size();
|
||||||
|
significanceModels = std::vector<double>(n_models, 1.0);
|
||||||
}
|
}
|
||||||
vector<string> AODE::graph(string title)
|
std::vector<std::string> AODE::graph(const std::string& title) const
|
||||||
{
|
{
|
||||||
return Ensemble::graph(title);
|
return Ensemble::graph(title);
|
||||||
}
|
}
|
@@ -5,10 +5,11 @@
|
|||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
class AODE : public Ensemble {
|
class AODE : public Ensemble {
|
||||||
protected:
|
protected:
|
||||||
void train() override;
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
public:
|
public:
|
||||||
AODE();
|
AODE();
|
||||||
vector<string> graph(string title = "AODE");
|
virtual ~AODE() {};
|
||||||
|
std::vector<std::string> graph(const std::string& title = "AODE") const override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
39
src/BayesNet/AODELd.cc
Normal file
39
src/BayesNet/AODELd.cc
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include "AODELd.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
AODELd::AODELd() : Ensemble(), Proposal(dataset, features, className) {}
|
||||||
|
AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
|
||||||
|
{
|
||||||
|
checkInput(X_, y_);
|
||||||
|
features = features_;
|
||||||
|
className = className_;
|
||||||
|
Xf = X_;
|
||||||
|
y = y_;
|
||||||
|
// Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
|
||||||
|
states = fit_local_discretization(y);
|
||||||
|
// We have discretized the input data
|
||||||
|
// 1st we need to fit the model to build the normal TAN structure, TAN::fit initializes the base Bayesian network
|
||||||
|
Ensemble::fit(dataset, features, className, states);
|
||||||
|
return *this;
|
||||||
|
|
||||||
|
}
|
||||||
|
void AODELd::buildModel(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
models.clear();
|
||||||
|
for (int i = 0; i < features.size(); ++i) {
|
||||||
|
models.push_back(std::make_unique<SPODELd>(i));
|
||||||
|
}
|
||||||
|
n_models = models.size();
|
||||||
|
significanceModels = std::vector<double>(n_models, 1.0);
|
||||||
|
}
|
||||||
|
void AODELd::trainModel(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
for (const auto& model : models) {
|
||||||
|
model->fit(Xf, y, features, className, states);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::vector<std::string> AODELd::graph(const std::string& name) const
|
||||||
|
{
|
||||||
|
return Ensemble::graph(name);
|
||||||
|
}
|
||||||
|
}
|
20
src/BayesNet/AODELd.h
Normal file
20
src/BayesNet/AODELd.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef AODELD_H
|
||||||
|
#define AODELD_H
|
||||||
|
#include "Ensemble.h"
|
||||||
|
#include "Proposal.h"
|
||||||
|
#include "SPODELd.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class AODELd : public Ensemble, public Proposal {
|
||||||
|
protected:
|
||||||
|
void trainModel(const torch::Tensor& weights) override;
|
||||||
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
|
public:
|
||||||
|
AODELd();
|
||||||
|
AODELd& fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_) override;
|
||||||
|
virtual ~AODELd() = default;
|
||||||
|
std::vector<std::string> graph(const std::string& name = "AODELd") const override;
|
||||||
|
static inline std::string version() { return "0.0.1"; };
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif // !AODELD_H
|
37
src/BayesNet/BaseClassifier.h
Normal file
37
src/BayesNet/BaseClassifier.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef BASE_H
|
||||||
|
#define BASE_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <vector>
|
||||||
|
namespace bayesnet {
|
||||||
|
enum status_t { NORMAL, WARNING, ERROR };
|
||||||
|
class BaseClassifier {
|
||||||
|
public:
|
||||||
|
// X is nxm std::vector, y is nx1 std::vector
|
||||||
|
virtual BaseClassifier& fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) = 0;
|
||||||
|
// X is nxm tensor, y is nx1 tensor
|
||||||
|
virtual BaseClassifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) = 0;
|
||||||
|
virtual BaseClassifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) = 0;
|
||||||
|
virtual BaseClassifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights) = 0;
|
||||||
|
virtual ~BaseClassifier() = default;
|
||||||
|
torch::Tensor virtual predict(torch::Tensor& X) = 0;
|
||||||
|
std::vector<int> virtual predict(std::vector<std::vector<int >>& X) = 0;
|
||||||
|
status_t virtual getStatus() const = 0;
|
||||||
|
float virtual score(std::vector<std::vector<int>>& X, std::vector<int>& y) = 0;
|
||||||
|
float virtual score(torch::Tensor& X, torch::Tensor& y) = 0;
|
||||||
|
int virtual getNumberOfNodes()const = 0;
|
||||||
|
int virtual getNumberOfEdges()const = 0;
|
||||||
|
int virtual getNumberOfStates() const = 0;
|
||||||
|
std::vector<std::string> virtual show() const = 0;
|
||||||
|
std::vector<std::string> virtual graph(const std::string& title = "") const = 0;
|
||||||
|
virtual std::string getVersion() = 0;
|
||||||
|
std::vector<std::string> virtual topological_order() = 0;
|
||||||
|
void virtual dump_cpt()const = 0;
|
||||||
|
virtual void setHyperparameters(const nlohmann::json& hyperparameters) = 0;
|
||||||
|
std::vector<std::string>& getValidHyperparameters() { return validHyperparameters; }
|
||||||
|
protected:
|
||||||
|
virtual void trainModel(const torch::Tensor& weights) = 0;
|
||||||
|
std::vector<std::string> validHyperparameters;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
163
src/BayesNet/BayesMetrics.cc
Normal file
163
src/BayesNet/BayesMetrics.cc
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
#include "BayesMetrics.h"
|
||||||
|
#include "Mst.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
//samples is n+1xm tensor used to fit the model
|
||||||
|
Metrics::Metrics(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int classNumStates)
|
||||||
|
: samples(samples)
|
||||||
|
, features(features)
|
||||||
|
, className(className)
|
||||||
|
, classNumStates(classNumStates)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
//samples is nxm std::vector used to fit the model
|
||||||
|
Metrics::Metrics(const std::vector<std::vector<int>>& vsamples, const std::vector<int>& labels, const std::vector<std::string>& features, const std::string& className, const int classNumStates)
|
||||||
|
: features(features)
|
||||||
|
, className(className)
|
||||||
|
, classNumStates(classNumStates)
|
||||||
|
, samples(torch::zeros({ static_cast<int>(vsamples[0].size()), static_cast<int>(vsamples.size() + 1) }, torch::kInt32))
|
||||||
|
{
|
||||||
|
for (int i = 0; i < vsamples.size(); ++i) {
|
||||||
|
samples.index_put_({ i, "..." }, torch::tensor(vsamples[i], torch::kInt32));
|
||||||
|
}
|
||||||
|
samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
|
||||||
|
}
|
||||||
|
std::vector<int> Metrics::SelectKBestWeighted(const torch::Tensor& weights, bool ascending, unsigned k)
|
||||||
|
{
|
||||||
|
// Return the K Best features
|
||||||
|
auto n = samples.size(0) - 1;
|
||||||
|
if (k == 0) {
|
||||||
|
k = n;
|
||||||
|
}
|
||||||
|
// compute scores
|
||||||
|
scoresKBest.clear();
|
||||||
|
featuresKBest.clear();
|
||||||
|
auto label = samples.index({ -1, "..." });
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
scoresKBest.push_back(mutualInformation(label, samples.index({ i, "..." }), weights));
|
||||||
|
featuresKBest.push_back(i);
|
||||||
|
}
|
||||||
|
// sort & reduce scores and features
|
||||||
|
if (ascending) {
|
||||||
|
sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
|
||||||
|
{ return scoresKBest[i] < scoresKBest[j]; });
|
||||||
|
sort(scoresKBest.begin(), scoresKBest.end(), std::less<double>());
|
||||||
|
if (k < n) {
|
||||||
|
for (int i = 0; i < n - k; ++i) {
|
||||||
|
featuresKBest.erase(featuresKBest.begin());
|
||||||
|
scoresKBest.erase(scoresKBest.begin());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
|
||||||
|
{ return scoresKBest[i] > scoresKBest[j]; });
|
||||||
|
sort(scoresKBest.begin(), scoresKBest.end(), std::greater<double>());
|
||||||
|
featuresKBest.resize(k);
|
||||||
|
scoresKBest.resize(k);
|
||||||
|
}
|
||||||
|
return featuresKBest;
|
||||||
|
}
|
||||||
|
std::vector<double> Metrics::getScoresKBest() const
|
||||||
|
{
|
||||||
|
return scoresKBest;
|
||||||
|
}
|
||||||
|
|
||||||
|
torch::Tensor Metrics::conditionalEdge(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
auto result = std::vector<double>();
|
||||||
|
auto source = std::vector<std::string>(features);
|
||||||
|
source.push_back(className);
|
||||||
|
auto combinations = doCombinations(source);
|
||||||
|
// Compute class prior
|
||||||
|
auto margin = torch::zeros({ classNumStates }, torch::kFloat);
|
||||||
|
for (int value = 0; value < classNumStates; ++value) {
|
||||||
|
auto mask = samples.index({ -1, "..." }) == value;
|
||||||
|
margin[value] = mask.sum().item<double>() / samples.size(1);
|
||||||
|
}
|
||||||
|
for (auto [first, second] : combinations) {
|
||||||
|
int index_first = find(features.begin(), features.end(), first) - features.begin();
|
||||||
|
int index_second = find(features.begin(), features.end(), second) - features.begin();
|
||||||
|
double accumulated = 0;
|
||||||
|
for (int value = 0; value < classNumStates; ++value) {
|
||||||
|
auto mask = samples.index({ -1, "..." }) == value;
|
||||||
|
auto first_dataset = samples.index({ index_first, mask });
|
||||||
|
auto second_dataset = samples.index({ index_second, mask });
|
||||||
|
auto weights_dataset = weights.index({ mask });
|
||||||
|
auto mi = mutualInformation(first_dataset, second_dataset, weights_dataset);
|
||||||
|
auto pb = margin[value].item<double>();
|
||||||
|
accumulated += pb * mi;
|
||||||
|
}
|
||||||
|
result.push_back(accumulated);
|
||||||
|
}
|
||||||
|
long n_vars = source.size();
|
||||||
|
auto matrix = torch::zeros({ n_vars, n_vars });
|
||||||
|
auto indices = torch::triu_indices(n_vars, n_vars, 1);
|
||||||
|
for (auto i = 0; i < result.size(); ++i) {
|
||||||
|
auto x = indices[0][i];
|
||||||
|
auto y = indices[1][i];
|
||||||
|
matrix[x][y] = result[i];
|
||||||
|
matrix[y][x] = result[i];
|
||||||
|
}
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
// To use in Python
|
||||||
|
std::vector<float> Metrics::conditionalEdgeWeights(std::vector<float>& weights_)
|
||||||
|
{
|
||||||
|
const torch::Tensor weights = torch::tensor(weights_);
|
||||||
|
auto matrix = conditionalEdge(weights);
|
||||||
|
std::vector<float> v(matrix.data_ptr<float>(), matrix.data_ptr<float>() + matrix.numel());
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
double Metrics::entropy(const torch::Tensor& feature, const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
torch::Tensor counts = feature.bincount(weights);
|
||||||
|
double totalWeight = counts.sum().item<double>();
|
||||||
|
torch::Tensor probs = counts.to(torch::kFloat) / totalWeight;
|
||||||
|
torch::Tensor logProbs = torch::log(probs);
|
||||||
|
torch::Tensor entropy = -probs * logProbs;
|
||||||
|
return entropy.nansum().item<double>();
|
||||||
|
}
|
||||||
|
// H(Y|X) = sum_{x in X} p(x) H(Y|X=x)
|
||||||
|
double Metrics::conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
int numSamples = firstFeature.sizes()[0];
|
||||||
|
torch::Tensor featureCounts = secondFeature.bincount(weights);
|
||||||
|
std::unordered_map<int, std::unordered_map<int, double>> jointCounts;
|
||||||
|
double totalWeight = 0;
|
||||||
|
for (auto i = 0; i < numSamples; i++) {
|
||||||
|
jointCounts[secondFeature[i].item<int>()][firstFeature[i].item<int>()] += weights[i].item<double>();
|
||||||
|
totalWeight += weights[i].item<float>();
|
||||||
|
}
|
||||||
|
if (totalWeight == 0)
|
||||||
|
return 0;
|
||||||
|
double entropyValue = 0;
|
||||||
|
for (int value = 0; value < featureCounts.sizes()[0]; ++value) {
|
||||||
|
double p_f = featureCounts[value].item<double>() / totalWeight;
|
||||||
|
double entropy_f = 0;
|
||||||
|
for (auto& [label, jointCount] : jointCounts[value]) {
|
||||||
|
double p_l_f = jointCount / featureCounts[value].item<double>();
|
||||||
|
if (p_l_f > 0) {
|
||||||
|
entropy_f -= p_l_f * log(p_l_f);
|
||||||
|
} else {
|
||||||
|
entropy_f = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entropyValue += p_f * entropy_f;
|
||||||
|
}
|
||||||
|
return entropyValue;
|
||||||
|
}
|
||||||
|
// I(X;Y) = H(Y) - H(Y|X)
|
||||||
|
double Metrics::mutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
return entropy(firstFeature, weights) - conditionalEntropy(firstFeature, secondFeature, weights);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Compute the maximum spanning tree considering the weights as distances
|
||||||
|
and the indices of the weights as nodes of this square matrix using
|
||||||
|
Kruskal algorithm
|
||||||
|
*/
|
||||||
|
std::vector<std::pair<int, int>> Metrics::maximumSpanningTree(const std::vector<std::string>& features, const torch::Tensor& weights, const int root)
|
||||||
|
{
|
||||||
|
auto mst = MST(features, weights, root);
|
||||||
|
return mst.maximumSpanningTree();
|
||||||
|
}
|
||||||
|
}
|
49
src/BayesNet/BayesMetrics.h
Normal file
49
src/BayesNet/BayesMetrics.h
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#ifndef BAYESNET_METRICS_H
|
||||||
|
#define BAYESNET_METRICS_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
namespace bayesnet {
|
||||||
|
class Metrics {
|
||||||
|
private:
|
||||||
|
int classNumStates = 0;
|
||||||
|
std::vector<double> scoresKBest;
|
||||||
|
std::vector<int> featuresKBest; // sorted indices of the features
|
||||||
|
double conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
|
||||||
|
protected:
|
||||||
|
torch::Tensor samples; // n+1xm torch::Tensor used to fit the model where samples[-1] is the y std::vector
|
||||||
|
std::string className;
|
||||||
|
double entropy(const torch::Tensor& feature, const torch::Tensor& weights);
|
||||||
|
std::vector<std::string> features;
|
||||||
|
template <class T>
|
||||||
|
std::vector<std::pair<T, T>> doCombinations(const std::vector<T>& source)
|
||||||
|
{
|
||||||
|
std::vector<std::pair<T, T>> result;
|
||||||
|
for (int i = 0; i < source.size(); ++i) {
|
||||||
|
T temp = source[i];
|
||||||
|
for (int j = i + 1; j < source.size(); ++j) {
|
||||||
|
result.push_back({ temp, source[j] });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
template <class T>
|
||||||
|
T pop_first(std::vector<T>& v)
|
||||||
|
{
|
||||||
|
T temp = v[0];
|
||||||
|
v.erase(v.begin());
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
Metrics() = default;
|
||||||
|
Metrics(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int classNumStates);
|
||||||
|
Metrics(const std::vector<std::vector<int>>& vsamples, const std::vector<int>& labels, const std::vector<std::string>& features, const std::string& className, const int classNumStates);
|
||||||
|
std::vector<int> SelectKBestWeighted(const torch::Tensor& weights, bool ascending = false, unsigned k = 0);
|
||||||
|
std::vector<double> getScoresKBest() const;
|
||||||
|
double mutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
|
||||||
|
std::vector<float> conditionalEdgeWeights(std::vector<float>& weights); // To use in Python
|
||||||
|
torch::Tensor conditionalEdge(const torch::Tensor& weights);
|
||||||
|
std::vector<std::pair<int, int>> maximumSpanningTree(const std::vector<std::string>& features, const torch::Tensor& weights, const int root);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
209
src/BayesNet/BoostAODE.cc
Normal file
209
src/BayesNet/BoostAODE.cc
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
#include <set>
|
||||||
|
#include <functional>
|
||||||
|
#include <limits.h>
|
||||||
|
#include "BoostAODE.h"
|
||||||
|
#include "Colors.h"
|
||||||
|
#include "Folding.h"
|
||||||
|
#include "Paths.h"
|
||||||
|
#include "CFS.h"
|
||||||
|
#include "FCBF.h"
|
||||||
|
#include "IWSS.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
BoostAODE::BoostAODE() : Ensemble()
|
||||||
|
{
|
||||||
|
validHyperparameters = { "repeatSparent", "maxModels", "ascending", "convergence", "threshold", "select_features", "tolerance" };
|
||||||
|
|
||||||
|
}
|
||||||
|
void BoostAODE::buildModel(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
// Models shall be built in trainModel
|
||||||
|
models.clear();
|
||||||
|
n_models = 0;
|
||||||
|
// Prepare the validation dataset
|
||||||
|
auto y_ = dataset.index({ -1, "..." });
|
||||||
|
if (convergence) {
|
||||||
|
// Prepare train & validation sets from train data
|
||||||
|
auto fold = platform::StratifiedKFold(5, y_, 271);
|
||||||
|
dataset_ = torch::clone(dataset);
|
||||||
|
// save input dataset
|
||||||
|
auto [train, test] = fold.getFold(0);
|
||||||
|
auto train_t = torch::tensor(train);
|
||||||
|
auto test_t = torch::tensor(test);
|
||||||
|
// Get train and validation sets
|
||||||
|
X_train = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), train_t });
|
||||||
|
y_train = dataset.index({ -1, train_t });
|
||||||
|
X_test = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), test_t });
|
||||||
|
y_test = dataset.index({ -1, test_t });
|
||||||
|
dataset = X_train;
|
||||||
|
m = X_train.size(1);
|
||||||
|
auto n_classes = states.at(className).size();
|
||||||
|
metrics = Metrics(dataset, features, className, n_classes);
|
||||||
|
// Build dataset with train data
|
||||||
|
buildDataset(y_train);
|
||||||
|
} else {
|
||||||
|
// Use all data to train
|
||||||
|
X_train = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." });
|
||||||
|
y_train = y_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void BoostAODE::setHyperparameters(const nlohmann::json& hyperparameters_)
|
||||||
|
{
|
||||||
|
auto hyperparameters = hyperparameters_;
|
||||||
|
if (hyperparameters.contains("repeatSparent")) {
|
||||||
|
repeatSparent = hyperparameters["repeatSparent"];
|
||||||
|
hyperparameters.erase("repeatSparent");
|
||||||
|
}
|
||||||
|
if (hyperparameters.contains("maxModels")) {
|
||||||
|
maxModels = hyperparameters["maxModels"];
|
||||||
|
hyperparameters.erase("maxModels");
|
||||||
|
}
|
||||||
|
if (hyperparameters.contains("ascending")) {
|
||||||
|
ascending = hyperparameters["ascending"];
|
||||||
|
hyperparameters.erase("ascending");
|
||||||
|
}
|
||||||
|
if (hyperparameters.contains("convergence")) {
|
||||||
|
convergence = hyperparameters["convergence"];
|
||||||
|
hyperparameters.erase("convergence");
|
||||||
|
}
|
||||||
|
if (hyperparameters.contains("threshold")) {
|
||||||
|
threshold = hyperparameters["threshold"];
|
||||||
|
hyperparameters.erase("threshold");
|
||||||
|
}
|
||||||
|
if (hyperparameters.contains("tolerance")) {
|
||||||
|
tolerance = hyperparameters["tolerance"];
|
||||||
|
hyperparameters.erase("tolerance");
|
||||||
|
}
|
||||||
|
if (hyperparameters.contains("select_features")) {
|
||||||
|
auto selectedAlgorithm = hyperparameters["select_features"];
|
||||||
|
std::vector<std::string> algos = { "IWSS", "FCBF", "CFS" };
|
||||||
|
selectFeatures = true;
|
||||||
|
algorithm = selectedAlgorithm;
|
||||||
|
if (std::find(algos.begin(), algos.end(), selectedAlgorithm) == algos.end()) {
|
||||||
|
throw std::invalid_argument("Invalid selectFeatures value [IWSS, FCBF, CFS]");
|
||||||
|
}
|
||||||
|
hyperparameters.erase("select_features");
|
||||||
|
}
|
||||||
|
if (!hyperparameters.empty()) {
|
||||||
|
throw std::invalid_argument("Invalid hyperparameters" + hyperparameters.dump());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::unordered_set<int> BoostAODE::initializeModels()
|
||||||
|
{
|
||||||
|
std::unordered_set<int> featuresUsed;
|
||||||
|
torch::Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
|
||||||
|
int maxFeatures = 0;
|
||||||
|
if (algorithm == "CFS") {
|
||||||
|
featureSelector = new CFS(dataset, features, className, maxFeatures, states.at(className).size(), weights_);
|
||||||
|
} else if (algorithm == "IWSS") {
|
||||||
|
if (threshold < 0 || threshold >0.5) {
|
||||||
|
throw std::invalid_argument("Invalid threshold value for IWSS [0, 0.5]");
|
||||||
|
}
|
||||||
|
featureSelector = new IWSS(dataset, features, className, maxFeatures, states.at(className).size(), weights_, threshold);
|
||||||
|
} else if (algorithm == "FCBF") {
|
||||||
|
if (threshold < 1e-7 || threshold > 1) {
|
||||||
|
throw std::invalid_argument("Invalid threshold value [1e-7, 1]");
|
||||||
|
}
|
||||||
|
featureSelector = new FCBF(dataset, features, className, maxFeatures, states.at(className).size(), weights_, threshold);
|
||||||
|
}
|
||||||
|
featureSelector->fit();
|
||||||
|
auto cfsFeatures = featureSelector->getFeatures();
|
||||||
|
for (const int& feature : cfsFeatures) {
|
||||||
|
// std::cout << "Feature: [" << feature << "] " << feature << " " << features.at(feature) << std::endl;
|
||||||
|
featuresUsed.insert(feature);
|
||||||
|
std::unique_ptr<Classifier> model = std::make_unique<SPODE>(feature);
|
||||||
|
model->fit(dataset, features, className, states, weights_);
|
||||||
|
models.push_back(std::move(model));
|
||||||
|
significanceModels.push_back(1.0);
|
||||||
|
n_models++;
|
||||||
|
}
|
||||||
|
delete featureSelector;
|
||||||
|
return featuresUsed;
|
||||||
|
}
|
||||||
|
void BoostAODE::trainModel(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
std::unordered_set<int> featuresUsed;
|
||||||
|
if (selectFeatures) {
|
||||||
|
featuresUsed = initializeModels();
|
||||||
|
}
|
||||||
|
if (maxModels == 0)
|
||||||
|
maxModels = .1 * n > 10 ? .1 * n : n;
|
||||||
|
torch::Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
|
||||||
|
bool exitCondition = false;
|
||||||
|
// Variables to control the accuracy finish condition
|
||||||
|
double priorAccuracy = 0.0;
|
||||||
|
double delta = 1.0;
|
||||||
|
double threshold = 1e-4;
|
||||||
|
int count = 0; // number of times the accuracy is lower than the threshold
|
||||||
|
fitted = true; // to enable predict
|
||||||
|
// Step 0: Set the finish condition
|
||||||
|
// if not repeatSparent a finish condition is run out of features
|
||||||
|
// n_models == maxModels
|
||||||
|
// epsilon sub t > 0.5 => inverse the weights policy
|
||||||
|
// validation error is not decreasing
|
||||||
|
while (!exitCondition) {
|
||||||
|
// Step 1: Build ranking with mutual information
|
||||||
|
auto featureSelection = metrics.SelectKBestWeighted(weights_, ascending, n); // Get all the features sorted
|
||||||
|
std::unique_ptr<Classifier> model;
|
||||||
|
auto feature = featureSelection[0];
|
||||||
|
if (!repeatSparent || featuresUsed.size() < featureSelection.size()) {
|
||||||
|
bool used = true;
|
||||||
|
for (const auto& feat : featureSelection) {
|
||||||
|
if (std::find(featuresUsed.begin(), featuresUsed.end(), feat) != featuresUsed.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
used = false;
|
||||||
|
feature = feat;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (used) {
|
||||||
|
exitCondition = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
featuresUsed.insert(feature);
|
||||||
|
model = std::make_unique<SPODE>(feature);
|
||||||
|
model->fit(dataset, features, className, states, weights_);
|
||||||
|
auto ypred = model->predict(X_train);
|
||||||
|
// Step 3.1: Compute the classifier amout of say
|
||||||
|
auto mask_wrong = ypred != y_train;
|
||||||
|
auto mask_right = ypred == y_train;
|
||||||
|
auto masked_weights = weights_ * mask_wrong.to(weights_.dtype());
|
||||||
|
double epsilon_t = masked_weights.sum().item<double>();
|
||||||
|
double wt = (1 - epsilon_t) / epsilon_t;
|
||||||
|
double alpha_t = epsilon_t == 0 ? 1 : 0.5 * log(wt);
|
||||||
|
// Step 3.2: Update weights for next classifier
|
||||||
|
// Step 3.2.1: Update weights of wrong samples
|
||||||
|
weights_ += mask_wrong.to(weights_.dtype()) * exp(alpha_t) * weights_;
|
||||||
|
// Step 3.2.2: Update weights of right samples
|
||||||
|
weights_ += mask_right.to(weights_.dtype()) * exp(-alpha_t) * weights_;
|
||||||
|
// Step 3.3: Normalise the weights
|
||||||
|
double totalWeights = torch::sum(weights_).item<double>();
|
||||||
|
weights_ = weights_ / totalWeights;
|
||||||
|
// Step 3.4: Store classifier and its accuracy to weigh its future vote
|
||||||
|
models.push_back(std::move(model));
|
||||||
|
significanceModels.push_back(alpha_t);
|
||||||
|
n_models++;
|
||||||
|
if (convergence) {
|
||||||
|
auto y_val_predict = predict(X_test);
|
||||||
|
double accuracy = (y_val_predict == y_test).sum().item<double>() / (double)y_test.size(0);
|
||||||
|
if (priorAccuracy == 0) {
|
||||||
|
priorAccuracy = accuracy;
|
||||||
|
} else {
|
||||||
|
delta = accuracy - priorAccuracy;
|
||||||
|
}
|
||||||
|
if (delta < threshold) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exitCondition = n_models >= maxModels && repeatSparent || epsilon_t > 0.5 || count > tolerance;
|
||||||
|
}
|
||||||
|
if (featuresUsed.size() != features.size()) {
|
||||||
|
status = WARNING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::vector<std::string> BoostAODE::graph(const std::string& title) const
|
||||||
|
{
|
||||||
|
return Ensemble::graph(title);
|
||||||
|
}
|
||||||
|
}
|
33
src/BayesNet/BoostAODE.h
Normal file
33
src/BayesNet/BoostAODE.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef BOOSTAODE_H
|
||||||
|
#define BOOSTAODE_H
|
||||||
|
#include "Ensemble.h"
|
||||||
|
#include <map>
|
||||||
|
#include "SPODE.h"
|
||||||
|
#include "FeatureSelect.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
class BoostAODE : public Ensemble {
|
||||||
|
public:
|
||||||
|
BoostAODE();
|
||||||
|
virtual ~BoostAODE() = default;
|
||||||
|
std::vector<std::string> graph(const std::string& title = "BoostAODE") const override;
|
||||||
|
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||||
|
protected:
|
||||||
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
|
void trainModel(const torch::Tensor& weights) override;
|
||||||
|
private:
|
||||||
|
torch::Tensor dataset_;
|
||||||
|
torch::Tensor X_train, y_train, X_test, y_test;
|
||||||
|
std::unordered_set<int> initializeModels();
|
||||||
|
// Hyperparameters
|
||||||
|
bool repeatSparent = false; // if true, a feature can be selected more than once
|
||||||
|
int maxModels = 0;
|
||||||
|
int tolerance = 0;
|
||||||
|
bool ascending = false; //Process KBest features ascending or descending order
|
||||||
|
bool convergence = false; //if true, stop when the model does not improve
|
||||||
|
bool selectFeatures = false; // if true, use feature selection
|
||||||
|
std::string algorithm = ""; // Selected feature selection algorithm
|
||||||
|
FeatureSelect* featureSelector = nullptr;
|
||||||
|
double threshold = -1;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
72
src/BayesNet/CFS.cc
Normal file
72
src/BayesNet/CFS.cc
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#include "CFS.h"
|
||||||
|
#include <limits>
|
||||||
|
#include "bayesnetUtils.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
void CFS::fit()
|
||||||
|
{
|
||||||
|
initialize();
|
||||||
|
computeSuLabels();
|
||||||
|
auto featureOrder = argsort(suLabels); // sort descending order
|
||||||
|
auto continueCondition = true;
|
||||||
|
auto feature = featureOrder[0];
|
||||||
|
selectedFeatures.push_back(feature);
|
||||||
|
selectedScores.push_back(suLabels[feature]);
|
||||||
|
selectedFeatures.erase(selectedFeatures.begin());
|
||||||
|
while (continueCondition) {
|
||||||
|
double merit = std::numeric_limits<double>::lowest();
|
||||||
|
int bestFeature = -1;
|
||||||
|
for (auto feature : featureOrder) {
|
||||||
|
selectedFeatures.push_back(feature);
|
||||||
|
// Compute merit with selectedFeatures
|
||||||
|
auto meritNew = computeMeritCFS();
|
||||||
|
if (meritNew > merit) {
|
||||||
|
merit = meritNew;
|
||||||
|
bestFeature = feature;
|
||||||
|
}
|
||||||
|
selectedFeatures.pop_back();
|
||||||
|
}
|
||||||
|
if (bestFeature == -1) {
|
||||||
|
// meritNew has to be nan due to constant features
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
selectedFeatures.push_back(bestFeature);
|
||||||
|
selectedScores.push_back(merit);
|
||||||
|
featureOrder.erase(remove(featureOrder.begin(), featureOrder.end(), bestFeature), featureOrder.end());
|
||||||
|
continueCondition = computeContinueCondition(featureOrder);
|
||||||
|
}
|
||||||
|
fitted = true;
|
||||||
|
}
|
||||||
|
bool CFS::computeContinueCondition(const std::vector<int>& featureOrder)
|
||||||
|
{
|
||||||
|
if (selectedFeatures.size() == maxFeatures || featureOrder.size() == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (selectedScores.size() >= 5) {
|
||||||
|
/*
|
||||||
|
"To prevent the best first search from exploring the entire
|
||||||
|
feature subset search space, a stopping criterion is imposed.
|
||||||
|
The search will terminate if five consecutive fully expanded
|
||||||
|
subsets show no improvement over the current best subset."
|
||||||
|
as stated in Mark A.Hall Thesis
|
||||||
|
*/
|
||||||
|
double item_ant = std::numeric_limits<double>::lowest();
|
||||||
|
int num = 0;
|
||||||
|
std::vector<double> lastFive(selectedScores.end() - 5, selectedScores.end());
|
||||||
|
for (auto item : lastFive) {
|
||||||
|
if (item_ant == std::numeric_limits<double>::lowest()) {
|
||||||
|
item_ant = item;
|
||||||
|
}
|
||||||
|
if (item > item_ant) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
num++;
|
||||||
|
item_ant = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (num == 5) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
20
src/BayesNet/CFS.h
Normal file
20
src/BayesNet/CFS.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef CFS_H
|
||||||
|
#define CFS_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <vector>
|
||||||
|
#include "FeatureSelect.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
class CFS : public FeatureSelect {
|
||||||
|
public:
|
||||||
|
// dataset is a n+1xm tensor of integers where dataset[-1] is the y std::vector
|
||||||
|
CFS(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights) :
|
||||||
|
FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
virtual ~CFS() {};
|
||||||
|
void fit() override;
|
||||||
|
private:
|
||||||
|
bool computeContinueCondition(const std::vector<int>& featureOrder);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
12
src/BayesNet/CMakeLists.txt
Normal file
12
src/BayesNet/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/src/BayesNet)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/src/PyClassifiers)
|
||||||
|
include_directories(${Python3_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
add_library(BayesNet bayesnetUtils.cc Network.cc Node.cc BayesMetrics.cc Classifier.cc
|
||||||
|
KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANLd.cc KDBLd.cc SPODELd.cc AODELd.cc BoostAODE.cc
|
||||||
|
Mst.cc Proposal.cc CFS.cc FCBF.cc IWSS.cc FeatureSelect.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc)
|
||||||
|
target_link_libraries(BayesNet mdlp "${TORCH_LIBRARIES}")
|
160
src/BayesNet/Classifier.cc
Normal file
160
src/BayesNet/Classifier.cc
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
#include "Classifier.h"
|
||||||
|
#include "bayesnetUtils.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
Classifier::Classifier(Network model) : model(model), m(0), n(0), metrics(Metrics()), fitted(false) {}
|
||||||
|
Classifier& Classifier::build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
this->features = features;
|
||||||
|
this->className = className;
|
||||||
|
this->states = states;
|
||||||
|
m = dataset.size(1);
|
||||||
|
n = dataset.size(0) - 1;
|
||||||
|
checkFitParameters();
|
||||||
|
auto n_classes = states.at(className).size();
|
||||||
|
metrics = Metrics(dataset, features, className, n_classes);
|
||||||
|
model.initialize();
|
||||||
|
buildModel(weights);
|
||||||
|
trainModel(weights);
|
||||||
|
fitted = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
void Classifier::buildDataset(torch::Tensor& ytmp)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
auto yresized = torch::transpose(ytmp.view({ ytmp.size(0), 1 }), 0, 1);
|
||||||
|
dataset = torch::cat({ dataset, yresized }, 0);
|
||||||
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
|
std::cerr << e.what() << '\n';
|
||||||
|
std::cout << "X dimensions: " << dataset.sizes() << "\n";
|
||||||
|
std::cout << "y dimensions: " << ytmp.sizes() << "\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Classifier::trainModel(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
model.fit(dataset, weights, features, className, states);
|
||||||
|
}
|
||||||
|
// X is nxm where n is the number of features and m the number of samples
|
||||||
|
Classifier& Classifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
|
||||||
|
{
|
||||||
|
dataset = X;
|
||||||
|
buildDataset(y);
|
||||||
|
const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble);
|
||||||
|
return build(features, className, states, weights);
|
||||||
|
}
|
||||||
|
// X is nxm where n is the number of features and m the number of samples
|
||||||
|
Classifier& Classifier::fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
|
||||||
|
{
|
||||||
|
dataset = torch::zeros({ static_cast<int>(X.size()), static_cast<int>(X[0].size()) }, torch::kInt32);
|
||||||
|
for (int i = 0; i < X.size(); ++i) {
|
||||||
|
dataset.index_put_({ i, "..." }, torch::tensor(X[i], torch::kInt32));
|
||||||
|
}
|
||||||
|
auto ytmp = torch::tensor(y, torch::kInt32);
|
||||||
|
buildDataset(ytmp);
|
||||||
|
const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble);
|
||||||
|
return build(features, className, states, weights);
|
||||||
|
}
|
||||||
|
Classifier& Classifier::fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
|
||||||
|
{
|
||||||
|
this->dataset = dataset;
|
||||||
|
const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble);
|
||||||
|
return build(features, className, states, weights);
|
||||||
|
}
|
||||||
|
Classifier& Classifier::fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
this->dataset = dataset;
|
||||||
|
return build(features, className, states, weights);
|
||||||
|
}
|
||||||
|
void Classifier::checkFitParameters()
|
||||||
|
{
|
||||||
|
if (torch::is_floating_point(dataset)) {
|
||||||
|
throw std::invalid_argument("dataset (X, y) must be of type Integer");
|
||||||
|
}
|
||||||
|
if (n != features.size()) {
|
||||||
|
throw std::invalid_argument("Classifier: X " + std::to_string(n) + " and features " + std::to_string(features.size()) + " must have the same number of features");
|
||||||
|
}
|
||||||
|
if (states.find(className) == states.end()) {
|
||||||
|
throw std::invalid_argument("className not found in states");
|
||||||
|
}
|
||||||
|
for (auto feature : features) {
|
||||||
|
if (states.find(feature) == states.end()) {
|
||||||
|
throw std::invalid_argument("feature [" + feature + "] not found in states");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
torch::Tensor Classifier::predict(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("Classifier has not been fitted");
|
||||||
|
}
|
||||||
|
return model.predict(X);
|
||||||
|
}
|
||||||
|
std::vector<int> Classifier::predict(std::vector<std::vector<int>>& X)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("Classifier has not been fitted");
|
||||||
|
}
|
||||||
|
auto m_ = X[0].size();
|
||||||
|
auto n_ = X.size();
|
||||||
|
std::vector<std::vector<int>> Xd(n_, std::vector<int>(m_, 0));
|
||||||
|
for (auto i = 0; i < n_; i++) {
|
||||||
|
Xd[i] = std::vector<int>(X[i].begin(), X[i].end());
|
||||||
|
}
|
||||||
|
auto yp = model.predict(Xd);
|
||||||
|
return yp;
|
||||||
|
}
|
||||||
|
float Classifier::score(torch::Tensor& X, torch::Tensor& y)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("Classifier has not been fitted");
|
||||||
|
}
|
||||||
|
torch::Tensor y_pred = predict(X);
|
||||||
|
return (y_pred == y).sum().item<float>() / y.size(0);
|
||||||
|
}
|
||||||
|
float Classifier::score(std::vector<std::vector<int>>& X, std::vector<int>& y)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("Classifier has not been fitted");
|
||||||
|
}
|
||||||
|
return model.score(X, y);
|
||||||
|
}
|
||||||
|
std::vector<std::string> Classifier::show() const
|
||||||
|
{
|
||||||
|
return model.show();
|
||||||
|
}
|
||||||
|
void Classifier::addNodes()
|
||||||
|
{
|
||||||
|
// Add all nodes to the network
|
||||||
|
for (const auto& feature : features) {
|
||||||
|
model.addNode(feature);
|
||||||
|
}
|
||||||
|
model.addNode(className);
|
||||||
|
}
|
||||||
|
int Classifier::getNumberOfNodes() const
|
||||||
|
{
|
||||||
|
// Features does not include class
|
||||||
|
return fitted ? model.getFeatures().size() : 0;
|
||||||
|
}
|
||||||
|
int Classifier::getNumberOfEdges() const
|
||||||
|
{
|
||||||
|
return fitted ? model.getNumEdges() : 0;
|
||||||
|
}
|
||||||
|
int Classifier::getNumberOfStates() const
|
||||||
|
{
|
||||||
|
return fitted ? model.getStates() : 0;
|
||||||
|
}
|
||||||
|
std::vector<std::string> Classifier::topological_order()
|
||||||
|
{
|
||||||
|
return model.topological_sort();
|
||||||
|
}
|
||||||
|
void Classifier::dump_cpt() const
|
||||||
|
{
|
||||||
|
model.dump_cpt();
|
||||||
|
}
|
||||||
|
void Classifier::setHyperparameters(const nlohmann::json& hyperparameters)
|
||||||
|
{
|
||||||
|
//For classifiers that don't have hyperparameters
|
||||||
|
}
|
||||||
|
}
|
54
src/BayesNet/Classifier.h
Normal file
54
src/BayesNet/Classifier.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#ifndef CLASSIFIER_H
|
||||||
|
#define CLASSIFIER_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include "BaseClassifier.h"
|
||||||
|
#include "Network.h"
|
||||||
|
#include "BayesMetrics.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class Classifier : public BaseClassifier {
|
||||||
|
private:
|
||||||
|
Classifier& build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights);
|
||||||
|
protected:
|
||||||
|
bool fitted;
|
||||||
|
int m, n; // m: number of samples, n: number of features
|
||||||
|
Network model;
|
||||||
|
Metrics metrics;
|
||||||
|
std::vector<std::string> features;
|
||||||
|
std::string className;
|
||||||
|
std::map<std::string, std::vector<int>> states;
|
||||||
|
torch::Tensor dataset; // (n+1)xm tensor
|
||||||
|
status_t status = NORMAL;
|
||||||
|
void checkFitParameters();
|
||||||
|
virtual void buildModel(const torch::Tensor& weights) = 0;
|
||||||
|
void trainModel(const torch::Tensor& weights) override;
|
||||||
|
void buildDataset(torch::Tensor& y);
|
||||||
|
public:
|
||||||
|
Classifier(Network model);
|
||||||
|
virtual ~Classifier() = default;
|
||||||
|
Classifier& fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
|
||||||
|
Classifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
|
||||||
|
Classifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
|
||||||
|
Classifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights) override;
|
||||||
|
void addNodes();
|
||||||
|
int getNumberOfNodes() const override;
|
||||||
|
int getNumberOfEdges() const override;
|
||||||
|
int getNumberOfStates() const override;
|
||||||
|
torch::Tensor predict(torch::Tensor& X) override;
|
||||||
|
status_t getStatus() const override { return status; }
|
||||||
|
std::string getVersion() override { return "0.2.0"; };
|
||||||
|
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
|
||||||
|
float score(torch::Tensor& X, torch::Tensor& y) override;
|
||||||
|
float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override;
|
||||||
|
std::vector<std::string> show() const override;
|
||||||
|
std::vector<std::string> topological_order() override;
|
||||||
|
void dump_cpt() const override;
|
||||||
|
void setHyperparameters(const nlohmann::json& hyperparameters) override; //For classifiers that don't have hyperparameters
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
141
src/BayesNet/Ensemble.cc
Normal file
141
src/BayesNet/Ensemble.cc
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
#include "Ensemble.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
|
||||||
|
Ensemble::Ensemble() : Classifier(Network()), n_models(0) {}
|
||||||
|
|
||||||
|
void Ensemble::trainModel(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
n_models = models.size();
|
||||||
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
|
// fit with std::vectors
|
||||||
|
models[i]->fit(dataset, features, className, states);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::vector<int> Ensemble::voting(torch::Tensor& y_pred)
|
||||||
|
{
|
||||||
|
auto y_pred_ = y_pred.accessor<int, 2>();
|
||||||
|
std::vector<int> y_pred_final;
|
||||||
|
int numClasses = states.at(className).size();
|
||||||
|
// y_pred is m x n_models with the prediction of every model for each sample
|
||||||
|
for (int i = 0; i < y_pred.size(0); ++i) {
|
||||||
|
// votes store in each index (value of class) the significance added by each model
|
||||||
|
// i.e. votes[0] contains how much value has the value 0 of class. That value is generated by the models predictions
|
||||||
|
std::vector<double> votes(numClasses, 0.0);
|
||||||
|
for (int j = 0; j < n_models; ++j) {
|
||||||
|
votes[y_pred_[i][j]] += significanceModels.at(j);
|
||||||
|
}
|
||||||
|
// argsort in descending order
|
||||||
|
auto indices = argsort(votes);
|
||||||
|
y_pred_final.push_back(indices[0]);
|
||||||
|
}
|
||||||
|
return y_pred_final;
|
||||||
|
}
|
||||||
|
torch::Tensor Ensemble::predict(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("Ensemble has not been fitted");
|
||||||
|
}
|
||||||
|
torch::Tensor y_pred = torch::zeros({ X.size(1), n_models }, torch::kInt32);
|
||||||
|
auto threads{ std::vector<std::thread>() };
|
||||||
|
std::mutex mtx;
|
||||||
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
|
threads.push_back(std::thread([&, i]() {
|
||||||
|
auto ypredict = models[i]->predict(X);
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
y_pred.index_put_({ "...", i }, ypredict);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
for (auto& thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
return torch::tensor(voting(y_pred));
|
||||||
|
}
|
||||||
|
std::vector<int> Ensemble::predict(std::vector<std::vector<int>>& X)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("Ensemble has not been fitted");
|
||||||
|
}
|
||||||
|
long m_ = X[0].size();
|
||||||
|
long n_ = X.size();
|
||||||
|
std::vector<std::vector<int>> Xd(n_, std::vector<int>(m_, 0));
|
||||||
|
for (auto i = 0; i < n_; i++) {
|
||||||
|
Xd[i] = std::vector<int>(X[i].begin(), X[i].end());
|
||||||
|
}
|
||||||
|
torch::Tensor y_pred = torch::zeros({ m_, n_models }, torch::kInt32);
|
||||||
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
|
y_pred.index_put_({ "...", i }, torch::tensor(models[i]->predict(Xd), torch::kInt32));
|
||||||
|
}
|
||||||
|
return voting(y_pred);
|
||||||
|
}
|
||||||
|
float Ensemble::score(torch::Tensor& X, torch::Tensor& y)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("Ensemble has not been fitted");
|
||||||
|
}
|
||||||
|
auto y_pred = predict(X);
|
||||||
|
int correct = 0;
|
||||||
|
for (int i = 0; i < y_pred.size(0); ++i) {
|
||||||
|
if (y_pred[i].item<int>() == y[i].item<int>()) {
|
||||||
|
correct++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (double)correct / y_pred.size(0);
|
||||||
|
}
|
||||||
|
float Ensemble::score(std::vector<std::vector<int>>& X, std::vector<int>& y)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("Ensemble has not been fitted");
|
||||||
|
}
|
||||||
|
auto y_pred = predict(X);
|
||||||
|
int correct = 0;
|
||||||
|
for (int i = 0; i < y_pred.size(); ++i) {
|
||||||
|
if (y_pred[i] == y[i]) {
|
||||||
|
correct++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (double)correct / y_pred.size();
|
||||||
|
}
|
||||||
|
std::vector<std::string> Ensemble::show() const
|
||||||
|
{
|
||||||
|
auto result = std::vector<std::string>();
|
||||||
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
|
auto res = models[i]->show();
|
||||||
|
result.insert(result.end(), res.begin(), res.end());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
std::vector<std::string> Ensemble::graph(const std::string& title) const
|
||||||
|
{
|
||||||
|
auto result = std::vector<std::string>();
|
||||||
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
|
auto res = models[i]->graph(title + "_" + std::to_string(i));
|
||||||
|
result.insert(result.end(), res.begin(), res.end());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
int Ensemble::getNumberOfNodes() const
|
||||||
|
{
|
||||||
|
int nodes = 0;
|
||||||
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
|
nodes += models[i]->getNumberOfNodes();
|
||||||
|
}
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
int Ensemble::getNumberOfEdges() const
|
||||||
|
{
|
||||||
|
int edges = 0;
|
||||||
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
|
edges += models[i]->getNumberOfEdges();
|
||||||
|
}
|
||||||
|
return edges;
|
||||||
|
}
|
||||||
|
int Ensemble::getNumberOfStates() const
|
||||||
|
{
|
||||||
|
int nstates = 0;
|
||||||
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
|
nstates += models[i]->getNumberOfStates();
|
||||||
|
}
|
||||||
|
return nstates;
|
||||||
|
}
|
||||||
|
}
|
39
src/BayesNet/Ensemble.h
Normal file
39
src/BayesNet/Ensemble.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#ifndef ENSEMBLE_H
|
||||||
|
#define ENSEMBLE_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include "Classifier.h"
|
||||||
|
#include "BayesMetrics.h"
|
||||||
|
#include "bayesnetUtils.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class Ensemble : public Classifier {
|
||||||
|
private:
|
||||||
|
Ensemble& build(std::vector<std::string>& features, std::string className, std::map<std::string, std::vector<int>>& states);
|
||||||
|
protected:
|
||||||
|
unsigned n_models;
|
||||||
|
std::vector<std::unique_ptr<Classifier>> models;
|
||||||
|
std::vector<double> significanceModels;
|
||||||
|
void trainModel(const torch::Tensor& weights) override;
|
||||||
|
std::vector<int> voting(torch::Tensor& y_pred);
|
||||||
|
public:
|
||||||
|
Ensemble();
|
||||||
|
virtual ~Ensemble() = default;
|
||||||
|
torch::Tensor predict(torch::Tensor& X) override;
|
||||||
|
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
|
||||||
|
float score(torch::Tensor& X, torch::Tensor& y) override;
|
||||||
|
float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override;
|
||||||
|
int getNumberOfNodes() const override;
|
||||||
|
int getNumberOfEdges() const override;
|
||||||
|
int getNumberOfStates() const override;
|
||||||
|
std::vector<std::string> show() const override;
|
||||||
|
std::vector<std::string> graph(const std::string& title) const override;
|
||||||
|
std::vector<std::string> topological_order() override
|
||||||
|
{
|
||||||
|
return std::vector<std::string>();
|
||||||
|
}
|
||||||
|
void dump_cpt() const override
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
44
src/BayesNet/FCBF.cc
Normal file
44
src/BayesNet/FCBF.cc
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "bayesnetUtils.h"
|
||||||
|
#include "FCBF.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
|
||||||
|
FCBF::FCBF(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold) :
|
||||||
|
FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights), threshold(threshold)
|
||||||
|
{
|
||||||
|
if (threshold < 1e-7) {
|
||||||
|
throw std::invalid_argument("Threshold cannot be less than 1e-7");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void FCBF::fit()
|
||||||
|
{
|
||||||
|
initialize();
|
||||||
|
computeSuLabels();
|
||||||
|
auto featureOrder = argsort(suLabels); // sort descending order
|
||||||
|
auto featureOrderCopy = featureOrder;
|
||||||
|
for (const auto& feature : featureOrder) {
|
||||||
|
// Don't self compare
|
||||||
|
featureOrderCopy.erase(featureOrderCopy.begin());
|
||||||
|
if (suLabels.at(feature) == 0.0) {
|
||||||
|
// The feature has been removed from the list
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (suLabels.at(feature) < threshold) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Remove redundant features
|
||||||
|
for (const auto& featureCopy : featureOrderCopy) {
|
||||||
|
double value = computeSuFeatures(feature, featureCopy);
|
||||||
|
if (value >= suLabels.at(featureCopy)) {
|
||||||
|
// Remove feature from list
|
||||||
|
suLabels[featureCopy] = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
selectedFeatures.push_back(feature);
|
||||||
|
selectedScores.push_back(suLabels[feature]);
|
||||||
|
if (selectedFeatures.size() == maxFeatures) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fitted = true;
|
||||||
|
}
|
||||||
|
}
|
17
src/BayesNet/FCBF.h
Normal file
17
src/BayesNet/FCBF.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef FCBF_H
|
||||||
|
#define FCBF_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <vector>
|
||||||
|
#include "FeatureSelect.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
class FCBF : public FeatureSelect {
|
||||||
|
public:
|
||||||
|
// dataset is a n+1xm tensor of integers where dataset[-1] is the y std::vector
|
||||||
|
FCBF(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold);
|
||||||
|
virtual ~FCBF() {};
|
||||||
|
void fit() override;
|
||||||
|
private:
|
||||||
|
double threshold = -1;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
79
src/BayesNet/FeatureSelect.cc
Normal file
79
src/BayesNet/FeatureSelect.cc
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#include "FeatureSelect.h"
|
||||||
|
#include <limits>
|
||||||
|
#include "bayesnetUtils.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
FeatureSelect::FeatureSelect(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights) :
|
||||||
|
Metrics(samples, features, className, classNumStates), maxFeatures(maxFeatures == 0 ? samples.size(0) - 1 : maxFeatures), weights(weights)
|
||||||
|
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void FeatureSelect::initialize()
|
||||||
|
{
|
||||||
|
selectedFeatures.clear();
|
||||||
|
selectedScores.clear();
|
||||||
|
}
|
||||||
|
double FeatureSelect::symmetricalUncertainty(int a, int b)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Compute symmetrical uncertainty. Normalize* information gain (mutual
|
||||||
|
information) with the entropies of the features in order to compensate
|
||||||
|
the bias due to high cardinality features. *Range [0, 1]
|
||||||
|
(https://www.sciencedirect.com/science/article/pii/S0020025519303603)
|
||||||
|
*/
|
||||||
|
auto x = samples.index({ a, "..." });
|
||||||
|
auto y = samples.index({ b, "..." });
|
||||||
|
auto mu = mutualInformation(x, y, weights);
|
||||||
|
auto hx = entropy(x, weights);
|
||||||
|
auto hy = entropy(y, weights);
|
||||||
|
return 2.0 * mu / (hx + hy);
|
||||||
|
}
|
||||||
|
void FeatureSelect::computeSuLabels()
|
||||||
|
{
|
||||||
|
// Compute Simmetrical Uncertainty between features and labels
|
||||||
|
// https://en.wikipedia.org/wiki/Symmetric_uncertainty
|
||||||
|
for (int i = 0; i < features.size(); ++i) {
|
||||||
|
suLabels.push_back(symmetricalUncertainty(i, -1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double FeatureSelect::computeSuFeatures(const int firstFeature, const int secondFeature)
|
||||||
|
{
|
||||||
|
// Compute Simmetrical Uncertainty between features
|
||||||
|
// https://en.wikipedia.org/wiki/Symmetric_uncertainty
|
||||||
|
try {
|
||||||
|
return suFeatures.at({ firstFeature, secondFeature });
|
||||||
|
}
|
||||||
|
catch (const std::out_of_range& e) {
|
||||||
|
double result = symmetricalUncertainty(firstFeature, secondFeature);
|
||||||
|
suFeatures[{firstFeature, secondFeature}] = result;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double FeatureSelect::computeMeritCFS()
|
||||||
|
{
|
||||||
|
double result;
|
||||||
|
double rcf = 0;
|
||||||
|
for (auto feature : selectedFeatures) {
|
||||||
|
rcf += suLabels[feature];
|
||||||
|
}
|
||||||
|
double rff = 0;
|
||||||
|
int n = selectedFeatures.size();
|
||||||
|
for (const auto& item : doCombinations(selectedFeatures)) {
|
||||||
|
rff += computeSuFeatures(item.first, item.second);
|
||||||
|
}
|
||||||
|
return rcf / sqrt(n + (n * n - n) * rff);
|
||||||
|
}
|
||||||
|
std::vector<int> FeatureSelect::getFeatures() const
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::runtime_error("FeatureSelect not fitted");
|
||||||
|
}
|
||||||
|
return selectedFeatures;
|
||||||
|
}
|
||||||
|
std::vector<double> FeatureSelect::getScores() const
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::runtime_error("FeatureSelect not fitted");
|
||||||
|
}
|
||||||
|
return selectedScores;
|
||||||
|
}
|
||||||
|
}
|
30
src/BayesNet/FeatureSelect.h
Normal file
30
src/BayesNet/FeatureSelect.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef FEATURE_SELECT_H
|
||||||
|
#define FEATURE_SELECT_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <vector>
|
||||||
|
#include "BayesMetrics.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
class FeatureSelect : public Metrics {
|
||||||
|
public:
|
||||||
|
// dataset is a n+1xm tensor of integers where dataset[-1] is the y std::vector
|
||||||
|
FeatureSelect(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights);
|
||||||
|
virtual ~FeatureSelect() {};
|
||||||
|
virtual void fit() = 0;
|
||||||
|
std::vector<int> getFeatures() const;
|
||||||
|
std::vector<double> getScores() const;
|
||||||
|
protected:
|
||||||
|
void initialize();
|
||||||
|
void computeSuLabels();
|
||||||
|
double computeSuFeatures(const int a, const int b);
|
||||||
|
double symmetricalUncertainty(int a, int b);
|
||||||
|
double computeMeritCFS();
|
||||||
|
const torch::Tensor& weights;
|
||||||
|
int maxFeatures;
|
||||||
|
std::vector<int> selectedFeatures;
|
||||||
|
std::vector<double> selectedScores;
|
||||||
|
std::vector<double> suLabels;
|
||||||
|
std::map<std::pair<int, int>, double> suFeatures;
|
||||||
|
bool fitted = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
47
src/BayesNet/IWSS.cc
Normal file
47
src/BayesNet/IWSS.cc
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "IWSS.h"
|
||||||
|
#include <limits>
|
||||||
|
#include "bayesnetUtils.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
IWSS::IWSS(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold) :
|
||||||
|
FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights), threshold(threshold)
|
||||||
|
{
|
||||||
|
if (threshold < 0 || threshold > .5) {
|
||||||
|
throw std::invalid_argument("Threshold has to be in [0, 0.5]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void IWSS::fit()
|
||||||
|
{
|
||||||
|
initialize();
|
||||||
|
computeSuLabels();
|
||||||
|
auto featureOrder = argsort(suLabels); // sort descending order
|
||||||
|
auto featureOrderCopy = featureOrder;
|
||||||
|
// Add first and second features to result
|
||||||
|
// First with its own score
|
||||||
|
auto first_feature = pop_first(featureOrderCopy);
|
||||||
|
selectedFeatures.push_back(first_feature);
|
||||||
|
selectedScores.push_back(suLabels.at(first_feature));
|
||||||
|
// Second with the score of the candidates
|
||||||
|
selectedFeatures.push_back(pop_first(featureOrderCopy));
|
||||||
|
auto merit = computeMeritCFS();
|
||||||
|
selectedScores.push_back(merit);
|
||||||
|
for (const auto feature : featureOrderCopy) {
|
||||||
|
selectedFeatures.push_back(feature);
|
||||||
|
// Compute merit with selectedFeatures
|
||||||
|
auto meritNew = computeMeritCFS();
|
||||||
|
double delta = merit != 0.0 ? abs(merit - meritNew) / merit : 0.0;
|
||||||
|
if (meritNew > merit || delta < threshold) {
|
||||||
|
if (meritNew > merit) {
|
||||||
|
merit = meritNew;
|
||||||
|
}
|
||||||
|
selectedScores.push_back(meritNew);
|
||||||
|
} else {
|
||||||
|
selectedFeatures.pop_back();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (selectedFeatures.size() == maxFeatures) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fitted = true;
|
||||||
|
}
|
||||||
|
}
|
17
src/BayesNet/IWSS.h
Normal file
17
src/BayesNet/IWSS.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef IWSS_H
|
||||||
|
#define IWSS_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <vector>
|
||||||
|
#include "FeatureSelect.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
class IWSS : public FeatureSelect {
|
||||||
|
public:
|
||||||
|
// dataset is a n+1xm tensor of integers where dataset[-1] is the y std::vector
|
||||||
|
IWSS(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold);
|
||||||
|
virtual ~IWSS() {};
|
||||||
|
void fit() override;
|
||||||
|
private:
|
||||||
|
double threshold = -1;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
@@ -1,11 +1,21 @@
|
|||||||
#include "KDB.h"
|
#include "KDB.h"
|
||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
using namespace std;
|
KDB::KDB(int k, float theta) : Classifier(Network()), k(k), theta(theta)
|
||||||
using namespace torch;
|
{
|
||||||
|
validHyperparameters = { "k", "theta" };
|
||||||
|
|
||||||
KDB::KDB(int k, float theta) : BaseClassifier(Network()), k(k), theta(theta) {}
|
}
|
||||||
void KDB::train()
|
void KDB::setHyperparameters(const nlohmann::json& hyperparameters)
|
||||||
|
{
|
||||||
|
if (hyperparameters.contains("k")) {
|
||||||
|
k = hyperparameters["k"];
|
||||||
|
}
|
||||||
|
if (hyperparameters.contains("theta")) {
|
||||||
|
theta = hyperparameters["theta"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void KDB::buildModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
1. For each feature Xi, compute mutual information, I(X;C),
|
1. For each feature Xi, compute mutual information, I(X;C),
|
||||||
@@ -28,25 +38,25 @@ namespace bayesnet {
|
|||||||
*/
|
*/
|
||||||
// 1. For each feature Xi, compute mutual information, I(X;C),
|
// 1. For each feature Xi, compute mutual information, I(X;C),
|
||||||
// where C is the class.
|
// where C is the class.
|
||||||
vector <float> mi;
|
addNodes();
|
||||||
|
const torch::Tensor& y = dataset.index({ -1, "..." });
|
||||||
|
std::vector<double> mi;
|
||||||
for (auto i = 0; i < features.size(); i++) {
|
for (auto i = 0; i < features.size(); i++) {
|
||||||
Tensor firstFeature = X.index({ "...", i });
|
torch::Tensor firstFeature = dataset.index({ i, "..." });
|
||||||
mi.push_back(metrics.mutualInformation(firstFeature, y));
|
mi.push_back(metrics.mutualInformation(firstFeature, y, weights));
|
||||||
}
|
}
|
||||||
// 2. Compute class conditional mutual information I(Xi;XjIC), f or each
|
// 2. Compute class conditional mutual information I(Xi;XjIC), f or each
|
||||||
auto conditionalEdgeWeights = metrics.conditionalEdge();
|
auto conditionalEdgeWeights = metrics.conditionalEdge(weights);
|
||||||
// 3. Let the used variable list, S, be empty.
|
// 3. Let the used variable list, S, be empty.
|
||||||
vector<int> S;
|
std::vector<int> S;
|
||||||
// 4. Let the DAG network being constructed, BN, begin with a single
|
// 4. Let the DAG network being constructed, BN, begin with a single
|
||||||
// class node, C.
|
// class node, C.
|
||||||
model.addNode(className, states[className].size());
|
|
||||||
// 5. Repeat until S includes all domain features
|
// 5. Repeat until S includes all domain features
|
||||||
// 5.1. Select feature Xmax which is not in S and has the largest value
|
// 5.1. Select feature Xmax which is not in S and has the largest value
|
||||||
// I(Xmax;C).
|
// I(Xmax;C).
|
||||||
auto order = argsort(mi);
|
auto order = argsort(mi);
|
||||||
for (auto idx : order) {
|
for (auto idx : order) {
|
||||||
// 5.2. Add a node to BN representing Xmax.
|
// 5.2. Add a node to BN representing Xmax.
|
||||||
model.addNode(features[idx], states[features[idx]].size());
|
|
||||||
// 5.3. Add an arc from C to Xmax in BN.
|
// 5.3. Add an arc from C to Xmax in BN.
|
||||||
model.addEdge(className, features[idx]);
|
model.addEdge(className, features[idx]);
|
||||||
// 5.4. Add m = min(lSl,/c) arcs from m distinct features Xj in S with
|
// 5.4. Add m = min(lSl,/c) arcs from m distinct features Xj in S with
|
||||||
@@ -56,9 +66,9 @@ namespace bayesnet {
|
|||||||
S.push_back(idx);
|
S.push_back(idx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void KDB::add_m_edges(int idx, vector<int>& S, Tensor& weights)
|
void KDB::add_m_edges(int idx, std::vector<int>& S, torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
auto n_edges = min(k, static_cast<int>(S.size()));
|
auto n_edges = std::min(k, static_cast<int>(S.size()));
|
||||||
auto cond_w = clone(weights);
|
auto cond_w = clone(weights);
|
||||||
bool exit_cond = k == 0;
|
bool exit_cond = k == 0;
|
||||||
int num = 0;
|
int num = 0;
|
||||||
@@ -70,7 +80,7 @@ namespace bayesnet {
|
|||||||
model.addEdge(features[max_minfo], features[idx]);
|
model.addEdge(features[max_minfo], features[idx]);
|
||||||
num++;
|
num++;
|
||||||
}
|
}
|
||||||
catch (const invalid_argument& e) {
|
catch (const std::invalid_argument& e) {
|
||||||
// Loops are not allowed
|
// Loops are not allowed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -80,11 +90,12 @@ namespace bayesnet {
|
|||||||
exit_cond = num == n_edges || candidates.size(0) == 0;
|
exit_cond = num == n_edges || candidates.size(0) == 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vector<string> KDB::graph(string title)
|
std::vector<std::string> KDB::graph(const std::string& title) const
|
||||||
{
|
{
|
||||||
|
std::string header{ title };
|
||||||
if (title == "KDB") {
|
if (title == "KDB") {
|
||||||
title += " (k=" + to_string(k) + ", theta=" + to_string(theta) + ")";
|
header += " (k=" + std::to_string(k) + ", theta=" + std::to_string(theta) + ")";
|
||||||
}
|
}
|
||||||
return model.graph(title);
|
return model.graph(header);
|
||||||
}
|
}
|
||||||
}
|
}
|
21
src/BayesNet/KDB.h
Normal file
21
src/BayesNet/KDB.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef KDB_H
|
||||||
|
#define KDB_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include "Classifier.h"
|
||||||
|
#include "bayesnetUtils.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
class KDB : public Classifier {
|
||||||
|
private:
|
||||||
|
int k;
|
||||||
|
float theta;
|
||||||
|
void add_m_edges(int idx, std::vector<int>& S, torch::Tensor& weights);
|
||||||
|
protected:
|
||||||
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
|
public:
|
||||||
|
explicit KDB(int k, float theta = 0.03);
|
||||||
|
virtual ~KDB() = default;
|
||||||
|
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||||
|
std::vector<std::string> graph(const std::string& name = "KDB") const override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
29
src/BayesNet/KDBLd.cc
Normal file
29
src/BayesNet/KDBLd.cc
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "KDBLd.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
KDBLd::KDBLd(int k) : KDB(k), Proposal(dataset, features, className) {}
|
||||||
|
KDBLd& KDBLd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
|
||||||
|
{
|
||||||
|
checkInput(X_, y_);
|
||||||
|
features = features_;
|
||||||
|
className = className_;
|
||||||
|
Xf = X_;
|
||||||
|
y = y_;
|
||||||
|
// Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
|
||||||
|
states = fit_local_discretization(y);
|
||||||
|
// We have discretized the input data
|
||||||
|
// 1st we need to fit the model to build the normal KDB structure, KDB::fit initializes the base Bayesian network
|
||||||
|
KDB::fit(dataset, features, className, states);
|
||||||
|
states = localDiscretizationProposal(states, model);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
torch::Tensor KDBLd::predict(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
auto Xt = prepareX(X);
|
||||||
|
return KDB::predict(Xt);
|
||||||
|
}
|
||||||
|
std::vector<std::string> KDBLd::graph(const std::string& name) const
|
||||||
|
{
|
||||||
|
return KDB::graph(name);
|
||||||
|
}
|
||||||
|
}
|
18
src/BayesNet/KDBLd.h
Normal file
18
src/BayesNet/KDBLd.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef KDBLD_H
|
||||||
|
#define KDBLD_H
|
||||||
|
#include "KDB.h"
|
||||||
|
#include "Proposal.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class KDBLd : public KDB, public Proposal {
|
||||||
|
private:
|
||||||
|
public:
|
||||||
|
explicit KDBLd(int k);
|
||||||
|
virtual ~KDBLd() = default;
|
||||||
|
KDBLd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states) override;
|
||||||
|
std::vector<std::string> graph(const std::string& name = "KDB") const override;
|
||||||
|
torch::Tensor predict(torch::Tensor& X) override;
|
||||||
|
static inline std::string version() { return "0.0.1"; };
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif // !KDBLD_H
|
@@ -1,15 +1,14 @@
|
|||||||
#include "Mst.h"
|
#include "Mst.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <list>
|
||||||
/*
|
/*
|
||||||
Based on the code from https://www.softwaretestinghelp.com/minimum-spanning-tree-tutorial/
|
Based on the code from https://www.softwaretestinghelp.com/minimum-spanning-tree-tutorial/
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
using namespace std;
|
Graph::Graph(int V) : V(V), parent(std::vector<int>(V))
|
||||||
Graph::Graph(int V)
|
|
||||||
{
|
{
|
||||||
parent = vector<int>(V);
|
|
||||||
for (int i = 0; i < V; i++)
|
for (int i = 0; i < V; i++)
|
||||||
parent[i] = i;
|
parent[i] = i;
|
||||||
G.clear();
|
G.clear();
|
||||||
@@ -34,37 +33,46 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
void Graph::kruskal_algorithm()
|
void Graph::kruskal_algorithm()
|
||||||
{
|
{
|
||||||
int i, uSt, vEd;
|
|
||||||
// sort the edges ordered on decreasing weight
|
// sort the edges ordered on decreasing weight
|
||||||
sort(G.begin(), G.end(), [](auto& left, auto& right) {return left.first > right.first;});
|
stable_sort(G.begin(), G.end(), [](const auto& left, const auto& right) {return left.first > right.first;});
|
||||||
for (i = 0; i < G.size(); i++) {
|
for (int i = 0; i < G.size(); i++) {
|
||||||
|
int uSt, vEd;
|
||||||
uSt = find_set(G[i].second.first);
|
uSt = find_set(G[i].second.first);
|
||||||
vEd = find_set(G[i].second.second);
|
vEd = find_set(G[i].second.second);
|
||||||
if (uSt != vEd) {
|
if (uSt != vEd) {
|
||||||
T.push_back(G[i]); // add to mst vector
|
T.push_back(G[i]); // add to mst std::vector
|
||||||
union_set(uSt, vEd);
|
union_set(uSt, vEd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void Graph::display_mst()
|
void Graph::display_mst()
|
||||||
{
|
{
|
||||||
cout << "Edge :" << " Weight" << endl;
|
std::cout << "Edge :" << " Weight" << std::endl;
|
||||||
for (int i = 0; i < T.size(); i++) {
|
for (int i = 0; i < T.size(); i++) {
|
||||||
cout << T[i].second.first << " - " << T[i].second.second << " : "
|
std::cout << T[i].second.first << " - " << T[i].second.second << " : "
|
||||||
<< T[i].first;
|
<< T[i].first;
|
||||||
cout << endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<pair<int, int>> reorder(vector<pair<float, pair<int, int>>> T, int root_original)
|
void insertElement(std::list<int>& variables, int variable)
|
||||||
{
|
{
|
||||||
auto result = vector<pair<int, int>>();
|
if (std::find(variables.begin(), variables.end(), variable) == variables.end()) {
|
||||||
auto visited = vector<int>();
|
variables.push_front(variable);
|
||||||
auto nextVariables = unordered_set<int>();
|
}
|
||||||
nextVariables.emplace(root_original);
|
}
|
||||||
|
|
||||||
|
std::vector<std::pair<int, int>> reorder(std::vector<std::pair<float, std::pair<int, int>>> T, int root_original)
|
||||||
|
{
|
||||||
|
// Create the edges of a DAG from the MST
|
||||||
|
// replacing unordered_set with list because unordered_set cannot guarantee the order of the elements inserted
|
||||||
|
auto result = std::vector<std::pair<int, int>>();
|
||||||
|
auto visited = std::vector<int>();
|
||||||
|
auto nextVariables = std::list<int>();
|
||||||
|
nextVariables.push_front(root_original);
|
||||||
while (nextVariables.size() > 0) {
|
while (nextVariables.size() > 0) {
|
||||||
int root = *nextVariables.begin();
|
int root = nextVariables.front();
|
||||||
nextVariables.erase(nextVariables.begin());
|
nextVariables.pop_front();
|
||||||
for (int i = 0; i < T.size(); ++i) {
|
for (int i = 0; i < T.size(); ++i) {
|
||||||
auto [weight, edge] = T[i];
|
auto [weight, edge] = T[i];
|
||||||
auto [from, to] = edge;
|
auto [from, to] = edge;
|
||||||
@@ -72,10 +80,10 @@ namespace bayesnet {
|
|||||||
visited.insert(visited.begin(), i);
|
visited.insert(visited.begin(), i);
|
||||||
if (from == root) {
|
if (from == root) {
|
||||||
result.push_back({ from, to });
|
result.push_back({ from, to });
|
||||||
nextVariables.emplace(to);
|
insertElement(nextVariables, to);
|
||||||
} else {
|
} else {
|
||||||
result.push_back({ to, from });
|
result.push_back({ to, from });
|
||||||
nextVariables.emplace(from);
|
insertElement(nextVariables, from);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,15 +103,14 @@ namespace bayesnet {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
MST::MST(vector<string>& features, Tensor& weights, int root) : features(features), weights(weights), root(root) {}
|
MST::MST(const std::vector<std::string>& features, const torch::Tensor& weights, const int root) : features(features), weights(weights), root(root) {}
|
||||||
vector<pair<int, int>> MST::maximumSpanningTree()
|
std::vector<std::pair<int, int>> MST::maximumSpanningTree()
|
||||||
{
|
{
|
||||||
auto num_features = features.size();
|
auto num_features = features.size();
|
||||||
Graph g(num_features);
|
Graph g(num_features);
|
||||||
|
|
||||||
// Make a complete graph
|
// Make a complete graph
|
||||||
for (int i = 0; i < num_features - 1; ++i) {
|
for (int i = 0; i < num_features - 1; ++i) {
|
||||||
for (int j = i; j < num_features; ++j) {
|
for (int j = i + 1; j < num_features; ++j) {
|
||||||
g.addEdge(i, j, weights[i][j].item<float>());
|
g.addEdge(i, j, weights[i][j].item<float>());
|
||||||
}
|
}
|
||||||
}
|
}
|
33
src/BayesNet/Mst.h
Normal file
33
src/BayesNet/Mst.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef MST_H
|
||||||
|
#define MST_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
namespace bayesnet {
|
||||||
|
class MST {
|
||||||
|
private:
|
||||||
|
torch::Tensor weights;
|
||||||
|
std::vector<std::string> features;
|
||||||
|
int root = 0;
|
||||||
|
public:
|
||||||
|
MST() = default;
|
||||||
|
MST(const std::vector<std::string>& features, const torch::Tensor& weights, const int root);
|
||||||
|
std::vector<std::pair<int, int>> maximumSpanningTree();
|
||||||
|
};
|
||||||
|
class Graph {
|
||||||
|
private:
|
||||||
|
int V; // number of nodes in graph
|
||||||
|
std::vector <std::pair<float, std::pair<int, int>>> G; // std::vector for graph
|
||||||
|
std::vector <std::pair<float, std::pair<int, int>>> T; // std::vector for mst
|
||||||
|
std::vector<int> parent;
|
||||||
|
public:
|
||||||
|
explicit Graph(int V);
|
||||||
|
void addEdge(int u, int v, float wt);
|
||||||
|
int find_set(int i);
|
||||||
|
void union_set(int u, int v);
|
||||||
|
void kruskal_algorithm();
|
||||||
|
void display_mst();
|
||||||
|
std::vector <std::pair<float, std::pair<int, int>>> get_mst() { return T; }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
413
src/BayesNet/Network.cc
Normal file
413
src/BayesNet/Network.cc
Normal file
@@ -0,0 +1,413 @@
|
|||||||
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
#include "Network.h"
|
||||||
|
#include "bayesnetUtils.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
Network::Network() : features(std::vector<std::string>()), className(""), classNumStates(0), fitted(false), laplaceSmoothing(0) {}
|
||||||
|
Network::Network(float maxT) : features(std::vector<std::string>()), className(""), classNumStates(0), maxThreads(maxT), fitted(false), laplaceSmoothing(0) {}
|
||||||
|
Network::Network(Network& other) : laplaceSmoothing(other.laplaceSmoothing), features(other.features), className(other.className), classNumStates(other.getClassNumStates()), maxThreads(other.
|
||||||
|
getmaxThreads()), fitted(other.fitted)
|
||||||
|
{
|
||||||
|
for (const auto& node : other.nodes) {
|
||||||
|
nodes[node.first] = std::make_unique<Node>(*node.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Network::initialize()
|
||||||
|
{
|
||||||
|
features = std::vector<std::string>();
|
||||||
|
className = "";
|
||||||
|
classNumStates = 0;
|
||||||
|
fitted = false;
|
||||||
|
nodes.clear();
|
||||||
|
samples = torch::Tensor();
|
||||||
|
}
|
||||||
|
float Network::getmaxThreads()
|
||||||
|
{
|
||||||
|
return maxThreads;
|
||||||
|
}
|
||||||
|
torch::Tensor& Network::getSamples()
|
||||||
|
{
|
||||||
|
return samples;
|
||||||
|
}
|
||||||
|
void Network::addNode(const std::string& name)
|
||||||
|
{
|
||||||
|
if (name == "") {
|
||||||
|
throw std::invalid_argument("Node name cannot be empty");
|
||||||
|
}
|
||||||
|
if (nodes.find(name) != nodes.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (find(features.begin(), features.end(), name) == features.end()) {
|
||||||
|
features.push_back(name);
|
||||||
|
}
|
||||||
|
nodes[name] = std::make_unique<Node>(name);
|
||||||
|
}
|
||||||
|
std::vector<std::string> Network::getFeatures() const
|
||||||
|
{
|
||||||
|
return features;
|
||||||
|
}
|
||||||
|
int Network::getClassNumStates() const
|
||||||
|
{
|
||||||
|
return classNumStates;
|
||||||
|
}
|
||||||
|
int Network::getStates() const
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
for (auto& node : nodes) {
|
||||||
|
result += node.second->getNumStates();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
std::string Network::getClassName() const
|
||||||
|
{
|
||||||
|
return className;
|
||||||
|
}
|
||||||
|
bool Network::isCyclic(const std::string& nodeId, std::unordered_set<std::string>& visited, std::unordered_set<std::string>& recStack)
|
||||||
|
{
|
||||||
|
if (visited.find(nodeId) == visited.end()) // if node hasn't been visited yet
|
||||||
|
{
|
||||||
|
visited.insert(nodeId);
|
||||||
|
recStack.insert(nodeId);
|
||||||
|
for (Node* child : nodes[nodeId]->getChildren()) {
|
||||||
|
if (visited.find(child->getName()) == visited.end() && isCyclic(child->getName(), visited, recStack))
|
||||||
|
return true;
|
||||||
|
else if (recStack.find(child->getName()) != recStack.end())
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recStack.erase(nodeId); // remove node from recursion stack before function ends
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
void Network::addEdge(const std::string& parent, const std::string& child)
|
||||||
|
{
|
||||||
|
if (nodes.find(parent) == nodes.end()) {
|
||||||
|
throw std::invalid_argument("Parent node " + parent + " does not exist");
|
||||||
|
}
|
||||||
|
if (nodes.find(child) == nodes.end()) {
|
||||||
|
throw std::invalid_argument("Child node " + child + " does not exist");
|
||||||
|
}
|
||||||
|
// Temporarily add edge to check for cycles
|
||||||
|
nodes[parent]->addChild(nodes[child].get());
|
||||||
|
nodes[child]->addParent(nodes[parent].get());
|
||||||
|
std::unordered_set<std::string> visited;
|
||||||
|
std::unordered_set<std::string> recStack;
|
||||||
|
if (isCyclic(nodes[child]->getName(), visited, recStack)) // if adding this edge forms a cycle
|
||||||
|
{
|
||||||
|
// remove problematic edge
|
||||||
|
nodes[parent]->removeChild(nodes[child].get());
|
||||||
|
nodes[child]->removeParent(nodes[parent].get());
|
||||||
|
throw std::invalid_argument("Adding this edge forms a cycle in the graph.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::map<std::string, std::unique_ptr<Node>>& Network::getNodes()
|
||||||
|
{
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
void Network::checkFitData(int n_samples, int n_features, int n_samples_y, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
if (weights.size(0) != n_samples) {
|
||||||
|
throw std::invalid_argument("Weights (" + std::to_string(weights.size(0)) + ") must have the same number of elements as samples (" + std::to_string(n_samples) + ") in Network::fit");
|
||||||
|
}
|
||||||
|
if (n_samples != n_samples_y) {
|
||||||
|
throw std::invalid_argument("X and y must have the same number of samples in Network::fit (" + std::to_string(n_samples) + " != " + std::to_string(n_samples_y) + ")");
|
||||||
|
}
|
||||||
|
if (n_features != featureNames.size()) {
|
||||||
|
throw std::invalid_argument("X and features must have the same number of features in Network::fit (" + std::to_string(n_features) + " != " + std::to_string(featureNames.size()) + ")");
|
||||||
|
}
|
||||||
|
if (n_features != features.size() - 1) {
|
||||||
|
throw std::invalid_argument("X and local features must have the same number of features in Network::fit (" + std::to_string(n_features) + " != " + std::to_string(features.size() - 1) + ")");
|
||||||
|
}
|
||||||
|
if (find(features.begin(), features.end(), className) == features.end()) {
|
||||||
|
throw std::invalid_argument("className not found in Network::features");
|
||||||
|
}
|
||||||
|
for (auto& feature : featureNames) {
|
||||||
|
if (find(features.begin(), features.end(), feature) == features.end()) {
|
||||||
|
throw std::invalid_argument("Feature " + feature + " not found in Network::features");
|
||||||
|
}
|
||||||
|
if (states.find(feature) == states.end()) {
|
||||||
|
throw std::invalid_argument("Feature " + feature + " not found in states");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Network::setStates(const std::map<std::string, std::vector<int>>& states)
|
||||||
|
{
|
||||||
|
// Set states to every Node in the network
|
||||||
|
for_each(features.begin(), features.end(), [this, &states](const std::string& feature) {
|
||||||
|
nodes.at(feature)->setNumStates(states.at(feature).size());
|
||||||
|
});
|
||||||
|
classNumStates = nodes.at(className)->getNumStates();
|
||||||
|
}
|
||||||
|
// X comes in nxm, where n is the number of features and m the number of samples
|
||||||
|
void Network::fit(const torch::Tensor& X, const torch::Tensor& y, const torch::Tensor& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states)
|
||||||
|
{
|
||||||
|
checkFitData(X.size(1), X.size(0), y.size(0), featureNames, className, states, weights);
|
||||||
|
this->className = className;
|
||||||
|
torch::Tensor ytmp = torch::transpose(y.view({ y.size(0), 1 }), 0, 1);
|
||||||
|
samples = torch::cat({ X , ytmp }, 0);
|
||||||
|
for (int i = 0; i < featureNames.size(); ++i) {
|
||||||
|
auto row_feature = X.index({ i, "..." });
|
||||||
|
}
|
||||||
|
completeFit(states, weights);
|
||||||
|
}
|
||||||
|
void Network::fit(const torch::Tensor& samples, const torch::Tensor& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states)
|
||||||
|
{
|
||||||
|
checkFitData(samples.size(1), samples.size(0) - 1, samples.size(1), featureNames, className, states, weights);
|
||||||
|
this->className = className;
|
||||||
|
this->samples = samples;
|
||||||
|
completeFit(states, weights);
|
||||||
|
}
|
||||||
|
// input_data comes in nxm, where n is the number of features and m the number of samples
|
||||||
|
void Network::fit(const std::vector<std::vector<int>>& input_data, const std::vector<int>& labels, const std::vector<double>& weights_, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states)
|
||||||
|
{
|
||||||
|
const torch::Tensor weights = torch::tensor(weights_, torch::kFloat64);
|
||||||
|
checkFitData(input_data[0].size(), input_data.size(), labels.size(), featureNames, className, states, weights);
|
||||||
|
this->className = className;
|
||||||
|
// Build tensor of samples (nxm) (n+1 because of the class)
|
||||||
|
samples = torch::zeros({ static_cast<int>(input_data.size() + 1), static_cast<int>(input_data[0].size()) }, torch::kInt32);
|
||||||
|
for (int i = 0; i < featureNames.size(); ++i) {
|
||||||
|
samples.index_put_({ i, "..." }, torch::tensor(input_data[i], torch::kInt32));
|
||||||
|
}
|
||||||
|
samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
|
||||||
|
completeFit(states, weights);
|
||||||
|
}
|
||||||
|
void Network::completeFit(const std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
setStates(states);
|
||||||
|
laplaceSmoothing = 1.0 / samples.size(1); // To use in CPT computation
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
for (auto& node : nodes) {
|
||||||
|
threads.emplace_back([this, &node, &weights]() {
|
||||||
|
node.second->computeCPT(samples, features, laplaceSmoothing, weights);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (auto& thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
fitted = true;
|
||||||
|
}
|
||||||
|
torch::Tensor Network::predict_tensor(const torch::Tensor& samples, const bool proba)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("You must call fit() before calling predict()");
|
||||||
|
}
|
||||||
|
torch::Tensor result;
|
||||||
|
result = torch::zeros({ samples.size(1), classNumStates }, torch::kFloat64);
|
||||||
|
for (int i = 0; i < samples.size(1); ++i) {
|
||||||
|
const torch::Tensor sample = samples.index({ "...", i });
|
||||||
|
auto psample = predict_sample(sample);
|
||||||
|
auto temp = torch::tensor(psample, torch::kFloat64);
|
||||||
|
// result.index_put_({ i, "..." }, torch::tensor(predict_sample(sample), torch::kFloat64));
|
||||||
|
result.index_put_({ i, "..." }, temp);
|
||||||
|
}
|
||||||
|
if (proba)
|
||||||
|
return result;
|
||||||
|
return result.argmax(1);
|
||||||
|
}
|
||||||
|
// Return mxn tensor of probabilities
|
||||||
|
torch::Tensor Network::predict_proba(const torch::Tensor& samples)
|
||||||
|
{
|
||||||
|
return predict_tensor(samples, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return mxn tensor of probabilities
|
||||||
|
torch::Tensor Network::predict(const torch::Tensor& samples)
|
||||||
|
{
|
||||||
|
return predict_tensor(samples, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return mx1 std::vector of predictions
|
||||||
|
// tsamples is nxm std::vector of samples
|
||||||
|
std::vector<int> Network::predict(const std::vector<std::vector<int>>& tsamples)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("You must call fit() before calling predict()");
|
||||||
|
}
|
||||||
|
std::vector<int> predictions;
|
||||||
|
std::vector<int> sample;
|
||||||
|
for (int row = 0; row < tsamples[0].size(); ++row) {
|
||||||
|
sample.clear();
|
||||||
|
for (int col = 0; col < tsamples.size(); ++col) {
|
||||||
|
sample.push_back(tsamples[col][row]);
|
||||||
|
}
|
||||||
|
std::vector<double> classProbabilities = predict_sample(sample);
|
||||||
|
// Find the class with the maximum posterior probability
|
||||||
|
auto maxElem = max_element(classProbabilities.begin(), classProbabilities.end());
|
||||||
|
int predictedClass = distance(classProbabilities.begin(), maxElem);
|
||||||
|
predictions.push_back(predictedClass);
|
||||||
|
}
|
||||||
|
return predictions;
|
||||||
|
}
|
||||||
|
// Return mxn std::vector of probabilities
|
||||||
|
std::vector<std::vector<double>> Network::predict_proba(const std::vector<std::vector<int>>& tsamples)
|
||||||
|
{
|
||||||
|
if (!fitted) {
|
||||||
|
throw std::logic_error("You must call fit() before calling predict_proba()");
|
||||||
|
}
|
||||||
|
std::vector<std::vector<double>> predictions;
|
||||||
|
std::vector<int> sample;
|
||||||
|
for (int row = 0; row < tsamples[0].size(); ++row) {
|
||||||
|
sample.clear();
|
||||||
|
for (int col = 0; col < tsamples.size(); ++col) {
|
||||||
|
sample.push_back(tsamples[col][row]);
|
||||||
|
}
|
||||||
|
predictions.push_back(predict_sample(sample));
|
||||||
|
}
|
||||||
|
return predictions;
|
||||||
|
}
|
||||||
|
double Network::score(const std::vector<std::vector<int>>& tsamples, const std::vector<int>& labels)
|
||||||
|
{
|
||||||
|
std::vector<int> y_pred = predict(tsamples);
|
||||||
|
int correct = 0;
|
||||||
|
for (int i = 0; i < y_pred.size(); ++i) {
|
||||||
|
if (y_pred[i] == labels[i]) {
|
||||||
|
correct++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (double)correct / y_pred.size();
|
||||||
|
}
|
||||||
|
// Return 1xn std::vector of probabilities
|
||||||
|
std::vector<double> Network::predict_sample(const std::vector<int>& sample)
|
||||||
|
{
|
||||||
|
// Ensure the sample size is equal to the number of features
|
||||||
|
if (sample.size() != features.size() - 1) {
|
||||||
|
throw std::invalid_argument("Sample size (" + std::to_string(sample.size()) +
|
||||||
|
") does not match the number of features (" + std::to_string(features.size() - 1) + ")");
|
||||||
|
}
|
||||||
|
std::map<std::string, int> evidence;
|
||||||
|
for (int i = 0; i < sample.size(); ++i) {
|
||||||
|
evidence[features[i]] = sample[i];
|
||||||
|
}
|
||||||
|
return exactInference(evidence);
|
||||||
|
}
|
||||||
|
// Return 1xn std::vector of probabilities
|
||||||
|
std::vector<double> Network::predict_sample(const torch::Tensor& sample)
|
||||||
|
{
|
||||||
|
// Ensure the sample size is equal to the number of features
|
||||||
|
if (sample.size(0) != features.size() - 1) {
|
||||||
|
throw std::invalid_argument("Sample size (" + std::to_string(sample.size(0)) +
|
||||||
|
") does not match the number of features (" + std::to_string(features.size() - 1) + ")");
|
||||||
|
}
|
||||||
|
std::map<std::string, int> evidence;
|
||||||
|
for (int i = 0; i < sample.size(0); ++i) {
|
||||||
|
evidence[features[i]] = sample[i].item<int>();
|
||||||
|
}
|
||||||
|
return exactInference(evidence);
|
||||||
|
}
|
||||||
|
double Network::computeFactor(std::map<std::string, int>& completeEvidence)
|
||||||
|
{
|
||||||
|
double result = 1.0;
|
||||||
|
for (auto& node : getNodes()) {
|
||||||
|
result *= node.second->getFactorValue(completeEvidence);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
std::vector<double> Network::exactInference(std::map<std::string, int>& evidence)
|
||||||
|
{
|
||||||
|
std::vector<double> result(classNumStates, 0.0);
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
std::mutex mtx;
|
||||||
|
for (int i = 0; i < classNumStates; ++i) {
|
||||||
|
threads.emplace_back([this, &result, &evidence, i, &mtx]() {
|
||||||
|
auto completeEvidence = std::map<std::string, int>(evidence);
|
||||||
|
completeEvidence[getClassName()] = i;
|
||||||
|
double factor = computeFactor(completeEvidence);
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
result[i] = factor;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (auto& thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
// Normalize result
|
||||||
|
double sum = accumulate(result.begin(), result.end(), 0.0);
|
||||||
|
transform(result.begin(), result.end(), result.begin(), [sum](const double& value) { return value / sum; });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
std::vector<std::string> Network::show() const
|
||||||
|
{
|
||||||
|
std::vector<std::string> result;
|
||||||
|
// Draw the network
|
||||||
|
for (auto& node : nodes) {
|
||||||
|
std::string line = node.first + " -> ";
|
||||||
|
for (auto child : node.second->getChildren()) {
|
||||||
|
line += child->getName() + ", ";
|
||||||
|
}
|
||||||
|
result.push_back(line);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
std::vector<std::string> Network::graph(const std::string& title) const
|
||||||
|
{
|
||||||
|
auto output = std::vector<std::string>();
|
||||||
|
auto prefix = "digraph BayesNet {\nlabel=<BayesNet ";
|
||||||
|
auto suffix = ">\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n";
|
||||||
|
std::string header = prefix + title + suffix;
|
||||||
|
output.push_back(header);
|
||||||
|
for (auto& node : nodes) {
|
||||||
|
auto result = node.second->graph(className);
|
||||||
|
output.insert(output.end(), result.begin(), result.end());
|
||||||
|
}
|
||||||
|
output.push_back("}\n");
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
std::vector<std::pair<std::string, std::string>> Network::getEdges() const
|
||||||
|
{
|
||||||
|
auto edges = std::vector<std::pair<std::string, std::string>>();
|
||||||
|
for (const auto& node : nodes) {
|
||||||
|
auto head = node.first;
|
||||||
|
for (const auto& child : node.second->getChildren()) {
|
||||||
|
auto tail = child->getName();
|
||||||
|
edges.push_back({ head, tail });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return edges;
|
||||||
|
}
|
||||||
|
int Network::getNumEdges() const
|
||||||
|
{
|
||||||
|
return getEdges().size();
|
||||||
|
}
|
||||||
|
std::vector<std::string> Network::topological_sort()
|
||||||
|
{
|
||||||
|
/* Check if al the fathers of every node are before the node */
|
||||||
|
auto result = features;
|
||||||
|
result.erase(remove(result.begin(), result.end(), className), result.end());
|
||||||
|
bool ending{ false };
|
||||||
|
while (!ending) {
|
||||||
|
ending = true;
|
||||||
|
for (auto feature : features) {
|
||||||
|
auto fathers = nodes[feature]->getParents();
|
||||||
|
for (const auto& father : fathers) {
|
||||||
|
auto fatherName = father->getName();
|
||||||
|
if (fatherName == className) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Check if father is placed before the actual feature
|
||||||
|
auto it = find(result.begin(), result.end(), fatherName);
|
||||||
|
if (it != result.end()) {
|
||||||
|
auto it2 = find(result.begin(), result.end(), feature);
|
||||||
|
if (it2 != result.end()) {
|
||||||
|
if (distance(it, it2) < 0) {
|
||||||
|
// if it is not, insert it before the feature
|
||||||
|
result.erase(remove(result.begin(), result.end(), fatherName), result.end());
|
||||||
|
result.insert(it2, fatherName);
|
||||||
|
ending = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw std::logic_error("Error in topological sort because of node " + feature + " is not in result");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw std::logic_error("Error in topological sort because of node father " + fatherName + " is not in result");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
void Network::dump_cpt() const
|
||||||
|
{
|
||||||
|
for (auto& node : nodes) {
|
||||||
|
std::cout << "* " << node.first << ": (" << node.second->getNumStates() << ") : " << node.second->getCPT().sizes() << std::endl;
|
||||||
|
std::cout << node.second->getCPT() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
62
src/BayesNet/Network.h
Normal file
62
src/BayesNet/Network.h
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
#ifndef NETWORK_H
|
||||||
|
#define NETWORK_H
|
||||||
|
#include "Node.h"
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class Network {
|
||||||
|
private:
|
||||||
|
std::map<std::string, std::unique_ptr<Node>> nodes;
|
||||||
|
bool fitted;
|
||||||
|
float maxThreads = 0.95;
|
||||||
|
int classNumStates;
|
||||||
|
std::vector<std::string> features; // Including classname
|
||||||
|
std::string className;
|
||||||
|
double laplaceSmoothing;
|
||||||
|
torch::Tensor samples; // nxm tensor used to fit the model
|
||||||
|
bool isCyclic(const std::string&, std::unordered_set<std::string>&, std::unordered_set<std::string>&);
|
||||||
|
std::vector<double> predict_sample(const std::vector<int>&);
|
||||||
|
std::vector<double> predict_sample(const torch::Tensor&);
|
||||||
|
std::vector<double> exactInference(std::map<std::string, int>&);
|
||||||
|
double computeFactor(std::map<std::string, int>&);
|
||||||
|
void completeFit(const std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights);
|
||||||
|
void checkFitData(int n_features, int n_samples, int n_samples_y, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights);
|
||||||
|
void setStates(const std::map<std::string, std::vector<int>>&);
|
||||||
|
public:
|
||||||
|
Network();
|
||||||
|
explicit Network(float);
|
||||||
|
explicit Network(Network&);
|
||||||
|
~Network() = default;
|
||||||
|
torch::Tensor& getSamples();
|
||||||
|
float getmaxThreads();
|
||||||
|
void addNode(const std::string&);
|
||||||
|
void addEdge(const std::string&, const std::string&);
|
||||||
|
std::map<std::string, std::unique_ptr<Node>>& getNodes();
|
||||||
|
std::vector<std::string> getFeatures() const;
|
||||||
|
int getStates() const;
|
||||||
|
std::vector<std::pair<std::string, std::string>> getEdges() const;
|
||||||
|
int getNumEdges() const;
|
||||||
|
int getClassNumStates() const;
|
||||||
|
std::string getClassName() const;
|
||||||
|
/*
|
||||||
|
Notice: Nodes have to be inserted in the same order as they are in the dataset, i.e., first node is first column and so on.
|
||||||
|
*/
|
||||||
|
void fit(const std::vector<std::vector<int>>& input_data, const std::vector<int>& labels, const std::vector<double>& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states);
|
||||||
|
void fit(const torch::Tensor& X, const torch::Tensor& y, const torch::Tensor& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states);
|
||||||
|
void fit(const torch::Tensor& samples, const torch::Tensor& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states);
|
||||||
|
std::vector<int> predict(const std::vector<std::vector<int>>&); // Return mx1 std::vector of predictions
|
||||||
|
torch::Tensor predict(const torch::Tensor&); // Return mx1 tensor of predictions
|
||||||
|
torch::Tensor predict_tensor(const torch::Tensor& samples, const bool proba);
|
||||||
|
std::vector<std::vector<double>> predict_proba(const std::vector<std::vector<int>>&); // Return mxn std::vector of probabilities
|
||||||
|
torch::Tensor predict_proba(const torch::Tensor&); // Return mxn tensor of probabilities
|
||||||
|
double score(const std::vector<std::vector<int>>&, const std::vector<int>&);
|
||||||
|
std::vector<std::string> topological_sort();
|
||||||
|
std::vector<std::string> show() const;
|
||||||
|
std::vector<std::string> graph(const std::string& title) const; // Returns a std::vector of std::strings representing the graph in graphviz format
|
||||||
|
void initialize();
|
||||||
|
void dump_cpt() const;
|
||||||
|
inline std::string version() { return "0.2.0"; }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
135
src/BayesNet/Node.cc
Normal file
135
src/BayesNet/Node.cc
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
#include "Node.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
|
||||||
|
Node::Node(const std::string& name)
|
||||||
|
: name(name), numStates(0), cpTable(torch::Tensor()), parents(std::vector<Node*>()), children(std::vector<Node*>())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void Node::clear()
|
||||||
|
{
|
||||||
|
parents.clear();
|
||||||
|
children.clear();
|
||||||
|
cpTable = torch::Tensor();
|
||||||
|
dimensions.clear();
|
||||||
|
numStates = 0;
|
||||||
|
}
|
||||||
|
std::string Node::getName() const
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
void Node::addParent(Node* parent)
|
||||||
|
{
|
||||||
|
parents.push_back(parent);
|
||||||
|
}
|
||||||
|
void Node::removeParent(Node* parent)
|
||||||
|
{
|
||||||
|
parents.erase(std::remove(parents.begin(), parents.end(), parent), parents.end());
|
||||||
|
}
|
||||||
|
void Node::removeChild(Node* child)
|
||||||
|
{
|
||||||
|
children.erase(std::remove(children.begin(), children.end(), child), children.end());
|
||||||
|
}
|
||||||
|
void Node::addChild(Node* child)
|
||||||
|
{
|
||||||
|
children.push_back(child);
|
||||||
|
}
|
||||||
|
std::vector<Node*>& Node::getParents()
|
||||||
|
{
|
||||||
|
return parents;
|
||||||
|
}
|
||||||
|
std::vector<Node*>& Node::getChildren()
|
||||||
|
{
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
int Node::getNumStates() const
|
||||||
|
{
|
||||||
|
return numStates;
|
||||||
|
}
|
||||||
|
void Node::setNumStates(int numStates)
|
||||||
|
{
|
||||||
|
this->numStates = numStates;
|
||||||
|
}
|
||||||
|
torch::Tensor& Node::getCPT()
|
||||||
|
{
|
||||||
|
return cpTable;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
The MinFill criterion is a heuristic for variable elimination.
|
||||||
|
The variable that minimizes the number of edges that need to be added to the graph to make it triangulated.
|
||||||
|
This is done by counting the number of edges that need to be added to the graph if the variable is eliminated.
|
||||||
|
The variable with the minimum number of edges is chosen.
|
||||||
|
Here this is done computing the length of the combinations of the node neighbors taken 2 by 2.
|
||||||
|
*/
|
||||||
|
unsigned Node::minFill()
|
||||||
|
{
|
||||||
|
std::unordered_set<std::string> neighbors;
|
||||||
|
for (auto child : children) {
|
||||||
|
neighbors.emplace(child->getName());
|
||||||
|
}
|
||||||
|
for (auto parent : parents) {
|
||||||
|
neighbors.emplace(parent->getName());
|
||||||
|
}
|
||||||
|
auto source = std::vector<std::string>(neighbors.begin(), neighbors.end());
|
||||||
|
return combinations(source).size();
|
||||||
|
}
|
||||||
|
std::vector<std::pair<std::string, std::string>> Node::combinations(const std::vector<std::string>& source)
|
||||||
|
{
|
||||||
|
std::vector<std::pair<std::string, std::string>> result;
|
||||||
|
for (int i = 0; i < source.size(); ++i) {
|
||||||
|
std::string temp = source[i];
|
||||||
|
for (int j = i + 1; j < source.size(); ++j) {
|
||||||
|
result.push_back({ temp, source[j] });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
void Node::computeCPT(const torch::Tensor& dataset, const std::vector<std::string>& features, const double laplaceSmoothing, const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
dimensions.clear();
|
||||||
|
// Get dimensions of the CPT
|
||||||
|
dimensions.push_back(numStates);
|
||||||
|
transform(parents.begin(), parents.end(), back_inserter(dimensions), [](const auto& parent) { return parent->getNumStates(); });
|
||||||
|
|
||||||
|
// Create a tensor of zeros with the dimensions of the CPT
|
||||||
|
cpTable = torch::zeros(dimensions, torch::kFloat) + laplaceSmoothing;
|
||||||
|
// Fill table with counts
|
||||||
|
auto pos = find(features.begin(), features.end(), name);
|
||||||
|
if (pos == features.end()) {
|
||||||
|
throw std::logic_error("Feature " + name + " not found in dataset");
|
||||||
|
}
|
||||||
|
int name_index = pos - features.begin();
|
||||||
|
for (int n_sample = 0; n_sample < dataset.size(1); ++n_sample) {
|
||||||
|
c10::List<c10::optional<at::Tensor>> coordinates;
|
||||||
|
coordinates.push_back(dataset.index({ name_index, n_sample }));
|
||||||
|
for (auto parent : parents) {
|
||||||
|
pos = find(features.begin(), features.end(), parent->getName());
|
||||||
|
if (pos == features.end()) {
|
||||||
|
throw std::logic_error("Feature parent " + parent->getName() + " not found in dataset");
|
||||||
|
}
|
||||||
|
int parent_index = pos - features.begin();
|
||||||
|
coordinates.push_back(dataset.index({ parent_index, n_sample }));
|
||||||
|
}
|
||||||
|
// Increment the count of the corresponding coordinate
|
||||||
|
cpTable.index_put_({ coordinates }, cpTable.index({ coordinates }) + weights.index({ n_sample }).item<double>());
|
||||||
|
}
|
||||||
|
// Normalize the counts
|
||||||
|
cpTable = cpTable / cpTable.sum(0);
|
||||||
|
}
|
||||||
|
float Node::getFactorValue(std::map<std::string, int>& evidence)
|
||||||
|
{
|
||||||
|
c10::List<c10::optional<at::Tensor>> coordinates;
|
||||||
|
// following predetermined order of indices in the cpTable (see Node.h)
|
||||||
|
coordinates.push_back(at::tensor(evidence[name]));
|
||||||
|
transform(parents.begin(), parents.end(), std::back_inserter(coordinates), [&evidence](const auto& parent) { return at::tensor(evidence[parent->getName()]); });
|
||||||
|
return cpTable.index({ coordinates }).item<float>();
|
||||||
|
}
|
||||||
|
std::vector<std::string> Node::graph(const std::string& className)
|
||||||
|
{
|
||||||
|
auto output = std::vector<std::string>();
|
||||||
|
auto suffix = name == className ? ", fontcolor=red, fillcolor=lightblue, style=filled " : "";
|
||||||
|
output.push_back(name + " [shape=circle" + suffix + "] \n");
|
||||||
|
transform(children.begin(), children.end(), back_inserter(output), [this](const auto& child) { return name + " -> " + child->getName(); });
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
36
src/BayesNet/Node.h
Normal file
36
src/BayesNet/Node.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef NODE_H
|
||||||
|
#define NODE_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
namespace bayesnet {
|
||||||
|
class Node {
|
||||||
|
private:
|
||||||
|
std::string name;
|
||||||
|
std::vector<Node*> parents;
|
||||||
|
std::vector<Node*> children;
|
||||||
|
int numStates; // number of states of the variable
|
||||||
|
torch::Tensor cpTable; // Order of indices is 0-> node variable, 1-> 1st parent, 2-> 2nd parent, ...
|
||||||
|
std::vector<int64_t> dimensions; // dimensions of the cpTable
|
||||||
|
std::vector<std::pair<std::string, std::string>> combinations(const std::vector<std::string>&);
|
||||||
|
public:
|
||||||
|
explicit Node(const std::string&);
|
||||||
|
void clear();
|
||||||
|
void addParent(Node*);
|
||||||
|
void addChild(Node*);
|
||||||
|
void removeParent(Node*);
|
||||||
|
void removeChild(Node*);
|
||||||
|
std::string getName() const;
|
||||||
|
std::vector<Node*>& getParents();
|
||||||
|
std::vector<Node*>& getChildren();
|
||||||
|
torch::Tensor& getCPT();
|
||||||
|
void computeCPT(const torch::Tensor& dataset, const std::vector<std::string>& features, const double laplaceSmoothing, const torch::Tensor& weights);
|
||||||
|
int getNumStates() const;
|
||||||
|
void setNumStates(int);
|
||||||
|
unsigned minFill();
|
||||||
|
std::vector<std::string> graph(const std::string& clasName); // Returns a std::vector of std::strings representing the graph in graphviz format
|
||||||
|
float getFactorValue(std::map<std::string, int>&);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
110
src/BayesNet/Proposal.cc
Normal file
110
src/BayesNet/Proposal.cc
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
#include "Proposal.h"
|
||||||
|
#include "ArffFiles.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
Proposal::Proposal(torch::Tensor& dataset_, std::vector<std::string>& features_, std::string& className_) : pDataset(dataset_), pFeatures(features_), pClassName(className_) {}
|
||||||
|
Proposal::~Proposal()
|
||||||
|
{
|
||||||
|
for (auto& [key, value] : discretizers) {
|
||||||
|
delete value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Proposal::checkInput(const torch::Tensor& X, const torch::Tensor& y)
|
||||||
|
{
|
||||||
|
if (!torch::is_floating_point(X)) {
|
||||||
|
throw std::invalid_argument("X must be a floating point tensor");
|
||||||
|
}
|
||||||
|
if (torch::is_floating_point(y)) {
|
||||||
|
throw std::invalid_argument("y must be an integer tensor");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
map<std::string, std::vector<int>> Proposal::localDiscretizationProposal(const map<std::string, std::vector<int>>& oldStates, Network& model)
|
||||||
|
{
|
||||||
|
// order of local discretization is important. no good 0, 1, 2...
|
||||||
|
// although we rediscretize features after the local discretization of every feature
|
||||||
|
auto order = model.topological_sort();
|
||||||
|
auto& nodes = model.getNodes();
|
||||||
|
map<std::string, std::vector<int>> states = oldStates;
|
||||||
|
std::vector<int> indicesToReDiscretize;
|
||||||
|
bool upgrade = false; // Flag to check if we need to upgrade the model
|
||||||
|
for (auto feature : order) {
|
||||||
|
auto nodeParents = nodes[feature]->getParents();
|
||||||
|
if (nodeParents.size() < 2) continue; // Only has class as parent
|
||||||
|
upgrade = true;
|
||||||
|
int index = find(pFeatures.begin(), pFeatures.end(), feature) - pFeatures.begin();
|
||||||
|
indicesToReDiscretize.push_back(index); // We need to re-discretize this feature
|
||||||
|
std::vector<std::string> parents;
|
||||||
|
transform(nodeParents.begin(), nodeParents.end(), back_inserter(parents), [](const auto& p) { return p->getName(); });
|
||||||
|
// Remove class as parent as it will be added later
|
||||||
|
parents.erase(remove(parents.begin(), parents.end(), pClassName), parents.end());
|
||||||
|
// Get the indices of the parents
|
||||||
|
std::vector<int> indices;
|
||||||
|
indices.push_back(-1); // Add class index
|
||||||
|
transform(parents.begin(), parents.end(), back_inserter(indices), [&](const auto& p) {return find(pFeatures.begin(), pFeatures.end(), p) - pFeatures.begin(); });
|
||||||
|
// Now we fit the discretizer of the feature, conditioned on its parents and the class i.e. discretizer.fit(X[index], X[indices] + y)
|
||||||
|
std::vector<std::string> yJoinParents(Xf.size(1));
|
||||||
|
for (auto idx : indices) {
|
||||||
|
for (int i = 0; i < Xf.size(1); ++i) {
|
||||||
|
yJoinParents[i] += to_string(pDataset.index({ idx, i }).item<int>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto arff = ArffFiles();
|
||||||
|
auto yxv = arff.factorize(yJoinParents);
|
||||||
|
auto xvf_ptr = Xf.index({ index }).data_ptr<float>();
|
||||||
|
auto xvf = std::vector<mdlp::precision_t>(xvf_ptr, xvf_ptr + Xf.size(1));
|
||||||
|
discretizers[feature]->fit(xvf, yxv);
|
||||||
|
}
|
||||||
|
if (upgrade) {
|
||||||
|
// Discretize again X (only the affected indices) with the new fitted discretizers
|
||||||
|
for (auto index : indicesToReDiscretize) {
|
||||||
|
auto Xt_ptr = Xf.index({ index }).data_ptr<float>();
|
||||||
|
auto Xt = std::vector<float>(Xt_ptr, Xt_ptr + Xf.size(1));
|
||||||
|
pDataset.index_put_({ index, "..." }, torch::tensor(discretizers[pFeatures[index]]->transform(Xt)));
|
||||||
|
auto xStates = std::vector<int>(discretizers[pFeatures[index]]->getCutPoints().size() + 1);
|
||||||
|
iota(xStates.begin(), xStates.end(), 0);
|
||||||
|
//Update new states of the feature/node
|
||||||
|
states[pFeatures[index]] = xStates;
|
||||||
|
}
|
||||||
|
const torch::Tensor weights = torch::full({ pDataset.size(1) }, 1.0 / pDataset.size(1), torch::kDouble);
|
||||||
|
model.fit(pDataset, weights, pFeatures, pClassName, states);
|
||||||
|
}
|
||||||
|
return states;
|
||||||
|
}
|
||||||
|
map<std::string, std::vector<int>> Proposal::fit_local_discretization(const torch::Tensor& y)
|
||||||
|
{
|
||||||
|
// Discretize the continuous input data and build pDataset (Classifier::dataset)
|
||||||
|
int m = Xf.size(1);
|
||||||
|
int n = Xf.size(0);
|
||||||
|
map<std::string, std::vector<int>> states;
|
||||||
|
pDataset = torch::zeros({ n + 1, m }, torch::kInt32);
|
||||||
|
auto yv = std::vector<int>(y.data_ptr<int>(), y.data_ptr<int>() + y.size(0));
|
||||||
|
// discretize input data by feature(row)
|
||||||
|
for (auto i = 0; i < pFeatures.size(); ++i) {
|
||||||
|
auto* discretizer = new mdlp::CPPFImdlp();
|
||||||
|
auto Xt_ptr = Xf.index({ i }).data_ptr<float>();
|
||||||
|
auto Xt = std::vector<float>(Xt_ptr, Xt_ptr + Xf.size(1));
|
||||||
|
discretizer->fit(Xt, yv);
|
||||||
|
pDataset.index_put_({ i, "..." }, torch::tensor(discretizer->transform(Xt)));
|
||||||
|
auto xStates = std::vector<int>(discretizer->getCutPoints().size() + 1);
|
||||||
|
iota(xStates.begin(), xStates.end(), 0);
|
||||||
|
states[pFeatures[i]] = xStates;
|
||||||
|
discretizers[pFeatures[i]] = discretizer;
|
||||||
|
}
|
||||||
|
int n_classes = torch::max(y).item<int>() + 1;
|
||||||
|
auto yStates = std::vector<int>(n_classes);
|
||||||
|
iota(yStates.begin(), yStates.end(), 0);
|
||||||
|
states[pClassName] = yStates;
|
||||||
|
pDataset.index_put_({ n, "..." }, y);
|
||||||
|
return states;
|
||||||
|
}
|
||||||
|
torch::Tensor Proposal::prepareX(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
auto Xtd = torch::zeros_like(X, torch::kInt32);
|
||||||
|
for (int i = 0; i < X.size(0); ++i) {
|
||||||
|
auto Xt = std::vector<float>(X[i].data_ptr<float>(), X[i].data_ptr<float>() + X.size(1));
|
||||||
|
auto Xd = discretizers[pFeatures[i]]->transform(Xt);
|
||||||
|
Xtd.index_put_({ i }, torch::tensor(Xd, torch::kInt32));
|
||||||
|
}
|
||||||
|
return Xtd;
|
||||||
|
}
|
||||||
|
}
|
30
src/BayesNet/Proposal.h
Normal file
30
src/BayesNet/Proposal.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef PROPOSAL_H
|
||||||
|
#define PROPOSAL_H
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include "Network.h"
|
||||||
|
#include "CPPFImdlp.h"
|
||||||
|
#include "Classifier.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class Proposal {
|
||||||
|
public:
|
||||||
|
Proposal(torch::Tensor& pDataset, std::vector<std::string>& features_, std::string& className_);
|
||||||
|
virtual ~Proposal();
|
||||||
|
protected:
|
||||||
|
void checkInput(const torch::Tensor& X, const torch::Tensor& y);
|
||||||
|
torch::Tensor prepareX(torch::Tensor& X);
|
||||||
|
map<std::string, std::vector<int>> localDiscretizationProposal(const map<std::string, std::vector<int>>& states, Network& model);
|
||||||
|
map<std::string, std::vector<int>> fit_local_discretization(const torch::Tensor& y);
|
||||||
|
torch::Tensor Xf; // X continuous nxm tensor
|
||||||
|
torch::Tensor y; // y discrete nx1 tensor
|
||||||
|
map<std::string, mdlp::CPPFImdlp*> discretizers;
|
||||||
|
private:
|
||||||
|
torch::Tensor& pDataset; // (n+1)xm tensor
|
||||||
|
std::vector<std::string>& pFeatures;
|
||||||
|
std::string& pClassName;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
|
|
||||||
SPODE::SPODE(int root) : BaseClassifier(Network()), root(root) {}
|
SPODE::SPODE(int root) : Classifier(Network()), root(root) {}
|
||||||
|
|
||||||
void SPODE::train()
|
void SPODE::buildModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
// 0. Add all nodes to the model
|
// 0. Add all nodes to the model
|
||||||
addNodes();
|
addNodes();
|
||||||
@@ -17,7 +17,7 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vector<string> SPODE::graph(string name )
|
std::vector<std::string> SPODE::graph(const std::string& name) const
|
||||||
{
|
{
|
||||||
return model.graph(name);
|
return model.graph(name);
|
||||||
}
|
}
|
17
src/BayesNet/SPODE.h
Normal file
17
src/BayesNet/SPODE.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef SPODE_H
|
||||||
|
#define SPODE_H
|
||||||
|
#include "Classifier.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class SPODE : public Classifier {
|
||||||
|
private:
|
||||||
|
int root;
|
||||||
|
protected:
|
||||||
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
|
public:
|
||||||
|
explicit SPODE(int root);
|
||||||
|
virtual ~SPODE() = default;
|
||||||
|
std::vector<std::string> graph(const std::string& name = "SPODE") const override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
47
src/BayesNet/SPODELd.cc
Normal file
47
src/BayesNet/SPODELd.cc
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "SPODELd.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
SPODELd::SPODELd(int root) : SPODE(root), Proposal(dataset, features, className) {}
|
||||||
|
SPODELd& SPODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
|
||||||
|
{
|
||||||
|
checkInput(X_, y_);
|
||||||
|
features = features_;
|
||||||
|
className = className_;
|
||||||
|
Xf = X_;
|
||||||
|
y = y_;
|
||||||
|
// Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
|
||||||
|
states = fit_local_discretization(y);
|
||||||
|
// We have discretized the input data
|
||||||
|
// 1st we need to fit the model to build the normal SPODE structure, SPODE::fit initializes the base Bayesian network
|
||||||
|
SPODE::fit(dataset, features, className, states);
|
||||||
|
states = localDiscretizationProposal(states, model);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
SPODELd& SPODELd::fit(torch::Tensor& dataset, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
|
||||||
|
{
|
||||||
|
if (!torch::is_floating_point(dataset)) {
|
||||||
|
throw std::runtime_error("Dataset must be a floating point tensor");
|
||||||
|
}
|
||||||
|
Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
|
||||||
|
y = dataset.index({ -1, "..." }).clone();
|
||||||
|
features = features_;
|
||||||
|
className = className_;
|
||||||
|
// Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
|
||||||
|
states = fit_local_discretization(y);
|
||||||
|
// We have discretized the input data
|
||||||
|
// 1st we need to fit the model to build the normal SPODE structure, SPODE::fit initializes the base Bayesian network
|
||||||
|
SPODE::fit(dataset, features, className, states);
|
||||||
|
states = localDiscretizationProposal(states, model);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
torch::Tensor SPODELd::predict(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
auto Xt = prepareX(X);
|
||||||
|
return SPODE::predict(Xt);
|
||||||
|
}
|
||||||
|
std::vector<std::string> SPODELd::graph(const std::string& name) const
|
||||||
|
{
|
||||||
|
return SPODE::graph(name);
|
||||||
|
}
|
||||||
|
}
|
18
src/BayesNet/SPODELd.h
Normal file
18
src/BayesNet/SPODELd.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef SPODELD_H
|
||||||
|
#define SPODELD_H
|
||||||
|
#include "SPODE.h"
|
||||||
|
#include "Proposal.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class SPODELd : public SPODE, public Proposal {
|
||||||
|
public:
|
||||||
|
explicit SPODELd(int root);
|
||||||
|
virtual ~SPODELd() = default;
|
||||||
|
SPODELd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states) override;
|
||||||
|
SPODELd& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states) override;
|
||||||
|
std::vector<std::string> graph(const std::string& name = "SPODE") const override;
|
||||||
|
torch::Tensor predict(torch::Tensor& X) override;
|
||||||
|
static inline std::string version() { return "0.0.1"; };
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif // !SPODELD_H
|
@@ -1,30 +1,27 @@
|
|||||||
#include "TAN.h"
|
#include "TAN.h"
|
||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
using namespace std;
|
TAN::TAN() : Classifier(Network()) {}
|
||||||
using namespace torch;
|
|
||||||
|
|
||||||
TAN::TAN() : BaseClassifier(Network()) {}
|
void TAN::buildModel(const torch::Tensor& weights)
|
||||||
|
|
||||||
void TAN::train()
|
|
||||||
{
|
{
|
||||||
// 0. Add all nodes to the model
|
// 0. Add all nodes to the model
|
||||||
addNodes();
|
addNodes();
|
||||||
// 1. Compute mutual information between each feature and the class and set the root node
|
// 1. Compute mutual information between each feature and the class and set the root node
|
||||||
// as the highest mutual information with the class
|
// as the highest mutual information with the class
|
||||||
auto mi = vector <pair<int, float >>();
|
auto mi = std::vector <std::pair<int, float >>();
|
||||||
Tensor class_dataset = dataset.index({ "...", -1 });
|
torch::Tensor class_dataset = dataset.index({ -1, "..." });
|
||||||
for (int i = 0; i < static_cast<int>(features.size()); ++i) {
|
for (int i = 0; i < static_cast<int>(features.size()); ++i) {
|
||||||
Tensor feature_dataset = dataset.index({ "...", i });
|
torch::Tensor feature_dataset = dataset.index({ i, "..." });
|
||||||
auto mi_value = metrics.mutualInformation(class_dataset, feature_dataset);
|
auto mi_value = metrics.mutualInformation(class_dataset, feature_dataset, weights);
|
||||||
mi.push_back({ i, mi_value });
|
mi.push_back({ i, mi_value });
|
||||||
}
|
}
|
||||||
sort(mi.begin(), mi.end(), [](auto& left, auto& right) {return left.second < right.second;});
|
sort(mi.begin(), mi.end(), [](const auto& left, const auto& right) {return left.second < right.second;});
|
||||||
auto root = mi[mi.size() - 1].first;
|
auto root = mi[mi.size() - 1].first;
|
||||||
// 2. Compute mutual information between each feature and the class
|
// 2. Compute mutual information between each feature and the class
|
||||||
auto weights = metrics.conditionalEdge();
|
auto weights_matrix = metrics.conditionalEdge(weights);
|
||||||
// 3. Compute the maximum spanning tree
|
// 3. Compute the maximum spanning tree
|
||||||
auto mst = metrics.maximumSpanningTree(features, weights, root);
|
auto mst = metrics.maximumSpanningTree(features, weights_matrix, root);
|
||||||
// 4. Add edges from the maximum spanning tree to the model
|
// 4. Add edges from the maximum spanning tree to the model
|
||||||
for (auto i = 0; i < mst.size(); ++i) {
|
for (auto i = 0; i < mst.size(); ++i) {
|
||||||
auto [from, to] = mst[i];
|
auto [from, to] = mst[i];
|
||||||
@@ -35,7 +32,7 @@ namespace bayesnet {
|
|||||||
model.addEdge(className, feature);
|
model.addEdge(className, feature);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vector<string> TAN::graph(string title)
|
std::vector<std::string> TAN::graph(const std::string& title) const
|
||||||
{
|
{
|
||||||
return model.graph(title);
|
return model.graph(title);
|
||||||
}
|
}
|
15
src/BayesNet/TAN.h
Normal file
15
src/BayesNet/TAN.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef TAN_H
|
||||||
|
#define TAN_H
|
||||||
|
#include "Classifier.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
class TAN : public Classifier {
|
||||||
|
private:
|
||||||
|
protected:
|
||||||
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
|
public:
|
||||||
|
TAN();
|
||||||
|
virtual ~TAN() = default;
|
||||||
|
std::vector<std::string> graph(const std::string& name = "TAN") const override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
30
src/BayesNet/TANLd.cc
Normal file
30
src/BayesNet/TANLd.cc
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include "TANLd.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
TANLd::TANLd() : TAN(), Proposal(dataset, features, className) {}
|
||||||
|
TANLd& TANLd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
|
||||||
|
{
|
||||||
|
checkInput(X_, y_);
|
||||||
|
features = features_;
|
||||||
|
className = className_;
|
||||||
|
Xf = X_;
|
||||||
|
y = y_;
|
||||||
|
// Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
|
||||||
|
states = fit_local_discretization(y);
|
||||||
|
// We have discretized the input data
|
||||||
|
// 1st we need to fit the model to build the normal TAN structure, TAN::fit initializes the base Bayesian network
|
||||||
|
TAN::fit(dataset, features, className, states);
|
||||||
|
states = localDiscretizationProposal(states, model);
|
||||||
|
return *this;
|
||||||
|
|
||||||
|
}
|
||||||
|
torch::Tensor TANLd::predict(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
auto Xt = prepareX(X);
|
||||||
|
return TAN::predict(Xt);
|
||||||
|
}
|
||||||
|
std::vector<std::string> TANLd::graph(const std::string& name) const
|
||||||
|
{
|
||||||
|
return TAN::graph(name);
|
||||||
|
}
|
||||||
|
}
|
18
src/BayesNet/TANLd.h
Normal file
18
src/BayesNet/TANLd.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef TANLD_H
|
||||||
|
#define TANLD_H
|
||||||
|
#include "TAN.h"
|
||||||
|
#include "Proposal.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class TANLd : public TAN, public Proposal {
|
||||||
|
private:
|
||||||
|
public:
|
||||||
|
TANLd();
|
||||||
|
virtual ~TANLd() = default;
|
||||||
|
TANLd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states) override;
|
||||||
|
std::vector<std::string> graph(const std::string& name = "TAN") const override;
|
||||||
|
torch::Tensor predict(torch::Tensor& X) override;
|
||||||
|
static inline std::string version() { return "0.0.1"; };
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif // !TANLD_H
|
25
src/BayesNet/bayesnetUtils.cc
Normal file
25
src/BayesNet/bayesnetUtils.cc
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
#include "bayesnetUtils.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
// Return the indices in descending order
|
||||||
|
std::vector<int> argsort(std::vector<double>& nums)
|
||||||
|
{
|
||||||
|
int n = nums.size();
|
||||||
|
std::vector<int> indices(n);
|
||||||
|
iota(indices.begin(), indices.end(), 0);
|
||||||
|
sort(indices.begin(), indices.end(), [&nums](int i, int j) {return nums[i] > nums[j];});
|
||||||
|
return indices;
|
||||||
|
}
|
||||||
|
std::vector<std::vector<int>> tensorToVector(torch::Tensor& tensor)
|
||||||
|
{
|
||||||
|
// convert mxn tensor to nxm std::vector
|
||||||
|
std::vector<std::vector<int>> result;
|
||||||
|
// Iterate over cols
|
||||||
|
for (int i = 0; i < tensor.size(1); ++i) {
|
||||||
|
auto col_tensor = tensor.index({ "...", i });
|
||||||
|
auto col = std::vector<int>(col_tensor.data_ptr<int>(), col_tensor.data_ptr<int>() + tensor.size(0));
|
||||||
|
result.push_back(col);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
9
src/BayesNet/bayesnetUtils.h
Normal file
9
src/BayesNet/bayesnetUtils.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef BAYESNET_UTILS_H
|
||||||
|
#define BAYESNET_UTILS_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include <vector>
|
||||||
|
namespace bayesnet {
|
||||||
|
std::vector<int> argsort(std::vector<double>& nums);
|
||||||
|
std::vector<std::vector<int>> tensorToVector(torch::Tensor& tensor);
|
||||||
|
}
|
||||||
|
#endif //BAYESNET_UTILS_H
|
@@ -1,2 +0,0 @@
|
|||||||
add_library(BayesNet utils.cc Network.cc Node.cc Metrics.cc BaseClassifier.cc KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc Mst.cc)
|
|
||||||
target_link_libraries(BayesNet "${TORCH_LIBRARIES}")
|
|
112
src/Ensemble.cc
112
src/Ensemble.cc
@@ -1,112 +0,0 @@
|
|||||||
#include "Ensemble.h"
|
|
||||||
|
|
||||||
namespace bayesnet {
|
|
||||||
using namespace std;
|
|
||||||
using namespace torch;
|
|
||||||
|
|
||||||
Ensemble::Ensemble() : m(0), n(0), n_models(0), metrics(Metrics()), fitted(false) {}
|
|
||||||
Ensemble& Ensemble::build(vector<string>& features, string className, map<string, vector<int>>& states)
|
|
||||||
{
|
|
||||||
dataset = cat({ X, y.view({y.size(0), 1}) }, 1);
|
|
||||||
this->features = features;
|
|
||||||
this->className = className;
|
|
||||||
this->states = states;
|
|
||||||
auto n_classes = states[className].size();
|
|
||||||
metrics = Metrics(dataset, features, className, n_classes);
|
|
||||||
// Build models
|
|
||||||
train();
|
|
||||||
// Train models
|
|
||||||
n_models = models.size();
|
|
||||||
for (auto i = 0; i < n_models; ++i) {
|
|
||||||
models[i]->fit(Xv, yv, features, className, states);
|
|
||||||
}
|
|
||||||
fitted = true;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Ensemble& Ensemble::fit(vector<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states)
|
|
||||||
{
|
|
||||||
this->X = torch::zeros({ static_cast<int64_t>(X[0].size()), static_cast<int64_t>(X.size()) }, kInt64);
|
|
||||||
Xv = X;
|
|
||||||
for (int i = 0; i < X.size(); ++i) {
|
|
||||||
this->X.index_put_({ "...", i }, torch::tensor(X[i], kInt64));
|
|
||||||
}
|
|
||||||
this->y = torch::tensor(y, kInt64);
|
|
||||||
yv = y;
|
|
||||||
return build(features, className, states);
|
|
||||||
}
|
|
||||||
Tensor Ensemble::predict(Tensor& X)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("Ensemble has not been fitted");
|
|
||||||
}
|
|
||||||
Tensor y_pred = torch::zeros({ X.size(0), n_models }, kInt64);
|
|
||||||
for (auto i = 0; i < n_models; ++i) {
|
|
||||||
y_pred.index_put_({ "...", i }, models[i]->predict(X));
|
|
||||||
}
|
|
||||||
return torch::tensor(voting(y_pred));
|
|
||||||
}
|
|
||||||
vector<int> Ensemble::voting(Tensor& y_pred)
|
|
||||||
{
|
|
||||||
auto y_pred_ = y_pred.accessor<int64_t, 2>();
|
|
||||||
vector<int> y_pred_final;
|
|
||||||
for (int i = 0; i < y_pred.size(0); ++i) {
|
|
||||||
vector<float> votes(states[className].size(), 0);
|
|
||||||
for (int j = 0; j < y_pred.size(1); ++j) {
|
|
||||||
votes[y_pred_[i][j]] += 1;
|
|
||||||
}
|
|
||||||
auto indices = argsort(votes);
|
|
||||||
y_pred_final.push_back(indices[0]);
|
|
||||||
}
|
|
||||||
return y_pred_final;
|
|
||||||
}
|
|
||||||
vector<int> Ensemble::predict(vector<vector<int>>& X)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("Ensemble has not been fitted");
|
|
||||||
}
|
|
||||||
long m_ = X[0].size();
|
|
||||||
long n_ = X.size();
|
|
||||||
vector<vector<int>> Xd(n_, vector<int>(m_, 0));
|
|
||||||
for (auto i = 0; i < n_; i++) {
|
|
||||||
Xd[i] = vector<int>(X[i].begin(), X[i].end());
|
|
||||||
}
|
|
||||||
Tensor y_pred = torch::zeros({ m_, n_models }, kInt64);
|
|
||||||
for (auto i = 0; i < n_models; ++i) {
|
|
||||||
y_pred.index_put_({ "...", i }, torch::tensor(models[i]->predict(Xd), kInt64));
|
|
||||||
}
|
|
||||||
return voting(y_pred);
|
|
||||||
}
|
|
||||||
float Ensemble::score(vector<vector<int>>& X, vector<int>& y)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("Ensemble has not been fitted");
|
|
||||||
}
|
|
||||||
auto y_pred = predict(X);
|
|
||||||
int correct = 0;
|
|
||||||
for (int i = 0; i < y_pred.size(); ++i) {
|
|
||||||
if (y_pred[i] == y[i]) {
|
|
||||||
correct++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (double)correct / y_pred.size();
|
|
||||||
|
|
||||||
}
|
|
||||||
vector<string> Ensemble::show()
|
|
||||||
{
|
|
||||||
auto result = vector<string>();
|
|
||||||
for (auto i = 0; i < n_models; ++i) {
|
|
||||||
auto res = models[i]->show();
|
|
||||||
result.insert(result.end(), res.begin(), res.end());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
vector<string> Ensemble::graph(string title)
|
|
||||||
{
|
|
||||||
auto result = vector<string>();
|
|
||||||
for (auto i = 0; i < n_models; ++i) {
|
|
||||||
auto res = models[i]->graph(title + "_" + to_string(i));
|
|
||||||
result.insert(result.end(), res.begin(), res.end());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,42 +0,0 @@
|
|||||||
#ifndef ENSEMBLE_H
|
|
||||||
#define ENSEMBLE_H
|
|
||||||
#include <torch/torch.h>
|
|
||||||
#include "BaseClassifier.h"
|
|
||||||
#include "Metrics.hpp"
|
|
||||||
#include "utils.h"
|
|
||||||
using namespace std;
|
|
||||||
using namespace torch;
|
|
||||||
|
|
||||||
namespace bayesnet {
|
|
||||||
class Ensemble {
|
|
||||||
private:
|
|
||||||
bool fitted;
|
|
||||||
long n_models;
|
|
||||||
Ensemble& build(vector<string>& features, string className, map<string, vector<int>>& states);
|
|
||||||
protected:
|
|
||||||
vector<unique_ptr<BaseClassifier>> models;
|
|
||||||
int m, n; // m: number of samples, n: number of features
|
|
||||||
Tensor X;
|
|
||||||
vector<vector<int>> Xv;
|
|
||||||
Tensor y;
|
|
||||||
vector<int> yv;
|
|
||||||
Tensor dataset;
|
|
||||||
Metrics metrics;
|
|
||||||
vector<string> features;
|
|
||||||
string className;
|
|
||||||
map<string, vector<int>> states;
|
|
||||||
void virtual train() = 0;
|
|
||||||
vector<int> voting(Tensor& y_pred);
|
|
||||||
public:
|
|
||||||
Ensemble();
|
|
||||||
virtual ~Ensemble() = default;
|
|
||||||
Ensemble& fit(vector<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states);
|
|
||||||
Tensor predict(Tensor& X);
|
|
||||||
vector<int> predict(vector<vector<int>>& X);
|
|
||||||
float score(Tensor& X, Tensor& y);
|
|
||||||
float score(vector<vector<int>>& X, vector<int>& y);
|
|
||||||
vector<string> show();
|
|
||||||
vector<string> graph(string title);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
20
src/KDB.h
20
src/KDB.h
@@ -1,20 +0,0 @@
|
|||||||
#ifndef KDB_H
|
|
||||||
#define KDB_H
|
|
||||||
#include "BaseClassifier.h"
|
|
||||||
#include "utils.h"
|
|
||||||
namespace bayesnet {
|
|
||||||
using namespace std;
|
|
||||||
using namespace torch;
|
|
||||||
class KDB : public BaseClassifier {
|
|
||||||
private:
|
|
||||||
int k;
|
|
||||||
float theta;
|
|
||||||
void add_m_edges(int idx, vector<int>& S, Tensor& weights);
|
|
||||||
protected:
|
|
||||||
void train() override;
|
|
||||||
public:
|
|
||||||
KDB(int k, float theta = 0.03);
|
|
||||||
vector<string> graph(string name = "KDB") override;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
131
src/Metrics.cc
131
src/Metrics.cc
@@ -1,131 +0,0 @@
|
|||||||
#include "Metrics.hpp"
|
|
||||||
#include "Mst.h"
|
|
||||||
using namespace std;
|
|
||||||
namespace bayesnet {
|
|
||||||
Metrics::Metrics(torch::Tensor& samples, vector<string>& features, string& className, int classNumStates)
|
|
||||||
: samples(samples)
|
|
||||||
, features(features)
|
|
||||||
, className(className)
|
|
||||||
, classNumStates(classNumStates)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
Metrics::Metrics(const vector<vector<int>>& vsamples, const vector<int>& labels, const vector<string>& features, const string& className, const int classNumStates)
|
|
||||||
: features(features)
|
|
||||||
, className(className)
|
|
||||||
, classNumStates(classNumStates)
|
|
||||||
{
|
|
||||||
samples = torch::zeros({ static_cast<int64_t>(vsamples[0].size()), static_cast<int64_t>(vsamples.size() + 1) }, torch::kInt64);
|
|
||||||
for (int i = 0; i < vsamples.size(); ++i) {
|
|
||||||
samples.index_put_({ "...", i }, torch::tensor(vsamples[i], torch::kInt64));
|
|
||||||
}
|
|
||||||
samples.index_put_({ "...", -1 }, torch::tensor(labels, torch::kInt64));
|
|
||||||
}
|
|
||||||
vector<pair<string, string>> Metrics::doCombinations(const vector<string>& source)
|
|
||||||
{
|
|
||||||
vector<pair<string, string>> result;
|
|
||||||
for (int i = 0; i < source.size(); ++i) {
|
|
||||||
string temp = source[i];
|
|
||||||
for (int j = i + 1; j < source.size(); ++j) {
|
|
||||||
result.push_back({ temp, source[j] });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
torch::Tensor Metrics::conditionalEdge()
|
|
||||||
{
|
|
||||||
auto result = vector<double>();
|
|
||||||
auto source = vector<string>(features);
|
|
||||||
source.push_back(className);
|
|
||||||
auto combinations = doCombinations(source);
|
|
||||||
// Compute class prior
|
|
||||||
auto margin = torch::zeros({ classNumStates });
|
|
||||||
for (int value = 0; value < classNumStates; ++value) {
|
|
||||||
auto mask = samples.index({ "...", -1 }) == value;
|
|
||||||
margin[value] = mask.sum().item<float>() / samples.sizes()[0];
|
|
||||||
}
|
|
||||||
for (auto [first, second] : combinations) {
|
|
||||||
int64_t index_first = find(features.begin(), features.end(), first) - features.begin();
|
|
||||||
int64_t index_second = find(features.begin(), features.end(), second) - features.begin();
|
|
||||||
double accumulated = 0;
|
|
||||||
for (int value = 0; value < classNumStates; ++value) {
|
|
||||||
auto mask = samples.index({ "...", -1 }) == value;
|
|
||||||
auto first_dataset = samples.index({ mask, index_first });
|
|
||||||
auto second_dataset = samples.index({ mask, index_second });
|
|
||||||
auto mi = mutualInformation(first_dataset, second_dataset);
|
|
||||||
auto pb = margin[value].item<float>();
|
|
||||||
accumulated += pb * mi;
|
|
||||||
}
|
|
||||||
result.push_back(accumulated);
|
|
||||||
}
|
|
||||||
long n_vars = source.size();
|
|
||||||
auto matrix = torch::zeros({ n_vars, n_vars });
|
|
||||||
auto indices = torch::triu_indices(n_vars, n_vars, 1);
|
|
||||||
for (auto i = 0; i < result.size(); ++i) {
|
|
||||||
auto x = indices[0][i];
|
|
||||||
auto y = indices[1][i];
|
|
||||||
matrix[x][y] = result[i];
|
|
||||||
matrix[y][x] = result[i];
|
|
||||||
}
|
|
||||||
return matrix;
|
|
||||||
}
|
|
||||||
vector<float> Metrics::conditionalEdgeWeights()
|
|
||||||
{
|
|
||||||
auto matrix = conditionalEdge();
|
|
||||||
std::vector<float> v(matrix.data_ptr<float>(), matrix.data_ptr<float>() + matrix.numel());
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
double Metrics::entropy(torch::Tensor& feature)
|
|
||||||
{
|
|
||||||
torch::Tensor counts = feature.bincount();
|
|
||||||
int totalWeight = counts.sum().item<int>();
|
|
||||||
torch::Tensor probs = counts.to(torch::kFloat) / totalWeight;
|
|
||||||
torch::Tensor logProbs = torch::log(probs);
|
|
||||||
torch::Tensor entropy = -probs * logProbs;
|
|
||||||
return entropy.nansum().item<double>();
|
|
||||||
}
|
|
||||||
// H(Y|X) = sum_{x in X} p(x) H(Y|X=x)
|
|
||||||
double Metrics::conditionalEntropy(torch::Tensor& firstFeature, torch::Tensor& secondFeature)
|
|
||||||
{
|
|
||||||
int numSamples = firstFeature.sizes()[0];
|
|
||||||
torch::Tensor featureCounts = secondFeature.bincount();
|
|
||||||
unordered_map<int, unordered_map<int, double>> jointCounts;
|
|
||||||
double totalWeight = 0;
|
|
||||||
for (auto i = 0; i < numSamples; i++) {
|
|
||||||
jointCounts[secondFeature[i].item<int>()][firstFeature[i].item<int>()] += 1;
|
|
||||||
totalWeight += 1;
|
|
||||||
}
|
|
||||||
if (totalWeight == 0)
|
|
||||||
throw invalid_argument("Total weight should not be zero");
|
|
||||||
double entropyValue = 0;
|
|
||||||
for (int value = 0; value < featureCounts.sizes()[0]; ++value) {
|
|
||||||
double p_f = featureCounts[value].item<double>() / totalWeight;
|
|
||||||
double entropy_f = 0;
|
|
||||||
for (auto& [label, jointCount] : jointCounts[value]) {
|
|
||||||
double p_l_f = jointCount / featureCounts[value].item<double>();
|
|
||||||
if (p_l_f > 0) {
|
|
||||||
entropy_f -= p_l_f * log(p_l_f);
|
|
||||||
} else {
|
|
||||||
entropy_f = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
entropyValue += p_f * entropy_f;
|
|
||||||
}
|
|
||||||
return entropyValue;
|
|
||||||
}
|
|
||||||
// I(X;Y) = H(Y) - H(Y|X)
|
|
||||||
double Metrics::mutualInformation(torch::Tensor& firstFeature, torch::Tensor& secondFeature)
|
|
||||||
{
|
|
||||||
return entropy(firstFeature) - conditionalEntropy(firstFeature, secondFeature);
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
Compute the maximum spanning tree considering the weights as distances
|
|
||||||
and the indices of the weights as nodes of this square matrix using
|
|
||||||
Kruskal algorithm
|
|
||||||
*/
|
|
||||||
vector<pair<int, int>> Metrics::maximumSpanningTree(vector<string> features, Tensor& weights, int root)
|
|
||||||
{
|
|
||||||
auto result = vector<pair<int, int>>();
|
|
||||||
auto mst = MST(features, weights, root);
|
|
||||||
return mst.maximumSpanningTree();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,28 +0,0 @@
|
|||||||
#ifndef BAYESNET_METRICS_H
|
|
||||||
#define BAYESNET_METRICS_H
|
|
||||||
#include <torch/torch.h>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
namespace bayesnet {
|
|
||||||
using namespace std;
|
|
||||||
using namespace torch;
|
|
||||||
class Metrics {
|
|
||||||
private:
|
|
||||||
Tensor samples;
|
|
||||||
vector<string> features;
|
|
||||||
string className;
|
|
||||||
int classNumStates;
|
|
||||||
public:
|
|
||||||
Metrics() = default;
|
|
||||||
Metrics(Tensor&, vector<string>&, string&, int);
|
|
||||||
Metrics(const vector<vector<int>>&, const vector<int>&, const vector<string>&, const string&, const int);
|
|
||||||
double entropy(Tensor&);
|
|
||||||
double conditionalEntropy(Tensor&, Tensor&);
|
|
||||||
double mutualInformation(Tensor&, Tensor&);
|
|
||||||
vector<float> conditionalEdgeWeights();
|
|
||||||
Tensor conditionalEdge();
|
|
||||||
vector<pair<string, string>> doCombinations(const vector<string>&);
|
|
||||||
vector<pair<int, int>> maximumSpanningTree(vector<string> features, Tensor& weights, int root);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
35
src/Mst.h
35
src/Mst.h
@@ -1,35 +0,0 @@
|
|||||||
#ifndef MST_H
|
|
||||||
#define MST_H
|
|
||||||
#include <torch/torch.h>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
namespace bayesnet {
|
|
||||||
using namespace std;
|
|
||||||
using namespace torch;
|
|
||||||
class MST {
|
|
||||||
private:
|
|
||||||
Tensor weights;
|
|
||||||
vector<string> features;
|
|
||||||
int root;
|
|
||||||
public:
|
|
||||||
MST() = default;
|
|
||||||
MST(vector<string>& features, Tensor& weights, int root);
|
|
||||||
vector<pair<int, int>> maximumSpanningTree();
|
|
||||||
};
|
|
||||||
class Graph {
|
|
||||||
private:
|
|
||||||
int V; // number of nodes in graph
|
|
||||||
vector <pair<float, pair<int, int>>> G; // vector for graph
|
|
||||||
vector <pair<float, pair<int, int>>> T; // vector for mst
|
|
||||||
vector<int> parent;
|
|
||||||
public:
|
|
||||||
Graph(int V);
|
|
||||||
void addEdge(int u, int v, float wt);
|
|
||||||
int find_set(int i);
|
|
||||||
void union_set(int u, int v);
|
|
||||||
void kruskal_algorithm();
|
|
||||||
void display_mst();
|
|
||||||
vector <pair<float, pair<int, int>>> get_mst() { return T; }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
276
src/Network.cc
276
src/Network.cc
@@ -1,276 +0,0 @@
|
|||||||
#include <thread>
|
|
||||||
#include <mutex>
|
|
||||||
#include "Network.h"
|
|
||||||
namespace bayesnet {
|
|
||||||
Network::Network() : laplaceSmoothing(1), features(vector<string>()), className(""), classNumStates(0), maxThreads(0.8), fitted(false) {}
|
|
||||||
Network::Network(float maxT) : laplaceSmoothing(1), features(vector<string>()), className(""), classNumStates(0), maxThreads(maxT), fitted(false) {}
|
|
||||||
Network::Network(float maxT, int smoothing) : laplaceSmoothing(smoothing), features(vector<string>()), className(""), classNumStates(0), maxThreads(maxT), fitted(false) {}
|
|
||||||
Network::Network(Network& other) : laplaceSmoothing(other.laplaceSmoothing), features(other.features), className(other.className), classNumStates(other.getClassNumStates()), maxThreads(other.getmaxThreads()), fitted(other.fitted)
|
|
||||||
{
|
|
||||||
for (auto& pair : other.nodes) {
|
|
||||||
nodes[pair.first] = make_unique<Node>(*pair.second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
float Network::getmaxThreads()
|
|
||||||
{
|
|
||||||
return maxThreads;
|
|
||||||
}
|
|
||||||
torch::Tensor& Network::getSamples()
|
|
||||||
{
|
|
||||||
return samples;
|
|
||||||
}
|
|
||||||
void Network::addNode(string name, int numStates)
|
|
||||||
{
|
|
||||||
if (nodes.find(name) != nodes.end()) {
|
|
||||||
// if node exists update its number of states
|
|
||||||
nodes[name]->setNumStates(numStates);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
nodes[name] = make_unique<Node>(name, numStates);
|
|
||||||
}
|
|
||||||
vector<string> Network::getFeatures()
|
|
||||||
{
|
|
||||||
return features;
|
|
||||||
}
|
|
||||||
int Network::getClassNumStates()
|
|
||||||
{
|
|
||||||
return classNumStates;
|
|
||||||
}
|
|
||||||
int Network::getStates()
|
|
||||||
{
|
|
||||||
int result = 0;
|
|
||||||
for (auto& node : nodes) {
|
|
||||||
result += node.second->getNumStates();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
string Network::getClassName()
|
|
||||||
{
|
|
||||||
return className;
|
|
||||||
}
|
|
||||||
bool Network::isCyclic(const string& nodeId, unordered_set<string>& visited, unordered_set<string>& recStack)
|
|
||||||
{
|
|
||||||
if (visited.find(nodeId) == visited.end()) // if node hasn't been visited yet
|
|
||||||
{
|
|
||||||
visited.insert(nodeId);
|
|
||||||
recStack.insert(nodeId);
|
|
||||||
for (Node* child : nodes[nodeId]->getChildren()) {
|
|
||||||
if (visited.find(child->getName()) == visited.end() && isCyclic(child->getName(), visited, recStack))
|
|
||||||
return true;
|
|
||||||
else if (recStack.find(child->getName()) != recStack.end())
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
recStack.erase(nodeId); // remove node from recursion stack before function ends
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
void Network::addEdge(const string parent, const string child)
|
|
||||||
{
|
|
||||||
if (nodes.find(parent) == nodes.end()) {
|
|
||||||
throw invalid_argument("Parent node " + parent + " does not exist");
|
|
||||||
}
|
|
||||||
if (nodes.find(child) == nodes.end()) {
|
|
||||||
throw invalid_argument("Child node " + child + " does not exist");
|
|
||||||
}
|
|
||||||
// Temporarily add edge to check for cycles
|
|
||||||
nodes[parent]->addChild(nodes[child].get());
|
|
||||||
nodes[child]->addParent(nodes[parent].get());
|
|
||||||
unordered_set<string> visited;
|
|
||||||
unordered_set<string> recStack;
|
|
||||||
if (isCyclic(nodes[child]->getName(), visited, recStack)) // if adding this edge forms a cycle
|
|
||||||
{
|
|
||||||
// remove problematic edge
|
|
||||||
nodes[parent]->removeChild(nodes[child].get());
|
|
||||||
nodes[child]->removeParent(nodes[parent].get());
|
|
||||||
throw invalid_argument("Adding this edge forms a cycle in the graph.");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
map<string, std::unique_ptr<Node>>& Network::getNodes()
|
|
||||||
{
|
|
||||||
return nodes;
|
|
||||||
}
|
|
||||||
void Network::fit(const vector<vector<int>>& input_data, const vector<int>& labels, const vector<string>& featureNames, const string& className)
|
|
||||||
{
|
|
||||||
features = featureNames;
|
|
||||||
this->className = className;
|
|
||||||
dataset.clear();
|
|
||||||
|
|
||||||
// Build dataset & tensor of samples
|
|
||||||
samples = torch::zeros({ static_cast<int64_t>(input_data[0].size()), static_cast<int64_t>(input_data.size() + 1) }, torch::kInt64);
|
|
||||||
for (int i = 0; i < featureNames.size(); ++i) {
|
|
||||||
dataset[featureNames[i]] = input_data[i];
|
|
||||||
samples.index_put_({ "...", i }, torch::tensor(input_data[i], torch::kInt64));
|
|
||||||
}
|
|
||||||
dataset[className] = labels;
|
|
||||||
samples.index_put_({ "...", -1 }, torch::tensor(labels, torch::kInt64));
|
|
||||||
classNumStates = *max_element(labels.begin(), labels.end()) + 1;
|
|
||||||
int maxThreadsRunning = static_cast<int>(std::thread::hardware_concurrency() * maxThreads);
|
|
||||||
if (maxThreadsRunning < 1) {
|
|
||||||
maxThreadsRunning = 1;
|
|
||||||
}
|
|
||||||
vector<thread> threads;
|
|
||||||
mutex mtx;
|
|
||||||
condition_variable cv;
|
|
||||||
int activeThreads = 0;
|
|
||||||
int nextNodeIndex = 0;
|
|
||||||
|
|
||||||
while (nextNodeIndex < nodes.size()) {
|
|
||||||
unique_lock<mutex> lock(mtx);
|
|
||||||
cv.wait(lock, [&activeThreads, &maxThreadsRunning]() { return activeThreads < maxThreadsRunning; });
|
|
||||||
|
|
||||||
if (nextNodeIndex >= nodes.size()) {
|
|
||||||
break; // No more work remaining
|
|
||||||
}
|
|
||||||
|
|
||||||
threads.emplace_back([this, &nextNodeIndex, &mtx, &cv, &activeThreads]() {
|
|
||||||
while (true) {
|
|
||||||
unique_lock<mutex> lock(mtx);
|
|
||||||
if (nextNodeIndex >= nodes.size()) {
|
|
||||||
break; // No more work remaining
|
|
||||||
}
|
|
||||||
auto& pair = *std::next(nodes.begin(), nextNodeIndex);
|
|
||||||
++nextNodeIndex;
|
|
||||||
lock.unlock();
|
|
||||||
|
|
||||||
pair.second->computeCPT(dataset, laplaceSmoothing);
|
|
||||||
lock.lock();
|
|
||||||
nodes[pair.first] = std::move(pair.second);
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
lock_guard<mutex> lock(mtx);
|
|
||||||
--activeThreads;
|
|
||||||
cv.notify_one();
|
|
||||||
});
|
|
||||||
|
|
||||||
++activeThreads;
|
|
||||||
}
|
|
||||||
for (auto& thread : threads) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
fitted = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<int> Network::predict(const vector<vector<int>>& tsamples)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("You must call fit() before calling predict()");
|
|
||||||
}
|
|
||||||
vector<int> predictions;
|
|
||||||
vector<int> sample;
|
|
||||||
for (int row = 0; row < tsamples[0].size(); ++row) {
|
|
||||||
sample.clear();
|
|
||||||
for (int col = 0; col < tsamples.size(); ++col) {
|
|
||||||
sample.push_back(tsamples[col][row]);
|
|
||||||
}
|
|
||||||
vector<double> classProbabilities = predict_sample(sample);
|
|
||||||
// Find the class with the maximum posterior probability
|
|
||||||
auto maxElem = max_element(classProbabilities.begin(), classProbabilities.end());
|
|
||||||
int predictedClass = distance(classProbabilities.begin(), maxElem);
|
|
||||||
predictions.push_back(predictedClass);
|
|
||||||
}
|
|
||||||
return predictions;
|
|
||||||
}
|
|
||||||
vector<vector<double>> Network::predict_proba(const vector<vector<int>>& tsamples)
|
|
||||||
{
|
|
||||||
if (!fitted) {
|
|
||||||
throw logic_error("You must call fit() before calling predict_proba()");
|
|
||||||
}
|
|
||||||
vector<vector<double>> predictions;
|
|
||||||
vector<int> sample;
|
|
||||||
for (int row = 0; row < tsamples[0].size(); ++row) {
|
|
||||||
sample.clear();
|
|
||||||
for (int col = 0; col < tsamples.size(); ++col) {
|
|
||||||
sample.push_back(tsamples[col][row]);
|
|
||||||
}
|
|
||||||
predictions.push_back(predict_sample(sample));
|
|
||||||
}
|
|
||||||
return predictions;
|
|
||||||
}
|
|
||||||
double Network::score(const vector<vector<int>>& tsamples, const vector<int>& labels)
|
|
||||||
{
|
|
||||||
vector<int> y_pred = predict(tsamples);
|
|
||||||
int correct = 0;
|
|
||||||
for (int i = 0; i < y_pred.size(); ++i) {
|
|
||||||
if (y_pred[i] == labels[i]) {
|
|
||||||
correct++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (double)correct / y_pred.size();
|
|
||||||
}
|
|
||||||
vector<double> Network::predict_sample(const vector<int>& sample)
|
|
||||||
{
|
|
||||||
// Ensure the sample size is equal to the number of features
|
|
||||||
if (sample.size() != features.size()) {
|
|
||||||
throw invalid_argument("Sample size (" + to_string(sample.size()) +
|
|
||||||
") does not match the number of features (" + to_string(features.size()) + ")");
|
|
||||||
}
|
|
||||||
map<string, int> evidence;
|
|
||||||
for (int i = 0; i < sample.size(); ++i) {
|
|
||||||
evidence[features[i]] = sample[i];
|
|
||||||
}
|
|
||||||
return exactInference(evidence);
|
|
||||||
|
|
||||||
}
|
|
||||||
double Network::computeFactor(map<string, int>& completeEvidence)
|
|
||||||
{
|
|
||||||
double result = 1.0;
|
|
||||||
for (auto& node : getNodes()) {
|
|
||||||
result *= node.second->getFactorValue(completeEvidence);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
vector<double> Network::exactInference(map<string, int>& evidence)
|
|
||||||
{
|
|
||||||
vector<double> result(classNumStates, 0.0);
|
|
||||||
vector<thread> threads;
|
|
||||||
mutex mtx;
|
|
||||||
for (int i = 0; i < classNumStates; ++i) {
|
|
||||||
threads.emplace_back([this, &result, &evidence, i, &mtx]() {
|
|
||||||
auto completeEvidence = map<string, int>(evidence);
|
|
||||||
completeEvidence[getClassName()] = i;
|
|
||||||
double factor = computeFactor(completeEvidence);
|
|
||||||
lock_guard<mutex> lock(mtx);
|
|
||||||
result[i] = factor;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
for (auto& thread : threads) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize result
|
|
||||||
double sum = accumulate(result.begin(), result.end(), 0.0);
|
|
||||||
for (double& value : result) {
|
|
||||||
value /= sum;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
vector<string> Network::show()
|
|
||||||
{
|
|
||||||
vector<string> result;
|
|
||||||
// Draw the network
|
|
||||||
for (auto& node : nodes) {
|
|
||||||
string line = node.first + " -> ";
|
|
||||||
for (auto child : node.second->getChildren()) {
|
|
||||||
line += child->getName() + ", ";
|
|
||||||
}
|
|
||||||
result.push_back(line);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
vector<string> Network::graph(string title)
|
|
||||||
{
|
|
||||||
auto output = vector<string>();
|
|
||||||
auto prefix = "digraph BayesNet {\nlabel=<BayesNet ";
|
|
||||||
auto suffix = ">\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n";
|
|
||||||
string header = prefix + title + suffix;
|
|
||||||
output.push_back(header);
|
|
||||||
for (auto& node : nodes) {
|
|
||||||
auto result = node.second->graph(className);
|
|
||||||
output.insert(output.end(), result.begin(), result.end());
|
|
||||||
}
|
|
||||||
output.push_back("}\n");
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,52 +0,0 @@
|
|||||||
#ifndef NETWORK_H
|
|
||||||
#define NETWORK_H
|
|
||||||
#include "Node.h"
|
|
||||||
#include <map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace bayesnet {
|
|
||||||
class Network {
|
|
||||||
private:
|
|
||||||
map<string, std::unique_ptr<Node>> nodes;
|
|
||||||
map<string, vector<int>> dataset;
|
|
||||||
bool fitted;
|
|
||||||
float maxThreads;
|
|
||||||
int classNumStates;
|
|
||||||
vector<string> features;
|
|
||||||
string className;
|
|
||||||
int laplaceSmoothing;
|
|
||||||
torch::Tensor samples;
|
|
||||||
bool isCyclic(const std::string&, std::unordered_set<std::string>&, std::unordered_set<std::string>&);
|
|
||||||
vector<double> predict_sample(const vector<int>&);
|
|
||||||
vector<double> exactInference(map<string, int>&);
|
|
||||||
double computeFactor(map<string, int>&);
|
|
||||||
double mutual_info(torch::Tensor&, torch::Tensor&);
|
|
||||||
double entropy(torch::Tensor&);
|
|
||||||
double conditionalEntropy(torch::Tensor&, torch::Tensor&);
|
|
||||||
double mutualInformation(torch::Tensor&, torch::Tensor&);
|
|
||||||
public:
|
|
||||||
Network();
|
|
||||||
Network(float, int);
|
|
||||||
Network(float);
|
|
||||||
Network(Network&);
|
|
||||||
torch::Tensor& getSamples();
|
|
||||||
float getmaxThreads();
|
|
||||||
void addNode(string, int);
|
|
||||||
void addEdge(const string, const string);
|
|
||||||
map<string, std::unique_ptr<Node>>& getNodes();
|
|
||||||
vector<string> getFeatures();
|
|
||||||
int getStates();
|
|
||||||
int getClassNumStates();
|
|
||||||
string getClassName();
|
|
||||||
void fit(const vector<vector<int>>&, const vector<int>&, const vector<string>&, const string&);
|
|
||||||
vector<int> predict(const vector<vector<int>>&);
|
|
||||||
//Computes the conditional edge weight of variable index u and v conditioned on class_node
|
|
||||||
torch::Tensor conditionalEdgeWeight();
|
|
||||||
vector<vector<double>> predict_proba(const vector<vector<int>>&);
|
|
||||||
double score(const vector<vector<int>>&, const vector<int>&);
|
|
||||||
vector<string> show();
|
|
||||||
vector<string> graph(string title); // Returns a vector of strings representing the graph in graphviz format
|
|
||||||
inline string version() { return "0.1.0"; }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
122
src/Node.cc
122
src/Node.cc
@@ -1,122 +0,0 @@
|
|||||||
#include "Node.h"
|
|
||||||
|
|
||||||
namespace bayesnet {
|
|
||||||
|
|
||||||
Node::Node(const std::string& name, int numStates)
|
|
||||||
: name(name), numStates(numStates), cpTable(torch::Tensor()), parents(vector<Node*>()), children(vector<Node*>())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
string Node::getName() const
|
|
||||||
{
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
void Node::addParent(Node* parent)
|
|
||||||
{
|
|
||||||
parents.push_back(parent);
|
|
||||||
}
|
|
||||||
void Node::removeParent(Node* parent)
|
|
||||||
{
|
|
||||||
parents.erase(std::remove(parents.begin(), parents.end(), parent), parents.end());
|
|
||||||
}
|
|
||||||
void Node::removeChild(Node* child)
|
|
||||||
{
|
|
||||||
children.erase(std::remove(children.begin(), children.end(), child), children.end());
|
|
||||||
}
|
|
||||||
void Node::addChild(Node* child)
|
|
||||||
{
|
|
||||||
children.push_back(child);
|
|
||||||
}
|
|
||||||
vector<Node*>& Node::getParents()
|
|
||||||
{
|
|
||||||
return parents;
|
|
||||||
}
|
|
||||||
vector<Node*>& Node::getChildren()
|
|
||||||
{
|
|
||||||
return children;
|
|
||||||
}
|
|
||||||
int Node::getNumStates() const
|
|
||||||
{
|
|
||||||
return numStates;
|
|
||||||
}
|
|
||||||
void Node::setNumStates(int numStates)
|
|
||||||
{
|
|
||||||
this->numStates = numStates;
|
|
||||||
}
|
|
||||||
torch::Tensor& Node::getCPT()
|
|
||||||
{
|
|
||||||
return cpTable;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
The MinFill criterion is a heuristic for variable elimination.
|
|
||||||
The variable that minimizes the number of edges that need to be added to the graph to make it triangulated.
|
|
||||||
This is done by counting the number of edges that need to be added to the graph if the variable is eliminated.
|
|
||||||
The variable with the minimum number of edges is chosen.
|
|
||||||
Here this is done computing the length of the combinations of the node neighbors taken 2 by 2.
|
|
||||||
*/
|
|
||||||
unsigned Node::minFill()
|
|
||||||
{
|
|
||||||
unordered_set<string> neighbors;
|
|
||||||
for (auto child : children) {
|
|
||||||
neighbors.emplace(child->getName());
|
|
||||||
}
|
|
||||||
for (auto parent : parents) {
|
|
||||||
neighbors.emplace(parent->getName());
|
|
||||||
}
|
|
||||||
auto source = vector<string>(neighbors.begin(), neighbors.end());
|
|
||||||
return combinations(source).size();
|
|
||||||
}
|
|
||||||
vector<pair<string, string>> Node::combinations(const vector<string>& source)
|
|
||||||
{
|
|
||||||
vector<pair<string, string>> result;
|
|
||||||
for (int i = 0; i < source.size(); ++i) {
|
|
||||||
string temp = source[i];
|
|
||||||
for (int j = i + 1; j < source.size(); ++j) {
|
|
||||||
result.push_back({ temp, source[j] });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
void Node::computeCPT(map<string, vector<int>>& dataset, const int laplaceSmoothing)
|
|
||||||
{
|
|
||||||
// Get dimensions of the CPT
|
|
||||||
dimensions.push_back(numStates);
|
|
||||||
for (auto father : getParents()) {
|
|
||||||
dimensions.push_back(father->getNumStates());
|
|
||||||
}
|
|
||||||
auto length = dimensions.size();
|
|
||||||
// Create a tensor of zeros with the dimensions of the CPT
|
|
||||||
cpTable = torch::zeros(dimensions, torch::kFloat) + laplaceSmoothing;
|
|
||||||
// Fill table with counts
|
|
||||||
for (int n_sample = 0; n_sample < dataset[name].size(); ++n_sample) {
|
|
||||||
torch::List<c10::optional<torch::Tensor>> coordinates;
|
|
||||||
coordinates.push_back(torch::tensor(dataset[name][n_sample]));
|
|
||||||
for (auto father : getParents()) {
|
|
||||||
coordinates.push_back(torch::tensor(dataset[father->getName()][n_sample]));
|
|
||||||
}
|
|
||||||
// Increment the count of the corresponding coordinate
|
|
||||||
cpTable.index_put_({ coordinates }, cpTable.index({ coordinates }) + 1);
|
|
||||||
}
|
|
||||||
// Normalize the counts
|
|
||||||
cpTable = cpTable / cpTable.sum(0);
|
|
||||||
}
|
|
||||||
float Node::getFactorValue(map<string, int>& evidence)
|
|
||||||
{
|
|
||||||
torch::List<c10::optional<torch::Tensor>> coordinates;
|
|
||||||
// following predetermined order of indices in the cpTable (see Node.h)
|
|
||||||
coordinates.push_back(torch::tensor(evidence[name]));
|
|
||||||
for (auto parent : getParents()) {
|
|
||||||
coordinates.push_back(torch::tensor(evidence[parent->getName()]));
|
|
||||||
}
|
|
||||||
return cpTable.index({ coordinates }).item<float>();
|
|
||||||
}
|
|
||||||
vector<string> Node::graph(string className)
|
|
||||||
{
|
|
||||||
auto output = vector<string>();
|
|
||||||
auto suffix = name == className ? ", fontcolor=red, fillcolor=lightblue, style=filled " : "";
|
|
||||||
output.push_back(name + " [shape=circle" + suffix + "] \n");
|
|
||||||
for (auto& child : children) {
|
|
||||||
output.push_back(name + " -> " + child->getName());
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
}
|
|
36
src/Node.h
36
src/Node.h
@@ -1,36 +0,0 @@
|
|||||||
#ifndef NODE_H
|
|
||||||
#define NODE_H
|
|
||||||
#include <torch/torch.h>
|
|
||||||
#include <unordered_set>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
namespace bayesnet {
|
|
||||||
using namespace std;
|
|
||||||
class Node {
|
|
||||||
private:
|
|
||||||
string name;
|
|
||||||
vector<Node*> parents;
|
|
||||||
vector<Node*> children;
|
|
||||||
int numStates; // number of states of the variable
|
|
||||||
torch::Tensor cpTable; // Order of indices is 0-> node variable, 1-> 1st parent, 2-> 2nd parent, ...
|
|
||||||
vector<int64_t> dimensions; // dimensions of the cpTable
|
|
||||||
public:
|
|
||||||
vector<pair<string, string>> combinations(const vector<string>&);
|
|
||||||
Node(const std::string&, int);
|
|
||||||
void addParent(Node*);
|
|
||||||
void addChild(Node*);
|
|
||||||
void removeParent(Node*);
|
|
||||||
void removeChild(Node*);
|
|
||||||
string getName() const;
|
|
||||||
vector<Node*>& getParents();
|
|
||||||
vector<Node*>& getChildren();
|
|
||||||
torch::Tensor& getCPT();
|
|
||||||
void computeCPT(map<string, vector<int>>&, const int);
|
|
||||||
int getNumStates() const;
|
|
||||||
void setNumStates(int);
|
|
||||||
unsigned minFill();
|
|
||||||
vector<string> graph(string clasName); // Returns a vector of strings representing the graph in graphviz format
|
|
||||||
float getFactorValue(map<string, int>&);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
343
src/Platform/BestResults.cc
Normal file
343
src/Platform/BestResults.cc
Normal file
@@ -0,0 +1,343 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <set>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include "BestResults.h"
|
||||||
|
#include "Result.h"
|
||||||
|
#include "Colors.h"
|
||||||
|
#include "Statistics.h"
|
||||||
|
#include "BestResultsExcel.h"
|
||||||
|
#include "CLocale.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
// function ftime_to_std::string, Code taken from
|
||||||
|
// https://stackoverflow.com/a/58237530/1389271
|
||||||
|
template <typename TP>
|
||||||
|
std::string ftime_to_string(TP tp)
|
||||||
|
{
|
||||||
|
auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(tp - TP::clock::now()
|
||||||
|
+ std::chrono::system_clock::now());
|
||||||
|
auto tt = std::chrono::system_clock::to_time_t(sctp);
|
||||||
|
std::tm* gmt = std::gmtime(&tt);
|
||||||
|
std::stringstream buffer;
|
||||||
|
buffer << std::put_time(gmt, "%Y-%m-%d %H:%M");
|
||||||
|
return buffer.str();
|
||||||
|
}
|
||||||
|
namespace platform {
|
||||||
|
std::string BestResults::build()
|
||||||
|
{
|
||||||
|
auto files = loadResultFiles();
|
||||||
|
if (files.size() == 0) {
|
||||||
|
std::cerr << Colors::MAGENTA() << "No result files were found!" << Colors::RESET() << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
json bests;
|
||||||
|
for (const auto& file : files) {
|
||||||
|
auto result = Result(path, file);
|
||||||
|
auto data = result.load();
|
||||||
|
for (auto const& item : data.at("results")) {
|
||||||
|
bool update = false;
|
||||||
|
// Check if results file contains only one dataset
|
||||||
|
auto datasetName = item.at("dataset").get<std::string>();
|
||||||
|
if (bests.contains(datasetName)) {
|
||||||
|
if (item.at("score").get<double>() > bests[datasetName].at(0).get<double>()) {
|
||||||
|
update = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
update = true;
|
||||||
|
}
|
||||||
|
if (update) {
|
||||||
|
bests[datasetName] = { item.at("score").get<double>(), item.at("hyperparameters"), file };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::string bestFileName = path + bestResultFile();
|
||||||
|
if (FILE* fileTest = fopen(bestFileName.c_str(), "r")) {
|
||||||
|
fclose(fileTest);
|
||||||
|
std::cout << Colors::MAGENTA() << "File " << bestFileName << " already exists and it shall be overwritten." << Colors::RESET() << std::endl;
|
||||||
|
}
|
||||||
|
std::ofstream file(bestFileName);
|
||||||
|
file << bests;
|
||||||
|
file.close();
|
||||||
|
return bestFileName;
|
||||||
|
}
|
||||||
|
std::string BestResults::bestResultFile()
|
||||||
|
{
|
||||||
|
return "best_results_" + score + "_" + model + ".json";
|
||||||
|
}
|
||||||
|
std::pair<std::string, std::string> getModelScore(std::string name)
|
||||||
|
{
|
||||||
|
// results_accuracy_BoostAODE_MacBookpro16_2023-09-06_12:27:00_1.json
|
||||||
|
int i = 0;
|
||||||
|
auto pos = name.find("_");
|
||||||
|
auto pos2 = name.find("_", pos + 1);
|
||||||
|
std::string score = name.substr(pos + 1, pos2 - pos - 1);
|
||||||
|
pos = name.find("_", pos2 + 1);
|
||||||
|
std::string model = name.substr(pos2 + 1, pos - pos2 - 1);
|
||||||
|
return { model, score };
|
||||||
|
}
|
||||||
|
std::vector<std::string> BestResults::loadResultFiles()
|
||||||
|
{
|
||||||
|
std::vector<std::string> files;
|
||||||
|
using std::filesystem::directory_iterator;
|
||||||
|
std::string fileModel, fileScore;
|
||||||
|
for (const auto& file : directory_iterator(path)) {
|
||||||
|
auto fileName = file.path().filename().string();
|
||||||
|
if (fileName.find(".json") != std::string::npos && fileName.find("results_") == 0) {
|
||||||
|
tie(fileModel, fileScore) = getModelScore(fileName);
|
||||||
|
if (score == fileScore && (model == fileModel || model == "any")) {
|
||||||
|
files.push_back(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
json BestResults::loadFile(const std::string& fileName)
|
||||||
|
{
|
||||||
|
std::ifstream resultData(fileName);
|
||||||
|
if (resultData.is_open()) {
|
||||||
|
json data = json::parse(resultData);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
throw std::invalid_argument("Unable to open result file. [" + fileName + "]");
|
||||||
|
}
|
||||||
|
std::vector<std::string> BestResults::getModels()
|
||||||
|
{
|
||||||
|
std::set<std::string> models;
|
||||||
|
std::vector<std::string> result;
|
||||||
|
auto files = loadResultFiles();
|
||||||
|
if (files.size() == 0) {
|
||||||
|
std::cerr << Colors::MAGENTA() << "No result files were found!" << Colors::RESET() << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
std::string fileModel, fileScore;
|
||||||
|
for (const auto& file : files) {
|
||||||
|
// extract the model from the file name
|
||||||
|
tie(fileModel, fileScore) = getModelScore(file);
|
||||||
|
// add the model to the std::vector of models
|
||||||
|
models.insert(fileModel);
|
||||||
|
}
|
||||||
|
result = std::vector<std::string>(models.begin(), models.end());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
std::vector<std::string> BestResults::getDatasets(json table)
|
||||||
|
{
|
||||||
|
std::vector<std::string> datasets;
|
||||||
|
for (const auto& dataset : table.items()) {
|
||||||
|
datasets.push_back(dataset.key());
|
||||||
|
}
|
||||||
|
return datasets;
|
||||||
|
}
|
||||||
|
void BestResults::buildAll()
|
||||||
|
{
|
||||||
|
auto models = getModels();
|
||||||
|
for (const auto& model : models) {
|
||||||
|
std::cout << "Building best results for model: " << model << std::endl;
|
||||||
|
this->model = model;
|
||||||
|
build();
|
||||||
|
}
|
||||||
|
model = "any";
|
||||||
|
}
|
||||||
|
void BestResults::listFile()
|
||||||
|
{
|
||||||
|
std::string bestFileName = path + bestResultFile();
|
||||||
|
if (FILE* fileTest = fopen(bestFileName.c_str(), "r")) {
|
||||||
|
fclose(fileTest);
|
||||||
|
} else {
|
||||||
|
std::cerr << Colors::MAGENTA() << "File " << bestFileName << " doesn't exist." << Colors::RESET() << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
auto temp = ConfigLocale();
|
||||||
|
auto date = ftime_to_string(std::filesystem::last_write_time(bestFileName));
|
||||||
|
auto data = loadFile(bestFileName);
|
||||||
|
auto datasets = getDatasets(data);
|
||||||
|
int maxDatasetName = (*max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||||
|
int maxFileName = 0;
|
||||||
|
int maxHyper = 15;
|
||||||
|
for (auto const& item : data.items()) {
|
||||||
|
maxHyper = std::max(maxHyper, (int)item.value().at(1).dump().size());
|
||||||
|
maxFileName = std::max(maxFileName, (int)item.value().at(2).get<std::string>().size());
|
||||||
|
}
|
||||||
|
std::stringstream oss;
|
||||||
|
oss << Colors::GREEN() << "Best results for " << model << " as of " << date << std::endl;
|
||||||
|
std::cout << oss.str();
|
||||||
|
std::cout << std::string(oss.str().size() - 8, '-') << std::endl;
|
||||||
|
std::cout << Colors::GREEN() << " # " << std::setw(maxDatasetName + 1) << std::left << "Dataset" << "Score " << std::setw(maxFileName) << "File" << " Hyperparameters" << std::endl;
|
||||||
|
std::cout << "=== " << std::string(maxDatasetName, '=') << " =========== " << std::string(maxFileName, '=') << " " << std::string(maxHyper, '=') << std::endl;
|
||||||
|
auto i = 0;
|
||||||
|
bool odd = true;
|
||||||
|
double total = 0;
|
||||||
|
for (auto const& item : data.items()) {
|
||||||
|
auto color = odd ? Colors::BLUE() : Colors::CYAN();
|
||||||
|
double value = item.value().at(0).get<double>();
|
||||||
|
std::cout << color << std::setw(3) << std::fixed << std::right << i++ << " ";
|
||||||
|
std::cout << std::setw(maxDatasetName) << std::left << item.key() << " ";
|
||||||
|
std::cout << std::setw(11) << std::setprecision(9) << std::fixed << value << " ";
|
||||||
|
std::cout << std::setw(maxFileName) << item.value().at(2).get<std::string>() << " ";
|
||||||
|
std::cout << item.value().at(1) << " ";
|
||||||
|
std::cout << std::endl;
|
||||||
|
total += value;
|
||||||
|
odd = !odd;
|
||||||
|
}
|
||||||
|
std::cout << Colors::GREEN() << "=== " << std::string(maxDatasetName, '=') << " ===========" << std::endl;
|
||||||
|
std::cout << std::setw(5 + maxDatasetName) << "Total.................. " << std::setw(11) << std::setprecision(8) << std::fixed << total << std::endl;
|
||||||
|
}
|
||||||
|
json BestResults::buildTableResults(std::vector<std::string> models)
|
||||||
|
{
|
||||||
|
json table;
|
||||||
|
auto maxDate = std::filesystem::file_time_type::max();
|
||||||
|
for (const auto& model : models) {
|
||||||
|
this->model = model;
|
||||||
|
std::string bestFileName = path + bestResultFile();
|
||||||
|
if (FILE* fileTest = fopen(bestFileName.c_str(), "r")) {
|
||||||
|
fclose(fileTest);
|
||||||
|
} else {
|
||||||
|
std::cerr << Colors::MAGENTA() << "File " << bestFileName << " doesn't exist." << Colors::RESET() << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
auto dateWrite = std::filesystem::last_write_time(bestFileName);
|
||||||
|
if (dateWrite < maxDate) {
|
||||||
|
maxDate = dateWrite;
|
||||||
|
}
|
||||||
|
auto data = loadFile(bestFileName);
|
||||||
|
table[model] = data;
|
||||||
|
}
|
||||||
|
table["dateTable"] = ftime_to_string(maxDate);
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
void BestResults::printTableResults(std::vector<std::string> models, json table)
|
||||||
|
{
|
||||||
|
std::stringstream oss;
|
||||||
|
oss << Colors::GREEN() << "Best results for " << score << " as of " << table.at("dateTable").get<std::string>() << std::endl;
|
||||||
|
std::cout << oss.str();
|
||||||
|
std::cout << std::string(oss.str().size() - 8, '-') << std::endl;
|
||||||
|
std::cout << Colors::GREEN() << " # " << std::setw(maxDatasetName + 1) << std::left << std::string("Dataset");
|
||||||
|
for (const auto& model : models) {
|
||||||
|
std::cout << std::setw(maxModelName) << std::left << model << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
std::cout << "=== " << std::string(maxDatasetName, '=') << " ";
|
||||||
|
for (const auto& model : models) {
|
||||||
|
std::cout << std::string(maxModelName, '=') << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
auto i = 0;
|
||||||
|
bool odd = true;
|
||||||
|
std::map<std::string, double> totals;
|
||||||
|
int nDatasets = table.begin().value().size();
|
||||||
|
for (const auto& model : models) {
|
||||||
|
totals[model] = 0.0;
|
||||||
|
}
|
||||||
|
auto datasets = getDatasets(table.begin().value());
|
||||||
|
for (auto const& dataset : datasets) {
|
||||||
|
auto color = odd ? Colors::BLUE() : Colors::CYAN();
|
||||||
|
std::cout << color << std::setw(3) << std::fixed << std::right << i++ << " ";
|
||||||
|
std::cout << std::setw(maxDatasetName) << std::left << dataset << " ";
|
||||||
|
double maxValue = 0;
|
||||||
|
// Find out the max value for this dataset
|
||||||
|
for (const auto& model : models) {
|
||||||
|
double value = table[model].at(dataset).at(0).get<double>();
|
||||||
|
if (value > maxValue) {
|
||||||
|
maxValue = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Print the row with red colors on max values
|
||||||
|
for (const auto& model : models) {
|
||||||
|
std::string efectiveColor = color;
|
||||||
|
double value = table[model].at(dataset).at(0).get<double>();
|
||||||
|
if (value == maxValue) {
|
||||||
|
efectiveColor = Colors::RED();
|
||||||
|
}
|
||||||
|
totals[model] += value;
|
||||||
|
std::cout << efectiveColor << std::setw(maxModelName) << std::setprecision(maxModelName - 2) << std::fixed << value << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
odd = !odd;
|
||||||
|
}
|
||||||
|
std::cout << Colors::GREEN() << "=== " << std::string(maxDatasetName, '=') << " ";
|
||||||
|
for (const auto& model : models) {
|
||||||
|
std::cout << std::string(maxModelName, '=') << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
std::cout << Colors::GREEN() << std::setw(5 + maxDatasetName) << " Totals...................";
|
||||||
|
double max = 0.0;
|
||||||
|
for (const auto& total : totals) {
|
||||||
|
if (total.second > max) {
|
||||||
|
max = total.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const auto& model : models) {
|
||||||
|
std::string efectiveColor = Colors::GREEN();
|
||||||
|
if (totals[model] == max) {
|
||||||
|
efectiveColor = Colors::RED();
|
||||||
|
}
|
||||||
|
std::cout << efectiveColor << std::right << std::setw(maxModelName) << std::setprecision(maxModelName - 4) << std::fixed << totals[model] << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
void BestResults::reportSingle(bool excel)
|
||||||
|
{
|
||||||
|
listFile();
|
||||||
|
if (excel) {
|
||||||
|
auto models = getModels();
|
||||||
|
// Build the table of results
|
||||||
|
json table = buildTableResults(models);
|
||||||
|
std::vector<std::string> datasets = getDatasets(table.begin().value());
|
||||||
|
BestResultsExcel excel(score, datasets);
|
||||||
|
excel.reportSingle(model, path + bestResultFile());
|
||||||
|
messageExcelFile(excel.getFileName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void BestResults::reportAll(bool excel)
|
||||||
|
{
|
||||||
|
auto models = getModels();
|
||||||
|
// Build the table of results
|
||||||
|
json table = buildTableResults(models);
|
||||||
|
std::vector<std::string> datasets = getDatasets(table.begin().value());
|
||||||
|
maxModelName = (*max_element(models.begin(), models.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||||
|
maxModelName = std::max(12, maxModelName);
|
||||||
|
maxDatasetName = (*max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||||
|
maxDatasetName = std::max(25, maxDatasetName);
|
||||||
|
// Print the table of results
|
||||||
|
printTableResults(models, table);
|
||||||
|
// Compute the Friedman test
|
||||||
|
std::map<std::string, std::map<std::string, float>> ranksModels;
|
||||||
|
if (friedman) {
|
||||||
|
Statistics stats(models, datasets, table, significance);
|
||||||
|
auto result = stats.friedmanTest();
|
||||||
|
stats.postHocHolmTest(result);
|
||||||
|
ranksModels = stats.getRanks();
|
||||||
|
}
|
||||||
|
if (excel) {
|
||||||
|
BestResultsExcel excel(score, datasets);
|
||||||
|
excel.reportAll(models, table, ranksModels, friedman, significance);
|
||||||
|
if (friedman) {
|
||||||
|
int idx = -1;
|
||||||
|
double min = 2000;
|
||||||
|
// Find out the control model
|
||||||
|
auto totals = std::vector<double>(models.size(), 0.0);
|
||||||
|
for (const auto& dataset : datasets) {
|
||||||
|
for (int i = 0; i < models.size(); ++i) {
|
||||||
|
totals[i] += ranksModels[dataset][models[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < models.size(); ++i) {
|
||||||
|
if (totals[i] < min) {
|
||||||
|
min = totals[i];
|
||||||
|
idx = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
model = models.at(idx);
|
||||||
|
excel.reportSingle(model, path + bestResultFile());
|
||||||
|
}
|
||||||
|
messageExcelFile(excel.getFileName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void BestResults::messageExcelFile(const std::string& fileName)
|
||||||
|
{
|
||||||
|
std::cout << Colors::YELLOW() << "** Excel file generated: " << fileName << Colors::RESET() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
36
src/Platform/BestResults.h
Normal file
36
src/Platform/BestResults.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef BESTRESULTS_H
|
||||||
|
#define BESTRESULTS_H
|
||||||
|
#include <string>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
using json = nlohmann::json;
|
||||||
|
namespace platform {
|
||||||
|
class BestResults {
|
||||||
|
public:
|
||||||
|
explicit BestResults(const std::string& path, const std::string& score, const std::string& model, bool friedman, double significance = 0.05)
|
||||||
|
: path(path), score(score), model(model), friedman(friedman), significance(significance)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
std::string build();
|
||||||
|
void reportSingle(bool excel);
|
||||||
|
void reportAll(bool excel);
|
||||||
|
void buildAll();
|
||||||
|
private:
|
||||||
|
std::vector<std::string> getModels();
|
||||||
|
std::vector<std::string> getDatasets(json table);
|
||||||
|
std::vector<std::string> loadResultFiles();
|
||||||
|
void messageExcelFile(const std::string& fileName);
|
||||||
|
json buildTableResults(std::vector<std::string> models);
|
||||||
|
void printTableResults(std::vector<std::string> models, json table);
|
||||||
|
std::string bestResultFile();
|
||||||
|
json loadFile(const std::string& fileName);
|
||||||
|
void listFile();
|
||||||
|
std::string path;
|
||||||
|
std::string score;
|
||||||
|
std::string model;
|
||||||
|
bool friedman;
|
||||||
|
double significance;
|
||||||
|
int maxModelName = 0;
|
||||||
|
int maxDatasetName = 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif //BESTRESULTS_H
|
300
src/Platform/BestResultsExcel.cc
Normal file
300
src/Platform/BestResultsExcel.cc
Normal file
@@ -0,0 +1,300 @@
|
|||||||
|
#include <sstream>
|
||||||
|
#include "BestResultsExcel.h"
|
||||||
|
#include "Paths.h"
|
||||||
|
#include <map>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include "Statistics.h"
|
||||||
|
#include "ReportExcel.h"
|
||||||
|
|
||||||
|
namespace platform {
|
||||||
|
json loadResultData(const std::string& fileName)
|
||||||
|
{
|
||||||
|
json data;
|
||||||
|
std::ifstream resultData(fileName);
|
||||||
|
if (resultData.is_open()) {
|
||||||
|
data = json::parse(resultData);
|
||||||
|
} else {
|
||||||
|
throw std::invalid_argument("Unable to open result file. [" + fileName + "]");
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
std::string getColumnName(int colNum)
|
||||||
|
{
|
||||||
|
std::string columnName = "";
|
||||||
|
if (colNum == 0)
|
||||||
|
return "A";
|
||||||
|
while (colNum > 0) {
|
||||||
|
int modulo = colNum % 26;
|
||||||
|
columnName = char(65 + modulo) + columnName;
|
||||||
|
colNum = (int)((colNum - modulo) / 26);
|
||||||
|
}
|
||||||
|
return columnName;
|
||||||
|
}
|
||||||
|
BestResultsExcel::BestResultsExcel(const std::string& score, const std::vector<std::string>& datasets) : score(score), datasets(datasets)
|
||||||
|
{
|
||||||
|
workbook = workbook_new((Paths::excel() + fileName).c_str());
|
||||||
|
setProperties("Best Results");
|
||||||
|
int maxDatasetName = (*max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||||
|
datasetNameSize = std::max(datasetNameSize, maxDatasetName);
|
||||||
|
createFormats();
|
||||||
|
}
|
||||||
|
void BestResultsExcel::reportAll(const std::vector<std::string>& models, const json& table, const std::map<std::string, std::map<std::string, float>>& ranks, bool friedman, double significance)
|
||||||
|
{
|
||||||
|
this->table = table;
|
||||||
|
this->models = models;
|
||||||
|
ranksModels = ranks;
|
||||||
|
this->friedman = friedman;
|
||||||
|
this->significance = significance;
|
||||||
|
worksheet = workbook_add_worksheet(workbook, "Best Results");
|
||||||
|
int maxModelName = (*std::max_element(models.begin(), models.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
|
||||||
|
modelNameSize = std::max(modelNameSize, maxModelName);
|
||||||
|
formatColumns();
|
||||||
|
build();
|
||||||
|
}
|
||||||
|
void BestResultsExcel::reportSingle(const std::string& model, const std::string& fileName)
|
||||||
|
{
|
||||||
|
worksheet = workbook_add_worksheet(workbook, "Report");
|
||||||
|
if (FILE* fileTest = fopen(fileName.c_str(), "r")) {
|
||||||
|
fclose(fileTest);
|
||||||
|
} else {
|
||||||
|
std::cerr << "File " << fileName << " doesn't exist." << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
json data = loadResultData(fileName);
|
||||||
|
|
||||||
|
std::string title = "Best results for " + model;
|
||||||
|
worksheet_merge_range(worksheet, 0, 0, 0, 4, title.c_str(), styles["headerFirst"]);
|
||||||
|
// Body header
|
||||||
|
row = 3;
|
||||||
|
int col = 1;
|
||||||
|
writeString(row, 0, "Nº", "bodyHeader");
|
||||||
|
writeString(row, 1, "Dataset", "bodyHeader");
|
||||||
|
writeString(row, 2, "Score", "bodyHeader");
|
||||||
|
writeString(row, 3, "File", "bodyHeader");
|
||||||
|
writeString(row, 4, "Hyperparameters", "bodyHeader");
|
||||||
|
auto i = 0;
|
||||||
|
std::string hyperparameters;
|
||||||
|
int hypSize = 22;
|
||||||
|
std::map<std::string, std::string> files; // map of files imported and their tabs
|
||||||
|
for (auto const& item : data.items()) {
|
||||||
|
row++;
|
||||||
|
writeInt(row, 0, i++, "ints");
|
||||||
|
writeString(row, 1, item.key().c_str(), "text");
|
||||||
|
writeDouble(row, 2, item.value().at(0).get<double>(), "result");
|
||||||
|
auto fileName = item.value().at(2).get<std::string>();
|
||||||
|
std::string hyperlink = "";
|
||||||
|
try {
|
||||||
|
hyperlink = files.at(fileName);
|
||||||
|
}
|
||||||
|
catch (const std::out_of_range& oor) {
|
||||||
|
auto tabName = "table_" + std::to_string(i);
|
||||||
|
auto worksheetNew = workbook_add_worksheet(workbook, tabName.c_str());
|
||||||
|
json data = loadResultData(Paths::results() + fileName);
|
||||||
|
auto report = ReportExcel(data, false, workbook, worksheetNew);
|
||||||
|
report.show();
|
||||||
|
hyperlink = "#table_" + std::to_string(i);
|
||||||
|
files[fileName] = hyperlink;
|
||||||
|
}
|
||||||
|
hyperlink += "!H" + std::to_string(i + 6);
|
||||||
|
std::string fileNameText = "=HYPERLINK(\"" + hyperlink + "\",\"" + fileName + "\")";
|
||||||
|
worksheet_write_formula(worksheet, row, 3, fileNameText.c_str(), efectiveStyle("text"));
|
||||||
|
hyperparameters = item.value().at(1).dump();
|
||||||
|
if (hyperparameters.size() > hypSize) {
|
||||||
|
hypSize = hyperparameters.size();
|
||||||
|
}
|
||||||
|
writeString(row, 4, hyperparameters, "text");
|
||||||
|
}
|
||||||
|
row++;
|
||||||
|
// Set Totals
|
||||||
|
writeString(row, 1, "Total", "bodyHeader");
|
||||||
|
std::stringstream oss;
|
||||||
|
auto colName = getColumnName(2);
|
||||||
|
oss << "=sum(" << colName << "5:" << colName << row << ")";
|
||||||
|
worksheet_write_formula(worksheet, row, 2, oss.str().c_str(), styles["bodyHeader_odd"]);
|
||||||
|
// Set format
|
||||||
|
worksheet_freeze_panes(worksheet, 4, 2);
|
||||||
|
std::vector<int> columns_sizes = { 5, datasetNameSize, modelNameSize, 66, hypSize + 1 };
|
||||||
|
for (int i = 0; i < columns_sizes.size(); ++i) {
|
||||||
|
worksheet_set_column(worksheet, i, i, columns_sizes.at(i), NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BestResultsExcel::~BestResultsExcel()
|
||||||
|
{
|
||||||
|
workbook_close(workbook);
|
||||||
|
}
|
||||||
|
void BestResultsExcel::formatColumns()
|
||||||
|
{
|
||||||
|
worksheet_freeze_panes(worksheet, 4, 2);
|
||||||
|
std::vector<int> columns_sizes = { 5, datasetNameSize };
|
||||||
|
for (int i = 0; i < models.size(); ++i) {
|
||||||
|
columns_sizes.push_back(modelNameSize);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < columns_sizes.size(); ++i) {
|
||||||
|
worksheet_set_column(worksheet, i, i, columns_sizes.at(i), NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void BestResultsExcel::addConditionalFormat(std::string formula)
|
||||||
|
{
|
||||||
|
// Add conditional format for max/min values in scores/ranks sheets
|
||||||
|
lxw_format* custom_format = workbook_add_format(workbook);
|
||||||
|
format_set_bg_color(custom_format, 0xFFC7CE);
|
||||||
|
format_set_font_color(custom_format, 0x9C0006);
|
||||||
|
// Create a conditional format object. A static object would also work.
|
||||||
|
lxw_conditional_format* conditional_format = (lxw_conditional_format*)calloc(1, sizeof(lxw_conditional_format));
|
||||||
|
conditional_format->type = LXW_CONDITIONAL_TYPE_FORMULA;
|
||||||
|
std::string col = getColumnName(models.size() + 1);
|
||||||
|
std::stringstream oss;
|
||||||
|
oss << "=C5=" << formula << "($C5:$" << col << "5)";
|
||||||
|
auto formulaValue = oss.str();
|
||||||
|
conditional_format->value_string = formulaValue.c_str();
|
||||||
|
conditional_format->format = custom_format;
|
||||||
|
worksheet_conditional_format_range(worksheet, 4, 2, datasets.size() + 3, models.size() + 1, conditional_format);
|
||||||
|
}
|
||||||
|
void BestResultsExcel::build()
|
||||||
|
{
|
||||||
|
// Create Sheet with scores
|
||||||
|
header(false);
|
||||||
|
body(false);
|
||||||
|
// Add conditional format for max values
|
||||||
|
addConditionalFormat("max");
|
||||||
|
footer(false);
|
||||||
|
if (friedman) {
|
||||||
|
// Create Sheet with ranks
|
||||||
|
worksheet = workbook_add_worksheet(workbook, "Ranks");
|
||||||
|
formatColumns();
|
||||||
|
header(true);
|
||||||
|
body(true);
|
||||||
|
addConditionalFormat("min");
|
||||||
|
footer(true);
|
||||||
|
// Create Sheet with Friedman Test
|
||||||
|
doFriedman();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::string BestResultsExcel::getFileName()
|
||||||
|
{
|
||||||
|
return Paths::excel() + fileName;
|
||||||
|
}
|
||||||
|
void BestResultsExcel::header(bool ranks)
|
||||||
|
{
|
||||||
|
row = 0;
|
||||||
|
std::string message = ranks ? "Ranks for score " + score : "Best results for " + score;
|
||||||
|
worksheet_merge_range(worksheet, 0, 0, 0, 1 + models.size(), message.c_str(), styles["headerFirst"]);
|
||||||
|
// Body header
|
||||||
|
row = 3;
|
||||||
|
int col = 1;
|
||||||
|
writeString(row, 0, "Nº", "bodyHeader");
|
||||||
|
writeString(row, 1, "Dataset", "bodyHeader");
|
||||||
|
for (const auto& model : models) {
|
||||||
|
writeString(row, ++col, model.c_str(), "bodyHeader");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void BestResultsExcel::body(bool ranks)
|
||||||
|
{
|
||||||
|
row = 4;
|
||||||
|
int i = 0;
|
||||||
|
json origin = table.begin().value();
|
||||||
|
for (auto const& item : origin.items()) {
|
||||||
|
writeInt(row, 0, i++, "ints");
|
||||||
|
writeString(row, 1, item.key().c_str(), "text");
|
||||||
|
int col = 1;
|
||||||
|
for (const auto& model : models) {
|
||||||
|
double value = ranks ? ranksModels[item.key()][model] : table[model].at(item.key()).at(0).get<double>();
|
||||||
|
writeDouble(row, ++col, value, "result");
|
||||||
|
}
|
||||||
|
++row;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void BestResultsExcel::footer(bool ranks)
|
||||||
|
{
|
||||||
|
// Set Totals
|
||||||
|
writeString(row, 1, "Total", "bodyHeader");
|
||||||
|
int col = 1;
|
||||||
|
for (const auto& model : models) {
|
||||||
|
std::stringstream oss;
|
||||||
|
auto colName = getColumnName(col + 1);
|
||||||
|
oss << "=SUM(" << colName << "5:" << colName << row << ")";
|
||||||
|
worksheet_write_formula(worksheet, row, ++col, oss.str().c_str(), styles["bodyHeader_odd"]);
|
||||||
|
}
|
||||||
|
if (ranks) {
|
||||||
|
row++;
|
||||||
|
writeString(row, 1, "Average ranks", "bodyHeader");
|
||||||
|
int col = 1;
|
||||||
|
for (const auto& model : models) {
|
||||||
|
auto colName = getColumnName(col + 1);
|
||||||
|
std::stringstream oss;
|
||||||
|
oss << "=SUM(" << colName << "5:" << colName << row - 1 << ")/" << datasets.size();
|
||||||
|
worksheet_write_formula(worksheet, row, ++col, oss.str().c_str(), styles["bodyHeader_odd"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void BestResultsExcel::doFriedman()
|
||||||
|
{
|
||||||
|
worksheet = workbook_add_worksheet(workbook, "Friedman");
|
||||||
|
std::vector<int> columns_sizes = { 5, datasetNameSize };
|
||||||
|
for (int i = 0; i < models.size(); ++i) {
|
||||||
|
columns_sizes.push_back(modelNameSize);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < columns_sizes.size(); ++i) {
|
||||||
|
worksheet_set_column(worksheet, i, i, columns_sizes.at(i), NULL);
|
||||||
|
}
|
||||||
|
worksheet_merge_range(worksheet, 0, 0, 0, 1 + models.size(), "Friedman Test", styles["headerFirst"]);
|
||||||
|
row = 2;
|
||||||
|
Statistics stats(models, datasets, table, significance, false);
|
||||||
|
auto result = stats.friedmanTest();
|
||||||
|
stats.postHocHolmTest(result);
|
||||||
|
auto friedmanResult = stats.getFriedmanResult();
|
||||||
|
auto holmResult = stats.getHolmResult();
|
||||||
|
worksheet_merge_range(worksheet, row, 0, row, 1 + models.size(), "Null hypothesis: H0 'There is no significant differences between all the classifiers.'", styles["headerSmall"]);
|
||||||
|
row += 2;
|
||||||
|
writeString(row, 1, "Friedman Q", "bodyHeader");
|
||||||
|
writeDouble(row, 2, friedmanResult.statistic, "bodyHeader");
|
||||||
|
row++;
|
||||||
|
writeString(row, 1, "Critical χ2 value", "bodyHeader");
|
||||||
|
writeDouble(row, 2, friedmanResult.criticalValue, "bodyHeader");
|
||||||
|
row++;
|
||||||
|
writeString(row, 1, "p-value", "bodyHeader");
|
||||||
|
writeDouble(row, 2, friedmanResult.pvalue, "bodyHeader");
|
||||||
|
writeString(row, 3, friedmanResult.reject ? "<" : ">", "bodyHeader");
|
||||||
|
writeDouble(row, 4, significance, "bodyHeader");
|
||||||
|
writeString(row, 5, friedmanResult.reject ? "Reject H0" : "Accept H0", "bodyHeader");
|
||||||
|
row += 3;
|
||||||
|
worksheet_merge_range(worksheet, row, 0, row, 1 + models.size(), "Holm Test", styles["headerFirst"]);
|
||||||
|
row += 2;
|
||||||
|
worksheet_merge_range(worksheet, row, 0, row, 1 + models.size(), "Null hypothesis: H0 'There is no significant differences between the control model and the other models.'", styles["headerSmall"]);
|
||||||
|
row += 2;
|
||||||
|
std::string controlModel = "Control Model: " + holmResult.model;
|
||||||
|
worksheet_merge_range(worksheet, row, 1, row, 7, controlModel.c_str(), styles["bodyHeader_odd"]);
|
||||||
|
row++;
|
||||||
|
writeString(row, 1, "Model", "bodyHeader");
|
||||||
|
writeString(row, 2, "p-value", "bodyHeader");
|
||||||
|
writeString(row, 3, "Rank", "bodyHeader");
|
||||||
|
writeString(row, 4, "Win", "bodyHeader");
|
||||||
|
writeString(row, 5, "Tie", "bodyHeader");
|
||||||
|
writeString(row, 6, "Loss", "bodyHeader");
|
||||||
|
writeString(row, 7, "Reject H0", "bodyHeader");
|
||||||
|
row++;
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& item : holmResult.holmLines) {
|
||||||
|
writeString(row, 1, item.model, "text");
|
||||||
|
if (first) {
|
||||||
|
// Control model info
|
||||||
|
first = false;
|
||||||
|
writeString(row, 2, "", "text");
|
||||||
|
writeDouble(row, 3, item.rank, "result");
|
||||||
|
writeString(row, 4, "", "text");
|
||||||
|
writeString(row, 5, "", "text");
|
||||||
|
writeString(row, 6, "", "text");
|
||||||
|
writeString(row, 7, "", "textCentered");
|
||||||
|
} else {
|
||||||
|
// Rest of the models info
|
||||||
|
writeDouble(row, 2, item.pvalue, "result");
|
||||||
|
writeDouble(row, 3, item.rank, "result");
|
||||||
|
writeInt(row, 4, item.wtl.win, "ints");
|
||||||
|
writeInt(row, 5, item.wtl.tie, "ints");
|
||||||
|
writeInt(row, 6, item.wtl.loss, "ints");
|
||||||
|
writeString(row, 7, item.reject ? "Yes" : "No", "textCentered");
|
||||||
|
}
|
||||||
|
row++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
39
src/Platform/BestResultsExcel.h
Normal file
39
src/Platform/BestResultsExcel.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#ifndef BESTRESULTS_EXCEL_H
|
||||||
|
#define BESTRESULTS_EXCEL_H
|
||||||
|
#include "ExcelFile.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
namespace platform {
|
||||||
|
|
||||||
|
class BestResultsExcel : ExcelFile {
|
||||||
|
public:
|
||||||
|
BestResultsExcel(const std::string& score, const std::vector<std::string>& datasets);
|
||||||
|
~BestResultsExcel();
|
||||||
|
void reportAll(const std::vector<std::string>& models, const json& table, const std::map<std::string, std::map<std::string, float>>& ranks, bool friedman, double significance);
|
||||||
|
void reportSingle(const std::string& model, const std::string& fileName);
|
||||||
|
std::string getFileName();
|
||||||
|
private:
|
||||||
|
void build();
|
||||||
|
void header(bool ranks);
|
||||||
|
void body(bool ranks);
|
||||||
|
void footer(bool ranks);
|
||||||
|
void formatColumns();
|
||||||
|
void doFriedman();
|
||||||
|
void addConditionalFormat(std::string formula);
|
||||||
|
const std::string fileName = "BestResults.xlsx";
|
||||||
|
std::string score;
|
||||||
|
std::vector<std::string> models;
|
||||||
|
std::vector<std::string> datasets;
|
||||||
|
json table;
|
||||||
|
std::map<std::string, std::map<std::string, float>> ranksModels;
|
||||||
|
bool friedman;
|
||||||
|
double significance;
|
||||||
|
int modelNameSize = 12; // Min size of the column
|
||||||
|
int datasetNameSize = 25; // Min size of the column
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif //BESTRESULTS_EXCEL_H
|
28
src/Platform/BestScore.h
Normal file
28
src/Platform/BestScore.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef BESTSCORE_H
|
||||||
|
#define BESTSCORE_H
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <utility>
|
||||||
|
#include "DotEnv.h"
|
||||||
|
namespace platform {
|
||||||
|
class BestScore {
|
||||||
|
public:
|
||||||
|
static std::pair<std::string, double> getScore(const std::string& metric)
|
||||||
|
{
|
||||||
|
static std::map<std::pair<std::string, std::string>, std::pair<std::string, double>> data = {
|
||||||
|
{{"discretiz", "accuracy"}, {"STree_default (linear-ovo)", 22.109799}},
|
||||||
|
{{"odte", "accuracy"}, {"STree_default (linear-ovo)", 22.109799}},
|
||||||
|
};
|
||||||
|
auto env = platform::DotEnv();
|
||||||
|
std::string experiment = env.get("experiment");
|
||||||
|
try {
|
||||||
|
return data[{experiment, metric}];
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
return { "", 0.0 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
22
src/Platform/CLocale.h
Normal file
22
src/Platform/CLocale.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef LOCALE_H
|
||||||
|
#define LOCALE_H
|
||||||
|
#include <locale>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
namespace platform {
|
||||||
|
struct separation : std::numpunct<char> {
|
||||||
|
char do_decimal_point() const { return ','; }
|
||||||
|
char do_thousands_sep() const { return '.'; }
|
||||||
|
std::string do_grouping() const { return "\03"; }
|
||||||
|
};
|
||||||
|
class ConfigLocale {
|
||||||
|
public:
|
||||||
|
explicit ConfigLocale()
|
||||||
|
{
|
||||||
|
std::locale mylocale(std::cout.getloc(), new separation);
|
||||||
|
std::locale::global(mylocale);
|
||||||
|
std::cout.imbue(mylocale);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
22
src/Platform/CMakeLists.txt
Normal file
22
src/Platform/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
include_directories(${BayesNet_SOURCE_DIR}/src/BayesNet)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/src/PyClassifiers)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
|
||||||
|
include_directories(${BayesNet_SOURCE_DIR}/lib/libxlsxwriter/include)
|
||||||
|
include_directories(${Python3_INCLUDE_DIRS})
|
||||||
|
include_directories(${MPI_CXX_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
add_executable(b_best b_best.cc BestResults.cc Result.cc Statistics.cc BestResultsExcel.cc ReportExcel.cc ReportBase.cc Datasets.cc Dataset.cc ExcelFile.cc)
|
||||||
|
add_executable(b_grid b_grid.cc GridSearch.cc GridData.cc HyperParameters.cc Folding.cc Datasets.cc Dataset.cc)
|
||||||
|
add_executable(b_list b_list.cc Datasets.cc Dataset.cc)
|
||||||
|
add_executable(b_main b_main.cc Folding.cc Experiment.cc Datasets.cc Dataset.cc Models.cc HyperParameters.cc ReportConsole.cc ReportBase.cc)
|
||||||
|
add_executable(b_manage b_manage.cc Results.cc ManageResults.cc CommandParser.cc Result.cc ReportConsole.cc ReportExcel.cc ReportBase.cc Datasets.cc Dataset.cc ExcelFile.cc)
|
||||||
|
|
||||||
|
target_link_libraries(b_best Boost::boost "${XLSXWRITER_LIB}" "${TORCH_LIBRARIES}" ArffFiles mdlp)
|
||||||
|
target_link_libraries(b_grid BayesNet PyWrap ${MPI_CXX_LIBRARIES})
|
||||||
|
target_link_libraries(b_list ArffFiles mdlp "${TORCH_LIBRARIES}")
|
||||||
|
target_link_libraries(b_main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}" PyWrap)
|
||||||
|
target_link_libraries(b_manage "${TORCH_LIBRARIES}" "${XLSXWRITER_LIB}" ArffFiles mdlp)
|
15
src/Platform/Colors.h
Normal file
15
src/Platform/Colors.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef COLORS_H
|
||||||
|
#define COLORS_H
|
||||||
|
class Colors {
|
||||||
|
public:
|
||||||
|
static std::string MAGENTA() { return "\033[1;35m"; }
|
||||||
|
static std::string BLUE() { return "\033[1;34m"; }
|
||||||
|
static std::string CYAN() { return "\033[1;36m"; }
|
||||||
|
static std::string GREEN() { return "\033[1;32m"; }
|
||||||
|
static std::string YELLOW() { return "\033[1;33m"; }
|
||||||
|
static std::string RED() { return "\033[1;31m"; }
|
||||||
|
static std::string WHITE() { return "\033[1;37m"; }
|
||||||
|
static std::string IBLUE() { return "\033[0;94m"; }
|
||||||
|
static std::string RESET() { return "\033[0m"; }
|
||||||
|
};
|
||||||
|
#endif // COLORS_H
|
87
src/Platform/CommandParser.cc
Normal file
87
src/Platform/CommandParser.cc
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#include "CommandParser.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include "Colors.h"
|
||||||
|
#include "Utils.h"
|
||||||
|
|
||||||
|
namespace platform {
|
||||||
|
void CommandParser::messageError(const std::string& message)
|
||||||
|
{
|
||||||
|
std::cout << Colors::RED() << message << Colors::RESET() << std::endl;
|
||||||
|
}
|
||||||
|
std::pair<char, int> CommandParser::parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int maxIndex)
|
||||||
|
{
|
||||||
|
bool finished = false;
|
||||||
|
while (!finished) {
|
||||||
|
std::stringstream oss;
|
||||||
|
std::string line;
|
||||||
|
oss << color << "Choose option (";
|
||||||
|
bool first = true;
|
||||||
|
for (auto& option : options) {
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
oss << ", ";
|
||||||
|
}
|
||||||
|
oss << std::get<char>(option) << "=" << std::get<std::string>(option);
|
||||||
|
}
|
||||||
|
oss << "): ";
|
||||||
|
std::cout << oss.str();
|
||||||
|
getline(std::cin, line);
|
||||||
|
std::cout << Colors::RESET();
|
||||||
|
line = trim(line);
|
||||||
|
if (line.size() == 0)
|
||||||
|
continue;
|
||||||
|
if (all_of(line.begin(), line.end(), ::isdigit)) {
|
||||||
|
command = defaultCommand;
|
||||||
|
index = stoi(line);
|
||||||
|
if (index > maxIndex || index < 0) {
|
||||||
|
messageError("Index out of range");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
finished = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bool found = false;
|
||||||
|
for (auto& option : options) {
|
||||||
|
if (line[0] == std::get<char>(option)) {
|
||||||
|
found = true;
|
||||||
|
// it's a match
|
||||||
|
line.erase(line.begin());
|
||||||
|
line = trim(line);
|
||||||
|
if (std::get<bool>(option)) {
|
||||||
|
// The option requires a value
|
||||||
|
if (line.size() == 0) {
|
||||||
|
messageError("Option " + std::get<std::string>(option) + " requires a value");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
index = stoi(line);
|
||||||
|
if (index > maxIndex || index < 0) {
|
||||||
|
messageError("Index out of range");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::invalid_argument& ia) {
|
||||||
|
messageError("Invalid value: " + line);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (line.size() > 0) {
|
||||||
|
messageError("option " + std::get<std::string>(option) + " doesn't accept values");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
command = std::get<char>(option);
|
||||||
|
finished = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
messageError("I don't know " + line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { command, index };
|
||||||
|
}
|
||||||
|
} /* namespace platform */
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user