Skip to content

Commit

Permalink
clean up using rel-units
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasaarholt committed Jan 29, 2021
1 parent 082de07 commit 3c92e23
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions hyperspy/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ def add_widget(self, signal, axes=None, widget=None,
signal.axes_manager, **kwargs)
widget.color = color

# Remove existing ROI, if it exsists and axes match
# Remove existing ROI, if it exists and axes match
if signal in self.signal_map and \
self.signal_map[signal][1] == axes:
self.remove_widget(signal)
Expand Down Expand Up @@ -572,8 +572,7 @@ def guess_vertical_or_horizontal(axes, signal):
"Could not find valid widget type for the given `axes` value")

def get_central_half_limits_of_axis(ax):
diff = ax.high_value - ax.low_value
return (ax.low_value + diff/4, ax.low_value + 3*diff/4)
return ax._parse_value("rel0.25"), ax._parse_value("rel0.75")

@add_gui_method(toolkey="hyperspy.Point1DROI")
class Point1DROI(BasePointROI):
Expand Down Expand Up @@ -606,11 +605,10 @@ def __getitem__(self, *args, **kwargs):
return _tuple.__getitem__(*args, **kwargs)

def _set_default_attributes(self, signal):
axes = self._parse_axes(None, signal.axes_manager)
ax0, *_ = self._parse_axes(None, signal.axes_manager)
# If roi attributes are undefined, use center of axes
if t.Undefined in self._get_attributes():
l, h = axes[0].low_value, axes[0].high_value
self.value = l + (h - l)/2
self.value = ax0._parse_value('rel0.5')

def _get_attributes(self):
return (self.value, )
Expand All @@ -625,7 +623,6 @@ def _get_ranges(self):
ranges = ((self.value,),)
return ranges


def _set_from_widget(self, widget):
self.value = widget.position[0]

Expand Down Expand Up @@ -682,13 +679,11 @@ def __getitem__(self, *args, **kwargs):
return _tuple.__getitem__(*args, **kwargs)

def _set_default_attributes(self, signal):
axes = self._parse_axes(None, signal.axes_manager)
ax0, ax1 = self._parse_axes(None, signal.axes_manager)
# If roi attributes are undefined, use center of axes
if t.Undefined in self._get_attributes():
l, h = axes[0].low_value, axes[0].high_value
self.x = l + (h - l)/2
l, h = axes[1].low_value, axes[1].high_value
self.y = l + (h - l)/2
self.x = ax0._parse_value("rel0.5")
self.y = ax1._parse_value("rel0.5")

def _get_attributes(self):
return self.x, self.y
Expand Down Expand Up @@ -754,10 +749,10 @@ def __getitem__(self, *args, **kwargs):
return _tuple.__getitem__(*args, **kwargs)

def _set_default_attributes(self, signal):
axes = self._parse_axes(None, signal.axes_manager)
ax0, *_ = self._parse_axes(None, signal.axes_manager)
# If roi attributes are undefined, use center of axes
if t.Undefined in self._get_attributes():
self.left, self.right = get_central_half_limits_of_axis(axes[0])
self.left, self.right = get_central_half_limits_of_axis(ax0)

def _get_attributes(self):
return self.left, self.right
Expand Down Expand Up @@ -844,11 +839,11 @@ def _set_default_attributes(self, signal):
# Need to turn of bounds checking or undefined values trigger error
old_bounds_check = self._bounds_check
self._bounds_check = False
axes = self._parse_axes(None, signal.axes_manager)
ax0, ax1 = self._parse_axes(None, signal.axes_manager)
# If roi attributes are undefined, use center of axes
if t.Undefined in self._get_attributes():
self.left, self.right = get_central_half_limits_of_axis(axes[0])
self.top, self.bottom = get_central_half_limits_of_axis(axes[1])
self.left, self.right = get_central_half_limits_of_axis(ax0)
self.top, self.bottom = get_central_half_limits_of_axis(ax1)
self._bounds_check = old_bounds_check

def _get_attributes(self):
Expand Down Expand Up @@ -1008,16 +1003,15 @@ def __getitem__(self, *args, **kwargs):
return _tuple.__getitem__(*args, **kwargs)

def _set_default_attributes(self, signal):
axes = self._parse_axes(None, signal.axes_manager)
ax0, ax1 = self._parse_axes(None, signal.axes_manager)
# If roi attributes are undefined, use center of axes
if t.Undefined in (self.cx, self.cy, self.r):
l, h = axes[0].low_value, axes[0].high_value
rx = (h - l)/2
self.cx = l + rx
l, h = axes[1].low_value, axes[1].high_value
ry = (h - l)/2
self.cy = l + ry
self.r = np.min([rx,ry])
self.cx = ax0._parse_value('rel0.5')
self.cy = ax1._parse_value('rel0.5')

rx = (ax0.high_value - ax0.low_value)/2
ry = (ax1.high_value - ax1.low_value)/2
self.r = min(rx,ry)

def _get_attributes(self):
# should really get r_inner here, but it is
Expand Down Expand Up @@ -1200,11 +1194,11 @@ def __getitem__(self, *args, **kwargs):
return _tuple.__getitem__(*args, **kwargs)

def _set_default_attributes(self, signal):
axes = self._parse_axes(None, signal.axes_manager)
ax0, ax1 = self._parse_axes(None, signal.axes_manager)
# If roi attributes are undefined, use center of axes
if t.Undefined in (self.x1, self.y1, self.x2, self.y2):
self.x1, self.x2 = get_central_half_limits_of_axis(axes[0])
self.y1, self.y2 = get_central_half_limits_of_axis(axes[1])
self.x1, self.x2 = get_central_half_limits_of_axis(ax0)
self.y1, self.y2 = get_central_half_limits_of_axis(ax1)

def _get_attributes(self):
return self.x1, self.y1, self.x2, self.y2, self.linewidth
Expand Down

0 comments on commit 3c92e23

Please sign in to comment.