| 1 |  |  | import torchio as tio | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from ...utils import TorchioTestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class TestProjection(TorchioTestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     """Tests for :class:`tio.Projection` class.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     def test_default_projection(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |         transform = tio.Projection('S') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         transformed = transform(self.sample_subject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         axis_index = self.sample_subject.t1.axis_name_to_index('S') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         self.assertEqual(transformed.t1.shape[axis_index], 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     def test_invalid_axis_name(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         with self.assertRaises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             transform = tio.Projection('123') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             transform(self.sample_subject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     def test_projection_type_invalid_input(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         with self.assertRaises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             tio.Projection('S', projection_type='bad') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     def test_q_is_required(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         with self.assertRaises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             tio.Projection('S', projection_type='quantile') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     def test_q_is_invalid(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         with self.assertRaises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             tio.Projection('S', projection_type='quantile', q=2) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     def test_full_slabs_only(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         sub = tio.datasets.Colin27() | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         transform1 = tio.Projection('S', slab_thickness=100, stride=100) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         transform2 = tio.Projection( | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             'S', slab_thickness=100, stride=100, full_slabs_only=False) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         transformed1 = transform1(sub) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         transformed2 = transform2(sub) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         axis_index = sub.t1.axis_name_to_index('S') | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         self.assertEqual(transformed1.t1.shape[axis_index], 1) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         self.assertEqual(transformed2.t1.shape[axis_index], 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     def test_maximum_intensity_projection(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         transform = tio.Projection('S', projection_type='max') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         transform(self.sample_subject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     def test_minimum_intensity_projection(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         transform = tio.Projection('S', projection_type='min') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         transform(self.sample_subject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     def test_mean_intensity_projection(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         transform = tio.Projection('S', projection_type='mean') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         transform(self.sample_subject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     def test_median_intensity_projection(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         transform = tio.Projection('S', projection_type='median') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         transform(self.sample_subject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     def test_quantile_intensity_projection(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         transform = tio.Projection('S', projection_type='quantile', q=0.75) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |         transform(self.sample_subject) | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  |