From 308d0afcb6096d9b33ff4eee316cc3be6173b826 Mon Sep 17 00:00:00 2001
From: Jakhes <dean.schmitz@schmitzbauer.de>
Date: Tue, 30 Aug 2022 14:41:12 +0200
Subject: [PATCH] Moving all extra predicates into a helper file

---
 src/helper_files/helper.pl                    | 60 +++++++++++++++++++
 .../bayesian_linear_regression.pl             | 56 ++---------------
 2 files changed, 65 insertions(+), 51 deletions(-)
 create mode 100644 src/helper_files/helper.pl

diff --git a/src/helper_files/helper.pl b/src/helper_files/helper.pl
new file mode 100644
index 0000000..153ab43
--- /dev/null
+++ b/src/helper_files/helper.pl
@@ -0,0 +1,60 @@
+:- module(helper, [convert_list_to_float_array/3, convert_list_to_float_array/2, fill_float_array/3, convert_float_array_to_list/3 , convert_float_array_to_list/4, len/2, convert_record_to_arr/2, take_csv_row/1]).
+
+:- use_module(library(structs)).
+:- use_module(library(csv)).
+
+
+%% Functions for editing float arrays
+
+convert_list_to_float_array(Arr, Row_num, array(Size, Row_num,Mem)) :-
+        len(Arr, Size),
+        Size >= Row_num,
+        Row_num > 0,
+        mod(Size, Row_num) =:= 0,
+        new(float_array, Size, Mem),
+        fill_float_array(Arr, 0, Mem).
+
+convert_list_to_float_array(Arr, array(Size,Mem)) :-
+        len(Arr, Size),
+        new(float_array, Size, Mem),
+        fill_float_array(Arr, 0, Mem).
+
+fill_float_array([], _, _):- !.
+fill_float_array([H|Tail], Index, Mem) :-
+        put_contents(Mem, Index, H),
+        New_index is Index + 1,
+        fill_float_array(Tail, New_index, Mem).
+
+convert_float_array_to_list(Mem, Count, Out) :-
+        convert_float_array_to_list(Mem, 0, Count, Out).
+
+convert_float_array_to_list(_, Count, Count, []) :- !.
+
+convert_float_array_to_list(Mem, Index, Count, [Val|Rest]) :-
+        NewIndex is Index + 1,
+        convert_float_array_to_list(Mem, NewIndex, Count, Rest),
+        get_contents(Mem, Index , Val).
+
+
+
+
+%% returns the length of a list
+len([], 0):- !.
+len([_|Tail], List_L) :-
+        len(Tail, Tail_L),
+        List_L is Tail_L + 1.
+
+%% Funktions for reading the csv Files
+%% take the elements in a csv record and put them in a list
+convert_record_to_arr([], []):- !.
+convert_record_to_arr([float(Num,_)|Tail], [Num|Rest]) :-
+        convert_record_to_arr(Tail, Rest).
+convert_record_to_arr([string(_)|Tail], Rest) :-
+        convert_record_to_arr(Tail, Rest).
+
+
+take_csv_row(Out) :-
+        open('/home/afkjakhes/eclipse-workspace/Prolog mlpack Libary/iris.csv', read, A),
+        read_record(A, _),
+        read_record(A, Rec),
+        convert_record_to_arr(Rec, Out).
\ No newline at end of file
diff --git a/src/methods/bayesian_linear_regression/bayesian_linear_regression.pl b/src/methods/bayesian_linear_regression/bayesian_linear_regression.pl
index 356dd27..aa25c55 100644
--- a/src/methods/bayesian_linear_regression/bayesian_linear_regression.pl
+++ b/src/methods/bayesian_linear_regression/bayesian_linear_regression.pl
@@ -1,9 +1,12 @@
+:- module(bayesian_linear_regression, [initModel/4, alpha/1, beta/1, dataOffset/2, dataScale/2, omega/2, predict/5, predictWithStd/7, rmse/6, train/5, variance/1]).
+
 :- load_files(library(str_decl),
               [when(compile_time), if(changed)]).
 
 %% needed for using the array type and for reading from csv
 :- use_module(library(structs)).
 :- use_module(library(csv)).
+:- use_module('/home/afkjakhes/git/prolog-mlpack-libary/src/helper_files/helper').
 
 %% type definitions for the float array
 :- foreign_type
@@ -29,60 +32,10 @@ foreign_resource(bayesian_linear_regression, [initModel, alpha, beta, dataOffset
 :- load_foreign_resource(bayesian_linear_regression).
 
 
-%% Functions for editing float arrays
-
-convert_list_to_float_array(Arr, Row_num, array(Size, Row_num,Mem)) :-
-        len(Arr, Size),
-        new(float_array, Size, Mem),
-        fill_float_array(Arr, 0, Mem).
-
-convert_list_to_float_array(Arr, array(Size,Mem)) :-
-        len(Arr, Size),
-        new(float_array, Size, Mem),
-        fill_float_array(Arr, 0, Mem).
-
-fill_float_array([], _, _):- !.
-fill_float_array([H|Tail], Index, Mem) :-
-        put_contents(Mem, Index, H),
-        New_index is Index + 1,
-        fill_float_array(Tail, New_index, Mem).
-
-convert_float_array_to_list(Mem, Count, Out) :-
-        convert_float_array_to_list(Mem, 0, Count, Out).
-
-convert_float_array_to_list(_, Count, Count, []) :- !.
-
-convert_float_array_to_list(Mem, Index, Count, [Val|Rest]) :-
-        NewIndex is Index + 1,
-        convert_float_array_to_list(Mem, NewIndex, Count, Rest),
-        get_contents(Mem, Index , Val).
-
-
-%% Funktions for reading the csv Files
-
-%% returns the length of a list
-len([], 0):- !.
-len([_|Tail], List_L) :-
-        len(Tail, Tail_L),
-        List_L is Tail_L + 1.
-
-%% take the elements in a csv record and put them in a list
-convert_record_to_arr([], []):- !.
-convert_record_to_arr([float(Num,_)|Tail], [Num|Rest]) :-
-        convert_record_to_arr(Tail, Rest).
-convert_record_to_arr([string(_)|Tail], Rest) :-
-        convert_record_to_arr(Tail, Rest).
-
-
-take_csv_row(Out) :-
-        open('/home/afkjakhes/eclipse-workspace/Prolog mlpack Libary/iris.csv', read, A),
-        read_record(A, _),
-        read_record(A, Rec),
-        convert_record_to_arr(Rec, Out).
-
 
 %% Some funktions that use the BayesianLinearRegression
 
+%% First trains the model and then tries to predict the targets and the std for the given data
 predict(PredictList, StdList) :-
         train,
         convert_list_to_float_array([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5],3, array(Xsize, Xrownum, X)),
@@ -90,6 +43,7 @@ predict(PredictList, StdList) :-
         convert_float_array_to_list(Predic, PredicSize, PredictList),
         convert_float_array_to_list(Std, StdSize, StdList).
 
+%% Trains the model with the given data and targets
 train :-
         convert_list_to_float_array([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5],3, array(Xsize, Xrownum, X)),
         convert_list_to_float_array([0.2,0.2,0.2,0.2], array(Ysize, Y)),
-- 
GitLab