LSST Applications  21.0.0+c4f5df5339,21.0.0+e70536a077,21.0.0-1-ga51b5d4+7c60f8a6ea,21.0.0-10-gcf60f90+74aa0801fd,21.0.0-12-g63909ac9+643a1044a5,21.0.0-15-gedb9d5423+1041c3824f,21.0.0-2-g103fe59+a356b2badb,21.0.0-2-g1367e85+6d3f3f41db,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g5242d73+6d3f3f41db,21.0.0-2-g7f82c8f+8d7c04eab9,21.0.0-2-g8f08a60+9c9a9cfcc8,21.0.0-2-ga326454+8d7c04eab9,21.0.0-2-gde069b7+bedfc5e1fb,21.0.0-2-gecfae73+6cb6558258,21.0.0-2-gfc62afb+6d3f3f41db,21.0.0-3-g21c7a62+f6e98b25aa,21.0.0-3-g357aad2+bd62456bea,21.0.0-3-g4be5c26+6d3f3f41db,21.0.0-3-g65f322c+03a4076c01,21.0.0-3-g7d9da8d+c4f5df5339,21.0.0-3-gaa929c8+c6b98066dc,21.0.0-3-gc44e71e+a26d5c1aea,21.0.0-3-ge02ed75+04b527a9d5,21.0.0-35-g64f566ff+b27e5ef93e,21.0.0-4-g591bb35+04b527a9d5,21.0.0-4-g88306b8+8773047b2e,21.0.0-4-gc004bbf+80a0b7acb7,21.0.0-4-gccdca77+a5c54364a0,21.0.0-4-ge8fba5a+ccfc328107,21.0.0-5-gdf36809+87b8d260e6,21.0.0-6-g00874e7+7eeda2b6ba,21.0.0-6-g2d4f3f3+e70536a077,21.0.0-6-g4e60332+04b527a9d5,21.0.0-6-g5ef7dad+f53629abd8,21.0.0-7-gc8ca178+b63e69433b,21.0.0-8-gfbe0b4b+c6b98066dc,w.2021.06
LSST Data Management Base Package
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
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
SeriesMap then(Mapping const &next) const
Return a series compound mapping this(first(input)) containing shallow copies of the original.
Definition: Mapping.cc:37
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
A Mapping which "zooms" a set of points about the origin by multiplying all coordinate values by the ...
Definition: ZoomMap.h:45
std::shared_ptr< const ast::Mapping > getMapping() const
Get the contained mapping.
Definition: Transform.h:135
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
ToPoint applyForward(FromPoint const &point) const
Transform one point in the forward direction ("from" to "to")
An abstract base class for 2-d functions defined on an integer bounding boxes.
Definition: BoundedField.h:55
lsst::geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
Definition: BoundedField.h:112
A BoundedField based on geom::Transform<Poin2Endpoint, GenericEndpoint<1>>.
bool operator==(BoundedField const &rhs) const override
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal.
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
TransformBoundedField(lsst::geom::Box2I const &bbox, Transform const &transform)
Create a TransformBoundedField from a bounding box and transform.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
afw::geom::Transform< afw::geom::Point2Endpoint, afw::geom::GenericEndpoint > Transform
Transform getTransform() const
Get the contained Transform.
std::shared_ptr< BoundedField > operator*(double const scale) const override
Return a scaled BoundedField.
virtual double evaluate(lsst::geom::Point2D const &position) const=0
Evaluate the field at the given point.
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
An object passed to Persistable::write to allow it to persist itself.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
An integer coordinate rectangle.
Definition: Box.h:55
Reports invalid arguments.
Definition: Runtime.h:66
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:109
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
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
BoxKey< lsst::geom::Box2I > Box2IKey
Definition: aggregates.h:201
A base class for image defects.
STL namespace.
table::Key< int > transform
int col
Definition: CR.cc:144
int y
Definition: SpanSet.cc:49
double x
table::Box2IKey bbox
table::Key< table::Array< std::uint8_t > > frameSet
table::Schema schema
T to_string(T... args)