Commit a55182d1 authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Adds script to convert shed data into csv.

parent 0ce2dcf5
#!/usr/bin/env python
import csv
import json
import sys
from argparse import ArgumentParser
from collections import defaultdict, namedtuple
from itertools import groupby
from os import path
import networkx as nx
def main(args):
seers = set()
observations = set()
with open(args['csv_path']) as csv_file:
csv_file = csv.DictReader(csv_file)
for row in csv_file:
time = int(row[args['time_column']])
seer = row[args['seer_column']]
seen = row[args['seen_column']]
if time >= 0:
seers.add(seer)
observations.add((time, seer, seen))
graph = nx.Graph()
for time, seer, seen in observations:
if seen not in seers:
continue
if not graph.has_edge(seer, seen):
graph.add_edge(seer, seen, {'times': set()})
graph[seer][seen]['times'].add(time)
nodes = max(nx.connected_components(graph), key=len)
nodes = {node: index for index, node in enumerate(nodes)}
contacts = []
for node_a, node_b, times in graph.edges(nbunch=nodes, data='times'):
times = sorted(times)
node_a, node_b = nodes[node_a], nodes[node_b]
for _, group in groupby(enumerate(times), lambda p: p[0]-p[1]):
contact = list(map(lambda g: g[1], group))
contacts.append((contact[0], node_a, node_b, True))
contacts.append((contact[-1] + 1, node_a, node_b, False))
contacts.sort(key=lambda c: c[0])
start = contacts[0][0]
duration = contacts[-1][0] - start
metadata = {
'nodes': len(nodes),
'contacts': len(contacts)/2, # account for both up and down
'duration': duration,
}
meta_path = path.join(args['outdir'], 'metadata.json')
with open(meta_path, 'w') as meta_file:
json.dump(metadata, meta_file)
print('wrote: %s' % meta_path)
contact_path = path.join(args['outdir'], 'contact.csv')
with open(contact_path, 'w') as contact_file:
writer = csv.writer(contact_file)
writer.writerow(['time','a','b','join'])
for contact in contacts:
contact = list(map(int, contact))
contact[0] = (contact[0] - start) * args['duty_cycle_length']
print(contact)
writer.writerow(contact)
print('wrote: %s' % contact_path)
def parse_args(args):
parser = ArgumentParser()
parser.add_argument('csv_path',
help='path to csv to process')
parser.add_argument('outdir',
help='directory to output metadata and contacts')
parser.add_argument('time_column',
help='column used for time slots')
parser.add_argument('seer_column',
help='column used for devices that scan for others.')
parser.add_argument('seen_column',
help='column used for devices are seen during a scan.')
parser.add_argument('--duty_cycle_length', default=300, type=int)
return vars(parser.parse_args(args))
if __name__ == '__main__':
exit(main(parse_args(sys.argv[1:])))
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