Beispiel #1
0
def load_params(params_file, module_name="params"):
    """ Helper to load a python file used to provide input parameters. """
    pscript = "from visit_utils import *\n" + open(params_file).read()
    params = define_module(module_name, pscript)
    # auto setup 'root' PropertyTree
    if not 'root' in dir(params):
        root = PropertyTree()
        for val in dir(params):
            if val.count("__") != 2:
                root[val] = params.__dict__[val]
        params.__dict__['root'] = root
    return params
Beispiel #2
0
 def __init__(self, name, context):
     if not self.__validate():
         raise InvalidFilterDefinitionError(self)
     self.name = name
     self.params = PropertyTree(init=self.default_params)
     self.context = context
Beispiel #3
0
class Filter(object):
    """
    Base class for filters.
    """
    def __init__(self, name, context):
        if not self.__validate():
            raise InvalidFilterDefinitionError(self)
        self.name = name
        self.params = PropertyTree(init=self.default_params)
        self.context = context

    def input(self, id):
        """
        Fetches an input obj.

        Usage in subclass:
          Fetch by index:
            self.input(0) # first input
          Fetch by name:
            self.input("named_port") # input from port "named_port"
        """
        if isinstance(id, int):
            id = self.input_ports[id]
        return self.__inputs[id]

    def execute(self):
        """
        Main filter entry point.
        """
        return None

    def set_state_vector(self, svec):
        """
        Sets the filter node's state vector.

        (Used by workspace runtime.)
        """
        self.state_vector = svec

    def set_params(self, params):
        self.params.update(params)

    def set_inputs(self, inputs):
        """
        Sets the filter node's inputs.

        (Used by workspace runtime.)
        """
        self.__inputs = inputs

    def set_context(self, context):
        """
        Sets the filter node's context.

        (Used by workspace runtime.)
        """
        self.context = context

    def number_of_input_ports(self):
        """
        Returns the number of input ports.
        """
        return len(self.input_ports)

    def parameters(self):
        return self.params.properties()

    def __getitem__(self, path):
        """
        Fetches an entry from the params PropertyTree.
        """
        return self.params[path]

    def __setitem__(self, path, obj):
        """
        Sets an entry in the params PropertyTree.
        """
        self.params[path] = obj

    def __str__(self):
        """
        String pretty print.
        """
        res = "%s:[%s]" % (self.name, self.filter_type)
        res += "(%s)" % str(self.context)
        return res

    @classmethod
    def default_parameters(cls):
        if isinstance(cls.default_params, PropertyTree):
            return cls.default_params.properties()
        else:
            return dict(cls.default_params)

    @classmethod
    def info(cls):
        """
        Returns class level pretty print string.
        """
        vals = cls.__check_fields()
        res = "Filter Name     = "
        if vals[0]:
            res += cls.filter_type
        else:
            res += "{error: missing 'filter_type'}"
        res += "\nInput Port(s)   = "
        if vals[1]:
            res += str(cls.input_ports)
        else:
            res += "{error: missing 'input_ports'}"
        res += "\nDefault Params: "
        if vals[2]:
            res += str(cls.default_params)
        else:
            res += "{error: missing 'default_params'}"
        res += "\nOutput Port:    "
        if vals[3]:
            res += str(cls.output_port)
        else:
            res += "{error: missing 'output_port'}"
        return res + "\n"

    @classmethod
    def __validate(cls):
        """
        Helper that checks if the class members required by the Filter
        interface are defined.
        """
        vals = cls.__check_fields()
        if False in vals:
            return False
        return True

    @classmethod
    def __check_fields(cls):
        """
        Returns a list of boolean values that represent which of the class
        members required by the Filter interface are defined.
        """
        cdir = dir(cls)
        return [
            val in cdir for val in
            ["filter_type", "input_ports", "default_params", "output_port"]
        ]
Beispiel #4
0
 def __init__(self, name, context):
     if not self.__validate():
         raise InvalidFilterDefinitionError(self)
     self.name     = name
     self.params   = PropertyTree(init=self.default_params)
     self.context  = context
Beispiel #5
0
class Filter(object):
    """
    Base class for filters.
    """
    def __init__(self, name, context):
        if not self.__validate():
            raise InvalidFilterDefinitionError(self)
        self.name     = name
        self.params   = PropertyTree(init=self.default_params)
        self.context  = context
    def input(self,id):
        """
        Fetches an input obj.

        Usage in subclass:
          Fetch by index:
            self.input(0) # first input
          Fetch by name:
            self.input("named_port") # input from port "named_port"
        """
        if isinstance(id,int):
            id = self.input_ports[id]
        return self.__inputs[id]
    def execute(self):
        """
        Main filter entry point.
        """
        return None
    def set_state_vector(self,svec):
        """
        Sets the filter node's state vector.

        (Used by workspace runtime.)
        """
        self.state_vector = svec
    def set_params(self,params):
        self.params.update(params)
    def set_inputs(self,inputs):
        """
        Sets the filter node's inputs.

        (Used by workspace runtime.)
        """
        self.__inputs = inputs
    def set_context(self,context):
        """
        Sets the filter node's context.

        (Used by workspace runtime.)
        """
        self.context = context
    def number_of_input_ports(self):
        """
        Returns the number of input ports.
        """
        return len(self.input_ports)
    def parameters(self):
        return self.params.properties()
    def __getitem__(self,path):
        """
        Fetches an entry from the params PropertyTree.
        """
        return self.params[path]
    def __setitem__(self,path,obj):
        """
        Sets an entry in the params PropertyTree.
        """
        self.params[path] = obj
    def __str__(self):
        """
        String pretty print.
        """
        res  = "%s:[%s]" % (self.name, self.filter_type)
        res += "(%s)"  % str(self.context)
        return res
    @classmethod
    def default_parameters(cls):
        if isinstance(cls.default_params,PropertyTree):
            return cls.default_params.properties()
        else:
            return dict(cls.default_params)
    @classmethod
    def info(cls):
        """
        Returns class level pretty print string.
        """
        vals = cls.__check_fields()
        res = "Filter Name     = "
        if vals[0]:
            res += cls.filter_type
        else:
            res += "{error: missing 'filter_type'}"
        res += "\nInput Port(s)   = "
        if vals[1]:
            res +=  str(cls.input_ports)
        else:
            res += "{error: missing 'input_ports'}"
        res += "\nDefault Params: "
        if vals[2]:
            res += str(cls.default_params)
        else:
            res += "{error: missing 'default_params'}"
        res += "\nOutput Port:    "
        if vals[3]:
            res += str(cls.output_port)
        else:
            res += "{error: missing 'output_port'}"
        return res + "\n"
    @classmethod
    def __validate(cls):
        """
        Helper that checks if the class members required by the Filter
        interface are defined.
        """
        vals = cls.__check_fields()
        if False in vals:
            return False
        return True
    @classmethod
    def __check_fields(cls):
        """
        Returns a list of boolean values that represent which of the class
        members required by the Filter interface are defined.
        """
        cdir = dir(cls)
        return [val in cdir for val in ["filter_type",
                                        "input_ports",
                                        "default_params",
                                        "output_port"]]