Skip to content

Commit

Permalink
Merge pull request #1045 from dstl/deprecated
Browse files Browse the repository at this point in the history
Minor deprecation fixes
  • Loading branch information
sdhiscocks authored Jun 19, 2024
2 parents d557bef + c6503fe commit 35ec8fd
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 25 deletions.
5 changes: 3 additions & 2 deletions docs/examples/readers/Custom_Pandas_Dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

from typing import Sequence, Collection

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from dateutil.parser import parse


Expand Down Expand Up @@ -84,7 +84,8 @@ def _get_time(self, row):
time_field_value = datetime.strptime(row[self.time_field], self.time_field_format)
elif self.timestamp:
fractional, timestamp = modf(float(row[self.time_field]))
time_field_value = datetime.utcfromtimestamp(int(timestamp))
time_field_value = datetime.fromtimestamp(
int(timestamp), timezone.utc).replace(tzinfo=None)
time_field_value += timedelta(microseconds=fractional * 1E6)
else:
time_field_value = row[self.time_field]
Expand Down
6 changes: 3 additions & 3 deletions stonesoup/measures/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,10 @@ def __call__(self, state1, state2):
inv_state2_covar = np.linalg.inv(state2.covar)
trace_term = np.trace(inv_state2_covar@state1.covar)

delta = state2.state_vector - state1.state_vector
mahalanobis_term = delta.T @ inv_state2_covar @ delta
delta = (state2.state_vector - state1.state_vector).ravel()
mahalanobis_term = np.dot(np.dot(delta, inv_state2_covar), delta)

kld = float(0.5*(log_term - n_dims + trace_term + mahalanobis_term))
kld = 0.5*(log_term - n_dims + trace_term + mahalanobis_term)

else:
raise ValueError(f'The state dimensions are not compatible '
Expand Down
4 changes: 2 additions & 2 deletions stonesoup/movable/movable.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ def _get_angle(vec: StateVector, axis: np.ndarray) -> float:
Angle : float
Angle, in radians, between the two vectors
"""
vel_norm = vec / np.linalg.norm(vec)
axis_norm = axis / np.linalg.norm(axis)
vel_norm = (vec / np.linalg.norm(vec)).ravel()
axis_norm = (axis / np.linalg.norm(axis)).ravel()

return np.arccos(np.clip(np.dot(axis_norm, vel_norm), -1.0, 1.0))

Expand Down
6 changes: 3 additions & 3 deletions stonesoup/reader/aishub.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from datetime import datetime
from datetime import datetime, timezone

import numpy as np

Expand Down Expand Up @@ -59,8 +59,8 @@ def detections_gen(self):
lon_value = float(record['LONGITUDE'])/600000

# extract timestamp values from JSON record
time_value = datetime.utcfromtimestamp(float(
record['TIME']))
time_value = datetime.fromtimestamp(float(
record['TIME']), timezone.utc).replace(tzinfo=None)

# delete lat, lon, timestamp from JSON record;
# the rest is metadata
Expand Down
5 changes: 3 additions & 2 deletions stonesoup/reader/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import csv
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Sequence, Collection, Mapping

from math import modf
Expand Down Expand Up @@ -52,7 +52,8 @@ def _get_time(self, row):
time_field_value = datetime.strptime(row[self.time_field], self.time_field_format)
elif self.timestamp is True:
fractional, timestamp = modf(float(row[self.time_field]))
time_field_value = datetime.utcfromtimestamp(int(timestamp))
time_field_value = datetime.fromtimestamp(
int(timestamp), timezone.utc).replace(tzinfo=None)
time_field_value += timedelta(microseconds=fractional * 1E6)
else:
time_field_value = parse(row[self.time_field], ignoretz=True)
Expand Down
5 changes: 3 additions & 2 deletions stonesoup/reader/hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<https://docs.h5py.org/>`_ library.
"""

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Collection, Sequence

try:
Expand Down Expand Up @@ -122,7 +122,8 @@ def _get_time(self, raw_time_val):
if self.time_field_format is not None:
time_field_value = datetime.strptime(raw_time_val, self.time_field_format)
elif self.timestamp is True:
time_field_value = datetime.utcfromtimestamp(raw_time_val)
time_field_value = datetime.fromtimestamp(
raw_time_val, timezone.utc).replace(tzinfo=None)
else:
time_field_value = parse(raw_time_val, ignoretz=True)

Expand Down
5 changes: 3 additions & 2 deletions stonesoup/reader/kafka.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import sys
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from math import modf
from queue import Empty, Queue
from threading import Thread
Expand Down Expand Up @@ -83,7 +83,8 @@ def _get_time(self, data):
)
elif self.timestamp is True:
fractional, timestamp = modf(float(data[self.time_field]))
time_field_value = datetime.utcfromtimestamp(int(timestamp))
time_field_value = datetime.fromtimestamp(
int(timestamp), timezone.utc).replace(tzinfo=None)
time_field_value += timedelta(microseconds=fractional * 1e6)
else:
time_field_value = parse(data[self.time_field], ignoretz=True)
Expand Down
9 changes: 6 additions & 3 deletions stonesoup/reader/opensky.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def data_gen(self):
# Must have position (lon, lat, geo-alt)
if not all(state[index] for index in (5, 6, 13)):
continue
timestamp = datetime.datetime.utcfromtimestamp(state[3])
timestamp = datetime.datetime.fromtimestamp(
state[3], datetime.timezone.utc).replace(tzinfo=None)
# Skip old detections
if time is not None and timestamp <= time:
continue
Expand All @@ -100,10 +101,12 @@ def data_gen(self):
'source': self.sources[state[16]],
}
))
time = datetime.datetime.utcfromtimestamp(data['time'])
time = datetime.datetime.fromtimestamp(
data['time'], datetime.timezone.utc).replace(tzinfo=None)
yield time, states_and_metadata

while time + self.timestep > datetime.datetime.utcnow():
while time + self.timestep > datetime.datetime.now(
datetime.timezone.utc).replace(tzinfo=None):
sleep(0.1)


Expand Down
5 changes: 3 additions & 2 deletions stonesoup/reader/pandas_reader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from dateutil.parser import parse
from math import modf
from typing import Sequence, Collection
Expand Down Expand Up @@ -47,7 +47,8 @@ def _get_time(self, row):
time_field_value = datetime.strptime(row[self.time_field], self.time_field_format)
elif self.timestamp:
fractional, timestamp = modf(float(row[self.time_field]))
time_field_value = datetime.utcfromtimestamp(int(timestamp))
time_field_value = datetime.fromtimestamp(
int(timestamp), timezone.utc).replace(tzinfo=None)
time_field_value += timedelta(microseconds=fractional * 1E6)
else:
time_field_value = row[self.time_field]
Expand Down
4 changes: 2 additions & 2 deletions stonesoup/tests/test_stitcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def params():
np.random.uniform(-2, 2, 1)]*n_spacial_dimensions

for i in range(number_of_targets):
number_of_segments = int(np.random.choice(range(1, max_segments), 1))
truthlet0_length = np.random.choice(range(max_track_start), 1)
number_of_segments = np.random.choice(range(1, max_segments))
truthlet0_length = np.random.choice(range(max_track_start))
truthlet_lengths = np.random.choice(range(min_segment_length, max_segment_length),
number_of_segments)
disjoint_lengths = np.random.choice(range(min_disjoint_length, max_disjoint_length),
Expand Down
4 changes: 2 additions & 2 deletions stonesoup/types/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def __new__(cls, *args, **kwargs):
array = np.asarray(*args, **kwargs)
return array.view(cls)

def __array_wrap__(self, array):
return self._cast(array)
def __array_wrap__(self, array, context=None, return_scalar=False):
return array[()] if return_scalar else self._cast(array)

@classmethod
def _cast(cls, val):
Expand Down

0 comments on commit 35ec8fd

Please sign in to comment.