-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from cs-24-sw-8-11/extended-prediction-tests
Extended prediction tests
- Loading branch information
Showing
11 changed files
with
203 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
use nix | ||
use flake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ __pycache__ | |
*.db3 | ||
build | ||
test.cpp | ||
.vscode | ||
.vscode | ||
.direnv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# P8 Backend software | ||
|
||
* Install: | ||
|
||
if using nix flakes, run | ||
```sh | ||
nix shell github:cs-24-sw-8-11/Backend | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
description = "P8 Project nix flake"; | ||
inputs = { | ||
nixpkgs.url = "nixpkgs/nixos-unstable"; | ||
}; | ||
outputs = { self, nixpkgs }: let | ||
system = "x86_64-linux"; | ||
pkgs = nixpkgs.legacyPackages.${system}; | ||
build-dir = "./build"; | ||
compile = pkgs.writeScriptBin "build-project" '' | ||
${pkgs.cmake}/bin/cmake -B ${build-dir} | ||
${pkgs.cmake}/bin/cmake --build ${build-dir} | ||
''; | ||
run = pkgs.writeScriptBin "run-project" '' | ||
${build-dir}/src/backend $@ | ||
''; | ||
test = pkgs.writeScriptBin "test-project" '' | ||
${pkgs.cmake}/bin/ctest --test-dir ${build-dir} $@ | ||
''; | ||
|
||
backend = pkgs.stdenv.mkDerivation { | ||
name = "Backend"; | ||
pname = "Backend"; | ||
src = pkgs.fetchgit { | ||
rev = "6fe8a6c"; | ||
url = "https://github.com/cs-24-sw-8-11/Backend"; | ||
hash = "sha256-lxewBXvm1jCP6lYFtGL57CIknYvEXD+8N0Ktfk4Mgcc="; | ||
fetchSubmodules = true; | ||
}; | ||
nativeBuildInputs = [ pkgs.cmake pkgs.gcc ]; | ||
}; | ||
|
||
in { | ||
devShells.${system}.default = pkgs.mkShell { | ||
packages = [ | ||
compile | ||
run | ||
test | ||
backend | ||
pkgs.sqlite | ||
pkgs.sqlite-interactive | ||
pkgs.cmake | ||
]; | ||
}; | ||
packages.default = backend; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,116 @@ | ||
#include <format> | ||
#include <functional> | ||
|
||
#include "PredictionManager.hpp" | ||
#include "TestTemplate.hpp" | ||
|
||
auto num_entries = 1000; | ||
|
||
class PredictionTest : public Test<std::function<void()>> { | ||
void init(){ | ||
} | ||
public: | ||
PredictionManager manager; | ||
}; | ||
|
||
template<typename T = int> | ||
std::vector<T> make_range(int n, std::function<T(int)> functor){ | ||
std::vector<T> result; | ||
for(auto i = 0; i < n; i++){ | ||
result.push_back(functor(i)); | ||
} | ||
return result; | ||
} | ||
|
||
template<typename T = int> | ||
std::vector<T> make_range(int n){ | ||
std::vector<T> result; | ||
for(auto i = 0; i < n; i++) | ||
result.push_back((T)i); | ||
return result; | ||
} | ||
|
||
template<typename T = double> | ||
bool is_sorted(std::vector<T> values){ | ||
for(auto i = 0; i < values.size()-1; i++){ | ||
if(values[i] > values[i+1]){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
int main(){ | ||
PredictionTest test; | ||
test.add_test("add-valued-data", [&](){ | ||
auto uid = 1; | ||
auto data = { //list of pairs, keys are the qid and values are the valued data | ||
std::make_pair("1", 0.1), | ||
std::make_pair("2", 0.9), | ||
std::make_pair("3", 0.6), | ||
std::make_pair("4", 0.7) | ||
}; | ||
auto data = make_range<std::pair<std::string, double>>(num_entries, [](int i){ | ||
return std::make_pair(std::format("{}", i), i/num_entries); | ||
}); | ||
auto predictionBuilder = test.manager.create_new_prediction(uid); | ||
for(auto pair : data){ | ||
predictionBuilder.add_valued_data(pair.first, pair.second); | ||
for(auto [name, value] : data){ | ||
predictionBuilder.add_valued_data(name, value); | ||
} | ||
assert(predictionBuilder.size() == data.size()); | ||
}); | ||
test.add_test("add-boolean-data", [&](){ | ||
auto uid = 1; | ||
auto data = { | ||
std::make_pair("1", true), | ||
std::make_pair("2", false), | ||
std::make_pair("3", false), | ||
std::make_pair("4", true) | ||
}; | ||
auto data = make_range<std::pair<std::string, bool>>(num_entries, [](int i){ | ||
return std::make_pair(std::format("{}", i), i%2==0); | ||
}); | ||
auto predictionBuilder = test.manager.create_new_prediction(uid); | ||
for(auto pair : data){ | ||
predictionBuilder.add_boolean_data(pair.first, pair.second); | ||
for(auto [name, value] : data){ | ||
predictionBuilder.add_boolean_data(name, value); | ||
} | ||
assert(predictionBuilder.size() == data.size()); | ||
}); | ||
test.add_test("run-prediction", [&](){ | ||
auto uid = 1; | ||
auto data = { | ||
std::make_pair("1", true), | ||
std::make_pair("2", false), | ||
std::make_pair("3", false), | ||
std::make_pair("4", false) | ||
}; | ||
auto data = make_range<std::pair<std::string, bool>>(num_entries, [](int i){ | ||
return std::make_pair(std::format("{}", i), true); | ||
}); | ||
|
||
auto predictionBuilder = test.manager.create_new_prediction(uid); | ||
for(auto pair : data){ | ||
predictionBuilder.add_boolean_data(pair.first, pair.second); | ||
for(auto [name, value] : data){ | ||
predictionBuilder.add_boolean_data(name, value); | ||
} | ||
auto stresslevel = predictionBuilder.build(); // function that does the heavy lifting | ||
assert(stresslevel >= 0.0 && stresslevel <= 1.0); | ||
}); | ||
test.run(); | ||
|
||
PredictionTest extended; | ||
|
||
extended.add_test("regression", [&](){ | ||
auto xs = make_range<double>(100); | ||
auto ys = make_range<double>(100); | ||
auto f = calculate_regression(xs, ys); | ||
std::vector<double> result; | ||
for(auto x : xs){ | ||
result.push_back(f(x+100)); | ||
} | ||
|
||
for(auto [value, expectedValue] : std::ranges::zip_view(result, make_range<double>(100, [](int i){return i+100;}))){ | ||
assert(value == expectedValue); | ||
} | ||
}); | ||
|
||
extended.add_test("applied regression", [&](){ | ||
auto uid = 1; | ||
std::vector<double> result; | ||
for(auto i = 0; i < num_entries; i++){ | ||
auto data = make_range<std::pair<std::string, double>>(5, [](int i){ | ||
return std::make_pair(std::format("{}", i), (i%5)+i); // positive linear data | ||
}); | ||
auto predictionBuilder = extended.manager.create_new_prediction(uid); | ||
for(auto [name, value] : data){ | ||
predictionBuilder.add_valued_data(name, value); | ||
} | ||
result.push_back(predictionBuilder.build()); | ||
} | ||
auto f = calculate_regression(make_range<double>(result.size()), result); | ||
assert(is_sorted(make_range<double>(1000, [=](int i){ return f(i); }))); | ||
}); | ||
|
||
extended.run(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters