Skip to content

Commit

Permalink
Make time span parsing a util
Browse files Browse the repository at this point in the history
And add a test.
Fixes #76
  • Loading branch information
e-lo committed Sep 18, 2019
1 parent f44ff12 commit 19cedb8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
11 changes: 2 additions & 9 deletions network_wrangler/TransitNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from partridge.gtfs import Feed

from .Logger import WranglerLogger

from .Utils import parse_time_spans

class TransitNetwork(object):
"""
Expand Down Expand Up @@ -158,14 +158,7 @@ def select_transit_features(self, selection: dict) -> pd.Series:

# If a time key exists, filter trips using frequency table
if selection.get('time') is not None:
# If time is given without seconds, add 00
if len(selection['time'][0]) <= 5:
selection['time'] = [i + ':00' for i in selection['time']]

# Convert times to seconds from midnight (Partride's time storage)
for i, val in enumerate(selection['time']):
h, m, s = val.split(":")
selection['time'][i] = int(h) * 3600 + int(m) * 60 + int(s)
selection['time'] = parse_time_spans(selection['time'])

# Filter freq to trips in selection
freq = freq[freq.trip_id.isin(trips['trip_id'])]
Expand Down
41 changes: 40 additions & 1 deletion network_wrangler/Utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pandas as pd

from .Logger import WranglerLogger

def point_df_to_geojson(df: pd.DataFrame, properties: list):
"""
Expand Down Expand Up @@ -66,3 +66,42 @@ def make_slug(text, delimiter: str = "_"):

text = re.sub("[,.;@#?!&$']+", "", text.lower())
return re.sub("[\ ]+", delimiter, text)

def parse_time_spans(times):
"""
parse time spans into tuples of seconds from midnight
can also be used as an apply function for a pandas series
Parameters
-----------
times: tuple(string)
returns
--------
tuple(integer)
time span as seconds from midnight
"""
try:
start_time, end_time = times
except:
WranglerLogger.error(
"ERROR: times should be a tuple or list of two strings"
)
raise ValueError()

start_time=start_time.strip()
end_time=end_time.strip()

# If time is given without seconds, add 00
if len(start_time) <= 5:
start_time += ':00'
if len(end_time) <= 5:
end_time += ':00'

# Convert times to seconds from midnight (Partride's time storage)
h0, m0, s0 = start_time.split(":")
start_time_sec = int(h0) * 3600 + int(m0) * 60 + int(s0)

h1, m1, s1 = end_time.split(":")
end_time_sec = int(h1) * 3600 + int(m1) * 60 + int(s1)

return (start_time_sec, end_time_sec)
25 changes: 23 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import pytest



slug_test_list = [
{'text':"I am a roadway", 'delim':"_", "answer": "i_am_a_roadway"},
{'text':"I'm a roadway", 'delim':"_", "answer": "im_a_roadway"},
Expand All @@ -18,3 +16,26 @@ def test_get_slug(request, slug_test):
print('From: {} \nTo: {}'.format(slug_test['text'],slug))
print('Expected: {}'.format(slug_test['answer']))
assert(slug == slug_test['answer'])

@pytest.mark.travis
@pytest.mark.menow
def test_time_convert(request):
print("\n--Starting:", request.node.name)

time_tests = [
( ("00:00:00","00:00:10"), (0,10) ),
( ("0:00","0:10:00") , (0,600) ),
( ("01:02:03","01:02:23"), (3723,3743) ),
( ("1:02","1:02:13") , (3720, 3733) ),
( ("25:24:23","25:24:33"), (91463, 91473) ),
( ("250:24:23","250:24:25"), (901463,901465) ) ]

from pandas import DataFrame
df = DataFrame(time_tests, columns=['time','time_results'])
print("Original Time Series",df)

from network_wrangler.Utils import parse_time_spans
df["time"]= df["time"].apply(parse_time_spans)
print("Result Time Series",df)
from pandas.testing import assert_series_equal
assert_series_equal(df["time"], df["time_results"], check_names=False)

0 comments on commit 19cedb8

Please sign in to comment.