Skip to content

Commit 164178e

Browse files
committed
Refactor entropy computation to handle missing and empty state data safely:
- Replaced ambiguous truth checks with type-safe logic for lists and NumPy arrays - Added guard against NoneType access for `states[group_id]` to prevent iteration errors
1 parent ccf6747 commit 164178e

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

CodeEntropy/entropy.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,17 @@ def _process_united_atom_entropy(
354354
t_matrix, "torque", self._args.temperature, highest
355355
)
356356

357-
if any(states[key]):
358-
S_conf_res = ce.conformational_entropy_calculation(
359-
states[key], number_frames
360-
)
361-
else:
362-
S_conf_res = 0
357+
values = states[key]
358+
359+
contains_non_empty_states = (
360+
np.any(values) if isinstance(values, np.ndarray) else any(values)
361+
)
362+
363+
S_conf_res = (
364+
ce.conformational_entropy_calculation(values, number_frames)
365+
if contains_non_empty_states
366+
else 0
367+
)
363368

364369
S_trans += S_trans_res
365370
S_rot += S_rot_res
@@ -424,12 +429,22 @@ def _process_conformational_entropy(
424429
start, end, step (int): Frame bounds.
425430
n_frames (int): Number of frames used.
426431
"""
427-
if states[group_id]:
428-
S_conf = ce.conformational_entropy_calculation(
429-
states[group_id], number_frames
432+
group_states = states[group_id] if group_id < len(states) else None
433+
434+
if group_states is not None:
435+
contains_state_data = (
436+
group_states.any()
437+
if isinstance(group_states, np.ndarray)
438+
else any(group_states)
430439
)
431440
else:
432-
S_conf = 0
441+
contains_state_data = False
442+
443+
S_conf = (
444+
ce.conformational_entropy_calculation(group_states, number_frames)
445+
if contains_state_data
446+
else 0
447+
)
433448

434449
self._data_logger.add_results_data(group_id, level, "Conformational", S_conf)
435450

0 commit comments

Comments
 (0)