Exemplo n.º 1
0
 def __init__(self, validator, random_sections, model=MLDelay):
     self.kvv_processor_service = KvvProcessorService()
     self.nn_data = []
     self.min_trip_len = 3
     self.categories = {}
     self.validator = validator
     self.model = model
     self.random_sections = random_sections
Exemplo n.º 2
0
class TestKvvProcessorServiceIntegration(unittest.TestCase):
    def setUp(self):
        self.kvv_processor_service = KvvProcessorService()

    def test_neo_stops_for_line(self):
        stops = self.kvv_processor_service.neo_stops_for_line('kvv:21003:E:H')
        self.assertIsNotNone(stops)
        self.assertGreater(len(stops), 0)

    def test_stops_for_line(self):
        stops = self.kvv_processor_service.stops_for_line(39)
        self.assertIsNotNone(stops)
        self.assertGreater(len(stops), 0)

        stops = self.kvv_processor_service.stops_for_line('kvv:21003:E:H')
        self.assertIsNotNone(stops)
        self.assertGreater(len(stops), 0)
Exemplo n.º 3
0
class DataLoader(object):
    def __init__(self, validator, random_sections, model=MLDelay):
        self.kvv_processor_service = KvvProcessorService()
        self.nn_data = []
        self.min_trip_len = 3
        self.categories = {}
        self.validator = validator
        self.model = model
        self.random_sections = random_sections

    def load(self, line_id, line_id_official):
        # return self.load_data_map(line_id)
        # return self._load_from_database(line_id)
        data_map = self._load_from_database(line_id)
        data_map_neo = self._load_from_database(line_id_official)
        data_map.add_neo(data_map_neo.df)
        return data_map

    def load_data_map(self, line_id):
        try:
            return self._load_from_file(line_id)
        except:
            logger.info('Data Map not chached, auto load from database')
            return self._load_from_database(line_id)

    def _load_from_file(self, line_id):
        return DataMapIO(line_id).read(self.model)

    def _load_from_database(self, line_id):
        trips = self.load_trips(line_id)
        trips = self.clean_trips(trips)
        trips = self.validator.check(trips)
        sections = self.create_sections(trips)
        return DataMapFactory().create_from(sections, self.model)

    def create_sections(self, trips):
        sections = []
        for trip in trips:
            sections = sections + self.random_sections.create(trip, self.model)
        return sections

    def load_trips(self, line_id):
        stops = self.kvv_processor_service.stops_for_line(line_id)
        return self.group_by_journey(stops)

    def clean_trips(self, trips):
        cleaned_trips = []
        for trip in trips:
            if len(trips[trip]) >= self.min_trip_len:
                cleaned_trips.append(trips[trip])
        return cleaned_trips

    def group_by_journey(self, stops):
        groups = defaultdict(list)

        for stop in stops:
            groups[stop.journey_id].append(stop)
        return groups
import logging
from kvv_processor.kvv_processor_service import KvvProcessorService
from kvv_processor.kvv_service import Kvv_Service

logger = logging.getLogger(__name__)

if __name__ == '__main__':

    kvv_service = Kvv_Service()
    lines = kvv_service.list_of_lines()
    kvv_processor_service = KvvProcessorService()
    kvv_processor_service.calculate_travel_times(lines)
Exemplo n.º 5
0
 def setUp(self):
     self.kvv_processor_service = KvvProcessorService()
 def __init__(self):
     self.kvv_processor_service = KvvProcessorService()
     self.journey_id = 0
     self.travel_times = []
class Journey_Calculator:
    def __init__(self):
        self.kvv_processor_service = KvvProcessorService()
        self.journey_id = 0
        self.travel_times = []

    def calculate_journeys_for_line(self, line_id):

        trip_sections = self.kvv_processor_service.trip_sections_by_line(
            line_id)

        if not trip_sections:
            return

        stop_index = 0

        time = datetime.datetime(2018, 12,10)
        stops_for_line = self.kvv_processor_service.basic_stops_for_line(line_id, time)
        logger.info('Start with journey 0')

        while len(stops_for_line) > 0:
            logger.debug('Line %s Stops left: %s', line_id,
                         len(stops_for_line))

            if stop_index is None:
                logger.warning('No stop index found - aborted')
                return

            current_stop = stops_for_line[stop_index]
            semantic_journey_id = str(line_id) + '-' + str(self.journey_id)
            journey = Journey(semantic_journey_id, current_stop.stop_id)
            self.write_journey_to_db(journey)
            stops_for_line.remove(current_stop)

            next_trip_section = self.find_next_trip_section(
                current_stop, trip_sections)

            if next_trip_section:
                stop_index = self.find_index_next_stop(current_stop,
                                                       next_trip_section,
                                                       stops_for_line,
                                                       trip_sections)
            else:
                self.increment_journey_id()

    @classmethod
    def find_next_trip_section(self, current_stop, trip_sections):

        for trip_section in trip_sections:
            if trip_section.station_from.compare(current_stop.station_id):
                return trip_section

    def find_index_next_stop(self, current_stop, trip_section, stops_for_line,
                             trip_sections):

        destination_departure = current_stop.time_tabled_time + timedelta(
            seconds=trip_section.travel_time)

        for i, stop_for_line in enumerate(stops_for_line):

            if stop_for_line.time_tabled_time > destination_departure:
                new_curent_stop = Stop(0, trip_section.station_to, 0,
                                       destination_departure,
                                       destination_departure, self.journey_id)
                next_trip_section = self.find_next_trip_section(
                    new_curent_stop, trip_sections)
                if next_trip_section:
                    return self.find_index_next_stop(new_curent_stop,
                                                     next_trip_section,
                                                     stops_for_line,
                                                     trip_sections)
                self.increment_journey_id()
                return 0

            if stop_for_line.time_tabled_time == destination_departure and trip_section.station_to.compare(
                    stop_for_line.station_id):
                return i

    def write_journey_to_db(self, journey):
        self.kvv_processor_service.insert_journey(journey)

    def increment_journey_id(self):
        self.journey_id = self.journey_id + 1
        logger.info('new journey id: %s', self.journey_id)
# from multiprocessing import Pool
import logging
from kvv_processor.journey_calculator import Journey_Calculator
from kvv_processor.kvv_processor_service import KvvProcessorService

logger = logging.getLogger(__name__)
LINE = 1


def calculate_journey(line):

    try:
        logger.info('Line: %s', str(line.line_id))
        journey_calculator = Journey_Calculator()
        journey_calculator.calculate_journeys_for_line(line.line_id)
    except Exception as e:
        logger.error(e)
        logger.error(line.line_id)


if __name__ == '__main__':
    lines = KvvProcessorService().list_of_lines()
    calculate_journey(lines[LINE - 1])