Пример #1
0
def test_to_grid():
    l1 = [1, 2, 3, 3, 4, 5, 5]
    grid1 = support.to_grid(l1, 1, lambda x: x, 1, 5)
    run_tests.compare_answer(grid1, [1,2,3,4,5], "grid1")

    l2 = [100, 130, 100, 100, 150, 130, 100]
    grid2 = support.to_grid(l2, 10, lambda x: x, 100, 150)
    run_tests.compare_answer(grid2, [100, None, None, 130, None, 150], "grid2")
Пример #2
0
    def reduce_to_time_resolution(list_of_user_info, time_resolution):
        grouped_by_dates_orddict = support.group_by(list_of_user_info, lambda x: x.date)
        result = []

        for _, grouped_by_date in grouped_by_dates_orddict.items():
            time_grid_day = support.to_grid(grouped_by_date, time_resolution, lambda x: support.time_total_seconds(x.time), 0, common_config.SECONDS_IN_A_DAY)
            support.remove_none_from_list(time_grid_day)
            result = result + time_grid_day

        return result
Пример #3
0
    def reduce_to_time_resolution(list_of_user_info, time_resolution):
        grouped_by_dates_orddict = support.group_by(list_of_user_info,
                                                    lambda x: x.date)
        result = []

        for _, grouped_by_date in grouped_by_dates_orddict.items():
            time_grid_day = support.to_grid(
                grouped_by_date, time_resolution,
                lambda x: support.time_total_seconds(x.time), 0,
                common_config.SECONDS_IN_A_DAY)
            support.remove_none_from_list(time_grid_day)
            result = result + time_grid_day

        return result
Пример #4
0
def handle_incoming_mp_single_user_request(frontend, connection, msg):
    err.log_error(err.INFO, "Using MP algorithm")
    predicted_date_time = datetime.datetime.combine(msg.current_date, msg.current_time) + datetime.timedelta(seconds=msg.future_time_delta)
    predicted_date = predicted_date_time.date()
    predicted_time = predicted_date_time.time()

    '''
    if user_id is 0000 (i.e. 0, because it is an integer), we need to use multi-user prediction, not single user prediction
    '''
    if msg.user_id != 0:
        mp_fetched = mp.retrieve_user_mobility_data_single_user(frontend.dbcc, msg.user_id, msg.current_date, predicted_date)
        grouped_by_date_mp_fetched = support.group_by(mp_fetched, lambda x: x.date)

        grouped_by_date_grid_by_minute_of_day_mp_fetched = {}
        for date, mp_fetched_list in grouped_by_date_mp_fetched.items():
            minute_of_day_grid = support.to_grid( mp_fetched_list
                                                , 1
                                                , lambda x: x.minute_of_day
                                                , 0
                                                , common_config.MINUTES_IN_A_DAY - 1
                                                )
            grouped_by_date_grid_by_minute_of_day_mp_fetched[date] = minute_of_day_grid

        missing_ranges = mp.check_user_mobility_data_single_user_integrity( grouped_by_date_grid_by_minute_of_day_mp_fetched
                                                                          , msg.current_date
                                                                          )

        if len(missing_ranges) > 0:
            #Missing user information data, queue request and ask data from supplier
            missing_resource = mr.MissingUserInfo(missing_ranges)
            frontend.queue_as_pending_request(connection, msg, [missing_resource])

            err.log_error(err.INFO, "Did not have enough user info data. Asking supplier for: " + str(missing_ranges))

            error_message = mo.Error(102, "MP Request will take longer than expected because user data is missing at MOBaaS")
            connection.add_message(error_message)

            for missing_range in missing_ranges:
                start_time = missing_range.min_range.time()
                start_date = missing_range.min_range.date()
                time_span = int((missing_range.max_range - missing_range.min_range).total_seconds())

                supplier_message = mo.MaaSSingleUserDataRequest(msg.user_id, start_time, start_date, time_span, 60)
                frontend.ask_at_supplier(supplier_message)
        else:
            #There is all the info we need, calculate the spots
            current_day_str = support.calc_day(msg.current_date)
            predicted_day_str = support.calc_day(predicted_date)

            pykov_chain = mp.prepare_pykov_chain_with_single_user_mobility_states(grouped_by_date_grid_by_minute_of_day_mp_fetched
                                                                                 , current_day_str
                                                                                 , predicted_day_str
                                                                                 )
            chance_distribution_dict = mp.mobility_prediction(pykov_chain, msg.current_cell_id, msg.current_date, msg.current_time, predicted_date, predicted_time)

            #Create mobaas_protocol.prediction objects from the answers
            mobaas_protocol_predictions = mp.from_pykov_distribution_dict_to_predictions(chance_distribution_dict)

            answer_message = mo.MPSingleUserAnswer(msg.user_id, predicted_time, predicted_date, mobaas_protocol_predictions)
            connection.add_message(answer_message)
            err.log_error(err.INFO, "Found an answer to the request! Answer:" + str(answer_message))
    else:
        print 'error: should not get here!'