def build_combined_distributions(centroids: List[Centroid], grid_points: List[GridPoint], zones: List[Zone], normalized_distributions: List[NormalizedMatrix], training_data: List[Sample], combination_mode: str, location_mode: str, num_combination: int) -> Tuple[List[CombinedMatrix], List[NormalizedMatrix]]: calculated_co_ordinate = Tuple[float, float] combined_vector = Dict[Zone, float] # sort_matrices(normalized_distributions) # Set combination method: combine_vectors = get_combination_function(combination_mode) # Containers to hold results. combined_distributions = list() # type: List[CombinedMatrix] normalized_combinations = list() # type: List[NormalizedMatrix] # For every combination: for matrix_list in __combinations(normalized_distributions, num_combination): # Empty matrix to hold the results of all tests. combined_matrix = CombinedMatrix(*matrix_list, size=matrix_list[0].size) for sample in training_data: vectors = list() # type: List[Dict[Zone, float]] answers = list() for matrix in matrix_list: ap_rssi_dict = sample.get_ap_rssi_dict(*matrix.access_points) location_method = get_calculation_function(location_mode) if location_mode == "NNv4" or location_mode == "kNNv2" or location_mode == "kNNv1": calculated_co_ordinate = location_method(centroid_points=centroids, rssis=ap_rssi_dict) if location_mode == "kNNv3": calculated_co_ordinate = location_method(grid_points=grid_points, rssis=ap_rssi_dict) # coord = get_NNv4(centroid_points=centroids, # rssis=sample.get_ap_rssi_dict(*matrix.access_points)) zone = get_zone(zones=zones, co_ordinate=calculated_co_ordinate) # print("find at " + str(zone) + " in matrix " + str(matrix.id)) vector = matrix.get_vector(zone) vectors.append(vector) answers.append(zone) if combination_mode == "WGT": # Get combined vector from combination of above vectors. combined_vector = combine_vectors(answers, *vectors) # Add resultant vector the the ResultantMatrix object. combined_matrix.increment_cell(sample.answer, combined_vector) # Normalize the resultant ResultantMatrix object: normalized_combination = NormalizedMatrix(combined_matrix, combine_ids=True) # Append to both container lists: combined_distributions.append(combined_matrix) normalized_combinations.append(normalized_combination) # OfflineData.insert(normalized_combination) return combined_distributions, normalized_combinations
def test_combined_matrix(normalized_combination: NormalizedMatrix, centroids: List[Centroid], zones: List[Zone], testing_data: List[Sample], combination_method: Callable) -> TestResult: combine_vectors = combination_method test_result = TestResult() resultant = normalized_combination.parent_matrix for sample in testing_data: vectors = list() # type: List[Dict[Zone, float]] for distribution in resultant.normalizations: ap_rssi_dict = sample.get_ap_rssi_dict(*distribution.access_points) coord = get_NNv4(centroid_points=centroids, rssis=ap_rssi_dict) zone = get_zone(zones=zones, co_ordinate=coord) vector = distribution.get_vector(zone) vectors.append(vector) resultant_vector = combine_vectors(sample.answer, *vectors) test_result.record(sample.answer, resultant_vector) return test_result
def __test(self) -> None: for sample in self.__correct_matrix.keys(): for matrix in self.__correct_matrix[sample].keys(): ap_rssi_dict = sample.get_ap_rssi_dict(*matrix.access_points) coord = get_NNv4(centroid_points=self.__centroids, rssis=ap_rssi_dict) zone = get_zone(zones=self.__zones, co_ordinate=coord) # True/False for Pass/Fail self.__correct_matrix[sample][matrix] = zone == sample.answer
def test_normalized_list(normalized_list: List[NormalizedMatrix], centroids: List[Centroid], zones: List[Zone], testing_data: List[Sample]): test_results = dict() # type: Dict[NormalizedMatrix, TestResult] for normalized in normalized_list: test_result = TestResult() # JC-01 used the measured/reported zone instead of the actual zone to the algorithm. for sample in testing_data: ap_rssi_dict = sample.get_ap_rssi_dict(*normalized.access_points) coord = get_NNv4_RSSI(centroid_points=centroids, rssis=ap_rssi_dict) zone = get_zone(zones=zones, co_ordinate=coord) vector = normalized.get_vector(zone) test_result.record(sample.answer, vector) test_results[normalized] = test_result return test_results
def test_normalized_dict( normalized_dict: Dict[int, List[NormalizedMatrix]], centroids: List[Centroid], zones: List[Zone], testing_data: List[Sample]) -> Dict[int, List[TestResult]]: test_dict = dict() # type: Dict[int, List[TestResult]] for d, normalized_matrices in normalized_dict.items(): test_results = list() # type: List[TestResult] for normalized_matrix in normalized_matrices: test_result = TestResult() for sample in testing_data: ap_rssi_dict = sample.get_ap_rssi_dict( *normalized_matrix.access_points) coord = get_NNv4_RSSI(centroid_points=centroids, rssis=ap_rssi_dict) zone = get_zone(zones=zones, co_ordinate=coord) vector = normalized_matrix.get_vector(zone) test_result.record(sample.answer, vector) test_results.append(test_result) test_dict[d] = test_results return test_dict
def test_combination_matrices( normalized_combined_distributions: List[NormalizedMatrix], centroids: List[Centroid], zones: List[Zone], testing_data: List[Sample], combination_method: Callable) -> Dict[NormalizedMatrix, TestResult]: combine_vectors = combination_method test_results = dict() # type: Dict[NormalizedMatrix, TestResult] for normalized in normalized_combined_distributions: resultant = normalized.parent_matrix test_result = TestResult() # JC-01 used the measured/reported zone instead of the actual zone to the algorithm. for sample in testing_data: vectors = list() # type: List[Dict[Zone, float]] answers = list() for distribution in resultant.normalizations: ap_rssi_dict = sample.get_ap_rssi_dict( *distribution.access_points) coord = get_NNv4_RSSI(centroid_points=centroids, rssis=ap_rssi_dict) # coord = get_NNv4(centroid_points=centroids, rssis=ap_rssi_dict) zone = get_zone(zones=zones, co_ordinate=coord) vector = distribution.get_vector(zone) vectors.append(vector) answers.append(zone) NormalizedMatrix.theAnswer = sample.answer # JC-01 - used to pass the true answer around for run-time validation - used by dbg_combine_vector resultant_vector = combine_vectors(answers, *vectors) test_result.record(sample.answer, resultant_vector) test_results[normalized] = test_result return test_results
def build_probability_distributions( access_points: List[AccessPoint], access_point_combinations: List[Tuple[AccessPoint, ...]], centroids: List[Centroid], grid_points: List[GridPoint], zones: List[Zone], training_data: List[Sample], location_mode: str) -> List[ProbabilityMatrix]: probability_matrices = list() # type: List[ProbabilityMatrix] calculated_co_ordinate = Tuple[float, float] # For every combination of Access Points, create one Probability Matrix. for access_point_tuple in access_point_combinations: matrix = Matrix(access_points=[*access_point_tuple], zones=zones, size=len(zones)) # probability_matrix = ProbabilityMatrix(Matrix(access_points=[*access_point_tuple], zones=zones, size=len(zones))) # For every zone file: for zone in zones: zone_appearance_tracker = dict() # type: Dict[Zone, int] samples_taken = 0 # type: int for sample in training_data: if sample.answer is not zone: continue # Get a dictionary of the AP: RSSI we want. ap_rssi_dict = sample.get_ap_rssi_dict(*access_point_tuple) location_method = get_calculation_function(location_mode) if location_mode == "NNv4" or location_mode == "kNNv2" or location_mode == "kNNv1": calculated_co_ordinate = location_method( centroid_points=centroids, rssis=ap_rssi_dict) if location_mode == "kNNv3": calculated_co_ordinate = location_method( grid_points=grid_points, rssis=ap_rssi_dict) calculated_zone = get_zone(zones, calculated_co_ordinate) matrix.increment_value(measured_zone=calculated_zone, actual_zone=zone) # Update the appearance tracker. if calculated_zone in zone_appearance_tracker: zone_appearance_tracker[calculated_zone] += 1 else: zone_appearance_tracker[calculated_zone] = 1 # Update the samples tracker samples_taken += 1 # Calculate the column of the Probability Matrix: # for measured_zone, count in zone_appearance_tracker.items(): # # P(E | H) = (Number of times appeared + (1 / sample size)) / (sample size + 1) # probability = (count * samples_taken + 1) / (samples_taken * (samples_taken + 1)) # probability_matrix.set_value( # measured_zone=measured_zone, # actual_zone=zone, # value=probability) probability_matrix = ProbabilityMatrix(matrix) probability_matrices.append(probability_matrix) return probability_matrices