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

DM-10803: Refactor grid generation in ImageMapReduceTask #64

Merged
merged 1 commit into from Jun 9, 2017

Conversation

djreiss
Copy link
Contributor

@djreiss djreiss commented Jun 5, 2017

  • Rename 'gridCentroidsX/Y' to 'cellCentroidsX/Y'
  • Rename 'gridSizeX/Y' to 'cellSizeX/Y'
  • Refactor grid generation to iterate over centroids rather than
    offsetting bounding boxes

Copy link
Contributor

@parejkoj parejkoj left a comment

Choose a reason for hiding this comment

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

Handful of minor suggestions, plus question about clipping and suggestion to use meshgrid instead of double-list-comprehension.

if scaleByFwhm:
self.log.info("Scaling grid parameters by %f" % psfFwhm)
if cellCentroidsX is None or len(cellCentroidsX) <= 0:
# No given centroids; construct them from cellSize/gridStep
Copy link
Contributor

Choose a reason for hiding this comment

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

"Not given centroids"?


def rescaleValue(val):
psfFwhm = (exposure.getPsf().computeShape().getDeterminantRadius() *
2. * np.sqrt(2. * np.log(2.)))
Copy link
Contributor

Choose a reason for hiding this comment

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

No spaces around multiplication.

borderSizeX = rescaleValue(borderSizeX)
borderSizeY = rescaleValue(borderSizeY)

nGridX = bbox.getWidth() // gridStepX
Copy link
Contributor

Choose a reason for hiding this comment

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

No spaces around division (floor, or otherwise). Also below.

# Readjust spacings so that they fit perfectly in the image.
nGridX = bbox.getWidth() // cellSizeX + 1
nGridY = bbox.getHeight() // cellSizeY + 1
cellCentroids = [(x, y) for x in np.linspace(cellSizeX//2, bbox.getWidth() - cellSizeX//2,
Copy link
Contributor

Choose a reason for hiding this comment

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

Double-nested list comprehensions are a bit unpleasant. I think what you want to do is use np.meshgrid here. Possibly:

np.meshgrid(np.linspace(...), np.linspace(...))

See: https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See my comment below for why.

if bb.getWidth() % 2 == 1: # grow to the right
bb.include(afwGeom.Point2I(bb.getMaxX()+1, bb.getMaxY())) # Expand by 1 pixel!
bb.clip(bbox)
if bb.getWidth() % 2 == 1: # clipped at right -- so grow to the left
Copy link
Contributor

Choose a reason for hiding this comment

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

Could it ever be clipped on both sides, such that this this expansion also fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, it's only possible if the cell size is > the image size. This shouldn't happen but I suppose if it does I can shrink the bb instead of growing it.

Ugh, Box2Ds are not very easily manipulatable. I'm just going to raise an exception if it is not even-sized after these two attempts, let's see how often it actually happens.

# Use given or grid-parameterized centroids as centers for bounding boxes
if cellCentroidsX is not None and len(cellCentroidsX) > 0:
for i, centroidX in enumerate(cellCentroidsX):
centroidY = cellCentroidsY[i]
centroid = afwGeom.Point2D(centroidX, centroidY)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you make a meshgrid above, you should be able to do:

for xy in cellCentroids.flatten():
   centroid = afwGeom.Point2D(xy[0], xy[1])
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't work as meshgrid returns a list of arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Plus, the grid may not actually be a grid (if the config-ed cellCentroidsX and cellCentroidsY are not None). So I'm leaving as-is but refactoring a bit more to reduce code duplication.

- Rename 'gridCentroidsX/Y' to 'cellCentroidsX/Y'
- Rename 'gridSizeX/Y' to 'cellSizeX/Y'
- Refactor grid generation to iterate over centroids rather than
   offsetting bounding boxes
@djreiss djreiss merged commit c2cefa3 into master Jun 9, 2017
@ktlim ktlim deleted the tickets/DM-10803 branch August 25, 2018 06:44
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.

None yet

2 participants