Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to create event-based obs out of media-based observations #336

Open
damianooldoni opened this issue Sep 19, 2024 · 1 comment
Milestone

Comments

@damianooldoni
Copy link
Member

The need of such a function emerged in #332.

@damianooldoni damianooldoni added this to the v1.x milestone Sep 19, 2024
@Rafnuss
Copy link
Collaborator

Rafnuss commented Dec 29, 2024

I've been developing such a function for my personal use (Wildlife insight media-based data). But I'm all a bit confused now on which standard I should be developing the code (camtrap-dp 0.1.6 or 1.0.1).

The function uses the assess_temporal_independence() (see #353 ) and group together media/record according to the rules (minDeltaTime_duration and deltaTimeComparedTo). I use sequenceID (or eventID?) of the first media for the sequences. I also modify the sequenceID of media, is that correct? I finally delete the non-independant observation. But because I don't have the observationStart and observationEnd (only timestamp), I'm loosing quite a lot of information here.

Let me know if you'd like to use such a function and I can make a PR. No worries if that's not the approach you were thinking of, just thought I could share this.

#' Convert media based to event-based
#'
#' @param pkg A datapackage
#' @param minDeltaTime_dur: Duration in minutes between records of the same
#'   species at the same station to be considered independent.
#' @param deltaTimeComparedTo: Character, `"lastIndependentRecord"` or
#'   `"lastRecord"`.
#'   For two records to be considered independent, must the second one be at
#'   least `minDeltaTime` minutes after the last independent record of the same
#'   species (`deltaTimeComparedTo = "lastIndependentRecord"`), or
#'   `minDeltaTime` minutes after the last record (`deltaTimeComparedTo =
#'   "lastRecord"`)?
#'   If `minDeltaTime` is 0, `deltaTimeComparedTo` should be NULL.
#' @export
media2event <- function(pkg,
                        minDeltaTime_duration = 60,
                        deltaTimeComparedTo = "lastRecord") { # or "lastIndependentRecord"

  obs <- pkg$data$observations %>%
    dplyr::group_by(.data$scientificName, .data$deploymentID) %>%
    dplyr::arrange(.data$scientificName, .data$deploymentID, .data$timestamp) %>%
    mutate(
      independent = assess_temporal_independence(
        .data$timestamp,
        minDeltaTime_dur = minDeltaTime_duration,
        deltaTimeComparedTo = deltaTimeComparedTo
      )
    ) %>%
    ungroup()

  obs$NewSequenceID <- obs$sequenceID[obs$independent][cumsum(obs$independent)]

  # Replace sequenceID in media
  pkg$data$media <- pkg$data$media %>%
    left_join(
      obs %>% select(sequenceID, NewSequenceID),
      by = "sequenceID"
    ) %>%
    mutate(sequenceID = NewSequenceID) %>%
    select(-NewSequenceID)

  # Replace sequenceID in obs and remove non-independant obs.
  pkg$data$observations <- obs %>%
    mutate(sequenceID = NewSequenceID) %>%
    filter(independent) %>%
    select(-c(independent, NewSequenceID))

  return(pkg)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants