the-last-thing/code/parse_copenhagen.py

95 lines
2.4 KiB
Python
Raw Normal View History

2021-09-22 21:44:38 +02:00
#!/usr/bin/env python3
import sys
sys.path.insert(1, 'lib')
import argparse
import csv
from datetime import datetime
from geopy.distance import distance
import io
import lmdk_lib
import numpy as np
import os
import matplotlib.pyplot as plt
import time
import zipfile
# https://cloud.delkappa.com/s/ACMsDr2jnW3b6Np
# Copenhagen data format
# Header size
hdr = 1
# Timestamp
tim = 0
# User ID A
uid_a = 1
# User ID B
uid_b = 2
# Received Signal Strength Indicator (RSSI)
rssi = 3
def main(args):
# All the contacts [tim, uid_a, uid_b].
cont = []
try:
print('Extracting %s... ' %(os.path.abspath(args.arc)), end='', flush=True)
with zipfile.ZipFile(args.arc, 'r') as arc:
print('[OK]')
with io.TextIOWrapper(arc.open(args.cont), newline='\n') as dat:
try:
print('Finding contacts... ', end='', flush=True)
# Get the contacts by skipping the header
cont_l = list(csv.reader(dat, delimiter=','))[hdr:]
# Check each contact
for c in cont_l:
if c[uid_b] != '-1' and c[rssi] != '0' and c[uid_b] != '-2' and c[uid_a] != c[uid_b]:
# Add valid contact
cont.append([c[tim], c[uid_a], c[uid_b]])
print('[OK]')
except Exception as e:
print('[Error: %s]' %(e))
except Exception as e:
print('[Error: %s]' %(e))
# Save to results
lmdk_lib.save_data(args, np.array(cont), 'cont')
'''
Parse arguments.
Optional:
arc - The data archive file.
cont - The contacts data file.
res - The results archive file.
'''
def parse_args():
# Create argument parser.
parser = argparse.ArgumentParser()
# Mandatory arguments.
# Optional arguments.
parser.add_argument('-a', '--arc', help='The data archive file.', type=str, default='/home/manos/Cloud/Data/Copenhagen/Data.zip')
parser.add_argument('-c', '--cont', help='The contacts data file.', type=str, default='bt_symmetric.csv')
parser.add_argument('-r', '--res', help='The results archive file.', type=str, default='/home/manos/Cloud/Data/Copenhagen/Results.zip')
# Parse arguments.
args = parser.parse_args()
return args
if __name__ == '__main__':
try:
start_time = time.time()
main(parse_args())
end_time = time.time()
print('##############################')
print('Time : %.4fs' % (end_time - start_time))
print('##############################')
except KeyboardInterrupt:
print('Interrupted by user.')
exit()