Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
JBeckwith committed Apr 5, 2024
1 parent 93f7437 commit 03a2142
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 1 addition & 9 deletions pyRASP_Example_SingleImageAnalysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "639d889a-964f-48c6-96be-f3070b50a506",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -43,14 +43,6 @@
" large_thres=450., gsigma=1.4, rwave=2., image_size=300, save_figure=True,\n",
" cell_analysis=True, cell_file=cell_file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adddd2e3-cac5-4ac7-8078-ba2faa2770f6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
16 changes: 13 additions & 3 deletions src/AnalysisFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def generate_mask_and_spot_indices(self, mask, centroids, image_size):
spot_indices = np.ravel_multi_index(centroids.T, image_size, order='F')
return mask_indices, spot_indices

def calculate_spot_colocalisation_likelihood_ratio(self, spot_indices, mask_indices, image_size, tol=0.01, n_iter=100):
def calculate_spot_colocalisation_likelihood_ratio(self, spot_indices, mask_indices, image_size, tol=0.01, n_iter=100, blur_degree=1, max_iter=10000):
"""
gets spot colocalisation likelihood ratio, as well as reporting error
bounds on the likelihood ratio for one image
Expand All @@ -136,6 +136,9 @@ def calculate_spot_colocalisation_likelihood_ratio(self, spot_indices, mask_indi
image_size (tuple): Image dimensions (height, width).
tol (float): default 0.01; tolerance for convergence
n_iter (int): default 100; number of iterations to start with
blur_degree (int): number of pixels to blur spot indices with
(i.e. number of pixels surrounding centroid to
consider part of spot). Default 1
Returns:
colocalisation_likelihood_ratio (float): likelihood ratio of spots for mask
Expand All @@ -144,9 +147,16 @@ def calculate_spot_colocalisation_likelihood_ratio(self, spot_indices, mask_indi
expected_spots (float): number of spots we expect based on mask % of image
n_iter (int): how many iterations it took to converge
"""
n_spots = len(spot_indices) # get number of spots
if blur_degree > 0:
for i, index in enumerate(spot_indices):
if i == 0:
new_spot_indices = self.dilate_pixel(index, image_size, width=blur_degree+1, edge=blur_degree)
else:
new_spot_indices = np.hstack([new_spot_indices, self.dilate_pixel(index, image_size, width=blur_degree+1, edge=blur_degree)])
spot_indices = new_spot_indices
n_iter_rec = n_iter
possible_indices = np.arange(0, np.prod(image_size)) # get list of where is possible to exist in an image
n_spots = len(spot_indices) # get number of spots
mask_fill = self.calculate_mask_fill(mask_indices, image_size) # get mask_fill
expected_spots = np.multiply(mask_fill, n_spots) # get expected number of spots
if np.isclose(expected_spots, 0., atol=1e-4):
Expand All @@ -167,7 +177,7 @@ def calculate_spot_colocalisation_likelihood_ratio(self, spot_indices, mask_indi

meanCSR = np.divide(np.nanmean(CSR), expected_spots) # should be close to 1
CSR_diff = np.abs(meanCSR - 1.)
while CSR_diff > tol: # do n_iter more tests iteratively until convergence
while (CSR_diff > tol) and (n_iter_rec < max_iter): # do n_iter more tests iteratively until convergence
n_iter_rec = n_iter_rec + n_iter # add n_iter iterations
CSR_new = np.zeros([n_iter])
random_spot_locations = np.random.choice(possible_indices, size=(n_iter, n_spots)) # get random spot locations
Expand Down
Binary file modified src/__pycache__/AnalysisFunctions.cpython-310.pyc
Binary file not shown.

0 comments on commit 03a2142

Please sign in to comment.