async def test_flat_correct(self): shape = (2, 2) dark = np.ones(shape) flat = np.ones(shape) * 10 truth_base = np.ones(shape) async def check(producer): i = 1 async for frame in producer: value = (i - 1) / 9.0 np.testing.assert_almost_equal(frame, truth_base * value) i += 1 await check(flat_correct(flat, produce_frames(), dark=dark))
def test_flat_correct(self): shape = (2, 2) dark = np.ones(shape) flat = np.ones(shape) * 10 truth_base = np.ones(shape) @coroutine def check(): i = 1 while True: frame = yield value = (i - 1) / 9.0 np.testing.assert_almost_equal(frame, truth_base * value) i += 1 frame_producer(flat_correct(flat, check(), dark=dark))
def center_rotation_axis(camera, motor, initial_motor_step, num_iterations=2, num_projections=None, flat=None, dark=None): """ Center the rotation axis controlled by *motor*. Use an iterative approach to center the rotation axis. Around *motor*s current position, we evaluate five points by running a reconstruction. *rotation_motor* rotates the sample around the tomographic axis. *num_iterations* controls the final resolution of the step size, halving each iteration. *flat* is a flat field frame and *dark* is a dark field frame which will be used for flat correcting the acuired projections. """ width_2 = camera.roi_width.magnitude / 2.0 axis_pos = width_2 # Crop the dark and flat if flat is not None: middle = flat.shape[0] / 2 flat = flat[middle, :] if dark is not None: dark = dark[middle, :] n = num_projections or tomo_projections_number(camera.roi_width) angle_step = np.pi / n * q.rad step = initial_motor_step current = motor.position for i in range(num_iterations): frm = current - step to = current + step div = 2.0 * step / 5.0 positions = (frm, frm + div, current, current + div, to) scores = [] for position in positions: motor.position = position backproject = Backproject(axis_pos) sino_result = Result() sino_coro = sino_result() if flat is not None: sino_coro = flat_correct(flat, sino_coro, dark=dark) inject(frames(n, camera, callback=lambda: rotation_motor.move(angle_step).join()), middle_row(sinograms(n, sino_coro))) sinogram = (sinogram.result[0, :, :], ) result = Result() m0 = np.mean(np.sum(sinogram[0], axis=1)) inject(sinogram, backproject(result())) backproject.wait() img = result.result # Other possibilities: sum(abs(img)) or sum(img * heaviside(-img)) score = np.sum(np.abs(np.gradient(img))) / m0 scores.append(score) current = positions[scores.index(min(scores))] step /= 2.0