Skip to content

Commit

Permalink
cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
rmari committed Apr 21, 2016
1 parent ccc3cc7 commit a5f651b
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 204 deletions.
160 changes: 92 additions & 68 deletions src/homerFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,38 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

from string import *
import sys, os
import sys
import os
import numpy as np
import io
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtOpenGL import *
from PySide import QtCore
import homerFrame
import numpy.core.defchararray as charray

color_fname = "homer_palette.py"
if os.path.isfile(color_fname):
sys.path.append(".")
import homer_palette
color_palette = np.array(homer_palette.color_palette)
else:
color_palette = np.array([Qt.black, Qt.gray, Qt.white, Qt.green, Qt.yellow, Qt.red, Qt.blue, Qt.magenta, Qt.darkGreen, Qt.cyan, Qt.gray, Qt.white, Qt.green, Qt.yellow, Qt.red, Qt.blue, Qt.magenta, Qt.darkGreen, Qt.cyan])
color_palette = np.array([QtCore.Qt.black, QtCore.Qt.gray, QtCore.Qt.white,
QtCore.Qt.green, QtCore.Qt.yellow, QtCore.Qt.red,
QtCore.Qt.blue, QtCore.Qt.magenta,
QtCore.Qt.darkGreen, QtCore.Qt.cyan,
QtCore.Qt.gray, QtCore.Qt.white, QtCore.Qt.green,
QtCore.Qt.yellow, QtCore.Qt.red, QtCore.Qt.blue,
QtCore.Qt.magenta, QtCore.Qt.darkGreen,
QtCore.Qt.cyan])


class homerFile:
def __init__(self, filename):

self.is_file=True
self.fname=filename
self.chunksize = 5000000#10000000
self.is_file = True
self.fname = filename
self.chunksize = 5000000 # 10000000
self.frames = []
self.read_all = False
self.infile = open(self.fname,'r')
self.infile = open(self.fname, 'r')
self.trailing_frame = []
self.trailing_attributes = []

Expand All @@ -53,64 +59,73 @@ def read_chunk(self):

ftype = np.float32

in_raw_data = np.core.defchararray.partition(np.asarray(self.infile.readlines(self.chunksize), dtype=np.str_), ' ')
in_raw_data =\
charray.partition(
np.asarray(self.infile.readlines(self.chunksize),
dtype=np.str_), ' ')
if in_raw_data.shape[0] == 0:
self.read_all = True
return False

in_raw_data = in_raw_data[:,[0,2]]
in_raw_data = in_raw_data[:, [0, 2]]

while not np.any(in_raw_data[:,0]=='\n'):
b = np.core.defchararray.partition(np.asarray(self.infile.readlines(self.chunksize), dtype=np.str_), ' ')
while not np.any(in_raw_data[:, 0] == '\n'):
b = charray.partition(
np.asarray(self.infile.readlines(self.chunksize),
dtype=np.str_), ' ')
if b.shape[0] == 0:
break
b = b[:,[0,2]]
in_raw_data=np.vstack((in_raw_data,b))
b = b[:, [0, 2]]
in_raw_data = np.vstack((in_raw_data, b))

obj_nb = np.shape(in_raw_data)[0]

attributes = np.zeros(obj_nb, dtype=[('r', ftype), ('@', np.object), ('y', ftype)])
attributes = np.zeros(obj_nb, dtype=[('r', ftype),
('@', np.object),
('y', ftype)])
all_att_mask = np.ones(obj_nb, dtype=np.bool)
for at in ['r','y']:
att_mask = in_raw_data[:,0]==at
for at in ['r', 'y']:
att_mask = in_raw_data[:, 0] == at
all_att_mask -= att_mask
pos = np.nonzero(att_mask)[0] # locate the attributes changes
pos = np.nonzero(att_mask)[0] # locate the attributes changes
if len(self.trailing_attributes):
attributes[at][:pos[0]] = self.trailing_attributes[at][-1]

if len(pos)>0:
if len(pos) > 0:
for i in range(len(pos)-1):
attributes[at][pos[i]:pos[i+1]] = in_raw_data[:,1][pos[i]]
attributes[at][pos[-1]:] = in_raw_data[:,1][pos[-1]]
attributes[at][pos[i]:pos[i+1]] = in_raw_data[:, 1][pos[i]]
attributes[at][pos[-1]:] = in_raw_data[:, 1][pos[-1]]

at = '@' # special case: color change
att_mask = in_raw_data[:,0]==at
at = '@' # special case: color change
att_mask = in_raw_data[:, 0] == at
all_att_mask -= att_mask
pos = np.nonzero(att_mask)[0] # locate the attributes changes
pos = np.nonzero(att_mask)[0] # locate the attributes changes
if len(self.trailing_attributes):
attributes[at][:pos[0]] = self.trailing_attributes[at][-1]

if len(pos)>0:
if len(pos) > 0:
for i in range(len(pos)-1):
attributes[at][pos[i]:pos[i+1]] = color_palette[in_raw_data[:,1][pos[i]].astype(np.int)]
attributes[at][pos[-1]:] = color_palette[in_raw_data[:,1][pos[-1]].astype(np.int)]

attributes[at][pos[i]:pos[i+1]] =\
color_palette[in_raw_data[:, 1][pos[i]].astype(np.int)]
attributes[at][pos[-1]:] =\
color_palette[in_raw_data[:, 1][pos[-1]].astype(np.int)]

in_raw_data = in_raw_data[all_att_mask] # remove the lines defining color, size, layer, etc
# remove the lines defining color, size, layer, etc
in_raw_data = in_raw_data[all_att_mask]
attributes = attributes[all_att_mask]
if len(self.trailing_frame):
in_raw_data = np.vstack((self.trailing_frame,in_raw_data))
attributes = np.concatenate((self.trailing_attributes,attributes))
in_raw_data = np.vstack((self.trailing_frame, in_raw_data))
attributes = np.concatenate((self.trailing_attributes, attributes))

# now split frames
framebreaks = np.nonzero(in_raw_data[:,0]=='\n')[0]+1
framebreaks = np.nonzero(in_raw_data[:, 0] == '\n')[0]+1
in_raw_data = np.split(in_raw_data, framebreaks)
attributes = np.split(attributes, framebreaks)

# and split according to object types
obj_list = ['c','s','l','p','t', 'tt']
obj_list = ['c', 's', 'l', 'p', 't', 'tt']

if len(in_raw_data)>1:
if len(in_raw_data) > 1:
frange = range(len(in_raw_data)-1)
else:
frange = [0]
Expand All @@ -121,70 +136,79 @@ def read_chunk(self):
frame = in_raw_data[i][:-1]
attrs = attributes[i][:-1]

obj_masks = {o: frame[:,0]==o for o in obj_list}
obj_masks = {o: frame[:, 0] == o for o in obj_list}

o='c'
o = 'c'
if np.count_nonzero(obj_masks[o]):
obj_vals[o] = np.reshape(np.genfromtxt(frame[:,1][obj_masks[o]], dtype=np.float),(-1,3))
obj_vals[o][:,2] = -obj_vals[o][:,2]
obj_vals[o] =\
np.reshape(np.genfromtxt(frame[:, 1][obj_masks[o]],
dtype=np.float), (-1, 3))
obj_vals[o][:, 2] = -obj_vals[o][:, 2]
obj_attrs[o] = attrs[obj_masks[o]]

o='s'
o = 's'
if np.count_nonzero(obj_masks[o]):
obj_vals[o] = np.reshape(np.genfromtxt(frame[:,1][obj_masks[o]], dtype=np.float),(-1,6))
obj_vals[o][:,2] = -obj_vals[o][:,2]
obj_vals[o][:,5] = -obj_vals[o][:,5]
obj_vals[o] =\
np.reshape(np.genfromtxt(frame[:, 1][obj_masks[o]],
dtype=np.float), (-1, 6))
obj_vals[o][:, 2] = -obj_vals[o][:, 2]
obj_vals[o][:, 5] = -obj_vals[o][:, 5]
obj_attrs[o] = attrs[obj_masks[o]]


o='l'
o = 'l'
if np.count_nonzero(obj_masks[o]):
obj_vals[o] = np.reshape(np.genfromtxt(frame[:,1][obj_masks[o]], dtype=np.float),(-1,6))
obj_vals[o][:,2] = -obj_vals[o][:,2]
obj_vals[o][:,5] = -obj_vals[o][:,5]
obj_vals[o] =\
np.reshape(np.genfromtxt(frame[:, 1][obj_masks[o]],
dtype=np.float), (-1, 6))
obj_vals[o][:, 2] = -obj_vals[o][:, 2]
obj_vals[o][:, 5] = -obj_vals[o][:, 5]
obj_attrs[o] = attrs[obj_masks[o]]

o='p'
o = 'p'
if np.count_nonzero(obj_masks[o]):
split_vals = np.core.defchararray.partition(frame[:,1][obj_masks[o]], ' ')
polygon_sizes = split_vals[:,0].astype(np.int)
polygon_coords = split_vals[:,2]
split_vals = charray.partition(frame[:, 1][obj_masks[o]], ' ')
polygon_sizes = split_vals[:, 0].astype(np.int)
polygon_coords = split_vals[:, 2]

full_str = ''
for str_a in polygon_coords:
full_str +=str_a
full_str += str_a

obj_vals[o] = (polygon_sizes,np.reshape(np.fromstring(full_str, sep=' '),(-1,3)))
obj_vals[o][1][:,2] = -obj_vals[o][1][:,2]
obj_vals[o] = (polygon_sizes,
np.reshape(np.fromstring(full_str, sep=' '),
(-1, 3)))
obj_vals[o][1][:, 2] = -obj_vals[o][1][:, 2]
obj_attrs[o] = attrs[obj_masks[o]]

o='t'
o = 't'
if np.count_nonzero(obj_masks[o]):
split_vals = np.asarray(np.core.defchararray.split(frame[:,1][obj_masks[o]], maxsplit=3))
x = np.array([a[0] for a in split_vals], dtype=ftype) # split returns array of lists, sorry :(
y = np.array([a[1] for a in split_vals], dtype=ftype) # split returns array of lists, sorry :(
split_vals =\
np.asarray(charray.split(frame[:, 1][obj_masks[o]],
maxsplit=3))
x = np.array([a[0] for a in split_vals], dtype=ftype)
y = np.array([a[1] for a in split_vals], dtype=ftype)
# y = -y
z = np.array([a[2] for a in split_vals], dtype=ftype) # split returns array of lists, sorry :(
z = np.array([a[2] for a in split_vals], dtype=ftype)
z = -z
text = np.core.defchararray.strip(np.array([a[3] for a in split_vals], dtype=np.str),"\n") # split returns array of lists, sorry :(
text = charray.strip(np.array([a[3] for a in split_vals],
dtype=np.str), "\n")

obj_vals[o] = (np.column_stack((x,y,z)), text)
obj_vals[o] = (np.column_stack((x, y, z)), text)
obj_attrs[o] = attrs[obj_masks[o]]

o='tt'
o = 'tt'
if np.count_nonzero(obj_masks[o]):
text = np.core.defchararray.strip(frame[:,1][obj_masks[o]], "\n")
text = charray.strip(frame[:, 1][obj_masks[o]], "\n")
obj_vals[o] = text
obj_attrs[o] = attrs[obj_masks[o]]
self.frames.append(homerFrame.homerFrame(obj_vals, obj_attrs))

if len(in_raw_data)>1:
if len(in_raw_data) > 1:
self.trailing_frame = in_raw_data[-1]
self.trailing_attributes = attributes[-1]
else:
self.trailing_frame = []
self.trailing_attributes = []


self.is_init = False
return True
Loading

0 comments on commit a5f651b

Please sign in to comment.