def _find_spots( self, data_stack: ImageStack, verbose: bool = False, n_processes: Optional[int] = None ) -> Dict[Tuple[int, int], np.ndarray]: """Find spots in all (z, y, x) volumes of an ImageStack. Parameters ---------- data_stack : ImageStack Stack containing spots to find. Returns ------- Dict[Tuple[int, int], np.ndarray] Dictionary mapping (round, channel) pairs to a spot table generated by skimage blob_log or blob_dog. """ # find spots in each (r, c) volume transform_results = data_stack.transform( self._spot_finder, group_by=determine_axes_to_group_by(self.is_volume), n_processes=n_processes, ) # create output dictionary spot_results = {} for spot_calls, axes_dict in transform_results: r = axes_dict[Axes.ROUND] c = axes_dict[Axes.CH] spot_results[r, c] = spot_calls return spot_results
def run( self, image_stack: ImageStack, reference_image: Optional[ImageStack] = None, n_processes: Optional[int] = None, *args, **kwargs ) -> SpotFindingResults: """ Find spots in the given ImageStack using a local maxima finding algorithm. If a reference image is provided the spots will be detected there then measured across all rounds and channels in the corresponding ImageStack. If a reference_image is not provided spots will be detected _independently_ in each channel. This assumes a non-multiplex imaging experiment, as only one (ch, round) will be measured for each spot. Parameters ---------- image_stack : ImageStack ImageStack where we find the spots in. reference_image : Optional[ImageStack] (Optional) a reference image. If provided, spots will be found in this image, and then the locations that correspond to these spots will be measured across each channel. n_processes : Optional[int] = None, Number of processes to devote to spot finding. """ spot_finding_method = partial(self.image_to_spots, **self.kwargs) if reference_image: shape = reference_image.shape assert shape[Axes.ROUND] == 1 assert shape[Axes.CH] == 1 spot_attributes_lists = reference_image.transform( func=spot_finding_method, group_by=determine_axes_to_group_by(self.is_volume), n_processes=n_processes ) spot_attributes_lists = combine_spot_attributes_by_round_channel(spot_attributes_lists) assert len(spot_attributes_lists) == 1 results = spot_finding_utils.measure_intensities_at_spot_locations_across_imagestack( data_image=image_stack, reference_spots=spot_attributes_lists[0][0], measurement_function=self.measurement_function) else: spot_attributes_lists = image_stack.transform( func=spot_finding_method, group_by=determine_axes_to_group_by(self.is_volume), n_processes=n_processes ) spot_attributes_lists = combine_spot_attributes_by_round_channel(spot_attributes_lists) results = SpotFindingResults(imagestack_coords=image_stack.xarray.coords, log=image_stack.log, spot_attributes_list=spot_attributes_lists) return results
def run( self, image_stack: ImageStack, reference_image: Optional[ImageStack] = None, n_processes: Optional[int] = None, *args, ) -> SpotFindingResults: """ Find spots in the given ImageStack using a gaussian blob finding algorithm. If a reference image is provided the spots will be detected there then measured across all rounds and channels in the corresponding ImageStack. If a reference_image is not provided spots will be detected _independently_ in each channel. This assumes a non-multiplex imaging experiment, as only one (ch, round) will be measured for each spot. Parameters ---------- image_stack : ImageStack ImageStack where we find the spots in. reference_image : Optional[ImageStack] (Optional) a reference image. If provided, spots will be found in this image, and then the locations that correspond to these spots will be measured across each channel. n_processes : Optional[int] = None, Number of processes to devote to spot finding. """ spot_finding_method = partial(self.image_to_spots, *args) if reference_image: data_image = reference_image._squeezed_numpy( *{Axes.ROUND, Axes.CH}) if self.detector_method is blob_doh and data_image.ndim > 2: raise ValueError("blob_doh only support 2d images") reference_spots = spot_finding_method(data_image) results = spot_finding_utils.measure_intensities_at_spot_locations_across_imagestack( data_image=image_stack, reference_spots=reference_spots, measurement_function=self.measurement_function) else: if self.detector_method is blob_doh and self.is_volume: raise ValueError("blob_doh only support 2d images") spot_attributes_list = image_stack.transform( func=spot_finding_method, group_by=determine_axes_to_group_by(self.is_volume), n_processes=n_processes) results = SpotFindingResults( imagestack_coords=image_stack.xarray.coords, log=image_stack.log, spot_attributes_list=spot_attributes_list) return results
def run( self, image_stack: ImageStack, reference_image: Optional[ImageStack] = None, n_processes: Optional[int] = None, *args, ) -> SpotFindingResults: """ Find spots. Parameters ---------- image_stack : ImageStack ImageStack where we find the spots in. reference_image : xr.DataArray (Optional) a reference image. If provided, spots will be found in this image, and then the locations that correspond to these spots will be measured across each channel. n_processes : Optional[int] = None, Number of processes to devote to spot finding. """ spot_finding_method = partial(self.image_to_spots, *args) if reference_image: data_image = reference_image._squeezed_numpy( *{Axes.ROUND, Axes.CH}) reference_spots = spot_finding_method(data_image) results = spot_finding_utils.measure_intensities_at_spot_locations_across_imagestack( image_stack, reference_spots, measurement_function=self.measurement_function, radius_is_gyration=self.radius_is_gyration) else: spot_attributes_list = image_stack.transform( func=spot_finding_method, group_by=determine_axes_to_group_by(self.is_volume), n_processes=n_processes) results = SpotFindingResults( imagestack_coords=image_stack.xarray.coords, log=image_stack.log, spot_attributes_list=spot_attributes_list) return results
def detect_spots(data_stack: ImageStack, spot_finding_method: Callable[..., SpotAttributes], spot_finding_kwargs: Dict = None, reference_image: Optional[ImageStack] = None, reference_image_max_projection_axes: Optional[Tuple[ Axes, ...]] = None, measurement_function: Callable[[Sequence], Number] = np.max, radius_is_gyration: bool = False, n_processes: Optional[int] = None) -> IntensityTable: """Apply a spot_finding_method to a ImageStack Parameters ---------- data_stack : ImageStack The ImageStack containing spots spot_finding_method : Callable[..., IntensityTable] The method to identify spots spot_finding_kwargs : Dict additional keyword arguments to pass to spot_finding_method reference_image : xr.DataArray (Optional) a reference image. If provided, spots will be found in this image, and then the locations that correspond to these spots will be measured across each channel and round, filling in the values in the IntensityTable reference_image_max_projection_axes : Tuple[Axes] Generate the reference image by max-projecting reference_image across these axes. measurement_function : Callable[[Sequence], Number] the function to apply over the spot area to extract the intensity value (default 'np.max') radius_is_gyration : bool if True, indicates that the radius corresponds to radius of gyration, which is a function of spot intensity, but typically is a smaller unit than the sigma generated by blob_log. In this case, the spot's bounding box is rounded up instead of down when measuring intensity. (default False) is_volume: bool If True, pass 3d volumes (x, y, z) to func, else pass 2d tiles (x, y) to func. (default True) n_processes : Optional[int] The number of processes to use in stack.transform if reference image is None. If None, uses the output of os.cpu_count() (default = None). Notes ----- - This class will always detect spots in 3d. If 2d spot detection is desired, the data should be projected down to "fake 3d" prior to submission to this function - If neither reference_image nor reference_from_max_projection are passed, spots will be detected _independently_ in each channel. This assumes a non-multiplex imaging experiment, as only one (ch, round) will be measured for each spot. Returns ------- IntensityTable : IntensityTable containing the intensity of each spot, its radius, and location in pixel coordinates """ if spot_finding_kwargs is None: spot_finding_kwargs = {} if reference_image is not None: if reference_image_max_projection_axes is not None: reference_image = reference_image.max_proj( *reference_image_max_projection_axes) data_image = reference_image._squeezed_numpy( *reference_image_max_projection_axes) else: data_image = reference_image.xarray reference_spot_locations = spot_finding_method(data_image, **spot_finding_kwargs) intensity_table = measure_spot_intensities( data_image=data_stack, spot_attributes=reference_spot_locations, measurement_function=measurement_function, radius_is_gyration=radius_is_gyration, ) else: # don't use a reference image, measure each spot_finding_method = partial(spot_finding_method, **spot_finding_kwargs) spot_attributes_list = data_stack.transform( func=spot_finding_method, group_by={Axes.ROUND, Axes.CH}, n_processes=n_processes) intensity_table = concatenate_spot_attributes_to_intensities( spot_attributes_list) transfer_physical_coords_from_imagestack_to_intensity_table( image_stack=data_stack, intensity_table=intensity_table) return intensity_table