Problem with Scipy


we are using the following function to convert an ngsolve matrix to a scipy matrix

def NgsToScipy( ngs_mat ):
from scipy.sparse import coo_matrix
row,col,data = ngs_mat.COO()
return coo_matrix((data, (row, col)))

It works well on my apple computer, but not on Ubuntu, where I get the following error:

File “/users/pilli/Desktop/kaust code/”, line 36, in NgsToScipy
return coo_matrix((data, (row, col)))
File “/usr/lib/python3/dist-packages/scipy/sparse/”, line 120, in init
if isshape(arg1):
File “/usr/lib/python3/dist-packages/scipy/sparse/”, line 207, in isshape
if isintlike(M) and isintlike(N):
File “/usr/lib/python3/dist-packages/scipy/sparse/”, line 190, in isintlike
if int(x) == x:
ValueError: invalid literal for int() with base 10: b’E+\xe8\xb4\x81N;?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xda.1-\x8d\xe7E?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00:+\x

I think it has something to do with the scipy version I am using, but I could not find any solution so far. Could you please advise what to do?

Thank you very much,

I think there is a bug in scipy < 0.13 or so which needs our data to be wrapped in numpy arrays first. Does this fix the problem?

coo_matrix((np.array(data,copy=False), (np.array(row, copy=False), np.array(col,copy=False))))

Best Christopher

Thank you very much!
Now it is working!


Computers store numbers in a variety of different ways. Python has two main ones. Integers, which store whole numbers (ℤ), and floating point numbers, which store real numbers (ℝ). You need to use the right one based on what you require. This error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that’s not an integer to the int() function . In other words it’s either empty, or has a character in it other than a digit.

You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

val = “10.10”
if val.isdigit():

The other way to overcome this issue is to wrap your code inside a Python try…except block to handle this error.

Or if you are trying to convert a float string (eg. “10.10”) to an integer, simply calling float first then converting that to an int will work:

output = int(float(input))