Skip to content

Commit 844e443

Browse files
committed
Bug fix: Ensure numpy arrays are converted back to native data types
1 parent ce4fec6 commit 844e443

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

crystal_toolkit/core/mpcomponent.py

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ def get_numerical_input(
442442
def matrix_element(idx, value=0):
443443
# TODO: maybe move element out of the name
444444
mid = self.id(kwarg_label, is_kwarg=True, idx=idx, hint=shape)
445+
if isinstance(value, np.ndarray):
446+
value = value.item()
445447
if not is_int:
446448
return dcc.Input(
447449
id=mid,
@@ -456,7 +458,7 @@ def matrix_element(idx, value=0):
456458
"marginBottom": "0.2rem",
457459
"height": "36px",
458460
},
459-
value=value,
461+
value=float(value) if value is not None else None,
460462
persistence=True,
461463
type="number",
462464
**kwargs,
@@ -475,7 +477,7 @@ def matrix_element(idx, value=0):
475477
"marginBottom": "0.2rem",
476478
"height": "36px",
477479
},
478-
value=value,
480+
value=int(value) if value is not None else None,
479481
persistence=True,
480482
type="number",
481483
step=1,
@@ -679,33 +681,48 @@ def reconstruct_kwargs_from_state(self, state) -> Dict:
679681

680682
# TODO: catch Exceptions here, and display validation error to user if incorrect kwargs supplied
681683

682-
if isinstance(k_type, tuple):
683-
# matrix or vector
684-
if kwarg_label not in kwargs:
685-
kwargs[kwarg_label] = np.empty(k_type)
686-
v = literal_eval(str(v))
687-
if v is not None and kwargs[kwarg_label] is not None:
688-
kwargs[kwarg_label][idx] = literal_eval(str(v))
689-
else:
690-
# require all elements to have value, otherwise set
691-
# entire kwarg to None
692-
kwargs[kwarg_label] = None
693-
694-
elif k_type == "literal":
695-
696-
try:
697-
kwargs[kwarg_label] = literal_eval(str(v))
698-
except ValueError:
699-
kwargs[kwarg_label] = str(v)
700-
701-
elif k_type == "bool":
702-
kwargs[kwarg_label] = bool("enabled" in v)
703-
704-
elif k_type == "slider":
705-
kwargs[kwarg_label] = v
706-
707-
elif k_type == "dict":
708-
pass
684+
try:
685+
686+
if isinstance(k_type, tuple):
687+
# matrix or vector
688+
if kwarg_label not in kwargs:
689+
kwargs[kwarg_label] = np.empty(k_type)
690+
v = literal_eval(str(v))
691+
if (v is not None) and (kwargs[kwarg_label] is not None):
692+
if isinstance(v, list):
693+
print(
694+
"This shouldn't happen! Debug required.",
695+
kwarg_label,
696+
idx,
697+
v,
698+
)
699+
kwargs[kwarg_label][idx] = None
700+
else:
701+
kwargs[kwarg_label][idx] = v
702+
else:
703+
# require all elements to have value, otherwise set
704+
# entire kwarg to None
705+
kwargs[kwarg_label] = None
706+
707+
elif k_type == "literal":
708+
709+
try:
710+
kwargs[kwarg_label] = literal_eval(str(v))
711+
except ValueError:
712+
kwargs[kwarg_label] = str(v)
713+
714+
elif k_type == "bool":
715+
kwargs[kwarg_label] = bool("enabled" in v)
716+
717+
elif k_type == "slider":
718+
kwargs[kwarg_label] = v
719+
720+
elif k_type == "dict":
721+
pass
722+
723+
except Exception as exc:
724+
# Not raised intentionally but if you notice this in logs please investigate.
725+
print("This is a problem, debug required.", exc)
709726

710727
for k, v in kwargs.items():
711728
if isinstance(v, np.ndarray):

0 commit comments

Comments
 (0)