LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
_syntax.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2015 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 """
23 Special Python syntactic sugar for Catalogs and Records.
24 
25 This module is imported by tableLib.py, and should not need to be imported by any other module.
26 I've moved the code out of the .i file here to avoid recompiling when only pure-Python code is
27 changed.
28 """
29 
30 import fnmatch
31 import re
32 import numpy
33 import collections
34 
35 def Schema_extract(self, *patterns, **kwds):
36  """
37  Extract a dictionary of {<name>: <schema-item>} in which the field names
38  match the given shell-style glob pattern(s).
39 
40  Any number of glob patterns may be passed; the result will be the union of all
41  the result of each glob considered separately.
42 
43  Additional optional arguments may be passed as keywords:
44 
45  regex ------ A regular expression to be used in addition to any glob patterns passed
46  as positional arguments. Note that this will be compared with re.match,
47  not re.search.
48 
49  sub -------- A replacement string template (see re.MatchObject.expand) used to set the
50  dictionary keys of any fields matched by regex. The field name in the
51  SchemaItem is not modified.
52 
53  ordered----- If True, a collections.OrderedDict will be returned instead of a standard
54  dict, with the order corresponding to the definition order of the Schema.
55 
56  """
57  if kwds.pop("ordered", False):
58  d = collections.OrderedDict()
59  else:
60  d = dict()
61  regex = kwds.pop("regex", None)
62  sub = kwds.pop("sub", None)
63  if sub is not None and regex is None:
64  raise ValueError("'sub' keyword argument to extract is invalid without 'regex' argument")
65  if kwds:
66  raise ValueError("Unrecognized keyword arguments for extract: %s" % ", ".join(kwds.keys()))
67  for item in self:
68  trueName = item.field.getName()
69  names = [trueName]
70  for alias, target in self.getAliasMap().iteritems():
71  if trueName.startswith(target):
72  names.append(trueName.replace(target, alias, 1))
73  for name in names:
74  if regex is not None:
75  m = re.match(regex, name)
76  if m is not None:
77  if sub is not None:
78  name = m.expand(sub)
79  d[name] = item
80  continue # continue middle loop so we don't match the same name twice
81  for pattern in patterns:
82  if fnmatch.fnmatchcase(name, pattern):
83  d[name] = item
84  break # break inner loop so we don't match the same name twice
85  return d
86 
87 def BaseRecord_extract(self, *patterns, **kwds):
88  """
89  Extract a dictionary of {<name>: <field-value>} in which the field names
90  match the given shell-style glob pattern(s).
91 
92  Any number of glob patterns may be passed; the result will be the union of all
93  the result of each glob considered separately.
94 
95  Additional optional arguments may be passed as keywords:
96 
97  items ------ The result of a call to self.schema.extract(); this will be used instead
98  of doing any new matching, and allows the pattern matching to be reused
99  to extract values from multiple records. This keyword is incompatible
100  with any position arguments and the regex, sub, and ordered keyword
101  arguments.
102 
103  split ------ If True, fields with named subfields (e.g. points) will be split into
104  separate items in the dict; instead of {"point": lsst.afw.geom.Point2I(2,3)},
105  for instance, you'd get {"point.x": 2, "point.y": 3}.
106  Default is False.
107 
108  regex ------ A regular expression to be used in addition to any glob patterns passed
109  as positional arguments. Note that this will be compared with re.match,
110  not re.search.
111 
112  sub -------- A replacement string (see re.MatchObject.expand) used to set the
113  dictionary keys of any fields matched by regex.
114 
115  ordered----- If True, a collections.OrderedDict will be returned instead of a standard
116  dict, with the order corresponding to the definition order of the Schema.
117  Default is False.
118 
119  """
120  d = kwds.pop("items", None)
121  split = kwds.pop("split", False)
122  if d is None:
123  d = self.schema.extract(*patterns, **kwds).copy()
124  elif kwds:
125  raise ValueError("Unrecognized keyword arguments for extract: %s" % ", ".join(kwds.keys()))
126  for name, schemaItem in d.items(): # can't use iteritems because we might be adding/deleting elements
127  key = schemaItem.key
128  if split and key.HAS_NAMED_SUBFIELDS:
129  for subname, subkey in zip(key.subfields, key.subkeys):
130  d["%s.%s" % (name, subname)] = self.get(subkey)
131  del d[name]
132  else:
133  d[name] = self.get(schemaItem.key)
134  return d
135 
136 def BaseColumnView_extract(self, *patterns, **kwds):
137  """
138  Extract a dictionary of {<name>: <column-array>} in which the field names
139  match the given shell-style glob pattern(s).
140 
141  Any number of glob patterns may be passed; the result will be the union of all
142  the result of each glob considered separately.
143 
144  Note that extract("*", copy=True) provides an easy way to transform a row-major
145  ColumnView into a possibly more efficient set of contiguous NumPy arrays.
146 
147  This routines unpacks Flag columns into full boolean arrays and covariances into dense
148  (i.e. non-triangular packed) arrays with dimension (N,M,M), where N is the number of
149  records and M is the dimension of the covariance matrix. Fields with named subfields
150  (e.g. points) are always split into separate dictionary items, as is done in
151  BaseRecord.extract(..., split=True). String fields are silently ignored.
152 
153  Additional optional arguments may be passed as keywords:
154 
155  items ------ The result of a call to self.schema.extract(); this will be used instead
156  of doing any new matching, and allows the pattern matching to be reused
157  to extract values from multiple records. This keyword is incompatible
158  with any position arguments and the regex, sub, and ordered keyword
159  arguments.
160 
161  where ------ Any expression that can be passed as indices to a NumPy array, including
162  slices, boolean arrays, and index arrays, that will be used to index
163  each column array. This is applied before arrays are copied when
164  copy is True, so if the indexing results in an implicit copy no
165  unnecessary second copy is performed.
166 
167  copy ------- If True, the returned arrays will be contiguous copies rather than strided
168  views into the catalog. This ensures that the lifetime of the catalog is
169  not tied to the lifetime of a particular catalog, and it also may improve
170  the performance if the array is used repeatedly.
171  Default is False.
172 
173  regex ------ A regular expression to be used in addition to any glob patterns passed
174  as positional arguments. Note that this will be compared with re.match,
175  not re.search.
176 
177  sub -------- A replacement string (see re.MatchObject.expand) used to set the
178  dictionary keys of any fields matched by regex.
179 
180  ordered----- If True, a collections.OrderedDict will be returned instead of a standard
181  dict, with the order corresponding to the definition order of the Schema.
182  Default is False.
183 
184  """
185  copy = kwds.pop("copy", False)
186  where = kwds.pop("where", None)
187  d = kwds.pop("items", None)
188  if d is None:
189  d = self.schema.extract(*patterns, **kwds).copy()
190  elif kwds:
191  raise ValueError("Unrecognized keyword arguments for extract: %s" % ", ".join(kwds.keys()))
192  def processArray(a):
193  if where is not None:
194  a = a[where]
195  if copy:
196  a = numpy.ascontiguousarray(a)
197  return a
198  for name, schemaItem in d.items(): # can't use iteritems because we might be adding/deleting elements
199  key = schemaItem.key
200  if key.getTypeString() == "String":
201  del d[name]
202  else:
203  d[name] = processArray(self.get(schemaItem.key))
204  return d
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390