Esempio n. 1
0
        def target():
            from gi.repository import Ufo

            if self._config:
                sched = Ufo.Scheduler(config=self._config)
            else:
                sched = Ufo.Scheduler()

            sched.run(self._graph)
Esempio n. 2
0
def measure_ufo(out_path, metric, axis, width, height):
    pm = Ufo.PluginManager()
    sched = Ufo.Scheduler()
    graph = Ufo.TaskGraph()
    input_path = 'data/measure.tif'

    image = make_input(width, height)
    tifffile.imsave(input_path, image)

    reader = pm.get_task('read')
    measure = pm.get_task('measure')
    output = Ufo.OutputTask()

    reader.props.path = input_path
    measure.props.axis = axis
    measure.props.metric = metric

    graph.connect_nodes(reader, measure)
    graph.connect_nodes(measure, output)

    sched.run(graph)

    buf = output.get_output_buffer()
    gpu_result = ufo.numpy.asarray(buf)
    write_image(out_path, gpu_result)
Esempio n. 3
0
    def __init__(self, graph, get_output=False, output_dims=2, scheduler=None, copy_inputs=False):
        self.output_tasks = []
        self.sched = scheduler if scheduler else Ufo.Scheduler()
        self._started = False
        self.copy_inputs = copy_inputs

        if isinstance(graph, Ufo.TaskGraph):
            self.graph = graph
            roots = self.graph.get_roots()
        elif isinstance(graph, Ufo.TaskNode):
            self.graph = Ufo.TaskGraph()
            roots = [graph]
        else:
            msg = 'graph is neither Ufo.TaskGraph nor Ufo.TaskNode'
            raise ValueError(msg)

        # Initialize inputs
        self.input_tasks = {}
        self.ufo_buffers = {}
        for root in roots:
            self.input_tasks[root] = []
            self.ufo_buffers[root] = []
            num_inputs = root.get_num_inputs()
            for i in range(num_inputs):
                self.input_tasks[root].append(Ufo.InputTask())
                self.ufo_buffers[root].append(None)
                self.graph.connect_nodes_full(self.input_tasks[root][i], root, i)

        if get_output:
            for i, leave in enumerate(self.graph.get_leaves()):
                self.output_tasks.append(Ufo.OutputTask())
                self.output_tasks[-1].props.num_dims = output_dims
                self.graph.connect_nodes(leave, self.output_tasks[-1])
Esempio n. 4
0
def compute_phase_spectrum_density(args):
    graph = Ufo.TaskGraph()
    scheduler = Ufo.Scheduler()

    build_power_spectrum_density_pipeline(args, graph, scheduler)
    # scheduler.run(graph) # runned inside for now
    duration = scheduler.props.time

    return duration
Esempio n. 5
0
def run_flat_correct(args):
    graph = Ufo.TaskGraph()
    sched = Ufo.Scheduler()
    pm = Ufo.PluginManager()

    out_task = get_writer(args)
    flat_task = create_flat_correct_pipeline(args, graph)
    graph.connect_nodes(flat_task, out_task)
    sched.run(graph)
Esempio n. 6
0
    def generate_partial(append=False):
        graph = Ufo.TaskGraph()
        sched = Ufo.Scheduler()

        args.output_append = append
        writer = get_writer(args)

        sinos = create_sinogram_pipeline(args, graph)
        graph.connect_nodes(sinos, writer)
        sched.run(graph)
Esempio n. 7
0
def run_flat_correct(args):
    graph = Ufo.TaskGraph()
    sched = Ufo.Scheduler()
    pm = Ufo.PluginManager()

    out_task = pm.get_task('write')
    out_task.props.filename = args.output
    flat_task = create_flat_correct_pipeline(args, graph)
    graph.connect_nodes(flat_task, out_task)
    sched.run(graph)
Esempio n. 8
0
def run_preprocessing(args):
    graph = Ufo.TaskGraph()
    sched = Ufo.Scheduler()
    pm = Ufo.PluginManager()

    out_task = get_writer(args)
    current = create_preprocessing_pipeline(args, graph)
    graph.connect_nodes(current, out_task)

    sched.run(graph)
Esempio n. 9
0
    def generate_partial(append=False):
        pm = Ufo.PluginManager()
        graph = Ufo.TaskGraph()
        sched = Ufo.Scheduler()

        writer = pm.get_task('write')
        writer.props.filename = args.output
        writer.props.append = append

        sinos = create_sinogram_pipeline(args, graph)
        graph.connect_nodes(sinos, writer)
        sched.run(graph)
Esempio n. 10
0
    def initialize(self):
        self.data    = self.get("DataStore").data()
        self.graph   = Ufo.TaskGraph()
        self.sched   = Ufo.Scheduler()

        manager = Ufo.PluginManager()
        self.read = manager.get_task('memory-in')
        self.sino = manager.get_task('transpose-projections')
        self.pad  = manager.get_task('pad')
        self.fft  = manager.get_task('fft')
        self.fltr = manager.get_task('filter')
        self.ifft = manager.get_task('ifft')
        self.bp   = manager.get_task('backproject')
        self.crop = manager.get_task('crop')
        self.write= manager.get_task('memory-out')
        self.LogInfo("initialized, UFO Reconstruction")
        return True
Esempio n. 11
0
def test_wrong_connection():
    from ufo import Read, FlatFieldCorrect, Write

    darks = Read()
    flats = Read()
    radios = Read()
    write = Write()
    ffc = FlatFieldCorrect()

    g = Ufo.TaskGraph()
    g.connect_nodes_full(radios.task, ffc.task, 0)
    g.connect_nodes_full(darks.task, ffc.task, 1)
    g.connect_nodes_full(flats.task, ffc.task, 0)
    g.connect_nodes(ffc.task, write.task)

    sched = Ufo.Scheduler()
    sched.run(g)
Esempio n. 12
0
def ufo_dfi(tomo, center, recon, theta, **kwargs):
    """
    Reconstruct object using UFO's Direct Fourier pipeline
    """
    import gi
    gi.require_version('Ufo', '0.0')
    from gi.repository import Ufo

    theta = theta[1] - theta[0]
    center = center[0]

    g = Ufo.TaskGraph()
    pm = Ufo.PluginManager()
    sched = Ufo.Scheduler()

    input_task = Ufo.InputTask()
    output_task = Ufo.OutputTask()
    pad = pm.get_task('zeropad')
    fft = pm.get_task('fft')
    ifft = pm.get_task('ifft')
    dfi = pm.get_task('dfi-sinc')
    swap_forward = pm.get_task('swap-quadrants')
    swap_backward = pm.get_task('swap-quadrants')

    pad.set_properties(oversampling=1, center_of_rotation=center)
    fft.set_properties(dimensions=1, auto_zeropadding=False)
    ifft.set_properties(dimensions=2)
    dfi.set_properties(angle_step=theta)

    g.connect_nodes(input_task, pad)
    g.connect_nodes(pad, fft)
    g.connect_nodes(fft, dfi)
    g.connect_nodes(dfi, swap_forward)
    g.connect_nodes(swap_forward, ifft)
    g.connect_nodes(ifft, swap_backward)
    g.connect_nodes(swap_backward, output_task)

    args = (input_task, output_task, tomo, recon)
    thread = threading.Thread(target=_process_data, args=args)
    thread.start()
    sched.run(g)
    thread.join()

    logger.info("UFO+DFI run time: {}s".format(sched.props.time))
Esempio n. 13
0
def transpose_ufo(width, height, path):
    pm = Ufo.PluginManager()
    graph = Ufo.TaskGraph()
    sched = Ufo.Scheduler()
    input_path = 'data/transpose.tif'

    image = make_input(width, height)
    tifffile.imsave(input_path, image)

    reader = pm.get_task('read')
    transpose = pm.get_task('transpose')
    writer = pm.get_task('write')

    reader.props.path = input_path
    writer.props.filename = path

    graph.connect_nodes(reader, transpose)
    graph.connect_nodes(transpose, writer)
    sched.run(graph)
Esempio n. 14
0
def test_broadcast():
    from ufo import Generate, Writer
    import glob

    with tempdir() as d:
        generate = Generate(number=5, width=512, height=512)
        write1 = Writer(filename=d.path('foo-%i.tif'))
        write2 = Writer(filename=d.path('bar-%i.tif'))

        g = Ufo.TaskGraph()
        g.connect_nodes(generate.task, write1.task)
        g.connect_nodes(generate.task, write2.task)

        sched = Ufo.Scheduler()
        sched.run(g)

        foos = glob.glob(d.path('foo-*'))
        bars = glob.glob(d.path('bar-*'))
        assert (len(foos) == 5)
        assert (len(bars) == 5)
Esempio n. 15
0
def ufo_fbp(tomo, center, recon, theta, **kwargs):
    """
    Reconstruct object using UFO's FBP pipeline
    """
    import gi
    gi.require_version('Ufo', '0.0')
    from gi.repository import Ufo

    width = tomo.shape[2]
    theta = theta[1] - theta[0]
    center = center[0]

    g = Ufo.TaskGraph()
    pm = Ufo.PluginManager()
    sched = Ufo.Scheduler()

    input_task = Ufo.InputTask()
    output_task = Ufo.OutputTask()
    fft = pm.get_task('fft')
    ifft = pm.get_task('ifft')
    fltr = pm.get_task('filter')
    backproject = pm.get_task('backproject')

    ifft.set_properties(crop_width=width)
    backproject.set_properties(axis_pos=center,
                               angle_step=theta,
                               angle_offset=np.pi)

    g.connect_nodes(input_task, fft)
    g.connect_nodes(fft, fltr)
    g.connect_nodes(fltr, ifft)
    g.connect_nodes(ifft, backproject)
    g.connect_nodes(backproject, output_task)

    args = (input_task, output_task, tomo, recon)
    thread = threading.Thread(target=_process_data, args=args)
    thread.start()
    sched.run(g)
    thread.join()

    logger.info("UFO+FBP run time: {}s".format(sched.props.time))
Esempio n. 16
0
    def run(self, **props):
        """
        Run the tasks.

        Args:
            props (dict): parameter dict used to create the scheduler.
        """

        sched = Ufo.Scheduler()
        sched.set_properties(**props)

        def run_scheduler():
            self.env.done = False
            sched.run(self.env.graph)
            self.env.done = True
            self.env.reset()

        self.thread = threading.Thread(target=run_scheduler)
        self.thread.start()
        self.env.started = True
        return self
Esempio n. 17
0
 def run_scheduler():
     sched = Ufo.Scheduler()
     sched.run(self.graph)
Esempio n. 18
0
def tomo(params):
    # Create reader and writer
    if params.projections and params.sinograms:
        raise RuntimeError("Cannot specify both --projections and --sinograms.")

    if params.projections is None and params.sinograms is None:
        reader, width, height = get_dummy_reader(params)
    else:
        if params.projections:
            reader, width, height = get_projection_reader(params)
        else:
            reader, width, height = get_sinogram_reader(params)

    axis = params.axis or width / 2.0

    if params.projections and params.resize:
        width /= params.resize
        height /= params.resize
        axis /= params.resize

    LOG.debug("Input dimensions: {}x{} pixels".format(width, height))

    writer = get_writer(pm, params)

    # Setup graph depending on the chosen method and input data
    g = Ufo.TaskGraph()

    if params.projections is not None:
        if params.number:
            count = len(range(params.start, params.start + params.number, params.step))
        else:
            count = len(get_filenames(params.projections))

        LOG.debug("Number of projections: {}".format(count))
        sino_output = get_task('transpose-projections', number=count)

        if params.darks and params.flats:
            g.connect_nodes(create_flat_correct_pipeline(params, g), sino_output)
        else:
            g.connect_nodes(reader, sino_output)

        if height:
            # Sinogram height is the one needed for further padding
            height = count
    else:
        sino_output = reader

    if params.method == 'fbp':
        fft = get_task('fft', dimensions=1)
        ifft = get_task('ifft', dimensions=1)
        fltr = get_task('filter', filter=params.projection_filter)
        bp = get_task('backproject', axis_pos=axis)

        if params.angle:
            bp.props.angle_step = params.angle

        if params.offset:
            bp.props.angle_offset = params.offset

        if width and height:
            # Pad the image with its extent to prevent reconstuction ring
            pad = get_task('pad')
            crop = get_task('crop')
            setup_padding(pad, crop, width, height, params.projection_padding_mode)

            LOG.debug("Padding input to: {}x{} pixels".format(pad.props.width, pad.props.height))

            g.connect_nodes(sino_output, pad)
            g.connect_nodes(pad, fft)
            g.connect_nodes(fft, fltr)
            g.connect_nodes(fltr, ifft)
            g.connect_nodes(ifft, crop)
            g.connect_nodes(crop, bp)
        else:
            if params.crop_width:
                ifft.props.crop_width = int(params.crop_width)
                LOG.debug("Cropping to {} pixels".format(ifft.props.crop_width))

            g.connect_nodes(sino_output, fft)
            g.connect_nodes(fft, fltr)
            g.connect_nodes(fltr, ifft)
            g.connect_nodes(ifft, bp)

        g.connect_nodes(bp, writer)

    if params.method in ('sart', 'sirt', 'sbtv', 'asdpocs'):
        projector = pm.get_task_from_package('ir', 'parallel-projector')
        projector.set_properties(model='joseph', is_forward=False)

        projector.set_properties(axis_position=axis)
        projector.set_properties(step=params.angle if params.angle else np.pi / 180.0)

        method = pm.get_task_from_package('ir', params.method)
        method.set_properties(projector=projector, num_iterations=params.num_iterations)

        if params.method in ('sart', 'sirt'):
            method.set_properties(relaxation_factor=params.relaxation_factor)

        if params.method == 'asdpocs':
            minimizer = pm.get_task_from_package('ir', 'sirt')
            method.set_properties(df_minimizer=minimizer)

        if params.method == 'sbtv':
            # FIXME: the lambda keyword is preventing from the following
            # assignment ...
            # method.props.lambda = params.lambda
            method.set_properties(mu=params.mu)

        g.connect_nodes(sino_output, method)
        g.connect_nodes(method, writer)

    if params.method == 'dfi':
        oversampling = params.oversampling or 1
        pad = get_task('zeropad', center_of_rotation=axis, oversampling=oversampling)
        fft = get_task('fft', dimensions=1, auto_zeropadding=0)
        dfi = get_task('dfi-sinc')
        ifft = get_task('ifft', dimensions=2)
        swap_forward = get_task('swap-quadrants')
        swap_backward = get_task('swap-quadrants')

        if params.angle:
            dfi.props.angle_step = params.angle

        g.connect_nodes(sino_output, pad)
        g.connect_nodes(pad, fft)
        g.connect_nodes(fft, dfi)
        g.connect_nodes(dfi, swap_forward)
        g.connect_nodes(swap_forward, ifft)
        g.connect_nodes(ifft, swap_backward)

        if width:
            crop = get_task('crop')
            crop.set_properties(from_center=True, width=width, height=width)
            g.connect_nodes(swap_backward, crop)
            g.connect_nodes(crop, writer)
        else:
            g.connect_nodes(swap_backward, writer)

    scheduler = Ufo.Scheduler()

    if hasattr(scheduler.props, 'enable_tracing'):
        LOG.debug("Use tracing: {}".format(params.enable_tracing))
        scheduler.props.enable_tracing = params.enable_tracing

    scheduler.run(g)
    duration = scheduler.props.time
    LOG.info("Execution time: {} s".format(duration))

    return duration
Esempio n. 19
0
from __future__ import absolute_import

import sys
import re
import time
import threading
import Queue as queue
import numpy as np
import gi
from .numpy import asarray, empty_like
gi.require_version('Ufo', '0.0')
from gi.repository import GObject, Ufo

Ufo.Scheduler()


class Environment(object):
    def __init__(self, pm):
        self.pm = pm
        self.reset()
        self.done = False
        self.started = False

    def reset(self):
        self.graph = Ufo.TaskGraph()


class Task(object):
    def __init__(self, env, name, cargs, task=None):
        self.env = env
        self.name = name