Merge and aggregate datasets: Difference between revisions

Content added Content deleted
(→‎{{header|Prolog}}: Use date atoms in XSB implementation)
(→‎{{header|Prolog}}: Update both versions)
Line 846: Line 846:
Mean is Sum/Count.
Mean is Sum/Count.


test :-
bagof(summaryDates(Id, Lastname, LastDate),
summaryDates(Id, Lastname, LastDate),
summaryDates(Id, Lastname, LastDate),
writeln(summaryDates(Id, Lastname, LastDate)),
Bag), writeln(Bag), fail.
fail.


test :-
bagof(summaryScores(Id, Lastname, ScoreSum, ScoreMean),
summaryScores(Id, Lastname, ScoreSum, ScoreMean),
summaryScores(Id, Lastname, ScoreSum, ScoreMean),
writeln(summaryScores(Id, Lastname, ScoreSum, ScoreMean)),
Bag), writeln(Bag), fail.</lang>
fail.</lang>
{{output}}
{{output}}
<pre>[summaryDates(1001,Hopper,2020-11-19),summaryDates(2002,Gosling,2020-10-08),summaryDates(3003,Kemeny,2020-11-12),summaryDates(4004,Wirth,2020-11-05)]
<pre>summaryDates(1001,Hopper,2020-11-19)
summaryDates(2002,Gosling,2020-10-08)
[summaryScores(1001,Hopper,17.4,5.8),summaryScores(2002,Gosling,6.8,6.8),summaryScores(4004,Wirth,15.4,7.7)]
summaryDates(3003,Kemeny,2020-11-12)
summaryDates(4004,Wirth,2020-11-05)
summaryScores(1001,Hopper,17.4,5.8)
summaryScores(2002,Gosling,6.8,6.8)
summaryScores(4004,Wirth,15.4,7.7)
false.</pre>
false.</pre>
Implemented using XSB Prolog:
Implemented using XSB Prolog:
<lang prolog>:- import bagMax/2, bagCount/2, bagSum/2 from aggregs.
<lang prolog>:- import bagMax/2, bagCount/2, bagSum/2, bagReduce/4 from aggregs.
:- import number_codes/2 from basics.
:- import julian_date/7, date_string/3 from iso8601.
:- import load_csv/2, add_cvt_type_hook/2 from proc_files.


?- add_cvt_type_hook(date,date_converter(_,_)).
patient(1001,'Hopper').
patient(4004,'Wirth').
patient(3003,'Kemeny').
patient(2002,'Gosling').
patient(5005,'Kurtz').
visit(2002,'2020-09-10',6.8).
visit(1001,'2020-09-17',5.5).
visit(4004,'2020-09-24',8.4).
visit(2002,'2020-10-08',nan).
visit(1001,'',6.6).
visit(3003,'2020-11-12',nan).
visit(4004,'2020-11-05',7.0).
visit(1001,'2020-11-19',5.3).


date_converter(Atom,Date) :- date_string('YYYY-MM-DD',Date,Atom).
number_datecodes(Number, [Y1,Y2,Y3,Y4,45,M1,M2,45,D1,D2]) :-

number_codes(Number, [Y1,Y2,Y3,Y4,M1,M2,D1,D2]).
:- load_csv('visit.csv',visit(integer,date,float)).
:- load_csv('patient.csv',patient(integer,atom)).


summaryDates(Id, Lastname, LastDate) :-
summaryDates(Id, Lastname, LastDate) :-
bagMax(date_number(Id), LastDateNumber),
bagMax(date_number(Id), LastDateNumber),
patient(Id,Lastname),
patient(Id,Lastname),
number_datecodes(LastDateNumber,LastDateCodes),
julian_date(LastDateNumber, Y, M, D, _, _, _),
date_string('YYYY-MM-DD', date(Y,M,D), LastDate).
atom_codes(LastDate, LastDateCodes).


summaryScores(Id, Lastname, Sum, Mean) :-
summaryScores(Id, Lastname, Sum, Mean) :-
bagSum(scores(Id), Sum),
bagSum(scores(Id), Sum),
bagCount(scores(Id), Count),
bagCount(scores(Id), Count),
Mean is Sum/Count,
patient(Id,Lastname),
patient(Id,Lastname).
Mean is Sum/Count.

test :-
summaryDates(Id,Lastname,LastDate),
writeln(summaryDates(Id,Lastname,LastDate)), fail.

test :-
summaryScores(Id, Lastname, ScoreSum, ScoreMean),
writeln(summaryScores(Id, Lastname, ScoreSum, ScoreMean)), fail.


/* Put hilog declarations together */
/* Put hilog declarations together */


date_number(Id)(Number) :-
date_number(Id)(Number) :-
visit(Id, Date, _), Date \= "",
visit(Id, date(Y,M,D), _),
julian_date(Number, Y, M, D, _, _, _).
atom_codes(Date,Codes),
number_datecodes(Number,Codes).
scores(Id)(Score) :- visit(Id, _, Score), Score \= nan.
scores(Id)(Score) :-
visit(Id, _, Score), Score>0.0. %% nan.0 is very small


:- hilog maximum.
:- hilog maximum.
Line 905: Line 910:
sum(X,Y,Z) :- Z is X+Y.
sum(X,Y,Z) :- Z is X+Y.
:- hilog successor.
:- hilog successor.
successor(X,_Y,Z) :- Z is X+1.
successor(X,_Y,Z) :- Z is X+1.</lang>

bagof(summaryDates(Id,Lastname,LastDate),
summaryDates(Id,Lastname,LastDate),
Bag), writeln(Bag), fail.

bagof(summaryScores(Id, Lastname, ScoreSum, ScoreMean),
summaryScores(Id, Lastname, ScoreSum, ScoreMean),
Bag), writeln(Bag), fail.</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==