|
19 | 19 | from imas.ids_struct_array import IDSStructArray |
20 | 20 | from imas.ids_structure import IDSStructure |
21 | 21 | from imas.ids_toplevel import IDSToplevel |
22 | | -from imas.util import idsdiffgen, visit_children |
| 22 | +from imas.util import idsdiffgen, tree_iter |
23 | 23 |
|
24 | 24 | logger = logging.getLogger(__name__) |
25 | 25 |
|
@@ -130,29 +130,13 @@ def maybe_set_random_value( |
130 | 130 | if primitive.metadata.name.endswith("_error_upper"): |
131 | 131 | name = primitive.metadata.name[: -len("_error_upper")] |
132 | 132 | 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: |
142 | 134 | return |
143 | 135 | shape = list(data.shape) |
144 | 136 | elif primitive.metadata.name.endswith("_error_lower"): |
145 | 137 | name = primitive.metadata.name[: -len("_error_lower")] + "_error_upper" |
146 | 138 | 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: |
156 | 140 | return |
157 | 141 | shape = list(data.shape) |
158 | 142 | else: |
@@ -317,21 +301,29 @@ def fill_consistent( |
317 | 301 |
|
318 | 302 |
|
319 | 303 | 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 | + |
320 | 314 | # Unset the coordinate quantity |
321 | | - coordinate.value = [] |
| 315 | + unset(coordinate) |
322 | 316 | # Find all elements that also have this as a coordinate and unset... |
323 | 317 | parent = coordinate._dd_parent |
324 | 318 | while parent.metadata.data_type is not IDSDataType.STRUCT_ARRAY: |
325 | 319 | parent = parent._dd_parent |
326 | 320 |
|
327 | | - def callback(element): |
| 321 | + for element in tree_iter(parent): |
328 | 322 | if hasattr(element, "coordinates") and element.has_value: |
329 | 323 | for ele_coor in element.coordinates: |
330 | 324 | if ele_coor is coordinate: |
331 | | - element.value = [] |
332 | | - return |
333 | | - |
334 | | - visit_children(callback, parent) |
| 325 | + unset(element) |
| 326 | + break |
335 | 327 |
|
336 | 328 |
|
337 | 329 | def compare_children(st1, st2, deleted_paths=set(), accept_lazy=False): |
|
0 commit comments