-
Notifications
You must be signed in to change notification settings - Fork 0
Add numpy array support to initialize_cells for napari integration #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
Changes from all commits
b6f26c9
e78a798
f1a2349
98fdb0b
76b9059
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,9 +12,9 @@ | |||||||
| from src.pyVertexModel.algorithm.vertexModel import create_tetrahedra | ||||||||
| from src.pyVertexModel.algorithm.vertexModelBubbles import build_topo, SeedWithBoundingBox, generate_first_ghost_nodes, \ | ||||||||
| delaunay_compute_entities, VertexModelBubbles | ||||||||
| from src.pyVertexModel.algorithm.vertexModelVoronoiFromTimeImage import build_triplets_of_neighs, calculate_neighbours, \ | ||||||||
| VertexModelVoronoiFromTimeImage, add_tetrahedral_intercalations, build_2d_voronoi_from_image, \ | ||||||||
| populate_vertices_info, calculate_vertices, get_four_fold_vertices, divide_quartets_neighbours, process_image | ||||||||
| from src.pyVertexModel.algorithm.vertexModelVoronoiFromTimeImage import build_triplets_of_neighs, \ | ||||||||
| VertexModelVoronoiFromTimeImage, add_tetrahedral_intercalations, \ | ||||||||
| get_four_fold_vertices, divide_quartets_neighbours, process_image | ||||||||
| from src.pyVertexModel.geometry.degreesOfFreedom import DegreesOfFreedom | ||||||||
| from src.pyVertexModel.util.utils import save_backup_vars | ||||||||
|
|
||||||||
|
|
@@ -496,3 +496,84 @@ def test_initialize_voronoi_from_time_image(self): | |||||||
| assert_array1D(g_test, mat_info['g']) | ||||||||
| assert_matrix(K_test, mat_info['K']) | ||||||||
|
|
||||||||
| def test_initialize_cells_with_numpy_array(self): | ||||||||
| """ | ||||||||
| Test that initialize_cells can accept a numpy array directly. | ||||||||
| :return: | ||||||||
| """ | ||||||||
| import scipy.io | ||||||||
| from src.pyVertexModel.parameters.set import Set | ||||||||
|
|
||||||||
| # Load an existing image as a numpy array | ||||||||
| mat_data = scipy.io.loadmat('resources/LblImg_imageSequence.mat') | ||||||||
| img_array = mat_data['imgStackLabelled'] | ||||||||
|
|
||||||||
| # Create a simple 2D labeled image for testing | ||||||||
| # Using a subset to make it faster | ||||||||
| img_2d = img_array[:, :, 0] | ||||||||
|
|
||||||||
| # Create settings | ||||||||
| set_test = Set(set_option='voronoi_from_image') | ||||||||
| set_test.TotalCells = 50 # Use fewer cells for faster testing | ||||||||
| set_test.CellHeight = 10 | ||||||||
|
|
||||||||
| # Test with numpy array input | ||||||||
|
Comment on lines
+516
to
+520
|
||||||||
| vModel_test = VertexModelVoronoiFromTimeImage(set_option='voronoi_from_image', set_test=set_test, | ||||||||
| create_output_folder=False) | ||||||||
| vModel_test.initialize_cells(img_2d) | ||||||||
|
|
||||||||
| # Verify that the geometry was created | ||||||||
| assert vModel_test.geo is not None, "Geometry should be initialized" | ||||||||
| assert vModel_test.geo.nCells > 0, "Should have cells" | ||||||||
| assert len(vModel_test.geo.Cells) > 0, "Should have Cell objects" | ||||||||
|
|
||||||||
| def test_process_image_with_numpy_array(self): | ||||||||
| """ | ||||||||
| Test that process_image can handle numpy array input. | ||||||||
| :return: | ||||||||
| """ | ||||||||
| # Create a simple labeled image | ||||||||
| test_img = np.zeros((100, 100), dtype=np.uint16) | ||||||||
| # Create some labeled regions | ||||||||
| test_img[10:30, 10:30] = 1 | ||||||||
| test_img[40:60, 40:60] = 2 | ||||||||
| test_img[70:90, 70:90] = 3 | ||||||||
|
|
||||||||
| # Test process_image with numpy array | ||||||||
| img2d, img_stack = process_image(test_img) | ||||||||
|
|
||||||||
| # Verify the output | ||||||||
| assert img2d is not None, "2D image should be returned" | ||||||||
| assert img_stack is not None, "Image stack should be returned" | ||||||||
| assert img2d.shape == test_img.shape, "2D image should have same shape as input" | ||||||||
|
Comment on lines
+535
to
+548
|
||||||||
|
|
||||||||
| def test_initialize_with_numpy_array(self): | ||||||||
| """ | ||||||||
| Test that initialize method can accept a numpy array. | ||||||||
| :return: | ||||||||
| """ | ||||||||
| import scipy.io | ||||||||
| from src.pyVertexModel.parameters.set import Set | ||||||||
|
|
||||||||
| # Load an existing image as a numpy array | ||||||||
| mat_data = scipy.io.loadmat('resources/LblImg_imageSequence.mat') | ||||||||
| img_array = mat_data['imgStackLabelled'] | ||||||||
|
|
||||||||
| # Use a 2D slice for faster testing | ||||||||
| img_2d = img_array[:, :, 0] | ||||||||
|
|
||||||||
| # Create settings | ||||||||
| set_test = Set(set_option='voronoi_from_image') | ||||||||
|
||||||||
| set_test = Set(set_option='voronoi_from_image') | |
| set_test = Set() | |
| set_test.set_option = 'voronoi_from_image' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -251,9 +251,11 @@ def __init__(self, set_option='wing_disc', c_set=None, create_output_folder=True | |
| self.tr = 0 | ||
| self.numStep = 1 | ||
|
|
||
| def initialize(self): | ||
| def initialize(self, img_input=None): | ||
| """ | ||
| Initialize the geometry and the topology of the model. | ||
| :param img_input: Optional. Either a filename (str) or a numpy array containing the image. | ||
| If None, uses the filename from settings. | ||
| """ | ||
| filename = os.path.join(PROJECT_DIRECTORY, self.set.initial_filename_state) | ||
|
|
||
|
|
@@ -285,7 +287,10 @@ def initialize(self): | |
| self.geo = Geo(mat_info['Geo']) | ||
| self.geo.update_measures() | ||
| else: | ||
| self.initialize_cells(filename) | ||
| if img_input is None: | ||
| self.initialize_cells(filename) | ||
| else: | ||
| self.initialize_cells(img_input) | ||
|
Comment on lines
+290
to
+293
|
||
|
|
||
| # Resize the geometry to a given cell volume average | ||
| self.geo.resize_tissue() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import list was trimmed, but this test module still calls
calculate_neighbours,build_2d_voronoi_from_image,populate_vertices_info, andcalculate_verticeslater in the file. Those names are now undefined, so the test suite will fail withNameError. Either restore the needed imports or update the tests to call these via aVertexModelVoronoiFromTimeImageinstance (if they are instance methods).