Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print(AmorphousLayer) and print(UnitCell) fail #158

Closed
ghlqz opened this issue Sep 19, 2024 · 5 comments
Closed

print(AmorphousLayer) and print(UnitCell) fail #158

ghlqz opened this issue Sep 19, 2024 · 5 comments

Comments

@ghlqz
Copy link

ghlqz commented Sep 19, 2024

I am currently starting to work with udkm1Dsim and encountered a problem in following the examples in the documentation. Printing an amorphous layer or a unit cell throws an error, "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". Some debugging showed that an array "output" is generated in the respective __str__ functions of the AmorphousLayer and UnitCell classes which cannot be handled by the tabulate command (it also does not have the proper np.shape() ).

The problematic array entries are actually inherited from the __str__ of the Layer class. There, some variables are not converted to strings. An example is the Debye-Waller factor, which remains a quantity object with unit which leads to the error.

A possible workaround is to replace
['Debye Waller Factor', self.deb_wal_fac.to('meter**2')],
by
['Debye Waller Factor', '{:.4~P}'.format(1.0*self.deb_wal_fac.to('meter**2'))],
in __str__ of the class Layer (and analogous for the other affected variables; the 1.0 factor converts the default integer value to a float because otherwise the format throws an error).

Is this behavior due to some library version conflict, or actually a bug? I am currently using

Python 3.11.5
NumPy 1.26.4
udkm1Dsim 2.0.2
tabulate 0.9.0
Pint 0.24.3

Thanks!

@dschick
Copy link
Owner

dschick commented Sep 19, 2024

Hi @ghlqz,

Many thanks for reporting this issue.
I actually encountered the same error just this morning - so many thanks for starting the debugging!

I will check my latest commits and changes in the dependencies to nail this down asap.

Best

Daniel

@dschick
Copy link
Owner

dschick commented Sep 19, 2024

If I remember correctly, this problem was introduced with one of the latest numpy version, which handles boolean operations on arrays differently.
See also this issue in the tabulate project: astanin/python-tabulate#339

I will try, if I can implement a simple workaround or otherwise switch to a more recent converter for tabular output.

Best

Daniel

@dschick
Copy link
Owner

dschick commented Sep 19, 2024

@ghlqz I will follow your proposal and convert all quantities to strings before handing them over to the tabulate function.

@dschick
Copy link
Owner

dschick commented Sep 19, 2024

I created an PR #159 which should fix this issue.
Could you please test it?

@dschick
Copy link
Owner

dschick commented Sep 20, 2024

closing

@dschick dschick closed this as completed Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants