Exemple #1
0
def imread_bioformats(path):
    """Thin wrapper around bioformats, slow and shitty,
    but works in a pinch, only reads z stack -- no time"""
    # import modules tp ise
    import javabridge
    import bioformats
    import gc
    import itertools
    # start the jave VM with the appropriate classes loaded
    javabridge.start_vm(class_path=bioformats.JARS)
    # init the reader
    with bioformats.ImageReader(path) as reader:
        # init container
        data = []
        for i in itertool.count():
            try:
                data.append(reader.read(z=i, rescale=False))
            except javabridge.JavaException:
                # reached end of file, break
                break
    # clean up a little
    javabridge.kill_vm()
    gc.collect()
    # return ndarray
    return np.asarray(data)
Exemple #2
0
    def SubmitAction(self):
        roi_buff = [
            int(self.roi_xbuff_line.text()),
            int(self.roi_ybuff_line.text())
        ]
        analyzer = RecruitmentMovieAnalyzer()
        analyzer.SetParameters(protein_channel=self.prot_channel_line.text(),
                               nuclear_channel=self.nuc_channel_line.text(),
                               additional_rois=int(self.roi_spinbox.value()),
                               irrad_frame=int(self.irrad_frame_line.text()),
                               bleach_frames=int(self.irrad_frame_line.text()),
                               save_direct=self.out_direct_line.text(),
                               roi_buffer=roi_buff)
        if self.single_file_button.isChecked():
            analyzer.LoadFile(video_file=self.movie_file_line.text(),
                              roi_file=self.roi_file_line.text())
        else:
            analyzer.LoadDirectory(video_direct=self.batch_direct_line.text(),
                                   extension=self.movie_file_line.text(),
                                   roi_extension=self.roi_file_line.text())

        javabridge.start_vm(class_path=bioformats.JARS)
        t0 = time.time()
        #try:
        if 1:
            analyzer.ProcessFileList()
        #except:
        #    print("Error processing the filelist!!!")

        tf = time.time()
        print("Processed file(s) in "\
             +str(np.around((tf-t0)/60,decimals=3))\
             +" minutes.")
        javabridge.kill_vm()
        pass
Exemple #3
0
 def __init__(self, dv_path, tag_path=None, cache_all=True, speak=True):
     self.dv_path = dv_path
     self.tag_path = tag_path
     self.cache_all = cache_all
     javabridge.start_vm(class_path=bioformats.JARS)
     if speak:
         print('Getting DV metadata....')
     self.meta_str = bioformats.get_omexml_metadata(path=self.dv_path)
     if speak:
         print('Solving metadata...')
     self.meta_xml = bioformats.omexml.OMEXML(self.meta_str)
     if cache_all:
         if speak:
             print('Caching all images')
         self.cache_image = self.__read_image()
     else:
         self.cache_image = np.empty(0)
     self.reader = bioformats.get_image_reader(None, self.dv_path)
     if tag_path is not None:
         tmp = sio.loadmat(tag_path)['tag']
         if len(tmp.shape) < 3:
             tmp = np.reshape(tmp, newshape=(1, tmp.shape[0], tmp.shape[1]))
         self.tags = tmp.astype(np.int32)
     else:
         self.tags = np.empty(0)
Exemple #4
0
 def __enter__(self):
     javabridge.start_vm(class_path=bioformats.JARS, run_headless=True)
     self.reader = bioformats.ImageReader(self.path)
     self.metadatastr = bioformats.get_omexml_metadata(self.path)
     self.metadata = ETree.fromstring(self.metadatastr.encode('utf-8'))
     self.initialize()
     return self
Exemple #5
0
def extract_merged_images(lif_file, out_dir):
    """
    Extract merged images, in lif, into output directory

    :param lif: path to lif file
    :param out_dir: path to directory to store images
    :return: {path: (physical_size)} - metadata of images written.
    """
    jv.start_vm(class_path=bf.JARS, max_heap_size='8G')
    merged_images = get_merged_image_ids(lif_file)

    image_data = {}
    for series in merged_images:
        image_ids, max_z, physical_size = merged_images[series]
        image_data[series] = (read_images(image_ids, max_z,
                                          lif_file), physical_size[0])

        # Only read first merged image
        break

    # Finished reading data from the lif file
    jv.kill_vm()

    image_metadata = {}
    # Write images to out_dir
    for series in image_data:
        image_data, physical_size = image_data[series]
        # Write data in image_data
        for i, data in enumerate(image_data):
            out_path = out_dir + "/" + series + "_Z" + str(i) + ".tif"
            cv2.imwrite(out_path, data)

            image_metadata[out_path] = (physical_size)

    return image_metadata
Exemple #6
0
def read_file(input_directory, pixelsize, output_directory):

    img_pixelsize_x = pixelsize
    img_pixelsize_y = pixelsize
    modelfile_path = "2d_cell_net_v0-cytoplasm.modeldef.h5"
    weightfile_path = "snapshot_cytoplasm_iter_1000.caffemodel.h5"
    iofile_path = "output.h5"
    out_path = Path(output_directory)
    rootdir1 = Path(input_directory)
    """ Convert the tif to tiled tiff """
    javabridge.start_vm(args=["-Dlog4j.configuration=file:{}".format(LOG4J)],
                        class_path=JARS,
                        run_headless=True)
    i = 0
    try:
        for PATH in rootdir1.glob('**/*'):
            tile_grid_size = 1
            tile_size = tile_grid_size * 1024

            # Set up the BioReader
            with BioReader(PATH, backend='java',
                           max_workers=cpu_count()) as br:

                # Loop through timepoints
                for t in range(br.T):

                    # Loop through channels
                    for c in range(br.C):

                        with BioWriter(out_path.joinpath(f"final{i}.ome.tif"),
                                       metadata=br.metadata,
                                       backend='java') as bw:

                            # Loop through z-slices
                            for z in range(br.Z):

                                # Loop across the length of the image
                                for y in range(0, br.Y, tile_size):
                                    y_max = min([br.Y, y + tile_size])

                                    # Loop across the depth of the image
                                    for x in range(0, br.X, tile_size):
                                        x_max = min([br.X, x + tile_size])

                                        input_img = np.squeeze(br[y:y_max,
                                                                  x:x_max,
                                                                  z:z + 1, c,
                                                                  t])
                                        img = unet_segmentation(
                                            input_img, img_pixelsize_x,
                                            img_pixelsize_y, modelfile_path,
                                            weightfile_path, iofile_path)
                                        bw[y:y_max, x:x_max, ...] = img
                                        os.remove("output.h5")

            i += 1

    finally:
        # Close the javabridge. Since this is in the finally block, it is always run
        javabridge.kill_vm()
Exemple #7
0
def convert_and_resample_from_tif(file_name, output_file='image_cropped_ana.h5', z_factor=2.35, path_to_image='data'):
    import javabridge
    import bioformats
    
    javabridge.start_vm(class_path=bioformats.JARS)
    r = bioformats.ImageReader(file_name)
    
    shape = (r.rdr.getSizeT(), r.rdr.getSizeC(), r.rdr.getSizeZ(), r.rdr.getSizeY(), r.rdr.getSizeX())
    shape_r = (r.rdr.getSizeT(), r.rdr.getSizeC(), int(z_factor * r.rdr.getSizeZ()), r.rdr.getSizeY(), r.rdr.getSizeX())
    
    img = numpy.zeros(shape, dtype=numpy.float32)
    img_r = numpy.zeros(shape_r, dtype=numpy.float32)
    img_r_prefilter = numpy.zeros(shape_r, dtype=numpy.float32)
    for t in range(shape[0]):
        print "T:", t,
        for c in range(shape[1]):
            for z in range(shape[2]):
                img[t, c, z, :, :,] = r.read(c=c, t=t, z=z)
            img_r[t,c,:,:,:] = vigra.sampling.resizeVolumeSplineInterpolation(img[t,c,:,:,:], shape_r[2:])
            img_r_prefilter[t, c, :, :, :] = ndimage.spline_filter(img_r[t,c,:,:,:])
        
    f = h5py.File(output_file, 'w')
    f["/"].create_dataset(path_to_image, data=img)
    f["/"].create_dataset(path_to_image + "_resampled", data=img_r)
    f["/"].create_dataset(path_to_image + "_resampled_prefiltered", data=img_r_prefilter)
    f.close()
    javabridge.kill_vm()  
Exemple #8
0
def ensure_VM():
    global numVMRefs
    
    if numVMRefs <1:
        javabridge.start_vm(class_path=bioformats.JARS, run_headless=True)
    
    numVMRefs += 1
Exemple #9
0
def main():
    javabridge.start_vm(class_path=bioformats.JARS)
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    sys.exit(app.exec_())
    javabridge.kill_vm()
Exemple #10
0
def convert_nd2_all_samples(config, parallelize=0):
    """
    """
    # TODO: Fill in docstring

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

    samples = config["samples"]

    # TODO: Figure out if it's possible to parallelize by sample here.
    # TODO: Figure out better error handling here (e.g. catch error and
    # kill JVM if error occurs)

    javabridge.start_vm(class_path=bioformats.JARS)

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

        else:
            convert_nd2_single_sample(sample, input_directory,
                                      output_directory)

    for process in processes:
        process.join()

    javabridge.kill_vm()
Exemple #11
0
def main(args):
    if hasattr(sys, 'frozen'):
        jar_path = os.path.join(os.path.dirname(sys.executable), "jars")
        jars = [os.path.join(jar_path, os.path.split(jar)[1])
                for jar in bioformats.JARS]
    else:
        jars = bioformats.JARS
    javabridge.start_vm(class_path=jars)
    try:
        filenames = sum(map(glob.glob, sys.argv[1:]), [])
        app = wx.PySimpleApp()
        if len(filenames) == 0:
            with wx.FileDialog(
                None,
                "Pick files for z-stack",
                wildcard="Tiff files (*.tif)|*.tif|All files (*.*)|*.*",
                style = wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE | wx.FD_OPEN) as dlg:
                assert isinstance(dlg, wx.FileDialog)
                if dlg.ShowModal() != wx.ID_OK:
                    return
                filenames = dlg.Paths
        planes = [bioformats.load_image(filename) for filename in filenames]
        img_red, img_green, img_blue = [
            np.dstack([plane[:, :, i] for plane in planes]) *255
            for i in range(3)]
        frame = Q3DFrame(img_red, img_green, img_blue, None,
                         size=(1024, 768))
        frame.SetTitle("Q3DStack: %s" %filenames[0])
        app.MainLoop()
        
    finally:
        javabridge.kill_vm()
Exemple #12
0
def _startJavabridge(logger):
    global _javabridgeStarted

    if _javabridgeStarted is None:
        # Only import these when first asked.  They are slow to import.
        global bioformats
        global javabridge
        if bioformats is None:
            import bioformats
        if javabridge is None:
            import javabridge

        # We need something to wake up at exit and shut things down
        monitor = threading.Thread(target=_monitor_thread)
        monitor.daemon = True
        monitor.start()
        try:
            javabridge.start_vm(class_path=bioformats.JARS, run_headless=True)
            _reduceLogging()
            atexit.register(_stopJavabridge)
            logger.info('Started JVM for Bioformats tile source.')
            _javabridgeStarted = True
        except RuntimeError as exc:
            logger.exception('Cannot start JVM for Bioformats tile source.',
                             exc)
            _javabridgeStarted = False
    return _javabridgeStarted
Exemple #13
0
    def _start_lif_reader(self):
        jv.start_vm(class_path=bf.JARS)

        log_level = 'ERROR'
	# reduce log level

        # currently does not work in new conda environment

        #rootLoggerName = jv.get_static_field("org/slf4j/Logger", "ROOT_LOGGER_NAME", "Ljava/lang/String;")
        #rootLogger = jv.static_call("org/slf4j/LoggerFactory", "getLogger", "(Ljava/lang/String;)Lorg/slf4j/Logger;", rootLoggerName)
        #logLevel = jv.get_static_field("ch/qos/logback/classic/Level", log_level, "Lch/qos/logback/classic/Level;")
        #jv.call(rootLogger, "setLevel", "(Lch/qos/logback/classic/Level;)V", logLevel)

        self.ir = bf.ImageReader(self.lif_file_path, perform_init=True)
        mdroot = et.ElementTree.fromstring(bf.get_omexml_metadata(self.lif_file_path))
        mds = list(map(lambda e: e.attrib, mdroot.iter('{http://www.openmicroscopy.org/Schemas/OME/2016-06}Pixels')))

        # lif can contain multiple images, select one that is likely to be the timeseries
        self.metadata = None
        self.lif_stack_idx = 0
        for idx, md in enumerate(mds): 
            if int(md['SizeT']) > 1: 
                self.lif_stack_idx = idx
                self.metadata      = md
        if not self.metadata: raise ValueError('lif does not contain an image with sizeT > 1')
    def __init__(self, imgfile, output_path):

        self.imgfile = imgfile
        try:
            self.image = I.ImarisImage(self.imgfile)
            if not os.path.isdir(output_path):
                raise IOError('Invalid output directory: ', output_path)
            else:
                # create unique output directory from image filename
                filename = basename(imgfile)
                new_folder = splitext(filename)[0]
                image_folder = join(output_path, new_folder)
                if not os.path.isdir(image_folder):
                    os.makedirs(image_folder)

                self.output_folder = image_folder

            try:
                javabridge.start_vm(run_headless=True, class_path=bioformats.JARS)
                javabridge.attach()
            except Exception as e:
                raise (e)

            javabridge.attach()
            self.basicmeta = javabridge.jutil.to_string(
                bioformats.formatreader.make_iformat_reader_class().getMetadata(bioformats.ImageReader(imgfile).rdr)).split(',')
            self.basicmeta = sorted(self.basicmeta)
            self.omemeta = bioformats.get_omexml_metadata(imgfile).split("></")
            self.omemeta1 = self.omemeta[0].split(".xsd")
            self.omemeta2 = self.omemeta[1:]
            #javabridge.detach()
        except Exception as e:
            raise (e)
Exemple #15
0
def main(*args):
    jars = javabridge.JARS
    classpath = os.getenv("CLASSPATH", None)
    if not classpath:
        classpath = subprocess.check_output("clj -Spath",
                                            shell=True).decode('utf-8')

    if classpath:
        jars = jars + [classpath]

    javabridge.start_vm(run_headless=True, class_path=jars)

    env = javabridge.get_env()
    stringclass = env.find_class("java/lang/String")

    if len(args) == 0:
        args = ["-r"]

    args = ["-e", "(require 'clj-python-trampoline.interpreter)"] + list(args)

    cljargs = env.make_object_array(len(args), stringclass)

    for i, arg in enumerate(args):
        env.set_object_array_element(cljargs, i, env.new_string_utf(arg))

    c = env.find_class("clojure/main")
    method_id = env.get_static_method_id(c, "main", "([Ljava/lang/String;)V")
    env.call_static_method(c, method_id, cljargs)
Exemple #16
0
def readZPositions(stk_files):

    # the only entry parameter for this function is a list with
    # the stk files

    print('\n\t\treading z-values... ')
    javabridge.start_vm(class_path=bioformats.JARS)

    zs = []  # array with z-positions
    ts = []  # array with time point (got from data files)

    print('starting loop...')

    for file_path in stk_files:

        print('reading file: {}'.format(file_path))

        md = bioformats.get_omexml_metadata(file_path)
        ome = bioformats.OMEXML(md)

        # create an instance of an image to read z-position
        zp = ome.image().Pixels.Plane().get_PositionZ()

        time = int(file_path[:-4].split('t')[-1])

        zs.append(zp)
        ts.append(time)

    z_offsets = np.array([item - min(np.array(zs)) for item in zs])

    javabridge.kill_vm()

    return np.array(zs), np.array(ts), z_offsets
Exemple #17
0
    def __init__(self):

        jb.start_vm(class_path=bf.JARS)
        self.omexml = bf.metadatatools.createOMEXMLMetadata()
        self.ImageReader = bf.formatreader.make_image_reader_class()
        self.reader = self.ImageReader()
        self.reader.setMetadataStore(self.omexml)
Exemple #18
0
def start_vm(args=None,
             class_path=None,
             max_heap_size=None,
             run_headless=False):
    """
    Start the JVM via Javabridge.
    """
    if class_path is None:
        class_path = get_java_class_path()
    # Start the JVM.
    javabridge.start_vm(args=args,
                        class_path=class_path,
                        max_heap_size=max_heap_size,
                        run_headless=run_headless)
    # Suppress Java logging to stdout.
    java_stack = javabridge.make_instance('java/io/ByteArrayOutputStream',
                                          "()V")
    java_stack_ps = javabridge.make_instance('java/io/PrintStream',
                                             "(Ljava/io/OutputStream;)V",
                                             java_stack)
    javabridge.static_call('Ljava/lang/System;', "setErr",
                           '(Ljava/io/PrintStream;)V', java_stack_ps)
    java_out = javabridge.make_instance('java/io/ByteArrayOutputStream', "()V")
    java_out_ps = javabridge.make_instance('java/io/PrintStream',
                                           "(Ljava/io/OutputStream;)V",
                                           java_out)
    javabridge.static_call('Ljava/lang/System;', "setOut",
                           '(Ljava/io/PrintStream;)V', java_out_ps)
    javabridge.attach()
def main():
    javabridge.start_vm(class_path=bioformats.JARS)
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    sys.exit(app.exec_())
    javabridge.kill_vm()
    def OnInit(self):
        """Initialize CPA
        """

        """List of tables created by the user during this session"""
        self.user_tables = []

        # splashscreen
        splashimage = cpa.icons.cpa_splash.ConvertToBitmap()
        # If the splash image has alpha, it shows up transparently on
        # windows, so we blend it into a white background.
        splashbitmap = wx.EmptyBitmapRGBA(splashimage.GetWidth(), splashimage.GetHeight(), 255, 255, 255, 255)
        dc = wx.MemoryDC()
        dc.SelectObject(splashbitmap)
        dc.DrawBitmap(splashimage, 0, 0)
        dc.Destroy()  # necessary to avoid a crash in splashscreen
        splash = wx.SplashScreen(splashbitmap, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, None, -1)
        self.splash = splash

        p = Properties.getInstance()
        if not p.is_initialized():
            from cpa.guiutils import show_load_dialog

            splash.Destroy()
            if not show_load_dialog():
                logging.error("CellProfiler Analyst requires a properties file. Exiting.")
                return False
        self.frame = MainGUI(p, None, size=(860, -1))
        self.frame.Show(True)
        db = cpa.dbconnect.DBConnect.getInstance()
        # Black magic: Bus errors occur on Mac OS X if we wait until
        # the JVM or the wx event look has started to connect. But it
        # has to be done after we have read the properties file. So we
        # do it here.
        db.connect()
        db.register_gui_parent(self.frame)

        # The JVM has to be started after db.connect(), otherwise bus errors
        # occur on Mac OS X.
        javabridge.start_vm(class_path=bioformats.JARS, run_headless=False)
        javabridge.attach()
        javabridge.activate_awt()

        try:
            if __version__ != -1:
                import cellprofiler.utilities.check_for_updates as cfu

                cfu.check_for_updates(
                    "http://cellprofiler.org/CPAupdate.html",
                    max(__version__, cpaprefs.get_skip_version()),
                    new_version_cb,
                    user_agent="CPAnalyst/2.0.%d" % (__version__),
                )
        except ImportError:
            logging.warn(
                "CPA was unable to check for updates. Could not import cellprofiler.utilities.check_for_updates."
            )

        return True
Exemple #21
0
def readBioFormats(fn,debug=True):
	
	"""Reads bioformats image file.
	
	Args:
		fn (str): Path to file.
	
	Keyword Args:
		debug (bool): Show debugging output.
	
	Returns:
		tuple: Tuple containing:
		
			* images (list): List of datasets
			* meta (OMEXML): meta data of all data.
	
	"""
	
	javabridge.start_vm(class_path=bioformats.JARS)
	
	meta=readBioFormatsMeta(fn)
	
	#Empty list to put datasets in	
	images=[]
	
	#Open with reader class
	with bioformats.ImageReader(fn) as reader:
		
		#Loop through all images
		for i in range(meta.image_count):
			
			channels=[]
			
			#Check if there is a corrupt zstack for this particular dataset
			problematicStacks=checkProblematicStacks(reader,meta,i,debug=debug)
			
			#Loop through all channels
			for j in range(meta.image(i).Pixels.SizeC):
				
				zStacks=[]
				
				#Loop through all zStacks
				for k in range(meta.image(i).Pixels.SizeZ):
					
					#Check if corrupted
					if k not in problematicStacks:
						img=reader.read(series=i, c=j, z=k, rescale=False)
						zStacks.append(img)
					
				#Append to channels
				channels.append(zStacks)
			
			#Append to list of datasets and converts it to numpy array
			images.append(np.asarray(channels))
	
	#Kill java VM
	javabridge.kill_vm()
	
	return images,meta
Exemple #22
0
    def save_images(self):
        """Saves the individual images as a npy file

        2D might have more acquisitions +/- focal plane, (usually 3 images).
        focal_plane_idx corresponds to the plane to consider. Mid-plane is the
        one in focus and the +/- on either side would be blurred. For 2D
        acquisitions, this is stored along the Z dimension. How is this handled
        for 3D acquisitions?
        """

        if not os.path.exists(self.lif_fname):
            raise FileNotFoundError("LIF file doesn't exist at:",
                                    self.lif_fname)
        os.makedirs(self.split_dir, exist_ok=True)

        jv.start_vm(class_path=bf.JARS, max_heap_size='8G')
        metadata = bf.get_omexml_metadata(self.lif_fname)
        omexml_object = bf.OMEXML(metadata)
        num_channels = omexml_object.image().Pixels.channel_count
        num_samples = omexml_object.get_image_count()
        num_timepoints = omexml_object.image().Pixels.SizeT
        num_pix_z = omexml_object.image().Pixels.SizeZ
        size_x_um = omexml_object.image().Pixels.PhysicalSizeX
        size_y_um = omexml_object.image().Pixels.PhysicalSizeY
        size_z_um = omexml_object.image().Pixels.PhysicalSizeZ

        reader = bf.ImageReader(self.lif_fname, perform_init=True)

        records = []
        for timepoint_idx in range(num_timepoints):
            timepoint_dir = os.path.join(self.split_dir,
                                         'timepoint_{}'.format(timepoint_idx))
            os.makedirs(timepoint_dir, exist_ok=True)

            for channel_idx in range(num_channels):
                channel_dir = os.path.join(timepoint_dir,
                                           'channel_{}'.format(channel_idx))
                os.makedirs(channel_dir, exist_ok=True)
                for sample_idx in range(15, 412):  # num_samples
                    cur_records = self.save_each_image(reader, num_pix_z,
                                                       channel_dir,
                                                       timepoint_idx,
                                                       channel_idx, sample_idx,
                                                       size_x_um, size_y_um,
                                                       size_z_um)
                    records.extend(cur_records)
                msg = 'Wrote files for tp:{}, channel:{}'.format(
                    timepoint_idx, channel_idx)
                self._log_info(msg)
        df = pd.DataFrame.from_records(records,
                                       columns=[
                                           'timepoint', 'channel_num',
                                           'sample_num', 'slice_num', 'fname',
                                           'size_x_microns', 'size_y_microns',
                                           'size_z_microns'
                                       ])
        metadata_fname = os.path.join(self.split_dir, 'split_images_info.csv')
        df.to_csv(metadata_fname, sep=',')
        jv.kill_vm()
Exemple #23
0
    def start_vm(self, java_max_heap_size = None):
        tetrad_libdir = os.path.join(os.path.dirname(__file__), 'lib')

        for l in glob.glob(tetrad_libdir + os.sep + "*.jar"):
            javabridge.JARS.append(str(l))

        javabridge.start_vm(run_headless=True, max_heap_size = java_max_heap_size)
        javabridge.attach()        
Exemple #24
0
def get_decompounder():
    """
    Restarts the JVM with the decompounder. It is necessary once in a while.
    """
    javabridge.start_vm(class_path=["tf/jwordsplitter/target/jwordsplitter-4.2-SNAPSHOT.jar"])
    java_instance = javabridge.make_instance("de/danielnaber/jwordsplitter/GermanWordSplitter", "(Z)V", False)
    decompounder = javabridge.JWrapper(java_instance)
    return decompounder
    def __init__(self, thoughtland_jar=None, *args, **kwargs):

        kwargs.setdefault("class_path", []).append(
            thoughtland_jar or THOUGTHLAND_JAR
        )
        kwargs.setdefault("run_headless", True)
        import ipdb; ipdb.set_trace()
        javabridge.start_vm(*args, **kwargs)
Exemple #26
0
 def __init__(self):
     try:
         jb.start_vm(
             class_path=jb.JARS +
             [resource_filename('noesis', Noesis.__DEFAULT_JAR_FILE__)],
             run_headless=False)
     except:
         jb.kill_vm()
Exemple #27
0
    def start_vm(self, java_max_heap_size = None):
        tetrad_libdir = os.path.join(os.path.dirname(__file__), 'lib')

        for l in glob.glob(tetrad_libdir + os.sep + "*.jar"):
            javabridge.JARS.append(str(l))

        javabridge.start_vm(run_headless=True, max_heap_size = java_max_heap_size)
        javabridge.attach()        
Exemple #28
0
def command(input, output, channels, image_size, verbose):
    input_directory = os.path.realpath(input)

    output_directory = os.path.realpath(output)

    if not os.path.exists(output_directory):
        os.mkdir(output_directory)

    # Collect subdirectories.
    subdirectories = [
        directory
        for directory in glob.glob(os.path.join(input_directory, "*"))
        if os.path.isdir(directory)
    ]

    # Collect channels to parse.
    if channels:
        channels = _parse_channels(channels)

    jvm_started = False

    for subdirectory in subdirectories:
        # Collect images in subdirectory, filtering out unsupported image formats.
        paths = _collect(subdirectory)
        ext = os.path.splitext(paths[0])[-1].lower()

        if ext == ".cif" and not jvm_started:
            import javabridge

            log_config = pkg_resources.resource_filename(
                "deepometry", "resources/logback.xml")

            javabridge.start_vm(args=[
                "-Dlogback.configurationFile={}".format(log_config),
                "-Dloglevel={}".format("DEBUG" if verbose else "OFF")
            ],
                                class_path=bioformats.JARS,
                                max_heap_size="8G",
                                run_headless=True)

            jvm_started = True

        label = os.path.split(subdirectory)[-1]
        label_directory = os.path.join(output_directory, label)

        try:
            deepometry.parse.parse(paths,
                                   output_directory=label_directory,
                                   size=image_size,
                                   channels=channels)
        except Exception as exception:
            if jvm_started:
                javabridge.kill_vm()

            raise exception

    if jvm_started:
        javabridge.kill_vm()
Exemple #29
0
 def __init__(self, pythonEncoding, encodingName):
     self.pythonEncoding = pythonEncoding
     self.encodingName = encodingName
     
     self.controlCharRegex = self.buildControlCharRegex()
     
     if(not self.pythonEncoding):
         if JAVABRIDGEINCLUDED:
             javabridge.start_vm(run_headless=True)
Exemple #30
0
def test_java_bridge():
    javabridge.start_vm(run_headless=True)
    try:
        print(
            javabridge.run_script(
                'java.lang.String.format("Hello, %s!", name);',
                dict(name='world')))
    finally:
        javabridge.kill_vm()
    def begin(self):
        import javabridge

        javabridge.start_vm(self.extra_jvm_args,
                            class_path=self.class_path.split(os.pathsep),
                            run_headless=self.headless,
                            max_heap_size=self.max_heap_size)
        if not self.headless:
            javabridge.activate_awt()
Exemple #32
0
def __main__(image, output, image_size, montage_size):
    try:
        javabridge.start_vm(class_path=bioformats.JARS, max_heap_size='8G')

        os.mkdir(output)

        __stitch(image, output, image_size, montage_size)
    finally:
        javabridge.kill_vm()
Exemple #33
0
    def begin(self):
        import javabridge

        javabridge.start_vm(self.extra_jvm_args,
                            class_path=self.class_path.split(os.pathsep),
                            run_headless=self.headless,
                            max_heap_size=self.max_heap_size)
        if not self.headless:
            javabridge.activate_awt()
Exemple #34
0
def init_javabridge():  #max_heap_size='4G'):
    if not vm_is_active():
        javabridge.start_vm(class_path=bioformats.JARS,
                            run_headless=True)  # max_heap_size=max_heap_size)
        suppressMsgs()
        #atexit.register(uninit_javabridge)

    else:
        attach()
def JVstart(max_heap_size='8G'):
    """Start the Java Virtual Machine, enabling bioformats IO.
    max_heap_size : string, optional
        The maximum memory usage by the virtual machine. Valid strings
        include '256M', '64k', and '2G'. Expect to need a lot.
    """
    jv.start_vm(class_path=bf.JARS, max_heap_size=max_heap_size)
    global VM_STARTED
    VM_STARTED = True
Exemple #36
0
def init_javabridge():  #max_heap_size='4G'):
    if not vm_is_active():
        #print 'activate'
        javabridge.start_vm(class_path=bioformats.JARS,
                            run_headless=True)  # max_heap_size=max_heap_size)
        suppressMsgs()

    else:
        #print 'trying to attach'
        attach()
    def get(self):
        total = dm.get_total_object_count()
        objKeys = dm.GetRandomObjects(total)

        # Start the virtual machine
        javabridge.start_vm(class_path=bioformats.JARS, run_headless=True)
        javabridge.attach()
        javabridge.activate_awt()

        return calculateAllJSON(objKeys, base64=True)
Exemple #38
0
def jvm():
    print()
    print('Starting javabridge...')
    log_config = Path(__file__).parent.joinpath("log4j.properties")
    jutil.start_vm(args=["-Dlog4j.configuration=file:{}".format(bfio.LOG4J)],
                   class_path=bfio.JARS)
    yield
    print()
    print('Closing javabridge...')
    jutil.kill_vm()
Exemple #39
0
 def process(self, i, o):
     javabridge.start_vm(
         class_path=javabridge.JARS +
         ["/apps/whyis/jars/whyis-java-jar-with-dependencies.jar"],
         run_headless=True)
     try:
         javabridge.run_script('edu.rpi.tw.whyis.HermiTAgent.reason();',
                               dict(greetee='world'))
     finally:
         javabridge.kill_vm()
def run_javabridge(java_code_to_run):
    javabridge.start_vm(run_headless=True)
    try:
        output = javabridge.run_script(java_code_to_run)
        print(output)

        # need to process and return some stuff here
        
    finally:
        javabridge.kill_vm()
Exemple #41
0
def get_decompounder():
    """
    Restarts the JVM with the decompounder. It is necessary once in a while.
    """
    javabridge.start_vm(
        class_path=["tf/jwordsplitter/target/jwordsplitter-4.2-SNAPSHOT.jar"])
    java_instance = javabridge.make_instance(
        "de/danielnaber/jwordsplitter/GermanWordSplitter", "(Z)V", False)
    decompounder = javabridge.JWrapper(java_instance)
    return decompounder
    def get(self):
        if cached_json["base64"] == False:
            # Start the virtual machine
            javabridge.start_vm(class_path=bioformats.JARS, run_headless=True)
            javabridge.attach()
            javabridge.activate_awt()

            # Calculate the Training DataSet and store it
            cached_json["json"] = calculateTrainingSetJSON(base64=True)
            cached_json["base64"] = True

        return cached_json["json"]
def main():
    # Parse command line input
    parser = configure_parser()
    args = parser.parse_args()

    experiment_path = path.expanduser(args.experiment_path)
    net_path = path.expanduser(args.net_path)
    model_path = path.expanduser(args.model_path)

    # Configure io managers for the output database and for the metadata
    db_manager = io.ImageDbManager(args.output_db_path, readonly=False)
    metadata_manager = io.MetadataManager(experiment_path=args.experiment_path)

    # Configure fish_net.  This will be done internally when CellDetectors are initialized at some point....
    fish_net = caffe.Net(net_path, model_path, caffe.TEST)

    # Configure a cell detector
    # The cell_radius is important for processing the output of fish_net (ex. separating nearby cells)
    # The signal_channel specifies the channel in the input image that contains the relevant signal (i.e. the ISH stain)
    cell_detector = detection.CellDetector(net=fish_net, cell_radius=12, signal_channel=0)

    # Configure and start the JVM for loading vsi images with bioformats
    javabridge.start_vm(class_path=bioformats.JARS)
    log4j.basic_config()

    metadata = metadata_manager.load_metadata()

    for i, image_metadata in enumerate(metadata):
        print "Analyzing image %d of %d" % (i, len(metadata))
        try:
            # Load an image descriptor using an images metadata.  
            # The experiment path is necessary to locate files that are mentioned in the metadata
            image_descriptor = data.ImageDescriptor.from_metadata(image_metadata, experiment_path=args.experiment_path)

            # Load the vsi image that the metadata references, and pass it to the cell_detector
            vsi_path = path.join(args.experiment_path, image_metadata['vsiPath'])
            vsi_image = load_vsi(vsi_path)
            vsi_chunker = detection.ImageChunker(vsi_image.transpose(2,0,1), chunk_size=args.chunk_size)


            for chunk in vsi_chunker:
                cell_detector.set_image(chunk.transpose(1,2,0))
                detected_cells = filter_duplicates(cell_detector.detect_cells(), vsi_chunker.current_chunk_row, vsi_chunker.current_chunk_col, args.chunk_size)
                image_descriptor.cells += map(lambda cell: data.PhysicalCell.from_cell(cell, VSI_PIXEL_SCALE), detected_cells)

            # Compute and set the offset of the region map
            vsi_resolution = numpy.asarray(vsi_image.shape[:1], dtype=numpy.int32)
            image_descriptor.region_map_offset = compute_offset(vsi_resolution)

            # Send the image descriptor to the database
            db_manager.add_image(image_descriptor)
        except:
            print "Cell detection failed for %s" % image_metadata['vsiPath']
def setup_and_teardown():
    log_config = os.path.join(os.path.split(__file__)[0], "resources", "log4j.properties")

    javabridge.start_vm(
        args=[
            "-Dlog4j.configuration=file:{}".format(log_config),
        ],
        class_path=bioformats.JARS,
        run_headless=True
    )

    yield

    javabridge.kill_vm()
def start(class_path=None, bundled=True, packages=False, system_cp=False, max_heap_size=None):
    """
    Initializes the javabridge connection (starts up the JVM).

    :param class_path: the additional classpath elements to add
    :type class_path: list
    :param bundled: whether to add jars from the "lib" directory
    :type bundled: bool
    :param packages: whether to add jars from Weka packages as well (bool) or an alternative Weka home directory (str)
    :type packages: bool or str
    :param system_cp: whether to add the system classpath as well
    :type system_cp: bool
    :param max_heap_size: the maximum heap size (-Xmx parameter, eg 512m or 4g)
    :type max_heap_size: str
    """
    global started

    if started is not None:
        logger.info("JVM already running, call jvm.stop() first")
        return

    # add user-defined jars first
    if class_path is not None:
        for cp in class_path:
            logger.debug("Adding user-supplied classpath=" + cp)
            javabridge.JARS.append(cp)

    if bundled:
        logger.debug("Adding bundled jars")
        add_bundled_jars()

    if packages is not None:
        if isinstance(packages, bool):
            if packages:
                logger.debug("Adding Weka packages")
                add_weka_packages()
        else:
            logger.debug("Adding Weka packages, using: " + packages)
            add_weka_packages(packages)

    if system_cp:
        logger.debug("Adding system classpath")
        add_system_classpath()

    logger.debug("Classpath=" + str(javabridge.JARS))
    logger.debug("MaxHeapSize=" + ("default" if (max_heap_size is None) else max_heap_size))

    javabridge.start_vm(run_headless=True, max_heap_size=max_heap_size)
    javabridge.attach()
    started = True
def start_vm( args=None, class_path=None, max_heap_size=None, run_headless=False ):
    """
    Start the JVM via Javabridge.
    """
    if class_path is None:
        class_path = get_java_class_path()
    # Start the JVM.
    javabridge.start_vm( args=args, class_path=class_path, max_heap_size=max_heap_size, run_headless=run_headless )
    # Suppress Java logging to stdout.
    java_stack = javabridge.make_instance( 'java/io/ByteArrayOutputStream', "()V" )
    java_stack_ps = javabridge.make_instance( 'java/io/PrintStream', "(Ljava/io/OutputStream;)V", java_stack )
    javabridge.static_call( 'Ljava/lang/System;', "setErr", '(Ljava/io/PrintStream;)V', java_stack_ps )
    java_out = javabridge.make_instance( 'java/io/ByteArrayOutputStream', "()V" )
    java_out_ps = javabridge.make_instance( 'java/io/PrintStream', "(Ljava/io/OutputStream;)V", java_out )
    javabridge.static_call( 'Ljava/lang/System;', "setOut", '(Ljava/io/PrintStream;)V', java_out_ps )
    javabridge.attach()
Exemple #47
0
def setUpModule():
    try:
        os.environ["PYJNIUS_ACTIVATE"]
    except KeyError:
        import javabridge

        try:
            # include user added classpath
            classpath = os.environ["CLASSPATH"]
            classpath = classpath.split(os.pathsep)
            javabridge.JARS.extend(classpath)
        except KeyError:
            None

        javabridge.start_vm()
        javabridge.attach()
Exemple #48
0
def check_and_start_jvm():
    """ Checks global to see whether a JVM is running and if not starts
    a new one. If JVM starts successfully the global variable mtbf_jvm_started
    is updated to ensure that another JVM is not started.
    """
    global mtbf_jvm_started
    if not mtbf_jvm_started:
        logger.debug("Starting a new JVM")
        try:
            mh_size = getattr(settings, 'MTBF_MAX_HEAP_SIZE', '4G')
            javabridge.start_vm(class_path=bioformats.JARS,
                                max_heap_size=mh_size,
                                run_headless=True)
            mtbf_jvm_started = True
        except javabridge.JVMNotFoundError, e:
            logger.debug(e)
Exemple #49
0
def cp_start_vm():
    '''Start CellProfiler's JVM via Javabridge

    JVM parameters are harvested from preferences and
    the environment variables:

    CP_JDWP_PORT - port # for debugging Java within the JVM
    cpprefs.get_awt_headless() - controls java.awt.headless to prevent
        awt from being invoked
    '''
    args = ["-Dloci.bioformats.loaded=true",
            "-Djava.util.prefs.PreferencesFactory=" +
            "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"]

    logback_path = find_logback_xml()

    if logback_path is not None:
        if sys.platform.startswith("win"):
            logback_path = logback_path.replace("\\", "/")
            if logback_path[1] == ':':
                # \\localhost\x$ is same as x:
                logback_path = "//localhost/" + logback_path[0] + "$" + \
                               logback_path[2:]
        args.append("-Dlogback.configurationFile=%s" % logback_path)

    class_path = get_jars()
    awt_headless = cellprofiler.preferences.get_awt_headless()
    if awt_headless:
        logger.debug("JVM will be started with AWT in headless mode")
        args.append("-Djava.awt.headless=true")

    heap_size = str(cellprofiler.preferences.get_jvm_heap_mb()) + "m"
    if os.environ.has_key("CP_JDWP_PORT"):
        args.append(
                ("-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:%s"
                 ",server=y,suspend=n") % os.environ["CP_JDWP_PORT"])
    javabridge.start_vm(args=args,
                        class_path=class_path,
                        max_heap_size=heap_size)
    #
    # Enable Bio-Formats directory cacheing
    #

    c_location = javabridge.JClassWrapper("loci.common.Location")
    c_location.cacheDirectoryListings(True)
    logger.debug("Enabled Bio-formats directory cacheing")
def main():
    parser = configure_parser()
    args = parser.parse_args()

    metadata_path = path.join(args.experiment_path, '.registrationData', 'metadata.json')
    metadata = json.load(open(metadata_path))

    javabridge.start_vm(class_path=bioformats.JARS)
    log4j.basic_config()

    image_descriptors = ifilter(
        lambda d: d is not None, 
        (get_image_descriptor(label_path, metadata, args) for label_path in args.image_paths)
    )
    df = dataframes.get_dataframe_from_image_sequence(image_descriptors)
    df.to_csv(args.output_path)

    javabridge.kill_vm()
def begin_javabridge(max_heap_size='8G'):
    ''' Begin the jave virtual machine.

    Parameters
    ----------
    max_heap_size : string, optional
        Allocated memory for the virtual machine.

    Notes
    -----
    Remember to end the javabridge!
    '''

    global JVM_BEGIN

    javabridge.start_vm(class_path=bioformats.JARS,max_heap_size=max_heap_size)
    log4j.basic_config()

    JVM_BEGIN = True
Exemple #52
0
def start_jvm(max_heap_size='4G'):

    """
    Start the Java Virtual Machine, enabling BioFormats IO.
    Optional: Specify the path to the bioformats_package.jar to your needs by calling.
    set_bfpath before staring to read the image data

    Parameters
    ----------
    max_heap_size : string, optional
    The maximum memory usage by the virtual machine. Valid strings
    include '256M', '64k', and '2G'. Expect to need a lot.
    """

    # TODO - include check for the OS, so that the file paths are always working

    jars = jv.JARS + [BFPATH]
    jv.start_vm(class_path=jars, max_heap_size=max_heap_size)
    VM_STARTED = True
Exemple #53
0
def upload_3d_hela_collection(data_path, is_test=False):
  """
  The HeLa images are all in the same directory with the dataset, channel,
  and cell indicated by the filename. We combine each cell's channel files into
  a single image and upload it to OMERO, organizing the datasets together under
  the HeLa project, which is created it if it doesn't exist.

  If is_test is true, only upload one image.
  """
  check_structure(data_path)
  conn = get_omero_connection()
  dataset_to_ids = dict()
  javabridge.start_vm(class_path=bioformats.JARS)
  bioformats.log4j.basic_config()
  for dataset in DATASETS:
    im_ids = upload_dataset(conn, data_path, dataset, is_test)
    dataset_to_ids[dataset['name']] = im_ids
    if is_test:
      break
  organize_uploaded_images(conn, dataset_to_ids)
  javabridge.kill_vm()
  conn._closeSession()
def main():
    parser = configure_parser()
    args = parser.parse_args()

    meta_man = io.MetadataManager(experiment_path=args.experiment_path)

    number_of_vsi_samples = numpy.ceil(len(meta_man.metadata) * args.vsi_fraction)
    metadata_sample = list(numpy.random.choice(meta_man.metadata, number_of_vsi_samples, replace=False))

    output_file = open(args.output_file, "w")

    javabridge.start_vm(class_path=bioformats.JARS)
    log4j.basic_config()

    (histogram, bins) = numpy.histogram([], bins=args.number_of_bins, range=(0.0, 2 ** 16 - 1), density=True)
    n = 0.0

    for entry in metadata_sample:
        image = load_vsi(os.path.join(args.experiment_path, entry["vsiPath"]))
        number_of_samples = numpy.ceil(image.size * args.pixel_fraction)
        samples = numpy.random.choice(image.flatten(), number_of_samples, replace=False)

        sample_hist, sample_bins = numpy.histogram(samples, bins=args.number_of_bins, range=(0.0, 2 ** 16 - 1))
        sample_hist = sample_hist.astype(numpy.float64) / number_of_samples

        if n > 0:
            gamma = number_of_samples / n
            histogram = update_histogram(histogram, sample_hist, gamma)
        else:
            histogram = sample_hist

        n += samples.size

    javabridge.kill_vm()

    output_obj = {"bins": list(bins), "histogram": list(histogram), "n": n}

    json.dump(output_obj, output_file)
    output_file.close()
Exemple #55
0
    def start(tassel_dir=None):
        """
        Starts the JVM and connects to the TASSEL JAR

        Arguments:

        tassel_dir -- The directory containing the TASSEL JAR
        """
        if tassel_dir is None:
            use_jars = javabridge.JARS + list(iglob(os.path.join(os.path.dirname(__file__), 'lib', '*.jar')))
        else:
            use_jars = javabridge.JARS + [os.path.join(tassel_dir, 'sTASSEL.jar')]
            use_jars += list(iglob(os.path.join(tassel_dir, 'lib', '*.jar')))
        # Start VM
        try:
            try:
                javabridge.start_vm(['-Djava.class.path=' +
                                     os.pathsep.join(use_jars)], class_path=use_jars,
                                    run_headless=True)
            except:
                javabridge.start_vm(class_path=use_jars, run_headless=True)
        except:
            print "Could not start JVM!"
def main():
    parser = configure_parser()
    args = parser.parse_args()

    meta_man = io.MetadataManager(experiment_path=args.experiment_path)
    output_file = open(args.output_file, 'w')

    entry = meta_man.get_entry_from_name(os.path.basename(args.vsi_filename))

    javabridge.start_vm(class_path=bioformats.JARS)
    log4j.basic_config()

    if entry is not None:
        image = load_vsi(os.path.join(args.experiment_path, entry['vsiPath']))
        number_of_samples = numpy.ceil(image.size * args.pixel_fraction)
        samples = numpy.random.choice(image.flatten(), number_of_samples, replace=False)

        histogram, bins = numpy.histogram(samples, bins=args.number_of_bins, range=(0.0, 2**16 - 1))
        percentiles = map(float, range(0, 101))
        percentile_values = numpy.percentile(image, percentiles)
    else:
        print "Could not locate %s in metadata!" %s args.vsi_filename
        javabridge.kill_vm()
        return

    javabridge.kill_vm()
    
    output_obj = {
        'bins': list(bins),
        'histogram': list(histogram),
        'n': number_of_samples,
        'percentiles': percentiles,
        'percentile_values': list(percentile_values)
    }

    json.dump(output_obj, output_file)
    output_file.close()
def upload_3d_synthetic_hela_collection(data_path, is_test=False):
  """
  Given the path to the 3d synthetic hela images, they are in a
  one-image-per-channel format. For each dataset, merge corresponding channels
  of the same image into one and upload them to an OMERO instance. Look for a
  HeLa project and create it if it doesn't exist. Otherwise, upload the datasets
  to the existing HeLa project.

  If is_test is true, only upload one image.
  """
  check_structure(data_path)
  conn = get_omero_connection()
  dataset_to_ids = dict()
  javabridge.start_vm(class_path=bioformats.JARS)
  bioformats.log4j.basic_config()
  for dataset in DATASETS:
    name, infix = dataset['name'], dataset['infix']
    im_ids = upload_dataset(conn, dataset, data_path, is_test)
    dataset_to_ids[name] = im_ids
    if is_test:
      break
  organize_uploaded_images(conn, dataset_to_ids)
  javabridge.kill_vm()
  conn._closeSession()
def start(class_path=None, bundled=True, packages=False, system_cp=False, max_heap_size=None):
    """
    Initializes the javabridge connection (starts up the JVM).
    :param class_path: the additional classpath elements to add
    :type class_path: str
    :param bundled: whether to add jars from the "lib" directory
    :type bundled: bool
    :param packages: whether to add jars from Weka packages as well
    :type packages: bool
    :param system_cp: whether to add the system classpath as well
    :type system_cp: bool
    :param max_heap_size: the maximum heap size (-Xmx parameter, eg 512m or 4g)
    :type max_heap_size: str
    """
    global started

    if not started is None:
        return

    # add user-defined jars first
    if not class_path is None:
        for cp in class_path:
            javabridge.JARS.append(cp)

    if bundled:
        add_bundled_jars()

    if packages:
        add_weka_packages()

    if system_cp:
        add_system_classpath()

    javabridge.start_vm(run_headless=True, max_heap_size=max_heap_size)
    javabridge.attach()
    started = True
import numpy as np
import sys
from PIL import Image
import os
import glob
import time
from subprocess import call
import tifffile
from scipy import stats
from scipy import ndimage
import javabridge as jv
import bioformats as bf
jv.start_vm(class_path=bf.JARS)

##print(sys.argv[1])
os.chdir(sys.argv[1])


##CtrlWhile = 0
##MedSize = 101 ##Make it even
MedSteps = 10
GaussRad = 100##LinSTD_fn = "LinearSTD.dat"
##SqrSTD_fn = "SquareSTD.dat"
Counts = 0##np.loadtxt('Count.STD')

#FileNamePattern = '*.tif'
#File8bitSuff = '-8B'
#ConvertOpt = "-quiet"##" -quiet -auto-level -depth 8"

FolderName = sys.argv[1]
CfgFile = 'Cfg.tmp'
Exemple #60
0
def cp_start_vm():
    '''Start CellProfiler's JVM via Javabridge

    JVM parameters are harvested from preferences and
    the environment variables:

    CP_JDWP_PORT - port # for debugging Java within the JVM
    cpprefs.get_awt_headless() - controls java.awt.headless to prevent
        awt from being invoked
    '''
    args = ["-Dloci.bioformats.loaded=true",
            "-Djava.util.prefs.PreferencesFactory="+
            "org.cellprofiler.headlesspreferences.HeadlessPreferencesFactory"]
    add_logback_xml_arg(args)
    class_path = get_jars()
    args += get_patcher_args(class_path)
    awt_headless = cpprefs.get_awt_headless()
    if awt_headless:
        logger.debug("JVM will be started with AWT in headless mode")
        args.append("-Djava.awt.headless=true")

    heap_size = str(cpprefs.get_jvm_heap_mb())+"m"
    if os.environ.has_key("CP_JDWP_PORT"):
        args.append(
            ("-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:%s"
             ",server=y,suspend=n") % os.environ["CP_JDWP_PORT"])
    javabridge.start_vm(args=args,
                        class_path=class_path,
                        max_heap_size = heap_size)
    #
    # Enable Bio-Formats directory cacheing
    #
    try:
        c_location = javabridge.JClassWrapper("loci.common.Location")
        c_location.cacheDirectoryListings(True)
        logger.debug("Enabled Bio-formats directory cacheing")
    except:
        logger.warning("Bioformats version does not support directory cacheing")
    #
    # Monkey-patch bioformats.formatreader.get_class_list to add
    # the classes we added to loci.formats.in
    #
    import bioformats.formatreader

    old_get_class_list = bioformats.formatreader.get_class_list

    def get_class_list():
        "Return a wrapped instance of loci.formats.ClassList"

        env = javabridge.get_env()
        class_list = old_get_class_list()
        rais_classname = 'loci/common/RandomAccessInputStream'
        #
        # Move any class to the back that thinks it can read garbage
        #
        fd, path = tempfile.mkstemp(suffix=".garbage",
                                    dir=cpprefs.get_temporary_directory())
        stream = None
        try:
            os.write(fd, "This is not an image file")
            os.fsync(fd)
            stream = javabridge.make_instance(
                rais_classname, '(Ljava/lang/String;)V', path)
            problem_classes = []
            for klass in env.get_object_array_elements(class_list.get_classes()):
                try:
                    instance =  javabridge.call(
                        klass, "newInstance", "()Ljava/lang/Object;")
                    can_read_garbage = javabridge.call(
                        instance, "isThisType",
                        "(L%s;)Z" % rais_classname, stream)
                    if can_read_garbage:
                        problem_classes.append(klass)
                        class_list.remove_class(klass)
                except:
                    logger.info("Caught exception in %s.isThisType",
                                javabridge.to_string(klass))
        finally:
            os.close(fd)
            javabridge.call(stream, "close", "()V")
            os.remove(path)

        for classname in ("loci.formats.in.FlowSightReader",
                          "loci.formats.in.IM3Reader"):
            try:
                klass = javabridge.class_for_name(classname)
                class_list.add_class(klass)
            except:
                logger.warn("Could not find Bio-formats reader %s" % classname,
                            exc_info=1)
        for klass in problem_classes:
            class_list.add_class(klass)
        return class_list
    bioformats.formatreader.get_class_list = get_class_list