Skip to content

Commit

Permalink
Merge 6fa7d1b into b591d38
Browse files Browse the repository at this point in the history
  • Loading branch information
ackagel authored Jan 19, 2022
2 parents b591d38 + 6fa7d1b commit 51d3d48
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 114 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ env
*/_build
*/_markdown
*/visualizations

*.c
*.html
*.so
*.egg-info
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cache: pip

# command to run tests
script:
- python -m pip install --editable .
- python -m pytest --cov=ark --pycodestyle ark

jobs:
Expand Down
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ RUN apt-get update && apt-get install -y gcc
WORKDIR /scripts

# copy over the requirements.txt, install dependencies, and README
COPY setup.py requirements.txt README.md /opt/ark-analysis/
COPY setup.py pyproject.toml requirements.txt README.md /opt/ark-analysis/
COPY .toks /home/.toks
RUN pip install -r /opt/ark-analysis/requirements.txt
RUN python -m pip install -r /opt/ark-analysis/requirements.txt

# copy the scripts over
# this should catch changes to the scripts from updates
COPY ark /opt/ark-analysis/ark

# Install the package via setup.py
RUN pip install /opt/ark-analysis
RUN cd /opt/ark-analysis && python -m pip install .

# jupyter lab
CMD jupyter lab --ip=0.0.0.0 --allow-root --no-browser --port=$JUPYTER_PORT
8 changes: 4 additions & 4 deletions ark/analysis/spatial_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ def calculate_channel_spatial_enrichment(dist_matrices_dict, marker_thresholds,
dist_matrix = dist_matrices_dict[fov]

# Get close_num and close_num_rand
close_num, channel_nums, _ = spatial_analysis_utils.compute_close_cell_num(
close_num, channel_nums, mark_pos_labels = spatial_analysis_utils.compute_close_cell_num(
dist_mat=dist_matrix, dist_lim=100, analysis_type="channel",
current_fov_data=current_fov_data, current_fov_channel_data=current_fov_channel_data,
thresh_vec=thresh_vec)

close_num_rand = spatial_analysis_utils.compute_close_cell_num_random(
channel_nums, dist_matrix, dist_lim, bootstrap_num)
channel_nums, mark_pos_labels, dist_matrix, dist_lim, bootstrap_num)

values.append((close_num, close_num_rand))

Expand Down Expand Up @@ -342,12 +342,12 @@ def calculate_cluster_spatial_enrichment(all_data, dist_matrices_dict, included_
dist_mat = dist_matrices_dict[fov]

# Get close_num and close_num_rand
close_num, pheno_nums, pheno_nums_per_id = spatial_analysis_utils.compute_close_cell_num(
close_num, pheno_nums, mark_pos_labels = spatial_analysis_utils.compute_close_cell_num(
dist_mat=dist_mat, dist_lim=dist_lim, analysis_type="cluster",
current_fov_data=current_fov_pheno_data, cluster_ids=cluster_ids)

close_num_rand = spatial_analysis_utils.compute_close_cell_num_random(
pheno_nums, dist_mat, dist_lim, bootstrap_num)
pheno_nums, mark_pos_labels, dist_mat, dist_lim, bootstrap_num)

# close_num_rand_context = spatial_analysis_utils.compute_close_cell_num_random(
# pheno_nums_per_id, dist_mat, dist_lim, bootstrap_num)
Expand Down
42 changes: 11 additions & 31 deletions ark/analysis/spatial_analysis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,38 @@ def test_batch_channel_spatial_enrichment():

all_data = test_utils.spoof_cell_table_from_labels(label_maps)

# fix seed
random.seed(0)
vals_pos, stats_pos = \
spatial_analysis.calculate_channel_spatial_enrichment(
dist_mats, marker_thresholds, all_data, excluded_channels=EXCLUDE_CHANNELS,
bootstrap_num=100, dist_lim=100)

# fix seed
random.seed(0)
vals_pos_batch, stats_pos_batch = \
spatial_analysis.batch_channel_spatial_enrichment(
label_dir, marker_thresholds, all_data, excluded_channels=EXCLUDE_CHANNELS,
bootstrap_num=100, dist_lim=100, batch_size=5)

# fix seed
random.seed(0)
vals_pos_batch_2, stats_pos_batch_2 = \
spatial_analysis.batch_channel_spatial_enrichment(
label_dir, marker_thresholds, all_data, excluded_channels=EXCLUDE_CHANNELS,
bootstrap_num=100, dist_lim=100, batch_size=1
)

np.testing.assert_equal(vals_pos, vals_pos_batch)
xr.testing.assert_equal(stats_pos, stats_pos_batch)
np.testing.assert_equal(vals_pos[0][0], vals_pos_batch[0][0])
np.testing.assert_equal(vals_pos[1][0], vals_pos_batch[1][0])

# batch function should match for multi batch process
np.testing.assert_equal(vals_pos, vals_pos_batch_2)
xr.testing.assert_equal(stats_pos, stats_pos_batch_2)
np.testing.assert_equal(vals_pos[0][0], vals_pos_batch_2[0][0])
np.testing.assert_equal(vals_pos[1][0], vals_pos_batch_2[1][0])

# test fov inclusion w/ fixed seed
random.seed(0)
vals_pos_fov8, stats_pos_fov8 = \
spatial_analysis.batch_channel_spatial_enrichment(
label_dir, marker_thresholds, all_data, excluded_channels=EXCLUDE_CHANNELS,
bootstrap_num=100, dist_lim=100, batch_size=5, included_fovs=["fov8"]
)

np.testing.assert_equal(vals_pos_fov8[0], vals_pos[0])
np.testing.assert_equal(vals_pos_fov8[0][0], vals_pos[0][0])
assert len(vals_pos_fov8) == 1

xr.testing.assert_equal(stats_pos_fov8, stats_pos.loc[["fov8"], :, :])


def test_batch_cluster_spatial_enrichment():

Expand All @@ -111,43 +101,33 @@ def test_batch_cluster_spatial_enrichment():

all_data = test_utils.spoof_cell_table_from_labels(label_maps)

# fix seed
random.seed(0)
vals_pos, stats_pos = \
spatial_analysis.calculate_cluster_spatial_enrichment(
all_data, dist_mats, bootstrap_num=100, dist_lim=100)

# fix seed
random.seed(0)
vals_pos_batch, stats_pos_batch = \
spatial_analysis.batch_cluster_spatial_enrichment(
label_dir, all_data, bootstrap_num=100, dist_lim=100, batch_size=5)

# fix seed
random.seed(0)
vals_pos_batch_2, stats_pos_batch_2 = \
spatial_analysis.batch_cluster_spatial_enrichment(
label_dir, all_data, bootstrap_num=100, dist_lim=100, batch_size=1)

np.testing.assert_equal(vals_pos, vals_pos_batch)
xr.testing.assert_equal(stats_pos, stats_pos_batch)
np.testing.assert_equal(vals_pos[0][0], vals_pos_batch[0][0])
np.testing.assert_equal(vals_pos[1][0], vals_pos_batch[1][0])

# batch function should match for multi batch process
np.testing.assert_equal(vals_pos, vals_pos_batch_2)
xr.testing.assert_equal(stats_pos, stats_pos_batch_2)
np.testing.assert_equal(vals_pos[0][0], vals_pos_batch_2[0][0])
np.testing.assert_equal(vals_pos[1][0], vals_pos_batch_2[1][0])

# test fov inclusion w/ fixed seed
random.seed(0)
vals_pos_fov8, stats_pos_fov8 = \
spatial_analysis.batch_cluster_spatial_enrichment(
label_dir, all_data, bootstrap_num=100, dist_lim=100, batch_size=5,
included_fovs=["fov8"])

np.testing.assert_equal(vals_pos_fov8[0], vals_pos[0])
np.testing.assert_equal(vals_pos_fov8[0][0], vals_pos[0][0])
assert len(vals_pos_fov8) == 1

xr.testing.assert_equal(stats_pos_fov8, stats_pos.loc[["fov8"], :, :])


def test_calculate_channel_spatial_enrichment():
dist_lim = 100
Expand Down Expand Up @@ -189,6 +169,7 @@ def test_calculate_channel_spatial_enrichment():
# Test both fov8 and fov9
# Extract the p-values and z-scores of the distance of marker 1 vs marker 2 for negative
# enrichment as tested against a random set of distances between centroids

assert stats_neg.loc["fov8", "p_neg", 2, 3] < .05
assert stats_neg.loc["fov8", "p_pos", 2, 3] > .05
assert stats_neg.loc["fov8", "z", 2, 3] < 0
Expand Down Expand Up @@ -275,7 +256,6 @@ def test_calculate_cluster_spatial_enrichment():
# Negative enrichment
all_data_neg, dist_mat_neg = test_utils._make_dist_exp_mats_spatial_test(
enrichment_type="negative", dist_lim=dist_lim)

_, stats_neg = \
spatial_analysis.calculate_cluster_spatial_enrichment(
all_data_neg, dist_mat_neg,
Expand Down
Loading

0 comments on commit 51d3d48

Please sign in to comment.