def execute(self): """Called by vtkPythonAnnotationFilter.""" expression = self.GetExpression() inputDO = self.GetCurrentInputDataObject() if not expression or not inputDO: return True inputs = [dsa.WrapDataObject(inputDO)] association = self.GetArrayAssociation() ns = _get_ns(self, inputs[0], association) try: result = calculator.compute(inputs, expression, ns=ns) except: print("Failed to evaluate expression '%s'. "\ "The following exception stack should provide additional "\ "developer specific information. This typically implies a malformed "\ "expression. Verify that the expression is valid.\n\n" \ "Variables in current scope are %s \n" % (expression, list(ns)), file=sys.stderr) raise self.SetComputedAnnotationValue("%s" % result) return True
def execute(self): """Called by vtkPythonAnnotationFilter.""" expression = self.GetExpression() inputDO = self.GetCurrentInputDataObject() if not expression or not inputDO: return True inputs = [dsa.WrapDataObject(inputDO)] association = self.GetArrayAssociation() ns = _get_ns(self, inputs[0], association) try: result = calculator.compute(inputs, expression, ns=ns) except: from sys import stderr print >> stderr, "Failed to evaluate expression '%s'. "\ "The following exception stack should provide additional "\ "developer specific information. This typically implies a malformed "\ "expression. Verify that the expression is valid.\n\n" \ "Variables in current scope are %s \n" % (expression, ns.keys()) raise self.SetComputedAnnotationValue("%s" % result) return True
def execute(self): inputDO = self.GetInputDataObject(0, 0) inputSEL = self.GetInputDataObject(1, 0) outputDO = self.GetOutputDataObject(0) assert inputSEL.GetNumberOfNodes() >= 1 selectionNode = inputSEL.GetNode(0) field_type = selectionNode.GetFieldType() if field_type == selectionNode.CELL: attributeType = vtk.vtkDataObject.CELL elif field_type == selectionNode.POINT: attributeType = vtk.vtkDataObject.POINT elif field_type == selectionNode.ROW: attributeType = vtk.vtkDataObject.ROW else: raise RuntimeError ("Unsupported field attributeType %r" % field_type) # evaluate expression on the inputDO. # this is equivalent to executing the Python Calculator on the input dataset # to produce a mask array. inputs = [] inputs.append(dsa.WrapDataObject(inputDO)) query = selectionNode.GetQueryString() # get a dictionary for arrays in the dataset attributes. We pass that # as the variables in the eval namespace for calculator.compute(). elocals = calculator.get_arrays(inputs[0].GetAttributes(attributeType)) if not elocals.has_key("id") and re.search(r'\bid\b', query): # add "id" array if the query string refers to id. # This is a temporary fix. We should look into # accelerating id-based selections in the future. elocals["id"] = _create_id_array(inputs[0], attributeType) try: maskArray = calculator.compute(inputs, query, ns=elocals) except: from sys import stderr print ("Error: Failed to evaluate Expression '%s'. "\ "The following exception stack should provide additional developer "\ "specific information. This typically implies a malformed "\ "expression. Verify that the expression is valid.\n" % query, file=sys.stderr) raise if not maskarray_is_valid(maskArray): raise RuntimeError( "Expression '%s' did not produce a valid mask array. The value "\ "produced is of the type '%s'. This typically implies a malformed "\ "expression. Verify that the expression is valid." % \ (query, type(maskArray))) # if inverse selection is requested, just logical_not the mask array. if selectionNode.GetProperties().Has(selectionNode.INVERSE()) and \ selectionNode.GetProperties().Get(selectionNode.INVERSE()) == 1: maskArray = algos.logical_not(maskArray) output = dsa.WrapDataObject(outputDO) if self.GetPreserveTopology(): # when preserving topology, just add the mask array as # vtkSignedCharArray to the output. vtkPythonExtractSelection should # have already ensured that the input is shallow copied over properly # before this method gets called. # note: since mask array is a bool-array, we multiply it by int8(1) to # make it a type of array that can be represented as vtkSignedCharArray. output.GetAttributes(attributeType).append(maskArray * np.int8(1), "vtkInsidedness") else: # handle extraction. # flatnonzero() will give is array of indices where the arrays is # non-zero (or non-False in our case). We then pass that to # vtkPythonExtractSelection to extract the selected ids. nonzero_indices = algos.flatnonzero(maskArray) output.FieldData.append(nonzero_indices, "vtkSelectedIds"); #print (output.FieldData["vtkSelectedIds"]) self.ExtractElements(attributeType, inputDO, outputDO) del nonzero_indices del maskArray
def execute(self): inputDO = self.GetInputDataObject(0, 0) inputSEL = self.GetInputDataObject(1, 0) outputDO = self.GetOutputDataObject(0) assert inputSEL.GetNumberOfNodes() >= 1 selectionNode = inputSEL.GetNode(0) field_type = selectionNode.GetFieldType() if field_type == selectionNode.CELL: attributeType = vtk.vtkDataObject.CELL elif field_type == selectionNode.POINT: attributeType = vtk.vtkDataObject.POINT elif field_type == selectionNode.ROW: attributeType = vtk.vtkDataObject.ROW else: raise RuntimeError, "Unsupported field attributeType %r" % field_type # evaluate expression on the inputDO. # this is equivalent to executing the Python Calculator on the input dataset # to produce a mask array. inputs = [] inputs.append(dsa.WrapDataObject(inputDO)) # get a dictionary for arrays in the dataset attributes. We pass that # as the variables in the eval namespace for calculator.compute(). elocals = calculator.get_arrays(inputs[0].GetAttributes(attributeType)) try: maskArray = calculator.compute(inputs, selectionNode.GetQueryString(), ns=elocals) except: from sys import stderr print >> stderr, "Error: Failed to evaluate Expression '%s'. "\ "The following exception stack should provide additional developer "\ "specific information. This typically implies a malformed "\ "expression. Verify that the expression is valid.\n" % \ selectionNode.GetQueryString() raise if not maskarray_is_valid(maskArray): raise RuntimeError,\ "Expression '%s' did not produce a valid mask array. The value "\ "produced is of the type '%s'. This typically implies a malformed "\ "expression. Verify that the expression is valid." % \ (selectionNode.GetQueryString(), type(maskArray)) # if inverse selection is requested, just logical_not the mask array. if selectionNode.GetProperties().Has(selectionNode.INVERSE()) and \ selectionNode.GetProperties().Get(selectionNode.INVERSE()) == 1: maskArray = algos.logical_not(maskArray) output = dsa.WrapDataObject(outputDO) if self.GetPreserveTopology(): # when preserving topology, just add the mask array as # vtkSignedCharArray to the output. vtkPythonExtractSelection should # have already ensured that the input is shallow copied over properly # before this method gets called. # note: since mask array is a bool-array, we multiply it by int8(1) to # make it a type of array that can be represented as vtkSignedCharArray. output.GetAttributes(attributeType).append(maskArray * np.int8(1), "vtkInsidedness") else: # handle extraction. # flatnonzero() will give is array of indices where the arrays is # non-zero (or non-False in our case). We then pass that to # vtkPythonExtractSelection to extract the selected ids. nonzero_indices = algos.flatnonzero(maskArray) output.FieldData.append(nonzero_indices, "vtkSelectedIds") #print output.FieldData["vtkSelectedIds"] self.ExtractElements(attributeType, inputDO, outputDO) del nonzero_indices del maskArray
def execute(inputDO, selectionNode, insidednessArrayName, outputDO): field_type = selectionNode.GetFieldType() if field_type == selectionNode.CELL: attributeType = vtkDataObject.CELL elif field_type == selectionNode.POINT: attributeType = vtkDataObject.POINT elif field_type == selectionNode.ROW: attributeType = vtkDataObject.ROW else: raise RuntimeError ("Unsupported field attributeType %r" % field_type) # Evaluate expression on the inputDO. # This is equivalent to executing the Python Calculator on the input dataset # to produce a mask array. inputs = [] inputs.append(dsa.WrapDataObject(inputDO)) query = selectionNode.GetQueryString() # Get a dictionary for arrays in the dataset attributes. We pass that # as the variables in the eval namespace for calculator.compute(). elocals = calculator.get_arrays(inputs[0].GetAttributes(attributeType)) if ("id" not in elocals) and re.search(r'\bid\b', query): # Add "id" array if the query string refers to id. # This is a temporary fix. We should look into # accelerating id-based selections in the future. elocals["id"] = _create_id_array(inputs[0], attributeType) try: maskArray = calculator.compute(inputs, query, ns=elocals) except: from sys import stderr print ("Error: Failed to evaluate Expression '%s'. "\ "The following exception stack should provide additional developer "\ "specific information. This typically implies a malformed "\ "expression. Verify that the expression is valid.\n" % query, file=stderr) raise if not maskarray_is_valid(maskArray): raise RuntimeError( "Expression '%s' did not produce a valid mask array. The value "\ "produced is of the type '%s'. This typically implies a malformed "\ "expression. Verify that the expression is valid." % \ (query, type(maskArray))) # Preserve topology. Just add the mask array as vtkSignedCharArray to the # output. # Note: we must force the data type to VTK_SIGNED_CHAR or the array will # be ignored by the freeze selection operation from paraview.vtk.util import numpy_support output = dsa.WrapDataObject(outputDO) if type(maskArray) is not dsa.VTKNoneArray: if isinstance(maskArray, dsa.VTKCompositeDataArray): for ds, array in izip(output, maskArray.Arrays): if array is not None: insidedness = numpy_support.numpy_to_vtk(array, deep=1, array_type=vtkConstants.VTK_SIGNED_CHAR) insidedness.SetName(insidednessArrayName) ds.GetAttributes(attributeType).VTKObject.AddArray(insidedness) else: insidedness = numpy_support.numpy_to_vtk(maskArray, deep=1, array_type=vtkConstants.VTK_SIGNED_CHAR) insidedness.SetName(insidednessArrayName) output.GetAttributes(attributeType).VTKObject.AddArray(insidedness)
def execute(inputDO, selectionNode, insidednessArrayName, outputDO): field_type = selectionNode.GetFieldType() if field_type == selectionNode.CELL: attributeType = vtkDataObject.CELL elif field_type == selectionNode.POINT: attributeType = vtkDataObject.POINT elif field_type == selectionNode.ROW: attributeType = vtkDataObject.ROW else: raise RuntimeError("Unsupported field attributeType %r" % field_type) # Evaluate expression on the inputDO. # This is equivalent to executing the Python Calculator on the input dataset # to produce a mask array. inputs = [] inputs.append(dsa.WrapDataObject(inputDO)) query = selectionNode.GetQueryString() # Get a dictionary for arrays in the dataset attributes. We pass that # as the variables in the eval namespace for calculator.compute(). elocals = calculator.get_arrays(inputs[0].GetAttributes(attributeType)) if ("id" not in elocals) and re.search(r'\bid\b', query): # Add "id" array if the query string refers to id. # This is a temporary fix. We should look into # accelerating id-based selections in the future. elocals["id"] = _create_id_array(inputs[0], attributeType) try: maskArray = calculator.compute(inputs, query, ns=elocals) except: from sys import stderr print ("Error: Failed to evaluate Expression '%s'. "\ "The following exception stack should provide additional developer "\ "specific information. This typically implies a malformed "\ "expression. Verify that the expression is valid.\n" % query, file=stderr) raise if not maskarray_is_valid(maskArray): raise RuntimeError( "Expression '%s' did not produce a valid mask array. The value "\ "produced is of the type '%s'. This typically implies a malformed "\ "expression. Verify that the expression is valid." % \ (query, type(maskArray))) # Preserve topology. Just add the mask array as vtkSignedCharArray to the # output. # Note: we must force the data type to VTK_SIGNED_CHAR or the array will # be ignored by the freeze selection operation from paraview.vtk.util import numpy_support output = dsa.WrapDataObject(outputDO) if type(maskArray) is not dsa.VTKNoneArray: if isinstance(maskArray, dsa.VTKCompositeDataArray): for ds, array in izip(output, maskArray.Arrays): if array is not None: insidedness = numpy_support.numpy_to_vtk( array, deep=1, array_type=vtkConstants.VTK_SIGNED_CHAR) insidedness.SetName(insidednessArrayName) ds.GetAttributes(attributeType).VTKObject.AddArray( insidedness) else: insidedness = numpy_support.numpy_to_vtk( maskArray, deep=1, array_type=vtkConstants.VTK_SIGNED_CHAR) insidedness.SetName(insidednessArrayName) output.GetAttributes(attributeType).VTKObject.AddArray(insidedness)
def execute(self): inputDO = self.GetInputDataObject(0, 0) inputSEL = self.GetInputDataObject(1, 0) outputDO = self.GetOutputDataObject(0) assert inputSEL.GetNumberOfNodes() >= 1 selectionNode = inputSEL.GetNode(0) field_type = selectionNode.GetFieldType() if field_type == selectionNode.CELL: attributeType = vtkDataObject.CELL elif field_type == selectionNode.POINT: attributeType = vtkDataObject.POINT elif field_type == selectionNode.ROW: attributeType = vtkDataObject.ROW else: raise RuntimeError("Unsupported field attributeType %r" % field_type) # evaluate expression on the inputDO. # this is equivalent to executing the Python Calculator on the input dataset # to produce a mask array. inputs = [] inputs.append(dsa.WrapDataObject(inputDO)) query = selectionNode.GetQueryString() # get a dictionary for arrays in the dataset attributes. We pass that # as the variables in the eval namespace for calculator.compute(). elocals = calculator.get_arrays(inputs[0].GetAttributes(attributeType)) if ("id" not in elocals) and re.search(r'\bid\b', query): # add "id" array if the query string refers to id. # This is a temporary fix. We should look into # accelerating id-based selections in the future. elocals["id"] = _create_id_array(inputs[0], attributeType) try: maskArray = calculator.compute(inputs, query, ns=elocals) except: from sys import stderr print ("Error: Failed to evaluate Expression '%s'. "\ "The following exception stack should provide additional developer "\ "specific information. This typically implies a malformed "\ "expression. Verify that the expression is valid.\n" % query, file=stderr) raise if not maskarray_is_valid(maskArray): raise RuntimeError( "Expression '%s' did not produce a valid mask array. The value "\ "produced is of the type '%s'. This typically implies a malformed "\ "expression. Verify that the expression is valid." % \ (query, type(maskArray))) # if inverse selection is requested, just logical_not the mask array. if selectionNode.GetProperties().Has(selectionNode.INVERSE()) and \ selectionNode.GetProperties().Get(selectionNode.INVERSE()) == 1: maskArray = algos.logical_not(maskArray) output = dsa.WrapDataObject(outputDO) if self.GetPreserveTopology(): # when preserving topology, just add the mask array as # vtkSignedCharArray to the output. vtkPythonExtractSelection should # have already ensured that the input is shallow copied over properly # before this method gets called. # Note: we must force the data type to VTK_SIGNED_CHAR or the array will # be ignored by the freeze selection operation from vtkmodules.util.numpy_support import numpy_to_vtk if type(maskArray) is not dsa.VTKNoneArray: insidedness = numpy_to_vtk(maskArray, deep=1, array_type=vtkConstants.VTK_SIGNED_CHAR) insidedness.SetName("vtkInsidedness") output.GetAttributes(attributeType).VTKObject.AddArray(insidedness) else: # handle extraction. # flatnonzero() will give is array of indices where the arrays is # non-zero (or non-False in our case). We then pass that to # vtkPythonExtractSelection to extract the selected ids. nonzero_indices = algos.flatnonzero(maskArray) output.FieldData.append(nonzero_indices, "vtkSelectedIds") #print (output.FieldData["vtkSelectedIds"]) self.ExtractElements(attributeType, inputDO, outputDO) del nonzero_indices del maskArray