[Paraview] Python Script: "Rescale to Data Range"

Magician f_magician at mac.com
Mon Feb 11 00:15:42 EST 2013


Hi Roman,


I have an other solution about this problem using Python.
The code takes you the ranges of any values:

> sm = servermanager.Fetch(your_source)
> sm.GetCellData().GetArray(your_value).GetRange(0)

...and you can apply them to your Color Maps.

You should apply your source's and value's name (ex. 'Slice1' 'Velocity').
If you want to get point data, change GetCellData() to GetPointData().
And if your sources have high-dimensional value (such as vectors)
and want to get the range of n-th component, change n value of GetRange(n).

If your sources are MultiBlock dataset, you should get only one block
before getting value as below:

> sm.GetBlock(N).GetCellData().GetArray(your_value).GetRange(0)

N is the number of blocks.


# Now I have not enough samples, perhaps the codes couldn't work, sorry.


Magician


On 2013/02/09, at 0:38, paraview-request at paraview.org wrote:

> If you have the source code, you can look at the <pv
> source>/Plugins/CoProcessingScriptGenerator/cp_export.py. It might be a bit
> confusing but the section you'll want to look at is below. I think you're
> problem is that you don't have any slice object to get the data information
> from.
> 
> Andy
> 
> =========
>    if %s : # rescale data range
>        import math
>        for view in cp_views:
>            if timestep %% view.cpFrequency == 0 or
> datadescription.GetForceOutput() == True:
>                reps = view.Representations
>                for rep in reps:
>                    if hasattr(rep, 'Visibility') and rep.Visibility == 1
> and hasattr(rep, 'MapScalars') and rep.MapScalars != '':
>                        input = rep.Input
>                        input.UpdatePipeline() #make sure range is
> up-to-date
>                        lut = rep.LookupTable
>                        if lut == None:
>                            continue
>                        if rep.ColorAttributeType == 'POINT_DATA':
>                            datainformation =
> input.GetPointDataInformation()
>                        elif rep.ColorAttributeType == 'CELL_DATA':
>                            datainformation = input.GetCellDataInformation()
>                        else:
>                            print 'something strange with color attribute
> type', rep.ColorAttributeType
> 
>                        if lut.VectorMode != 'Magnitude' or \
> 
> datainformation.GetArray(rep.ColorArrayName).GetNumberOfComponents() == 1:
>                            datarange =
> datainformation.GetArray(rep.ColorArrayName).GetRange(lut.VectorComponent)
>                        else:
>                            datarange = [0,0]
>                            for i in
> range(datainformation.GetArray(rep.ColorArrayName).GetNumberOfComponents()):
>                                for j in range(2):
>                                    datarange[j] +=
> datainformation.GetArray(rep.ColorArrayName).GetRange(i)[j]*datainformation.GetArray(rep.ColorArrayName).GetRange(i)[j]
>                            datarange[0] = math.sqrt(datarange[0])
>                            datarange[1] = math.sqrt(datarange[1])
> 
>                        rgbpoints = lut.RGBPoints.GetData()
>                        numpts = len(rgbpoints)/4
>                        minvalue = min(datarange[0], rgbpoints[0])
>                        maxvalue = max(datarange[1],
> rgbpoints[(numpts-1)*4])
>                        if minvalue != rgbpoints[0] or maxvalue !=
> rgbpoints[(numpts-1)*4]:
>                            # rescale all of the points
>                            oldrange = rgbpoints[(numpts-1)*4] -
> rgbpoints[0]
>                            newrange = maxvalue - minvalue
>                            newrgbpoints = list(rgbpoints)
>                            for v in range(numpts):
>                                newrgbpoints[v*4] =
> minvalue+(rgbpoints[v*4] - rgbpoints[0])*newrange/oldrange
> 
>                            lut.RGBPoints.SetData(newrgbpoints)
> 
> 
> On Fri, Feb 8, 2013 at 6:35 AM, Kisler <Roman.Kisler at tnt.tu-berlin.de>wrote:
> 
>> Hi everyone,
>> 
>> I write a python script (Paraview 3.14.1 64-bit) in which a loop loads
>> files,
>> makes slices etc., prints an image and deletes all variables within each
>> iteration step. Therefore I need a python code which rescales the colorbar
>> in
>> each step.
>> 
>> So far I found this code in another mailing conversation (see
>> 
>> http://www.paraview.org/pipermail/paraview/2009-January/010809.html).Unfortunately
>> the command
>> 
>> 
>> range=slice.GetDataInformation().GetPointDataInformation().GetArrayInformation(0).GetComponentRange(0)
>> 
>> doesn't work in my script and delivers the following error message:
>> 
>> " AttributeError: 'NoneType' object has no attribute 'GetDataInformation' "
>> 
>> and refers to the line written above (range=...).
>> 
>> I would appreciate any help, be it a more detailed explanation of the
>> script
>> which can be found in the link posted above or another possibility of
>> implementing it.
>> 
>> Kindest regards,
>> Roman



More information about the ParaView mailing list