-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63ecf2f
commit a4e7c7f
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,63 @@ | ||
def structure_from_viewer(coordinates, atlas_layer, atlas): | ||
""" | ||
Get brain region info from mouse position in napari viewer. | ||
Return brainglobe (BG) structure number, name, hemisphere, and a | ||
"pretty" string that can be displayed for example in the status bar. | ||
Parameter | ||
--------- | ||
coordinates : tuple, nx3 coordinate of cursor position, from | ||
Viewer.cursor.position | ||
atlas_layer : Napari viewer layer | ||
Layer, which contains the annotation / region | ||
information for every structure in the (registered) | ||
atlas | ||
atlas : Brainglobe atlas (bg_atlasapi.bg_atlas.BrainGlobeAtlas) | ||
Returns | ||
------- | ||
region_info : str | ||
A string containing info about structure | ||
and hemisphere | ||
Returns empty string if not found | ||
""" | ||
|
||
# Using a regex, extract list of coordinates from status string | ||
assert hasattr(atlas_layer, "data"), "Atlas layer appears to be empty" | ||
assert atlas_layer.data.ndim == 3, ( | ||
"Atlas layer data does not have the right dim " | ||
f'("{atlas_layer.data.ndim}")' | ||
) | ||
|
||
coord_list = tuple( | ||
[int(x / r) for x, r in zip(coordinates, atlas.resolution)] | ||
) | ||
|
||
# Extract structure number | ||
try: | ||
structure_no = atlas_layer.data[coord_list] | ||
except IndexError: | ||
return None, None, None, "" | ||
|
||
if structure_no in [0]: # 0 is "Null" region | ||
return None, None, None, "" | ||
|
||
# Extract structure information | ||
try: | ||
structure = atlas.structures[structure_no]["name"] | ||
except KeyError: | ||
return None, None, None, "" | ||
|
||
# ... and make string pretty | ||
region_info = [] | ||
for struct in structure.split(","): | ||
region_info.append(struct.strip().capitalize()) | ||
hemisphere = atlas.hemisphere_from_coords( | ||
coord_list, as_string=True | ||
).capitalize() | ||
region_info.append(hemisphere) | ||
region_info = " | ".join(region_info) | ||
|
||
return structure_no, structure, hemisphere, region_info |