Skip to content

Commit

Permalink
numpy-style docstrings for the snapshot module
Browse files Browse the repository at this point in the history
  • Loading branch information
jobovy committed Oct 20, 2023
1 parent e890271 commit b041386
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 166 deletions.
33 changes: 15 additions & 18 deletions galpy/snapshot/GadgetSnapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ class GadgetSnapshot:

def __init__(self, *args, **kwargs):
"""
NAME:
__init__
PURPOSE:
initialize a Gadget snapshot object
INPUT:
Initialize using:
1) filename
OUTPUT:
HISTORY:
2011-08-15 - Started - Bovy
Initialize a Gadget snapshot object.
Parameters
----------
*args : str
Filename of the snapshot.
Notes
-----
- 2011-08-15 - Started - Bovy
"""
if not _PYNBODYENABLED:
raise ImportError("pynbody could not be loaded to read the gadget snapshot")
Expand All @@ -38,15 +39,11 @@ def __getattr__(self, i):

def plot(self, *args, **kwargs):
"""
NAME:
plot
PURPOSE:
plot the snapshot
INPUT:
Plot the snapshot.
OUTPUT:
HISTORY:
2011-08-15 - Started - Bovy (NYU)
Notes
-----
- 2011-08-15 - Started - Bovy (NYU)
"""
labeldict = {
"t": r"$t$",
Expand Down
154 changes: 80 additions & 74 deletions galpy/snapshot/Snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ class Snapshot:

def __init__(self, *args, **kwargs):
"""
NAME:
__init__
PURPOSE:
initialize a snapshot object
INPUT:
Initialize using:
1) list of orbits, list of masses (masses=)
Coming soon:
2) observations
3) DFs to draw from
OUTPUT:
HISTORY:
2011-02-02 - Started - Bovy
Initialize a snapshot object.
Parameters
----------
*args : list
List of orbits, list of masses (masses=)
**kwargs : dict
Coming soon:
1) observations
2) DFs to draw from
Notes
-----
- 2011-02-02 - Started - Bovy
"""
if isinstance(args[0], list) and isinstance(args[0][0], Orbit):
self.orbits = args[0]
Expand All @@ -35,19 +36,29 @@ def __init__(self, *args, **kwargs):

def integrate(self, t, pot=None, method="test-particle", **kwargs):
"""
NAME:
integrate
PURPOSE:
integrate the snapshot in time
INPUT:
t - numpy.array of times to save the snapshots at (must start at 0)
pot= potential object or list of such objects (default=None)
method= method to use ('test-particle' or 'direct-python' for now)
OUTPUT:
list of snapshots at times t
HISTORY:
2011-02-02 - Written - Bovy (NYU)
Integrate the snapshot in time.
Parameters
----------
t : numpy.ndarray
Times to save the snapshots at (must start at 0).
pot : object or list of objects, optional
Potential object(s) (default=None).
method : str, optional
Method to use ('test-particle' or 'direct-python' for now).
**kwargs
Additional keyword arguments to pass to the integration method.
Returns
-------
list
List of snapshots at times t.
Notes
-----
- 2011-02-02 - Written - Bovy (NYU)
"""

if method.lower() == "test-particle":
return self._integrate_test_particle(t, pot)
elif method.lower() == "direct-python":
Expand Down Expand Up @@ -156,30 +167,27 @@ def _integrate_direct_python(self, t, pot, **kwargs):
# Plotting
def plot(self, *args, **kwargs):
"""
NAME:
plot
PURPOSE:
plot the snapshot (with reasonable defaults)
INPUT:
d1= first dimension to plot ('x', 'y', 'R', 'vR', 'vT', 'z', 'vz', ...)
d2= second dimension to plot
matplotlib.plot inputs+galpy.util.plot.plot inputs
OUTPUT:
sends plot to output device
HISTORY:
2011-02-06 - Written based on Orbit's plot
Plot the snapshot (with reasonable defaults)
Parameters
----------
d1 : str, optional
First dimension to plot ('x', 'y', 'R', 'vR', 'vT', 'z', 'vz', ...).
d2 : str, optional
Second dimension to plot.
*args : tuple
Matplotlib.plot inputs + galpy.util.plot.plot inputs.
**kwargs : dict
Matplotlib.plot inputs + galpy.util.plot.plot inputs.
Returns
-------
None
Sends plot to output device.
Notes
-----
- 2011-02-06 - Written based on Orbit's plot
"""
labeldict = {
"t": r"$t$",
Expand Down Expand Up @@ -274,31 +282,29 @@ def plot(self, *args, **kwargs):

def plot3d(self, *args, **kwargs):
"""
NAME:
plot3d
PURPOSE:
plot the snapshot in 3D (with reasonable defaults)
INPUT:
d1= first dimension to plot ('x', 'y', 'R', 'vR', 'vT', 'z', 'vz', ...)
d2= second dimension to plot
d3= third dimension to plot
matplotlib.plot inputs+galpy.util.plot.plot3d inputs
OUTPUT:
sends plot to output device
HISTORY:
2011-02-06 - Written based on Orbit's plot3d
Plot the snapshot in 3D (with reasonable defaults)
Parameters
----------
d1 : str, optional
First dimension to plot ('x', 'y', 'R', 'vR', 'vT', 'z', 'vz', ...).
d2 : str, optional
Second dimension to plot.
d3 : str, optional
Third dimension to plot.
*args
Matplotlib.plot inputs+galpy.util.plot.plot3d inputs.
**kwargs
Matplotlib.plot inputs+galpy.util.plot.plot3d inputs.
Returns
-------
None
Sends plot to output device.
Notes
-----
- 2011-02-06 - Written based on Orbit's plot3d
"""
labeldict = {
Expand Down
43 changes: 27 additions & 16 deletions galpy/snapshot/directnbody.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,33 @@ def direct_nbody(
rtol=None,
):
"""
NAME:
direct_nbody
PURPOSE:
N-body code using direct summation for force evaluation
INPUT:
q - list of initial positions (numpy.ndarrays)
p - list of initial momenta (numpy.ndarrays)
m - list of masses
t - times at which output is desired
pot= external potential (galpy.potential or list of galpy.potentials)
softening_model= type of softening to use ('plummer')
softening_length= (optional)
OUTPUT:
list of [q,p] at times t
HISTORY:
2011-02-03 - Written - Bovy (NYU)
N-body code using direct summation for force evaluation
Parameters
----------
q : list
list of initial positions (numpy.ndarrays)
p : list
list of initial momenta (numpy.ndarrays)
m : list
list of masses
t : list
times at which output is desired
pot : galpy.potential or list of galpy.potentials, optional
external potential
softening_model : str, optional
type of softening to use ('plummer')
softening_length : float, optional
softening length
Parameters
----------
list
list of [q,p] at times t
Notes
-----
- 2011-02-03 - Written - Bovy (NYU).
"""
# Set up everything
if softening_model.lower() == "plummer":
Expand Down
31 changes: 19 additions & 12 deletions galpy/snapshot/nemo_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@

def read(filename, ext=None, swapyz=False):
"""
NAME:
read
PURPOSE:
read a NEMO snapshot file consisting of mass,position,velocity
INPUT:
filename - name of the file
ext= if set, 'nemo' for NEMO binary format, otherwise assumed ASCII; if not set, gleaned from extension
swapyz= (False) if True, swap the y and z axes in the output (only for position and velocity)
OUTPUT:
snapshots [nbody,ndim,nt]
HISTORY:
2015-11-18 - Written - Bovy (UofT)
Read a NEMO snapshot file consisting of mass, position, velocity.
Parameters
----------
filename : str
Name of the file.
ext : str, optional
If set, 'nemo' for NEMO binary format, otherwise assumed ASCII; if not set, gleaned from extension.
swapyz : bool, optional
If True, swap the y and z axes in the output (only for position and velocity).
Returns
-------
ndarray
Array of shape (nbody, ndim, nt).
Notes
-----
- 2015-11-18 - Written - Bovy (UofT).
"""
if ext is None and filename.split(".")[-1] == "nemo":
ext = "nemo"
Expand Down
74 changes: 28 additions & 46 deletions galpy/snapshot/snapshotMovies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,34 @@

def snapshotToMovie(snap, filename, *args, **kwargs):
"""
NAME:
snapshotToMovie
PURPOSE:
turn a list of snapshots into a movie
INPUT:
snap - the snapshots (list)
filename - name of the file to save the movie to
framerate= in fps
bitrate= ?
thumbnail=False : create thumbnail image (filename-extension+.jpg)
thumbsize= size of thumbnail
+Snapshot.plot args and kwargs
OUTPUT:
movie is saved to file
DEPENDENCIES:
this procedure uses ffmpeg and convert
BUGS:
matplotlib's 'Agg' backend has a memory leak that prevents it from
creating hundred's of figures. It is recommended to call
import matplotlib
matplotlib.use('PDF')
at the beginning of the movie creating script as the PDF backend does
not have the same memory leak.
HISTORY:
2011-02-06 - Written - Bovy (NYU)
Turn a list of snapshots into a movie
Parameters
----------
snap : list
The snapshots
filename : str
Name of the file to save the movie to
framerate : int, optional
Frames per second (default is 25)
bitrate : int, optional
Bitrate (default is 1000)
thumbnail : bool, optional
Create thumbnail image (filename-extension+.jpg) (default is False)
thumbsize : int, optional
Size of thumbnail (default is 300)
*args
Arguments for Snapshot.plot
**kwargs
Keyword arguments for Snapshot.plot
Returns
-------
None
Notes
-----
- 2011-02-06 - Written - Bovy (NYU)
"""
if kwargs.has_key("tmpdir"):
Expand Down

0 comments on commit b041386

Please sign in to comment.