Skip to content

Commit

Permalink
Merge pull request #62 from /issues/51
Browse files Browse the repository at this point in the history
Issues/51
  • Loading branch information
LemurPwned committed Apr 11, 2018
2 parents d938c74 + c5bffee commit bcb7436
Show file tree
Hide file tree
Showing 19 changed files with 3,543 additions and 3,729 deletions.
71 changes: 9 additions & 62 deletions ColorPolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@
from cython_modules.cython_parse import getLayerOutline
from cython_modules.color_policy import multi_iteration_dot_product
from multiprocessing import Pool
from multiprocessing_parse import asynchronous_pool_order
import scipy.signal
from copy import deepcopy


def asynchronous_pool_order(func, args, object_list):
pool = Pool()
output_list = []
multiple_results = [pool.apply_async(func, (object_list[i], *args))
for i in range(len(object_list))]
for result in multiple_results:
value = result.get()
output_list.append(value)
return output_list

class ColorPolicy:
def __init__(self):
self.averaging_kernel = None
Expand Down Expand Up @@ -91,47 +82,6 @@ def conv2d(self, a, f):
subM = strd(a, shape = s, strides = a.strides * 2)
return np.einsum('ij,ijkl->kl', f, subM)

def apply_normalization(self, color_array, xc, yc, zc):
"""
normalizes a large input color_array to unit vectors
:param color_array: to be normalized, numpy array
:param xc: nodes in x direction
:param yc: nodes in y direction
:parac zc: nodes in z direction
"""
pool = Pool()
if zc > 1:
multiple_results = [pool.apply_async(
self.multilayer_normalization,
(color_array[i], xc, yc, zc))
for i in range(len(color_array))]
else:
multiple_results = [pool.apply_async(
self.atomic_normalization,
(color_array[i], xc, yc, zc))
for i in range(len(color_array))]
color_array = [result.get(timeout=12) for result
in multiple_results]
return np.array(color_array)

def multilayer_normalization(self, multilayer_matrix, xc, yc, zc):
layer_array = []
for layer in multilayer_matrix:
layer_array.append(self.atomic_normalization(layer, xc, yc, 1))
return np.array(layer_array)

def atomic_normalization(self, color_array, xc, yc, zc):
"""
performs unit normalization on tiny arrays
:param xc: nodes in x direction
:param yc: nodes in y direction
:parac zc: nodes in z direction
"""
normalized_color_array = np.array([x/np.linalg.norm(x)
if x.any() else [0.0,0.0,0.0] for x in color_array])\
.reshape(xc*yc*zc, 3)
return normalized_color_array

def apply_vbo_format(self, color_array, k=24):
"""
transforms a given numpy array matrix representing vecotrs in space
Expand All @@ -140,13 +90,9 @@ def apply_vbo_format(self, color_array, k=24):
:param k: indicates how many times should vertex be padded
"""
pool = Pool()
multiple_results = [pool.apply_async(self.color_matrix_flatten,
(p, k)) for p in color_array]
new_color_matrix = []
for result in multiple_results:
repeated_array = result.get(timeout=20)
new_color_matrix.append(repeated_array)
return new_color_matrix
output = asynchronous_pool_order(self.color_matrix_flatten, (k, ),
color_array)
return np.array(output, dtype='float32')

def color_matrix_flatten(self, vector, times):
return np.repeat(vector, times, axis=0).flatten()
Expand All @@ -167,17 +113,18 @@ def standard_procedure(self, outline, color, iterations, averaging, xc, yc, zc,
disableDot=True):
color = np.array(color)
outline = np.array(outline)
if decimate != 1:
color = color[:,::decimate,:]
outline = outline[::decimate, :]
if type(picked_layer) == int:
# if single layer is picked modify memory data
zc = 1
layer_thickness = xc*yc
picked_layer = picked_layer*layer_thickness
color = color[:, picked_layer:picked_layer+layer_thickness, :]
outline = outline[picked_layer:picked_layer+layer_thickness]

# input is in form (iterations, zc*yc*xc, 3) and vectors are normalized
if decimate != 1:
color = color[:,::decimate,:]
outline = outline[::decimate, :]
if averaging != 1:
averaging_intensity = float(1/averaging)
# generate mask of shape (zc*yc*xc, 3)
Expand All @@ -199,7 +146,7 @@ def standard_procedure(self, outline, color, iterations, averaging, xc, yc, zc,
(vector_set,), color)
else:
dotted_color = color
dotted_color = np.array(dotted_color)
dotted_color = np.array(dotted_color)
outline = np.array(outline)
# this should have shape (iterations, zc*yc*xc, 3)
if not decimate:
Expand Down
7 changes: 6 additions & 1 deletion Widgets/openGL_widgets/AbstractGLContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ def __init__(self, parent=None):
self.drawing_function = None
self.function_select = 'fast'

def shareData(self, **kwargs):
super().shareData(**kwargs)
self.receivedOptions()

def receivedOptions(self):
print(self.options)
self.color_scheme = self.options[0]
self.normalize = self.options[0]
self.averaging = int(self.options[1])
self.layer = self.options[2]
self.scale = int(self.options[3])
Expand Down Expand Up @@ -55,6 +59,7 @@ def initializeGL(self):
"""
Initializes openGL context and scenery
"""

gl.glClearColor(0.0, 0.0, 0.0, 1.0)
gl.glEnable(gl.GL_DEPTH_TEST)

Expand Down
31 changes: 26 additions & 5 deletions Widgets/openGL_widgets/CubicGLContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

from PyQt5.QtWidgets import QWidget
from PyQt5.Qt import Qt
from PyQt5.QtCore import QPoint, QThread

from cython_modules.cython_parse import generate_cubes, getLayerOutline, genCubes

from Widgets.openGL_widgets.AbstractGLContext import AbstractGLContext
from cython_modules.cython_parse import getLayerOutline, genCubes
from cython_modules.color_policy import multi_iteration_normalize

from Widgets.openGL_widgets.AbstractGLContext import AbstractGLContext

from ColorPolicy import ColorPolicy
from multiprocessing import Pool
Expand All @@ -24,13 +26,14 @@ def __init__(self, data_dict):
self.vertices = 0

self.buffers = None
self.fbo = None
self.buffer_len = 0

self.shareData(**data_dict)


def shareData(self, **kwargs):
super().shareData(**kwargs)
self.receivedOptions()

self.spacer = self.spacer*self.scale
xc = int(self.omf_header['xnodes'])
Expand All @@ -57,15 +60,24 @@ def shareData(self, **kwargs):
# this is arbitrary
self.spacer *= decimate*3

if self.normalize:
multi_iteration_normalize(self.color_vectors)

if self.function_select == 'fast':
self.drawing_function = self.vbo_cubic_draw
self.buffers = None
# if vbo drawing is selected, do additional processing
self.color_vectors = custom_color_policy.apply_vbo_format(self.color_vectors)
self.vectors_list, self.vertices = genCubes(self.vectors_list,
self.spacer)
print(np.array(self.vectors_list).shape, self.vertices)
print(np.array(self.color_vectors).shape)
self.buffer_len = len(self.color_vectors[0])

# TODO: temporary fix, dont know why x4, should not be multiplied
# at all!
self.buffer_len = len(self.color_vectors[0])*4
print("BUFFER LEN" , self.buffer_len)

elif self.function_select == 'slow':
self.drawing_function = self.slower_cubic_draw

Expand All @@ -74,7 +86,8 @@ def create_vbo(self):
# vertices buffer
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffers[0])
gl.glBufferData(gl.GL_ARRAY_BUFFER,
np.array(self.vectors_list, dtype='float32'),
np.array(self.vectors_list,
dtype='float32').flatten(),
gl.GL_STATIC_DRAW)
# color buffer
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffers[1])
Expand All @@ -87,13 +100,21 @@ def create_vbo(self):
def vbo_cubic_draw(self):
if self.buffers is None:
self.buffers = self.create_vbo()
if self.fbo is None:
fbo_handler = self.defaultFramebufferObject()
# self.fbo = gl.glGenFramebuffers(1)
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, fbo_handler)
self.fbo = 1
else:
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.buffers[1])
# later move to set_i function so that reference changes
# does not cause buffer rebinding
gl.glBufferSubData(gl.GL_ARRAY_BUFFER, 0, self.buffer_len,
np.array(self.color_vectors[self.i],
dtype='float32').flatten())

# if self.grabFramebuffer().save('./SCR/'+str(self.i), 'JPG'):
# print("successfull saving")
self.draw_vbo()

def draw_vbo(self):
Expand Down
33 changes: 9 additions & 24 deletions Widgets/openGL_widgets/VectorGLContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

from ctypes import c_void_p
from PyQt5.Qt import Qt
from PyQt5.QtCore import QPoint
from PyQt5.QtCore import QPoint, QThread

from cython_modules.cython_parse import generate_cubes, getLayerOutline
from cython_modules.cython_parse import getLayerOutline
from cython_modules.color_policy import multi_iteration_normalize

import numpy as np
import OpenGL.GLU as glu
import OpenGL.GL as gl
import math as mt
from multiprocessing import Pool


class VectorGLContext(AbstractGLContext, QWidget):
def __init__(self, data_dict):
super().__init__()
Expand All @@ -27,7 +27,7 @@ def __init__(self, data_dict):

def shareData(self, **kwargs):
super().shareData(**kwargs)
self.receivedOptions()
self.spacer = 0.2

self.drawing_function = self.slow_arrow_draw
self.vectors_list = getLayerOutline(self.omf_header)
Expand All @@ -50,30 +50,15 @@ def shareData(self, **kwargs):
self.vector_set,
self.decimate,
self.disableDot)

if self.normalize:
multi_iteration_normalize(self.color_vectors)

if decimate is not None:
# this is arbitrary
self.spacer *= decimate*3

if self.function_select == 'fast':
# doesnt work yet
normalized = [[0,0,0]]
self.drawing_function = self.vbo_arrow_draw
# transform into interleaved vbo format
self.color_vectors = custom_color_policy.apply_vbo_format(self.color_vectors,
k=2)
self.vectors_list = \
custom_color_policy.apply_interleaved_format(self.vectors_list,
normalized)
self.buffer_len = len(self.color_vectors[0])
self.vectors_list = np.array(self.vectors_list)

self.color_vectors = np.array(self.color_vectors)
print(self.color_vectors[0], self.color_vectors.any())
self.vertices = 1600/2
print("DRAWING SHAPES {}, {}".format(self.vectors_list.shape,
self.color_vectors.shape))
elif self.function_select == 'slow':
self.drawing_function = self.slow_arrow_draw
self.drawing_function = self.slow_arrow_draw

def slow_arrow_draw(self):
for vector, color in zip(self.vectors_list,
Expand Down
8 changes: 3 additions & 5 deletions Windows/MainWindowTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def setupUi(self, MainWindow):
self.gridLayout.setObjectName("gridLayout")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 863, 22))
self.menubar.setGeometry(QtCore.QRect(0, 0, 863, 26))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
Expand Down Expand Up @@ -64,7 +64,6 @@ def setupUi(self, MainWindow):
self.actionWindow3Delete.setObjectName("actionWindow3Delete")
self.menuFile.addAction(self.actionLoad_File)
self.menuFile.addAction(self.actionLoad_Directory)
self.menuEdit.addAction(self.actionPlot)
self.menuEdit.addAction(self.actionAnimation)
self.menuEdit.addSeparator()
self.menuEdit.addAction(self.actionWindow0Delete)
Expand All @@ -75,7 +74,6 @@ def setupUi(self, MainWindow):
self.menuView.addAction(self.action2_Windows_Grid)
self.menuView.addAction(self.action4_Windows_Grid)
self.menuOptions.addAction(self.actionPerformance)
self.menuOptions.addAction(self.actionVectors)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.menubar.addAction(self.menuView.menuAction())
Expand All @@ -98,8 +96,8 @@ def retranslateUi(self, MainWindow):
self.actionPlot.setText(_translate("MainWindow", "Plot"))
self.actionAnimation.setText(_translate("MainWindow", "Animation"))
self.action4_Windows_Grid.setText(_translate("MainWindow", "4 Windows Grid"))
self.actionPerformance.setText(_translate("MainWindow", "Performance"))
self.actionVectors.setText(_translate("MainWindow", "Vectors"))
self.actionPerformance.setText(_translate("MainWindow", "Frame directory"))
self.actionVectors.setText(_translate("MainWindow", "Save"))
self.actionWindow0Delete.setText(_translate("MainWindow", "Window0 delete"))
self.actionWindow1Delete.setText(_translate("MainWindow", "Window1 delete"))
self.actionWindow2Delete.setText(_translate("MainWindow", "Window2 delete"))
Expand Down
9 changes: 7 additions & 2 deletions Windows/PerfOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def __init__(self, layer_size=None):
self.setupUi(self)
self.loaded = True
self.layer_size = layer_size['znodes']
if self.layer_size == 1:
self.checkBox.setEnabled(False)
self.checkBox.setChecked(True)

self.decimate = 1
self.averaging = 1
Expand Down Expand Up @@ -105,16 +108,17 @@ def sizeChange(self):

def optionsVerifier(self):
# order as follows: color scheme, averaging, layer
# checkBox_5 is normalize
if self.checkBox.isChecked():
optionsList = [self.comboBox.currentText(),
optionsList = [ self.checkBox_5.isChecked(),
self.averaging,
'all',
self.horizontalSlider_3.value(),
self.parseVectors(),
self.decimate,
self.color_disable]
else:
optionsList = [self.comboBox.currentText(),
optionsList = [ self.checkBox_5.isChecked(),
self.averaging,
self.horizontalSlider_2.value(),
self.horizontalSlider_3.value(),
Expand Down Expand Up @@ -148,6 +152,7 @@ def setEventHandler(self, handler):
self.eventHandler = handler

def accept(self):
self.hide()
try:
self.options = self.optionsVerifier()
self.eventHandler(self.options)
Expand Down
Loading

0 comments on commit bcb7436

Please sign in to comment.