Skip to contents

Given an object of class "tracklets" containing a list of tracklets and a custom function (or list of functions), this function iterate trough the tracklet lists to perform the specified computation and returns the original object of class "tracklets" with the result of the analysis appended.

Usage

analyseTracklets(trackDat = NULL, customFunc = NULL, progress = TRUE)

Arguments

trackDat

An object of class "tracklets" containing a list of tracklets and their characteristics classically used for further computations.

customFunc

A function or a list of functions used to perform the computation over all tracklets NB: in case customFunc is a list of unnamed function it will try to retrieve their names by returning the first character string following the function() call as result column name.

progress

A logical value (i.e., TRUE or FALSE) indicating whether a progress bar should be displayed to inform process progression (default = TRUE).

Value

this function returns an object of class "tracklets" with the result of the specified computation appended to each tracklet.

Author

Quentin PETITJEAN

Examples


set.seed(2023)
# generate some dummy tracklets
## start to specify some parameters to generate tracklets
TrackN <- 40 # the number of tracklet to simulate
TrackL <-
  1:1000 # the length of the tracklets or a sequence to randomly sample tracklet length
id <- 0
TrackList <- MoveR::trackletsClass(stats::setNames(lapply(lapply(seq(TrackN), function(i)
  trajr::TrajGenerate(sample(TrackL, 1), random = TRUE, fps = 1)), function(j) {
    id <<- id + 1
    data.frame(
      x.pos = j$x - min(j$x),
      y.pos = j$y - min(j$y),
      frame = j$time,
      identity = paste("Tracklet", id, sep = "_")
    )
  }), seq(TrackN)))

# check the tracklets
MoveR::drawTracklets(TrackList)


# Run some computation on the dataset using analyseTracklets
TrackList2 <-
  MoveR::analyseTracklets(
    TrackList,
    customFunc = list(
      # specify a first function to compute speed over each tracklet (a modulus present within the MoveR package)
      speed = function(x)
        MoveR::speed(x),
      # compute turning angle in radians over each tracklet (a modulus present within the MoveR package)
      TurnAngle = function(x)
        MoveR::turnAngle(
          x,
          unit = "radians"),
      # convert the time expressed in frame in second using a conversion factor of 25 frame per second
      TimeSec = function(x)
        x[["frame"]] / 25,
      # or in minutes
      TimeMin = function(x)
        x[["frame"]] / 25 / 60
    )
  )

# check the result for the first tracklet
str(TrackList2[["1"]])
#> 'data.frame':	886 obs. of  8 variables:
#>  $ x.pos    : num  15.3 17.5 19.5 20.6 21.9 ...
#>  $ y.pos    : num  7.13 6.64 5.92 4.52 3.17 ...
#>  $ frame    : num  0 1 2 3 4 5 6 7 8 9 ...
#>  $ identity : chr  "Tracklet_1" "Tracklet_1" "Tracklet_1" "Tracklet_1" ...
#>  $ speed    : num  NA 2.29 2.12 1.75 1.92 ...
#>  $ TurnAngle: num  NA -0.1317 -0.5853 0.1499 -0.0297 ...
#>  $ TimeSec  : num  0 0.04 0.08 0.12 0.16 0.2 0.24 0.28 0.32 0.36 ...
#>  $ TimeMin  : num  0 0.000667 0.001333 0.002 0.002667 ...

# plot the frequency plot of the particles' speed
par(mfrow = c(1, 2))
hist(MoveR::convert2List(TrackList2)[["speed"]],
     main = "Frequency plot of the particles' speed")

# plot the frequency plot of the particles' turning angle
Ht = circular::circular(
  MoveR::convert2List(TrackList2)[["TurnAngle"]],
  type = "angle",
  units = "radians",
  zero = 0
)
circular::rose.diag(
  Ht,
  bins = 24,
  shrink = 0.89,
  xlim = c(-1, 1),
  ylim = c(-1, 1),
  prop = 2,
  col = "gray",
  border = "black",
  units = 'radians',
  ticks = TRUE,
  main = "Frequency plot of the particles' turning angle"
)