Problem with Scipy

More
5 years 9 months ago #1410 by Pilli
Problem with Scipy was created by Pilli
Hi,

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/utils.py", line 36, in NgsToScipy
return coo_matrix((data, (row, col)))
File "/usr/lib/python3/dist-packages/scipy/sparse/coo.py", line 120, in __init__
if isshape(arg1):
File "/usr/lib/python3/dist-packages/scipy/sparse/sputils.py", line 207, in isshape
if isintlike(M) and isintlike(N):
File "/usr/lib/python3/dist-packages/scipy/sparse/sputils.py", 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,
Giulia
More
5 years 9 months ago #1411 by christopher
Replied by christopher on topic Problem with Scipy
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?
Code:
coo_matrix((np.array(data,copy=False), (np.array(row, copy=False), np.array(col,copy=False))))

Best Christopher
More
5 years 9 months ago #1413 by Pilli
Replied by Pilli on topic Problem with Scipy
Thank you very much!
Now it is working!

Best,
Giulia
More
2 years 3 months ago #4485 by romankris
Replied by romankris on topic Problem with Scipy
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():
  print(int(val))

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))

 
Time to create page: 0.105 seconds