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

New IlastikPretrainedProbability filter #1740

Merged
merged 1 commit into from
Jan 29, 2020
Merged

Conversation

shanaxel42
Copy link
Collaborator

@shanaxel42 shanaxel42 commented Jan 16, 2020

Creates a IlastikPretrainedProbability that takes in the path to an ilastick executable file, and ilastick project file and an Imagestack and runs ilastick headless to apply the pretrained prediction model to the Imagestack. The output of ilastik is then loaded back in as an ImageStack.

Test plan: So I tested everything locally but not sure how to write a unit test without having to install ilastick on travis...

fixes: #1499

@shanaxel42 shanaxel42 force-pushed the saxelrod-ilastik-call-out branch 2 times, most recently from 992a4dc to 7063b29 Compare January 16, 2020 22:49
@shanaxel42 shanaxel42 force-pushed the saxelrod-from-ilastick-labeled-image branch from 8a63ef8 to c0d7f4f Compare January 16, 2020 22:51
@shanaxel42 shanaxel42 force-pushed the saxelrod-ilastik-call-out branch from 7063b29 to b35f97e Compare January 16, 2020 22:53
@codecov-io
Copy link

codecov-io commented Jan 16, 2020

Codecov Report

Merging #1740 into master will decrease coverage by 0.14%.
The diff coverage is 40.54%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1740      +/-   ##
==========================================
- Coverage   90.09%   89.94%   -0.15%     
==========================================
  Files         248      252       +4     
  Lines        9186     9579     +393     
==========================================
+ Hits         8276     8616     +340     
- Misses        910      963      +53
Impacted Files Coverage Δ
starfish/core/image/Filter/__init__.py 100% <100%> (ø) ⬆️
...re/image/Filter/ilastik_pre_trained_probability.py 38.88% <38.88%> (ø)
starfish/data.py 48.21% <0%> (-1.79%) ⬇️
...fish/core/imagestack/parser/tilefetcher/_parser.py 90.47% <0%> (ø)
...ish/core/imagestack/parser/tilefetcher/__init__.py 100% <0%> (ø)
...fish/core/imagestack/test/test_from_tilefetcher.py 100% <0%> (ø)
...tarfish/core/test/test_multiprocessing_workflow.py 94.87% <0%> (+0.13%) ⬆️
starfish/core/imagestack/imagestack.py 93.58% <0%> (+0.62%) ⬆️
...periment/builder/test/structured_formatter/util.py 96.7% <0%> (+1.24%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 99a7f43...29edcd4. Read the comment docs.

@shanaxel42 shanaxel42 requested a review from ttung January 21, 2020 18:08
@shanaxel42 shanaxel42 force-pushed the saxelrod-from-ilastick-labeled-image branch from c0d7f4f to 054dd28 Compare January 22, 2020 17:41
Copy link
Collaborator

@ttung ttung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test plan: So I tested everything locally but not sure how to write a unit test without having to install ilastick on travis...

Is that a hard thing to do?

"""

def __init__(self, ilastik_executable: Union[Path, str], ilastik_project: Union[Path, str]):
ilastik_executable = ilastik_executable \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the assignment extends to multiple lines, I don’t see much of a point to doing x = y if cond else z (vs the traditional if statement)


def __init__(self, ilastik_executable: Union[Path, str], ilastik_project: Union[Path, str]):
ilastik_executable = ilastik_executable \
if isinstance(ilastik_executable, Path) else Path(ilastik_executable)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think Path(path) works if path is already a Path. So you might as well do:

x = Path(x)

But you may want to check that.

@@ -5,6 +5,7 @@
python-dateutil < 2.8.1
click
dataclasses==0.6
h5py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a big dependency?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah it's not crazy just an interface between HDF5 and python

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't that require sucking in a huge HDF5 library? Although we might already have that by virtue of xarray -> netcdf...

stack : ImageStack
Dapi image to be run through ilastik.
in_place : bool
N/A
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "this parameter is ignored for this filter"?

np.save(dapi_file, stack.xarray.values.squeeze())

# env {} is needed to fix the weird virtualenv stuff
subprocess.run(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want subprocess.check_call? or you can pass check=True to subprocess.run

probability_images = h5["exported_data"][:]
h5.close()
cell_probabilities, _ = probability_images[:, :, 0], probability_images[:, :, 1]
cell_threshold = threshold_otsu(cell_probabilities)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, I thought we were going to leave the thresholding to the user.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errr good question. This was how @ambrosejcarr and @dganguli loaded in the cell_probabilities in their notebook?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok talked with deep, will delete the thresholding bit

@shanaxel42 shanaxel42 force-pushed the saxelrod-ilastik-call-out branch from b35f97e to be32e23 Compare January 22, 2020 18:44
@shanaxel42 shanaxel42 changed the base branch from saxelrod-from-ilastick-labeled-image to master January 22, 2020 18:45
@shanaxel42 shanaxel42 force-pushed the saxelrod-ilastik-call-out branch from be32e23 to 632a0aa Compare January 28, 2020 19:53
@shanaxel42 shanaxel42 requested a review from ttung January 28, 2020 19:53
Copy link
Collaborator

@ttung ttung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments, lgtm otherwise.

f"{stack.num_rounds}")
if stack.num_chs != 1:
raise ValueError(
f"{IlastikPretrainedProbability.__name__} given an image with more than one channel"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f"{IlastikPretrainedProbability.__name__} given an image with more than one channel"
f"{IlastikPretrainedProbability.__name__} given an image with more than one channel "

f"{stack.num_chs}")

# temp files
temp_dir = tempfile.TemporaryDirectory()
Copy link
Collaborator

@ttung ttung Jan 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
temp_dir = tempfile.TemporaryDirectory()
with tempfile.TemporaryDirectory() as temp_dir:

this will ensure cleanup.


# temp files
temp_dir = tempfile.TemporaryDirectory()
dapi_file = f"{temp_dir.name}_dapi.npy"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dapi_file = f"{temp_dir.name}_dapi.npy"
dapi_file = os.path.join(temp_dir.name, "dapi.npy")

# temp files
temp_dir = tempfile.TemporaryDirectory()
dapi_file = f"{temp_dir.name}_dapi.npy"
output_file = f"{temp_dir.name}_dapi_Probabilities.h5"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
output_file = f"{temp_dir.name}_dapi_Probabilities.h5"
output_file = os.path.join(temp_dir.name, "dapi_Probabilities.h5")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives me a "Directory not found error"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something wrong then, you're not creating the temporary files in the right place.

@shanaxel42 shanaxel42 force-pushed the saxelrod-ilastik-call-out branch from 632a0aa to 29edcd4 Compare January 29, 2020 17:05
@shanaxel42 shanaxel42 merged commit b82237f into master Jan 29, 2020
@neuromusic
Copy link
Collaborator

🎉

ttung pushed a commit that referenced this pull request Jan 31, 2020
#1740 added h5py but not to the pinned requirements files.  That resulted in unhappy travis runs (e.g., https://travis-ci.com/spacetx/starfish/builds/146897038)

Test plan: none
ttung pushed a commit that referenced this pull request Jan 31, 2020
#1740 added h5py but not to the pinned requirements files.  That resulted in unhappy travis runs (e.g., https://travis-ci.com/spacetx/starfish/builds/146897038)

Test plan: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support applying pre-trained Segmentation models (Ilastik)
5 participants