Attribute error - requiring help

This is my first time using python and I have been given a code to process some data. I have run the code but I get the following error:

AttributeError: ‘NoneType’ object has no attribute ‘GetDataType’

Could anybody provide me with some help on how to fix this please?

You likely need to include your code for anyone to be able to give you precise pointers as to how to fix this. The error message you got from Python also included some other information, about the line number where the error occurred. On that line, you likely had code that looked like this:

   ....  some_variable.GetDataType ...

where some_variable had a value of None. That is all that can be deduced from the information you included.

Yes, I have a line which look like that, I have included my code below:

code start

import vtk
from vtk.util.numpy_support import vtk_to_numpy
import numpy as np
import scipy.io as sio

c = 0

out = {}

starts = 0
ends = 101

for i in range(starts,ends):

fname = f"BACKGROUND_IC_{i}.vtp"

# --- read a vtp file ---
points = vtk.vtkXMLPolyDataReader()
points.SetFileName(fname)
points.Update()

# print the arrays out
data = points.GetOutput()
point_data = data.GetPointData()

vel = vtk_to_numpy(point_data.GetAbstractArray('Velocity'))
points = vtk_to_numpy(data.GetPoints().GetData())

c = c+1;

name = f"out_{c}.mat"
sio.savemat(name, {'vel':vel, 'points':points})
print(i)

Thank you for showing your code, but are we supposed to guess which
line
gives the error you have reported?

You reported the error:

AttributeError: 'NoneType' object has no attribute 'GetDataType'

but no line in you code refers to GetDataType. That could mean:

  1. You have given us the wrong code.

  2. You have given us the wrong error message.

  3. The error is deeper in the code, possibly inside the vtk library.

  4. Or any combination of two or more of the above.

Please copy and paste the full traceback of your error, starting with
the line “Traceback…” and ending with the error message.

Sorry for not providing the right information, I am new to this.

The error is:

Traceback (most recent call last):

File “C:\Users\sgcgrif3\Documents\MFIX Files\Rice Pile\130421 working mfix files\New folder\convert.py”, line 27, in
vel = vtk_to_numpy(point_data.GetAbstractArray(‘Velocity’))

File “C:\Users\sgcgrif3\Anaconda3\envs\mfix-21.1.0\lib\site-packages\vtkmodules\util\numpy_support.py”, line 216, in vtk_to_numpy
typ = vtk_array.GetDataType()

AttributeError: ‘NoneType’ object has no attribute ‘GetDataType’

Can anybody help me please?

Sorry for not providing the right information, I am new to this.

It’s an acquired skill, but generally useful.

The error is:
Traceback (most recent call last):
File “C:\Users\sgcgrif3\Documents\MFIX Files\Rice Pile\130421 working
mfix files\New folder\convert.py”, line 27, in
vel = vtk_to_numpy(point_data.GetAbstractArray(‘Velocity’))
File
“C:\Users\sgcgrif3\Anaconda3\envs\mfix-21.1.0\lib\site-packages\vtkmodules\util\numpy_support.py”,
line 216, in vtk_to_numpy
typ = vtk_array.GetDataType()

AttributeError: ‘NoneType’ object has no attribute ‘GetDataType’

Based on this, we can infer that “vtk_array” is None, instead of the
array type that numpy_support.py expected. So why?

Backing up, the previous line (from your own code) is:

vel = vtk_to_numpy(point_data.GetAbstractArray('Velocity'))

so we may guess that vtk_to_numpy() has perhaps been passed None instead
of the desired array object. That would imply that:

point_data.GetAbstractArray('Velocity')

returned None. A bit of poking around with a search engine finds this:

https://vtk.org/doc/nightly/html/classvtkFieldData.html#ac21ae27f8d0bdcfba55523ed7a2d01da

which is to documentation for the GetAbstractArray method from the C++
VTK library (your Python code will be accessing this via a little
adaptor). It says:

vtkAbstractArray* vtkFieldData::GetAbstractArray(int i)

Returns the ith array in the field.

Unlike GetArray(), this method returns a vtkAbstractArray and can be 
used to access any array type. A nullptr is returned only if the index i 
is out of range.

That suggests that the matching Python method probably returns None if
‘Velocity’ is unknown (I’d guess that keys like ‘Velocity’ are mapped to
an index for the underlying library).

This asks the question: is ‘Velocity’ the correct key? For example, is
there a ‘Velocity’ field in your data? Alternatively, do you need to
convert ‘Velocity’ into some integer index (this seems less likely)?

I would start by printing out what
point_data.GetAbstractArray(‘Velocity’) returns:

ary = point_data.GetAbstractArray('Velocity')
print("ary =", type(ary), ary)
vel = vtk_to_numpy(ary)

That will tell you if you’re getting None or a more useful thing. Then
adjust your code so that you get the more useful thing.

Cheers,
Cameron Simpson cs@cskk.id.au

Thank you for your very detailed response, it has been very helpful. I have tried your suggestion in printing out the data. This showed the following result:

ary = <class ‘NoneType’> None

Right, so this:

point_data.GetAbstractArray('Velocity')

is returning None. Is ‘Velocity’ a valid argument for GetAbstractArray?
Is in in your point_data with that name? These are the questions you now
need to chase down.

What does this:

help(point_data)

print out? Ideally it has useful information about the methods available
and what they need as parameters.

Cheers,
Cameron Simpson cs@cskk.id.au

The data I have is stored in vtp files with the format as shown in the picture i’ve attached. Somebody wrote the code for me, where the aim is to store the velocity and coordinates data.

Sorry, that data means nothing to me. I cannot even begin to interpret
what that means and how it relates to your Python code :frowning:

As Cameron suggested, can you read the documentation for your
point_data variable? If you can recreate your code in the interactive
interpreter, then you can use:

help(point_data)

but if not, you can insert this into your script and see what it prints
out:

# put this *below* the code that creates the point_data variable,
# just before the line that errors out
import inspect
print(inspect.getdoc(point_data))

The help(...) line is better, but it requires you to be working
interactively which might not be practical.

If that print line fails too, then you will have to resort to reading
the docs for the vtk library and try to work out why

point_data.GetAbstractArray('Velocity')

is returning None.

Actually, this works even in a script. Tested that last night while
posting the suggestion :slight_smile:

Cheers,
Cameron Simpson cs@cskk.id.au

help() works in a script? How wonderful! TIL.