This example is a bit contrived, but it demonstrates the basic features of a Trieste Collection in a simple way. We will load a color image, break it into three channels (R, G, B), and store each channel in a 2-D Trieste Array. The three Arrays will then be stored in a Collection, which will be saved to disk and then read from disk. Finally, the contents of the file will be examined.

In [1]:
%pylab notebook

Populating the interactive namespace from numpy and matplotlib

In [2]:
import trieste as tr


In [3]:
image = plt.imread("IMG_2054.jpg")


This is what the image looks like:

In [4]:
plt.imshow(image)

Out[4]:
<matplotlib.image.AxesImage at 0x7f036a671630>
In [5]:
plt.close()


The image object is a 3-dimensional NumPy array. We can decompose it into red, green and blue channels:

In [6]:
red = image[:,:,0]

green = image[:,:,1]

blue = image[:,:,2]


Now, we can store each channel in a Trieste Array:

In [7]:
R = tr.Array(red)

G = tr.Array(green)

B = tr.Array(blue)


These three 2-dimensional Array objects can be stored together in a Collection. Let's add some metadata---a caption:

In [8]:
meta = {'caption': 'This is a caption for the photo taken in 2010 at the Monterey Bay Aquarium.'}

In [9]:
jelly = tr.Collection([R,G,B], name='jellyfish', metadata=meta)


You can add a documentation string / README page to the collection to explain what it is:

In [10]:
read_me = """
This is potentially a long document describing the content of this Collection.
"""

In [11]:
jelly.set_readme(read_me)


Save the collection as a file:

In [12]:
jelly.save_as('jellies')


Now, we can load the file:

In [13]:
J = tr.load('jellies.npz')

In [14]:
J.name

Out[14]:
'jellyfish'

Since the file only contains one top-level object (a Collection), the load() function creates a Collection object, rather than a File object:

In [15]:
J

Out[15]:
<jellyfish: a Collection of 3 2-D Arrays>
In [16]:
type(J)

Out[16]:
trieste.core.Collection
In [17]:
print(J.readme)

This is potentially a long document describing the content of this Collection.



The table of contents for the Collection:

In [18]:
J.toc

Out[18]:
['red', 'green', 'blue']

A Collection can be indexed using the name of an object that it contains, as in:

In [19]:
J['red']

Out[19]:
<red: a Trieste 2-D Array>
In [20]:
plt.imshow(J['blue'].data, cmap='hot')