@@ -164,7 +164,7 @@ def id_to_index(self, id) -> int:
164164 def annotate (self , ** new_annotations ) -> None :
165165 self ._annotations .update (new_annotations )
166166
167- def set_annotation (self , annotation_key , value : Any , overwrite = False ) -> None :
167+ def set_annotation (self , annotation_key : str , value : Any , overwrite = False ) -> None :
168168 """This function adds an entry to the annotations dictionary.
169169
170170 Parameters
@@ -192,7 +192,7 @@ def get_preferred_mp_context(self):
192192 """
193193 return self ._preferred_mp_context
194194
195- def get_annotation (self , key , copy : bool = True ) -> Any :
195+ def get_annotation (self , key : str , copy : bool = True ) -> Any :
196196 """
197197 Get a annotation.
198198 Return a copy by default
@@ -205,7 +205,13 @@ def get_annotation(self, key, copy: bool = True) -> Any:
205205 def get_annotation_keys (self ) -> List :
206206 return list (self ._annotations .keys ())
207207
208- def set_property (self , key , values : Sequence , ids : Optional [Sequence ] = None , missing_value : Any = None ) -> None :
208+ def set_property (
209+ self ,
210+ key ,
211+ values : list | np .ndarray | tuple ,
212+ ids : list | np .ndarray | tuple | None = None ,
213+ missing_value : Any = None ,
214+ ) -> None :
209215 """
210216 Set property vector for main ids.
211217
@@ -223,6 +229,7 @@ def set_property(self, key, values: Sequence, ids: Optional[Sequence] = None, mi
223229 Array of values for the property
224230 ids : list/np.array, default: None
225231 List of subset of ids to set the values, default: None
232+ if None which is the default all the ids are set or changed
226233 missing_value : object, default: None
227234 In case the property is set on a subset of values ("ids" not None),
228235 it specifies the how the missing values should be filled.
@@ -240,23 +247,26 @@ def set_property(self, key, values: Sequence, ids: Optional[Sequence] = None, mi
240247 dtype_kind = dtype .kind
241248
242249 if ids is None :
243- assert values .shape [0 ] == size
250+ assert (
251+ values .shape [0 ] == size
252+ ), f"Values must have the same size as the main ids: { size } but got array of size { values .shape [0 ]} "
244253 self ._properties [key ] = values
245254 else :
246255 ids = np .array (ids )
247- assert np .unique (ids ).size == ids .size , "'ids' are not unique!"
256+ unique_ids = np .unique (ids )
257+ if unique_ids .size != ids .size :
258+ non_unique_ids = [id for id in ids if np .count_nonzero (ids == id ) > 1 ]
259+ raise ValueError (f"IDs are not unique: { non_unique_ids } " )
248260
261+ # Not clear where this branch is used, perhaps on aggregation of extractors?
249262 if ids .size < size :
250263 if key not in self ._properties :
251- # create the property with nan or empty
252- shape = (size ,) + values .shape [1 :]
253264
254265 if missing_value is None :
255266 if dtype_kind not in self .default_missing_property_values .keys ():
256- raise Exception (
257- "For values dtypes other than float, string, object or unicode, the missing value "
258- "cannot be automatically inferred. Please specify it with the 'missing_value' "
259- "argument."
267+ raise ValueError (
268+ f"Can't infer a natural missing value for dtype { dtype_kind } . "
269+ "Please provide it with the `missing_value` argument"
260270 )
261271 else :
262272 missing_value = self .default_missing_property_values [dtype_kind ]
@@ -268,15 +278,18 @@ def set_property(self, key, values: Sequence, ids: Optional[Sequence] = None, mi
268278 "as the values."
269279 )
270280
281+ # create the property with nan or empty
282+ shape = (size ,) + values .shape [1 :]
271283 empty_values = np .zeros (shape , dtype = dtype )
272284 empty_values [:] = missing_value
273285 self ._properties [key ] = empty_values
274286 if ids .size == 0 :
275287 return
276288 else :
277- assert dtype_kind == self ._properties [key ].dtype .kind , (
278- "Mismatch between existing property dtype " "values dtype."
279- )
289+ existing_property = self ._properties [key ].dtype
290+ assert (
291+ dtype_kind == existing_property .kind
292+ ), f"Mismatch between existing property dtype { existing_property .kind } and provided values dtype { dtype_kind } ."
280293
281294 indices = self .ids_to_indices (ids )
282295 self ._properties [key ][indices ] = values
@@ -285,7 +298,7 @@ def set_property(self, key, values: Sequence, ids: Optional[Sequence] = None, mi
285298 self ._properties [key ] = np .zeros_like (values , dtype = values .dtype )
286299 self ._properties [key ][indices ] = values
287300
288- def get_property (self , key , ids : Optional [Iterable ] = None ) -> np .ndarray :
301+ def get_property (self , key : str , ids : Optional [Iterable ] = None ) -> np .ndarray :
289302 values = self ._properties .get (key , None )
290303 if ids is not None and values is not None :
291304 inds = self .ids_to_indices (ids )
0 commit comments