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:
observations.add((time, seer, seen))
graph = nx.Graph()
for time, seer, seen in observations:
if seen not in seers:
if not graph.has_edge(seer, seen):
graph.add_edge(seer, seen, {'times': set()})
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)
for contact in contacts:
contact = list(map(int, contact))
contact[0] = (contact[0] - start) * args['duty_cycle_length']
print('wrote: %s' % contact_path)
def parse_args(args):
parser = ArgumentParser()
help='path to csv to process')
help='directory to output metadata and contacts')
help='column used for time slots')
help='column used for devices that scan for others.')
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__':
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