Skip to content

Commit

Permalink
Merge branch 'main' into overwrite_option
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed Feb 23, 2023
2 parents 5e4bd95 + d7e7106 commit 9b51d5a
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 230 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
python-version: 3.8
cache-dependency-path: "**/pyproject.toml"
cache: "pip"

- name: Get Example Dataset Cache
uses: actions/cache@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion src/ark/analysis/spatial_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tqdm.notebook import tqdm

import ark.settings as settings
from ark.utils import spatial_analysis_utils
from ark.analysis import spatial_analysis_utils


def generate_channel_spatial_enrichment_stats(label_dir, dist_mat_dir, marker_thresholds, all_data,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/ark/segmentation/marker_quantification.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import ark.settings as settings
from ark.segmentation.regionprops_extraction import REGIONPROPS_FUNCTION
from ark.segmentation.signal_extraction import EXTRACTION_FUNCTION
from ark.utils import segmentation_utils
from ark.segmentation import segmentation_utils


def get_single_compartment_props(segmentation_labels, regionprops_base,
Expand Down
File renamed without changes.
65 changes: 0 additions & 65 deletions src/ark/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,71 +421,6 @@ def generate_and_save_neighborhood_cluster_masks(fovs: List[str],
neigh_mask_progress.update(1)


# TODO: Add metadata for channel name (eliminates need for fixed-order channels)
def generate_deepcell_input(data_dir, tiff_dir, nuc_channels, mem_channels, fovs,
is_mibitiff=False, img_sub_folder="TIFs", dtype="int16"):
"""Saves nuclear and membrane channels into deepcell input format.
Either nuc_channels or mem_channels should be specified.
Writes summed channel images out as multitiffs (channels first).
Args:
data_dir (str):
location to save deepcell input tifs
tiff_dir (str):
directory containing folders of images, is_mibitiff determines what type
nuc_channels (list):
nuclear channels to be summed over
mem_channels (list):
membrane channels to be summed over
fovs (list):
list of folders to or MIBItiff files to load imgs from
is_mibitiff (bool):
if the images are of type MIBITiff
img_sub_folder (str):
if is_mibitiff is False, define the image subfolder for each fov
ignored if is_mibitiff is True
dtype (str/type):
optional specifier of image type. Overwritten with warning for float images
Raises:
ValueError:
Raised if nuc_channels and mem_channels are both None or empty
"""

# cannot have no nuclear and no membrane channels
if not nuc_channels and not mem_channels:
raise ValueError('Either nuc_channels or mem_channels should be non-empty.')

# define the channels list by combining nuc_channels and mem_channels
channels = (nuc_channels if nuc_channels else []) + (mem_channels if mem_channels else [])

# filter channels for None (just in case)
channels = [channel for channel in channels if channel is not None]

for fov in fovs:
# load the images in the current fov batch
if is_mibitiff:
data_xr = load_utils.load_imgs_from_mibitiff(
tiff_dir, mibitiff_files=[fov], channels=channels
)
else:
data_xr = load_utils.load_imgs_from_tree(
tiff_dir, img_sub_folder=img_sub_folder, fovs=[fov], channels=channels
)

fov_name = data_xr.fovs.values[0]
out = np.zeros((2, data_xr.shape[1], data_xr.shape[2]), dtype=data_xr.dtype)

# sum over channels and add to output
if nuc_channels:
out[0] = np.sum(data_xr.loc[fov_name, :, :, nuc_channels].values, axis=2)
if mem_channels:
out[1] = np.sum(data_xr.loc[fov_name, :, :, mem_channels].values, axis=2)

save_path = os.path.join(data_dir, f"{fov_name}.tiff")
image_utils.save_image(save_path, out)


def split_img_stack(stack_dir, output_dir, stack_list, indices, names, channels_first=True):
"""Splits the channels in a given directory of images into separate files
Expand Down
67 changes: 66 additions & 1 deletion src/ark/utils/deepcell_service_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from requests.exceptions import RetryError
from requests.packages.urllib3.util import Retry
from tifffile import imread
from alpineer import image_utils, io_utils, misc_utils
from alpineer import image_utils, io_utils, misc_utils, load_utils
from tqdm.notebook import tqdm


Expand Down Expand Up @@ -326,6 +326,71 @@ def run_deepcell_direct(input_dir, output_dir, host='https://deepcell.org',
return 0


# TODO: Add metadata for channel name (eliminates need for fixed-order channels)
def generate_deepcell_input(data_dir, tiff_dir, nuc_channels, mem_channels, fovs,
is_mibitiff=False, img_sub_folder="TIFs", dtype="int16"):
"""Saves nuclear and membrane channels into deepcell input format.
Either nuc_channels or mem_channels should be specified.
Writes summed channel images out as multitiffs (channels first).
Args:
data_dir (str):
location to save deepcell input tifs
tiff_dir (str):
directory containing folders of images, is_mibitiff determines what type
nuc_channels (list):
nuclear channels to be summed over
mem_channels (list):
membrane channels to be summed over
fovs (list):
list of folders to or MIBItiff files to load imgs from
is_mibitiff (bool):
if the images are of type MIBITiff
img_sub_folder (str):
if is_mibitiff is False, define the image subfolder for each fov
ignored if is_mibitiff is True
dtype (str/type):
optional specifier of image type. Overwritten with warning for float images
Raises:
ValueError:
Raised if nuc_channels and mem_channels are both None or empty
"""

# cannot have no nuclear and no membrane channels
if not nuc_channels and not mem_channels:
raise ValueError('Either nuc_channels or mem_channels should be non-empty.')

# define the channels list by combining nuc_channels and mem_channels
channels = (nuc_channels if nuc_channels else []) + (mem_channels if mem_channels else [])

# filter channels for None (just in case)
channels = [channel for channel in channels if channel is not None]

for fov in fovs:
# load the images in the current fov batch
if is_mibitiff:
data_xr = load_utils.load_imgs_from_mibitiff(
tiff_dir, mibitiff_files=[fov], channels=channels
)
else:
data_xr = load_utils.load_imgs_from_tree(
tiff_dir, img_sub_folder=img_sub_folder, fovs=[fov], channels=channels
)

fov_name = data_xr.fovs.values[0]
out = np.zeros((2, data_xr.shape[1], data_xr.shape[2]), dtype=data_xr.dtype)

# sum over channels and add to output
if nuc_channels:
out[0] = np.sum(data_xr.loc[fov_name, :, :, nuc_channels].values, axis=2)
if mem_channels:
out[1] = np.sum(data_xr.loc[fov_name, :, :, mem_channels].values, axis=2)

save_path = os.path.join(data_dir, f"{fov_name}.tiff")
image_utils.save_image(save_path, out)


def _convert_deepcell_seg_masks(seg_mask: bytes) -> np.ndarray:
"""Converts the segmentation masks provided by deepcell from `np.float32` to `inp.nt32`.
Expand Down
10 changes: 5 additions & 5 deletions templates/1_Segment_Image_Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"import xarray as xr\n",
"from alpineer import io_utils\n",
"\n",
"from ark.segmentation import marker_quantification\n",
"from ark.utils import (data_utils, deepcell_service_utils, example_dataset,\n",
" plot_utils, segmentation_utils)"
"from ark.segmentation import marker_quantification, segmentation_utils\n",
"from ark.utils import (deepcell_service_utils, example_dataset,\n",
" plot_utils)"
]
},
{
Expand Down Expand Up @@ -213,9 +213,9 @@
},
"outputs": [],
"source": [
"# generate and save deepcell input tifs\n",
"# generate and save deepcell input tiffs\n",
"# set img_sub_folder param to None if the image files in tiff_dir are not in a separate sub folder \n",
"data_utils.generate_deepcell_input(\n",
"deepcell_service_utils.generate_deepcell_input(\n",
" deepcell_input_dir,\n",
" tiff_dir,\n",
" nucs,\n",
Expand Down
Loading

0 comments on commit 9b51d5a

Please sign in to comment.