Skip to content

Commit

Permalink
Add Python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
MuellerDominik committed Jan 20, 2020
1 parent 0b32759 commit 5a0ec18
Show file tree
Hide file tree
Showing 8 changed files with 680 additions and 3 deletions.
129 changes: 129 additions & 0 deletions sw/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
94 changes: 94 additions & 0 deletions sw/python/aionfpga_daa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env python3

'''aionfpga ~ data acquisition application (daa)
Copyright (C) 2019 Dominik Müller and Nico Canzani
'''

import os
import shutil
import subprocess
from time import time

import modules.daa as daa

# location of the camera interface binary
cam_interface = r'..\cam-interface\cam-interface.exe'

def main():
try:
throwid = daa.fetch_field(daa.tab_settings, 0, 'throwid')
except Exception as e:
print(e)
input()
raise e

session_throwid = 1

for f in os.listdir(daa.dir_temp):
os.unlink(daa.dir_temp / f)

while True:
daa.clear()

print(f'Throw #{session_throwid} ({throwid} in total)\n')
print(r'''
_____ _
| __ \ | |
| |__) |___ __ _ __| |_ _
| _ // _ \/ _` |/ _` | | | |
| | \ \ __/ (_| | (_| | |_| |
|_| \_\___|\__,_|\__,_|\__, |
__/ |
|___/
''')

subp = [cam_interface]
res = subprocess.run(subp, stdout=subprocess.PIPE)
# stdout = res.stdout.decode('utf-8')

daa.clear()

print(f'Throw #{session_throwid} ({throwid} in total)\n')

valid = daa.choice('Throw valid?', 'No', 'Yes')
print('\n')

if valid:
inp = daa.print_objects_list()
print('\n')

throwid += 1
session_throwid += 1

ts = int(time())
obj = daa.objects[inp]
obj_san = daa.objects_san[inp]
framegood = True
partial = False

img_name = lambda i : f'{i}.png'
frame = lambda i : f'{ts}_{throwid}_{i}_{obj_san}.png'

rows = []
for idx in range(1, len(os.listdir(daa.dir_temp))+1):
row = [ts, throwid, idx, f"'{frame(idx)}'", f"'{obj}'", framegood, partial]
rows.append(row)

daa.insert_rows(daa.tab_frames, rows)

for idx, f in enumerate(os.listdir(daa.dir_temp)):
shutil.move(daa.dir_temp / img_name(idx),
daa.dir_frames / frame(idx+1))

try:
daa.update_field(daa.tab_settings, 0, 'throwid', throwid)
except Exception as e:
print(e)
input()
raise e
else:
for f in os.listdir(daa.dir_temp):
os.unlink(daa.dir_temp / f)

if __name__ == '__main__':
main()
36 changes: 36 additions & 0 deletions sw/python/aionfpga_db_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3

'''aionfpga ~ db backup
Copyright (C) 2019 Dominik Müller and Nico Canzani
'''

import csv
from time import time

import modules.daa as daa

def backup_db():
columns = daa.fetch_rows_and('COLUMNS', ['TABLE_SCHEMA', 'TABLE_NAME'],
[daa.database, daa.tab_frames], 'COLUMN_NAME',
database='INFORMATION_SCHEMA')
columns = [column[0] for column in columns]
rows = daa.fetch_all_rows(daa.tab_frames)
timestamp = int(time())
with open(f'aionfpga_db_{timestamp}.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',')
csv_writer.writerow(columns)
csv_writer.writerows(rows)
return timestamp

def print_db(timestamp):
with open(f'aionfpga_db_{timestamp}.csv', 'r', newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
print(row)

def main():
ts = backup_db()
print_db(ts)

if __name__ == '__main__':
main()
137 changes: 137 additions & 0 deletions sw/python/aionfpga_fix_frameid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/env python3

'''aionfpga ~ fix frameid
Copyright (C) 2019 Dominik Müller and Nico Canzani
'''

import os
import re
import shutil

import modules.daa as daa

directory = daa.dir_frames

# Functions for unit-testing

test_ext = '.txt'

test_ts = 1574937102
test_tid = 1
test_object = 'spiky-ball'
test_name = lambda i : f'{test_ts}_{test_tid}_{i}_{test_object}'

def unit_test():
for i in range(100):
create_unordered_files(i+1)
fix_frameid(test_ts, test_tid, i+1, test_object, test_ext)
res = check_files()
if res:
if res is not True:
print(f"Fixing {i+1} didn't work.")
break
else:
pass # no error
else:
print(f'Error in {i+1} (RegEx).')
break

def create_unordered_files(amount):
# remove everything in the frames directory
for f in os.listdir(directory):
os.unlink(f'{directory}\\{f}')

for i in range(amount):
with open(f'{directory}\\{i}', 'w') as f:
f.write(str(i))

for (i, f) in enumerate(os.listdir(directory)):
shutil.move(f'{directory}\\{f}', f'{directory}\\{test_name(i+1)}{test_ext}')

def check_files():
for file in os.listdir(directory):
with open(f'{directory}\\{file}', 'r') as f:
frameid_search = re.search("^[\\d]*_[\\d]*_([\\d]*)_[a-z-]*.[a-z]*$", file)
if frameid_search:
frameid = int(frameid_search.group(1))
else:
return None
text = f.read()
if int(frameid) != int(text)+1:
return False
return True

# Functions to fix the frameid

def move_file(name_1, name_2):
shutil.move(f'{directory}\\{name_1}', f'{directory}\\{name_2}')

# fix up to 100 frames
def fix_frameid(timestamp, throwid, frames, object_safe, file_ext='.png'):
nof = frames
rest_list_shift = [0 for x in range(nof) if x < 2]
rest_list = [x+1 for x in range(nof) if x < 2]

tmp_ext = '.tmp'
name = lambda i : f'{timestamp}_{throwid}_{i}_{object_safe}'

# add '.tmp' to all files
for i in range(frames):
move_file(f'{name(i+1)}{file_ext}', f'{name(i+1)}{file_ext}{tmp_ext}')

if nof > 10:
packets = [(p*11 + 3) for p in range(int((nof-10)/10))] # start of full packet
rest_list_shift.extend([10*(i+1) for (i, p) in enumerate(packets) if p+10 < 100])
rest_list.extend([p+10 for p in packets if p+10 < 100])

shift = 8
for i in packets: # handle full packets
for j in range(10):
move_file(f'{name(i+j)}{file_ext}{tmp_ext}', f'{name(i+j+shift)}{file_ext}')
shift = shift - 1

nof_left = nof - len(packets)*10 - 10 # len(packets)*10: handled by full packages / 10: 1-10
if nof_left > 0:
index = len(packets)*11 + 3
rest_list_shift.extend([rest_list_shift[len(rest_list_shift)-1]+nof_left]*(10-len(rest_list_shift)))
rest_list.extend(range(index+nof_left, nof+1))
for i in range(nof_left): # handle not full packets
move_file(f'{name(index)}{file_ext}{tmp_ext}', f'{name(index+shift)}{file_ext}')
index = index + 1
else:
rest_list_shift.extend([rest_list_shift[len(rest_list_shift)-1]]*(10-len(rest_list_shift)))
rest_list.extend(range(len(packets)*10+len(rest_list)+1, nof+1))
else:
rest_list_shift.extend([0 for x in range(nof-len(rest_list))])
rest_list.extend([x+3 for x in range(nof-len(rest_list))])

for (i, r) in enumerate(rest_list): # handle the lowest
move_file(f'{name(r)}{file_ext}{tmp_ext}', f'{name(r-rest_list_shift[i])}{file_ext}')

def main():
num_of_frames = 0
try:
throws = daa.fetch_field(daa.tab_settings, 0, 'throwid')
except Exception as e:
print(e)
input()
raise e
for i in range(throws):
try:
throw = daa.fetch_rows(daa.tab_frames, 'throwid', i+1, '*')
except Exception as e:
print(e)
input()
raise e
rowcount = len(throw)
ts = throw[0][1]
obj = throw[0][5]
obj_san = daa.objects_san[daa.objects.index(obj)]
fix_frameid(ts, i+1, rowcount, obj_san)
num_of_frames += rowcount
print(f'{i+1}: {ts} {obj_san} {rowcount}')
print(f'#Frames = {num_of_frames}')

if __name__ == '__main__':
# unit_test()
main()
Loading

0 comments on commit 5a0ec18

Please sign in to comment.