
getwd()
rm(list = ls())
source('./src/data_pattern_generator.R')

library(FNN)
library(dplyr)

#----------------------- Get lgcp data ---------------------------
load("./data/BrS1R1.input2npgg.RData")

xy_coords <- ptmat[, c("x", "y")]

# 确定观测域的边界（稍微扩大以避免点落在边界上）
buffer <- 0.5  # 边界缓冲值，可根据数据尺度调整
x_min <- min(meta.data2$x)/1000 - buffer
x_max <- max(meta.data2$x)/1000 + buffer
y_min <- min(meta.data2$y)/1000 - buffer
y_max <- max(meta.data2$y)/1000 + buffer

xy.c_observed = as.matrix(xy_coords)
xy.c_observed = unname(xy.c_observed) /1000
plot(xy.c_observed)

#------------------------------ mesh --------------------------------------
win <- owin(c(x_min,x_max), c(y_min,y_max))
pl.dom = cbind(c(x_min,x_max,x_max,x_min), c(y_min,y_min,y_max,y_max))
bnd1 = Polygon(pl.dom, hole = FALSE)
max_edge = min(x_max, y_max)/100

mesh_full = inla.mesh.2d(pl.dom, boundary = bnd1, max.edge = max_edge)
plot(mesh_full)
points(xy.c_observed)

mesh_vertices <- mesh_full$loc[ ,1:2] # test set
loc_test = mesh_vertices
N = nrow(xy.c_observed)
n = mesh_full$n


#------------------------------- Dual mesh -------------------------------
alpha_weight = DualMesh_parameter(mesh_full)[[1]]
Dualmesh_loc = DualMesh_parameter(mesh_full)[[2]]

loc_n_N = rbind(mesh_vertices, xy.c_observed)

#------------------------------- covariate ----------------------------------
X_cov$DCIS_1 = scale(X_cov$DCIS_1)
X_cov$`Myoepi_KRT15+`= scale(X_cov$`Myoepi_KRT15+`)

# 观测点的 covarite: X_cov1
id1 = c()
for (i in 1:N) {
  dist = cbind(X_cov$x, X_cov$y) - t(replicate(nrow(X_cov), xy.c_observed[i, ]))
  id1[i] = which.min(sqrt(dist[ ,1]^2 + dist[ ,2]^2))
}
dat_pres1 = data.frame(
  "x" = xy.c_observed[ ,1],
  "y" = xy.c_observed[ ,2],
  "quad.size" = 0,
  "DCIS_1" = X_cov$DCIS_1[id1],
  "Myoepi_KRT15" = X_cov$`Myoepi_KRT15+`[id1],
  "pres" = 1
)
# 预测点（积分点）的 covarite: X_cov2
id2 = c()
for (i in 1:nrow(Dualmesh_loc)) {
  dist = cbind(X_cov$x, X_cov$y) - t(replicate(nrow(X_cov), Dualmesh_loc[i, ]))
  id2[i] = which.min(sqrt(dist[ ,1]^2 + dist[ ,2]^2))
}
dat_pres0 = data.frame(
  "x" = mesh_full$loc[ ,1],
  "y" = mesh_full$loc[ ,2],
  "quad.size" = alpha_weight,
  "DCIS_1" = X_cov$DCIS_1[id2],
  "Myoepi_KRT15" = X_cov$`Myoepi_KRT15+`[id2],
  "pres" = 0
)

dat_observed = rbind(dat_pres1, dat_pres0)

X = cbind(rep(1, N), dat_pres1$DCIS_1, dat_pres1$Myoepi_KRT15)
X_tilde = cbind(rep(1, n), dat_pres0$DCIS_1, dat_pres0$Myoepi_KRT15)
X_test = X_tilde
# A_tilde and A
zero_block1 = matrix(0, nrow = n, ncol = N)
zero_block2 = matrix(0, nrow = N, ncol = n)
A_tilde = as(cbind(diag(n), zero_block1), "dgCMatrix")
A = as(cbind(zero_block2, diag(N)), "dgCMatrix")

rm(zero_block1, zero_block2)

getwd()
save(list = ls(all.names = TRUE), file = "./data/lgcp_data_full.RData", envir = .GlobalEnv)
