LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
CModel.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2013 LSST Corporation.
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 #ifndef LSST_MEAS_MODELFIT_CModelFit_h_INCLUDED
25 #define LSST_MEAS_MODELFIT_CModelFit_h_INCLUDED
26 
27 #include <bitset>
28 #include <string>
29 
30 #include "ndarray.h"
31 
32 #include "lsst/geom.h"
33 #include "lsst/pex/config.h"
35 #include "lsst/afw/table/Source.h"
45 
46 namespace lsst { namespace meas { namespace modelfit {
47 
143 
145  profileName("lux"),
146  priorSource("EMPIRICAL"),
147  priorName(),
148  nComponents(8),
149  maxRadius(0),
150  usePixelWeights(false),
151  weightsMultiplier(1.0),
152  doRecordHistory(true),
153  doRecordTime(true)
154  {}
155 
158  }
159 
160  PTR(Model) getModel() const;
161 
162  PTR(Prior) getPrior() const;
163 
166  "Name of the shapelet.RadialProfile that defines the model to fit"
167  );
168 
171  "One of 'FILE', 'LINEAR', 'EMPIRICAL', or 'NONE', indicating whether the prior should be loaded "
172  "from disk, created from one of the nested prior config/control objects, or None"
173  );
174 
177  "Name of the Prior that defines the model to fit (a filename in $MEAS_MODELFIT_DIR/data, "
178  "with no extension), if priorSource='FILE'. Ignored for forced fitting."
179  );
180 
183  "Configuration for a linear prior, used if priorSource='LINEAR'."
184  );
185 
188  "Configuration for an empirical prior, used if priorSource='EMPIRICAL'."
189  );
190 
191  LSST_CONTROL_FIELD(nComponents, int, "Number of Gaussian used to approximate the profile");
192 
194  maxRadius,
195  int,
196  "Maximum radius used in approximating profile with Gaussians (0=default for this profile)"
197  );
198 
201  bool,
202  "Use per-pixel variances as weights in the nonlinear fit (the final linear fit for"
203  " flux never uses per-pixel variances)"
204  );
205 
208  double,
209  "Scale the likelihood by this factor to artificially reweight it w.r.t. the prior."
210  );
211 
214  "Configuration for how the objective surface is explored. Ignored for forced fitting"
215  );
216 
218  doRecordHistory, bool,
219  "Whether to record the steps the optimizer takes (or just the number, if running as a plugin)"
220  );
221 
223  doRecordTime, bool,
224  "Whether to record the time spent in this stage"
225  );
226 
227 };
228 
234 
236  psfName("modelfit_DoubleShapeletPsfApprox"),
237  minInitialRadius(0.1),
238  fallbackInitialMomentsPsfFactor(1.5)
239  {
240  initial.nComponents = 3; // use very rough model in initial fit
241  initial.optimizer.gradientThreshold = 1E-3; // with slightly coarser convergence criteria
242  initial.optimizer.minTrustRadiusThreshold = 1E-2;
243  initial.usePixelWeights = true;
244  dev.profileName = "luv";
245  exp.nComponents = 6;
246  exp.optimizer.maxOuterIterations = 250;
247  }
248 
250  psfName,
251  std::string,
252  "Field name prefix of the Shapelet PSF approximation used to convolve the galaxy model; "
253  "must contain a set of fields matching the schema defined by shapelet.MultiShapeletFunctionKey."
254  );
255 
258  "Configuration parameters related to the determination of the pixels to include in the fit."
259  );
260 
263  "An initial fit (usually with a fast, approximate model) used to warm-start the exp and dev fits, "
264  "convolved with only the zeroth-order terms in the multi-shapelet PSF approximation."
265  );
266 
269  "Independent fit of the exponential component"
270  );
271 
274  "Independent fit of the de Vaucouleur component"
275  );
276 
278  minInitialRadius, double,
279  "Minimum initial radius in pixels (used to regularize initial moments-based PSF deconvolution)"
280  );
281 
283  fallbackInitialMomentsPsfFactor, double,
284  "If the 2nd-moments shape used to initialize the fit failed, use the PSF moments multiplied by this."
285  " If <= 0.0, abort the fit early instead."
286  );
287 
288 };
289 
294 
296  enum FlagBit {
297  FAILED=0,
298  TR_SMALL,
299  MAX_ITERATIONS,
301  NUMERIC_ERROR,
303  BAD_REFERENCE,
305  N_FLAGS
306  };
307 
309 
320 
321  ndarray::Array<Scalar const,1,1> nonlinear;
322  ndarray::Array<Scalar const,1,1> amplitudes;
323  ndarray::Array<Scalar const,1,1> fixed;
324 
327 };
328 
333 struct CModelResult {
334 
336  enum FlagBit {
337  FAILED=0,
338  REGION_MAX_AREA,
340  REGION_MAX_BAD_PIXEL_FRACTION,
341  REGION_USED_FOOTPRINT_AREA,
343  REGION_USED_PSF_AREA,
346  REGION_USED_INITIAL_ELLIPSE_MIN,
348  REGION_USED_INITIAL_ELLIPSE_MAX,
350  NO_SHAPE,
352  SMALL_SHAPE,
354  NO_SHAPELET_PSF,
358  N_FLAGS
359  };
360 
361  CModelResult();
362 
367  Scalar objective;
369 
373 
376 
379 };
380 
390 public:
391 
394 
408  std::string const & name,
409  Control const & ctrl,
411  );
412 
429  std::string const & name,
430  Control const & ctrl,
431  afw::table::SchemaMapper & schemaMapper
432  );
433 
441  explicit CModelAlgorithm(Control const & ctrl);
442 
444  Control const & getControl() const { return _ctrl; }
445 
462  Result apply(
463  afw::image::Exposure<Pixel> const & exposure,
465  geom::Point2D const & center,
467  Scalar approxFlux=-1,
468  Scalar kronRadius=-1,
469  int footprintArea=-1
470  ) const;
471 
484  Result applyForced(
485  afw::image::Exposure<Pixel> const & exposure,
487  geom::Point2D const & center,
488  Result const & reference,
489  Scalar approxFlux=-1
490  ) const;
491 
504  void measure(
505  afw::table::SourceRecord & measRecord,
506  afw::image::Exposure<Pixel> const & exposure
507  ) const;
508 
524  void measure(
525  afw::table::SourceRecord & measRecord,
526  afw::image::Exposure<Pixel> const & exposure,
527  afw::table::SourceRecord const & refRecord
528  ) const;
529 
538  void fail(
539  afw::table::SourceRecord & measRecord,
541  ) const;
542 
544  void writeResultToRecord(Result const & result, afw::table::BaseRecord & record) const;
545 
546 private:
547 
548  friend class CModelAlgorithmControl;
549 
550  // Actual implementations go here; we use an output argument for the result so we can get partial
551  // results to the plugin version when we throw.
552  void _applyImpl(
553  Result & result,
554  afw::image::Exposure<Pixel> const & exposure,
556  geom::Point2D const & center,
557  afw::geom::ellipses::Quadrupole const & moments,
558  Scalar approxFlux,
559  Scalar kronRadius=-1,
560  int footprintArea=-1
561  ) const;
562 
563  // Actual implementations go here; we use an output argument for the result so we can get partial
564  // results to the SourceRecord version when we throw.
565  void _applyForcedImpl(
566  Result & result,
567  afw::image::Exposure<Pixel> const & exposure,
569  geom::Point2D const & center,
570  Result const & reference,
571  Scalar approxFlux
572  ) const;
573 
574  // gets/checks inputs from SourceRecord that are needed by both apply and applyForced
575  template <typename PixelT>
576  shapelet::MultiShapeletFunction _processInputs(
578  afw::image::Exposure<PixelT> const & exposure
579  ) const;
580 
581  class Impl;
582 
583  Control _ctrl;
584  PTR(Impl) _impl;
585 };
586 
587 }}} // namespace lsst::meas::modelfit
588 
589 #endif // !LSST_MEAS_MODELFIT_CModelFit_h_INCLUDED
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:47
Defines the fields and offsets for a table.
Definition: Schema.h:50
shapelet::RadialProfile const & getProfile() const
Definition: CModel.h:156
LocalUnitTransform fitSysToMeasSys
Transforms to the coordinate system where parameters are defined.
Definition: CModel.h:377
boost::shared_ptr< OptimizerObjective > objfunc
Objective class used by the optimizer.
Definition: CModel.h:312
Control const & getControl() const
Return the control object the algorithm was constructed with.
Definition: CModel.h:444
Scalar objective
Value of the objective function at the best fit point: chisq/2 - ln(prior)
Definition: CModel.h:317
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
Scalar instFluxInner
Flux measured strictly within the fit region (no extrapolation).
Definition: CModel.h:365
std::bitset< N_FLAGS > flags
Array of flags.
Definition: CModel.h:378
ndarray::Array< Scalar const, 1, 1 > amplitudes
Opaque linear parameters in specialized units.
Definition: CModel.h:322
afw::geom::ellipses::Quadrupole ellipse
Best fit half-light ellipse in pixel coordinates.
Definition: CModel.h:319
Scalar instFluxErr
Flux uncertainty from the final linear fit.
Definition: CModel.h:364
double Scalar
Typedefs to be used for probability and parameter values.
Definition: common.h:44
boost::shared_ptr< Model > model
Model object that defines the parametrization (defined fully by Control struct)
Definition: CModel.h:310
ndarray::Array< Scalar const, 1, 1 > nonlinear
Opaque nonlinear parameters in specialized units.
Definition: CModel.h:321
CModelResult Result
Typedef to the master Result struct.
Definition: CModel.h:393
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
#define LSST_CONTROL_FIELD(NAME, TYPE, DOC)
A preprocessor macro used to define fields in C++ "control object" structs.
Definition: config.h:43
double weightsMultiplier
"Scale the likelihood by this factor to artificially reweight it w.r.t. the prior." ;
Definition: CModel.h:210
std::string profileName
"Name of the shapelet.RadialProfile that defines the model to fit" ;
Definition: CModel.h:167
bool doRecordTime
"Whether to record the time spent in this stage" ;
Definition: CModel.h:225
Nested control object for CModel that configures one of the three ("initial", "exp", "dev") nonlinear fitting stages.
Definition: CModel.h:142
CModelStageResult dev
Results from the de Vaucouleur (Sersic n=4) fit.
Definition: CModel.h:372
static RadialProfile & get(std::string const &name)
Return a predefined radial profile given its name.
STL class.
Registry and utility class for multi-Gaussian approximations to radial profiles.
Definition: RadialProfile.h:60
CModelStageResult initial
Results from the initial approximate nonlinear fit that feeds the others.
Definition: CModel.h:370
Abstract base class and concrete factories that define multi-shapelet galaxy models.
Definition: Model.h:56
boost::shared_ptr< Prior > prior
Bayesian priors on the parameters (defined fully by Control struct)
Definition: CModel.h:311
std::string priorSource
"One of &#39;FILE&#39;, &#39;LINEAR&#39;, &#39;EMPIRICAL&#39;, or &#39;NONE&#39;, indicating whether the prior should be loaded " "fr...
Definition: CModel.h:173
Main public interface class for CModel algorithm.
Definition: CModel.h:389
std::bitset< N_FLAGS > flags
Array of flags.
Definition: CModel.h:326
A base class for image defects.
A multi-scale shapelet function.
std::string priorName
"Name of the Prior that defines the model to fit (a filename in $MEAS_MODELFIT_DIR/data, " "with no extension), if priorSource=&#39;FILE&#39;. Ignored for forced fitting." ;
Definition: CModel.h:179
The main control object for CModel, containing parameters for the final linear fit and aggregating th...
Definition: CModel.h:233
table::Schema schema
Definition: Amplifier.cc:115
Scalar instFluxInner
Flux measured strictly within the fit region (no extrapolation).
Definition: CModel.h:316
afw::geom::ellipses::Quadrupole initialFitRegion
Pixels used in the initial fit.
Definition: CModel.h:374
afw::geom::ellipses::Quadrupole finalFitRegion
Pixels used in the exp, dev, and linear fits.
Definition: CModel.h:375
Scalar instFluxErr
Flux uncertainty from just this stage fit.
Definition: CModel.h:315
Scalar time
Time spent in this fit in seconds.
Definition: CModel.h:318
#define LSST_NESTED_CONTROL_FIELD(NAME, MODULE, TYPE, DOC)
A preprocessor macro used to define fields in C++ "control object" structs, for nested control object...
Definition: config.h:69
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
int nComponents
"Number of Gaussian used to approximate the profile" ;
Definition: CModel.h:191
bool doRecordHistory
"Whether to record the steps the optimizer takes (or just the number, if running as a plugin)" ; ...
Definition: CModel.h:220
SoftenedLinearPriorControl linearPriorConfig
"Configuration for a linear prior, used if priorSource=&#39;LINEAR&#39;." ;
Definition: CModel.h:184
VectorQ moments
Definition: simpleShape.cc:151
Configuration object for Optimizer.
Definition: optimizer.h:187
Scalar fracDev
Fraction of flux from the final linear fit in the de Vaucouleur component (always between 0 and 1)...
Definition: CModel.h:366
Base class for Bayesian priors.
Definition: Prior.h:36
FlagBit
Flags that apply to all four CModel fits or just the last one.
Definition: CModel.h:336
A local mapping between two UnitSystems.
Definition: UnitSystem.h:80
boost::shared_ptr< UnitTransformedLikelihood > likelihood
Object used to evaluate models and compare to data.
Definition: CModel.h:313
SemiEmpiricalPriorControl empiricalPriorConfig
"Configuration for an empirical prior, used if priorSource=&#39;EMPIRICAL&#39;." ;
Definition: CModel.h:189
Scalar instFlux
Flux from the final linear fit.
Definition: CModel.h:363
Base class for all records.
Definition: BaseRecord.h:31
CModelControl Control
Typedef to the master Control struct.
Definition: CModel.h:392
bool usePixelWeights
"Use per-pixel variances as weights in the nonlinear fit (the final linear fit for" " flux never uses...
Definition: CModel.h:204
int maxRadius
"Maximum radius used in approximating profile with Gaussians (0=default for this profile)" ; ...
Definition: CModel.h:197
def measure(mi, x, y, size, statistic, stats)
Definition: fringe.py:506
Reference fit failed, so forced fit will fail as well.
Definition: CModel.h:357
Record class that contains measurements made on a single exposure.
Definition: Source.h:80
FlagBit
Flags for a single CModel stage (note that there are additional flags for the full multi-stage fit) ...
Definition: CModel.h:296
Base class for objective functions for Optimizer.
Definition: optimizer.h:44
ndarray::Array< Scalar const, 1, 1 > fixed
Opaque fixed parameters in specialized units.
Definition: CModel.h:323
afw::table::BaseCatalog history
Trace of the optimizer&#39;s path, if enabled by diagnostic options.
Definition: CModel.h:325
Master result object for CModel, containing results for the final linear fit and three nested CModelS...
Definition: CModel.h:333
boost::shared_ptr< Prior > getPrior() const
Result object for a single nonlinear fitting stage of the CModel algorithm.
Definition: CModel.h:293
OptimizerControl optimizer
"Configuration for how the objective surface is explored. Ignored for forced fitting" ; ...
Definition: CModel.h:215
py::object result
Definition: _schema.cc:429
#define PTR(...)
Definition: base.h:41
boost::shared_ptr< Model > getModel() const
A concrete Likelihood class that does not require its parameters and data to be in the same UnitSyste...
Input centroid did not land within the fit region.
Definition: CModel.h:356
Scalar instFlux
Flux measured from just this stage fit.
Definition: CModel.h:314
CModelStageResult exp
Results from the exponential (Sersic n=1) fit.
Definition: CModel.h:371