LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Public Member Functions | List of all members
astshim.test.MappingTestCase Class Reference
Inheritance diagram for astshim.test.MappingTestCase:
astshim.test.ObjectTestCase

Public Member Functions

def checkRoundTrip (self, amap, poslist, rtol=1e-05, atol=1e-08)
 
def checkBasicSimplify (self, amap)
 
def checkMappingPersistence (self, amap, poslist)
 
def checkMemoryForCompoundObject (self, obj1, obj2, cmpObj, isSeries)
 
def assertObjectsIdentical (self, obj1, obj2, checkType=True)
 
def checkCopy (self, obj)
 
def checkPersistence (self, obj, typeFromChannel=None)
 

Detailed Description

Base class for unit tests of mappings

Definition at line 92 of file test.py.

Member Function Documentation

◆ assertObjectsIdentical()

def astshim.test.ObjectTestCase.assertObjectsIdentical (   self,
  obj1,
  obj2,
  checkType = True 
)
inherited
Assert that two astshim objects are identical.

Identical means the objects are of the same class (if checkType)
and all properties are identical (including whether set or defaulted).

Definition at line 18 of file test.py.

18  def assertObjectsIdentical(self, obj1, obj2, checkType=True):
19  """Assert that two astshim objects are identical.
20 
21  Identical means the objects are of the same class (if checkType)
22  and all properties are identical (including whether set or defaulted).
23  """
24  if checkType:
25  self.assertIs(type(obj1), type(obj2))
26  self.assertEqual(obj1.show(), obj2.show())
27  self.assertEqual(str(obj1), str(obj2))
28  self.assertEqual(repr(obj1), repr(obj2))
29 
table::Key< int > type
Definition: Detector.cc:163

◆ checkBasicSimplify()

def astshim.test.MappingTestCase.checkBasicSimplify (   self,
  amap 
)
Check basic simplfication for a reversible mapping

Check the following:
- A compound mapping of a amap and its inverse simplifies to
  a unit amap.
- A compound mapping of a amap and a unit amap simplifies to
  the original amap.

Definition at line 135 of file test.py.

135  def checkBasicSimplify(self, amap):
136  """Check basic simplfication for a reversible mapping
137 
138  Check the following:
139  - A compound mapping of a amap and its inverse simplifies to
140  a unit amap.
141  - A compound mapping of a amap and a unit amap simplifies to
142  the original amap.
143  """
144  amapinv = amap.inverted()
145  cmp1 = amap.then(amapinv)
146  unit1 = cmp1.simplified()
147  self.assertEqual(unit1.className, "UnitMap")
148  self.assertEqual(amap.nIn, cmp1.nIn)
149  self.assertEqual(amap.nIn, cmp1.nOut)
150  self.assertEqual(cmp1.nIn, unit1.nIn)
151  self.assertEqual(cmp1.nOut, unit1.nOut)
152 
153  cmp2 = amapinv.then(amap)
154  unit2 = cmp2.simplified()
155  self.assertEqual(unit2.className, "UnitMap")
156  self.assertEqual(amapinv.nIn, cmp2.nIn)
157  self.assertEqual(amapinv.nIn, cmp2.nOut)
158  self.assertEqual(cmp2.nIn, unit2.nIn)
159  self.assertEqual(cmp2.nOut, unit2.nOut)
160 
161  for ma, mb, desmap3 in (
162  (unit1, amap, amap),
163  (amap, unit2, amap),
164  (unit2, amapinv, amapinv),
165  (amapinv, unit1, amapinv),
166  ):
167  cmp3 = ma.then(mb)
168  cmp3simp = cmp3.simplified()
169  self.assertEqual(cmp3simp.className, amap.simplified().className)
170  self.assertEqual(ma.nIn, cmp3.nIn)
171  self.assertEqual(mb.nOut, cmp3.nOut)
172  self.assertEqual(cmp3.nIn, cmp3simp.nIn)
173  self.assertEqual(cmp3.nOut, cmp3simp.nOut)
174 

◆ checkCopy()

def astshim.test.ObjectTestCase.checkCopy (   self,
  obj 
)
inherited
Check that an astshim object can be deep-copied

Definition at line 30 of file test.py.

30  def checkCopy(self, obj):
31  """Check that an astshim object can be deep-copied
32  """
33  nobj = obj.getNObject()
34  nref = obj.getRefCount()
35 
36  def copyIter(obj):
37  yield obj.copy()
38  yield type(obj)(obj)
39 
40  for cp in copyIter(obj):
41  self.assertObjectsIdentical(obj, cp)
42  self.assertEqual(obj.getNObject(), nobj + 1)
43  # Object.copy makes a new pointer instead of copying the old one,
44  # so the reference count of the old one does not increase
45  self.assertEqual(obj.getRefCount(), nref)
46  self.assertFalse(obj.same(cp))
47  self.assertEqual(cp.getNObject(), nobj + 1)
48  self.assertEqual(cp.getRefCount(), 1)
49  # changing an attribute of the copy does not affect the original
50  originalIdent = obj.ident
51  cp.ident = obj.ident + " modified"
52  self.assertEqual(obj.ident, originalIdent)
53 
54  del cp
55  self.assertEqual(obj.getNObject(), nobj)
56  self.assertEqual(obj.getRefCount(), nref)
57 

◆ checkMappingPersistence()

def astshim.test.MappingTestCase.checkMappingPersistence (   self,
  amap,
  poslist 
)
Check that a mapping gives identical answers to unpersisted copy

poslist is a list of input position for a forward transform
    (if it exists), or the inverse transform (if not).
    A numpy array with shape [nAxes, num points]
    or collection that can be cast to same

Checks each direction, if present. However, for generality,
does not check that the two directions are inverses of each other;
call checkRoundTrip for that.

Does everything checkPersistence does, so no need to call both.

Definition at line 175 of file test.py.

175  def checkMappingPersistence(self, amap, poslist):
176  """Check that a mapping gives identical answers to unpersisted copy
177 
178  poslist is a list of input position for a forward transform
179  (if it exists), or the inverse transform (if not).
180  A numpy array with shape [nAxes, num points]
181  or collection that can be cast to same
182 
183  Checks each direction, if present. However, for generality,
184  does not check that the two directions are inverses of each other;
185  call checkRoundTrip for that.
186 
187  Does everything checkPersistence does, so no need to call both.
188  """
189  for channelType, options in (
190  (Channel, ""),
191  (FitsChan, "Encoding=Native"),
192  (XmlChan, ""),
193  ):
194  ss = StringStream()
195  chan = Channel(ss)
196  chan.write(amap)
197  ss.sinkToSource()
198  amap_copy = chan.read()
199  self.assertEqual(amap.className, amap_copy.className)
200  self.assertEqual(amap.show(), amap_copy.show())
201  self.assertEqual(str(amap), str(amap_copy))
202  self.assertEqual(repr(amap), repr(amap_copy))
203 
204  if amap.hasForward:
205  outPoslist = amap.applyForward(poslist)
206  assert_array_equal(outPoslist, amap_copy.applyForward(poslist))
207 
208  if amap.hasInverse:
209  assert_array_equal(amap.applyInverse(outPoslist),
210  amap_copy.applyInverse(outPoslist))
211 
212  elif amap.hasInverse:
213  assert_array_equal(amap.applyInverse(poslist),
214  amap_copy.applyInverse(poslist))
215 
216  else:
217  raise RuntimeError("mapping has neither forward nor inverse transform")
218 

◆ checkMemoryForCompoundObject()

def astshim.test.MappingTestCase.checkMemoryForCompoundObject (   self,
  obj1,
  obj2,
  cmpObj,
  isSeries 
)
Check the memory usage for a compoundObject

obj1: first object in compound object
obj2: second object in compound object
cmpObj: compound object (SeriesMap, ParallelMap, CmpMap or CmpFrame)
isSeries: is compound object in series? None to not test
  (e.g. CmpFrame)

Definition at line 219 of file test.py.

219  def checkMemoryForCompoundObject(self, obj1, obj2, cmpObj, isSeries):
220  """Check the memory usage for a compoundObject
221 
222  obj1: first object in compound object
223  obj2: second object in compound object
224  cmpObj: compound object (SeriesMap, ParallelMap, CmpMap or CmpFrame)
225  isSeries: is compound object in series? None to not test
226  (e.g. CmpFrame)
227  """
228  # if obj1 and obj2 are the same type then copying the compound object
229  # will increase the NObject of each by 2, otherwise 1
230  deltaObj = 2 if type(obj1) == type(obj2) else 1
231 
232  initialNumObj1 = obj1.getNObject()
233  initialNumObj2 = obj2.getNObject()
234  initialNumCmpObj = cmpObj.getNObject()
235  initialRefCountObj1 = obj1.getRefCount()
236  initialRefCountObj2 = obj2.getRefCount()
237  initialRefCountCmpObj = cmpObj.getRefCount()
238  self.assertEqual(obj1.getNObject(), initialNumObj1)
239  self.assertEqual(obj2.getNObject(), initialNumObj2)
240  if isSeries is not None:
241  if isSeries is True:
242  self.assertTrue(cmpObj.series)
243  elif isSeries is False:
244  self.assertFalse(cmpObj.series)
245 
246  # Making a deep copy should increase the object count of the contained
247  # objects but should not affect the reference count.
248  cp = cmpObj.copy()
249  self.assertEqual(cmpObj.getRefCount(), initialRefCountCmpObj)
250  self.assertEqual(cmpObj.getNObject(), initialNumCmpObj + 1)
251  self.assertEqual(obj1.getRefCount(), initialRefCountObj1)
252  self.assertEqual(obj2.getRefCount(), initialRefCountObj2)
253  self.assertEqual(obj1.getNObject(), initialNumObj1 + deltaObj)
254  self.assertEqual(obj2.getNObject(), initialNumObj2 + deltaObj)
255 
256  # deleting the deep copy should restore ref count and nobject
257  del cp
258  self.assertEqual(cmpObj.getRefCount(), initialRefCountCmpObj)
259  self.assertEqual(cmpObj.getNObject(), initialNumCmpObj)
260  self.assertEqual(obj1.getRefCount(), initialRefCountObj1)
261  self.assertEqual(obj1.getNObject(), initialNumObj1)
262  self.assertEqual(obj2.getRefCount(), initialRefCountObj2)
263  self.assertEqual(obj2.getNObject(), initialNumObj2)
264 
265 

◆ checkPersistence()

def astshim.test.ObjectTestCase.checkPersistence (   self,
  obj,
  typeFromChannel = None 
)
inherited
Check that an astshim object can be persisted and unpersisted

@param[in] obj  Object to be checked
@param[in] typeFromChannel  Type of object expected to be read from
                a channel (since some thin wrapper types are read
                as the underlying type); None if the original type

Check persistence using Channel, FitsChan (with native encoding,
as the only encoding compatible with all AST objects), XmlChan
and pickle.

Definition at line 58 of file test.py.

58  def checkPersistence(self, obj, typeFromChannel=None):
59  """Check that an astshim object can be persisted and unpersisted
60 
61  @param[in] obj Object to be checked
62  @param[in] typeFromChannel Type of object expected to be read from
63  a channel (since some thin wrapper types are read
64  as the underlying type); None if the original type
65 
66  Check persistence using Channel, FitsChan (with native encoding,
67  as the only encoding compatible with all AST objects), XmlChan
68  and pickle.
69  """
70  for channelType, options in (
71  (Channel, ""),
72  (FitsChan, "Encoding=Native"),
73  (XmlChan, ""),
74  ):
75  ss = StringStream()
76  chan = channelType(ss, options)
77  chan.write(obj)
78  ss.sinkToSource()
79  if channelType is FitsChan:
80  chan.clearCard()
81  obj_copy = chan.read()
82  if typeFromChannel is not None:
83  self.assertIs(type(obj_copy), typeFromChannel)
84  self.assertObjectsIdentical(obj, obj_copy, checkType=False)
85  else:
86  self.assertObjectsIdentical(obj, obj_copy)
87 
88  obj_copy = pickle.loads(pickle.dumps(obj))
89  self.assertObjectsIdentical(obj, obj_copy)
90 
91 

◆ checkRoundTrip()

def astshim.test.MappingTestCase.checkRoundTrip (   self,
  amap,
  poslist,
  rtol = 1e-05,
  atol = 1e-08 
)
Check that a mapping's reverse transform is the opposite of forward

amap is the mapping to test
poslist is a list of input position for a forward transform;
    a numpy array with shape [nin, num points]
    or collection that can be cast to same
rtol is the relative tolerance for numpy.testing.assert_allclose
atol is the absolute tolerance for numpy.testing.assert_allclose

Definition at line 97 of file test.py.

97  def checkRoundTrip(self, amap, poslist, rtol=1e-05, atol=1e-08):
98  """Check that a mapping's reverse transform is the opposite of forward
99 
100  amap is the mapping to test
101  poslist is a list of input position for a forward transform;
102  a numpy array with shape [nin, num points]
103  or collection that can be cast to same
104  rtol is the relative tolerance for numpy.testing.assert_allclose
105  atol is the absolute tolerance for numpy.testing.assert_allclose
106  """
107  poslist = np.array(poslist, dtype=float)
108  if len(poslist.shape) == 1:
109  # supplied data was a single list of points
110  poslist.shape = (1, len(poslist))
111  # forward with applyForward, inverse with applyInverse
112  to_poslist = amap.applyForward(poslist)
113  rt_poslist = amap.applyInverse(to_poslist)
114  assert_allclose(poslist, rt_poslist, rtol=rtol, atol=atol)
115 
116  # forward with applyForward, inverse with inverted().applyForward
117  amapinv = amap.inverted()
118  rt2_poslist = amapinv.applyForward(to_poslist)
119  assert_allclose(poslist, rt2_poslist, rtol=rtol, atol=atol)
120 
121  # forward and inverse with a compound map of amap.then(amap.inverted())
122  acmp = amap.then(amapinv)
123  assert_allclose(poslist, acmp.applyForward(poslist), rtol=rtol, atol=atol)
124 
125  # test vector versions of forward and inverse
126  posvec = list(poslist.flat)
127  to_posvec = amap.applyForward(posvec)
128  # cast to_poslist to np.array because if poslist has 1 axis then
129  # a list is returned, which has no `flat` attribute
130  assert_allclose(to_posvec, list(to_poslist.flat), rtol=rtol, atol=atol)
131 
132  rt_posvec = amap.applyInverse(to_posvec)
133  assert_allclose(posvec, rt_posvec, rtol=rtol, atol=atol)
134 
daf::base::PropertyList * list
Definition: fits.cc:913

The documentation for this class was generated from the following file: