1313import numpy as np
1414
1515from power_grid_model_ds ._core import fancypy as fp
16+ from power_grid_model_ds ._core .model .arrays import IdArray
1617from power_grid_model_ds ._core .model .arrays .base .array import FancyArray
1718from power_grid_model_ds ._core .model .arrays .base .errors import RecordDoesNotExist
1819from power_grid_model_ds ._core .model .constants import EMPTY_ID
@@ -75,7 +76,7 @@ def find_array_field(cls, array_type: Type[FancyArray]) -> dataclasses.Field:
7576 @property
7677 def max_id (self ) -> int :
7778 """Returns the max id across all arrays within the container."""
78- max_per_array = [np .max (array . id ) if array .size > 0 else 0 for array in self .all_arrays ()]
79+ max_per_array = [np .max (array [ "id" ] ) if array .size > 0 else 0 for array in self .all_arrays ()]
7980 return int (max (max_per_array ))
8081
8182 def check_ids (self , check_between_arrays : bool = True , check_within_arrays : bool = True ) -> None :
@@ -89,7 +90,7 @@ def check_ids(self, check_between_arrays: bool = True, check_within_arrays: bool
8990 ValueError: if duplicates are found.
9091 """
9192
92- id_arrays = [array for array in self .all_arrays () if hasattr (array , "id" )]
93+ id_arrays = [array for array in self .all_arrays () if isinstance (array , IdArray )]
9394 if not id_arrays :
9495 return # no arrays to check
9596
@@ -113,7 +114,7 @@ def append(self, array: FancyArray, check_max_id: bool = True) -> None:
113114
114115 Args:
115116 array(FancyArray): the asset_array to be appended (e.g. a NodeArray instance).
116- check_max_id(bool): whether to check max(array.id ) with the id counter
117+ check_max_id(bool): whether to check max(array["id"] ) with the id counter
117118
118119 Returns:
119120 None
@@ -132,12 +133,12 @@ def attach_ids(self, array: FancyArray) -> FancyArray:
132133 if not array .size :
133134 return array
134135
135- if (id_set := set (array . id )) != {array .get_empty_value ("id" )}:
136+ if (id_set := set (array [ "id" ] )) != {array .get_empty_value ("id" )}:
136137 raise ValueError (f"Cannot attach ids to array that contains non-empty ids: { id_set } " )
137138
138139 start = self ._id_counter + 1
139140 end = start + len (array )
140- array . id = np .arange (start , end )
141+ array [ "id" ] = np .arange (start , end )
141142 self ._id_counter = max (self ._id_counter , end - 1 )
142143
143144 return array
@@ -175,15 +176,15 @@ def _append(self, array: FancyArray, check_max_id: bool = True) -> None:
175176 Append the given asset_array to the corresponding field of Grid and generate ids.
176177 Args:
177178 array: the asset_array to be appended (e.g. a KabelArray instance).
178- check_max_id: whether to check max(array.id ) with the id counter
179+ check_max_id: whether to check max(array["id"] ) with the id counter
179180 Returns: None.
180181 """
181182 if array .size == 0 :
182183 return
183184
184185 array_field = self .find_array_field (array .__class__ )
185186
186- if hasattr (array , "id" ):
187+ if isinstance (array , IdArray ):
187188 self ._update_id_counter (array , check_max_id )
188189
189190 # Add the given asset_array to the corresponding array in the Grid.
@@ -208,30 +209,30 @@ def _get_empty_arrays(cls) -> dict:
208209 }
209210
210211 def _update_id_counter (self , array , check_max_id : bool = True ):
211- if np .all (array . id == EMPTY_ID ):
212+ if np .all (array [ "id" ] == EMPTY_ID ):
212213 array = self .attach_ids (array )
213- elif np .any (array . id == EMPTY_ID ):
214+ elif np .any (array [ "id" ] == EMPTY_ID ):
214215 raise ValueError (f"Cannot append: array contains empty [{ EMPTY_ID } ] and non-empty ids." )
215216 elif check_max_id and self .id_counter > 0 :
216217 # Only check for overlaps when array has prescribed (non-empty) IDs
217218 # Check if any incoming ID might overlap with existing IDs
218219 # This prevents overlaps since counter tracks the highest used ID
219- new_min_id = np .min (array . id )
220+ new_min_id = np .min (array [ "id" ] )
220221 if new_min_id <= self ._id_counter :
221222 raise ValueError (
222223 f"Cannot append: minimum id { new_min_id } is not greater than "
223224 f"the current id counter { self ._id_counter } "
224225 )
225226
226- new_max_id = np .max (array . id )
227+ new_max_id = np .max (array [ "id" ] )
227228 # Update _id_counter
228229 self ._id_counter = max (self ._id_counter , new_max_id )
229230
230231 @staticmethod
231- def _get_duplicates_between_arrays (id_arrays : list [FancyArray ], check : bool ) -> np .ndarray :
232+ def _get_duplicates_between_arrays (id_arrays : list [IdArray ], check : bool ) -> np .ndarray :
232233 if not check :
233234 return np .array ([])
234- unique_ids_per_array = [np .unique (array . id ) for array in id_arrays ]
235+ unique_ids_per_array = [np .unique (array [ "id" ] ) for array in id_arrays ]
235236
236237 all_ids = np .concatenate (unique_ids_per_array )
237238
@@ -240,7 +241,7 @@ def _get_duplicates_between_arrays(id_arrays: list[FancyArray], check: bool) ->
240241 return unique_ids [duplicate_mask ]
241242
242243 @staticmethod
243- def _get_arrays_with_duplicates (id_arrays : list [FancyArray ], check : bool ) -> list :
244+ def _get_arrays_with_duplicates (id_arrays : list [IdArray ], check : bool ) -> list :
244245 arrays_with_duplicates : list [Type ] = []
245246 if not check :
246247 return arrays_with_duplicates
0 commit comments