From 2f5364415fcf9f66c505c3b9b4e09b2ecab1b1c4 Mon Sep 17 00:00:00 2001
From: Edan Bainglass <45081142+edan-bainglass@users.noreply.github.com>
Date: Fri, 17 Jan 2025 09:50:08 +0100
Subject: [PATCH] Add guards on null active eigenvalues (#1102)
There were no guards on the active eigenvalues. So if someone selected to define eigenvalues, but didn't (leaving all as -1), it raised errors regarding reshaping a null-size array.
---
.../configuration/advanced/hubbard/hubbard.py | 6 +++--
.../configuration/advanced/hubbard/model.py | 24 ++++++++++++-------
tests/configuration/test_advanced.py | 6 ++---
3 files changed, 22 insertions(+), 14 deletions(-)
diff --git a/src/aiidalab_qe/app/configuration/advanced/hubbard/hubbard.py b/src/aiidalab_qe/app/configuration/advanced/hubbard/hubbard.py
index 9170e9e81..b7dce95a1 100644
--- a/src/aiidalab_qe/app/configuration/advanced/hubbard/hubbard.py
+++ b/src/aiidalab_qe/app/configuration/advanced/hubbard/hubbard.py
@@ -39,10 +39,12 @@ def render(self):
)
self.eigenvalues_help = ipw.HTML("""
-
+
For transition metals and lanthanoids, the starting eigenvalues can be defined (magnetic calculation).
- It is useful to suggest the desired orbital occupations when the default choice takes another path.
+ It is useful to suggest the desired orbital occupations when the default choice takes another path.
+
+ To do so, tick the checkbox below and set the desired eigenvalues to a value other than -1 (unset).
""")
self.define_eigenvalues_checkbox = ipw.Checkbox(
diff --git a/src/aiidalab_qe/app/configuration/advanced/hubbard/model.py b/src/aiidalab_qe/app/configuration/advanced/hubbard/model.py
index 12672457e..d3c98560a 100644
--- a/src/aiidalab_qe/app/configuration/advanced/hubbard/model.py
+++ b/src/aiidalab_qe/app/configuration/advanced/hubbard/model.py
@@ -66,16 +66,22 @@ def update(self, specific=""): # noqa: ARG002
self.needs_eigenvalues_widget = len(self.applicable_kind_names) > 0
def get_active_eigenvalues(self):
- active_eigenvalues = [
- orbital_eigenvalue
- for element_eigenvalues in self.eigenvalues
- for spin_row in element_eigenvalues
- for orbital_eigenvalue in spin_row
- if orbital_eigenvalue[-1] != -1
- ]
+ if not (
+ active_eigenvalues := [
+ orbital_eigenvalue
+ for element_eigenvalues in self.eigenvalues
+ for spin_row in element_eigenvalues
+ for orbital_eigenvalue in spin_row
+ if orbital_eigenvalue[-1] != -1
+ ]
+ ):
+ return []
eigenvalues_array = np.array(active_eigenvalues, dtype=object)
- new_shape = (np.prod(eigenvalues_array.shape[:-1]), 4)
- return eigenvalues_array.reshape(new_shape).tolist()
+ new_shape = (int(np.prod(eigenvalues_array.shape[:-1])), 4)
+ return [
+ tuple(eigenvalue)
+ for eigenvalue in eigenvalues_array.reshape(new_shape).tolist()
+ ]
def set_active_eigenvalues(self, eigenvalues: list):
eigenvalues_array = np.array(eigenvalues, dtype=object)
diff --git a/tests/configuration/test_advanced.py b/tests/configuration/test_advanced.py
index 07506c2c1..9a2fcedf1 100644
--- a/tests/configuration/test_advanced.py
+++ b/tests/configuration/test_advanced.py
@@ -172,7 +172,7 @@ def test_advanced_hubbard_settings(generate_structure_data):
Co_spin_down_row.children[5].value = "1"
assert model.get_active_eigenvalues() == [
- [1, 1, "Co", 1],
- [3, 1, "Co", 1],
- [5, 1, "Co", 1],
+ (1, 1, "Co", 1),
+ (3, 1, "Co", 1),
+ (5, 1, "Co", 1),
]