Skip to content

Commit

Permalink
Merge branch 'develop' into beiwe-taskrunner
Browse files Browse the repository at this point in the history
  • Loading branch information
hackdna authored Jan 2, 2024
2 parents f2a8205 + f230edd commit 18bcf89
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
7 changes: 6 additions & 1 deletion forest/jasmine/data2mobmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ def collapse_data(
# Filter out rows where the GPS accuracy is beyond
# the provided accuracy_limit
data = data[data.accuracy < accuracy_limit]
if data.shape[0] == 0:
raise ValueError(
f"No GPS record with accuracy less than {accuracy_limit}."
)

# Get the start and end timestamps in seconds
t_start = sorted(np.array(data.timestamp))[0] / 1000
Expand Down Expand Up @@ -1000,7 +1004,8 @@ def infer_mobmat(mobmat: np.ndarray, interval: float, r: float) -> np.ndarray:
(mobmat, np.ones(n_rows).reshape(n_rows, 1))
)
# Append new pauses to the trajectory matrix
mobmat = np.vstack((mobmat, new_pauses_array))
if new_pauses_array.shape[0] > 0:
mobmat = np.vstack((mobmat, new_pauses_array))
# Sort the matrix by start time
mobmat = mobmat[mobmat[:, 3].argsort()].astype(float)

Expand Down
20 changes: 12 additions & 8 deletions forest/jasmine/mobmat2traj.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import logging
import math
import sys
from typing import Optional, Tuple

import numpy as np
Expand Down Expand Up @@ -278,7 +277,7 @@ def indicate_flight(
# Calculate k1 using the specified method
k1 = calculate_k1(method, current_t, current_x, current_y, bv_subset, pars)
if k1 is None:
sys.exit("Invalid method for calculate_k1.")
raise ValueError("Invalid method for calculate_k1.")

# Select flight and pause indicators from the bv_subset
flight_k = k1[bv_subset[:, 0] == 1]
Expand Down Expand Up @@ -662,8 +661,8 @@ def forward_impute(
method, start_t, start_x, start_y,
flight_table, pars
)
if weight is None:
sys.exit("Invalid method for calculate_k1.")
if weight is None or len(weight) == 0:
raise ValueError("Invalid method for calculate_k1.")

normalize_w = (weight + 1e-5) / float(sum(weight + 1e-5))
flight_index = np.random.choice(flight_table.shape[0], p=normalize_w)
Expand Down Expand Up @@ -743,7 +742,7 @@ def forward_impute(
pause_table, pars
)
if weight is None:
sys.exit("Invalid method for calculate_k1.")
raise ValueError("Invalid method for calculate_k1.")

normalize_w = (weight + 1e-5) / float(sum(weight + 1e-5))
pause_index = np.random.choice(pause_table.shape[0], p=normalize_w)
Expand Down Expand Up @@ -832,7 +831,7 @@ def backward_impute(
flight_table, pars
)
if weight is None:
sys.exit("Invalid method for calculate_k1.")
raise ValueError("Invalid method for calculate_k1.")

normalize_w = (weight + 1e-5) / float(sum(weight + 1e-5))
flight_index = np.random.choice(flight_table.shape[0], p=normalize_w)
Expand Down Expand Up @@ -907,8 +906,8 @@ def backward_impute(
method, end_t, end_x, end_y,
pause_table, pars
)
if weight is None:
sys.exit("Invalid method for calculate_k1.")
if weight is None or len(weight) == 0:
raise ValueError("Invalid method for calculate_k1.")

normalize_w = (weight + 1e-5) / float(sum(weight + 1e-5))
pause_index = np.random.choice(pause_table.shape[0], p=normalize_w)
Expand Down Expand Up @@ -972,6 +971,11 @@ def impute_gps(
# for observed flights, observed pauses, and missing intervals
flight_table, pause_table, mis_table = create_tables(mob_mat, bv_subset)

if len(flight_table) == 0:
raise ValueError("No flight observed in the data.")
if len(pause_table) == 0:
raise ValueError("No pause observed in the data.")

# initialize the imputed trajectory table
imp_table = np.zeros((1, 7))

Expand Down
7 changes: 7 additions & 0 deletions forest/oak/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def preprocess_bout(t_bout: np.ndarray, x_bout: np.ndarray, y_bout: np.ndarray,
- t_bout_interp: resampled timestamp (in Unix)
- vm_bout_interp: vector magnitude of acceleration
"""

if (
len(t_bout) < 2 or len(x_bout) < 2 or
len(y_bout) < 2 or len(z_bout) < 2
):
return np.array([]), np.array([])

t_bout_interp = t_bout - t_bout[0]
t_bout_interp = np.arange(t_bout_interp[0], t_bout_interp[-1], (1/fs))
t_bout_interp = t_bout_interp + t_bout[0]
Expand Down

0 comments on commit 18bcf89

Please sign in to comment.