File: test_plugins.py

package info (click to toggle)
ufo-core 0.17.0.22.gc831aec-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,184 kB
  • sloc: ansic: 10,768; python: 1,004; lisp: 266; cpp: 98; xml: 55; makefile: 25; sh: 25
file content (281 lines) | stat: -rw-r--r-- 7,020 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import ufo.numpy
import numpy as np
import tifffile
import contextlib
from common import disable, tempdir


a, b = 1.5, 2.5
ones = np.ones((512, 512))
zeros = np.zeros((512, 512))
small = np.ones((256, 256))
random = np.random.random((512, 512))


def have_camera_plugin():
    from gi.repository import Ufo

    return 'camera' in Ufo.PluginManager().get_all_task_names()


@contextlib.contextmanager
def single_tiff_setup(n_images, fmt='foo-{:05}.tif'):
    with tempdir() as d:
        data = np.ones((512, 512), dtype=np.float32)

        for i in range(n_images):
            tifffile.imsave(d.path(fmt.format(i)), data)

        yield d


def test_read_single_tiffs():
    from ufo import Read, Null

    with single_tiff_setup(32) as d:
        read = Read(path=d.root)
        null = Null()

        null(read()).run().join()
        assert(null.task.props.num_processed == 32)


def test_read_single_tiffs_stepped():
    from ufo import Read, Null

    with single_tiff_setup(32) as d:
        read = Read(path=d.root, step=2)
        null = Null()
        null(read()).run().join()
        assert(null.task.props.num_processed == 32 / 2)


def test_read_single_tiffs_start_modified():
    from ufo import Read, Null

    with single_tiff_setup(32) as d:
        read = Read(path=d.root, start=15)
        null = Null()

        null(read()).run().join()
        assert(null.task.props.num_processed == 32 - 15)


@disable
def test_read_multi_tiffs():
    from ufo import Read, Null

    with tempdir() as d:
        n_images = 32
        data = np.ones((512, 512, n_images))
        tifffile.imsave(d.path('foo.tif'), data)

        read = Read(path=d.path('foo.tif'))
        null = Null()

        null(read()).run().join()
        assert(null.task.props.num_processed == n_images)


def test_average():
    from ufo import Average
    average = Average()

    for x in average([a * ones, b * ones]):
        expected = (a + b) / 2
        assert(np.all(x == expected))


def test_buffer():
    from ufo import DummyData, Buffer

    data = DummyData(number=10, width=512, height=256)
    buffr = Buffer()
    result = list(buffr(data()))
    assert(len(result) == 10)

    for r in result:
        assert(r.shape[0] == 256)
        assert(r.shape[1] == 512)


def test_rescale():
    from ufo import Rescale
    rescale = Rescale(factor=0.5)
    result = list(rescale([a * ones, b * small]))
    assert(np.mean(result[0]) == a)
    assert(np.mean(result[1]) == b)


@disable
def test_roi():
    from ufo import CutRoi

    x, y = 10, 20
    w, h = 256, 128
    roi = CutRoi(x=x, y=y, width=w, height=h)
    result = list(roi([random, random]))
    ref = random[y:y+h, x:x+w]
    assert(ref.shape[0] == h)
    assert(ref.shape[1] == w)
    assert(np.all(ref == result[0]))


def test_stack():
    from ufo import Stack
    stack = Stack(number=2)

    for x in stack([a * ones, b * ones]):
        assert(x.shape[0] == 2)
        assert(np.all(x[0,:,:] == a))
        assert(np.all(x[1,:,:] == b))


def test_flatten():
    from ufo import FlattenInplace

    summing = FlattenInplace(mode="sum")
    result = list(summing([a * ones, b * ones]).items())
    assert(np.all(result[0] == a + b))


def test_fft_1d():
    from ufo import Fft, Ifft

    fft = Fft(dimensions=1)
    ifft = Ifft(dimensions=1)
    orig_a = a * ones
    orig_b = b * random
    result = list(ifft(fft([orig_a, orig_b])))

    assert(np.sum(orig_a - result[0]) < 0.001)
    assert(np.sum(orig_b - result[1]) < 0.01)


def test_fft_2d():
    from ufo import Fft, Ifft

    fft = Fft(dimensions=2)
    ifft = Ifft(dimensions=2)
    orig_a = a * ones
    orig_b = b * random
    result = list(ifft(fft([orig_a, orig_b])))

    assert(np.sum(orig_a - result[0]) < 0.001)
    assert(np.sum(orig_b - result[1]) < 0.1)


def test_flatfield_correction():
    from ufo import FlatFieldCorrect

    darks = np.ones((512, 512)) * 1.5
    flats = np.ones((512, 512)) * 11.5
    projs = np.ones((512, 512)) * 100.0
    ffc = FlatFieldCorrect()

    expected = (projs - darks) / (flats - darks)
    result = list(ffc([projs, projs], [darks, darks], [flats, flats]))[0]
    assert(np.sum(np.abs(expected - result)) < 1)

    expected = - np.log((projs - darks) / (flats - darks))
    ffc = FlatFieldCorrect(absorption_correct=True)
    result = list(ffc([projs, projs], [darks, darks], [flats, flats]))[0]
    assert(np.sum(np.abs(expected - result)) < 1)


def test_measure():
    from ufo import Measure

    measures = []

    def measure_callback(m, a):
        measures.append(ufo.numpy.asarray(a))

    measure = Measure(metric='mean', axis=-1)
    measure.connect('result', measure_callback)
    measure([a * ones, b * ones]).run().join()
    assert(len(measures) == 2)


def test_dummy_data():
    from ufo import DummyData

    data = DummyData(number=10, width=256, height=128)
    result = list(data())
    assert(len(result) == 10)
    assert(all(r.shape[0] == 128 and r.shape[1] == 256 for r in result))


def test_metaballs():
    from ufo import Metaballs

    metaballs = Metaballs(number=5, number_balls=5, width=512, height=256)
    result = list(metaballs())
    assert(len(result) == 5)
    assert(all(r.shape[0] == 256 and r.shape[1] == 512 for r in result))


def test_transpose():
    from ufo import Transpose

    transpose = Transpose()
    ones = np.ones((256, 512))
    zeros = np.zeros((256, 128))
    result = list(transpose([ones, zeros]))
    assert(np.all(result[0] == ones.transpose()))
    assert(np.all(result[1] == zeros.transpose()))


def test_uca():
    if have_camera_plugin():
        from ufo import Camera

        camera = Camera(name='mock', number=2)
        result = list(camera())
        assert(len(result) == 2)


def test_uca_direct():
    try:
        from gi.repository import Ufo, Uca

        if have_camera_plugin():
            from ufo import Camera

            uca_pm = Uca.PluginManager()
            mock = uca_pm.get_camerav('mock', [])
            camera = Camera(camera=mock, count=3)

            result = list(camera())
            assert(len(result) == 3)
    except ImportError:
        pass


def test_memory_in():
    with tempdir() as d:
        from ufo import MemoryIn, Write

        ref = random.astype(np.float32)
        read = MemoryIn(pointer=ref.__array_interface__['data'][0], number=1,
                        width=ref.shape[1], height=ref.shape[0])
        write = Write(filename=d.path('foo.tif'))

        write(read()).run().join()
        result = tifffile.imread(d.path('foo.tif'))
        assert(np.all(ref == result))


def test_memory_out():
    with tempdir() as d:
        from ufo import MemoryOut, Read

        ref = random.astype(np.float32)
        out = np.zeros_like(ref).astype(np.float32)
        tifffile.imsave(d.path('foo.tif'), ref)

        read = Read(path=d.path('foo.tif'))
        write = MemoryOut(pointer=out.__array_interface__['data'][0], max_size=ref.nbytes)

        write(read()).run().join()
        assert(np.all(out == ref))