Skip to content

Commit

Permalink
Code review comments part 1 (no directory structure fix yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed Apr 14, 2021
1 parent 9c70276 commit a493bcf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 33 deletions.
18 changes: 12 additions & 6 deletions ark/phenotyping/som_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def compute_cluster_avg(fovs, channels, base_dir, cluster_col,
Name of the column to group by
cluster_dir (str):
Name of the file containing the pixel data with cluster labels
Returns:
pandas.DataFrame:
Contains the average channel values for each SOM cluster
"""

# define the cluster averages DataFrame
Expand Down Expand Up @@ -64,7 +68,7 @@ def compute_cluster_avg(fovs, channels, base_dir, cluster_col,


def create_fov_pixel_data(fov, channels, img_data, seg_labels,
blur_factor=2, subset_proportion=0.1, seed=None):
blur_factor=2, subset_proportion=0.1, seed=42):
"""Preprocess pixel data for one fov
Saves preprocessed data to pre_dir and subsetted data to sub_dir
Expand All @@ -87,10 +91,10 @@ def create_fov_pixel_data(fov, channels, img_data, seg_labels,
Returns:
tuple:
A tuple containing two pd.Dataframes:
Contains the following:
- The full preprocessed pixel dataset for a fov
- The subsetted pixel dataset for a fov
- pandas.DataFrame: Gaussian blurred and channel sum normalized pixel data for a fov
- pandas.DataFrame: subset of the preprocessed pixel dataset for a fov
"""

# for each marker, compute the Gaussian blur
Expand Down Expand Up @@ -129,10 +133,10 @@ def create_fov_pixel_data(fov, channels, img_data, seg_labels,
def create_pixel_matrix(fovs, base_dir, tiff_dir, seg_dir,
pre_dir='pixel_mat_preprocessed',
sub_dir='pixel_mat_subsetted', is_mibitiff=False,
blur_factor=2, subset_proportion=0.1, seed=None):
blur_factor=2, subset_proportion=0.1, seed=42):
"""For each fov, add a Gaussian blur to each channel and normalize channel sums for each pixel
Saves preprocessed data to pre_dir and subsetted data to sub_dir
Saves data to pre_dir and subsetted data to sub_dir
Args:
fovs (list):
Expand Down Expand Up @@ -366,6 +370,8 @@ def consensus_cluster(fovs, channels, base_dir, max_k=20, cap=3,
consensus_dir='pixel_mat_consensus', seed=42):
"""Run consensus clustering algorithm on summed data across channels
Saves data with consensus cluster labels to consensus_dir
Args:
fovs (list):
The list of fovs to subset on
Expand Down
2 changes: 1 addition & 1 deletion ark/utils/notebooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_segment_image_data_folder(tb):
def test_flowsom_cluster_folder(tb):
with tdir() as base_dir:
# create input files
notebooks_test_utils.flowsom_setup(tb, flowsom_dir=base_dir, seed=42)
notebooks_test_utils.flowsom_setup(tb, flowsom_dir=base_dir)

# run the FlowSOM preprocessing and clustering
notebooks_test_utils.flowsom_run(tb, fovs=['fov0', 'fov1'], channels=['chan0', 'chan1'])
10 changes: 1 addition & 9 deletions ark/utils/notebooks_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def segment_notebook_setup(tb, deepcell_tiff_dir, deepcell_input_dir, deepcell_o


def flowsom_setup(tb, flowsom_dir, img_shape=(50, 50), num_fovs=3, num_chans=3,
seed=None, is_mibitiff=False, mibitiff_suffix="-MassCorrected-Filtered",
is_mibitiff=False, mibitiff_suffix="-MassCorrected-Filtered",
dtype=np.uint16):
"""Creates the directories, data, and MIBItiff settings for testing FlowSOM clustering
Expand All @@ -95,8 +95,6 @@ def flowsom_setup(tb, flowsom_dir, img_shape=(50, 50), num_fovs=3, num_chans=3,
The number of test fovs to generate
num_chans (int):
The number of test channels to generate
seed (int):
The random seed to set
is_mibitiff (bool):
Whether we're working with mibitiff files or not
mibitiff_suffix (str):
Expand Down Expand Up @@ -151,12 +149,6 @@ def flowsom_setup(tb, flowsom_dir, img_shape=(50, 50), num_fovs=3, num_chans=3,
""" % (flowsom_dir, tiff_dir, seg_dir, dt.now().strftime("%Y-%m-%dT%H:%M:%S"))
tb.inject(define_paths, after='file_path')

# set a random seed
if seed is not None:
tb.inject("seed = %d" % seed, after='set_seed')
else:
tb.execute_cell('set_seed')

# will set MIBItiff and MIBItiff_suffix
tb.execute_cell('mibitiff_set')
if is_mibitiff:
Expand Down
20 changes: 3 additions & 17 deletions templates/example_flowsom_clustering.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,6 @@
"consensus_clustered_dir = os.path.join(som_dir, 'pixel_mat_consensus')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"set_seed"
]
},
"outputs": [],
"source": [
"# set a random seed if desired\n",
"seed = None"
]
},
{
"cell_type": "code",
"execution_count": 3,
Expand Down Expand Up @@ -131,7 +117,7 @@
"source": [
"# run FlowSOM preprocessing\n",
"som_utils.create_pixel_matrix(\n",
" fovs, base_dir, tiff_dir, segmentation_dir, pre_dir=preprocessed_dir, sub_dir=subsetted_dir, seed=seed\n",
" fovs, base_dir, tiff_dir, segmentation_dir, pre_dir=preprocessed_dir, sub_dir=subsetted_dir,\n",
")"
]
},
Expand Down Expand Up @@ -169,7 +155,7 @@
"outputs": [],
"source": [
"# run the SOM clustering\n",
"som_utils.train_som(fovs, channels, base_dir, sub_dir=subsetted_dir, seed=seed)"
"som_utils.train_som(fovs, channels, base_dir, sub_dir=subsetted_dir)"
]
},
{
Expand Down Expand Up @@ -235,7 +221,7 @@
"source": [
"# run hierarchical clustering based on SOM cluster assignments\n",
"som_utils.consensus_cluster(\n",
" fovs, channels, base_dir, cluster_dir=som_clustered_dir, consensus_dir=consensus_clustered_dir, seed=seed\n",
" fovs, channels, base_dir, cluster_dir=som_clustered_dir, consensus_dir=consensus_clustered_dir\n",
")"
]
},
Expand Down

0 comments on commit a493bcf

Please sign in to comment.