Skip to content

Instantly share code, notes, and snippets.

@JVero
Last active October 10, 2018 16:59
Show Gist options
  • Save JVero/d51c87347d459bf89eef96846ec64cfd to your computer and use it in GitHub Desktop.
Save JVero/d51c87347d459bf89eef96846ec64cfd to your computer and use it in GitHub Desktop.
import datetime
import re
import msvcrt
import time
import sys
def birthday(string):
regex = re.compile("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$")
a = regex.search(string)
try:
year, month, day = a[1], a[2], a[3]
try:
their_birthday = datetime.date(int(year), int(month), int(day))
except:
print(f"Invalid date, must be a valid date between 1900-1-1 and {datetime.datetime.now().strftime('%Y-%m-%d')}")
raise ValueError("Invalid Birthday")
before = datetime.date(1900, 1, 1)
after = datetime.datetime.now().date()
if their_birthday < before or their_birthday > after:
print(f"Invalid date, must be a valid date between 1900-1-1 and {datetime.datetime.now().strftime('%Y-%m-%d')}")
raise ValueError("Invalid Birthday")
return a[1], a[2], a[3]
except:
raise ValueError("Invalid birthday")
def patientcode(string):
regex = re.compile("^([A-z]{1,4})([0-9]{1,5})$")
if regex.match(string):
return string
else:
print("Invalid Pattern, should be 1-4 letters followed by 1-5 numbers, without spaces")
raise ValueError("Invalid Pattern")
def get_value(prompt, type_converter = str):
while True:
try:
value = type_converter(input(prompt+'\t'))
except ValueError:
print("Invalid Type!")
continue
else:
return value
print("Please input the following values:")
patientId = get_value("patient ID (with prefix, ie: PT12)", patientcode)
first_name = get_value("first name")
last_name = get_value("last name")
year, month, day = get_value('birthday (YYYY-MM-DD)', birthday)
condition = get_value('Condition? (control, parkinsons, alzheimers)').lower()
task = get_value('Task? (pointing, memory, peg)').lower()
pre_notes = get_value('Pre-experiment notes')
cooldown = 0
print("Press space to record a timestamp, press escape to exit")
num_samples = 0
markers = []
logs = [f"Recording started at {datetime.datetime.now()}"]
while True:
# logging and checking to exit
if msvcrt.kbhit() and cooldown == 0:
ch = msvcrt.getch()
if ch == b' ':
cooldown += 5000 # cooldown is sampling rate dependant, for 240hz polhemus, cooldown of 240 means 1 second minimum between markers
markers.append(datetime.datetime.now())
logs.append(f"Timestamp {len(markers)} recorded at {markers[-1]} at sample {num_samples}")
print(logs[-1])
elif ch == b'\x1b':
break
else:
print(ch)
elif msvcrt.kbhit() and cooldown != 0:
while msvcrt.kbhit():
ch = msvcrt.getch()
if ch == b' ':
print("Too fast, preventing redundant markers from the same button press", cooldown, '\n')
elif ch == '\x1b':
break
else:
pass
#print(ch, cooldown)
cooldown = max(cooldown-1, 0)
sys.stdout.flush()
num_samples += 1
logs.append(f"Recording ended at {datetime.datetime.now()}")
logs.append(f"Number of Samples: {num_samples}")
print('\n\n\n')
for log in logs:
print(log)
post_notes = get_value('\nPost-experiment notes')
date = datetime.datetime.now().strftime("%Y-%m-%d")
filename = f"{patientId}-{date}{task}".replace(" ", "_")
print(f"{filename}.txt")
print(patientId, first_name, last_name, year, month, day, condition)
with open(f'{filename}.txt', 'a+') as f:
f.writelines(f"PatientID: {patientId}\n")
f.writelines(f"Name: {first_name} {last_name}\n")
f.writelines(f"Birthday: {year}-{month}-{day}\n")
f.writelines(f"Condition: {condition}\n")
f.writelines(f"Pre-Experiment Notes: {pre_notes}\n")
for log in logs:
f.write(log+'\n')
f.writelines(post_notes+'\n\n\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment