Skip to content

Commit 77e4390

Browse files
authored
gh-998: Change all references to len to .shape[0] (#1032)
1 parent aa0e3ef commit 77e4390

File tree

15 files changed

+48
-47
lines changed

15 files changed

+48
-47
lines changed

glass/fields.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ def _generate_grf(
373373
rng = _rng.rng_dispatcher(xp=xp)
374374

375375
# number of gls and number of fields
376-
ngls = len(gls)
377-
ngrf = nfields_from_nspectra(ngls)
376+
ngrf = nfields_from_nspectra(len(gls))
378377

379378
# number of correlated fields if not specified
380379
if ncorr is None:
@@ -548,10 +547,10 @@ def getcl(
548547
i, j = j, i
549548
cl = cls[i * (i + 1) // 2 + i - j]
550549
if lmax is not None:
551-
if cl.size > lmax + 1:
550+
if cl.shape[0] > lmax + 1:
552551
cl = cl[: lmax + 1]
553552
else:
554-
cl = xpx.pad(cl, (0, lmax + 1 - cl.size))
553+
cl = xpx.pad(cl, (0, lmax + 1 - cl.shape[0]))
555554
return cl # ty: ignore[invalid-return-type]
556555

557556

@@ -764,7 +763,7 @@ def compute_gaussian_spectra(
764763

765764
gls = []
766765
for i, j, cl in enumerate_spectra(spectra):
767-
gl = glass.grf.compute(cl, fields[i], fields[j]) if cl.size > 0 else 0 * cl
766+
gl = glass.grf.compute(cl, fields[i], fields[j]) if cl.shape[0] > 0 else 0 * cl
768767
gls.append(gl)
769768
return gls
770769

@@ -802,12 +801,12 @@ def solve_gaussian_spectra(
802801

803802
gls = []
804803
for i, j, cl in enumerate_spectra(spectra):
805-
if cl.size > 0:
804+
if cl.shape[0] > 0:
806805
# transformation pair
807806
t1, t2 = fields[i], fields[j]
808807

809808
# set zero-padding of solver to 2N
810-
pad = 2 * cl.size
809+
pad = 2 * cl.shape[0]
811810

812811
# if the desired monopole is zero, that is most likely
813812
# and artefact of the theory spectra -- the variance of the
@@ -1008,7 +1007,7 @@ def cov_from_spectra(
10081007
n = nfields_from_nspectra(len(spectra))
10091008

10101009
# first case: maximum length in input spectra
1011-
k = max((cl.size for cl in spectra), default=0) if lmax is None else lmax + 1
1010+
k = max((cl.shape[0] for cl in spectra), default=0) if lmax is None else lmax + 1
10121011

10131012
# this is the covariance matrix of the spectra
10141013
# the leading dimension is k, then it is a n-by-n covariance matrix
@@ -1020,7 +1019,7 @@ def cov_from_spectra(
10201019
# if the spectra are ragged, some entries at high ell may remain zero
10211020
# only fill the lower triangular part, everything is symmetric
10221021
for i, j, cl in enumerate_spectra(spectra):
1023-
size = min(k, cl.size)
1022+
size = min(k, cl.shape[0])
10241023
cl_flat = xp.reshape(cl, (-1,))
10251024
cov = xpx.at(cov)[:size, i, j].set(cl_flat[:size])
10261025
cov = xpx.at(cov)[:size, j, i].set(cl_flat[:size])

glass/grf/_solver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def solve( # noqa: PLR0912, PLR0913
8989
if t2 is None:
9090
t2 = t1
9191

92-
n = len(cl)
92+
n = cl.shape[0]
9393
if pad < 0:
9494
msg = "pad must be a positive integer"
9595
raise ValueError(msg)
@@ -98,7 +98,7 @@ def solve( # noqa: PLR0912, PLR0913
9898
gl = corrtocl(glass.grf.icorr(t1, t2, cltocorr(cl)))
9999
else:
100100
gl = np.zeros(n)
101-
gl[: len(initial)] = initial[:n]
101+
gl[: initial.shape[0]] = initial[:n]
102102

103103
if monopole is not None:
104104
gl[0] = monopole

glass/healpix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def almxfl(
122122
alm
123123
The alm to multiply.
124124
fl
125-
The function (at l=0..fl.size-1) by which alm must be multiplied.
125+
The function (at l=0..fl.shape[0]-1) by which alm must be multiplied.
126126
inplace
127127
If True, modify the given alm, otherwise make a copy before multiplying.
128128

glass/observations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def tomo_nz_gausserr(
332332
Returns
333333
-------
334334
The tomographic redshift bins convolved with a gaussian error.
335-
Array has a shape (nbins, len(z))
335+
Array has a shape (nbins, z.shape[0])
336336
337337
See Also
338338
--------

glass/shells.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def _calculate_zeff(self) -> float:
260260
The effective redshift depending on the size of ``za``.
261261
262262
"""
263-
if self.za.size > 0:
263+
if self.za.shape[0] > 0:
264264
return uxpx.trapezoid(
265265
self.za * self.wa,
266266
self.za,
@@ -313,7 +313,7 @@ def tophat_windows(
313313
if zbins.ndim != 1:
314314
msg = "zbins must be a 1D array"
315315
raise ValueError(msg)
316-
if zbins.size < 2:
316+
if zbins.shape[0] < 2:
317317
msg = "zbins must have at least two entries"
318318
raise ValueError(msg)
319319
if zbins[0] != 0:
@@ -381,7 +381,7 @@ def linear_windows(
381381
if zgrid.ndim != 1:
382382
msg = "zgrid must be a 1D array"
383383
raise ValueError(msg)
384-
if zgrid.size < 3:
384+
if zgrid.shape[0] < 3:
385385
msg = "nodes must have at least 3 entries"
386386
raise ValueError(msg)
387387
if zgrid[0] != 0:
@@ -451,7 +451,7 @@ def cubic_windows(
451451
if zgrid.ndim != 1:
452452
msg = "zgrid must be a 1D array"
453453
raise ValueError(msg)
454-
if zgrid.size < 3:
454+
if zgrid.shape[0] < 3:
455455
msg = "nodes must have at least 3 entries"
456456
raise ValueError(msg)
457457
if zgrid[0] != 0:
@@ -705,13 +705,13 @@ def partition_lstsq(
705705
a = xp.concat([a, mult * xp.ones((len(shells), 1))], axis=-1)
706706
b = xp.concat([b, mult * xp.reshape(uxpx.trapezoid(fz, z), (*dims, 1))], axis=-1)
707707

708-
# now a is a matrix of shape (len(shells), len(zp) + 1)
709-
# and b is a matrix of shape (*dims, len(zp) + 1)
708+
# now a is a matrix of shape (len(shells), zp.shape[0] + 1)
709+
# and b is a matrix of shape (*dims, zp.shape[0] + 1)
710710
# need to find weights x such that b == x @ a over all axes of b
711711
# do the least-squares fit over partially flattened b, then reshape
712712
x = uxpx.linalg_lstsq(
713713
xp.matrix_transpose(a),
714-
xp.matrix_transpose(xp.reshape(b, (-1, zp.size + 1))),
714+
xp.matrix_transpose(xp.reshape(b, (-1, zp.shape[0] + 1))),
715715
rcond=None,
716716
)[0]
717717
x = xp.reshape(xp.matrix_transpose(x), (*dims, len(shells)))
@@ -777,8 +777,8 @@ def partition_nnls(
777777
a = xp.concat([a, mult * xp.ones((len(shells), 1))], axis=-1)
778778
b = xp.concat([b, mult * xp.reshape(uxpx.trapezoid(fz, z), (*dims, 1))], axis=-1)
779779

780-
# now a is a matrix of shape (len(shells), len(zp) + 1)
781-
# and b is a matrix of shape (*dims, len(zp) + 1)
780+
# now a is a matrix of shape (len(shells), zp.shape[0] + 1)
781+
# and b is a matrix of shape (*dims, zp.shape[0] + 1)
782782
# for each dim, find non-negative weights x such that b == a.T @ x
783783

784784
# reduce the dimensionality of the problem using a thin QR decomposition

glass/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def save_cls(
5353
Angular matter power spectra in *GLASS* ordering.
5454
5555
"""
56-
split = np.cumulative_sum([len(cl) for cl in cls[:-1]])
56+
split = np.cumulative_sum([cl.shape[0] for cl in cls[:-1]])
5757
values = np.concatenate(cls)
5858
np.savez(filename, values=values, split=split)
5959

tests/benchmarks/test_fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_getcl_lmax_0(
244244
lmax=0,
245245
)
246246
expected = xpb.asarray([max(random_i, random_j)], dtype=xpb.float64)
247-
assert result.size == 1
247+
assert result.shape[0] == 1
248248
compare.assert_allclose(result, expected)
249249

250250

@@ -276,5 +276,5 @@ def test_getcl_lmax_larger_than_cls(
276276
lmax=lmax,
277277
)
278278
expected = xpb.zeros((lmax - 1,), dtype=xpb.float64)
279-
assert result.size == lmax + 1
279+
assert result.shape[0] == lmax + 1
280280
compare.assert_allclose(result[2:], expected)

tests/benchmarks/test_lensing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ def multi_plane_weights_add_window(
129129
rounds=100,
130130
)
131131

132-
assert len(actual_convergence.kappa) == 10
132+
assert actual_convergence.kappa.shape[0] == 10
133133
for x in actual_convergence.kappa:
134134
assert x is not None

tests/core/grf/test_solver.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def test_pad(cl: FloatArray, rng: np.random.Generator) -> None:
3838
t = glass.grf.Lognormal(lam)
3939

4040
# check that output size matches pad
41-
_, cl_out, _ = glass.grf.solve(cl, t, pad=2 * cl.size)
41+
_, cl_out, _ = glass.grf.solve(cl, t, pad=2 * cl.shape[0])
4242

43-
assert cl_out.size == 3 * cl.size
43+
assert cl_out.shape[0] == 3 * cl.shape[0]
4444

4545
with pytest.raises(ValueError, match="pad must be a positive integer"):
4646
glass.grf.solve(cl, t, pad=-1)
@@ -87,12 +87,12 @@ def test_lognormal(
8787

8888
assert info > 0
8989

90-
compare.assert_allclose(cl_[1 : cl.size], cl[1:], atol=0.0, rtol=cltol)
90+
compare.assert_allclose(cl_[1 : cl.shape[0]], cl[1:], atol=0.0, rtol=cltol)
9191

9292
gl_ = glass.grf.compute(cl_, t1, t2)
9393

9494
assert gl[0] == gl0
95-
compare.assert_allclose(gl_[1 : gl.size], gl[1:])
95+
compare.assert_allclose(gl_[1 : gl.shape[0]], gl[1:])
9696

9797

9898
def test_monopole(

tests/core/test_fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,13 @@ def test_getcl(compare: type[Compare], xp: ModuleType) -> None:
475475
# check slicing
476476
result = glass.getcl(cls, i, j, lmax=0)
477477
expected = xp.asarray([max(i, j)], dtype=xp.float64)
478-
assert result.size == 1
478+
assert result.shape[0] == 1
479479
compare.assert_allclose(result, expected)
480480

481481
# check padding
482482
result = glass.getcl(cls, i, j, lmax=50)
483483
expected = xp.zeros((49,), dtype=xp.float64)
484-
assert result.size == 51
484+
assert result.shape[0] == 51
485485
compare.assert_allclose(result[2:], expected)
486486

487487

@@ -596,7 +596,7 @@ def test_compute_gaussian_spectra_gh639(mocker: MockerFixture, xp: ModuleType) -
596596
assert mock.call_args_list[0] == mocker.call(spectra[0], fields[0], fields[0])
597597
assert mock.call_args_list[1] == mocker.call(spectra[1], fields[1], fields[1])
598598
assert gls[:2] == [mock.return_value, mock.return_value]
599-
assert gls[2].size == 0
599+
assert gls[2].shape[0] == 0
600600

601601

602602
def test_solve_gaussian_spectra(mocker: MockerFixture, xp: ModuleType) -> None:

0 commit comments

Comments
 (0)