LSSTApplications  19.0.0-14-gb0260a2+492360823e,20.0.0+1c463ce1c5,20.0.0+332b39b81b,20.0.0+34a42eae2c,20.0.0+41b643067b,20.0.0+6e3b0e79d6,20.0.0+8558dd3f48,20.0.0+90990fb0e1,20.0.0+b0de6306d8,20.0.0+b290a576ab,20.0.0+b2ea66fa67,20.0.0+c2e041a55f,20.0.0+dcf29472a8,20.0.0+f45b7d88f4,20.0.0+fd13e6d5b6,20.0.0-1-g10df615+0d65e0fd03,20.0.0-1-g253301a+dcf29472a8,20.0.0-1-g498fb60+ff88705a28,20.0.0-1-g4d801e7+20a86399e6,20.0.0-1-g8a53f90+2817c06967,20.0.0-1-gc96f8cb+41b643067b,20.0.0-1-gd1c87d7+2817c06967,20.0.0-1-gdb27ee5+0eaf4f30a3,20.0.0-11-gda4966f+0cec7fe7b8,20.0.0-17-g98646ba4+f780282b58,20.0.0-2-g25c5319+ec537bc764,20.0.0-2-gec03fae+3bc057fb2a,20.0.0-27-g22bab1b+96b59cb0f5,20.0.0-3-gd2e950e+f45b7d88f4,20.0.0-4-g4a2362f+f45b7d88f4,20.0.0-5-gac0d578b1+6c871ee35c,20.0.0-5-gfcebe35+d7a723b4eb,20.0.0-6-g01203fff+106a5fd355,20.0.0-7-g3c4151b+2efbc3ed6b,20.0.0-8-g9573cbd+0eaf4f30a3,20.0.0-8-gc2abeef+41b643067b,w.2020.32
LSSTDataManagementBasePackage
TransformBoundedField.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2017 AURA/LSST.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include <cstdint>
25 #include <memory>
26 #include <string>
27 
28 #include "ndarray/eigen.h"
29 #include "astshim.h"
37 
38 namespace lsst {
39 namespace afw {
40 
41 template std::shared_ptr<math::TransformBoundedField> table::io::PersistableFacade<
42  math::TransformBoundedField>::dynamicCast(std::shared_ptr<table::io::Persistable> const&);
43 
44 namespace math {
45 
47  : BoundedField(bbox), _transform(transform) {
48  if (transform.getToEndpoint().getNAxes() != 1) {
50  "To EndPoint of transform must have 1 axis but has " +
51  std::to_string(transform.getToEndpoint().getNAxes()));
52  }
53 }
54 
56  return _transform.applyForward(position)[0];
57 }
58 
59 ndarray::Array<double, 1, 1> TransformBoundedField::evaluate(ndarray::Array<double const, 1> const& x,
60  ndarray::Array<double const, 1> const& y) const {
61  if (x.getSize<0>() != y.getSize<0>()) {
63  "x length " + std::to_string(x.getSize<0>()) + "!= y length " +
64  std::to_string(x.getSize<0>()));
65  }
66 
67  // TODO if Mapping.applyForward gains support for x, y (DM-11226) then use that instead of copying data
68  int const nPoints = x.getSize<0>();
69  ndarray::Array<double, 2, 2> xy = ndarray::allocate(ndarray::makeVector(2, nPoints));
70  for (int col = 0; col < nPoints; ++col) {
71  xy[0][col] = x[col];
72  xy[1][col] = y[col];
73  }
74 
75  auto res2D = _transform.getMapping()->applyForward(xy);
76 
77  // res2D has shape 1 x N; return a 1-D view with the extra dimension stripped
78  auto resShape = ndarray::makeVector(nPoints);
79  auto resStrides = ndarray::makeVector(1);
80  return ndarray::external(res2D.getData(), resShape, resStrides, res2D);
81 }
82 
83 // ------------------ persistence ---------------------------------------------------------------------------
84 
85 namespace {
86 
87 struct PersistenceHelper {
88  table::Schema schema;
90  // store the FrameSet as string encoded as a variable-length vector of bytes
91  table::Key<table::Array<std::uint8_t>> frameSet;
92 
93  PersistenceHelper()
94  : schema(),
95  bbox(table::Box2IKey::addFields(schema, "bbox", "bounding box", "pixel")),
96  frameSet(schema.addField<table::Array<std::uint8_t>>(
97  "frameSet", "FrameSet contained in the Transform", "", 0)) {}
98 
99  PersistenceHelper(table::Schema const& s) : schema(s), bbox(s["bbox"]), frameSet(s["frameSet"]) {}
100 };
101 
102 class TransformBoundedFieldFactory : public table::io::PersistableFactory {
103 public:
104  explicit TransformBoundedFieldFactory(std::string const& name)
105  : afw::table::io::PersistableFactory(name) {}
106 
107  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
108  CatalogVector const& catalogs) const override {
109  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
110  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
111  table::BaseRecord const& record = catalogs.front().front();
112  PersistenceHelper const keys(record.getSchema());
113  auto bbox = record.get(keys.bbox);
114  auto frameSetStr = formatters::bytesToString(record.get(keys.frameSet));
115  auto transform =
117  frameSetStr);
118  return std::make_shared<TransformBoundedField>(bbox, *transform);
119  }
120 };
121 
122 std::string getTransformBoundedFieldPersistenceName() { return "TransformBoundedField"; }
123 
124 TransformBoundedFieldFactory registration(getTransformBoundedFieldPersistenceName());
125 
126 } // namespace
127 
129  return getTransformBoundedFieldPersistenceName();
130 }
131 
132 std::string TransformBoundedField::getPythonModule() const { return "lsst.afw.math"; }
133 
135  PersistenceHelper const keys;
136  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
137  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
138  record->set(keys.bbox, getBBox());
139  record->set(keys.frameSet, formatters::stringToBytes(getTransform().writeString()));
140  handle.saveCatalog(catalog);
141 }
142 
144  auto zoomMap = ast::ZoomMap(1, scale);
145  auto newMapping = getTransform().getMapping()->then(zoomMap);
146  auto newTransform = Transform(newMapping);
147  return std::make_shared<TransformBoundedField>(getBBox(), newTransform);
148 }
149 
151  auto rhsCasted = dynamic_cast<TransformBoundedField const*>(&rhs);
152  if (!rhsCasted) return false;
153 
154  return getBBox() == rhsCasted->getBBox() &&
155  *(getTransform().getMapping()) == *(rhsCasted->getTransform().getMapping());
156 }
157 
158 std::string TransformBoundedField::toString() const { return "TransformBoundedField"; }
159 
160 } // namespace math
161 } // namespace afw
162 } // namespace lsst
y
int y
Definition: SpanSet.cc:49
lsst::afw::math::TransformBoundedField::evaluate
double evaluate(lsst::geom::Point2D const &position) const override
Evaluate the field at the given point.
Definition: TransformBoundedField.cc:55
std::string
STL class.
TransformBoundedField.h
std::shared_ptr
STL class.
lsst::afw::math::BoundedField
An abstract base class for 2-d functions defined on an integer bounding boxes.
Definition: BoundedField.h:55
lsst::afw::geom::Transform< afw::geom::Point2Endpoint, afw::geom::GenericEndpoint >
LSST_ARCHIVE_ASSERT
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
lsst::afw::math::TransformBoundedField::Transform
afw::geom::Transform< afw::geom::Point2Endpoint, afw::geom::GenericEndpoint > Transform
Definition: TransformBoundedField.h:46
lsst::afw::table::io::OutputArchiveHandle
An object passed to Persistable::write to allow it to persist itself.
Definition: OutputArchive.h:118
ast::Mapping::then
SeriesMap then(Mapping const &next) const
Return a series compound mapping this(first(input)) containing shallow copies of the original.
Definition: Mapping.cc:37
lsst::afw::math::TransformBoundedField::operator==
bool operator==(BoundedField const &rhs) const override
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal.
Definition: TransformBoundedField.cc:150
lsst::afw::math::TransformBoundedField::getPersistenceName
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: TransformBoundedField.cc:128
lsst::afw::table::io::OutputArchiveHandle::saveCatalog
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Definition: OutputArchive.cc:211
lsst::afw
Definition: imageAlgorithm.dox:1
Persistable.cc
lsst::afw::geom::Transform::applyForward
ToPoint applyForward(FromPoint const &point) const
Transform one point in the forward direction ("from" to "to")
Definition: Transform.cc:79
astshim.keyMap.keyMapContinued.keys
def keys(self)
Definition: keyMapContinued.py:6
ast::ZoomMap
A Mapping which "zooms" a set of points about the origin by multiplying all coordinate values by the ...
Definition: ZoomMap.h:45
bbox
table::Box2IKey bbox
Definition: TransformBoundedField.cc:89
CatalogVector.h
lsst::afw::math::TransformBoundedField
A BoundedField based on geom::Transform<Poin2Endpoint, GenericEndpoint<1>>.
Definition: TransformBoundedField.h:44
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
aggregates.h
lsst::afw::math::TransformBoundedField::TransformBoundedField
TransformBoundedField(lsst::geom::Box2I const &bbox, Transform const &transform)
Create a TransformBoundedField from a bounding box and transform.
Definition: TransformBoundedField.cc:46
astshim.h
lsst::afw::table::io::OutputArchiveHandle::makeCatalog
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
Definition: OutputArchive.cc:207
lsst::afw::formatters::bytesToString
std::string bytesToString(ndarray::Array< std::uint8_t const, 1, 1 > const &bytes)
Decode a std::string from a vector of uint8 returned by stringToBytes.
Definition: Utils.cc:173
lsst::afw::math::TransformBoundedField::getPythonModule
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: TransformBoundedField.cc:132
lsst::afw::table::Box2IKey
BoxKey< lsst::geom::Box2I > Box2IKey
Definition: aggregates.h:201
lsst::afw::math::TransformBoundedField::getTransform
Transform getTransform() const
Get the contained Transform.
Definition: TransformBoundedField.h:61
x
double x
Definition: ChebyshevBoundedField.cc:277
frameSet
table::Key< table::Array< std::uint8_t > > frameSet
Definition: TransformBoundedField.cc:91
std::to_string
T to_string(T... args)
ast::Mapping::applyForward
void applyForward(ConstArray2D const &from, Array2D const &to) const
Perform a forward transformation on 2-D array, putting the results into a pre-allocated 2-D array.
Definition: Mapping.h:260
lsst::afw::geom::Transform::readString
static std::shared_ptr< Transform< FromEndpoint, ToEndpoint > > readString(std::string &str)
Deserialize a Transform of this type from a string, using the same format as readStream.
Definition: Transform.cc:151
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::geom::Transform::getMapping
std::shared_ptr< const ast::Mapping > getMapping() const
Get the contained mapping.
Definition: Transform.h:135
lsst::afw::math::BoundedField::getBBox
lsst::geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
Definition: BoundedField.h:112
Utils.h
lsst::afw::formatters::stringToBytes
ndarray::Array< std::uint8_t, 1, 1 > stringToBytes(std::string const &str)
Encode a std::string as a vector of uint8.
Definition: Utils.cc:162
lsst::pex::exceptions::InvalidParameterError
Reports invalid arguments.
Definition: Runtime.h:66
schema
table::Schema schema
Definition: TransformBoundedField.cc:88
col
int col
Definition: CR.cc:144
std
STL namespace.
lsst::geom::Point< double, 2 >
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::afw.display.ds9.scale
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:109
transform
table::Key< int > transform
Definition: TransformMap.cc:299
InputArchive.h
lsst::afw::math::TransformBoundedField::operator*
std::shared_ptr< BoundedField > operator*(double const scale) const override
Return a scaled BoundedField.
Definition: TransformBoundedField.cc:143
lsst::afw::table::CatalogT::addNew
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:485
lsst::afw::math::TransformBoundedField::write
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: TransformBoundedField.cc:134
lsst::afw::table::CatalogT< BaseRecord >
OutputArchive.h