<jupyter_start><jupyter_text>LIS590DV Final Project - Group Athena Part1 - Total Number of Bus stops and Density Distribution Author: <NAME><jupyter_code>%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import csv
from collections import Counter
stops_file = "GTFS Dataset/stops.csv"
data = {}
with open(stops_file, "r") as f:
    reader = csv.reader(f)
    header = next(reader)
    for column in header:
        data[column] = []
    for row in reader:
        for column, value in zip(header,row):
            data[column].append(value)
data.keys()
len(data)
len(data["stop_id"])
Counter(data["location_type"])
Counter(data["parent_station"])
# This part of codes are referenced from Professor <NAME>'s lecture notes of LIS590DV course.
class Dataset:
    def __init__(self, data):
        self.data = data.copy()
        
    def convert(self, column, dtype):
        self.data[column] = np.array(self.data[column], dtype=dtype)
        
    def columns(self):
        return self.data.keys()
    
    def filter_eq(self, column, value):
        good = (self.data[column] == value)
        new_data = {}
        for column in self.data:
            new_data[column] = self.data[column][good]
        return Dataset(new_data)
    
    def filter_lt(self, column, value):
        good = (self.data[column] < value)
        new_data = {}
        for column in self.data:
            new_data[column] = self.data[column][good]
        return Dataset(new_data)
    
    def filter_gt(self, column, value):
        good = (self.data[column] > value)
        new_data = {}
        for column in self.data:
            new_data[column] = self.data[column][good]
        return Dataset(new_data)
    
    def filter_ne(self, column, value):
        good = (self.data[column] != value)
        new_data = {}
        for column in self.data:
            new_data[column] = self.data[column][good]
        return Dataset(new_data)
    
    def size(self):
        for key in self.data:
            return self.data[key].size

    def split(self, column):
        new_datasets = {}
        for split_value in np.unique(self.data[column]):
            new_datasets[split_value] = self.filter_eq(column, split_value)
        return new_datasets

    def stats(self):
        statistics = {}
        for key in self.data:
            if self.data[key].dtype not in ("float", "int"):
                continue
            values = self.data[key]
            statistics[key] = (values.min(), values.max(), values.std(), values.mean())
        return statistics
    
    def compare(self, other):
        stats1 = self.stats()
        stats2 = other.stats()
        for column in self.columns():
            if column not in stats1: continue
            print("Column '{0:25s}'".format(column))
            for s1, s2 in zip(stats1[column], stats2[column]):
                print("    {0} vs {1}".format(s1, s2))
    
    def plot(self, x_column, y_column):
        plt.plot(self.data[x_column], self.data[y_column], '.')
# This part of codes are referenced from Professor <NAME>'s lecture notes of LIS590DV course.
stops = Dataset(data)
value_types = {'stop_lat': 'float',
               'stop_lon': 'float',}
for v in stops.columns():
    stops.convert(v, value_types.get(v, "str"))
stops.stats()
plt.rcParams["figure.figsize"] = (10, 10)
plt.plot(data["stop_lon"], data["stop_lat"], ".", alpha = 0.4)
plt.xlabel("Longitude",fontsize=20)
plt.ylabel("Latitude",fontsize=20)
plt.title("Location of Bus Stops (2496 Stops in Total)",fontweight="bold",fontsize=22)
plt.grid(color='gray', linestyle='dotted')
plt.savefig('Location of Bus Stops.svg', bbox_inches='tight')
plt.savefig('Location of Bus Stops.png', bbox_inches='tight')
    # The binning function codes are referenced from Professor <NAME>'s lecture notes of LIS590DV course.
    stats = stops.stats()
    lon_min = stats["stop_lon"][0]
    lon_max = stats["stop_lon"][1]
    lat_min = stats["stop_lat"][0]
    lat_max = stats["stop_lat"][1]
    def bin_stops(num_bins = 16):
        lon = np.mgrid[lon_min:lon_max:(num_bins+1)*1j]
        lat = np.mgrid[lat_min:lat_max:(num_bins+1)*1j]

        stop_count = np.zeros((num_bins, num_bins))
        for i in range(num_bins):
            left_lat = lat[i]
            right_lat = lat[i+1]
            filter_lat_left = stops.filter_gt("stop_lat", left_lat)
            filter_lat_right = filter_lat_left.filter_lt("stop_lat", right_lat)
            for j in range(num_bins):
                left_lon = lon[j]
                right_lon = lon[j+1]
                filter_lon_left = filter_lat_right.filter_gt("stop_lon", left_lon)
                filter_lon_right = filter_lon_left.filter_lt("stop_lon", right_lon)
                stop_count[i,j] += filter_lon_right.size()

        plt.imshow(stop_count, extent=(lon_min,lon_max,lat_min,lat_max), origin="lower")
        plt.xlabel("Longitude",fontsize=20)
        plt.ylabel("Latitude",fontsize=20)
        plt.title("Density of Bus Stops of CUMTD", fontweight="bold",fontsize=22)
        color_bar = plt.colorbar()
        color_bar.set_label("Count")
        
        ax=plt.gca()
        # Cicle the areas with highest density
        ellipse1 = Ellipse(xy=(-88.242,40.123),width=0.044,height=0.021,alpha=0.9,facecolor="None",edgecolor="hotpink",lw=2)
        ellipse2 = Ellipse(xy=(-88.201,40.101),width=0.023,height=0.028,alpha=0.9,facecolor="None",edgecolor="hotpink",lw=2)
        ax.add_patch(ellipse1)
        ax.add_patch(ellipse2)

    bin_stops()
    plt.savefig('Density of Bus Stops.svg', bbox_inches='tight')
    plt.savefig('Density of Bus Stops.png', bbox_inches='tight')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13240418118466898, 'fraction_numerical': 0.018641114982578397, 'mean_word_length': 2.853020134228188, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '328733', 'license': 'MIT', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1896, 'n_words': 409, 'path': 'project-spring2017/part1/Final-Part1-Bus stops.ipynb', 'repo_name': 'huilyu2/DataVisualization', 'size': '5759'}