Skip to content

Commit 9ec0f32

Browse files
authored
Improved style and added some comments
1 parent 41a1684 commit 9ec0f32

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

tads.r

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
library('RANN')
2-
# main function that generates the paths
3-
generate_tad_random = function(hops = 1000, origin = rep(0,3), limit =1, abs_limit = limit * 4 ,scale = 0.1, min_delta=scale, cis=50, warp = NULL){
2+
# main function that generates the paths. A path is a list of connected x,y,x coords and it's length is set by _hops_.
3+
# origin = the triplet coordinate where a path begins
4+
# limit = linear distance away from the origin after the path will be penalized and forced to return towards the origin
5+
# it determines the extent of a domain.
6+
# abs_limit = as the path grows (see warp) one might one to bound the scope of the full path. This determines the global limits of the path
7+
# scale = scaling factor
8+
# min_delta = minimim step required by the walker to call a hop valid
9+
# cis =
10+
# warp = a path can suddenly do an abrupt, long-range hop in order to start populating a different volume. This setting determines how ofter an warp will happen. Once the path warps, the origin is reset.
11+
generate_tad_random = function(hops = 1000, origin = rep(0,3), limit =1, abs_limit = limit * 4 ,scale = 0.1, min_delta = scale, cis = 50, warp = NULL){
412

513
central_origin = origin
614
tad_loc = data.frame(x = origin[1], y = origin[2], z = origin[3])
715
#tad_loc = origin
816
current = origin
9-
pos = data.frame(x=current[1], y=current[2], z=current[3])
17+
pos = data.frame(x = current[1], y = current[2], z = current[3])
1018

1119
delta = c(0,0,0)
1220
mem_trail = delta
@@ -49,17 +57,17 @@ generate_tad_random = function(hops = 1000, origin = rep(0,3), limit =1, abs_lim
4957
}
5058
}
5159
iter = iter +1
52-
fhop = tensor(origin, current, limit= limit, scale = scale)
53-
if(hop >=cis){
60+
fhop = tensor(origin, current, limit = limit, scale = scale)
61+
if(hop >= cis){
5462
memory = memory_centroid(pos[(nrow(pos)-cis):nrow(pos), ])
5563
} else {
5664
memory = colMeans(pos) #current + rnorm(3, 0, min_delta)
5765
}
5866

5967
#cat(sprintf("last jump was of %.2f", euclidean_distance(fhop$pos, current)))
6068
#message(sprintf("from the origin %.2f", euclidean_distance(fhop$pos, origin)))
61-
if(nrow(pos)>0){
62-
knn = nn2(as.matrix(pos), matrix(fhop$pos, ncol=3, nrow=1), k = nrow(pos))
69+
if(nrow(pos) > 0){
70+
knn = nn2(as.matrix(pos), matrix(fhop$pos, ncol = 3, nrow = 1), k = nrow(pos))
6371
knn = knn$nn.dist[1,1] > min_delta *1.52
6472
}else{
6573
knn = TRUE
@@ -75,7 +83,7 @@ generate_tad_random = function(hops = 1000, origin = rep(0,3), limit =1, abs_lim
7583
delta = c(delta, fhop$delta)
7684
current = fhop$pos
7785
hop = hop + 1
78-
cat(sprintf("\r%.2f%%\t%s", 100*hop/hops, iter))
86+
cat(sprintf("\r%.2f%%\t%s", 100 * hop/hops, iter))
7987
}
8088

8189

@@ -85,16 +93,16 @@ generate_tad_random = function(hops = 1000, origin = rep(0,3), limit =1, abs_lim
8593
#path = matrix(pos, ncol=3, byrow=T)
8694
path = pos
8795
colnames(path) = c("x","y","z")
88-
deltas = matrix(delta, ncol=3, byrow=T)
96+
deltas = matrix(delta, ncol = 3, byrow = T)
8997
colnames(deltas) = c("dx","dy","dz")
9098

9199
#as.data.frame(cbind(path, deltas))
92100
#as.data.frame(path)
93-
list(path=as.data.frame(path), deltas=deltas, mem_trail=mem_trail, cis=cis)
101+
list(path = as.data.frame(path), deltas = deltas, mem_trail = mem_trail, cis = cis)
94102
}
95103

96104
# main engine that suggests the next step on a path
97-
tensor= function(origin=c(0,0,0), current=c(0,0,0), limit = 1, penalty_factor = 0.25, scale = 0.1){
105+
tensor= function(origin = c(0,0,0), current = c(0,0,0), limit = 1, penalty_factor = 0.25, scale = 0.1){
98106
pf = penalty_factor
99107
x = current[1]
100108
y = current[2]
@@ -106,15 +114,15 @@ tensor= function(origin=c(0,0,0), current=c(0,0,0), limit = 1, penalty_factor =
106114
nx = rnorm(1, 0, scale)
107115
ny = rnorm(1, 0, scale)
108116
nz = rnorm(1, 0, scale)
109-
delta = c(nx, ny, nz) + (c(dx,dy,dz) * sapply(c(0,0,0), function(x){ifelse(sign(x)==0, 1, sign(x))}))
117+
delta = c(nx, ny, nz) + (c(dx,dy,dz) * sapply(c(0,0,0), function(x){ifelse(sign(x) == 0, 1, sign(x))}))
110118

111119
} else {
112120
nx = rnorm(1, 0, scale)
113121
ny = rnorm(1, 0, scale)
114122
nz = rnorm(1, 0, scale)
115123
delta = -c(nx, ny, nz)
116124
}
117-
return(list(pos = current + delta, delta=delta))
125+
return(list(pos = current + delta, delta = delta))
118126
}
119127
# basic functions
120128
euclidean_distance = function(a, b){
@@ -131,12 +139,12 @@ memory_centroid = function(a){
131139
colMeans(a)
132140
}
133141

134-
knn_path = function(tad_list, k=100, len=20, region=20:30){
142+
knn_path = function(tad_list, k = 100, len = 20, region = 20:30){
135143

136144
end = nrow(tad_list$path)
137-
start = tad_list$cis + (end*0.1)
145+
start = tad_list$cis + (end * 0.1)
138146

139-
knn = nn2(tad_list$path, tad_list$path, k=100)
147+
knn = nn2(tad_list$path, tad_list$path, k = 100)
140148
k = c()
141149

142150
x = sample(start:end, 1)
@@ -149,7 +157,7 @@ knn_path = function(tad_list, k=100, len=20, region=20:30){
149157
}
150158
}
151159
dev.new()
152-
plot(tad_list$path[k,], t='l')
160+
plot(tad_list$path[k,], t = 'l')
153161
return(tad_list$path[k,])
154162
}
155163

@@ -167,19 +175,18 @@ plot_progress = function(pos, mem_trail, warp = NULL){
167175
n_mem = nrow(mem_trail)
168176
f_warp = 1:n_mem %% warp == 0
169177

170-
plot(pos[, 1], pos[, 2], t='l', xlab="X", ylab="Y")
171-
points(mem_trail[f_warp, 1], mem_trail[f_warp, 2], pch=19, col='red')
172-
abline(v=mem_trail[n_mem, 1], h=mem_trail[n_mem, 2], col='lightblue', lty=2, lwd=2)
178+
plot(pos[, 1], pos[, 2], t = 'l', xlab = "X", ylab = "Y")
179+
points(mem_trail[f_warp, 1], mem_trail[f_warp, 2], pch = 19, col = 'red')
180+
abline(v = mem_trail[n_mem, 1], h = mem_trail[n_mem, 2], col = 'lightblue', lty = 2, lwd = 2)
173181

174-
plot(pos[, 1], pos[, 3], t='l', xlab="X", ylab="Z")
175-
points(mem_trail[f_warp, 1], mem_trail[f_warp, 3],pch=19, col='red')
176-
abline(v=mem_trail[n_mem, 1], h=mem_trail[n_mem, 3], col='lightblue', lty=2, lwd=2)
182+
plot(pos[, 1], pos[, 3], t = 'l', xlab = "X", ylab = "Z")
183+
points(mem_trail[f_warp, 1], mem_trail[f_warp, 3], pch = 19, col = 'red')
184+
abline(v = mem_trail[n_mem, 1], h = mem_trail[n_mem, 3], col = 'lightblue', lty = 2, lwd = 2)
177185

178-
plot(pos[, 2], pos[, 3], t='l', xlab="Y", ylab="Z")
179-
points(mem_trail[f_warp, 2], mem_trail[f_warp, 3],pch=19, col='red')
180-
abline(v=mem_trail[n_mem, 2], h=mem_trail[n_mem, 3], col='lightblue', lty=2, lwd=2)
186+
plot(pos[, 2], pos[, 3], t = 'l', xlab = "Y", ylab = "Z")
187+
points(mem_trail[f_warp, 2], mem_trail[f_warp, 3], pch = 19, col = 'red')
188+
abline(v = mem_trail[n_mem, 2], h = mem_trail[n_mem, 3], col = 'lightblue', lty = 2, lwd = 2)
181189

182-
183190
# plot(mem_trail[,1], mem_trail[,2], t='l', col='red')
184191
# plot(mem_trail[,2], mem_trail[,3], t='l', col='red')
185192
# plot(mem_trail[,1], mem_trail[,3], t='l', col='red')

0 commit comments

Comments
 (0)