Using QuickView in Py-SPHViewer 1.0.4

A couple of months ago I decided that Py-SPHviewer is mature enough to start including some tools, which are essentially independent on the core of the code. So, I created a new directory called “tools”, and started to fill it.

One tool that I added recently is QuickView, which can be regarded as a first step towards an API for sphviewer.

QuickView simplifies the process of creating an image. In addition, I committed a file with the coordinates of the dark matter particles that belong to a dark matter halo, extracted from a bigger cosmological simulation, which will allow users to test the code. I use this file 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 wish to reproduce the example, please download the example file. If you are in a Python interpreter, just type:

 import urllib
example_file = "https://github.com/alejandrobll/py-sphviewer/raw/master/examples/dm_halo.h5py"
urllib.urlretrieve(example_file, "dm_halo.h5py") 

Now you may read the coordinates of the particles using the h5py library. Coordinates are given in Mpc/h, and 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 

A regular scatter plot of the x-y projection of the particles looks like this:

download

In order to visualise the same halo but using QuickView, we can use the following code:

 from sphviewer.tools import QuickView
qv = QuickView(pos.T, r='infinity', plot=False)
qv.imshow()

download (1)

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:

 qv.imshow(cmap="gist_stern", vmax=2)

download (2)

 

You can also retrieve the image itself, and plot it afterwards. This is done by using 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')

 

download (3)

Finally, a very simple movie can now be done in a few lines. 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, the relevant lines are::

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)

 

 

Advertisements

One Comment Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s