Skip to content

Instantly share code, notes, and snippets.

@kschlottmann
Created December 1, 2018 16:53
Show Gist options
  • Select an option

  • Save kschlottmann/2832765be96ff32df102be7f19acc4ec to your computer and use it in GitHub Desktop.

Select an option

Save kschlottmann/2832765be96ff32df102be7f19acc4ec to your computer and use it in GitHub Desktop.
Takes Aeon reader in/out data and caculcates the maximum number of readers
import json
import csv
import os
#function that takes a series of data formatted date, start time, end time and returns max simultaneous calls
def maximumOverlap(calls):
times = []
for call in calls:
date, startTime, endTime = call
times.append((startTime, 'start'))
times.append((endTime, 'end'))
times = sorted(times)
count = 0
maxCount = 0
for time in times:
if time[1] == 'start':
count += 1 # increment on arrival/start
else:
count -= 1 # decrement on departure/end
maxCount = max(count, maxCount) # maintain maximum
return date, maxCount
# calls a directory of text files
# files should be csvs formatted as date, start time, end time
# loop through each file, and pull data into an array
directory = 'C:\Users\Kevin\Desktop\maxReaders\source'
for filename in os.listdir(directory):
if filename.endswith(".txt"):
f = open(filename)
calls = []
with open(filename) as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) # change contents to floats
for row in reader: # each row is a list
calls.append(row)
#call function
print(maximumOverlap(calls))
continue
else:
continue
NB: Very manual, file names and paths hardcoded, etc.
1) Start with a spreadsheet like this of reader in/out times:
Date Time In Date Time Out
10/31/2017 1:22 PM 10/31/2017 1:28:00 PM
10/31/2017 1:23 PM 10/31/2017 1:29:00 PM
10/31/2017 1:28 PM 10/31/2017 8:45:00 PM
10/31/2017 1:29 PM 10/31/2017 8:45:00 PM
10/31/2017 1:43 PM 10/31/2017 6:08:00 PM
2) Converted to numbers and removed third column, save as .csv file
Date Time In Time Out
43039 , 0.556944444 , 0.561111111
43039 , 0.557638889 , 0.561805556
43039 , 0.561111111 , 0.864583333
43039 , 0.561805556 , 0.864583333
3) Run splitter.py (below) across the csv, resulting in one .txt file for each date formatted like this:
43039 , 0.556944444 , 0.561111111
4) Run maxReaders.py (below)
5) Copy output, paste into Excel, and convert back to human-readable date:
Date Max readers
Monday, November 06, 2017 24
Thursday, August 09, 2018 23
Friday, April 20, 2018 22
Friday, April 13, 2018 21
Tuesday, June 26, 2018 21
import csv
from itertools import groupby
for key, rows in groupby(csv.reader(open("foo.csv")),
lambda row: row[0]):
with open("%s.txt" % key, "w") as output:
for row in rows:
output.write(",".join(row) + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment