Skip to content

Commit 5805010

Browse files
committed
fix linting
1 parent 74171b9 commit 5805010

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

pymove/query/query.py

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33
44
range_query,
55
knn_query,
6-
query_all_points_by_range,
6+
query_all_points_by_range,
77
88
"""
99

10+
from datetime import timedelta
1011
from typing import Optional, Text
1112

1213
import numpy as np
1314
import pandas as pd
15+
from IPython.display import clear_output
1416
from pandas import DataFrame
1517

1618
from pymove.utils import distances
1719
from pymove.utils.constants import DATETIME, LATITUDE, LONGITUDE, MEDP, MEDT, TRAJ_ID
1820
from pymove.utils.log import logger, progress_bar
1921

20-
from datetime import timedelta
21-
from IPython.display import clear_output
22-
2322

2423
def range_query(
2524
traj: DataFrame,
@@ -185,10 +184,12 @@ def dist_measure(traj, this, latitude, longitude, datetime):
185184

186185
def _datetime_filter(row, move_df, minimum_distance):
187186
"""
187+
Returns all the points of the DataFrame which are in a temporal distance.
188+
188189
Given a row referencing to a point, a DataFrame with
189190
multiple points and a minimum distance, it returns
190191
all the points of the DataFrame which are in a temporal
191-
distance equal or smaller than the minimum distance
192+
distance equal or smaller than the minimum distance
192193
parameter.
193194
194195
Parameters
@@ -207,25 +208,30 @@ def _datetime_filter(row, move_df, minimum_distance):
207208
a temporal distance equal or smaller than the minimum
208209
distance parameter.
209210
"""
210-
datetime = row['datetime'];
211-
move_df['temporal_distance'] = (move_df['datetime'] - datetime).abs();
212-
filtered = move_df[(move_df['temporal_distance'] < minimum_distance) & (move_df['temporal_distance'] > -minimum_distance)];
213-
211+
datetime = row['datetime']
212+
move_df['temporal_distance'] = (move_df['datetime'] - datetime).abs()
213+
filtered = move_df[
214+
(move_df['temporal_distance'] < minimum_distance)
215+
& (move_df['temporal_distance'] > -minimum_distance)
216+
]
217+
214218
if (filtered.shape[0] > 0):
215219
filtered['target_id'] = row['id']
216220
filtered['target_lat'] = row['lat']
217221
filtered['target_lon'] = row['lon']
218222
filtered['target_datetime'] = row['datetime']
219-
223+
220224
return filtered
221225

222226

223227
def _meters_filter(row, move_df, minimum_distance):
224228
"""
229+
Returns all the points of the DataFrame which are in a spatial distance.
230+
225231
Given a row referencing to a point, a DataFrame with
226232
multiple points and a minimum distance, it returns
227233
all the points of the DataFrame which are in a spatial
228-
distance (in meters) equal or smaller than the minimum distance
234+
distance (in meters) equal or smaller than the minimum distance
229235
parameter.
230236
231237
Parameters
@@ -236,31 +242,42 @@ def _meters_filter(row, move_df, minimum_distance):
236242
The input trajectory data.
237243
minimum_distance: float
238244
the minimum spatial distance between the points in meters.
245+
239246
Returns
240-
-------
247+
-------
241248
DataFrame
242249
dataframe with all the points of move_df which are in
243250
a spatial distance equal or smaller than the minimum
244251
distance parameter.
245252
"""
246253
lat = row['lat']
247254
lon = row['lon']
248-
move_df['spatial_distance'] = distances.euclidean_distance_in_meters(lat1=lat, lon1=lon, lat2=move_df['lat'], lon2=move_df['lon'])
255+
move_df['spatial_distance'] = distances.euclidean_distance_in_meters(
256+
lat1=lat, lon1=lon, lat2=move_df['lat'], lon2=move_df['lon']
257+
)
249258
filtered = move_df[move_df['spatial_distance'] < minimum_distance]
250-
259+
251260
if (filtered.shape[0] > 0):
252261
filtered['target_id'] = row['id']
253262
filtered['target_lat'] = row['lat']
254263
filtered['target_lon'] = row['lon']
255264
filtered['target_datetime'] = row['datetime']
256-
265+
257266
return filtered
258267

259268

260-
def query_all_points_by_range(traj1, move_df, minimum_meters=100, minimum_time=timedelta(minutes=2), datetime_label=DATETIME):
269+
def query_all_points_by_range(
270+
traj1,
271+
move_df,
272+
minimum_meters=100,
273+
minimum_time=None,
274+
datetime_label=DATETIME
275+
):
261276
"""
262-
Selects only the points between two Move Dataframes
263-
that have the closest point within a spatial range
277+
Queries closest point within a spatial range based on meters and a temporal range.
278+
279+
Selects only the points between two Move Dataframes
280+
that have the closest point within a spatial range
264281
based on meters and a temporal range.
265282
266283
Parameters
@@ -283,21 +300,24 @@ def query_all_points_by_range(traj1, move_df, minimum_meters=100, minimum_time=t
283300
a spatial distance and temporal distance equal or smaller
284301
than the minimum distance parameters.
285302
"""
286-
result = pd.DataFrame([]);
303+
if minimum_time is None:
304+
minimum_time = timedelta(minutes=2)
305+
306+
result = pd.DataFrame([])
287307
total = traj1.shape[0]
288308
count = 0
289-
for index, row in traj1.iterrows():
309+
for _, row in traj1.iterrows():
290310
clear_output(wait=True)
291-
print("{} de {}".format(count, total))
292-
print("{:.2f}%".format((count*100/total)))
311+
print('{} de {}'.format(count, total))
312+
print('{:.2f}%'.format((count * 100 / total)))
293313
coinc_points = _meters_filter(row, move_df, minimum_meters)
294314
coinc_points = _datetime_filter(row, coinc_points, minimum_time)
295315
result = coinc_points.append(result)
296-
316+
297317
count += 1
298-
318+
299319
clear_output(wait=True)
300-
print("{} de {}".format(count, total))
301-
print("{:.2f}%".format((count*100/total)))
320+
print('{} de {}'.format(count, total))
321+
print('{:.2f}%'.format((count * 100 / total)))
302322

303323
return result

pymove/tests/test_query.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from pandas import DataFrame, Timestamp, Timedelta
1+
from datetime import timedelta
2+
3+
from pandas import DataFrame, Timedelta, Timestamp
24
from pandas.testing import assert_frame_equal
35

46
from pymove import MoveDataFrame
57
from pymove.query import query
68
from pymove.utils.constants import DATETIME, LATITUDE, LONGITUDE, TRAJ_ID
79

8-
from datetime import timedelta
9-
1010
traj_example = [[16.4, -54.9, Timestamp('2014-10-11 18:00:00'),
1111
' GONZALO'],
1212
[16.4, -55.9, Timestamp('2014-10-12 00:00:00'),
@@ -527,7 +527,7 @@ def test_knn_query():
527527
medt_move_df = query.knn_query(traj_df, move_df, k=2, distance='MEDT')
528528
assert_frame_equal(medt_move_df, expected_medt)
529529

530-
def test__datetime_filter():
530+
def test__datetime_filter():
531531
traj_df = _default_traj_df()
532532
firstpoint = traj_df.iloc[0]
533533
move_df = _default_move_df()
@@ -543,7 +543,7 @@ def test__datetime_filter():
543543
assert_frame_equal(result, expected)
544544

545545

546-
def test__meters_filter():
546+
def test__meters_filter():
547547
traj_df = _default_traj_df()
548548
firstpoint = traj_df.iloc[0]
549549
move_df = _default_move_df()
@@ -571,6 +571,3 @@ def test_query_all_points_by_range():
571571

572572
result = query.query_all_points_by_range(traj_df, move_df, minimum_meters=1900000, minimum_time=timedelta(hours=19000))
573573
assert_frame_equal(result, expected)
574-
575-
576-

0 commit comments

Comments
 (0)