Skip to content

Commit

Permalink
analysis: refactor python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovanniGrieco committed Jun 2, 2024
1 parent 5cd4e26 commit 0fd4722
Show file tree
Hide file tree
Showing 26 changed files with 1,144 additions and 1,022 deletions.
58 changes: 40 additions & 18 deletions analysis/drone_peripheral_consumption_to_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,55 @@
from argparse import ArgumentParser
from enum import Enum

P = ArgumentParser(description='Given power law of a peripheral, convert a given CSV with peripheral power data to deduce its state.')
P.add_argument('input_csv_filepath', type=str, help='Input CSV filepath containing peripheral power data.')
P.add_argument('output_csv_filepath', type=str, help='Output CSV filepath to report peripheral state.')
P.add_argument('off_state_power_W', type=float, help='Power consumption of the peripheral when in OFF state.')
P.add_argument('on_state_power_W', type=float, help='Power consumption of the peripheral when in ON state.')
P.add_argument('standby_state_power_W', type=float, help='Power consumption of the peripheral when in STANDBY state.')
P = ArgumentParser(
description="Given power law of a peripheral, convert a given CSV with peripheral power data to deduce its state."
)
P.add_argument(
"input_csv_filepath",
type=str,
help="Input CSV filepath containing peripheral power data.",
)
P.add_argument(
"output_csv_filepath",
type=str,
help="Output CSV filepath to report peripheral state.",
)
P.add_argument(
"off_state_power_W",
type=float,
help="Power consumption of the peripheral when in OFF state.",
)
P.add_argument(
"on_state_power_W",
type=float,
help="Power consumption of the peripheral when in ON state.",
)
P.add_argument(
"standby_state_power_W",
type=float,
help="Power consumption of the peripheral when in STANDBY state.",
)
args = P.parse_args()

peripheral_state = {
args.off_state_power_W: 'OFF',
args.on_state_power_W: 'ON',
args.standby_state_power_W: 'STANDBY'
args.off_state_power_W: "OFF",
args.on_state_power_W: "ON",
args.standby_state_power_W: "STANDBY",
}

inf = open(args.input_csv_filepath, 'r')
inf = open(args.input_csv_filepath, "r")
inr = csv.reader(inf)
next(inr) # ignore header
next(inr) # ignore header

outf = open(args.output_csv_filepath, 'w')
outw = csv.writer(outf, lineterminator='\n')
outw.writerow(['time','state'])
outf = open(args.output_csv_filepath, "w")
outw = csv.writer(outf, lineterminator="\n")
outw.writerow(["time", "state"])

for r in inr:
t = float(r[0])
p = float(r[1])
s = peripheral_state[p]
outw.writerow([t,s])
t = float(r[0])
p = float(r[1])
s = peripheral_state[p]
outw.writerow([t, s])

inf.close()
outf.close()
66 changes: 31 additions & 35 deletions analysis/geosnr.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,57 @@
#%%
# %%
from pathlib import Path

import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import plotly.graph_objects as go

#%% Helper functions

# %% Helper functions
def get_seed_name(x):
# param x: Path object
return int(x.name.split('-')[1].split('_')[1])
# param x: Path object
return int(x.name.split("-")[1].split("_")[1])


#%%
results_path = Path('../results')
results = [x for x in results_path.iterdir() if x.is_dir() and x.name.startswith('ntn_hap-seed')]
# %%
results_path = Path("../results")
results = [
x
for x in results_path.iterdir()
if x.is_dir() and x.name.startswith("ntn_hap-seed")
]

# Expected format: ntn_hap-seed_<seed>-<date>.<time>
results.sort(key=get_seed_name)

#%%
traces = [x / 'ntn-snr-trace.txt' for x in results]
# %%
traces = [x / "ntn-snr-trace.txt" for x in results]

# Read traces and store in a dataframe
df = pd.DataFrame()

for trace in traces:
temp_df = pd.read_csv(trace, sep=' ', header=None, names=['time', 'snr', 'propagationLoss'])
df = pd.concat((df, temp_df), axis=1)
temp_df = pd.read_csv(
trace, sep=" ", header=None, names=["time", "snr", "propagationLoss"]
)
df = pd.concat((df, temp_df), axis=1)

mean_snr = df[['snr']].mean(axis=1)
mean_propagation_loss = df[['propagationLoss']].mean(axis=1)
mean_snr = df[["snr"]].mean(axis=1)
mean_propagation_loss = df[["propagationLoss"]].mean(axis=1)

#%%
# %%
# plot SNR
fig = go.Figure()
fig.add_trace(go.Scatter(
y=mean,
mode='lines',
name='SNR'
))
fig.update_layout(
title='SNR',
xaxis_title='Time (s)',
yaxis_title='SNR (dB)'
)
fig.add_trace(go.Scatter(y=mean, mode="lines", name="SNR"))
fig.update_layout(title="SNR", xaxis_title="Time (s)", yaxis_title="SNR (dB)")
fig.show()

#%%
# %%
# plot propagation loss
fig = go.Figure()
fig.add_trace(go.Scatter(
y=mean_propagation_loss,
mode='lines',
name='PropagationLoss (dB)'
))
fig.add_trace(
go.Scatter(y=mean_propagation_loss, mode="lines", name="PropagationLoss (dB)")
)
fig.update_layout(
title='Propagation Loss',
xaxis_title='Time (s)',
yaxis_title='PropagationLoss (dB)'
title="Propagation Loss", xaxis_title="Time (s)", yaxis_title="PropagationLoss (dB)"
)
fig.show()
fig.show()
40 changes: 23 additions & 17 deletions analysis/get_rssi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,37 @@

# TODO -- this script does not work, yet

P = ArgumentParser(description='Retrieve RSSI measurements from summary XML file and export them in separate CSVs, '
'one per drone.')
P.add_argument('summary_filepath', type=str, help='Input summary XML file of the scenario.')
P = ArgumentParser(
description="Retrieve RSSI measurements from summary XML file and export them in separate CSVs, "
"one per drone."
)
P.add_argument(
"summary_filepath", type=str, help="Input summary XML file of the scenario."
)
args = P.parse_args()

base_path = PurePath(args.summary_filepath).parent
output_prefix = 'rssi-drone'
output_prefix = "rssi-drone"

xmlroot = ET.parse(args.summary_filepath).getroot()
zsp_addr = xmlroot.find('ZSPs').find('ZSP')
zsp_addr = xmlroot.find("ZSPs").find("ZSP")
print(zsp_addr)
zsp_addr = zsp_addr.find('NetDevices').find('NetDevice').find('phy').find('address').text
zsp_addr = (
zsp_addr.find("NetDevices").find("NetDevice").find("phy").find("address").text
)

drones = xmlroot.find('Drones').findall('Drone')
drones = xmlroot.find("Drones").findall("Drone")
i_drone = 0
for d in drones:
for nd in d.find('NetDevices').findall('NetDevice'):
dtype = nd.find('type').text
if (dtype == 'ns3::WifiNetDevice'):
with open(f'{base_path}/{output_prefix}_{i_drone}.csv', 'w') as outf:
csvw = csv.writer(outf, lineterminator='\n')
csvw.writerow(['time', 'rssi'])
for nd in d.find("NetDevices").findall("NetDevice"):
dtype = nd.find("type").text
if dtype == "ns3::WifiNetDevice":
with open(f"{base_path}/{output_prefix}_{i_drone}.csv", "w") as outf:
csvw = csv.writer(outf, lineterminator="\n")
csvw.writerow(["time", "rssi"])

for s in nd.find('signal').findall('rssi'):
attrs = s.attrib
csvw.writerow([attrs['time'], attrs['value']])
for s in nd.find("signal").findall("rssi"):
attrs = s.attrib
csvw.writerow([attrs["time"], attrs["value"]])

i_drone += 1
i_drone += 1
Loading

0 comments on commit 0fd4722

Please sign in to comment.