SourceXtractorPlusPlus  0.8
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Example2.cpp
Go to the documentation of this file.
1 
23 #include <iostream>
24 #include <random>
25 #include <functional>
26 #include <vector>
38 #include "utils.h"
39 
40 using namespace std;
41 using namespace ModelFitting;
42 
43 // This example demonstrates how to create a FrameModel with a single constant
44 // model and a random number of point and extended models.
45 
46 int main() {
47 
48  // First we create the random number generators we will use. We create one that
49  // creates random intengers in the range [10,20] and one that creates random
50  // doubles in the range [0.,1.]
51  random_device rd;
52  default_random_engine generator {rd()};
53  uniform_int_distribution<int> int_distribution {10, 20};
54  auto rand_int = bind(int_distribution, generator);
55  uniform_real_distribution<double> double_distribution {0., 1.};
56  auto rand_double = bind(double_distribution, generator);
57 
58  // To create a FrameModel we need to specify its size and its pixel scale
59  double pixel_scale = .1;
60  std::size_t width = 401;
61  std::size_t height = 201;
62 
63  // The FrameModel can contain any number of constant, point and extended
64  // models. Here we construct the vectors which will keep these models.
65  vector<ConstantModel> constant_models {};
66  vector<PointModel> point_models {};
67  vector<TransformedModel> extended_models {};
68 
69  // We will use a single constant model, which simulates the background. The
70  // constant model gets a single parameter, its value.
71  auto back_value = std::make_shared<ManualParameter>(1E-6);
72  constant_models.emplace_back(back_value);
73 
74  // We add a random number of random point sources
75  for (auto point_no=rand_int(); point_no>0; --point_no) {
76  // The point sources get three parameters, its position on the frame (in
77  // pixels) and its value
78  auto x = std::make_shared<ManualParameter>(rand_double() * width);
79  auto y = std::make_shared<ManualParameter>(rand_double() * height);
80  auto value = std::make_shared<ManualParameter>(rand_double() * 5.);
81  point_models.emplace_back(x, y, value);
82  }
83 
84  // We add a random number of random extended sources
85  for (auto ext_no=rand_int(); ext_no>0; --ext_no) {
86  // The extended source gets the following parameters:
87  // - Its model components
89  // - Its scale factors
90  auto x_scale = std::make_shared<ManualParameter>((rand_double() * .4) + .1);
91  auto y_scale = std::make_shared<ManualParameter>((rand_double() * .4) + .1);
92  // - Its rotation angle
93  auto rot_angle = std::make_shared<ManualParameter>(rand_double() * M_PI);
94  // - Its position on the frame
95  auto x = std::make_shared<ManualParameter>(rand_double() * width);
96  auto y = std::make_shared<ManualParameter>(rand_double() * height);
97  // - Its size in arcsec (??? from detection step ???)
98  double width = 15. * std::max(x_scale->getValue(), y_scale->getValue());
99  double height = 15. * std::max(x_scale->getValue(), y_scale->getValue());
100 
101  // We add two model components, an exponential:
102  auto exp_i0 = std::make_shared<ManualParameter>(rand_double() * 1E2 + .1);
103  auto exp_n = std::make_shared<ManualParameter>(1.);
104  auto exp_k = std::make_shared<ManualParameter>(1.);
105  auto exp_reg_man = make_unique<OnlySmooth>();
106  auto exp = make_unique<SersicModelComponent>(move(exp_reg_man), exp_i0, exp_n, exp_k);
107  component_list.emplace_back(move(exp));
108  // and a De Vaucouleurs
109  auto dev_i0 = std::make_shared<ManualParameter>(rand_double() * 5E3 + 1.);
110  auto dev_n = std::make_shared<ManualParameter>(4.);
111  auto dev_k = std::make_shared<ManualParameter>(7.66924944);
112  auto dev_reg_man = make_unique<AutoSharp>();
113  auto dev = make_unique<SersicModelComponent>(move(dev_reg_man), dev_i0, dev_n, dev_k);
114 
115  // Finally we create the extended model and we add it to the list
116  extended_models.emplace_back(std::move(component_list), x_scale, y_scale,
117  rot_angle, width, height, x, y);
118  }
119 
120  // The FrameModel needs a PSF so it can convolve its models. The type of the
121  // PSF is abstracted as a template parameter. The following creates an instance
122  // of OpenCvPsf (which is a concrete implementation of the interface) from a
123  // FITS file.
124  // Note that we use the PathSearch tool of elements to locate the file in the
125  // auxdir directory
126  auto psf_path = Elements::pathSearchInEnvVariable("psf.fits", "ELEMENTS_AUX_PATH");
127  auto psf = readPsf(psf_path[0].string());
128 
129  // Finally we can create the FrameModel. Note that we must include the file
130  // ModelFitting/Image/OpenCvMatImageTraits.h to enable the ImageTraits for
131  // the cv::Mat type.
132  FrameModel<OpenCvPsf, cv::Mat> frame_model {
133  pixel_scale, width, height, move(constant_models),
134  move(point_models), move(extended_models), move(psf)
135  };
136 
137  //
138  // Model demonstration
139  //
140  // As an example of using the FrameModel we get its image and we store it in
141  // a file
142  auto image = frame_model.getImage();
143  writeToFits(image, "example2.fits");
144 
145 }
T exp(T...args)
int main()
Definition: Example1.cpp:47
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
ModelFitting::OpenCvPsf readPsf(const std::string &filename)
Definition: utils.h:53
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
T bind(T...args)
T max(T...args)
T move(T...args)
void writeToFits(const cv::Mat &image, const std::string &filename)
Definition: utils.h:40
STL class.
ELEMENTS_API std::vector< boost::filesystem::path > pathSearchInEnvVariable(const std::string &file_name, const std::string &path_like_env_variable, SearchType search_type=SearchType::Recursive)
const double pixel_scale
Definition: TestImage.cpp:72
T emplace_back(T...args)