Skip to content

Commit

Permalink
Merge branch 'master' into k_means_mat
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed Oct 15, 2020
2 parents db2c071 + 14f7df2 commit c8e703b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
28 changes: 18 additions & 10 deletions ark/utils/load_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ 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)

if not mibitiff_files:
mibitiff_files = iou.list_files(data_dir, substrs=['.tif'])

if len(mibitiff_files) == 0:
raise ValueError("No mibitiff files specified in the data directory %s" % data_dir)

# extract fov names w/ delimiter agnosticism
fovs = iou.extract_delimited_names(mibitiff_files, delimiter=delimiter)

Expand Down Expand Up @@ -100,6 +105,8 @@ 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)

if fovs is None:
# get all fovs
fovs = iou.list_folders(data_dir)
Expand Down Expand Up @@ -211,17 +218,18 @@ def load_imgs_from_dir(data_dir, files=None, delimiter=None, xr_dim_name='compar
xarray with shape [fovs, x_dim, y_dim, tifs]
Raises:
ValueError:
Raised in the following cases:
* data_dir is not a directory, <data_dir>/img is
not a file for some img in the input 'files' list, or no images are found.
* channels_indices are invalid according to the shape of the images.
* the provided dtype is too small to represent the data.
* The length of xr_channel_names (if provided) does not match the number
of channels in the input.
ValueError:
Raised in the following cases:
- data_dir is not a directory, <data_dir>/img is
not a file for some img in the input 'files' list, or no images are found.
- channels_indices are invalid according to the shape of the images.
- the provided dtype is too small to represent the data.
- The length of xr_channel_names (if provided) does not match the number
of channels in the input.
"""
if not os.path.isdir(data_dir):
raise ValueError(f"Invalid value for data_dir. {data_dir} is not a directory.")

iou.validate_paths(data_dir)

if files is None:
imgs = iou.list_files(data_dir, substrs=['.tif', '.jpg', '.png'])
Expand Down
42 changes: 42 additions & 0 deletions ark/utils/load_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@


def test_load_imgs_from_mibitiff():
# invalid directory is provided
with pytest.raises(ValueError):
loaded_xr = \
load_utils.load_imgs_from_mibitiff('not_a_dir', channels=None, delimiter='_')

with tempfile.TemporaryDirectory() as temp_dir:
# temp_dir contains no images
with pytest.raises(ValueError):
loaded_xr = load_utils.load_imgs_from_mibitiff(temp_dir,
channels=None,
delimiter='_')

# config test environment
fovs, channels = test_utils.gen_fov_chan_names(num_fovs=2, num_chans=3, use_delimiter=True)
Expand Down Expand Up @@ -64,8 +73,24 @@ def test_load_imgs_from_mibitiff():


def test_load_imgs_from_tree():
# invalid directory is provided
with pytest.raises(ValueError):
loaded_xr = \
load_utils.load_imgs_from_tree('not_a_dir', img_sub_folder="TIFs", dtype="int16")

# test loading from within fov directories
with tempfile.TemporaryDirectory() as temp_dir:
# temp_dir contains no images
with pytest.raises(ValueError):
loaded_xr = \
load_utils.load_imgs_from_tree(temp_dir, img_sub_folder="TIFs", dtype="int16")

with pytest.raises(ValueError):
# attempt to pass an empty channels list
loaded_xr = \
load_utils.load_imgs_from_tree(temp_dir, img_sub_folder="TIFs",
dtype="int16", channels=[])

fovs, chans, imgs = test_utils.gen_fov_chan_names(num_fovs=3, num_chans=3,
return_imgs=True)

Expand Down Expand Up @@ -106,6 +131,7 @@ def test_load_imgs_from_tree():

assert loaded_xr.equals(data_xr)

# test loading with data_xr containing float values
with tempfile.TemporaryDirectory() as temp_dir:
fovs, chans, imgs = test_utils.gen_fov_chan_names(num_fovs=1, num_chans=2,
return_imgs=True)
Expand All @@ -124,6 +150,22 @@ def test_load_imgs_from_tree():
# test swap int16 -> float
assert np.issubdtype(loaded_xr.dtype, np.floating)

# test loading with variable sizes
with tempfile.TemporaryDirectory() as temp_dir:
fovs, chans, imgs = test_utils.gen_fov_chan_names(num_fovs=3, num_chans=3,
return_imgs=True)

filelocs, data_xr = test_utils.create_paired_xarray_fovs(
temp_dir, fovs, chans, img_shape=(10, 10), delimiter='_', fills=True, sub_dir="TIFs",
dtype="int16"
)

loaded_xr = \
load_utils.load_imgs_from_tree(temp_dir, img_sub_folder="TIFs", dtype="int16",
variable_sizes=True)

assert loaded_xr.shape == (3, 1024, 1024, 3)


def test_load_imgs_from_dir():
# invalid directory is provided
Expand Down

0 comments on commit c8e703b

Please sign in to comment.