Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-10799: Rename Transform::tranForward to applyForward #20

Merged
merged 1 commit into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/mainpage.dox
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ that support time, spectra and tables have yet been wrapped. The python wrapper
## Differences between this and Starlink AST

- Most functions in AST are classes or methods in this shim.
- `Mapping::tranForward` and `Mapping::tranInverse` methods transform single points or lists of points.
- `Mapping::applyForward` and `Mapping::applyInverse` methods transform single points or lists of points.
These replace AST's `astTran<X>` functions and no `invert` flag is supported.
There are three versions of each method:
- Accept a std::vector<double> and return a new std::vector<double>.
Expand Down
12 changes: 6 additions & 6 deletions examples/basics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ int main() {
<< std::endl;

ast::Array2D to = ndarray::allocate(ndarray::makeVector(3, 2));
zoomMap.tranForward(from, to);
zoomMap.applyForward(from, to);

auto invZoomPtr = zoomMap.getInverse();
ast::Array2D rndTrip = ndarray::allocate(ndarray::makeVector(3, 2));
invZoomPtr->tranForward(to, rndTrip);
invZoomPtr->applyForward(to, rndTrip);
std::cout << "x =" << from << std::endl;
std::cout << "zoom(x) =" << to << std::endl;
std::cout << "round trip =" << rndTrip << std::endl;
Expand All @@ -55,15 +55,15 @@ int main() {
auto shiftMap = ast::ShiftMap(shift);

auto seriesMap = shiftMap.then(zoomMap);
seriesMap.tranForward(from, to);
seriesMap.applyForward(from, to);
std::cout << "\n\nx =" << from << std::endl;
shiftMap.tranForward(from, to);
shiftMap.applyForward(from, to);
std::cout << "shift(x) =" << to << std::endl;
seriesMap.tranForward(from, to);
seriesMap.applyForward(from, to);
std::cout << "zoom(shift(x) =" << to << std::endl;

auto shiftMapCopy = seriesMap[0];
std::cout << "\n\nx =" << from << std::endl;
shiftMapCopy->tranForward(from, to);
shiftMapCopy->applyForward(from, to);
std::cout << "shiftCopy(x) =" << to << std::endl;
}
2 changes: 1 addition & 1 deletion examples/readwcs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main() {
std::vector<double> fromVec = {0, 1000, 0, 1000, 0, 0, 1000, 1000};
ast::Array2D from = ast::arrayFromVector(fromVec, 2);
ast::Array2D to = ndarray::allocate(ndarray::makeVector(4, 2));
frameSetPtr->tranForward(from, to);
frameSetPtr->applyForward(from, to);
std::cout << "\n\npixels =" << from << std::endl;
std::cout << "sky =" << to << std::endl;
}
2 changes: 1 addition & 1 deletion include/astshim/CmpMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CmpMap : public Mapping {
/**
Construct a @ref CmpMap
@param[in] map1 First mapping. When calling @ref Mapping.tranForward
@param[in] map1 First mapping. When calling @ref Mapping.applyForward
the first mapping is applied to the input points in a series mapping,
and to the lower numbered coordinates in a parallel mapping.
@param[in] map2 Second mapping.
Expand Down
18 changes: 9 additions & 9 deletions include/astshim/Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ class Mapping : public Object {

An inverse mapping is a deep copy of a mapping whose @ref Mapping_Invert "Invert" attribute
has been toggled, as indicated by @ref isInverted. This swaps the meaning of "input" and "output",
and of "forward" and "inverse". Thus it swaps the behavior of @ref tranForward and @ref tranInverse,
and of "forward" and "inverse". Thus it swaps the behavior of @ref applyForward and @ref applyInverse,
@ref getNIn and @ref getNOut, @ref hasForward and @ref hasInverse and so on.

Note that the inverse mapping contains exactly the same model coefficients as the original,
but they are used by @ref tranInverse instead of @ref tranForward. Thus for example if a @ref ZoomMap
but they are used by @ref applyInverse instead of @ref applyForward. Thus for example if a @ref ZoomMap
has a zoom factor of 4.0 then its inverse also reports a zoom factor of 4.0 (despite behaving
like an uninverted @ref ZoomMap with zoom factor of 0.25).
*/
Expand Down Expand Up @@ -250,15 +250,15 @@ class Mapping : public Object {
@param[in] from input coordinates, with dimensions (nPts, nIn)
@param[out] to transformed coordinates, with dimensions (nPts, nOut)
*/
void tranForward(ConstArray2D const &from, Array2D const &to) const { _tran(from, true, to); }
void applyForward(ConstArray2D const &from, Array2D const &to) const { _tran(from, true, to); }

/**
Perform a forward transformation on a 2-D array, returning the results as a new array

@param[in] from input coordinates, with dimensions (nPts, nIn)
@return the results as a new array with dimensions (nPts, nOut)
*/
Array2D tranForward(ConstArray2D const &from) const {
Array2D applyForward(ConstArray2D const &from) const {
Array2D to = ndarray::allocate(getNOut(), from.getSize<1>());
_tran(from, true, to);
return to;
Expand All @@ -270,7 +270,7 @@ class Mapping : public Object {
@param[in] from input coordinates as a vector, with axes adjacent, e.g. x0, y0, x1, y1...xn, yn
@return the results as a new vector
*/
std::vector<double> tranForward(std::vector<double> const &from) const {
std::vector<double> applyForward(std::vector<double> const &from) const {
auto fromArr = arrayFromVector(from, getNIn());
std::vector<double> to(fromArr.getSize<1>() * getNOut());
auto toArr = arrayFromVector(to, getNOut());
Expand All @@ -284,15 +284,15 @@ class Mapping : public Object {
@param[in] from input coordinates, with dimensions (nPts, nOut)
@param[out] to transformed coordinates, with dimensions (nPts, nIn)
*/
void tranInverse(ConstArray2D const &from, Array2D const &to) const { _tran(from, false, to); }
void applyInverse(ConstArray2D const &from, Array2D const &to) const { _tran(from, false, to); }

/**
Perform an inverse transformation on a 2-D array, returning the results as a new 2-D array

@param[in] from output coordinates, with dimensions (nPts, nOut)
@return the results as a new array with dimensions (nPts, nIn)
*/
Array2D tranInverse(ConstArray2D const &from) const {
Array2D applyInverse(ConstArray2D const &from) const {
Array2D to = ndarray::allocate(getNIn(), from.getSize<1>());
_tran(from, false, to);
return to;
Expand All @@ -304,7 +304,7 @@ class Mapping : public Object {
@param[in] from input coordinates as a vector, with axes adjacent, e.g. x0, y0, x1, y1...xn, yn
@return the results as a new vector
*/
std::vector<double> tranInverse(std::vector<double> const &from) const {
std::vector<double> applyInverse(std::vector<double> const &from) const {
auto fromArr = arrayFromVector(from, getNOut());
std::vector<double> to(fromArr.getSize<1>() * getNIn());
auto toArr = arrayFromVector(to, getNIn());
Expand Down Expand Up @@ -422,7 +422,7 @@ class Mapping : public Object {

private:
/**
Implement tranForward and tranInverse, putting the results into a pre-allocated 2-D array.
Implement applyForward and applyInverse, putting the results into a pre-allocated 2-D array.

@param[in] from input coordinates, with dimensions (nPts, nIn)
@param[in] doForward if true then perform a forward transform, else inverse
Expand Down
4 changes: 2 additions & 2 deletions include/astshim/TimeMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ class TimeMap : public Mapping {
- Julian Dates and offsets: days

The arguments used in the above conversions are the zero-points
used by the @ref Mapping.tranForward function.
used by the @ref Mapping.applyForward function.
The axis values supplied and returned by
@ref Mapping.tranForward are offsets away from these zero-points:
@ref Mapping.applyForward are offsets away from these zero-points:

- `MJDOFF`: The zero-point being used with MJD values.
- `JDOFF`: The zero-point being used with Julian Date values.
Expand Down
14 changes: 7 additions & 7 deletions python/astshim/mapping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ PYBIND11_PLUGIN(mapping) {
cls.def("under", &Mapping::under, "next"_a);
cls.def("rate", &Mapping::rate, "at"_a, "ax1"_a, "ax2"_a);
cls.def("simplify", &Mapping::simplify);
// wrap the overloads of tranForward, tranInverse, tranGridForward and tranGridInverse that return a new
// wrap the overloads of applyForward, applyInverse, tranGridForward and tranGridInverse that return a new
// result
cls.def("tranForward", (Array2D(Mapping::*)(ConstArray2D const &) const) & Mapping::tranForward,
cls.def("applyForward", (Array2D(Mapping::*)(ConstArray2D const &) const) & Mapping::applyForward,
"from"_a);
cls.def("tranForward",
(std::vector<double>(Mapping::*)(std::vector<double> const &) const) & Mapping::tranForward,
cls.def("applyForward",
(std::vector<double>(Mapping::*)(std::vector<double> const &) const) & Mapping::applyForward,
"from"_a);
cls.def("tranInverse", (Array2D(Mapping::*)(ConstArray2D const &) const) & Mapping::tranInverse,
cls.def("applyInverse", (Array2D(Mapping::*)(ConstArray2D const &) const) & Mapping::applyInverse,
"from"_a);
cls.def("tranInverse",
(std::vector<double>(Mapping::*)(std::vector<double> const &) const) & Mapping::tranInverse,
cls.def("applyInverse",
(std::vector<double>(Mapping::*)(std::vector<double> const &) const) & Mapping::applyInverse,
"from"_a);
cls.def("tranGridForward",
(Array2D(Mapping::*)(PointI const &, PointI const &, double, int, int) const) &
Expand Down
16 changes: 8 additions & 8 deletions python/astshim/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,28 @@ def checkRoundTrip(self, amap, poslist, rtol=1e-05, atol=1e-08):
if len(poslist.shape) == 1:
# supplied data was a single list of points
poslist.shape = (1, len(poslist))
# forward with tranForward, inverse with tranInverse
to_poslist = amap.tranForward(poslist)
rt_poslist = amap.tranInverse(to_poslist)
# forward with applyForward, inverse with applyInverse
to_poslist = amap.applyForward(poslist)
rt_poslist = amap.applyInverse(to_poslist)
assert_allclose(poslist, rt_poslist, rtol=rtol, atol=atol)

# forward with tranForward, inverse with getInverse().tranForward
# forward with applyForward, inverse with getInverse().applyForward
amapinv = amap.getInverse()
rt2_poslist = amapinv.tranForward(to_poslist)
rt2_poslist = amapinv.applyForward(to_poslist)
assert_allclose(poslist, rt2_poslist, rtol=rtol, atol=atol)

# forward and inverse with a compound map of amap.then(amap.getInverse())
acmp = amap.then(amapinv)
assert_allclose(poslist, acmp.tranForward(poslist), rtol=rtol, atol=atol)
assert_allclose(poslist, acmp.applyForward(poslist), rtol=rtol, atol=atol)

# test vector versions of forward and inverse
posvec = list(poslist.flat)
to_posvec = amap.tranForward(posvec)
to_posvec = amap.applyForward(posvec)
# cast to_poslist to np.array because if poslist has 1 axis then
# a list is returned, which has no `flat` attribute
assert_allclose(to_posvec, list(to_poslist.flat), rtol=rtol, atol=atol)

rt_posvec = amap.tranInverse(to_posvec)
rt_posvec = amap.applyInverse(to_posvec)
assert_allclose(posvec, rt_posvec, rtol=rtol, atol=atol)

def checkBasicSimplify(self, amap):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ def test_AppendEffect(self):
append(set2, set1)

x = [1.2, 3.4]
y_merged = set12.tranForward(x)
y_separate = set2.tranForward(set1.tranForward(x))
y_merged = set12.applyForward(x)
y_separate = set2.applyForward(set1.applyForward(x))
assert_allclose(y_merged, y_separate)

y = [-0.3]
x_merged = set12.tranInverse(y)
x_separate = set1.tranInverse(set2.tranInverse(y))
x_merged = set12.applyInverse(y)
x_separate = set1.applyInverse(set2.applyInverse(y))
assert_allclose(x_merged, x_separate)

# No side effects
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_AppendIndependent(self):

nTotal = set12.nFrame
x = [1.2, 3.4, 5.6]
y = set12.tranForward(x)
y = set12.applyForward(x)

set1.addFrame(2, makeTwoWayPolyMap(4, 2), Frame(2, "Ident=extra"))
set1.addFrame(1, makeTwoWayPolyMap(3, 3), Frame(3, "Ident=legume"))
Expand All @@ -120,7 +120,7 @@ def test_AppendIndependent(self):

# Use exact equality because nothing should change
self.assertEquals(set12.nFrame, nTotal)
self.assertEquals(set12.tranForward(x), y)
self.assertEquals(set12.applyForward(x), y)

def test_AppendMismatch(self):
"""Check that append behaves as expected when joining non-identical frames.
Expand All @@ -132,8 +132,8 @@ def test_AppendMismatch(self):
set12 = append(set1, set2)

x = [1.2, 3.4, 5.6]
y_merged = set12.tranForward(x)
y_separate = set2.tranForward(set1.tranForward(x))
y_merged = set12.applyForward(x)
y_separate = set2.applyForward(set1.applyForward(x))
assert_allclose(y_merged, y_separate)

iFrom = set1.current
Expand Down
40 changes: 20 additions & 20 deletions tests/test_chebyMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def normalize(inArray, lbnd, ubnd):
----------
inArray : `numpy.array` of float
Value(s) to normalize; a list of nAxes x nPoints values
(the form used by astshim.Mapping.tranForward)
(the form used by astshim.Mapping.applyForward)
lbnd : sequence of `float`
Lower bounds (one element per axis)
ubnd : sequence of `float`
Expand Down Expand Up @@ -48,8 +48,8 @@ def __init__(self, referenceCheby, lbnd, ubnd):
referenceCheby : callable
A function that takes a normalized point (as a list of floats)
that has been normalized to the range [-1, 1]
and returns the expected results from ChebyPoly.tranForward
or tranInverse for the corresponding un-normalized point
and returns the expected results from ChebyPoly.applyForward
or applyInverse for the corresponding un-normalized point
lbnd : list of float
Lower bounds of inputs (for normalization)
ubnd : list of float
Expand All @@ -65,8 +65,8 @@ def transform(self, inArray):
Parameters
----------
inArray : `numpy.array`
Input array of points in the form used by ChebyMap.tranForward
or tranInverse.
Input array of points in the form used by ChebyMap.applyForward
or applyInverse.

Returns
-------
Expand Down Expand Up @@ -153,10 +153,10 @@ def referenceFunc(point):
npt.assert_allclose(domain1.lbnd, lbnd_f)
npt.assert_allclose(domain1.ubnd, ubnd_f)

outdata = chebyMap1.tranForward(indata)
outdata = chebyMap1.applyForward(indata)

with self.assertRaises(RuntimeError):
chebyMap1.tranInverse(indata)
chebyMap1.applyInverse(indata)

pred_outdata = refCheby.transform(indata)
npt.assert_allclose(outdata, pred_outdata)
Expand All @@ -177,11 +177,11 @@ def referenceFunc(point):
npt.assert_allclose(domain2.lbnd, lbnd_f)
npt.assert_allclose(domain2.ubnd, ubnd_f)

outdata2 = chebyMap2.tranForward(indata)
outdata2 = chebyMap2.applyForward(indata)
npt.assert_allclose(outdata2, outdata)

with self.assertRaises(RuntimeError):
chebyMap2.tranInverse(indata)
chebyMap2.applyInverse(indata)

# bidirectional constructor, inverse only specified
chebyMap3 = astshim.ChebyMap(null_coeff, coeff_f, [], [], lbnd_f, ubnd_f)
Expand All @@ -196,11 +196,11 @@ def referenceFunc(point):
npt.assert_allclose(domain3.lbnd, lbnd_f)
npt.assert_allclose(domain3.ubnd, ubnd_f)

outdata3 = chebyMap3.tranInverse(indata)
outdata3 = chebyMap3.applyInverse(indata)
npt.assert_allclose(outdata3, outdata)

with self.assertRaises(RuntimeError):
chebyMap3.tranForward(indata)
chebyMap3.applyForward(indata)

def test_ChebyMapBidirectional(self):
"""Test a ChebyMap with separate forward and inverse mappings
Expand Down Expand Up @@ -275,12 +275,12 @@ def referenceFunc_i(point):
self.checkCopy(chebyMap)
self.checkPersistence(chebyMap)

outdata_f = chebyMap.tranForward(indata_f)
outdata_f = chebyMap.applyForward(indata_f)
des_outdata_f = refCheby_f.transform(indata_f)

npt.assert_allclose(outdata_f, des_outdata_f)

outdata_i = chebyMap.tranInverse(indata_i)
outdata_i = chebyMap.applyInverse(indata_i)
des_outdata_i = refCheby_i.transform(indata_i)

npt.assert_allclose(outdata_i, des_outdata_i)
Expand Down Expand Up @@ -336,7 +336,7 @@ def referenceFunc(point):
self.assertTrue(chebyMap1.hasForward)
self.assertFalse(chebyMap1.hasInverse)

outdata = chebyMap1.tranForward(indata)
outdata = chebyMap1.applyForward(indata)

referenceCheby = ReferenceCheby(referenceFunc, lbnd_f, ubnd_f)
des_outdata = referenceCheby.transform(indata)
Expand All @@ -349,8 +349,8 @@ def referenceFunc(point):
self.assertTrue(chebyMap2.hasForward)
self.assertTrue(chebyMap2.hasInverse)
# forward should be identical to the original
npt.assert_equal(chebyMap2.tranForward(indata), outdata)
roundTripIn2 = chebyMap2.tranInverse(outdata)
npt.assert_equal(chebyMap2.applyForward(indata), outdata)
roundTripIn2 = chebyMap2.applyInverse(outdata)
npt.assert_allclose(roundTripIn2, indata, atol=0.0002)

# fit an inverse transform with default bounds (which are the same bounds
Expand All @@ -359,8 +359,8 @@ def referenceFunc(point):
self.assertTrue(chebyMap2.hasForward)
self.assertTrue(chebyMap2.hasInverse)
# forward should be identical to the original
npt.assert_equal(chebyMap3.tranForward(indata), outdata)
roundTripIn3 = chebyMap3.tranInverse(outdata)
npt.assert_equal(chebyMap3.applyForward(indata), outdata)
roundTripIn3 = chebyMap3.applyInverse(outdata)
npt.assert_equal(roundTripIn3, roundTripIn2)

def test_ChebyMapChebyMapUnivertible(self):
Expand Down Expand Up @@ -408,7 +408,7 @@ def referenceFunc(point):
self.assertTrue(chebyMap1.hasForward)
self.assertFalse(chebyMap1.hasInverse)

outdata = chebyMap1.tranForward(indata)
outdata = chebyMap1.applyForward(indata)

referenceCheby = ReferenceCheby(referenceFunc, lbnd_f, ubnd_f)
des_outdata = referenceCheby.transform(indata)
Expand Down Expand Up @@ -446,7 +446,7 @@ def test_chebyGetDomain(self):
x1Grid, x2Grid = np.meshgrid(x1Edge, x2Edge)
indata = np.array([x1Grid.ravel(), x2Grid.ravel()])

outdata = chebyMap1.tranForward(indata)
outdata = chebyMap1.applyForward(indata)
pred_lbnd = outdata.min(1)
pred_ubnd = outdata.max(1)

Expand Down