-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
297 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# Copyright 2016-2020 The Van Valen Lab at the California Institute of | ||
# Technology (Caltech), with support from the Paul Allen Family Foundation, | ||
# Google, & National Institutes of Health (NIH) under Grant U24CA224309-01. | ||
# All rights reserved. | ||
# | ||
# Licensed under a modified Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.github.com/vanvalenlab/kiosk-redis-consumer/LICENSE | ||
# | ||
# The Work provided may be used for non-commercial academic purposes only. | ||
# For any other use of the Work, including commercial use, please contact: | ||
# vanvalenlab@gmail.com | ||
# | ||
# Neither the name of Caltech nor the names of its contributors may be used | ||
# to endorse or promote products derived from this software without specific | ||
# prior written permission. | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================ | ||
"""ImageFileConsumer class for consuming image segmentation jobs.""" | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os | ||
import timeit | ||
|
||
import numpy as np | ||
|
||
from redis_consumer.consumers import ImageFileConsumer | ||
from redis_consumer import utils | ||
from redis_consumer import settings | ||
|
||
|
||
class MibiConsumer(ImageFileConsumer): | ||
"""Consumes image files and uploads the results""" | ||
|
||
def _consume(self, redis_hash): | ||
start = timeit.default_timer() | ||
self._redis_hash = redis_hash # workaround for logging. | ||
hvals = self.redis.hgetall(redis_hash) | ||
|
||
if hvals.get('status') in self.finished_statuses: | ||
self.logger.warning('Found completed hash `%s` with status %s.', | ||
redis_hash, hvals.get('status')) | ||
return hvals.get('status') | ||
|
||
self.logger.debug('Found hash to process `%s` with status `%s`.', | ||
redis_hash, hvals.get('status')) | ||
|
||
self.update_key(redis_hash, { | ||
'status': 'started', | ||
'identity_started': self.name, | ||
}) | ||
|
||
# Get model_name and version | ||
model_name, model_version = settings.MIBI_MODEL.split(':') | ||
|
||
_ = timeit.default_timer() | ||
|
||
# Load input image | ||
with utils.get_tempdir() as tempdir: | ||
fname = self.storage.download(hvals.get('input_file_name'), tempdir) | ||
# TODO: tiffs expand the last axis, is that a problem here? | ||
image = utils.get_image(fname) | ||
|
||
# Pre-process data before sending to the model | ||
self.update_key(redis_hash, { | ||
'status': 'pre-processing', | ||
'download_time': timeit.default_timer() - _, | ||
}) | ||
|
||
# Calculate scale of image and rescale | ||
scale = hvals.get('scale', '') | ||
if not scale: | ||
# Detect scale of image (Default to 1) | ||
# TODO: implement SCALE_DETECT here for mibi model | ||
# scale = self.detect_scale(image) | ||
# self.logger.debug('Image scale detected: %s', scale) | ||
# self.update_key(redis_hash, {'scale': scale}) | ||
self.logger.debug('Scale was not given. Defaults to 1') | ||
scale = 1 | ||
else: | ||
scale = float(scale) | ||
self.logger.debug('Image scale already calculated: %s', scale) | ||
|
||
# Rescale each channel of the image | ||
image = utils.rescale(image, scale) | ||
image = np.expand_dims(image, axis=0) # add in the batch dim | ||
|
||
# Preprocess image | ||
image = self.preprocess(image, ['histogram_normalization']) | ||
|
||
# Send data to the model | ||
self.update_key(redis_hash, {'status': 'predicting'}) | ||
image = self.predict(image, model_name, model_version) | ||
|
||
# Post-process model results | ||
self.update_key(redis_hash, {'status': 'post-processing'}) | ||
image = self.postprocess(image, ['mibi']) | ||
|
||
# Save the post-processed results to a file | ||
_ = timeit.default_timer() | ||
self.update_key(redis_hash, {'status': 'saving-results'}) | ||
|
||
save_name = hvals.get('original_name', fname) | ||
dest, output_url = self.save_output(image, redis_hash, save_name, scale) | ||
|
||
# Update redis with the final results | ||
t = timeit.default_timer() - start | ||
self.update_key(redis_hash, { | ||
'status': self.final_status, | ||
'output_url': output_url, | ||
'upload_time': timeit.default_timer() - _, | ||
'output_file_name': dest, | ||
'total_jobs': 1, | ||
'total_time': t, | ||
'finished_at': self.get_current_timestamp() | ||
}) | ||
return self.final_status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Copyright 2016-2020 The Van Valen Lab at the California Institute of | ||
# Technology (Caltech), with support from the Paul Allen Family Foundation, | ||
# Google, & National Institutes of Health (NIH) under Grant U24CA224309-01. | ||
# All rights reserved. | ||
# | ||
# Licensed under a modified Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.github.com/vanvalenlab/kiosk-redis-consumer/LICENSE | ||
# | ||
# The Work provided may be used for non-commercial academic purposes only. | ||
# For any other use of the Work, including commercial use, please contact: | ||
# vanvalenlab@gmail.com | ||
# | ||
# Neither the name of Caltech nor the names of its contributors may be used | ||
# to endorse or promote products derived from this software without specific | ||
# prior written permission. | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================ | ||
"""Tests for MibiConsumer""" | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import itertools | ||
|
||
import numpy as np | ||
|
||
import pytest | ||
|
||
from redis_consumer import consumers | ||
from redis_consumer.testing_utils import redis_client, DummyStorage | ||
|
||
|
||
class TestMibiConsumer(object): | ||
# pylint: disable=R0201 | ||
|
||
def test_is_valid_hash(self, mocker, redis_client): | ||
storage = DummyStorage() | ||
mocker.patch.object(redis_client, 'hget', lambda *x: x[0]) | ||
|
||
consumer = consumers.MibiConsumer(redis_client, storage, 'mibi') | ||
assert consumer.is_valid_hash(None) is False | ||
assert consumer.is_valid_hash('file.ZIp') is False | ||
assert consumer.is_valid_hash('predict:1234567890:file.ZIp') is False | ||
assert consumer.is_valid_hash('track:123456789:file.zip') is False | ||
assert consumer.is_valid_hash('predict:123456789:file.zip') is False | ||
assert consumer.is_valid_hash('mibi:1234567890:file.tiff') is True | ||
assert consumer.is_valid_hash('mibi:1234567890:file.png') is True | ||
|
||
def test__consume(self, mocker, redis_client): | ||
# pylint: disable=W0613 | ||
|
||
def make_model_metadata_of_size(model_shape=(-1, 256, 256, 2)): | ||
|
||
def get_model_metadata(model_name, model_version): | ||
return [{ | ||
'in_tensor_name': 'image', | ||
'in_tensor_dtype': 'DT_FLOAT', | ||
'in_tensor_shape': ','.join(str(s) for s in model_shape), | ||
}] | ||
return get_model_metadata | ||
|
||
def make_grpc_image(model_shape=(-1, 256, 256, 2)): | ||
# pylint: disable=E1101 | ||
shape = model_shape[1:-1] | ||
|
||
def grpc(data, *args, **kwargs): | ||
inner = np.random.random((1,) + shape + (1,)) | ||
outer = np.random.random((1,) + shape + (1,)) | ||
fgbg = np.random.random((1,) + shape + (2,)) | ||
feature = np.random.random((1,) + shape + (3,)) | ||
return [inner, outer, fgbg, feature] | ||
return grpc | ||
|
||
image_shape = (300, 300, 2) | ||
model_shapes = [ | ||
(-1, 600, 600, 2), # image too small, pad | ||
(-1, 300, 300, 2), # image is exactly the right size | ||
(-1, 150, 150, 2), # image too big, tile | ||
] | ||
|
||
scales = ['.9', ''] | ||
|
||
job_data = { | ||
'input_file_name': 'file.tiff', | ||
} | ||
|
||
consumer = consumers.MibiConsumer(redis_client, DummyStorage(), 'mibi') | ||
|
||
test_hash = 0 | ||
# test finished statuses are returned | ||
for status in (consumer.failed_status, consumer.final_status): | ||
test_hash += 1 | ||
data = job_data.copy() | ||
data['status'] = status | ||
redis_client.hmset(test_hash, data) | ||
result = consumer._consume(test_hash) | ||
assert result == status | ||
result = redis_client.hget(test_hash, 'status') | ||
assert result == status | ||
test_hash += 1 | ||
|
||
mocker.patch('redis_consumer.utils.get_image', | ||
lambda x: np.random.random(image_shape)) | ||
|
||
for model_shape, scale in itertools.product(model_shapes, scales): | ||
metadata = make_model_metadata_of_size(model_shape) | ||
grpc_image = make_grpc_image(model_shape) | ||
mocker.patch.object(consumer, 'get_model_metadata', metadata) | ||
mocker.patch.object(consumer, 'grpc_image', grpc_image) | ||
|
||
data = job_data.copy() | ||
data['scale'] = scale | ||
|
||
redis_client.hmset(test_hash, data) | ||
result = consumer._consume(test_hash) | ||
assert result == consumer.final_status | ||
result = redis_client.hget(test_hash, 'status') | ||
assert result == consumer.final_status | ||
test_hash += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters