Simulator class now returns pandas dataframe instead of raw data

This commit is contained in:
Andreas Tsouchlos 2022-11-22 17:05:58 +01:00
parent 8599ea9662
commit 3c27a0cc18

View File

@ -199,30 +199,27 @@ class Simulator:
"""Stop the simulation."""
self._sim_running = False
@property
def SNRs_and_BERs(self) -> typing.Tuple[np.array, np.array]:
def get_current_results(self) -> pd.DataFrame:
"""Get the current results.
If the simulation has not yet completed, the BERs which have not yet been calculated are set to 0.
:return: Tuple of numpy arrays of the form (SNRs, BERs), where BERs is a list of the form
[BER_decoder_1, BER_decoder_2, ...]
:return: pandas Dataframe with the columns ["SNR", "BER_1", "BER_2", ...]
"""
SNRs = np.array(self._SNRs)
data = {"SNR": np.array(self._SNRs)}
# If the BERs of a decoder have not been calculated for all SNRs,
# fill the rest up with zeros to match the length of the 'SNRs' array
BERs = []
for decoder_BER_list in self._BERs:
for i, decoder_BER_list in enumerate(self._BERs):
padded = np.pad(decoder_BER_list, (0, len(self._SNRs) - len(decoder_BER_list)))
BERs.append(padded)
data[f"BER_{i}"] = padded
# If the BERs have not been calculated for all decoders, fill up the BERs list
# with zero-vectors to match the length of the 'decoders' list
for i in range(len(self._decoders) - len(BERs)):
BERs.append(np.zeros(len(self._SNRs)))
for i in range(len(self._decoders), len(self._BERs)):
data[f"BER_{i}"] = np.zeros(len(self._SNRs))
return SNRs, BERs
return pd.DataFrame(data)
# TODO: Fix typing.Any or Simulator
@ -310,13 +307,7 @@ class SimulationDeSerializer:
json.dump(metadata, f, ensure_ascii=False, indent=4)
# Save results
SNRs, BERs = simulator.SNRs_and_BERs
data_dict = {"SNR": SNRs}
for i, BER in enumerate(BERs):
data_dict[f"BER_{i}"] = BER
df = pd.DataFrame(data_dict)
df = simulator.get_current_results()
df.to_csv(self._get_results_path(sim_name))
def read_results(self, sim_name: str) -> typing.Tuple[pd.DataFrame, typing.Dict]: