Skip to content

Commit b237166

Browse files
Optimize append and union methods to minimize repeated calls to append
1 parent cdfeac2 commit b237166

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

climada/hazard/centroids/centr.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,16 @@ def from_pnt_bounds(cls, points_bounds, res, crs=DEF_CRS):
331331
}
332332
)
333333

334-
def append(self, centr):
335-
"""Append Centroids to the current object for batch concatenation.
334+
def append(self, *centr):
335+
"""Append Centroids to the current centroid object for concatenation.
336336
337-
This method adds the `centr.gdf` to the list of DataFrames to be concatenated
338-
later with `finalize_append()`. Instead of concatenating immediately, it accumulates
339-
the centroids in `_batch_gdf` to perform the concatenation all at once, which is more
340-
efficient for multiple appends.
337+
This method check that all centroids use the same CRS, append the list of centroids to
338+
the initial Centroid object and eventually concatenate them to create a single centroid
339+
object with the union of all centroids.
341340
342341
Note that the result might contain duplicate points if the object to append has an overlap
343-
with the current object.
342+
with the current object. Duplicates points will be removed in `union`
343+
by calling `remove_duplicate_points`.
344344
345345
Parameters
346346
----------
@@ -356,34 +356,18 @@ def append(self, centr):
356356
union : Union of Centroid objects.
357357
remove_duplicate_points : Remove duplicate points in a Centroids object.
358358
"""
359-
360-
if not hasattr(self, "_batch_gdf"):
361-
self._batch_gdf = [] # Initialize the batch
362-
363-
if not u_coord.equal_crs(self.crs, centr.crs):
364-
raise ValueError(
365-
f"The given centroids use different CRS: {self.crs}, {centr.crs}. "
366-
"The centroids are incompatible and cannot be concatenated."
367-
)
368-
self._batch_gdf.append(centr.gdf)
369-
370-
def finalize_append(self):
371-
"""Concatenate all batch-appended centroids into the main GeoDataFrame (gdf).
372-
373-
This method should be called after all `append` operations have been performed on the
374-
Centroids object. It concatenates all the accumulated GeoDataFrames stored in the
375-
`_batch_gdf` list into the `gdf` attribute of the Centroids object. By doing this in one
376-
step, it avoids the performance overhead associated with repeated concatenations.
377-
378-
Once concatenation is complete, the `_batch_gdf` list is cleared to prepare for future
379-
append operations.
380-
"""
381-
382-
self.gdf = pd.concat([self.gdf] + self._batch_gdf, ignore_index=True)
383-
self._batch_gdf = [] # clear the batch after concatenation
359+
for cc in centr:
360+
if not u_coord.equal_crs(self.crs, cc.crs):
361+
raise ValueError(
362+
f"The given centroids use different CRS: {self.crs}, {cc.crs}. "
363+
"The centroids are incompatible and cannot be concatenated."
364+
)
365+
self.gdf = pd.concat([self.gdf] + [cc.gdf for cc in centr])
384366

385367
def union(self, *others):
386-
"""Create the union of Centroids objects
368+
"""Create the union of the current Centroids object with one or more other centroids
369+
objects by passing the list of centroids to `append` for concatenation and then
370+
removes duplicates.
387371
388372
All centroids must have the same CRS. Points that are contained in more than one of the
389373
Centroids objects will only be contained once (i.e. duplicates are removed).
@@ -399,9 +383,8 @@ def union(self, *others):
399383
Centroids object containing the union of all Centroids.
400384
"""
401385
centroids = copy.deepcopy(self)
402-
for cent in others:
403-
centroids.append(cent)
404-
centroids.finalize_append()
386+
centroids.append(*others)
387+
405388
return centroids.remove_duplicate_points()
406389

407390
def remove_duplicate_points(self):

0 commit comments

Comments
 (0)