46
46
from __future__ import annotations
47
47
48
48
import collections .abc
49
+ import itertools
49
50
import typing
50
51
import warnings
51
52
58
59
if typing .TYPE_CHECKING :
59
60
from cosmology import Cosmology
60
61
61
- ArrayLike1D = typing . Union [ collections .abc .Sequence [float ], npt .NDArray [np .float64 ] ]
62
+ ArrayLike1D = collections .abc .Sequence [float ] | npt .NDArray [np .float64 ]
62
63
WeightFunc = typing .Callable [[ArrayLike1D ], npt .NDArray [np .float64 ]]
63
64
64
65
@@ -232,17 +233,11 @@ def tophat_windows(
232
233
wht : WeightFunc
233
234
wht = weight if weight is not None else np .ones_like
234
235
ws = []
235
- for zmin , zmax in zip (zbins , zbins [ 1 :] ):
236
+ for zmin , zmax in itertools . pairwise (zbins ):
236
237
n = max (round ((zmax - zmin ) / dz ), 2 )
237
238
z = np .linspace (zmin , zmax , n )
238
239
w = wht (z )
239
- zeff = np .trapz ( # type: ignore[attr-defined]
240
- w * z ,
241
- z ,
242
- ) / np .trapz ( # type: ignore[attr-defined]
243
- w ,
244
- z ,
245
- )
240
+ zeff = np .trapezoid (w * z , z ) / np .trapezoid (w , z )
246
241
ws .append (RadialWindow (z , w , zeff ))
247
242
return ws
248
243
@@ -296,7 +291,7 @@ def linear_windows(
296
291
warnings .warn ("first triangular window does not start at z=0" , stacklevel = 2 )
297
292
298
293
ws = []
299
- for zmin , zmid , zmax in zip (zgrid , zgrid [1 :], zgrid [2 :]):
294
+ for zmin , zmid , zmax in zip (zgrid , zgrid [1 :], zgrid [2 :], strict = False ):
300
295
n = max (round ((zmid - zmin ) / dz ), 2 ) - 1
301
296
m = max (round ((zmax - zmid ) / dz ), 2 )
302
297
z = np .concatenate (
@@ -361,7 +356,7 @@ def cubic_windows(
361
356
warnings .warn ("first cubic spline window does not start at z=0" , stacklevel = 2 )
362
357
363
358
ws = []
364
- for zmin , zmid , zmax in zip (zgrid , zgrid [1 :], zgrid [2 :]):
359
+ for zmin , zmid , zmax in zip (zgrid , zgrid [1 :], zgrid [2 :], strict = False ):
365
360
n = max (round ((zmid - zmin ) / dz ), 2 ) - 1
366
361
m = max (round ((zmax - zmid ) / dz ), 2 )
367
362
z = np .concatenate (
@@ -580,11 +575,7 @@ def partition_lstsq(
580
575
581
576
# create the window function matrix
582
577
a = np .array ([np .interp (zp , za , wa , left = 0.0 , right = 0.0 ) for za , wa , _ in shells ])
583
- a /= np .trapz ( # type: ignore[attr-defined]
584
- a ,
585
- zp ,
586
- axis = - 1 ,
587
- )[..., None ]
578
+ a /= np .trapezoid (a , zp , axis = - 1 )[..., None ]
588
579
a = a * dz
589
580
590
581
# create the target vector of distribution values
@@ -594,20 +585,7 @@ def partition_lstsq(
594
585
# append a constraint for the integral
595
586
mult = 1 / sumtol
596
587
a = np .concatenate ([a , mult * np .ones ((len (shells ), 1 ))], axis = - 1 )
597
- b = np .concatenate (
598
- [
599
- b ,
600
- mult
601
- * np .reshape (
602
- np .trapz ( # type: ignore[attr-defined]
603
- fz ,
604
- z ,
605
- ),
606
- (* dims , 1 ),
607
- ),
608
- ],
609
- axis = - 1 ,
610
- )
588
+ b = np .concatenate ([b , mult * np .reshape (np .trapezoid (fz , z ), (* dims , 1 ))], axis = - 1 )
611
589
612
590
# now a is a matrix of shape (len(shells), len(zp) + 1)
613
591
# and b is a matrix of shape (*dims, len(zp) + 1)
@@ -672,11 +650,7 @@ def partition_nnls(
672
650
for za , wa , _ in shells
673
651
],
674
652
)
675
- a /= np .trapz ( # type: ignore[attr-defined]
676
- a ,
677
- zp ,
678
- axis = - 1 ,
679
- )[..., None ]
653
+ a /= np .trapezoid (a , zp , axis = - 1 )[..., None ]
680
654
a = a * dz
681
655
682
656
# create the target vector of distribution values
@@ -686,20 +660,7 @@ def partition_nnls(
686
660
# append a constraint for the integral
687
661
mult = 1 / sumtol
688
662
a = np .concatenate ([a , mult * np .ones ((len (shells ), 1 ))], axis = - 1 )
689
- b = np .concatenate (
690
- [
691
- b ,
692
- mult
693
- * np .reshape (
694
- np .trapz ( # type: ignore[attr-defined]
695
- fz ,
696
- z ,
697
- ),
698
- (* dims , 1 ),
699
- ),
700
- ],
701
- axis = - 1 ,
702
- )
663
+ b = np .concatenate ([b , mult * np .reshape (np .trapezoid (fz , z ), (* dims , 1 ))], axis = - 1 )
703
664
704
665
# now a is a matrix of shape (len(shells), len(zp) + 1)
705
666
# and b is a matrix of shape (*dims, len(zp) + 1)
@@ -743,11 +704,7 @@ def partition_restrict(
743
704
part = np .empty ((len (shells ),) + np .shape (fz )[:- 1 ])
744
705
for i , w in enumerate (shells ):
745
706
zr , fr = restrict (z , fz , w )
746
- part [i ] = np .trapz ( # type: ignore[attr-defined]
747
- fr ,
748
- zr ,
749
- axis = - 1 ,
750
- )
707
+ part [i ] = np .trapezoid (fr , zr , axis = - 1 )
751
708
return part
752
709
753
710
@@ -879,15 +836,11 @@ def combine(
879
836
* np .interp (
880
837
z ,
881
838
shell .za ,
882
- shell .wa
883
- / np .trapz ( # type: ignore[attr-defined]
884
- shell .wa ,
885
- shell .za ,
886
- ),
839
+ shell .wa / np .trapezoid (shell .wa , shell .za ),
887
840
left = 0.0 ,
888
841
right = 0.0 ,
889
842
)
890
- for shell , weight in zip (shells , weights )
843
+ for shell , weight in zip (shells , weights , strict = False )
891
844
],
892
845
axis = 0 ,
893
846
)
0 commit comments