for stop, stop_profile in self._stop_profiles.items():<EOL><INDENT>assert (isinstance(stop_profile, NodeProfileMultiObjective))<EOL>neighbor_label_bags = []<EOL>walk_durations_to_neighbors = []<EOL>departure_arrival_stop_pairs = []<EOL>if stop_profile.get_walk_to_target_duration() != <NUM_LIT:0> and stop in self._walk_network.node:<EOL><INDENT>neighbors = networkx.all_neighbors(self._walk_network, stop)<EOL>for neighbor in neighbors:<EOL><INDENT>neighbor_profile = self._stop_profiles[neighbor]<EOL>assert (isinstance(neighbor_profile, NodeProfileMultiObjective))<EOL>neighbor_real_connection_labels = neighbor_profile.get_labels_for_real_connections()<EOL>neighbor_label_bags.append(neighbor_real_connection_labels)<EOL>walk_durations_to_neighbors.append(int(self._walk_network.get_edge_data(stop, neighbor)["<STR_LIT>"] /<EOL>self._walk_speed))<EOL>departure_arrival_stop_pairs.append((stop, neighbor))<EOL><DEDENT><DEDENT>stop_profile.finalize(neighbor_label_bags, walk_durations_to_neighbors, departure_arrival_stop_pairs)<EOL><DEDENT>

Deal with the first walks by joining profiles to other stops within walking distance.


assert self._has_run<EOL>return self._stop_profiles<EOL>

Returns
-------
_stop_profiles : dict[int, NodeProfileMultiObjective]
    The pareto tuples necessary.


to_select = []<EOL>where_clauses = []<EOL>to_select.append("<STR_LIT>")<EOL>to_select.append("<STR_LIT>")<EOL>if from_stop_I is not None:<EOL><INDENT>where_clauses.append("<STR_LIT>" + str(int(from_stop_I)))<EOL><DEDENT>if to_stop_I is not None:<EOL><INDENT>where_clauses.append("<STR_LIT>" + str(int(to_stop_I)))<EOL><DEDENT>where_clause = "<STR_LIT>"<EOL>if len(where_clauses) > <NUM_LIT:0>:<EOL><INDENT>where_clause = "<STR_LIT>" + "<STR_LIT>".join(where_clauses)<EOL><DEDENT>if not statistic:<EOL><INDENT>to_select.extend(["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"])<EOL><DEDENT>else:<EOL><INDENT>to_select.append(statistic)<EOL><DEDENT>to_select_clause = "<STR_LIT:U+002C>".join(to_select)<EOL>if not to_select_clause:<EOL><INDENT>to_select_clause = "<STR_LIT:*>"<EOL><DEDENT>sql = "<STR_LIT>" + to_select_clause + "<STR_LIT>" + travel_impedance_measure + where_clause + "<STR_LIT:;>"<EOL>df = pd.read_sql(sql, self.conn)<EOL>return df<EOL>

Recover pre-computed travel_impedance between od-pairs from the database.

Returns
-------
values: number | Pandas DataFrame


def insert_data(self, travel_impedance_measure_name, data):

f = float<EOL>data_tuple = [(int(x["<STR_LIT>"]), int(x["<STR_LIT>"]), f(x["<STR_LIT>"]), f(x["<STR_LIT>"]), f(x["<STR_LIT>"]), f(x["<STR_LIT>"])) for<EOL>x in data]<EOL>insert_stmt = '''<STR_LIT>''' + travel_impedance_measure_name + '''<STR_LIT>'''<EOL>self.conn.executemany(insert_stmt, data_tuple)<EOL>self.conn.commit()<EOL>

Parameters
----------
travel_impedance_measure_name: str
data: list[dict]
    Each list element must contain keys:
    "from_stop_I", "to_stop_I", "min", "max", "median" and "mean"


def __init__(self, transit_events, seed_stop, start_time,<EOL>end_time, transfer_margin, walk_network, walk_speed):

AbstractRoutingAlgorithm.__init__(self)<EOL>self._seed = seed_stop<EOL>self._connections = transit_events<EOL>self._start_time = start_time<EOL>self._end_time = end_time<EOL>self._transfer_margin = transfer_margin<EOL>self._walk_network = walk_network<EOL>self._walk_speed = walk_speed<EOL>self.__stop_labels = defaultdict(lambda: float('<STR_LIT>'))<EOL>self.__stop_labels[seed_stop] = start_time<EOL>self.__trip_reachable = defaultdict(lambda: False)<EOL>

Parameters
----------
transit_events: list[Connection]
seed_stop: int
    index of the seed node
start_time : int
    start time in unixtime seconds
end_time: int
    end time in unixtime seconds (no new connections will be scanned after this time)
transfer_margin: int
    required extra margin required for transfers in seconds
walk_speed: float
    walking speed between stops in meters / second
walk_network: networkx.Graph
    each edge should have the walking distance as a data attribute ("d_walk") expressed in meters


assert self._has_run<EOL>return self.__stop_labels<EOL>

Returns
-------
arrival_times: dict[int, float]
    maps integer stop_ids to floats


def _scan_footpaths(self, stop_id, walk_departure_time):

for _, neighbor, data in self._walk_network.edges_iter(nbunch=[stop_id], data=True):<EOL><INDENT>d_walk = data["<STR_LIT>"]<EOL>arrival_time = walk_departure_time + d_walk / self._walk_speed<EOL>self._update_stop_label(neighbor, arrival_time)<EOL><DEDENT>

Scan the footpaths originating from stop_id

Parameters
----------
stop_id: int


if dep_times is None:<EOL><INDENT>dep_times = []<EOL><DEDENT>n_dep_times = len(dep_times)<EOL>assert n_dep_times == len(set(dep_times)), "<STR_LIT>"<EOL>self._departure_times = list(reversed(sorted(dep_times)))<EOL>self.dep_times_to_index = dict(zip(self._departure_times, range(len(self._departure_times))))<EOL>self._label_bags = [[]] * len(self._departure_times)<EOL>self._walk_to_target_duration = walk_to_target_duration<EOL>self._min_dep_time = float('<STR_LIT>')<EOL>self.label_class = label_class<EOL>self.closest_target = closest_target<EOL>if self.label_class == LabelTimeBoardingsAndRoute and self._walk_to_target_duration < float('<STR_LIT>'):<EOL><INDENT>assert (self.closest_target is not None)<EOL><DEDENT>if transit_connection_dep_times is not None:<EOL><INDENT>self._connection_dep_times = transit_connection_dep_times<EOL><DEDENT>else:<EOL><INDENT>self._connection_dep_times = dep_times<EOL><DEDENT>assert (isinstance(self._connection_dep_times, (list, numpy.ndarray)))<EOL>self._closed = False<EOL>self._finalized = False<EOL>self._final_pareto_optimal_labels = None<EOL>self._real_connection_labels = None<EOL>self.node_id = node_id<EOL>

Parameters
----------
dep_times
walk_to_target_duration
label_class: label class to be used
transit_connection_dep_times:
    if not given, all connections are assumed to be real connections
closest_target: int, optional
    stop_I of the closest target if within walking distance (and Routes are recorded)


assert dep_time <= self._min_dep_time, "<STR_LIT>"<EOL>dep_time_index = self.dep_times_to_index[dep_time]<EOL>if self._min_dep_time < float('<STR_LIT>'):<EOL><INDENT>min_dep_index = self.dep_times_to_index[self._min_dep_time]<EOL>assert min_dep_index == dep_time_index or (min_dep_index == dep_time_index - <NUM_LIT:1>),"<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>assert dep_time_index is <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT>self._min_dep_time = dep_time<EOL>

A simple checker, that connections are coming in descending order of departure time
and that no departure time has been "skipped".

Parameters
----------
dep_time

Returns
-------
None


return self._walk_to_target_duration<EOL>

Get walking distance to target node.

Returns
-------
walk_to_target_duration: float


def update(self, new_labels, departure_time_backup=None):

if self._closed:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>try:<EOL><INDENT>departure_time = next(iter(new_labels)).departure_time<EOL><DEDENT>except StopIteration:<EOL><INDENT>departure_time = departure_time_backup<EOL><DEDENT>self._check_dep_time_is_valid(departure_time)<EOL>for new_label in new_labels:<EOL><INDENT>assert (new_label.departure_time == departure_time)<EOL><DEDENT>dep_time_index = self.dep_times_to_index[departure_time]<EOL>if dep_time_index > <NUM_LIT:0>:<EOL><INDENT>mod_prev_labels = [label.get_copy_with_specified_departure_time(departure_time) for label<EOL>in self._label_bags[dep_time_index - <NUM_LIT:1>]]<EOL><DEDENT>else:<EOL><INDENT>mod_prev_labels = list()<EOL><DEDENT>mod_prev_labels += self._label_bags[dep_time_index]<EOL>walk_label = self._get_label_to_target(departure_time)<EOL>if walk_label:<EOL><INDENT>new_labels = new_labels + [walk_label]<EOL><DEDENT>new_frontier = merge_pareto_frontiers(new_labels, mod_prev_labels)<EOL>self._label_bags[dep_time_index] = new_frontier<EOL>return True<EOL>

Update the profile with the new labels.
Each new label should have the same departure_time.

Parameters
----------
new_labels: list[LabelTime]

Returns
-------
added: bool
    whether new_pareto_tuple was added to the set of pareto-optimal tuples


def evaluate(self, dep_time, first_leg_can_be_walk=True, connection_arrival_time=None):

walk_labels = list()<EOL>if first_leg_can_be_walk and self._walk_to_target_duration != float('<STR_LIT>'):<EOL><INDENT>if connection_arrival_time is not None:<EOL><INDENT>walk_labels.append(self._get_label_to_target(connection_arrival_time))<EOL><DEDENT>else:<EOL><INDENT>walk_labels.append(self._get_label_to_target(dep_time))<EOL><DEDENT><DEDENT>if dep_time in self.dep_times_to_index:<EOL><INDENT>assert (dep_time != float('<STR_LIT>'))<EOL>index = self.dep_times_to_index[dep_time]<EOL>labels = self._label_bags[index]<EOL>pareto_optimal_labels = merge_pareto_frontiers(labels, walk_labels)<EOL><DEDENT>else:<EOL><INDENT>pareto_optimal_labels = walk_labels<EOL><DEDENT>if not first_leg_can_be_walk:<EOL><INDENT>pareto_optimal_labels = [label for label in pareto_optimal_labels if not label.first_leg_is_walk]<EOL><DEDENT>return pareto_optimal_labels<EOL>

Get the pareto_optimal set of Labels, given a departure time.

Parameters
----------
dep_time : float, int
    time in unix seconds
first_leg_can_be_walk : bool, optional
    whether to allow walking to target to be included into the profile
    (I.e. whether this function is called when scanning a pseudo-connection:
    "double" walks are not allowed.)
connection_arrival_time: float, int, optional
    used for computing the walking label if dep_time, i.e., connection.arrival_stop_next_departure_time, is infinity)
connection: connection object

Returns
-------
pareto_optimal_labels : set
    Set of Labels


assert self._finalized, "<STR_LIT>"<EOL>return self._final_pareto_optimal_labels<EOL>

Get pareto-optimal labels.

Returns
-------


def finalize(self, neighbor_label_bags=None, walk_durations=None, departure_arrival_stop_pairs=None):

assert (not self._finalized)<EOL>if self._final_pareto_optimal_labels is None:<EOL><INDENT>self._compute_real_connection_labels()<EOL><DEDENT>if neighbor_label_bags is not None:<EOL><INDENT>assert (len(walk_durations) == len(neighbor_label_bags))<EOL>self._compute_final_pareto_optimal_labels(neighbor_label_bags,<EOL>walk_durations,<EOL>departure_arrival_stop_pairs)<EOL><DEDENT>else:<EOL><INDENT>self._final_pareto_optimal_labels = self._real_connection_labels<EOL><DEDENT>self._finalized = True<EOL>self._closed = True<EOL>

Parameters
----------
neighbor_label_bags: list
    each list element is a list of labels corresponding to a neighboring node
     (note: only labels with first connection being a departure should be included)
walk_durations: list
departure_arrival_stop_pairs: list of tuples
Returns
-------
None


def __init__(self, profile_blocks, cutoff_distance=None, **kwargs):

for i, block in enumerate(profile_blocks[:-<NUM_LIT:1>]):<EOL><INDENT>assert block.start_time < block.end_time<EOL>assert block.end_time == profile_blocks[i + <NUM_LIT:1>].start_time<EOL>assert block.distance_start >= block.distance_end<EOL><DEDENT>self._profile_blocks = profile_blocks<EOL>self._start_time = profile_blocks[<NUM_LIT:0>].start_time<EOL>self._end_time = profile_blocks[-<NUM_LIT:1>].end_time<EOL>self._cutoff_distance = cutoff_distance<EOL>if cutoff_distance is not None:<EOL><INDENT>self._apply_cutoff(cutoff_distance)<EOL><DEDENT>self.from_stop_I = None<EOL>self.to_stop_I = None<EOL>for key, value in kwargs.items():<EOL><INDENT>if key == "<STR_LIT>":<EOL><INDENT>self.from_stop_I = value<EOL><DEDENT>if key == "<STR_LIT>":<EOL><INDENT>self.to_stop_I = value<EOL><DEDENT><DEDENT>

Parameters
----------
profile_blocks: list[gtfspy.routing.profile_block.ProfileBlock]


block_start_distances = [block.distance_start for block in self._profile_blocks if<EOL>block.distance_start < float('<STR_LIT>')]<EOL>block_end_distances = [block.distance_end for block in self._profile_blocks if<EOL>block.distance_end < float('<STR_LIT>')]<EOL>distances = block_start_distances + block_end_distances<EOL>if len(distances) > <NUM_LIT:0>:<EOL><INDENT>return max(distances)<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Compute the maximum temporal distance.

Returns
-------
max_temporal_distance : float


distance_split_points = set()<EOL>for block in self._profile_blocks:<EOL><INDENT>if block.distance_start != float('<STR_LIT>'):<EOL><INDENT>distance_split_points.add(block.distance_end)<EOL>distance_split_points.add(block.distance_start)<EOL><DEDENT><DEDENT>distance_split_points_ordered = numpy.array(sorted(list(distance_split_points)))<EOL>temporal_distance_split_widths = distance_split_points_ordered[<NUM_LIT:1>:] - distance_split_points_ordered[:-<NUM_LIT:1>]<EOL>trip_counts = numpy.zeros(len(temporal_distance_split_widths))<EOL>delta_peaks = defaultdict(lambda: <NUM_LIT:0>)<EOL>for block in self._profile_blocks:<EOL><INDENT>if block.distance_start == block.distance_end:<EOL><INDENT>delta_peaks[block.distance_end] += block.width()<EOL><DEDENT>else:<EOL><INDENT>start_index = numpy.searchsorted(distance_split_points_ordered, block.distance_end)<EOL>end_index = numpy.searchsorted(distance_split_points_ordered, block.distance_start)<EOL>trip_counts[start_index:end_index] += <NUM_LIT:1><EOL><DEDENT><DEDENT>unnormalized_cdf = numpy.array([<NUM_LIT:0>] + list(numpy.cumsum(temporal_distance_split_widths * trip_counts)))<EOL>if not (numpy.isclose(<EOL>[unnormalized_cdf[-<NUM_LIT:1>]],<EOL>[self._end_time - self._start_time - sum(delta_peaks.values())], atol=<NUM_LIT><EOL>).all()):<EOL><INDENT>print(unnormalized_cdf[-<NUM_LIT:1>], self._end_time - self._start_time - sum(delta_peaks.values()))<EOL>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>if len(delta_peaks) > <NUM_LIT:0>:<EOL><INDENT>for peak in delta_peaks.keys():<EOL><INDENT>if peak == float('<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>index = numpy.nonzero(distance_split_points_ordered == peak)[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>unnormalized_cdf = numpy.insert(unnormalized_cdf, index, unnormalized_cdf[index])<EOL>distance_split_points_ordered = numpy.insert(distance_split_points_ordered, index,<EOL>distance_split_points_ordered[index])<EOL>unnormalized_cdf[(index + <NUM_LIT:1>):] = unnormalized_cdf[(index + <NUM_LIT:1>):] + delta_peaks[peak]<EOL><DEDENT><DEDENT>norm_cdf = unnormalized_cdf / (unnormalized_cdf[-<NUM_LIT:1>] + delta_peaks[float('<STR_LIT>')])<EOL>return distance_split_points_ordered, norm_cdf<EOL>

Temporal distance cumulative density function.

Returns
-------
x_values: numpy.array
    values for the x-axis
cdf: numpy.array
    cdf values


temporal_distance_split_points_ordered, norm_cdf = self._temporal_distance_cdf()<EOL>delta_peak_loc_to_probability_mass = {}<EOL>non_delta_peak_split_points = [temporal_distance_split_points_ordered[<NUM_LIT:0>]]<EOL>non_delta_peak_densities = []<EOL>for i in range(<NUM_LIT:0>, len(temporal_distance_split_points_ordered) - <NUM_LIT:1>):<EOL><INDENT>left = temporal_distance_split_points_ordered[i]<EOL>right = temporal_distance_split_points_ordered[i + <NUM_LIT:1>]<EOL>width = right - left<EOL>prob_mass = norm_cdf[i + <NUM_LIT:1>] - norm_cdf[i]<EOL>if width == <NUM_LIT:0.0>:<EOL><INDENT>delta_peak_loc_to_probability_mass[left] = prob_mass<EOL><DEDENT>else:<EOL><INDENT>non_delta_peak_split_points.append(right)<EOL>non_delta_peak_densities.append(prob_mass / float(width))<EOL><DEDENT><DEDENT>assert (len(non_delta_peak_densities) == len(non_delta_peak_split_points) - <NUM_LIT:1>)<EOL>return numpy.array(non_delta_peak_split_points),numpy.array(non_delta_peak_densities), delta_peak_loc_to_probability_mass<EOL>

Temporal distance probability density function.

Returns
-------
non_delta_peak_split_points: numpy.array
non_delta_peak_densities: numpy.array
    len(density) == len(temporal_distance_split_points_ordered) -1
delta_peak_loc_to_probability_mass : dict


def get_upstream_stops_ratio(self, target, trough_stops, ratio):

if isinstance(trough_stops, list):<EOL><INDENT>trough_stops = "<STR_LIT:U+002C>".join(trough_stops)<EOL><DEDENT>query = """<STR_LIT>""".format(target=target, trough_stops=trough_stops, ratio=ratio)<EOL>df = read_sql_query(query, self.conn)<EOL>return df<EOL>

Selects the stops for which the ratio or higher proportion of trips to the target passes trough a set of trough stops
:param target: target of trips
:param trough_stops: stops where the selected trips are passing trough
:param ratio: threshold for inclusion
:return:


pass<EOL>


def __init__(self, gtfs_path, journey_db_path, routing_params=None, multitarget_routing=False,<EOL>track_vehicle_legs=True, track_route=False):

self.multitarget_routing = multitarget_routing<EOL>self.track_route = track_route<EOL>self.track_vehicle_legs = track_vehicle_legs<EOL>self.gtfs_path = gtfs_path<EOL>self.gtfs = GTFS(self.gtfs_path)<EOL>self.gtfs_meta = self.gtfs.meta<EOL>self.gtfs._dont_close = True<EOL>self.od_pairs = None<EOL>self._targets = None<EOL>self._origins = None<EOL>self.diff_conn = None<EOL>if not routing_params:<EOL><INDENT>routing_params = dict()<EOL><DEDENT>self.routing_params_input = routing_params<EOL>assert os.path.exists(journey_db_path) or routing_params is not None<EOL>journey_db_pre_exists = os.path.isfile(journey_db_path)<EOL>timeout = <NUM_LIT:1000><EOL>self.conn = sqlite3.connect(journey_db_path, timeout)<EOL>if not journey_db_pre_exists:<EOL><INDENT>self.initialize_database()<EOL><DEDENT>self.routing_parameters = Parameters(self.conn)<EOL>self._assert_journey_computation_paramaters_match()<EOL>self.journey_properties = {"<STR_LIT>": (_T_WALK_STR, _T_WALK_STR)}<EOL>if routing_params.get('<STR_LIT>', False) orself.routing_parameters.get('<STR_LIT>', False):<EOL><INDENT>self.journey_properties["<STR_LIT>"] = (float("<STR_LIT>"), <NUM_LIT:0>)<EOL><DEDENT>if self.track_route:<EOL><INDENT>additional_journey_parameters = {<EOL>"<STR_LIT>": (float('<STR_LIT>'), <NUM_LIT:0>),<EOL>"<STR_LIT>": (float('<STR_LIT>'), <NUM_LIT:0>),<EOL>"<STR_LIT>": (_T_WALK_STR, _T_WALK_STR),<EOL>"<STR_LIT>": (float('<STR_LIT>'), <NUM_LIT:0>)<EOL>}<EOL>self.journey_properties.update(additional_journey_parameters)<EOL><DEDENT>self.travel_impedance_measure_names = list(self.journey_properties.keys())<EOL>self.travel_impedance_measure_names += ["<STR_LIT>"]<EOL>

:param gtfs: GTFS object
:param list_of_stop_profiles: dict of NodeProfileMultiObjective
:param multitarget_routing: bool


@timeit<EOL><INDENT>def import_journey_data_for_target_stop(self, target_stop_I, origin_stop_I_to_journey_labels, enforce_synchronous_writes=False):<DEDENT>

Parameters
----------
origin_stop_I_to_journey_labels: dict
    key: origin_stop_Is
    value: list of labels
target_stop_I: int


def _insert_journeys_into_db_no_route(self, stop_profiles, target_stop=None):<EOL>

print("<STR_LIT>")<EOL>journey_id = <NUM_LIT:1><EOL>journey_list = []<EOL>tot = len(stop_profiles)<EOL>for i, (origin_stop, labels) in enumerate(stop_profiles.items(), start=<NUM_LIT:1>):<EOL><INDENT>for label in labels:<EOL><INDENT>assert (isinstance(label, LabelTimeWithBoardingsCount))<EOL>if self.multitarget_routing:<EOL><INDENT>target_stop = None<EOL><DEDENT>else:<EOL><INDENT>target_stop = int(target_stop)<EOL><DEDENT>values = [int(journey_id),<EOL>int(origin_stop),<EOL>target_stop,<EOL>int(label.departure_time),<EOL>int(label.arrival_time_target),<EOL>int(label.n_boardings)]<EOL>journey_list.append(values)<EOL>journey_id += <NUM_LIT:1><EOL><DEDENT><DEDENT>print("<STR_LIT>")<EOL>insert_journeys_stmt = '''<STR_LIT>''' % ("<STR_LIT:U+002CU+0020>".join(["<STR_LIT:?>" for x in range(<NUM_LIT:6>)]))<EOL>self._executemany_exclusive(insert_journeys_stmt, journey_list)<EOL>self.conn.commit()<EOL>

con.isolation_level = 'EXCLUSIVE'
con.execute('BEGIN EXCLUSIVE')
#exclusive access starts here. Nothing else can r/w the db, do your magic here.
con.commit()


def _journey_label_generator(self, destination_stop_Is=None, origin_stop_Is=None):

conn = self.conn<EOL>conn.row_factory = sqlite3.Row<EOL>if destination_stop_Is is None:<EOL><INDENT>destination_stop_Is = self.get_targets_having_journeys()<EOL><DEDENT>if origin_stop_Is is None:<EOL><INDENT>origin_stop_Is = self.get_origins_having_journeys()<EOL><DEDENT>for destination_stop_I in destination_stop_Is:<EOL><INDENT>if self.track_route:<EOL><INDENT>label_features = "<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>label_features = "<STR_LIT>""<STR_LIT>"<EOL><DEDENT>sql = "<STR_LIT>" + label_features + "<STR_LIT>" % destination_stop_I<EOL>df = pd.read_sql_query(sql, self.conn)<EOL>for origin_stop_I in origin_stop_Is:<EOL><INDENT>selection = df.loc[df['<STR_LIT>'] == origin_stop_I]<EOL>journey_labels = []<EOL>for journey in selection.to_dict(orient='<STR_LIT>'):<EOL><INDENT>journey["<STR_LIT>"] = -<NUM_LIT:1><EOL>try:<EOL><INDENT>journey_labels.append(LabelGeneric(journey))<EOL><DEDENT>except Exception as e:<EOL><INDENT>print(journey)<EOL>raise e<EOL><DEDENT><DEDENT>yield origin_stop_I, destination_stop_I, journey_labels<EOL><DEDENT><DEDENT>

Parameters
----------
destination_stop_Is: list-like
origin_stop_Is: list-like

Yields
------
(origin_stop_I, destination_stop_I, journey_labels) : tuple


def _insert_travel_impedance_data_to_db(self, travel_impedance_measure_name, data):

f = float<EOL>data_tuple = [(x["<STR_LIT>"], x["<STR_LIT>"], f(x["<STR_LIT>"]), f(x["<STR_LIT>"]), f(x["<STR_LIT>"]), f(x["<STR_LIT>"])) for x in data]<EOL>insert_stmt = '''<STR_LIT>''' + travel_impedance_measure_name + '''<STR_LIT>'''<EOL>self.conn.executemany(insert_stmt, data_tuple)<EOL>self.conn.commit()<EOL>

Parameters
----------
travel_impedance_measure_name: str
data: list[dict]
    Each list element must contain keys:
    "from_stop_I", "to_stop_I", "min", "max", "median" and "mean"


def get_transit_connections(gtfs, start_time_ut, end_time_ut):

if start_time_ut + <NUM_LIT:20> * <NUM_LIT> < end_time_ut:<EOL><INDENT>warn("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>assert (isinstance(gtfs, GTFS))<EOL>events_df = temporal_network(gtfs, start_time_ut=start_time_ut, end_time_ut=end_time_ut)<EOL>assert (isinstance(events_df, pandas.DataFrame))<EOL>return list(map(lambda e: Connection(e.from_stop_I, e.to_stop_I, e.dep_time_ut, e.arr_time_ut, e.trip_I, e.seq),<EOL>events_df.itertuples()<EOL>)<EOL>)<EOL>

Parameters
----------
gtfs: gtfspy.GTFS
end_time_ut: int
start_time_ut: int

Returns
-------
list[Connection]


assert (isinstance(gtfs, GTFS))<EOL>return walk_transfer_stop_to_stop_network(gtfs, max_link_distance=max_link_distance_m)<EOL>

Parameters
----------
gtfs: gtfspy.GTFS

Returns
-------
walk_network: networkx.Graph:


assert (isinstance(new_label, LabelTime))<EOL>if self._labels:<EOL><INDENT>assert (new_label.departure_time <= self._labels[-<NUM_LIT:1>].departure_time)<EOL>best_later_departing_arrival_time = self._labels[-<NUM_LIT:1>].arrival_time_target<EOL><DEDENT>else:<EOL><INDENT>best_later_departing_arrival_time = float('<STR_LIT>')<EOL><DEDENT>walk_to_target_arrival_time = new_label.departure_time + self._walk_to_target_duration<EOL>best_arrival_time = min(walk_to_target_arrival_time,<EOL>best_later_departing_arrival_time,<EOL>new_label.arrival_time_target)<EOL>if (new_label.arrival_time_target < walk_to_target_arrival_time and<EOL>new_label.arrival_time_target < best_later_departing_arrival_time):<EOL><INDENT>self._labels.append(LabelTime(new_label.departure_time, best_arrival_time))<EOL>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

Parameters
----------
new_label: LabelTime

Returns
-------
updated: bool


def evaluate_earliest_arrival_time_at_target(self, dep_time, transfer_margin):

minimum = dep_time + self._walk_to_target_duration<EOL>for label in self._labels[::-<NUM_LIT:1>]:<EOL><INDENT>if label.departure_time >= dep_time + transfer_margin:<EOL><INDENT>minimum = min(minimum, label.arrival_time_target)<EOL>break<EOL><DEDENT><DEDENT>return float(minimum)<EOL>

Get the earliest arrival time at the target, given a departure time.

Parameters
----------
dep_time : float, int
    time in unix seconds
transfer_margin: float, int
    transfer margin in seconds

Returns
-------
arrival_time : float
    Arrival time in the given time unit (seconds after unix epoch).


self.legs = []<EOL>self.departure_time = None<EOL>self.arrival_time = None<EOL>self.trip_ids = set()<EOL>self.n_boardings = <NUM_LIT:0><EOL>if legs is not None:<EOL><INDENT>for leg in legs:<EOL><INDENT>self.add_leg(leg)<EOL><DEDENT><DEDENT>

Parameters
----------
legs: list[Connection]


assert(isinstance(leg, Connection))<EOL>if not self.legs:<EOL><INDENT>self.departure_time = leg.departure_time<EOL><DEDENT>self.arrival_time = leg.arrival_time<EOL>if leg.trip_id and (not self.legs or (leg.trip_id != self.legs[-<NUM_LIT:1>].trip_id)):<EOL><INDENT>self.n_boardings += <NUM_LIT:1><EOL><DEDENT>self.arrival_time = leg.arrival_time<EOL>self.legs.append(leg)<EOL>


transfer_stop_pairs = []<EOL>previous_arrival_stop = None<EOL>current_trip_id = None<EOL>for leg in self.legs:<EOL><INDENT>if leg.trip_id is not None and leg.trip_id != current_trip_id and previous_arrival_stop is not None:<EOL><INDENT>transfer_stop_pair = (previous_arrival_stop, leg.departure_stop)<EOL>transfer_stop_pairs.append(transfer_stop_pair)<EOL><DEDENT>previous_arrival_stop = leg.arrival_stop<EOL>current_trip_id = leg.trip_id<EOL><DEDENT>return transfer_stop_pairs<EOL>

Get stop pairs through which transfers take place

Returns
-------
transfer_stop_pairs: list


AbstractRoutingAlgorithm.__init__(self)<EOL>self._target = target_stop<EOL>self._connections = transit_events<EOL>if start_time is None:<EOL><INDENT>start_time = transit_events[-<NUM_LIT:1>].departure_time<EOL><DEDENT>if end_time is None:<EOL><INDENT>end_time = transit_events[<NUM_LIT:0>].departure_time<EOL><DEDENT>self._start_time = start_time<EOL>self._end_time = end_time<EOL>self._transfer_margin = transfer_margin<EOL>if walk_network is None:<EOL><INDENT>walk_network = networkx.Graph()<EOL><DEDENT>self._walk_network = walk_network<EOL>self._walk_speed = float(walk_speed)<EOL>self._verbose = verbose<EOL>self.__trip_min_arrival_time = defaultdict(lambda: float("<STR_LIT>"))<EOL>self._stop_profiles = defaultdict(lambda: NodeProfileSimple())<EOL>self._stop_profiles[self._target] = NodeProfileSimple(<NUM_LIT:0>)<EOL>if target_stop in walk_network.nodes():<EOL><INDENT>for target_neighbor in walk_network.neighbors(target_stop):<EOL><INDENT>edge_data = walk_network.get_edge_data(target_neighbor, target_stop)<EOL>walk_duration = edge_data["<STR_LIT>"] / self._walk_speed<EOL>self._stop_profiles[target_neighbor] = NodeProfileSimple(walk_duration)<EOL><DEDENT><DEDENT>

Parameters
----------
transit_events: list[Connection]
    events are assumed to be ordered in DECREASING departure_time (!)
target_stop: int
    index of the target stop
start_time : int, optional
    start time in unixtime seconds
end_time: int, optional
    end time in unixtime seconds (no connections will be scanned after this time)
transfer_margin: int, optional
    required extra margin required for transfers in seconds
walk_speed: float, optional
    walking speed between stops in meters / second.
walk_network: networkx.Graph, optional
    each edge should have the walking distance as a data attribute ("distance_shape") expressed in meters
verbose: boolean, optional
    whether to print out progress


def _scan_footpaths_to_departure_stop(self, connection_dep_stop, connection_dep_time, arrival_time_target):

for _, neighbor, data in self._walk_network.edges_iter(nbunch=[connection_dep_stop],<EOL>data=True):<EOL><INDENT>d_walk = data['<STR_LIT>']<EOL>neighbor_dep_time = connection_dep_time - d_walk / self._walk_speed<EOL>pt = LabelTimeSimple(departure_time=neighbor_dep_time, arrival_time_target=arrival_time_target)<EOL>self._stop_profiles[neighbor].update_pareto_optimal_tuples(pt)<EOL><DEDENT>

A helper method for scanning the footpaths. Updates self._stop_profiles accordingly


assert self._has_run<EOL>return self._stop_profiles<EOL>

Returns
-------
_stop_profiles : dict[int, NodeProfileSimple]
    The pareto tuples necessary.


def __init__(self, labels, walk_time_to_target, start_time_dep, end_time_dep):

self.start_time_dep = start_time_dep<EOL>self.end_time_dep = end_time_dep<EOL>all_pareto_optimal_tuples = [pt for pt in labels if<EOL>(start_time_dep < pt.departure_time < end_time_dep)]<EOL>labels_after_dep_time = [label for label in labels if label.departure_time >= self.end_time_dep]<EOL>if labels_after_dep_time:<EOL><INDENT>next_label_after_end_time = min(labels_after_dep_time, key=lambda el: el.arrival_time_target)<EOL>all_pareto_optimal_tuples.append(next_label_after_end_time)<EOL><DEDENT>all_pareto_optimal_tuples = sorted(all_pareto_optimal_tuples, key=lambda ptuple: ptuple.departure_time)<EOL>arrival_time_target_at_end_time = end_time_dep + walk_time_to_target<EOL>previous_trip = None<EOL>for trip_tuple in all_pareto_optimal_tuples:<EOL><INDENT>if previous_trip:<EOL><INDENT>assert(trip_tuple.arrival_time_target > previous_trip.arrival_time_target)<EOL><DEDENT>if trip_tuple.departure_time > self.end_time_depand trip_tuple.arrival_time_target < arrival_time_target_at_end_time:<EOL><INDENT>arrival_time_target_at_end_time = trip_tuple.arrival_time_target<EOL><DEDENT>previous_trip = trip_tuple<EOL><DEDENT>self._walk_time_to_target = walk_time_to_target<EOL>self._profile_blocks = []<EOL>previous_departure_time = start_time_dep<EOL>self.trip_durations = []<EOL>self.trip_departure_times = []<EOL>for trip_pareto_tuple in all_pareto_optimal_tuples:<EOL><INDENT>if trip_pareto_tuple.departure_time > self.end_time_dep:<EOL><INDENT>continue<EOL><DEDENT>if self._walk_time_to_target <= trip_pareto_tuple.duration():<EOL><INDENT>print(self._walk_time_to_target, trip_pareto_tuple.duration())<EOL>assert(self._walk_time_to_target > trip_pareto_tuple.duration())<EOL><DEDENT>effective_trip_previous_departure_time = max(<EOL>previous_departure_time,<EOL>trip_pareto_tuple.departure_time - (self._walk_time_to_target - trip_pareto_tuple.duration())<EOL>)<EOL>if effective_trip_previous_departure_time > previous_departure_time:<EOL><INDENT>walk_block = ProfileBlock(start_time=previous_departure_time,<EOL>end_time=effective_trip_previous_departure_time,<EOL>distance_start=self._walk_time_to_target,<EOL>distance_end=self._walk_time_to_target<EOL>)<EOL>self._profile_blocks.append(walk_block)<EOL><DEDENT>trip_waiting_time = trip_pareto_tuple.departure_time - effective_trip_previous_departure_time<EOL>trip_block = ProfileBlock(end_time=trip_pareto_tuple.departure_time,<EOL>start_time=effective_trip_previous_departure_time,<EOL>distance_start=trip_pareto_tuple.duration() + trip_waiting_time,<EOL>distance_end=trip_pareto_tuple.duration())<EOL>self.trip_durations.append(trip_pareto_tuple.duration())<EOL>self.trip_departure_times.append(trip_pareto_tuple.departure_time)<EOL>self._profile_blocks.append(trip_block)<EOL>previous_departure_time = trip_pareto_tuple.departure_time<EOL><DEDENT>if not self._profile_blocks or self._profile_blocks[-<NUM_LIT:1>].end_time < end_time_dep:<EOL><INDENT>if len(self._profile_blocks) > <NUM_LIT:0>:<EOL><INDENT>dep_previous = self._profile_blocks[-<NUM_LIT:1>].end_time<EOL><DEDENT>else:<EOL><INDENT>dep_previous = start_time_dep<EOL><DEDENT>waiting_time = end_time_dep - dep_previous<EOL>distance_end_trip = arrival_time_target_at_end_time - end_time_dep<EOL>walking_wait_time = min(end_time_dep - dep_previous,<EOL>waiting_time - (self._walk_time_to_target - distance_end_trip))<EOL>walking_wait_time = max(<NUM_LIT:0>, walking_wait_time)<EOL>if walking_wait_time > <NUM_LIT:0>:<EOL><INDENT>walk_block = ProfileBlock(start_time=dep_previous,<EOL>end_time=dep_previous + walking_wait_time,<EOL>distance_start=self._walk_time_to_target,<EOL>distance_end=self._walk_time_to_target<EOL>)<EOL>assert (walk_block.start_time <= walk_block.end_time)<EOL>assert (walk_block.distance_end <= walk_block.distance_start)<EOL>self._profile_blocks.append(walk_block)<EOL><DEDENT>trip_waiting_time = waiting_time - walking_wait_time<EOL>if trip_waiting_time > <NUM_LIT:0>:<EOL><INDENT>try:<EOL><INDENT>trip_block = ProfileBlock(start_time=dep_previous + walking_wait_time,<EOL>end_time=dep_previous + walking_wait_time + trip_waiting_time,<EOL>distance_start=distance_end_trip + trip_waiting_time,<EOL>distance_end=distance_end_trip)<EOL>assert (trip_block.start_time <= trip_block.end_time)<EOL>assert (trip_block.distance_end <= trip_block.distance_start)<EOL>self._profile_blocks.append(trip_block)<EOL><DEDENT>except AssertionError as e:<EOL><INDENT>assert(trip_waiting_time < <NUM_LIT:10>**-<NUM_LIT:5>)<EOL><DEDENT><DEDENT><DEDENT>self.profile_block_analyzer = ProfileBlockAnalyzer(profile_blocks=self._profile_blocks)<EOL>

Initialize the data structures required by

Parameters
----------
node_profile: NodeProfileSimple


return float(len(self.trip_durations))<EOL>

Get number of pareto-optimal trips

Returns
-------
n_trips: float


return numpy.min(self.trip_durations)<EOL>

Get minimum travel time to destination.

Returns
-------
float: min_trip_duration
    float('nan') if no trips take place


return numpy.max(self.trip_durations)<EOL>

Get minimum travel time to destination.

Returns
-------
float: max_trip_duration
    float('inf') if no trips take place


return numpy.mean(self.trip_durations)<EOL>

Get average travel time to destination.

Returns
-------
float: max_trip_duration
    float('inf') if no trips take place


return numpy.median(self.trip_durations)<EOL>


total_width = self.end_time_dep - self.start_time_dep<EOL>total_area = sum([block.area() for block in self._profile_blocks])<EOL>return total_area / total_width<EOL>

Get mean temporal distance (in seconds) to the target.

Returns
-------
mean_temporal_distance : float


return self.profile_block_analyzer.median()<EOL>

Returns
-------
median_temporal_distance : float


return self.profile_block_analyzer.min()<EOL>

Compute the minimum temporal distance to target.

Returns
-------
min_temporal_distance: float


return self.profile_block_analyzer.max()<EOL>

Compute the maximum temporal distance.

Returns
-------
max_temporal_distance : float


return self.profile_block_analyzer.largest_finite_distance()<EOL>


xvalues, cdf = self.profile_block_analyzer._temporal_distance_cdf()<EOL>fig = plt.figure()<EOL>ax = fig.add_subplot(<NUM_LIT>)<EOL>xvalues = numpy.array(xvalues) / <NUM_LIT><EOL>ax.plot(xvalues, cdf, "<STR_LIT>")<EOL>ax.fill_between(xvalues, cdf, color="<STR_LIT>", alpha=<NUM_LIT>)<EOL>ax.set_ylabel("<STR_LIT>")<EOL>ax.set_xlabel("<STR_LIT>")<EOL>return fig<EOL>

Plot the temporal distance cumulative density function.

Returns
-------
fig: matplotlib.Figure


def plot_temporal_distance_pdf(self, use_minutes=True, color="<STR_LIT>", ax=None):

from matplotlib import pyplot as plt<EOL>plt.rc('<STR_LIT:text>', usetex=True)<EOL>temporal_distance_split_points_ordered, densities, delta_peaks = self._temporal_distance_pdf()<EOL>xs = []<EOL>for i, x in enumerate(temporal_distance_split_points_ordered):<EOL><INDENT>xs.append(x)<EOL>xs.append(x)<EOL><DEDENT>xs = numpy.array(xs)<EOL>ys = [<NUM_LIT:0>]<EOL>for y in densities:<EOL><INDENT>ys.append(y)<EOL>ys.append(y)<EOL><DEDENT>ys.append(<NUM_LIT:0>)<EOL>ys = numpy.array(ys)<EOL>xlabel = "<STR_LIT>"<EOL>ylabel = "<STR_LIT>"<EOL>if use_minutes:<EOL><INDENT>xs /= <NUM_LIT><EOL>ys *= <NUM_LIT><EOL>xlabel = "<STR_LIT>"<EOL>delta_peaks = {peak / <NUM_LIT>: mass for peak, mass in delta_peaks.items()}<EOL><DEDENT>if ax is None:<EOL><INDENT>fig = plt.figure()<EOL>ax = fig.add_subplot(<NUM_LIT>)<EOL><DEDENT>ax.plot(xs, ys, "<STR_LIT>")<EOL>ax.fill_between(xs, ys, color="<STR_LIT>", alpha=<NUM_LIT>)<EOL>if delta_peaks:<EOL><INDENT>peak_height = max(ys) * <NUM_LIT><EOL>max_x = max(xs)<EOL>min_x = min(xs)<EOL>now_max_x = max(xs) + <NUM_LIT> * (max_x - min_x)<EOL>now_min_x = min_x - <NUM_LIT:0.1> * (max_x - min_x)<EOL>text_x_offset = <NUM_LIT:0.1> * (now_max_x - max_x)<EOL>for loc, mass in delta_peaks.items():<EOL><INDENT>ax.plot([loc, loc], [<NUM_LIT:0>, peak_height], color="<STR_LIT>", lw=<NUM_LIT:5>)<EOL>ax.text(loc + text_x_offset, peak_height * <NUM_LIT>, "<STR_LIT>" % (mass), color="<STR_LIT>")<EOL><DEDENT>ax.set_xlim(now_min_x, now_max_x)<EOL>tot_delta_peak_mass = sum(delta_peaks.values())<EOL>transit_text_x = (min_x + max_x) / <NUM_LIT:2><EOL>transit_text_y = min(ys[ys > <NUM_LIT:0>]) / <NUM_LIT><EOL>ax.text(transit_text_x,<EOL>transit_text_y,<EOL>"<STR_LIT>" % (<NUM_LIT:1> - tot_delta_peak_mass),<EOL>color="<STR_LIT>",<EOL>va="<STR_LIT>",<EOL>ha="<STR_LIT>")<EOL><DEDENT>ax.set_xlabel(xlabel)<EOL>ax.set_ylabel(ylabel)<EOL>ax.set_ylim(bottom=<NUM_LIT:0>)<EOL>return ax.figure<EOL>

Plot the temporal distance probability density function.

Returns
-------
fig: matplotlib.Figure


from matplotlib import pyplot as plt<EOL>plt.rc('<STR_LIT:text>', usetex=True)<EOL>if ax is None:<EOL><INDENT>fig = plt.figure()<EOL>ax = fig.add_subplot(<NUM_LIT>)<EOL><DEDENT>temporal_distance_split_points_ordered, densities, delta_peaks = self._temporal_distance_pdf()<EOL>xs = []<EOL>for i, x in enumerate(temporal_distance_split_points_ordered):<EOL><INDENT>xs.append(x)<EOL>xs.append(x)<EOL><DEDENT>xs = numpy.array(xs)<EOL>ys = [<NUM_LIT:0>]<EOL>for y in densities:<EOL><INDENT>ys.append(y)<EOL>ys.append(y)<EOL><DEDENT>ys.append(<NUM_LIT:0>)<EOL>ys = numpy.array(ys)<EOL>xlabel = "<STR_LIT>"<EOL>ylabel = "<STR_LIT>"<EOL>if use_minutes:<EOL><INDENT>xs /= duration_divider<EOL>ys *= duration_divider<EOL>xlabel = "<STR_LIT>"<EOL>delta_peaks = {peak / <NUM_LIT>: mass for peak, mass in delta_peaks.items()}<EOL><DEDENT>if delta_peaks:<EOL><INDENT>peak_height = max(ys) * <NUM_LIT><EOL>max_x = max(xs)<EOL>min_x = min(xs)<EOL>now_max_x = max(xs) + <NUM_LIT> * (max_x - min_x)<EOL>now_min_x = min_x - <NUM_LIT:0.1> * (max_x - min_x)<EOL>text_x_offset = <NUM_LIT:0.1> * (now_max_x - max_x)<EOL>for loc, mass in delta_peaks.items():<EOL><INDENT>text = "<STR_LIT>" + ("<STR_LIT>" % (mass))<EOL>ax.plot([<NUM_LIT:0>, peak_height], [loc, loc], color=color, lw=<NUM_LIT:5>, label=text)<EOL><DEDENT><DEDENT>ax.plot(ys, xs, "<STR_LIT>")<EOL>if delta_peaks:<EOL><INDENT>tot_delta_peak_mass = sum(delta_peaks.values())<EOL>fill_label = "<STR_LIT>" % (<NUM_LIT:1>-tot_delta_peak_mass)<EOL><DEDENT>else:<EOL><INDENT>fill_label = None<EOL><DEDENT>ax.fill_betweenx(xs, ys, color=color, alpha=<NUM_LIT>, label=fill_label)<EOL>ax.set_ylabel(xlabel)<EOL>ax.set_xlabel(ylabel)<EOL>ax.set_xlim(left=<NUM_LIT:0>, right=max(ys) * <NUM_LIT>)<EOL>if delta_peaks:<EOL><INDENT>if legend_font_size is None:<EOL><INDENT>legend_font_size = <NUM_LIT:12><EOL><DEDENT>if legend_loc is None:<EOL><INDENT>legend_loc = "<STR_LIT>"<EOL><DEDENT>ax.legend(loc=legend_loc, prop={'<STR_LIT:size>': legend_font_size})<EOL><DEDENT>if True:<EOL><INDENT>line_tyles = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT:->"][::-<NUM_LIT:1>]<EOL>to_plot_funcs = [self.max_temporal_distance, self.mean_temporal_distance, self.min_temporal_distance]<EOL>xmin, xmax = ax.get_xlim()<EOL>for to_plot_func, ls in zip(to_plot_funcs, line_tyles):<EOL><INDENT>y = to_plot_func() / duration_divider<EOL>assert y < float('<STR_LIT>')<EOL>ax.plot([xmin, xmax*<NUM_LIT:10>], [y, y], color="<STR_LIT>", ls=ls, lw=<NUM_LIT:1>)<EOL><DEDENT><DEDENT>return ax.figure<EOL>


if ax is None:<EOL><INDENT>fig = plt.figure()<EOL>ax = fig.add_subplot(<NUM_LIT>)<EOL><DEDENT>if timezone is None:<EOL><INDENT>warnings.warn("<STR_LIT>")<EOL>timezone = pytz.timezone("<STR_LIT>")<EOL><DEDENT>def _ut_to_unloc_datetime(ut):<EOL><INDENT>dt = datetime.datetime.fromtimestamp(ut, timezone)<EOL>return dt.replace(tzinfo=None)<EOL><DEDENT>if format_string:<EOL><INDENT>x_axis_formatter = md.DateFormatter(format_string)<EOL>ax.xaxis.set_major_formatter(x_axis_formatter)<EOL><DEDENT>else:<EOL><INDENT>_ut_to_unloc_datetime = lambda x: x<EOL><DEDENT>ax.set_xlim(<EOL>_ut_to_unloc_datetime(self.start_time_dep),<EOL>_ut_to_unloc_datetime(self.end_time_dep)<EOL>)<EOL>if plot_tdist_stats:<EOL><INDENT>line_tyles = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT:->"][::-<NUM_LIT:1>]<EOL>to_plot_labels  = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]<EOL>to_plot_funcs = [self.max_temporal_distance, self.mean_temporal_distance, self.min_temporal_distance]<EOL>xmin, xmax = ax.get_xlim()<EOL>for to_plot_label, to_plot_func, ls in zip(to_plot_labels, to_plot_funcs, line_tyles):<EOL><INDENT>y = to_plot_func() / duration_divider<EOL>assert y < float('<STR_LIT>'), to_plot_label<EOL>to_plot_label = to_plot_label + "<STR_LIT>" % (y)<EOL>ax.plot([xmin, xmax], [y, y], color="<STR_LIT>", ls=ls, lw=<NUM_LIT:1>, label=to_plot_label)<EOL><DEDENT><DEDENT>if plot_trip_stats:<EOL><INDENT>assert (not plot_tdist_stats)<EOL>line_tyles = ["<STR_LIT:->", "<STR_LIT>", "<STR_LIT>"]<EOL>to_plot_labels = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]<EOL>to_plot_funcs = [self.min_trip_duration, self.max_trip_duration, self.mean_trip_duration]<EOL>xmin, xmax = ax.get_xlim()<EOL>for to_plot_label, to_plot_func, ls in zip(to_plot_labels, to_plot_funcs, line_tyles):<EOL><INDENT>y = to_plot_func() / duration_divider<EOL>if not numpy.math.isnan(y):<EOL><INDENT>ax.plot([xmin, xmax], [y, y], color="<STR_LIT>", ls=ls, lw=<NUM_LIT:2>)<EOL>txt = to_plot_label + "<STR_LIT>" % y<EOL>ax.text(xmax + <NUM_LIT> * (xmax - xmin), y, txt, color="<STR_LIT>", va="<STR_LIT>", ha="<STR_LIT:left>")<EOL><DEDENT><DEDENT>old_xmax = xmax<EOL>xmax += (xmax - xmin) * <NUM_LIT><EOL>ymin, ymax = ax.get_ylim()<EOL>ax.fill_between([old_xmax, xmax], ymin, ymax, color="<STR_LIT>", alpha=<NUM_LIT:0.1>)<EOL>ax.set_xlim(xmin, xmax)<EOL><DEDENT>vertical_lines, slopes = self.profile_block_analyzer.get_vlines_and_slopes_for_plotting()<EOL>for i, line in enumerate(slopes):<EOL><INDENT>xs = [_ut_to_unloc_datetime(x) for x in line['<STR_LIT:x>']]<EOL>if i is <NUM_LIT:0>:<EOL><INDENT>label = u"<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>label = None<EOL><DEDENT>ax.plot(xs, numpy.array(line['<STR_LIT:y>']) / duration_divider, "<STR_LIT:->", color=color, lw=lw, label=label)<EOL><DEDENT>for line in vertical_lines:<EOL><INDENT>xs = [_ut_to_unloc_datetime(x) for x in line['<STR_LIT:x>']]<EOL>ax.plot(xs, numpy.array(line['<STR_LIT:y>']) / duration_divider, "<STR_LIT::>", color=color)  <EOL><DEDENT>assert (isinstance(ax, plt.Axes))<EOL>if plot_journeys:<EOL><INDENT>xs = [_ut_to_unloc_datetime(x) for x in self.trip_departure_times]<EOL>ys = self.trip_durations<EOL>ax.plot(xs, numpy.array(ys) / duration_divider, "<STR_LIT:o>", color="<STR_LIT>", ms=<NUM_LIT:8>, label="<STR_LIT>")<EOL>if journey_letters is None:<EOL><INDENT>journey_letters = "<STR_LIT>"<EOL>def cycle_journey_letters(journey_letters):<EOL><INDENT>saved = []<EOL>for element in journey_letters:<EOL><INDENT>yield element<EOL>saved.append(element)<EOL><DEDENT>count = <NUM_LIT:1><EOL>while saved:<EOL><INDENT>for element in saved:<EOL><INDENT>yield element + str(count)<EOL><DEDENT>count += <NUM_LIT:1><EOL><DEDENT><DEDENT>journey_letters_iterator = cycle_journey_letters(journey_letters)<EOL><DEDENT>time_letters = {int(time): letter for letter, time in zip(journey_letters_iterator, self.trip_departure_times)}<EOL>for x, y, letter in zip(xs, ys, journey_letters_iterator):<EOL><INDENT>walking = - self._walk_time_to_target / <NUM_LIT:30> if numpy.isfinite(self._walk_time_to_target) else <NUM_LIT:0><EOL>ax.text(x + datetime.timedelta(seconds=(self.end_time_dep - self.start_time_dep) / <NUM_LIT>),<EOL>(y + walking) / duration_divider, letter, va="<STR_LIT>", ha="<STR_LIT:left>")<EOL><DEDENT><DEDENT>fill_between_x = []<EOL>fill_between_y = []<EOL>for line in slopes:<EOL><INDENT>xs = [_ut_to_unloc_datetime(x) for x in line['<STR_LIT:x>']]<EOL>fill_between_x.extend(xs)<EOL>fill_between_y.extend(numpy.array(line["<STR_LIT:y>"]) / duration_divider)<EOL><DEDENT>ax.fill_between(fill_between_x, y1=fill_between_y, color=fill_color, alpha=alpha, label=label)<EOL>ax.set_ylim(bottom=<NUM_LIT:0>)<EOL>ax.set_ylim(ax.get_ylim()[<NUM_LIT:0>], ax.get_ylim()[<NUM_LIT:1>] * <NUM_LIT>)<EOL>if rcParams['<STR_LIT>']:<EOL><INDENT>ax.set_xlabel(r"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>ax.set_xlabel("<STR_LIT>")<EOL><DEDENT>ax.set_ylabel(r"<STR_LIT>")<EOL>if plot_journeys and return_letters:<EOL><INDENT>return ax, time_letters<EOL><DEDENT>else:<EOL><INDENT>return ax<EOL><DEDENT>

Parameters
----------
timezone: str
color: color
format_string: str, None
    if None, the original values are used
plot_journeys: bool, optional
    if True, small dots are plotted at the departure times


temporal_distance_split_points_ordered, norm_cdf = self.profile_block_analyzer._temporal_distance_cdf()<EOL>delta_peak_loc_to_probability_mass = {}<EOL>non_delta_peak_split_points = [temporal_distance_split_points_ordered[<NUM_LIT:0>]]<EOL>non_delta_peak_densities = []<EOL>for i in range(<NUM_LIT:0>, len(temporal_distance_split_points_ordered) - <NUM_LIT:1>):<EOL><INDENT>left = temporal_distance_split_points_ordered[i]<EOL>right = temporal_distance_split_points_ordered[i + <NUM_LIT:1>]<EOL>width = right - left<EOL>prob_mass = norm_cdf[i + <NUM_LIT:1>] - norm_cdf[i]<EOL>if width == <NUM_LIT:0.0>:<EOL><INDENT>delta_peak_loc_to_probability_mass[left] = prob_mass<EOL><DEDENT>else:<EOL><INDENT>non_delta_peak_split_points.append(right)<EOL>non_delta_peak_densities.append(prob_mass / float(width))<EOL><DEDENT><DEDENT>assert (len(non_delta_peak_densities) == len(non_delta_peak_split_points) - <NUM_LIT:1>)<EOL>return numpy.array(non_delta_peak_split_points),numpy.array(non_delta_peak_densities),delta_peak_loc_to_probability_mass<EOL>

Temporal distance probability density function.

Returns
-------
non_delta_peak_split_points: numpy.array
non_delta_peak_densities: numpy.array
    len(density) == len(temporal_distance_split_points_ordered) -1
delta_peak_loc_to_probability_mass : dict


AbstractRoutingAlgorithm.__init__(self)<EOL>self._target = target_stop<EOL>self._transit_connections = transit_events<EOL>if start_time is None:<EOL><INDENT>start_time = transit_events[-<NUM_LIT:1>].departure_time<EOL><DEDENT>if end_time is None:<EOL><INDENT>end_time = transit_events[<NUM_LIT:0>].departure_time<EOL><DEDENT>self._start_time = start_time<EOL>self._end_time = end_time<EOL>self._transfer_margin = transfer_margin<EOL>if walk_network is None:<EOL><INDENT>walk_network = networkx.Graph()<EOL><DEDENT>self._walk_network = walk_network<EOL>self._walk_speed = float(walk_speed)<EOL>self._verbose = verbose<EOL>self.__trip_min_arrival_time = defaultdict(lambda: float("<STR_LIT>"))<EOL>self._stop_profiles = defaultdict(lambda: NodeProfileC())<EOL>self._stop_profiles[self._target] = NodeProfileC(<NUM_LIT:0>)<EOL>if target_stop in walk_network.nodes():<EOL><INDENT>for target_neighbor in walk_network.neighbors(target_stop):<EOL><INDENT>edge_data = walk_network.get_edge_data(target_neighbor, target_stop)<EOL>walk_duration = edge_data["<STR_LIT>"] / self._walk_speed<EOL>self._stop_profiles[target_neighbor] = NodeProfileC(walk_duration)<EOL><DEDENT><DEDENT>pseudo_connection_set = compute_pseudo_connections(transit_events, self._start_time, self._end_time,<EOL>self._transfer_margin, self._walk_network,<EOL>self._walk_speed)<EOL>self._pseudo_connections = list(pseudo_connection_set)<EOL>self._all_connections = self._pseudo_connections + self._transit_connections<EOL>self._all_connections.sort(key=lambda connection: -connection.departure_time)<EOL>


assert self._has_run<EOL>return self._stop_profiles<EOL>


def __init__(self, labels, start_time_dep, end_time_dep, walk_duration=float('<STR_LIT>'), label_props_to_consider=None, **kwargs):

for label in labels:<EOL><INDENT>assert (hasattr(label, "<STR_LIT>"))<EOL>assert (hasattr(label, "<STR_LIT>"))<EOL><DEDENT>self.start_time_dep = start_time_dep<EOL>self.end_time_dep = end_time_dep<EOL>self.walk_duration = walk_duration<EOL>if label_props_to_consider is None:<EOL><INDENT>self.label_props = []<EOL><DEDENT>else:<EOL><INDENT>self.label_props = label_props_to_consider<EOL><DEDENT>self._fastest_path_labels = self._compute_fastest_path_labels(labels)<EOL>for label in self._fastest_path_labels:<EOL><INDENT>for prop in self.label_props:<EOL><INDENT>assert (hasattr(label, prop))<EOL><DEDENT><DEDENT>self.kwargs = kwargs<EOL>

Parameters
----------
labels: list
    List of labels (each label should at least have attributes "departure_time" and "arrival_time")
walk_duration: float
    What is the maximum duration for a journey to be considered.
label_props_to_consider: list


return NodeProfileAnalyzerTime(self._fastest_path_labels,<EOL>self.walk_duration,<EOL>self.start_time_dep,<EOL>self.end_time_dep)<EOL>


def get_prop_analyzer_flat(self, property, value_no_next_journey, value_cutoff):

kwargs = self.kwargs<EOL>fp_blocks = self.get_fastest_path_temporal_distance_blocks()<EOL>prop_blocks = []<EOL>for b in fp_blocks:<EOL><INDENT>if b.is_flat():<EOL><INDENT>if b.distance_end == self.walk_duration and b.distance_end != float('<STR_LIT>'):<EOL><INDENT>prop_value = value_cutoff<EOL><DEDENT>else:<EOL><INDENT>prop_value = value_no_next_journey<EOL><DEDENT><DEDENT>else:<EOL><INDENT>prop_value = b[property]<EOL><DEDENT>prop_block = ProfileBlock(b.start_time, b.end_time, prop_value, prop_value)<EOL>prop_blocks.append(prop_block)<EOL><DEDENT>return ProfileBlockAnalyzer(prop_blocks, **kwargs)<EOL>

Get a journey property analyzer, where each journey is weighted by the number of.

Parameters
----------
property: string
    Name of the property, needs to be one of label_props given on initialization.
value_no_next_journey:
    Value of the profile, when there is no next journey available.
value_cutoff: number
    default value of the property when cutoff is applied

Returns
-------
ProfileBlockAnalyzer


def compute_pseudo_connections(transit_connections, start_time_dep,<EOL>end_time_dep, transfer_margin,<EOL>walk_network, walk_speed):

<EOL>pseudo_connection_set = set()  <EOL>for c in transit_connections:<EOL><INDENT>if start_time_dep <= c.departure_time <= end_time_dep:<EOL><INDENT>walk_arr_stop = c.departure_stop<EOL>walk_arr_time = c.departure_time - transfer_margin<EOL>for _, walk_dep_stop, data in walk_network.edges(nbunch=[walk_arr_stop], data=True):<EOL><INDENT>walk_dep_time = walk_arr_time - data['<STR_LIT>'] / float(walk_speed)<EOL>if walk_dep_time > end_time_dep or walk_dep_time < start_time_dep:<EOL><INDENT>continue<EOL><DEDENT>pseudo_connection = Connection(walk_dep_stop,<EOL>walk_arr_stop,<EOL>walk_dep_time,<EOL>walk_arr_time,<EOL>Connection.WALK_TRIP_ID,<EOL>Connection.WALK_SEQ,<EOL>is_walk=True)<EOL>pseudo_connection_set.add(pseudo_connection)<EOL><DEDENT><DEDENT><DEDENT>return pseudo_connection_set<EOL>

Given a set of transit events and the static walk network,
"transform" the static walking network into a set of "pseudo-connections".

As a first approximation, we add pseudo-connections to depart after each arrival of a transit connection
to it's arrival stop.

Parameters
----------
transit_connections: list[Connection]
start_time_dep : int
    start time in unixtime seconds
end_time_dep: int
    end time in unixtime seconds (no new connections will be scanned after this time)
transfer_margin: int
    required extra margin required for transfers in seconds
walk_speed: float
    walking speed between stops in meters / second
walk_network: networkx.Graph
    each edge should have the walking distance as a data attribute ("d_walk") expressed in meters

Returns
-------
pseudo_connections: set[Connection]


def add_walk_distances_to_db_python(gtfs, osm_path, cutoff_distance_m=<NUM_LIT:1000>):

if isinstance(gtfs, str):<EOL><INDENT>gtfs = GTFS(gtfs)<EOL><DEDENT>assert (isinstance(gtfs, GTFS))<EOL>print("<STR_LIT>")<EOL>walk_network = create_walk_network_from_osm(osm_path)<EOL>print("<STR_LIT>")<EOL>stop_I_to_nearest_osm_node, stop_I_to_nearest_osm_node_distance = match_stops_to_nodes(gtfs, walk_network)<EOL>transfers = gtfs.get_straight_line_transfer_distances()<EOL>from_I_to_to_stop_Is = {stop_I: set() for stop_I in stop_I_to_nearest_osm_node}<EOL>for transfer_tuple in transfers.itertuples():<EOL><INDENT>from_I = transfer_tuple.from_stop_I<EOL>to_I = transfer_tuple.to_stop_I<EOL>from_I_to_to_stop_Is[from_I].add(to_I)<EOL><DEDENT>print("<STR_LIT>")<EOL>for from_I, to_stop_Is in from_I_to_to_stop_Is.items():<EOL><INDENT>from_node = stop_I_to_nearest_osm_node[from_I]<EOL>from_dist = stop_I_to_nearest_osm_node_distance[from_I]<EOL>shortest_paths = networkx.single_source_dijkstra_path_length(walk_network,<EOL>from_node,<EOL>cutoff=cutoff_distance_m - from_dist,<EOL>weight="<STR_LIT>")<EOL>for to_I in to_stop_Is:<EOL><INDENT>to_distance = stop_I_to_nearest_osm_node_distance[to_I]<EOL>to_node = stop_I_to_nearest_osm_node[to_I]<EOL>osm_distance = shortest_paths.get(to_node, float('<STR_LIT>'))<EOL>total_distance = from_dist + osm_distance + to_distance<EOL>from_stop_I_transfers = transfers[transfers['<STR_LIT>'] == from_I]<EOL>straigth_distance = from_stop_I_transfers[from_stop_I_transfers["<STR_LIT>"] == to_I]["<STR_LIT:d>"].values[<NUM_LIT:0>]<EOL>assert (straigth_distance < total_distance + <NUM_LIT:2>)  <EOL>if total_distance <= cutoff_distance_m:<EOL><INDENT>gtfs.conn.execute("<STR_LIT>"<EOL>"<STR_LIT>" + str(int(total_distance)) +<EOL>"<STR_LIT>" + str(from_I) + "<STR_LIT>" + str(to_I))<EOL><DEDENT><DEDENT><DEDENT>gtfs.conn.commit()<EOL>

Computes the walk paths between stops, and updates these to the gtfs database.

Parameters
----------
gtfs: gtfspy.GTFS or str
    A GTFS object or a string representation.
osm_path: str
    path to the OpenStreetMap file
cutoff_distance_m: number
    maximum allowed distance in meters

Returns
-------
None

See Also
--------
gtfspy.calc_transfers
compute_walk_paths_java


network_nodes = walk_network.nodes(data="<STR_LIT:true>")<EOL>stop_Is = set(gtfs.get_straight_line_transfer_distances()['<STR_LIT>'])<EOL>stops_df = gtfs.stops()<EOL>geo_index = GeoGridIndex(precision=<NUM_LIT:6>)<EOL>for net_node, data in network_nodes:<EOL><INDENT>geo_index.add_point(GeoPoint(data['<STR_LIT>'], data['<STR_LIT>'], ref=net_node))<EOL><DEDENT>stop_I_to_node = {}<EOL>stop_I_to_dist = {}<EOL>for stop_I in stop_Is:<EOL><INDENT>stop_lat = float(stops_df[stops_df.stop_I == stop_I].lat)<EOL>stop_lon = float(stops_df[stops_df.stop_I == stop_I].lon)<EOL>geo_point = GeoPoint(stop_lat, stop_lon)<EOL>min_dist = float('<STR_LIT>')<EOL>min_dist_node = None<EOL>search_distances_m = [<NUM_LIT>, <NUM_LIT>]<EOL>for search_distance_m in search_distances_m:<EOL><INDENT>for point, distance in geo_index.get_nearest_points(geo_point, search_distance_m, "<STR_LIT>"):<EOL><INDENT>if distance < min_dist:<EOL><INDENT>min_dist = distance * <NUM_LIT:1000><EOL>min_dist_node = point.ref<EOL><DEDENT><DEDENT>if min_dist_node is not None:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if min_dist_node is None:<EOL><INDENT>warn("<STR_LIT>" + str(stops_df[stops_df.stop_I == stop_I]))<EOL><DEDENT>stop_I_to_node[stop_I] = min_dist_node<EOL>stop_I_to_dist[stop_I] = min_dist<EOL><DEDENT>return stop_I_to_node, stop_I_to_dist<EOL>

Parameters
----------
gtfs : a GTFS object
walk_network : networkx.Graph

Returns
-------
stop_I_to_node: dict
    maps stop_I to closest walk_network node
stop_I_to_dist: dict
    maps stop_I to the distance to the closest walk_network node


def compute_walk_paths_java(gtfs_db_path, osm_file, cache_db=None):

raise NotImplementedError("<STR_LIT>")<EOL>

Parameters
----------
gtfs_db_path: str (path to the gtfs database)
osm_file: str
cache_db: str

Returns
-------
None


cls.gtfs_source_dir = os.path.join(os.path.dirname(__file__), "<STR_LIT>")<EOL>cls.G = GTFS.from_directory_as_inmemory_db(cls.gtfs_source_dir)<EOL>

This method is run once before executing any tests


self.gtfs_source_dir = self.__class__.gtfs_source_dir<EOL>self.gtfs = self.__class__.G<EOL>self.extract_output_dir = os.path.join(self.gtfs_source_dir, "<STR_LIT>", "<STR_LIT>")<EOL>if not os.path.exists(self.extract_output_dir):<EOL><INDENT>makedirs(self.extract_output_dir)<EOL><DEDENT>

This method is run once before _each_ test method is executed


self.gtfs = GTFS.from_directory_as_inmemory_db(self.gtfs_source_dir)<EOL>


pass<EOL>

This method is run once for each class before any tests are run


pass<EOL>

This method is run once for each class _after_ all tests are run


self.conn = sqlite3.connect('<STR_LIT>')<EOL>self.agencyText ='<STR_LIT>''<STR_LIT>'<EOL>self.stopsText ='<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>'<EOL>self.calendarText ='<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>'<EOL>self.calendarDatesText ='<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>'<EOL>self.tripText ="<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL>self.routesText ="<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL>self.shapeText ="<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL>self.stopTimesText ="<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL>self.frequenciesText ="<STR_LIT>""<STR_LIT>"<EOL>self.transfersText ="<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL>self.feedInfoText ="<STR_LIT>""<STR_LIT>"<EOL>self.fdict = {<EOL>'<STR_LIT>':           self.agencyText,<EOL>'<STR_LIT>':            self.stopsText,<EOL>'<STR_LIT>':         self.calendarText,<EOL>'<STR_LIT>':   self.calendarDatesText,<EOL>'<STR_LIT>':            self.tripText,<EOL>'<STR_LIT>':           self.routesText,<EOL>'<STR_LIT>':           self.shapeText,<EOL>'<STR_LIT>':       self.stopTimesText,<EOL>'<STR_LIT>':      self.frequenciesText,<EOL>'<STR_LIT>':        self.transfersText,<EOL>'<STR_LIT>':        self.feedInfoText<EOL>}<EOL>self.orig_row_factory = self.conn.row_factory<EOL>


prev_row_factory = self.setRowConn()<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>" + table_name)<EOL>print("<STR_LIT>")<EOL>cur = self.conn.execute("<STR_LIT>" % table_name)<EOL>names = [d[<NUM_LIT:0>] for d in cur.description]<EOL>for name in names:<EOL><INDENT>print(name + '<STR_LIT:U+002CU+0020>', end="<STR_LIT>")<EOL><DEDENT>print("<STR_LIT>")<EOL>for row in cur:<EOL><INDENT>print(row)<EOL><DEDENT>self.conn.row_factory = prev_row_factory<EOL>

Pretty prints a table with name table_name.

Parameters
----------
table_name : str
    name of the table


pass<EOL>

This method is run once after _each_ test method is executed


self.gtfs_source_dir = self.__class__.gtfs_source_dir<EOL>self.gtfs = self.__class__.G<EOL>


pass<EOL>


def remove_all_trips_fully_outside_buffer(db_conn, center_lat, center_lon, buffer_km, update_secondary_data=True):

distance_function_str = add_wgs84_distance_function_to_db(db_conn)<EOL>stops_within_buffer_query_sql = "<STR_LIT>" + distance_function_str +"<STR_LIT>".format(lat=float(center_lat), lon=float(center_lon), d_m=int(<NUM_LIT:1000>*buffer_km))<EOL>select_all_trip_Is_where_stop_I_is_within_buffer_sql = "<STR_LIT>" + stops_within_buffer_query_sql + "<STR_LIT:)>"<EOL>trip_Is_to_remove_sql = "<STR_LIT>" + select_all_trip_Is_where_stop_I_is_within_buffer_sql + "<STR_LIT:)>"<EOL>trip_Is_to_remove = pandas.read_sql(trip_Is_to_remove_sql, db_conn)["<STR_LIT>"].values<EOL>trip_Is_to_remove_string = "<STR_LIT:U+002C>".join([str(trip_I) for trip_I in trip_Is_to_remove])<EOL>remove_all_trips_fully_outside_buffer_sql = "<STR_LIT>" + trip_Is_to_remove_string + "<STR_LIT:)>"<EOL>remove_all_stop_times_where_trip_I_fully_outside_buffer_sql = "<STR_LIT>" + trip_Is_to_remove_string  + "<STR_LIT:)>"<EOL>db_conn.execute(remove_all_trips_fully_outside_buffer_sql)<EOL>db_conn.execute(remove_all_stop_times_where_trip_I_fully_outside_buffer_sql)<EOL>delete_stops_not_in_stop_times_and_not_as_parent_stop(db_conn)<EOL>db_conn.execute(DELETE_ROUTES_NOT_PRESENT_IN_TRIPS_SQL)<EOL>db_conn.execute(DELETE_SHAPES_NOT_REFERENCED_IN_TRIPS_SQL)<EOL>db_conn.execute(DELETE_DAYS_ENTRIES_NOT_PRESENT_IN_TRIPS_SQL)<EOL>db_conn.execute(DELETE_DAY_TRIPS2_ENTRIES_NOT_PRESENT_IN_TRIPS_SQL)<EOL>db_conn.execute(DELETE_CALENDAR_ENTRIES_FOR_NON_REFERENCE_SERVICE_IS_SQL)<EOL>db_conn.execute(DELETE_CALENDAR_DATES_ENTRIES_FOR_NON_REFERENCE_SERVICE_IS_SQL)<EOL>db_conn.execute(DELETE_FREQUENCIES_ENTRIES_NOT_PRESENT_IN_TRIPS)<EOL>db_conn.execute(DELETE_AGENCIES_NOT_REFERENCED_IN_ROUTES_SQL)<EOL>if update_secondary_data:<EOL><INDENT>update_secondary_data_copies(db_conn)<EOL><DEDENT>

Not used in the regular filter process for the time being.

Parameters
----------
db_conn: sqlite3.Connection
    connection to the GTFS object
center_lat: float
center_lon: float
buffer_km: float


db_conn.execute(DELETE_SHAPES_NOT_REFERENCED_IN_TRIPS_SQL)<EOL>SELECT_MIN_MAX_SHAPE_BREAKS_BY_TRIP_I_SQL ="<STR_LIT>"<EOL>trip_min_max_shape_seqs= pandas.read_sql(SELECT_MIN_MAX_SHAPE_BREAKS_BY_TRIP_I_SQL, db_conn)<EOL>rows = []<EOL>for row in trip_min_max_shape_seqs.itertuples():<EOL><INDENT>shape_id, min_shape_break, max_shape_break = row.shape_id, row.min_shape_break, row.max_shape_break<EOL>if min_shape_break is None or max_shape_break is None:<EOL><INDENT>min_shape_break = float('<STR_LIT>')<EOL>max_shape_break = float('<STR_LIT>')<EOL><DEDENT>rows.append( (shape_id, min_shape_break, max_shape_break) )<EOL><DEDENT>DELETE_SQL_BASE = "<STR_LIT>"<EOL>db_conn.executemany(DELETE_SQL_BASE, rows)<EOL>remove_dangling_shapes_references(db_conn)<EOL>

Remove dangling entries from the shapes directory.

Parameters
----------
db_conn: sqlite3.Connection
    connection to the GTFS object


if start_date and end_date:<EOL><INDENT>if isinstance(start_date, (datetime.datetime, datetime.date)):<EOL><INDENT>self.start_date = start_date.strftime("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self.start_date = start_date<EOL><DEDENT>if isinstance(end_date, (datetime.datetime, datetime.date)):<EOL><INDENT>end_date_dt = end_date<EOL>self.end_date = end_date.strftime("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self.end_date = end_date<EOL>end_date_dt = datetime.datetime.strptime(self.end_date, "<STR_LIT>")<EOL><DEDENT>end_date_to_include = end_date_dt - datetime.timedelta(days=<NUM_LIT:1>)<EOL>self.end_date_to_include_str = end_date_to_include.strftime("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self.start_date = None<EOL>self.end_date = None<EOL><DEDENT>self.copy_db_conn = None<EOL>self.copy_db_path = copy_db_path<EOL>self.agency_ids_to_preserve = agency_ids_to_preserve<EOL>self.gtfs = G<EOL>self.buffer_lat = buffer_lat<EOL>self.buffer_lon = buffer_lon<EOL>self.buffer_distance_km = buffer_distance_km<EOL>self.update_metadata = update_metadata<EOL>if agency_distance is not None:<EOL><INDENT>raise NotImplementedError<EOL><DEDENT>self.this_db_path = self.gtfs.get_main_database_path()<EOL>assert os.path.exists(self.this_db_path), "<STR_LIT>"<EOL>assert os.path.exists(os.path.dirname(os.path.abspath(copy_db_path))),"<STR_LIT>"<EOL>assert not os.path.exists(copy_db_path), "<STR_LIT>" % copy_db_path<EOL>

Copy a database, and then based on various filters.
Only method `create_filtered_copy` is provided as we do not want to take the risk of
losing the data stored in the original database.

G: gtfspy.gtfs.GTFS
    the original database
copy_db_path : str
    path to another database database
update_metadata : boolean, optional
    whether to update metadata of the feed, defaulting to true
    (this option is mainly available for testing purposes)
start_date : str, or datetime.datetime
    filter out all data taking place before end_date (the start_time_ut of the end date)
    Date format "YYYY-MM-DD"
    (end_date_ut is not included after filtering)
end_date : str, or datetime.datetime
    Filter out all data taking place after end_date
    The end_date is not included after filtering.
agency_ids_to_preserve : iterable
    List of agency_ids to retain (str) (e.g. 'HSL' for Helsinki)
    Only routes by the listed agencies are then considered
agency_distance : float
    Only evaluated in combination with agency filter.
    Distance (in km) to the other near-by stops that should be included in addition to
    the ones defined by the agencies.
    All vehicle trips going through at least two such stops would then be included in the
    export. Note that this should not be a recursive thing.
    Or should it be? :)
buffer_lat : float
    Latitude of the buffer zone center
buffer_lon : float
    Longitude of the buffer zone center
buffer_distance : float
    Distance from the buffer zone center (in kilometers)

Returns
-------
None


<EOL>if (self.start_date is not None) and (self.end_date is not None):<EOL><INDENT>start_date_ut = self.gtfs.get_day_start_ut(self.start_date)<EOL>end_date_ut = self.gtfs.get_day_start_ut(self.end_date)<EOL>if self.copy_db_conn.execute("<STR_LIT>"<EOL>"<STR_LIT>").fetchone() != (<NUM_LIT:0>,):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>logging.info("<STR_LIT>")<EOL>table_to_preserve_map = {<EOL>"<STR_LIT>": "<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>",<EOL>"<STR_LIT>": "<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>",<EOL>"<STR_LIT>": '<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>"<STR_LIT>": "<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>}<EOL>table_to_remove_map = {key: "<STR_LIT>" + to_preserve + "<STR_LIT>"<EOL>for key, to_preserve in table_to_preserve_map.items() }<EOL>GTFS(self.copy_db_conn).set_current_process_time_zone()<EOL>for table, query_template in table_to_remove_map.items():<EOL><INDENT>param_dict = {"<STR_LIT>": str(start_date_ut),<EOL>"<STR_LIT>": str(end_date_ut)}<EOL>query = "<STR_LIT>" + table + "<STR_LIT:U+0020>" +query_template.format(**param_dict)<EOL>self.copy_db_conn.execute(query)<EOL>self.copy_db_conn.commit()<EOL><DEDENT>return FILTERED<EOL><DEDENT>else:<EOL><INDENT>return NOT_FILTERED<EOL><DEDENT>

Removes rows from the sqlite database copy that are out of the time span defined by start_date and end_date
:param gtfs: GTFS object
:param copy_db_conn: sqlite database connection
:param start_date:
:param end_date:
:return:


update calendar table's services
:param copy_db_conn:
:param start_date:
:param end_date:
:return:


if self.agency_ids_to_preserve is not None:<EOL><INDENT>logging.info("<STR_LIT>")<EOL>agency_ids_to_preserve = list(self.agency_ids_to_preserve)<EOL>agencies = pandas.read_sql("<STR_LIT>", self.copy_db_conn)<EOL>agencies_to_remove = []<EOL>for idx, row in agencies.iterrows():<EOL><INDENT>if row['<STR_LIT>'] not in agency_ids_to_preserve:<EOL><INDENT>agencies_to_remove.append(row['<STR_LIT>'])<EOL><DEDENT><DEDENT>for agency_id in agencies_to_remove:<EOL><INDENT>self.copy_db_conn.execute('<STR_LIT>', (agency_id,))<EOL><DEDENT>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.commit()<EOL>return FILTERED<EOL><DEDENT>else:<EOL><INDENT>return NOT_FILTERED<EOL><DEDENT>

filter by agency ids
:param copy_db_conn:
:param agency_ids_to_preserve:
:return:


if self.buffer_lat is None or self.buffer_lon is None or self.buffer_distance_km is None:<EOL><INDENT>return NOT_FILTERED<EOL><DEDENT>print("<STR_LIT>" + str(self.buffer_lat) +<EOL>"<STR_LIT>" + str(self.buffer_lon) +<EOL>"<STR_LIT>" + str(self.buffer_distance_km))<EOL>remove_all_trips_fully_outside_buffer(self.copy_db_conn,<EOL>self.buffer_lat,<EOL>self.buffer_lon,<EOL>self.buffer_distance_km,<EOL>update_secondary_data=False)<EOL>logging.info("<STR_LIT>")<EOL>find_distance_func_name = add_wgs84_distance_function_to_db(self.copy_db_conn)<EOL>assert find_distance_func_name == "<STR_LIT>"<EOL>stop_distance_filter_sql_base = (<EOL>"<STR_LIT>" +<EOL>"<STR_LIT>" +<EOL>"<STR_LIT>"<EOL>)<EOL>stops_within_buffer_sql = stop_distance_filter_sql_base.format(<EOL>buffer_lat=float(self.buffer_lat),<EOL>buffer_lon=float(self.buffer_lon),<EOL>buffer_distance_meters=int(self.buffer_distance_km * <NUM_LIT:1000>)<EOL>)<EOL>stops_within_buffer = set(row[<NUM_LIT:0>] for row in self.copy_db_conn.execute(stops_within_buffer_sql))<EOL>stops_within_buffer_string = "<STR_LIT:(>" +"<STR_LIT:U+002C>".join(str(stop_I) for stop_I in stops_within_buffer) +  "<STR_LIT:)>"<EOL>trip_min_max_include_seq_sql =  (<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>).format(stop_I_list=stops_within_buffer_string)<EOL>trip_I_min_seq_max_seq_df = pandas.read_sql(trip_min_max_include_seq_sql, self.copy_db_conn)<EOL>for trip_I_seq_row in trip_I_min_seq_max_seq_df.itertuples():<EOL><INDENT>trip_I = trip_I_seq_row.trip_I<EOL>min_seq = trip_I_seq_row.min_seq<EOL>max_seq = trip_I_seq_row.max_seq<EOL>if min_seq == max_seq:<EOL><INDENT>self.copy_db_conn.execute("<STR_LIT>".format(trip_I=trip_I))<EOL>self.copy_db_conn.execute("<STR_LIT>".format(trip_I=trip_I))<EOL><DEDENT>else:<EOL><INDENT>DELETE_STOP_TIME_ENTRIES_SQL ="<STR_LIT>".format(trip_I=trip_I, max_seq=max_seq, min_seq=min_seq)<EOL>self.copy_db_conn.execute(DELETE_STOP_TIME_ENTRIES_SQL)<EOL>STOPS_NOT_WITHIN_BUFFER__FOR_TRIP_SQL ="<STR_LIT>".format(stops_within_hard_buffer=stops_within_buffer_string, trip_I=trip_I)<EOL>stop_times_within_buffer_df = pandas.read_sql(STOPS_NOT_WITHIN_BUFFER__FOR_TRIP_SQL, self.copy_db_conn)<EOL>if stop_times_within_buffer_df['<STR_LIT>'].all():<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>_split_trip(self.copy_db_conn, trip_I, stop_times_within_buffer_df)<EOL><DEDENT><DEDENT><DEDENT>SHAPE_IDS_NOT_WITHIN_BUFFER_SQL ="<STR_LIT>""<STR_LIT>".format(buffer_lat=self.buffer_lat,<EOL>buffer_lon=self.buffer_lon,<EOL>buffer_distance_meters=self.buffer_distance_km * <NUM_LIT:1000>)<EOL>DELETE_ALL_SHAPE_IDS_NOT_WITHIN_BUFFER_SQL = "<STR_LIT>"+ SHAPE_IDS_NOT_WITHIN_BUFFER_SQL + "<STR_LIT:)>"<EOL>self.copy_db_conn.execute(DELETE_ALL_SHAPE_IDS_NOT_WITHIN_BUFFER_SQL)<EOL>SET_SHAPE_ID_TO_NULL_FOR_HARD_BUFFER_FILTERED_SHAPE_IDS ="<STR_LIT>" + SHAPE_IDS_NOT_WITHIN_BUFFER_SQL + "<STR_LIT:)>"<EOL>self.copy_db_conn.execute(SET_SHAPE_ID_TO_NULL_FOR_HARD_BUFFER_FILTERED_SHAPE_IDS)<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.copy_db_conn.execute('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>delete_stops_not_in_stop_times_and_not_as_parent_stop(self.copy_db_conn)<EOL>self.copy_db_conn.execute(DELETE_TRIPS_NOT_REFERENCED_IN_STOP_TIMES)<EOL>self.copy_db_conn.execute(DELETE_ROUTES_NOT_PRESENT_IN_TRIPS_SQL)<EOL>self.copy_db_conn.execute(DELETE_AGENCIES_NOT_REFERENCED_IN_ROUTES_SQL)<EOL>self.copy_db_conn.execute(DELETE_SHAPES_NOT_REFERENCED_IN_TRIPS_SQL)<EOL>self.copy_db_conn.execute(DELETE_STOP_DISTANCE_ENTRIES_WITH_NONEXISTENT_STOPS_SQL)<EOL>self.copy_db_conn.execute(DELETE_FREQUENCIES_ENTRIES_NOT_PRESENT_IN_TRIPS)<EOL>remove_dangling_shapes(self.copy_db_conn)<EOL>self.copy_db_conn.commit()<EOL>return FILTERED<EOL>

Filter the feed based on self.buffer_distance_km from self.buffer_lon and self.buffer_lat.

1. First include all stops that are within self.buffer_distance_km from self.buffer_lon and self.buffer_lat.
2. Then include all intermediate stops that are between any of the included stop pairs with some PT trip.
3. Repeat step 2 until no more stops are to be included.

As a summary this process should get rid of PT network tendrils, but should preserve the PT network intact
at its core.


stats = get_stats(gtfs)<EOL>lon_min = stats['<STR_LIT>']<EOL>lon_max = stats['<STR_LIT>']<EOL>lat_min = stats['<STR_LIT>']<EOL>lat_max = stats['<STR_LIT>']<EOL>if as_dict:<EOL><INDENT>return {'<STR_LIT>': lon_min, '<STR_LIT>': lon_max, '<STR_LIT>': lat_min, '<STR_LIT>': lat_max}<EOL><DEDENT>else:<EOL><INDENT>return lon_min, lon_max, lat_min, lat_max<EOL><DEDENT>

Parameters
----------
gtfs

Returns
-------
min_lon: float
max_lon: float
min_lat: float
max_lat: float


stops = gtfs.get_table("<STR_LIT>")<EOL>median_lat = numpy.percentile(stops['<STR_LIT>'].values, <NUM_LIT:50>)<EOL>median_lon = numpy.percentile(stops['<STR_LIT>'].values, <NUM_LIT:50>)<EOL>return median_lat, median_lon<EOL>

Get median latitude AND longitude of stops

Parameters
----------
gtfs: GTFS

Returns
-------
median_lat : float
median_lon : float


stops = gtfs.get_table("<STR_LIT>")<EOL>mean_lat = numpy.mean(stops['<STR_LIT>'].values)<EOL>mean_lon = numpy.mean(stops['<STR_LIT>'].values)<EOL>return mean_lat, mean_lon<EOL>

Get mean latitude AND longitude of stops

Parameters
----------
gtfs: GTFS

Returns
-------
mean_lat : float
mean_lon : float


def write_stats_as_csv(gtfs, path_to_csv, re_write=False):

stats_dict = get_stats(gtfs)<EOL>if re_write:<EOL><INDENT>os.remove(path_to_csv)<EOL>

Writes data from get_stats to csv file

Parameters
----------
gtfs: GTFS
path_to_csv: str
    filepath to the csv file to be generated
re_write:
    insted of appending, create a new one.


stats = {}<EOL>for table in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>stats["<STR_LIT>" + table] = gtfs.get_row_count(table)<EOL><DEDENT>agencies = gtfs.get_table("<STR_LIT>")<EOL>stats["<STR_LIT>"] = "<STR_LIT:_>".join(agencies['<STR_LIT:name>'].values)<EOL>stops = gtfs.get_table("<STR_LIT>")<EOL>lats = stops['<STR_LIT>'].values<EOL>lons = stops['<STR_LIT>'].values<EOL>percentiles = [<NUM_LIT:0>, <NUM_LIT:10>, <NUM_LIT:50>, <NUM_LIT>, <NUM_LIT:100>]<EOL>try:<EOL><INDENT>lat_percentiles = numpy.percentile(lats, percentiles)<EOL><DEDENT>except IndexError:<EOL><INDENT>lat_percentiles = [None] * <NUM_LIT:5><EOL><DEDENT>lat_min, lat_10, lat_median, lat_90, lat_max = lat_percentiles<EOL>stats["<STR_LIT>"] = lat_min<EOL>stats["<STR_LIT>"] = lat_10<EOL>stats["<STR_LIT>"] = lat_median<EOL>stats["<STR_LIT>"] = lat_90<EOL>stats["<STR_LIT>"] = lat_max<EOL>try:<EOL><INDENT>lon_percentiles = numpy.percentile(lons, percentiles)<EOL><DEDENT>except IndexError:<EOL><INDENT>lon_percentiles = [None] * <NUM_LIT:5><EOL><DEDENT>lon_min, lon_10, lon_median, lon_90, lon_max = lon_percentiles<EOL>stats["<STR_LIT>"] = lon_min<EOL>stats["<STR_LIT>"] = lon_10<EOL>stats["<STR_LIT>"] = lon_median<EOL>stats["<STR_LIT>"] = lon_90<EOL>stats["<STR_LIT>"] = lon_max<EOL>if len(lats) > <NUM_LIT:0>:<EOL><INDENT>stats["<STR_LIT>"] = wgs84_distance(lat_min, lon_median, lat_max, lon_median) / <NUM_LIT><EOL>stats["<STR_LIT>"] = wgs84_distance(lon_min, lat_median, lon_max, lat_median) / <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>stats["<STR_LIT>"] = None<EOL>stats["<STR_LIT>"] = None<EOL><DEDENT>first_day_start_ut, last_day_start_ut = gtfs.get_day_start_ut_span()<EOL>stats["<STR_LIT>"] = first_day_start_ut<EOL>if last_day_start_ut is None:<EOL><INDENT>stats["<STR_LIT>"] = None<EOL><DEDENT>else:<EOL><INDENT>stats["<STR_LIT>"] = last_day_start_ut + <NUM_LIT> * <NUM_LIT><EOL><DEDENT>stats["<STR_LIT>"] = gtfs.get_min_date()<EOL>stats["<STR_LIT>"] = gtfs.get_max_date()<EOL>max_activity_date = gtfs.execute_custom_query(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>').fetchone()<EOL>if max_activity_date:<EOL><INDENT>stats["<STR_LIT>"] = max_activity_date[<NUM_LIT:1>]<EOL>max_activity_hour = gtfs.get_cursor().execute(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>', (stats["<STR_LIT>"],)).fetchone()<EOL>if max_activity_hour:<EOL><INDENT>stats["<STR_LIT>"] = max_activity_hour[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>stats["<STR_LIT>"] = None<EOL><DEDENT><DEDENT>if max_activity_date and max_activity_hour:<EOL><INDENT>fleet_size_estimates = _fleet_size_estimate(gtfs, stats['<STR_LIT>'], stats['<STR_LIT>'])<EOL>stats.update(fleet_size_estimates)<EOL><DEDENT>stats['<STR_LIT>'] = _distribution(gtfs, '<STR_LIT>', '<STR_LIT:type>')<EOL>stats['<STR_LIT>'] = _distribution(gtfs, '<STR_LIT>', '<STR_LIT>')<EOL>stats['<STR_LIT>'] = _distribution(gtfs, '<STR_LIT>', '<STR_LIT>')<EOL>stats['<STR_LIT>'] = _distribution(gtfs, '<STR_LIT>', '<STR_LIT>')<EOL>stats['<STR_LIT>'] = _distribution(gtfs, '<STR_LIT>', '<STR_LIT>')<EOL>stats['<STR_LIT>'] = _distribution(gtfs, '<STR_LIT>', '<STR_LIT>')<EOL>stats = _feed_calendar_span(gtfs, stats)<EOL>return stats<EOL>

Get basic statistics of the GTFS data.

Parameters
----------
gtfs: GTFS

Returns
-------
stats: dict
    A dictionary of various statistics.
    Keys should be strings, values should be inputtable to a database (int, date, str, ...)
    (but not a list)


cur = gtfs.conn.cursor()<EOL>cur.execute('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(column=column, table=table))<EOL>return '<STR_LIT:U+0020>'.join('<STR_LIT>' % (t, c) for t, c in cur)<EOL>

Count occurrences of values AND return it as a string.

    Example return value:   '1:5 2:15


def _fleet_size_estimate(gtfs, hour, date):

results = {}<EOL>fleet_size_list = []<EOL>cur = gtfs.conn.cursor()<EOL>rows = cur.execute(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT:(>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT:(>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>', (hour, hour, date, date))<EOL>for row in rows:<EOL><INDENT>fleet_size_list.append(str(row[<NUM_LIT:0>]) + '<STR_LIT::>' + str(row[<NUM_LIT:1>]))<EOL><DEDENT>results['<STR_LIT>'] = "<STR_LIT:U+0020>".join(fleet_size_list)<EOL>fleet_size_list = []<EOL>fleet_size_dict = {}<EOL>if hour:<EOL><INDENT>for minute in range(hour * <NUM_LIT>, (hour + <NUM_LIT:1>) * <NUM_LIT>, <NUM_LIT>):<EOL><INDENT>rows = gtfs.conn.cursor().execute(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>(minute, minute, date))<EOL>for row in rows:<EOL><INDENT>if fleet_size_dict.get(row[<NUM_LIT:0>], <NUM_LIT:0>) < row[<NUM_LIT:1>]:<EOL><INDENT>fleet_size_dict[row[<NUM_LIT:0>]] = row[<NUM_LIT:1>]<EOL><DEDENT><DEDENT><DEDENT><DEDENT>for key in fleet_size_dict.keys():<EOL><INDENT>fleet_size_list.append(str(key) + '<STR_LIT::>' + str(fleet_size_dict[key]))<EOL><DEDENT>results["<STR_LIT>"] = '<STR_LIT:U+0020>'.join(fleet_size_list)<EOL>return results<EOL>

Calculates fleet size estimates by two separate formula:
 1. Considering all routes separately with no interlining and doing a deficit calculation at every terminal
 2. By looking at the maximum number of vehicles in simultaneous movement

Parameters
----------
gtfs: GTFS
hour: int
date: ?

Returns
-------
results: dict
    a dict with keys:
        fleet_size_route_based
        fleet_size_max_movement


n_feeds = _n_gtfs_sources(gtfs)[<NUM_LIT:0>]<EOL>max_start = None<EOL>min_end = None<EOL>if n_feeds > <NUM_LIT:1>:<EOL><INDENT>for i in range(n_feeds):<EOL><INDENT>feed_key = "<STR_LIT>" + str(i) + "<STR_LIT:_>"<EOL>start_key = feed_key + "<STR_LIT>"<EOL>end_key = feed_key + "<STR_LIT>"<EOL>calendar_span = gtfs.conn.cursor().execute(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>', (feed_key + '<STR_LIT:%>',)).fetchone()<EOL>stats[start_key] = calendar_span[<NUM_LIT:0>]<EOL>stats[end_key] = calendar_span[<NUM_LIT:1>]<EOL>if calendar_span[<NUM_LIT:0>] is not None and calendar_span[<NUM_LIT:1>] is not None:<EOL><INDENT>if not max_start and not min_end:<EOL><INDENT>max_start = calendar_span[<NUM_LIT:0>]<EOL>min_end = calendar_span[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>if gtfs.get_day_start_ut(calendar_span[<NUM_LIT:0>]) > gtfs.get_day_start_ut(max_start):<EOL><INDENT>max_start = calendar_span[<NUM_LIT:0>]<EOL><DEDENT>if gtfs.get_day_start_ut(calendar_span[<NUM_LIT:1>]) < gtfs.get_day_start_ut(min_end):<EOL><INDENT>min_end = calendar_span[<NUM_LIT:1>]<EOL><DEDENT><DEDENT><DEDENT><DEDENT>stats["<STR_LIT>"] = max_start<EOL>stats["<STR_LIT>"] = min_end<EOL><DEDENT>else:<EOL><INDENT>stats["<STR_LIT>"] = stats["<STR_LIT>"]<EOL>stats["<STR_LIT>"] = stats["<STR_LIT>"]<EOL><DEDENT>return stats<EOL>

Computes the temporal coverage of each source feed

Parameters
----------
gtfs: gtfspy.GTFS object
stats: dict
    where to append the stats

Returns
-------
stats: dict


stats = get_stats(gtfs)<EOL>gtfs.update_stats(stats)<EOL>

Computes stats AND stores them into the underlying gtfs object (i.e. database).

Parameters
----------
gtfs: GTFS


conn = gtfs.conn<EOL>conn.create_function("<STR_LIT>", <NUM_LIT:4>, wgs84_distance)<EOL>cur = conn.cursor()<EOL>query = '<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>'<EOL>q_result = pd.read_sql_query(query, conn)<EOL>q_result['<STR_LIT>'] = <NUM_LIT> * q_result['<STR_LIT>'] / q_result['<STR_LIT>']<EOL>q_result['<STR_LIT>'] = q_result['<STR_LIT>'] / <NUM_LIT:1000><EOL>q_result['<STR_LIT>'] = q_result['<STR_LIT>'] / <NUM_LIT><EOL>q_result = q_result.loc[q_result['<STR_LIT>'] != float("<STR_LIT>")]<EOL>if results_by_mode:<EOL><INDENT>q_results = {}<EOL>for type in q_result['<STR_LIT:type>'].unique().tolist():<EOL><INDENT>q_results[type] = q_result.loc[q_result['<STR_LIT:type>'] == type]<EOL><DEDENT>return q_results<EOL><DEDENT>else:<EOL><INDENT>return q_result<EOL><DEDENT>

Parameters
----------
gtfs: GTFS
results_by_mode: bool

Returns
-------
if results_by_mode is False:
    q_result: pandas.DataFrame
if results_by_mode is True:
    q_results: dict
        a dict with the following keys:
            [ADD HERE]


Return the frequency of all types of routes per day.

Parameters
-----------
gtfs: GTFS

Returns
-------
pandas.DataFrame with columns
    route_I, type, frequency


def hourly_frequencies(gtfs, st, et, route_type):

timeframe = et-st<EOL>hours = timeframe/ <NUM_LIT><EOL>day = gtfs.get_suitable_date_for_daily_extract()<EOL>stops = gtfs.get_stops_for_route_type(route_type).T.drop_duplicates().T<EOL>query = ("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(h=hours, st=st, et=et, day=day))<EOL>try:<EOL><INDENT>trips_frequency = gtfs.execute_custom_query_pandas(query).T.drop_duplicates().T<EOL>df = pd.merge(stops[['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']], trips_frequency[['<STR_LIT>', '<STR_LIT>']],<EOL>on='<STR_LIT>', how='<STR_LIT>')<EOL>return df.apply(pd.to_numeric)<EOL><DEDENT>except:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>

Return all the number of vehicles (i.e. busses,trams,etc) that pass hourly through a stop in a time frame.

Parameters
----------
gtfs: GTFS
st : int
    start time of the time framein unix time
et : int
    end time of the time frame in unix time
route_type: int

Returns
-------
numeric pandas.DataFrame with columns
    stop_I, lat, lon, frequency


day = gtfs.get_suitable_date_for_daily_extract()<EOL>query = ("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(day=day, route_type=route_type))<EOL>df = gtfs.execute_custom_query_pandas(query)<EOL>return df['<STR_LIT>'].item()<EOL>

Return the sum of vehicle hours in a particular day by route type.


query = (<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return(gtfs.execute_custom_query_pandas(query))<EOL>

Get the frequency of trip_I in a particular day


if isinstance(fname_or_conn, string_types):<EOL><INDENT>if os.path.isfile(fname_or_conn):<EOL><INDENT>self.conn = sqlite3.connect(fname_or_conn)<EOL>self.fname = fname_or_conn<EOL>self.conn.execute('<STR_LIT>')<EOL>self.conn.execute('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise FileNotFoundError("<STR_LIT>" + fname_or_conn + "<STR_LIT>")<EOL><DEDENT><DEDENT>elif isinstance(fname_or_conn, sqlite3.Connection):<EOL><INDENT>self.conn = fname_or_conn<EOL>self._dont_close = True<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError(<EOL>"<STR_LIT>" + str(type(fname_or_conn)) + "<STR_LIT>")<EOL><DEDENT>assert self.conn.execute("<STR_LIT>").fetchone() is not None<EOL>self.meta = GTFSMetadata(self.conn)<EOL>self.conn.create_function("<STR_LIT>", <NUM_LIT:4>, wgs84_distance)<EOL>self._timezone = pytz.timezone(self.get_timezone_name())<EOL>

Open a GTFS object

        Parameters
        ----------
        fname_or_conn: str | sqlite3.Connection
            path to the preprocessed gtfs database or a connection to a gtfs database


<EOL>from gtfspy.import_gtfs import import_gtfs<EOL>conn = sqlite3.connect("<STR_LIT>")<EOL>import_gtfs(gtfs_directory,<EOL>conn,<EOL>preserve_connection=True,<EOL>print_progress=False)<EOL>return cls(conn)<EOL>

Instantiate a GTFS object by computing

Parameters
----------
gtfs_directory: str
    path to the directory for importing the database


cur = self.conn.cursor()<EOL>cur.execute("<STR_LIT>")<EOL>rows = cur.fetchall()<EOL>for row in rows:<EOL><INDENT>if row[<NUM_LIT:1>] == str("<STR_LIT>"):<EOL><INDENT>return row[<NUM_LIT:2>]<EOL><DEDENT><DEDENT>

Should return the path to the database

Returns
-------
path : unicode
    path to the database, empty string for in-memory databases


def get_shape_distance_between_stops(self, trip_I, from_stop_seq, to_stop_seq):

query_template = "<STR_LIT>"<EOL>stop_seqs = [from_stop_seq, to_stop_seq]<EOL>shape_breaks = []<EOL>for seq in stop_seqs:<EOL><INDENT>q = query_template.format(seq=seq, trip_I=trip_I)<EOL>shape_breaks.append(self.conn.execute(q).fetchone())<EOL><DEDENT>query_template = "<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL>distance_query = query_template.format(trip_I=trip_I, from_stop_seq=from_stop_seq, to_stop_seq=to_stop_seq)<EOL>return self.conn.execute(distance_query).fetchone()[<NUM_LIT:0>]<EOL>

Get the distance along a shape between stops

Parameters
----------
trip_I : int
    trip_ID along which we travel
from_stop_seq : int
    the sequence number of the 'origin' stop
to_stop_seq : int
    the sequence number of the 'destination' stop

Returns
-------
distance : float, None
    If the shape calculation succeeded, return a float, otherwise return None
    (i.e. in the case where the shapes table is empty)


def get_directly_accessible_stops_within_distance(self, stop, distance):

query = """<STR_LIT>""" % (stop, distance)<EOL>return pd.read_sql_query(query, self.conn)<EOL>

Returns stops that are accessible without transfer from the stops that are within a specific walking distance
:param stop: int
:param distance: int
:return:


return self.conn.cursor()<EOL>

Return a cursor to the underlying sqlite3 object


return pd.read_sql("<STR_LIT>" + table_name, self.conn)<EOL>

Return a pandas.DataFrame object corresponding to the sql table

Parameters
----------
table_name: str
    name of the table in the database

Returns
-------
df : pandas.DataFrame


return self.conn.cursor().execute("<STR_LIT>" + table).fetchone()[<NUM_LIT:0>]<EOL>


return list(pd.read_sql("<STR_LIT>", self.conn)["<STR_LIT:name>"])<EOL>

Return a list of the underlying tables in the database.

Returns
-------
table_names: list[str]


TZ = self.conn.execute('<STR_LIT>').fetchall()[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>return set_process_timezone(TZ)<EOL>

This function queries a GTFS connection, finds the timezone of this
database, and sets it in the TZ environment variable.  This is a
process-global configuration, by the nature of the C library!

Returns
-------
None

Alters os.environ['TZ']


tz_name = self.conn.execute('<STR_LIT>').fetchone()<EOL>if tz_name is None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return tz_name[<NUM_LIT:0>]<EOL>

Get name of the GTFS timezone

Returns
-------
timezone_name : str
    name of the time zone, e.g. "Europe/Helsinki"


if dt is None:<EOL><INDENT>download_date = self.meta.get('<STR_LIT>')<EOL>if download_date:<EOL><INDENT>dt = datetime.datetime.strptime(download_date, '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>dt = datetime.datetime.today()<EOL><DEDENT><DEDENT>loc_dt = self._timezone.localize(dt)<EOL>timezone_string = loc_dt.strftime("<STR_LIT>")<EOL>return timezone_string<EOL>

Return the timezone of the GTFS database object as a string.
The assumed time when the timezone (difference) is computed
is the download date of the file.
This might not be optimal in all cases.

So this function should return values like:
    "+0200" or "-1100"

Parameters
----------
dt : datetime.datetime, optional
    The (unlocalized) date when the timezone should be computed.
    Defaults first to download_date, and then to the runtime date.

Returns
-------
timezone_string : str


return datetime.datetime.fromtimestamp(unixtime, self._timezone)<EOL>

Convert unixtime to localized datetime

Parameters
----------
unixtime : int

Returns
-------
gtfs_datetime: datetime.datetime
    time localized to gtfs_datetime's timezone


loc_dt = self._timezone.localize(unlocalized_datetime)<EOL>unixtime_seconds = calendar.timegm(loc_dt.utctimetuple())<EOL>return unixtime_seconds<EOL>

Convert datetime (in GTFS timezone) to unixtime

Parameters
----------
unlocalized_datetime : datetime.datetime
    (tz coerced to GTFS timezone, should NOT be UTC.)

Returns
-------
output : int (unixtime)


if isinstance(date, string_types):<EOL><INDENT>date = datetime.datetime.strptime(date, '<STR_LIT>')<EOL><DEDENT>date_noon = datetime.datetime(date.year, date.month, date.day, <NUM_LIT:12>, <NUM_LIT:0>, <NUM_LIT:0>)<EOL>ut_noon = self.unlocalized_datetime_to_ut_seconds(date_noon)<EOL>return ut_noon - <NUM_LIT:12> * <NUM_LIT> * <NUM_LIT><EOL>

Get day start time (as specified by GTFS) as unix time in seconds

Parameters
----------
date : str | unicode | datetime.datetime
    something describing the date

Returns
-------
day_start_ut : int
    start time of the day in unixtime


def get_trip_trajectories_within_timespan(self, start, end, use_shapes=True, filter_name=None):

trips = []<EOL>trip_df = self.get_tripIs_active_in_range(start, end)<EOL>print("<STR_LIT>" + str(len(trip_df)) + "<STR_LIT>")<EOL>shape_cache = {}<EOL>for row in trip_df.itertuples():<EOL><INDENT>trip_I = row.trip_I<EOL>day_start_ut = row.day_start_ut<EOL>shape_id = row.shape_id<EOL>trip = {}<EOL>name, route_type = self.get_route_name_and_type_of_tripI(trip_I)<EOL>trip['<STR_LIT>'] = int(route_type)<EOL>trip['<STR_LIT:name>'] = str(name)<EOL>if filter_name and (name != filter_name):<EOL><INDENT>continue<EOL><DEDENT>stop_lats = []<EOL>stop_lons = []<EOL>stop_dep_times = []<EOL>shape_breaks = []<EOL>stop_seqs = []<EOL>stop_time_df = self.get_trip_stop_time_data(trip_I, day_start_ut)<EOL>for stop_row in stop_time_df.itertuples():<EOL><INDENT>stop_lats.append(float(stop_row.lat))<EOL>stop_lons.append(float(stop_row.lon))<EOL>stop_dep_times.append(float(stop_row.dep_time_ut))<EOL>try:<EOL><INDENT>stop_seqs.append(int(stop_row.seq))<EOL><DEDENT>except TypeError:<EOL><INDENT>stop_seqs.append(None)<EOL><DEDENT>if use_shapes:<EOL><INDENT>try:<EOL><INDENT>shape_breaks.append(int(stop_row.shape_break))<EOL><DEDENT>except (TypeError, ValueError):<EOL><INDENT>shape_breaks.append(None)<EOL><DEDENT><DEDENT><DEDENT>if use_shapes:<EOL><INDENT>if shape_id not in shape_cache:<EOL><INDENT>shape_cache[shape_id] = shapes.get_shape_points2(self.conn.cursor(), shape_id)<EOL><DEDENT>shape_data = shape_cache[shape_id]<EOL>try:<EOL><INDENT>trip['<STR_LIT>'] = shapes.interpolate_shape_times(shape_data['<STR_LIT:d>'], shape_breaks, stop_dep_times)<EOL>trip['<STR_LIT>'] = shape_data['<STR_LIT>']<EOL>trip['<STR_LIT>'] = shape_data['<STR_LIT>']<EOL>start_break = shape_breaks[<NUM_LIT:0>]<EOL>end_break = shape_breaks[-<NUM_LIT:1>]<EOL>trip['<STR_LIT>'] = trip['<STR_LIT>'][start_break:end_break + <NUM_LIT:1>]<EOL>trip['<STR_LIT>'] = trip['<STR_LIT>'][start_break:end_break + <NUM_LIT:1>]<EOL>trip['<STR_LIT>'] = trip['<STR_LIT>'][start_break:end_break + <NUM_LIT:1>]<EOL><DEDENT>except:<EOL><INDENT>trip['<STR_LIT>'] = stop_dep_times<EOL>trip['<STR_LIT>'] = stop_lats<EOL>trip['<STR_LIT>'] = stop_lons<EOL><DEDENT><DEDENT>else:<EOL><INDENT>trip['<STR_LIT>'] = stop_dep_times<EOL>trip['<STR_LIT>'] = stop_lats<EOL>trip['<STR_LIT>'] = stop_lons<EOL><DEDENT>trips.append(trip)<EOL><DEDENT>return {"<STR_LIT>": trips}<EOL>

Get complete trip data for visualizing public transport operation based on gtfs.

Parameters
----------
start: number
    Earliest position data to return (in unix time)
end: number
    Latest position data to return (in unix time)
use_shapes: bool, optional
    Whether or not shapes should be included
filter_name: str
    Pick only routes having this name.

Returns
-------
trips: dict
    trips['trips'] is a list whose each element (e.g. el = trips['trips'][0])
    is a dict with the following properties:
        el['lats'] -- list of latitudes
        el['lons'] -- list of longitudes
        el['times'] -- list of passage_times
        el['route_type'] -- type of vehicle as specified by GTFS
        el['name'] -- name of the route


def get_stop_count_data(self, start_ut, end_ut):

<EOL>trips_df = self.get_tripIs_active_in_range(start_ut, end_ut)<EOL>stop_counts = Counter()<EOL>for row in trips_df.itertuples():<EOL><INDENT>stops_seq = self.get_trip_stop_time_data(row.trip_I, row.day_start_ut)<EOL>for stop_time_row in stops_seq.itertuples(index=False):<EOL><INDENT>if (stop_time_row.dep_time_ut >= start_ut) and (stop_time_row.dep_time_ut <= end_ut):<EOL><INDENT>stop_counts[stop_time_row.stop_I] += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>all_stop_data = self.stops()<EOL>counts = [stop_counts[stop_I] for stop_I in all_stop_data["<STR_LIT>"].values]<EOL>all_stop_data.loc[:, "<STR_LIT:count>"] = pd.Series(counts, index=all_stop_data.index)<EOL>return all_stop_data<EOL>

Get stop count data.

Parameters
----------
start_ut : int
    start time in unixtime
end_ut : int
    end time in unixtime

Returns
-------
stopData : pandas.DataFrame
    each row in the stopData dataFrame is a dictionary with the following elements
        stop_I, count, lat, lon, name
    with data types
        (int, int, float, float, str)


def get_segment_count_data(self, start, end, use_shapes=True):

cur = self.conn.cursor()<EOL>trips_df = self.get_tripIs_active_in_range(start, end)<EOL>segment_counts = Counter()<EOL>seg_to_info = {}<EOL>tripI_to_seq = defaultdict(list)<EOL>for row in trips_df.itertuples():<EOL><INDENT>stops_df = self.get_trip_stop_time_data(row.trip_I, row.day_start_ut)<EOL>for i in range(len(stops_df) - <NUM_LIT:1>):<EOL><INDENT>(stop_I, dep_time_ut, s_lat, s_lon, s_seq, shape_break) = stops_df.iloc[i]<EOL>(stop_I_n, dep_time_ut_n, s_lat_n, s_lon_n, s_seq_n, shape_break_n) = stops_df.iloc[i + <NUM_LIT:1>]<EOL>if (dep_time_ut >= start) and (dep_time_ut_n <= end):<EOL><INDENT>seg = (stop_I, stop_I_n)<EOL>segment_counts[seg] += <NUM_LIT:1><EOL>if seg not in seg_to_info:<EOL><INDENT>seg_to_info[seg] = {<EOL>u"<STR_LIT>": row.trip_I,<EOL>u"<STR_LIT>": [s_lat, s_lat_n],<EOL>u"<STR_LIT>": [s_lon, s_lon_n],<EOL>u"<STR_LIT>": row.shape_id,<EOL>u"<STR_LIT>": [s_seq, s_seq_n],<EOL>u"<STR_LIT>": [shape_break, shape_break_n]<EOL>}<EOL>tripI_to_seq[row.trip_I].append(seg)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>stop_names = {}<EOL>for (stop_I, stop_J) in segment_counts.keys():<EOL><INDENT>for s in [stop_I, stop_J]:<EOL><INDENT>if s not in stop_names:<EOL><INDENT>stop_names[s] = self.stop(s)[u'<STR_LIT:name>'].values[<NUM_LIT:0>]<EOL><DEDENT><DEDENT><DEDENT>seg_data = []<EOL>for seg, count in segment_counts.items():<EOL><INDENT>segInfo = seg_to_info[seg]<EOL>shape_breaks = segInfo[u"<STR_LIT>"]<EOL>seg_el = {}<EOL>if use_shapes and shape_breaks and shape_breaks[<NUM_LIT:0>] and shape_breaks[<NUM_LIT:1>]:<EOL><INDENT>shape = shapes.get_shape_between_stops(<EOL>cur,<EOL>segInfo[u'<STR_LIT>'],<EOL>shape_breaks=shape_breaks<EOL>)<EOL>seg_el[u'<STR_LIT>'] = segInfo[u'<STR_LIT>'][:<NUM_LIT:1>] + shape[u'<STR_LIT>'] + segInfo[u'<STR_LIT>'][<NUM_LIT:1>:]<EOL>seg_el[u'<STR_LIT>'] = segInfo[u'<STR_LIT>'][:<NUM_LIT:1>] + shape[u'<STR_LIT>'] + segInfo[u'<STR_LIT>'][<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>seg_el[u'<STR_LIT>'] = segInfo[u'<STR_LIT>']<EOL>seg_el[u'<STR_LIT>'] = segInfo[u'<STR_LIT>']<EOL><DEDENT>seg_el[u'<STR_LIT:name>'] = stop_names[seg[<NUM_LIT:0>]] + u"<STR_LIT:->" + stop_names[seg[<NUM_LIT:1>]]<EOL>seg_el[u'<STR_LIT:count>'] = count<EOL>seg_data.append(seg_el)<EOL><DEDENT>return seg_data<EOL>

Get segment data including PTN vehicle counts per segment that are
fully _contained_ within the interval (start, end)

Parameters
----------
start : int
    start time of the simulation in unix time
end : int
    end time of the simulation in unix time
use_shapes : bool, optional
    whether to include shapes (if available)

Returns
-------
seg_data : list
    each element in the list is a dict containing keys:
        "trip_I", "lats", "lons", "shape_id", "stop_seqs", "shape_breaks"


cur = self.conn.cursor()<EOL>query = "<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>""<STR_LIT>"<EOL>data = pd.read_sql_query(query, self.conn)<EOL>routeShapes = []<EOL>for i, row in enumerate(data.itertuples()):<EOL><INDENT>datum = {"<STR_LIT:name>": str(row.name), "<STR_LIT:type>": int(row.type), "<STR_LIT>": row.route_I, "<STR_LIT>": str(row.agency_id),<EOL>"<STR_LIT>": str(row.agency_name)}<EOL>if use_shapes and row.shape_id:<EOL><INDENT>shape = shapes.get_shape_points2(cur, row.shape_id)<EOL>lats = shape['<STR_LIT>']<EOL>lons = shape['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>stop_shape = self.get_trip_stop_coordinates(row.trip_I)<EOL>lats = list(stop_shape['<STR_LIT>'])<EOL>lons = list(stop_shape['<STR_LIT>'])<EOL><DEDENT>datum['<STR_LIT>'] = [float(lat) for lat in lats]<EOL>datum['<STR_LIT>'] = [float(lon) for lon in lons]<EOL>routeShapes.append(datum)<EOL><DEDENT>return routeShapes<EOL>

Get the shapes of all routes.

Parameters
----------
use_shapes : bool, optional
    by default True (i.e. use shapes as the name of the function indicates)
    if False (fall back to lats and longitudes)

Returns
-------
routeShapes: list of dicts that should have the following keys
    name, type, agency, lats, lons
    with types
    list, list, str, list, list


def get_tripIs_active_in_range(self, start, end):

to_select = "<STR_LIT>"<EOL>query = "<STR_LIT>" + to_select +"<STR_LIT>""<STR_LIT>""<STR_LIT>".format(start_ut=start, end_ut=end)<EOL>return pd.read_sql_query(query, self.conn)<EOL>

Obtain from the (standard) GTFS database, list of trip_IDs (and other trip_related info)
that are active between given 'start' and 'end' times.

The start time of a trip is determined by the departure time at the last stop of the trip.
The end time of a trip is determined by the arrival time at the last stop of the trip.

Parameters
----------
start, end : int
    the start and end of the time interval in unix time seconds

Returns
-------
active_trips : pandas.DataFrame with columns
    trip_I, day_start_ut, start_time_ut, end_time_ut, shape_id