Skip to content

Commit e85d8f2

Browse files
committed
use numpy array to create outlet array rather than list comprehension
1 parent ba08cab commit e85d8f2

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed

pyflwdir/basins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def interbasin_mask(idxs_ds, seq, region, stream=None):
5252
if mask[idx0]:
5353
mask[idxs_ds[idx0]] = True
5454
else:
55-
mask = np.array([bool(1) for _ in range(region.size)]) # all True
55+
mask = np.ones(region.size, dtype=np.bool_)
5656
# keep only the most downstream contiguous area within region
5757
for idx0 in seq: # down- to upstream
5858
idx_ds = idxs_ds[idx0]

pyflwdir/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def snap(
485485
def inflow_idxs(idxs_ds, seq, region):
486486
"""returns linear indices of most upstream cells within region"""
487487
idxs = []
488-
mask = np.array([bool(1) for _ in range(idxs_ds.size)]) # all True
488+
mask = np.ones(idxs_ds.size, dtype=np.bool_)
489489
for idx0 in seq[::-1]: # up- to downstream
490490
idx_ds = idxs_ds[idx0]
491491
if idx0 != idx_ds:
@@ -502,7 +502,7 @@ def inflow_idxs(idxs_ds, seq, region):
502502
def outflow_idxs(idxs_ds, seq, region):
503503
"""returns linear indices of most downstream cells within region"""
504504
idxs = []
505-
mask = np.array([bool(1) for _ in range(idxs_ds.size)]) # all True
505+
mask = np.ones(idxs_ds.size, dtype=np.bool_)
506506
for idx0 in seq: # down- to upstream
507507
idx_ds = idxs_ds[idx0]
508508
# at mask and region and (pit or out)

pyflwdir/dem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def adjust_elevation(idxs_ds, seq, elevtn, mv=_mv):
154154
2012.
155155
"""
156156
elevtn_out = elevtn.copy()
157-
mask = np.array([bool(0) for _ in range(elevtn.size)]) # True for checked cells
157+
mask = np.zeros(idxs_ds.size, dtype=np.bool_)
158158
for idx0 in seq[::-1]: # from up- to downstream starting from longest stream paths
159159
if mask[idx0] == False: # @ head water cell
160160
# get downstream indices up to earlier fixed stream path

pyflwdir/streams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def streams(idxs_ds, seq, mask=None, max_len=0, mv=core._mv):
154154
nup = core.upstream_count(idxs_ds=idxs_ds, mask=mask, mv=mv)
155155
# get list of indices arrays of segments
156156
streams = []
157-
done = np.array([bool(0) for _ in range(idxs_ds.size)]) # all False
157+
done = np.zeros(idxs_ds.size, dtype=np.bool_)
158158
for idx0 in seq[::-1]: # up- to downstream
159159
if done[idx0] or (mask is not None and ~mask[idx0]):
160160
continue

pyflwdir/subgrid.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def segment_length(
177177
channel section length [m]
178178
"""
179179
# temp binary array with outlets
180-
outlets = np.array([bool(0) for _ in range(idxs_nxt.size)])
180+
outlets = np.zeros(idxs_nxt.size, dtype=np.bool_)
181181
for idx0 in idxs_out:
182182
if idx0 != mv:
183183
outlets[idx0] = bool(1)
@@ -240,7 +240,7 @@ def segment_average(
240240
segment mean value [m]
241241
"""
242242
# temp binary array with outlets
243-
outlets = np.array([bool(0) for _ in range(idxs_nxt.size)])
243+
outlets = np.zeros(idxs_nxt.size, dtype=np.bool_)
244244
for idx0 in idxs_out:
245245
if idx0 != mv:
246246
outlets[idx0] = bool(1)
@@ -305,7 +305,7 @@ def segment_median(
305305
segment median value [m]
306306
"""
307307
# temp binary array with outlets
308-
outlets = np.array([bool(0) for _ in range(idxs_nxt.size)])
308+
outlets = np.zeros(idxs_nxt.size, dtype=np.bool_)
309309
for idx0 in idxs_out:
310310
if idx0 != mv:
311311
outlets[idx0] = bool(1)
@@ -373,7 +373,7 @@ def segment_indices(
373373
linear indices of streams
374374
"""
375375
# temp binary array with outlets
376-
outlets = np.array([bool(0) for _ in range(idxs_nxt.size)])
376+
outlets = np.zeros(idxs_nxt.size, dtype=np.bool_)
377377
for idx0 in idxs_out:
378378
if idx0 != mv:
379379
outlets[idx0] = bool(1)
@@ -446,7 +446,7 @@ def segment_slope(
446446
channel section slope [m]
447447
"""
448448
# temp binary array with outlets
449-
outlets = np.array([bool(0) for _ in range(idxs_nxt.size)])
449+
outlets = np.zeros(idxs_nxt.size, dtype=np.bool_)
450450
for idx0 in idxs_out:
451451
if idx0 != mv:
452452
outlets[idx0] = bool(1)

pyflwdir/upscale.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ def upscale_error(subidxs_out, idxs_ds, subidxs_ds, mv=_mv):
13351335
assert subidxs_out.size == idxs_ds.size
13361336
# binary array with outlets
13371337
subn = subidxs_ds.size
1338-
outlets = np.array([bool(0) for _ in range(subn)])
1338+
outlets = np.zeros(subn, dtype=np.bool_)
13391339
for subidx in subidxs_out:
13401340
if subidx == mv:
13411341
continue
@@ -1368,7 +1368,7 @@ def upscale_check(subidxs_out, idxs_ds, subidxs_ds, minlen=0, mv=_mv):
13681368
# array with outlets (>=0) and streams (-1); nodata value is -9
13691369
assert subidxs_out.size <= 2147483648
13701370
streams = np.full(subidxs_ds.size, -9, dtype=np.int32)
1371-
valid = np.array([bool(1) for _ in range(idxs_ds.size)])
1371+
valid = np.ones(idxs_ds.size, dtype=np.bool_)
13721372
for idx in range(subidxs_out.size):
13731373
subidx = subidxs_out[idx]
13741374
if subidx == mv:

0 commit comments

Comments
 (0)