Skip to content

Commit

Permalink
Merge branch 'develop' into coerce_longitude_9_jan
Browse files Browse the repository at this point in the history
  • Loading branch information
clementzach committed Jan 19, 2024
2 parents d7e73ca + 6355d51 commit ad325fc
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 312 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Install Forest dependencies
- name: Install Forest dependencies for Linux
# required by librosa
if: ${{ startsWith(matrix.os, 'ubuntu') }}
run: |
sudo apt-get update
sudo apt-get install -y ffmpeg libsndfile1
- name: Install Forest dependencies for Windows
# required by librosa
if: ${{ startsWith(matrix.os, 'windows') }}
uses: FedericoCarboni/setup-ffmpeg@v2
id: setup-ffmpeg
- name: Install Forest
run: pip install -e .
- name: Install dev dependencies
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
__pycache__/
.DS_Store

# IntelliJ project files
# IntelliJ, VsCode project files
.idea
.vscode

# for installing Forest in editable mode when developing
/forest.egg-info/
Expand All @@ -18,3 +19,6 @@ __pycache__/

#sphinx build
docs/_build/

# any python environment files
.python-version
7 changes: 0 additions & 7 deletions docs/source/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ import logging
logger = logging.getLogger(__name__)
```

Or like this:

```
from logging import getLogger
logger = getLogger(__name__)
```

## 3. How to insert log messages into definitions

Basic `logging` messages:
Expand Down
4 changes: 2 additions & 2 deletions forest/bonsai/simulate_gps_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
TRAVELLING_STATUS_LIST = range(11)


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class PossibleExits(Enum):
Expand Down
13 changes: 7 additions & 6 deletions forest/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

class Frequency(Enum):
"""This class enumerates possible frequencies for summary data."""
HOURLY = 1
DAILY = 24
HOURLY_AND_DAILY = "hourly_and_daily"
THREE_HOURLY = 3
SIX_HOURLY = 6
TWELVE_HOURLY = 12
MINUTE = 1
HOURLY = 60
THREE_HOURLY = 3 * 60
SIX_HOURLY = 6 * 60
TWELVE_HOURLY = 12 * 60
DAILY = 24 * 60
HOURLY_AND_DAILY = -1


class OSMTags(Enum):
Expand Down
11 changes: 8 additions & 3 deletions forest/jasmine/data2mobmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
TOLERANCE = 1e-6


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def cartesian(
Expand Down 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 @@ -1008,7 +1012,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
24 changes: 14 additions & 10 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 All @@ -13,8 +12,8 @@
from .data2mobmat import great_circle_dist, exist_knot


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


# the details of the functions are in paper [Liu and Onnela (2020)]
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
4 changes: 2 additions & 2 deletions forest/jasmine/sogp_gps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import numpy as np


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def calculate_k0(x1: np.ndarray, x2: np.ndarray, pars: list) -> float:
Expand Down
8 changes: 4 additions & 4 deletions forest/jasmine/tests/test_traj2stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def test_compute_window_size(sample_trajectory):
"""Testing window size is correct"""

window, _ = compute_window_and_count(
sample_trajectory[0, 3], sample_trajectory[-1, 6], 1
sample_trajectory[0, 3], sample_trajectory[-1, 6], 60
)

assert window == 3600
Expand All @@ -664,7 +664,7 @@ def test_compute_window_count(sample_trajectory):
"""Testing number of windows is correct"""

_, num_windows = compute_window_and_count(
sample_trajectory[0, 3], sample_trajectory[-1, 6], 1
sample_trajectory[0, 3], sample_trajectory[-1, 6], 60
)

assert num_windows == 24
Expand All @@ -674,7 +674,7 @@ def test_compute_window_size_6_hour(sample_trajectory):
"""Testing window size is correct 6 hour window"""

window, _ = compute_window_and_count(
sample_trajectory[0, 3], sample_trajectory[-1, 6], 6
sample_trajectory[0, 3], sample_trajectory[-1, 6], 360
)

assert window == 3600 * 6
Expand All @@ -684,7 +684,7 @@ def test_compute_window_count_6_hour(sample_trajectory):
"""Testing number of windows is correct 6 hour window"""

_, num_windows = compute_window_and_count(
sample_trajectory[0, 3], sample_trajectory[-1, 6], 6
sample_trajectory[0, 3], sample_trajectory[-1, 6], 360
)

assert num_windows == 4
Loading

0 comments on commit ad325fc

Please sign in to comment.