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
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 "astshim.h"
35 
36 namespace lsst {
37 namespace afw {
38 
39 template std::shared_ptr<math::TransformBoundedField> table::io::PersistableFacade<
40  math::TransformBoundedField>::dynamicCast(std::shared_ptr<table::io::Persistable> const&);
41 
42 namespace math {
43 
45  : BoundedField(bbox), _transform(transform) {
46  if (transform.getToEndpoint().getNAxes() != 1) {
48  "To EndPoint of transform must have 1 axis but has " +
49  std::to_string(transform.getToEndpoint().getNAxes()));
50  }
51 }
52 
54  return _transform.applyForward(position)[0];
55 }
56 
57 ndarray::Array<double, 1, 1> TransformBoundedField::evaluate(ndarray::Array<double const, 1> const& x,
58  ndarray::Array<double const, 1> const& y) const {
59  if (x.getSize<0>() != y.getSize<0>()) {
61  "x length " + std::to_string(x.getSize<0>()) + "!= y length " +
62  std::to_string(x.getSize<0>()));
63  }
64 
65  // TODO if Mapping.applyForward gains support for x, y (DM-11226) then use that instead of copying data
66  int const nPoints = x.getSize<0>();
67  ndarray::Array<double, 2, 2> xy = ndarray::allocate(ndarray::makeVector(2, nPoints));
68  for (int col = 0; col < nPoints; ++col) {
69  xy[0][col] = x[col];
70  xy[1][col] = y[col];
71  }
72 
73  auto res2D = _transform.getMapping()->applyForward(xy);
74 
75  // res2D has shape 1 x N; return a 1-D view with the extra dimension stripped
76  auto resShape = ndarray::makeVector(nPoints);
77  auto resStrides = ndarray::makeVector(1);
78  return ndarray::external(res2D.getData(), resShape, resStrides, res2D);
79 }
80 
81 // ------------------ persistence ---------------------------------------------------------------------------
82 
83 namespace {
84 
85 struct PersistenceHelper {
86  table::Schema schema;
88  // store the FrameSet as string encoded as a variable-length vector of bytes
89  table::Key<table::Array<std::uint8_t>> frameSet;
90 
91  PersistenceHelper()
92  : schema(),
93  bbox(table::Box2IKey::addFields(schema, "bbox", "bounding box", "pixel")),
94  frameSet(schema.addField<table::Array<std::uint8_t>>(
95  "frameSet", "FrameSet contained in the Transform", "", 0)) {}
96 
97  PersistenceHelper(table::Schema const& s) : schema(s), bbox(s["bbox"]), frameSet(s["frameSet"]) {}
98 };
99 
100 class TransformBoundedFieldFactory : public table::io::PersistableFactory {
101 public:
102  explicit TransformBoundedFieldFactory(std::string const& name)
103  : afw::table::io::PersistableFactory(name) {}
104 
105  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
106  CatalogVector const& catalogs) const override {
107  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
108  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
109  table::BaseRecord const& record = catalogs.front().front();
110  PersistenceHelper const keys(record.getSchema());
111  auto bbox = record.get(keys.bbox);
112  auto frameSetStr = formatters::bytesToString(record.get(keys.frameSet));
113  auto transform =
115  frameSetStr);
116  return std::make_shared<TransformBoundedField>(bbox, *transform);
117  }
118 };
119 
120 std::string getTransformBoundedFieldPersistenceName() { return "TransformBoundedField"; }
121 
122 TransformBoundedFieldFactory registration(getTransformBoundedFieldPersistenceName());
123 
124 } // namespace
125 
127  return getTransformBoundedFieldPersistenceName();
128 }
129 
130 std::string TransformBoundedField::getPythonModule() const { return "lsst.afw.math"; }
131 
133  PersistenceHelper const keys;
134  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
135  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
136  record->set(keys.bbox, getBBox());
137  record->set(keys.frameSet, formatters::stringToBytes(getTransform().writeString()));
138  handle.saveCatalog(catalog);
139 }
140 
142  auto zoomMap = ast::ZoomMap(1, scale);
143  auto newMapping = getTransform().getMapping()->then(zoomMap);
144  auto newTransform = Transform(newMapping);
145  return std::make_shared<TransformBoundedField>(getBBox(), newTransform);
146 }
147 
149  auto rhsCasted = dynamic_cast<TransformBoundedField const*>(&rhs);
150  if (!rhsCasted) return false;
151 
152  return getBBox() == rhsCasted->getBBox() &&
153  *(getTransform().getMapping()) == *(rhsCasted->getTransform().getMapping());
154 }
155 
156 std::string TransformBoundedField::toString() const { return "TransformBoundedField"; }
157 
158 } // namespace math
159 } // namespace afw
160 } // namespace lsst
table::Key< std::string > name
Definition: Amplifier.cc:116
double x
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
int y
Definition: SpanSet.cc:48
table::Box2IKey bbox
table::Key< table::Array< std::uint8_t > > frameSet
table::Schema schema
table::Key< int > transform
#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:150
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:490
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:108
ndarray::Array< std::uint8_t, 1, 1 > stringToBytes(std::string const &str)
Encode a std::string as a vector of uint8.
Definition: Utils.cc:161
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:172
BoxKey< lsst::geom::Box2I > Box2IKey
Definition: aggregates.h:201
A base class for image defects.
STL namespace.
int col
Definition: CR.cc:144
T to_string(T... args)
std::shared_ptr< table::io::Persistable > read(table::io::InputArchive const &archive, table::io::CatalogVector const &catalogs) const override
Definition: warpExposure.cc:0