def register(view_index,
             filepaths,
             modelclass,
             csv_dir,
             params,
             n_threads=0,
             workaround2487=True):
    # Work around jython bug https://bugs.jython.org/issue2487 , a race condition on type initialization
    if 0 == view_index and workaround2487:
        # bogus call for first two filepaths: single-threaded execution to initalize types
        register(view_index, {view_index: filepaths[view_index][0:2]},
                 modelclass,
                 "/tmp/",
                 params,
                 n_threads=1,
                 workaround2487=False)

    exe = newFixedThreadPool(n_threads)
    paths = filepaths[view_index]
    try:
        name = "matrices-view-%i" % view_index
        matrices = loadMatrices(name, csv_dir)
        if not matrices:
            matrices = computeForwardTransforms(paths, klb_loader,
                                                getCalibration, csv_dir, exe,
                                                modelclass, params)
            # Debug: print identity transforms
            identity_indices = [
                i for i, m in enumerate(matrices) if 1.0 == m[0]
                and 0.0 == m[1] and 0.0 == m[2] and 0.0 == m[3] and 0.0 == m[4]
                and 1.0 == m[5] and 0.0 == m[6] and 0.0 == m[7] and 0.0 == m[8]
                and 0.0 == m[9] and 1.0 == m[10] and 0.0 == m[11]
            ]
            syncPrint("View %i: identity matrices at [%s]" %
                      (view_index, ", ".join(
                          str(index) for index in identity_indices)))
            saveMatrices(name, matrices, csv_dir)
    finally:
        exe.shutdown()

    transforms = asBackwardConcatTransforms(matrices,
                                            transformclass=Translation3D)
    path_transforms = dict(izip(paths, transforms))
    registered_loader = RegisteredLoader(klb_loader, path_transforms)

    return Load.lazyStack(paths, registered_loader)
def align(filepaths, csvDir, params, paramsTileConfiguration):
    name = "matrices"
    matrices = loadMatrices(name, csvDir)
    if matrices:
        return matrices

    # Optimize
    tiles = makeLinkedTiles(filepaths, csvDir, params,
                            paramsTileConfiguration["n_adjacent"])
    tc = TileConfiguration()
    tc.addTiles(tiles)
    tc.fixTile(tiles[len(tiles) / 2])  # middle tile

    maxAllowedError = paramsTileConfiguration["maxAllowedError"]
    maxPlateauwidth = paramsTileConfiguration["maxPlateauwidth"]
    maxIterations = paramsTileConfiguration["maxIterations"]
    damp = paramsTileConfiguration["damp"]
    tc.optimizeSilentlyConcurrent(ErrorStatistic(maxPlateauwidth + 1),
                                  maxAllowedError, maxIterations,
                                  maxPlateauwidth, damp)

    # TODO problem: can fail when there are 0 inliers

    # Return model matrices as double[] arrays with 6 values
    matrices = []
    for tile in tiles:
        # BUG in TransformationModel2D.toMatrix   # TODO can be updated now, it's been fixed
        #a = nativeArray('d', [2, 3])
        #tile.getModel().toMatrix(a)
        #matrices.append(a[0] + a[1])
        # Instead:
        a = zeros(6, 'd')
        tile.getModel().toArray(a)
        matrices.append(array([a[0], a[2], a[4], a[1], a[3], a[5]], 'd'))

    saveMatrices(name, matrices,
                 csvDir)  # TODO check: saving correctly, now that it's 2D?

    return matrices
import sys
sys.path.append("/home/albert/lab/scripts/python/imagej/IsoView-GCaMP/")
from lib.registration import saveMatrices, loadMatrices
from jarray import array

matrices = [array((j + i for i in xrange(12)), 'd') for j in xrange(3)]

print matrices

saveMatrices("matrices", matrices, "/tmp/")

m = loadMatrices("matrices", "/tmp/")

print m
Example #4
0
# Triggers the whole alignment and ends by showing a virtual stack of the aligned sections.
# Crashware: can be restarted anytime, will resume from where it left off.
viewAligned(filepaths, csvDir, params, paramsSIFT, paramsTileConfiguration,
            dimensions, FinalInterval([x0, y0], [x1, y1]))

# When the alignment is good enough, then export as N5 by swapping "False" for "True" below:

if False:
    # Write the whole volume in N5 format
    name = srcDir.split('/')[-2]
    exportDir = os.path.join(tgtDir, "n5")
    # Export ROI:
    # x=864 y=264 width=15312 h=17424
    interval = FinalInterval([0, 0], [dimensions[0] - 1, dimensions[1] - 1])

    export8bitN5(
        filepaths,
        loadFn,
        dimensions,
        loadMatrices("matrices",
                     csvDir),  # expects matrices.csv file to exist already
        name,
        exportDir,
        interval,
        gzip_compression=
        0,  # Don't use compression: less than 5% gain, at considerable processing cost
        invert=True,
        CLAHE_params=[200, 256, 3.0],
        n5_threads=0,
        block_size=[256, 256, 64])  # ~4 MB per block