Ejemplo n.º 1
0
    def connect(self):
        """Connect producer with consumers."""
        started = []
        for not_started in self.consumers:
            started.append(not_started())

        inject(self.producer(), broadcast(*started))
Ejemplo n.º 2
0
 def run_test(data, shape=None, dtype=None):
     accumulate = Accumulate(shape=shape, dtype=dtype)
     inject(data, accumulate())
     np.testing.assert_equal(accumulate.items, data)
     target_type = np.ndarray if shape else list
     self.assertTrue(isinstance(accumulate.items, target_type))
     if shape:
         self.assertEqual(accumulate.items.dtype, data.dtype)
Ejemplo n.º 3
0
    def __call__(self):
        """Run the acquisition, i.e. connect the producer and consumer."""
        LOG.debug("Running acquisition '{}'".format(self))

        started = []
        for not_started in self.consumers:
            started.append(not_started())

        inject(self.generator(), broadcast(*started))
Ejemplo n.º 4
0
def test_frames():
    @coroutine
    def count():
        while True:
            yield
            count.i += 1
    count.i = 0
    inject(frames(5, Camera()), count())
    assert count.i == 5
Ejemplo n.º 5
0
    def write(self, data=None, dsetname=None):
        """Write a sequence of *data* if specified, otherwise this method turns into a coroutine.
        The data set name is given by *dsetname*.
        """
        write_coro = self._write_coroutine(dsetname=dsetname)

        if data is None:
            return write_coro
        else:
            inject(data, write_coro)
Ejemplo n.º 6
0
    def write(self, data=None, dsetname=None):
        """Write a sequence of *data* if specified, otherwise this method turns into a coroutine.
        The data set name is given by *dsetname*.
        """
        write_coro = self._write_coroutine(dsetname=dsetname)

        if data is None:
            return write_coro
        else:
            inject(data, write_coro)
Ejemplo n.º 7
0
def plot_exposure_scan(min_exposure=1*q.ms, max_exposure=500*q.ms, num_points=10):
    """
    Plot the mean value of the detector image for exposure times between
    *min_exposure* and *max_exposure*, use *num_points* data points.

    Returns: a tuple with exposure times and corresponding mean values.
    """
    accum = Accumulate()
    region = Region(camera['exposure_time'], np.linspace(min_exposure, max_exposure, num_points))

    with camera.recording():
        inject(resolve(get_exposure_result(region)), broadcast(PyplotViewer(style='-o')(), accum()))

    return zip(*accum.items)
Ejemplo n.º 8
0
def compute_rotation_axis(sinogram, initial_step=None, max_iterations=14,
                          slice_consumer=None, score_consumer=None):

    width_2 = sinogram.shape[1] / 2.0
    iteration = 0
    step = initial_step or width_2 / 2
    current = width_2

    while step > 1 and iteration < max_iterations:
        frm = current - step
        to = current + step
        div = 2.0 * step / 5.0

        axes = (frm, frm + div, current, current + div, to)
        scores = []

        for axis in axes:
            backproject = Backproject(axis)
            result = Result()

            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)))
            scores.append(score)
            if slice_consumer:
                slice_consumer.send(img)
            if score_consumer:
                score_consumer.send(axis * q.px)

        current = axes[scores.index(min(scores))]
        step /= 2.0
        iteration += 1

    return current
Ejemplo n.º 9
0
 def test_injection(self):
     inject(generator(), self.consume())
     self.assertEqual(self.data, 4)
Ejemplo n.º 10
0
 def setUp(self):
     self.timer = Timer()
     inject([1, 2, 3], self.timer(null()))
Ejemplo n.º 11
0
 def test_process(self):
     result = Result()
     inject((1,), process(lambda x: -x, result()))
     self.assertEqual(-1, result.result)
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
 def test_coroutine(self):
     inject(self.data, self.walker.write(dsetname='foo'))
     self.check()