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

Conversion between pixel_mask and image_mask #766

Merged
merged 7 commits into from
Jul 31, 2019

Conversation

mamelara
Copy link
Contributor

@mamelara mamelara commented Dec 18, 2018

Motivation

Addresses Issue #669

How to test the behavior?

Unit tests are included. To run the tests, use tox.

Checklist

  • Have you checked our Contributing document?
  • Have you ensured the PR description clearly describes problem and the solution?
  • Is your contribution compliant with our coding style ? This can be checked running flake8 from the source directory.
  • Have you checked to ensure that there aren't other open Pull Requests for the same change?
  • Have you included the relevant issue number using #XXX notation where XXX is the issue number ?

@mamelara
Copy link
Contributor Author

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.

@@ -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:
Copy link
Contributor

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


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)
Copy link
Contributor

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
Copy link

codecov bot commented Dec 21, 2018

Codecov Report

Merging #766 into dev will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/pynwb/ophys.py 95.41% <100%> (+0.77%) ⬆️
src/pynwb/form/utils.py 87.2% <0%> (-0.59%) ⬇️
src/pynwb/file.py 79.66% <0%> (-0.52%) ⬇️
src/pynwb/misc.py 92.55% <0%> (-0.48%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d8fb82a...6a6843e. Read the comment docs.

1 similar comment
@codecov
Copy link

codecov bot commented Dec 21, 2018

Codecov Report

Merging #766 into dev will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/pynwb/ophys.py 95.41% <100%> (+0.77%) ⬆️
src/pynwb/form/utils.py 87.2% <0%> (-0.59%) ⬇️
src/pynwb/file.py 79.66% <0%> (-0.52%) ⬇️
src/pynwb/misc.py 92.55% <0%> (-0.48%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d8fb82a...6a6843e. Read the comment docs.

@codecov
Copy link

codecov bot commented Dec 21, 2018

Codecov Report

Merging #766 into dev will increase coverage by 1.6%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/pynwb/ophys.py 95.41% <100%> (+1.4%) ⬆️
src/pynwb/epoch.py 73.13% <0%> (-9.22%) ⬇️
src/pynwb/core.py 70.6% <0%> (-3.15%) ⬇️
src/pynwb/__init__.py 69.49% <0%> (-2.55%) ⬇️
src/pynwb/icephys.py 88.88% <0%> (-0.4%) ⬇️
src/pynwb/ecephys.py 97.29% <0%> (-0.08%) ⬇️
src/pynwb/ogen.py 100% <0%> (ø) ⬆️
src/pynwb/behavior.py 100% <0%> (ø) ⬆️
src/pynwb/validate.py 0% <0%> (ø) ⬆️
src/pynwb/retinotopy.py 100% <0%> (ø) ⬆️
... and 33 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1acfa8f...6f4f634. Read the comment docs.

@@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def pixel_to_image(self, pixel_mask):
def pixel_to_image(pixel_mask):

image_matrix[y_coords, x_coords] = weights
return image_matrix

def image_to_pixel(self, image_mask):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def image_to_pixel(self, image_mask):
def image_to_pixel(image_mask):

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]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x_coords = tuple(map(int, npmask[:, 0]))
x_coords = npmask[:, 0].astype(np.int)

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]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
y_coords = tuple(map(int, npmask[:, 1]))
y_coords = npmask[:, 1].astype(np.int)

@mamelara mamelara changed the title WIP: Conversion between pixel_mask and image_mask Conversion between pixel_mask and image_mask Jan 10, 2019
@oruebel
Copy link
Contributor

oruebel commented Jul 31, 2019

@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?

mamelara added 6 commits July 31, 2019 14:19
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.
@mamelara
Copy link
Contributor Author

Rebased to current dev and added suggested changes

oruebel
oruebel previously approved these changes Jul 31, 2019
Copy link
Contributor

@oruebel oruebel left a 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.

@oruebel oruebel merged commit 8b010af into NeurodataWithoutBorders:dev Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants