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

NiftiFile.inspect()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
1
from datetime import datetime
2
from niprov.basefile import BaseFile
3
from niprov.libraries import Libraries
4
5
6
class NiftiFile(BaseFile):
7
8
    def __init__(self, location, **kwargs):
9
        super(NiftiFile, self).__init__(location, **kwargs)
10
        self.libs = self.dependencies.getLibraries()
11
        self.polaroid = self.dependencies.getPolaroid()
12
        self.pictureCache = self.dependencies.getPictureCache()
13
14
    def attach(self, form='json'):
15
        """
16
        Attach the current provenance to the file by injecting it as a 
17
        json-encoded extension to the nifti header.
18
19
        Args:
20
            form (str): Data format in which to serialize provenance. Defaults 
21
                to 'json'.
22
        """
23
        img = self.libs.nibabel.load(self.path)
24
        provstr = self.getProvenance(form)
25
        ext = self.libs.nibabel.nifti1.Nifti1Extension('comment', provstr)
26
        hdr = img.get_header()
27
        hdr.extensions.append(ext)
28
        img.to_filename(self.path)
29
30
    def inspect(self):
31
        provenance = super(NiftiFile, self).inspect()
32
        img = self.libs.nibabel.load(self.path)
33
        self.polaroid.saveSnapshot(img.get_data(), to=self.pictureCache)
34
        return provenance
35