Completed
Pull Request — master (#136)
by Jasper
01:25
created

Camera   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 46
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
B takeSnapshot() 0 26 2
A saveSnapshot() 0 13 1
1
from niprov.dependencies import Dependencies
2
import webbrowser
3
4
5
class Camera(object):
6
7
    def __init__(self, dependencies):
8
        self.film = dependencies.getPictureCache()
9
        self.libs = dependencies.getLibraries()
10
11
    def saveSnapshot(self, data):
12
        """Plot an overview of the image and store it.
13
14
        Uses :class:`.PictureCache` as service that provides a file-like 
15
                handle to save the plotted picture to.
16
        Calls takeSnapshot() to do the actual plotting.
17
18
        Args:
19
            data (numpy.ndarray): Array of 2, 3 or 4 dimensions with image data.
20
        """
21
        newPicture = self.film.new()
22
        self.takeSnapshot(data, on=newPicture)
23
        self.film.keep(newPicture)
24
25
    def takeSnapshot(data, on='shouldnotbehere.png'):
26
        """Plot an overview of the image using matplotlib.pyplot.
27
28
        Args:
29
            data (numpy.ndarray): Array of 2, 3 or 4 dimensions with image data.
30
            on (str or file-like object): Where to save figure to.
31
        """
32
        plt = self.libs.pyplot
33
34
        ## 3D
35
        ndims = len(data.shape)
36
        sliceOrder = [1, 0, 2]
37
38
        fig, axs = plt.subplots(nrows=1, ncols=ndims)
39
40
        for d in range(ndims):
41
            slicing = [slice(None)]*ndims
42
            slicing[sliceOrder[d]] = int(data.shape[d]/2)
43
            axs[d].matshow(data[slicing].T, origin='lower', 
44
                cmap = plt.get_cmap('gray'), vmin = 0, vmax = data.max())
45
            axs[d].locator_params(nbins=3)
46
            axs[d].tick_params(axis='both', which='major', labelsize=8)
47
48
        plt.tight_layout()
49
50
        plt.savefig(on)
51
52