Skip to content

Commit

Permalink
update examples and improve 2D projections
Browse files Browse the repository at this point in the history
  • Loading branch information
Henley13 committed May 5, 2020
1 parent e506909 commit 141ef56
Show file tree
Hide file tree
Showing 6 changed files with 1,095 additions and 55 deletions.
5 changes: 4 additions & 1 deletion bigfish/plot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
plot_cell_coordinates, plot_layers_coordinates,
plot_cell)
from .plot_classification import plot_confusion_matrix, plot_2d_projection
from .plot_quality import plot_sharpness


_images = ["plot_yx", "plot_images", "plot_channels_2d",
Expand All @@ -25,4 +26,6 @@

_classification = ["plot_confusion_matrix", "plot_2d_projection"]

__all__ = _images + _coordinates + _classification
_quality = ["plot_sharpness"]

__all__ = _images + _coordinates + _classification + _quality
95 changes: 95 additions & 0 deletions bigfish/plot/plot_quality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# -*- coding: utf-8 -*-

"""
Function to plot quality control indicators.
"""

import bigfish.stack as stack

import matplotlib.pyplot as plt
import numpy as np

from .utils import save_plot


def plot_sharpness(focus_measures, labels=None, title=None, framesize=(5, 5),
size_title=20, size_axes=15, size_legend=15,
path_output=None, ext="png", show=True):
"""
Parameters
----------
focus_measures : np.ndarray or List[np.ndarray]
A list of 1-d array with the sharpness measure for each z-slices.
labels : List[str]
List of labels for the different measures to compare.
title : str
Title of the plot.
framesize : tuple
Size of the frame used to plot with 'plt.figure(figsize=framesize)'.
size_title : int
Size of the title.
size_axes : int
Size of the axes label.
size_legend : int
Size of the legend.
path_output : str
Path to save the image (without extension).
ext : str or List[str]
Extension used to save the plot. If it is a list of strings, the plot
will be saved several times.
show : bool
Show the figure or not.
Returns
-------
"""
# enlist image if necessary
if isinstance(focus_measures, np.ndarray):
focus_measures = [focus_measures]

# check parameters
stack.check_parameter(focus_measures=list,
labels=(list, type(None)),
title=(str, list, type(None)),
framesize=tuple,
size_title=int,
size_axes=int,
size_legend=int,
path_output=(str, type(None)),
ext=(str, list),
show=bool)
length = 0
for focus_measure in focus_measures:
stack.check_array(focus_measure,
ndim=1,
dtype=[np.float32, np.float64])
length = max(length, focus_measure.size)

# plot
plt.figure(figsize=framesize)
y = np.array([i for i in range(length)])
for i, focus_measure in enumerate(focus_measures):
if labels is not None:
plt.plot(focus_measure, y, label=labels[i])
else:
plt.plot(focus_measure, y)

# axes
if title is not None :
plt.title(title, fontweight="bold", fontsize=size_title)
plt.xlabel("sharpness measure", fontweight="bold", fontsize=size_axes)
plt.ylabel("z-slices", fontweight="bold", fontsize=size_axes)
if labels is not None:
plt.legend(prop={'size': size_legend})

plt.tight_layout()
if path_output is not None:
save_plot(path_output, ext)
if show:
plt.show()
else:
plt.close()

return
11 changes: 7 additions & 4 deletions bigfish/stack/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def mean_projection(tensor, return_float=False):
if return_float:
projected_tensor = tensor.mean(axis=0)
else:
projected_tensor = tensor.mean(axis=0, dtype=tensor.dtype)
projected_tensor = tensor.mean(axis=0).astype(tensor.dtype)

return projected_tensor

Expand Down Expand Up @@ -315,8 +315,10 @@ def focus_measurement(image, neighborhood_size=30, cast_8bit=False):
allow_nan=False)
check_parameter(neighborhood_size=int)

# cast image in np.uint8
if cast_8bit:
# cast image in np.uint
if image.dtype == np.uint8:
pass
elif cast_8bit:
image = cast_img_uint8(image, catch_warning=True)
else:
image = cast_img_uint16(image)
Expand Down Expand Up @@ -435,7 +437,7 @@ def get_in_focus_indices(global_focus, proportion):
Returns
-------
indices_to_keep : List[int]
Sorted indices of slices with the best focus score (decreasing score).
Indices of slices with the best focus score.
"""
# check parameters
Expand All @@ -453,6 +455,7 @@ def get_in_focus_indices(global_focus, proportion):
# select the best z-slices
n = min(n, global_focus.size)
indices_to_keep = list(np.argsort(-global_focus)[:n])
indices_to_keep = sorted(indices_to_keep)

return indices_to_keep

Expand Down
97 changes: 73 additions & 24 deletions examples/1 - Read and write images.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:39.962027Z",
"start_time": "2020-03-04T13:10:38.781548Z"
"end_time": "2020-03-09T15:13:15.290117Z",
"start_time": "2020-03-09T15:13:14.573327Z"
}
},
"outputs": [],
Expand All @@ -27,13 +27,13 @@
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:39.966851Z",
"start_time": "2020-03-04T13:10:39.964191Z"
"end_time": "2020-03-09T15:13:15.294066Z",
"start_time": "2020-03-09T15:13:15.291847Z"
}
},
"outputs": [],
"source": [
"# Hard-code the paths of our input and output directories\n",
"# hard-code the paths of our input and output directories\n",
"path_input = \"../data/input\"\n",
"path_output = \"../data/output\""
]
Expand Down Expand Up @@ -71,8 +71,8 @@
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:39.978018Z",
"start_time": "2020-03-04T13:10:39.968521Z"
"end_time": "2020-03-09T15:13:15.303052Z",
"start_time": "2020-03-09T15:13:15.295369Z"
}
},
"outputs": [
Expand Down Expand Up @@ -103,8 +103,8 @@
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:39.983368Z",
"start_time": "2020-03-04T13:10:39.980468Z"
"end_time": "2020-03-09T15:13:15.308257Z",
"start_time": "2020-03-09T15:13:15.305397Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -136,8 +136,8 @@
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:39.989422Z",
"start_time": "2020-03-04T13:10:39.985365Z"
"end_time": "2020-03-09T15:13:15.314151Z",
"start_time": "2020-03-09T15:13:15.310104Z"
}
},
"outputs": [
Expand Down Expand Up @@ -173,7 +173,7 @@
" \"pattern\": str\n",
" }\n",
" \n",
"__Note 2:__ If you have a large number of images to read, keeping the same template to name your files will simplify your life. "
"__Note 2:__ If you have a large number of images to read, keeping the same template to name your files will simplify your life."
]
},
{
Expand All @@ -188,8 +188,8 @@
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:40.036375Z",
"start_time": "2020-03-04T13:10:39.990999Z"
"end_time": "2020-03-09T15:13:15.356664Z",
"start_time": "2020-03-09T15:13:15.315390Z"
}
},
"outputs": [
Expand Down Expand Up @@ -241,8 +241,8 @@
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:40.043160Z",
"start_time": "2020-03-04T13:10:40.038014Z"
"end_time": "2020-03-09T15:13:15.362754Z",
"start_time": "2020-03-09T15:13:15.358035Z"
}
},
"outputs": [
Expand Down Expand Up @@ -274,8 +274,8 @@
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:40.127003Z",
"start_time": "2020-03-04T13:10:40.044555Z"
"end_time": "2020-03-09T15:13:15.439122Z",
"start_time": "2020-03-09T15:13:15.364144Z"
}
},
"outputs": [
Expand Down Expand Up @@ -309,6 +309,55 @@
" print(\"\\r dtype: {0}\".format(image.dtype))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Note:__ A generator can be used to read several images from the same recipe if you provide several FoV in this recipe."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-09T15:13:15.493163Z",
"start_time": "2020-03-09T15:13:15.440766Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Image 0\n",
"\r",
" shape: (1, 2, 23, 650, 500)\n",
"\r",
" dtype: uint8\n",
"Image 1\n",
"\r",
" shape: (1, 2, 23, 650, 500)\n",
"\r",
" dtype: uint8\n"
]
}
],
"source": [
"recipe = {\n",
" \"fov\": [\"fov_1\", \"fov_1\"],\n",
" \"c\": [\"dapi\", \"smfish\"],\n",
" \"opt\": \"experience_1\",\n",
" \"ext\": \"tif\",\n",
" \"pattern\": \"opt_c_fov.ext\"}\n",
"data_map = [(recipe, path_input)]\n",
"image_generator = stack.build_stacks(data_map)\n",
"for i, image in enumerate(image_generator):\n",
" print(\"Image {0}\".format(i))\n",
" print(\"\\r shape: {0}\".format(image.shape))\n",
" print(\"\\r dtype: {0}\".format(image.dtype))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -325,11 +374,11 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:40.158775Z",
"start_time": "2020-03-04T13:10:40.128748Z"
"end_time": "2020-03-09T15:13:15.523494Z",
"start_time": "2020-03-09T15:13:15.495137Z"
}
},
"outputs": [
Expand Down Expand Up @@ -376,11 +425,11 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2020-03-04T13:10:40.172930Z",
"start_time": "2020-03-04T13:10:40.160264Z"
"end_time": "2020-03-09T15:13:15.536967Z",
"start_time": "2020-03-09T15:13:15.525216Z"
}
},
"outputs": [
Expand Down
Loading

0 comments on commit 141ef56

Please sign in to comment.