Skip to content

Commit

Permalink
Merge pull request Mindwerks#249 from MM1nd/faster_matrix2
Browse files Browse the repository at this point in the history
Final speedup PR
  • Loading branch information
psi29a authored Oct 1, 2017
2 parents 4f8d80f + 08588f3 commit 20d93d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
31 changes: 14 additions & 17 deletions worldengine/model/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,21 @@ def protobuf_unserialize(cls, serialized):

@staticmethod
def _to_protobuf_matrix(matrix, p_matrix, transformation=None):
for row in matrix:

m = matrix
if transformation is not None:
t = numpy.vectorize(transformation)
m = t(m)

for row in m:
p_row = p_matrix.rows.add()
for cell in row:
'''
When using numpy, certain primitive types are replaced with
numpy-specifc versions that, even though mostly compatible,
cannot be digested by protobuf. This might change at some point;
for now a conversion is necessary.
'''
if type(cell) is numpy.bool_:
value = bool(cell)
elif type(cell) is numpy.uint16:
value = int(cell)
else:
value = cell
if transformation:
value = transformation(value)
p_row.cells.append(value)
'''
When using numpy, certain primitive types are replaced with
numpy-specifc versions that, even though mostly compatible,
cannot be digested by protobuf. This might change at some point;
for now a conversion is necessary.
'''
p_row.cells.extend(row.tolist())

@staticmethod
def _to_protobuf_quantiles(quantiles, p_quantiles):
Expand Down
4 changes: 2 additions & 2 deletions worldengine/simulations/erosion.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def river_sources(world, water_flow, water_path):
# we mark them as rivers. While looking, the cells with no
# out-going flow, above water flow threshold and are still
# above sea level are marked as 'sources'.
for x in range(0, world.width - 1):
for y in range(0, world.height - 1):
for y in range(0, world.height - 1):
for x in range(0, world.width - 1):
rain_fall = world.layers['precipitation'].data[y, x]
water_flow[y, x] = rain_fall

Expand Down

0 comments on commit 20d93d9

Please sign in to comment.