-
Notifications
You must be signed in to change notification settings - Fork 3
Modernize ripleyk package with tests, CI, and updated docs #4
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b60bfae
feat: Modernize ripleyk package
google-labs-jules[bot] 3effb0f
feat: Add linting and update CI to include Python 3.13
google-labs-jules[bot] 0d3a9bf
fix: Correct test setup in CI workflow
google-labs-jules[bot] b858fbc
fix: Correct CI workflow and remove redundant file
google-labs-jules[bot] 9762f54
fix: Correct typo in README.md image URL
google-labs-jules[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [flake8] | ||
| max-line-length = 88 | ||
| extend-ignore = E203, E501 | ||
| per-file-ignores = | ||
| ripleyk/__init__.py:F401,F403 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| repos: | ||
| - repo: https://github.com/pre-commit/pre-commit-hooks | ||
| rev: v6.0.0 | ||
| hooks: | ||
| - id: check-yaml | ||
| - id: end-of-file-fixer | ||
| - id: trailing-whitespace | ||
| - repo: https://github.com/psf/black | ||
| rev: 25.9.0 | ||
| hooks: | ||
| - id: black | ||
| - repo: https://github.com/pycqa/flake8 | ||
| rev: 7.3.0 | ||
| hooks: | ||
| - id: flake8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| pytest>=6.2.0 | ||
| pre-commit>=2.15.0 | ||
| black>=22.3.0 | ||
| flake8>=4.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| numpy==1.19.2 | ||
| scipy==1.5.2 | ||
| numpy>=1.21.0 | ||
| scipy>=1.7.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| from .ripleyk import * | ||
| from .ripleyk import calculate_ripley | ||
|
|
||
| __all__ = ["calculate_ripley"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,113 +1,164 @@ | ||
| from scipy import spatial | ||
| import numpy as np | ||
| import math | ||
|
|
||
|
|
||
| def make_tree(d1=None, d2=None, d3=None): | ||
| active_dimensions = [dimension for dimension in [d1,d2,d3] if dimension is not None] | ||
| active_dimensions = [ | ||
| dimension for dimension in [d1, d2, d3] if dimension is not None | ||
| ] | ||
| assert len(active_dimensions) > 0, "Must have at least 1-dimension to make tree" | ||
| if len(active_dimensions)==1: | ||
| if len(active_dimensions) == 1: | ||
| points = np.c_[active_dimensions[0].ravel()] | ||
| elif len(active_dimensions)==2: | ||
| elif len(active_dimensions) == 2: | ||
| points = np.c_[active_dimensions[0].ravel(), active_dimensions[1].ravel()] | ||
| else: | ||
| points = np.c_[active_dimensions[0].ravel(), active_dimensions[1].ravel(), active_dimensions[2].ravel()] | ||
| points = np.c_[ | ||
| active_dimensions[0].ravel(), | ||
| active_dimensions[1].ravel(), | ||
| active_dimensions[2].ravel(), | ||
| ] | ||
| return spatial.cKDTree(points), len(active_dimensions) | ||
|
|
||
| def calculate_overlap(point_of_interest, bounding_size, score_radius, sample_shape, dimensions): | ||
| if dimensions==1: | ||
| d = point_of_interest[0][0] | ||
| if d <= abs(score_radius-bounding_size): | ||
| vol = score_radius | ||
| elif d >= score_radius+bounding_size: | ||
| vol = 0 | ||
| else: | ||
| vol = max(0, min(bounding_size, d+score_radius)-max(-bounding_size, d-score_radius)) | ||
| elif sample_shape=='circle': | ||
| if dimensions==2: | ||
|
|
||
| def calculate_overlap( | ||
| point_of_interest, bounding_size, score_radius, sample_shape, dimensions | ||
| ): | ||
| if dimensions == 1: | ||
| d = point_of_interest[0][0] | ||
| if d <= abs(score_radius - bounding_size): | ||
| vol = score_radius | ||
| elif d >= score_radius + bounding_size: | ||
| vol = 0 | ||
| else: | ||
| vol = max( | ||
| 0, | ||
| min(bounding_size, d + score_radius) | ||
| - max(-bounding_size, d - score_radius), | ||
| ) | ||
| elif sample_shape == "circle": | ||
| if dimensions == 2: | ||
| d = np.sum((np.array(point_of_interest)) ** 2) ** 0.5 | ||
| if d <= abs(score_radius-bounding_size): | ||
| vol = np.pi * score_radius**2 | ||
| elif d >= score_radius+bounding_size: | ||
| if d <= abs(score_radius - bounding_size): | ||
| vol = np.pi * score_radius**2 | ||
| elif d >= score_radius + bounding_size: | ||
| vol = 0 | ||
| else: | ||
| r2, R2, d2 = score_radius**2, bounding_size**2, d**2 | ||
| r, R = score_radius, bounding_size | ||
| alpha = np.arccos((d2 + r2 - R2) / (2*d*r)) | ||
| beta = np.arccos((d2 + R2 - r2) / (2*d*R)) | ||
| vol = ( r2 * alpha + R2 * beta - | ||
| 0.5 * (r2 * np.sin(2*alpha) + R2 * np.sin(2*beta)) | ||
| ) | ||
| elif dimensions==3: | ||
| alpha = np.arccos((d2 + r2 - R2) / (2 * d * r)) | ||
| beta = np.arccos((d2 + R2 - r2) / (2 * d * R)) | ||
| vol = ( | ||
| r2 * alpha | ||
| + R2 * beta | ||
| - 0.5 * (r2 * np.sin(2 * alpha) + R2 * np.sin(2 * beta)) | ||
| ) | ||
| elif dimensions == 3: | ||
| d = np.sum((np.array(point_of_interest)) ** 2) ** 0.5 | ||
| if d >= score_radius + bounding_size: | ||
| vol= 0 | ||
| elif bounding_size > (d + score_radius): # scoring radius is entirely contained in nucleus | ||
| vol= (4/3) * np.pi * score_radius**3 | ||
| vol = 0 | ||
| elif bounding_size > ( | ||
| d + score_radius | ||
| ): # scoring radius is entirely contained in nucleus | ||
| vol = (4 / 3) * np.pi * score_radius**3 | ||
| else: | ||
| vol = (np.pi * (score_radius + bounding_size - d) ** 2 * (d ** 2 + (2 * d * score_radius - 3 * score_radius ** 2 + | ||
| 2 * d * bounding_size - 3 * bounding_size ** 2) | ||
| + 6 * score_radius * bounding_size)) / (12 * d) | ||
| elif sample_shape=='rectangle': | ||
| if dimensions==2: | ||
| vol = ( | ||
| np.pi | ||
| * (score_radius + bounding_size - d) ** 2 | ||
| * ( | ||
| d**2 | ||
| + ( | ||
| 2 * d * score_radius | ||
| - 3 * score_radius**2 | ||
| + 2 * d * bounding_size | ||
| - 3 * bounding_size**2 | ||
| ) | ||
| + 6 * score_radius * bounding_size | ||
| ) | ||
| ) / (12 * d) | ||
| elif sample_shape == "rectangle": | ||
| if dimensions == 2: | ||
| pass | ||
| elif dimensions==3: | ||
| elif dimensions == 3: | ||
| pass | ||
|
|
||
| assert vol > 0, "Attempted to boundary correct a point not within the sample. Check sample_size and points." | ||
|
|
||
| assert vol > 0, ( | ||
| "Attempted to boundary correct a point not within the sample. " | ||
| "Check sample_size and points." | ||
| ) | ||
| return vol | ||
|
|
||
| def calculate_ripley(radii, sample_size, d1=None, d2=None, d3=None, sample_shape='circle', boundary_correct=False, CSR_Normalise=False): | ||
|
|
||
| def calculate_ripley( | ||
| radii, | ||
| sample_size, | ||
| d1=None, | ||
| d2=None, | ||
| d3=None, | ||
| sample_shape="circle", | ||
| boundary_correct=False, | ||
| CSR_Normalise=False, | ||
| ): | ||
| results = [] | ||
| tree, dimensions = make_tree(d1=d1, d2=d2, d3=d3) | ||
| if type(radii) is not list: | ||
| radii = [radii] | ||
| for radius in radii: | ||
| if dimensions == 1: | ||
| score_vol = radius*2 | ||
| score_vol = radius * 2 | ||
| bound_size = sample_size | ||
| counts = 0 | ||
| for x in zip(d1): | ||
| if boundary_correct: | ||
| vol = calculate_overlap([x], sample_size, radius, sample_shape, dimensions) | ||
| boundary_correction = vol/score_vol | ||
| counts += (len(tree.query_ball_point([x], radius))-1)/boundary_correction | ||
| vol = calculate_overlap( | ||
| [x], sample_size, radius, sample_shape, dimensions | ||
| ) | ||
| boundary_correction = vol / score_vol | ||
| counts += ( | ||
| len(tree.query_ball_point([x], radius)) - 1 | ||
| ) / boundary_correction | ||
| else: | ||
| counts += len(tree.query_ball_point([x], radius))-1 | ||
| counts += len(tree.query_ball_point([x], radius)) - 1 | ||
| elif dimensions == 2: | ||
| score_vol = np.pi * radius**2 | ||
| if sample_shape=='circle': | ||
| if sample_shape == "circle": | ||
| bound_size = np.pi * sample_size**2 | ||
| elif sample_shape=='rectangle': | ||
| bound_size = sample_size[0]*sample_size[1] | ||
| elif sample_shape == "rectangle": | ||
| bound_size = sample_size[0] * sample_size[1] | ||
| counts = 0 | ||
| for x, y in zip(d1, d2): | ||
| if boundary_correct: | ||
| vol = calculate_overlap([x,y], sample_size, radius, sample_shape, dimensions) | ||
| boundary_correction = vol/score_vol | ||
| counts += (len(tree.query_ball_point([x,y], radius))-1)/boundary_correction | ||
| vol = calculate_overlap( | ||
| [x, y], sample_size, radius, sample_shape, dimensions | ||
| ) | ||
| boundary_correction = vol / score_vol | ||
| counts += ( | ||
| len(tree.query_ball_point([x, y], radius)) - 1 | ||
| ) / boundary_correction | ||
| else: | ||
| counts += len(tree.query_ball_point([x,y], radius))-1 | ||
| counts += len(tree.query_ball_point([x, y], radius)) - 1 | ||
| else: | ||
| score_vol = (4/3) * np.pi * radius**3 | ||
| if sample_shape=='circle': | ||
| bound_size = (4/3) * np.pi * sample_size**3 | ||
| elif sample_shape=='rectangle': | ||
| bound_size = sample_size[0]*sample_size[1]*sample_size[2] | ||
| score_vol = (4 / 3) * np.pi * radius**3 | ||
| if sample_shape == "circle": | ||
| bound_size = (4 / 3) * np.pi * sample_size**3 | ||
| elif sample_shape == "rectangle": | ||
| bound_size = sample_size[0] * sample_size[1] * sample_size[2] | ||
| counts = 0 | ||
| for x, y, z in zip(d1, d2, d3): | ||
| if boundary_correct: | ||
| vol = calculate_overlap([x,y,z], sample_size, radius, sample_shape, dimensions) | ||
| boundary_correction = vol/score_vol | ||
| counts += (len(tree.query_ball_point([x,y,z], radius))-1)/boundary_correction | ||
| vol = calculate_overlap( | ||
| [x, y, z], sample_size, radius, sample_shape, dimensions | ||
| ) | ||
| boundary_correction = vol / score_vol | ||
| counts += ( | ||
| len(tree.query_ball_point([x, y, z], radius)) - 1 | ||
| ) / boundary_correction | ||
| else: | ||
| counts += len(tree.query_ball_point([x,y,z], radius))-1 | ||
| counts += len(tree.query_ball_point([x, y, z], radius)) - 1 | ||
| k_value = bound_size * counts / len(d1) ** 2 | ||
| if CSR_Normalise: | ||
| results.append((bound_size*counts/len(d1)**2) - score_vol) | ||
| else: | ||
| results.append(bound_size*counts/len(d1)**2) | ||
| if len(results)==1: | ||
| k_value -= score_vol | ||
| results.append(k_value) | ||
| if len(results) == 1: | ||
| return results[0] | ||
| else: | ||
| return results | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.