extraction.core.extractor.Extractor¶
- class Extractor(parameters, store=None, tiler=None)[source]¶
Bases:
StepABC
Apply a metric to cells identified in the tiles.
Using the cell masks, the Extractor applies a metric, such as area or median, to cells identified in the image tiles.
Its methods require both tile images and masks.
Usually the metric is applied to only a tile’s masked area, but some metrics depend on the whole tile.
Extraction follows a three-level tree structure. Channels, such as GFP, are the root level; the reduction algorithm, such as maximum projection, is the second level; the specific metric, or operation, to apply to the masks is the third level.
- Parameters
- parameters: core.extractor Parameters
Parameters that include the channels, and reduction and extraction functions.
- store: str
Path to the h5 file, which must contain the cell masks.
- tiler: pipeline-core.core.segmentation tiler
Class that contains or fetches the images used for segmentation.
- Attributes
channels
Get a tuple of the available channels.
- current_position
- group
- parameters
Methods
extract_funs
(traps, masks, metrics, **kwargs)Returns dict with metrics as key and metrics applied to data as values for data from one timepoint.
extract_tp
(tp[, tree, tile_size, masks, labels])Extract for an individual time-point.
extract_traps
(traps, masks, metric, labels)Apply a function to a whole position.
from_img
(parameters, store, img_meta)Initiate from images.
from_tiler
(parameters, store, tiler)Initiate from a tiler instance.
get_imgs
(channel, traps[, channels])Return image from a correct source, either raw or bgsub.
get_tiles
(tp[, channels, z])Find tiles for a given time point and given channels and z-stacks.
Define any custom functions to be functions of cell_masks and trap_image only.
Load metadata from h5 file.
reduce_dims
(img[, method])Collapse a z-stack into 2d array using method.
reduce_extract
(traps, masks, red_metrics, ...)Wrapper to apply reduction and then extraction.
run_tp
(**kwargs)save_to_hdf
(dict_series[, path])Save the extracted data to the h5 file.
get_meta
load_funs
run
Initialise Extractor.
- Parameters
- parameters: ExtractorParameters object
- store: str
Name of h5 file
- tiler: Tiler object
- Attributes
channels
Get a tuple of the available channels.
- current_position
- group
- parameters
Methods
extract_funs
(traps, masks, metrics, **kwargs)Returns dict with metrics as key and metrics applied to data as values for data from one timepoint.
extract_tp
(tp[, tree, tile_size, masks, labels])Extract for an individual time-point.
extract_traps
(traps, masks, metric, labels)Apply a function to a whole position.
from_img
(parameters, store, img_meta)Initiate from images.
from_tiler
(parameters, store, tiler)Initiate from a tiler instance.
get_imgs
(channel, traps[, channels])Return image from a correct source, either raw or bgsub.
get_tiles
(tp[, channels, z])Find tiles for a given time point and given channels and z-stacks.
Define any custom functions to be functions of cell_masks and trap_image only.
Load metadata from h5 file.
reduce_dims
(img[, method])Collapse a z-stack into 2d array using method.
reduce_extract
(traps, masks, red_metrics, ...)Wrapper to apply reduction and then extraction.
run_tp
(**kwargs)save_to_hdf
(dict_series[, path])Save the extracted data to the h5 file.
get_meta
load_funs
run
- __init__(parameters, store=None, tiler=None)[source]¶
Initialise Extractor.
- Parameters
- parameters: ExtractorParameters object
- store: str
Name of h5 file
- tiler: Tiler object
Methods
__init__
(parameters[, store, tiler])Initialise Extractor.
extract_funs
(traps, masks, metrics, **kwargs)Returns dict with metrics as key and metrics applied to data as values for data from one timepoint.
extract_tp
(tp[, tree, tile_size, masks, labels])Extract for an individual time-point.
extract_traps
(traps, masks, metric, labels)Apply a function to a whole position.
from_img
(parameters, store, img_meta)Initiate from images.
from_tiler
(parameters, store, tiler)Initiate from a tiler instance.
get_imgs
(channel, traps[, channels])Return image from a correct source, either raw or bgsub.
get_meta
(flds)get_tiles
(tp[, channels, z])Find tiles for a given time point and given channels and z-stacks.
Define any custom functions to be functions of cell_masks and trap_image only.
load_funs
()Load metadata from h5 file.
reduce_dims
(img[, method])Collapse a z-stack into 2d array using method.
reduce_extract
(traps, masks, red_metrics, ...)Wrapper to apply reduction and then extraction.
run
()run_tp
(**kwargs)save_to_hdf
(dict_series[, path])Save the extracted data to the h5 file.
Attributes
Get a tuple of the available channels.
current_position
default_meta
group
parameters
- property channels¶
Get a tuple of the available channels.
- extract_funs(traps, masks, metrics, **kwargs)[source]¶
Returns dict with metrics as key and metrics applied to data as values for data from one timepoint.
- Return type
Dict
[str
,Series
]
- extract_tp(tp, tree=None, tile_size=117, masks=None, labels=None, **kwargs)[source]¶
Extract for an individual time-point.
- Parameters
- tpint
Time point being analysed.
- treedict
Nested dictionary indicating channels, reduction functions and metrics to be used. For example: {‘general’: {‘None’: [‘area’, ‘volume’, ‘eccentricity’]}}
- tile_sizeint
Size of the tile to be extracted.
- maskslist of arrays
A list of masks per trap with each mask having dimensions (ncells, tile_size, tile_size).
- labelsdict
A dictionary with trap_ids as keys and cell_labels as values.
- **kwargskeyword arguments
Passed to extractor.reduce_extract.
- Returns
- d: dict
Dictionary of the results with three levels of dictionaries. The first level has channels as keys. The second level has reduction metrics as keys. The third level has cell or background metrics as keys and a two-tuple as values. The first tuple is the result of applying the metrics to a particular cell or trap; the second tuple is either (trap_id, cell_label) for a metric applied to a cell or a trap_id for a metric applied to a trap.
An example is d[“GFP”][“np_max”][“mean”][0], which gives a tuple of the calculated mean GFP fluorescence for all cells.
- rtype
Dict
[str
,Dict
[str
,Dict
[str
,tuple
]]] ..
- extract_traps(traps, masks, metric, labels)[source]¶
Apply a function to a whole position.
- Parameters
- traps: list of arrays
List of images.
- masks: list of arrays
List of masks.
- metric: str
Metric to extract.
- labels: dict
A dict of cell labels with trap_ids as keys and a list of cell labels as values.
- pos_info: bool
Whether to add the position as an index or not.
- Returns
- res_idx: a tuple of tuples
A two-tuple of a tuple of results and a tuple with the corresponding trap_id and cell labels
- rtype
Tuple
[Union
[Tuple
[float
],Tuple
[Tuple
[int
]]]] ..
- get_imgs(channel, traps, channels=None)[source]¶
Return image from a correct source, either raw or bgsub.
- Parameters
- channel: str
Name of channel to get.
- traps: ndarray
An array of the image data having dimensions of (trap_id, channel, tp, tile_size, tile_size, n_zstacks).
- channels: list of str (optional)
List of available channels.
- Returns
- img: ndarray
An array of image data with dimensions (no traps, X, Y, no Z channels)
- get_tiles(tp, channels=None, z=None, **kwargs)[source]¶
Find tiles for a given time point and given channels and z-stacks.
Returns None if no tiles are found.
Any additional keyword arguments are passed to tiler.get_tiles_timepoint
- Parameters
- tp: int
Time point of interest
- channels: list of strings (optional)
Channels of interest
- z: list of integers (optional)
Indices for the z-stacks of interest
- :rtype: :py:data:`~typing.Optional`[:py:class:`~numpy.ndarray`]
- load_custom_funs()[source]¶
Define any custom functions to be functions of cell_masks and trap_image only.
Any other parameters are taken from the experiment’s metadata and automatically applied. These parameters therefore must be loaded within an Extractor instance.
- reduce_dims(img, method=None)[source]¶
Collapse a z-stack into 2d array using method.
If method is None, return the original data.
- Parameters
- img: array
An array of the image data arranged as (X, Y, Z)
- method: function
The reduction function
- :rtype: :py:class:`~numpy.ndarray`
- reduce_extract(traps, masks, red_metrics, **kwargs)[source]¶
Wrapper to apply reduction and then extraction.
- Parameters
- traps: array
An array of image data arranged as (traps, X, Y, Z)
- masks: list of arrays
An array of masks for each trap: one per cell at the trap
- red_metrics: dict
dict for which keys are reduction functions and values are either a list or a set of strings giving the metric functions. For example: {‘np_max’: {‘max5px’, ‘mean’, ‘median’}}
- **kwargs: dict
All other arguments passed to Extractor.extract_funs.
- Returns
- ——
- Dictionary of dataframes with the corresponding reductions and metrics nested.
- :rtype: :py:class:`~typing.Dict`[:py:class:`str`, :py:class:`~typing.Dict`[:py:data:`~typing.Union`[:py:data:`~typing.Callable`, :py:class:`str`, :py:obj:`None`], :py:class:`~typing.Dict`[:py:class:`str`, :py:class:`~pandas.core.series.Series`]]]