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 54 forks source link

Arrays in Python #87

Closed uvainio closed 5 years ago

uvainio commented 5 years ago

Hi Tom (and others),

do you think it would be a big task to change the Python implementation such that the functions would take in energy also as an array (e.g. numpy), such that one does not need to for loop always the calls to xraylib over many input energies? I think it would give a great speed increase.

Kind regards,

Ulla

tschoonj commented 5 years ago

Hi Ulla,

That's already possible: just import xraylib_np instead of xraylib:

import numpy as np
import xraylib_np as xrl_np

Z = np.arange(1,94,dtype=int)
energies = np.arange(10,10000,dtype=np.double)/100.0
CS = xrl_np.CS_Total_Kissel(Z,energies)

The thing is that you will be forced to pass single element numpy arrays if you want to do this e.g. for just one atomic number.

Best,

Tom

uvainio commented 5 years ago

Hi Tom,

thanks a lot! I was totally unaware of this xraylib_np. However, it seems it is missing some functions like the compound ones (e.g. CS_Total_Kissel_CP). And ideally the same function would take ints and numpy arrays both, so one would not need to switch between libraries and make variables numpy arrays. Would this be something where I could easily help if I somehow could reconstruct the xraylib-code such that it would also consider numpy arrays (mostly just in the energy!)?

Cheers,

Ulla

tschoonj commented 5 years ago

Hey Ulla,

When I wrote these bindings I explicitly left out all functions that had strings as arguments as I did not know what to do with them in Cython.

I recently learned a few new Cython tricks and I think that I should get that working now, after a couple of changes.

I am not entirely happy with the xraylib_np implementation for a number of reasons, including the fact that it enforces the arguments to be numpy arrays instead of allowing also simple scalars.

A rewrite is indeed in order, but as writing these bindings is very tedious it is probably best to have the Cython code generated with a python script.

I would be more than happy if you could help out here: let me know if you need any help and I will gladly help out!

Best,

Tom

tschoonj commented 5 years ago

Hi Ulla,

I will be closing this for now.

Best,

Tom