Пример #1
0
def plot(method, strength=None):
    # test distribution
    source = SampleImage(n=2 * rmax - 1).image / scale
    Isrc, _ = Ibeta(source)
    Inorm = (Isrc**2).sum()

    # simulated projection fith Poissonian noise
    proj, _ = rbasex_transform(source, direction='forward')
    proj[proj < 0] = 0
    proj = np.random.RandomState(0).poisson(proj)  # (reproducible, see NEP 19)

    # reconstructed image and intensity
    if strength is None:
        reg = method
    else:
        reg = (method, strength)
    im, distr = rbasex_transform(proj, reg=reg)
    I, _ = distr.Ibeta()

    # plot...
    fig = plt.figure(figsize=(7, 3.5), frameon=False)

    # image
    plt.subplot(121)

    fig = plt.imshow(rescaleI(im), vmin=-vlim, vmax=vlim, cmap='bwr')

    plt.axis('off')
    plt.text(0, 2 * rmax, method, va='top')

    # intensity
    ax = plt.subplot2grid((3, 2), (0, 1), rowspan=2)

    ax.plot(Isrc, c='r', lw=1)
    ax.plot(I, c='k', lw=1)

    ax.set_xlim((0, rmax))
    ax.set_ylim(Ilim)

    # error
    plt.subplot(326)

    plt.axhline(c='r', lw=1)
    plt.plot(I - Isrc, c='b', lw=1)

    plt.xlim((0, rmax))
    plt.ylim(dIlim)

    # finish
    plt.subplots_adjust(left=0,
                        right=0.97,
                        wspace=0.1,
                        bottom=0.08,
                        top=0.98,
                        hspace=0.5)
Пример #2
0
    def __init__(self,
                 n=[301, 501],
                 shape='half',
                 rmax='MIN',
                 order=2,
                 weight=['none', 'sin', 'sin+array'],
                 method='all',
                 repeat=1,
                 t_min=0.1):
        self.n = _ensure_list(n)

        if shape == 'Q':
            origin = 'll'
            self.shape = 'One quadrant'
        elif shape == 'half':
            origin = 'cl'
            self.shape = 'Half image'
        elif shape == 'full':
            origin = 'cc'
            self.shape = 'Full image'
        else:
            raise ValueError('Incorrect shape "{}"'.format(shape))

        self.rmaxs = rmaxs = _ensure_list(rmax)

        self.order = order

        weights = _ensure_list(weight)
        if 'all' in weights:
            weights = ['none', 'sin', 'array', 'sin+array']
        self.weights = weights

        methods = _ensure_list(method)
        if 'all' in methods:
            methods = ['nearest', 'linear', 'remap']
        self.methods = methods

        # create the timing function
        time = Timent(skip=-1, repeat=repeat, duration=t_min).time

        # dictionary for the results
        self.results = {
            m: {r: {w: []
                    for w in weights}
                for r in rmaxs}
            for m in methods
        }

        from abel.tools.vmi import Ibeta, Distributions
        # make sure that everything is loaded
        # (otherwise the 1st timing call is very slow)
        Ibeta(np.array([[0]]))

        # Loop over all image sizes
        for ni in self.n:
            ni = int(ni)

            # create image and weight array
            rows = (ni + 1) // 2 if shape == 'Q' else ni
            cols = (ni + 1) // 2 if shape in ['Q', 'half'] else ni
            IM = np.random.randn(rows, cols)
            warray = np.random.randn(rows, cols)

            # benchmark each combination of parameters
            for method in methods:
                for rmax in rmaxs:
                    for weight in weights:
                        if weight == 'none':
                            w = {'use_sin': False, 'weights': None}
                        elif weight == 'sin':
                            w = {'use_sin': True, 'weights': None}
                        elif weight == 'array':
                            w = {'use_sin': False, 'weights': warray}
                        elif weight == 'sin+array':
                            w = {'use_sin': True, 'weights': warray}
                        else:
                            raise ValueError(
                                'Incorrect weight "{}"'.format(weight))
                        # single-image
                        t1 = time(Ibeta,
                                  IM,
                                  origin,
                                  rmax,
                                  order,
                                  method=method,
                                  **w)
                        # cached
                        distr = Distributions(origin,
                                              rmax,
                                              order,
                                              method=method,
                                              **w)
                        distr(IM)  # trigger precalculations

                        def distrIMIbeta(IM):
                            return distr(IM).Ibeta()

                        tn = time(distrIMIbeta, IM)
                        # save results
                        self.results[method][rmax][weight].append(
                            (t1 * 1000, tn * 1000))
Пример #3
0
from __future__ import division, print_function

import numpy as np
import matplotlib.pyplot as plt

from abel.tools.analytical import SampleImage
from abel.tools.vmi import Ibeta
from abel.rbasex import rbasex_transform

# test distribution
rmax = 200
scale = 10000
source = SampleImage(n=2 * rmax - 1).image / scale
Isrc, _ = Ibeta(source)
Inorm = (Isrc**2).sum()

# simulated projection fith Poissonian noise
proj, _ = rbasex_transform(source, direction='forward')
proj[proj < 0] = 0
proj = np.random.RandomState(0).poisson(proj)  # (reproducible, see NEP 19)


# calculate relative RMS error for given regularization parameters
def rmse(method, strengths=None):
    if strengths is None:
        _, distr = rbasex_transform(proj, reg=method, out=None)
        I, _ = distr.Ibeta()
        return ((I - Isrc)**2).sum() / Inorm

    err = np.empty_like(strengths, dtype=float)
    for n, strength in enumerate(strengths):