matiasandina / fed3

An R package to read and analyze data from FED3 devices
https://matiasandina.github.io/fed3/
Other
0 stars 0 forks source link

bin_lightcycle will fail when using it with a zt #12

Closed matiasandina closed 1 year ago

matiasandina commented 1 year ago

The lights_on_hour is given in local time instead of being given in zt time. This will produce erroneous binning. I think that it's easy to catch that bin_pellets_lightcycle is being called with zt and modify lights_on_hour to 0 and lights_off_hour to 12. This function will mostly be used with zt anyway since we want the light/dark info instead of the date, so it's better this way. We can also add a zt_date to this result to make clear that we are talking about zt datetime instead of date.

> df %>% recalculate_pellets() %>% add_zt(Pi_Time, lights_on_hour=7) %>% bin_pellets_lightcycle(Pi_Time, lights_on_hour=7)
# A tibble: 6 × 3
# Groups:   date, light_cycle [6]
  date       light_cycle pellets
  <date>     <chr>         <int>
1 2023-08-03 dark             89
2 2023-08-03 light            23
3 2023-08-04 dark            168
4 2023-08-04 light            45
5 2023-08-05 dark             87
6 2023-08-05 light            14
> df %>% recalculate_pellets() %>% add_zt(Pi_Time, lights_on_hour=7) %>% bin_pellets_lightcycle(zt, lights_on_hour=7)
# A tibble: 5 × 3
# Groups:   date, light_cycle [5]
  date       light_cycle pellets
  <date>     <chr>         <int>
1 2023-08-03 dark             53
2 2023-08-03 light           138
3 2023-08-04 dark             71
4 2023-08-04 light           150
5 2023-08-05 dark             14
matiasandina commented 1 year ago

This could potentially solve the issue

df %>% 
  recalculate_pellets() %>% 
  add_zt(Pi_Time, lights_on_hour = 7) %>%
  bin_pellets_lightcycle(zt, lights_on_hour = 0, lights_off_hour = 12)

But I think the user should not need to know this, it's better to intercept the use of zt and adjust. When using zt, we should also have a zt_date and a date, since it can be really confusing for the user. Unfortunately, we would need to add an extra parameter to the function to keep the original date...Will evaluate in the future if it makes sense at all.