Skip to content

Instantly share code, notes, and snippets.

@sapphire-arches
Created May 9, 2018 00:18
Show Gist options
  • Save sapphire-arches/c7c0342046dec2dd0efced078ffa5797 to your computer and use it in GitHub Desktop.
Save sapphire-arches/c7c0342046dec2dd0efced078ffa5797 to your computer and use it in GitHub Desktop.
from os import path
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import os
import pyqtgraph as pg
import pyqtgraph.exporters as pg_export
import time
OUTPUT_PATH = './vis_output'
def clean_point_name(point):
""" Converts:
"Mid(bb995[0])" to 0995_000_b
and
"Start(bb995[0])" to 0995_000_a
"""
paren_start = point.find('(')
squar_start = point.find('[')
squar_end = point.find(']')
point_type = None
if point[1] == 'S':
point_type = 'a'
else:
point_type = 'b'
bb_nr = int(point[paren_start + 3:squar_start])
stmt = int(point[squar_start + 1:squar_end])
return '{:06d}_{:04d}_{}'.format(bb_nr, stmt, point_type)
max_region = 200
def dump_point(point, relations):
global max_region
point_name = clean_point_name(point)
def relation_to_number(r):
global max_region
s = r.find('#') + 1
e = r.find('r')
re = int(r[s:e])
if re > max_region:
max_region = re
return re
relations = map(lambda x: (relation_to_number(x[0]), relation_to_number(x[1])), relations)
relations = list(relations)
return point_name, relations
def main():
try:
os.makedirs(OUTPUT_PATH)
except FileExistsError as e:
pass
FILE = 'clap_reference/subset.facts'
# FILE = 'out/subset.facts'
with open(FILE) as f:
relations_at_point = {}
for line in f:
line = line.split()
if line[0] not in relations_at_point:
relations_at_point[line[0]] = []
relations_at_point[line[0]].append((line[1], line[2]))
relation_items = sorted(map(lambda x: dump_point(x[0], x[1]), relations_at_point.items()))
del relations_at_point
print('Relations loaded and sorted')
# Create window
app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
view = pg.GraphicsLayoutWidget()
mw.setCentralWidget(view)
mw.setWindowTitle('relation view')
# Wet out areas
w1 = view.addPlot(title=relation_items[501][0])
# win.setWindowtitle('region relations')
s1 = pg.ScatterPlotItem()
w1.addItem(s1);
exporter = pg_export.ImageExporter(s1)
exporter.parameters()['width'] = 1280
exporter.parameters()['height'] = 720
# s1.enableAutoRange('x', 0.95)
# s1.enableAutoRange('y', 0.95)
# s1.setAspectLocked(True)
i = 0
pos_count = 0
REPORT_EVERY = 300
print(len(relation_items))
start_time = time.time()
point_count = 0
for k, v in relation_items:
pos_count += 1
s1.setData([{'pos': x, 'data': k} for x in v])
point_count += len(v)
# exporter.export(os.path.join('.', 'vis_output', k + '.png'))
if pos_count % REPORT_EVERY == 0:
end_time = time.time()
delta_time = end_time - start_time
print('Just finished {}, rate: {:0.07f} frames per second / {:0.07f} points per second ({} / {})'.format(
k,
REPORT_EVERY / delta_time,
point_count / delta_time,
pos_count,
len(relation_items)
))
point_count = 0
start_time = end_time
w1.setTitle(k)
mw.show()
break
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment