Skip to content

Commit

Permalink
Merge branch 'master' into new_som
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed Mar 5, 2021
2 parents a2a7add + 1db8d54 commit ac0c214
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,8 @@ If you run into trouble, please first refer to our [FAQ](https://ark-analysis.re
## Want to contribute?

If you would like to help make `ark` better, please take a look at our [contributing guidelines](https://ark-analysis.readthedocs.io/en/latest/_rtd/contributing.html).

## Citation
Please cite our paper if you found our repo useful!

[Greenwald, Miller et al. Whole-cell segmentation of tissue images with human-level performance using large-scale data annotation and deep learning](https://www.biorxiv.org/content/10.1101/2021.03.01.431313v2)
2 changes: 1 addition & 1 deletion ark/segmentation/marker_quantification.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def compute_marker_counts(input_images, segmentation_labels, nuclear_counts=Fals
nuc_segmentation_labels=nuc_labels,
cell_ids=unique_cell_ids)

nuc_props = get_single_compartment_props(segmentation_labels.loc[:, :, 'nuclear'].values,
nuc_props = get_single_compartment_props(nuc_labels,
regionprops_base, regionprops_single_comp,
**reg_props)

Expand Down
19 changes: 19 additions & 0 deletions ark/segmentation/marker_quantification_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,25 @@ def test_compute_marker_counts_nuc_whole_cell_diff():
assert np.array_equal(segmentation_output_unequal.loc['nuclear', :, 'cell_size'],
segmentation_output_unequal.loc['nuclear', :, 'area'])

# check that splitting large nuclei works as expected

# swap nuclear and cell masks so that nuc is bigger
big_nuc_masks = np.concatenate((nuc_mask, cell_mask), axis=-1)
segmentation_labels_big_nuc = test_utils.make_labels_xarray(
label_data=big_nuc_masks,
compartment_names=['whole_cell', 'nuclear']
)

# test utils output is 4D but tests require 3D
segmentation_labels_big_nuc = segmentation_labels_big_nuc[0]

segmentation_output_big_nuc = \
marker_quantification.compute_marker_counts(
input_images=input_images,
segmentation_labels=segmentation_labels_big_nuc,
nuclear_counts=True,
split_large_nuclei=True)


def test_compute_marker_counts_no_coords():
cell_mask, channel_data = test_utils.create_test_extraction_data()
Expand Down
6 changes: 4 additions & 2 deletions ark/utils/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import warnings


def validate_paths(paths):
def validate_paths(paths, data_prefix=True):
"""Verifys that paths exist and don't leave Docker's scope
Args:
paths (str or list):
paths to verify.
data_prefix (bool):
if True, checks that directory starts with /data, necessary when inside the docker
Raises:
ValueError:
Expand All @@ -21,7 +23,7 @@ def validate_paths(paths):

for path in paths:
if not os.path.exists(path):
if str(path).startswith('../data'):
if str(path).startswith('../data') or not data_prefix:
for parent in reversed(pathlib.Path(path).parents):
if not os.path.exists(parent):
raise ValueError(
Expand Down
17 changes: 17 additions & 0 deletions ark/utils/io_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ def test_validate_paths():
with pytest.raises(ValueError, match=r".*The file/path.*not_a_real_file.*"):
iou.validate_paths(wrong_file)

# make tempdir for testing outside of docker
with tempfile.TemporaryDirectory() as valid_path:

# make valid subdirectory
valid_parts = [p for p in pathlib.Path(valid_path).parts]
valid_parts[0] = 'not_a_real_directory'

# test no '../data' prefix
starts_out_of_scope = os.path.join(*valid_parts)

# test out of scope when specifying out of scope
with pytest.raises(ValueError, match=r".*not_a_real_directory*"):
iou.validate_paths(starts_out_of_scope, data_prefix=False)

with pytest.raises(ValueError, match=r".*is not prefixed with.*"):
iou.validate_paths(starts_out_of_scope, data_prefix=True)

# reset cwd after testing
os.chdir('../')

Expand Down
6 changes: 3 additions & 3 deletions ark/utils/load_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def load_imgs_from_mibitiff(data_dir, mibitiff_files=None, channels=None, delimi
xarray with shape [fovs, x_dim, y_dim, channels]
"""

iou.validate_paths(data_dir)
iou.validate_paths(data_dir, data_prefix=False)

if not mibitiff_files:
mibitiff_files = iou.list_files(data_dir, substrs=['.tif'])
Expand Down Expand Up @@ -108,7 +108,7 @@ def load_imgs_from_tree(data_dir, img_sub_folder=None, fovs=None, channels=None,
xarray with shape [fovs, x_dim, y_dim, tifs]
"""

iou.validate_paths(data_dir)
iou.validate_paths(data_dir, data_prefix=False)

if fovs is None:
# get all fovs
Expand Down Expand Up @@ -237,7 +237,7 @@ def load_imgs_from_dir(data_dir, files=None, delimiter=None, xr_dim_name='compar
of channels in the input.
"""

iou.validate_paths(data_dir)
iou.validate_paths(data_dir, data_prefix=False)

if files is None:
imgs = iou.list_files(data_dir, substrs=['.tif', '.jpg', '.png'])
Expand Down
2 changes: 1 addition & 1 deletion ark/utils/segmentation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def find_nuclear_label_id(nuc_segmentation_labels, cell_coords):
return nuclear_label_id


def split_large_nuclei(cell_segmentation_labels, nuc_segmentation_labels, cell_ids, min_size=5):
def split_large_nuclei(cell_segmentation_labels, nuc_segmentation_labels, cell_ids, min_size=15):
"""Splits nuclei that are bigger than the corresponding cell into multiple pieces
Args:
Expand Down
2 changes: 1 addition & 1 deletion ark/utils/spatial_analysis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def calc_dist_matrix(label_maps, save_path=None):
# Check that file path exists, if given

if save_path is not None:
io_utils.validate_paths(save_path)
io_utils.validate_paths(save_path, data_prefix=False)

dist_mats_list = []

Expand Down

0 comments on commit ac0c214

Please sign in to comment.