Creates a track from a track expression.

emr_track.create(
  track,
  space,
  categorical,
  expr,
  stime = NULL,
  etime = NULL,
  iterator = NULL,
  keepref = FALSE,
  filter = NULL,
  override = FALSE
)

Arguments

track

the name of the newly created track

space

db path, one of the paths supplied in emr_db.connect

categorical

if 'TRUE' track is marked as categorical

expr

track expression

stime

start time scope

etime

end time scope

iterator

track expression iterator. If 'NULL' iterator is determined implicitly based on track expressions. See also 'iterator' section.

keepref

If 'TRUE' references are preserved in the iterator

filter

Iterator filter

override

Boolean indicating whether the creation intends to override an existing track (default FALSE)

Value

None.

Details

This function creates a new track based on the values from the track expression. The location of the track is controlled via 'space' parameter which can be any of the db_dirs supplied in emr_db.connect

iterator

There are a few types of iterators:

  • Track iterator: Track iterator returns the points (including the reference) from the specified track. Track name is specified as a string. If `keepref=FALSE` the reference of each point is set to `-1`
    Example:

    # Returns the level of glucose one hour after the insulin shot was made
    emr_vtrack.create("glucose", "glucose_track", func="avg", time.shift=1)
    emr_extract("glucose", iterator="insulin_shot_track")

  • Id-Time Points Iterator: Id-Time points iterator generates points from an *id-time points table*. If `keepref=FALSE` the reference of each point is set to `-1`.
    Example:

    # Returns the level of glucose one hour after the insulin shot was made
    emr_vtrack.create("glucose", "glucose_track", func = "avg", time.shift = 1)
    r <- emr_extract("insulin_shot_track") # <-- implicit iterator is used here
    emr_extract("glucose", iterator = r)

  • Ids Iterator: Ids iterator generates points with ids taken from an *ids table* and times that run from `stime` to `etime` with a step of 1. If `keepref=TRUE` for each id-time pair the iterator generates 255 points with references running from `0` to `254`. If `keepref=FALSE` only one point is generated for the given id and time, and its reference is set to `-1`.
    Example:

    stime <- emr_date2time(1, 1, 2016, 0)
    etime <- emr_date2time(31, 12, 2016, 23)
    emr_extract("glucose", iterator = data.frame(id = c(2, 5)), stime = stime, etime = etime)

  • Time Intervals Iterator: *Time intervals iterator* generates points for all the ids that appear in 'patients.dob' track with times taken from a *time intervals table* (see: Appendix). Each time starts at the beginning of the time interval and runs to the end of it with a step of 1. That being said the points that lie outside of `[stime, etime]` range are skipped.
    If `keepref=TRUE` for each id-time pair the iterator generates 255 points with references running from `0` to `254`. If `keepref=FALSE` only one point is generated for the given id and time, and its reference is set to `-1`.
    Example:
    # Returns the level of hangover for all patients the next day after New Year Eve for the years 2015 and 2016
    stime1 <- emr_date2time(1, 1, 2015, 0)
    etime1 <- emr_date2time(1, 1, 2015, 23)
    stime2 <- emr_date2time(1, 1, 2016, 0)
    etime2 <- emr_date2time(1, 1, 2016, 23)
    emr_extract("alcohol_level_track", iterator = data.frame(
    stime = c(stime1, stime2),
    etime = c(etime1, etime2)
    ))

  • Id-Time Intervals Iterator: *Id-Time intervals iterator* generates for each id points that cover `['stime', 'etime']` time range as specified in *id-time intervals table* (see: Appendix). Each time starts at the beginning of the time interval and runs to the end of it with a step of 1. That being said the points that lie outside of `[stime, etime]` range are skipped.
    If `keepref=TRUE` for each id-time pair the iterator generates 255 points with references running from `0` to `254`. If `keepref=FALSE` only one point is generated for the given id and time, and its reference is set to `-1`

  • Beat Iterator: *Beat Iterator* generates a "time beat" at the given period for each id that appear in 'patients.dob' track. The period is given always in hours.
    Example:
    emr_extract("glucose_track", iterator=10, stime=1000, etime=2000)
    This will create a beat iterator with a period of 10 hours starting at `stime` up until `etime` is reached. If, for example, `stime` equals `1000` then the beat iterator will create for each id iterator points at times: 1000, 1010, 1020, ...
    If `keepref=TRUE` for each id-time pair the iterator generates 255 points with references running from `0` to `254`. If `keepref=FALSE` only one point is generated for the given id and time, and its reference is set to `-1`.

  • Extended Beat Iterator: *Extended beat iterator* is as its name suggests a variation on the beat iterator. It works by the same principle of creating time points with the given period however instead of basing the times count on `stime` it accepts an additional parameter - a track or a *Id-Time Points table* - that instructs what should be the initial time point for each of the ids. The two parameters (period and mapping) should come in a list. Each id is required to appear only once and if a certain id does not appear at all, it is skipped by the iterator.
    Anyhow points that lie outside of `[stime, etime]` range are not generated.
    Example:
    # Returns the maximal weight of patients at one year span starting from their birthdays
    emr_vtrack.create("weight", "weight_track", func = "max", time.shift = c(0, year()))
    emr_extract("weight", iterator = list(year(), "birthday_track"), stime = 1000, etime = 2000)

  • Periodic Iterator: periodic iterator goes over every year/month. You can use it by running emr_monthly_iterator or emr_yearly_iterator.
    Example:
    iter <- emr_yearly_iterator(emr_date2time(1, 1, 2002), emr_date2time(1, 1, 2017))
    emr_extract("dense_track", iterator = iter, stime = 1, etime = 3)
    iter <- emr_monthly_iterator(emr_date2time(1, 1, 2002), n = 15)
    emr_extract("dense_track", iterator = iter, stime = 1, etime = 3)

  • Implicit Iterator: The iterator is set implicitly if its value remains `NULL` (which is the default). In that case the track expression is analyzed and searched for track names. If all the track variables or virtual track variables point to the same track, this track is used as a source for a track iterator. If more then one track appears in the track expression, an error message is printed out notifying ambiguity.

Revealing Current Iterator Time: During the evaluation of a track expression one can access a specially defined variable named `EMR_TIME` (Python: `TIME`). This variable contains a vector (`numpy.ndarray` in Python) of current iterator times. The length of the vector matches the length of the track variable (which is a vector too).
Note that some values in `EMR_TIME` might be set 0. Skip those intervals and the values of the track variables at the corresponding indices.
# Returns times of the current iterator as a day of month
emr_extract("emr_time2dayofmonth(EMR_TIME)", iterator = "sparse_track")

Examples

emr_db.init_examples()
#> NULL

emr_track.create("new_dense_track", expr = "dense_track * 2", categorical = FALSE)
#> NULL
emr_extract("new_dense_track")
#>    id time ref new_dense_track
#> 1  22    1  -1              26
#> 2  24    1  -1              26
#> 3  25    1  -1              20
#> 4  25    2  -1              48
#> 5  25    3  -1              68
#> 6  25    6  -1             122
#> 7  25    8  -1             164
#> 8  25    9  -1             186
#> 9  25   10  -1             208
#> 10 25   12  -1             248
#> 11 27   23  -1             468
#> 12 27   50  -1            1000
#> 13 28    1  -1              26