コード例 #1
0
def add_trip_type_to_trip_stop_times_dict(trips_dict, trip_type_dict):
    # Takes trips_dict of format dict(trip_id: dict(stop_sequence: stopDeparture))
    #  Returns dict(trip_id: tripInfo(tripStops=dict(stop_sequence: stopDeparture), tripType=routeInfo)).
    #  tripInfo and stopDeparture are defined in data_structures.py.
    for trip_id in trips_dict:
        trips_dict[trip_id] = tripInfo(
            tripStops=trips_dict[trip_id],
            tripRouteInfo=trip_type_dict.get(trip_id).tripRouteInfo,
            serviceId=trip_type_dict.get(trip_id).serviceId
        )
    return trips_dict
コード例 #2
0
def create_trip_type_dict(trip_type_namedtuple, route_type_dict):
    trip_type_dict = dict()
    trip_data = trip_type_namedtuple.data
    trip_cols = trip_type_namedtuple.columns

    for trip in trip_data:
        route_type = route_type_dict[trip[trip_cols['route_id']]]
        trip_type_dict[trip[trip_cols['trip_id']]] = tripInfo(
            tripStops=list(),
            tripRouteInfo=routeInfo(routeId=trip[trip_cols['route_id']], routeType=route_type),
            serviceId=trip[trip_cols['service_id']]
        )

    return trip_type_dict
コード例 #3
0
    def testTripTypeStopTimeDict(self):
        trip_type_stop_time_dict_expected = {
            'Logan-22-Weekday-trip':
            tripInfo(tripStops={
                '1':
                stopDeparture(stopId='Logan-Subway', departureTime='08:00:00'),
                '2':
                stopDeparture(stopId='Logan-RentalCarCenter',
                              departureTime='08:04:00')
            },
                     tripRouteInfo=routeInfo(routeId='Logan-22',
                                             routeType='3'),
                     serviceId='Logan-Weekday'),
            'CR-Saturday-Fall-17-1752':
            tripInfo(tripStops={
                '1':
                stopDeparture(stopId='Readville', departureTime='7:30:00'),
                '2':
                stopDeparture(stopId='Fairmount', departureTime='7:33:00')
            },
                     tripRouteInfo=routeInfo(routeId='CR-Fairmount',
                                             routeType='2'),
                     serviceId='CR-Saturday-SouthSide-Fall-17-FMT')
        }

        trip_stop_time_dict_given = {
            'Logan-22-Weekday-trip': {
                '1':
                stopDeparture(stopId='Logan-Subway', departureTime='08:00:00'),
                '2':
                stopDeparture(stopId='Logan-RentalCarCenter',
                              departureTime='08:04:00')
            },
            'CR-Saturday-Fall-17-1752': {
                '1': stopDeparture(stopId='Readville',
                                   departureTime='7:30:00'),
                '2': stopDeparture(stopId='Fairmount', departureTime='7:33:00')
            }
        }

        with open(
                './gtfs_parsing/tests/test_read_data/csv_files_for_tests/test_trips.txt'
        ) as g:
            with open(
                    './gtfs_parsing/tests/test_read_data/csv_files_for_tests/test_routes.txt'
            ) as h:
                trip_type_stop_time_dict_actual = add_trip_type_to_trip_stop_times_dict(
                    trip_stop_time_dict_given,
                    create_trip_type_dict(
                        self.readTestFile(g),
                        create_route_type_dict(self.readTestFile(h))))

        for key in trip_type_stop_time_dict_expected:
            self.assertIn(key, trip_type_stop_time_dict_actual)
        for key in trip_type_stop_time_dict_actual:
            self.assertIn(key, trip_type_stop_time_dict_expected)

            self.assertEqual(
                trip_type_stop_time_dict_expected[key].tripRouteInfo,
                trip_type_stop_time_dict_actual[key].tripRouteInfo)

            for nested_key in trip_type_stop_time_dict_expected[key].tripStops:
                self.assertIn(nested_key,
                              trip_type_stop_time_dict_actual[key].tripStops)
            for nested_key in trip_type_stop_time_dict_actual[key].tripStops:
                self.assertIn(nested_key,
                              trip_type_stop_time_dict_expected[key].tripStops)
コード例 #4
0
    def testTripTypeDict(self):
        trip_type_dict_expected = {
            'Logan-22-Weekday-trip':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='Logan-22',
                                             routeType='3'),
                     serviceId='Logan-Weekday'),
            'Logan-22-Weekend-trip':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='Logan-22',
                                             routeType='3'),
                     serviceId='Logan-Weekend'),
            'Logan-33-Weekday-trip':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='Logan-33',
                                             routeType='3'),
                     serviceId='Logan-Weekday'),
            'Logan-33-Weekend-trip':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='Logan-33',
                                             routeType='3'),
                     serviceId='Logan-Weekend'),
            'Logan-55-Weekday-trip':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='Logan-55',
                                             routeType='3'),
                     serviceId='Logan-Weekday'),
            'Logan-55-Weekend-trip':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='Logan-55',
                                             routeType='3'),
                     serviceId='Logan-Weekend'),
            'Logan-66-Weekday-trip':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='Logan-66',
                                             routeType='3'),
                     serviceId='Logan-Weekday'),
            'Logan-66-Weekend-trip':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='Logan-66',
                                             routeType='3'),
                     serviceId='Logan-Weekend'),
            'CR-Saturday-Fall-17-1752':
            tripInfo(tripStops=list(),
                     tripRouteInfo=routeInfo(routeId='CR-Fairmount',
                                             routeType='2'),
                     serviceId='CR-Saturday-SouthSide-Fall-17-FMT')
        }

        with open(
                './gtfs_parsing/tests/test_read_data/csv_files_for_tests/test_trips.txt'
        ) as f:
            with open(
                    './gtfs_parsing/tests/test_read_data/csv_files_for_tests/test_routes.txt'
            ) as g:
                input_namedtuple = self.readTestFile(f)
                trip_type_dict_actual = create_trip_type_dict(
                    input_namedtuple,
                    create_route_type_dict(self.readTestFile(g)))

        for key in trip_type_dict_expected:
            self.assertIn(key, trip_type_dict_actual)
        for key in trip_type_dict_actual:
            self.assertIn(key, trip_type_dict_expected)
            self.assertEqual(trip_type_dict_expected[key],
                             trip_type_dict_actual[key])
コード例 #5
0
class TestReadData(unittest.TestCase):
    TEST_SERVICE_EXCEPTIONS_DICT = {
        's2': {
            datetime(2018, 11, 22): False,
            datetime(2018, 11, 12): True
        }
    }
    TEST_SERVICE_DATES_DICT = {
        's1':
        serviceDates(start_date=datetime(year=2018, month=11, day=11),
                     end_date=datetime(year=2018, month=11, day=25),
                     serviceDays={
                         'm': True,
                         't': False,
                         'w': True,
                         'r': False,
                         'f': True,
                         's': False,
                         'u': True
                     }),
        's2':
        serviceDates(start_date=datetime(year=2018, month=11, day=11),
                     end_date=datetime(year=2018, month=11, day=23),
                     serviceDays={
                         'm': False,
                         't': True,
                         'w': False,
                         'r': True,
                         'f': False,
                         's': True,
                         'u': False
                     })
    }
    TEST_TRIP_DICT = {
        't1':
        tripInfo(tripStops={
            '1': stopDeparture(stopId='s1', departureTime='12:00'),
            '2': stopDeparture(stopId='s2', departureTime='12:01'),
            '3': stopDeparture(stopId='s3', departureTime='12:02')
        },
                 tripRouteInfo=routeInfo(routeId="testId", routeType="Plane"),
                 serviceId='s1'),
        't2':
        tripInfo(tripStops={
            '1': stopDeparture(stopId='s1', departureTime='12:00'),
            '2': stopDeparture(stopId='s2', departureTime='12:01'),
            '3': stopDeparture(stopId='s3', departureTime='12:02')
        },
                 tripRouteInfo=routeInfo(routeId="differentTestId",
                                         routeType="Bus"),
                 serviceId='s1'),
        't3':
        tripInfo(tripStops={
            '1': stopDeparture(stopId='s2', departureTime='12:00'),
            '2': stopDeparture(stopId='s3', departureTime='12:01')
        },
                 tripRouteInfo=routeInfo(routeId="testId", routeType="Plane"),
                 serviceId='s2')
    }

    def test_to_date_trip_dict(self):
        expected_date_trip_dict = {
            datetime(year=2018, month=11, day=11): {'t1', 't2'},
            datetime(year=2018, month=11, day=12): {'t1', 't2', 't3'},
            datetime(year=2018, month=11, day=13): {'t3'},
            datetime(year=2018, month=11, day=14): {'t1', 't2'},
            datetime(year=2018, month=11, day=15): {'t3'},
            datetime(year=2018, month=11, day=16): {'t1', 't2'},
            datetime(year=2018, month=11, day=17): {'t3'},
            datetime(year=2018, month=11, day=18): {'t1', 't2'},
            datetime(year=2018, month=11, day=19): {'t1', 't2'},
            datetime(year=2018, month=11, day=20): {'t3'},
            datetime(year=2018, month=11, day=21): {'t1', 't2'},
            datetime(year=2018, month=11, day=22): set(),
            datetime(year=2018, month=11, day=23): {'t1', 't2'},
            datetime(year=2018, month=11, day=24): set(),
            datetime(year=2018, month=11, day=25): {'t1', 't2'}
        }

        actual_date_trip_dict = to_date_trip_dict(
            self.TEST_TRIP_DICT, '2018-11-11', '2018-11-25',
            self.TEST_SERVICE_DATES_DICT, self.TEST_SERVICE_EXCEPTIONS_DICT)

        self.assertEqual(tuple(expected_date_trip_dict.keys()),
                         tuple(actual_date_trip_dict.keys()))

        for key in expected_date_trip_dict:
            self.assertTrue(expected_date_trip_dict[key].issubset(
                actual_date_trip_dict[key]))
            self.assertTrue(actual_date_trip_dict[key].issubset(
                expected_date_trip_dict[key]))

    def test_date_outside_of_service_range(self):
        nov1 = datetime(year=2018, month=11, day=1)
        nov3 = datetime(year=2018, month=11, day=3)
        nov5 = datetime(year=2018, month=11, day=5)

        # Arguments are: (current date, start date, service end date, service start date, end date)
        self.assertFalse(
            date_outside_of_service_range(nov3, nov1, nov5, nov1, nov5))
        self.assertFalse(
            date_outside_of_service_range(nov3, nov3, nov5, nov1, nov5))
        self.assertFalse(
            date_outside_of_service_range(nov3, nov1, nov5, nov3, nov5))
        self.assertFalse(
            date_outside_of_service_range(nov3, nov1, nov5, nov1, nov3))
        self.assertFalse(
            date_outside_of_service_range(nov3, nov1, nov3, nov1, nov5))
        self.assertTrue(
            date_outside_of_service_range(nov3, nov1, nov5, nov5, nov5))
        self.assertTrue(
            date_outside_of_service_range(nov3, nov1, nov1, nov1, nov5))
        self.assertTrue(
            date_outside_of_service_range(nov5, nov5, nov1, nov5, nov5))
        self.assertTrue(
            date_outside_of_service_range(nov5, nov5, nov5, nov5, nov1))

    def test_add_trip_if_no_exception(self):
        valid_trips_dict = {
            datetime(year=2018, month=11, day=12): set(),
            datetime(year=2018, month=11, day=13): set(),
            datetime(year=2018, month=11, day=14): set(),
            datetime(year=2018, month=11, day=22): set(),
        }

        def wrap_add_trip_if_no_exception(current_date, trp, svc_id):
            add_trip_if_no_exception(
                self.TEST_SERVICE_EXCEPTIONS_DICT, svc_id, current_date,
                self.TEST_SERVICE_DATES_DICT[svc_id].serviceDays,
                date_helpers.to_day_of_week(current_date.weekday()),
                valid_trips_dict, trp)

        expected_date_trip_dict = {
            datetime(year=2018, month=11, day=12): {'t3'},
            datetime(year=2018, month=11, day=13): {'t3'},
            datetime(year=2018, month=11, day=14): set(),
            datetime(year=2018, month=11, day=22): set(),
        }

        for trip in self.TEST_TRIP_DICT:
            service_id = self.TEST_TRIP_DICT[trip].serviceId
            if service_id != 's2':
                continue
            wrap_add_trip_if_no_exception(
                datetime(year=2018, month=11, day=12), trip, service_id)
            wrap_add_trip_if_no_exception(
                datetime(year=2018, month=11, day=13), trip, service_id)
            wrap_add_trip_if_no_exception(
                datetime(year=2018, month=11, day=14), trip, service_id)
            wrap_add_trip_if_no_exception(
                datetime(year=2018, month=11, day=22), trip, service_id)

        self.assertEqual(tuple(expected_date_trip_dict.keys()),
                         tuple(valid_trips_dict.keys()))

        for key in expected_date_trip_dict:
            self.assertTrue(expected_date_trip_dict[key].issubset(
                valid_trips_dict[key]))
            self.assertTrue(valid_trips_dict[key].issubset(
                expected_date_trip_dict[key]))

    def test_add_trip_if_valid_day_of_week(self):
        valid_trips_dict = {datetime(year=2018, month=11, day=13): set()}

        expected_date_trip_dict = {
            datetime(year=2018, month=11, day=13): {'t3'}
        }

        for trip in self.TEST_TRIP_DICT:
            add_trip_if_valid_day_of_week(
                self.TEST_SERVICE_DATES_DICT[
                    self.TEST_TRIP_DICT[trip].serviceId].serviceDays,
                date_helpers.to_day_of_week(
                    datetime(year=2018, month=11, day=13).weekday()),
                valid_trips_dict, datetime(year=2018, month=11, day=13), trip)

        self.assertEqual(tuple(expected_date_trip_dict.keys()),
                         tuple(valid_trips_dict.keys()))

        for key in expected_date_trip_dict:
            self.assertTrue(expected_date_trip_dict[key].issubset(
                valid_trips_dict[key]))
            self.assertTrue(valid_trips_dict[key].issubset(
                expected_date_trip_dict[key]))