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
|
|
|
# $Id: dezing_filter.py 467 2016-02-16 11:40:42Z kny48981 $ |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
""" |
18
|
|
|
.. module:: dezinger_simple_deprecated |
19
|
|
|
:platform: Unix |
20
|
|
|
:synopsis: A plugin to remove zingers |
21
|
|
|
|
22
|
|
|
.. moduleauthor:: Mark Basham <[email protected]> |
23
|
|
|
|
24
|
|
|
""" |
25
|
|
|
|
26
|
|
|
import numpy as np |
27
|
|
|
|
28
|
|
|
import scipy.signal.signaltools as sig |
29
|
|
|
|
30
|
|
|
from savu.plugins.filters.base_filter import BaseFilter |
31
|
|
|
from savu.plugins.driver.cpu_plugin import CpuPlugin |
32
|
|
|
from savu.plugins.utils import register_plugin |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
class DezingerSimpleDeprecated(BaseFilter, CpuPlugin): |
36
|
|
|
""" |
37
|
|
|
""" |
38
|
|
|
|
39
|
|
|
def __init__(self): |
40
|
|
|
super(DezingerSimpleDeprecated, self).__init__("DezingerSimpleDeprecated") |
41
|
|
|
self.zinger_proportion = 0.0 |
42
|
|
|
self.frame_limit = 8 |
43
|
|
|
|
44
|
|
|
def pre_process(self): |
45
|
|
|
inData = self.get_in_datasets()[0] |
46
|
|
|
self.proj_dim = inData.data.proj_dim |
47
|
|
|
|
48
|
|
|
self._kernel = [1]*3 |
49
|
|
|
self._kernel[self.proj_dim] = self.kernel_size |
50
|
|
|
|
51
|
|
|
pad_list = [(0, 0)]*3 |
52
|
|
|
pad_list[self.proj_dim] = (self.pad, self.pad) |
53
|
|
|
|
54
|
|
|
dark = inData.data.dark() |
55
|
|
|
flat = inData.data.flat() |
56
|
|
|
if dark.size: |
57
|
|
|
dark = np.pad(inData.data.dark(), pad_list, mode='edge') |
58
|
|
|
dark = self._process_calibration_frames(dark) |
59
|
|
|
inData.data.update_dark(dark[self.pad:-self.pad]) |
60
|
|
|
if flat.size: |
61
|
|
|
flat = np.pad(inData.data.flat(), pad_list, mode='edge') |
62
|
|
|
flat = self._process_calibration_frames(flat) |
63
|
|
|
inData.data.update_flat(flat[self.pad:-self.pad]) |
64
|
|
|
|
65
|
|
|
def _process_calibration_frames(self, data): |
66
|
|
|
nSlices = data.shape[self.proj_dim] - 2*self.pad |
67
|
|
|
nSublists = int(np.ceil(nSlices/float(self.frame_limit))) |
68
|
|
|
idx = np.array_split(np.arange(self.pad, nSlices+self.pad), nSublists) |
69
|
|
|
idx = [np.arange(a[0]-self.pad, a[-1]+self.pad+1) for a in idx] |
70
|
|
|
out_sl = np.tile([slice(None)]*3, [len(idx), 1]) |
71
|
|
|
out_sl[:, self.proj_dim] = idx |
72
|
|
|
result = np.empty_like(data) |
73
|
|
|
for sl in out_sl: |
74
|
|
|
result[tuple(sl)] = self._dezing(data[tuple(sl)]) |
|
|
|
|
75
|
|
|
return result |
76
|
|
|
|
77
|
|
|
def _dezing(self, data): |
78
|
|
|
result = data[...] |
79
|
|
|
median_result = sig.medfilt(data, self._kernel) |
80
|
|
|
differrence = np.abs(data-median_result) |
81
|
|
|
replace_mask = differrence > self.parameters['outlier_mu'] |
82
|
|
|
self.zinger_proportion = \ |
83
|
|
|
max(self.zinger_proportion, np.sum(replace_mask)/( |
84
|
|
|
np.size(replace_mask)*1.0)) |
85
|
|
|
result[replace_mask] = median_result[replace_mask] |
86
|
|
|
return result |
87
|
|
|
|
88
|
|
|
def process_frames(self, data): |
89
|
|
|
return self._dezing(data[0]) |
90
|
|
|
|
91
|
|
|
def get_max_frames(self): |
92
|
|
|
""" Setting nFrames to multiple with an upper limit of 4 frames. """ |
93
|
|
|
return ['multiple', self.frame_limit] |
94
|
|
|
|
95
|
|
|
def raw_data(self): |
96
|
|
|
return True |
97
|
|
|
|
98
|
|
|
def set_filter_padding(self, in_data, out_data): |
99
|
|
|
# kernel size must be odd |
100
|
|
|
ksize = self.parameters['kernel_size'] |
101
|
|
|
self.kernel_size = ksize+1 if ksize % 2 == 0 else ksize |
102
|
|
|
|
103
|
|
|
in_data = in_data[0] |
104
|
|
|
self.pad = (self.kernel_size - 1) // 2 |
105
|
|
|
self.data_size = in_data.get_shape() |
106
|
|
|
in_data.padding = {'pad_multi_frames': self.pad} |
107
|
|
|
out_data[0].padding = {'pad_multi_frames': self.pad} |
108
|
|
|
|
109
|
|
|
def executive_summary(self): |
110
|
|
|
if self.zinger_proportion > 0.5: |
111
|
|
|
return ["Over 50% of the pixels were treated as zingers!!"] |
112
|
|
|
if self.zinger_proportion > 0.05: |
113
|
|
|
return ["Over 5% of the pixels were treated as zingers"] |
114
|
|
|
return ["Nothing to Report"] |
115
|
|
|
|