Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove .tif/.tiff extension from list_files for MIBItiff reading in Segment_Image_Data.ipynb #281

Merged
merged 24 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c7cb22b
Add method to split delimited files from MIBItiff reading
alex-l-kong Oct 16, 2020
3c73bfc
Need to add the actual file exts to this
alex-l-kong Oct 16, 2020
6193795
Merge branch 'master' into tiff_ext
alex-l-kong Oct 16, 2020
d05e494
Merge branch 'master' into tiff_ext
alex-l-kong Oct 16, 2020
08dd4f3
Merge branch 'master' into tiff_ext
alex-l-kong Oct 17, 2020
df80326
Hacky fix for MIBItiff loading
alex-l-kong Oct 17, 2020
99a8f9b
See if removing setup.py install fixes the issue we've been having
alex-l-kong Oct 21, 2020
03cc682
Move logic from Segment_Image_Data to deepcell_service_utils
alex-l-kong Oct 22, 2020
55deeb8
Better (?) way of handling .tif removing?
alex-l-kong Oct 22, 2020
60c95ac
Merge branch 'master' into tiff_ext
alex-l-kong Oct 23, 2020
d8bbf5f
Add comments to create_deepcell_output and add mixed fov name/file li…
alex-l-kong Oct 23, 2020
9964723
Merge branch 'tiff_ext' of https://github.com/angelolab/ark-analysis …
alex-l-kong Oct 23, 2020
b9e837c
Fix typo: existant to existent
alex-l-kong Oct 23, 2020
2ef71bd
Remove extra slash from comment
alex-l-kong Oct 23, 2020
7f9287d
Merge branch 'master' into tiff_ext
alex-l-kong Oct 24, 2020
db19213
Merge branch 'master' into tiff_ext
alex-l-kong Oct 24, 2020
23a31f1
Remove extra check for whether .tif file exists
alex-l-kong Oct 24, 2020
b5ad33d
Fix typo and make sure we're handling both .tif and .tiff files when …
alex-l-kong Oct 26, 2020
ea341f8
Fix comments
alex-l-kong Oct 26, 2020
354bb0d
Merge branch 'master' into tiff_ext
alex-l-kong Oct 27, 2020
ec7a44b
Merge branch 'master' into tiff_ext
alex-l-kong Oct 27, 2020
45b0eea
Merge branch 'master' into tiff_ext
alex-l-kong Oct 27, 2020
ffcec4b
Don't remove .zip file after creation, and make sure to handle mixed …
alex-l-kong Oct 27, 2020
ce223f7
Explicitly print out error message that zip file is being overwritten
alex-l-kong Oct 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions ark/utils/deepcell_service_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from zipfile import ZipFile
import warnings

from ark.utils import misc_utils


def create_deepcell_output(deepcell_input_dir, deepcell_output_dir, fovs=None,
suffix='_feature_0', host='https://deepcell.org', job_type='multiplex'):
Expand Down Expand Up @@ -37,30 +39,49 @@ def create_deepcell_output(deepcell_input_dir, deepcell_output_dir, fovs=None,
ValueError:
Raised if there is some fov X (from fovs list) s.t.
the file <deepcell_input_dir>/fovX.tif does not exist
"""
"""

# extract all the files from deepcell_input_dir
input_files = io_utils.list_files(deepcell_input_dir, substrs=['.tif', '.tiff'])

# set fovs equal to input_files it not already set
if fovs is None:
tifs = io_utils.list_files(deepcell_input_dir, substrs='.tif')
fovs = io_utils.extract_delimited_names(tifs, delimiter='.')
fovs = input_files

# now extract only the names of the fovs without the file extension
fovs = io_utils.extract_delimited_names(fovs, delimiter='.')

# make sure that all fovs actually exist in the list of input_files
misc_utils.verify_in_list(
fovs=fovs,
deepcell_input_files=io_utils.extract_delimited_names(input_files, delimiter='.'))

# define the location of the zip file for our fovs
zip_path = os.path.join(deepcell_input_dir, 'fovs.zip')
if os.path.isfile(zip_path):
warnings.warn(f'{zip_path} will be overwritten.')
print(f'{zip_path} will be overwritten')
warnings.warn('overwriting')

# write all files to the zip file
print('Zipping preprocessed tif files.')

with ZipFile(zip_path, 'w') as zipObj:
for fov in fovs:
filename = os.path.join(deepcell_input_dir, fov + '.tif')
if not os.path.isfile(filename):
raise ValueError('Could not find .tif file for %s. '
'Invalid value for %s' % (fov, filename))
# file has .tif extension
if fov + '.tif' in input_files:
filename = os.path.join(deepcell_input_dir, fov + '.tif')
# file has .tiff extension
else:
filename = os.path.join(deepcell_input_dir, fov + '.tiff')

zipObj.write(filename, os.path.basename(filename))

print('Uploading files to DeppCell server.')
# pass the zip file to deepcell.org
print('Uploading files to DeepCell server.')
run_deepcell_task(zip_path, deepcell_output_dir, host, job_type)
os.remove(zip_path)

print('Extracting tif files from DeepCell response.')
# extract the .tif output
print('Extracting tif files from DeepCell response.')
zip_files = glob.glob(os.path.join(deepcell_output_dir, '*.zip'))
zip_files.sort(key=os.path.getmtime)
with ZipFile(zip_files[-1], 'r') as zipObj:
Expand All @@ -87,7 +108,7 @@ def run_deepcell_task(input_dir, output_dir, host='https://deepcell.org',
job_type (str):
Name of job workflow (multiplex, segmentation, tracking).
Default: 'multiplex'
"""
"""

mgr_kwargs = {
'host': host,
Expand Down
53 changes: 42 additions & 11 deletions ark/utils/deepcell_service_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
def mocked_run_deepcell(input_dir, output_dir, host, job_type):
pathlib.Path(os.path.join(output_dir, 'fov1_feature_0.tif')).touch()
pathlib.Path(os.path.join(output_dir, 'fov2_feature_0.tif')).touch()
pathlib.Path(os.path.join(output_dir, 'fov3_feature_0.tif')).touch()

zip_path = os.path.join(output_dir, 'example_output.zip')
with ZipFile(zip_path, 'w') as zipObj:
for i in [1, 2]:
for i in range(1, 4):
filename = os.path.join(output_dir, f'fov{i}_feature_0.tif')
zipObj.write(filename, os.path.basename(filename))
os.remove(filename)
Expand All @@ -25,12 +26,19 @@ def test_create_deepcell_output(mocker):

input_dir = os.path.join(temp_dir, 'input_dir')
os.makedirs(input_dir)
pathlib.Path(os.path.join(input_dir, 'fov1.tif')).touch()
pathlib.Path(os.path.join(input_dir, 'fov2.tif')).touch()
pathlib.Path(os.path.join(input_dir, 'fov3.tiff')).touch()

output_dir = os.path.join(temp_dir, 'output_dir')
os.makedirs(output_dir)
pathlib.Path(os.path.join(input_dir, 'fov1.tif')).touch()
pathlib.Path(os.path.join(input_dir, 'fov2.tif')).touch()

with pytest.raises(ValueError):
# fail if non-existent fovs are specified
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir,
fovs=['fov1', 'fov1000'])

# test with specified fov list
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir,
fovs=['fov1', 'fov2'])

Expand All @@ -41,18 +49,40 @@ def test_create_deepcell_output(mocker):
assert os.path.exists(os.path.join(output_dir, 'fov1_feature_0.tif'))
assert os.path.exists(os.path.join(output_dir, 'fov2_feature_0.tif'))

# if fovs is None, all .tif files in input dir should be taken
os.remove(os.path.join(output_dir, 'fov1_feature_0.tif'))
os.remove(os.path.join(output_dir, 'fov2_feature_0.tif'))
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir)
os.remove(os.path.join(output_dir, 'example_output.zip'))

# test with mixed fov/file list
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir,
alex-l-kong marked this conversation as resolved.
Show resolved Hide resolved
fovs=['fov1', 'fov2.tif', 'fov3.tiff'])

# make sure DeepCell (.zip) output exists
assert os.path.exists(os.path.join(output_dir, 'example_output.zip'))

# DeepCell output .zip file should be extracted
assert os.path.exists(os.path.join(output_dir, 'fov1_feature_0.tif'))
assert os.path.exists(os.path.join(output_dir, 'fov2_feature_0.tif'))
assert os.path.exists(os.path.join(output_dir, 'fov3_feature_0.tif'))

os.remove(os.path.join(output_dir, 'fov1_feature_0.tif'))
os.remove(os.path.join(output_dir, 'fov2_feature_0.tif'))
os.remove(os.path.join(output_dir, 'fov3_feature_0.tif'))
os.remove(os.path.join(output_dir, 'example_output.zip'))

# if fovs is None, all .tif files in input dir should be taken
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir)

# /fovs.zip file should be removed from input folder after completion
assert not os.path.isfile(os.path.join(input_dir, 'fovs.zip'))
# make sure DeepCell (.zip) output exists
assert os.path.exists(os.path.join(output_dir, 'example_output.zip'))

assert os.path.exists(os.path.join(output_dir, 'fov1_feature_0.tif'))
assert os.path.exists(os.path.join(output_dir, 'fov2_feature_0.tif'))
assert os.path.exists(os.path.join(output_dir, 'fov3_feature_0.tif'))

pathlib.Path(os.path.join(input_dir, 'fovs.zip')).touch()
# Warning should be displayed if /fovs.zip file exists (will be overwritten)

# Warning should be displayed if fovs.zip file exists (will be overwritten)
with pytest.warns(UserWarning):
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir,
fovs=['fov1'])
Expand All @@ -61,12 +91,13 @@ def test_create_deepcell_output(mocker):
with pytest.warns(UserWarning):
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir,
suffix='_other_suffix', fovs=['fov1'])
pathlib.Path(os.path.join(input_dir, 'fov3.tif')).touch()

pathlib.Path(os.path.join(input_dir, 'fov4.tif')).touch()
with pytest.warns(UserWarning):
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir,
fovs=['fov1', 'fov2', 'fov3'])
fovs=['fov1', 'fov2', 'fov3', 'fov4'])

# ValueError should be raised if .tif file does not exists for some fov in fovs
with pytest.raises(ValueError):
create_deepcell_output(deepcell_input_dir=input_dir, deepcell_output_dir=output_dir,
fovs=['fov1', 'fov4'])
fovs=['fov1', 'fov5'])