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

NiftiFile   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 10
wmc 3

3 Methods

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