Example #1
0
    def __init__(self,image_path=None):
        self._metaData=self._meta_init()
        if image_path is not None:
            self.importMeta(image_path)

        bioformats.clear_image_reader_cache()
        javabridge._javabridge.reap()
Example #2
0
    def get_filename(self, workspace, make_dirs=True, check_overwrite=True):
        """Concoct a filename for the current image based on the user settings"""

        measurements = workspace.measurements
        if self.file_name_method == FN_SINGLE_NAME:
            filename = self.single_file_name.value
            filename = workspace.measurements.apply_metadata(filename)
        elif self.file_name_method == FN_SEQUENTIAL:
            filename = self.single_file_name.value
            filename = workspace.measurements.apply_metadata(filename)
            n_image_sets = workspace.measurements.image_set_count
            ndigits = int(numpy.ceil(numpy.log10(n_image_sets + 1)))
            ndigits = max((ndigits, self.number_of_digits.value))
            padded_num_string = str(
                measurements.image_set_number).zfill(ndigits)
            filename = "%s%s" % (filename, padded_num_string)
        else:
            file_name_feature = self.source_file_name_feature
            filename = measurements.get_current_measurement(
                "Image", file_name_feature)
            filename = os.path.splitext(filename)[0]
            if self.wants_file_name_suffix:
                suffix = self.file_name_suffix.value
                suffix = workspace.measurements.apply_metadata(suffix)
                filename += suffix

        filename = "%s.%s" % (filename, self.get_file_format())
        pathname = self.pathname.get_absolute_path(measurements)
        if self.create_subdirectories:
            image_path = self.source_path(workspace)
            subdir = os.path.relpath(image_path,
                                     self.root_dir.get_absolute_path())
            pathname = os.path.join(pathname, subdir)
        if len(pathname) and not os.path.isdir(pathname) and make_dirs:
            try:
                os.makedirs(pathname)
            except:
                #
                # On cluster, this can fail if the path was created by
                # another process after this process found it did not exist.
                #
                if not os.path.isdir(pathname):
                    raise
        result = os.path.join(pathname, filename)
        if check_overwrite and not self.check_overwrite(result, workspace):
            return

        if check_overwrite and os.path.isfile(result):
            try:
                os.remove(result)
            except:
                import bioformats

                bioformats.clear_image_reader_cache()
                os.remove(result)
        return result
Example #3
0
    def get_filename(self, workspace, make_dirs=True, check_overwrite=True):
        "Concoct a filename for the current image based on the user settings"

        measurements = workspace.measurements
        if self.file_name_method == FN_SINGLE_NAME:
            filename = self.single_file_name.value
            filename = workspace.measurements.apply_metadata(filename)
        elif self.file_name_method == FN_SEQUENTIAL:
            filename = self.single_file_name.value
            filename = workspace.measurements.apply_metadata(filename)
            n_image_sets = workspace.measurements.image_set_count
            ndigits = int(numpy.ceil(numpy.log10(n_image_sets + 1)))
            ndigits = max((ndigits, self.number_of_digits.value))
            padded_num_string = str(measurements.image_set_number).zfill(ndigits)
            filename = '%s%s' % (filename, padded_num_string)
        else:
            file_name_feature = self.source_file_name_feature
            filename = measurements.get_current_measurement('Image',
                                                            file_name_feature)
            filename = os.path.splitext(filename)[0]
            if self.wants_file_name_suffix:
                suffix = self.file_name_suffix.value
                suffix = workspace.measurements.apply_metadata(suffix)
                filename += suffix

        filename = "%s.%s" % (filename, self.get_file_format())
        pathname = self.pathname.get_absolute_path(measurements)
        if self.create_subdirectories:
            image_path = self.source_path(workspace)
            subdir = os.path.relpath(image_path, self.root_dir.get_absolute_path())
            pathname = os.path.join(pathname, subdir)
        if len(pathname) and not os.path.isdir(pathname) and make_dirs:
            try:
                os.makedirs(pathname)
            except:
                #
                # On cluster, this can fail if the path was created by
                # another process after this process found it did not exist.
                #
                if not os.path.isdir(pathname):
                    raise
        result = os.path.join(pathname, filename)
        if check_overwrite and not self.check_overwrite(result, workspace):
            return

        if check_overwrite and os.path.isfile(result):
            try:
                os.remove(result)
            except:
                import bioformats
                bioformats.clear_image_reader_cache()
                os.remove(result)
        return result
Example #4
0
 def __init__(self,image_path=None):
     super().__init__(image_path)
     if image_path is not None:
         self.pixels=np.zeros((self._metaData["size_z"],self._metaData["size_y"],self._metaData["size_x"],self._metaData["size_c"]),
             dtype=self.xml.image().Pixels.PixelType)
         self._metaData["DimensionOrder"] = "ZYXC"
         self._metaData["xml_original"].Pixels.DimensionOrder = "ZYXC"
         with ImageReader(image_path) as rdr:
             for channel in range(self._metaData["size_c"]):
                 for z_index in range(self._metaData["size_z"]):
                     self.pixels[z_index,:,:,channel]= rdr.read(c=channel,z=z_index,rescale=False)
             rdr.close()
         bioformats.clear_image_reader_cache()
         javabridge._javabridge.reap()
         self.sumprj=(np.sum(self.pixels,axis=0))
         self.maxprj=(np.amax(self.pixels,axis=0))
         self.meanprj=(np.mean(self.pixels,axis=0))
Example #5
0
def loadBioImageSeriesFromFile(filepath, meta: BioMeta) -> np.ndarray:

    assert meta.shape is not None, "No parsed BioMeta provided"
    file = np.zeros(meta.shape)
    try:
        # loads a cached reader that reads every frame
        with bioformats.ImageReader(filepath, perform_init=True) as reader:
            for c in range(meta.sizec):
                for z in range(meta.sizez):
                    for t in range(meta.sizet):

                        # bioformats appears to swap axes for tif images and read all three channels at a time for RGB
                        im1 = reader.read(c=c,
                                          z=z,
                                          t=t,
                                          series=meta.series,
                                          rescale=True,
                                          channel_names=None)
                        if im1.ndim == 3:
                            if im1.shape[2] == 3:
                                # Three channels are red
                                im2 = im1[:, :, c]
                            else:
                                im2 = im1
                        else:
                            im2 = im1
                        if (meta.sizex == im2.shape[1]) and (
                                meta.sizey == im2.shape[0]
                        ) and not meta.sizex == meta.sizey:
                            # x and y are swapped
                            logging.warning(
                                "Image might be transposed. Swapping X and Y")
                            im3 = im2.transpose()
                        else:
                            im3 = im2

                        file[:, :, c, z, t] = im3

        logging.info("BioImage Parsed")
    except Exception as e:
        logging.error("Something went wrong whilst parsing the BioImageFile")
        logging.error(e)
    finally:
        bioformats.clear_image_reader_cache()

    return file
    def get_filename(self, workspace, make_dirs=True, check_overwrite=True):
        """Concoct a filename for the current image based on the user settings"""

        measurements = workspace.measurements
        file_name_feature = self.source_file_name_feature
        filename = measurements.get_current_measurement(
            "Image", file_name_feature)
        filename = os.path.splitext(filename)[0]
        if self.wants_file_name_suffix:
            suffix = self.file_name_suffix.value
            suffix = workspace.measurements.apply_metadata(suffix)
            filename += suffix

        filename = "%s.%s" % (filename, self.get_file_format())
        pathname = self.pathname.get_absolute_path(measurements)
        if self.create_subdirectories:
            image_path = self.source_path(workspace)
            subdir = os.path.relpath(image_path,
                                     self.root_dir.get_absolute_path())
            pathname = os.path.join(pathname, subdir)
        if len(pathname) and not os.path.isdir(pathname) and make_dirs:
            try:
                os.makedirs(pathname)
            except:
                #
                # On cluster, this can fail if the path was created by
                # another process after this process found it did not exist.
                #
                if not os.path.isdir(pathname):
                    raise
        result = os.path.join(pathname, filename)
        if check_overwrite and not self.check_overwrite(result, workspace):
            return

        if check_overwrite and os.path.isfile(result):
            try:
                os.remove(result)
            except:
                import bioformats

                bioformats.clear_image_reader_cache()
                os.remove(result)
        return result
    raw_data = np.empty([y_range, x_range, z_range, c_range], dtype=np.uint8)
    #raw_data = []
    with bioformats.get_image_reader(key="cached_bf_reader", path=path_to_data, \
                                     url=None) as reader:
        # Read the stack for every channel
        for c in range(c_range):
            # Read the whole stack
            for z in range(z_range):
                # Ignore the time
                raw_image = reader.read(c=c, z=z, t=0, series=None, index=None, \
                                        rescale=False, wants_max_intensity=False, \
                                        channel_names=None, XYWH=None)
                raw_data[:, :, z, c] = raw_image
                
        bioformats.release_image_reader("cached_bf_reader")
        bioformats.clear_image_reader_cache()
        reader.close()
        
if depth == 't':
    # Get the reader and read every image from the stack of the first image 
    # series (series 0) in dimension order XYZC
    raw_data = np.empty([y_range, x_range, t_range, c_range], dtype=np.uint8)
    #raw_data = []
    with bioformats.get_image_reader(key="cached_bf_reader", path=path_to_data, \
                                     url=None) as reader:
        # Read the stack for every channel
        for c in range(c_range):
            # Read the whole stack
            for t in range(t_range):
                # Ignore the time
                raw_image = reader.read(c=c, z=0, t=t, series=None, index=None, \
Example #8
0
async def convertBioImageToXArray(filepath, index, progress):
    await progress("Loading Biometa")
    biometa = loadBioMetaFromFile(filepath)
    imagemeta = biometa.findAll("Image")[index]

    # General Parsing Parameters
    name = imagemeta.attrs["Name"]
    parsedimagemeta = parseMeta(imagemeta, imagetree)
    scan = parsedimagemeta["scan"]
    shape = (scan["SizeX"], scan["SizeY"], scan["SizeC"], scan["SizeZ"], scan["SizeT"])

    # Channels and Planes (TODO: Planes are given to be in z,c,t shape in xml format?, if error: sort)
    zlinearorderedplanes = constructDim(imagetree, parsedimagemeta, "planes", "z")
    zctorderplanes = zlinearorderedplanes.data.reshape((scan["SizeZ"], scan["SizeC"], scan["SizeT"]))

    channels = constructDim(imagetree, parsedimagemeta, "channels", "c")
    planes = xr.DataArray(zctorderplanes, dims=("z", "c", "t"))

    rescale, scalefactor = await calculateRescaleAndScaleFactor(scan, progress)
    if rescale: await progress(f"Rescaling because we want float64")
    await progress(f"Insignificant Bits detected: Scalefactor of {scalefactor}x applies")
    # Population
    newfile = da.zeros(shape)
    newfile = xr.DataArray(newfile, dims=["x", "y", "c", "z", "t"])
    newfile = xr.DataArray(newfile, coords={"physx": newfile.x * float(scan["PhysicalSizeX"]),
                                            "physy": newfile.y * float(scan["PhysicalSizeY"]),
                                            "phsyz": newfile.z * float(scan["PhysicalSizeZ"]),
                                            "physt": newfile.t * float(
                                                scan.get("TimeIncrement", 1) if scan.get("TimeIncrement",
                                                                                         1) is not None else 1),
                                            "x": newfile.x,
                                            "y": newfile.y,
                                            "z": newfile.z,
                                            "t": newfile.t,
                                            "c": newfile.c,
                                            "channels": channels,
                                            "planes": planes
                                            })

    try:
        # loads a cached reader that reads every frame
        with bioformats.ImageReader(filepath, perform_init=True) as reader:
            for c in range(shape[2]):
                await progress(f"Processing channel {channels[c].data['Name']}")
                for z in range(shape[3]):
                    for t in range(shape[4]):

                        # bioformats appears to swap axes for tif images and read all three channels at a time for RGB
                        im1 = reader.read(c=c, z=z, t=t, series=index, rescale=rescale, channel_names=None)
                        if scalefactor != 1:
                            im1 = im1 * scalefactor
                        if im1.ndim == 3:
                            if im1.shape[2] == 3:
                                # Three channels are red
                                im2 = im1[:, :, c]
                            else:
                                im2 = im1
                        else:
                            im2 = im1
                        if im2.shape[0] == shape[1] and im2.shape[0] != shape[0]:
                            # Transposed axes for whatever reason
                            im3 = np.swapaxes(im2, 0, 1)
                        else:
                            im3 = im2

                        newfile[:, :, c, z, t] = im3

        await progress("BioImage Parsed")
    finally:
        bioformats.clear_image_reader_cache()

    newfile.attrs["scan"] = [scan]
    newfile.attrs["seriesname"] = name

    newfile.name = name

    return newfile, parsedimagemeta
Example #9
0
def get_tif_stack(filepath, series=0, depth='z', return_dim_order='YXZC'):
    """
    This function assumes, that the Javabridge-VM is started and initialized
    with the bioformats jars:
        javabridge.start_vm(class_path=bioformats.JARS)
    To kill the vom use:
        javabridge.kill_vm()
    
    Parameters:
    filepath (string): Path to the multistack tif file
    series: (int): Number of the image series which shuld be read. Default = 0
    time_step: Number of the time step which should be read. Default = 0
    (NOT IMPLEMENTED)
    depth (string): 'z', when the depth is the z-axis or 't', when the depth is the 
    t-axis
    bit_depth (integer): Power of two from the bit-depth of the file to be read.
    return_dim_order (string):  In which order the raw data should be returned. 
    XYZC, YXZC ...

    Returns:
    meta_data (dictionary): Metadata Dictionary of the tif file
    raw_data (numpy-array): Numpy-Array which contains the raw data of the 
    image stack in the tif file
    """
    # Get XML metadata of complete file
    xml_string = bioformats.get_omexml_metadata(filepath)
    ome_xml = bioformats.OMEXML(xml_string)

    # Read the metadata from the image -> series 0
    iome = ome_xml.image(series)
    series_count = ome_xml.get_image_count()
    image_name = iome.get_Name()
    image_id = iome.get_ID()
    image_acquisition = iome.get_AcquisitionDate()

    # Geth the pixel meta data from the image
    ch_count = iome.Pixels.get_channel_count()
    dim_order = iome.Pixels.get_DimensionOrder()
    pic_id = iome.Pixels.get_ID()
    phy_x = iome.Pixels.get_PhysicalSizeX()
    phy_x_unit = iome.Pixels.get_PhysicalSizeXUnit()
    phy_y = iome.Pixels.get_PhysicalSizeY()
    phy_y_unit = iome.Pixels.get_PhysicalSizeYUnit()
    phy_z = iome.Pixels.get_PhysicalSizeZ()
    phy_z_unit = iome.Pixels.get_PhysicalSizeZUnit()
    pix_type = iome.Pixels.get_PixelType()
    plane_count = iome.Pixels.get_plane_count()
    c_range = iome.Pixels.get_SizeC()
    t_range = iome.Pixels.get_SizeT()
    x_range = iome.Pixels.get_SizeX()
    y_range = iome.Pixels.get_SizeY()
    z_range = iome.Pixels.get_SizeZ()

    # Make a dictionary out of the metadata
    meta_data = {}
    meta_data['series_count'] = series_count
    meta_data['image_name'] = image_name
    meta_data['image_id'] = image_id
    meta_data['image_acquisition'] = image_acquisition
    meta_data['channel_count'] = ch_count
    meta_data['original_dimension_order'] = dim_order
    meta_data['numpy_array_dimension_order'] = 'XYZC'
    meta_data['picture_id'] = pic_id
    meta_data['physical_size_x'] = phy_x
    meta_data['physical_size_x_unit'] = phy_x_unit
    meta_data['physical_size_y'] = phy_y
    meta_data['physical_size_y_unit'] = phy_y_unit
    meta_data['physical_size_z'] = phy_z
    meta_data['physical_size_z_unit'] = phy_z_unit
    meta_data['pixel_data_type'] = pix_type
    meta_data['plane_count'] = plane_count
    meta_data['channel_size'] = c_range
    meta_data['time_size'] = t_range
    meta_data['x_size'] = x_range
    meta_data['y_size'] = y_range
    meta_data['z_size'] = z_range

    # Get the datatype of each pixel
    if pix_type == 'uint8':
        np_dtype = np.uint8
    if pix_type == 'uint16':
        np_dtype = np.uint16

    if depth == 'z':
        # Allocate a numpy array for the given dimension
        raw_data = np.empty([y_range, x_range, z_range, c_range],
                            dtype=np_dtype)
        # Get the reader and read every image from the stack of the first image
        # series (series 0) in dimension order XYZC
        with bioformats.get_image_reader(key="cached_bf_reader", path=filepath, \
                                         url=None) as reader:
            # Read the stack for every channel
            for c in range(c_range):
                # Read the whole stack
                for z in range(z_range):
                    # Ignore the time-axis
                    raw_image = reader.read(c=c, z=z, t=0, series=None, index=None, \
                                            rescale=False, wants_max_intensity=False, \
                                            channel_names=None, XYWH=None)
                    raw_data[:, :, z, c] = raw_image

    if depth == 't':
        # Allocate a numpy array for the given dimension
        raw_data = np.empty([y_range, x_range, t_range, c_range],
                            dtype=np_dtype)
        # Get the reader and read every image from the stack of the first image
        # series (series 0) in dimension order XYZC
        with bioformats.get_image_reader(key="cached_bf_reader", path=filepath, \
                                         url=None) as reader:
            # Read the stack for every channel
            for c in range(c_range):
                # Read the whole stack
                for t in range(t_range):
                    # Ignore the z-axis
                    raw_image = reader.read(c=c, z=0, t=t, series=None, index=None, \
                                            rescale=False, wants_max_intensity=False, \
                                            channel_names=None, XYWH=None)
                    raw_data[:, :, t, c] = raw_image

    # Clear the cache and close the reader
    bioformats.release_image_reader("cached_bf_reader")
    bioformats.clear_image_reader_cache()
    reader.close()

    if return_dim_order == 'XYZC':
        return meta_data, np.transpose(raw_data, axes=(1, 0, 2, 3))
    else:  # YXZC
        return meta_data, raw_data
Example #10
0
parser.add_argument('--file',
                    metavar='file',
                    help='file containing the image .lif extension')
parser.add_argument('--size',
                    metavar='size',
                    help='which series ranges to extract')
parser.add_argument(
    '--dest',
    metavar='dest',
    help='the numpy array format text file name as destination of extraction')

# Reading the file
args = parser.parse_args()

# Converting range
t = tuple([int(x) for x in args.size.split(",")])
window = range(*t)

# Program Here
javabridge.start_vm(class_path=bf.JARS)

reader = bf.get_image_reader('r1', path="./180509_Permeabilisation_Test.lif")
#meta = bf.get_omexml_metadata(args.file)
#xmlome = bf.OMEXML(meta)
#mdroot = ETree.fromstring(meta.encode('utf-8'))
for i in window:
    n, m = reader.read(c=0, series=i, rescale=False, wants_max_intensity=True)
    np.savetxt(str(i) + args.dest, n)

bf.clear_image_reader_cache()
javabridge.kill_vm()
Example #11
0
def compaction_fish(pars):
    flowb_c = 0
    global temp_report_dir
    temp_report_dir = tempfile.TemporaryDirectory(dir="./")
    flowb_c = angler.PDF_gen(angler.rep_first_page(
        pars, title="ANGLER: compaction FISH analysis"),
                             temp_report_dir.name,
                             counter=flowb_c)

    sample_style_sheet = getSampleStyleSheet()
    if pars["#ofImages"] is "all":
        files = glob.glob(pars['folder_path'] + "/*" + pars['file_ext'])[:]
    else:
        files = glob.glob(pars['folder_path'] + "/*" +
                          pars['file_ext'])[:pars['#ofImages']]
    # files = glob.glob(pars['folder_path'] + "/*" + pars['file_ext'])[:150]
    tot = len(files)
    # report_cols=['file_name','ch#','crop#','feret-diameter','convex_hull']
    measurements = pd.DataFrame()
    print("start")
    for j, file_path in enumerate(files):
        flowbs = []
        #         Open Image
        img = angler.MicImage(image_path=file_path)
        para = Paragraph(path_leaf(file_path), sample_style_sheet['Heading2'])
        flowbs.append(para)

        for i, ch in enumerate(pars["FISH_ch"]):
            ch_measurements = pd.DataFrame()
            # crds = angler.FISH_finder(img.maxprj[..., ch], thresh=pars['FISH_finder_threshold'][ch],crop_size=pars['crop_size']) # For 2D crds
            crds = angler.FISH_finder(
                img.pixels[..., ch],
                thresh=pars['FISH_finder_threshold'][ch],
                crop_size=pars['crop_size'])  #For 3D crds
            crds = [[i, [j, k]] for i, j, k in crds]
            for crd_no, (z_crd, xy_crd) in enumerate(crds):
                try:
                    measurement = {}
                    measurement = {"file_name": path_leaf(file_path)}
                    measurement.update({"channel": ch})
                    measurement.update({"crop#": crd_no})
                    measurement.update({"crop_coordinates": xy_crd})
                    # crop each crd
                    # crp=img.crop(center_coord=crd,channel=ch,crop_size=pars['crop_size']) # For 2D

                    if pars['use_z']:
                        crp = crp = img.crop(center_coord=xy_crd,
                                             channel=ch,
                                             crop_size=pars['crop_size'],
                                             z_coord=z_crd,
                                             z_size=pars['number_of_stacks'])
                    else:
                        crp = crp = img.crop(center_coord=xy_crd,
                                             channel=ch,
                                             crop_size=pars['crop_size'],
                                             z_coord=None)

                    if pars["noise_removal"]:
                        # remove Background
                        no_noise = angler.subtract_bkg(crp)
                        # measure feret
                        feret_m = angler.feret(
                            prj=no_noise.getPRJ(pars["prj_method"]),
                            pixel_size=angler.pixel_size(no_noise),
                            threshold=pars['feret_threshold'])
                    else:
                        # measure feret
                        feret_m = angler.feret(
                            prj=crp.getPRJ(pars["prj_method"]),
                            pixel_size=angler.pixel_size(crp),
                            threshold=pars['feret_threshold'])

                    #color code for plot
                    measurement.update(feret_m)
                    ch_measurements = ch_measurements.append(pd.DataFrame(
                        [measurement]),
                                                             ignore_index=True)
                except:
                    print(
                        'Something went wrong with feret measurement of image',
                        str(measurement['file_name']),
                        str(measurement['crop#']))

            # Make the graph
            measurements = measurements.append(ch_measurements,
                                               ignore_index=True)
            # try:
            fig = angler.compaction_plotter(img, ch, ch_measurements, pars)
            buf = BytesIO()
            fig.savefig(buf, dpi=300, format="tiff")
            buf.seek(0)
            plt.close(fig)
            fig_img = Image(buf, 6.3 * inch, 4.5 * inch)
            flowbs.append(fig_img)
            # except:
            # print('Something went wrong with crd plotting of image',str(measurement['file_name']),str(measurement['crop#']))

        flowb_c = angler.PDF_gen(flowables=flowbs,
                                 path=temp_report_dir.name,
                                 counter=flowb_c)
        #         buf.close()
        bioformats.clear_image_reader_cache()
        angler.update_progress("Compaction_FISH:", index=j + 1, total=tot)
    pdf_paths = glob.glob(temp_report_dir.name + '/*.pdf')
    pdf_paths.sort()
    angler.pdf_merger(pars["pdf_report_path"], pdf_paths)
    temp_report_dir.cleanup()
    print("\n PDF report created in:{}".format(pars["pdf_report_path"]))
    return measurements