On Thu, Oct 19, 2017 at 11:02 AM, Edoardo Pasca <edo.pask...@gmail.com> wrote:
> Hi Cory, > > thanks for your reply. I'd love to simplify the code like that but there > are 2 issues with that programmable filter > > 1) I miss information on the input and output variables. Where are defined? > It's a bit complicated to select them in the user interface, but it is possible. There was a related discussion on the mailing list recently on how to do this: http://paraview.markmail.org/thread/j4ynkclxcucpqoa5 In the worst case, you can hard-code the array names if they don't change. Relying on the active scalar attribute is old-style VTK and may go away someday in the not-too-near future. Or you could just keep your original code for defining array B. > 2) Paraview crashes. Paraview for windows 5.4.1 and 5.3.0 (official > kitware build) > > It seems a memory access problem. > To debug, I suggest writing your Programmable Filter script a few lines at a time to determine which line causes the crash. HTH, Cory > Edo > > On Thu, Oct 19, 2017 at 2:52 PM, Cory Quammen <cory.quam...@kitware.com> > wrote: > >> Hi Edo, >> >> Documentation for the VTK/Numpy adapter can be found here: >> >> https://www.paraview.org/ParaView/Doc/Nightly/www/py-doc/ >> paraview.vtk.numpy_interface.html?highlight=numpy_ >> interface#numpy-interface-package >> >> You will likely be interested in the dataset_adapter module that provides >> a convenient way to wrap VTK data objects in a Python class that provides >> easy read/write access to VTK data arrays as Numpy arrays. >> >> You could simplify your code quite a bit using this adapter as in the >> following: >> >> import numpy >> >> from vtk.util import numpy_support, vtkImageImportFromArray >> >> import vtk.numpy_interface.dataset_adapter as dsa >> >> >> inData = dsa.WrapDataObject(inputs[0]) >> >> B = inData.PointData['array_name'] # best practice is to name the array >> >> >> #B = numpy_support.vtk_to_numpy( >> >> # inputs[0].GetPointData().GetScalars()) >> >> >> ifLarger = lambda x,M: x if x<=M else 0 >> >> fgt = numpy.frompyfunc(ifLarger,2,1) >> >> #A = fgt(B,int(2**16*0.8)) >> >> >> ifSmaller = lambda x,M: x if x>M else 0 >> >> flt = numpy.frompyfunc(ifSmaller,2,1) >> >> #A = flt(B,int(2**16*0.3)) >> >> >> ifBetween = lambda x,m,M: ifSmaller(x,m) if x<=M else 0 >> >> fbetw = numpy.frompyfunc(ifBetween,3,1) >> >> >> A = fbetw(B,int(2**16*0.3), int(2**16*0.95)) >> >> >> outData = dsa.WrapDataObject(output) >> >> outData.PointData.append(A,'new_array_name') >> >> >> #A = numpy.asarray(A,dtype=uint16) >> >> #dims = inputs[0].GetDimensions() >> >> #A = numpy.reshape(A, (dims[2],dims[1],dims[0]), order='C') >> >> #A = numpy.ascontiguousarray(A) >> >> #importer = vtkImageImportFromArray.vtkImageImportFromArray() >> >> #importer.SetArray(A) >> >> #importer.SetDataSpacing(inputs[0].GetSpacing()) >> >> #importer.SetDataOrigin(inputs[0].GetOrigin()) >> >> #importer.Update() >> >> >> #output.DeepCopy(importer.GetOutput()) >> >> >> Hope that helps, >> >> Cory >> >> On Tue, Oct 17, 2017 at 10:38 AM, Edoardo Pasca <edo.pask...@gmail.com> >> wrote: >> >>> Hi there, >>> >>> In my programmable filter I want to use numpy to perform some operations >>> on the pixels (in this case a simple thresholding). I've come up with this >>> solution which works. I'm wondering if there are other ways to do the >>> conversion from numpy array to vtkImageData. >>> >>> in the following code I get a numpy array out of the input vtkImageData >>> as B. Then I do something on the content an store the result in a numpy >>> array, A. >>> >>> Then I convert the numpy array to a vtkImageData with >>> vtkImageImportFromArray importer class. >>> Finally I copy the importer output to the output of the programmable >>> filter. >>> >>> Thanks for any suggestions >>> >>> Edo >>> >>> import numpy >>> >>> from vtk.util import numpy_support, vtkImageImportFromArray >>> >>> import vtk.numpy_interface.dataset_adapter as dsa >>> >>> >>> B = numpy_support.vtk_to_numpy( >>> >>> inputs[0].GetPointData().GetScalars()) >>> >>> >>> ifLarger = lambda x,M: x if x<=M else 0 >>> >>> fgt = numpy.frompyfunc(ifLarger,2,1) >>> >>> #A = fgt(B,int(2**16*0.8)) >>> >>> >>> ifSmaller = lambda x,M: x if x>M else 0 >>> >>> flt = numpy.frompyfunc(ifSmaller,2,1) >>> >>> #A = flt(B,int(2**16*0.3)) >>> >>> >>> ifBetween = lambda x,m,M: ifSmaller(x,m) if x<=M else 0 >>> >>> fbetw = numpy.frompyfunc(ifBetween,3,1) >>> >>> >>> A = fbetw(B,int(2**16*0.3), int(2**16*0.95)) >>> >>> >>> >>> A = numpy.asarray(A,dtype=uint16) >>> >>> dims = inputs[0].GetDimensions() >>> >>> A = numpy.reshape(A, (dims[2],dims[1],dims[0]), order='C') >>> >>> A = numpy.ascontiguousarray(A) >>> >>> importer = vtkImageImportFromArray.vtkImageImportFromArray() >>> >>> importer.SetArray(A) >>> >>> importer.SetDataSpacing(inputs[0].GetSpacing()) >>> >>> importer.SetDataOrigin(inputs[0].GetOrigin()) >>> >>> importer.Update() >>> >>> >>> output.DeepCopy(importer.GetOutput()) >>> >>> >>> >>> -- >>> Edo >>> I know you think you understand what you thought I said, but I'm not >>> sure you realize that what you heard is not what I meant (prob. Alan >>> Greenspan) >>> :wq >>> >>> _______________________________________________ >>> Powered by www.kitware.com >>> >>> Visit other Kitware open-source projects at >>> http://www.kitware.com/opensource/opensource.html >>> >>> Please keep messages on-topic and check the ParaView Wiki at: >>> http://paraview.org/Wiki/ParaView >>> >>> Search the list archives at: http://markmail.org/search/?q=ParaView >>> >>> Follow this link to subscribe/unsubscribe: >>> http://public.kitware.com/mailman/listinfo/paraview >>> >>> >> >> >> -- >> Cory Quammen >> Staff R&D Engineer >> Kitware, Inc. >> > > > > -- > Edo > I know you think you understand what you thought I said, but I'm not sure > you realize that what you heard is not what I meant (prob. Alan Greenspan) > :wq > -- Cory Quammen Staff R&D Engineer Kitware, Inc.
_______________________________________________ Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the ParaView Wiki at: http://paraview.org/Wiki/ParaView Search the list archives at: http://markmail.org/search/?q=ParaView Follow this link to subscribe/unsubscribe: http://public.kitware.com/mailman/listinfo/paraview