In [5]:
from __future__ import division
import datetime as dt
from collections import OrderedDict
import sys, os
import dateutil.relativedelta as rd
import json
from pathlib import Path

import utm
import pandas as pd
import numpy as np
import shapely.geometry as sg

DIR = Path('..')
sys.path.append(str(DIR))

import gtfstk as gt

%load_ext autoreload
%autoreload 2

DATA_DIR = DIR/'data'


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

In [6]:
#path = DATA_DIR/'cairns_gtfs.zip'
path = "../../Desktop/IDFM_gtfs.zip"
feed = gt.read_gtfs(path, dist_units='km')

In [7]:
route_ids = feed.routes.loc[lambda x: x.agency_id == "372", "route_id"]
display(route_ids)
little_feed = feed.restrict_to_routes(route_ids=route_ids)


0     067167003:E
1     067067003:F
2     067167006:G
3     067167008:D
4     067167002:A
5     067167001:B
6     067067005:C
7     067167004:H
8     067067020:M
9     067167010:N
10    067167005:O
11    067067017:L
12    067167009:I
13    067067002:J
14    067067064:K
Name: route_id, dtype: object

In [8]:
vv = little_feed.validate()
vv


Out[8]:
type message table rows
0 warning Unrecognized column stop_time_desc stop_times []
1 warning Repeated pair (trip_id, departure_time) stop_times [17468, 17478, 17488, 17608, 17618, 17628, 176...
2 warning Unrecognized column trip_desc trips []

In [ ]:
freq = "8H"
f = gt.downsample(rts, freq=freq)
display(f)

In [ ]:
(
    rts
    .pipe(gt.unstack_time_series)
    .merge(feed.routes.filter(["route_id", "route_type"]), how="left")
    .groupby(["datetime", "indicator", "route_type"])
    .agg({"value": lambda x: x.sum(skipna=True, min_count=1)})  # Preserve NaNs
    .reset_index("datetime")
#     .fillna(-1)
#     .pivot_table(
#         index=["datetime"], columns=["indicator", "route_type"]
#     )
#     .value
#     .replace({-1: np.nan})
)

In [ ]:
# Slicing 
display(fts.loc[:, ("num_trips", slice(None))])
display(fts.xs("num_trips", axis="columns"))

In [ ]:
f = fts = feed.compute_feed_time_series(trip_stats, dates[:1], freq="12H", split_route_types=False)
display(f)
g = gt.unstack_time_series(f)

# columns = [c for c in g.columns if c not in ["datetime", "value"]]
# h = g.pivot_table(index="datetime", columns=columns).value.sort_index(
#     axis="columns"
# )

# hours = (h.index[1] - h.index[0]).components.hours
# if hours != 0:
#     freqy = f"{hours}H"
# else:
#     freqy = "D"

# print(freqy)

h = gt.restack_time_series(g)
h

In [ ]:
f.index

In [ ]:
delta.components.hours

In [ ]:
feed1 = feed.copy()
cal = feed1.calendar.copy()
cal["monday"] = 0
feed1.calendar = cal
feed1.compute_feed_time_series(trip_stats, dates, freq="12H")

In [ ]:
s1 = pd.Series({"hello": 12, "goodbye": 13})
s2 = s1.copy()
pd.DataFrame([s1, s2])

In [ ]:
feed.compute_stop_stats(['20140601', '20140603'])

In [ ]:
trip_stats = feed.compute_trip_stats()
feed = feed.append_dist_to_stop_times(trip_stats)

# Load screen line
with (DATA_DIR/'cairns_screen_line.geojson').open() as src:
    line = json.load(src)
    line = sg.shape(line['features'][0]['geometry'])

In [ ]:
f = feed.compute_screen_line_counts(line, dates[:7])
f[f['crossing_time'] < '06:00:00']

In [ ]:
feed.compute_feed_time_series(ts, dates[-2:], freq='12H')

In [ ]:
sd = False
#%time p1 = proto1(feed, ts, dates[:14], split_directions=sd)
%time p2 = proto2(feed, [dates[0], '20010101'], split_directions=sd, freq='12H')
p2

In [ ]:
feed.compute_feed_stats(ts, dates[0])

In [ ]:
feed.describe()

In [ ]:
feed.summarize()

In [ ]: