b_real.pl -- Interface predicates to commonly used R functions.

Interface predicates to commonly used R functions. This library allows Prolog users to do same simple tasks in R with out writing any R code or call any R functions explicitly.

Dependancies

pack(real)
obviously
r_lib(NMF)
aheatmap/2 depends on this R library
pack(mtx)
some predicates depend on pack mtx which is useful when working with matrices
author
- nicos angelopoulos
version
- 0.1.0 2015/6/17
- 0.1.1 2015/7/24, added gg_bar_plot/2, then on 12/16 added pl_plot_on/2.
- 0.2 2016/1/23
- 0.3 2017/3/11, works with lib 2.0 and stoics_lib
- 0.4 2017/4/25, fixes + r_hist/2.
- 0.7 2021/9/2, scripts/pack_dnloads.pl
- 0.8 2021/12/30, docs spruce up
- 0:9 2022/12/29, small fixes and imporements (eg pl_vector/3)
- 1:0 2023/9/1, gg_lollipop/2, gg_outputs/2
 b_real
Documentation predicate. Anchor reference to the package to this predicate.
 b_real_version(-Version, -Date)
Version (Mj:Mn:Fx) and date and of publication (date(Y,M,D)).
?- b_real_version( Vers, Date ).
Vers = 1:0:0,
Date = date(2023, 9, 1).
author
- nicos angelopoulos
version
- 1:0 2023/9/1, gg_lollipop/2, gg_outputs/2
 c25(-Colours)
A pallete of 25 colours. Colours is a c/25 term. From: http://stackoverflow.com/questions/9563711/r-color-palettes-for-many-data-classes
?- c25( C25 ), colours_pie( C25 ).
 colours_pie(+Colours)
 colours_pie(+Colours, +Opts)
Use the Colours to draw a coloured pie chart via library(real). Colours can be a list or a flat compound, typically, c(c11,cl2,c3). This should be digestable by real as an input to a named argument. Requires library(real).

Opts

cex(Cex=1)
size of the labels
main()
main label
prefix(Pfx=number)
or false to exclude labels
radius(Radius=1)
radius for the pie
weights(Whg=1)
relative weights
labels(Lbs=Clrs)
labels for the portions
 ?- lib(b_real:c25/1).
 ?- c25(C25), colours_pie( C25 ).

 ?- lib(b_real:colour_cb/1).
 ?- colour_cb(Cb), colours_pie( Cb ).

 ?- lib(real).
 ?- <- library("RColorBrewer").
 ?- Set1 <- brewer.pal(9,"Set1"), colours_pie( Set1, main(main) ).

 ?- colfunc <- colorRampPalette(c("white", "blue")),Ten <- colfunc(10),colours_pie(Ten).

 ?- lib(r("colorspace")).
 ?- Pal <- diverge_hcl(7), colours_pie( Pal ).
 ?- Pal <- sequential_hcl(7), colours_pie( Pal ). % gives you shades of blue
 ?- R <- rainbow_hcl( 4 ), colours_pie( R ).
 ?- colours_pie( ["#008000","#CC0000"], weights(c(1,2)) ).
 ?- colours_pie( ["#008000","#CC0000"], [weights(c(1,2)),prefix(false)] ).
 ?- colours_pie( ["#008000","#CC0000"], [weights(c(1,1)),labels([a,b])] ).
author
- nicos angelopoulos
version
- 0.2 2014/6/9 added Opts, main/1
 colour_cb(-Cb)
Colour blind palette with grey. Cb is a c/8 term.
?- lib(b_real:colours_pie/1).
?- colour_cb(Cb), colours_pie(Cb).
Cb = c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7").
?- lib(real).
?- colour_cb(Cb), <- svg("clr_cb.svg"), colours_pie(Cb), r_devoff.

Produces file: clr_cb.svg

author
- nicos angelopoulos
version
- 0.1 2014/02/10
See also
- http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/
- http://jfly.iam.u-tokyo.ac.jp/color/
 colour_cbb(-Cbb)
Colour blind palette with black (instead of grey when compared to colour_cb/1). Cbb is a c/8 term.
?- lib(b_real:colours_pie/1).
?- colour_cbb(Cbb), colours_pie(Cbb).
Cbb = c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7").
author
- nicos angelopoulos
version
- 0.1 2014/02/10
See also
- colour_cb/1
 aheatmap(+Csv)
 aheatmap(+Csv, +Opts)
Generic aheatmap plots with aheatmap.2 from NMF. Csv can be a mtx/1 recognised matrix or an R variable.

Note that standard version of aheatmap() has a bug when breaks and colours are given: https://github.com/renozao/NMF/issues/12 2014/08/12

Opts

centre(Centre=false)
1.0 or 0.0, or non number for not centering
cellheight(32)
height of the cell
cellwidth(8)
width of the cell
rvar(hm_data)
the R variable on which to load the data (if Csv is not an R var itself
hp_token(pastel_ryg)
hmap([])
aheatmap() call additional parameters given in Name(Value) syntax
scale(true)
should we scale ?
rowV(RowV=true)
do not use a rows dendrogram
colV(ColV=true)
do not use a columns dendrogram

?- Mpg <- mtcars$mpg, aheatmap( Mpg, [rowV(false),colV(false)]  ).
?- Mpg <- mtcars$mpg, x <- [Mpg], colnames(x) <- rownames(mtcars),
   aheatmap( x, [cellheight=32,scale(false),rowV(false)]  ).

?-
   Mtc <- as.list(mtcars), memberchk(hp=HP,Mtc), memberchk(disp=Disp,Mtc),
   x <- [HP, Disp], rownames(x) <- c("horsepower","displacement"),
   colnames(x) <- rownames(mtcars),
   aheatmap(x).
author
- nicos angelopoulos
version
- 0.1 2014/5/18
 lm_plot(+Vect1, +Vect2, +Opts)
Create a scatter plot for vectors Vect1 and Vect2 (pl_vector/2) along with fitting a linear model.

Pval is the anova p.value of Clm2 ~ Clm1, Coef is the coefficient of the regression, R2 is R squared (coefficient of determination). See options.

Opts

Ropt = Rval
pairs are passed to the plot call (via r_call/2) (defaults for main, xlab, ylab and col provided)
coef(Coef)
return value of the coefficient
max_frac(MxFrac=4)
maximum number of fractional part
mtx(Mtx)
used if Vect1,2 are not lists, see pl_vector/2
outputs(Outs)
outputs to produce, see r_call/2
pval(Pval)
return value of the p value
r2(R2)
return value of the R2
stem(Stem)
stem for any output files (see r_call/2)
RcallOpts
options supported by r_call/2
?- mtx_data( mtcars, Mt ),
   lm_plot( mpg, disp, mtx(Mt) ).

?- assert( lm_mtx( [row(a,b,c),row(1,2,3),row(2,3,4),row(3,4,5)] ) ).
?- lm_mtx( Mtx ), lm_plot( a, b, C, R2, P, mtx(Mtx) ).
C = 1.0,
P = 0.0.

?- lm_mtx(Mtx),
   lm_plot(Mtx,a,b,C,R2,P,true,).

C = 1.0,
P = 0.0.

?- ls.
what.png
?- use_module( library(by_unix) ).
?- @ eog(what.png).
author
- nicos angelopoulos
version
- 0.1 2014/6/30
- 0.1 2014/1/23, mtx updates + R2
 gg_bar_plot(+Pairs, +Opts)
Plot a bar plot of population Pairs (Name-Populations).

Opts is a combination of options controlling the predicate as per normal Prolog convention, and term structures that translate to '+' ggplot2 terms.

Originally this only supported lists at the value part of Pairs. Now single values make the predicate plot a non-grouped barplot.

Opts

bar_draw_colour(Bdc=white)
draw colour for bars
debug(Dbg=false)
true turns debugging on
df_rvar(Df=gbp_df)
data frame R variable
df_rvar_rmv(DfR=true)
remove Df after call
extra_legend(XtLeg=false)
adds extra legend
extra_legend_position(Xn, Xx, Yn, Yx)
position for extra legend
fill_colours(Clrs=true)
false removes scale_fill_manual() term, true use def. colours, else give list of colours
flip(Flip=true)
flip coordinates
geom_bar(Gb=true)
false removes removes geom_bar() term
geom_bar_draw_colour(Gc=white)
colour for bar outlines
geom_bar_position(Gp=dodge)
or stack; positions bars
gg_terms([])
arbitrary terms to add to the plot
labels(X, Y, Main)
X='',Y='',Main='', if Flip==true then you should also swap X with Y here
legend_reverse(Lrv=true)
reverse legend order (Lrv=false when Flip=false)
legend_title(Lt)
legend title ([] for default ggplot2() one). only works for Clrs \== false
legend_labels(Ll=[])
labels for legend (only valid if colours are given)
keep_order(Kord=true)
display x axis according to given order (else, passes through sort)
output(Outp=false)
else send a R command such as pdf("myfile.pdf")
theme(PnlTheme=standard)
theme for the whole panel (graph)- see gg_theme/2
?- Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, true ).

?- FClrs = ["gold1", "#E31A1C", "blue1"],
BoldTitle = theme(plot.title(element_text(face(+"bold")))),
Pairs = [a-[1,2,3],b-[2,4,6]],
gg_bar_plot( Pairs, [debug(true), geom_bar_draw_colour(black), labels(x,y,main), fill_colours(FClrs), gg_terms(BoldTitle),legend_title(leeg)] ).

?-  FClrs = ["gold1", "#E31A1C","blue1","darkolivegreen"], Pairs = [a-1,b-2,c-3,d-4],
gg_bar_plot( Pairs, [flip(false),geom_bar(empty),fill_colours(FClrs)] ).

?- Pairs = [a-1,b-2,c-3,d-4],
   gg_bar_plot( Pairs, [flip(false),geom_bar(empty),fill_colours(true),df_rvar_rmv(false)] ).
author
- nicos angelopoulos
version
- 0.1 2014/10/21
- 0.2 2016/01/23
- 0.3 2016/08/31, added singleton groups as normal barplots
- 0.4 2022/02/16, re-introduced dependency to ggpubr
 gg_lollipop(+Data, +Opts)
Plot a lollipop chart for the Data using ggplot2.

Currently the expected format for Data is Label-Value KVPairs.

Opts

clr_point(CPoint=blue)
colour for the head/point of the lollipops
clr_stem(CStem=skyblue)
colour for the stem/stick of the lollipops
debug(Dbg=false)
informational, progress messages
df_rvar(Df=glp_df)
data frame R variable
flip(Flip=false)
whether to flip x- and y-axes. When not, the predicate also turns the x-tick labels 270 degrees. Use strict for not flipping and not turning, or a number (such as 90) for the actual degrees.
g_terms(Gterms=[])
if given, terms in this list are appended (+) to the ggplot() call
gplot_rvar(Gp=glp_gp)
ggplot R variable
labels(X=Category,Y=Quantity,M=)
labels to use on x- and y-axes and main title
order(Ord=true)
false displays according to ggplot2 reverse reverses it true keeps the order of labels in Data as is. size order in descending size of the numerical values in Data, and size_reverse orders elements in ascending size of the numerical values in Data
theme(Theme=lolli)
use some basic theme adjustments making the plots a bit starker (see gg_theme/2). The default changes to lolli if flip(true) is given in Opts.
rvar_rmv(RvRmv=true)
remove Df and Gp after call
stem(Stem=gg_lollipop)
stem for any output files (overrides def of gg_outputs/2)

Examples

?- gg_lollipop([a-2,b-5,c-1,d-3], true).

Options are passed to gg_outputs/2.

?- gg_lollipop([a-2,b-5,c-1,d-3], outputs(svg)).

Produces file: gg_lollipop.svg

Change basic parameters of the plot

?- gg_lollipop([a-2,b-5,c-1,d-3], [clr_point("red"),clr_stem("green"),theme(false)] ).
?- gg_lollipop( [a-2,b-5,c-1,d-3], theme(blank) ).

Change the labels

?- gg_lollipop([a-2,b-5,c-1,d-3], labels(doom,gloom,all_around)).

Inject arbitrary ggplot2 terms

?- gg_lollipop( [a-2,b-5,c-1,d-3], theme(false) ).
?- gg_lollipop( [a-2,b-5,c-1,d-3], [theme(false),g_terms(theme_light())] ).

Adjust angle of tick labels, when not flipping

?- gg_lollipop([a-2,b-5,c-1,d-3], flip(45) ).

"ordering" can be counter-intuitive. By Order=true we mean keep the order as in the given list. By default, ggplot2 presents the categorical variable in lexicographical order.

?- gg_lollipop([b-5,a-2,d-3,c-1], true ).
?- gg_lollipop([b-5,a-2,d-3,c-1], order(false) ).
?- gg_lollipop([b-5,a-2,d-3,c-1], order(reverse) ).

The plot can also be ordered by the numerical values in the Data.

?- gg_lollipop([a-2,b-5,c-1,d-3], order(size) ).
?- gg_lollipop([a-2,b-5,c-1,d-3], order(size_reverse) ).

Can keep the R variables around for later use with

?- gg_lollipop([a-2,b-5,c-1,d-3], rvar_rmv(false)).
?- lib(real).
?- <- ls().

?- <- print(ls()).
[1] "glp_df" "glp_gp"
true.

?- DF <- glp_df.
DF = [x=[1, 2, 3, 4], y=[2, 5, 1, 3]].

?- <- summary(glp_gp).
data: x, y [4x2]
mapping:  x = ~x, y = ~y
...
author
- nicos angelopoulos
version
- 0.1 2023/08/30
See also
- gg_outputs/2
- gg_theme/2
 gg_outputs(+Ggplot, +Opts)
Display a Ggplot to a variety of formats via ggsave().

Ggplot should be an R variable holding a ggplot2 plot. In addition to be called directly by users, this predicate is also used in a number of Prolog library predicates that interface to ggplot2 plots.

Opts

debug(Dbg=false)
informational, progress messages
dir(Dir=(.))
output directory
outputs(Outs=x11)
atoms which are taken to be either x11 (screen output), or filename extensions that dictate type of output. Terms can be given with functor as described in last sentence, and can include = pairs which are passed to the ggsave() goal
plot_height(Height=7)
height for plots
plot_width(Width=7)
width for plots
stem(Stem=gg_output)
file name stem for any output to files

Examples

?- lib(real), lib(r(ggplot2)).
?- ddf <- 'data.frame'(x='LETTERS'[1:26], y=abs(rnorm(26)) ).
?- ggp <-  ggplot(ddf, aes(x=x, y=y)) + geom_segment(aes(x=x,xend=x,y=0,yend=y)) + geom_point(size=4,alpha=0.6).
?- gg_outputs(ggp,[]).
?- lib(real), lib(r(ggplot2)).
?- ddf <- 'data.frame'(x='LETTERS'[1:26], y=abs(rnorm(26)) ).
?- ggp <-  ggplot(ddf, aes(x=x, y=y)) + geom_segment(aes(x=x,xend=x,y=0,yend=y)) + geom_point(size=4,alpha=0.6).
?- gg_outputs( ggp, outputs(png(file="abc.svg")) ).

Produces file: abc.svg

Width, Height and Stem can be overridden by = options within Outs. In the example below, the pdf gets Width 8, from the plot_width parameter, while the png gets Width 9.

?- lib(real), lib(r(ggplot2)).
?- ddf <- 'data.frame'(x='LETTERS'[1:26], y=abs(rnorm(26)) ).
?- ggp <-  ggplot(ddf, aes(x=x, y=y)) + geom_segment(aes(x=x,xend=x,y=0,yend=y)) + geom_point(size=4,alpha=0.6).
?- gg_outputs( ggp, [plot_width(8),outputs([pdf,png(file="abc.png",width=9)]),debug(true)] ).
% Sending to Real: ggsave(plot=ggp,width=8,height=7,file= +gg_output.pdf)
% Sending to Real: ggsave(plot=ggp,height=7,file=abc.png,width=9)
author
- nicos angelopoulos
version
- 0.1 2023/08/31
 gg_theme(+Panel, -GgTerms)
Expand a theme panel token to its corresponding ggplot terms.

Panel

axes
as blank theme, but with black axes lines
blank
produces a panel with white background, no grid lines and no axes lines
false
no theme, (empty list of terms)
lolli
theme suitable for lollipop plots (gg_lollipop/2) in normal orientation, light theme + no borders, no major and ticks on y
lolly
theme suitable for lollipop plots (gg_lollipop/2) in flipped orientation, light theme + no borders, no major and ticks on y
standard
standard panel decorations, i.e. no theme (empty list)
?- Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, true ).
    % shows a plot with grid lines and axis lines
?- Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, theme(blank) ).
    % shows a plot with neither grid lines and axis lines
?- Pairs = [a-[1,2,3],b-[2,4,6]], gg_bar_plot( Pairs, theme(axes) ).
    % shows a plot with no-background colour and no grid lines, but with lines on both axes
author
- nicos angelopoulos
version
- 0:2 2020/7/27, renamed and added to b_real (from hrmn), new theme
- 0:3 2023/8/31, added lolli,lolly themes (and aliased false to [])
 vectors_subed_gg_bar_plot(+Vectors, +Val, +Vect2, +Opts)
Plot frequencies of a value in Vectors with subdivisions for each of the values apppearing in Vect2. All vectors are defined relative to Mtx.

Opts

colours(Clrs=Clrs)
list of fill colours (default palettes has 4 colours: dark red (Alizarin), amber, baby blue and air force blue )
mtx(Mtx)
matrix to extract the vectors from
x_axis_colour(XaClrB=false)
whether to colour x-axis tick labels
sort_x(SortX=false)
else true (lexicographical), or totals for total counts
Mtx = [  row(v,x,y,z),
         row(1,0,0,a),
         row(0,0,1,a),
            row(0,1,1,b),
            row(1,1,1,b),
            row(0,1,0,c)
      ],
assert( ex1(Mtx) ).

?- ex1(Mtx), vectors_subed_gg_bar_plot( [v,x,y], 1, z, mtx(Mtx) ).
[[1,1,0],[0,2,1],[1,2,0]]

Opts are passed to

author
- nicos angelopoulos
version
- 0.1 2015/11/25
See also
- pl_vector/3
- vector_factor_indices/3
- gg_bar_plot/2
 mtx_pheatmap(+Mtx, +OptS)
Plots the data in Mtx using pheatmap() from omonymous package. See mtx_column_pheatmap/3. Here we print vertically though.

Opts

Ropt = Rarg
pass Ropt=Rarg to the pheatmap() R call
rvar(Rvar=mtx_heatmap)
R variable to use
names(Names=[])
list of names or column id

Predicate uses r_call/2 which takes its own options.

Dependencies

?- pack_install( real ).
?- use_module( library(real) ).
?- install.packages( "pheatmap" ).

Examples

?- use_module( library(real) ).
?- <- write.csv( mtcars, "mtcars.csv" ).  % mtcars is an example dataset in R
?- <- csv_read_file( Mt, 'mtcars.csv' ), assert( mt(Mt) ).
?- mt(Mt), mtx_pheatmap( Mt, [names(1),scale="column"] ).
?- mt(Mt), mtx_pheatmap( Mt, [names(1),scale="column",debug(true)] ).
author
- nicos angelopoulos
version
- 0.1 2015/1/13
See also
- http://cran.r-project.org/web/packages/pheatmap/pheatmap.pdf
To be done
- centre around value (balanced and unbalanced // interval)
 mtx_column_pheatmap(+Mtx, +Cid, +Opts)
Plot pheatmap of column Cid (mtx_column/4) of matrix Mtx (mtx/2).

This is mainly constructed as a demonstration of using R's pheatmap function from the omonymous library (http://cran.r-project.org/web/packages/pheatmap/pheatmap.pdf).

Opts

Ropt = Rarg
pass Ropt=Rarg to the underlying (see below for defaults)
breakpoints(Bkps=51)
number of breakpoints if Centre is a number (should be colours + 1)
centre(Centre=false)
when number is given, center breakpoints around this value
cname(Cnm)
returns the column name of Cid
column(Clm)
returns the values of Cid in Mtx (does not include Cnm)
cposition(Cps)
returns the column position of Cid
mtx(Mtx)
returns the cannonical representation of Mtx
names(Names=[])
defines heatmap's columns names, either a list or a column identifier in Mtx
outputs(Outs=x11)
a single or list of [x11,pdf], see r_call/2
privates(Prv=false)
if true, include private Ropts
rvar(Rvar=mcp)
R variable to use for the data
rvar_rmv(Rdel=true)
delete the Rvariable at end of call fixme: not implemented yet...
stem(Stem)
stem for output files. Default: atomic_list_concat([Cnm,phmap],'_',Stem).

Ropt

?- use_module( library(real) ).
?- <- write.csv( mtcars, "mtcars.csv" ).  % mtcars is an example dataset in R
?- csv_read_file( 'mtcars.csv', Mt ), mtx_column_pheatmap( Mt, 2, [] ).
?- csv_read_file( 'mtcars.csv', Mt ), mtx_column_pheatmap( Mt, 2, [names(1)] ).
?- csv_read_file( 'mtcars.csv', Mt ), mtx_column_pheatmap( Mt, 2, [names(1),legend='TRUE'] ).
?- csv_read_file( 'mtcars.csv', Mt ), mtx_column_pheatmap( Mt, 2, [names(1),outputs(pdf),stem(mtcars_c2)] ).
?- shell( 'ls -l mtcars_c2.pdf' ).
-rw------- 1 user user 4212 Jan 13 13:56 mtcars_c2.pdf
author
- nicos angelopoulos
version
- 0.1 2014/1/7
See also
- http://cran.r-project.org/web/packages/pheatmap/pheatmap.pdf
- mtx_column/3
- mtx/2
To be done
- remove NAs, with reprecations to names()
 options_rvar_rmv(+RvarS, +Opts)
r_remove/1 R variable Rvar, or list of RvarS, iff rvar_rmv(true) is in Opts.

The rationale is to make rvar_rmv(_) a commonly occured option in b_real predicates.

?- x <- 1.
?- <- x.
[1] 1
true.
?- options_rvar_rmv( x, [rvar_rmv(true)] ).
?- <- x.
% Error in print(x) : object 'x' not found
% ERROR: R was unable to digest your statement, either syntax or existance error
?- x <- 1.
?- options_rvar_rmv( x, [rvar_rmv(false),debug(true)] ).
% Keeping R variable: x
true.

?- y <- 2.
?- options_rvar_rmv( [x,y], [rvar_rmv(true)] ).
true.

?- <- x.
ERROR: R was unable to digest your statement, either syntax or existance error.
author
- nicos angelopoulos
version
- 0.1 2015/1/15
- 0.2 2022/12/16, can operate on lists of Rvars now
To be done
- debug messages via options_debug/3 library(options)
 pl_vector(+VectSpec, -Vect, +Opts)
True iff VectSpec is a regognisable representation of a vector whose canonical representation (a list) is Vect. Through Options you can also control max and min values.

Recognisable represenation are:

list
which is also the canonical representation
Cid
when memberchk(mtx(MTx),Opts) and mtx_column( Mtx, Cid, Vect )

Opts

Currently, k() and v() are inompatible to max() and min().

?- pl_vector( [1,2,3], V, true ).
V = [1, 2, 3].

?- mtx_data( mtcars, Mc ),
   pl_vector( 1, Vect, [mtx(Mc),cnm(Cnm)] ),
   max_list( Vect, Max ).

Mc = [row(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb), row(21.0, 6.0|...],
Vect = [21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8|...],
Cnm = mpg,
Max = 33.9.

?- mtx_data( mtcars, Mc ),
   pl_vector( 1, Vect, [mtx(Mc),cnm(Cnm),max(30)] ),
   max_list( Vect, Max ).

Mc = [row(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb), row(21.0, 6.0|...],
Vect = [21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8|...],
Cnm = mpg,
Max = 30.
author
- nicos angelopoulos
version
- 0.2 2016/6/7, added where() and k(),v() pairs
- 0.3 2020/7/27, changed order of clauses (mtx with complex column name was matching as R variable)
- 0.4 2022/2/16, "complex" still tripped this, changed the order that if mtx/1 is given is tried first.
See also
- pl_vector_curtail/3,4
To be done
- add to real ? but it needs mtx ...
 vector_scale(+VectSpec, -Scaled, +Opts)
This is a funnel predicate that can be used to scale a vector (VectSpec) through options. Scaled should be something Real (pack(real)) understands as a LHS to <- assignment. That is, if it is a Prolog variable, it will be instantiated to the vector as a list, or a ground term, will translate that to an R expression (typically an R variable) to which the scaled vector is assigned to.

For details on VectSpec, see pl_vector/3 and options affecting this.

Opts

scale(Scale=false)
or true (full scale, subtract mean and divide by stadard deviation) or centre (subtract mean)

See R's scale().

?- Mtc <- as.vector( mtcars[*,3] ), Mean <- mean(Mtc), vector_scale( Mtc, Sca, [scale(true)] ),
   ScaMean <- mean(Sca), ScaVrc <- var(Sca).

Mtc = ...
Mean = 230.721875,
Sca = ...
ScaMean = -9.084936579070724e-17,
ScaVrc = [[1.0]].

?- Mtc <- as.vector( mtcars[*,3] ), Mean <- mean(Mtc), vector_scale( Mtc, Sca, [scale(centre)] ),
   ScaMean <- mean(Sca), ScaVrc <- var(Sca).

Mtc = ...
Mean = 230.721875,
Sca = ...
ScaMean = -1.199040866595169e-14,
ScaVrc = [[15360.799828629033]].
author
- nicos angelopoulos
version
- 0.1 2015/7/17
 pl_plot_on(+Goal, +On)
Call Goal while recording (R) output to On. On should be wrappend in plot_on/1, else it is ignored silently. As per order, this execution allows maplisting options through this predicate. Goal is called once for plot_on, in that scenario. If On's term/atom name is x11, or its == open, then the device is left open otherwise r_devoff/0 is called.
 ?- pl_plot_on( <- plot(c(1,2,3)), plot_on(x11()) ).
 ?- pl_plot_on( <- plot(c(1,2,3)), plot_on(pdf(+ex.pdf)) ).
 ?- shell( 'evince ex.pdf' ).
 ?- pl_plot_on( <- plot(c(1,2,3)), plot_on(x11(width=14)) ).
author
- nicos angelopoulos
version
- 0.1 2015/12/16
To be done
- move this to Real ?
 r_mtx(+Rvar, ?Mtx)
 r_mtx(+Rvar, ?Mtx, +Opts)
Load Mtx onto Rvar and vice versa. Mtx is passed through mtx/2. It can also be an Real matrix, ie. a list of lists.
Rows in mtx/2 are of the form of a list of n-ary compounds (as in csv/3).

Opts

rownames(Rnames=integer)
number indicates column position, list for given names and integer for auto naming 1...n .
header(Hdr=true)
whether the file incorporates a header
colnames(Cnames=header)
or list for given names or integer for 1...n. If Cnames is header and Hdr is false, integer column names are used.
debug(Dbg=false)
or true for self debugging call. Note that false turns off, not just ignore and that in both cases original debugging status is re-instated at end of call.
?- mtx_data( mtcars, Mt ), r_mtx( rv, Mt, [debug(true),rownames(1)] ).
author
- nicos angelopoulos
version
- 0.1 2014/8/20
To be done
- add generic selection predicate(s) rows + columns with/out renames for columns and rows
- add reading through R's read.csv()/read.table()
- the Rvar -> Mtx mode !
 r_data_frame(+DfIn, +Df)
Convert DfIn to an R data frame.

If DfIn, is an R variable, and Df is atomic it is assumed to be a data frame and Df <- DfIn, is called (this can be elaborated upon later), whereas if Df is variable then Df = DfIn is called.

If DfIn is not an R variable, it is passed through mtx/2 and the result is passed through mtx_df/2. When Df is a variable in this context, a unique variable is generated that looks like tmp.df.N where N is an integer.

See also
- mtx/2
- mtx_df/2
- r_mtx/3
 r_unique(+Stem, -Unique)
Create a Unique R variable of the form Stem.N where N is an integer. The value 'NA' is passed to the variable.
?- r_unique( df.data, Uniq ).
Uniq = df.data.1.

?- r_unique( df.data, Uniq ).
Uniq = df.data.2.
 r_hist(+VectSpec, +Opts)
Plots a histogram or density of an arithmetic vector.

VectSpec should be as that recognised by the 1st argument of pl_vector/3. A list is the simplest representation of a vector.

Displaying is via r_call/2, so Opts can influence that call.

Opts

as_density(AsDense=false)
when true it plots the density instead of the histogram
name(Name)
set Name (string or +atom) default value for x-axis and main labels
transparency_colour(TransClr=lightblue)
colour for the density, transparency colour
transparent(Trans=true)
use transparent colour to highlight density (currently only with AsDens=true)
?- lib(real).
?- Mtc <- as.matrix(mtcars), r_hist( 2, mtx(Mtc) ).
?- rns <- rnorm(1000).
?- Rns <- rns, r_hist( Rns, name("rnorm") ).
?- Rns <- rns, r_hist( Rns, [name("rnorm"),main="Main Title",outputs(svg),stem(rh1)] ).

Produces file: rh1.svg

?- lib(real).
?- rnsm <- rnorm(10000), r_hist( rnsm, true ).
?- r_hist( rnsm, as_density(true) ).
?- pl_vector( rnsm, Rnsm, if_rvar(prolog) ), r_hist( Rnsm, name(+true) ).
?- r_hist( rnsm, [as_density(true),transparent(false)] ).
?- r_hist( rnsm, [as_density(true),transparency_colour("lightgreen"),outputs(svg),stem(rh2)] ).

Produces file: rh2.svg

author
- nicos angelopoulos
version
- 0.1 2017/4/25
 columns_fisher_test(+Data, +Inters, +Odds, -Lods, +Opts)
Perform pairwise Fisher tests on the columns of Data (mtx/1).

Opts

plot(Plot=false)
whether to plot the test results
plot_on(PlotOns)
see pl_plot_on/2, multiple allowed- but Plot needs to be true for them to fire
?- cd( '/usr/local/users/nicos/work/2015/15.11.04-hmrn/Exploristics/data' ).
?- columns_fisher_test( hmrn_muts_t10.csv, inters, odds, Lods, [plot(true),plot_on(x11()),plot_on(pdf())] ).
author
- nicos angelopoulos
version
- 0.1 2015/12/16
 dot(+StemS, +ExtS, -Dotted)
Create a dotted amalgamation (atom_concat-enation) of StemS and ExtS. Both can be atomic or lists.

Note that at_con/3 is used so '' is handled differently to atomic_list_concat/3.

?- dot( tmp, rv, Dot ).
Dot = tmp.rv.

?- dot( [tmp,rv], phase1, Dot ).
Dot = tmp.rv.phase1.

?- dot( '', x, Dot ).
Dot = x.

?- dot( x, ['',x], Dot ).
Dot = x.x.
author
- nicos angelopoulos
version
- 0.1 2015/12/16
 mtx_df(-Mtx, +Df, +Opts)
 mtx_df(+Mtx, +Df)
Mtx is the csv representation of R data frame Df.

Opts

check_names(ChNames=true)
in R the default is also true
?- mtx_df( [row(a,b,c),row(1,2,3),row(4,5,6)], df1 ),
   <- df1,
   csv_df( Csv1, df1 ).
 
$a
[1] 1 4
 
$b
[1] 2 5
  
$c
[1] 3 6

Csv1 = [row(a, b, c), row(1, 2, 3), row(4, 5, 6)].
  

This seems to get stuck for very large matrices, (>130,000). See implementation in r_sqlite_load.pl .

author
- nicos angelopoulos
version
- 0.1 2014/6/19
- 0.2 2015/12/14, added options (check_names/1)
 quantiles(List, Q, Qist)
Replace each element in List by an element in Q producing Qist, with the ith element in Q replacing for the elemnts in List on the ith quantile. Qist contains overall length(Q) distinct elements spliting the list ot Length(Q)-1 quantiles. The predicate would always replace the same value in List to the same Q element. Thus the quantiles might be imbalanced in lengths.

Strictly speaking quantiles are the cut points but this predicate produces the rank of the elements. The purpose is to discretise vector of continuous values or to crate a vector of fewer factor values for a discrete vector.

?- quantiles( [6,5,4,3,2,1], [0,1], BinV ).
BinV = [1, 1, 1, 0, 0, 0].

?- quantiles( [6,6,6,2,2,2], [0,1], BinV ).
BinV = [1, 1, 1, 0, 0, 0].

?- quantiles( [6,6,6,6,6,2], [0,1], BinV ).
BinV = [1, 1, 1, 1, 1, 0].

?- quantiles( [6,6,6,6,6,6], [0,1], BinV ).
BinV = [0, 0, 0, 0, 0, 0].
 symboled_graph_display(+SymbG, +UpAtms, +DwAtms, +Opts)
This is the graphviz version, SymbG is expected to be a ugraph and information about edges should be already in Opts.

We need to modernise this and add it is an alternative to wgraph_plot. Modernise by dealing with widths within the predicate and letting SymbG be a wgraph.

See string_symboled_display/4.

Used to have 2 more args, Stem & Title; 4+5, which are now in Opts.

Opts

output(Dev)
output device |
symb_colours([])
GeneSymb-ColourString pairs
focus_k(Fk=2)
the length of the focus chain
focus_genes(Fgs=[])
genes to focus graph on. no focus when []
edge_width(true)
whether to use edge width
edge_width_max(EWmax=2)
maximum edge width
edge_width_min(EWmin=1)
minimum edge width
range_max(RGmax=1000)
maximum for range of weights
range_min(RGmin=500)
minimum for range of weights
stem(Stem=symb_graph)
stem for filename
show_orphans(SwOrph=true)
else false
title(Title=)
title for the graph
?- assert( g([a-b,b-c,c-a]) ), assert ns([a,b,c]).
?- g(G), ns(Ns), symboled_graph_display( G, Ns, [], Ns, [] ).
?- g(G), ns(Ns), symboled_graph_display( G, Ns, [], Ns, [output(svg)] ).
?- @ eog( abc.svg ).
author
- nicos angelopoulos
version
- 0.1 2015
To be done
- allow flag that tells us the Graph is not a de_regulation graph (as to use a different colour for instance).
 string_symbols_graph(+Symbols, -Graph, +Opts)
Collect weight bearing edges among all Symbols as per string database. string_graph/3 will become a generalisation of this This version uses, bio_db for the string data.

Opts

catenator(Cat=(;))
atom to use as catenator in multi-symbol splits
min_w(MinW=500)
threshold below or at which edges are ignored (0 =< MinW =< 1000).
See also
- string_graph/3.
To be done
- cite string database location, and in the intereactions file we need meta-info