Skip to content

Commit

Permalink
Merge pull request #167 from mehta-lab/bkg-correction-fixes2
Browse files Browse the repository at this point in the history
Fix background correction for non-square images and online mode
  • Loading branch information
talonchandler authored Sep 9, 2022
2 parents 3c570ec + 78af85f commit bc8344a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
20 changes: 19 additions & 1 deletion recOrder/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def load_bg(bg_path, height, width, ROI=None):
for i in range(len(bg_paths)):
img = tiff.imread(bg_paths[i])

if ROI is not None and ROI != (0, 0, height, width):
if ROI is not None and ROI != (0, 0, width, height):
bg_data[i, :, :] = np.mean(img[ROI[1]:ROI[1] + ROI[3], ROI[0]:ROI[0] + ROI[2]])

else:
Expand Down Expand Up @@ -165,3 +165,21 @@ def get_unimodal_threshold(input_image):
max_dist = per_dist
assert best_threshold > -np.inf, 'Error in unimodal thresholding'
return best_threshold
def rec_bkg_to_wo_bkg(recorder_option) -> str:
"""
Converts recOrder's background options to waveorder's background options.
Parameters
----------
recorder_option
Returns
-------
waveorder_option
"""
if recorder_option == 'local_fit+':
return 'local_fit'
else:
return recorder_option

9 changes: 2 additions & 7 deletions recOrder/pipelines/qlipp_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from waveorder.io.reader import WaveorderReader
from waveorder.io.writer import WaveorderWriter
from recOrder.io import MetadataReader
from recOrder.io.utils import load_bg, MockEmitter
from recOrder.io.utils import load_bg, MockEmitter, rec_bkg_to_wo_bkg
from recOrder.compute.qlipp_compute import reconstruct_qlipp_birefringence, reconstruct_qlipp_stokes, \
reconstruct_phase2D, reconstruct_phase3D, initialize_reconstructor
import numpy as np
Expand Down Expand Up @@ -72,11 +72,7 @@ def __init__(self, config: ConfigReader, data: WaveorderReader, writer: Waveorde
self.data_shape = (self.t, len(self.output_channels), self.slices, self.img_dim[0], self.img_dim[1])
self.chunk_size = (1, 1, 1, self.data_shape[-2], self.data_shape[-1])

# Convert 'local_fit+' to 'local_fit' for waveorder
if self.config.background_correction == 'local_fit+':
wo_background_correction = 'local_fit'
else:
wo_background_correction = self.config.background_correction
wo_background_correction = rec_bkg_to_wo_bkg(self.config.background_correction)

# Initialize Reconstructor
if self.no_phase:
Expand Down Expand Up @@ -122,7 +118,6 @@ def __init__(self, config: ConfigReader, data: WaveorderReader, writer: Waveorde
else:
self.bg_stokes = None


def _check_output_channels(self, output_channels):
self.no_birefringence = True
self.no_phase = True
Expand Down
6 changes: 3 additions & 3 deletions recOrder/plugin/widget/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ def _check_requirements_for_acq(self, mode):
raise_error = True
self._set_tab_red('General', True)

if self.bg_option == 'local_fit' or self.bg_option == 'local_fit+' or self.bg_option == 'Global':
if self.bg_option == 'local_fit' or self.bg_option == 'local_fit+' or self.bg_option == 'global':
success = self._check_line_edit('bg_path')
if not success:
raise_error = True
Expand Down Expand Up @@ -1376,7 +1376,7 @@ def _populate_from_config(self):
self.bg_option = self.config_reader.background_correction
if self.bg_option == 'None':
self.ui.cb_bg_method.setCurrentIndex(0)
elif self.bg_option == 'Global':
elif self.bg_option == 'global':
self.ui.cb_bg_method.setCurrentIndex(1)
elif self.bg_option == 'local_fit':
self.ui.cb_bg_method.setCurrentIndex(2)
Expand Down Expand Up @@ -2117,7 +2117,7 @@ def enter_bg_correction(self):
self.ui.label_bg_path.setHidden(False)
self.ui.le_bg_path.setHidden(False)
self.ui.qbutton_browse_bg_path.setHidden(False)
self.bg_option = 'Global'
self.bg_option = 'global'
elif state == 2:
self.ui.label_bg_path.setHidden(True)
self.ui.le_bg_path.setHidden(True)
Expand Down
17 changes: 12 additions & 5 deletions recOrder/plugin/workers/acquisition_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
deconvolve_fluorescence_2D, calculate_background
from recOrder.io.zarr_converter import ZarrConverter
from recOrder.io.metadata_reader import MetadataReader, get_last_metadata_file
from recOrder.io.utils import rec_bkg_to_wo_bkg
from napari.qt.threading import WorkerBaseSignals, WorkerBase
import logging
from waveorder.io.writer import WaveorderWriter
Expand Down Expand Up @@ -959,6 +960,8 @@ def _reconstruct(self, stack):

self._check_abort()

wo_background_correction = rec_bkg_to_wo_bkg(self.calib_window.bg_option)

# Initialize the heavy reconstuctor
if self.mode == 'phase' or self.mode == 'all':

Expand All @@ -981,7 +984,7 @@ def _reconstruct(self, stack):
z_step_um=self.calib_window.z_step,
pad_z=self.calib_window.pad_z,
pixel_size_um=self.calib_window.ps,
bg_correction=self.calib_window.bg_option,
bg_correction=wo_background_correction,
n_obj_media=self.calib_window.n_media,
mode=self.calib_window.phase_dim,
use_gpu=self.calib_window.use_gpu,
Expand Down Expand Up @@ -1010,7 +1013,7 @@ def _reconstruct(self, stack):
z_step_um=self.calib_window.z_step,
pad_z=self.calib_window.pad_z,
pixel_size_um=self.calib_window.ps,
bg_correction=self.calib_window.bg_option,
bg_correction=wo_background_correction,
n_obj_media=self.calib_window.n_media,
mode=self.calib_window.phase_dim,
use_gpu=self.calib_window.use_gpu,
Expand All @@ -1033,11 +1036,12 @@ def _reconstruct(self, stack):
calibration_scheme=self.calib_window.calib_scheme,
wavelength_nm=self.calib_window.wavelength,
swing=self.calib_window.swing,
bg_correction=self.calib_window.bg_option,
bg_correction=wo_background_correction,
n_slices=self.n_slices)

# Check to see if background correction is desired and compute BG stokes
if self.calib_window.bg_option != 'None':
# Prepare background corrections for waveorder
# This block mimics qlipp_pipeline.py L110-119.
if self.calib_window.bg_option in ['global', 'local_fit+']:
logging.debug('Loading BG Data')
self._check_abort()
bg_data = self._load_bg(self.calib_window.acq_bg_directory, stack.shape[-2], stack.shape[-1])
Expand All @@ -1046,6 +1050,9 @@ def _reconstruct(self, stack):
self._check_abort()
bg_stokes = recon.Stokes_transform(bg_stokes)
self._check_abort()
elif self.calib_window.bg_option == 'local_fit':
bg_stokes = np.zeros((5, stack.shape[-2], stack.shape[-1]))
bg_stokes[0, ...] = 1 # Set background to "identity" Stokes parameters.
else:
logging.debug('No Background Correction method chosen')
bg_stokes = None
Expand Down

0 comments on commit bc8344a

Please sign in to comment.