Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.idea/*
67 changes: 67 additions & 0 deletions bioloaders/loaders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
A utility class to load various types of images and convert them to numpy.arrays

Author: Sarah Kefayati
Email: [email protected]
"""

__author__ = "Sarah Kefayati"
__email__ = "[email protected]"

import os
import shutil

import bioformats as bf
import javabridge as jv
import numpy as np
import pandas as pd


class Loaders:

@staticmethod
def lif_reader(lif_file_path, pickled_data_path):
"""
This function reads lif file, separates channels, series, and time points and
converts the images into 3D numpy.arrays. It then stores the images into pickle
files and saves their path in a pandas.dataframe.

:param lif_file_path: Input lif file.
:param pickled_data_path: Path of the folder to store pickled images.
:return: A dataframe containing the metadata for pickled images.
"""
jv.start_vm(class_path=bf.JARS, max_heap_size='8G')
md = bf.get_omexml_metadata(lif_file_path)
o = bf.OMEXML(md)
n_channels = o.image().Pixels.channel_count
n_series = o.get_image_count()
time_points = o.image().Pixels.SizeT
size_x = o.image().Pixels.SizeX
size_y = o.image().Pixels.SizeY
size_z = o.image().Pixels.SizeZ
rdr = bf.ImageReader(lif_file_path, perform_init=True)

if os.path.exists(pickled_data_path):
shutil.rmtree(pickled_data_path)
os.makedirs(pickled_data_path)

records = []

for c in range(0, n_channels):
for n in range(n_series):
for t in range(time_points):
img_out = np.empty(shape=(size_x, size_y, size_z))

full_path_out = os.path.join(pickled_data_path, 'image_c{}_n{}_t{}'.format(c, n, t))

if not os.path.exists(full_path_out):
for z in range(size_z):
img_out[:, :, z] = rdr.read(c=c, z=z, t=t, series=n, rescale=False)
np.save(full_path_out, img_out, allow_pickle=True, fix_imports=True)

records.append((full_path_out, c, n, t))

df = pd.DataFrame.from_records(records, columns=['image_path', 'channel', 'series', 'time'])

jv.kill_vm()
return df
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
javabridge
python-bioformats
numpy
scipy
matplotlib
Expand Down