Commit 90869845 authored by Maxim Onciul's avatar Maxim Onciul
Browse files

good data creation

parent c62e0511
#!/usr/bin/env python3
import argparse
import os
import subprocess
from typing import Dict, List
from contextlib import ExitStack
from dataclasses import make_dataclass
from tempfile import gettempdir
import filecmp
from measure import MeasurePi
from pexpect import pxssh
from sklearn.model_selection import ParameterGrid
import pandas as pd
tempdir = os.path.join(gettempdir(), "ty82xile")
os.makedirs(tempdir, exist_ok=True)
param_grid = ParameterGrid(
{
# "repeat": [0] * 3,
"num_runs": [4],
"num_runs": [1],
"k": ["8"],
"m": ["32"],
"l": ["5"],
"d": ["30", "25"],
# "d": ["32", "25"],
"d": ["31", "25", "20"],
"rx": [0, 1],
# "b": ["0", "1", "2", "5", "10", "20"],
"b": ["0"],
# "B": ["10", "20", "50", "100"],
"B": ["10"],
"b": ["0", "1", "5", "10", "20"],
"B": ["10", "20", "50"],
}
)
ips = ["maximPi1", "maximPi2"]
common = {
"logfile": "exp/measure.log",
"exp": "some_unique_ID",
......@@ -32,8 +37,97 @@ common = {
pi_dir = "/home/pi/pi_files/experiment/"
Run = make_dataclass(
"Run",
[
("id", int),
("k", int),
("m", int),
("l", int),
("d", int),
("b", int),
("B", int),
("derr", int),
("cerr", int),
("egy", float),
("tx", str),
("rx", str),
],
)
def write_exp_description(conf, idx, fname):
with open(conf["logfile"](idx), "r") as f:
lines = f.readlines()
print("Measured:")
print("\t", lines[-4])
measurement = lines[-4]
with open(fname, "a") as f:
f.write(
f"""
# Experiment
- This experiment is executed as {['rx', 'tx'][conf['rx']]}
- One code word of the convolutional code is 64 bytes long
- l: {conf['l']}
- d: {conf['d']}
- k: {conf['k']}
- m: {conf['m']}
- Measured: {measurement}
"""
)
return measurement.split(" ")[1]
def count_errs(conf, idx):
rx, tx = ips[conf["rx"]], ips[conf["rx"] ^ 1]
source = f"pi@{tx}:/home/pi/pi_files/exps/{conf['exp']}/data"
reconst = f"pi@{rx}:/home/pi/pi_files/exps/{conf['exp']}/reconst"
cipher_clean = f"pi@{ips[1]}:/home/pi/pi_files/exps/{conf['exp']}/cipher"
cipher_corrupt = f"pi@{ips[1]}:/home/pi/pi_files/exps/{conf['exp']}/corrupt"
remotes = [source, reconst, cipher_clean, cipher_corrupt]
files = [f"{tempdir}/{file}" for file in ["data", "reconst", "cipher", "corrupt"]]
for file, remote in zip(files, remotes):
subprocess.run(["scp", remote, file])
# Manual comparison
def hex_to_bits(line):
# ignore '\n'
for ch in line[:-1]:
yield bin(int(ch, 16))[2:].zfill(4)
def cmp_bits(line1, line2):
for four_bits1, four_bits2 in zip(
hex_to_bits(line1.replace(" ", "")), hex_to_bits(line2.replace(" ", ""))
):
for b1, b2 in zip(four_bits1, four_bits2):
if b1 == b2:
yield 0
else:
yield 1
def cmp_two_files(file1, file2):
with ExitStack() as exit_stack:
sf = exit_stack.enter_context(open(file1, "r"))
rf = exit_stack.enter_context(open(file2, "r"))
return sum(
sum(cmp_bits(sline, rline))
for sline, rline in zip(sf.readlines(), rf.readlines())
)
return cmp_two_files(*files[:2]), cmp_two_files(*files[2:])
def run_experiment(clients: List[pxssh.pxssh], conf: Dict[str, str]):
curr = os.path.dirname(os.path.abspath(__file__))
folder = os.path.join(curr, "exps")
os.makedirs(folder, exist_ok=True)
fname = os.path.join(folder, "desc")
rx, tx = clients[conf["rx"]], clients[conf["rx"] ^ 1]
......@@ -59,34 +153,34 @@ def run_experiment(clients: List[pxssh.pxssh], conf: Dict[str, str]):
# collect receiver
rx.prompt()
yield Run(
idx,
conf["k"],
conf["m"],
conf["l"],
conf["d"],
conf["b"],
conf["B"],
*count_errs(conf, idx),
float(write_exp_description(conf, idx, fname)),
ips[conf["rx"] ^ 1],
ips[conf["rx"]],
)
def write_eval(conf: Dict[str, str]):
curr = os.path.dirname(os.path.abspath(__file__))
folder = os.path.join(curr, "exps")
os.makedirs(folder, exist_ok=True)
fname = os.path.join(folder, "desc")
for idx in range(conf["num_runs"]):
def create_data(args, clients):
# run
for idx, conf in enumerate(param_grid):
conf["exp"] = f"{args.name}_{idx}"
conf["logdir"] = lambda i: os.path.join(
"/home/cip/iuk/ty82xile/stud/pi_files/eval", f"{args.name}_{i}"
)
conf["logfile"] = lambda i: os.path.join(conf["logdir"](i), "picocom.log")
with open(conf["logfile"](idx), "r") as f:
lines = f.readlines()
print("Measured:")
print("\t", lines[-4])
measurement = lines[-4]
with open(fname, "a") as f:
f.write(
f"""
# Experiment
- This experiment is executed as {['rx', 'tx'][conf['rx']]}
- One code word of the convolutional code is 64 bytes long
- l: {conf['l']}
- d: {conf['d']}
- k: {conf['k']}
- m: {conf['m']}
- Measured: {measurement}
"""
)
for i in range(conf["num_runs"]):
os.makedirs(conf["logdir"](i), exist_ok=True)
yield from run_experiment(clients, conf)
def main():
......@@ -94,33 +188,28 @@ def main():
parser.add_argument("name", help="experiment name")
args = parser.parse_args()
with ExitStack() as stack:
ips = ["maximPi1", "maximPi2"]
paths = [os.path.join(f"/home/cip/iuk/ty82xile/stud/pi_files/eval/{args.name}_{client}") for client in ips]
clients = [pxssh.pxssh(timeout=None, encoding="utf8") for _ in ips]
paths = [
os.path.join(f"/home/cip/iuk/ty82xile/stud/pi_files/eval/{args.name}_{client}")
for client in ips
]
clients = [pxssh.pxssh(timeout=None, encoding="utf8") for _ in ips]
with ExitStack() as stack:
files = [stack.enter_context(open(path, "w")) for path in paths]
for client, file in zip(clients, files):
client.logfile = file
for i in range(len(ips)):
clients[i].login(ips[i], "pi")
# run
for idx, conf in enumerate(param_grid):
conf["exp"] = f"{args.name}_{idx}"
conf["logdir"] = lambda i: os.path.join("/home/cip/iuk/ty82xile/stud/pi_files/eval", f"{args.name}_{i}")
conf["logfile"] = lambda i: os.path.join(conf["logdir"](i), "picocom.log")
for client, ip in zip(clients, ips):
client.login(ip, "pi")
for i in range(conf["num_runs"]):
os.makedirs(conf["logdir"](i), exist_ok=True)
dataframe = pd.DataFrame(list(create_data(args, clients)))
run_experiment(clients, conf)
write_eval(conf)
for client in clients:
client.logout()
for i in range(len(ips)):
clients[i].logout()
dataframe.to_csv(
f"/home/cip/iuk/ty82xile/stud/pi_files/eval/{args.name}_res.csv", sep=","
)
if __name__ == "__main__":
......
......@@ -3,12 +3,12 @@ from contextlib import ContextDecorator
import pexpect as pe
_PROG = '/usr/bin/picocom'
_ARGS = ['-b', '115200', '/dev/ttyUSB0']
_START = 'Terminal ready'
_SETUP = ['1-2:* uJ*', '3-4:* uJ*', '5-6:* uJ*', pe.TIMEOUT]
_INIT = 'V1-2:*'
_TERM = 'Thanks for using picocom'
_PROG = "/usr/bin/picocom"
_ARGS = ["-b", "115200", "/dev/ttyUSB0"]
_START = "Terminal ready"
_SETUP = ["1-2:* uJ*", "3-4:* uJ*", "5-6:* uJ*", pe.TIMEOUT]
_INIT = "V1-2:*"
_TERM = "Thanks for using picocom"
class MeasurePi(ContextDecorator):
......@@ -17,23 +17,21 @@ class MeasurePi(ContextDecorator):
with MeasurePi():
# trigger the things to measure
"""
def __init__(self, logfile: str = 'picocom.log'):
def __init__(self, logfile: str = "picocom.log"):
self.logfile = logfile
self.process = None
def __enter__(self) -> None:
self.process = pe.spawn(_PROG,
_ARGS,
encoding='utf-8',
codec_errors='replace')
self.process = pe.spawn(_PROG, _ARGS, encoding="utf-8", codec_errors="replace")
idx = self.process.expect([_START, pe.TIMEOUT], timeout=1)
if idx != 0:
print('Measurement could not be started.')
print('Is process alive? ', self.process.isalive())
print("Measurement could not be started.")
print("Is process alive? ", self.process.isalive())
return
self.ofile = open(self.logfile, 'w')
self.process.send(str('e'))
self.ofile = open(self.logfile, "w")
self.process.send(str("e"))
self.process.expect(_INIT)
# egy = self.process.expect(_SETUP, timeout=1)
......@@ -42,12 +40,12 @@ class MeasurePi(ContextDecorator):
# return
self.process.logfile = self.ofile
print('Measure started.')
self.process.send(str('0'))
print("Measure started.")
self.process.send(str("0"))
def __exit__(self, exc_type, exc_value, traceback) -> None:
self.process.sendcontrol(str('a'))
self.process.sendcontrol(str('x'))
self.process.sendcontrol(str("a"))
self.process.sendcontrol(str("x"))
if exc_type:
print(traceback)
......@@ -57,8 +55,8 @@ class MeasurePi(ContextDecorator):
idx = self.process.expect([_TERM, pe.TIMEOUT], timeout=3)
self.ofile.close()
if idx == 0:
print('Terminated normally.')
print("Terminated normally.")
else:
print('Timeout, killing process...')
print("Timeout, killing process...")
self.process.close()
print('Killed process.')
print("Killed process.")
......@@ -14,7 +14,7 @@ def receive_data():
config["socket"].bind(config["me"])
with open(config["cipher_file"], "w") as f:
for _ in range(config['k']):
for _ in range(config["k"]):
f.write(config["socket"].recvfrom(4096)[0].decode("utf-8"))
# f.write('\n')
......@@ -23,26 +23,28 @@ def receive_data():
# as pi2: add corruption, pi1: use received data
rx_file = run_pi.map_pi(
lambda _, __: config["cipher_file"],
lambda _, __: run_pi.create_errs(config))
call: List[str] = [config['code']] + [
'-e',
'-l', str(config['l']),
'-d', str(config['d']),
'-m', str(config['m']),
'-k', str(config['k']),
lambda _, __: config["cipher_file"], lambda _, __: run_pi.create_errs(config)
)
call: List[str] = [config["code"]] + [
"-e",
"-l",
str(config["l"]),
"-d",
str(config["d"]),
"-m",
str(config["m"]),
"-k",
str(config["k"]),
]
print(call)
with ExitStack() as exit_stack:
rx_stream = exit_stack.enter_context(open(rx_file, 'r'))
reconst = exit_stack.enter_context(open(config["reconst_file"], 'w'))
rx_stream = exit_stack.enter_context(open(rx_file, "r"))
reconst = exit_stack.enter_context(open(config["reconst_file"], "w"))
subprocess.run(call,
encoding='UTF-8',
stdin=rx_stream,
stdout=reconst)
subprocess.run(call, encoding="UTF-8", stdin=rx_stream, stdout=reconst)
if __name__ == '__main__':
if __name__ == "__main__":
receive_data()
......@@ -79,12 +79,9 @@ def create_errs(config):
len_word = len(f.readline())
call = [config["errs"]] + [
"-l",
str(len_word),
"-b",
str(config["b"]),
"-B",
str(config["B"]),
"-l", str(len_word),
"-b", str(config["b"]),
"-B", str(config["B"]),
]
with ExitStack() as exit_stack:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment