Skip to content

Commit

Permalink
Merge pull request #12 from fish-quant/release/v0.2.0
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
Henley13 authored May 15, 2020
2 parents eb3a092 + d8ecf17 commit 91a54bc
Show file tree
Hide file tree
Showing 10 changed files with 1,427 additions and 525 deletions.
41 changes: 23 additions & 18 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
MIT License
BSD 3-Clause License

Copyright (c) 2018 Arthur Imbert
Copyright © 2020, Arthur Imbert
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 changes: 10 additions & 3 deletions bigfish/classification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
patterns of the RNA.
"""

# from .squeezenet import SqueezeNet0
from .input_preparation import (prepare_coordinate_data,
build_boundaries_layers, build_surface_layers,
build_distance_layers, Generator)
from .squeezenet import SqueezeNet0, SqueezeNet_qbi
from .features import get_features, get_features_name

# ### Load models ###

_features = ["get_features", "get_features_name"]

# _squeezenet = ["SqueezeNet0"]
_input_preparation = ["prepare_coordinate_data", "build_boundaries_layers",
"build_surface_layers", "build_distance_layers",
"Generator"]

__all__ = _features
_squeezenet = ["SqueezeNet0", "SqueezeNet_qbi"]

__all__ = _features + _input_preparation + _squeezenet
120 changes: 9 additions & 111 deletions bigfish/classification/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
Functions to craft features.
"""

import bigfish.stack as stack

import numpy as np
from scipy import ndimage as ndi

from skimage.measure import regionprops
from skimage.morphology import binary_opening
from skimage.morphology.selem import disk

from .input_preparation import prepare_coordinate_data


# TODO add sanity check functions
# TODO add documentation
# TODO allow to return intermediate results (distance map, etc.)
# TODO round float results


def get_features(cyt_coord, nuc_coord, rna_coord,
compute_distance=True,
compute_intranuclear=True,
Expand Down Expand Up @@ -233,112 +232,6 @@ def get_features_name(names_features_distance=True,
return features_name


# ### Prepare the data ###

def from_coord_to_matrix(cyt_coord, nuc_coord):
# get size of the frame
max_y = cyt_coord[:, 0].max() + stack.get_offset_value() * 2
max_x = cyt_coord[:, 1].max() + stack.get_offset_value() * 2
image_shape = (max_y, max_x)

# cytoplasm
cyt = np.zeros(image_shape, dtype=bool)
cyt[cyt_coord[:, 0] + stack.get_offset_value(),
cyt_coord[:, 1] + stack.get_offset_value()] = True

# nucleus
nuc = np.zeros(image_shape, dtype=bool)
nuc[nuc_coord[:, 0] + stack.get_offset_value(),
nuc_coord[:, 1] + stack.get_offset_value()] = True

return cyt, nuc


def get_centroid_surface(mask):
# get centroid
region = regionprops(mask.astype(np.uint8))[0]
centroid = np.array(region.centroid, dtype=np.int64)

return centroid


def get_centroid_rna(rna_coord):
# get rna centroids
centroid_rna = np.mean(rna_coord[:, :3], axis=0, dtype=np.int64)
return centroid_rna


def get_centroid_distance_map(centroid_coordinate, mask_cyt):
if centroid_coordinate.size == 3:
centroid_coordinate_2d = centroid_coordinate[1:]
else:
centroid_coordinate_2d = centroid_coordinate.copy()

# get mask centroid
mask_centroid = np.zeros_like(mask_cyt)
mask_centroid[centroid_coordinate_2d[0], centroid_coordinate_2d[1]] = True

# compute distance map
distance_map = ndi.distance_transform_edt(~mask_centroid)
distance_map[mask_cyt == 0] = 0
distance_map = distance_map.astype(np.float32)

return distance_map


def prepare_coordinate_data(cyt_coord, nuc_coord, rna_coord):
# get a binary representation of the coordinates
cyt, nuc = from_coord_to_matrix(cyt_coord, nuc_coord)
rna_coord[:, 1:3] += stack.get_offset_value()

# fill in masks
mask_cyt, mask_nuc = stack.get_surface_layers(cyt, nuc, cast_float=False)

# get mask cytoplasm outside nucleus
mask_cyt_out = mask_cyt.copy()
mask_cyt_out[mask_nuc] = False

# compute distance maps for the cytoplasm and the nucleus
distance_cyt, distance_nuc = stack.get_distance_layers(cyt, nuc,
normalized=False)

# normalize distance maps between 0 and 1
distance_cyt_normalized = distance_cyt / distance_cyt.max()
distance_cyt_normalized = stack.cast_img_float32(distance_cyt_normalized)
distance_nuc_normalized = distance_nuc / distance_nuc.max()
distance_nuc_normalized = stack.cast_img_float32(distance_nuc_normalized)

# get rna outside nucleus
mask_rna_in = mask_nuc[rna_coord[:, 1], rna_coord[:, 2]]
rna_coord_out = rna_coord[~mask_rna_in]

# get centroids
centroid_cyt = get_centroid_surface(mask_cyt)
centroid_nuc = get_centroid_surface(mask_nuc)
centroid_rna = get_centroid_rna(rna_coord)
if len(rna_coord_out) == 0:
centroid_rna_out = centroid_cyt.copy()
else:
centroid_rna_out = get_centroid_rna(rna_coord_out)

# get centroid distance maps
distance_cyt_centroid = get_centroid_distance_map(centroid_cyt, mask_cyt)
distance_nuc_centroid = get_centroid_distance_map(centroid_nuc, mask_cyt)
distance_rna_out_centroid = get_centroid_distance_map(centroid_rna_out,
mask_cyt)

prepared_inputs = (mask_cyt, mask_nuc, mask_cyt_out,
distance_cyt, distance_nuc,
distance_cyt_normalized, distance_nuc_normalized,
rna_coord_out,
centroid_cyt, centroid_nuc,
centroid_rna, centroid_rna_out,
distance_cyt_centroid, distance_nuc_centroid,
distance_rna_out_centroid)

return prepared_inputs


# ### Other features ###

def features_distance(rna_coord_out, distance_cyt, distance_nuc, mask_cyt_out):
Expand Down Expand Up @@ -376,6 +269,11 @@ def features_distance(rna_coord_out, distance_cyt, distance_nuc, mask_cyt_out):


def features_in_out_nucleus(rna_coord, rna_coord_out):
# case where no mRNAs outside the nucleus are detected
if len(rna_coord) == 0:
features = [0, 0, 0]
return features

# number of mRNAs outside and inside nucleus
nb_rna_out = len(rna_coord_out)
nb_rna_in = len(rna_coord) - nb_rna_out
Expand Down Expand Up @@ -509,7 +407,7 @@ def features_topography(rna_coord, rna_coord_out, mask_cyt, mask_nuc,
nb_rna_out = len(rna_coord_out)

# case where no mRNAs outside the nucleus are detected
if nb_rna_out == 0:
if nb_rna == 0 or nb_rna_out == 0:
features = [0., 0.]
features += [0., 0.] * 5
features += [0., 0.] * 6
Expand Down
Loading

0 comments on commit 91a54bc

Please sign in to comment.