Commit 688e29cc authored by Berk Geveci's avatar Berk Geveci

Initial commit

parents
This diff is collapsed.
import cinema_store
import itertools
class Explorer(object):
"""
Middleman that connects an arbitrary producing codes to the CinemaStore.
The purpose of this class is to run through the parameter sets, and tell a
set of tracks (in order) to do something with the parameter values
it cares about.
"""
def __init__(self,
cinema_store,
parameters, #these are the things that this explorer is responsible for and their ranges
tracks #the things we pass off values to in order to do the work
):
self.__cinema_store = cinema_store
self.parameters = parameters
self.tracks = tracks
@property
def cinema_store(self):
return self.__cinema_store
def list_parameters(self):
"""
parameters is an ordered list of parameters that the Explorer varies over
"""
return self.parameters
def prepare(self):
""" Give tracks a chance to get ready for a run """
if self.tracks:
for e in self.tracks:
res = e.prepare(self)
def execute(self, desc):
# Create the document/data product for this sample.
doc = cinema_store.Document(desc)
for e in self.tracks:
e.execute(doc)
self.insert(doc)
def explore(self, fixedargs=None):
"""Explore the problem space to populate the store"""
self.prepare()
ordered = self.list_parameters()
args = []
values = []
for name in ordered:
vals = self.cinema_store.get_parameter(name)['values']
args.append(name)
values.append(vals)
for element in itertools.product(*values):
desc = dict(itertools.izip(args, element))
if fixedargs != None:
desc.update(fixedargs)
self.execute(desc)
self.finish()
def finish(self):
""" Give tracks a chance to clean up after a run """
if self.tracks:
for e in self.tracks:
res = e.finish()
def insert(self, doc):
self.cinema_store.insert(doc)
class Track(object):
"""
abstract interface for things that can produce data
to use this:
caller should set up some visualization
then tie a particular set of parameters to an action with a track
"""
def __init__(self):
pass
def prepare(self, explorer):
""" subclasses get ready to run here """
pass
def finish(self):
""" subclasses cleanup after running here """
pass
def execute(self, document):
""" subclasses operate on parameters here"""
pass
"""
Module consisting of explorers and tracks that connect arbitrary paraview
pipelines to cinema stores.
"""
import explorers
import paraview.simple as simple
from vtk.numpy_interface import dataset_adapter as dsa
import numpy as np
class ImageExplorer(explorers.Explorer):
"""
An explorer that connects a paraview script's views to a store
and makes it save new images into the store.
"""
def __init__(self,
cinema_store, parameters, tracks,
view=None):
super(ImageExplorer, self).__init__(cinema_store, parameters, tracks)
self.view = view
def insert(self, document):
if not self.view:
return
image = self.view.CaptureWindow(1)
npview = dsa.WrapDataObject(image)
idata = npview.PointData[0]
ext = image.GetExtent()
width = ext[1]-ext[0]+1
height = ext[3]-ext[2]+1
imageslice = np.flipud(idata.reshape(height,width,3))
document.data = imageslice
super(ImageExplorer, self).insert(document)
class Camera(explorers.Track):
"""
A track that connects a paraview script's camera to the phi and theta tracks.
This allows the creation of spherical camera stores where the user can
view the data from many points around it.
"""
def __init__(self, center, axis, distance, view):
super(Camera, self).__init__()
try:
# Z => 0 | Y => 2 | X => 1
self.offset = (axis.index(1) + 1 ) % 3
except ValueError:
raise Exception("Rotation axis not supported", axis)
self.center = center
self.distance = distance
self.view = view
def execute(self, document):
import math
theta = document.descriptor['theta']
phi = document.descriptor['phi']
theta_rad = float(theta) / 180.0 * math.pi
phi_rad = float(phi) / 180.0 * math.pi
pos = [
float(self.center[0]) - math.cos(phi_rad) * self.distance * math.cos(theta_rad),
float(self.center[1]) + math.sin(phi_rad) * self.distance * math.cos(theta_rad),
float(self.center[2]) + math.sin(theta_rad) * self.distance
]
up = [
+ math.cos(phi_rad) * math.sin(theta_rad),
- math.sin(phi_rad) * math.sin(theta_rad),
+ math.cos(theta_rad)
]
self.view.CameraPosition = pos
self.view.CameraViewUp = up
self.view.CameraFocalPoint = self.center
@staticmethod
def obtain_angles(angular_steps=[10,15]):
import math
thetas = []
phis = []
theta_offset = 90 % angular_steps[1]
if theta_offset == 0:
theta_offset += angular_steps[1]
for theta in range(-90 + theta_offset,
90 - theta_offset + 1, angular_steps[1]):
theta_rad = float(theta) / 180.0 * math.pi
for phi in range(0, 360, angular_steps[0]):
phi_rad = float(phi) / 180.0 * math.pi
thetas.append(theta)
phis.append(phi)
return thetas, phis
class Slice(explorers.Track):
"""
A track that connects slice filters to a scalar valued parameter.
"""
def __init__(self, parameter, filt):
super(Slice, self).__init__()
self.parameter = parameter
self.slice = filt
def prepare(self, explorer):
super(Slice, self).prepare(explorer)
explorer.cinema_store.add_metadata({'type' : 'parametric-image-stack'})
def execute(self, doc):
o = doc.descriptor[self.parameter]
self.slice.SliceOffsetValues=[o]
class Contour(explorers.Track):
"""
A track that connects contour filters to a scalar valued parameter.
"""
def __init__(self, parameter, filt):
super(Contour, self).__init__()
self.parameter = parameter
self.contour = filt
self.control = 'Isosurfaces'
def prepare(self, explorer):
super(Contour, self).prepare(explorer)
explorer.cinema_store.add_metadata({'type': "parametric-image-stack"})
def execute(self, doc):
o = doc.descriptor[self.parameter]
self.contour.SetPropertyWithName(self.control,[o])
class Templated(explorers.Track):
"""
A track that connects any type of filter to a scalar valued
'control' parameter.
"""
def __init__(self, parameter, filt, control):
explorers.Track.__init__(self)
self.parameter = parameter
self.filt = filt
self.control = control
def execute(self, doc):
o = doc.descriptor[self.parameter]
self.filt.SetPropertyWithName(self.control,[o])
class ColorList():
"""
A helper that creates a dictionary of color controls for ParaView. The Color engine takes in
a Color name from the Explorer and looks up into a ColorList to determine exactly what
needs to be set to apply the color.
"""
def __init__(self):
self._dict = {}
def AddSolidColor(self, name, RGB):
self._dict[name] = {'type':'rgb','content':RGB}
def AddLUT(self, name, lut):
self._dict[name] = {'type':'lut','content':lut}
def getColor(self, name):
return self._dict[name]
class Color(explorers.Track):
"""
A track that connects a parameter to a choice of surface rendered color maps.
"""
def __init__(self, parameter, colorlist, rep):
super(Color, self).__init__()
self.parameter = parameter
self.colorlist = colorlist
self.rep = rep
def execute(self, doc):
o = doc.descriptor[self.parameter]
spec = self.colorlist.getColor(o)
if spec['type'] == 'rgb':
self.rep.DiffuseColor = spec['content']
self.rep.ColorArrayName = None
if spec['type'] == 'lut':
self.rep.LookupTable = spec['content']
self.rep.ColorArrayName = o
This diff is collapsed.
"""
Module consisting of explorers and tracks that connect arbitrary VTK
pipelines to cinema stores.
"""
import explorers
import vtk
from vtk.util import numpy_support
from vtk.numpy_interface import dataset_adapter as dsa
import numpy as np
class ImageExplorer(explorers.Explorer):
"""
An explorer that connects a VTK program's render window to a store
and makes it save new images into the store.
"""
def __init__(self, cinema_store, parameters, engines, rw):
super(ImageExplorer, self).__init__(cinema_store, parameters, engines)
self.rw = rw
self.w2i = vtk.vtkWindowToImageFilter()
self.w2i.SetInput(self.rw)
def insert(self, document):
self.rw.Render()
self.w2i.Modified()
self.w2i.Update()
image = self.w2i.GetOutput()
npview = dsa.WrapDataObject(image)
idata = npview.PointData[0]
ext = image.GetExtent()
width = ext[1]-ext[0]+1
height = ext[3]-ext[2]+1
imageslice = np.flipud(idata.reshape(width,height,3))
document.data = imageslice
super(ImageExplorer, self).insert(document)
class Clip(explorers.Track):
"""
A track that connects clip filters to a scalar valued parameter.
"""
def __init__(self, argument, clip):
super(Clip, self).__init__()
self.argument = argument
self.clip = clip
def prepare(self, explorer):
super(Clip, self).prepare(explorer)
explorer.cinema_store.add_metadata({'type': 'parametric-image-stack'})
def execute(self, doc):
o = doc.descriptor[self.argument]
self.clip.SetValue(o) #<---- the most important thing!
#TODO: add templated classes so we don't end up with a track for each
#vtkAlgorithm
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment