-
Notifications
You must be signed in to change notification settings - Fork 85
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
Conversion between pixel_mask and image_mask #766
Conversion between pixel_mask and image_mask #766
Conversation
This is still WIP, but just wanted to see if I'm going in the right direction with this. I also need to take into account that an image_mask can have multiple image matrices in it. |
src/pynwb/ophys.py
Outdated
@@ -271,6 +271,27 @@ def add_roi(self, **kwargs): | |||
rkwargs['voxel_mask'] = voxel_mask | |||
return super(PlaneSegmentation, self).add_row(**rkwargs) | |||
|
|||
def pixel_to_image(self, pixel_mask, width, height): | |||
image_matrix = np.zeros((width, height)) | |||
for mask in pixel_mask: |
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.
I think something like this might be a bit faster than setting all the elements one-by-one in the for loop:
npmask = np.asarray(pixel_mask)
image_matrix[npmask[:,0], npmask[:,1]] = npmask[:,-1]
return image_matrix
src/pynwb/ophys.py
Outdated
|
||
def image_to_pixel(self, image_mask): | ||
"""Convert a image_mask to a pixel_mask that contains all the elements of the form (x, y, weight)""" | ||
width, height = np.shape(image_mask) |
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.
Here numpy nditer
might help simplify things:
pixel_mask = []
it = np.nditer(image_mask, flags=['multi_index'])
while not it.finished:
pixel_mask.append( [it.multi_index[0], it.multi_index[1], it[0][()]] )
it.iternext()
return pixel_mask
Codecov Report
@@ Coverage Diff @@
## dev #766 +/- ##
==========================================
+ Coverage 74.63% 74.68% +0.04%
==========================================
Files 60 60
Lines 6932 6963 +31
Branches 1450 1457 +7
==========================================
+ Hits 5174 5200 +26
Misses 1353 1353
- Partials 405 410 +5
Continue to review full report at Codecov.
|
1 similar comment
Codecov Report
@@ Coverage Diff @@
## dev #766 +/- ##
==========================================
+ Coverage 74.63% 74.68% +0.04%
==========================================
Files 60 60
Lines 6932 6963 +31
Branches 1450 1457 +7
==========================================
+ Hits 5174 5200 +26
Misses 1353 1353
- Partials 405 410 +5
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## dev #766 +/- ##
=========================================
+ Coverage 73.07% 74.68% +1.6%
=========================================
Files 36 60 +24
Lines 2715 6963 +4248
Branches 521 1457 +936
=========================================
+ Hits 1984 5200 +3216
- Misses 615 1353 +738
- Partials 116 410 +294
Continue to review full report at Codecov.
|
src/pynwb/ophys.py
Outdated
@@ -272,6 +272,27 @@ def add_roi(self, **kwargs): | |||
rkwargs['voxel_mask'] = voxel_mask | |||
return super(PlaneSegmentation, self).add_row(**rkwargs) | |||
|
|||
def pixel_to_image(self, pixel_mask): |
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.
def pixel_to_image(self, pixel_mask): | |
def pixel_to_image(pixel_mask): |
src/pynwb/ophys.py
Outdated
image_matrix[y_coords, x_coords] = weights | ||
return image_matrix | ||
|
||
def image_to_pixel(self, image_mask): |
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.
def image_to_pixel(self, image_mask): | |
def image_to_pixel(image_mask): |
src/pynwb/ophys.py
Outdated
def pixel_to_image(self, pixel_mask): | ||
image_matrix = np.zeros(np.shape(pixel_mask)) | ||
npmask = np.asarray(pixel_mask) | ||
x_coords = tuple(map(int, npmask[:, 0])) |
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.
x_coords = tuple(map(int, npmask[:, 0])) | |
x_coords = npmask[:, 0].astype(np.int) |
src/pynwb/ophys.py
Outdated
image_matrix = np.zeros(np.shape(pixel_mask)) | ||
npmask = np.asarray(pixel_mask) | ||
x_coords = tuple(map(int, npmask[:, 0])) | ||
y_coords = tuple(map(int, npmask[:, 1])) |
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.
y_coords = tuple(map(int, npmask[:, 1])) | |
y_coords = npmask[:, 1].astype(np.int) |
@mamelara what is the status of this PR? Looks like there were a few minor changes requested, could you add those so we can close and merge? |
Work in progress for creating methods that convert pixel to image and image to pixel mask. Eventually, will need to handle image masks that have multiple image matrices.
Uses numpy's way of indexing and iterating over numpy arrays to create images and pixels.
Modify some of the tests and also add a helper function for creating a basic plane segmentation when the boiler plate objects are not needed for testing
Suggestions include making pixel_to_image and image_to_pixel as staticmethods and using np.mask rather than tuples.
888b76f
to
c335433
Compare
Rebased to current dev and added suggested changes |
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.
It would be good to add a brief docstring to the pixel_to_image and image_to_pixel methods to describe what they do, but other than that I think this looks good.
Motivation
Addresses Issue #669
How to test the behavior?
Unit tests are included. To run the tests, use tox.
Checklist
flake8
from the source directory.#XXX
notation whereXXX
is the issue number ?