Skip to content

Commit

Permalink
Merge branch 'hackdna/fix-logging' into beiwe-taskrunner
Browse files Browse the repository at this point in the history
  • Loading branch information
biblicabeebli committed Nov 30, 2023
2 parents 810ef6c + 3db2547 commit abab9ad
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 72 deletions.
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
MINUTELY = 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
4 changes: 2 additions & 2 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
4 changes: 2 additions & 2 deletions forest/jasmine/mobmat2traj.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,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
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
89 changes: 85 additions & 4 deletions forest/jasmine/tests/test_traj2stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,87 @@ def test_gps_summaries_log_format(
assert np.all(dates_stats == dates_log)


def test_gps_summaries_summary_vals(
coords1, sample_trajectory, sample_nearby_locations, mocker
):
"""Testing gps summaries summary values are correct"""
mocker.patch(
"forest.jasmine.traj2stats.get_nearby_locations",
return_value=sample_nearby_locations,
)
mocker.patch("forest.jasmine.traj2stats.locate_home", return_value=coords1)

parameters = Hyperparameters()

summary, _ = gps_summaries(
traj=sample_trajectory,
tz_str="Europe/London",
frequency=Frequency.DAILY,
parameters=parameters,
)

assert np.all(summary["obs_duration"] == 24)
assert summary["obs_day"].iloc[0] == 10
assert summary["obs_night"].iloc[0] == 14
assert summary["obs_day"].iloc[1] == 24
assert summary["obs_night"].iloc[1] == 0
assert np.all(summary["home_time"] == 0)
assert summary["dist_traveled"].iloc[0] == 0.208
assert summary["dist_traveled"].iloc[1] == 0
assert np.round(summary["max_dist_home"].iloc[0], 3) == 0.915
assert np.round(summary["max_dist_home"].iloc[1], 3) == 0.915
assert np.round(summary["radius"].iloc[0], 3) == 0.013
assert summary["radius"].iloc[1] == 0
assert np.round(summary["diameter"].iloc[0], 3) == 0.064
assert summary["diameter"].iloc[1] == 0
assert summary["num_sig_places"].iloc[0] == 2
assert summary["num_sig_places"].iloc[1] == 1
assert np.round(summary["entropy"].iloc[0], 3) == 0.468
assert summary["entropy"].iloc[1] == 0
assert round(summary["total_flight_time"].iloc[0], 3) == 1.528
assert summary["total_flight_time"].iloc[1] == 0
assert round(summary["av_flight_length"].iloc[0], 3) == 0.052
assert summary["av_flight_length"].iloc[1] == 0
assert round(summary["sd_flight_length"].iloc[0], 3) == 0.012
assert summary["sd_flight_length"].iloc[1] == 0
assert round(summary["av_flight_duration"].iloc[0], 3) == 0.382
assert summary["av_flight_duration"].iloc[1] == 0
assert round(summary["sd_flight_duration"].iloc[0], 3) == 0.132
assert summary["sd_flight_duration"].iloc[1] == 0
assert round(summary["total_pause_time"].iloc[0], 3) == 22.472
assert summary["total_pause_time"].iloc[1] == 24
assert round(summary["av_pause_duration"].iloc[0], 3) == 4.494
assert summary["av_pause_duration"].iloc[1] == 24
assert round(summary["sd_pause_duration"].iloc[0], 3) == 3.496
assert summary["sd_pause_duration"].iloc[1] == 0


def test_gps_summaries_pcr(
coords1, sample_trajectory, sample_nearby_locations, mocker
):
"""Testing gps summaries pcr"""
mocker.patch(
"forest.jasmine.traj2stats.get_nearby_locations",
return_value=sample_nearby_locations,
)
mocker.patch("forest.jasmine.traj2stats.locate_home", return_value=coords1)

parameters = Hyperparameters()
parameters.pcr_bool = True

summary, _ = gps_summaries(
traj=sample_trajectory,
tz_str="Europe/London",
frequency=Frequency.DAILY,
parameters=parameters,
)

assert summary["physical_circadian_rhythm"].iloc[0] == 0
assert summary["physical_circadian_rhythm"].iloc[1] == 1
assert summary["physical_circadian_rhythm_stratified"].iloc[0] == 0
assert summary["physical_circadian_rhythm_stratified"].iloc[1] == 0


@pytest.fixture()
def mobmat1():
"""mobility matrix 1"""
Expand Down Expand Up @@ -573,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 @@ -583,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 @@ -593,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 @@ -603,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
22 changes: 14 additions & 8 deletions forest/jasmine/traj2stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
from forest.utils import get_ids


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


@dataclass
Expand Down Expand Up @@ -1125,8 +1125,8 @@ def gps_summaries(
ValueError: Frequency is not valid
"""

if frequency == Frequency.HOURLY_AND_DAILY:
raise ValueError("Frequency must be 'hourly' or 'daily'")
if frequency in [Frequency.HOURLY_AND_DAILY, Frequency.MINUTELY]:
raise ValueError(f"Frequency cannot be {frequency.name.lower()}.")

if frequency != Frequency.DAILY:
parameters.split_day_night = False
Expand Down Expand Up @@ -1161,7 +1161,7 @@ def gps_summaries(
traj, [3, 4, 5], tz_str, 3600*24
)
window, num_windows = compute_window_and_count(
start_stamp, end_stamp, 24, parameters.split_day_night
start_stamp, end_stamp, 24*60, parameters.split_day_night
)

if num_windows <= 0:
Expand Down Expand Up @@ -1484,23 +1484,23 @@ def get_time_range(


def compute_window_and_count(
start_stamp: int, end_stamp: int, window_hours: int,
start_stamp: int, end_stamp: int, window_minutes: int,
split_day_night: bool = False
) -> Tuple[int, int]:
"""Computes the window and number of windows based on given time stamps.
Args:
start_stamp: int, starting time stamp
end_stamp: int, ending time stamp
window_hours: int, window in hours
window_minutes: int, window in minutes
split_day_night: bool, True if split day and night
Returns:
A tuple of two integers (window, num_windows):
window: int, window in seconds
num_windows: int, number of windows
"""

window = window_hours * 60 * 60
window = window_minutes * 60
num_windows = (end_stamp - start_stamp) // window
if split_day_night:
num_windows *= 2
Expand Down Expand Up @@ -1596,7 +1596,13 @@ def gps_stats_main(
as pickle files for future use
and a record csv file to show which users are processed
and logger csv file to show warnings and bugs during the run
Raises:
ValueError: Frequency is not valid
"""
# no minutely analysis on GPS data
if frequency == Frequency.MINUTELY:
raise ValueError("Frequency cannot be minutely.")

if parameters is None:
parameters = Hyperparameters()

Expand Down
Loading

0 comments on commit abab9ad

Please sign in to comment.