Skip to content

Commit 489d60c

Browse files
committed
Use squeeze safely.
1 parent d9fe3c9 commit 489d60c

2 files changed

Lines changed: 50 additions & 31 deletions

File tree

swiftsimio/conversions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ def swift_neutrinos_to_astropy(N_eff, N_ur, M_nu_eV, deg_nu):
4646
"SWIFTsimIO uses astropy, which cannot handle this cosmological model."
4747
)
4848
print(N_eff, deg_nu, N_ur)
49-
if not int(N_eff) == deg_nu.astype(int).sum() + int(N_ur.squeeze()):
49+
if not int(N_eff) == deg_nu.astype(int).sum() + int(np.squeeze(N_ur)):
5050
raise AttributeError(
5151
"SWIFTSimIO uses astropy, which cannot handle this cosmological model."
5252
)
5353
ap_m_nu = [[m] * int(d) for m, d in zip(M_nu_eV, deg_nu)] # replicate
5454
ap_m_nu = sum(ap_m_nu, []) + [0.0] * int(
55-
N_ur.squeeze()
55+
np.squeeze(N_ur)
5656
) # flatten + add massless
5757
ap_m_nu = np.array(ap_m_nu) * astropy_units.eV
5858
return ap_m_nu

swiftsimio/objects.py

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
For developers, see also :mod:`~swiftsimio._array_functions` containing
88
helpers, wrappers and implementations that enable most :mod:`numpy` and
9-
:mod:`unyt` functions to work with our cosmology-aware arrays.
9+
:mod:`unyt` functions to work with our cosmology-aware arrays.
1010
"""
1111

1212
from unyt import unyt_array, unyt_quantity
@@ -161,7 +161,7 @@ def __init__(
161161
) -> None:
162162
"""
163163
Constructor for warning of invalid conversion.
164-
164+
165165
Parameters
166166
----------
167167
message : str, optional
@@ -239,61 +239,85 @@ class cosmo_factor:
239239
comoving and physical coordinates.
240240
241241
This takes the expected exponent of the array that can be parsed
242-
by sympy, and the current value of the cosmological scale factor ``a``.
242+
by :mod:`sympy`, and the current value of the cosmological scale factor ``a``.
243243
244244
This should be given as the conversion from comoving to physical, i.e.
245+
:math:`r = a^f \times r` where :math:`a` is the scale factor,
246+
:math:`r` is a physical quantity and :math`r'` a comoving quantity.
245247
246-
r = cosmo_factor * r' with r in physical and r' comoving
248+
Parameters
249+
----------
250+
expr : sympy.expr
251+
Expression used to convert between comoving and physical coordinates.
252+
scale_factor : float
253+
The scale factor (a).
247254
255+
Attributes
256+
----------
257+
expr : sympy.expr
258+
Expression used to convert between comoving and physical coordinates.
259+
scale_factor : float
260+
The scale factor (a).
261+
248262
Examples
249263
--------
250-
Typically this would make cosmo_factor = a for the conversion between
251-
comoving positions r' and physical co-ordinates r.
264+
Mass density transforms as :math:`a^3`. To set up a ``cosmo_factor``, supposing
265+
a current ``scale_factor=0.97``, we import the scale factor ``a`` and initialize
266+
as:
252267
253-
To do this, use the a imported from objects multiplied as you'd like:
268+
::
254269
255-
``density_cosmo_factor = cosmo_factor(a**3, scale_factor=0.97)``
270+
from swiftsimio.objects import a # the scale factor (a sympy symbol object)
271+
density_cosmo_factor = cosmo_factor(a**3, scale_factor=0.97)
256272
273+
:class:`~swiftsimio.objects.cosmo_factor` support arithmetic, for example:
274+
275+
::
276+
277+
>>> cosmo_factor(a**2, scale_factor=0.5) * cosmo_factor(a**-1, scale_factor=0.5)
278+
cosmo_factor(expr=a, scale_factor=0.5)
257279
"""
258280

281+
expr: sympy.expr
282+
scale_factor: float
283+
259284
def __init__(self, expr, scale_factor):
260285
"""
261286
Constructor for cosmology factor class.
262287
263288
Parameters
264289
----------
265290
expr : sympy.expr
266-
expression used to convert between comoving and physical coordinates
291+
Expression used to convert between comoving and physical coordinates.
267292
scale_factor : float
268-
the scale factor of the simulation data
293+
The scale factor (a).
269294
"""
270295
self.expr = expr
271296
self.scale_factor = scale_factor
272297
pass
273298

274299
def __str__(self):
275300
"""
276-
Print exponent and current scale factor
301+
Print exponent and current scale factor.
277302
278303
Returns
279304
-------
280-
str
281-
string to print exponent and current scale factor
305+
out : str
306+
String with exponent and current scale factor.
282307
"""
283308
return str(self.expr) + f" at a={self.scale_factor}"
284309

285310
@property
286311
def a_factor(self):
287312
"""
288-
The a-factor for the unit.
313+
The multiplicative factor for conversion from comoving to physical.
289314
290-
e.g. for density this is 1 / a**3.
315+
For example, for density this is :math:`a^{-3}`.
291316
292317
Returns
293318
-------
294-
295-
float
296-
the a-factor for given unit
319+
out : float
320+
The multiplicative factor for conversion from comoving to physical.
297321
"""
298322
if (self.expr is None) or (self.scale_factor is None):
299323
return None
@@ -302,20 +326,15 @@ def a_factor(self):
302326
@property
303327
def redshift(self):
304328
"""
305-
Compute the redshift from the scale factor.
329+
The redshift computed from the scale factor.
330+
331+
Returns the redshift :math:`z = \frac{1}{a} - 1`, where :math:`a` is the scale
332+
factor.
306333
307334
Returns
308335
-------
309-
310-
float
311-
redshift from the given scale factor
312-
313-
Notes
314-
-----
315-
316-
Returns the redshift
317-
..math:: z = \\frac{1}{a} - 1,
318-
where :math: `a` is the scale factor
336+
out : float
337+
The redshift.
319338
"""
320339
if self.scale_factor is None:
321340
return None

0 commit comments

Comments
 (0)