% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/compute_velocity_on_grid.R
\name{compute_velocity_on_grid}
\alias{compute_velocity_on_grid}
\title{Compute velocity on grid}
\usage{
compute_velocity_on_grid(
  x_emb,
  v_emb,
  density = 1,
  smooth = 0.5,
  n_neighbors = ceiling(n_obs/50),
  min_mass = 1,
  scale = 1,
  adjust_for_stream = FALSE,
  cutoff_perc = 5
)
}
\arguments{
\item{x_emb}{A matrix of dimension n_obs x n_dim specifying the embedding coordinates of the cells.}

\item{v_emb}{A matrix of dimension n_obs x n_dim specifying the velocity vectors of the cells.}

\item{density}{A numeric value specifying the density of the grid points along each dimension.
Default is \code{1}.}

\item{smooth}{A numeric value specifying the smoothing factor for the velocity vectors.
Default is \code{0.5}.}

\item{n_neighbors}{A numeric value specifying the number of nearest neighbors for each grid point.
Default is \code{ceiling(n_obs / 50)}.}

\item{min_mass}{A numeric value specifying the minimum mass required for a grid point to be considered.
Default is \code{1}.}

\item{scale}{A numeric value specifying the scaling factor for the velocity vectors.
Default is \code{1}.}

\item{adjust_for_stream}{Whether to adjust the velocity vectors for streamlines.
Default is \code{FALSE}.}

\item{cutoff_perc}{A numeric value specifying the percentile cutoff for removing low-density grid points.
Default is \code{5}.}
}
\value{
A list with two components:
\code{x_grid}, the grid coordinates, and \code{v_grid}, the smoothed velocity vectors on the grid.
}
\description{
Compute velocity on grid
}
\examples{
x_emb <- matrix(
  c(
    0, 0,
    1, 0,
    0, 1,
    1, 1
  ),
  ncol = 2,
  byrow = TRUE
)
v_emb <- matrix(
  c(
    1, 0,
    1, 0,
    0, 1,
    0, 1
  ),
  ncol = 2,
  byrow = TRUE
)

velocity_grid <- compute_velocity_on_grid(
  x_emb = x_emb,
  v_emb = v_emb,
  density = 0.1,
  n_neighbors = 2,
  adjust_for_stream = TRUE
)

names(velocity_grid)
dim(velocity_grid$x_grid)
dim(velocity_grid$v_grid)
head(velocity_grid$x_grid)
head(velocity_grid$v_grid)

grid_df <- expand.grid(
  x = velocity_grid$x_grid[1, ],
  y = velocity_grid$x_grid[2, ]
)

plot_df <- data.frame(
  x = grid_df$x,
  y = grid_df$y,
  xend = grid_df$x + c(velocity_grid$v_grid[1, , ]) * 0.2,
  yend = grid_df$y + c(velocity_grid$v_grid[2, , ]) * 0.2
)

ggplot2::ggplot(plot_df) +
  ggplot2::geom_segment(
    ggplot2::aes(x = x, y = y, xend = xend, yend = yend),
    arrow = grid::arrow(length = grid::unit(0.12, "inches")),
    na.rm = TRUE
  ) +
  ggplot2::coord_equal()
}
\references{
\url{https://github.com/theislab/scvelo/blob/master/scvelo/plotting/velocity_embedding_grid.py}
}
