Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions pycinema/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io
import logging as log
import os
import glob
import importlib.resources

CORE_NAN_VALUES = ['NaN', 'NAN', 'nan']
Expand Down Expand Up @@ -62,7 +63,7 @@ def getModulePath():
return str(os.path.dirname(resource_path))

except FileNotFoundError:
raise FileNotFoundError(f"Resource '{resource}' not found in package '{package}'")
raise FileNotFoundError(f"Resource 'Core.py' not found in package 'pycinema'")
except Exception as e:
raise Exception(f"An error occurred: {e}")

Expand Down Expand Up @@ -107,8 +108,8 @@ def getPathForScript(name):
else:
if os.path.isfile(possible_script + ".py"):
scriptpath = possible_script + ".py"
else:
log.debug("script directory does not exist: \'" + scriptdir + "\'")
else:
log.debug("script directory does not exist: \'" + scriptdir + "\'")

return scriptpath

Expand All @@ -120,6 +121,9 @@ def getPathForScript(name):
def getColumnIndexFromTable(table, colname):
ID = -1

if len(table) < 1:
return ID

colnames = table[0]
if colname in colnames:
ID = colnames.index(colname)
Expand Down Expand Up @@ -162,9 +166,9 @@ def getColumnFromTable(table, colname, autocast=False, nan_remove=False, nan_rep
i += 1

# TODO: create a separate function call to determine column type
if autocast:
if (any(isNumber(item) for item in cleaned_column) and not (isinstance(cleaned_column, np.ndarray) and np.issubdtype(cleaned_column.dtype, np.floating))):
cleaned_column = np.array(cleaned_column, dtype=float)
if autocast and not isinstance(cleaned_column, np.ndarray):
if any(isNumber(item) for item in cleaned_column):
cleaned_column = np.array(cleaned_column, dtype=float)

return cleaned_column

Expand All @@ -173,28 +177,28 @@ def getColumnFromTable(table, colname, autocast=False, nan_remove=False, nan_rep
def getTableExtent(table):
try:
nRows = len(table)
if nRows < 1:
if nRows < 2:
return (0,0)
nCols = len(table[1])
for i in range(2,nRows):
if len(table[i]) != nCols:
return (nRows,-1)
return (nRows,nCols)
except:
except (TypeError, IndexError, KeyError, AttributeError):
return (-1,-1)

################################################################################
# Image Class
################################################################################
class Image():
image_id_counter = -1
image_id_counter = 0

def __init__(self, channels=None, meta=None):
self.meta = meta or {}
self.channels = channels or {}
if 'id' not in self.meta:
self.meta['id'] = Image.image_id_counter
Image.image_id_counter += -1
Image.image_id_counter += 1

def __str__(self):
result = '{ PyCinemaImage:\n'
Expand Down Expand Up @@ -302,7 +306,7 @@ def set(self, value, update=True, propagate_back=False):
try:
np.testing.assert_equal(self._value,value)
return
except:
except Exception:
pass
# if self._value == value:
# return
Expand Down Expand Up @@ -387,7 +391,11 @@ def trigger(eventName, data):
for listener in Filter._listeners[eventName]:
listener(data)

def __init__(self, inputs={}, outputs={}):
def __init__(self, inputs=None, outputs=None):
if inputs is None:
inputs = {}
if outputs is None:
outputs = {}
if Filter._debug:
print('created',self)
cls = self.__class__.__name__
Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/CSVReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _update(self):
rows = csv.reader(csvfile, delimiter=',')
for row in rows:
table.append(row)
except:
except (IOError, OSError, PermissionError, csv.Error):
log.error("Unable to open file: '" + csvPath + "'")
self.outputs.table.set([[]])
return 0
Expand Down
13 changes: 9 additions & 4 deletions pycinema/filters/Calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import numpy as np
from asteval import Interpreter

class Calculator(Filter):

Expand Down Expand Up @@ -33,14 +34,18 @@ def _update(self):
if isNumber(oTable[1][c]):
variables[oTable[0][c]] = np.array([float(row[c]) for row in oTable[1:]])

variables['__result'] = ''

expression = self.inputs.expression.get().strip()
if expression=='':
expression = '0'

exec('__result = '+expression,variables)
result = variables['__result']
aeval = Interpreter()
aeval.symtable.update(variables)

result = aeval(expression)

if aeval.error:
err = aeval.error[0]
raise RuntimeError(err.get_error())

if type(result) not in [list, np.ndarray]:
result = [result for i in range(0,extent[0]-1)]
Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/CinemaDatabaseReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _update(self):
rows = csv.reader(csvfile, delimiter=',')
for row in rows:
table.append(row)
except:
except (IOError, OSError, PermissionError, csv.Error):
log.error(" Unable to open data.csv")
self.outputs.table.set([[]])
return 0
Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/ColorMapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def updateWidgets(self):
for widgets in self.widgets:
widgets['c'].setEnabled(len(images)>0 and len(images[0].channels)>0)

if len(images)<1 or self.channel_model==None:
if len(images)<1 or self.channel_model is None:
return
else:
iChannel = self.inputs.channel.get()
Expand Down
6 changes: 3 additions & 3 deletions pycinema/filters/DepthCompositing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def toList(self,data):
return [data]

def getKeys(self,image,compose):
if compose[0]==None:
if compose[0] is None:
return image.meta.keys()
ignore = [compose[0]] + ['^id','^file','^camera','^resolution','_range']
return [p for p in image.meta.keys() if not any([re.search(i, p, re.IGNORECASE) for i in ignore])]
Expand Down Expand Up @@ -145,7 +145,7 @@ def _update(self):
try:
for i in imagesA:
i.getChannel(depthChannel)
except:
except Exception:
self.outputs.images.set(imagesA)
return 1

Expand All @@ -158,7 +158,7 @@ def _update(self):
for key, images in imagesMap.items():
result = images[0].copy()

if metaCompositing[0]!=None and 'composition_mask' not in result.channels:
if metaCompositing[0] is not None and 'composition_mask' not in result.channels:
result.channels['composition_mask'] = numpy.full(result.shape[:2], metaCompositing[1][str(result.meta[metaCompositing[0]])], dtype=numpy.ubyte)
for i in range(1,len(images)):
result = self.compose(result,images[i],depthChannel)
Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/ImageAnnotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __get_font(self, size):
except IOError:
continue

if font==None:
if font is None:
print('unable to detect font')

return font
Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/ImageReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _update(self):
file = h5py.File(path, 'r')
for (g,v) in [('channels',image.channels), ('meta',image.meta)]:
group = file.get(g)
if group==None:
if group is None:
raise ValueError('h5 file not formatted correctly')
for k in group.keys():
data = numpy.atleast_1d(numpy.squeeze(numpy.array(group.get(k))))
Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/MLTFReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _update(self):
models[int(row[0])] = model

#check if training configuration exists, if not give error
except:
except (IOError, OSError, PermissionError, csv.Error, ValueError, IndexError):
log.error('[ERROR] Unable to open ML Model Directory')
self.outputs.models.set([])
return 0
Expand Down
2 changes: 2 additions & 0 deletions pycinema/filters/ParallelCoordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def __init__(self, parameter, filter):
self.bar.hide()

def snapToValueIdx(self,y):
if self.y1 == self.y0:
return 0
l = (y - self.y0) / (self.y1 - self.y0)
l = max(min(l,1),0)
return round(l*(self.n_values-1))
Expand Down
5 changes: 2 additions & 3 deletions pycinema/filters/Python.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ def _update(self):
self.watcher.addPath(code)

# fetch code
script_file = open(code, "r")
code = script_file.read()
script_file.close()
with open(code, "r") as script_file:
code = script_file.read()
elif len(watched_files)>0:
self.watcher.removePaths(watched_files)

Expand Down
16 changes: 12 additions & 4 deletions pycinema/filters/Shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ class Shader(Filter):
quad = None
fbo = None

def __init__(self, inputs={}, outputs={}, textures=[], varyings=['uv'], quad=True):
def __init__(self, inputs=None, outputs=None, textures=None, varyings=None, quad=True):
if inputs is None:
inputs = {}
if outputs is None:
outputs = {}
if textures is None:
textures = []
if varyings is None:
varyings = ['uv']

# program
self.program = Shader.ctx.program(
Expand All @@ -34,8 +42,8 @@ def __init__(self, inputs={}, outputs={}, textures=[], varyings=['uv'], quad=Tru
super().__init__(inputs, outputs)

def initFramebuffer(self,res,components=[1],dtypes=['f1']):
if Shader.fbo==None or Shader.fbo.size!=res:
if Shader.fbo!=None:
if Shader.fbo is None or Shader.fbo.size!=res:
if Shader.fbo is not None:
Shader.fbo.release()
if len(components)==1 and dtypes[0]=='f1':
Shader.fbo = Shader.ctx.simple_framebuffer(res)
Expand Down Expand Up @@ -110,5 +118,5 @@ def releaseTextures(self):
1.0, 1.0
]).astype('f4').tobytes()
)
except:
except Exception:
log.warning("Unable to setup OpenGL context.")
2 changes: 1 addition & 1 deletion pycinema/filters/ShaderFXAA.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _update(self):
try:
for image in images:
results.append( self.render(image) )
except:
except Exception:
self.outputs.images.set(images)
return 1

Expand Down
18 changes: 11 additions & 7 deletions pycinema/filters/ShaderIBS.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,22 @@ def _update(self):
images = self.inputs.images.get()

# set uniforms
self.program['radius'].value = float(self.inputs.radius.get())
self.program['samples'].value = int(self.inputs.samples.get())
self.program['diff_area'].value = float(self.inputs.diff.get())
self.program['silhouette'].value = float(self.inputs.silhouette.get()*500)
self.program['ambient'].value = float(self.inputs.ambient.get())
self.program['luminance'].value = float(self.inputs.luminance.get())
try:
self.program['radius'].value = float(self.inputs.radius.get())
self.program['samples'].value = int(self.inputs.samples.get())
self.program['diff_area'].value = float(self.inputs.diff.get())
self.program['silhouette'].value = float(self.inputs.silhouette.get()*500)
self.program['ambient'].value = float(self.inputs.ambient.get())
self.program['luminance'].value = float(self.inputs.luminance.get())
except (ValueError, TypeError):
self.outputs.images.set(images)
return 1

# render images
try:
for image in images:
results.append( self.render(image) )
except:
except Exception:
self.outputs.images.set(images)
return 1

Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/ShaderLineAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def _update(self):
try:
for image in images:
results.append( self.render(image) )
except:
except Exception:
self.outputs.images.set(images)
return 1

Expand Down
14 changes: 9 additions & 5 deletions pycinema/filters/ShaderPBR.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,20 @@ def _update(self):
images = self.inputs.images.get()

# set uniforms
self.program['uAmbient'].value = float(self.inputs.ambient.get())
self.program['uDiffuse'].value = float(self.inputs.diffuse.get())
self.program['uRoughness'].value = float(self.inputs.roughness.get())
self.program['uMetallic'].value = float(self.inputs.metallic.get())
try:
self.program['uAmbient'].value = float(self.inputs.ambient.get())
self.program['uDiffuse'].value = float(self.inputs.diffuse.get())
self.program['uRoughness'].value = float(self.inputs.roughness.get())
self.program['uMetallic'].value = float(self.inputs.metallic.get())
except (ValueError, TypeError):
self.outputs.images.set(images)
return 1

# render images
try:
for image in images:
results.append( self.render(image) )
except:
except Exception:
self.outputs.images.set(images)
return 1

Expand Down
14 changes: 9 additions & 5 deletions pycinema/filters/ShaderPhong.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,20 @@ def _update(self):
images = self.inputs.images.get()

# set uniforms
self.program['uAmbient'].value = float(self.inputs.ambient.get())
self.program['uDiffuse'].value = float(self.inputs.diffuse.get())
self.program['uSpecular'].value = float(self.inputs.specular.get())
self.program['uExponent'].value = float(self.inputs.exponent.get())
try:
self.program['uAmbient'].value = float(self.inputs.ambient.get())
self.program['uDiffuse'].value = float(self.inputs.diffuse.get())
self.program['uSpecular'].value = float(self.inputs.specular.get())
self.program['uExponent'].value = float(self.inputs.exponent.get())
except (ValueError, TypeError):
self.outputs.images.set(images)
return 1

# render images
try:
for image in images:
results.append( self.render(image) )
except:
except Exception:
self.outputs.images.set(images)
return 1

Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/ShaderPointAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def _update(self):
try:
for image in images:
results.append( self.render(image) )
except:
except Exception:
self.outputs.images.set(images)
return 1

Expand Down
2 changes: 1 addition & 1 deletion pycinema/filters/ShaderSSAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _update(self):
try:
for image in images:
results.append( self.render(image) )
except:
except Exception:
self.outputs.images.set(images)
return 1

Expand Down
Loading
Loading