Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/cheatsheet.tex
928 views
1
\documentclass[10pt,landscape]{article}
2
\usepackage{multicol}
3
\usepackage{calc}
4
\usepackage{ifthen}
5
\usepackage[landscape]{geometry}
6
\usepackage[hyphens]{url}
7
8
% To make this come out properly in landscape mode, do one of the following
9
% 1.
10
% pdflatex cheatsheet.tex
11
%
12
% 2.
13
% latex cheatsheet.tex
14
% dvips -P pdf -t landscape cheatsheet.dvi
15
% ps2pdf cheatsheet.ps
16
17
18
% If you're reading this, be prepared for confusion. Making this was
19
% a learning experience for me, and it shows. Much of the placement
20
% was hacked in; if you make it better, let me know...
21
22
23
% 2008-04
24
% Changed page margin code to use the geometry package. Also added code for
25
% conditional page margins, depending on paper size. Thanks to Uwe Ziegenhagen
26
% for the suggestions.
27
28
% 2006-08
29
% Made changes based on suggestions from Gene Cooperman. <gene at ccs.neu.edu>
30
31
% 2012-11 - Stephen Skory
32
% Converted the latex cheat sheet to a yt cheat sheet, taken from
33
% http://www.stdout.org/~winston/latex/
34
35
36
% This sets page margins to .5 inch if using letter paper, and to 1cm
37
% if using A4 paper. (This probably isn't strictly necessary.)
38
% If using another size paper, use default 1cm margins.
39
\ifthenelse{\lengthtest { \paperwidth = 11in}}
40
{ \geometry{top=.5in,left=.5in,right=.5in,bottom=0.85in} }
41
{\ifthenelse{ \lengthtest{ \paperwidth = 297mm}}
42
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
43
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
44
}
45
46
% Turn off header and footer
47
\pagestyle{empty}
48
49
50
% Redefine section commands to use less space
51
\makeatletter
52
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
53
{-1ex plus -.5ex minus -.2ex}%
54
{0.5ex plus .2ex}%x
55
{\normalfont\large\bfseries}}
56
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
57
{-1explus -.5ex minus -.2ex}%
58
{0.5ex plus .2ex}%
59
{\normalfont\normalsize\bfseries}}
60
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
61
{-1ex plus -.5ex minus -.2ex}%
62
{1ex plus .2ex}%
63
{\normalfont\small\bfseries}}
64
\makeatother
65
66
% Define BibTeX command
67
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
68
T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
69
70
% Don't print section numbers
71
\setcounter{secnumdepth}{0}
72
73
74
\setlength{\parindent}{0pt}
75
\setlength{\parskip}{0pt plus 0.5ex}
76
77
78
% -----------------------------------------------------------------------
79
80
\begin{document}
81
82
\raggedright
83
\fontsize{3mm}{3mm}\selectfont
84
\begin{multicols}{3}
85
86
87
% multicol parameters
88
% These lengths are set only within the two main columns
89
%\setlength{\columnseprule}{0.25pt}
90
\setlength{\premulticols}{1pt}
91
\setlength{\postmulticols}{1pt}
92
\setlength{\multicolsep}{1pt}
93
\setlength{\columnsep}{2pt}
94
95
\begin{center}
96
\Large{\textbf{yt Cheat Sheet}} \\
97
\end{center}
98
99
\subsection{General Info}
100
For everything yt please see \url{http://yt-project.org}.
101
Documentation \url{http://yt-project.org/doc/index.html}.
102
Need help? Start here \url{http://yt-project.org/doc/help/} and then
103
try the IRC chat room \url{http://yt-project.org/irc.html},
104
or the mailing list \url{https://mail.python.org/archives/list/yt-users@python.org/}. \\
105
106
\subsection{Installing yt} The easiest way to install yt is to use the
107
installation script found on the yt homepage or the docs linked above. If you
108
already have python set up with \texttt{numpy}, \texttt{scipy},
109
\texttt{matplotlib}, \texttt{h5py}, and \texttt{cython}, you can also use
110
\texttt{pip install yt}
111
112
\subsection{Command Line yt}
113
yt, and its convenience functions, are launched from a command line prompt.
114
Many commands have flags to control behavior.
115
Commands can be followed by
116
{\bf {-}{-}help} (e.g. {\bf yt render {-}{-}help}) for detailed help for that command
117
including a list of the available flags.
118
119
\texttt{yt load} \textit{dataset} \textemdash\ Load a single dataset. \\
120
\texttt{yt help} \textemdash\ Print yt help information. \\
121
\texttt{yt stats} \textit{dataset} \textemdash\ Print stats of a dataset. \\
122
\texttt{yt update} \textemdash\ Update yt to most recent version.\\
123
\texttt{yt update --all} \textemdash\ Update yt and dependencies to most recent version. \\
124
\texttt{yt version} \textemdash\ yt installation information. \\
125
\texttt{yt upload\_image} \textit{image.png} \textemdash\ Upload PNG image to imgur.com. \\
126
\texttt{yt upload\_notebook} \textit{notebook.nb} \textemdash\ Upload IPython notebook to \url{https://girder.hub.yt}.\\
127
\texttt{yt plot} \textit{dataset} \textemdash\ Create a set of images.\\
128
\texttt{yt render} \textit{dataset} \textemdash\ Create a simple
129
volume rendering. \\
130
\texttt{yt mapserver} \textit{dataset} \textemdash\ View a plot/projection in a Gmaps-like
131
interface. \\
132
\texttt{yt pastebin} \textit{text.out} \textemdash\ Post text to the pastebin at
133
paste.yt-project.org. \\
134
\texttt{yt pastebin\_grab} \textit{identifier} \textemdash\ Print content of pastebin to
135
STDOUT. \\
136
\texttt{yt bugreport} \textemdash\ Report a yt bug. \\
137
\texttt{yt hop} \textit{dataset} \textemdash\ Run hop on a dataset. \\
138
139
\subsection{yt Imports}
140
In order to use yt, Python must load the relevant yt modules into memory.
141
The import commands are entered in the Python/IPython shell or
142
used as part of a script.
143
\newlength{\MyLen}
144
\settowidth{\MyLen}{\texttt{letterpaper}/\texttt{a4paper} \ }
145
\texttt{import yt} \textemdash\
146
Load yt. \\
147
\texttt{from yt.config import ytcfg} \textemdash\
148
Used to set yt configuration options.
149
If used, must be called before importing any other module.\\
150
\texttt{from yt.analysis\_modules.\emph{halo\_finding}.api import \textasteriskcentered} \textemdash\
151
Load halo finding modules. Other modules
152
are loaded in a similar way by swapping the
153
\emph{emphasized} text.
154
See the \textbf{Analysis Modules} section for a listing and short descriptions of each.
155
156
\subsection{YTArray}
157
Simulation data in yt is returned as a YTArray. YTArray is a numpy array that
158
has unit data attached to it and can automatically handle unit conversions and
159
detect unit errors. Just like a numpy array, YTArray provides a wealth of
160
built-in functions to calculate properties of the data in the array. Here is a
161
very brief list of some useful ones.
162
\settowidth{\MyLen}{\texttt{multicol} }\\
163
\texttt{v = a.in\_cgs()} \textemdash\ Return the array in CGS units \\
164
\texttt{v = a.in\_units('Msun/pc**3')} \textemdash\ Return the array in solar masses per cubic parsec \\
165
\texttt{v = a.max(), a.min()} \textemdash\ Return maximum, minimum of \texttt{a}. \\
166
\texttt{index = a.argmax(), a.argmin()} \textemdash\ Return index of max,
167
min value of \texttt{a}.\\
168
\texttt{v = a[}\textit{index}\texttt{]} \textemdash\ Select a single value from \texttt{a} at location \textit{index}.\\
169
\texttt{b = a[}\textit{i:j}\texttt{]} \textemdash\ Select the slice of values from
170
\texttt{a} between
171
locations \textit{i} to \textit{j-1} saved to a new Numpy array \texttt{b} with length \textit{j-i}. \\
172
\texttt{sel = (a > const)} \textemdash\ Create a new boolean Numpy array
173
\texttt{sel}, of the same shape as \texttt{a},
174
that marks which values of \texttt{a > const}. Other operators (e.g. \textless, !=, \%) work as well.\\
175
\texttt{b = a[sel]} \textemdash\ Create a new Numpy array \texttt{b} made up of
176
elements from \texttt{a} that correspond to elements of \texttt{sel}
177
that are \textit{True}. In the above example \texttt{b} would be all elements of \texttt{a} that are greater than \texttt{const}.\\
178
\texttt{a.write\_hdf5(\textit{filename.h5})} \textemdash\ Save \texttt{a} to the hdf5 file \textit{filename.h5}.\\
179
180
\subsection{IPython Tips}
181
\settowidth{\MyLen}{\texttt{multicol} }
182
These tips work if IPython has been loaded, typically either by invoking
183
\texttt{yt load} on the command line.
184
\texttt{Tab complete} \textemdash\ IPython will attempt to auto-complete a
185
variable or function name when the \texttt{Tab} key is pressed, e.g. \textit{HaloFi}\textendash\texttt{Tab} would auto-complete
186
to \textit{HaloFinder}. This also works with imports, e.g. \textit{from numpy.random.}\textendash\texttt{Tab}
187
would give you a list of random functions (note the trailing period before hitting \texttt{Tab}).\\
188
\texttt{?, ??} \textemdash\ Appending one or two question marks at the end of any object gives you
189
detailed information about it, e.g. \textit{variable\_name}?.\\
190
Below a few IPython ``magics'' are listed, which are IPython-specific shortcut commands.\\
191
\texttt{\%paste} \textemdash\ Paste content from the system clipboard into the IPython shell.\\
192
\texttt{\%hist} \textemdash\ Print recent command history.\\
193
\texttt{\%quickref} \textemdash\ Print IPython quick reference.\\
194
\texttt{\%pdb} \textemdash\ Automatically enter the Python debugger at an exception.\\
195
\texttt{\%debug} \textemdash\ Drop into a debugger at the location of the last unhandled exception. \\
196
\texttt{\%time, \%timeit} \textemdash\ Find running time of expressions for benchmarking.\\
197
\texttt{\%lsmagic} \textemdash\ List all available IPython magics. Hint: \texttt{?} works with magics.\\
198
199
200
Please see \url{http://ipython.org/documentation.html} for the full
201
IPython documentation.
202
203
\subsection{Load and Access Data}
204
The first step in using yt is to reference a simulation snapshot.
205
After that, simulation data is generally accessed in yt using \textit{Data Containers} which are Python objects
206
that define a region of simulation space from which data should be selected.
207
\settowidth{\MyLen}{\texttt{multicol} }
208
\texttt{ds = yt.load(}\textit{dataset}\texttt{)} \textemdash\ Reference a single snapshot.\\
209
\texttt{dd = ds.all\_data()} \textemdash\ Select the entire volume.\\
210
\texttt{a = dd[}\textit{field\_name}\texttt{]} \textemdash\ Copies the contents of \textit{field} into the
211
YTArray \texttt{a}. Similarly for other data containers.\\
212
\texttt{ds.field\_list} \textemdash\ A list of available fields in the snapshot. \\
213
\texttt{ds.derived\_field\_list} \textemdash\ A list of available derived fields
214
in the snapshot. \\
215
\texttt{val, loc = ds.find\_max("Density")} \textemdash\ Find the \texttt{val}ue of
216
the maximum of the field \texttt{Density} and its \texttt{loc}ation. \\
217
\texttt{sp = ds.sphere(}\textit{cen}\texttt{,}\textit{radius}\texttt{)} \textemdash\ Create a spherical data
218
container. \textit{cen} may be a coordinate, or ``max'' which
219
centers on the max density point. \textit{radius} may be a float in
220
code units or a tuple of (\textit{length, unit}).\\
221
222
\texttt{re = ds.region(\textit{cen}, \textit{left edge}, \textit{right edge})} \textemdash\ Create a
223
rectilinear data container. \textit{cen} is required but not used.
224
\textit{left} and \textit{right edge} are coordinate values that define the region.
225
226
\texttt{di = ds.disk(\textit{cen}, \textit{normal}, \textit{radius}, \textit{height})} \textemdash\
227
Create a cylindrical data container centered at \textit{cen} along the
228
direction set by \textit{normal},with total length
229
2$\times$\textit{height} and with radius \textit{radius}. \\
230
231
\texttt{ds.save\_object(sp, \textit{``sp\_for\_later''})} \textemdash\ Save an object (\texttt{sp}) for later use.\\
232
\texttt{sp = ds.load\_object(\textit{``sp\_for\_later''})} \textemdash\ Recover a saved object.\\
233
234
235
\subsection{Defining New Fields}
236
\texttt{yt} expects on-disk fields, fields generated on-demand and in-memory.
237
Field can either be created before a dataset is loaded using \texttt{add\_field}:
238
\texttt{def \_metal\_mass(\textit{field},\textit{data})}\\
239
\texttt{\hspace{4 mm} return data["metallicity"]*data["cell\_mass"]}\\
240
\texttt{add\_field("metal\_mass", units='g', function=\_metal\_mass)}\\
241
Or added to an existing dataset using \texttt{ds.add\_field}:
242
\texttt{ds.add\_field("metal\_mass", units='g', function=\_metal\_mass)}\\
243
244
\subsection{Slices and Projections}
245
\settowidth{\MyLen}{\texttt{multicol} }
246
\texttt{slc = yt.SlicePlot(ds, \textit{axis or normal vector}, \textit{fields}, \textit{center=}, \textit{width=}, \textit{weight\_field=}, \textit{additional parameters})} \textemdash\ Make a slice plot
247
perpendicular to \textit{axis} (specified via 'x', 'y', or 'z') or a normal vector for an off-axis slice of \textit{fields} weighted by \textit{weight\_field} at (code-units) \textit{center} with
248
\textit{width} in code units or a (value, unit) tuple. Hint: try \textit{yt.SlicePlot?} in IPython to see additional parameters.\\
249
\texttt{slc.save(\textit{file\_prefix})} \textemdash\ Save the slice to a png with name prefix \textit{file\_prefix}.
250
\texttt{.save()} works similarly for the commands below.\\
251
252
\texttt{prj = yt.ProjectionPlot(ds, \textit{axis or normal vector}, \textit{fields}, \textit{additional params})} \textemdash\ Same as \texttt{yt.SlicePlot} but for projections.\\
253
254
\subsection{Plot Annotations}
255
\settowidth{\MyLen}{\texttt{multicol} }
256
Plot callbacks are functions itemized in a registry that is attached to every plot object. They can be accessed and then called like \texttt{ prj.annotate\_velocity(factor=16, normalize=False)}. Most callbacks also accept a \textit{plot\_args} dict that is fed to matplotlib annotator. \\
257
\texttt{velocity(\textit{factor=},\textit{scale=},\textit{scale\_units=}, \textit{normalize=})} \textemdash\ Uses field "x-velocity" to draw quivers\\
258
\texttt{magnetic\_field(\textit{factor=},\textit{scale=},\textit{scale\_units=}, \textit{normalize=})} \textemdash\ Uses field "Bx" to draw quivers\\
259
\texttt{quiver(\textit{field\_x},\textit{field\_y},\textit{factor=},\textit{scale=},\textit{scale\_units=}, \textit{normalize=})} \\
260
\texttt{contour(\textit{field=},\textit{levels=},\textit{factor=},\textit{clim=},\textit{take\_log=}, \textit{additional parameters})} \textemdash Plots a number of contours \textit{ncont} to interpolate \textit{field} optionally using \textit{take\_log}, upper and lower \textit{c}ontour\textit{lim}its and \textit{factor} number of points in the interpolation.\\
261
\texttt{grids(\textit{alpha=}, \textit{draw\_ids=}, \textit{periodic=}, \textit{min\_level=}, \textit{max\_level=})} \textemdash Add grid boundaries. \\
262
\texttt{streamlines(\textit{field\_x},\textit{field\_y},\textit{factor=},\textit{density=})}\\
263
\texttt{clumps(\textit{clumplist})} \textemdash\ Generate \textit{clumplist} using the clump finder and plot. \\
264
\texttt{arrow(\textit{pos}, \textit{code\_size})} Add an arrow at a \textit{pos}ition. \\
265
\texttt{point(\textit{pos}, \textit{text})} \textemdash\ Add text at a \textit{pos}ition. \\
266
\texttt{marker(\textit{pos}, \textit{marker=})} \textemdash\ Add a matplotlib-defined marker at a \textit{pos}ition. \\
267
\texttt{sphere(\textit{center}, \textit{radius}, \textit{text=})} \textemdash\ Draw a circle and append \textit{text}.\\
268
\texttt{hop\_circles(\textit{hop\_output}, \textit{max\_number=}, \textit{annotate=}, \textit{min\_size=}, \textit{max\_size=}, \textit{font\_size=}, \textit{print\_halo\_size=}, \textit{fixed\_radius=}, \textit{min\_mass=}, \textit{print\_halo\_mass=}, \textit{width=})} \textemdash\ Draw a halo, printing it's ID, mass, clipping halos depending on number of particles (\textit{size}) and optionally fixing the drawn circle radius to be constant for all halos.\\
269
\texttt{hop\_particles(\textit{hop\_output},\textit{max\_number=},\textit{p\_size=},\\
270
\textit{min\_size},\textit{alpha=})} \textemdash\ Draw particle positions for member halos with a certain number of pixels per particle.\\
271
\texttt{particles(\textit{width},\textit{p\_size=},\textit{col=}, \textit{marker=}, \textit{stride=}, \textit{ptype=}, \textit{stars\_only=}, \textit{dm\_only=}, \textit{minimum\_mass=}, \textit{alpha=})} \textemdash\ Draw particles of \textit{p\_size} pixels in a slab of \textit{width} with \textit{col}or using a matplotlib \textit{marker} plotting only every \textit{stride} number of particles.\\
272
\texttt{title(\textit{text})}\\
273
274
\subsection{The $\sim$/.yt/ Directory}
275
\settowidth{\MyLen}{\texttt{multicol} }
276
yt will automatically check for configuration files in a special directory (\texttt{\$HOME/.yt/}) in the user's home directory.
277
278
The \texttt{config} file \textemdash\ Settings that control runtime behavior. \\
279
The \texttt{my\_plugins.py} file \textemdash\ Add functions, derived fields, constants, or other commonly-used Python code to yt.
280
281
282
\subsection{Analysis Modules}
283
\settowidth{\MyLen}{\texttt{multicol}}
284
The import name for each module is listed at the end of each description (see \textbf{yt Imports}).
285
286
\texttt{Absorption Spectrum} \textemdash\ (\texttt{absorption\_spectrum}). \\
287
\texttt{Clump Finder} \textemdash\ Find clumps defined by density thresholds (\texttt{level\_sets}). \\
288
\texttt{Halo Finding} \textemdash\ Locate halos of dark matter particles (\texttt{halo\_finding}). \\
289
\texttt{Light Cone Generator} \textemdash\ Stitch datasets together to perform analysis over cosmological volumes. \\
290
\texttt{Light Ray Generator} \textemdash\ Analyze the path of light rays.\\
291
\texttt{Rockstar Halo Finding} \textemdash\ Locate halos of dark matter using the Rockstar halo finder (\texttt{halo\_finding.rockstar}). \\
292
\texttt{Star Particle Analysis} \textemdash\ Analyze star formation history and assemble spectra (\texttt{star\_analysis}). \\
293
\texttt{Sunrise Exporter} \textemdash\ Export data to the sunrise visualization format (\texttt{sunrise\_export}). \\
294
295
296
\subsection{Parallel Analysis}
297
\settowidth{\MyLen}{\texttt{multicol}}
298
Nearly all of yt is parallelized using
299
MPI\@. The \textit{mpi4py} package must be installed for parallelism in yt. To
300
install \textit{pip install mpi4py} on the command line usually works.
301
Execute python in parallel similar to this:\\
302
\textit{mpirun -n 12 python script.py}\\
303
The file \texttt{script.py} must call the \texttt{yt.enable\_parallelism()} to
304
turn on yt's parallelism. If this doesn't happen, all cores will execute the
305
same serial yt script. This command may differ for each system on which you use
306
yt; please consult the system documentation for details on how to run parallel
307
applications.
308
309
\texttt{parallel\_objects()} \textemdash\ A way to parallelize analysis over objects
310
(such as halos or clumps).\\
311
312
313
\subsection{Git}
314
\settowidth{\MyLen}{\texttt{multicol}}
315
Please see \url{https://git-scm.com/} for the latest Git documentation.
316
317
\texttt{git clone https://github.com/yt-project/yt} \textemdash\ Clone the yt
318
repository. \\
319
\texttt{git status} \textemdash\ Show status of working tree.\\
320
\texttt{git diff} \textemdash\ Show changed files in the working tree. \\
321
\texttt{git log} \textemdash\ Show a log of changes in reverse chronological
322
order.\\
323
\texttt{git revert <commit>} \textemdash\ Revert the changes in an existing
324
commit and create a new commit with reverted changes. \\
325
\texttt{git add <pathspec>} \textemdash\ Stage changes in the working tree to
326
the index. \\
327
\texttt{git commit} \textemdash\ Commit staged changes to the repository. \\
328
\texttt{git merge <branch>} Merge the revisions from the specified branch on
329
top of the current branch.\\
330
\texttt{git push <remote>} \textemdash\ Push changes to remote repository. \\
331
\texttt{git push <remote> <branch>} \textemdash\ Push changes in specified
332
branch to remote repository. \\
333
\texttt{git pull <remote> <branch>} \textemdash\ Pull changes from the
334
specified branch of the remote repository. This is equivalent to \texttt{git
335
fetch <remote>} and then \texttt{git merge <remote>/<branch>}.\\
336
337
\subsection{FAQ}
338
\settowidth{\MyLen}{\texttt{multicol}}
339
340
\texttt{slc.set\_log('field', False)} \textemdash\ When plotting \texttt{field}, use linear scaling instead of log scaling.
341
342
343
%\rule{0.3\linewidth}{0.25pt}
344
%\scriptsize
345
346
% Can put some final stuff here like copyright etc...
347
348
\end{multicols}
349
350
\end{document}
351
352