Exemple #1
0
def DimensionCalculator(url):
    dimension = 0
    ij = imagej.init('./Imagej/Fiji.app', headless=False)  # Start ImageJ from local file

    image = ij.io().open(url)
    ij.ui().show(image)
    macro = """
        run("8-bit");
        setAutoThreshold("Default dark");
        //run("Threshold...");
        //setThreshold(128, 255);
        setAutoThreshold("Default dark");
        setOption("BlackBackground", true);
        run("Convert to Mask", "method=Default background=Dark calculate black");
        run("Fractal Box Count...", "box=2,3,4,6,8,12,16,32,64");
        saveAs("Results","./Results/ResultsBC.csv");
        run("Close")
        run("Close")
        run("Close")
        """
    ij.py.run_macro(macro)

    # reading results from saved csv file
    with open('./Results/ResultsBC.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        line_count = 0
        for row in csv_reader:
            if line_count == 0:
                line_count += 1
            else:
                dimension = row[10]
                line_count += 1
    #print(f'Dimension is {dimension}, source image: {url}')
    return dimension
Exemple #2
0
def stitch_all_samples(config, parallelize=0):
    """
    """
    # TODO: Fill in docstring

    workspace_directory = config["workspace_directory"]
    input_directory = Path(workspace_directory, "unstitched")
    output_directory = Path(workspace_directory, "stitched")
    output_directory.mkdir(exist_ok=True)

    samples = config["samples"]
    path_to_fiji = config["path_to_fiji"]

    # TODO: Figure out if it's possible to parallelize by sample here.

    imagej_instance = imagej.init(path_to_fiji)

    if parallelize > 1:
        num_processes = mp.cpu_count()
        processes = []
        for sample in samples:
            process = mp.Process(target=stitch_single_sample,
                                 args=(sample, input_directory,
                                       output_directory, imagej_instance,
                                       parallelize - 1))
            process.start()
            processes.append(process)

        for process in processes:
            process.join()
    else:
        for sample in samples:
            stitch_single_sample(sample, input_directory, output_directory,
                                 imagej_instance, parallelize)
Exemple #3
0
def analyze_connection(in_dir, out_dir, in_filename='0000.tif', verbose=False):
    # unet = UNet(inifile='setting.ini')
    # ds = dataset.Dataset()
    os.makedirs(out_dir, exist_ok=True)

    # for windows
    fiji_path = os.getenv('FIJI_PATH',
                          r'C:\Users\tamaki\Downloads\fiji-win64\Fiji.app')
    if not os.path.exists(fiji_path):
        # for mac
        fiji_path = '/Users/tamaki/Research/isu/work/Fiji.app'

    ij = imagej.init(fiji_path)  # taking long time in first time

    if verbose:
        print('fiji imagej version : {}'.format(ij.getVersion()))

    ij.batchmode = True

    #ij.ui().showUI()
    # if not os.path.exists(unet.output_path):
    #     os.mkdir(unet.output_path)

    from imagej_reader import Process3DOC

    sample_path = os.path.join(in_dir, in_filename)
    savedir = out_dir
    savename = 'aaa'
    Process3DOC(ij, sample_path, savedir, savename)

    if verbose:
        print('completed')
Exemple #4
0
    def __init__(
        self,
        log_file='./utils/polus-imagej-util/full.log',
        log_template='./utils/polus-imagej-util/classes/logtemplates/mainlog.txt'
    ):
        """A method to instantiate a class Populate object
        
        Args:
            log_file: A str representing the path to the log file.
            log_template: A str representing the path to a txt file which is 
                used as the log header. This file should explain the format of 
                the final log file.
            
        Raises:
            None
        
        """

        # Instantiate the imagej instance
        self._ij = imagej.init(
            'sc.fiji:fiji:2.1.1+net.imagej:imagej-legacy:0.37.4',
            headless=True)

        # Store the log output file and log template file path
        self.log_file = log_file
        self.log_template = log_template

        # Create dictionary to store all plugins
        self._plugins = {}

        # Create logger for class member
        self.__logger(self.log_file, self.log_template)

        # Create imagej plug in by calling the parser member method
        self._parser()
Exemple #5
0
def ij(request):
    ij_dir = request.config.getoption('--ij')
    headless = bool(request.config.getoption('--headless'))

    ij_wrapper = imagej.init(ij_dir)

    return ij_wrapper
 def ijstitch(chdf, infile):
     #conda create -n imagej -c conda-forge openjdk=8 pyimagej
     #conda install -c conda-forge maven
     #conda install -c conda-forge pyjnius
     #conda install -c conda-forge imglyb
     import imagej
     ij = imagej.init(os.path.expanduser('~/utils/Fiji.app/'),
                      new_instance=True)
     #ij = imagej.init('sc.fiji:fiji',new_instance=True)
     plugin = 'Grid/Collection stitching'
     args = {
         'type': '[Positions from file]',
         'order': '[Defined by TileConfiguration]',
         'directory': '[]',
         'layout_file': infile,
         'regression_threshold': '0.0001',
         'fusion_method': '[Linear Blending]',
         'max/avg_displacement_threshold': '0.20',
         'absolute_displacement_threshold': '0.30',
         'subpixel_accuracy': True,
         #'downsample_tiles':True,'x':'.25', 'y':'.25','interpolation':'Bicubic average',"width":'512','height':'384','outputFile':infile+'.registered.txt',
         'compute_overlap': True,
         'use_virtual_input_images': True,
         'computation_parameters': '[Save memory (but be slower)]',
         'image_output': '[Fuse and display]'
     }
     #ij.py.run_plugin(plugin, args,ij1_style=False)
     #ij.py.run_macro('run("Grid/Collection stitching", "type=[Positions from file] order=[Defined by TileConfiguration] directory=[] layout_file=/home/mt/tmp/HOECHST.stitchy fusion_method=Average regression_threshold=0 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 compute_overlap subpixel_accuracy downsample_tiles use_virtual_input_images computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display] x=.25 y=.25 width=512 height=384 interpolation=Bicubic average");\n')
     #ij.py.run_script(language='js',script='importClass(Packages.ij.IJ);IJ.run("Grid/Collection stitching", "type=[Positions from file] order=[Defined by TileConfiguration] directory=[] layout_file=/home/mt/tmp/HOECHST.stitchy fusion_method=[Linear Blending] regression_threshold=.01 max/avg_displacement_threshold=.250 absolute_displacement_threshold=.35 compute_overlap subpixel_accuracy downsample_tiles use_virtual_input_images computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display] x=.2 y=.2 width=410 height=307 interpolation=Bicubic average");')
     ij.getContext().dispose()
Exemple #7
0
    def __init__(self, headless=False, version="2.1.0", plugins=[]) -> None:
        # concatenade version plus plugins
        build = [version] + plugins if len(plugins) > 0 else version

        self._ij = imagej.init(build, headless=headless)
        if not headless: self._ij.ui().showUI()
        super().__init__()
Exemple #8
0
    def ij_instance(self, q_in: mp.Queue, q_out: mp.Queue):
        """
        Function to be spawned in parallel with separate processes.
        Initializes ImageJ and feed in macro code with file names from multiprocessing.Queue.

        :param q_in: Input file names
        :param q_out: Mainly to track progress
        """
        # Use latest Fiji as default.
        ij = imagej.init(
            'sc.fiji:fiji') if self.ij_path is None else imagej.init(
                self.ij_path.as_posix())
        q_out.put('ready')
        while q_in.qsize() > 0:
            file = q_in.get()
            ij.py.run_macro(self.build_macro(file))
            q_out.put(file)
Exemple #9
0
 def create(self):
     self.ij = imagej.init(self['fiji_directory'], headless=False)
     import jnius
     global GLVector, jArray, jFloat
     jArray = jnius.autoclass("java.lang.reflect.Array")
     jFloat = jnius.autoclass("java.lang.Float")
     GLVector = jnius.autoclass('cleargl.GLVector')
     self.ij.ui().showUI()
     return self.run().getOutput('sciView')
Exemple #10
0
def ij_fixture(request):
    """
    Create an ImageJ instance to be used by the whole testing environment
    :param request: Pytest variable passed in to fixtures
    :return: pyimageJ instance
    """
    ij_dir = request.config.getoption('--ij')
    headless = request.config.getoption('--headless')

    ij_wrapper = imagej.init(ij_dir, headless=headless)

    return ij_wrapper
Exemple #11
0
def main():
    unet = UNet(inifile='setting.ini')
    ds = dataset.Dataset()

    fiji_path = os.path.join(unet.fiji_dir)
    ij = imagej.init(fiji_path, headless=False)
    print(ij.getVersion())
    ij.batchmode = True

    if not os.path.exists(unet.output_path):
        os.mkdir(unet.output_path)

    Process3DOC(ij, unet)
Exemple #12
0
def ij_fixture(request):
    """
    Create an ImageJ instance to be used by the whole testing environment
    :param request: Pytest variable passed in to fixtures
    """
    ij_dir = request.config.getoption("--ij")
    legacy = request.config.getoption("--legacy")
    headless = request.config.getoption("--headless")

    mode = "headless" if headless else "interactive"
    ij_wrapper = imagej.init(ij_dir, mode=mode, add_legacy=legacy)

    yield ij_wrapper

    ij_wrapper.dispose()
Exemple #13
0
import argparse
import sys
import unittest
import imagej

if "--ij" in sys.argv:
    parser = argparse.ArgumentParser()
    parser.add_argument('--ij',
                        default='/Applications/Fiji.app',
                        help="set ij_dir")
    args = parser.parse_args()
    print("set ij_dir to " + args.ij)
    ij_dir = args.ij
    ij = imagej.init(ij_dir)
    sys.argv = sys.argv[2:]
else:
    ij_dir = None  # Use newest release version, downloaded from Maven.
    ij = imagej.init(ij_dir)

from jnius import autoclass
import numpy as np


class TestImageJ(unittest.TestCase):
    def testFrangi(self):
        input_array = np.array([[1000, 1000, 1000, 2000, 3000],
                                [5000, 8000, 13000, 21000, 34000]])
        result = np.zeros(input_array.shape)
        ij.op().filter().frangiVesselness(ij.py.to_java(result),
                                          ij.py.to_java(input_array), [1, 1],
                                          4)
import numpy as np
import multiprocessing
import shutil
import pandas as pd
from StyleFrame import StyleFrame, Styler

import sites_of_interest_parser as sip
import os

import imagej

# Using lower level ImageJ APIs to do the stitching. Avoiding calling ImageJ1 plugins allows to use a maven
# distribution of ImageJ and makes this parallelizable as no temporary files are written and read anymore.
# See here: https://github.com/imagej/pyimagej/issues/35
# https://forum.image.sc/t/using-imagej-functions-like-type-conversion-and-setting-pixel-size-via-pyimagej/25755/10
ij = imagej.init('sc.fiji:fiji:2.0.0-pre-10+ch.fmi:faim-ij2-visiview-processing:0.0.1')


class Stitcher:
    """ Stitches Talos images based on csv files containing the necessary information

    This class handles the stitching of Talos images based on a csv file like the one created by the MapsXmlParser.
    It creates the necessary folders, calls MapsXmlParser for the parsing and saving of the necessary metadata and then
    manages the stitching of all annotations. Current implementation of stitching is hard-coded to stitch_radius = 1,
    the size of the Talos images and an overlap of 10% in its stitching parameters in the stitch_annotated_tiles
    function

    Args:
        base_path (str): Path (as a string) to the directory containing the project_name folder.
        project_name (str): Name of the directory containing the MAPSProject.xml file and the LayersData
            folder of the MAPS project. Will be used as the location for the output folders. Must be in base_path folder
Exemple #15
0
import os
os.environ[
    'PYJNIUS_JAR'] = r'C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\share\pyjnius\pyjnius.jar'
os.environ[
    'PATH'] = r'C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Library\jre\bin\server;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Library\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\install\Library\jre\bin\server;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\install\Library\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Library\jre\bin\server;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Library\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Library\mingw-w64\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Library\usr\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Library\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Scripts;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\Library\mingw-w64\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\Library\usr\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\Library\bin;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\Scripts;C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\MATLAB\R2017a\runtime\win64;C:\Program Files\MATLAB\R2017a\bin;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\apache-maven-3.3.9\bin;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\AMD\ATI.ACE\Core-Static;C:\Program Files (x86)\Aperio\Common;C:\Program Files\PuTTY\;C:\Program Files\Java\jdk1.8.0_91\bin\;C:\Program Files (x86)\Google\Chrome\Application;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MATLAB\R2016a\runtime\win64;C:\Program Files\MATLAB\R2016a\bin;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Users\mpinkert\AppData\Local\Programs\Git\cmd;C:\Users\mpinkert\AppData\Local\Microsoft\WindowsApps;C:\Users\mpinkert\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64'
os.environ['JAVA_HOME'] = r'C:\Program Files\Java\jdk1.8.0_91'
os.environ[
    'JDK_HOME'] = r'C:\Users\mpinkert\AppData\Local\Continuum\anaconda3\envs\pyimagej\Library'
import imagej
ij = imagej.init(r'C:\Users\mpinkert\Desktop\Fiji.app')

from jnius import autoclass, JavaException, cast


macro = r"""
run("Grid/Collection stitching", "type=[Grid: row-by-row] order=[Right & Down                ]""" +\
        r""" grid_size_x=3 grid_size_y=1 tile_overlap=27 first_file_index_i=0""" +\
        r""" directory=F:\\Research\\LINK\\US\\2018-08-17\\ file_names=FirstImage3D_Overlap-27_{i}.tif""" +\
        r""" output_textfile_name=TileConfiguration.txt fusion_method=[Linear Blending]""" +\
        r""" regression_threshold=0.30 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50""" +\
        r""" computation_parameters=[Save memory (but be slower)] image_output=[Write to disk] """ + \
        r"""output_directory=[F:\\Research\\LINK\\US\\2018-08-17\\grid output]");
"""

try:
    test = ij.script().run('macro.ijm', macro, True).get()
except JavaException as e:
    print("e.classname    -- {}".format(e.classname))
    print("e.innermessage -- {}".format(e.innermessage))
    for st in e.stacktrace:
        print(st)
Exemple #16
0
        return ij.ResultsTable.getResultsTable()

    return None


def get_dataframe(table):
    """
    Convert results table to pandas Dataframe.
    """
    Table = sj.jimport('org.scijava.table.Table')
    sci_table = ij.convert().convert(table, Table)

    return ij.py.from_java(sci_table)


if __name__ == "__main__":
    # initialize imagej
    ij = imagej.init(mode='interactive')
    print(f"ImageJ version: {ij.getVersion()}")

    # load sample data and run blob detection
    img = ij.io().open('../sample-data/test_image.tif')
    img_xr = ij.py.from_java(img)
    detected_blobs = find_blobs(img_xr,
                                min_sigma=0.5,
                                max_sigma=3,
                                num_sigma=10,
                                threshold=0.0075)
    results_table = process(img, detected_blobs)
    df = get_dataframe(results_table)
    print(f"Output: \n{df}")
Exemple #17
0
def run(imagej_dir,
        segmentation_results_dir,
        ground_truth_binary_masks_dir,
        windows=False):
    ij = imagej.init(imagej_dir)
    path = Path(segmentation_results_dir)
    if (windows):
        base_directory = (str(path.parent))
    else:
        base_directory = '/' + (str(path.parent))
    Temp_dir = base_directory + '/Temp'
    if os.path.exists(Temp_dir):
        shutil.rmtree(Temp_dir)
    os.mkdir(Temp_dir)
    for root, dirs, files in os.walk(segmentation_results_dir, topdown=True):
        if (len(dirs) > 0):
            childdirs = dirs
    parentdir = [ground_truth_binary_masks_dir, segmentation_results_dir]
    childdirs_imgs = []
    print("iterating the segmentation results directories ...")
    for item in tqdm(childdirs):
        Test_directory = parentdir[
            1] + "/" + item + "/*.png"  # Specify labels directory
        files = glob.glob(Test_directory)
        X_data = []
        for myFile in files:
            image = io.imread(myFile)
            X_data.append(image)
        tmp = np.asarray(X_data, dtype='float32')
        if (np.max(tmp) > 1): tmp = tmp / 255.0
        childdirs_imgs.append(tmp)
        tmp = np.vstack((tmp, tmp))
        imsave(Temp_dir + '/' + item + '.tif', tmp)
    Ground_data = np.asarray(list(imread_collection(parentdir[0] + "/*.png")),
                             dtype='float32')
    Ground_dataTmp = np.vstack((Ground_data, Ground_data))
    zipdata = zip(childdirs, childdirs_imgs)
    mydict = dict(zipdata)
    if (np.max(Ground_data) > 1): Ground_data = Ground_data / 255.0
    if (np.max(Ground_dataTmp) > 1): Ground_dataTmp = Ground_dataTmp / 255.0
    imsave(Temp_dir + '/Ground.tif', Ground_dataTmp)
    Language_extension = "BeanShell"
    macroVRand = """
    import trainableSegmentation.metrics.*;
    #@output String VRand
    metric = new RandError( originalLabels, proposedLabels );
    maxThres = 1.0;
    maxScore = metric.getMaximalVRandAfterThinning( 0.0, maxThres, 0.1, true );  
    VRand = maxScore;
    """
    macroVInfo = """
    import trainableSegmentation.metrics.*;
    #@output String VInfo
    metric = new VariationOfInformation( originalLabels, proposedLabels );
    maxThres =1.0;
    maxScore = metric.getMaximalVInfoAfterThinning( 0.0, maxThres, 0.1 );  
    VInfo = maxScore;
    """
    current_path = Temp_dir
    if (windows):
        current_path = current_path.replace("\\", "/")
    VRandDf = []
    VInfoDf = []
    print("Retrieving Rand/Info metrics for the networks ...")
    for item in tqdm(childdirs):
        macroVRandTmp = (
            '\nimport ij.IJ;\noriginalLabels=IJ.openImage("' + current_path +
            '/' + 'Ground' + '.tif");' + '\nproposedLabels=IJ.openImage("' +
            current_path + '/' + item + '.tif");' +
            '\nimport trainableSegmentation.metrics.*;' +
            '\n#@output String VRand' +
            '\nmetric = new RandError( originalLabels, proposedLabels );' +
            '\nmaxThres = 1.0;' +
            '\nmaxScore = metric.getMaximalVRandAfterThinning( 0.0, maxThres, 0.1, true );'
            + '\nVRand = maxScore;')
        VRand = float(
            ij.py.run_script(Language_extension,
                             macroVRandTmp).getOutput('VRand'))
        VRandDf.append(VRand)
        macroVInfoTmp = (
            '\nimport ij.IJ;\noriginalLabels=IJ.openImage("' + current_path +
            '/' + 'Ground' + '.tif");' + '\nproposedLabels=IJ.openImage("' +
            current_path + '/' + item + '.tif");' +
            '\nimport trainableSegmentation.metrics.*;' +
            '\n#@output String VInfo' +
            '\nmetric = new VariationOfInformation( originalLabels, proposedLabels );'
            + '\nmaxThres = 1.0;' +
            '\nmaxScore = metric.getMaximalVInfoAfterThinning( 0.0, maxThres, 0.1 );'
            + '\nVInfo = maxScore;')
        VInfo = float(
            ij.py.run_script(Language_extension,
                             macroVInfoTmp).getOutput('VInfo'))
        VInfoDf.append(VInfo)
    print("Retrieving standard metrics for the segmentation results ...")
    Ground = Ground_data.flatten()
    Ground = (Ground > 0.5).astype(np.int_)
    F1score = []
    accuracy = []
    sensitivity = []
    specificity = []
    PPV = []
    NPV = []
    Jaccard = []
    for item in tqdm(mydict):
        itemTmp = mydict[item]
        itemTmp = (itemTmp > 0.5).astype(np.int_)
        itemTmp = itemTmp.flatten()
        tn, fp, fn, tp = confusion_matrix(Ground, itemTmp).ravel()
        accuracy.append((tp + tn) / (tp + tn + fp + fn))
        sn = tp / (tp + fn)
        sensitivity.append(sn)
        specificity.append(tn / (tn + fp))
        pr = tp / (tp + fp)
        PPV.append(pr)
        NPV.append(tn / (tn + fn))
        Jaccard.append(jaccard_score(Ground, itemTmp))
        F1score.append(2 * (pr * sn) / (pr + sn))
    MyDataFrameDict = {
        'Network': childdirs,
        'F1-score': F1score,
        'VRand': VRandDf,
        'VInfo': VInfoDf,
        'accuracy': accuracy,
        'sensitivity': sensitivity,
        'specificity': specificity,
        'PPV': PPV,
        'NPV': NPV,
        'Jaccard': Jaccard
    }
    df = pd.DataFrame(MyDataFrameDict,
                      index=childdirs,
                      columns=[
                          'F1-score', 'VRand', 'VInfo', 'accuracy',
                          'sensitivity', 'specificity', 'PPV', 'NPV', 'Jaccard'
                      ])
    df = df.round(decimals=5)
    cm = sns.diverging_palette(20, 130, n=len(childdirs), l=70, as_cmap=True)
    styles = [
        dict(props=[("font-family", "Times New Roman"), ("text-align",
                                                         "center")])
    ]
    pd.set_option('precision', 5)
    df.sort_index(axis=0, inplace=True)
    html = (df.style.set_table_styles(styles).background_gradient(cmap=cm))
    f = open("Dataframe_output.html", "w")
    f.write(html.render())
    f.close()
    # html.to_csv('results.csv')
    plt.figure(figsize=(6, 6), dpi=300)
    sns.set(font_scale=0.8, font="sans-serif")
    csfont = {'fontname': 'sans-serif'}
    heat_map = sns.heatmap(df, cmap="Spectral")
    plt.suptitle(
        "Heatmap of the segmentation methods relative to different metrics",
        fontsize=10,
        y=1)
    plt.xlabel("Metrics", fontsize=8, **csfont)
    plt.ylabel("Methods", fontsize=8, **csfont)
    plt.autoscale()
    plt.savefig('Heatmap_output.png', dpi=300)
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import multiscale.ultrasound.reconstruction as recon
import imagej
from pathlib import Path

ij = imagej.init('C:/users/mpinkert/Fiji.app/')

# stitch_dir = r'F:\Research\LINK' #
stitch_dir = r'C:\Users\mpinkert\Box\Research\LINK'

study_dir = 'Mouse images'
date = r'2019-10-21 - Mouse 210'
base_dir_list = ['L22 Fiducial Z16696']
#pl_path = Path(r'C:\Users\mpinkert\Box\Research\LINK\Phantom Trials\2019-05-04', '2019-05-04_US - 3X 100YSep.pos')
pl_path_base = Path(stitch_dir, study_dir, date)
runs = ['1']

for base_dir in base_dir_list:
        for run in runs:
                # pl_path = pl_path_base
                pl_path = Path(pl_path_base, base_dir + '.pos')
Exemple #19
0
def start_imagej_process(input_queue, output_queue, init_string):
    """Python script to run when starting a new ImageJ process.
    
    All commands are initiated by adding a dictionary with a {pyimagej_key_command} entry to the {input_queue}. This
    indicating which supported command should be executed. Some commands may take additional input, which is specified
    in the dictionary with {pyimagej_key_input}.

    Outputs are returned by adding a dictionary to the {output_queue} with the {pyimagej_key_output} key, or
    {pyimagej_key_error} if an error occurred during script execution.
    
    Supported commands
    ----------
    {pyimagej_cmd_script_parse} : parse the parameters from an imagej script. 
        inputs: script filename
        outputs: dictionary with mappings
            {pyimagej_script_parse_inputs} -> dictionary of input field name/value pairs
            {pyimagej_script_parse_outputs} -> dictionary of output field name/value pairs
    {pyimagej_cmd_script_run} : takes a set of named inputs from CellProfiler and runs the given imagej script
        inputs: dictionary with mappings
            {pyimagej_script_run_file_key} -> script filename
            {pyimagej_script_run_input_key} -> input parameter name/value dictionary
        outputs: dictionary containing output field name/value pairs
    {pyimagej_cmd_exit} : shut down the pyimagej daemon.
        inputs: none
        outputs: none
    
    Return values
    ----------
    {pyimagej_status_cmd_unknown} : unrecognized command, no further output is coming

    Parameters
    ----------
    input_queue : multiprocessing.Queue, required
        This Queue will be polled for input commands to run through ImageJ
    output_queue : multiprocessing.Queue, required
        This Queue will be filled with outputs to return to CellProfiler
    init_string : str, optional
        This can be a path to a local ImageJ installation, or an initialization string per imagej.init(),
        e.g. sc.fiji:fiji:2.1.0
    """

    ij = False

    try:
        if init_string:
            # Attempt to initialize with the given string
            ij = imagej.init(init_string)
        else:
            ij = imagej.init()
    except jpype.JException as ex:
        # Initialization failed
        output_queue.put(PYIMAGEJ_STATUS_STARTUP_FAILED)
        jpype.shutdownJVM()
        return

    script_service = ij.script()

    # Signify output is complete
    output_queue.put(PYIMAGEJ_STATUS_STARTUP_COMPLETE)

    # Main daemon loop, polling the input queue
    while True:
        command_dictionary = input_queue.get()
        cmd = command_dictionary[PYIMAGEJ_KEY_COMMAND]
        if cmd == PYIMAGEJ_CMD_SCRIPT_PARSE:
            script_path = command_dictionary[PYIMAGEJ_KEY_INPUT]
            script_file = Path(script_path)
            script_info = script_service.getScript(script_file)
            script_inputs = {}
            script_outputs = {}
            for script_in in script_info.inputs():
                script_inputs[str(script_in.getName())] = str(script_in.getType().toString())
            for script_out in script_info.outputs():
                script_outputs[str(script_out.getName())] = str(script_out.getType().toString())
            output_queue.put({PYIMAGEJ_SCRIPT_PARSE_INPUTS: script_inputs,
                              PYIMAGEJ_SCRIPT_PARSE_OUTPUTS: script_outputs})
        elif cmd == PYIMAGEJ_CMD_SCRIPT_RUN:
            script_path = (command_dictionary[PYIMAGEJ_KEY_INPUT])[PYIMAGEJ_SCRIPT_RUN_FILE_KEY]
            script_file = Path(script_path)
            input_map = (command_dictionary[PYIMAGEJ_KEY_INPUT])[PYIMAGEJ_SCRIPT_RUN_INPUT_KEY]
            convert_types = (command_dictionary[PYIMAGEJ_KEY_INPUT])[PYIMAGEJ_SCRIPT_RUN_CONVERT_IMAGES]
            preprocess_script_inputs(ij, input_map, convert_types)
            script_out_map = script_service.run(script_file, True, input_map).get().getOutputs()
            output_dict = {}
            for entry in script_out_map.entrySet():
                key = str(entry.getKey())
                value = convert_java_to_python_type(ij, entry.getValue())
                if value is not None:
                    output_dict[key] = value

            output_queue.put({PYIMAGEJ_KEY_OUTPUT: output_dict})
        elif cmd == PYIMAGEJ_CMD_EXIT:
            break
        else:
            output_queue.put({PYIMAGEJ_KEY_ERROR: PYIMAGEJ_STATUS_CMD_UNKNOWN})

    # Shut down the daemon
    ij.getContext().dispose()
    jpype.shutdownJVM()
Exemple #20
0
#import imglyb

#import numpy as np
#from skimage import data, io, filters
import imagej
ij = imagej.init('sc.fiji:fiji')
#ij=imagej.init()
print(f'this is fijis version: ij.getVersion()')

# Import an image with scikit-image.

# NB: Blood vessel image from: https://www.fi.edu/heart/blood-vessels
img = io.imread(
    'https://www.fi.edu/sites/fi.live.franklinds.webair.com/files/styles/featured_large/public/General_EduRes_Heart_BloodVessels_0.jpg'
)

img = np.mean(img, axis=2)

# Invoke ImageJ's Frangi vesselness op.
vessels = np.zeros(img.shape, dtype=img.dtype)
ij.op().filter().frangiVesselness(ij.py.to_java(vessels), ij.py.to_java(img),
                                  [1, 1], 20)

# macro='Run_QA_Portland2019'
# language_extension = 'ijm'
# result_script = ij.py.run_script(language_extension, macro)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 17 17:33:59 2020

@author: bene
"""
import numpy as np
import imagej

import imagej
ij = imagej.init('/Applications/Fiji.app')
#ij = imagej.init('sc.fiji:fiji:2.0.0-pre-10')
ij.getVersion()

#%%
macro = """
open("/Users/bene/Dropbox/Dokumente/Promotion/PROJECTS/STORMoChip/2020_01_13-EColi_TetraSpecs100_P20_Video_RAW_Fluct/E.Coli_2_my_raw_result-1.tif - drift corrected.tif");
run("Slice Keeper", "first=1 last=100 increment=1");

run("Estimate Drift", "time=1 max=50 reference=[first frame (default, better for fixed)] show_drift_plot choose=/Users/bene/Downloads/test.njt");
close();
close();
close();
run("Correct Drift", "choose=/Users/bene/Downloads/testDriftTable.njt");

run("SRRF Analysis", "ring=1 radiality_magnification=5 axes=6 frames_per_time-point=0 start=0 end=0 max=100 preferred=0");
saveAs("Tiff", "/Users/bene/Downloads/result.tif");
"""

result = ij.py.run_macro(macro)
Exemple #22
0
    import jpype
    import numpy as np
    from bfio import BioReader, BioWriter
    from pathlib import Path

    # Bioformats throws a debug message, disable the loci debugger to mute it
    def disable_loci_logs():
        DebugTools = scyjava.jimport("loci.common.DebugTools")
        DebugTools.setRootLevel("WARN")

    scyjava.when_jvm_starts(disable_loci_logs)

    print("Starting JVM...")

    # This is the version of ImageJ pre-downloaded into the docker container
    ij = imagej.init("sc.fiji:fiji:2.1.1+net.imagej:imagej-legacy:0.37.4",
                     headless=True)

    # ArrayImgs = scyjava.jimport('net.imglib2.img.array.ArrayImgs')
    # UnsafeUtil = scyjava.jimport('net.imglib2.img.basictypelongaccess.unsafe.UnsafeUtil')
    # Arrays = scyjava.jimport('java.util.Arrays')
    # OwningFloatUnsafe = scyjava.jimport('net.imglib2.img.basictypelongaccess.unsafe.owning.OwningFloatUnsafe')
    # Fraction = scyjava.jimport('net.imglib2.util.Fraction')
    # LongStream = scyjava.jimport('java.util.stream.LongStream')

    NUMPY_TYPES = {
        "uint8": (np.uint8, imglyb.types.UnsignedByteType),
        "byte": (np.int8, imglyb.types.ByteType),
        "uint16": (np.uint16, imglyb.types.UnsignedShortType),
        "short": (np.int16, imglyb.types.ShortType),
        "uint32": (np.uint32, imglyb.types.UnsignedIntType),
        "int": (np.int32, imglyb.types.IntType),
Exemple #23
0
def demo(args):

    available_cuda = torch.cuda.is_available()
    if not available_cuda:
        print('gpu not avaibable')
    if args.use_cuda and available_cuda:
        device = 'cuda:' + str(args.which_gpu)
        torch.cuda.set_device(args.which_gpu)
        print('use GPU')
    else:
        device = 'cpu'

    model = md.GeneratorUNet(3, 1)
    DICT_DIR = args.weights
    state_dict = torch.load(DICT_DIR, map_location=torch.device(device))

    if args.load_multiple_gpu_weights:
        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            name = k[7:]
            new_state_dict[name] = v
        model.load_state_dict(new_state_dict)
    else:
        model.load_state_dict(state_dict)

    model.eval()
    model.to(device)

    print('loading ImageJ, please wait')
    ij = imagej.init('fiji/Fiji.app/')

    # use for SHG
    TASK = args.input_folder
    INPUT_DIR = 'input_test_' + TASK
    INPUT_PATCH_DIR = 'input_patch_temp/'
    OUTPUT_DIR = 'output_test_' + TASK
    OUTPUT_PATCH_DIR = 'output_patch_temp/'
    CHANNEL_DIR = 'channel_temp/'
    if os.path.exists(CHANNEL_DIR): shutil.rmtree(CHANNEL_DIR)
    os.mkdir(CHANNEL_DIR)
    if os.path.exists(OUTPUT_DIR): shutil.rmtree(OUTPUT_DIR)
    os.mkdir(OUTPUT_DIR)
    files = [f for f in os.listdir(INPUT_DIR)]
    files.sort()
    window_shape = (128, 128, 3)
    step_size = 96
    is_crf = False
    for k, fn in enumerate(files):
        print('forward pass, please wait...')
        if os.path.exists(INPUT_PATCH_DIR): shutil.rmtree(INPUT_PATCH_DIR)
        if os.path.exists(OUTPUT_PATCH_DIR): shutil.rmtree(OUTPUT_PATCH_DIR)
        os.mkdir(INPUT_PATCH_DIR)
        os.mkdir(OUTPUT_PATCH_DIR)

        fs = os.path.join(INPUT_DIR, fn)
        img = io.imread(fs)

        shape_0_factor = np.ceil(img.shape[0] / window_shape[0])  # height
        shape_1_factor = np.ceil(img.shape[1] / window_shape[1])  # width
        canvas_0 = int(window_shape[0] * shape_0_factor)
        canvas_1 = int(window_shape[1] * shape_1_factor)
        pad_0 = canvas_0 - img.shape[0]
        pad_1 = canvas_1 - img.shape[1]
        canvas = pad(img, ((0, pad_0), (0, pad_1), (0, 0)), mode='reflect')
        windows = view_as_windows(canvas, window_shape, step_size)
        with open(OUTPUT_PATCH_DIR + 'TileConfiguration.txt',
                  'w') as text_file:
            print('dim = {}'.format(2), file=text_file)
            for i in range(0, windows.shape[1]):
                for j in range(0, windows.shape[0]):
                    patch = windows[j, i, ]
                    patch = np.squeeze(patch)
                    io.imsave(INPUT_PATCH_DIR + '/{}_{}.tiff'.format(j, i),
                              patch)
                    print('{}_{}.tiff; ; ({}, {})'.format(
                        j, i, i * step_size, j * step_size),
                          file=text_file)
        csv_file_path = generate_csv(INPUT_PATCH_DIR, os.getcwd())
        dataset = SynthDataset(csv_file=csv_file_path)
        dataloader = DataLoader(dataset,
                                batch_size=1,
                                shuffle=False,
                                num_workers=1)
        with open(csv_file_path) as f:
            reader = csv.reader(f)
            name_list = list(reader)
            with torch.no_grad():
                for iteration, batch in enumerate(dataloader):
                    input = batch['image'].float().to(device).view(
                        1, 3, window_shape[0], window_shape[1])
                    prediction = model(input)
                    for i in range(prediction.size(0)):
                        img_name = str(
                            name_list[iteration]).split('/')[-1].split('\'')[0]
                        result_patch = prediction[
                            i, :, :, :, ].cpu().detach().numpy().transpose(
                                (1, 2, 0)) * 255
                        result_patch = result_patch.astype(np.uint8)
                        io.imsave(OUTPUT_PATCH_DIR + img_name, result_patch)

        print('stitching, please wait...')
        os.makedirs(CHANNEL_DIR, exist_ok=True)
        params = {
            'type': 'Positions from file',
            'order': 'Defined by TileConfiguration',
            'directory': OUTPUT_PATCH_DIR,
            'ayout_file': 'TileConfiguration.txt',
            'fusion_method': 'Linear Blending',
            'regression_threshold': '0.30',
            'max/avg_displacement_threshold': '2.50',
            'absolute_displacement_threshold': '3.50',
            'compute_overlap': False,
            'computation_parameters':
            'Save computation time (but use more RAM)',
            'image_output': 'Write to disk',
            'output_directory': CHANNEL_DIR
        }
        plugin = "Grid/Collection stitching"
        ij.py.run_plugin(plugin, params)

        output_name = os.path.join(OUTPUT_DIR, fn)
        listOfChannels = [f for f in os.listdir(CHANNEL_DIR)]
        c1 = io.imread(os.path.join(CHANNEL_DIR, listOfChannels[0]))
        c1 = c1[:img.shape[0], :img.shape[1]]
        c1 = img_as_ubyte(c1)
        c1 = exposure.rescale_intensity(c1,
                                        in_range=args.intensity,
                                        out_range=(0, 255))
        c1 = exposure.rescale_intensity(c1,
                                        in_range=(0, 255),
                                        out_range=(0, 1))
        print(
            str(k + 1) + "/" + str(len(files)) + " output saved as: " +
            output_name)
        io.imsave(output_name, img_as_ubyte(c1))
        if args.pilot:
            break
Exemple #24
0
def main(
    _opName: str,
    _in1: Path,
    _sigma: str,
    _calibration: str,
    _out: Path,
) -> None:
    """Initialize ImageJ"""

    # Bioformats throws a debug message, disable the loci debugger to mute it
    def disable_loci_logs():
        DebugTools = scyjava.jimport("loci.common.DebugTools")
        DebugTools.setRootLevel("WARN")

    scyjava.when_jvm_starts(disable_loci_logs)

    # This is the version of ImageJ pre-downloaded into the docker container
    logger.info("Starting ImageJ...")
    ij = imagej.init("sc.fiji:fiji:2.1.1+net.imagej:imagej-legacy:0.37.4",
                     headless=True)
    # ij_converter.ij = ij
    logger.info("Loaded ImageJ version: {}".format(ij.getVersion()))
    """ Validate and organize the inputs """
    args = []
    argument_types = []
    arg_len = 0

    # Validate opName
    opName_values = [
        "DefaultTubeness",
    ]
    assert _opName in opName_values, "opName must be one of {}".format(
        opName_values)

    # Validate in1
    in1_types = {
        "DefaultTubeness": "RandomAccessibleInterval",
    }

    # Check that all inputs are specified
    if _in1 is None and _opName in list(in1_types.keys()):
        raise ValueError("{} must be defined to run {}.".format(
            "in1", _opName))
    elif _in1 != None:
        in1_type = in1_types[_opName]

        # switch to images folder if present
        if _in1.joinpath("images").is_dir():
            _in1 = _in1.joinpath("images").absolute()

        args.append([f for f in _in1.iterdir() if f.is_file()])
        arg_len = len(args[-1])
    else:
        argument_types.append(None)
        args.append([None])

    # Validate sigma
    sigma_types = {
        "DefaultTubeness": "double",
    }

    # Check that all inputs are specified
    if _sigma is None and _opName in list(sigma_types.keys()):
        raise ValueError("{} must be defined to run {}.".format(
            "sigma", _opName))
    else:
        sigma = None

    # Validate calibration
    calibration_types = {
        "DefaultTubeness": "double[]",
    }

    # Check that all inputs are specified
    if _calibration is None and _opName in list(calibration_types.keys()):
        raise ValueError("{} must be defined to run {}.".format(
            "calibration", _opName))
    else:
        calibration = None

    for i in range(len(args)):
        if len(args[i]) == 1:
            args[i] = args[i] * arg_len
    """ Set up the output """
    out_types = {
        "DefaultTubeness": "IterableInterval",
    }
    """ Run the plugin """
    try:
        for ind, (in1_path, ) in enumerate(zip(*args)):
            if in1_path != None:

                # Load the first plane of image in in1 collection
                logger.info("Processing image: {}".format(in1_path))
                in1_br = BioReader(in1_path)

                # Convert to appropriate numpy array
                in1 = ij_converter.to_java(ij,
                                           np.squeeze(in1_br[:, :, 0:1, 0, 0]),
                                           in1_type)
                metadata = in1_br.metadata
                fname = in1_path.name
                dtype = ij.py.dtype(in1)
            if _sigma is not None:
                sigma = ij_converter.to_java(ij, _sigma, sigma_types[_opName],
                                             dtype)

            if _calibration is not None:
                calibration = ij_converter.to_java(ij, _calibration,
                                                   calibration_types[_opName],
                                                   dtype)

            logger.info("Running op...")
            if _opName == "DefaultTubeness":
                out = ij.op().filter().tubeness(in1, sigma, calibration)

            logger.info("Completed op!")
            if in1_path != None:
                in1_br.close()

            # Saving output file to out
            logger.info("Saving...")
            out_array = ij_converter.from_java(ij, out, out_types[_opName])
            bw = BioWriter(_out.joinpath(fname), metadata=metadata)
            bw.Z = 1
            bw.dtype = out_array.dtype
            bw[:] = out_array.astype(bw.dtype)
            bw.close()

    except:
        logger.error("There was an error, shutting down jvm before raising...")
        raise

    finally:
        # Exit the program
        logger.info("Shutting down jvm...")
        del ij
        jpype.shutdownJVM()
        logger.info("Complete!")
def start_fiji():
    ij = imagej.init('/srv/conda/vnc/Fiji.app', headless=False)
    ij.getVersion()
    return ij
Exemple #26
0
import napari, sys

if len(sys.argv) <= 1:
    print('Please specify one or more images as arguments.')
    exit(1)

try:
    import imagej
except ImportError:
    raise ImportError("""This example uses ImageJ but pyimagej is not
    installed. To install try 'conda install pyimagej'.""")

with napari.gui_qt():

    print('--> Initializing imagej')
    ij = imagej.init('sc.fiji:fiji') # Fiji includes Bio-Formats.

    for i in range(1, len(sys.argv)):
        path = sys.argv[i]
        print('--> Reading {}'.format(path))

        dataset = ij.io().open(path)
        image = ij.py.from_java(dataset)

        if i == 1:
            viewer = napari.view_image(image)
        else:
            viewer.add_image(image)

    ij.getContext().dispose()
Exemple #27
0
# demo.py
#
# This example script show how to process image in the GPU via
# python, numpy, pyimagej and clij.
#
# Author: Robert Haase, [email protected]
#         August 2019
#######################################################3333

# init pyimage to get access to jar files
import imagej
ij = imagej.init('C:/programs/fiji-win64/Fiji.app/')

# load some image data
from skimage import io
sk_img = io.imread('https://samples.fiji.sc/blobs.png')

# init clijpy to get access to the GPU
from jnius import autoclass
CLIJx = autoclass('net.haesleinhuepf.clijx.CLIJx')
clijx = CLIJx.getInstance()

# convert and array to an ImageJ2 img:
import numpy as np
np_arr = np.array(sk_img)
ij_img = ij.py.to_java(np_arr)

# push the image to the GPU
input = clijx.push(ij_img)
# allocate memory for the result image
output = clijx.create(input)
Exemple #28
0
    parser.add_argument('--cols', help='Number of columns')
    parser.add_argument('--rows', help='Number of rows')
    parser.add_argument('--fijipath', help='Path to Fiji.app')
    parser.add_argument('--grid-type',
                        help='Stitching pattern',
                        default='snake-by-rows')
    parser.add_argument('--overlap',
                        help='Percent overlap in tiles',
                        type=int,
                        default=10)
    args, _ = parser.parse_known_args()
    grid = ' '.join(args.grid_type.split('-'))
    round_directories = glob.glob(args.source_paths)

    # ImageJ/Fiji setup
    ij = imagej.init(args.fijipath)
    IJM_EXTENSION = ".ijm"
    STITCHING_MACRO = """
	#@ String sourceDirectory
	#@ String outputDirectory
	#@ int overlap
	#@ String grid
	#@ int cols
	#@ int rows

	print(outputDirectory);
	function action(sourceDirectory, outputDirectory, genericFilename, filename, coordinateFilename) {
	run("Grid/Collection stitching", "type=[Grid: " + grid + "] order=[Right & Down ] grid_size_x=" + cols + " grid_size_y=" + rows + " tile_overlap=" + overlap + " first_file_index_i=0 directory=" + sourceDirectory + " file_names=" + genericFilename + " output_textfile_name=" + coordinateFilename + " fusion_method=[Linear Blending] regression_threshold=0.30 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 subpixel_accuracy compute_overlap computation_parameters=[Save computation time (but use more RAM)] image_output=[Fuse and display]");
	saveAs("png", outputDirectory + filename);
	close();
	}
Exemple #29
0
def main(argv):

    # parse directory name from command line argument
    # parse command line arguments
    parser = argparse.ArgumentParser(description="Process raw OPM data.")
    parser.add_argument("-i",
                        "--ipath",
                        type=str,
                        help="supply the directory to be processed")
    parser.add_argument("-d",
                        "--decon",
                        type=int,
                        default=0,
                        help="0: no deconvolution (DEFAULT), 1: deconvolution")
    parser.add_argument(
        "-f",
        "--flatfield",
        type=int,
        default=0,
        help=
        "0: No flat field (DEFAULT), 1: flat field (FIJI) 2: flat field (python)"
    )
    parser.add_argument(
        "-s",
        "--save_type",
        type=int,
        default=1,
        help="0: TIFF stack output, 1: BDV output (DEFAULT), 2: Zarr output")
    parser.add_argument(
        "-z",
        "--z_down_sample",
        type=int,
        default=1,
        help="1: No downsampling (DEFAULT), n: Nx downsampling")
    args = parser.parse_args()

    input_dir_string = args.ipath
    decon_flag = args.decon
    flatfield_flag = args.flatfield
    save_type = args.save_type
    z_down_sample = args.z_down_sample

    # https://docs.python.org/3/library/pathlib.html
    # Create Path object to directory
    input_dir_path = Path(input_dir_string)

    # create parameter array from scan parameters saved by acquisition code
    df_metadata = data_io.read_metadata(input_dir_path /
                                        Path('scan_metadata.csv'))
    root_name = df_metadata['root_name']
    scan_type = df_metadata['scan_type']
    theta = df_metadata['theta']
    scan_step = df_metadata['scan_step']
    pixel_size = df_metadata['pixel_size']
    num_t = df_metadata['num_t']
    num_y = df_metadata['num_y']
    num_z = df_metadata['num_z']
    num_ch = df_metadata['num_ch']
    num_images = df_metadata['scan_axis_positions']
    y_pixels = df_metadata['y_pixels']
    x_pixels = df_metadata['x_pixels']
    chan_405_active = df_metadata['405_active']
    chan_488_active = df_metadata['488_active']
    chan_561_active = df_metadata['561_active']
    chan_635_active = df_metadata['635_active']
    chan_730_active = df_metadata['730_active']
    active_channels = [
        chan_405_active, chan_488_active, chan_561_active, chan_635_active,
        chan_730_active
    ]
    channel_idxs = [0, 1, 2, 3, 4]
    channels_in_data = list(compress(channel_idxs, active_channels))
    n_active_channels = len(channels_in_data)
    if not (num_ch == n_active_channels):
        print('Channel setup error. Check metatdata file and directory names.')
        sys.exit()

    # calculate pixel sizes of deskewed image in microns
    deskewed_x_pixel = pixel_size / 1000.
    deskewed_y_pixel = pixel_size / 1000.
    deskewed_z_pixel = pixel_size / 1000.
    print('Deskewed pixel sizes before downsampling (um). x=' +
          str(deskewed_x_pixel) + ', y=' + str(deskewed_y_pixel) + ', z=' +
          str(deskewed_z_pixel) + '.')

    # create output directory
    if decon_flag == 0 and flatfield_flag == 0:
        output_dir_path = input_dir_path / 'deskew_output'
    elif decon_flag == 0 and flatfield_flag > 0:
        output_dir_path = input_dir_path / 'deskew_flatfield_output'
    elif decon_flag == 1 and flatfield_flag == 0:
        output_dir_path = input_dir_path / 'deskew_decon_output'
    elif decon_flag == 1 and flatfield_flag > 1:
        output_dir_path = input_dir_path / 'deskew_flatfield_decon_output'
    output_dir_path.mkdir(parents=True, exist_ok=True)

    # Create TIFF if requested
    if (save_type == 0):
        # create directory for data type
        tiff_output_dir_path = output_dir_path / Path('tiff')
        tiff_output_dir_path.mkdir(parents=True, exist_ok=True)
    # Create BDV if requested
    elif (save_type == 1):
        # create directory for data type
        bdv_output_dir_path = output_dir_path / Path('bdv')
        bdv_output_dir_path.mkdir(parents=True, exist_ok=True)

        # https://github.com/nvladimus/npy2bdv
        # create BDV H5 file with sub-sampling for BigStitcher
        bdv_output_path = bdv_output_dir_path / Path(root_name + '_bdv.h5')
        bdv_writer = npy2bdv.BdvWriter(str(bdv_output_path),
                                       nchannels=num_ch,
                                       ntiles=num_y * num_z,
                                       subsamp=((1, 1, 1), (4, 8, 8), (8, 16,
                                                                       16)),
                                       blockdim=((32, 128, 128), ),
                                       compression=None)

        # create blank affine transformation to use for stage translation
        unit_matrix = np.array((
            (1.0, 0.0, 0.0, 0.0),  # change the 4. value for x_translation (px)
            (0.0, 1.0, 0.0, 0.0),  # change the 4. value for y_translation (px)
            (0.0, 0.0, 1.0,
             0.0)))  # change the 4. value for z_translation (px)
    # Create Zarr if requested
    elif (save_type == 2):
        # create directory for data type
        zarr_output_dir_path = output_dir_path / Path('zarr')
        zarr_output_dir_path.mkdir(parents=True, exist_ok=True)

        # create name for zarr directory
        zarr_output_path = zarr_output_dir_path / Path(root_name +
                                                       '_zarr.zarr')

        # calculate size of one volume
        # change step size from physical space (nm) to camera space (pixels)
        pixel_step = scan_step / pixel_size  # (pixels)

        # calculate the number of pixels scanned during stage scan
        scan_end = num_images * pixel_step  # (pixels)

        # calculate properties for final image
        ny = np.int64(
            np.ceil(scan_end +
                    y_pixels * np.cos(theta * np.pi / 180)))  # (pixels)
        nz = np.int64(np.ceil(y_pixels *
                              np.sin(theta * np.pi / 180)))  # (pixels)
        nx = np.int64(x_pixels)  # (pixels)

        # create and open zarr file
        root = zarr.open(str(zarr_output_path), mode="w")
        opm_data = root.zeros("opm_data",
                              shape=(num_t, num_y * num_z, num_ch, nz, ny, nx),
                              chunks=(1, 1, 1, 32, 128, 128),
                              dtype=np.uint16)
        root = zarr.open(str(zarr_output_path), mode="rw")
        opm_data = root["opm_data"]

    # if retrospective flatfield is requested, import and open pyimagej in interactive mode
    # because BaSiC flat-fielding plugin cannot run in headless mode
    if flatfield_flag == 1:
        from image_post_processing import manage_flat_field
        import imagej
        import scyjava

        scyjava.config.add_option('-Xmx12g')
        plugins_dir = Path('/home/dps/Fiji.app/plugins')
        scyjava.config.add_option(f'-Dplugins.dir={str(plugins_dir)}')
        ij_path = Path('/home/dps/Fiji.app')
        ij = imagej.init(str(ij_path), headless=False)
        ij.ui().showUI()
        print(
            'PyimageJ approach to flat fielding will be removed soon. Switch to GPU accelerated python BASIC code (-f 2).'
        )
    elif flatfield_flag == 2:
        from image_post_processing import manage_flat_field_py

    # if decon is requested, import microvolution wrapper
    if decon_flag == 1:
        from image_post_processing import mv_decon

    # initialize counters
    timepoints_in_data = list(range(num_t))
    y_tile_in_data = list(range(num_y))
    z_tile_in_data = list(range(num_z))
    ch_in_BDV = list(range(n_active_channels))
    tile_idx = 0

    # loop over all directories. Each directory will be placed as a "tile" into the BigStitcher file
    for (y_idx, z_idx) in product(y_tile_in_data, z_tile_in_data):
        for (t_idx, ch_BDV_idx) in product(timepoints_in_data, ch_in_BDV):

            ch_idx = channels_in_data[ch_BDV_idx]

            # open stage positions file
            stage_position_filename = Path('t' + str(t_idx).zfill(4) + '_y' +
                                           str(y_idx).zfill(4) + '_z' +
                                           str(z_idx).zfill(4) + '_ch' +
                                           str(ch_idx).zfill(4) +
                                           '_stage_positions.csv')
            stage_position_path = input_dir_path / stage_position_filename
            # check to see if stage poisition file exists yet
            while (not (stage_position_filename.exists())):
                time.sleep(60)

            df_stage_positions = data_io.read_metadata(stage_position_path)

            stage_x = np.round(float(df_stage_positions['stage_x']), 2)
            stage_y = np.round(float(df_stage_positions['stage_y']), 2)
            stage_z = np.round(float(df_stage_positions['stage_z']), 2)
            print('y tile ' + str(y_idx + 1) + ' of ' + str(num_y) +
                  '; z tile ' + str(z_idx + 1) + ' of ' + str(num_z) +
                  '; channel ' + str(ch_BDV_idx + 1) + ' of ' +
                  str(n_active_channels))
            print('Stage location (um): x=' + str(stage_x) + ', y=' +
                  str(stage_y) + ', z=' + str(stage_z) + '.')

            # construct directory name
            current_tile_dir_path = Path(root_name + '_t' +
                                         str(t_idx).zfill(4) + '_y' +
                                         str(y_idx).zfill(4) + '_z' +
                                         str(z_idx).zfill(4) + '_ch' +
                                         str(ch_idx).zfill(4) + '_1')
            tile_dir_path_to_load = input_dir_path / current_tile_dir_path

            # https://pycro-manager.readthedocs.io/en/latest/read_data.html
            dataset = Dataset(str(tile_dir_path_to_load))
            raw_data = data_io.return_data_numpy(dataset=dataset,
                                                 time_axis=None,
                                                 channel_axis=None,
                                                 num_images=num_images,
                                                 y_pixels=y_pixels,
                                                 x_pixels=x_pixels)

            # perform flat-fielding
            if flatfield_flag == 1:
                print('Flatfield.')
                corrected_stack = manage_flat_field(raw_data, ij)
            elif flatfield_flag == 2:
                corrected_stack = manage_flat_field_py(raw_data)
            else:
                corrected_stack = raw_data
            del raw_data

            # deskew
            print('Deskew.')
            deskewed = deskew(data=np.flipud(corrected_stack),
                              theta=theta,
                              distance=scan_step,
                              pixel_size=pixel_size)
            del corrected_stack

            # downsample in z due to oversampling when going from OPM to coverslip geometry
            if z_down_sample > 1:
                print('Downsample.')
                deskewed_downsample = block_reduce(deskewed,
                                                   block_size=(z_down_sample,
                                                               1, 1),
                                                   func=np.mean)
            else:
                deskewed_downsample = deskewed
            del deskewed

            # run deconvolution on deskewed image
            if decon_flag == 1:
                print('Deconvolve.')
                deskewed_downsample_decon = mv_decon(
                    deskewed_downsample, ch_idx, deskewed_y_pixel,
                    z_down_sample * deskewed_z_pixel)
            else:
                deskewed_downsample_decon = deskewed_downsample
            del deskewed_downsample

            # save deskewed image into TIFF stack
            if (save_type == 0):
                print('Write TIFF stack')
                tiff_filename = root_name + '_t' + str(t_idx).zfill(
                    3) + '_p' + str(tile_idx).zfill(4) + '_c' + str(
                        ch_idx).zfill(3) + '.tiff'
                tiff_output_path = tiff_output_dir_path / Path(tiff_filename)
                tifffile.imwrite(str(tiff_output_path),
                                 deskewed_downsample_decon,
                                 imagej=True,
                                 resolution=(1 / deskewed_x_pixel,
                                             1 / deskewed_y_pixel),
                                 metadata={
                                     'spacing':
                                     (z_down_sample * deskewed_z_pixel),
                                     'unit': 'um',
                                     'axes': 'ZYX'
                                 })

                metadata_filename = root_name + '_t' + str(t_idx).zfill(
                    3) + '_p' + str(tile_idx).zfill(4) + '_c' + str(
                        ch_idx).zfill(3) + '.csv'
                metadata_output_path = tiff_output_dir_path / Path(
                    metadata_filename)
                tiff_stage_metadata = [{
                    'stage_x': float(stage_x),
                    'stage_y': float(stage_y),
                    'stage_z': float(stage_z)
                }]
                data_io.write_metadata(tiff_stage_metadata[0],
                                       metadata_output_path)

            elif (save_type == 1):
                # create affine transformation for stage translation
                # swap x & y from instrument to BDV
                affine_matrix = unit_matrix
                affine_matrix[0, 3] = (stage_y) / (deskewed_y_pixel
                                                   )  # x-translation
                affine_matrix[1, 3] = (stage_x) / (deskewed_x_pixel
                                                   )  # y-translation
                affine_matrix[2, 3] = (-1 * stage_z) / (
                    z_down_sample * deskewed_z_pixel)  # z-translation

                # save tile in BDV H5 with actual stage positions
                print('Write into BDV H5.')
                bdv_writer.append_view(
                    deskewed_downsample_decon,
                    time=0,
                    channel=ch_BDV_idx,
                    tile=tile_idx,
                    voxel_size_xyz=(deskewed_x_pixel, deskewed_y_pixel,
                                    z_down_sample * deskewed_z_pixel),
                    voxel_units='um',
                    calibration=(1, 1, (z_down_sample * deskewed_z_pixel) /
                                 deskewed_y_pixel),
                    m_affine=affine_matrix,
                    name_affine='tile ' + str(tile_idx) + ' translation')

            elif (save_type == 2):
                print('Write data into Zarr container')
                opm_data[t_idx, tile_idx,
                         ch_BDV_idx, :, :, :] = deskewed_downsample_decon
                metadata_filename = root_name + '_t' + str(t_idx).zfill(
                    3) + '_p' + str(tile_idx).zfill(4) + '_c' + str(
                        ch_idx).zfill(3) + '.csv'
                metadata_output_path = zarr_output_dir_path / Path(
                    metadata_filename)
                zarr_stage_metadata = [{
                    'stage_x': float(stage_x),
                    'stage_y': float(stage_y),
                    'stage_z': float(stage_z)
                }]
                data_io.write_metadata(zarr_stage_metadata[0],
                                       metadata_output_path)

            # free up memory
            del deskewed_downsample_decon
            gc.collect()

        tile_idx = tile_idx + 1

    if (save_type == 2):
        # write BDV xml file
        # https://github.com/nvladimus/npy2bdv
        # bdv_writer.write_xml(ntimes=num_t)
        bdv_writer.write_xml()
        bdv_writer.close()

    # shut down pyimagej
    if (flatfield_flag == 1):
        ij.getContext().dispose()

    # exit
    print('Finished.')
    sys.exit()
# Create an ImageJ gateway with the newest available version of ImageJ.
import imagej
from skimage import io
import numpy as np
import matplotlib
# import cairocffi as cairo
# matplotlib.use('PS')

ij = imagej.init('/home2/kdean/Desktop/Applications/Fiji', headless=False)
print('ImageJ Version:' + str(ij.getVersion()))

# Load an image.
#image_url = 'https://samples.fiji.sc/new-lenna.jpg'

# jimage = ij.io().open('/archive/MIL/marciano/20210119_capillaryLooping/Control/fluospheres_sytox/210117/Cell1/max_pos1.jpg')
# print('Image Loaded')

# Convert the image from ImageJ to xarray, a package that adds
# labeled datasets to numpy (http://xarray.pydata.org/en/stable/).
# image = ij.py.from_java(jimage)

# Display the image (backed by matplotlib).
# ij.py.show(jimage, cmap='gray')

url = '/archive/MIL/marciano/20210119_capillaryLooping/Control/fluospheres_sytox/210117/Cell1/max_pos1.jpg'
img = io.imread(url)
# ij.py.show(img)

from matplotlib import pyplot as plt
plt.subplot(121)
plt.imshow(img)