Running BigStitcher plugin in pyimagej

Hi! I am trying to run the BigStitcher plugin through pyimagej using a macro. I am using a macro based off of the example provided in the BigStitcher documentation: https://imagej.net/BigStitcher_Headless

In particular, when I try to run the plugin with pyimagej, it prints

0
Cannot add scrollbars, it's not a Container but a GenericDialogPlus

and then just hangs for a while. However, when ImageJ through the command line, it runs fine.

STITCHING_MACRO = """
#@ String basePath
#@ String wellRound
#@ String fusePath

// define dataset
run("Define dataset ...",
    "define_dataset=[Automatic Loader (Bioformats based)]" +
    " project_filename=" + wellRound + ".xml path=" + basePath + wellRound + ".nd2 exclude=10" +
    " bioformats_series_are?=Tiles move_tiles_to_grid_(per_angle)?=[Do not move Tiles to Grid (use Metadata if available)]" +
    " how_to_load_images=[Re-save as multiresolution HDF5] dataset_save_path=" + basePath +
    " subsampling_factors=[{ {1,1,1}, {2,2,1} }]" +
    " hdf5_chunk_sizes=[{ {32,32,4}, {16,16,16} }]" +
    " timepoints_per_partition=1" +
    " setups_per_partition=0" +
    " use_deflate_compression" +
    " export_path=" + basePath + wellRound);

print(basePath);

// calculate pairwise shifts
run("Calculate pairwise shifts ...",
    "select=" + basePath + wellRound + ".xml" +
    " process_angle=[All angles]" +
    " process_channel=[All channels]" +
    " process_illumination=[All illuminations]" +
    " process_tile=[All tiles]" +
    " process_timepoint=[All Timepoints]" +
    " method=[Phase Correlation]" +
    " channels=[use Channel conf-405]" +
    " downsample_in_x=2" +
    " downsample_in_y=2" +
    " downsample_in_z=1");

// filter shifts
run("Filter pairwise shifts ...",
    "select=" + basePath + wellRound + ".xml" +
    " filter_by_link_quality" +
    " min_r=0.5" +
    " max_r=1" +
    " max_shift_in_x=0" +
    " max_shift_in_y=0" +
    " max_shift_in_z=0" +
    " max_displacement=0");

// Global optimize and apply
run("Optimize globally and apply shifts ...",
    "select=" + basePath + wellRound + ".xml" +
    " process_angle=[All angles]" +
    " process_channel=[All channels]" +
    " process_illumination=[All illuminations]" +
    " process_tile=[All tiles]" +
    " process_timepoint=[All Timepoints]" +
    " relative=2.500" +
    " absolute=3.500" +
    " global_optimization_strategy=[Two-Round using Metadata to align unconnected Tiles]" +
    " fix_group_0-0");

// fuse tiles
run("Fuse dataset ...",
    "select=" + basePath + wellRound + ".xml" +
    " process_angle=[All angles]" +
    " process_channel=[All channels]" +
    " process_illumination=[All illuminations]" +
    " process_tile=[All tiles]" +
    " process_timepoint=[All Timepoints]" +
    " bounding_box=[Currently Selected Views]" +
    " downsampling=1 pixel_type=[16-bit unsigned integer]" +
    " interpolation=[Linear Interpolation]" +
    " image=[Precompute Image]" +
    " interest_points_for_non_rigid=[-= Disable Non-Rigid =-]" +
    " blend preserve_original produce=[Each timepoint & channel]" +
    " fused_image=[Save as (compressed) TIFF stacks]" +
    " output_file_directory=" + fusePath +
    " filename_addition=" + wellRound);

// quit after we are finished
eval("script", "System.exit(0);");
"""

ij = imagej.init("/path/to/Fiji.app")
args = {
      'basePath': input_filepath,
      'wellRound': round_number,
      'fusePath': output_filepath
    }
ij.py.run_macro(STITCHING_MACRO, args)

As far as I can tell, it gets stuck before finishing the “Define dataset …” part, as it never gets to the print statement right after it. Any ideas on what the problem could be?

Thanks!

The error message makes me think that this is a problem with some GUI element opening in “headless” mode; maybe it’s related to this post:

Actually, I found this unresolved issue which is the same error:

The only thing is, it works for my case when I run it via the command line with the following command:

Fiji.app/ImageJ-linux64 --headless --console -macro STITCHING_MACRO.ijm {...arguments}

@StephanPreibisch I was wondering if you could help?

Hi @alam-shahul,

I tried replicating your error (using my local Fiji 2.0.0-rc-69/1.52p via pyimagej) and I indeed get the

 Cannot add scrollbars, it's not a Container but a GenericDialogPlus

However, the dataset definition works fine. I’ll also try the later steps.

Did some other error show up in your output?

Best,
David

Hi @hoerldavid,

Thanks for the response! I realize now that when I run the plugin via the command line, I also get the same warning about the scrollbars. However, after that message, the code proceeds and actually loads tiles from my .nd2 file. E.g. it prints something like this:

Tue Jul 07 08:29:44 UTC 2020: Reading image data from 'B2_Sst_Prox1_Flt1.nd2' [2048x2048x5 angle=0 ch=conf-640 illum=0 tp=0 tile=20 type=uint16 img=ArrayImg<UnsignedShortType>]
writing level 0
writing level 1
writing level 2
writing level 3

However, when I run it via pyimagej, the same macro just hangs after this scrollbars warning message and never proceeds.

Could it possibly be a memory issue (i.e. the pyimagej process is not allocated enough memory to load the tiles)?

@hoerldavid small update:

Unfortunately the ImageJ website is down, so I can’t access the documentation for BigStitcher, but I thought I would try the “Manual Loader (Bioformats based)” option to see if it changed anything. I don’t really know what parameters are required to make the manual loading correctly (again, I can’t access any documentation), but replacing the Automatic Loader with that option actually allowed the script to proceed past the “Define dataset…” command. Of course, it broke on the subsequent command because I don’t think I did the Manual Loader correctly. However, I’m even more confused what the problem is now.