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

Camera.view()   B

Complexity

Conditions 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
1
from niprov.dependencies import Dependencies
2
import webbrowser
3
4
5
class Camera(object):
6
    """
7
        to (:class:`.PictureCache`): Service that provides a file-like 
8
                handle to save the plotted picture to.
9
    """
10
11
    def __init__(self, dependencies):
12
        pass
13
14
    def saveSnapshot(self, data):
15
        """Plot an overview of the image and store it.
16
17
        Args:
18
            data (numpy.ndarray): Array of 2, 3 or 4 dimensions with image data.
19
        """
20
        pass
21
22
    def view(img, dependencies=Dependencies()):
23
        libs = dependencies.getLibraries()
24
        plt = libs.pyplot
25
        nibabel = libs.nibabel
26
        data = nibabel.load(img.path).get_data()
27
28
        ## 3D
29
        ndims = len(data.shape)
30
        sliceOrder = [1, 0, 2]
31
32
        fig, axs = plt.subplots(nrows=1, ncols=ndims)
33
34
        for d in range(ndims):
35
            slicing = [slice(None)]*ndims
36
            slicing[sliceOrder[d]] = int(data.shape[d]/2)
37
            axs[d].matshow(data[slicing].T, origin='lower', 
38
                cmap = plt.get_cmap('gray'), vmin = 0, vmax = data.max())
39
            axs[d].locator_params(nbins=3)
40
            axs[d].tick_params(axis='both', which='major', labelsize=8)
41
42
        plt.tight_layout()
43
44
        fname = 'myplot.png'
45
        plt.savefig(fname)
46
        webbrowser.open(fname)
47