tschoonj / xraylib

A library for X-ray matter interaction cross sections for X-ray fluorescence applications
https://github.com/tschoonj/xraylib/wiki
Other
120 stars 55 forks source link

int vs. np.int #38

Closed davlars closed 7 years ago

davlars commented 7 years ago

Some methods requiring (python-native) int-inputs (e.g. AtomicNumberToSymbol) seem to be crashing in the case that a non-native int (e.g. numpy.int) is provided. Is perhaps good to know (perhaps the issue could be solved by envoking np.asscalar for numpy inputs, but I'm not sure if that covers all potential input-problems)

tschoonj commented 7 years ago

I cannot seem to reproduce this (or maybe I am not understanding your issue :smile: ) on a RHEL6 Linux box running python 2.7.12 (Anaconda):

>>> import xraylib as xrl
>>> xrl.AtomicNumberToSymbol(26)
'Fe'
>>> import numpy as np
>>> a = np.array([26])
>>> xrl.AtomicNumberToSymbol(a[0])
'Fe'
>>> a = np.array([26], dtype=np.int)
>>> xrl.AtomicNumberToSymbol(a[0])
'Fe'

What versions of xraylib and python are you using, and on which platform are you running it?

davlars commented 7 years ago

Hmm, what you're running does not work for me. I'm running Python 3.5.2 on a Ubuntu 16.04 LTS system, using xraylib from the conda repo (https://anaconda.org/tacaswell/xraylib). It works fine for:

import xraylib as xrl
xrl.AtomicNumberToSymbol(1)

'H'

However not for:

import xraylib as xrl
a = np.array([1])
xrl.AtomicNumberToSymbol(a)

File "<ipython-input-12-10c1439dda30>", line 1, in <module>
   xrl.AtomicNumberToSymbol(a)
TypeError: in method 'AtomicNumberToSymbol', argument 1 of type 'int'

For that I have to define it as:

import xraylib as xrl
a = np.asscalar(np.array([1]))
xrl.AtomicNumberToSymbol(a)

'H'

Could it be a python2 vs python3 thing? Posted it as an xraylib-issue since I thought it was due to some native-int definition, but it could be my system doing strange stuff...

tschoonj commented 7 years ago

It's indeed a python3 feature which appears to be related to all integers being 64-bit in python3.

I am looking into it.

tschoonj commented 7 years ago

Think I fixed it in #39. From what I understand numpy scalars were actually never supposed to work with the SWIG generated bindings according to the numpy docs.

My latest changes appears to work fine with both built-in Python types as well numpy scalars. Feel free to give it a try,

davlars commented 7 years ago

Very nice! Thanks for the effort!

tschoonj commented 7 years ago

You're welcome, thanks for reporting this issue