LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
lsst::afw::table::SchemaMapper Class Referencefinal

A mapping between the keys of two Schemas, used to copy data between them. More...

#include <SchemaMapper.h>

Public Member Functions

Schema const getInputSchema () const
 Return the input schema (copy-on-write).
 
Schema const getOutputSchema () const
 Return the output schema (copy-on-write).
 
SchemaeditOutputSchema ()
 Return a reference to the output schema that allows it to be modified in place.
 
template<typename T >
Key< T > addOutputField (Field< T > const &newField, bool doReplace=false)
 Add a new field to the output Schema that is not connected to the input Schema.
 
template<typename T >
Key< T > addMapping (Key< T > const &inputKey, bool doReplace=false)
 Add a new field to the output Schema that is a copy of a field in the input Schema.
 
template<typename T >
Key< T > addMapping (Key< T > const &inputKey, Field< T > const &outputField, bool doReplace=false)
 Add a new mapped field to the output Schema with new descriptions.
 
template<typename T >
Key< T > addMapping (Key< T > const &inputKey, std::string const &outputName, bool doReplace=true)
 Add a new mapped field to the output Schema with a new name.
 
template<typename T >
Key< T > addMapping (Key< T > const &inputKey, char const *outputName, bool doReplace=true)=delete
 This deliberately deleted overload ensures we don't accidentally cast string literals to bool.
 
template<typename Predicate >
void addMappingsWhere (Predicate predicate, bool doReplace=true)
 Add mappings for all fields that match criteria defined by a predicate.
 
void addMinimalSchema (Schema const &minimal, bool doMap=true)
 Add the given minimal schema to the output schema.
 
void invert ()
 Swap the input and output schemas in-place.
 
template<typename T >
bool isMapped (Key< T > const &inputKey) const
 Return true if the given input Key is mapped to an output Key.
 
template<typename T >
Key< T > getMapping (Key< T > const &inputKey) const
 Return the output Key corresponding to the given input Key, or raise NotFoundError.
 
template<typename F >
void forEach (F &func) const
 Call the given functor for each key pair in the mapper.
 
template<typename F >
void forEach (F const &func) const
 
 SchemaMapper ()
 Construct an empty mapper; useless unless you assign a fully-constructed one to it.
 
 SchemaMapper (Schema const &input, Schema const &output)
 Construct a mapper from the given input Schema and initial output Schema.
 
 SchemaMapper (Schema const &input, bool shareAliasMap=false)
 Construct a mapper from the given input Schema.
 
 SchemaMapper (SchemaMapper const &other)
 Copy construct (copy-on-write).
 
 SchemaMapper (SchemaMapper &&other)
 
SchemaMapperoperator= (SchemaMapper const &other)
 Assignment (copy-on-write).
 
SchemaMapperoperator= (SchemaMapper &&other)
 
 ~SchemaMapper ()
 

Static Public Member Functions

static SchemaMapper removeMinimalSchema (Schema const &input, Schema const &minimal)
 Create a mapper by removing fields from the front of a schema.
 
static std::vector< SchemaMapperjoin (std::vector< Schema > const &inputs, std::vector< std::string > const &prefixes=std::vector< std::string >())
 Combine a sequence of schemas into one, creating a SchemaMapper for each.
 

Detailed Description

A mapping between the keys of two Schemas, used to copy data between them.

SchemaMapper is initialized with its input Schema, and contains member functions to add mapped or unmapped fields to the output Schema.

Definition at line 21 of file SchemaMapper.h.

Constructor & Destructor Documentation

◆ SchemaMapper() [1/5]

lsst::afw::table::SchemaMapper::SchemaMapper ( )
explicit

Construct an empty mapper; useless unless you assign a fully-constructed one to it.

Definition at line 105 of file SchemaMapper.cc.

105: _impl(new Impl(Schema(), Schema())) {}

◆ SchemaMapper() [2/5]

lsst::afw::table::SchemaMapper::SchemaMapper ( Schema const & input,
Schema const & output )
explicit

Construct a mapper from the given input Schema and initial output Schema.

Parameters
[in]inputThe Schema that fields will be mapped from.
[in]outputThe starting point for the Schema that fields will be mapped to (no mappings will be created automaticaly). Use addMapping() with doReplace=true to connect input fields to preexisting fields in the output schema.

Note that the addMapping() methods will not connect input schema fields to existing output schema fields unless doReplace=true; instead, these will by default append new fields to the output schema. So most often you'll want to start with an empty output schema and construct it as fields are mapped from the input schema, or be sure to always pass doReplace=true to addMapping.

Definition at line 111 of file SchemaMapper.cc.

111: _impl(new Impl(input, output)) {}

◆ SchemaMapper() [3/5]

lsst::afw::table::SchemaMapper::SchemaMapper ( Schema const & input,
bool shareAliasMap = false )
explicit

Construct a mapper from the given input Schema.

Parameters
[in]inputThe Schema that fields will be mapped from.
[in]shareAliasMapIf true, install the input Schema's AliasMap in the output Schema.

Note that the addMapping() methods will not connect input schema fields to existing output schema fields unless doReplace=true; instead, these will by default append new fields to the output schema. So most often you'll want to start with an empty output schema and construct it as fields are mapped from the input schema, or be sure to always pass doReplace=true to addMapping.

The initial (empty) output schema will have the same version as the input schema, and they will share the same AliasMap (use editOutputSchema().disconnectAliases() to use a copy of the AliasMap).

Definition at line 113 of file SchemaMapper.cc.

113 : _impl(new Impl(input, Schema())) {
114 if (shareAliasMap) {
115 editOutputSchema().setAliasMap(input.getAliasMap());
116 }
117}
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
Definition Schema.cc:533
Schema & editOutputSchema()
Return a reference to the output schema that allows it to be modified in place.

◆ SchemaMapper() [4/5]

lsst::afw::table::SchemaMapper::SchemaMapper ( SchemaMapper const & other)

Copy construct (copy-on-write).

Definition at line 107 of file SchemaMapper.cc.

107: _impl(new Impl(*other._impl)) {}

◆ SchemaMapper() [5/5]

lsst::afw::table::SchemaMapper::SchemaMapper ( SchemaMapper && other)

Definition at line 109 of file SchemaMapper.cc.

109: SchemaMapper(other) {}
SchemaMapper()
Construct an empty mapper; useless unless you assign a fully-constructed one to it.

◆ ~SchemaMapper()

lsst::afw::table::SchemaMapper::~SchemaMapper ( )
default

Member Function Documentation

◆ addMapping() [1/4]

template<typename T >
Key< T > lsst::afw::table::SchemaMapper::addMapping ( Key< T > const & inputKey,
bool doReplace = false )

Add a new field to the output Schema that is a copy of a field in the input Schema.

If the input Key has already been mapped, the existing output Key will be reused but the associated Field in the output Schema will be reset to a copy of the input Field.

If doReplace=True and a field with same name already exists in the output schema, that field will be mapped instead of adding a new field to the output schema. If doReplace=false and a name conflict occurs, an exception will be thrown.

Definition at line 130 of file SchemaMapper.cc.

130 {
131 typename Impl::KeyPairMap::iterator i =
132 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
133 Field<T> inputField = _impl->_input.find(inputKey).field;
134 if (i != _impl->_map.end()) {
135 Key<T> const &outputKey = std::get<std::pair<Key<T>, Key<T>>>(*i).second;
136 _impl->_output.replaceField(outputKey, inputField);
137 return outputKey;
138 } else {
139 Key<T> outputKey = _impl->_output.addField(inputField, doReplace);
140 _impl->_map.insert(i, std::make_pair(inputKey, outputKey));
141 return outputKey;
142 }
143}
T begin(T... args)
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition Schema.cc:479
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
Definition Schema.cc:485
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition Schema.cc:467
T end(T... args)
T find_if(T... args)
T insert(T... args)
T make_pair(T... args)

◆ addMapping() [2/4]

template<typename T >
Key< T > lsst::afw::table::SchemaMapper::addMapping ( Key< T > const & inputKey,
char const * outputName,
bool doReplace = true )
delete

This deliberately deleted overload ensures we don't accidentally cast string literals to bool.

See DM-13787 for more information.

◆ addMapping() [3/4]

template<typename T >
Key< T > lsst::afw::table::SchemaMapper::addMapping ( Key< T > const & inputKey,
Field< T > const & outputField,
bool doReplace = false )

Add a new mapped field to the output Schema with new descriptions.

If the input Key has already been mapped, the existing output Key will be reused but the associated Field will be replaced with the given one.

If doReplace=True and a field with same name already exists in the output schema, that field will be mapped instead of adding a new field to the output schema. If doReplace=false and a name conflict occurs, an exception will be thrown.

Definition at line 146 of file SchemaMapper.cc.

146 {
147 typename Impl::KeyPairMap::iterator i =
148 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
149 if (i != _impl->_map.end()) {
150 Key<T> const &outputKey = std::get<std::pair<Key<T>, Key<T>>>(*i).second;
151 _impl->_output.replaceField(outputKey, field);
152 return outputKey;
153 } else {
154 Key<T> outputKey = _impl->_output.addField(field, doReplace);
155 _impl->_map.insert(i, std::make_pair(inputKey, outputKey));
156 return outputKey;
157 }
158}

◆ addMapping() [4/4]

template<typename T >
Key< T > lsst::afw::table::SchemaMapper::addMapping ( Key< T > const & inputKey,
std::string const & outputName,
bool doReplace = true )

Add a new mapped field to the output Schema with a new name.

If the input Key has already been mapped, the existing output Key will be reused but the associated Field will be replaced with one with the given name.

If doReplace=True and a field with same name already exists in the output schema, that field will be mapped instead of adding a new field to the output schema. If doReplace=false and a name conflict occurs, an exception will be thrown.

Definition at line 161 of file SchemaMapper.cc.

161 {
162 typename Impl::KeyPairMap::iterator i =
163 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
164 if (i != _impl->_map.end()) {
165 Key<T> const &outputKey = std::get<std::pair<Key<T>, Key<T>>>(*i).second;
166 Field<T> field = _impl->_output.find(outputKey).field;
167 field = field.copyRenamed(outputName);
168 _impl->_output.replaceField(outputKey, field);
169 return outputKey;
170 } else {
171 Field<T> inputField = _impl->_input.find(inputKey).field;
172 Field<T> outputField = inputField.copyRenamed(outputName);
173 Key<T> outputKey = _impl->_output.addField(outputField, doReplace);
174 _impl->_map.insert(i, std::make_pair(inputKey, outputKey));
175 return outputKey;
176 }
177}

◆ addMappingsWhere()

template<typename Predicate >
void lsst::afw::table::SchemaMapper::addMappingsWhere ( Predicate predicate,
bool doReplace = true )

Add mappings for all fields that match criteria defined by a predicate.

A mapping in the output Schema will be created for each SchemaItem 'i' in the input Schema such that 'predicate(i)' is true. Note that the predicate must have a templated and/or sufficiently overloaded operator() to match all supported field types, not just those present in the input Schema.

If doReplace=True and a field with same name already exists in the output schema, that field will be mapped instead of adding a new field to the output schema. If doReplace=false and a name conflict occurs, an exception will be thrown.

Definition at line 247 of file SchemaMapper.h.

247 {
248 _impl->_input.forEach(AddMappingsWhere<Predicate>(this, predicate, doReplace));
249}
void forEach(F &func) const
Apply a functor to each SchemaItem in the Schema.
Definition Schema.h:214

◆ addMinimalSchema()

void lsst::afw::table::SchemaMapper::addMinimalSchema ( Schema const & minimal,
bool doMap = true )

Add the given minimal schema to the output schema.

This is intended to be used to ensure the output schema starts with some minimal schema. It must be called before any other fields are added to the output schema.

Parameters
[in]minimalMinimal schema to be added to the beginning of the output schema.
[in]doMapWhether to map minimal schema fields that are also present in the input schema.

Definition at line 179 of file SchemaMapper.cc.

179 {
180 if (getOutputSchema().getFieldCount() > 0) {
181 throw LSST_EXCEPT(pex::exceptions::LogicError,
182 "Must add minimal schema to mapper before adding any other fields");
183 }
184 MapMinimalSchema f(this, doMap);
185 minimal.forEach(f);
186}
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
Schema minimal
Schema const getOutputSchema() const
Return the output schema (copy-on-write).

◆ addOutputField()

template<typename T >
Key< T > lsst::afw::table::SchemaMapper::addOutputField ( Field< T > const & newField,
bool doReplace = false )
inline

Add a new field to the output Schema that is not connected to the input Schema.

Definition at line 34 of file SchemaMapper.h.

34 {
35 return _impl->_output.addField(newField, doReplace);
36 }

◆ editOutputSchema()

Schema & lsst::afw::table::SchemaMapper::editOutputSchema ( )
inline

Return a reference to the output schema that allows it to be modified in place.

Definition at line 30 of file SchemaMapper.h.

30{ return _impl->_output; }

◆ forEach() [1/2]

template<typename F >
void lsst::afw::table::SchemaMapper::forEach ( F & func) const
inline

Call the given functor for each key pair in the mapper.

Function objects should have a template and/or overloaded operator() that takes two Key objects with the same type:

struct Functor {
    template <typename T>
    void operator()(Key<T> const & input, Key<T> const & output) const;
};

The order of iteration is the same as the order in which mappings were added.

Definition at line 150 of file SchemaMapper.h.

150 {
151 for (auto const & item : _impl->_map) {
152 std::visit([&func](auto const & pair) { func(pair.first, pair.second); }, item);
153 }
154 }

◆ forEach() [2/2]

template<typename F >
void lsst::afw::table::SchemaMapper::forEach ( F const & func) const
inline

Definition at line 156 of file SchemaMapper.h.

156 {
157 for (auto const & item : _impl->_map) {
158 std::visit([&func](auto const & pair) { func(pair.first, pair.second); }, item);
159 }
160 }

◆ getInputSchema()

Schema const lsst::afw::table::SchemaMapper::getInputSchema ( ) const
inline

Return the input schema (copy-on-write).

Definition at line 24 of file SchemaMapper.h.

24{ return _impl->_input; }

◆ getMapping()

template<typename T >
Key< T > lsst::afw::table::SchemaMapper::getMapping ( Key< T > const & inputKey) const

Return the output Key corresponding to the given input Key, or raise NotFoundError.

Definition at line 206 of file SchemaMapper.cc.

206 {
207 typename Impl::KeyPairMap::iterator i =
208 std::find_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
209 if (i == _impl->_map.end()) {
210 throw LSST_EXCEPT(lsst::pex::exceptions::NotFoundError, "Input Key is not mapped.");
211 }
212 return std::get<std::pair<Key<T>, Key<T>>>(*i).second;
213}
Reports attempts to access elements using an invalid key.
Definition Runtime.h:151

◆ getOutputSchema()

Schema const lsst::afw::table::SchemaMapper::getOutputSchema ( ) const
inline

Return the output schema (copy-on-write).

Definition at line 27 of file SchemaMapper.h.

27{ return _impl->_output; }

◆ invert()

void lsst::afw::table::SchemaMapper::invert ( )

Swap the input and output schemas in-place.

Definition at line 195 of file SchemaMapper.cc.

195 {
196 std::swap(_impl->_input, _impl->_output);
197 std::for_each(_impl->_map.begin(), _impl->_map.end(), SwapKeyPair());
198}
T for_each(T... args)
T swap(T... args)

◆ isMapped()

template<typename T >
bool lsst::afw::table::SchemaMapper::isMapped ( Key< T > const & inputKey) const

Return true if the given input Key is mapped to an output Key.

Definition at line 201 of file SchemaMapper.cc.

201 {
202 return std::count_if(_impl->_map.begin(), _impl->_map.end(), KeyPairCompareEqual<T>(inputKey));
203}
T count_if(T... args)

◆ join()

std::vector< SchemaMapper > lsst::afw::table::SchemaMapper::join ( std::vector< Schema > const & inputs,
std::vector< std::string > const & prefixes = std::vector<std::string>() )
static

Combine a sequence of schemas into one, creating a SchemaMapper for each.

Parameters
[in]inputsA vector of input schemas to merge.
[in]prefixesAn optional vector of prefixes for the output field names, either empty or of the same size as the inputs vector.

Each of the returned SchemaMappers has the same output schema.

Definition at line 215 of file SchemaMapper.cc.

216 {
217 std::size_t const size = inputs.size();
218 if (!prefixes.empty() && prefixes.size() != inputs.size()) {
219 throw LSST_EXCEPT(
220 pex::exceptions::LengthError,
221 (boost::format("prefix vector size (%d) must be the same as input vector size (%d)") %
222 prefixes.size() % inputs.size())
223 .str());
224 }
226 for (std::size_t i = 0; i < size; ++i) {
227 result.emplace_back(inputs[i]);
228 }
229 for (std::size_t i = 0; i < size; ++i) {
230 for (std::size_t j = 0; j < size; ++j) {
231 if (i == j) {
232 AddMapped functor(&result[j]);
233 if (!prefixes.empty()) functor.prefix = prefixes[i];
234 inputs[i].forEach(functor);
235 } else {
236 AddUnmapped functor(&result[j]);
237 if (!prefixes.empty()) functor.prefix = prefixes[i];
238 inputs[i].forEach(functor);
239 }
240 }
241 }
242 return result;
243}
py::object result
Definition _schema.cc:429
T emplace_back(T... args)
T empty(T... args)
T size(T... args)

◆ operator=() [1/2]

SchemaMapper & lsst::afw::table::SchemaMapper::operator= ( SchemaMapper && other)

Definition at line 125 of file SchemaMapper.cc.

125{ return *this = other; }

◆ operator=() [2/2]

SchemaMapper & lsst::afw::table::SchemaMapper::operator= ( SchemaMapper const & other)

Assignment (copy-on-write).

Definition at line 119 of file SchemaMapper.cc.

119 {
120 std::unique_ptr<Impl> tmp(new Impl(*other._impl));
121 _impl.swap(tmp);
122 return *this;
123}

◆ removeMinimalSchema()

SchemaMapper lsst::afw::table::SchemaMapper::removeMinimalSchema ( Schema const & input,
Schema const & minimal )
static

Create a mapper by removing fields from the front of a schema.

The returned mapper maps all fields in the input schema to all fields that are not in the minimal schema (compared by keys, so the overlap must appear at the beginning of the input schema).

Parameters
[in]inputInput schema for the mapper.
[in]minimalThe minimal schema the input schema starts with.

Definition at line 188 of file SchemaMapper.cc.

188 {
189 SchemaMapper mapper(input);
190 RemoveMinimalSchema f(&mapper, minimal);
191 input.forEach(f);
192 return mapper;
193}
SchemaMapper * mapper

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