Skip to content

Commit

Permalink
Convert matplotlib input to pixel coordinate (close #509)
Browse files Browse the repository at this point in the history
  • Loading branch information
vallsv committed Jan 30, 2017
1 parent 202e30a commit b299a7b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
5 changes: 3 additions & 2 deletions pyFAI/massif.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
__contact__ = "[email protected]"
__license__ = "GPLv3+"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "24/11/2016"
__date__ = "12/12/2016"
__status__ = "production"

import sys
Expand Down Expand Up @@ -113,7 +113,8 @@ def find_peaks(self, x, nmax=200, annotate=None, massif_contour=None, stdout=sys
"""
All in one function that finds a maximum from the given seed (x)
then calculates the region extension and extract position of the neighboring peaks.
:param x: seed for the calculation, input coordinates
:param x: coordinates of the peak, seed for the calculation
:type x: tuple of integer
:param nmax: maximum number of peak per region
:param annotate: call back method taking number of points + coordinate as input.
:param massif_contour: callback to show the contour of a massif with the given index.
Expand Down
29 changes: 19 additions & 10 deletions pyFAI/peak_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
__contact__ = "[email protected]"
__license__ = "GPLv3+"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "27/10/2016"
__date__ = "12/12/2016"
__status__ = "production"

import os
Expand Down Expand Up @@ -413,23 +413,28 @@ def common_creation(points, gpt=None):
return gpt

def new_grp(event):
" * Right-click (click+n): try an auto find for a ring"
points = self.massif.find_peaks([event.ydata, event.xdata],
" * new_grp Right-click (click+n): try an auto find for a ring"
# ydata is a float, and matplotlib display pixels centered.
# we use floor (int cast) instead of round to avoid use of
# banker's rounding
ypix, xpix = int(event.ydata + 0.5), int(event.xdata + 0.5)
points = self.massif.find_peaks([ypix, xpix],
self.defaultNbPoints,
None, self.massif_contour)
if points:
gpt = common_creation(points)
annontate(points[0], [event.ydata, event.xdata], gpt=gpt)
annontate(points[0], [ypix, xpix], gpt=gpt)
logger.info("Created group #%2s with %i points", gpt.label, len(gpt))
else:
logger.warning("No peak found !!!")

def single_point(event):
" * Right-click + Ctrl (click+b): create new group with one single point"
newpeak = self.massif.nearest_peak([event.ydata, event.xdata])
ypix, xpix = int(event.ydata + 0.5), int(event.xdata + 0.5)
newpeak = self.massif.nearest_peak([ypix, xpix])
if newpeak:
gpt = common_creation([newpeak])
annontate(newpeak, [event.ydata, event.xdata], gpt=gpt)
annontate(newpeak, [ypix, xpix], gpt=gpt)
logger.info("Create group #%2s with single point x=%5.1f, y=%5.1f", gpt.label, newpeak[1], newpeak[0])
else:
logger.warning("No peak found !!!")
Expand All @@ -445,8 +450,10 @@ def append_more_points(event):
self.ax.lines.remove(gpt.plot[0])

update_fig(self.fig)
# matplotlib coord to pixel coord, avoinding use of banker's round
ypix, xpix = int(event.ydata + 0.5), int(event.xdata + 0.5)
# need to annotate only if a new group:
listpeak = self.massif.find_peaks([event.ydata, event.xdata],
listpeak = self.massif.find_peaks([ypix, xpix],
self.defaultNbPoints, None,
self.massif_contour)
if listpeak:
Expand All @@ -466,7 +473,9 @@ def append_1_point(event):
if gpt.plot[0] in self.ax.lines:
self.ax.lines.remove(gpt.plot[0])
update_fig(self.fig)
newpeak = self.massif.nearest_peak([event.ydata, event.xdata])
# matplotlib coord to pixel coord, avoinding use of banker's round
ypix, xpix = int(event.ydata + 0.5), int(event.xdata + 0.5)
newpeak = self.massif.nearest_peak([ypix, xpix])
if newpeak:
gpt.points.append(newpeak)
logger.info("x=%5.1f, y=%5.1f added to group #%2s", newpeak[1], newpeak[0], gpt.label)
Expand Down Expand Up @@ -511,8 +520,8 @@ def erase_1_point(event):
self.ax.lines.remove(gpt.plot[0])
if len(gpt) > 1:
# delete single closest point from current group
x0 = event.xdata
y0 = event.ydata
# matplotlib coord to pixel coord, avoinding use of banker's round
y0, x0 = int(event.ydata + 0.5), int(event.xdata + 0.5)
distsq = [((p[1] - x0) ** 2 + (p[0] - y0) ** 2) for p in gpt.points]
# index and distance of smallest distance:
indexMin = min(enumerate(distsq), key=operator.itemgetter(1))
Expand Down

0 comments on commit b299a7b

Please sign in to comment.