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

Add authentication to SpotDetection #75

Merged
merged 5 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
316 changes: 161 additions & 155 deletions deepcell_spots/applications/polaris_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ def test_polaris_app(self):

# test image type error
with self.assertRaises(ValueError):
_ = Polaris(image_type='x')
_ = Polaris(spots_model=spots_model, image_type='x')

# test segmentation type error
with self.assertRaises(ValueError):
_ = Polaris(segmentation_type='x')
_ = Polaris(spots_model=spots_model, segmentation_type='x')

# test threshold error
app = Polaris()
app = Polaris(spots_model=spots_model)
spots_image = np.random.rand(1, 128, 128, 1)
with self.assertRaises(ValueError):
_ = app.predict(spots_image=spots_image, threshold=1.1)
Expand All @@ -123,172 +123,178 @@ def test_polaris_app(self):
_ = app.predict(spots_image=spots_image, mask_threshold=-1.1)

# test segmentation app error
app = Polaris(segmentation_type='no segmentation')
app = Polaris(spots_model=spots_model, segmentation_type='no segmentation')
spots_image = np.random.rand(1, 128, 128, 1)
segmentation_image = np.random.rand(1, 128, 128, 1)
with self.assertRaises(ValueError):
_ = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image)

# REMOVED BC OF MESMER MODEL DOWNLOAD
# test multiplex image type
app = Polaris(image_type='multiplex', segmentation_type='mesmer')
self.assertIsNone(app.decoding_app)
# app = Polaris(spots_model=spots_model, image_type='multiplex',
# segmentation_type='mesmer')
# self.assertIsNone(app.decoding_app)

df_barcodes = pd.DataFrame(
[
["code1", 1, 1, 0, 0, 0, 0],
["code2", 0, 0, 1, 1, 0, 0],
["code3", 0, 0, 0, 0, 1, 1],
["code4", 1, 0, 0, 0, 1, 0],
["code5", 0, 0, 1, 0, 0, 1],
["code6", 0, 1, 0, 0, 1, 0],
["code7", 1, 0, 1, 0, 0, 0],
],
columns=["Gene", "r0c0", "r0c1", "r0c2", "r1c0", "r1c1", "r1c2"],
index=np.arange(7) + 1,
)
decoding_kwargs = {'df_barcodes': df_barcodes, 'rounds': 2,
'channels': 3, 'distribution': 'Relaxed Bernoulli',
'params_mode': '2*R*C'}
app = Polaris(image_type='multiplex', decoding_kwargs=decoding_kwargs)
self.assertIsNotNone(app.decoding_app)
# df_barcodes = pd.DataFrame(
# [
# ["code1", 1, 1, 0, 0, 0, 0],
# ["code2", 0, 0, 1, 1, 0, 0],
# ["code3", 0, 0, 0, 0, 1, 1],
# ["code4", 1, 0, 0, 0, 1, 0],
# ["code5", 0, 0, 1, 0, 0, 1],
# ["code6", 0, 1, 0, 0, 1, 0],
# ["code7", 1, 0, 1, 0, 0, 0],
# ],
# columns=["Gene", "r0c0", "r0c1", "r0c2", "r1c0", "r1c1", "r1c2"],
# index=np.arange(7) + 1,
# )
# decoding_kwargs = {'df_barcodes': df_barcodes, 'rounds': 2,
# 'channels': 3, 'distribution': 'Relaxed Bernoulli',
# 'params_mode': '2*R*C'}
# app = Polaris(spots_model=spots_model, image_type='multiplex',
# decoding_kwargs=decoding_kwargs)
# self.assertIsNotNone(app.decoding_app)

with self.assertRaises(ValueError):
spots_image = np.random.rand(1, 128, 128, 1)
_ = app.predict(spots_image=spots_image)
with self.assertRaises(ValueError):
spots_image = np.random.rand(1, 128, 128, 6)
segmentation_image = np.random.rand(2, 128, 128, 1)
_ = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image)
with self.assertRaises(ValueError):
spots_image = np.random.rand(1, 128, 128, 6)
segmentation_image = np.random.rand(1, 128, 128, 2)
_ = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image)
with self.assertRaises(ValueError):
spots_image = np.random.rand(1, 128, 128, 6)
segmentation_image = np.random.rand(2, 128, 128, 1)
background_image = np.random.rand(2, 128, 128, 1)
_ = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image)
with self.assertRaises(ValueError):
spots_image = np.random.rand(1, 128, 128, 6)
segmentation_image = np.random.rand(1, 128, 128, 2)
background_image = np.random.rand(1, 128, 128, 4)
_ = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image)
# with self.assertRaises(ValueError):
# spots_image = np.random.rand(1, 128, 128, 1)
# _ = app.predict(spots_image=spots_image)
# with self.assertRaises(ValueError):
# spots_image = np.random.rand(1, 128, 128, 6)
# segmentation_image = np.random.rand(2, 128, 128, 1)
# _ = app.predict(spots_image=spots_image,
# segmentation_image=segmentation_image)
# with self.assertRaises(ValueError):
# spots_image = np.random.rand(1, 128, 128, 6)
# segmentation_image = np.random.rand(1, 128, 128, 2)
# _ = app.predict(spots_image=spots_image,
# segmentation_image=segmentation_image)
# with self.assertRaises(ValueError):
# spots_image = np.random.rand(1, 128, 128, 6)
# segmentation_image = np.random.rand(2, 128, 128, 1)
# background_image = np.random.rand(2, 128, 128, 1)
# _ = app.predict(spots_image=spots_image,
# segmentation_image=segmentation_image)
# with self.assertRaises(ValueError):
# spots_image = np.random.rand(1, 128, 128, 6)
# segmentation_image = np.random.rand(1, 128, 128, 2)
# background_image = np.random.rand(1, 128, 128, 4)
# _ = app.predict(spots_image=spots_image,
# segmentation_image=segmentation_image)

# test mask rounds
df_barcodes = pd.DataFrame(
[
["code1", 1, 1, 0, 0, 0, 0],
["code2", 0, 0, 1, 1, 0, 0],
["code3", 0, 0, 0, 0, 1, 0],
["code4", 1, 0, 0, 0, 1, 0],
["code5", 0, 0, 1, 0, 0, 0],
["code6", 0, 1, 0, 0, 1, 0],
["code7", 1, 0, 1, 0, 0, 0],
],
columns=["Gene", "r0c0", "r0c1", "r0c2", "r1c0", "r1c1", "r1c2"],
index=np.arange(7) + 1,
)
r = 2
c = 3
decoding_kwargs = {'df_barcodes': df_barcodes, 'rounds': r,
'channels': c, 'distribution': 'Relaxed Bernoulli',
'params_mode': '2*R*C'}
app = Polaris(image_type='multiplex', decoding_kwargs=decoding_kwargs)
# # test mask rounds
# df_barcodes = pd.DataFrame(
# [
# ["code1", 1, 1, 0, 0, 0, 0],
# ["code2", 0, 0, 1, 1, 0, 0],
# ["code3", 0, 0, 0, 0, 1, 0],
# ["code4", 1, 0, 0, 0, 1, 0],
# ["code5", 0, 0, 1, 0, 0, 0],
# ["code6", 0, 1, 0, 0, 1, 0],
# ["code7", 1, 0, 1, 0, 0, 0],
# ],
# columns=["Gene", "r0c0", "r0c1", "r0c2", "r1c0", "r1c1", "r1c2"],
# index=np.arange(7) + 1,
# )
# r = 2
# c = 3
# decoding_kwargs = {'df_barcodes': df_barcodes, 'rounds': r,
# 'channels': c, 'distribution': 'Relaxed Bernoulli',
# 'params_mode': '2*R*C'}
# app = Polaris(spots_model=spots_model, image_type='multiplex',
# decoding_kwargs=decoding_kwargs)

spots_image = np.random.rand(1, 128, 128, r*c) + 1
segmentation_image = np.random.rand(1, 128, 128, 1)
background_image = np.random.rand(1, 128, 128, 1)
pred = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image,
background_image=background_image)
df_results = pred[0]
segmentation_result = pred[1]
# spots_image = np.random.rand(1, 128, 128, r*c) + 1
# segmentation_image = np.random.rand(1, 128, 128, 1)
# background_image = np.random.rand(1, 128, 128, 1)
# pred = app.predict(spots_image=spots_image,
# segmentation_image=segmentation_image,
# background_image=background_image)
# df_results = pred[0]
# segmentation_result = pred[1]

self.assertEqual(np.sum(df_results.values[:,-1]), 0)
# self.assertEqual(np.sum(df_results.values[:,-1]), 0)

# test prediction type -- singleplex
app = Polaris()
spots_image = np.random.rand(1, 128, 128, 1)
segmentation_image = np.random.rand(1, 128, 128, 1)
background_image = np.random.rand(1, 128, 128, 1)
pred = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image,
background_image=background_image)
df_results = pred[0]
segmentation_result = pred[1]
self.assertIsInstance(df_results, pd.DataFrame)
self.assertIsInstance(segmentation_result, np.ndarray)
self.assertAllEqual(segmentation_image.shape, segmentation_result.shape)
self.assertAllEqual(df_results.probability, [None]*len(df_results))
self.assertAllEqual(df_results.predicted_id, [None]*len(df_results))
self.assertAllEqual(df_results.predicted_name, [None]*len(df_results))
# # test prediction type -- singleplex
# app = Polaris(spots_model=spots_model)
# spots_image = np.random.rand(1, 128, 128, 1)
# segmentation_image = np.random.rand(1, 128, 128, 1)
# background_image = np.random.rand(1, 128, 128, 1)
# pred = app.predict(spots_image=spots_image,
# segmentation_image=segmentation_image,
# background_image=background_image)
# df_results = pred[0]
# segmentation_result = pred[1]
# self.assertIsInstance(df_results, pd.DataFrame)
# self.assertIsInstance(segmentation_result, np.ndarray)
# self.assertAllEqual(segmentation_image.shape, segmentation_result.shape)
# self.assertAllEqual(df_results.probability, [None]*len(df_results))
# self.assertAllEqual(df_results.predicted_id, [None]*len(df_results))
# self.assertAllEqual(df_results.predicted_name, [None]*len(df_results))

# test prediction type -- multivariate Gaussian
df_barcodes = pd.DataFrame(
[
["code1", 1, 1, 0, 0, 0, 0],
["code2", 0, 0, 1, 1, 0, 0],
["code3", 0, 0, 0, 0, 1, 1],
["code4", 1, 0, 0, 0, 1, 0],
["code5", 0, 0, 1, 0, 0, 1],
["code6", 0, 1, 0, 0, 1, 0],
["code7", 1, 0, 1, 0, 0, 0],
],
columns=["Gene", "r0c0", "r0c1", "r0c2", "r1c0", "r1c1", "r1c2"],
index=np.arange(7) + 1,
)
r = 2
c = 3
decoding_kwargs = {'df_barcodes': df_barcodes, 'rounds': r,
'channels': c, 'distribution': 'Gaussian'}
app = Polaris(image_type='multiplex', decoding_kwargs=decoding_kwargs)
# # test prediction type -- multivariate Gaussian
# df_barcodes = pd.DataFrame(
# [
# ["code1", 1, 1, 0, 0, 0, 0],
# ["code2", 0, 0, 1, 1, 0, 0],
# ["code3", 0, 0, 0, 0, 1, 1],
# ["code4", 1, 0, 0, 0, 1, 0],
# ["code5", 0, 0, 1, 0, 0, 1],
# ["code6", 0, 1, 0, 0, 1, 0],
# ["code7", 1, 0, 1, 0, 0, 0],
# ],
# columns=["Gene", "r0c0", "r0c1", "r0c2", "r1c0", "r1c1", "r1c2"],
# index=np.arange(7) + 1,
# )
# r = 2
# c = 3
# decoding_kwargs = {'df_barcodes': df_barcodes, 'rounds': r,
# 'channels': c, 'distribution': 'Gaussian'}
# app = Polaris(spots_model=spots_model, image_type='multiplex',
# decoding_kwargs=decoding_kwargs)

spots_image = np.random.rand(1, 128, 128, r*c) + 1
segmentation_image = np.random.rand(1, 128, 128, 1)
background_image = np.random.rand(1, 128, 128, 1)
pred = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image,
background_image=background_image)
df_results = pred[0]
segmentation_result = pred[1]
self.assertIsInstance(df_results, pd.DataFrame)
self.assertIsInstance(segmentation_result, np.ndarray)
self.assertAllEqual(segmentation_image.shape, segmentation_result.shape)
# spots_image = np.random.rand(1, 128, 128, r*c) + 1
# segmentation_image = np.random.rand(1, 128, 128, 1)
# background_image = np.random.rand(1, 128, 128, 1)
# pred = app.predict(spots_image=spots_image,
# segmentation_image=segmentation_image,
# background_image=background_image)
# df_results = pred[0]
# segmentation_result = pred[1]
# self.assertIsInstance(df_results, pd.DataFrame)
# self.assertIsInstance(segmentation_result, np.ndarray)
# self.assertAllEqual(segmentation_image.shape, segmentation_result.shape)

# test prediction type -- Bernoulli
df_barcodes = pd.DataFrame(
[
["code1", 1, 1, 0, 0, 0, 0],
["code2", 0, 0, 1, 1, 0, 0],
["code3", 0, 0, 0, 0, 1, 1],
["code4", 1, 0, 0, 0, 1, 0],
["code5", 0, 0, 1, 0, 0, 1],
["code6", 0, 1, 0, 0, 1, 0],
["code7", 1, 0, 1, 0, 0, 0],
],
columns=["Gene", "r0c0", "r0c1", "r0c2", "r1c0", "r1c1", "r1c2"],
index=np.arange(7) + 1,
)
r = 2
c = 3
decoding_kwargs = {'df_barcodes': df_barcodes, 'rounds': r,
'channels': c, 'distribution': 'Bernoulli'}
app = Polaris(image_type='multiplex', decoding_kwargs=decoding_kwargs)
# # test prediction type -- Bernoulli
# df_barcodes = pd.DataFrame(
# [
# ["code1", 1, 1, 0, 0, 0, 0],
# ["code2", 0, 0, 1, 1, 0, 0],
# ["code3", 0, 0, 0, 0, 1, 1],
# ["code4", 1, 0, 0, 0, 1, 0],
# ["code5", 0, 0, 1, 0, 0, 1],
# ["code6", 0, 1, 0, 0, 1, 0],
# ["code7", 1, 0, 1, 0, 0, 0],
# ],
# columns=["Gene", "r0c0", "r0c1", "r0c2", "r1c0", "r1c1", "r1c2"],
# index=np.arange(7) + 1,
# )
# r = 2
# c = 3
# decoding_kwargs = {'df_barcodes': df_barcodes, 'rounds': r,
# 'channels': c, 'distribution': 'Bernoulli'}
# app = Polaris(spots_model=spots_model, image_type='multiplex',
# decoding_kwargs=decoding_kwargs)

spots_image = np.random.rand(1, 128, 128, r*c) + 1
segmentation_image = np.random.rand(1, 128, 128, 1)
background_image = np.random.rand(1, 128, 128, 1)
pred = app.predict(spots_image=spots_image,
segmentation_image=segmentation_image,
background_image=background_image)
df_results = pred[0]
segmentation_result = pred[1]
self.assertIsInstance(df_results, pd.DataFrame)
self.assertIsInstance(segmentation_result, np.ndarray)
self.assertAllEqual(segmentation_image.shape, segmentation_result.shape)
self.assertAllInRange(df_results.probability, 0, 1)
# spots_image = np.random.rand(1, 128, 128, r*c) + 1
# segmentation_image = np.random.rand(1, 128, 128, 1)
# background_image = np.random.rand(1, 128, 128, 1)
# pred = app.predict(spots_image=spots_image,
# segmentation_image=segmentation_image,
# background_image=background_image)
# df_results = pred[0]
# segmentation_result = pred[1]
# self.assertIsInstance(df_results, pd.DataFrame)
# self.assertIsInstance(segmentation_result, np.ndarray)
# self.assertAllEqual(segmentation_image.shape, segmentation_result.shape)
# self.assertAllInRange(df_results.probability, 0, 1)
21 changes: 14 additions & 7 deletions deepcell_spots/applications/spot_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@
import timeit

import tensorflow as tf
from pathlib import Path
from deepcell.applications import Application
from deepcell.utils import fetch_data, extract_archive

from deepcell_spots.dotnet_losses import DotNetLosses
from deepcell_spots.utils.postprocessing_utils import y_annotations_to_point_list_max
from deepcell_spots.utils.preprocessing_utils import min_max_normalize


MODEL_PATH = ('https://deepcell-data.s3-us-west-1.amazonaws.com/'
'saved-models/SpotDetection-7.tar.gz')
MODEL_KEY = 'models/SpotDetection-7.tar.gz'
MODEL_NAME = 'SpotDetection'
MODEL_HASH = 'f52d473ad7e4ce33472f1a9a9cae2d85'


def output_to_dictionary(output_images, output_names):
Expand Down Expand Up @@ -108,12 +111,16 @@ class SpotDetection(Application):
def __init__(self, model=None):

if model is None:
archive_path = tf.keras.utils.get_file(
'SpotDetection.tgz', MODEL_PATH,
file_hash='f52d473ad7e4ce33472f1a9a9cae2d85',
extract=True, cache_subdir='models'
cache_subdir = "models"
model_dir = Path.home() / ".deepcell" / "models"

archive_path = fetch_data(
asset_key=MODEL_KEY,
cache_subdir=cache_subdir,
file_hash=MODEL_HASH
)
model_path = os.path.splitext(archive_path)[0]
extract_archive(archive_path, model_dir)
model_path = model_dir / MODEL_NAME
model = tf.keras.models.load_model(
model_path, custom_objects={
'regression_loss': DotNetLosses.regression_loss,
Expand Down
Loading
Loading