Ejemplo n.º 1
0
        {% endfor -%}
        {% for inp,val in cookiecutter._inputs|dictsort -%}
        {% for out,n in cookiecutter._outputs|dictsort -%}
        {% if val.type=="collection" and cookiecutter.use_bfio -%}
        # Loop through files in {{ inp }} image collection and process
        for i,f in enumerate({{ inp }}_files):
            # Load an image
            br = BioReader(Path({{ inp }}).joinpath(f))
            image = np.squeeze(br.read_image())

            # initialize the output
            out_image = np.zeros(image.shape,dtype=br._pix['type'])

            """ Do some math and science - you should replace this """
            logger.info('Processing image ({}/{}): {}'.format(i,len({{ inp }}_files),f))
            out_image = awesome_math_and_science_function(image)

            # Write the output
            bw = BioWriter(Path({{ out }}).joinpath(f),metadata=br.read_metadata())
            bw.write_image(np.reshape(out_image,(br.num_y(),br.num_x(),br.num_z(),1,1)))
        {%- endif %}{% endfor %}{% endfor %}
        
    finally:
        {%- if cookiecutter.use_bfio %}
        # Close the javabridge regardless of successful completion
        logger.info('Closing the javabridge')
        jutil.kill_vm()
        {%- endif %}
        
        # Exit the program
        sys.exit()
Ejemplo n.º 2
0
            # Modify the metadata to make sure channels are written correctly
            bw.num_c(len(paths))
            bw._metadata.image().Pixels.channel_count = bw.num_c()

            # Process the data in tiles
            threads = []
            count = 0
            total = bw.num_c() * bw.num_z() * \
                    (bw.num_x()//chunk_size + 1) * (bw.num_y()//chunk_size + 1)
            with ThreadPoolExecutor(cpu_count()) as executor:
                for c, file in enumerate(paths):
                    br = BioReader(file['file'])
                    C = [c]
                    first_image = True
                    for z in range(br.num_z()):
                        Z = [z, z + 1]
                        for x in range(0, br.num_x(), chunk_size):
                            X = [x, min([x + chunk_size, br.num_x()])]
                            for y in range(0, br.num_y(), chunk_size):
                                Y = [y, min([y + chunk_size, br.num_y()])]
                                threads.append(
                                    executor.submit(write_tile, br, bw, X, Y,
                                                    Z, C))

                                # Bioformats requires the first tile to be written
                                # before any other tile is written
                                if first_image or len(threads) == cpu_count():
                                    threads[0].result()
                                    del threads[0]
                                    first_image = False