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

Machine learning segmentation #62

Open
wants to merge 426 commits into
base: v0.1.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
426 commits
Select commit Hold shift + click to select a range
4cf60db
moving utility functions from segment.py to watershed.py
leonardosepulveda Feb 11, 2020
dafa7b1
removing utility functions from segment.py
leonardosepulveda Feb 11, 2020
3e1bddd
add comments to the header of multiple methods.
leonardosepulveda Feb 11, 2020
5352938
remove commented methods
leonardosepulveda Feb 11, 2020
8333d39
pep8 compliance
leonardosepulveda Feb 11, 2020
6e0e6f9
pep8 compliance
leonardosepulveda Feb 11, 2020
bbcf37d
pep8 compliance
leonardosepulveda Feb 11, 2020
5bef0ce
pep8 compliance
leonardosepulveda Feb 11, 2020
0b584ff
add george's modifications to dataportal
leonardosepulveda Feb 12, 2020
3a60dc8
adding additional modifications to aws
leonardosepulveda Feb 15, 2020
ba4935e
fixing indentation
leonardosepulveda Feb 18, 2020
b9e01eb
pep8 compliance
leonardosepulveda Feb 18, 2020
2cd12e3
added printing for debuging
leonardosepulveda Mar 2, 2020
e2c466e
added printing for debuging
leonardosepulveda Mar 2, 2020
eef6d92
added printing for debuging
leonardosepulveda Mar 2, 2020
ce8f8c1
removing self calls
leonardosepulveda Mar 2, 2020
70b6591
remove self calls
leonardosepulveda Mar 3, 2020
f134a3e
pep8 compliance
leonardosepulveda Mar 3, 2020
493e72d
change WatershedSegmentNucleiCV2 to WatershedSegmentCV2, modify acco…
leonardosepulveda Apr 23, 2020
1a8d8e5
change nuclei to compartment
leonardosepulveda Apr 23, 2020
39b5f06
change nuclei to compartment
leonardosepulveda Apr 23, 2020
7e94d77
added framework for MachineLearningSegment class
leonardosepulveda Apr 23, 2020
bdcaa7a
adding mls utils function
leonardosepulveda Apr 23, 2020
23f6cb4
add function definitions to mls.py
leonardosepulveda Apr 23, 2020
a0b75eb
add body of apply_machine_learning_segmentation function
leonardosepulveda Apr 24, 2020
8f12840
change nuclei to compartment in cv2 segmentation functions
leonardosepulveda Apr 25, 2020
4fac641
adding edge memebrane calculation to CV2 segmenetation
leonardosepulveda Apr 25, 2020
034b4ec
modify get_membrane_mask function
leonardosepulveda Apr 26, 2020
de95313
change watershed.py to segmentation.py, put contents of mls.py into …
leonardosepulveda Apr 26, 2020
e1999af
consolidate functions into segment_using_cellpose
leonardosepulveda May 6, 2020
9e2689f
put segmentation params into dict
leonardosepulveda Jun 18, 2020
c887887
add parameters as dict
leonardosepulveda Jun 19, 2020
5a46da5
change indentation
leonardosepulveda Jun 19, 2020
432188e
return watershed class name to previous state
leonardosepulveda Jun 19, 2020
7bd766f
add 2D>3D segmentation mask
leonardosepulveda Jun 19, 2020
c4675cb
changed segmentation output variable name
leonardosepulveda Jun 19, 2020
8c34233
changed value of the background mask from 100 to 0
leonardosepulveda Jun 24, 2020
e87a903
modifying tuple output in 2d>3d
leonardosepulveda Jun 24, 2020
5f1b580
pep8 compliance
leonardosepulveda Jul 1, 2020
ac617b3
pep8 compliance
leonardosepulveda Jul 1, 2020
066eb6b
pep8 compliance
leonardosepulveda Jul 1, 2020
a215a20
pep8 compliance
leonardosepulveda Jul 1, 2020
8387370
added Machine Learning Segment and CV2 to tests
leonardosepulveda Jul 1, 2020
f48e67d
remove print statements for timing
leonardosepulveda Jul 1, 2020
fca3e8e
copied dataportal.py from master
leonardosepulveda Jul 2, 2020
8ab8b3c
added explicit tuple output
leonardosepulveda Jul 2, 2020
8cbc8ab
add cellpose to requirements
leonardosepulveda Jul 2, 2020
f57bb92
updated docutils and pillow requirements
leonardosepulveda Jul 2, 2020
0e7536e
removed docutils requirement to avoid conflict with v017
leonardosepulveda Jul 2, 2020
ebccbd2
added input variables to get_membrane_mask
leonardosepulveda Jul 2, 2020
21ef865
added additional variable
leonardosepulveda Jul 2, 2020
9015b84
merge conflict to req..txt, datap..py seg..py
leonardosepulveda Jul 6, 2020
c7d70ab
added pillow and cellpose requirements
leonardosepulveda Jul 6, 2020
a472f97
Adding _get_membrane_mask and _get_nuclei_mask functions to Watershed…
leonardosepulveda Jan 22, 2020
6cf5637
changed formatting to comply to pep8
leonardosepulveda Jan 23, 2020
bdac53c
add borderMask to _get_nuclei_mask function
leonardosepulveda Jan 23, 2020
e65f631
added method _generate_markers to WatershedSegmentNucleiCV2
leonardosepulveda Jan 23, 2020
a6b51b8
added method _apply_watershed
leonardosepulveda Jan 23, 2020
257b902
added method _convert_grayscale_to_rgb
leonardosepulveda Jan 23, 2020
c247209
modify _apply_watershed method
leonardosepulveda Jan 23, 2020
a228f56
adding _combine_watershed_z_positions method, starting by pseudocode
leonardosepulveda Jan 23, 2020
eb26684
expanding _combine_watershed_z_positions and adding _get_overlapping_…
leonardosepulveda Jan 24, 2020
363de11
pep8 compliance
leonardosepulveda Jan 24, 2020
09f1f1e
pep8 compliance
leonardosepulveda Jan 24, 2020
27cb38e
pep8 compliance
leonardosepulveda Jan 24, 2020
1d8082f
pep8 compliance
leonardosepulveda Jan 25, 2020
20c8622
pep8 compliance
leonardosepulveda Jan 25, 2020
d045c61
pep8 compliance
leonardosepulveda Jan 25, 2020
e17bf95
pep8 compliance
leonardosepulveda Jan 25, 2020
0a8d94c
pep8 compliance
leonardosepulveda Jan 25, 2020
977dbba
pep8 compliance
leonardosepulveda Jan 25, 2020
b32ce41
pep8 compliance
leonardosepulveda Jan 25, 2020
02ba35e
pep8 compliance
leonardosepulveda Jan 25, 2020
d45769f
pep8 compliance
leonardosepulveda Jan 25, 2020
4b5fa61
pep8 compliance
leonardosepulveda Jan 25, 2020
c3052e8
pep8 compliance
leonardosepulveda Jan 25, 2020
21cf45a
pep8 compliance
leonardosepulveda Jan 25, 2020
5d465b4
pep8 compliance
leonardosepulveda Jan 25, 2020
28e875a
pep8 compliance
leonardosepulveda Jan 25, 2020
4104ef2
pep8 compliance
leonardosepulveda Jan 25, 2020
4c89a95
pep8 compliance
leonardosepulveda Jan 25, 2020
0002003
pep8 compliance
leonardosepulveda Jan 25, 2020
d346f27
pep8 compliance
leonardosepulveda Jan 25, 2020
6ee9e19
pep8 compliance
leonardosepulveda Jan 25, 2020
fa5e4ba
fixing invalid syntax
leonardosepulveda Jan 25, 2020
279c1c7
fixing invalid syntax
leonardosepulveda Jan 25, 2020
e84188f
fixing invalid syntax
leonardosepulveda Jan 25, 2020
a1c322c
fixing invalid syntax
leonardosepulveda Jan 25, 2020
0961624
fixing invalid syntax
leonardosepulveda Jan 25, 2020
2744396
fixing invalid syntax
leonardosepulveda Jan 25, 2020
40b228a
pep8 compliance
leonardosepulveda Jan 25, 2020
c1156d6
pep8 compliance
leonardosepulveda Jan 25, 2020
9cd1000
pep8 compliance
leonardosepulveda Jan 25, 2020
5b963de
added _read_and_filter_image_stack to WatershedSegmentNucleiCV2
leonardosepulveda Jan 25, 2020
0f35412
reorganized Watershed...CV2 to remove repeated method calls
leonardosepulveda Jan 25, 2020
332bbe2
cleaned watershed
leonardosepulveda Jan 25, 2020
f971acb
pep8 compliance
leonardosepulveda Jan 25, 2020
9d4bfc1
pep8 compliance
leonardosepulveda Jan 25, 2020
66c4d26
pep8 compliance
leonardosepulveda Jan 25, 2020
d3e3e60
pep8 compliance
leonardosepulveda Jan 25, 2020
c9c6973
pep8 compliance
leonardosepulveda Jan 25, 2020
36a5982
fixing invalid syntax
leonardosepulveda Jan 25, 2020
c44a71a
correct skimage function names
leonardosepulveda Jan 25, 2020
f5166b5
pep8 compliance
leonardosepulveda Jan 25, 2020
6e07a30
correct skimage function names
leonardosepulveda Jan 25, 2020
8b0ed8a
changed Image dimension order to fit MERlin's
leonardosepulveda Jan 26, 2020
fda36a2
change variable name
leonardosepulveda Jan 26, 2020
0fd6943
added missing self. in method call
leonardosepulveda Jan 26, 2020
83e2476
adding print statements for debugging
leonardosepulveda Jan 26, 2020
437fa2f
moving utility functions from segment.py to watershed.py
leonardosepulveda Feb 11, 2020
c663c25
removing utility functions from segment.py
leonardosepulveda Feb 11, 2020
b22bb6f
add comments to the header of multiple methods.
leonardosepulveda Feb 11, 2020
dad7592
remove commented methods
leonardosepulveda Feb 11, 2020
2cf338c
pep8 compliance
leonardosepulveda Feb 11, 2020
18dec1a
pep8 compliance
leonardosepulveda Feb 11, 2020
7a4d792
pep8 compliance
leonardosepulveda Feb 11, 2020
0b4b31c
pep8 compliance
leonardosepulveda Feb 11, 2020
de616ab
add george's modifications to dataportal
leonardosepulveda Feb 12, 2020
a41a46a
adding additional modifications to aws
leonardosepulveda Feb 15, 2020
5f6960f
added printing for debuging
leonardosepulveda Mar 2, 2020
6503d74
added printing for debuging
leonardosepulveda Mar 2, 2020
77c2ae8
added printing for debuging
leonardosepulveda Mar 2, 2020
c595a6a
removing self calls
leonardosepulveda Mar 2, 2020
ce0739a
remove self calls
leonardosepulveda Mar 3, 2020
11202c4
pep8 compliance
leonardosepulveda Mar 3, 2020
b1fe54c
change WatershedSegmentNucleiCV2 to WatershedSegmentCV2, modify acco…
leonardosepulveda Apr 23, 2020
3085ce1
change nuclei to compartment
leonardosepulveda Apr 23, 2020
825af84
change nuclei to compartment
leonardosepulveda Apr 23, 2020
d4385ef
added framework for MachineLearningSegment class
leonardosepulveda Apr 23, 2020
760df79
adding mls utils function
leonardosepulveda Apr 23, 2020
ce01117
add function definitions to mls.py
leonardosepulveda Apr 23, 2020
e8d1e2c
add body of apply_machine_learning_segmentation function
leonardosepulveda Apr 24, 2020
aa1d3d3
change nuclei to compartment in cv2 segmentation functions
leonardosepulveda Apr 25, 2020
427e746
adding edge memebrane calculation to CV2 segmenetation
leonardosepulveda Apr 25, 2020
1af4881
modify get_membrane_mask function
leonardosepulveda Apr 26, 2020
7c0cb8a
change watershed.py to segmentation.py, put contents of mls.py into …
leonardosepulveda Apr 26, 2020
80270d6
consolidate functions into segment_using_cellpose
leonardosepulveda May 6, 2020
7db8f4e
add diameter param to segment_using_cellpose
leonardosepulveda Jun 18, 2020
92d04e9
add diameter param to segment_using_cellpose
leonardosepulveda Jun 18, 2020
e33da73
put segmentation params into dict
leonardosepulveda Jun 18, 2020
b81c877
add parameters as dict
leonardosepulveda Jun 19, 2020
479d9db
change indentation
leonardosepulveda Jun 19, 2020
a87d601
return watershed class name to previous state
leonardosepulveda Jun 19, 2020
96d59bc
add 2D>3D segmentation mask
leonardosepulveda Jun 19, 2020
08ce7fc
changed segmentation output variable name
leonardosepulveda Jun 19, 2020
ec5b51f
changed value of the background mask from 100 to 0
leonardosepulveda Jun 24, 2020
7ad7836
mofified botched indentation in combine 2d>3d
leonardosepulveda Jun 24, 2020
5c3bc44
modifying tuple output in 2d>3d
leonardosepulveda Jun 24, 2020
eed7cc7
modifying tuple output in 2d>3d
leonardosepulveda Jun 24, 2020
cbb8aae
made tuple explicit
leonardosepulveda Jun 24, 2020
4ae5515
pep8 compliance
leonardosepulveda Jul 1, 2020
64ed96d
pep8 compliance
leonardosepulveda Jul 1, 2020
a655cf7
pep8 compliance
leonardosepulveda Jul 1, 2020
dc7e3bc
pep8 compliance
leonardosepulveda Jul 1, 2020
1414aba
added Machine Learning Segment and CV2 to tests
leonardosepulveda Jul 1, 2020
0d23441
remove print statements for timing
leonardosepulveda Jul 1, 2020
322c009
removed explicit tuple output from function
leonardosepulveda Jul 2, 2020
bbedf08
added explicit tuple output
leonardosepulveda Jul 2, 2020
dfee36d
add cellpose to requirements
leonardosepulveda Jul 2, 2020
bc81275
updated docutils and pillow requirements
leonardosepulveda Jul 2, 2020
b32ff15
changed marker bg in ge_cv2_markers
leonardosepulveda Jul 2, 2020
d34b73b
added input variables to get_membrane_mask
leonardosepulveda Jul 2, 2020
2987aaa
added additional variable
leonardosepulveda Jul 2, 2020
c926ae8
updated variable name
leonardosepulveda Jul 2, 2020
0ce75ca
added method _generate_markers to WatershedSegmentNucleiCV2
leonardosepulveda Jan 23, 2020
bdd35e6
pep8 compliance
leonardosepulveda Jan 25, 2020
3fcf517
pep8 compliance
leonardosepulveda Jan 25, 2020
5612c6c
pep8 compliance
leonardosepulveda Jan 25, 2020
04bca44
pep8 compliance
leonardosepulveda Jan 25, 2020
85b408e
pep8 compliance
leonardosepulveda Jan 25, 2020
ba00486
pep8 compliance
leonardosepulveda Jan 25, 2020
70cc46a
pep8 compliance
leonardosepulveda Jan 25, 2020
1be3c3d
pep8 compliance
leonardosepulveda Jan 25, 2020
6f5ab0c
cleaned watershed
leonardosepulveda Jan 25, 2020
5fd0ab0
pep8 compliance
leonardosepulveda Jan 25, 2020
70b0ee4
fixing invalid syntax
leonardosepulveda Jan 25, 2020
983f803
correct skimage function names
leonardosepulveda Jan 25, 2020
c2b1340
changed Image dimension order to fit MERlin's
leonardosepulveda Jan 26, 2020
083db37
adding print statements for debugging
leonardosepulveda Jan 26, 2020
54424ad
moving utility functions from segment.py to watershed.py
leonardosepulveda Feb 11, 2020
7c5b0fc
removing utility functions from segment.py
leonardosepulveda Feb 11, 2020
5bccb0e
add comments to the header of multiple methods.
leonardosepulveda Feb 11, 2020
373258c
pep8 compliance
leonardosepulveda Feb 11, 2020
a339af6
pep8 compliance
leonardosepulveda Feb 11, 2020
63d34bf
add george's modifications to dataportal
leonardosepulveda Feb 12, 2020
594cc10
pep8 compliance
leonardosepulveda Feb 18, 2020
d65d4be
added printing for debuging
leonardosepulveda Mar 2, 2020
3afd0c8
added printing for debuging
leonardosepulveda Mar 2, 2020
fa1dc3d
added printing for debuging
leonardosepulveda Mar 2, 2020
8fdc54b
removing self calls
leonardosepulveda Mar 2, 2020
4214761
remove self calls
leonardosepulveda Mar 3, 2020
6eabe1d
pep8 compliance
leonardosepulveda Mar 3, 2020
26edb2d
change WatershedSegmentNucleiCV2 to WatershedSegmentCV2, modify acco…
leonardosepulveda Apr 23, 2020
f7beb3a
change nuclei to compartment
leonardosepulveda Apr 23, 2020
caa3257
change nuclei to compartment
leonardosepulveda Apr 23, 2020
e1118bd
added framework for MachineLearningSegment class
leonardosepulveda Apr 23, 2020
331b773
adding mls utils function
leonardosepulveda Apr 23, 2020
05239a0
add function definitions to mls.py
leonardosepulveda Apr 23, 2020
42f9c09
add body of apply_machine_learning_segmentation function
leonardosepulveda Apr 24, 2020
c138a44
change nuclei to compartment in cv2 segmentation functions
leonardosepulveda Apr 25, 2020
bb82925
adding edge memebrane calculation to CV2 segmenetation
leonardosepulveda Apr 25, 2020
2ca4eba
change watershed.py to segmentation.py, put contents of mls.py into …
leonardosepulveda Apr 26, 2020
e16ac11
consolidate functions into segment_using_cellpose
leonardosepulveda May 6, 2020
ddd1912
put segmentation params into dict
leonardosepulveda Jun 18, 2020
b24ebf8
add parameters as dict
leonardosepulveda Jun 19, 2020
0be81d5
change indentation
leonardosepulveda Jun 19, 2020
142accc
return watershed class name to previous state
leonardosepulveda Jun 19, 2020
96e9c9c
add 2D>3D segmentation mask
leonardosepulveda Jun 19, 2020
d5db3cf
changed segmentation output variable name
leonardosepulveda Jun 19, 2020
75ff563
changed value of the background mask from 100 to 0
leonardosepulveda Jun 24, 2020
17d0158
modifying tuple output in 2d>3d
leonardosepulveda Jun 24, 2020
d1f19f4
pep8 compliance
leonardosepulveda Jul 1, 2020
a1d5070
pep8 compliance
leonardosepulveda Jul 1, 2020
94f91b3
added Machine Learning Segment and CV2 to tests
leonardosepulveda Jul 1, 2020
01f0e3a
remove print statements for timing
leonardosepulveda Jul 1, 2020
487fb44
copied dataportal.py from master
leonardosepulveda Jul 2, 2020
82f7fa2
added explicit tuple output
leonardosepulveda Jul 2, 2020
18584fd
add cellpose to requirements
leonardosepulveda Jul 2, 2020
1d0a7b0
updated docutils and pillow requirements
leonardosepulveda Jul 2, 2020
6ea18aa
removed docutils requirement to avoid conflict with v017
leonardosepulveda Jul 2, 2020
3ef602b
added input variables to get_membrane_mask
leonardosepulveda Jul 2, 2020
1b07f35
added pillow and cellpose requirements
leonardosepulveda Jul 6, 2020
3b1ae65
solve merge conflict
leonardosepulveda Jul 7, 2020
af6b6eb
Merge branch 'v0.1.7' into machine_learning_segmentation
leonardosepulveda Jul 7, 2020
f40d2fa
remove redundant functions from watershed.py
leonardosepulveda Jul 7, 2020
3b93e4c
solve merge conflict
leonardosepulveda Jul 7, 2020
e38eb7c
remove redundant tasks
leonardosepulveda Jul 7, 2020
9a59d35
add snakemakeParameters
leonardosepulveda Jul 7, 2020
673edaa
increase latency_wait
leonardosepulveda Jul 7, 2020
8c663b8
remove MLS and CV2 from merfish test
leonardosepulveda Jul 7, 2020
323e880
add test for new segmentation routines
leonardosepulveda Jul 7, 2020
a1055c0
add segmentation jsons to conftest
leonardosepulveda Jul 8, 2020
ef8ed16
added unique task analysis_name for cellpose and cv2 tests
leonardosepulveda Jul 8, 2020
20c659a
hard-coded to variable array size in convert_grayscale_to_rgb
leonardosepulveda Jul 8, 2020
0bbf2c7
added print statement for debogging
leonardosepulveda Jul 8, 2020
2979196
change test function names; added fiducial warp and globalign for seg…
leonardosepulveda Jul 8, 2020
fff22c0
add missing commas to json file
leonardosepulveda Jul 8, 2020
959a104
decrease snakemake latency wait
leonardosepulveda Jul 8, 2020
de2313d
comment snakemake params
leonardosepulveda Jul 9, 2020
06e9149
fix comment indentation
leonardosepulveda Jul 9, 2020
039eaf6
change json files for test segmentation
leonardosepulveda Jul 9, 2020
1940353
update conftest with new json names
leonardosepulveda Jul 9, 2020
1e90523
corrected variable typo
leonardosepulveda Jul 9, 2020
caf0518
change test name
leonardosepulveda Jul 9, 2020
b9f7088
simplify test json
leonardosepulveda Jul 9, 2020
5bbd4f6
change json name in test py file
leonardosepulveda Jul 9, 2020
9f2e68a
simplify json to increase codecov
leonardosepulveda Jul 9, 2020
0ac2e2e
100>1 in the cv2 segmentation
leonardosepulveda Aug 3, 2020
1b35306
add flow_threshold and cellpob_threshold parameters
leonardosepulveda Jan 8, 2021
8d112f6
adding flow_thresh and cellprob to machineLearningSegment class
leonardosepulveda Jan 8, 2021
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
216 changes: 208 additions & 8 deletions merlin/analysis/segment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import cv2
import numpy as np
from skimage import measure
from skimage import segmentation
from skimage import segmentation as skiseg
from skimage import morphology
from skimage import feature
from skimage import filters
import rtree
from shapely import geometry
from typing import List, Dict
Expand All @@ -10,10 +13,10 @@
from merlin.core import dataset
from merlin.core import analysistask
from merlin.util import spatialfeature
from merlin.util import watershed
from merlin.util import segmentation
import pandas
import networkx as nx

import time

class FeatureSavingAnalysisTask(analysistask.ParallelAnalysisTask):

Expand Down Expand Up @@ -90,13 +93,13 @@ def _run_analysis(self, fragmentIndex):
.get_data_channel_index(self.parameters['watershed_channel_name'])
watershedImages = self._read_and_filter_image_stack(fragmentIndex,
watershedIndex, 5)
seeds = watershed.separate_merged_seeds(
watershed.extract_seeds(seedImages))
normalizedWatershed, watershedMask = watershed.prepare_watershed_images(
watershedImages)
seeds = segmentation.separate_merged_seeds(
segmentation.extract_seeds(seedImages))
normalizedWatershed, watershedMask = segmentation\
.prepare_watershed_images(watershedImages)

seeds[np.invert(watershedMask)] = 0
watershedOutput = segmentation.watershed(
watershedOutput = skiseg.watershed(
normalizedWatershed, measure.label(seeds), mask=watershedMask,
connectivity=np.ones((3, 3, 3)), watershed_line=True)

Expand All @@ -120,6 +123,203 @@ def _read_and_filter_image_stack(self, fov: int, channelIndex: int,
for z in range(len(self.dataSet.get_z_positions()))])


class WatershedSegmentNucleiCV2(FeatureSavingAnalysisTask):

"""
An analysis task that determines the boundaries of features in the
image data in each field of view using a watershed algorithm
implemented in CV2.

A tutorial explaining the general scheme of the method can be
found in https://opencv-python-tutroals.readthedocs.io/en/latest/
py_tutorials/py_imgproc/py_watershed/py_watershed.html.

The watershed segmentation is performed in each z-position
independently and combined into 3D objects in a later step

The class can be used to segment either nuclear or cytoplasmic
compartments. If both the compartment and membrane channels are the
same, the membrane channel is calculated from the edge transform of
the provided channel.

Since each field of view is analyzed individually, the segmentation
results should be cleaned in order to merge cells that cross the
field of view boundary.
"""

def __init__(self, dataSet, parameters=None, analysisName=None):
super().__init__(dataSet, parameters, analysisName)

if 'membrane_channel_name' not in self.parameters:
self.parameters['membrane_channel_name'] = 'DAPI'
if 'compartment_channel_name' not in self.parameters:
self.parameters['compartment_channel_name'] = 'DAPI'

def fragment_count(self):
return len(self.dataSet.get_fovs())

def get_estimated_memory(self):
# TODO - refine estimate
return 2048

def get_estimated_time(self):
# TODO - refine estimate
return 5

def get_dependencies(self):
return [self.parameters['warp_task'],
self.parameters['global_align_task']]

def get_cell_boundaries(self) -> List[spatialfeature.SpatialFeature]:
featureDB = self.get_feature_database()
return featureDB.read_features()

def _run_analysis(self, fragmentIndex):
startTime = time.time()

globalTask = self.dataSet.load_analysis_task(
self.parameters['global_align_task'])

# read membrane and compartment indexes
membraneIndex = self.dataSet \
.get_data_organization() \
.get_data_channel_index(
self.parameters['membrane_channel_name'])
compartmentIndex = self.dataSet \
.get_data_organization() \
.get_data_channel_index(
self.parameters['compartment_channel_name'])

# read membrane and compartment images
membraneImages = self._read_image_stack(fragmentIndex, membraneIndex)
compartmentImages = self._read_image_stack(fragmentIndex,
compartmentIndex)

# Prepare masks for cv2 watershed
watershedMarkers = segmentation.get_cv2_watershed_markers(
compartmentImages,
membraneImages,
self.parameters['compartment_channel_name'],
self.parameters['membrane_channel_name'])

# perform watershed in individual z positions
watershedOutput = segmentation.apply_cv2_watershed(compartmentImages,
watershedMarkers)

# combine all z positions in watershed
watershedCombinedOutput = segmentation \
.combine_2d_segmentation_masks_into_3d(watershedOutput)

# get features from mask. This is the slowestart (6 min for the
# previous part, 15+ for the rest, for a 7 frame Image.
zPos = np.array(self.dataSet.get_data_organization().get_z_positions())
featureList = [spatialfeature.SpatialFeature.feature_from_label_matrix(
(watershedCombinedOutput == i), fragmentIndex,
globalTask.fov_to_global_transform(fragmentIndex), zPos)
for i in np.unique(watershedCombinedOutput) if i != 0]

featureDB = self.get_feature_database()
featureDB.write_features(featureList, fragmentIndex)

def _read_image_stack(self, fov: int, channelIndex: int) -> np.ndarray:
warpTask = self.dataSet.load_analysis_task(
self.parameters['warp_task'])
return np.array([warpTask.get_aligned_image(fov, channelIndex, z)
for z in range(len(self.dataSet.get_z_positions()))])


class MachineLearningSegment(FeatureSavingAnalysisTask):
"""
An analysis task that determines the boundaries of features in the
image data in each field of view using a the specified machine learning
method. The available method is cellpose (https://github.com/MouseLand/
cellpose).

TODO: implement unets / Ilastik
"""

def __init__(self, dataSet, parameters=None, analysisName=None):
super().__init__(dataSet, parameters, analysisName)

if 'method' not in self.parameters:
self.parameters['method'] = 'cellpose'
if 'diameter' not in self.parameters:
self.parameters['diameter'] = 50
if 'compartment_channel_name' not in self.parameters:
self.parameters['compartment_channel_name'] = 'DAPI'
if 'flow_threshold' not in self.parameters:
self.parameters['flow_threshold'] = 0.5
if 'cellprob_threshold' not in self.parameters:
self.parameters['cellprob_threshold'] = 1

def fragment_count(self):
return len(self.dataSet.get_fovs())

def get_estimated_memory(self):
# TODO - refine estimate
return 2048

def get_estimated_time(self):
# TODO - refine estimate
return 5

def get_dependencies(self):
return [self.parameters['warp_task'],
self.parameters['global_align_task']]

def get_cell_boundaries(self) -> List[spatialfeature.SpatialFeature]:
featureDB = self.get_feature_database()
return featureDB.read_features()

def _run_analysis(self, fragmentIndex):

globalTask = self.dataSet.load_analysis_task(
self.parameters['global_align_task'])

# read membrane and compartment indexes
compartmentIndex = self.dataSet \
.get_data_organization() \
.get_data_channel_index(
self.parameters['compartment_channel_name'])

# Read images and perform segmentation
compartmentImages = self._read_image_stack(fragmentIndex,
compartmentIndex)

if self.parameters['method'] == 'cellpose':
segParameters = dict({
'method': 'cellpose',
'diameter': self.parameters['diameter'],
'channel': self.parameters['compartment_channel_name'],
'flow_threshold': self.parameters['flow_threshold'],
'cellprob_threshold': self.parameters['cellprob_threshold']
})

segmentationOutput = segmentation.apply_machine_learning_segmentation(
compartmentImages, segParameters)

# combine all z positions in watershed
watershedCombinedOutput = segmentation \
.combine_2d_segmentation_masks_into_3d(segmentationOutput)

# get features from mask. This is the slowestart (6 min for the
# previous part, 15+ for the rest, for a 7 frame Image.
zPos = np.array(self.dataSet.get_data_organization().get_z_positions())
featureList = [spatialfeature.SpatialFeature.feature_from_label_matrix(
(watershedCombinedOutput == i), fragmentIndex,
globalTask.fov_to_global_transform(fragmentIndex), zPos)
for i in np.unique(watershedCombinedOutput) if i != 0]

featureDB = self.get_feature_database()
featureDB.write_features(featureList, fragmentIndex)

def _read_image_stack(self, fov: int, channelIndex: int) -> np.ndarray:
warpTask = self.dataSet.load_analysis_task(
self.parameters['warp_task'])
return np.array([warpTask.get_aligned_image(fov, channelIndex, z)
for z in range(len(self.dataSet.get_z_positions()))])


class CleanCellBoundaries(analysistask.ParallelAnalysisTask):
'''
A task to construct a network graph where each cell is a node, and overlaps
Expand Down
2 changes: 1 addition & 1 deletion merlin/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ def load_analysis_task(self, analysisTaskName: str) \
-> analysistask.AnalysisTask:
loadName = os.sep.join([self.get_task_subdirectory(
analysisTaskName), 'task.json'])

print(loadName)
with open(loadName, 'r') as inFile:
parameters = json.load(inFile)
analysisModule = importlib.import_module(parameters['module'])
Expand Down
6 changes: 6 additions & 0 deletions merlin/merlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ def run_with_snakemake(
dataSet: dataset.MERFISHDataSet, snakefilePath: str, coreCount: int,
snakemakeParameters: Dict = {}, report: bool = True):
print('Running MERlin pipeline through snakemake')
'''
if 'restart_times' not in snakemakeParameters:
snakemakeParameters['restart_times'] = 3
if 'latency_wait' not in snakemakeParameters:
snakemakeParameters['latency_wait'] = 60
'''
snakemake.snakemake(snakefilePath, cores=coreCount,
workdir=dataSet.get_snakemake_path(),
stats=snakefilePath + '.stats', lock=False,
Expand Down
1 change: 0 additions & 1 deletion merlin/util/dataportal.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,5 @@ def read_file_bytes(self, startByte, endByte):
endByte=endByte-1)
return file


def close(self) -> None:
pass
Loading