Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.) refactoring localMaxPeakFinder/osmFIsh #1594

Merged
merged 2 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions notebooks/osmFISH.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,22 @@
"metadata": {},
"outputs": [],
"source": [
"from starfish.spots import DetectSpots\n",
"from starfish.spots import DecodeSpots, FindSpots\n",
"from starfish.types import TraceBuildingStrategies\n",
"\n",
"lmp = DetectSpots.LocalMaxPeakFinder(\n",
"\n",
"lmp = FindSpots.LocalMaxPeakFinder(\n",
" min_distance=6,\n",
" stringency=0,\n",
" min_obj_area=6,\n",
" max_obj_area=600,\n",
" is_volume=True\n",
")\n",
"spot_intensities = lmp.run(mp)"
"spots = lmp.run(mp)\n",
"\n",
"decoder = DecodeSpots.PerRoundMaxChannel(codebook=experiment.codebook,\n",
" trace_building_strategy=TraceBuildingStrategies.SEQUENTIAL)\n",
"decoded_intensities = decoder.run(spots=spots)"
]
},
{
Expand Down Expand Up @@ -240,11 +247,11 @@
"outputs": [],
"source": [
"benchmark_spot_count = len(benchmark_peaks)\n",
"starfish_spot_count = len(spot_intensities)\n",
"starfish_spot_count = len(decoded_intensities)\n",
"\n",
"plt.figure(figsize=(10,10))\n",
"plt.plot(benchmark_peaks.x, -benchmark_peaks.y, \"o\")\n",
"plt.plot(spot_intensities[Axes.X.value], -spot_intensities[Axes.Y.value], \"x\")\n",
"plt.plot(decoded_intensities[Axes.X.value], -decoded_intensities[Axes.Y.value], \"x\")\n",
"\n",
"plt.legend([\"Benchmark: {} spots\".format(benchmark_spot_count),\n",
" \"Starfish: {} spots\".format(starfish_spot_count)])\n",
Expand Down
17 changes: 12 additions & 5 deletions notebooks/py/osmFISH.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,22 @@
# EPY: END markdown

# EPY: START code
from starfish.spots import DetectSpots
from starfish.spots import DecodeSpots, FindSpots
from starfish.types import TraceBuildingStrategies

lmp = DetectSpots.LocalMaxPeakFinder(

lmp = FindSpots.LocalMaxPeakFinder(
min_distance=6,
stringency=0,
min_obj_area=6,
max_obj_area=600,
is_volume=True
)
spot_intensities = lmp.run(mp)
spots = lmp.run(mp)

decoder = DecodeSpots.PerRoundMaxChannel(codebook=experiment.codebook,
trace_building_strategy=TraceBuildingStrategies.SEQUENTIAL)
decoded_intensities = decoder.run(spots=spots)
# EPY: END code

# EPY: START markdown
Expand Down Expand Up @@ -162,11 +169,11 @@ def get_benchmark_peaks(loaded_results, redo_flag=False):

# EPY: START code
benchmark_spot_count = len(benchmark_peaks)
starfish_spot_count = len(spot_intensities)
starfish_spot_count = len(decoded_intensities)

plt.figure(figsize=(10,10))
plt.plot(benchmark_peaks.x, -benchmark_peaks.y, "o")
plt.plot(spot_intensities[Axes.X.value], -spot_intensities[Axes.Y.value], "x")
plt.plot(decoded_intensities[Axes.X.value], -decoded_intensities[Axes.Y.value], "x")

plt.legend(["Benchmark: {} spots".format(benchmark_spot_count),
"Starfish: {} spots".format(starfish_spot_count)])
Expand Down
1 change: 1 addition & 0 deletions starfish/core/spots/DecodeSpots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ._base import DecodeSpotsAlgorithm
from .metric_decoder import MetricDistance
from .per_round_max_channel_decoder import PerRoundMaxChannel

# autodoc's automodule directive only captures the modules explicitly listed in __all__.
Expand Down
92 changes: 92 additions & 0 deletions starfish/core/spots/DecodeSpots/metric_decoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from starfish.core.codebook.codebook import Codebook
from starfish.core.intensity_table.decoded_intensity_table import DecodedIntensityTable
from starfish.core.intensity_table.intensity_table_coordinates import \
transfer_physical_coords_to_intensity_table
from starfish.core.spots.DecodeSpots.trace_builders import TRACE_BUILDERS
from starfish.core.types import Number, SpotFindingResults, TraceBuildingStrategies
from ._base import DecodeSpotsAlgorithm


class MetricDistance(DecodeSpotsAlgorithm):
"""
Normalizes both the magnitudes of the codes and the spot intensities, then decodes spots by
assigning each spot to the closest code, measured by the provided metric.

Codes greater than max_distance from the nearest code, or dimmer than min_intensity, are
discarded.

Parameters
----------
codebook : Codebook
codebook containing targets the experiment was designed to quantify
max_distance : Number
spots greater than this distance from their nearest target are not decoded
min_intensity : Number
spots dimmer than this intensity are not decoded
metric : str
the metric to use to measure distance. Can be any metric that satisfies the triangle
inequality that is implemented by :py:mod:`scipy.spatial.distance` (default "euclidean")
norm_order : int
the norm to use to normalize the magnitudes of spots and codes (default 2, L2 norm)
trace_building_strategy: TraceBuildingStrategies
Defines the strategy for building spot traces to decode across rounds and chs of spot
finding results.
anchor_round : Optional[int]
Only applicable if trace_building_strategy is TraceBuildingStrategies.NEAREST_NEIGHBORS.
The imaging round against which other rounds will be checked for spots in the same
approximate pixel location.
search_radius : Optional[int]
Only applicable if trace_building_strategy is TraceBuildingStrategies.NEAREST_NEIGHBORS.
Number of pixels over which to search for spots in other rounds and channels.
"""

def __init__(
self,
codebook: Codebook,
max_distance: Number,
min_intensity: Number,
norm_order: int = 2,
metric: str = "euclidean",
trace_building_strategy: TraceBuildingStrategies = TraceBuildingStrategies.EXACT_MATCH,
anchor_round: int = 1,
search_radius: int = 3,
) -> None:
self.codebook = codebook
self.max_distance = max_distance
self.min_intensity = min_intensity
self.norm_order = norm_order
self.metric = metric
self.trace_builder = TRACE_BUILDERS[trace_building_strategy]
self.anchor_round = anchor_round
self.search_radius = search_radius

def run(
self,
spots: SpotFindingResults,
*args
) -> DecodedIntensityTable:
"""Decode spots by selecting the max-valued channel in each sequencing round

Parameters
----------
spots : SpotFindingResults
A Dict of tile indices and their corresponding measured spots

Returns
-------
DecodedIntensityTable :
IntensityTable decoded and appended with Features.TARGET and Features.QUALITY values.

"""

intensities = self.trace_builder(spot_results=spots,
anchor_round=self.anchor_round,
search_radius=self.search_radius)
transfer_physical_coords_to_intensity_table(intensity_table=intensities, spots=spots)
return self.codebook.decode_metric(
intensities,
max_distance=self.max_distance,
min_intensity=self.min_intensity,
norm_order=self.norm_order,
metric=self.metric,
)
1 change: 1 addition & 0 deletions starfish/core/spots/FindSpots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ._base import FindSpotsAlgorithm
from .blob import BlobDetector
from .local_max_peak_finder import LocalMaxPeakFinder
from .trackpy_local_max_peak_finder import TrackpyLocalMaxPeakFinder

# autodoc's automodule directive only captures the modules explicitly listed in __all__.
Expand Down
Loading