Пример #1
0
def main():
    """Main function."""
    args = parse_args()
    syris.init(loglevel=logging.INFO, double_precision=args.double_precision)
    units = q.Quantity(1, args.units)
    triangles = make_cube(
    ).magnitude if args.input is None else read_blender_obj(args.input)
    triangles = triangles * units
    tr = geom.Trajectory([(0, 0, 0)] * units)
    mesh = Mesh(triangles,
                tr,
                center=args.center,
                iterations=args.supersampling)
    LOG.info('Number of triangles: {}'.format(mesh.num_triangles))

    shape = (args.n, args.n)
    if args.pixel_size is None:
        if args.input is None:
            fov = 4. * units
        else:
            # Maximum sample size in x and y direction
            max_diff = np.max(mesh.extrema[:-1, 1] - mesh.extrema[:-1, 0])
            fov = max_diff
        fov *= args.margin
        args.pixel_size = fov / args.n
    else:
        fov = args.n * args.pixel_size

    if args.translate is None:
        translate = (fov.simplified.magnitude / 2.,
                     fov.simplified.magnitude / 2., 0) * q.m
    else:
        translate = (args.translate[0].simplified.magnitude,
                     args.translate[1].simplified.magnitude, 0) * q.m
    LOG.info('Translation: {}'.format(translate.rescale(q.um)))

    mesh.translate(translate)
    mesh.rotate(args.y_rotate, geom.Y_AX)
    mesh.rotate(args.x_rotate, geom.X_AX)
    fmt = 'n: {}, pixel size: {}, FOV: {}'
    LOG.info(
        fmt.format(args.n, args.pixel_size.rescale(q.um), fov.rescale(q.um)))

    st = time.time()
    proj = mesh.project(shape, args.pixel_size, t=None).get()
    LOG.info('Duration: {} s'.format(time.time() - st))
    offset = (0, translate[1].simplified, -(fov / 2.).simplified) * q.m

    if args.projection_filename is not None:
        save_image(args.projection_filename, proj)

    if args.compute_slice:
        sl = mesh.compute_slices((1, ) + shape, args.pixel_size,
                                 offset=offset).get()[0]
        if args.slice_filename is not None:
            save_image(args.slice_filename, sl)
        show(sl, title='Slice at y = {}'.format(args.n / 2))

    show(proj, title='Projection')
    plt.show()
Пример #2
0
def make_motion(args):
    syris.init()
    n = 256
    shape = (n, n)
    energies = np.arange(5, 30, 1) * q.keV
    bm, detector = make_devices(n, energies)
    mb = create_sample(n, detector.pixel_size, velocity=20 * q.mm / q.s)
    mb_2 = create_sample(n, detector.pixel_size, velocity=10 * q.mm / q.s)
    mb.material = get_material('pmma_5_30_kev.mat')
    mb_2.material = mb.material

    cube = make_cube() / q.m * 30 * detector.pixel_size + 0.1 * detector.pixel_size
    fov = detector.pixel_size * n
    circle = make_circle().magnitude * fov / 30000 + fov / 2
    tr = Trajectory(circle, velocity=10 * q.um / q.s)
    glass = get_material('glass.mat')
    mesh = Mesh(cube, tr, material=glass)
    ex = Experiment([bm, mb, mb_2, mesh], bm, detector, 0 * q.m, energies)

    for sample in ex.samples:
        if sample != bm:
            sample.trajectory.bind(detector.pixel_size)

    if args.show_flat:
        show(get_flat(shape, energies, detector, bm), title='Counts')
        plt.show()

    if args.conduct:
        if args.output is not None and not os.path.exists(args.output):
            os.makedirs(args.output, mode=0o755)

        t_0 = 0 * q.s
        if args.num_images:
            t_1 = args.num_images / detector.camera.fps
        else:
            t_1 = ex.time

        st = time.time()
        mpl_im = None
        for i, proj in enumerate(ex.make_sequence(t_0, t_1)):
            image = get_host(proj)

            if args.show:
                if mpl_im is None:
                    plt.figure()
                    mpl_im = plt.imshow(image)
                    plt.show(False)
                else:
                    mpl_im.set_data(image)
                    plt.draw()

            if args.output:
                path = os.path.join(args.output, 'projection_{:>05}.png').format(i)
                scipy.misc.imsave(path, image)

        print 'Maximum intensity:', image.max()
        print 'Duration: {} s'.format(time.time() - st)

    plt.show()
Пример #3
0
def make_complex_trajectory_sequence(args):
    edge = 20
    x = np.linspace(0, args.n / 2 - args.n / 4 - edge - 5, num=10)
    y = z = np.zeros(x.shape)
    # Move along x axis
    traj_x = Trajectory(zip(x, y, z) * args.ps, velocity=args.ps / q.s, pixel_size=args.ps)
    # Move along y axis
    traj_y = Trajectory(zip(y, x, z) * args.ps, velocity=args.ps / q.s, pixel_size=args.ps)
    # Move along both x and y axes
    traj_xy = Trajectory(zip(x, x, z) * args.ps, velocity=args.ps / q.s, pixel_size=args.ps)
    # Circular trajectory of the composite body rotates around the image center and with radius
    # n / 4 pixels.
    circle = args.n / 2 * args.ps + make_circle().magnitude * args.n / 4 * args.ps
    traj_circle = Trajectory(circle, velocity=args.ps / q.s, pixel_size=args.ps)
    # Make the trajectory of the circle the same duration as the simple linear one.
    traj_circle = Trajectory(circle, velocity=traj_circle.length / traj_xy.length * args.ps / q.s)
    # three cubes in the same height and depth, shifted only along the x axis.
    traj_stationary = Trajectory([(0, 0, 0)] * args.ps)
    traj_stationary_1 = Trajectory([(-2 * edge, 0, 0)] * args.ps)
    traj_stationary_2 = Trajectory([(2 * edge, 0, 0)] * args.ps)

    cube = make_cube() / q.m * edge * args.ps
    # The cubes are elongated along y axis.
    cube[::2, :] /= 3

    mesh = Mesh(cube, traj_x, orientation=geom.Y_AX)
    mesh_2 = Mesh(cube, traj_y, orientation=geom.Y_AX)
    mesh_3 = Mesh(cube, traj_xy, orientation=geom.Y_AX)
    mesh_stationary = Mesh(cube, traj_stationary, orientation=geom.Y_AX)
    mesh_stationary_1 = Mesh(cube, traj_stationary_1, orientation=geom.Y_AX)
    mesh_stationary_2 = Mesh(cube, traj_stationary_2, orientation=geom.Y_AX)
    bodies = [mesh, mesh_2, mesh_3, mesh_stationary, mesh_stationary_1, mesh_stationary_2]
    composite = CompositeBody(traj_circle, bodies=bodies, orientation=geom.Y_AX)
    composite.bind_trajectory(args.ps)

    total_time = composite.time
    if args.t is None:
        times = np.linspace(0, 1, 100)
    else:
        if args.t < 0 or args.t > 1:
            raise ValueError('--t must be in the range [0, 1]')
        times = [args.t]

    im = None
    for index, i in enumerate(times):
        t = i * total_time
        composite.clear_transformation()
        composite.move(t)
        p = composite.project(args.shape, args.ps).get()
        if im is None:
            im = show(p, title='Projection')
        else:
            im.set_data(p)
            plt.draw()

    plt.show()
Пример #4
0
def make_cube_body(n, ps, cube_edge, phase_shift=None):
    fov = n * ps
    triangles = make_cube().magnitude * cube_edge / 2
    # Rotation around the vertical axis
    points = make_circle(axis='y', overall_angle=np.pi * q.rad,
                         phase_shift=phase_shift).magnitude
    points = points * fov / 4 + [n / 2, 0, 0] * ps
    trajectory = geom.Trajectory(points, pixel_size=ps, velocity=ps / q.s)
    # *orientation* aligns the object with the trajectory derivative
    mesh = Mesh(triangles, trajectory, orientation=geom.Z_AX)

    return mesh
Пример #5
0
def make_cube_body(n, ps, cube_edge, phase_shift=None):
    fov = n * ps
    triangles = make_cube().magnitude * cube_edge / 2
    # Rotation around the vertical axis
    points = make_circle(axis="y",
                         overall_angle=np.pi * q.rad,
                         phase_shift=phase_shift).magnitude
    points = points * fov / 4 + [n // 2, 0, 0] * ps
    trajectory = geom.Trajectory(points, pixel_size=ps, velocity=ps / q.s)
    # *orientation* aligns the object with the trajectory derivative
    mesh = Mesh(triangles, trajectory, orientation=geom.Z_AX)

    return mesh
Пример #6
0
def main():
    syris.init()
    n = 256
    shape = (n, n)
    ps = 1 * q.um
    fov = n * ps
    triangles = make_cube().magnitude * n / 8. * ps
    # Rotation around the vertical axis
    points = make_circle(axis='y').magnitude * fov / 30000 + fov / 2
    trajectory = geom.Trajectory(points, pixel_size=ps, velocity=10 * q.um / q.s)
    # *orientation* aligns the object with the trajectory derivative
    mesh = Mesh(triangles, trajectory, orientation=geom.Z_AX)
    # Compute projection at the angle Pi/4
    projection = mesh.project(shape, ps, t=trajectory.time / 8).get()

    show(projection)
    plt.show()
Пример #7
0
def main():
    syris.init()
    n = 256
    shape = (n, n)
    ps = 1 * q.um
    fov = n * ps
    triangles = make_cube().magnitude * n / 8. * ps
    # Rotation around the vertical axis
    points = make_circle(axis='y').magnitude * fov / 30000 + fov / 2
    trajectory = geom.Trajectory(points,
                                 pixel_size=ps,
                                 velocity=10 * q.um / q.s)
    # *orientation* aligns the object with the trajectory derivative
    mesh = Mesh(triangles, trajectory, orientation=geom.Z_AX)
    # Compute projection at the angle Pi/4
    projection = mesh.project(shape, ps, t=trajectory.time / 8).get()

    show(projection)
    plt.show()
Пример #8
0
def main():
    args = parse_args()
    syris.init(device_index=0)
    n = 512
    shape = (n, n)
    ps = 1 * q.um

    x = np.linspace(0, n, num=10)
    y = z = np.zeros(x.shape)
    traj_x = Trajectory(zip(x, y, z) * ps, velocity=ps / q.s)
    traj_y = Trajectory(zip(y, x, z) * ps, velocity=ps / q.s)
    traj_xy = Trajectory(zip(n - x, x, z) * ps, velocity=ps / q.s)
    mb = MetaBall(traj_x, n * ps / 16)
    cube = make_cube() / q.m * 16 * ps
    mesh = Mesh(cube, traj_xy)
    composite = CompositeBody(traj_y, bodies=[mb, mesh])
    composite.bind_trajectory(ps)
    t = args.t * n * q.s

    composite.move(t)
    p = composite.project(shape, ps).get()
    show(p, title='Projection')

    plt.show()
Пример #9
0
    def test_project_composite(self):
        n = 64
        shape = (n, n)
        ps = 1 * q.um
        x = np.linspace(0, n, num=10)
        y = z = np.zeros(x.shape)
        traj_x = Trajectory(zip(x, y, z) * ps, velocity=ps / q.s)
        traj_y = Trajectory(zip(y, x, z) * ps, velocity=ps / q.s)
        traj_xy = Trajectory(zip(n - x, x, z) * ps, velocity=ps / q.s)
        mb = MetaBall(traj_x, n * ps / 16)
        cube = make_cube() / q.m * 16 * ps / 4
        mesh = Mesh(cube, traj_xy)
        composite = CompositeBody(traj_y, bodies=[mb, mesh])
        composite.bind_trajectory(ps)

        composite.move(n / 2 * q.s)
        p = composite.project(shape, ps).get()
        composite.clear_transformation()

        # Compute
        composite.move(n / 2 * q.s)
        p_separate = (mb.project(shape, ps) + mesh.project(shape, ps)).get()

        np.testing.assert_almost_equal(p, p_separate)
Пример #10
0
def make_motion(args):
    syris.init()
    n = 256
    shape = (n, n)
    energies = np.arange(5, 30, 1) * q.keV
    bm, detector = make_devices(n, energies)
    mb = create_sample(n, detector.pixel_size, velocity=20 * q.mm / q.s)
    mb_2 = create_sample(n, detector.pixel_size, velocity=10 * q.mm / q.s)
    mb.material = get_material('pmma_5_30_kev.mat')
    mb_2.material = mb.material

    cube = make_cube(
    ) / q.m * 30 * detector.pixel_size + 0.1 * detector.pixel_size
    fov = detector.pixel_size * n
    circle = make_circle().magnitude * fov / 30000 + fov / 2
    tr = Trajectory(circle, velocity=10 * q.um / q.s)
    glass = get_material('glass.mat')
    mesh = Mesh(cube, tr, material=glass)
    ex = Experiment([bm, mb, mb_2, mesh], bm, detector, 0 * q.m, energies)

    for sample in ex.samples:
        if sample != bm:
            sample.trajectory.bind(detector.pixel_size)

    if args.show_flat:
        show(get_flat(shape, energies, detector, bm), title='Counts')
        plt.show()

    if args.conduct:
        if args.output is not None and not os.path.exists(args.output):
            os.makedirs(args.output, mode=0o755)

        t_0 = 0 * q.s
        if args.num_images:
            t_1 = args.num_images / detector.camera.fps
        else:
            t_1 = ex.time

        st = time.time()
        mpl_im = None
        for i, proj in enumerate(ex.make_sequence(t_0, t_1)):
            image = get_host(proj)

            if args.show:
                if mpl_im is None:
                    plt.figure()
                    mpl_im = plt.imshow(image)
                    plt.show(False)
                else:
                    mpl_im.set_data(image)
                    plt.draw()

            if args.output:
                path = os.path.join(args.output,
                                    'projection_{:>05}.png').format(i)
                scipy.misc.imsave(path, image)

        print 'Maximum intensity:', image.max()
        print 'Duration: {} s'.format(time.time() - st)

    plt.show()
Пример #11
0
 def setUp(self):
     default_syris_init()
     self.triangles = make_cube()
     self.trajectory = Trajectory([(0, 0, 0)] * q.m)
     self.mesh = Mesh(self.triangles, self.trajectory)
Пример #12
0
 def setUp(self):
     syris.init(device_index=0)
     self.triangles = make_cube()
     self.trajectory = Trajectory([(0, 0, 0)] * q.m)
     self.mesh = Mesh(self.triangles, self.trajectory)
Пример #13
0
def make_complex_trajectory_sequence(args):
    edge = 20
    x = np.linspace(0, args.n / 2 - args.n / 4 - edge - 5, num=10)
    y = z = np.zeros(x.shape)
    # Move along x axis
    traj_x = Trajectory(zip(x, y, z) * args.ps,
                        velocity=args.ps / q.s,
                        pixel_size=args.ps)
    # Move along y axis
    traj_y = Trajectory(zip(y, x, z) * args.ps,
                        velocity=args.ps / q.s,
                        pixel_size=args.ps)
    # Move along both x and y axes
    traj_xy = Trajectory(zip(x, x, z) * args.ps,
                         velocity=args.ps / q.s,
                         pixel_size=args.ps)
    # Circular trajectory of the composite body rotates around the image center and with radius
    # n / 4 pixels.
    circle = args.n / 2 * args.ps + make_circle(
    ).magnitude * args.n / 4 * args.ps
    traj_circle = Trajectory(circle,
                             velocity=args.ps / q.s,
                             pixel_size=args.ps)
    # Make the trajectory of the circle the same duration as the simple linear one.
    traj_circle = Trajectory(circle,
                             velocity=traj_circle.length / traj_xy.length *
                             args.ps / q.s)
    # three cubes in the same height and depth, shifted only along the x axis.
    traj_stationary = Trajectory([(0, 0, 0)] * args.ps)
    traj_stationary_1 = Trajectory([(-2 * edge, 0, 0)] * args.ps)
    traj_stationary_2 = Trajectory([(2 * edge, 0, 0)] * args.ps)

    cube = make_cube() / q.m * edge * args.ps
    # The cubes are elongated along y axis.
    cube[::2, :] /= 3

    mesh = Mesh(cube, traj_x, orientation=geom.Y_AX)
    mesh_2 = Mesh(cube, traj_y, orientation=geom.Y_AX)
    mesh_3 = Mesh(cube, traj_xy, orientation=geom.Y_AX)
    mesh_stationary = Mesh(cube, traj_stationary, orientation=geom.Y_AX)
    mesh_stationary_1 = Mesh(cube, traj_stationary_1, orientation=geom.Y_AX)
    mesh_stationary_2 = Mesh(cube, traj_stationary_2, orientation=geom.Y_AX)
    bodies = [
        mesh, mesh_2, mesh_3, mesh_stationary, mesh_stationary_1,
        mesh_stationary_2
    ]
    composite = CompositeBody(traj_circle,
                              bodies=bodies,
                              orientation=geom.Y_AX)
    composite.bind_trajectory(args.ps)

    total_time = composite.time
    if args.t is None:
        times = np.linspace(0, 1, 100)
    else:
        if args.t < 0 or args.t > 1:
            raise ValueError('--t must be in the range [0, 1]')
        times = [args.t]

    im = None
    for index, i in enumerate(times):
        t = i * total_time
        composite.clear_transformation()
        composite.move(t)
        p = composite.project(args.shape, args.ps).get()
        if im is None:
            im = show(p, title='Projection')
        else:
            im.set_data(p)
            plt.draw()

    plt.show()
Пример #14
0
 def setUp(self):
     syris.init(device_index=0)
     self.triangles = make_cube()
     self.trajectory = Trajectory([(0, 0, 0)] * q.m)
     self.mesh = Mesh(self.triangles, self.trajectory)