kuanb / peartree

peartree: A library for converting transit data into a directed graph for sketch network analysis.
MIT License
201 stars 23 forks source link

not able to plot GTFS data using peartree #159

Closed Pranav-Gairola closed 4 years ago

Pranav-Gairola commented 4 years ago

Hello everyone, I have been trying to implement an already existing code to plot GTFS feed but getting attribute error somewhere in pandas module getting called inside from peartree module. I would be thankful for any help. Python --version -3.8 OS -- Windows 10 P.S. I have updated required modules still the problem persists.

Script:-

try: import peartree as pt except: pass import os import pickle PKL_PATH = 'graph.pkl'

Creating the graph takes a gazillion time so I pickle it

if not os.path.exists(PKL_PATH):

Automatically identify the busiest day and

# read that in as a Partidge feed
feed = pt.get_representative_feed("F:\PhD\GTFS\GTFS_trial.zip")
# Set a target time period to
# use to summarize impedance
start = 7*60*60  # 7:00 AM
end = 10*60*60  # 10:00 AM
# Converts feed subset into a directed
# network multigraph
G = pt.load_feed_as_graph(feed, start, end)
with open(PKL_PATH, 'wb') as pkl_file:
    pickle.dump(G, pkl_file)

else: with open(PKL_PATH, 'rb') as pkl_file: G = pickle.load(pkl_file)

Traceback (most recent call last): File "F:/PhD/Python/Directed_multigraph.py", line 31, in feed = pt.get_representative_feed("F:\PhD\GTFS\GTFS_trial.zip") File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\paths.py", line 95, in get_representative_feed service_ids_by_date = ptg.read_service_ids_by_date(file_loc) File "C:\Anaconda3\envs\Python\lib\site-packages\partridge\readers.py", line 72, in read_service_ids_by_date return _service_ids_by_date(feed) File "C:\Anaconda3\envs\Python\lib\site-packages\partridge\readers.py", line 156, in _service_ids_by_date service_ids = set(feed.trips.service_id) File "C:\Anaconda3\envs\Python\lib\site-packages\pandas\core\generic.py", line 5274, in getattr return object.getattribute(self, name) AttributeError: 'DataFrame' object has no attribute 'service_id'

kuanb commented 4 years ago

HI @Pranav-Gairola looks like your GTFS feed might be missing a service id. Can you share the GTFS zip file you are using? This is the one you are trying to get a representative feed of:

F:\PhD\GTFS\GTFS_trial.zip

As you can see here in the GTFS specifications, the service_id is a required column. Without it, a GTFS feed is missing important information that allows a consumer to understand which schedules are associated with which trips, routes, etc.

Pranav-Gairola commented 4 years ago

Hi, thanks for responding Kuan. GTFS feed I'm using can be accessed from the link below. https://drive.google.com/open?id=1nJVaQP-O5urYDo6u0DBqh6eut3rZhznY

Pranav-Gairola commented 4 years ago

Hey! Can anyone help me out over this?

kuanb commented 4 years ago

Hi @Pranav-Gairola I would recommend reviewing the README for how to load in a GTFS zip file. There is no need to pickle your file to just load and review a GTFS feed.

Here is the script I ran:

import peartree as pt
print(pt.__version__)  # 0.6.3

path = "gh_issue_gtfs.zip"

# Automatically identify the busiest day and
# read that in as a Partidge feed
feed = pt.get_representative_feed(path)

# Set a target service time frame to model
start = 7*60*60  # 7:00 AM
end = 10*60*60  # 10:00 AM

# Convert feed into directed network multigraph
G = pt.load_feed_as_graph(feed, start, end)

This loaded in the file as the object G.

In my Jupyter notebook, I was then able to generate a plot of the network with the following line:

%matplotlib inline

pt.generate_plot(G)

The rendered graph looks like this: image

Pranav-Gairola commented 4 years ago

Hi @kuanb , Thanks for responding. I tried to follow your code in jupyter notebook and i got different error this time.

import peartree as pt path = "F:/PhD/GTFS/GTFS.zip" feed = pt.get_representative_feed(path) start = 56060 # 5:00 AM end = 126060 # 12:00 AM G = pt.load_feed_as_graph(feed, start, end) %matplotlib inline pt.plot.generate_plot(G)


AttributeError Traceback (most recent call last)

in ----> 1 import peartree as pt 2 path = "F:/PhD/GTFS/GTFS.zip" 3 feed = pt.get_representative_feed(path) 4 start = 5*60*60 # 5:00 AM 5 end = 12*60*60 # 12:00 AM C:\Anaconda3\lib\site-packages\peartree\__init__.py in ----> 1 from . import graph_tool # noqa: F401 2 from . import parallel # noqa: F401 3 4 from peartree.__version__ import __version__ # noqa: F401 5 from peartree.paths import ( C:\Anaconda3\lib\site-packages\peartree\graph_tool.py in 3 from typing import Any, Tuple 4 ----> 5 import networkx as nx 6 7 C:\Anaconda3\lib\site-packages\networkx\__init__.py in 113 from networkx.generators import * 114 --> 115 import networkx.readwrite 116 from networkx.readwrite import * 117 C:\Anaconda3\lib\site-packages\networkx\readwrite\__init__.py in 13 from networkx.readwrite.nx_yaml import * 14 from networkx.readwrite.gml import * ---> 15 from networkx.readwrite.graphml import * 16 from networkx.readwrite.gexf import * 17 from networkx.readwrite.nx_shp import * C:\Anaconda3\lib\site-packages\networkx\readwrite\graphml.py in 312 313 --> 314 class GraphML(object): 315 NS_GRAPHML = "http://graphml.graphdrawing.org/xmlns" 316 NS_XSI = "http://www.w3.org/2001/XMLSchema-instance" C:\Anaconda3\lib\site-packages\networkx\readwrite\graphml.py in GraphML() 342 else: 343 # prepend so that python types are created upon read (last entry wins) --> 344 types = [(np.float64, "float"), (np.float32, "float"), 345 (np.float16, "float"), (np.float_, "float"), 346 (np.int, "int"), (np.int8, "int"), AttributeError: module 'numpy' has no attribute 'float64'
Pranav-Gairola commented 4 years ago

@kuanb , If I run the exact same code using pycharm interpreter, i am getting different attribute error than the last time. If there is no issue with the GTFS feed as you were able to plot it, what is the issue here?

Traceback (most recent call last): File "F:/PhD/Python/Directed_multigraph.py", line 7, in G = pt.load_feed_as_graph(feed, start, end) File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\paths.py", line 214, in load_feed_as_graph wait_times_by_stop) = generate_summary_graph_elements(feed, File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\graph.py", line 112, in generate_summary_graph_elements all_wait_times) = generate_edge_and_wait_values( File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\summarizer.py", line 478, in generate_edge_and_wait_values stop_times = _linearly_interpolate_infill_times( File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\summarizer.py", line 292, in _linearly_interpolate_infill_times results = [trip_times_interpolator.generate_infilled_times(trip_id) File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\summarizer.py", line 292, in results = [trip_times_interpolator.generate_infilled_times(trip_id) File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\parallel.py", line 288, in generate_infilled_times sub_df[col] = apply_interpolation(sub_df[col]) File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\parallel.py", line 304, in apply_interpolation target_col_array[nans] = np.interp(x(nans), File "C:\Anaconda3\envs\Python\lib\site-packages\peartree\toolkit.py", line 160, in return (np.isnan(y), lambda z: z.nonzero()[0]) File "C:\Anaconda3\envs\Python\lib\site-packages\pandas\core\generic.py", line 5274, in getattr return object.getattribute(self, name) AttributeError: 'Series' object has no attribute 'nonzero'

kuanb commented 4 years ago

Your second nonzero error is captured in this issue. I think you may have an old version of pandas installed or an older version of peartree. I would recommend re-installing peartree to make sure you have the correct version of peartree and pandas.

Note how I printed the version of peartree I was using. You can do the same to make sure you are using the latest verson:

import peartree as pt
print(pt.__version__)  # 0.6.3
Pranav-Gairola commented 4 years ago

Thanks for this. My second non zero error is removed once i uninstalled and then re-installed pandas 1.0.5 and peartree 0.6.3 both. Although, this time i have encountered different error.

AttributeError Traceback (most recent call last)

in 5 end = 11*60*60 # 11:00 AM 6 G = pt.load_feed_as_graph(feed, start, end) ----> 7 get_ipython().run_line_magic('matplotlib', 'inline') 8 pt.plot.generate_plot(G) C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth) 2324 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals 2325 with self.builtin_trap: -> 2326 result = fn(*args, **kwargs) 2327 return result 2328 in matplotlib(self, line) C:\Anaconda3\lib\site-packages\IPython\core\magic.py in (f, *a, **k) 185 # but it's overkill for just that one bit of state. 186 def magic_deco(arg): --> 187 call = lambda f, *a, **k: f(*a, **k) 188 189 if callable(arg): C:\Anaconda3\lib\site-packages\IPython\core\magics\pylab.py in matplotlib(self, line) 97 print("Available matplotlib backends: %s" % backends_list) 98 else: ---> 99 gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui) 100 self._show_matplotlib_backend(args.gui, backend) 101 C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in enable_matplotlib(self, gui) 3429 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) 3430 -> 3431 pt.activate_matplotlib(backend) 3432 pt.configure_inline_support(self, backend) 3433 C:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in activate_matplotlib(backend) 308 309 import matplotlib --> 310 matplotlib.interactive(True) 311 312 # Matplotlib had a bug where even switch_backend could not force AttributeError: module 'matplotlib' has no attribute 'interactive'
kuanb commented 4 years ago

Now your issue looks like it is with your install of matplotlib, not anything related to peartree. It looks like you have a conda install of matplotlib. I'd recommend going to forums there and seeing how people resolved the issue you are experiencing.

Pranav-Gairola commented 4 years ago

Ok, i 'll check there. Thank you so much for your help throughout on this.

Pranav-Gairola commented 4 years ago

Hi, Kuan. I got the following error in the notebook.

TypeError Traceback (most recent call last)

in 6 G = pt.load_feed_as_graph(feed, start, end) 7 get_ipython().run_line_magic('matplotlib', 'inline') ----> 8 pt.plot.generate_plot(G) C:\Anaconda3\lib\site-packages\peartree\plot.py in generate_plot(G, use_agg) 33 edge_color='#e2dede', 34 edge_alpha=0.25, ---> 35 bgcolor='black') 36 return (fig, ax) TypeError: plot_graph() got an unexpected keyword argument 'fig_height' But once i used the same code in pycharm IDE and remove the parameter fig_height from the function ox.plot_graph, i was able to plot the graph with this warning (I am using pyproj version-2.6.1.post1 and osmnx version-0.15.1. ): FutureWarning: '+init=:' syntax is deprecated. ':' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6 return _prepare_from_string(" ".join(pjargs))
Pranav-Gairola commented 4 years ago