A couple of months ago I’ve decided that Py-SPHviewer is mature enough to start including some tools, which are essentially independent of the core of code. So, I’ve decided to create a new directory called “tools”, and started filling it.
I have recently added the QuickView tool, which can be regarded as a first step towards an API for sphviewer.
QuickView essentially simplifies the process of creating an image. In addition, I have committed a file with the coordinates of the dark matter particles that belong to a dark matter halo, extracted from a bigger cosmological simulation. This file will allow users to make concrete tests relying on a unique data set. I use this in the following example.
Looking at a dark matter halo with QuickView
In this example, I will create an image of a dark matter halo extracted from a cosmological simulation. If you want to reproduce the example, please download next file. From python, you can use:
import urllib example_file = &quot;https://github.com/alejandrobll/py-sphviewer/raw/master/examples/dm_halo.h5py&quot; urllib.urlretrieve(example_file, &quot;dm_halo.h5py&quot;)
Now, you can read the coordinates of the particles using the h5py library. Coordinates are given in Mpc/h, and, as I said, they correspond to the position of dark matter particles, at redfhift z=0, that trace the density field of a dark matter halo within a cosmological simulation.
import h5py halo = h5py.File('dm_halo.h5py', 'r') pos = halo['Coordinates'].value
When doing a regular scatter plot of the x-y projection of the particles we get an image like this:
Now, let us visualise the same halo but using QuickView. It is done by the following lines:
from sphviewer.tools import QuickView qv = QuickView(pos.T, r='infinity', plot=False) qv.imshow()
If you followed the py-sphviewer tutorial, you probably know what r=’infinity’ does mean. By default, QuickView retrieves the active axis, and shows the image on it. So, I used plot=False to avoid doing this. Every QuickView object has an associated method to show the final image. It is called imshow, and it uses the matplotlib.pyplot.imshow method. It does accept the same *kwargs as imshow. For example, I can change the colormap and maximum value used to represent the projected density of the dark matter particles with:
You can also make the plot by using the QuickView’s methods for getting the image and the extent:
img = qv.get_image() extent = qv.get_extent() fig = plt.figure(1, figsize=(7,7)) plt.imshow(img, extent=extent, cmap='gist_stern')
Finally, I would like to show how to make a simple movie using this new QuickView tool. Keep in mind, however, that this is not the most efficient way to use py-sphviewer. Follow the tutorial and you will realise how to do it efficiently. Having this in mind, let’s do it:
extent = [-0.1,0.1,-0.1,0.1] for i in xrange(360): qv = QuickView(pos.T, r='infinity', plot=False, extent=extent, nb=4, p=i) qv.imsave('image_'+str('%03d.png'% i), cmap='bone', vmin=0, vmax=2)