Пример #1
0
def test_decon_context(deskewed_image, decon_image):
    """test that we can deconvolve an image using the context manager"""
    with RLContext(deskewed_image.shape, OTF_PATH, dzdata=0.3) as ctx:
        decon_result = rl_decon(deskewed_image,
                                output_shape=ctx.out_shape,
                                n_iters=10,
                                background=98)
    npt.assert_allclose(decon_result, decon_image, atol=ATOL)
Пример #2
0
 def test_decon(self):
     """
     test that we can deconvolve an image
     """
     rl_init(self.deskewed.shape, self.otf, **self.config)
     decon_result = rl_decon(self.deskewed, **self.config)
     self.assertTrue(np.allclose(decon_result, self.stored_decon))
     rl_cleanup()
Пример #3
0
 def test_decon_context(self):
     """
     test that we can deconvolve an image using the context manager
     """
     with RLContext(self.deskewed.shape, self.otf, **self.config) as ctx:
         decon_result = rl_decon(self.deskewed,
                                 output_shape=ctx.out_shape,
                                 **self.config)
     self.assertTrue(np.allclose(decon_result, self.stored_decon))
Пример #4
0
def deconvolve_all():
    """Main deconvolution function, where all logging, file exploration and
    processing is wrapped. Continuous references to external libraries...
    Many thanks to tlambert01 for the libs mrc and pyCUDAdecon
    """

    log("# Starting deconvolution")
    log("# GPU information")
    log(getGPUinfo())

    # Get the paths for each filter to be used, in the same order
    filter_paths = args.psf

    # Iterate over the list of files provided
    # Note that queuing can be done using a simple .sh script to call the
    # application with different parameters
    for f in args.source:

        # Log file naming
        logfile = "{}.log".format(f)

        # Start a timer for profiling time elapsed
        start = time.time()

        # Check whether source provided is SpinningDisk-like
        if os.path.isdir(f):
            channels = args.spinning
            log(
                "# Reconstructing and saving .tif file from folder structure with channels {}"
                .format(channels),
                logfile,
            )
            f = join_spinning(f, channels)

        # Store the file extension to save in the same format
        extension = os.path.splitext(f)[1]

        # Generate an output name based on the source name
        outname = os.path.basename(os.path.splitext(f)[0])

        # Load the image
        log("# Loading the file {} and creating log".format(f), logfile)
        im = Image(f)

        # Calculate an appropriate number of wavelengths to process
        nw = min(len(filter_paths), im.nw)

        # Create the header for the current file
        log("\n\n# Processing file {}".format(f))
        log("# Starting deconvolution at {}".format(
            time.strftime("%b %d %Y %H:%M:%S", time.localtime(start))))
        log("####################################################",
            logfile,
            show=False)
        log("deCU v1.0.5, by DLP (CABD), 2020", logfile, show=False)
        log("####################################################",
            logfile,
            show=False)
        log("\n# Deconvolution settings", logfile, show=False)
        log("## Projection {}".format(args.project), logfile, show=False)
        log("## Channels {}".format(im.nw), logfile, show=False)
        log("## File {}".format(f), logfile, show=False)
        log(
            "## XY pixel size {}".format(round(im.pxx, 3)),
            logfile,
            show=False,
        )
        log(
            "## Z-stack depth {}".format(round(im.pxz, 3)),
            logfile,
            show=False,
        )
        log("## GPU information", logfile, show=False)
        log(getGPUinfo(), logfile, show=False)
        log(
            "## Time start {}".format(
                time.strftime("%b %d %Y %H:%M:%S", time.localtime(start)), ),
            logfile,
            show=False,
        )
        log("\n# Deconvolution process steps", logfile, show=False)

        # Iterate over the range of wavelengths
        for w in range(0, nw):
            # Skip wavelengths marked as null
            if filter_paths[w] == "_":
                continue

            log(
                "\n## Deconvolving channel {} with PSF {}. {} frame{}".format(
                    w, filter_paths[w], im.nt, "s" if im.nt > 1 else ""),
                logfile,
            )

            # Provisional list to store multiframe images
            stack = []

            # Loading all libraries for the deconvolution process
            with TemporaryOTF(filter_paths[w]) as otf:
                with stdout_redirected(to=logfile):
                    with RLContext([im.nz, im.nx, im.ny], otf.path) as ctx:
                        # Iterate over all frames in the image
                        for t in tqdm(range(0, im.nt)):

                            # Using echo to save into the logfile the frame no.
                            os.system("echo ## Frame {}".format(t))

                            # Retrieve current frame
                            image = im.frame(t, w)

                            # Perform pyCUDAdecon deconvolution
                            result = rl_decon(
                                image,
                                output_shape=ctx.out_shape,
                                dxdata=im.pxx,
                                dxpsf=args.xyfilter,
                                dzdata=im.pxz,
                                dzpsf=args.zfilter,
                                wavelength=im.wavelengths[w],
                                n_iters=args.i,
                                na=args.na,
                                nimm=args.refractive,
                            )

                            # Convert the result to uint16 to preserve format
                            result = result.astype(np.uint16)

                            # Append the timepoint to the stack
                            stack.append(result)

                        # Generate a np array from the stack
                        imResult = np.array(stack)

            # Generate a definitive saving name
            savename = ("{0}_{1}_D3D{2}").format(outname,
                                                 str(im.wavelengths[w]),
                                                 extension)

            log("## Saving file as {}".format(savename), logfile)
            saveformat[extension](
                savename,
                imResult,
            )

            # Check whether max-projection has to occur
            # TODO implement more types of projection
            if args.project:
                log("## Creating maximum projection", logfile)
                imProjected = max_projection(imResult,
                                             minimum=args.planes[0],
                                             maximum=args.planes[1])
                savename = ("{0}_{1}_PRJ_D3D{2}").format(
                    outname, str(im.wavelengths[w]), extension)
                log("## Saving maximum projection as {}".format(savename),
                    logfile)
                saveformat[extension](
                    savename,
                    imProjected,
                )

        # Stop profiling timer and calculate time elapsed
        end = time.time()
        elapsed = end - start

        log(
            "# Deconvolution ended at {}, elapsed {}".format(
                time.strftime("%b %d %Y %H:%M:%S", time.localtime(end)),
                "{} m : {} s".format(int(elapsed / 60), int(elapsed % 60)),
            ),
            logfile,
        )
Пример #5
0
def test_decon(deskewed_image, decon_image):
    """test that we can deconvolve an image"""
    rl_init(deskewed_image.shape, OTF_PATH, dzdata=0.3)
    decon_result = rl_decon(deskewed_image, n_iters=10, background=98)
    npt.assert_allclose(decon_result, decon_image, atol=ATOL)
    rl_cleanup()