Beispiel #1
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
Beispiel #2
0
 def close(self):
     if self.use_bioformats:
         self.filehandle.close()
         import javabridge
         javabridge.kill_vm()
     elif self.filetype == 'tif':  # Nothing to do if sequence directory
         self.filehandle.close()
Beispiel #3
0
 def close(self, is_last=False):
     self.reader.close()
     if is_last:
         try:
             javabridge.kill_vm()
         except AttributeError:  # java_vm was already killed
             pass
Beispiel #4
0
def uninit_javabridge():
    try:
        javabridge
        if vm_is_active() and vm_is_thread_alive():
            javabridge.kill_vm()
    except NameError:
        return
Beispiel #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
Beispiel #6
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()
Beispiel #7
0
def cp_stop_vm(kill=True):
    '''Shut down the Java VM

    Check for headlessness and the state of ImageJ and take
    whatever action is needed to stop AWT and the JVM.
    '''
    from imagej.imagej2 import allow_quit, the_imagej_context

    try:
        ij1 = javabridge.JClassWrapper("ij.IJ").getInstance()
    except javabridge.JavaException as e:
        logger.debug("No available instance: %s" % str(e))
        ij1 = None

    if the_imagej_context is not None:
        #
        # Tell the app service that it's OK to quit without prompt
        #
        allow_quit()
        javabridge.call(the_imagej_context.getContext(), "dispose", "()V")
    if ij1 is not None:
        #
        # Yes, the proper way to get ImageJ to quit is
        # to start it.
        #
        ij1.run()
    if kill:
        javabridge.kill_vm()
Beispiel #8
0
def tearDownModule():
    try:
        os.environ['PYJNIUS_ACTIVATE']
    except KeyError:
        import javabridge
        javabridge.detach()
        javabridge.kill_vm()
Beispiel #9
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()
Beispiel #10
0
def cp_stop_vm(kill=True):
    '''Shut down the Java VM

    Check for headlessness and the state of ImageJ and take
    whatever action is needed to stop AWT and the JVM.
    '''
    from imagej.imagej2 import allow_quit, the_imagej_context

    try:
        ij1 = javabridge.JClassWrapper("ij.IJ").getInstance()
    except javabridge.JavaException as e:
        logger.debug("No available instance: %s" % str(e))
        ij1 = None

    if the_imagej_context is not None:
        #
        # Tell the app service that it's OK to quit without prompt
        #
        allow_quit()
        javabridge.call(the_imagej_context.getContext(), "dispose", "()V")
    if ij1 is not None:
        #
        # Yes, the proper way to get ImageJ to quit is
        # to start it.
        #
        ij1.run()
    if kill:
        javabridge.kill_vm()
    def on_close(self, evt=None):
        # Classifier needs to be told to close so it can clean up it's threads
        classifier = wx.FindWindowById(ID_CLASSIFIER) or wx.FindWindowByName("Classifier")
        if classifier and classifier.Close() == False:
            return
        if any(wx.GetApp().get_plots()):
            dlg = wx.MessageDialog(
                self,
                "Some tools are open, are you sure you want to quit CPA?",
                "Quit CellProfiler Analyst?",
                wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
            )
            response = dlg.ShowModal()
            if response != wx.ID_YES:
                return
        try:
            import javabridge

            javabridge.kill_vm()
        except:
            print "Failed to kill the Java VM"
        # Blow up EVVVVERYTHIIINGGG!!! Muahahahahhahahah!
        for win in wx.GetTopLevelWindows():
            logging.debug("Destroying: %s" % (win))
            win.Destroy()
        if self.tbicon is not None:
            self.tbicon.Destroy()
        self.Destroy()
Beispiel #12
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)
Beispiel #13
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()  
Beispiel #14
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()
Beispiel #15
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()
Beispiel #16
0
def kill_jvm():
    """
    Kill the JVM. Once killed, it cannot be restarted.
    See the python-javabridge documentation for more information.
    """
    jv.kill_vm()
    VM_KILLED = True
def main():
    javabridge.start_vm(class_path=bioformats.JARS)
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    sys.exit(app.exec_())
    javabridge.kill_vm()
Beispiel #18
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
Beispiel #19
0
    def on_close(self, evt=None):
        # Classifier needs to be told to close so it can clean up it's threads
        classifier = wx.FindWindowById(ID_CLASSIFIER) or wx.FindWindowByName(
            'Classifier')
        if classifier and classifier.Close() == False:
            return

        if any(wx.GetApp().get_plots()):
            dlg = wx.MessageDialog(
                self,
                'Some tools are open, are you sure you want to quit CPA?',
                'Quit CellProfiler Analyst?',
                wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
            response = dlg.ShowModal()
            if response != wx.ID_YES:
                return

        try:
            logging.debug("Shutting of Java VM")
            import javabridge
            if javabridge.get_env() is not None:
                javabridge.kill_vm()
        except:
            logging.debug("Failed to kill the Java VM")

        # Blow up EVVVVERYTHIIINGGG!!! Muahahahahhahahah!
        for win in wx.GetTopLevelWindows():
            logging.debug('Destroying: %s' % (win))
            win.Destroy()
        if self.tbicon is not None:
            self.tbicon.Destroy()

        self.Destroy()
Beispiel #20
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()
Beispiel #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
Beispiel #22
0
def stop():
    """
    Kills the JVM.
    """
    global started
    if started is not None:
        started = None
        javabridge.kill_vm()
Beispiel #23
0
def loadSeriesFromFile(filepath, series) -> (BioMeta, np.ndarray):

    meta = loadBioMetaSeriesFromFile(filepath, series)
    image = loadBioImageSeriesFromFile(filepath, meta)

    javabridge.kill_vm()

    return meta, image
Beispiel #24
0
def tearDownModule():
    try:
        os.environ["PYJNIUS_ACTIVATE"]
    except KeyError:
        import javabridge

        javabridge.detach()
        javabridge.kill_vm()
def stop():
    """
    Kills the JVM.
    """
    global started
    if started is not None:
        started = None
        javabridge.kill_vm()
Beispiel #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()
Beispiel #27
0
    def __call__(self, id, filename, uri, **kwargs):
        """
        Extract metadata from a Datafile using the get_meta function and save
        the outputs as DatafileParameters. This function differs from
        process_meta in that it generates an output directory in the metadata
        store and passes it to the metadata processing func so that outputs
        e.g., preview images or metadata files) can be saved.

        param id: Datafile ID
        type id: integer

        param filename: Absolute path to a file for processing
        type filename: string

        param uri: Dataset URI
        type uri: string

        param kwargs: Extra arguments
        type kwargs: object

        return Extracted metadata
        rtype dict
        """

        _, extension = os.path.splitext(filename)
        if extension[1:] not in bioformats.READABLE_FORMATS:
            return None

        logger.info("Applying Bioformats filter to {}...".format(filename))

        # Need to start a JVM in each thread
        check_and_start_jvm()

        try:
            javabridge.attach()
            shush_logger()

            thumb_rel_path, thumb_abs_path = get_thumbnail_paths(
                id, filename, uri)

            if not os.path.exists(os.path.dirname(thumb_abs_path)):
                os.makedirs(os.path.dirname(thumb_abs_path))

            rsp = get_meta(filename, os.path.dirname(thumb_abs_path), **kwargs)
            if rsp is not None:
                metadata = []
                for i in rsp:
                    metadata.append(self.filter_metadata(i))
                return metadata[0]

        except Exception as e:
            logger.error(str(e))
            logger.debug(traceback.format_exc())
        finally:
            javabridge.detach()
            javabridge.kill_vm()

        return None
Beispiel #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()
Beispiel #29
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()
Beispiel #30
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()
Beispiel #31
0
def load_file(tiffile, pixel_per_nm=None, _init_vm=True, _kill_vm_on_end=True):
    # get metadata
    metadata = os.popen(
      os.path.dirname(os.path.realpath(__file__))+'/showinf ' + tiffile +\
      ' -nopix'
    ).read()

    #Acquisition
    if pixel_per_nm is None:
        if "Nanometers per pixel (X)" in metadata:
            pixel_size_x = float(metadata.split("Nanometers per pixel (X)",1)[-1]\
                          .split('\n')[0]\
                          .split(':')[-1].strip())
            pixel_size_y = float(metadata.split("Nanometers per pixel (Y)",1)[-1]\
                          .split('\n')[0]\
                          .split(':')[-1].strip())
        else:
            print(
                'Unknown TEM file format. Could not read nm per pixel. Set to 1'
            )
            pixel_size_x = 1
            pixel_size_y = 1

        if (pixel_size_x != pixel_size_y):
            print("WARNING. PIXEL SIXE X != PIXEL SIZE Y. UNEXPECTED")
            print(f'Pixel Size X: {pixel_size_x}')
            print(f'Pixel Size Y: {pixel_size_y}')
            print('Continuing with pixel size X only')

        nm_per_pixel = float(pixel_size_x)

    else:
        nm_per_pixel = 1 / pixel_per_nm

    # get image
    if _init_vm:
        init_vm()

    #properly select image reader to load data
    #see: https://github.com/CellProfiler/python-bioformats/issues/23
    image_reader = bioformats.formatreader.make_image_reader_class()()
    image_reader.allowOpenToCheckType(True)
    image_reader.setId(tiffile)
    wrapper = bioformats.formatreader.ImageReader(path=tiffile,
                                                  perform_init=False)
    wrapper.rdr = image_reader
    data = wrapper.read()[::-1, :].T

    if _kill_vm_on_end:
        javabridge.kill_vm()

    Nx, Ny = data.shape

    x = [x * nm_per_pixel for x in range(Nx)]
    y = [x * nm_per_pixel for x in range(Ny)]
    return {'x': x, 'y': y, 'data': data, 'nm_per_pixel': nm_per_pixel}
def extract_metadata(filepath,cycle_vm=True,meta_number=None,stage_loop=True,z_stack=False):
    if cycle_vm:
        javabridge.start_vm(class_path=bioformats.JARS)
    biof=extract_meta_bioformats(filepath)
    if meta_number is not None:
        filepath=change_file_num(filepath,meta_number)
    metadata=extract_meta_manual(filepath,metadata=biof,stage_loop=stage_loop,z_stack=z_stack)
    if cycle_vm:
        javabridge.kill_vm()
    return metadata
Beispiel #33
0
    def stop(self):
        if javabridge is None:
            return

        if self.isRunning:
            javabridge.kill_vm()
            self.isRunning = False
            print('bJavaBridge.stop()')
        else:
            print('bJavaBridge.stop() javabridge is not running')
Beispiel #34
0
def done():
    """Kill the JVM. Once killed, it cannot be restarted.

    Notes
    -----
    See the python-javabridge documentation for more information.
    """
    jv.kill_vm()
    global VM_KILLED
    VM_KILLED = True
Beispiel #35
0
def done():
    """Kill the JVM. Once killed, it cannot be restarted.

    Notes
    -----
    See the python-javabridge documentation for more information.
    """
    jv.kill_vm()
    global VM_KILLED
    VM_KILLED = True
Beispiel #36
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()
Beispiel #37
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()
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()
Beispiel #39
0
def write_ome_tiff(path,
                   img,
                   name,
                   fluor_names,
                   channel_names=None,
                   pixel_type=bioformats.omexml.PT_UINT16):
    FIELD_FLUOR = 'Fluor'
    omexml = bioformats.omexml.OMEXML()
    omexml.image(0).Name = name
    omexml.image(0).Pixels.PixelType = pixel_type
    omexml.image(0).Pixels.DimensionOrder = bioformats.omexml.DO_XYCZT
    omexml.image(0).Pixels.SizeX = img.shape[2]
    omexml.image(0).Pixels.SizeY = img.shape[1]
    omexml.image(0).Pixels.SizeC = img.shape[0]
    omexml.image(0).Pixels.SizeZ = 1
    omexml.image(0).Pixels.SizeT = 1
    assert len(fluor_names) == img.shape[0]
    if channel_names is None:
        channel_names = fluor_names
    assert (len(fluor_names) == len(channel_names))
    omexml.image(0).Pixels.channel_count = len(fluor_names)
    for i, (fluor_name,
            channel_name) in enumerate(zip(fluor_names, channel_names)):
        omexml.image(0).Pixels.Channel(i).Name = channel_name
        omexml.image(0).Pixels.Channel(i).SamplesPerPixel = 1
        omexml.image(0).Pixels.Channel(i).node.set(FIELD_FLUOR, fluor_name)
    javabridge.start_vm(class_path=bioformats.JARS)
    try:
        env = javabridge.get_env()
        buffer = env.make_object_array(img.shape[0], env.find_class('[B'))
        for i, channel_img in enumerate(img):
            channel_data = np.frombuffer(
                np.ascontiguousarray(channel_img).data, np.uint8)
            env.set_object_array_element(buffer, i,
                                         env.make_byte_array(channel_data))
        javabridge.run_script(
            """
            importClass(Packages.loci.common.DebugTools);
            importClass(Packages.loci.common.services.ServiceFactory);
            importClass(Packages.loci.formats.services.OMEXMLService);
            importClass(Packages.loci.formats.ImageWriter);
            DebugTools.enableLogging("INFO");
            var service = new ServiceFactory().getInstance(OMEXMLService);
            var metadata = service.createOMEXMLMetadata(xml);
            var writer = new ImageWriter();
            writer.setMetadataRetrieve(metadata);
            writer.setWriteSequentially(true);
            writer.setId(path);
            for (var i = 0; i < buffer.length; ++i) {
                writer.saveBytes(i, buffer[i]);
            }
            writer.close();
            """, dict(path=path, xml=omexml.to_xml(), buffer=buffer))
    finally:
        javabridge.kill_vm()
Beispiel #40
0
def readVWS(path):
    javabridge.start_vm(class_path=bf.JARS)

    try:
        logging.info('Data path: {}'.format(path))
        md = bf.get_omexml_metadata(path)
        # img = bf.ImageReader(path, perform_init=True)
    finally:
        javabridge.kill_vm()

    return md
def shutdown_cellprofiler():
    '''Oh sadness and so many threads that won't die...
    
    '''
    try:
        import javabridge
        javabridge.kill_vm()
    except:
        pass
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        pass
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 end_javabridge():

    ''' End the java virtual machine.

    Notes
    -----
    When killed, it cannot be restarted.
    '''

    global JVM_END

    javabridge.kill_vm()

    JVM_END = True
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 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()
Beispiel #46
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)
    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 main():
    #
    # For Windows build with Ilastik, look for site-packages
    # in order to find Ilastik sources.
    #
    if hasattr(sys, 'frozen') and sys.platform == "win32":
        root = os.path.split(sys.argv[0])[0]
        if len(root) == 0:
            root = os.curdir
        root = os.path.abspath(root)
        site_packages = os.path.join(root, 'site-packages').encode('utf-8')
        if os.path.exists(site_packages) and os.path.isdir(site_packages):
            import site
            site.addsitedir(site_packages)
    #
    # For OS/X set up the UI elements that users expect from
    # an app.
    #
    if sys.platform == "darwin":
        from cellprofiler.icons import get_builtin_images_path
        
        icon_path = os.path.join(get_builtin_images_path(), "CellProfilerIcon.png")
        os.environ["APP_NAME_%d" % os.getpid()] = "CellProfilerWorker"
        os.environ["APP_ICON_%d" % os.getpid()] = icon_path
    
    # Start the JVM
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    
    deadman_start_socket = the_zmq_context.socket(zmq.PAIR)
    deadman_start_socket.bind(DEADMAN_START_ADDR)
    
    # Start the deadman switch thread.
    start_daemon_thread(target=exit_on_stdin_close, 
                        name="exit_on_stdin_close")
    deadman_start_socket.recv()
    deadman_start_socket.close()
    
    from cellprofiler.knime_bridge import KnimeBridgeServer
    with AnalysisWorker(work_announce_address) as worker:
        worker_thread = threading.Thread(target = worker.run, 
                                         name="WorkerThread")
        worker_thread.setDaemon(True)
        worker_thread.start()
        with KnimeBridgeServer(the_zmq_context,
                               knime_bridge_address,
                               NOTIFY_ADDR, NOTIFY_STOP):
            enter_run_loop()
            worker_thread.join()
            
    #
    # Shutdown - need to handle some global cleanup here
    #
    try:
        from ilastik.core.jobMachine import GLOBAL_WM
        GLOBAL_WM.stopWorkers()
    except:
        logger.warn("Failed to stop Ilastik")
    try:
        from imagej.imagej2 import allow_quit
        allow_quit()
    except:
        logger.warn("Failed to signal ImageJ to stop")
    try:
        J.kill_vm()
    except:
        logger.warn("Failed to stop the Java VM")
def JVdone():
    jv.kill_vm()
    global VM_KILLED
    VM_KILLED = True
Beispiel #51
0
                
            def run(self):
                self.esteem = "through the roof"
        
        proxy = MyProxy()
        J.JWrapper(proxy.o).run()
        self.assertEqual(proxy.esteem, "through the roof")
        
    def test_01_04_args(self):
        my_observable = J.make_instance("java/util/Observable", "()V")
        def update(observable, obj):
            self.assertTrue(J.JWrapper(observable).equals(my_observable))
            self.assertTrue(J.JWrapper(obj).equals("bar"))
        proxy = J.JProxy('java.util.Observer', dict(update=update))
        J.JWrapper(proxy.o).update(my_observable, "bar")
        
    def test_01_05_return_value(self):
        def call():
            return "foo"
        proxy = J.JProxy('java.util.concurrent.Callable',
                         dict(call = call))
        self.assertEquals(J.JWrapper(proxy.o).call(), "foo")
    
if __name__=="__main__":
    import javabridge
    javabridge.start_vm()
    try:
        unittest.main()
    finally:
        javabridge.kill_vm()
def main():
    # Parse command line input
    parser = configure_parser()
    args = parser.parse_args()

    metadata = json.loads(args.input_metadata)
    experiment_path = path.expanduser(args.experiment_path)
    vsi_path = path.join(experiment_path, metadata['vsiPath'])

    net_path = path.expanduser(args.net_path)
    model_path = path.expanduser(args.model_path)
    output_path = path.expanduser(args.output_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)
    detector_chunker_params = {
        'chunk_size': args.chunk_size,
        'stride': 6,
        'window_size': 49,
        'num_classes': 2
    }

    vsi_chunker_params = {
        'chunk_size': args.chunk_size,
        'stride': 6,
        'window_size': 43,
        'num_classes': 1
    }

    cell_detector = detection.CellDetector(net=fish_net, cell_radius=11, signal_channel=0, chunker_params=detector_chunker_params)
    cell_detector.set_mode_cpu()

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

    # Creates an image descriptor from a metadata entry
    image_descriptor = data.ImageDescriptor.from_metadata(
        metadata, 
        experiment_path=experiment_path,
        flip=args.flip,
        flop=args.flop
    )

    vsi_image = load_vsi(vsi_path)
    javabridge.kill_vm() # Caffe will segfault if the jvm is running...

    vsi_chunker = detection.ImageChunkerWithOutput(vsi_image.transpose(2,0,1), **vsi_chunker_params)
    vsi_chunker.allocate_output()

    prev_end_row = prev_end_col = -1
    for chunk, _ in vsi_chunker:
        cell_detector.set_image(chunk.transpose(1,2,0))
        detected_cells = cell_detector.detect_cells()
        start_row, start_col = vsi_chunker.current_chunk_bounds[:2]

        detected_cells = shift_cells(detected_cells, start_row, start_col)
        detected_cells = filter_duplicates(detected_cells, prev_end_row, prev_end_col)

        physical_cells = map(lambda cell: data.PhysicalCell.from_cell(cell, VSI_PIXEL_SCALE), detected_cells)
        print "Adding %d cells to descriptor" % len(physical_cells)
        image_descriptor.cells += physical_cells

        prev_end_row, prev_end_col = vsi_chunker.current_chunk_bounds[2:]
        # Correct for borders. I just calculated these by hand. It's a script. Get over it.
        prev_end_row -= 21
        prev_end_col -= 21


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

    # pickle the image descriptor
    output_file = open(output_path, 'w')
    pickle.dump(image_descriptor, output_file)
    output_file.close()
    dist = setup(
        console=[{'script':'CellProfiler.py',
                  'icon_resources':[(1,'CellProfilerIcon.ico')]},
                 {'script':'cellprofiler\\analysis_worker.py',
                  'icon_resources':[(1,'CellProfilerIcon.ico')]}],
        name='Cell Profiler',
        data_files = data_files,
        cmdclass={'msi':CellProfilerMSI,
                  'codesign':CellProfilerCodesign
                  },
        options=opts)
except:
    import traceback
    traceback.print_exc()
finally:
    for tempfile, relative_path in rofiles:
        # TODO: extra credit for finding where the distribution
        #       is and changing the files back to read-only
        os.remove(tempfile)
        
    try:
        from javabridge import kill_vm
        kill_vm()
        sys.stderr.flush()
        sys.stdout.flush()
        os._exit(0)
    except:
        import traceback
        traceback.print_exc()
        print "Caught exception while killing VM"
Beispiel #54
0
 def stop_vm(self):
     javabridge.detach()
     javabridge.kill_vm()
Beispiel #55
0
 def finalize(self, result):
     javabridge.kill_vm()
Beispiel #56
0
 def stop():
     """
     Kills the JVM
     """
     javabridge.kill_vm()
       ## if (FilesNum == FilesNumOld):
       ##     CtrlWhile += 1 

## saves all input files in single tif
##CntFull=0
##DataSave = np.zeros((int(SeqSize),Data_temp.shape[0],Data_temp.shape[1]))
##for Files_fn in FilesOnFold:
##    if FullData [CntFull][0]:
##        DataSave[CntFull,:,:] = FullData [CntFull][1][:,:]
##    CntFull+=1
##
##OutPutSingleFile = '../'+WorkFolderName+'.tif'
##with tifffile.TiffWriter(OutPutSingleFile, bigtiff=True) as tif:
##    tif.save(DataSave, compress=0)

jv.kill_vm()
print "All Done!"

## saves all Bacground substracted files in single tif
FileNamePattern = BckgSubs_fn + '*'
FilesOnFold = sorted(glob.glob(FileNamePattern), key=os.path.getmtime)
Char1 = 'bool'
Data_temp = tifffile.imread(FilesOnFold)
Char2 = '('+str(Data_temp.shape[0])+','+str(Data_temp.shape[1])+')float32'
Char = Char1+', '+Char2
FullData = np.zeros(len(FilesOnFold),dtype = Char)
CntFull = 0
for Files_fn in FilesOnFold:
    if ~FullData [CntFull][0]:
        Data_temp = tifffile.imread(Files_fn)
        FullData [CntFull][0] = 1
def kill_vm():
    javabridge.detach()
    javabridge.kill_vm()
def main(args):
	javabridge.start_vm(class_path=bf.JARS)
	
	try:
		lifdir = sys.argv[1]
	except IndexError:
		print "Usage: %s lif_directory [sdx] [sdy] [sdz]" % os.path.basename(sys.argv[0])
		sys.exit(1)
		
	try:
            sdx, sdy, sdz = int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])
        except IndexError:
            print "Using default values for standard deviation"
            sdx, sdy, sdz = 4, 4, 3
		
	md = bf.get_omexml_metadata(lifdir)
	mdo = bf.OMEXML(md)	
	rdr = bf.ImageReader(lifdir, perform_init=True)
	
	
	names, sizes, resolutions = parse_xml_metadata(md)
	
	
	print '%s contains:' % lifdir
	for i in range(mdo.image_count):
		print '%d, %s, %s' % (i,names[i],(sizes[i],))
	
	
	
	for im_num in range(mdo.image_count):
		im_size = ()
		im_size = [sizes[im_num][1],sizes[im_num][2],sizes[im_num][0],3]
	
		image3d = np.empty(im_size, np.uint8)
		print 'projecting: %d, %s, %s' % (im_num,names[im_num],(sizes[im_num],))
		
		z_size = sizes[im_num][0]
		
		flush_message('Importing...')
		for z in range(z_size):
			image3d[:,:,z,:] = rdr.read(z=z, series=im_num, rescale=False)
		print 'done'

		#ma = greyScale(image3d)
		ma = np.amax(image3d, 3)
		
		#sdx, sdy, sdz = 4, 4, 3
		sds = 4
	
		output_dir = "./proj_%s_%s" % (lifdir, im_num)
	
		if not os.path.exists(output_dir):
			os.makedirs(output_dir)
		
		max_proj = np.amax(ma, axis=2)
		
		mpfilename = os.path.join(output_dir, "max-proj.png")
		scipy.misc.imsave(mpfilename, max_proj)
		
		bl = nd.gaussian_filter(ma, [sdx, sdy, sdz])

		#ps = find_projection_surface(bl)
		ps = proj.max_indices_z(bl)

		sps = nd.gaussian_filter(ps, sds)
		
		vis_factor = 255 / z_size
		sfilename = os.path.join(output_dir, "surface-g3d-%d-%d-%d-%d.png" % (sdx, sdy, sdz, sds))
		scipy.misc.imsave(sfilename, sps * vis_factor)

		res = proj.projection_from_surface(ma, sps, dm=3, dp=0)

		filename = os.path.join(output_dir, "proj-g3d-%d-%d-%d-%d.png" % (sdx, sdy, sdz, sds))
		pmax = np.amax(res)

		vis_scale = 255 / pmax

		scipy.misc.imsave(filename, res * vis_scale)

		flush_message("Post processing...")
		pp = projpp.proj_filter(res * vis_scale, 3, 60, 15)
		print " done"
		filename = os.path.join(output_dir, 'proj-pp-%d-%d-%d-%d.png' % (sdx, sdy, sdz, sds))
		scipy.misc.imsave(filename, pp)
	
	javabridge.kill_vm()
	return 0