Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add in geosynch #10

Merged
merged 1 commit into from
May 23, 2024
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ Geotime enables various ways of displaying and structuring geo-spatial-temporal

The methods currently supported are:
* time hexes H3 Geohashes with second dimension time (Original Niemeyer can also be used)
* chron-nets (https://www.nature.com/articles/s41467-020-17634-2)
* chron-net (https://www.nature.com/articles/s41467-020-17634-2)
* geotimehash (https://isprs-annals.copernicus.org/articles/IV-4-W2/31/2017/isprs-annals-IV-4-W2-31-2017.pdf)
* geosynchnet (geographic synchronous networks)


#### Basic Functionality
Expand All @@ -47,6 +48,9 @@ time_delta= dt.timedelta(hours=1), hash_func= hasher.hash_collection, self_loop
geotimehash_output = convert_geotimehash(fcol=Feature_Collection_of_time_shapes, precision = 8,
hash_func= hasher.hash_collection)

geosynchnet_output = convert_geosynchnet(fcol=Feature_Collection_of_time_shapes,
time_delta= dt.timedelta(hours=1), hash_func= hasher.hash_collection)

```


Expand Down
4 changes: 3 additions & 1 deletion geochron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from geochron.chronnet import convert_chronnet
from geochron.timehex import convert_timehex
from geochron.geotimehash import convert_geotimehash
from geochron.geosynchnet import convert_geosynchnet

ConditionalPackageInterceptor.permit_packages(
{
Expand All @@ -19,5 +20,6 @@
__all__ = [
'convert_chronnet',
'convert_timehex',
'convert_geotimehash'
'convert_geotimehash',
'convert_geosynchnet'
]
Binary file modified geochron/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified geochron/__pycache__/chronnet.cpython-39.pyc
Binary file not shown.
Binary file added geochron/__pycache__/geosynchnet.cpython-39.pyc
Binary file not shown.
Binary file modified geochron/__pycache__/geotimehash.cpython-39.pyc
Binary file not shown.
6 changes: 4 additions & 2 deletions geochron/chronnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from datetime import timedelta
import itertools
from typing import Callable, List
import networkx as nx # type: ignore
import numpy as np
import pandas as pd

Expand Down Expand Up @@ -49,7 +48,8 @@ def hash_tracks_into_netdf(track_list: List, timestamps: List, hash_func: Callab
def chronnet_create(df: pd.DataFrame, self_loops: bool, mode= str):
"""
Converts a properly formatted pandas dataframe with the columns
of cell and time to a networkx network.
of cell and time to a networkx network where nodes are locations
and edges are formed between nodes with consecutive times.

Args:
df: a pandas dataframe withe two columns cell and time
Expand All @@ -61,6 +61,8 @@ def chronnet_create(df: pd.DataFrame, self_loops: bool, mode= str):
Returns:
A networkx network
"""
# pylint: disable=import-outside-toplevel
import networkx as nx # type: ignore
time_seq = sorted(np.unique(df['time']))
if len(time_seq) < 2: # pragma: no cover
print("The total time interval in the dataset should be larger than two.")
Expand Down
64 changes: 64 additions & 0 deletions geochron/geosynchnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
""" Representation as geosynchnet"""
from datetime import timedelta
from typing import Callable
import pandas as pd

from geostructures.collections import FeatureCollection,Track
from geochron.chronnet import hash_tracks_into_netdf
from geochron.time_slicing import get_timestamp_intervals, time_slice_track

def geosynchnet_create(df: pd.DataFrame):
"""
Converts a properly formatted pandas dataframe with the columns
of cell and time to a networkx network where nodes are locations
and edges are formed between nodes with that share a time.

Args:
df: a pandas dataframe withe two columns cell and time

Returns:
A networkx network
"""
# pylint: disable=import-outside-toplevel
import networkx as nx # type: ignore
# Initialize an empty network
net = nx.Graph()

# Group the dataframe by time
grouped = df.groupby('time')

# For each time group, add edges between all cells in the group
for name, group in grouped:
cells = group['cell'].tolist()
for i in range(len(cells)):
for j in range(i+1, len(cells)):
net.add_edge(cells[i], cells[j])

return net

def convert_geosynchnet(fcol: FeatureCollection, time_delta: timedelta,
hash_func: Callable):
"""
Converts a FeatureCollection into a chronnet with a specified time interval
using a specified hashing function

Args:
fcol: a FeatureCollection with time bound shapes

time_delta: the desired time interval

hash_func: the hashing function

Returns:
A networkx network
"""
track = Track(fcol.geoshapes)
timestamps = get_timestamp_intervals(track, time_delta)

track_list = time_slice_track(track, timestamps)

df = hash_tracks_into_netdf(track_list, timestamps, hash_func)

geosynchnet = geosynchnet_create(df)

return geosynchnet
3 changes: 2 additions & 1 deletion geochron/geotimehash.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Callable, List
from collections import Counter
from datetime import datetime, timedelta
import timehash
from geostructures.structures import GeoShape
from geostructures.time import TimeInterval
from geostructures import FeatureCollection, Track
Expand Down Expand Up @@ -85,6 +84,8 @@ def timehash_geoshape(geoshape: GeoShape, precision: int):
Returns:
A timehash list
"""
# pylint: disable=import-outside-toplevel
import timehash
timehash_list = []
assert isinstance(geoshape.dt, TimeInterval)
start_time = geoshape.dt.start
Expand Down
Binary file not shown.
Binary file modified tests/__pycache__/test_geotimehash.cpython-39-pytest-8.2.0.pyc
Binary file not shown.
42 changes: 42 additions & 0 deletions tests/test_geosynchnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import datetime as dt
import pandas as pd
from geochron.geosynchnet import geosynchnet_create, convert_geosynchnet
from geostructures import Coordinate, GeoPoint
from geostructures.collections import Track
from geostructures.geohash import H3Hasher

hasher = H3Hasher(resolution = 10)

def test_geosynchnet_create():
test_dict={'cell': {0: '8a194ad32167fff',
1: '8a194ad32b07fff',
2: '8a194ad3056ffff',
3: '8a194ad3078ffff',
4: '8a194ad3078ffff'},
'time': {0: '2020-01-01 08:05:00, 2020-01-01 09:05:00',
1: '2020-01-01 09:05:00, 2020-01-01 10:05:01',
2: '2020-01-01 09:05:00, 2020-01-01 10:05:01',
3: '2020-01-01 09:05:00, 2020-01-01 10:05:01',
4: '2020-01-01 10:05:01, 2020-01-01 11:05:01',}}

test_df = pd.DataFrame(test_dict)
test_network = geosynchnet_create(test_df)

assert list(test_network)[0] == '8a194ad32b07fff'
assert test_network.has_edge('8a194ad32b07fff', '8a194ad3056ffff') == True
assert test_network.has_edge('8a194ad32167fff', '8a194ad3078ffff') == False

def test_convert_geosynchnet():
track = Track(
[
GeoPoint(Coordinate(-0.104154, 51.511920), dt=dt.datetime(2020, 1, 1, 8, 5)),
GeoPoint(Coordinate(-0.096533, 51.511903), dt=dt.datetime(2020, 1, 1, 9, 23)),
GeoPoint(Coordinate(-0.083765, 51.514423), dt=dt.datetime(2020, 1, 1, 9, 44)),
GeoPoint(Coordinate(-0.087478, 51.508595), dt=dt.datetime(2020, 1, 1, 10, 5)),
]
)

test_geosynchnet = convert_geosynchnet(track, dt.timedelta(hours=1), hasher.hash_collection)

assert list(test_geosynchnet)[0] == '8a194ad32b07fff'
assert test_geosynchnet.has_edge('8a194ad32b07fff', '8a194ad3056ffff') == True
Loading