- Thank you received: 0
Problem with Scipy
5 years 11 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
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
- christopher
- Offline
- Administrator
Less
More
- Thank you received: 101
5 years 11 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?
Best Christopher
Code:
coo_matrix((np.array(data,copy=False), (np.array(row, copy=False), np.array(col,copy=False))))
Best Christopher
5 years 10 months ago #1413
by Pilli
Replied by Pilli on topic Problem with Scipy
Thank you very much!
Now it is working!
Best,
Giulia
Now it is working!
Best,
Giulia
- romankris
- New Member
Less
More
2 years 4 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))
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.111 seconds