Skip to content

Commit

Permalink
Merge pull request #23 from cs-24-sw-8-11/extended-prediction-tests
Browse files Browse the repository at this point in the history
Extended prediction tests
  • Loading branch information
Taoshix authored Apr 10, 2024
2 parents 6fe8a6c + 5a8db02 commit 4a53872
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use nix
use flake
8 changes: 4 additions & 4 deletions .github/workflows/valgrind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
env:
CXX: g++-13
- name: test-database-tests
run: valgrind ./build/src/database_test
run: valgrind ./build/bin/database_test
- name: test-api-tests
run: valgrind ./build/src/api_test
run: valgrind ./build/bin/api_test
- name: test-questionare-tests
run: valgrind ./build/src/questionaire_test
run: valgrind ./build/bin/questionaire_test
- name: test-predictions-test
run: valgrind ./build/src/predictions_test
run: valgrind ./build/bin/predictions_test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ __pycache__
*.db3
build
test.cpp
.vscode
.vscode
.direnv
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.15)
project(cs-24-sw-8-11-Backend)
set(CMAKE_CXX_STANDARD 23)
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )

enable_testing()

Expand Down
8 changes: 8 additions & 0 deletions README.md
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
```
26 changes: 26 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions flake.nix
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;
};
}
41 changes: 31 additions & 10 deletions include/PredictionManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,50 @@
#include <string>
#include <vector>
#include <cmath>
#include <ranges>

// these will be used in later implementations
#define LOW_STRESS 0
#define MODERATE_STRESS 14
#define HIGH_STRESS 27

std::vector<double> normalizeCDF(std::vector<double> values){ // https://stackoverflow.com/questions/2328258/cumulative-normal-distribution-function-in-c-c lmao
std::vector<double> results;
template<typename T = double>
T sum(std::vector<T> values){
T result;
for(auto value : values){
results.push_back(0.5 * erfc(-value * M_SQRT1_2));
result += value;
}
return results;
return result;
}

double sum(std::vector<double> values){
double result;
template<typename T = double>
T mean(std::vector<T> values){
return sum(values)/values.size();
}

template<typename T = double>
std::vector<T> normalizeCDF(std::vector<T> values){ // https://stackoverflow.com/questions/2328258/cumulative-normal-distribution-function-in-c-c lmao
std::vector<T> results;
for(auto value : values){
result += value;
results.push_back(0.5 * erfc(-value * M_SQRT1_2));
}
return result;
return results;
}
double mean(std::vector<double> values){
return sum(values)/values.size();

template<typename T = double>
std::function<T(T)> calculate_regression(std::vector<T> xs, std::vector<T> ys){
auto x_mean = mean(xs);
auto y_mean = mean(ys);
auto numerator = 0.0;
auto denominator = 0.0;
for(auto [x, y] : std::ranges::zip_view(xs, ys)){
numerator += (x-x_mean) * (y-y_mean);
denominator += pow(x - x_mean, 2);
}
auto slope = numerator / denominator;
auto intercept = y_mean - slope * x_mean;

return [=](T x){ return intercept + (slope * x); };
}

class PredictionBuilder {
Expand Down
24 changes: 0 additions & 24 deletions shell.nix

This file was deleted.

106 changes: 82 additions & 24 deletions src/predictions_test.cpp
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();
};
2 changes: 1 addition & 1 deletion tests/api-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ password="$(cat ./files/testdata/default.json | jq -r .user.password)"
authjson="{\"username\":\"$username\", \"password\": \"$password\"}"

# start phase
build/src/backend --port "$(cat ./files/testdata/default.json | jq -r .port)" &
build/bin/backend --port "$(cat ./files/testdata/default.json | jq -r .port)" &
sleep 1


Expand Down

0 comments on commit 4a53872

Please sign in to comment.