main.py 5.02 KB
Newer Older
Lukas Eller's avatar
Lukas Eller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
import json
import pandas as pd
import numpy as np
from estimators import SingleEstimator, JointEstimator
from system_model import SystemModel
import os

class EvaluateEstimators():
    def __init__(self, measurement_path, single_estimators, joint_estimators, system_model, name=None):
        self._measurement_path = measurement_path
        self._single_estimators = single_estimators
        self._joint_estimators = joint_estimators
        self._system_model = system_model

        self._name = name if name is not None else ""

        self.setup()

    def setup(self):

        i = 0
        while os.path.exists(result_path := f"{self._name}_results_{i}"):
            i += 1
        os.mkdir(result_path)
        self._result_path = result_path

    def run(self):
        with open(self._measurement_path, "r") as f:
            measurements = json.load(f)
            results_benchmark = {}
            for eNodeB, data in measurements.items():
                results_benchmark[eNodeB] = {}

                eNodeB_results_path = os.path.join(self._result_path, eNodeB)
                if not os.path.exists(eNodeB_results_path):
                    os.mkdir(eNodeB_results_path)

                location_list, azimuth_list, pathloss_list = [], [], []

                for sector_index, (sector, sector_data) in enumerate(data["sectors"].items()):
                    results_benchmark[eNodeB][sector] = {}
                    results_benchmark[eNodeB][sector]['metadata'] = {}
                    results_benchmark[eNodeB][sector]['results'] = {}
                    results_benchmark[eNodeB][sector]['metadata']["ground_truth"] = data["ground_truth"][sector_index]

                    locations = np.concatenate(
                        (
                            np.array(sector_data["x"]).reshape(-1, 1),
                            np.array(sector_data["y"]).reshape(-1, 1)
                        ),
                        axis=1
                    )
                    azimuths = np.array(sector_data['azimuth'])
                    pathloss = np.array(sector_data['pathloss'])

                    location_list.append(locations)
                    azimuth_list.append(azimuths)
                    pathloss_list.append(pathloss)

                    self._system_model.plot(
                        locations,
                        pathloss,
                        azimuths,
                        save_path=eNodeB_results_path,
                        name=f"sector_{sector}"
                    )

                    for single_estimator in self._single_estimators:
                        single_estimator.save_path = eNodeB_results_path
                        single_estimator.run(
                            locations,
                            azimuths,
                            pathloss
                        )
                        results_benchmark[eNodeB][sector]['results'][single_estimator.name] = single_estimator.mmse_estimate()
                        single_estimator.traceplot(sector=sector)

                for joint_estimator in self._joint_estimators:
                    joint_estimator.save_path = eNodeB_results_path
                    joint_estimator.run(
                        location_list,
                        azimuth_list,
                        pathloss_list
                    )
                    joint_estimator.traceplot()
                    results = joint_estimator.mmse_estimate()

                    for sector_index, sector in enumerate(data["sectors"].keys()):
                        results_benchmark[eNodeB][sector]['results'][joint_estimator.name] = {}
                        results_benchmark[eNodeB][sector]['results'][joint_estimator.name]["MMSE"] = results[sector_index]
                        results_benchmark[eNodeB][sector]['results'][joint_estimator.name]["sector_decision"] = joint_estimator._w

                with open(os.path.join(self._result_path, "results.json"), "w") as fh_results:
                    json.dump(results_benchmark, fh_results)

if __name__ == "__main__":
    system_model = SystemModel(
        frequency=1.8
    )

    system_model_800 = SystemModel(
        frequency=0.8
    )

    EvaluateEstimators(
        "data/pathloss_measurements_1800_1.json",
        [
            SingleEstimator("single_shadow_fading", system_model),
        ],
        [
            JointEstimator("joint_shadow_fading", system_model)
        ],
        system_model,
        "band_1800_1"
    ).run()

    EvaluateEstimators(
        "data/pathloss_measurements_1800_2.json",
        [
            SingleEstimator("single_shadow_fading", system_model),
        ],
        [
            JointEstimator("joint_shadow_fading", system_model)
        ],
        system_model,
        "band_1800_2"
    ).run()


    EvaluateEstimators(
        "data/pathloss_measurements_800.json",
        [
            SingleEstimator("single_shadow_fading", system_model_800),
        ],
        [
            JointEstimator("joint_shadow_fading", system_model_800)
        ],
        system_model_800,
        "band_800"
    ).run()