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

Add back channel normalization before preprocessing #613

Merged
merged 4 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 11 additions & 3 deletions ark/phenotyping/som_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ def create_fov_pixel_data(fov, channels, img_data, seg_labels, pixel_norm_val,

def preprocess_fov(base_dir, tiff_dir, data_dir, subset_dir, seg_dir, seg_suffix,
img_sub_folder, is_mibitiff, channels, blur_factor,
subset_proportion, pixel_norm_val, dtype, seed, fov):
subset_proportion, pixel_norm_val, dtype, seed, channel_norm_df, fov):
"""Helper function to read in the FOV-level pixel data, run `create_fov_pixel_data`,
and save the preprocessed data.

Expand Down Expand Up @@ -784,6 +784,8 @@ def preprocess_fov(base_dir, tiff_dir, data_dir, subset_dir, seg_dir, seg_suffix
The type to load the image segmentation labels in
seed (int):
The random seed to set for subsetting
channel_norm_df (pandas.DataFrame):
The channel normalization values to use
fov (str):
The name of the FOV to preprocess

Expand Down Expand Up @@ -819,6 +821,13 @@ def preprocess_fov(base_dir, tiff_dir, data_dir, subset_dir, seg_dir, seg_suffix
# subset for the channel data
img_data = img_xr.loc[fov, :, :, channels].values.astype(np.float32)

# create vector for normalizing image data
norm_vect = channel_norm_df['norm_val'].values
norm_vect = np.array(norm_vect).reshape([1, 1, len(norm_vect)])

# normalize image data
img_data = img_data / norm_vect

# set seed for subsetting
np.random.seed(seed)

Expand Down Expand Up @@ -928,7 +937,6 @@ def create_pixel_matrix(fovs, channels, base_dir, tiff_dir, seg_dir,
channel_norm_path = os.path.join(base_dir, 'channel_norm.feather')

if not os.path.exists(channel_norm_path):

# compute channel percentiles
channel_norm_df = calculate_channel_percentiles(tiff_dir=tiff_dir, fovs=fovs,
channels=channels,
Expand Down Expand Up @@ -961,7 +969,7 @@ def create_pixel_matrix(fovs, channels, base_dir, tiff_dir, seg_dir,
fov_data_func = partial(
preprocess_fov, base_dir, tiff_dir, data_dir, subset_dir,
seg_dir, seg_suffix, img_sub_folder, is_mibitiff, channels, blur_factor,
subset_proportion, pixel_norm_val, dtype, seed
subset_proportion, pixel_norm_val, dtype, seed, channel_norm_df
)

# define the multiprocessing context
Expand Down
8 changes: 7 additions & 1 deletion ark/phenotyping/som_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,12 +1124,18 @@ def test_preprocess_fov():
io.imsave(os.path.join(seg_dir, file_name), rand_img,
check_contrast=False)

# generate sample channel normalization values
channel_norm_df = pd.DataFrame.from_dict({
'channel': chans,
'norm_val': np.repeat(10, repeats=len(chans))
})

# run the preprocessing for fov0
# NOTE: don't test the return value, leave that for test_create_pixel_matrix
som_utils.preprocess_fov(
temp_dir, tiff_dir, 'pixel_mat_data', 'pixel_mat_subsetted',
seg_dir, '_feature_0.tif', 'TIFs', False, ['chan0', 'chan1', 'chan2'],
2, 0.1, 1, 'int16', 42, 'fov0'
2, 0.1, 1, 'int16', 42, channel_norm_df, 'fov0'
)

fov_data_path = os.path.join(
Expand Down