Skip to content

Commit 0254f55

Browse files
authored
Merge pull request #19 from dd-harp/dev
minor bug fixes, mostly minor changes
2 parents 6af9a3c + 89c3171 commit 0254f55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+332
-566
lines changed

NAMESPACE

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ export(edgeSubset)
7676
export(edgeSubset_fracMass)
7777
export(frame_bq)
7878
export(frame_bqs)
79+
export(get_graph)
7980
export(get_matrix)
80-
export(get_net)
8181
export(init_adult_model)
8282
export(init_adult_model_BQ)
8383
export(init_adult_model_BQS)
@@ -105,8 +105,6 @@ export(make_model_squareLattice)
105105
export(make_model_unif)
106106
export(make_movie)
107107
export(make_tiles)
108-
export(mod_convex_hulls)
109-
export(mod_convex_hulls.i)
110108
export(net2patches)
111109
export(outline_points_b)
112110
export(outline_points_q)
@@ -137,6 +135,7 @@ export(plot_dispersal_VV)
137135
export(plot_graph)
138136
export(plot_matrix_xx)
139137
export(plot_matrix_xy)
138+
export(plot_meta)
140139
export(plot_patches)
141140
export(plot_points)
142141
export(plot_points_bq)

R/compute_EGG.R renamed to R/compute_G.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

2-
#' Compute the dispersion of eggs after one feeding cycle
2+
#' Compute egg dispersion
3+
#' @description
4+
#' Compute the dispersion of eggs away from natal habitats.
5+
#'
36
#'
47
#' @param model a compound [list] defining a model
58
#' @param Tmax the last time step
@@ -10,7 +13,7 @@ compute_G = function(model, Tmax){
1013
UseMethod("compute_G", model$Mpar)
1114
}
1215

13-
#' Compute the dispersion of eggs after one feeding cycle for the BQ model
16+
#' Compute egg dispersion, BQ model
1417
#'
1518
#' @param model a compound [list] defining a model
1619
#' @param Tmax the last time step
@@ -32,7 +35,7 @@ compute_G.BQ = function(model, Tmax=50){with(model, with(Mpar,{
3235
return(model)
3336
}))}
3437

35-
#' Compute the dispersion of eggs after one feeding cycle for the BQ model
38+
#' Compute egg dispersion, BQS model
3639
#'
3740
#' @param model a compound [list] defining a model
3841
#' @param Tmax the last time step
File renamed without changes.
File renamed without changes.
Lines changed: 42 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
11
# Visualize communities as convex hulls
22

3+
#' Stretch (or shrink) the convex hull for plotting
4+
#'
5+
#' @param xy a convex hull
6+
#' @param fac a factor to stretch (>1) or shrink (<1) the hull for plotting
7+
#'
8+
#' @return a new point set
9+
#' @export
10+
stretch_convex_hull = function(xy, fac){
11+
cx = mean(xy[,1])
12+
cy = mean(xy[,2])
13+
xn = xy[,1] - cx
14+
yn = xy[,2] - cy
15+
16+
r = sqrt(xn^2 + yn^2)
17+
theta = atan2(yn,xn)
18+
sxy = cbind(
19+
r*fac*cos(theta) + cx,
20+
r*fac*sin(theta) + cy
21+
)
22+
return(sxy)
23+
}
24+
25+
# Visualize communities as convex hulls
26+
327
#' Make a set of convex hulls to visualize the community
428
#'
529
#' @param model a model defined as a compound [list]
6-
#' @param net a network object
30+
#' @param graph a graphwork object
731
#' @param cut if !null, the number of communities for igraph::cut_at
832
#' @param clrs a set of colors
933
#' @param f_color a function that returns a list of colors (e.g. viridis::turbo)
1034
#'
11-
#' @return the network object with convex hulls attached
35+
#' @return the graphwork object with convex hulls attached
1236
#' @export
13-
make_convex_hulls = function(model, net, cut=NULL, clrs=NULL, f_color = viridis::turbo){
14-
clusters = net$walktrap_clusters
37+
make_convex_hulls = function(model, graph, cut=NULL, clrs=NULL, f_color = viridis::turbo){
38+
clusters = graph$walktrap_clusters
1539
memix = if(is.null(cut)){
1640
igraph::membership(clusters)
1741
} else {
1842
igraph::cut_at(clusters, cut)
1943
}
20-
if(net$type == "b"){
44+
if(graph$type == "b"){
2145
xy = model$b
2246
}
23-
if(net$type == "q"){
47+
if(graph$type == "q"){
2448
xy = model$q
2549
}
26-
if(net$type == "bq"){
50+
if(graph$type == "bq"){
2751
xy = with(model,rbind(b, q))
2852
}
29-
net$convex_hulls = list()
53+
graph$convex_hulls = list()
3054
if(is.null(clrs)) clrs = f_color(max(memix))
3155
for(i in 1:max(memix)){
32-
net$convex_hulls[[i]] = list()
33-
net$convex_hulls[[i]]$xy = make_convex_hull_i(i, memix, xy)
34-
net$convex_hulls[[i]]$clr = clrs[i]
56+
graph$convex_hulls[[i]] = list()
57+
graph$convex_hulls[[i]]$xy = make_convex_hull_i(i, memix, xy)
58+
graph$convex_hulls[[i]]$clr = clrs[i]
3559
}
36-
return(net)
60+
return(graph)
3761
}
3862

3963
#' Make the convex hull for the i^th community
@@ -47,17 +71,12 @@ make_convex_hulls = function(model, net, cut=NULL, clrs=NULL, f_color = viridis:
4771
#' @return a [list] with a hull
4872
#' @export
4973
make_convex_hull_i = function(i, memix, xy){
50-
ixj = which(memix == i)
51-
hpts <- grDevices::chull(xy[ixj,])
52-
ixk = c(hpts, hpts[1])
53-
hxy = xy[ixj[ixk],]
54-
return(hxy)
74+
ix = which(memix == i)
75+
return(make_convex_hull(xy[ix,]))
5576
}
5677

5778
#' Make the convex hull for the i^th community
5879
#'
59-
#' @param i the i^th community
60-
#' @param memix the community membership indices
6180
#' @param xy the point set
6281
#'
6382
#' @return the xy points that define a convex hull
@@ -74,16 +93,16 @@ make_convex_hull_xy = function(xy){
7493

7594
#' Add the convex hulls to a framed plot
7695
#'
77-
#' @param net a network object
96+
#' @param graph a graphwork object
7897
#' @param stretch make the hull slightly larger or slightly smaller
7998
#' @param lwd hull line width
8099
#'
81100
#' @return invisible(NULL)
82101
#' @export
83-
plot_convex_hulls = function(net, stretch=1.1, lwd=2){
84-
n = length(net$convex_hulls)
102+
plot_convex_hulls = function(graph, stretch=1.1, lwd=2){
103+
n = length(graph$convex_hulls)
85104
for(i in 1:n){
86-
with(net$convex_hulls[[i]],{
105+
with(graph$convex_hulls[[i]],{
87106
sxy = stretch_convex_hull(xy, stretch)
88107
graphics::polygon(sxy[,1], sxy[,2], border=clr, lwd=lwd)
89108
})
@@ -111,72 +130,3 @@ add_convex_hulls = function(memix, xy, clrs, stretch=0.1, lwd=2, llty=1){
111130
return(invisible())
112131
}
113132

114-
115-
#' Stretch (or shrink) the convex hull for plotting
116-
#'
117-
#' @param xy a convex hull
118-
#' @param fac a factor to stretch (>1) or shrink (<1) the hull for plotting
119-
#'
120-
#' @return a new point set
121-
#' @export
122-
stretch_convex_hull = function(xy, fac){
123-
cx = mean(xy[,1])
124-
cy = mean(xy[,2])
125-
xn = xy[,1] - cx
126-
yn = xy[,2] - cy
127-
128-
r = sqrt(xn^2 + yn^2)
129-
theta = atan2(yn,xn)
130-
sxy = cbind(
131-
r*fac*cos(theta) + cx,
132-
r*fac*sin(theta) + cy
133-
)
134-
return(sxy)
135-
}
136-
137-
138-
#' Make a set of convex hulls to visualize the community
139-
#'
140-
#' @param model a model defined as a compound [list]
141-
#' @param net a network object
142-
#' @param cut if !null, the number of communities for igraph::cut_at
143-
#' @param f_color a function that returns a list of colors (e.g. viridis::turbo)
144-
#'
145-
#' @return the network object with convex hulls attached
146-
#' @export
147-
mod_convex_hulls.i = function(model, i, cut=NULL, f_color=turbo, stretch=0.1, lwd=2){
148-
net = getNet.i(model, i)
149-
mod_convex_hulls.i(model, net, cut, clr_scheme, stretch, lwd)
150-
}
151-
152-
#' Make a set of convex hulls to visualize the community
153-
#'
154-
#' @param model a model defined as a compound [list]
155-
#' @param net a network object
156-
#' @param cut if !null, the number of communities for igraph::cut_at
157-
#' @param clrs a list of colors
158-
#' @param f_color a function that returns a list of colors (e.g. viridis::turbo)
159-
#' @param stretch a stretch factor for the hulls
160-
#' @param lwd the line width for plotting
161-
#'
162-
#' @return the network object with convex hulls attached
163-
#' @export
164-
mod_convex_hulls = function(model, net, cut=NULL, clrs=NULL, f_color=viridis::turbo, stretch=0.1, lwd=2){
165-
clusters = net$walktrap_clusters
166-
memix = if(is.null(cut)){
167-
membership(clusters)
168-
} else {
169-
cut_at(clusters, cut)
170-
}
171-
if(net$type == "b"){
172-
xy = model$b
173-
}
174-
if(net$type == "q"){
175-
xy = model$q
176-
}
177-
if(net$type == "bq"){
178-
xy = with(model,rbind(b, q))
179-
}
180-
if(is.null(clrs)) clrs = f_color(max(memix))
181-
add_convex_hulls(memix, xy, clrs, stretch, lwd)
182-
}

R/network_graphs.R renamed to R/graphs.R

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
# temporarily moved to RAMP-modelel-Library
22

3+
#' Choose one of the networks describing dispersion
4+
#' @description
5+
#' 1 -Kbb; 2 - Kqq;
6+
#' 3 - G; 4 - GG;
7+
#' 5 - V; 6 - VV;
8+
#' 7 - M; 8 - MM
9+
#'
10+
#'
11+
#' @param i an index for the type of model
12+
#' @param model a model defined as a compound [list]
13+
#'
14+
#' @return a net object
15+
#' @export
16+
get_graph = function(model, i){
17+
if(i==1) graph = model$graphs$Kbb_graph
18+
if(i==2) graph = model$graphs$Kqq_graph
19+
if(i==3) graph = model$graphs$G_graph
20+
if(i==4) graph = model$graphs$GG_graph
21+
if(i==5) graph = model$graphs$V_graph
22+
if(i==6) graph = model$graphs$VC_graph
23+
if(i==7) graph = model$graphs$M_graph
24+
if(i==8) graph = model$graphs$MM_graph
25+
return(graph)
26+
}
27+
28+
329
#' Make graph object
430
#'
531
#' @description
@@ -102,7 +128,7 @@ make_all_graphs.BQS = function(model){
102128
#' Plot the ouptuts of a graph using
103129
#'
104130
#' @param model a `ramp.micro` model object
105-
#' @param graphs a graphswork object
131+
#' @param graph a graph object
106132
#' @param cut optional arguent for cut_at
107133
#' @param alg walktrap = "wt" or greedy = "gr"
108134
#' @param f_color a function that returns a list of colors (e.g. viridis::turbo)
@@ -214,7 +240,7 @@ plot_subgraph = function(model, graph, cut=NULL, alg="wt",
214240
#' Plot the ouptuts of a graph using
215241
#'
216242
#' @param model a `ramp.micro` model object
217-
#' @param graphs a graphswork object
243+
#' @param graph a graph object
218244
#' @param cut optional arguent for cut_at
219245
#' @param alg walktrap = "wt" or greedy = "gr"
220246
#' @param f_color a function that returns a list of colors (e.g. viridis::turbo)
File renamed without changes.
File renamed without changes.

R/network_patches.R renamed to R/patches.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#' Use communities to define patches
33
#'
44
#' @param model a ramp.micro model object
5-
#' @param i which graph (see get_net)
5+
#' @param i which graph (see get_graph)
66
#' @param cut cutat argument, # communities (optional)
77
#' @importFrom igraph membership cut_at
88
#' @returns a ramp.micro model object
99
#' @export
1010
net2patches =function(model, i, cut=NULL){
11-
net = get_net(model, i)
11+
net = get_graph(model, i)
1212
clusters = net$walktrap_clusters
1313
if(net$type == "b") xy = model$b
1414
if(net$type == "q") xy = model$q
@@ -65,7 +65,7 @@ net2patches =function(model, i, cut=NULL){
6565
#' @returns a ramp.micro model object
6666
#' @export
6767
plot_patches = function(model, i, cut=NULL, f_color = viridis::turbo, stretch=0.1, lwd=2, bbend=3, mtl = NULL){
68-
net = get_net(model, i)
68+
net = get_graph(model, i)
6969
model = net2patches(model, i, cut)
7070
if(net$type == "b") patches <- model$b_patches
7171
if(net$type == "q") patches <- model$q_patches
@@ -87,7 +87,7 @@ plot_patches = function(model, i, cut=NULL, f_color = viridis::turbo, stretch=0.
8787
#' Plot the ouptuts of a graph using
8888
#'
8989
#' @param model a `ramp.micro` model object
90-
#' @param graphs a graphswork object
90+
#' @param graph a graphswork object
9191
#' @param cut optional arguent for cut_at
9292
#' @param alg walktrap = "wt" or greedy = "gr"
9393
#' @param f_color a function that returns a list of colors (e.g. viridis::turbo)

R/viz_K_matrices.R renamed to R/plot_K.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ plot_Kbb = function(model, cx_b=2, cx_q=0.3,
7979
clr_b ="#cc444bCC", clr_q ="#4361eeCC"
8080
){
8181
with(model,with(Mpar,
82-
{
83-
frame_bq(b, q)
84-
add_arrows_xx(b, KGV$Kbb, min_edge_frac=min_edge_frac,
85-
r=r, arw_lng=arw_lng, lwd=lwd, arw_clr=arw_clr, seg_clr=seg_clr)
86-
with(model, if(exists("s")) add_points_s(s, cx_s=cx_q))
87-
add_points_q(q, cx_q=cx_q, clr_q=clr_q)
88-
add_points_bb(b, KGV$Kbb, cx_b=cx_b, clr_bA=arw_clr, clr_bB=clr_b)
89-
}))
82+
{
83+
frame_bq(b, q)
84+
add_arrows_xx(b, KGV$Kbb, min_edge_frac=min_edge_frac,
85+
r=r, arw_lng=arw_lng, lwd=lwd, arw_clr=arw_clr, seg_clr=seg_clr)
86+
with(model, if(exists("s")) add_points_s(s, cx_s=cx_q))
87+
add_points_q(q, cx_q=cx_q, clr_q=clr_q)
88+
add_points_bb(b, KGV$Kbb, cx_b=cx_b, clr_bA=arw_clr, clr_bB=clr_b)
89+
}))
9090
return(invisible())
9191
}
9292

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

R/utils.R

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,3 @@ get_matrix = function(model, i){
7979
if(i==8) M = model$Mpar$bigMM
8080
return(M)
8181
}
82-
83-
#' Choose one of the networks describing dispersion
84-
#' @description
85-
#' 1. Kbb; 2. Kqq;
86-
#' 3. G; 4. GG;
87-
#' 5. V; 6. VV;
88-
#' 7. M; 8. MM;
89-
#'
90-
#'
91-
#' @param i an index for the type of model
92-
#' @param model a model defined as a compound [list]
93-
#'
94-
#' @return a net object
95-
#' @export
96-
get_net = function(model, i){
97-
if(i==1) net = model$graphs$Kbb_net
98-
if(i==2) net = model$graphs$Kqq_net
99-
if(i==3) net = model$graphs$G_net
100-
if(i==4) net = model$graphs$GG_net
101-
if(i==5) net = model$graphs$V_net
102-
if(i==6) net = model$graphs$VC_net
103-
if(i==7) net = model$graphs$M_net
104-
if(i==8) net = model$graphs$MM_net
105-
return(net)
106-
}

0 commit comments

Comments
 (0)