xboa
_bounding_ellipse.py
Go to the documentation of this file.
1 #This file is a part of xboa
2 #
3 #xboa is free software: you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation, either version 3 of the License, or
6 #(at your option) any later version.
7 #
8 #xboa is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 #GNU General Public License for more details.
12 #
13 #You should have received a copy of the GNU General Public License
14 #along with xboa in the doc folder. If not, see
15 #<http://www.gnu.org/licenses/>.
16 
17 """
18 BoundingEllipse class should be imported directly from xboa.bunch.weighting
19 """
20 
21 try:
22  import numpy
23 except ImportError:
24  pass
25 
26 import xboa.common as common
27 
28 class BoundingEllipse(object):
29  """
30  The BoundingEllipse class defines an arbitrary dimensional ellipse that can
31  be taken as a boundary for VoronoiWeighting.
32 
33  The bounding ellipse is defined by the locus of points \f$\vec{x}\f$ \n
34  \f$ (\vec{x}-\bar{x})^T \mathbf{V^{-1}} (\vec{x}-\bar{x}) = 1 \f$\n
35  where \f$\mathbf{V}\f$ is a matrix defining the ellipse orientation and
36  \f$ \bar{x} \f$ is a vector defining the ellipse centroid. The
37  BoundingEllipse can be used to eliminate points outside of the ellipse
38  and define a (finite) set of points distributed about the ellipse boundary.
39  """
40 
41  def __init__(self, limit_ellipse, limit_mean, limit_n_per_dim):
42  """
43  Initialise the bounding ellipse
44  - limit_ellipse: defines the ellipse. Should be a numpy.array with shape
45  (dimension, dimension).
46  - limit_mean: defines the ellipse centroid. Should be a numpy.array with
47  shape (dimension).
48  - limit_n_per_dim: integer that defines the number \f$n\f$ of points on
49  the ellipse boundary. xboa will set up points distributed evenly
50  about the ellipse, with the number given by \f$n^{D}\f$ where \f$D\f$
51  is the ellipse dimension.
52  """
53  common.has_numpy()
54  self.dim = numpy.shape(limit_ellipse)[0]
55  if type(limit_n_per_dim) != type(1) or limit_n_per_dim < 1:
56  raise ValueError("limit_n_per_dim should be an integer > 1")
57  if numpy.shape(limit_ellipse) != (self.dim, self.dim):
58  raise ValueError("limit_ellipse shape "+\
59  str(numpy.shape(limit_ellipse))+" should be "+\
60  str((self.dim, self.dim)))
61  if numpy.shape(limit_mean) != (self.dim,):
62  raise ValueError("limit_mean shape "+\
63  str(numpy.shape(limit_mean))+" should be "+\
64  str((self.dim,)))
65  self.bounding_points = common.make_shell(limit_n_per_dim,
66  limit_ellipse)
67  bp_temp = [None]*numpy.shape(self.bounding_points)[0]
68  for i, row in enumerate(self.bounding_points):
69  bp_temp[i] = [None]*self.dim
70  for j in range(self.dim):
71  bp_temp[i][j] = row[0, j]+limit_mean[j]
72  self.bounding_points = numpy.array(bp_temp)
73  self.ellipse_det = numpy.linalg.det(limit_ellipse)
74  self.ellipse_inv = numpy.linalg.inv(limit_ellipse)
75  self.mean = limit_mean
76 
77  def cut_on_bound(self, points_in):
78  """
79  Iterate over the points, and delete items that are outside the ellipse.
80  - points_in. Set of n points in form of a numpy array with shape
81  (n, dimension).
82  Returns a tuple of (points_out, not_cut_indices) where points_out is a
83  numpy array of shape (m, dimension) containing all points that sit on or
84  inside the ellipse boundary and not_cut_indices is a list of integers
85  of length m corresponding to the position in points_in of each of the m
86  points_out.
87  """
88  points = points_in
89  i = 0
90  index = 0
91  not_cut_indices = []
92  while i < len(points):
93  point = points[i]
94  delta = point - self.mean
95  delta_t = numpy.transpose(delta)
96  arg = delta.dot(self.ellipse_inv.dot(delta_t))
97  if arg > 1.:
98  points = numpy.delete(points, i, 0)
99  else:
100  not_cut_indices.append(index)
101  i += 1
102  index += 1
103  return points, not_cut_indices
104 
common module defines common utility data and functions that are used elsewhere
Definition: __init__.py:1
The BoundingEllipse class defines an arbitrary dimensional ellipse that can be taken as a boundary fo...
def cut_on_bound
Iterate over the points, and delete items that are outside the ellipse.