Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
^\.positai$
^\.claude$
^todo\.txt$
^README_cache$
^\.DS_Store$
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: walking
Title: Segments Accelerometry Data into Walking using the Python `forest` Module
Version: 0.5.1
Version: 0.6.0
Authors@R:
person(given = "John",
family = "Muschelli",
Expand Down
4 changes: 4 additions & 0 deletions R/aaa_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ oak_base = function() {
stop(
"Python package 'forest' is not installed in the active reticulate environment. ",
"Install it yourself before calling walking functions that use forest.\n",
"If installation fails with \"clang++: error: unsupported option '-fopenmp'\", ",
"that is a Python toolchain/OpenMP problem, not a walking problem.\n",
"Use a Python environment where forest and numba/llvmlite already resolve cleanly, ",
"then point reticulate at that environment.\n",
"See https://github.com/onnela-lab/forest/issues/293 and ",
"https://github.com/numba/llvmlite/issues/1389."
)
Expand Down
9 changes: 5 additions & 4 deletions R/find_walking.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ find_walking = function(
if (verbose) {
message("Bout is Preprocessed")
}
vm_bout = pp_out$vm_bout$vm
vm_bout = pp_out$vm_bout
vm = vm_bout$vm
vm_bout$vm = NULL
# step_frequency = do.call(reticulate::tuple, as.list(step_frequency))

oak = oak_base()
Expand All @@ -80,7 +82,7 @@ find_walking = function(
# fs = sample_rate)

cadence_bout = oak$find_walking(
vm_bout = vm_bout,
vm_bout = vm,
fs = sample_rate_analysis,
min_amp = min_amplitude,
step_freq = step_frequency,
Expand All @@ -91,9 +93,8 @@ find_walking = function(
if (verbose) {
message("OAK: Find walking is done")
}
vm_bout = pp_out$vm_bout

vm_bout$steps = cadence_bout
vm_bout$vm = NULL
vm_bout = as.data.frame(vm_bout)
# need to remove 1D aspect
vm_bout$time = c(vm_bout$time)
Expand Down
6 changes: 6 additions & 0 deletions R/preprocess_bout.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,17 @@ preprocess_bout_r = function(data, sample_rate = 10L) {
vm_bout_interp = np$sqrt(x_bout_interp**2 +
y_bout_interp**2 +
z_bout_interp**2) - 1
rm(x_bout_interp)
rm(y_bout_interp)
rm(z_bout_interp)

vm_bout = list(
t_bout_interp,
vm_bout_interp
)
rm(vm_bout_interp)
rm(t_bout_interp)

process_vm_bout(vm_bout, tz = orig_tz, sample_rate = sample_rate)
}

Expand Down
81 changes: 55 additions & 26 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ knitr::opts_chunk$set(
fig.path = "man/figures/README-",
out.width = "100%"
)
reticulate::py_require(
"git+https://github.com/onnela-lab/forest@45fb41038bd46c25d9e6a4442aa74fa03b501317",
python_version = "3.11")
```

# walking
Expand All @@ -33,10 +36,13 @@ devtools::install_github("muschellij2/walking")
`walking` no longer tries to install `forest` for you. Install `forest` in
your own Python environment first, then load `walking` from R.

If you hit the LLVM mismatch described in
If you hit `clang++: error: unsupported option '-fopenmp'` or the LLVM
mismatch described in
[onnela-lab/forest#293](https://github.com/onnela-lab/forest/issues/293) and
[numba/llvmlite#1389](https://github.com/numba/llvmlite/issues/1389), use a
Python environment where `forest` and its dependencies already resolve cleanly.
Python environment where `forest` and its dependencies already resolve cleanly,
then point `reticulate` at that environment with `use_condaenv()` or
`use_python()`.

## Example

Expand All @@ -54,33 +60,56 @@ print(res)
## Potential Conflicts

### Running `forest` and [`stepcount`](https://github.com/jhuwit/stepcount).
The two Python modules (`forest` and `stepcount`) can be be installed in the same `conda` environment, but if they are not, this will lead to an error message. The options. One solution is to run them in two separate R sessions (recommended).
The two Python modules (`forest` and `stepcount`) can have different conflicts of python dependencies and python versions. You can do specific call-outs with `callr` to try to resolve this:

Alternatively, you can try to install `forest` in the `stepcount` conda environment, such as:
## Running Walking with Different Python Environments

```{r, eval = FALSE}
envname = "stepcount2"
stepcount::conda_create_stepcount(envname = envname)
# if you have RETICULATE_PYTHON set
stepcount::unset_reticulate_python()
stepcount::use_stepcount_condaenv(envname = envname)
```

and then run examples such as:
If you want to use both walking estimation from `forest` and `stepcount`, you can run them in separate R processes using `callr` to avoid Python package conflicts.
Here's how you can do it:

```{r, eval = FALSE}
library(walking)
```{r, cache = TRUE}
library(callr)
library(stepcount)
envname = "stepcount2"

# if you have RETICULATE_PYTHON set
stepcount::unset_reticulate_python()
stepcount::use_stepcount_condaenv(envname = envname)

csv_file = system.file("test_data_bout.csv", package = "walking")
x = readr::read_csv(csv_file)
colnames(x)[colnames(x) == "UTC time"] = "time"
res = find_walking(data = x)
data = readr::read_csv(csv_file)
colnames(data)[colnames(data) == "UTC time"] = "time"

stepcount_callr = function(data,
...) {

reticulate::py_require("stepcount==3.11.0", python_version = "3.10")
sc <- reticulate::import("stepcount")
stepcount::stepcount_check()

res = stepcount::stepcount(data, ...)
return(res)
}

# 2. Run the isolated background R process
result <- callr::r(
func = stepcount_callr,
show = TRUE,
args = list(data = data) # Safely injects data into the process
)
head(result)

forest_callr = function(data,
...) {
reticulate::py_require(
"git+https://github.com/onnela-lab/forest@45fb41038bd46c25d9e6a4442aa74fa03b501317",
python_version = "3.11")
fr = reticulate::import("forest")
oak = fr$oak$base
oak
res = walking::estimate_steps_forest(data, ...)
return(res)
}

# 2. Run the isolated background R process
fresult <- callr::r(
func = forest_callr,
show = TRUE,
args = list(data = data) # Safely injects data into the process
)
head(fresult)
```

Remember, however, best practices for Python is "Always create a separate virtual environment for each project" and sometimes one for each "goal".
Loading
Loading