17
19
20:- module(cplint_r,
21 [ build_xy_list/3,
22 r_row/3,
23 get_set_from_xy_list/2,
24 prob_bar_r/1,
25 prob_bar_r/2,
26 mc_prob_bar_r/1,
27 mc_sample_bar_r/2,
28 mc_sample_arg_bar_r/3,
29 mc_sample_arg_first_bar_r/3,
30 mc_rejection_sample_arg_bar_r/4,
31 mc_mh_sample_arg_bar_r/5,
32 mc_mh_sample_arg_bar_r/6,
33 histogram_r/2,
34 density_r/1,
35 densities_r/2,
36 compute_areas_diagrams_r/3,
37 test_r/5
38 ]). 39
40
42
43:- use_module(library(r/r_call)). 44:- use_module(library(r/r_data)). 45:- use_module(library(lists)). 46:- use_module(library(pita)). 47:- use_module(library(mcintyre)). 48:- use_module(library(auc)). 49:- use_module(library(slipcover)). 50
52:- use_module(swish(lib/r_swish)). 53
55
56:-meta_predicate prob_bar_r(:). 57:-meta_predicate prob_bar_r(:,:). 58:-meta_predicate mc_prob_bar_r(:). 59:-meta_predicate mc_sample_bar_r(:,+). 60:-meta_predicate mc_sample_arg_bar_r(:,+,+). 61:-meta_predicate mc_sample_arg_first_bar_r(:,+,+). 62:-meta_predicate mc_rejection_sample_arg_bar_r(:,:,+,+). 63:-meta_predicate mc_mh_sample_arg_bar_r(:,:,+,+,+). 64:-meta_predicate mc_mh_sample_arg_bar_r(:,:,+,+,+,+). 65:-meta_predicate test_r(:,+,-,-,-). 66
67:- multifile sandbox:safe_primitive/1. 68sandbox:safe_primitive(current_predicate(_)).
69
73
74check_modules :-
75 current_module(r_call),
76 current_module(r_data),
77 current_module(lists),
78 current_module(pita),
79 current_module(mcintyre),
80 current_module(auc),
81 current_module(slipcover).
82
83check_modules :-
84 writeln("ERROR: Library/ies missing"),
85 abort.
86
87/* Debug purposes
88 *
89 * use_rendering(table). % should be available by default.
90 * <- df. % name of a data frame.
91 */
92
93load_r_libraries :-
94 /* To enable pdf output instead of using the default plotting window, add
95 * the following command
96 */
97 /* <- pdf("plot.pdf"), */
98 check_modules,
99 !,
100 <- library("ggplot2").
101
103finalize_r_graph :-
104 current_predicate(r_download/0),
105 !,
106 r_download.
107
108finalize_r_graph :-
109 true.
110
111bin_width(Min,Max,NBins,Width) :-
112 D is Max-Min,
113 Width is D/NBins.
121build_xy_list([], [], []).
122
123build_xy_list([XH|XT], [YH|YT], [XH-YH|Out]) :-
124 build_xy_list(XT, YT, Out).
131r_row(X,Y,r(X,Y)).
141get_set_from_xy_list(L,R) :-
142 maplist(key,L,X),
143 maplist(y,L,Y),
144 maplist(r_row,X,Y,R).
145
146
147/*******************************************
148 * Plot predicates *************************
149 *******************************************
150 * cd swish/examples/inference *************
151 * grep -l "<predicate_name>(" *.pl | less *
152 *******************************************/
153
154/* pita */
155
156
157/* Scale between 0 and 1 with 10 ticks (0.1,0.2,...,1)
158 * This represents a probability between 0 and 1.
159 */
160geom_prob_bar(PTrue,PFalse) :-
161 X=['T','F'],
162 Y=[PTrue,PFalse],
163 build_xy_list(X,Y,L),
164 get_set_from_xy_list(L,R),
165 r_data_frame_from_rows(df1, R),
166 colnames(df1) <- c("names", "prob"), df <- data.frame( ids=as.character(df1$names), probabilities=c(df1$prob) ), <- ggplot( data=df, aes( x=ids, y=probabilities, fill=ids ) ) + geom_bar( stat="identity", width=0.5 ) + scale_y_continuous( breaks=seq(0,1,0.1) ) + coord_flip( ylim=c(0,1) ) + theme( aspect.ratio=1/2 ).
201prob_bar_r(M:Goal) :-
202 load_r_libraries,
203 s(M:Goal,PT),
204 PF is 1.0-PT,
205 geom_prob_bar(PT,PF),
206 finalize_r_graph.
224prob_bar_r(M:Goal,M:Evidence):-
225 load_r_libraries,
226 prob(M:Goal,M:Evidence,PT),
227 PF is 1.0-PT,
228 geom_prob_bar(PT,PF),
229 finalize_r_graph.
230
239mc_prob_bar_r(M:Goal):-
240 load_r_libraries,
241 mc_prob(M:Goal,PT),
242 PF is 1.0-PT,
243 geom_prob_bar(PT,PF),
244 finalize_r_graph.
245
246
247geom_mc_sample_bar(PTrue,PFalse) :-
248 X=['T','F'],
249 Y=[PTrue,PFalse],
250 build_xy_list(X,Y,L),
251 get_set_from_xy_list(L,R),
252 r_data_frame_from_rows(df1, R),
253 colnames(df1) <- c("names", "prob"), df <- data.frame( ids=as.character(df1$names), probabilities=c(df1$prob) ), <- ggplot( data=df, aes( x=ids, y=probabilities, fill=ids ) ) + geom_bar( stat="identity", width=0.5 ) + coord_flip() + theme(aspect.ratio=1/2).
280mc_sample_bar_r(M:Goal,S):-
281 load_r_libraries,
282 mc_sample(M:Goal,S,T,F,_P),
283 geom_mc_sample_bar(T,F),
284 finalize_r_graph.
285
286
287/* Differences from the previous predicates:
288 * =========================================
289 *
290 * Transform names column into a string column.
291 *
292 * The use of max/1 instead of 'NA'/0
293 * is a hack (because NA does not work).
294 *
295 * Reorder by decreasing frequency.
296 */
297geom_mc_sample_arg_bar(L) :-
298 get_set_from_xy_list(L,R),
299 r_data_frame_from_rows(df1, R),
300 colnames(df1) <- c("names", "prob"), df <- data.frame( ids=as.character(df1$names), probabilities=c(df1$prob) ), scalingthresholD <- 20, {|r||xbreakS <- if(max(df$probabilities) > scalingthresholD) element_blank() else scale_y_continuous(breaks=seq(0,max(df$probabilities),1))|}, <- ggplot( data=df, aes( x=reorder( ids, probabilities ), y=probabilities ) ) + geom_bar( stat="identity", width=0.5 ) + xbreakS + coord_flip( ylim=c(0,max(df$probabilities)) ) + theme( axis.title.y=element_blank() ).
337mc_sample_arg_bar_r(M:Goal,S,Arg):-
338 load_r_libraries,
339 mc_sample_arg(M:Goal,S,Arg,ValList0),
340 maplist(to_atom,ValList0,ValList),
341 geom_mc_sample_arg_bar(ValList),
342 finalize_r_graph.
343
344
345geom_mc_sample_arg_first_bar(L) :-
346 get_set_from_xy_list(L,R),
347 r_data_frame_from_rows(df1, R),
348 colnames(df1) <- c("names", "prob"), df <- data.frame( ids=as.character(df1$names), probabilities=c(df1$prob) ), <- ggplot( data=df, aes( x=reorder( ids, probabilities ), y=probabilities ) ) + geom_bar( stat="identity", width=0.5 ) + coord_flip() + theme( axis.title.y=element_blank() ).
381mc_sample_arg_first_bar_r(M:Goal,S,Arg):-
382 load_r_libraries,
383 mc_sample_arg_first(M:Goal,S,Arg,ValList0),
384 maplist(to_atom,ValList0,ValList),
385 geom_mc_sample_arg_first_bar(ValList),
386 finalize_r_graph.
387
388
389geom_mc_rejection_sample_arg_bar(L) :-
390 geom_mc_sample_arg_first_bar(L).
403mc_rejection_sample_arg_bar_r(M:Goal,M:Ev,S,Arg):-
404 load_r_libraries,
405 mc_rejection_sample_arg(M:Goal,M:Ev,S,Arg,ValList0),
406 maplist(to_atom,ValList0,ValList),
407 geom_mc_rejection_sample_arg_bar(ValList),
408 finalize_r_graph.
409
410
411geom_mc_mh_sample_arg_bar(L) :-
412 geom_mc_sample_arg_first_bar(L).
426mc_mh_sample_arg_bar_r(M:Goal,M:Ev,S,Mix,L,Arg):-
427 load_r_libraries,
428 mc_mh_sample_arg(M:Goal,M:Ev,S,Mix,L,Arg,ValList0),
429 maplist(to_atom,ValList0,ValList),
430 geom_mc_mh_sample_arg_bar(ValList),
431 finalize_r_graph.
446mc_mh_sample_arg_bar_r(M:Goal,M:Ev,S,L,Arg):-
447 load_r_libraries,
448 mc_mh_sample_arg(M:Goal,M:Ev,S,L,Arg,ValList0),
449 maplist(to_atom,ValList0,ValList),
450 geom_mc_mh_sample_arg_bar(ValList),
451 finalize_r_graph.
452
453
454geom_histogram(L,Min,Max,BinWidth) :-
455 binwidtH <- BinWidth, get_set_from_xy_list(L,R), r_data_frame_from_rows(df, R), colnames(df) <- c("x", "y"), miN <- Min, maX <- Max, <- ggplot( data=df, aes_string( x="x" ) ) + geom_histogram( weight="y", binwidth=binwidtH ) + xlim( miN, maX ) + theme( axis.title.x=element_blank() ).
483histogram_r(L0,NBins) :-
484 load_r_libraries,
485 maplist(to_pair,L0,L1),
486 maplist(key,L1,L2),
487 max_list(L2,Max),
488 min_list(L2,Min),
489 histogram_r(L0,NBins,Min,Max),
490 finalize_r_graph.
500histogram_r(L0,NBins,Min,Max) :-
501 maplist(to_pair,L0,L1),
502 keysort(L1,L),
503 bin_width(Min,Max,NBins,BinWidth),
504 geom_histogram(L,Min,Max,BinWidth).
505
506
507geom_density(L) :-
508 get_set_from_xy_list(L,R),
509 r_data_frame_from_rows(df, R),
510 colnames(df) <- c("x", "y"), <- ggplot( data=df, aes(x) ) + geom_density( aes( fill="density", weights=y ), alpha=0.5 ) + theme( legend.title = element_blank(), axis.title.x=element_blank() ).
532density_r(Post0) :-
533 load_r_libraries,
534 maplist(to_pair,Post0,Post),
535 geom_density(Post),
536 finalize_r_graph.
537
538
539geom_densities(LPr,LPo) :-
540 get_set_from_xy_list(LPr,R1),
541 get_set_from_xy_list(LPo,R2),
542 r_data_frame_from_rows(df1, R1),
543 r_data_frame_from_rows(df2, R2),
544 colnames(df1) <- c("x1", "y1"), colnames(df2) <- c("x2", "y2"), df <- data.frame( x1=df1$x1, x2=df2$x2, y1=df1$y1, y2=df2$y2 ), alphA <- 0.5, <- ggplot( data=df ) + geom_density( aes( x=x1, fill="pre", weights=y1 ), alpha=alphA ) + geom_density( aes( x=x2, fill="post", weights=y2 ), alpha=alphA ) + theme( legend.title = element_blank(), axis.title.x=element_blank() ).
582densities_r(Pri0,Post0) :-
583 load_r_libraries,
584 maplist(to_pair,Pri0,Pri1),
585 maplist(to_pair,Post0,Post1),
586 geom_densities(Pri1,Post1),
587 finalize_r_graph.
588
589/* auc */
590
591geom_compute_areas_diagram(L,Title,XName,YName) :-
592 get_set_from_xy_list(L,R),
593 r_data_frame_from_rows(df, R),
594 titlE <- as.character(Title), labelS <- labs(title = titlE, x=XName, y=YName), colnames(df) <- c("x", "y"), <- ggplot( data=df, aes_string( x="x", y="y", group=1 ) ) + geom_line() + geom_point() + scale_x_continuous( limits=c(0,1), breaks=seq(0,1,0.1) ) + scale_y_continuous( limits=c(0,1), breaks=seq(0,1,0.1) ) + labelS + theme( legend.title = element_blank(), plot.title = element_text( size = rel(2) ) ).
637compute_areas_diagrams_r(LG,AUCROC,AUCPR) :-
638 load_r_libraries,
639 compute_areas(LG,AUCROC,ROC0,AUCPR,PR0),
640 geom_compute_areas_diagram(ROC0,"ROC","FPR","TPR"),
641 finalize_r_graph,
642 load_r_libraries,
643 geom_compute_areas_diagram(PR0,"PR","Precision","Recall"),
644 finalize_r_graph.
655test_r(P,TestFolds,LL,AUCROC,AUCPR):-
656 test_prob(P,TestFolds,_NPos,_NNeg,LL,LG),
657 compute_areas_diagrams_r(LG,AUCROC,AUCPR).
658
659:- multifile sandbox:safe_primitive/1. 660
661sandbox:safe_primitive(cplint_r:build_xy_list(_,_,_)).
662sandbox:safe_primitive(cplint_r:r_row(_,_,_)).
663sandbox:safe_primitive(cplint_r:get_set_from_xy_list(_,_)).
664sandbox:safe_primitive(cplint_r:histogram_r(_,_)).
665sandbox:safe_primitive(cplint_r:density_r(_)).
666sandbox:safe_primitive(cplint_r:densities_r(_,_)).
667sandbox:safe_primitive(cplint_r:compute_areas_diagrams_r(_,_,_)).
668
669:- multifile sandbox:safe_meta/2. 670
671sandbox:safe_meta(cplint_r:prob_bar_r(_),[]).
672sandbox:safe_meta(cplint_r:prob_bar_r(_,_),[]).
673sandbox:safe_meta(cplint_r:mc_prob_bar_r(_),[]).
674sandbox:safe_meta(cplint_r:mc_sample_bar_r(_,_),[]).
675sandbox:safe_meta(cplint_r:mc_sample_arg_bar_r(_,_,_),[]).
676sandbox:safe_meta(cplint_r:mc_sample_arg_first_bar_r(_,_,_),[]).
677sandbox:safe_meta(cplint_r:mc_rejection_sample_arg_bar_r(_,_,_,_),[]).
678sandbox:safe_meta(cplint_r:mc_mh_sample_arg_bar_r(_,_,_,_,_),[]).
679sandbox:safe_meta(cplint_r:mc_mh_sample_arg_bar_r(_,_,_,_,_,_),[]).
680sandbox:safe_meta(cplint_r:test_r(_,_,_,_,_), [])