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 read_metadata_with_bioformats(self):
     # Using javabridge to parse and store all required metadata
     # -------------------------------------------------
     javabridge.start_vm(class_path=bioformats.JARS,
                         run_headless=True,
                         max_heap_size='2G')
     bioformats_log4j.basic_config()  # Configure logging for "WARN" level
     rdr = javabridge.JClassWrapper('loci.formats.in.ZeissCZIReader')()
     # ZeissCZIReader is the file format reader for Zeiss .czi files.
     # rdr = javabridge.JClassWrapper('loci.formats.in.OMETiffReader')()
     # OMETiffReader is the file format reader for OME-TIFF files
     rdr.setOriginalMetadataPopulated(True)
     # Specifies whether to save proprietary metadata in the MetadataStore.
     clsOMEXMLService = javabridge.JClassWrapper(
         'loci.formats.services.OMEXMLService')
     # Classes in loci.formats.services that implement OMEXMLService
     serviceFactory = javabridge.JClassWrapper(
         'loci.common.services.ServiceFactory')()
     # Constructor loading runtime initiation of services from the defult location
     service = serviceFactory.getInstance(
         clsOMEXMLService.klass
     )  # Retrieves an instance of a given service.
     omemetadata = service.createOMEXMLMetadata()
     # Creates OME-XML metadata object using reflection - avoids direct dependencies on loci.formats.ome package.
     rdr.setMetadataStore(
         omemetadata)  # Sets the default metadata store for this reader.
     rdr.setId(self.fname)
     # Initializes a reader from the input file name.
     # Calls initFile(String id) to initializes the input file
     # Reads all of the metadata and sets the reader up for reading planes. This can take time here...
     # rdr.saveOriginalMetadata = True
     orgmetadata = service.getOriginalMetadata(omemetadata)
     globalmetadata = rdr.getGlobalMetadata()
     # Obtains the hashtable containing the metadata field/value pairs from the current file.
     keys = globalmetadata.keySet().toString().split(',')
     nkeys = globalmetadata.size()
     metadatavalues = globalmetadata.values().toString().split(',')
     metadatakeys = globalmetadata.keys()
     # Get additional atrributes
     self.dimOrder = copy.deepcopy(rdr.getDimensionOrder())
     self.isChannelInterleaved = copy.deepcopy(rdr.isInterleaved())
     self.isDimOrderCertain = copy.deepcopy(rdr.isOrderCertain())
     for i in np.arange(0, nkeys):
         key = metadatakeys.nextElement()
         value = globalmetadata.get(key)
         # print(key,value)
         if key in zeiss_keymaps.linescan:
             self.metadata[
                 zeiss_keymaps.linescan[key]] = comfun.string_convert(value)
     # -----------------------------------
     javabridge.kill_vm()  # kill javabridge
     print(self.metadata)
     print('Metadata reading successful')
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
Example #5
0
def load_vsi(vsi_path):
    """
    Load a vsi image at the given path.  The channels that are loaded, 
     and the order in which they are loaded are currently hard coded

    Note: This requires an active jvm via javabridge 
     (i.e. javabridge.start_vm(class_path=bioformats.JARS) must have been called prior to using this function)
    """
    javabridge.start_vm(class_path=bioformats.JARS)
    log4j.basic_config()

    print "Loading %s" % vsi_path
    with bioformats.ImageReader(vsi_path) as reader:
        dapi = reader.read(c=0, rescale=False).astype(numpy.uint16)
        cfos = reader.read(c=1, rescale=False).astype(numpy.uint16)

    javabridge.kill_vm()
    return numpy.dstack((numpy.zeros(cfos.shape,
                                     dtype=numpy.uint16), cfos, dapi))
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
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()
Example #8
0
def load_vsi(vsi_path):
    """
    Load a vsi image at the given path.  The channels that are loaded, 
     and the order in which they are loaded are currently hard coded

    This function uses the bioformats and javabridge packages. It also 
     requires a functional jvm
    """
    import javabridge
    import bioformats
    from bioformats import log4j

    javabridge.start_vm(class_path=bioformats.JARS)
    log4j.basic_config()
    print "Loading %s" % vsi_path

    with bioformats.ImageReader(vsi_path) as reader:
        dapi = reader.read(c=0, rescale=False).astype(numpy.uint16)
        cfos = reader.read(c=1, rescale=False).astype(numpy.uint16)

    javabridge.kill_vm()

    return numpy.dstack((cfos, dapi))
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()
Example #10
0
def process_meta_file_output(df_id, schema_name, overwrite=False, **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.

    Parameters
    ----------
    df_id: int
        ID of Datafile instance to process.
    schema_name: str
        Names of schema which describes ParameterNames
    add: Boolean (default: False)
        Specifies whether or not to add to an existing Parameterset for this
        Datafile rather that overwriting or exiting.
    overwrite: Boolean (default: False)
        Specifies whether to overwrite any exisiting parametersets for
        this datafile.


    Returns
    -------
    None
    """
    from .metadata import get_meta

    if acquire_datafile_lock(df_id):
        # Need to start a JVM in each thread
        check_and_start_jvm()

        try:
            javabridge.attach()
            log4j.basic_config()
            schema = Schema.objects.get(namespace__exact=schema_name)
            df = DataFile.objects.get(id=df_id)
            if DatafileParameterSet.objects\
                    .filter(schema=schema, datafile=df).exists():
                if overwrite:
                    psets = DatafileParameterSet.objects.get(schema=schema,
                                                             datafile=df)
                    logger.warning("Overwriting parametersets for %s",
                                   df.filename)
                    for ps in psets:
                        delete_old_parameterset(ps)
                else:
                    logger.warning("Parametersets for %s already exist.",
                                   df.filename)
                    return

            dfo = DataFileObject.objects.filter(datafile__id=df.id,
                                                verified=True).first()
            input_file_path = dfo.get_full_path()

            output_rel_path = os.path.join(
                os.path.dirname(urlparse.urlparse(dfo.uri).path), str(df.id))
            output_path = os.path.join(settings.METADATA_STORE_PATH,
                                       output_rel_path)

            if not os.path.exists(output_path):
                os.makedirs(output_path)

            logger.debug("Processing file: %s" % input_file_path)
            metadata_params = get_meta(input_file_path, output_path, **kwargs)
            if not metadata_params:
                logger.debug("No metadata to save")
                return

            for sm in metadata_params:
                ps = DatafileParameterSet(schema=schema, datafile=df)
                ps.save()

                logger.debug("Saving parameters for: %s", input_file_path)
                save_parameters(schema, ps, sm)
        except Exception as err:
            logger.exception(err)
        finally:
            release_datafile_lock(df_id)
            javabridge.detach()
Example #11
0
def process_meta(func, df, schema_name, overwrite=False, **kwargs):
    """Extract metadata from a Datafile using a provided function and save the
    outputs as DatafileParameters.

    Parameters
    ----------
    func: Function
        Function to extract metadata from a file. Function must have
        input_file_path as an argument e.g.:
        def meta_proc(input_file_path, **kwargs):
            ...
        It must return a dict containing ParameterNames as keys and the
        Parameters to be saved as values. Parameters (values) can be singular
        strings/numerics or a list of strings/numeric. If it's a list, each
        element will be saved as a new DatafileParameter.
    df: tardis.tardis_portal.models.Datafile
        Datafile instance to process.
    schema_name: str
        Names of schema which describes ParameterNames
    add: boolean (default: False)
        Specifies whether or not to add to an existing Parameterset for this
        Datafile rather that overwriting or exiting.
    overwrite: boolean (default: False)
        Specifies whether to overwrite any exisiting parametersets for
        this datafile.


    Returns
    -------
    None
    """
    if acquire_datafile_lock(df.id):
        # Need to start a JVM in each thread
        check_and_start_jvm()

        try:
            javabridge.attach()
            log4j.basic_config()
            schema = Schema.objects.get(namespace__exact=schema_name)
            if DatafileParameterSet.objects\
                    .filter(schema=schema, datafile=df).exists():
                if overwrite:
                    psets = DatafileParameterSet.objects.get(schema=schema,
                                                             datafile=df)
                    logger.warning("Overwriting parametersets for %s"
                                   % df.filename)
                    [delete_old_parameterset(ps) for ps in psets]
                else:
                    logger.warning("Parametersets for %s already exist."
                                   % df.filename)
                    return

            dfo = DataFileObject.objects.filter(datafile__id=df.id,
                                                verified=True).first()
            input_file_path = dfo.get_full_path()

            logger.debug("Processing file: %s" % input_file_path)
            metadata_params = func(input_file_path, **kwargs)

            if not metadata_params:
                logger.debug("No metadata to save")
                return

            for sm in metadata_params:
                ps = DatafileParameterSet(schema=schema, datafile=df)
                ps.save()

                logger.debug("Saving parameters for: %s" % input_file_path)
                save_parameters(schema, ps, sm)
        except Exception, e:
            logger.debug(e)
        finally:
Example #12
0
def main():
    from sys import argv
    if len(argv) < 3:
        print "Insufficient Arguments!"
        print "Proper Usage: %s [vsi_path] [model_path]" % argv[0]
        return

    fisherman_path = path.expanduser('~/code/fisherman')
    net_path = path.join(fisherman_path, 'caffe/fish_net_conv_deploy.prototxt')
    model_path = path.expanduser(argv[2])

    fish_net = caffe.Net(net_path, model_path, caffe.TEST)
    chunker_params = {'chunk_size': 954, 'window_size': 49}

    # Start and configure jvm
    javabridge.start_vm(class_path=bioformats.JARS)
    log4j.basic_config()

    print "Loading Image..."
    with bioformats.ImageReader(path=path.expanduser(argv[1])) as reader:
        cfos = reader.read(c=1, rescale=False)
        cfos = (median_normalize(cfos) * 25).astype(numpy.uint8)

        #dapi = reader.read(c=0, rescale=False)
        #dapi = cast_to_uint8(math.normalize_array(dapi))

    #input_image = numpy.dstack((cfos, dapi))
    input_image = cfos

    javabridge.kill_vm()

    print "Casting Fish Net..."
    detector = detection.CellDetector(image=input_image,
                                      net=fish_net,
                                      chunker_params=chunker_params,
                                      cell_radius=12)

    detector.set_mode_gpu()

    raw_mask = detector.get_fish_net_mask()

    print "Post processing mask..."
    clean_mask = detector.clean_fish_net_mask(raw_mask)

    raw_mask = detector.scale_mask_to_image(raw_mask)
    print "Image shape: ", input_image.shape
    print "Raw mask shape: ", raw_mask.shape

    figure()
    imshow(cfos, cmap=cm.Greys_r)
    imshow(raw_mask, alpha=.35, cmap=cm.Reds)
    title('Raw Mask')

    raw_mask = None
    clean_mask = detector.scale_mask_to_image(clean_mask)

    figure()
    imshow(cfos, cmap=cm.Greys_r)
    imshow(clean_mask, alpha=.35, cmap=cm.Reds)
    title('Clean Mask')

    show()

    return

    io.imsave('raw_mask_out.png',
              numpy.dstack([raw_mask.astype(numpy.uint8), input_image]))

    io.imsave('clean_mask_out.png',
              numpy.dstack([clean_mask.astype(numpy.uint8), input_image]))
def main():
    from sys import argv
    if len(argv) < 3:
        print "Not Enough Arguments!!"
        print "Proper Usage: %s [path_to_image] [model_path] [optional: save_path]" % argv[
            0]
        return
    elif len(argv) >= 4:
        save_path = path.expanduser(argv[3])
    else:
        save_path = None

    im_path = path.expanduser(argv[1])
    model_path = path.expanduser(argv[2])
    net_path = path.expanduser(
        '~/code/fisherman/caffe/fish_net_conv_deploy.prototxt')

    fish_net = caffe.Net(net_path, model_path, caffe.TEST)

    chunker_params = {
        'chunk_size': 954,
        'stride': 6,
        'window_size': 49,
        'num_classes': 2
    }

    detector = detection.CellDetector(net=fish_net,
                                      chunker_params=chunker_params,
                                      signal_channel=0,
                                      cell_radius=7)

    detector.set_mode_gpu()

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

    image = load_vsi(im_path)
    #image = io.imread(im_path).transpose(1, 2, 0)
    #image = math.median_normalize(image[..., 0]) * 25
    #image = image.astype(numpy.uint8)

    javabridge.kill_vm()

    detector.set_image(image)

    raw_mask = detector.get_fish_net_mask(cleaned=False,
                                          scaled=False).astype(numpy.float64)
    io.imsave('raw_mask_out.png', raw_mask)

    mask = detector.get_fish_net_mask(cleaned=True, scaled=True)
    io.imsave('mask_out.png', mask)

    labels = detector.separate_cell_mask(mask)
    print 'Detected %d cells!' % len(numpy.unique(labels))

    display_image = image.astype(numpy.float64) - image.min()
    display_image *= 1 / (numpy.percentile(display_image, 99.5))
    display_image[display_image > 1] = 1
    io.imsave('labels_out.png',
              color.label2rgb(labels[..., 0], image=display_image, bg_label=0))

    return

    mean_image = detector.get_mean_image()
    figure()
    imshow(mean_image)

    figure()
    imshow(image, cmap=cm.Greys_r)
    imshow(mask, cmap=cm.Reds, alpha=.35)

    figure()
    imshow(color.label2rgb(labels[..., 0], image=image))
    show()

    if save_path is not None:
        print "Saving image ....."
        io.imsave(save_path, color.label2rgb(cell_mask, image=image))
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()
Example #15
0
def process_meta(df_id, schema_name, overwrite=False, **kwargs):
    """Extract metadata from a Datafile using the get_meta function and save the
    outputs as DatafileParameters.

    Parameters
    ----------
    df_id: int
        ID of Datafile instance to process.
    schema_name: str
        Names of schema which describes ParameterNames
    add: boolean (default: False)
        Specifies whether or not to add to an existing Parameterset for this
        Datafile rather that overwriting or exiting.
    overwrite: boolean (default: False)
        Specifies whether to overwrite any exisiting parametersets for
        this datafile.


    Returns
    -------
    None
    """
    from .metadata import get_meta

    if acquire_datafile_lock(df_id):
        # Need to start a JVM in each thread
        check_and_start_jvm()

        try:
            javabridge.attach()
            log4j.basic_config()
            schema = Schema.objects.get(namespace__exact=schema_name)
            df = DataFile.objects.get(id=df_id)
            if DatafileParameterSet.objects\
                    .filter(schema=schema, datafile=df).exists():
                if overwrite:
                    psets = DatafileParameterSet.objects.get(schema=schema,
                                                             datafile=df)
                    logger.warning("Overwriting parametersets for %s",
                                   df.filename)
                    for ps in psets:
                        delete_old_parameterset(ps)
                else:
                    logger.warning("Parametersets for %s already exist.",
                                   df.filename)
                    return

            dfo = DataFileObject.objects.filter(datafile__id=df.id,
                                                verified=True).first()
            input_file_path = dfo.get_full_path()

            logger.debug("Processing file: %s", input_file_path)
            metadata_params = get_meta(input_file_path, **kwargs)

            if not metadata_params:
                logger.debug("No metadata to save")
                return

            for sm in metadata_params:
                ps = DatafileParameterSet(schema=schema, datafile=df)
                ps.save()

                logger.debug("Saving parameters for: %s", input_file_path)
                save_parameters(schema, ps, sm)
        except Exception as err:
            logger.exception(err)
        finally:
            release_datafile_lock(df_id)
            javabridge.detach()
def main():
    from sys import argv
    if len(argv) < 3:
        print "Insufficient Arguments!"
        print "Proper Usage: %s [vsi_images] [caffe_model]"
        return

    vsi_paths = get_matching_files(path.expanduser(argv[1]))
    model_path = path.expanduser(argv[2])
    net_path = path.expanduser('/groups/gray/image_processing/build/fisherman/caffe/fish_net_conv_deploy.prototxt')

    fish_net = caffe.Net(net_path, model_path, caffe.TEST)

    chunker_params = {
        'chunk_size': 954,
        'stride': 6,
        'window_size': 49,
        'num_classes': 2
    }

    detector = detection.CellDetector(
        net=fish_net,
        chunker_params=chunker_params,
        signal_channel=0,
        cell_radius=12
    )
    
    detector.set_compute_mask_on_signal_plane_only(True)
    detector.set_mode_cpu()

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

    for vsi_path in vsi_paths:
        print "Loading %s ....." % vsi_path
        reader = bioformats.ImageReader(vsi_path)
        cfos = reader.read(c=1, rescale=False)
        #dapi = reader.read(c=0, rescale=True).astype(numpy.uint8)

        print "vsi data type:", cfos.dtype;

        #image = numpy.dstack([cfos, dapi])
        image = cfos

        detector.set_image(image)

        raw_mask = detector.get_fish_net_mask(cleaned=False, scaled=False)
        cell_mask = detector.get_fish_net_mask(cleaned=True, scaled=True)
        io.imsave('/home/sam/Desktop/raw_mask_out.png', raw_mask)
        io.imsave('/home/sam/Desktop/clean_mask_out.png', cell_mask)
        del raw_mask

        cell_mask = detector.separate_cell_mask(cell_mask)

        image = crop_output(math.median_normalize(image) * 25)
        cell_mask = crop_output(cell_mask)

        output_base, _ = path.splitext(vsi_path)

        print "Saving image ....."

        for i in range(1,6):
            output_path = output_base + '_mask_out_%dx.png' % i
            io.imsave(output_path, color.label2rgb(cell_mask[..., 0], image=image.astype(numpy.uint8)*i, bg_label=0, alpha=0.15))
            io.imsave(output_base + '_no_labels_%dx.png' % i, image.astype(numpy.uint8)*i)

    javabridge.kill_vm()
Example #17
0
    def identifyHighDensityLargeSample(self,
                                       fileName,
                                       radius,
                                       outputMagnification,
                                       numberOfTilesX,
                                       numberOfTilesY,
                                       threshold=60,
                                       progressBar=None):

        javabridge.start_vm(class_path=bioformats.JARS,
                            run_headless=True,
                            max_heap_size='8G')

        try:
            log4j.basic_config()
            imageReader = bioformats.formatreader.make_image_reader_class()
            reader = imageReader()
            reader.setId(fileName)
            '''
            rdr = bioformats.get_image_reader(None, path=fileName)
            totalseries = 1
            try:
                totalseries = np.int(rdr.rdr.getSeriesCount())
            except:
                print("exc")
                totalseries = 1  # in case there is only ONE series
            '''
            #r = bioformats.get_image_reader(None, fileName)
            #r.rdr.openThumbBytes(10)

            # Get image Metadata
            ome = OMEXML(bioformats.get_omexml_metadata(path=fileName))
            sizeX = ome.image().Pixels.get_SizeX()
            sizeY = ome.image().Pixels.get_SizeY()

            physicalX = ome.image().Pixels.get_PhysicalSizeX()
            physicalY = ome.image().Pixels.get_PhysicalSizeY()

            print('Original size: ', sizeX, sizeY)
            print('Original physical pixel size: ', physicalX, physicalY)

            inputMagnification = np.round(
                np.float(
                    ome.instrument(0).Objective.get_NominalMagnification()), 0)

            # initialize variables
            tileBeginX = 0
            tileBeginY = 0
            #format_reader = bioformats.ImageReader(fileName).rdr
            tileCounter = 0

            hMosaicGray = []
            vMosaicDensity = []
            vMosaicGray = []

            for y in range(0, numberOfTilesY):  # <=

                # computing begin and height size
                tileBeginY = minMax(y, 0, numberOfTilesY, 0, sizeY)
                height = minMax(y + 1, 0, numberOfTilesY, 0,
                                sizeY) - tileBeginY

                for x in range(0, numberOfTilesX):  # <=

                    # computing begin and X size
                    tileBeginX = minMax(x, 0, numberOfTilesX, 0, sizeX)
                    width = minMax(x + 1, 0, numberOfTilesX, 0,
                                   sizeX) - tileBeginX

                    newResolution = computeResolution(physicalX, physicalY,
                                                      width, height,
                                                      inputMagnification,
                                                      outputMagnification)

                    # extracting tile

                    tile = reader.openBytesXYWH(0, tileBeginX, tileBeginY,
                                                width, height)
                    tile.shape = (height, width, 3)

                    # resize tile
                    tileResized = adaptiveResize(tile, newResolution)
                    tileGray = cv2.cvtColor(tileResized, cv2.COLOR_BGR2GRAY)

                    if (x > 0):

                        hMosaicGray = np.concatenate((hMosaicGray, tileGray),
                                                     axis=1)

                    else:

                        hMosaicGray = tileGray
                    # parallel computation goes here

                    tileCounter = tileCounter + 1

                if (y > 0):
                    vMosaicGray = np.concatenate((vMosaicGray, hMosaicGray),
                                                 axis=0)
                else:

                    vMosaicGray = hMosaicGray

                hMosaicGray = []
                progress = (tileCounter * 100) / (numberOfTilesX *
                                                  numberOfTilesY)
                print("processing", str(progress) + ' %')
                progressBar.setValue(progress)

            vMosaicDensity = self.identifyHighDensityTile(
                copy.deepcopy(vMosaicGray), radius)
            print("High density computation [ok]")
            high, low = self.connectedComponetsTile(vMosaicDensity,
                                                    vMosaicGray, threshold)
            print("Connected components [ok]")
        finally:
            javabridge.kill_vm()

        #self.writeDensityImage(vMosaicGray, fileName)
        print("success")
        return (high, low, vMosaicDensity, vMosaicGray)
Example #18
0
def process_meta_file_output(func, df, schema_name, overwrite=False, **kwargs):
    """Extract metadata from a Datafile using a provided 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.

    Parameters
    ----------
    func: Function
        Function to extract metadata from a file. Function must have
        input_file_path and output_path as arguments e.g.:
        def meta_proc(input_file_path, output_path, **kwargs):
            ...
        It must return a dict containing ParameterNames as keys and the
        Parameters to be saved as values. Parameters (values) can be singular
        strings/numerics or a list of strings/numeric. If it's a list, each
        element will be saved as a new DatafileParameter.
    df: tardis.tardis_portal.models.Datafile
        Datafile instance to process.
    schema_name: str
        Names of schema which describes ParameterNames
    add: Boolean (default: False)
        Specifies whether or not to add to an existing Parameterset for this
        Datafile rather that overwriting or exiting.
    overwrite: Boolean (default: False)
        Specifies whether to overwrite any exisiting parametersets for
        this datafile.


    Returns
    -------
    None
    """
    # import ipdb; ipdb.set_trace()
    # Need to start a JVM in each thread
    global mtbf_jvm_started
    if not mtbf_jvm_started:
        logger.debug("Starting a new JVM")
        javabridge.start_vm(class_path=bioformats.JARS, max_heap_size='4G',
                            run_headless=True)
        mtbf_jvm_started = True

    try:
        javabridge.attach()
        log4j.basic_config()
        schema = Schema.objects.get(namespace__exact=schema_name)
        if DatafileParameterSet.objects\
                .filter(schema=schema, datafile=df).exists():
            if overwrite:
                psets = DatafileParameterSet.objects.get(schema=schema,
                                                         datafile=df)
                logger.warning("Overwriting parametersets for %s"
                               % df.filename)
                [delete_old_parameterset(ps) for ps in psets]
            else:
                logger.warning("Parametersets for %s already exist."
                               % df.filename)
                return

        dfo = DataFileObject.objects.filter(datafile__id=df.id,
                                            verified=True).first()
        input_file_path = dfo.get_full_path()

        output_rel_path = os.path.join(
                    os.path.dirname(input_file_path),
                    str(df.id))
        output_path = os.path.join(
            settings.METADATA_STORE_PATH, output_rel_path)

        if not os.path.exists(output_path):
            os.makedirs(output_path)

        logger.debug("Processing file: %s" % input_file_path)
        metadata_params = func(input_file_path, output_path, **kwargs)
        if not metadata_params:
            logger.debug("No metadata to save")
            return

        for sm in metadata_params:
            ps = DatafileParameterSet(schema=schema, datafile=df)
            ps.save()

            logger.debug("Saving parameters for: %s" % input_file_path)
            save_parameters(schema, ps, sm)
    except Exception, e:
        logger.debug(e)
Example #19
0
#!/usr/bin/env python

import os
import javabridge
import bioformats
from bioformats import log4j
import sys

javabridge.start_vm(class_path=bioformats.JARS,
                    run_headless=True)
try:
    log4j.basic_config()
    if len(sys.argv) < 2:
        image_path = os.path.join(os.path.dirname(bioformats.__file__), 'tests', 
                                  'Channel1-01-A-01.tif')
    else:
        image_path = sys.argv[1]
    image, scale = bioformats.load_image(image_path, rescale=False,
                                         wants_max_intensity=True)
    try:
        import pylab
        
        pylab.imshow(image)
        pylab.gca().set_title(image_path)
        pylab.show()
    except:
        print image.shape
finally:
    javabridge.kill_vm()
    def Start(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():
                splash.Destroy()
                example_link_address = 'cellprofiler.org'
                dlg = wx.MessageDialog(None, 'CellProfiler Analyst requires a properties file. Download an example at %s' % (
                                           example_link_address), 'Properties file required', wx.OK)
                response = dlg.ShowModal()
                logging.error('CellProfiler Analyst requires a properties file. Exiting.')
                return False

        self.frame = MainGUI(p, None, size=(1000,-1))
        # def show_frame():
        #     import time
        #     res = self.frame.Show()
        # wx.CallAfter(show_frame)
        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=True)

        # removes the log4j warnings
        from bioformats import log4j
        log4j.basic_config()
        javabridge.attach()
        # javabridge.activate_awt()

        try:
            if __version__ != -1:
                import cpa.util.check_for_updates as cfu
                cfu.check_for_updates('http://cellprofiler.org/updates/CPA.html',
                                      max(__version__, cpa.cpaprefs.get_skip_version()),
                                      new_version_cb,
                                      user_agent='CPAnalyst/%s'%(__version__))
        except ImportError:
            logging.warn("CPA was unable to check for updates. Could not import cpa.util.check_for_updates.")
        
        self.frame.Show() # Show frame
        return True
Example #21
0
#!/usr/bin/env python

from __future__ import absolute_import, print_function, unicode_literals

import os
import javabridge
import bioformats
from bioformats import log4j
import sys

javabridge.start_vm(class_path=bioformats.JARS, run_headless=True)
try:
    log4j.basic_config()
    if len(sys.argv) < 2:
        image_path = os.path.join(os.path.dirname(bioformats.__file__), '..',
                                  'tests', 'resources', 'Channel1-01-A-01.tif')
    else:
        image_path = sys.argv[1]
    image, scale = bioformats.load_image(image_path,
                                         rescale=False,
                                         wants_max_intensity=True)
    try:
        import pylab

        pylab.imshow(image)
        pylab.gca().set_title(image_path)
        pylab.show()
    except:
        print(image.shape)
finally:
    javabridge.kill_vm()
 def setUp(self):
     J.attach()
     log4j.basic_config()
Example #23
0
def readVSI(inputFileName, outputMag=5, nTilesX=20, nTilesY=20):
    '''
    Read a vsi image tile by tile and return a resized RGB TIFF image
    param outMag:  output magnification
    '''
    # starting jvm
    javabridge.start_vm(class_path=bioformats.JARS,
                        run_headless=True,
                        max_heap_size='8G')

    try:

        log4j.basic_config()

        ome = OMEXML(bioformats.get_omexml_metadata(path=inputFileName))
        sizeX = ome.image().Pixels.get_SizeX()
        sizeY = ome.image().Pixels.get_SizeY()

        nominalMag = np.float(
            ome.instrument().Objective.get_NominalMagnification())

        physicalX = ome.image().Pixels.get_PhysicalSizeX()
        physicalY = ome.image().Pixels.get_PhysicalSizeY()

        imageReader = bioformats.formatreader.make_image_reader_class()
        reader = imageReader()
        reader.setId(inputFileName)

        # Printing some info
        print('Nominal Magnification: ', nominalMag)
        print('Image size: ', sizeX, sizeY)
        print('Physical pixel size in um: ', physicalX,
              physicalY)  # um = micrometers

        # Aux variables
        tileBeginX = 0
        tileBeginY = 0
        tileCounter = 0

        hMosaic = []
        vMosaic = []

        xFac, yFac = computeResizingFactors(physicalX, physicalY, nominalMag,
                                            outputMag)

        for y in range(0, nTilesY):

            # computing begin and height size
            tileBeginY = minMax(y, 0, nTilesY, 0, sizeY)
            height = minMax(y + 1, 0, nTilesY, 0, sizeY) - tileBeginY

            for x in range(0, nTilesX):

                tileBeginX = minMax(x, 0, nTilesX, 0, sizeX)

                width = minMax(x + 1, 0, nTilesX, 0, sizeX) - tileBeginX

                tile = reader.openBytesXYWH(0, tileBeginX, tileBeginY, width,
                                            height)

                tile.shape = (int(height), int(width), 3)

                #xFac, yFac = computeResolution(physicalX, physicalY, width, height, nominalMag , outMag)

                # resize tile
                tileResized = cv2.resize(tile,
                                         None,
                                         fx=xFac,
                                         fy=yFac,
                                         interpolation=cv2.INTER_AREA)

                if (x > 0):

                    hMosaic = np.concatenate((hMosaic, tileResized), axis=1)

                else:

                    hMosaic = tileResized

                tileCounter = tileCounter + 1

            if (y > 0):
                vMosaic = np.concatenate((vMosaic, hMosaic), axis=0)
            else:

                vMosaic = hMosaic

            hMosaic = []

            progress = (tileCounter * 100) / (nTilesX * nTilesY)
            print("processing", str(progress) + '%')

    finally:
        javabridge.kill_vm()

    #print("Resize microscope magnification OK")

    return vMosaic
    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=True)

        # removes the log4j warnings
        from bioformats import log4j
        log4j.basic_config()

        javabridge.attach()
        javabridge.activate_awt()

        try:
            if __version__ != -1:
                import cpa.util.check_for_updates as cfu
                cfu.check_for_updates('http://cellprofiler.org/CPAupdate.html',
                                      max(__version__, cpa.cpaprefs.get_skip_version()),
                                      new_version_cb,
                                      user_agent='CPAnalyst/2.0.%s'%(__version__))
        except ImportError:
            logging.warn("CPA was unable to check for updates. Could not import cpa.util.check_for_updates.")

        return True
 def setUp(self):
     javabridge.attach()
     log4j.basic_config()
Example #26
0
def start_java_bridge():
    javabridge.start_vm(class_path=bioformats.JARS)
    log4j.basic_config()