Skip to content

Commit

Permalink
refactor cell extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Henley13 committed May 5, 2020
1 parent 973e15c commit bfb83f3
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 296 deletions.
14 changes: 10 additions & 4 deletions bigfish/stack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
from .io import read_image
from .io import read_dv
from .io import read_array
from .io import read_compressed
from .io import read_uncompressed
from .io import read_cell_extracted
from .io import read_array_from_csv
from .io import save_image
from .io import save_array
from .io import save_cell_extracted
from .io import save_array_to_csv

from .preprocess import build_stacks
Expand Down Expand Up @@ -57,9 +59,10 @@
from .illumination import compute_illumination_surface
from .illumination import correct_illumination_surface

from .postprocess import identify_transcription_site
from .postprocess import remove_transcription_site
from .postprocess import extract_cell
from .postprocess import extract_spots_from_frame
from .postprocess import extract_coordinates_image
from .postprocess import center_mask_coord
from .postprocess import from_boundaries_to_surface
from .postprocess import from_surface_to_boundaries
Expand All @@ -85,10 +88,12 @@
"read_image",
"read_dv",
"read_array",
"read_compressed",
"read_uncompressed",
"read_cell_extracted",
"read_array_from_csv",
"save_image",
"save_array",
"save_cell_extracted",
"save_array_to_csv"]

_preprocess = [
Expand Down Expand Up @@ -128,9 +133,10 @@
"correct_illumination_surface"]

_postprocess = [
"identify_transcription_site",
"remove_transcription_site",
"extract_cell",
"extract_spots_from_frame",
"extract_coordinates_image",
"center_mask_coord",
"from_boundaries_to_surface",
"from_surface_to_boundaries",
Expand Down
81 changes: 75 additions & 6 deletions bigfish/stack/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from .utils import check_array
from .utils import check_parameter

# TODO add general read function with mime types


# ### Read ###

Expand Down Expand Up @@ -144,23 +146,23 @@ def read_array_from_csv(path, dtype=np.float64):
return array


def read_compressed(path, verbose=False):
def read_uncompressed(path, verbose=False):
"""Read a NpzFile object with 'npz' extension.
Parameters
----------
path : str
Path of the file to read.
verbose : bool
Return names of the different compressed objects.
Return names of the different objects.
Returns
-------
data : NpzFile object
NpzFile read.
"""
# check path
# check parameters
check_parameter(path=str,
verbose=bool)
if ".npz" not in path:
Expand All @@ -169,11 +171,43 @@ def read_compressed(path, verbose=False):
# read array file
data = np.load(path)
if verbose:
print("Compressed objects: {0} \n".format(", ".join(data.files)))
print("Available keys: {0} \n".format(", ".join(data.files)))

return data


def read_cell_extracted(path, verbose=False):
"""Read a NpzFile object with 'npz' extension, previously written with
bigfish.stack.save_cell_extracted.
Parameters
----------
path : str
Path of the file to read.
verbose : bool
Return names of the different objects.
Returns
-------
cell_results : Dict
Dictionary including information about the cell (image, masks,
coordinates arrays). Minimal information are :
- bbox : bounding box coordinates (min_y, min_x, max_y, max_x).
- cell_coord : boundary coordinates of the cell.
- cell_mask : mask of the cell.
"""
# read compressed file
data = read_uncompressed(path, verbose)

# store data in a dictionary
cell_results = {}
for key in data.files:
cell_results[key] = data[key]

return cell_results


# ### Write ###

def save_image(image, path, extension="tif"):
Expand Down Expand Up @@ -291,7 +325,7 @@ def save_array(array, path):
np.float16, np.float32, np.float64,
bool],
ndim=[2, 3, 4, 5])
if "." in path and "npy" not in path:
if "." in path and ".npy" not in path:
path_ = path.split(".")[0]
path = path_ + ".npy"

Expand Down Expand Up @@ -328,7 +362,10 @@ def save_array_to_csv(array, path):
ndim=2)

# save csv file
if ".csv" not in path:
if "." in path and ".csv" not in path:
path_ = path.split(".")[0]
path = path_ + ".csv"
elif "." not in path:
path += ".csv"
if array.dtype == np.float16:
fmt = "%.4f"
Expand All @@ -341,3 +378,35 @@ def save_array_to_csv(array, path):
np.savetxt(path, array, fmt=fmt, delimiter=';', encoding="utf-8")

return


def save_cell_extracted(cell_results, path):
"""Save cell-level results from bigfish.stack.extract_cell in a NpzFile
object with 'npz' extension.
Parameters
----------
cell_results : Dict
Dictionary including information about the cell (image, masks,
coordinates arrays). Minimal information are :
- bbox : bounding box coordinates (min_y, min_x, max_y, max_x).
- cell_coord : boundary coordinates of the cell.
- cell_mask : mask of the cell.
path : str
Path of the saved array.
Returns
-------
"""
# check parameters
check_parameter(cell_results=dict,
path=str)

# save compressed file
if "." in path and ".npz" not in path:
path_ = path.split(".")[0]
path = path_ + ".npz"
np.savez(path, **cell_results)

return
Loading

0 comments on commit bfb83f3

Please sign in to comment.