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

Export cells to brainrender from brainmapper widget #95

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 43 additions & 1 deletion brainglobe_utils/brainmapper/transform_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from brainglobe_utils.brainmapper.analysis import (
summarise_points_by_atlas_region,
)
from brainglobe_utils.brainmapper.export import export_points_to_brainrender
from brainglobe_utils.brainreg.transform import (
transform_points_from_downsampled_to_atlas_space,
)
Expand Down Expand Up @@ -146,7 +147,7 @@ def setup_main_layout(self) -> None:
self.add_points_combobox(row=1, column=0)
self.add_raw_data_combobox(row=2, column=0)
self.add_transform_button(row=3, column=0)

self.add_brainrender_export_button(row=3, column=1)
self.add_points_summary_table(row=4, column=0)
self.add_save_all_points_button(row=6, column=0)
self.add_save_points_summary_button(row=6, column=1)
Expand Down Expand Up @@ -229,6 +230,28 @@ def add_transform_button(self, row: int, column: int) -> None:
tooltip="Transform points layer to atlas space",
)

def add_brainrender_export_button(self, row: int, column: int) -> None:
"""
Add a button to export the points in atlas space in the brainrender
format.

Parameters
----------
row : int
Row in the grid layout.
column : int
Column in the grid layout.
"""
self.brainrender_export_button = add_button(
"Export to brainrender",
self.layout,
self.export_points_to_brainrender,
row=row,
column=column,
visibility=False,
tooltip="Export points in atlas space to brainrender",
)

def add_points_summary_table(self, row: int, column: int) -> None:
"""
Add a table to display the summary of points per atlas region.
Expand Down Expand Up @@ -565,6 +588,7 @@ def analyse_points(self) -> None:
)

self.populate_summary_table()
self.brainrender_export_button.setVisible(True)
self.save_all_points_button.setVisible(True)
self.save_points_summary_button.setVisible(True)

Expand Down Expand Up @@ -596,6 +620,24 @@ def populate_summary_table(
self.points_per_region_table_title.setVisible(True)
self.points_per_region_table.setVisible(True)

def export_points_to_brainrender(self) -> None:
"""
Export points in the format required for brainrender
N.B. assumes atlas is isotropic
"""
path, _ = QFileDialog.getSaveFileName(
self,
"Choose filename",
"",
"NumPy Files (*.npy)",
)

if path:
path = ensure_extension(path, ".npy")
export_points_to_brainrender(
self.points_in_atlas_space, self.atlas.resolution[0], path
)

def save_all_points_csv(self) -> None:
"""
Save the coordinate and atlas region
Expand Down
25 changes: 25 additions & 0 deletions tests/tests/test_brainmapper/test_transform_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,31 @@ def test_analysis(transformation_widget_with_transformed_points):
)


def test_export_to_brainmapper(
mocker,
transformation_widget_with_transformed_points,
tmp_path,
):
mock_get_save_file_name = mocker.patch(
"brainglobe_utils.brainmapper.transform_widget.QFileDialog.getSaveFileName"
)

save_path = tmp_path / "points.npy"
mock_get_save_file_name.return_value = (save_path, "NumPy Files (*.npy)")

transformation_widget_with_transformed_points.export_points_to_brainrender()

# Ensure the file dialog was called
mock_get_save_file_name.assert_called_once_with(
transformation_widget_with_transformed_points,
"Choose filename",
"",
"NumPy Files (*.npy)",
)

assert save_path.exists()


def test_save_df_to_csv(
mocker,
transformation_widget_with_transformed_points,
Expand Down
Loading