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