Test Failed
Push — master ( e47727...48c83a )
by
unknown
01:18 queued 20s
created

savu.plugins.savers.mrc_saver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A MrcSaver.__get_file_name() 0 6 1
A MrcSaver.__init__() 0 4 1
A MrcSaver.process_frames() 0 5 1
A MrcSaver.pre_process() 0 21 2
A MrcSaver.post_process() 0 4 1
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:: mrc_saver
17
   :platform: Unix
18
   :synopsis: Saves data as .mrc file
19
20
.. moduleauthor:: Elaine Ho <[email protected]>
21
"""
22
from savu.plugins.utils import register_plugin
23
from savu.plugins.plugin import Plugin
24
# Import any additional libraries or base plugins here.
25
import os
26
import mrcfile
27
import logging
28
29
from savu.plugins.savers.base_image_saver import BaseImageSaver
30
from savu.plugins.driver.cpu_plugin import CpuPlugin
31
import savu.core.utils as cu
32
33
# This decorator is required for the configurator to recognise the plugin
34
@register_plugin
35
class MrcSaver(BaseImageSaver, CpuPlugin):
36
# Each class must inherit from the Plugin class and a driver
37
38
    def __init__(self):
39
        super(MrcSaver, self).__init__("MrcSaver")
40
        self.in_data = None
41
        self.out_filename = None
42
43
    def pre_process(self):
44
        # Get metadata to create mrc file
45
        self.in_data = self.get_in_datasets()[0]
46
        self.out_filename = self.__get_file_name()
47
        data_type_lookup = {'int8': 0, 'int16': 1, 'float32':2, 'complex64':4, 'uint16':6}
48
        try:
49
            mrc_mode = data_type_lookup[self.parameters['mrc_mode']]
50
        except:
51
            msg = "Mrc mode must be one of {}, "\
52
                "defaulting to uint16".format(list(data_type_lookup.keys()))
53
            mrc_mode = data_type_lookup['uint16']
54
            logging.warning(msg)
55
            cu.user_message(msg)
56
57
        # Create mrc file
58
        self.mrc = mrcfile.new_mmap(
59
            name=self.out_filename,
60
            shape=self.in_data.get_shape(),
61
            mrc_mode=mrc_mode,
62
        )
63
        logging.info("Created mrc file {}".format(self.out_filename))
64
65
    def process_frames(self, data):
66
        # Save data to mrc in parallel
67
        idx = self.get_current_slice_list()[0]
68
        self.mrc.data[idx] = data[0]
69
        logging.debug("Processed frame {}".format(idx[0]))
70
71
    def post_process(self):
72
        # Close mrc file
73
        self.mrc.close()
74
        logging.info("MRC file closed")
75
76
    def __get_file_name(self):
77
        # Get the mrc output filename in the format <original_img_name>_processed.mrc
78
        out_path = self.exp.meta_data.get('out_path')
79
        imgname = os.path.splitext(os.path.split(self.exp.meta_data.get('data_file'))[-1])[0]
80
        fname = "{}_processed.mrc".format(imgname)
81
        return os.path.join(out_path, fname)
82