Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 21 additions & 32 deletions .github/workflows/lint_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,26 @@ name: Lint and Test
on: [push]

jobs:
lint:
name: Code Linting
lint-test:
name: Lint and Test
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9']
steps:
- uses: actions/checkout@main
- name: Set up Python 3.7
uses: actions/setup-python@main
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install --upgrade pip
make dev
- name: Lint
working-directory: ${{ github.workspace }}
run: |
make lint
test:
name: Code Testing
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- name: Set up Python 3.7
uses: actions/setup-python@main
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install --upgrade pip
make dev
- name: Test
working-directory: ${{ github.workspace }}
run: |
make test
- uses: actions/checkout@main
- uses: actions/setup-python@main
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
make dev
- name: Check code style
working-directory: ${{ github.workspace }}
run: |
make lint
- name: Runs unit tests
working-directory: ${{ github.workspace }}
run: |
make test
19 changes: 10 additions & 9 deletions pymove/core/dask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""DaskMoveDataFrame class."""
from __future__ import annotations

from typing import TYPE_CHECKING, Dict, List, Text, Union
from typing import TYPE_CHECKING

import dask
import numpy as np
Expand All @@ -27,11 +28,11 @@ class DaskMoveDataFrame(DataFrame, MoveDataFrameAbstractModel):

def __init__(
self,
data: Union[DataFrame, List, Dict],
latitude: Text = LATITUDE,
longitude: Text = LONGITUDE,
datetime: Text = DATETIME,
traj_id: Text = TRAJ_ID,
data: DataFrame | list | dict,
latitude: str = LATITUDE,
longitude: str = LONGITUDE,
datetime: str = DATETIME,
traj_id: str = TRAJ_ID,
n_partitions: int = 1,
):
"""
Expand Down Expand Up @@ -501,8 +502,8 @@ def to_csv(self, *args, **kwargs):
raise NotImplementedError('To be implemented')

def convert_to(
self, new_type: Text
) -> Union[MoveDataFrame, 'PandasMoveDataFrame', 'DaskMoveDataFrame']:
self, new_type: str
) -> MoveDataFrame | 'PandasMoveDataFrame' | 'DaskMoveDataFrame':
"""
Convert an object from one type to another specified by the user.

Expand Down Expand Up @@ -530,7 +531,7 @@ def convert_to(
else:
return self

def get_type(self) -> Text:
def get_type(self) -> str:
"""
Returns the type of the object.

Expand Down
19 changes: 9 additions & 10 deletions pymove/core/dataframe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""MoveDataFrame class."""

from typing import Dict, List, Text, Union
from __future__ import annotations

from dateutil.parser._parser import ParserError
from pandas.core.frame import DataFrame
Expand All @@ -21,12 +20,12 @@ class MoveDataFrame:
@staticmethod
def __new__(
self,
data: Union[DataFrame, Dict, List],
latitude: Text = LATITUDE,
longitude: Text = LONGITUDE,
datetime: Text = DATETIME,
traj_id: Text = TRAJ_ID,
type_: Text = TYPE_PANDAS,
data: DataFrame | dict | list,
latitude: str = LATITUDE,
longitude: str = LONGITUDE,
datetime: str = DATETIME,
traj_id: str = TRAJ_ID,
type_: str = TYPE_PANDAS,
n_partitions: int = 1,
):
"""
Expand Down Expand Up @@ -125,8 +124,8 @@ def validate_move_data_frame(data: DataFrame):

@staticmethod
def format_labels(
current_id: Text, current_lat: Text, current_lon: Text, current_datetime: Text
) -> Dict:
current_id: str, current_lat: str, current_lon: str, current_datetime: str
) -> dict:
"""
Format the labels for the PyMove lib pattern labels output lat, lon and datatime.

Expand Down
100 changes: 15 additions & 85 deletions pymove/core/grid.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Grid class."""
from __future__ import annotations

import math
from typing import Callable, Dict, Optional, Text, Tuple, Union
from typing import Callable

import joblib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import figure
from pandas import DataFrame
from shapely.geometry import Polygon

Expand All @@ -17,7 +16,6 @@
INDEX_GRID_LON,
LATITUDE,
LONGITUDE,
POLYGON,
TRAJ_ID,
)
from pymove.utils.conversions import lat_meters
Expand All @@ -30,9 +28,9 @@ class Grid:

def __init__(
self,
data: Union[DataFrame, Dict],
cell_size: Optional[float] = None,
meters_by_degree: Optional[float] = None
data: DataFrame | dict,
cell_size: float | None = None,
meters_by_degree: float | None = None
):
"""
Creates a virtual grid from the trajectories.
Expand All @@ -58,7 +56,7 @@ def __init__(
ValueError
If one of data or cell grid is not provided
"""
self.last_operation: Dict = dict()
self.last_operation: dict = dict()
if meters_by_degree is None:
meters_by_degree = lat_meters(-3.71839)
if isinstance(data, dict):
Expand All @@ -69,7 +67,7 @@ def __init__(
raise ValueError('Must pass either data or cell size.')
self.grid_polygon = None

def get_grid(self) -> Dict:
def get_grid(self) -> dict:
"""
Returns the grid object in a dict format.

Expand All @@ -91,7 +89,7 @@ def get_grid(self) -> Dict:
'cell_size_by_degree': self.cell_size_by_degree,
}

def _grid_from_dict(self, dict_grid: Dict):
def _grid_from_dict(self, dict_grid: dict):
"""
Coverts the dict grid to a Grid object.

Expand Down Expand Up @@ -218,8 +216,8 @@ def create_update_index_grid_feature(
def convert_two_index_grid_to_one(
self,
data: DataFrame,
label_grid_lat: Text = INDEX_GRID_LAT,
label_grid_lon: Text = INDEX_GRID_LON,
label_grid_lat: str = INDEX_GRID_LAT,
label_grid_lon: str = INDEX_GRID_LON,
):
"""
Converts grid lat-lon ids to unique values.
Expand All @@ -241,7 +239,7 @@ def convert_two_index_grid_to_one(
def convert_one_index_grid_to_two(
self,
data: DataFrame,
label_grid_index: Text = INDEX_GRID,
label_grid_index: str = INDEX_GRID,
):
"""
Converts grid lat-lon ids to unique values.
Expand Down Expand Up @@ -360,7 +358,7 @@ def create_all_polygons_to_all_point_on_grid(
self.last_operation = end_operation(operation)
return datapolygons

def point_to_index_grid(self, event_lat: float, event_lon: float) -> Tuple[int, int]:
def point_to_index_grid(self, event_lat: float, event_lon: float) -> tuple[int, int]:
"""
Locate the coordinates x and y in a grid of point (lat, long).

Expand Down Expand Up @@ -394,7 +392,7 @@ def point_to_index_grid(self, event_lat: float, event_lon: float) -> Tuple[int,

return indexes_lat_y, indexes_lon_x

def save_grid_pkl(self, filename: Text):
def save_grid_pkl(self, filename: str):
"""
Save a grid with new file .pkl.

Expand All @@ -409,7 +407,7 @@ def save_grid_pkl(self, filename: Text):
joblib.dump(self.get_grid(), f)
self.last_operation = end_operation(operation)

def read_grid_pkl(self, filename: Text) -> 'Grid':
def read_grid_pkl(self, filename: str) -> 'Grid':
"""
Read grid dict from a file .pkl.

Expand All @@ -431,74 +429,6 @@ def read_grid_pkl(self, filename: Text) -> 'Grid':
self.last_operation = end_operation(operation)
return grid

def show_grid_polygons(
self,
data: DataFrame,
markersize: float = 10,
linewidth: float = 2,
figsize: Tuple[int, int] = (10, 10),
return_fig: bool = True,
save_fig: bool = False,
name: Text = 'grid.png',
) -> Optional[figure]:
"""
Generate a visualization with grid polygons.

Parameters
----------
data : DataFrame
Input trajectory data
markersize : float, optional
Represents visualization size marker, by default 10
linewidth : float, optional
Represents visualization size line, by default 2
figsize : tuple(int, int), optional
Represents the size (float: width, float: height) of a figure,
by default (10, 10)
return_fig : bool, optional
Represents whether or not to save the generated picture, by default True
save_fig : bool, optional
Wether to save the figure, by default False
name : str, optional
Represents name of a file, by default 'grid.png'

Returns
-------
figure
The generated picture or None

Raises
------
If the dataframe does not contains the POLYGON feature
IndexError
If there is no user with the id passed

"""
if POLYGON not in data:
raise KeyError('POLYGON feature not in dataframe')

data.dropna(subset=[POLYGON], inplace=True)

operation = begin_operation('show_grid_polygons')

fig = plt.figure(figsize=figsize)

for _, row in data.iterrows():
xs, ys = row[POLYGON].exterior.xy
plt.plot(ys, xs, 'g', linewidth=linewidth, markersize=markersize)
xs_start, ys_start = data.iloc[0][POLYGON].exterior.xy
xs_end, ys_end = data.iloc[-1][POLYGON].exterior.xy
plt.plot(ys_start, xs_start, 'bo', markersize=markersize * 1.5)
plt.plot(ys_end, xs_end, 'bX', markersize=markersize * 1.5) # start point

if save_fig:
plt.savefig(fname=name)

self.last_operation = end_operation(operation)

if return_fig:
return fig

def __repr__(self) -> str:
"""
String representation of grid.
Expand All @@ -512,5 +442,5 @@ def __repr__(self) -> str:
grid_size_lon_x: grid longitude size
cell_size_by_degree: grid cell size
"""
text = ['{}: {}'.format(k, v) for k, v in self.get_grid().items()]
text = [f'{k}: {v}' for k, v in self.get_grid().items()]
return '\n'.join(text)
Loading