@@ -156,13 +156,9 @@ def _prepend_edge(arr, pad_amt, axis=-1):
156
156
if pad_amt == 0 :
157
157
return arr
158
158
159
- edge_slice = tuple (slice (None ) if i != axis else 0
159
+ edge_slice = tuple (slice (None ) if i != axis else slice ( 0 , 1 )
160
160
for (i , x ) in enumerate (arr .shape ))
161
-
162
- # Shape to restore singleton dimension after slicing
163
- pad_singleton = tuple (x if i != axis else 1
164
- for (i , x ) in enumerate (arr .shape ))
165
- edge_arr = arr [edge_slice ].reshape (pad_singleton )
161
+ edge_arr = arr [edge_slice ]
166
162
return np .concatenate ((edge_arr .repeat (pad_amt , axis = axis ), arr ),
167
163
axis = axis )
168
164
@@ -190,13 +186,9 @@ def _append_edge(arr, pad_amt, axis=-1):
190
186
if pad_amt == 0 :
191
187
return arr
192
188
193
- edge_slice = tuple (slice (None ) if i != axis else arr . shape [ axis ] - 1
189
+ edge_slice = tuple (slice (None ) if i != axis else slice ( x - 1 , x )
194
190
for (i , x ) in enumerate (arr .shape ))
195
-
196
- # Shape to restore singleton dimension after slicing
197
- pad_singleton = tuple (x if i != axis else 1
198
- for (i , x ) in enumerate (arr .shape ))
199
- edge_arr = arr [edge_slice ].reshape (pad_singleton )
191
+ edge_arr = arr [edge_slice ]
200
192
return np .concatenate ((arr , edge_arr .repeat (pad_amt , axis = axis )),
201
193
axis = axis )
202
194
@@ -236,15 +228,11 @@ def _prepend_ramp(arr, pad_amt, end, axis=-1):
236
228
reverse = True ).astype (np .float64 )
237
229
238
230
# Appropriate slicing to extract n-dimensional edge along `axis`
239
- edge_slice = tuple (slice (None ) if i != axis else 0
231
+ edge_slice = tuple (slice (None ) if i != axis else slice ( 0 , 1 )
240
232
for (i , x ) in enumerate (arr .shape ))
241
233
242
- # Shape to restore singleton dimension after slicing
243
- pad_singleton = tuple (x if i != axis else 1
244
- for (i , x ) in enumerate (arr .shape ))
245
-
246
- # Extract edge, reshape to original rank, and extend along `axis`
247
- edge_pad = arr [edge_slice ].reshape (pad_singleton ).repeat (pad_amt , axis )
234
+ # Extract edge, and extend along `axis`
235
+ edge_pad = arr [edge_slice ].repeat (pad_amt , axis )
248
236
249
237
# Linear ramp
250
238
slope = (end - edge_pad ) / float (pad_amt )
@@ -291,15 +279,11 @@ def _append_ramp(arr, pad_amt, end, axis=-1):
291
279
reverse = False ).astype (np .float64 )
292
280
293
281
# Slice a chunk from the edge to calculate stats on
294
- edge_slice = tuple (slice (None ) if i != axis else - 1
282
+ edge_slice = tuple (slice (None ) if i != axis else slice ( x - 1 , x )
295
283
for (i , x ) in enumerate (arr .shape ))
296
284
297
- # Shape to restore singleton dimension after slicing
298
- pad_singleton = tuple (x if i != axis else 1
299
- for (i , x ) in enumerate (arr .shape ))
300
-
301
- # Extract edge, reshape to original rank, and extend along `axis`
302
- edge_pad = arr [edge_slice ].reshape (pad_singleton ).repeat (pad_amt , axis )
285
+ # Extract edge, and extend along `axis`
286
+ edge_pad = arr [edge_
1E80
slice ].repeat (pad_amt , axis )
303
287
304
288
# Linear ramp
305
289
slope = (end - edge_pad ) / float (pad_amt )
@@ -351,12 +335,8 @@ def _prepend_max(arr, pad_amt, num, axis=-1):
351
335
max_slice = tuple (slice (None ) if i != axis else slice (num )
352
336
for (i , x ) in enumerate (arr .shape ))
353
337
354
- # Shape to restore singleton dimension after slicing
355
- pad_singleton = tuple (x if i != axis else 1
356
- for (i , x ) in enumerate (arr .shape ))
357
-
358
- # Extract slice, calculate max, reshape to add singleton dimension back
359
- max_chunk = arr [max_slice ].max (axis = axis ).reshape (pad_singleton )
338
+ # Extract slice, calculate max
339
+ max_chunk = arr [max_slice ].max (axis = axis , keepdims = True )
360
340
361
341
# Concatenate `arr` with `max_chunk`, extended along `axis` by `pad_amt`
362
342
return np .concatenate ((max_chunk .repeat (pad_amt , axis = axis ), arr ),
@@ -407,12 +387,8 @@ def _append_max(arr, pad_amt, num, axis=-1):
407
387
else :
408
388
max_slice = tuple (slice (None ) for x in arr .shape )
409
389
410
- # Shape to restore singleton dimension after slicing
411
- pad_singleton = tuple (x if i != axis else 1
412
- for (i , x ) in enumerate (arr .shape ))
413
-
414
- # Extract slice, calculate max, reshape to add singleton dimension back
415
- max_chunk = arr [max_slice ].max (axis = axis ).reshape (pad_singleton )
390
+ # Extract slice, calculate max
391
+ max_chunk = arr [max_slice ].max (axis = axis , keepdims = True )
416
392
417
393
# Concatenate `arr` with `max_chunk`, extended along `axis` by `pad_amt`
418
394
return np .concatenate ((arr , max_chunk .repeat (pad_amt , axis = axis )),
@@ -458,12 +434,8 @@ def _prepend_mean(arr, pad_amt, num, axis=-1):
458
434
mean_slice = tuple (slice (None ) if i != axis else slice (num )
459
435
for (i , x ) in enumerate (arr .shape ))
460
436
461
- # Shape to restore singleton dimension after slicing
462
- pad_singleton = tuple (x if i != axis else 1
463
- for (i , x ) in enumerate (arr .shape ))
464
-
465
- # Extract slice, calculate mean, reshape to add singleton dimension back
466
- mean_chunk = arr [mean_slice ].mean (axis ).reshape (pad_singleton )
437
+ # Extract slice, calculate mean
438
+ mean_chunk = arr [mean_slice ].mean (axis , keepdims = True )
467
439
_round_ifneeded (mean_chunk , arr .dtype )
468
440
469
441
# Concatenate `arr` with `mean_chunk`, extended along `axis` by `pad_amt`
@@ -515,12 +487,8 @@ def _append_mean(arr, pad_amt, num, axis=-1):
515
487
else :
516
488
mean_slice = tuple (slice (None ) for x in arr .shape )
517
489
518
- # Shape to restore singleton dimension after slicing
519
- pad_singleton = tuple (x if i != axis else 1
520
- for (i , x ) in enumerate (arr .shape ))
521
-
522
- # Extract slice, calculate mean, reshape to add singleton dimension back
523
- mean_chunk = arr [mean_slice ].mean (axis = axis ).reshape (pad_singleton )
490
+ # Extract slice, calculate mean
491
+ mean_chunk = arr [mean_slice ].mean (axis = axis , keepdims = True )
524
492
_round_ifneeded (mean_chunk , arr .dtype )
525
493
526
494
# Concatenate `arr` with `mean_chunk`, extended along `axis` by `pad_amt`
@@ -567,12 +535,8 @@ def _prepend_med(arr, pad_amt, num, axis=-1):
567
535
med_slice = tuple (slice (None ) if i != axis else slice (num )
568
536
for (i , x ) in enumerate (arr .shape ))
569
537
570
- # Shape to restore singleton dimension after slicing
571
- pad_singleton = tuple (x if i != axis else 1
572
- for (i , x ) in enumerate (arr .shape ))
573
-
574
- # Extract slice, calculate median, reshape to add singleton dimension back
575
- med_chunk = np .median (arr [med_slice ], axis = axis ).reshape (pad_singleton )
538
+ # Extract slice, calculate median
539
+ med_chunk = np .median (arr [med_slice ], axis = axis , keepdims = True )
576
540
_round_ifneeded (med_chunk , arr .dtype )
577
541
578
542
# Concatenate `arr` with `med_chunk`, extended along `axis` by `pad_amt`
@@ -624,12 +588,8 @@ def _append_med(arr, pad_amt, num, axis=-1):
624
588
else :
625
589
med_slice = tuple (slice (None ) for x in arr .shape )
626
590
627
- # Shape to restore singleton dimension after slicing
628
- pad_singleton = tuple (x if i != axis else 1
629
- for (i , x ) in enumerate (arr .shape ))
630
-
631
- # Extract slice, calculate median, reshape to add singleton dimension back
632
- med_chunk = np .median (arr [med_slice ], axis = axis ).reshape (pad_singleton )
591
+ # Extract slice, calculate median
592
+ med_chunk = np .median (arr [med_slice ], axis = axis , keepdims = True )
633
593
_round_ifneeded (med_chunk , arr .dtype )
634
594
635
595
# Concatenate `arr` with `med_chunk`, extended along `axis` by `pad_amt`
@@ -677,12 +637,8 @@ def _prepend_min(arr, pad_amt, num, axis=-1):
677
637
min_slice = tuple (slice (None ) if i != axis else slice (num )
678
638
for (i , x ) in enumerate (arr .shape ))
679
639
680
- # Shape to restore singleton dimension after slicing
681
- pad_singleton = tuple (x if i != axis else 1
682
- for (i , x ) in enumerate (arr .shape ))
683
-
684
- # Extract slice, calculate min, reshape to add singleton dimension back
685
- min_chunk = arr [min_slice ].min (axis = axis ).reshape (pad_singleton )
640
+ # Extract slice, calculate min
641
+ min_chunk = arr [min_slice ].min (axis = axis , keepdims = True )
686
642
687
643
# Concatenate `arr` with `min_chunk`, extended along `axis` by `pad_amt`
688
644
return np .concatenate ((min_chunk .repeat (pad_amt , axis = axis ), arr ),
@@ -733,12 +689,8 @@ def _append_min(arr, pad_amt, num, axis=-1):
733
689
else :
734
690
min_slice = tuple (slice (None ) for x in arr .shape )
735
691
736
- # Shape to restore singleton dimension after slicing
737
- pad_singleton = tuple (x if i != axis else 1
738
- for (i , x ) in enumerate (arr .shape ))
739
-
740
- # Extract slice, calculate min, reshape to add singleton dimension back
741
- min_chunk = arr [min_slice ].min (axis = axis ).reshape (pad_singleton )
692
+ # Extract slice, calculate min
693
+ min_chunk = arr [min_slice ].min (axis = axis , keepdims = True )
742
694
743
695
# Concatenate `arr` with `min_chunk`, extended along `axis` by `pad_amt`
744
696
return np .concatenate ((arr , min_chunk .repeat (pad_amt , axis = axis )),
@@ -790,17 +742,11 @@ def _pad_ref(arr, pad_amt, method, axis=-1):
790
742
791
743
ref_chunk1 = arr [ref_slice ]
792
744
793
- # Shape to restore singleton dimension after slicing
794
- pad_singleton = tuple (x if i != axis else 1
795
- for (i , x ) in enumerate (arr .shape ))
796
- if pad_amt [0 ] == 1 :
797
- ref_chunk1 = ref_chunk1 .reshape (pad_singleton )
798
-
799
745
# Memory/computationally more expensive, only do this if `method='odd'`
800
746
if 'odd' in method and pad_amt [0 ] > 0 :
801
- edge_slice1 = tuple (slice (None ) if i != axis else 0
747
+ edge_slice1 = tuple (slice (None ) if i != axis else slice ( 0 , 1 )
802
748
for (i , x ) in enumerate (arr .shape ))
803
- edge_chunk = arr [edge_slice1 ]. reshape ( pad_singleton )
749
+ edge_chunk = arr [edge_slice1 ]
804
750
ref_chunk1 = 2 * edge_chunk - ref_chunk1
805
751
del edge_chunk
806
752
@@ -816,13 +762,10 @@ def _pad_ref(arr, pad_amt, method, axis=-1):
816
762
for (i , x ) in enumerate (arr .shape ))
817
763
ref_chunk2 = arr [ref_slice ][rev_idx ]
818
764
819
- if pad_amt [1 ] == 1 :
820
- ref_chunk2 = ref_chunk2 .reshape (pad_singleton )
821
-
822
765
if 'odd' in method :
823
- edge_slice2 = tuple (slice (None ) if i != axis else - 1
766
+ edge_slice2 = tuple (slice (None ) if i != axis else slice ( x - 1 , x )
824
767
for (i , x ) in enumerate (arr .shape ))
825
- edge_chunk = arr [edge_slice2 ]. reshape ( pad_singleton )
768
+ edge_chunk = arr [edge_slice2 ]
826
769
ref_chunk2 = 2 * edge_chunk - ref_chunk2
827
770
del edge_chunk
828
771
@@ -876,17 +819,11 @@ def _pad_sym(arr, pad_amt, method, axis=-1):
876
819
for (i , x ) in enumerate (arr .shape ))
877
820
sym_chunk1 = arr [sym_slice ][rev_idx ]
878
821
879
- # Shape to restore singleton dimension after slicing
880
- pad_singleton = tuple (x if i != axis else 1
881
- for (i , x ) in enumerate (arr .shape ))
882
- if pad_amt [0 ] == 1 :
883
- sym_chunk1 = sym_chunk1 .reshape (pad_singleton )
884
-
885
822
# Memory/computationally more expensive, only do this if `method='odd'`
886
823
if 'odd' in method and pad_amt [0 ] > 0 :
887
- edge_slice1 = tuple (slice (None ) if i != axis else 0
824
+ edge_slice1 = tupl
F438
e (slice (None ) if i != axis else slice ( 0 , 1 )
888
825
for (i , x ) in enumerate (arr .shape ))
889
- edge_chunk = arr [edge_slice1 ]. reshape ( pad_singleton )
826
+ edge_chunk = arr [edge_slice1 ]
890
827
sym_chunk1 = 2 * edge_chunk - sym_chunk1
891
828
del edge_chunk
892
829
@@ -900,13 +837,10 @@ def _pad_sym(arr, pad_amt, method, axis=-1):
900
837
for (i , x ) in enumerate (arr .shape ))
901
838
sym_chunk2 = arr [sym_slice ][rev_idx ]
902
839
903
- if pad_amt [1 ] == 1 :
904
- sym_chunk2 = sym_chunk2 .reshape (pad_singleton )
905
-
906
840
if 'odd' in method :
907
- edge_slice2 = tuple (slice (None ) if i != axis else - 1
841
+ edge_slice2 = tuple (slice (None ) if i != axis else slice ( x - 1 , x )
908
842
for (i , x ) in enumerate (arr .shape ))
909
- edge_chunk = arr [edge_slice2 ]. reshape ( pad_singleton )
843
+ edge_chunk = arr [edge_slice2 ]
910
844
sym_chunk2 = 2 * edge_chunk - sym_chunk2
911
845
del edge_chunk
912
846
@@ -957,12 +891,6 @@ def _pad_wrap(arr, pad_amt, axis=-1):
957
891
for (i , x ) in enumerate (arr .shape ))
958
892
wrap_chunk1 = arr [wrap_slice ]
959
893
960
- # Shape to restore singleton dimension after slicing
961
- pad_singleton = tuple (x if i != axis else 1
962
- for (i , x ) in enumerate (arr .shape ))
963
- if pad_amt [0 ] == 1 :
964
- wrap_chunk1 = wrap_chunk1 .reshape (pad_singleton )
965
-
966
894
##########################################################################
967
895
# Appended region
968
896
@@ -971,9 +899,6 @@ def _pad_wrap(arr, pad_amt, axis=-1):
971
899
for (i , x ) in enumerate (arr .shape ))
972
900
wrap_chunk2 = arr [wrap_slice ]
973
901
974
- if pad_amt [1 ] == 1 :
975
- wrap_chunk2 = wrap_chunk2 .reshape (pad_singleton )
976
-
977
902
# Concatenate `arr` with both chunks, extending along `axis`
978
903
return np .concatenate ((wrap_chunk1 , arr , wrap_chunk2 ), axis = axis )
979
904
0 commit comments