Completed
Push — master ( 0d56a8...9e1177 )
by Jasper
10s
created

ParrecTests.test_getProtocolFields()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 4
rs 10
1
import unittest
2
from mock import Mock
3
from datetime import datetime, timedelta
4
from tests.test_basefile import BaseFileTests
5
6
7
class ParrecTests(BaseFileTests):
8
9
    def setUp(self):
10
        super(ParrecTests, self).setUp()
11
        self.libs = Mock()
12
        self.dependencies.getLibraries.return_value = self.libs
13
        self.setupNibabel()
14
        from niprov.parrec import ParrecFile
15
        self.constructor = ParrecFile
16
        self.file = ParrecFile(self.path, dependencies=self.dependencies)
17
18
    def test_Gets_basic_info_from_nibabel_and_returns_it(self):
19
        out = self.file.inspect()
20
        self.assertEqual(out['subject'], 'John Doeish')
21
        self.assertEqual(out['protocol'], 'T1 SENSE')
22
        self.assertEqual(out['acquired'], datetime(2014, 8, 5, 11, 27, 34))
23
24
    def test_Gets_dimensions(self):
25
        out = self.file.inspect()
26
        self.assertEqual(out['dimensions'], [80,80,10])
27
28
    def test_Gets_advanced_fields(self):
29
        out = self.file.inspect()
30
        self.assertEqual(out['technique'], 'T1TFE')
31
        self.assertEqual(out['repetition-time'], 4.364)
32
        self.assertEqual(out['field-of-view'], [130., 100., 154.375])
33
        self.assertEqual(out['epi-factor'], 1)
34
        self.assertEqual(out['magnetization-transfer-contrast'], False)
35
        self.assertEqual(out['diffusion'], False)
36
        self.assertEqual(out['duration'], timedelta(seconds=65))
37
        self.assertEqual(out['subject-position'], 'Head First Supine')
38
        self.assertEqual(out['water-fat-shift'], 1.117)
39
        # per-image
40
        self.assertEqual(out['slice-thickness'], 10.0)
41
        self.assertEqual(out['slice-orientation'], 1)
42
        self.assertEqual(out['echo-time'], 2.0800000000000001)
43
        self.assertEqual(out['flip-angle'], 8.0)
44
        self.assertEqual(out['inversion-time'], 0.0)
45
46
    def test_getProtocolFields(self):
47
        protocol = self.file.getProtocolFields()
48
        self.assertIn('repetition-time', protocol)
49
        self.assertIn('echo-time', protocol)
50
51
    def setupNibabel(self):
52
        import numpy
53
        img = Mock()
54
        img.header.general_info = {
55
             'acq_nr': 6,
56
             'angulation': numpy.array([-1.979,  0.546,  0.019]),
57
             'diffusion': 0,
58
             'diffusion_echo_time': 0.0,
59
             'dyn_scan': 0,
60
             'epi_factor': 1,
61
             'exam_date': '2014.08.05 / 11:27:34',
62
             'exam_name': 'test',
63
             'flow_compensation': 0,
64
             'fov': numpy.array([ 130.   ,  100.   ,  154.375]),
65
             'max_cardiac_phases': 1,
66
             'max_diffusion_values': 1,
67
             'max_dynamics': 1,
68
             'max_echoes': 1,
69
             'max_gradient_orient': 1,
70
             'max_mixes': 1,
71
             'max_slices': 10,
72
             'mtc': 0,
73
             'nr_label_types': 0,
74
             'off_center': numpy.array([-18.805,  22.157, -17.977]),
75
             'patient_name': 'John Doeish',
76
             'patient_position': 'Head First Supine',
77
             'phase_enc_velocity': numpy.array([ 0.,  0.,  0.]),
78
             'prep_direction': 'Right-Left',
79
             'presaturation': 0,
80
             'protocol_name': 'T1 SENSE',
81
             'recon_nr': 1,
82
             'repetition_time': 4.364,
83
             'scan_duration': 65.0,
84
             'scan_mode': '3D',
85
             'scan_resolution': numpy.array([76, 62]),
86
             'series_type': 'Image   MRSERIES',
87
             'spir': 0,
88
             'tech': 'T1TFE',
89
             'water_fat_shift': 1.117}
90
        img.header.image_defs = numpy.array([ (1, 1, 1, 1, 0, 2, 0, 16, 81, [80, 80], 0.0, 1.26032, 2.84925e-05, 133, 231, [-1.98, 0.55, 0.02], [-18.79, -22.82, -16.42], 10.0, 0.0, 0, 1, 0, 2, [1.912, 1.912], 2.08, 0.0, 0.0, 0.0, 1, 8.0, 0, 0, 0, 7, 0.0, 1, 1, '7', '0', [0.0, 0.0, 0.0], 1),
91
       (2, 1, 1, 1, 0, 2, 1, 16, 81, [80, 80], 0.0, 1.26032, 2.84925e-05, 294, 512, [-1.98, 0.55, 0.02], [-18.79, -12.82, -16.77], 10.0, 0.0, 0, 1, 0, 2, [1.912, 1.912], 2.08, 0.0, 0.0, 0.0, 1, 8.0, 0, 0, 0, 7, 0.0, 1, 1, '7', '0', [0.0, 0.0, 0.0], 1),
92
       (3, 1, 1, 1, 0, 2, 2, 16, 81, [80, 80], 0.0, 1.26032, 2.84925e-05, 427, 742, [-1.98, 0.55, 0.02], [-18.8, -2.83, -17.11], 10.0, 0.0, 0, 1, 0, 2, [1.912, 1.912], 2.08, 0.0, 0.0, 0.0, 1, 8.0, 0, 0, 0, 7, 0.0, 1, 1, '7', '0', [0.0, 0.0, 0.0], 1)], 
93
      dtype=[('slice number', '<i8'), ('echo number', '<i8'), ('dynamic scan number', '<i8'), ('cardiac phase number', '<i8'), ('image_type_mr', '<i8'), ('scanning sequence', '<i8'), ('index in REC file', '<i8'), ('image pixel size', '<i8'), ('scan percentage', '<i8'), ('recon resolution', '<i8', (2,)), ('rescale intercept', '<f8'), ('rescale slope', '<f8'), ('scale slope', '<f8'), ('window center', '<i8'), ('window width', '<i8'), ('image angulation', '<f8', (3,)), ('image offcentre', '<f8', (3,)), ('slice thickness', '<f8'), ('slice gap', '<f8'), ('image_display_orientation', '<i8'), ('slice orientation', '<i8'), ('fmri_status_indication', '<i8'), ('image_type_ed_es', '<i8'), ('pixel spacing', '<f8', (2,)), ('echo_time', '<f8'), ('dyn_scan_begin_time', '<f8'), ('trigger_time', '<f8'), ('diffusion_b_factor', '<f8'), ('number of averages', '<i8'), ('image_flip_angle', '<f8'), ('cardiac frequency', '<i8'), ('minimum RR-interval', '<i8'), ('maximum RR-interval', '<i8'), ('TURBO factor', '<i8'), ('Inversion delay', '<f8'), ('diffusion b value number', '<i8'), ('gradient orientation number', '<i8'), ('contrast type', 'S30'), ('diffusion anisotropy type', 'S30'), ('diffusion', '<f8', (3,)), ('label type', '<i8')])
94
        img.shape = (80,80,10)
95
        self.libs.nibabel.load.return_value = img
96
        self.libs.hasDependency.return_value = True
97
98
99
100
101