- Overview
- Installation
- Save points into local files
- Non-parametric estimation in R
- Parametric estimation in OPIPP
The interaction function estimates the probability of a distance between any pairs of points. It is well used in the point process model of spatial patterns. The common formulation of the function for mosaic simulation is
where u>δ
, the 0
. The threshold δ
usually uses the minimal distance in the spatial pattern, while φ
and α
needs a specific estimation method to find their suitable values.
In this part, we introduce how to use R
and OPIPP
to get the parameters of the interaction function
The estimation requires the spatstat libaray in R environment. For installing R
, you can get the download and related instructions from here. After installation of the environment, please open R
, the RGui
or Rterm
, usually both are already installed. You can find a console and then type the following line to install the spatstat
package,
install.packages('spatstat')
Here, we suppose you know the basic of a retinal mosaic and the usage of the OPIPP
module. Furthermore, you need to modify R scripts for your applications. Therefore, We recommend learning the basic of the R
language for further usage.
To estimate the interaction function from a mosaic, you need to dump points into local files so that the R
can access the data. We recommend using the Mosaic.save
method to complete this goal. For example, we save points in the natural_mosaic
into two files, as
# Separate coordinates into two texture files
# the 1st is "examples/natural/HC/F8-1-points-x.txt" with x-coordinates
# the 2nd is "examples/natural/HC/F8-1-points-y.txt" with y-coordinates
natural_mosaic.save("examples/natural/HC/F8-1-points.txt", separate=True)
The next step is to run the R
environment with the following script.
# import libraries
library(spatstat)
library(pracma)
# load files that have coordinates of points
x <- scan("examples/natural/HC/F8-1-points-x.txt") # x axis of points
y <- scan("examples/natural/HC/F8-1-points-y.txt") # y axis of points
# x-axis values (distances) in the interaction function
r <- linspace(2, 50, 17) # similar to numpy.linsapce
# r is an arithmetic sequence from 2 to 50 and the step is 3
# It means the estimate will yield a list of probabilities corresponding to distances from 2 to 50.
# Please modify these values depending on the value range of distances in a given mosaic.
# define a Poisson point process
u <- ppp(x, y, c(0, 300), c(0, 300))
# The 3rd argument is c(min_x, max_y)
# and the 4th is c(min_y, max_y),
# corresponding to attributes in OPIPP.Scope
# Please modify them when apply to other mosaics.
# non-parametric estimate
fit <- ppm(u ~1, PairPiece(r = r))
# draw results
plot(fitin(fit))
# Write results to local files
write.table(parameters(fit)$gammas, "examples/natural/HC/F8-1-h-gammas.txt", row.names = FALSE, col.names = FALSE)
write.table(parameters(fit)$r, "examples/natural/HC/F8-1-h-r.txt", row.names = FALSE, col.names = FALSE)
The last step is to use the estimate_interaction
method to fit the curve of
from OPIPP.utils import estimate_interaction
Mosaic.get_distances
method. For example, we get the minimal distance in the natural_mosiac
as
min_distance = natural_mosaic.get_distances().min()
To get parameters of the interaction function, you need load estimation results by R
and then deliver these data and the value of delta to the estimate_interaction
method. The return is three parameters of the function, as
import numpy as np
# load non-parametric estimation results
gammas = np.loadtxt("examples/natural/HC/F8-1-h-gammas.txt")
rs = np.loadtxt("examples/natural/HC/F8-1-h-r.txt")
# return is [7.5, 32.12066166693056, 2.6487686148563743]
# corresponding to [δ, φ, α]
parameters = estimate_interaction(gammas, rs, delta=7.5, draw=True)
Here, you can use draw=True
to check the results of estimation and fitting.
In the end, we get the interaction function of the HC mosaic, as
# at last, we get the interaction function
h_func = pattern.get_interaction_func(parameters)