Skip to content

Commit

Permalink
Merge branch 'master' into tiff_ext
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed Oct 27, 2020
2 parents 354bb0d + 91dc272 commit ec7a44b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions ark/utils/load_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def load_imgs_from_mibitiff(data_dir, mibitiff_files=None, channels=None, delimi

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

if len(mibitiff_files) == 0:
raise ValueError("No mibitiff files specified in the data directory %s" % data_dir)
Expand Down Expand Up @@ -77,8 +78,6 @@ def load_imgs_from_mibitiff(data_dir, mibitiff_files=None, channels=None, delimi
range(img_data[0].data.shape[1]), channels],
dims=["fovs", "rows", "cols", "channels"])

img_xr = img_xr.sortby('fovs').sortby('channels')

return img_xr


Expand Down Expand Up @@ -128,11 +127,20 @@ def load_imgs_from_tree(data_dir, img_sub_folder=None, fovs=None, channels=None,
channels.sort()
# otherwise, fill channel names with correct file extension
elif not all([img.endswith(("tif", "tiff", "jpg", "png")) for img in channels]):
# need this to reorder channels back because list_files may mess up the ordering
channels_no_delim = [img.split('.')[0] for img in channels]

channels = iou.list_files(
os.path.join(data_dir, fovs[0], img_sub_folder),
substrs=channels
)

# get the corresponding indices found in channels_no_delim
channels_indices = [channels_no_delim.index(chan.split('.')[0]) for chan in channels]

# reorder back to original
channels = [chan for _, chan in sorted(zip(channels_indices, channels))]

if len(channels) == 0:
raise ValueError("No images found in designated folder")

Expand Down Expand Up @@ -178,9 +186,6 @@ def load_imgs_from_tree(data_dir, img_sub_folder=None, fovs=None, channels=None,
img_xr = xr.DataArray(img_data, coords=[fovs, row_coords, col_coords, img_names],
dims=["fovs", "rows", "cols", "channels"])

# sort by fovs and channels for deterministic result
img_xr = img_xr.sortby('fovs').sortby('channels')

return img_xr


Expand Down Expand Up @@ -233,6 +238,7 @@ def load_imgs_from_dir(data_dir, files=None, delimiter=None, xr_dim_name='compar

if files is None:
imgs = iou.list_files(data_dir, substrs=['.tif', '.jpg', '.png'])
imgs.sort()
else:
imgs = files
for img in imgs:
Expand Down Expand Up @@ -311,6 +317,4 @@ def load_imgs_from_dir(data_dir, files=None, delimiter=None, xr_dim_name='compar
else range(img_data.shape[3])],
dims=["fovs", "rows", "cols", xr_dim_name])

img_xr = img_xr.sortby('fovs').sortby(xr_dim_name)

return img_xr
2 changes: 1 addition & 1 deletion ark/utils/load_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_load_imgs_from_tree():
load_utils.load_imgs_from_tree(temp_dir, img_sub_folder="TIFs", dtype="int16",
channels=some_chans)

assert loaded_xr.equals(data_xr[:, :, :, :2], )
assert loaded_xr.equals(data_xr[:, :, :, :2])

# check mixed extension presence
loaded_xr = \
Expand Down

0 comments on commit ec7a44b

Please sign in to comment.