Skip to content

Commit 96738c8

Browse files
maarten-icolivhoenen
authored andcommitted
Fix issue with fill_consistent and simplify logic
- Errorbars were not cleared by `unset_coordinate()`, this is fixed now - Use imas.util.tree_iter in unset_coordinate, which is more clear than visit_children and performs slightly better - Simplify some checks in `maybe_set_random_value()`
1 parent af759c3 commit 96738c8

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

imas/test/test_helpers.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from imas.ids_struct_array import IDSStructArray
2020
from imas.ids_structure import IDSStructure
2121
from imas.ids_toplevel import IDSToplevel
22-
from imas.util import idsdiffgen, visit_children
22+
from imas.util import idsdiffgen, tree_iter
2323

2424
logger = logging.getLogger(__name__)
2525

@@ -130,29 +130,13 @@ def maybe_set_random_value(
130130
if primitive.metadata.name.endswith("_error_upper"):
131131
name = primitive.metadata.name[: -len("_error_upper")]
132132
data = primitive._parent[name]
133-
if (
134-
not data.has_value
135-
or len(data.shape) == 0
136-
or any(s == 0 for s in data.shape)
137-
):
138-
return
139-
if any(
140-
same_as.references for same_as in primitive.metadata.coordinates_same_as
141-
):
133+
if not data.has_value:
142134
return
143135
shape = list(data.shape)
144136
elif primitive.metadata.name.endswith("_error_lower"):
145137
name = primitive.metadata.name[: -len("_error_lower")] + "_error_upper"
146138
data = primitive._parent[name]
147-
if (
148-
not data.has_value
149-
or len(data.shape) == 0
150-
or any(s == 0 for s in data.shape)
151-
):
152-
return
153-
if any(
154-
same_as.references for same_as in primitive.metadata.coordinates_same_as
155-
):
139+
if not data.has_value:
156140
return
157141
shape = list(data.shape)
158142
else:
@@ -317,21 +301,29 @@ def fill_consistent(
317301

318302

319303
def unset_coordinate(coordinate):
304+
def unset(element):
305+
# Unset element value
306+
element.value = []
307+
# But also its errorbars (if they exist)
308+
try:
309+
element._parent[element.metadata.name + "_error_upper"].value = []
310+
element._parent[element.metadata.name + "_error_lower"].value = []
311+
except AttributeError:
312+
pass # Ignore when element has no errorbars
313+
320314
# Unset the coordinate quantity
321-
coordinate.value = []
315+
unset(coordinate)
322316
# Find all elements that also have this as a coordinate and unset...
323317
parent = coordinate._dd_parent
324318
while parent.metadata.data_type is not IDSDataType.STRUCT_ARRAY:
325319
parent = parent._dd_parent
326320

327-
def callback(element):
321+
for element in tree_iter(parent):
328322
if hasattr(element, "coordinates") and element.has_value:
329323
for ele_coor in element.coordinates:
330324
if ele_coor is coordinate:
331-
element.value = []
332-
return
333-
334-
visit_children(callback, parent)
325+
unset(element)
326+
break
335327

336328

337329
def compare_children(st1, st2, deleted_paths=set(), accept_lazy=False):

0 commit comments

Comments
 (0)