Skip to content

Commit

Permalink
Performance improvement for add_feature starmap functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SorooshMani-NOAA committed Jan 17, 2024
1 parent eaf4928 commit f37b424
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
8 changes: 5 additions & 3 deletions ocsmesh/hfun/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,9 +1228,11 @@ def add_feature(
_logger.info(
f"Transform creation took {time() - start2:f}")
start2 = time()
win_feature = [
ops.transform(transformer.transform, linestring)
for linestring in win_feature]
win_feature = pool.starmap(
ops.transform,
[(transformer.transform, linestring)
for linestring in win_feature]
)
_logger.info(
f"Transform apply took {time() - start2:f}")

Expand Down
53 changes: 33 additions & 20 deletions ocsmesh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,17 @@ def get_mesh_polygons(mesh):

def repartition_features(linestring, max_verts):
features = []
if len(linestring.coords) > max_verts:
new_feat = []
for segment in list(map(LineString, zip(
linestring.coords[:-1],
linestring.coords[1:]))):
new_feat.append(segment)
if len(new_feat) == max_verts - 1:
features.append(linemerge(new_feat))
new_feat = []
if len(new_feat) != 0:
features.append(linemerge(new_feat))
lstr_len = len(linestring.coords)
if lstr_len > max_verts:
list_lens = [max_verts] * (lstr_len // max_verts)
if lstr_len % max_verts != 0:
list_lens += [lstr_len % max_verts]
new_idx = np.cumsum(list_lens) - 1
orig_coords = np.array(linestring.coords)
last_idx = 0
for idx in new_idx:
features.append(LineString(orig_coords[last_idx:idx + 1]))
last_idx = idx
else:
features.append(linestring)
return features
Expand All @@ -229,16 +229,29 @@ def transform_linestring(
linestring: LineString,
target_size: float,
):
distances = [0.]
while distances[-1] + target_size < linestring.length:
distances.append(distances[-1] + target_size)
distances.append(linestring.length)
linestring = LineString([
linestring.interpolate(distance)
for distance in distances
])
return linestring
lstr_len = linestring.length
distances = np.cumsum(np.ones(int(lstr_len // target_size)) * target_size)
if distances[-1] < lstr_len:
distances = np.append(distances, lstr_len)

orig_coords = np.array(linestring.coords)
lengths = ((orig_coords[1:] - orig_coords[:-1]) ** 2).sum(axis=1) ** 0.5
cum_len = np.cumsum(lengths)

assert(max(cum_len) == max(distances))

# Memory issue due to matrix creation?
idx = len(cum_len) - (distances[:, None] <= cum_len).sum(axis=1)
ratio = ((cum_len[idx] - distances) / lengths[idx])[:, None]

interp_coords = (
orig_coords[idx] * (ratio) + orig_coords[idx + 1] * (1 - ratio)
)
interp_coords = np.vstack((orig_coords[0], interp_coords))

linestring = LineString(interp_coords)

return linestring


def needs_sieve(mesh, area=None):
Expand Down
27 changes: 27 additions & 0 deletions tests/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,5 +1133,32 @@ def test_always_returns_multipolygon(self):
self.assertIsInstance(mesh_poly_1, MultiPolygon)
self.assertIsInstance(mesh_poly_2, MultiPolygon)


class RepartitionFeature(unittest.TestCase):
def test_io_types(self):
self.assertTrue(False)


def test_num_verts(self):
self.assertTrue(False)


def test_connecting_nodes(self):
self.assertTrue(False)


class TransformLineString(unittest.TestCase):
def test_io_types(self):
self.assertTrue(False)


def test_begin_and_end(self):
self.assertTrue(False)


def test_lengths(self)
self.assertTrue(False)


if __name__ == '__main__':
unittest.main()

0 comments on commit f37b424

Please sign in to comment.