Skip to content
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
44 changes: 44 additions & 0 deletions cellacdc/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,50 @@ def run_cli(ini_filepath):
logger.info('**********************************************')
logger.info(f'Cell-ACDC command-line closed. {myutils.get_salute_string()}')
logger.info('**********************************************')


def run_data_metadata(parser_args):
from cellacdc import myutils, cli

logger, logs_path, log_path, log_filename = myutils.setupLogger(
module='data_metadata', logs_path=None
)
kernel = cli.DataMetadataKernel(logger, log_path, parser_args)
kernel.run()

logger.info('**********************************************')
logger.info(f'Cell-ACDC metadata CLI closed. {myutils.get_salute_string()}')
logger.info('**********************************************')


def run_data_convert(parser_args):
from cellacdc import myutils, cli

logger, logs_path, log_path, log_filename = myutils.setupLogger(
module='data_convert', logs_path=None
)
kernel = cli.DataConvertKernel(logger, log_path, parser_args)
kernel.run()

logger.info('**********************************************')
logger.info(f'Cell-ACDC data convert closed. {myutils.get_salute_string()}')
logger.info('**********************************************')


def run_data_restructure(parser_args):
from cellacdc import myutils, cli

logger, logs_path, log_path, log_filename = myutils.setupLogger(
module='data_restructure', logs_path=None
)
kernel = cli.DataRestructureKernel(logger, log_path, parser_args)
kernel.run()

logger.info('**********************************************')
logger.info(
f'Cell-ACDC data restructure closed. {myutils.get_salute_string()}'
)
logger.info('**********************************************')


def _setup_numpy(caller_name='Cell-ACDC'):
Expand Down
97 changes: 90 additions & 7 deletions cellacdc/acdc_bioio_bioformats/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,54 @@ def __str__(self):
class Channel:
pass

def _get_reader_channel_wavelengths(bioimage):
reader = getattr(bioimage, 'reader', None)
if reader is None:
return None, None
emission = getattr(reader, 'channel_emission_wavelengths', None)
excitation = getattr(reader, 'channel_excitation_wavelengths', None)
return emission, excitation

def _read_channel_wavelengths_from_filepath(image_filepath):
from bioio import BioImage

try:
kwargs = set_reader(image_filepath)
bioimage = BioImage(image_filepath, **kwargs)
return _get_reader_channel_wavelengths(bioimage)
except Exception:
return None, None

class Node:
def __init__(self, image_filepath, bioimage_class):
_, ext = os.path.splitext(image_filepath)
self._node = {}
try:
self._node = {
'TimeIncrement': bioimage_class.time_interval.total_seconds(),
'TimeIncrementUnit': 's'
}
except Exception as err:
self._node = {}
except Exception:
time_increment = getattr(bioimage_class, 'time_increment', None)
if time_increment is not None:
self._node = {
'TimeIncrement': time_increment,
'TimeIncrementUnit': getattr(
bioimage_class, 'time_increment_unit', 's'
),
}

if ext not in EXTENSION_METADATA_ATTR_MAPPER:
return

name_expression_mapper = EXTENSION_METADATA_ATTR_MAPPER[ext]
for name, expression in name_expression_mapper.items():
try:
self._node[name] = safe_get_or_call(bioimage_class, expression)
value = safe_get_or_call(bioimage_class, expression)
if value is not None:
self._node[name] = value
except Exception as err:
self._node[name] = None
pass

def get(self, name):
value = self._node.get(name)
Expand All @@ -134,10 +162,36 @@ def get(self, name):

return value

class Pixels:
class Pixels:
def _get_channel_wavelengths(self):
if hasattr(self, '_channel_wavelength_cache'):
return self._channel_wavelength_cache
emission, excitation = _get_reader_channel_wavelengths(
getattr(self, 'bioimage', None)
)
if emission is None:
image_filepath = getattr(self, 'image_filepath', None)
if image_filepath is not None:
emission, excitation = _read_channel_wavelengths_from_filepath(
image_filepath
)
self._channel_wavelength_cache = (emission, excitation)
return self._channel_wavelength_cache

def Channel(self, c: int):
channel = Channel()
channel.Name = self.channel_names[c]
emission, excitation = self._get_channel_wavelengths()
node = {}
if emission is not None and c < len(emission):
em_wavelength = emission[c]
if em_wavelength is not None:
node['EmissionWavelength'] = str(em_wavelength)
if excitation is not None and c < len(excitation):
ex_wavelength = excitation[c]
if ex_wavelength is not None:
node['ExcitationWavelength'] = str(ex_wavelength)
channel.node = node
return channel

def get_omexml_metadata(image_filepath, qparent=None):
Expand All @@ -153,14 +207,17 @@ class BioImageMetadata:
def __init__(
self, SizeT, SizeC, SizeZ, SizeY, SizeX,
PhysicalSizeX, PhysicalSizeY, PhysicalSizeZ,
channel_names, image_count
channel_names, image_count,
time_increment=None, time_increment_unit='s',
):
self.shape = (SizeT, SizeC, SizeZ, SizeY, SizeX)
self.physical_pixel_sizes = PhysicalPixelSizes(
PhysicalSizeX, PhysicalSizeY, PhysicalSizeZ
)
self.channel_names = channel_names
self.scenes = list(range(image_count))
self.time_increment = time_increment
self.time_increment_unit = time_increment_unit

class OMEXML:
def __init__(self):
Expand All @@ -182,6 +239,8 @@ def init_from_metadata(self, metadata: Metadata):

def _init_Pixels(self, image_filepath):
self.Pixels = Pixels()
self.Pixels.bioimage = self.bioimage
self.Pixels.image_filepath = image_filepath
self.Pixels.node = Node(image_filepath, self.bioimage)
self.Pixels.channel_names = self.bioimage.channel_names

Expand All @@ -200,6 +259,16 @@ def __str__(self):
f'PhysicalSizeZ: {self.bioimage.physical_pixel_sizes.Z}\n'
f'Image count: {self.get_image_count()}'
)
try:
time_increment = self.Pixels.node.get('TimeIncrement')
time_increment_unit = self.Pixels.node.get('TimeIncrementUnit')
txt = (
f'{txt}\n'
f'TimeIncrement: {time_increment}\n'
f'TimeIncrementUnit: {time_increment_unit}'
)
except Exception:
pass
return txt

def to_file(self, filepath):
Expand Down Expand Up @@ -230,11 +299,25 @@ def init_from_file(self, filepath, image_filepath):
setattr(self, kwarg, dtype(value))
except Exception as err:
setattr(self, kwarg, default)

time_increment = None
time_increment_unit = 's'
time_increment_match = re.search(r'TimeIncrement: (.+)', txt)
if time_increment_match is not None:
try:
time_increment = float(time_increment_match.group(1))
except Exception:
pass
time_increment_unit_match = re.search(r'TimeIncrementUnit: (.+)', txt)
if time_increment_unit_match is not None:
time_increment_unit = time_increment_unit_match.group(1)

self.bioimage = BioImageMetadata(
self.SizeT, self.SizeC, self.SizeZ, self.SizeY, self.SizeX,
self.PhysicalSizeX, self.PhysicalSizeY, self.PhysicalSizeZ,
self.channel_names, self.image_count
self.channel_names, self.image_count,
time_increment=time_increment,
time_increment_unit=time_increment_unit,
)

self._init_Pixels(image_filepath)
Expand Down
43 changes: 43 additions & 0 deletions cellacdc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1995,3 +1995,46 @@ def _add_derived_cell_cycle_columns(self, all_frames_acdc_df):
self.sigLog.emit(traceback.format_exc())

return all_frames_acdc_df


class DataMetadataKernel(_WorkflowKernel):
def __init__(self, logger, log_path, parser_args):
super().__init__(logger, log_path, is_cli=True)
self.parser_args = parser_args

@exception_handler_cli
def run(self):
from cellacdc import data_cli

self.logger.info('Extracting metadata from raw microscopy file...')
data_cli.run_metadata_cli(self.parser_args)


class DataConvertKernel(_WorkflowKernel):
def __init__(self, logger, log_path, parser_args):
super().__init__(logger, log_path, is_cli=True)
self.parser_args = parser_args

@exception_handler_cli
def run(self):
from cellacdc import data_cli

self.logger.info('Starting BioIO data conversion...')
data_cli.run_convert_cli(
self.parser_args, logger_func=self.logger.info
)


class DataRestructureKernel(_WorkflowKernel):
def __init__(self, logger, log_path, parser_args):
super().__init__(logger, log_path, is_cli=True)
self.parser_args = parser_args

@exception_handler_cli
def run(self):
from cellacdc import data_cli

self.logger.info('Starting data restructure...')
data_cli.run_restructure_cli(
self.parser_args, logger_func=self.logger.info
)
Loading
Loading