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

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