xraypy / xraylarch

Larch: Applications and Python Library for Data Analysis of X-ray Absorption Spectroscopy (XAS, XANES, XAFS, EXAFS), X-ray Fluorescence (XRF) Spectroscopy and Imaging, and more.
https://xraypy.github.io/xraylarch
MIT License
138 stars 68 forks source link

Error reading XDIFile #506

Closed Gbauc closed 5 months ago

Gbauc commented 8 months ago

I'm having some trouble while trying to read XDI file using the function read_xdi

ValueError Traceback (most recent call last) Cell In[4], line 1 ----> 1 as2_data = read_xdi('MnO_rt_01') 2 print(as2_data.array_labels)

File ~/anaconda3/envs/ilumpy/lib/python3.11/site-packages/larch/io/xdi.py:288, in read_xdi(filename, labels) 252 def read_xdi(filename, labels=None): 253 """read an XDI File into a Group 254 255 Arguments: (...) 286 287 """ --> 288 xdif = XDIFile(filename, labels=labels) 289 group = Group() 290 for key, val in xdif.dict.items():

File ~/anaconda3/envs/ilumpy/lib/python3.11/site-packages/larch/io/xdi.py:112, in XDIFile.init(self, filename, labels) 110 self.user_labels = labels 111 if self.filename: --> 112 self.read(self.filename)

File ~/anaconda3/envs/ilumpy/lib/python3.11/site-packages/larch/io/xdi.py:131, in XDIFile.read(self, filename) 129 xdilib.XDI_cleanup(pxdi, self.status) 130 msg = 'Error reading XDIFile %s\n%s' % (filename, msg) --> 131 raise ValueError(msg) 133 xdi = pxdi.contents 134 for attr in dict(xdi.fields):

ValueError: Error reading XDIFile MnO_rt_01 invalid family name in meta-data

It gives me a ValueError and i can't solve it

Here's a copy of the file that I'm trying to read MnO_rt_01.txt

newville commented 8 months ago

@Gbauc Sorry for the trouble, but I can't reproduce that. I get

>>> from larch.io import read_xdi
>>> a = read_xdi('MnO_rt_01.txt')
>>> print(a.energy)
[6520.003 6521.001 6522.    6523.    6523.999 6524.999 6525.999 6527.
 6528.    6529.002 6530.003 6531.006 6532.008 6533.011 6534.014 6535.017...

I am not sure what "invalid family" is in this file -- it looks OK to me ;). Just to grasp at a straw or two: what version of Larch and which OS are you using?

As an alternative, using read_ascii ought to work with this file too, if not quite as featured at (supposedly!) knowing how to parse and use the metadata.

Gbauc commented 8 months ago

I think it's because I'm using xdi file and not the txt file as you used in your code. I just uploaded the txt file because github doesn't accept uploading xdi format.

I'm using the latest version of larch and Windows 11

newville commented 8 months ago

@Gbauc I would be surprised if changing the file extension made a difference. Larch does not decide a file is an XDI file based on the extension, but based on the first line of the file.

Did you try changing the name?

But, also, did you try using "read_ascii()" instead of "read_xdi()"

Gbauc commented 8 months ago

I tried using the txt file and it worked. But when I try using xdi file, I get the same error that I reported before.

from larch.io import read_xdi
mno = read_xdi('MnO_rt_01.txt')
print(mno.array_labels)
# It works if I try this way
mno = read_xdi('MnO_rt_01')
print(mno.array_labels)
# Both ways doesn't work
mno = read_xdi('MnO_rt_01.xdi')
print(mno.array_labels)

I tried using read_ascii() and it only works for txt files. Same thing for changing the name, it works fine for txt file, but not for xdi file extension

newville commented 8 months ago

Hm, that is very confusing. For sure, None of the "read_**()" functions in larch, nor the XDI library look at the extension used for the file name. That is just not really "a thing". The file extension does not ever identify XDI file - the first line of the text of the file does.

Renaming a file to have an extension ".txt", ".xdi", ".00022", or having no extension at all will not ever change the behavior of "read_xdi()" or "read_ascii()".

You describe 3 files, "MnO_rt_01.xdi", "MnO_rt_01.txt", and "MnO_rt_01". Are those all the same file? Can you demonstrate that the contents are identical? Can you send all 3 files, say put them into a zip file?

Gbauc commented 8 months ago

MnO_rt_01.zip The zip contains txt and xdi file.

MnO_rt_01 this one is the same file as MnO_rt_01.xdi, but in the code I tried putting the file extension like thismno = read_xdi('MnO_rt_01.xdi') and not putting the file extension like this mno = read_xdi('MnO_rt_01'), and both ways got me an error

newville commented 8 months ago

MnO_rt_01 this one is the same file as MnO_rt_01.xdi, but in the code I tried putting the file extension like thismno = read_xdi('MnO_rt_01.xdi') and not putting the file extension like this mno = read_xdi('MnO_rt_01'), and both ways got me an error

Well, read_xdi('MnO_rt_01') will not work if there is not a file named "MnO_rt_01". There is no assumption about file endings - that just never happens.

There are two files, named "MnO_rt_01.xdi" and "MnO_rt_01.txt" that have exactly the same content. Doing

from larch.io import read_xdi
x1 = read_xdi('MnO_rt_01.xdi')
x2 = read_xdi('MnO_rt_01.txt')

both work for me. Using "read_ascii()" works for these too.
I cannot reproduce what you are seeing.

Does read_xdi() work for you with other files, say, https://raw.githubusercontent.com/xraypy/xraylarch/master/examples/xafsdata/ni_metal_rt.xdi ?

Gbauc commented 8 months ago

I tried with other files and it didn't work as well, so I'll go on using txt files. Thanks for the help

newville commented 8 months ago

@Gbauc OK, hmm, I am not sure what could be going wrong. This is on Windows 11, yes?

Can you give the complete traceback from read_xdi('ni_metal_rt.xdi')?

Out of curiosity, can you open any of these files with the Larix GUI?

maurov commented 5 months ago

@Gbauc you did not give any further feedback on this, so I assume it is not an issue anymore.