有没有办法在spatstat中找到每个点周围特定区域内的近似配对?

huangapple go评论64阅读模式
英文:

Is where a way to find close pairs within a specific box eround each point in spatstat?

问题

I'm working with spatstat package in R.
我正在使用R中的spatstat包。

I have marked point patterns with mark variables Name, boxA, boxB. BoxA and boxB are dimensions of a rectangle around each point (bounding boxes).
我使用标记变量Name、boxA和boxB标记了点模式。BoxA和boxB是围绕每个点的矩形的尺寸(边界框)。

I want to delete points which lay inside the bounding boxes of other points.
我想要删除位于其他点的边界框内的点。

Previously I did it using closepairs and crosspairs and a fixed radius for neighboring points like this:
以前,我使用closepairscrosspairs以及固定半径来处理相邻点,如下所示:

I used different radius for both classes of points because their bounding boxes differed in size.
我为两类点使用了不同的半径,因为它们的边界框尺寸不同。

However now I have points with very different boxes within each class.
然而,现在每个类别内的点的边界框非常不同。

Is there a built-in function in spatstat to determine radius for the "close_list" for each point individually according to boxA or boxB or even to use for each point rectangles with their box dimensions instead of circles?
spatstat中是否有内置函数可以根据boxA或boxB为每个点单独确定“close_list”的半径,甚至可以为每个点使用具有其边界框尺寸而不是圆形的矩形?

英文:

I'm working with spatstat package in R.
I have marked point patterns with mark variables Name, boxA, boxB. BoxA and boxB are dimensions of a rectangle around each point (bounding boxes).
here is an example:

>split(u) # splits pattern in two classes of cells accord. to Name

Point pattern split by factor
 
negat:
Marked planar point pattern: 843917 points
Mark variables: Name, boxA, boxB 
window: rectangle = [3164, 66924] x [25085, 84325] units

posit:
Marked planar point pattern: 93569 points
Mark variables: Name, boxA, boxB 
window: rectangle = [3164, 66924] x [25085, 84325] units

I want to delete points which lay inside the bounding boxes of other points. Previously I did it using closepairs and crosspairs and a fixed radius for neighbouring points like this:

for (i in 1:length(names(u))) {
  if (names(u)[i] == "posit"){ R <- 20 }else{R <- 13} 
     close_list = closepairs(u[[i]], rmax = R, twice = FALSE)
     close_index <- close_list$j
        if(is.empty(close_index) == F){ u[[i]] <- u[[i]][-close_index] } 
                               }
  close_list <- NULL
  close_list = crosspairs(u$negat, u$posit, 13)
  close_index <- close_list$i
  if(is.empty(close_index) == F){u$negat <- u$negat[-close_index]}

I used different radius for both classes of points because their bounding boxes differed in size. However now I have points with very different boxes within each class.
Is where a build-in function in spatstat to determine radius for the "close_list" for each point individually according to boxA or boxB or even to use for each point rectangles with their box dimensions instead of circles?

@Adrian Baddeley offered a solution below to which I added a nested loop to find the numbers of points with overlapping boxes. Here is a small working example in which bounding boxes of points 3 and 4 overlap too much:

# pattern
X<-as.ppp(cbind(c(1,2.5,2,3),c(1,1,3,3)),c(0,5,0,5))

# marks (size of the bounding boxes (BB) around points)
M<-cbind("boxA"=c(1.5,2,4,3),"boxB"=c(1.5,2,4,3))
M<-as.data.frame(M)
X$marks<-M

# find and delete points inside BB of other points
df <- as.data.frame(X)
dx <- with(df, outer(x, x, "-"))
dy <- with(df, outer(y,y,"-"))
conflict <- (abs(dx) < df$boxA/2) & (abs(dy) < df$boxB/2)
diag(conflict) <- FALSE
conflict[upper.tri(conflict)] <- FALSE 
index<-c()
for(i in 1:dim(conflict)[1]){
   for(j in 1:dim(conflict)[2]){
      if(conflict[i,j] == TRUE){
      index <- c(index, i)}
   }
}
X<-X[-index]

This works fine with the small test pattern. I'll test it on real data though.

答案1

得分: 2

如果所有的框都具有相同的高宽比(高度与宽度的比率),那么您可以使用 nndist.ppppairdist.pppnncross.ppp,并使用参数 metric 来指定矩形距离度量(请参阅帮助文件和 convexmetric 的示例)。例如,如果高宽比是1.5,则

m <- convexmetric(owin(c(-1,1)/2, c(-1.5, 1.5)/2)
D <- pairdist(X, metric=m)

将给出所有点对之间的矩形距离。

如果框具有不同的高宽比,则 spatstat 中没有现有的支持来进行此计算。我建议将点模式数据转换为数据框,使用 as.data.frame,然后使用 outer 来执行计算。

英文:

If all the boxes have the same aspect ratio (ratio of height to width) then you could use nndist.ppp or pairdist.ppp or nncross.ppp using the argument metric to specify a rectangular distance metric (see the help file and examples for convexmetric). For example if the aspect ratio is 1.5 then

m &lt;- convexmetric(owin(c(-1,1)/2, c(-1.5, 1.5)/2)
D &lt;- pairdist(X, metric=m)

gives you the rectangular distances between all pairs of points.

If the boxes can have different aspect ratios, then there is no existing support in spatstat for this calculation. I would recommend converting the point pattern data to a data frame using as.data.frame and then using outer to perform the calculation.

huangapple
  • 本文由 发表于 2023年6月26日 21:27:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定