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
Classes | 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). More...
 
Schema const getOutputSchema () const
 Return the output schema (copy-on-write). More...
 
SchemaeditOutputSchema ()
 Return a reference to the output schema that allows it to be modified in place. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
template<typename Predicate >
void addMappingsWhere (Predicate predicate, bool doReplace=true)
 Add mappings for all fields that match criteria defined by a predicate. More...
 
void addMinimalSchema (Schema const &minimal, bool doMap=true)
 Add the given minimal schema to the output schema. More...
 
void invert ()
 Swap the input and output schemas in-place. More...
 
template<typename T >
bool isMapped (Key< T > const &inputKey) const
 Return true if the given input Key is mapped to an output Key. More...
 
template<typename T >
Key< T > getMapping (Key< T > const &inputKey) const
 Return the output Key corresponding to the given input Key, or raise NotFoundError. More...
 
template<typename F >
void forEach (F &func) const
 Call the given functor for each key pair in the mapper. More...
 
template<typename F >
void forEach (F const &func) const
 
 SchemaMapper ()
 Construct an empty mapper; useless unless you assign a fully-constructed one to it. More...
 
 SchemaMapper (Schema const &input, Schema const &output)
 Construct a mapper from the given input Schema and initial output Schema. More...
 
 SchemaMapper (Schema const &input, bool shareAliasMap=false)
 Construct a mapper from the given input Schema. More...
 
 SchemaMapper (SchemaMapper const &other)
 Copy construct (copy-on-write). More...
 
 SchemaMapper (SchemaMapper &&other)
 
SchemaMapperoperator= (SchemaMapper const &other)
 Assignment (copy-on-write). More...
 
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. More...
 
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. More...
 

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.
Definition: SchemaMapper.h:30

◆ 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 }
table::Key< int > field
Definition: ApCorrMap.cc:77

◆ 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).
Definition: SchemaMapper.h:27

◆ 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 empty(T... args)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
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
Definition: SchemaMapper.cc:71

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