Test Failed
Pull Request — master (#865)
by
unknown
03:45
created

savu.plugins.loaders.base_tomophantom_loader   F

Complexity

Total Complexity 63

Size/Duplication

Total Lines 385
Duplicated Lines 25.19 %

Importance

Changes 0
Metric Value
eloc 269
dl 97
loc 385
rs 3.36
c 0
b 0
f 0
wmc 63

18 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseTomophantomLoader.__init__() 0 2 1
A BaseTomophantomLoader.setup() 0 40 1
B BaseTomophantomLoader.__output_data() 19 19 6
A BaseTomophantomLoader._link_datafile_to_nexus_file() 11 11 2
A BaseTomophantomLoader._output_metadata_dict() 0 10 4
C BaseTomophantomLoader.__get_backing_file() 0 77 10
A BaseTomophantomLoader._get_n_entries() 0 2 1
A BaseTomophantomLoader.__create_dataset() 0 5 2
B BaseTomophantomLoader._populate_nexus_file() 0 32 5
A BaseTomophantomLoader.__add_nxs_data() 18 18 5
A BaseTomophantomLoader.__get_start_slice_list() 15 15 5
B BaseTomophantomLoader.__output_axis_labels() 24 24 6
A BaseTomophantomLoader.__add_nxs_entry() 0 9 3
A BaseTomophantomLoader.__convert_patterns() 0 15 3
A BaseTomophantomLoader.__parameter_checks() 0 4 2
A BaseTomophantomLoader._set_metadata() 0 8 2
A BaseTomophantomLoader._link_nexus_file() 0 21 3
A BaseTomophantomLoader.__output_data_patterns() 10 10 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like savu.plugins.loaders.base_tomophantom_loader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# Copyright 2014 Diamond Light Source Ltd.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
15
"""
16
.. module:: base_tomophantom_loader
17
   :platform: Unix
18
   :synopsis: A loader that generates synthetic 3D projection full-field tomo data\
19
        as hdf5 dataset of any size.
20
21
.. moduleauthor:: Daniil Kazantsev <[email protected]>
22
"""
23
24
import os
25
import h5py
26
import logging
27
import numpy as np
28
29
from savu.data.chunking import Chunking
30
from savu.plugins.utils import register_plugin
31
from savu.plugins.loaders.base_loader import BaseLoader
32
from savu.plugins.savers.utils.hdf5_utils import Hdf5Utils
33
34
from savu.data.meta_data import MetaData
35
from savu.core.transports.base_transport import BaseTransport
36
37
import tomophantom
38
from tomophantom import TomoP2D, TomoP3D
39
40
@register_plugin
41
class BaseTomophantomLoader(BaseLoader):
42
    def __init__(self, name='BaseTomophantomLoader'):
43
        super(BaseTomophantomLoader, self).__init__(name)
44
45
    def setup(self):
46
        exp = self.exp
47
        data_obj = exp.create_data_object('in_data', 'synth_proj_data')
48
49
50
        data_obj.set_axis_labels(*self.parameters['axis_labels'])
51
        self.__convert_patterns(data_obj,'synth_proj_data')
52
        self.__parameter_checks(data_obj)
53
54
        self.tomo_model = self.parameters['tomo_model']
55
        # setting angles for parallel beam geometry
56
        self.angles = np.linspace(0.0,180.0-(1e-14), self.parameters['proj_data_dims'][0], dtype='float32')
57
        path = os.path.dirname(tomophantom.__file__)
58
        self.path_library3D = os.path.join(path, "Phantom3DLibrary.dat")
59
60
61
        data_obj.backing_file = self.__get_backing_file(data_obj, 'synth_proj_data')
62
        data_obj.data = data_obj.backing_file['/']['entry1']['tomo_entry']['data']['data']
63
        #data_obj.data.dtype # Need to do something to .data to keep the file open!
64
65
        # create a phantom file
66
        data_obj2 = exp.create_data_object('in_data', 'phantom')
67
        data_obj2.set_axis_labels(*['voxel_x.voxel', 'voxel_y.voxel', 'voxel_z.voxel'])
68
        self.__convert_patterns(data_obj2, 'phantom')
69
        self.__parameter_checks(data_obj2)
70
71
        data_obj2.backing_file = self.__get_backing_file(data_obj2, 'phantom')
72
        data_obj2.data = data_obj2.backing_file['/']['phantom']['data']
73
        #data_obj2.data.dtype # Need to do something to .data to keep the file open!
74
        data_obj.set_shape(data_obj.data.shape)
75
        group_name = '1-TomoPhantomLoader-phantom'
76
77
        self.n_entries = data_obj.get_shape()[0]
78
        cor_val=0.5*(self.parameters['proj_data_dims'][2])
79
        self.cor=np.linspace(cor_val, cor_val, self.parameters['proj_data_dims'][1], dtype='float32')
80
        self._set_metadata(data_obj, self._get_n_entries())
81
82
        self._link_nexus_file(data_obj, 'synth_proj_data')
83
        self._link_nexus_file(data_obj2, 'phantom')
84
        return data_obj, data_obj2
85
86
    def __get_backing_file(self, data_obj, file_name):
87
        fname = '%s/%s.h5' % \
88
            (self.exp.get('out_path'), file_name)
89
90
        if os.path.exists(fname):
91
            return h5py.File(fname, 'r')
92
93
        self.hdf5 = Hdf5Utils(self.exp)
94
95
        dims_temp = self.parameters['proj_data_dims'].copy()
96
        proj_data_dims = tuple(dims_temp)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tuple does not seem to be defined.
Loading history...
97
        if (file_name == 'phantom'):
98
            dims_temp[0]=dims_temp[1]
99
            dims_temp[2]=dims_temp[1]
100
            proj_data_dims = tuple(dims_temp)
101
102
        patterns = data_obj.get_data_patterns()
103
        p_name = list(patterns.keys())[0]
104
        p_dict = patterns[p_name]
105
        p_dict['max_frames_transfer'] = 1
106
        nnext = {p_name: p_dict}
107
108
        pattern_idx = {'current': nnext, 'next': nnext}
109
        chunking = Chunking(self.exp, pattern_idx)
110
        chunks = chunking._calculate_chunking(proj_data_dims, np.int16)
111
112
        h5file = self.hdf5._open_backing_h5(fname, 'w')
113
114
        if file_name == 'phantom':
115
            group = h5file.create_group('/phantom', track_order=None)
116
        else:
117
            group = h5file.create_group('/entry1/tomo_entry/data', track_order=None)
118
119
        dset = self.hdf5.create_dataset_nofill(group, "data", proj_data_dims, data_obj.dtype, chunks = chunks)
120
121
        self.exp._barrier()
122
123
124
        slice_dirs = list(nnext.values())[0]['slice_dims']
125
        nDims = len(dset.shape)
126
        total_frames = np.prod([dset.shape[i] for i in slice_dirs])
127
        sub_size = \
128
            [1 if i in slice_dirs else dset.shape[i] for i in range(nDims)]
129
130
        # need an mpi barrier after creating the file before populating it
131
        idx = 0
132
        sl, total_frames = \
133
            self.__get_start_slice_list(slice_dirs, dset.shape, total_frames)
134
        # calculate the first slice
135
        for i in range(total_frames):
136
            if (file_name == 'synth_proj_data'):
137
                #generate projection data
138
                gen_data = TomoP3D.ModelSinoSub(self.tomo_model, proj_data_dims[1], proj_data_dims[2], proj_data_dims[1], (i, i+1), -self.angles, self.path_library3D)
139
            else:
140
                #generate phantom data
141
                gen_data = TomoP3D.ModelSub(self.tomo_model, proj_data_dims[1], (i, i+1), self.path_library3D)
142
            dset[tuple(sl)] = np.swapaxes(gen_data,0,1)
143
            if sl[slice_dirs[idx]].stop == dset.shape[slice_dirs[idx]]:
144
                idx += 1
145
                if idx == len(slice_dirs):
146
                    break
147
            tmp = sl[slice_dirs[idx]]
148
            sl[slice_dirs[idx]] = slice(tmp.start+1, tmp.stop+1)
149
150
        self.exp._barrier()
151
152
153
154
        try:
155
            #nxsfile = NXdata(h5file)
156
            #nxsfile.save(file_name + ".nxs")
157
158
            h5file.close()
159
        except IOError as exc:
160
            logging.debug('There was a problem trying to close the file in random_hdf5_loader')
161
162
        return self.hdf5._open_backing_h5(fname, 'r')
163
164 View Code Duplication
    def __get_start_slice_list(self, slice_dirs, shape, n_frames):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
165
        n_processes = len(self.exp.get('processes'))
166
        rank = self.exp.get('process')
167
        frames = np.array_split(np.arange(n_frames), n_processes)[rank]
168
        f_range = list(range(0, frames[0])) if len(frames) else []
169
        sl = [slice(0, 1) if i in slice_dirs else slice(None)
170
              for i in range(len(shape))]
171
        idx = 0
172
        for i in f_range:
173
            if sl[slice_dirs[idx]] == shape[slice_dirs[idx]]-1:
174
                idx += 1
175
            tmp = sl[slice_dirs[idx]]
176
            sl[slice_dirs[idx]] = slice(tmp.start+1, tmp.stop+1)
177
178
        return sl, len(frames)
179
180
    def __convert_patterns(self, data_obj, object_type):
181
        if (object_type == 'synth_proj_data'):
182
            pattern_list = self.parameters['patterns']
183
        else:
184
            pattern_list = self.parameters['patterns_tomo']
185
        for p in pattern_list:
186
            p_split = p.split('.')
187
            name = p_split[0]
188
            dims = p_split[1:]
189
            core_dims = tuple([int(i[0]) for i in [d.split('c') for d in dims]
190
                              if len(i) == 2])
191
            slice_dims = tuple([int(i[0]) for i in [d.split('s') for d in dims]
192
                               if len(i) == 2])
193
            data_obj.add_pattern(
194
                    name, core_dims=core_dims, slice_dims=slice_dims)
195
196
197
198
    def _set_metadata(self, data_obj, n_entries):
199
        n_angles = len(self.angles)
200
        data_angles = n_entries
201
        if data_angles != n_angles:
202
            raise Exception("The number of angles %s does not match the data "
203
                            "dimension length %s", n_angles, data_angles)
204
        data_obj.meta_data.set(['rotation_angle'], self.angles)
205
        data_obj.meta_data.set(['centre_of_rotation'], self.cor)
206
207
    def __parameter_checks(self, data_obj):
208
        if not self.parameters['proj_data_dims']:
209
            raise Exception(
210
                    'Please specifiy the dimensions of the dataset to create.')
211
212
    def _get_n_entries(self):
213
        return self.n_entries
214
215
    def _link_nexus_file(self, data_obj, name):
216
        filename = self.exp.meta_data.get('nxs_filename')
217
        fsplit = filename.split('/')
218
        plugin_number = len(self.exp.meta_data.plugin_list.plugin_list)
219
        if plugin_number == 1:
220
            fsplit[-1] = 'synthetic_data.nxs'
221
        else:
222
            fsplit[-1] = 'synthetic_data_processed.nxs'
223
        filename = '/'.join(fsplit)
224
        self.exp.meta_data.set('nxs_filename', filename)
225
        if name == 'phantom':
226
            data_obj.exp.meta_data.set(['group_name', 'phantom'], 'phantom')
227
            data_obj.exp.meta_data.set(['link_type', 'phantom'], 'final_result')
228
            data_obj.meta_data.set(["meta_data", "PLACEHOLDER", "VOLUME_XZ"], [10])
229
230
        else:
231
            data_obj.exp.meta_data.set(['group_name', 'synth_proj_data'], 'entry1/tomo_entry/data')
232
            data_obj.exp.meta_data.set(['link_type', 'synth_proj_data'], 'entry1')
233
234
        self._populate_nexus_file(data_obj)
235
        self._link_datafile_to_nexus_file(data_obj)
236
237
238
    def _populate_nexus_file(self, data):
239
        filename = self.exp.meta_data.get('nxs_filename')
240
        name = data.data_info.get('name')
241
        with h5py.File(filename, 'a') as nxs_file:
242
243
            group_name = self.exp.meta_data.get(['group_name', name])
244
            link_type = self.exp.meta_data.get(['link_type', name])
245
246
            if name == 'phantom':
247
                nxs_entry = nxs_file.create_group('entry')
248
                if link_type == 'final_result':
249
                    group_name = 'final_result_' + data.get_name()
250
                else:
251
                    link = nxs_entry.require_group(link_type.encode("ascii"))
252
                    link.attrs['NX_class'] = 'NXcollection'
253
                    nxs_entry = link
254
255
                # delete the group if it already exists
256
                if group_name in nxs_entry:
257
                    del nxs_entry[group_name]
258
259
                plugin_entry = nxs_entry.require_group(group_name)
260
261
            else:
262
                plugin_entry = nxs_file.create_group(f'/{group_name}')
263
264
            self.__output_data_patterns(data, plugin_entry)
265
            self._output_metadata_dict(plugin_entry, data.meta_data.get_dictionary())
266
            self.__output_axis_labels(data, plugin_entry)
267
268
269
            plugin_entry.attrs['NX_class'] = 'NXdata'
270
271
272 View Code Duplication
    def __output_axis_labels(self, data, entry):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
273
        axis_labels = data.data_info.get("axis_labels")
274
        ddict = data.meta_data.get_dictionary()
275
276
        axes = []
277
        count = 0
278
        for labels in axis_labels:
279
            name = list(labels.keys())[0]
280
            axes.append(name)
281
            entry.attrs[name + '_indices'] = count
282
283
            mData = ddict[name] if name in list(ddict.keys()) \
284
                else np.arange(self.parameters['proj_data_dims'][count])
285
            if isinstance(mData, list):
286
                mData = np.array(mData)
287
288
            if 'U' in str(mData.dtype):
289
                mData = mData.astype(np.string_)
290
            if name not in list(entry.keys()):
291
                axis_entry = entry.require_dataset(name, mData.shape, mData.dtype)
292
                axis_entry[...] = mData[...]
293
                axis_entry.attrs['units'] = list(labels.values())[0]
294
            count += 1
295
        entry.attrs['axes'] = axes
296
297 View Code Duplication
    def __output_data_patterns(self, data, entry):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
298
        data_patterns = data.data_info.get("data_patterns")
299
        entry = entry.require_group('patterns')
300
        entry.attrs['NX_class'] = 'NXcollection'
301
        for pattern in data_patterns:
302
            nx_data = entry.require_group(pattern)
303
            nx_data.attrs['NX_class'] = 'NXparameters'
304
            values = data_patterns[pattern]
305
            self.__output_data(nx_data, values['core_dims'], 'core_dims')
306
            self.__output_data(nx_data, values['slice_dims'], 'slice_dims')
307
308
    def _output_metadata_dict(self, entry, mData):
309
        entry.attrs['NX_class'] = 'NXcollection'
310
        for key, value in mData.items():
311
            if key != 'rotation_angle':
312
                nx_data = entry.require_group(key)
313
                if isinstance(value, dict):
314
                    self._output_metadata_dict(nx_data, value)
315
                else:
316
                    nx_data.attrs['NX_class'] = 'NXdata'
317
                    self.__output_data(nx_data, value, key)
318
319 View Code Duplication
    def __output_data(self, entry, data, name):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
320
        if isinstance(data, dict):
321
            entry = entry.require_group(name)
322
            entry.attrs['NX_class'] = 'NXcollection'
323
            for key, value in data.items():
324
                self.__output_data(entry, value, key)
325
        else:
326
            try:
327
                self.__create_dataset(entry, name, data)
328
            except Exception:
329
                try:
330
                    import json
331
                    data = np.array([json.dumps(data).encode("ascii")])
332
                    self.__create_dataset(entry, name, data)
333
                except Exception:
334
                    try:
335
                        self.__create_dataset(entry, name, data)
336
                    except:
337
                        raise Exception('Unable to output %s to file.' % name)
338
339
    def __create_dataset(self, entry, name, data):
340
        if name not in list(entry.keys()):
341
            entry.create_dataset(name, data=data)
342
        else:
343
            entry[name][...] = data
344
345 View Code Duplication
    def _link_datafile_to_nexus_file(self, data):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
346
        filename = self.exp.meta_data.get('nxs_filename')
347
348
        with h5py.File(filename, 'a') as nxs_file:
349
            # entry path in nexus file
350
            name = data.get_name()
351
            group_name = self.exp.meta_data.get(['group_name', name])
352
            link = self.exp.meta_data.get(['link_type', name])
353
            name = data.get_name(orig=True)
354
            nxs_entry = self.__add_nxs_entry(nxs_file, link, group_name, name)
355
            self.__add_nxs_data(nxs_file, nxs_entry, link, group_name, data)
356
357
    def __add_nxs_entry(self, nxs_file, link, group_name, name):
358
        if name == 'phantom':
359
            nxs_entry = '/entry/' + link
360
        else:
361
            nxs_entry = ''
362
        nxs_entry += '_' + name if link == 'final_result' else "/" + group_name
363
        nxs_entry = nxs_file[nxs_entry]
364
        nxs_entry.attrs['signal'] = 'data'
365
        return nxs_entry
366
367 View Code Duplication
    def __add_nxs_data(self, nxs_file, nxs_entry, link, group_name, data):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
368
        data_entry = nxs_entry.name + '/data'
369
        # output file path
370
        h5file = data.backing_file.filename
371
372
        if link == 'input_data':
373
            dataset = self.__is_h5dataset(data)
374
            if dataset:
375
                nxs_file[data_entry] = \
376
                    h5py.ExternalLink(os.path.abspath(h5file), dataset.name)
377
        else:
378
            # entry path in output file path
379
            m_data = self.exp.meta_data.get
380
            if not (link == 'intermediate' and
381
                    m_data('inter_path') != m_data('out_path')):
382
                h5file = h5file.split(m_data('out_folder') + '/')[-1]
383
            nxs_file[data_entry] = \
384
                h5py.ExternalLink(h5file, group_name + '/data')
385