SourceXtractorPlusPlus
0.21
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
SEImplementation
src
lib
Deblending
Cleaning.cpp
Go to the documentation of this file.
1
17
/*
18
* Cleaning.cpp
19
*
20
* Created on: 2018 M12 18
21
* Author: mschefer
22
*/
23
24
#include <vector>
25
#include <set>
26
#include <tuple>
27
28
#include "
SEFramework/Property/DetectionFrame.h
"
29
#include "
SEImplementation/Property/SourceId.h
"
30
#include "
SEImplementation/Property/PixelCoordinateList.h
"
31
32
#include "
SEImplementation/Plugin/MoffatModelFitting/MoffatModelFitting.h
"
33
#include "
SEImplementation/Plugin/MoffatModelFitting/MoffatModelEvaluator.h
"
34
35
#include "
SEImplementation/Plugin/DetectionFramePixelValues/DetectionFramePixelValues.h
"
36
37
#include "
SEImplementation/Deblending/Cleaning.h
"
38
39
namespace
SourceXtractor
{
40
41
inline
bool
operator<
(
SourceGroupInterface::iterator
a,
SourceGroupInterface::iterator
b) {
42
return
&(*a) < &(*b);
43
}
44
45
void
Cleaning::deblend
(
SourceGroupInterface
&
group
)
const
{
46
if
(
group
.size() <= 1) {
47
return
;
48
}
49
50
std::vector<SourceGroupInterface::iterator>
sources_to_clean
;
51
std::vector<SourceGroupInterface::iterator>
remaining_sources
;
52
53
// iterate through all sources
54
for
(
auto
it
=
group
.begin();
it
!=
group
.end(); ++
it
) {
55
if
(
shouldClean
(*
it
,
group
)) {
56
sources_to_clean
.push_back(
it
);
57
}
else
{
58
remaining_sources
.push_back(
it
);
59
}
60
}
61
62
if
(
sources_to_clean
.size() > 0) {
63
if
(
remaining_sources
.size() > 1) {
64
std::map<SourceGroupInterface::iterator, std::vector<SourceGroupInterface::iterator>
>
merging_map
;
65
for
(
auto
it
:
sources_to_clean
) {
66
auto
influential_source
=
findMostInfluentialSource
(*
it
,
remaining_sources
);
67
merging_map
[
influential_source
].push_back(
it
);
68
}
69
70
for
(
auto
merging_pair
:
merging_map
) {
71
if
(
merging_pair
.second.size() > 0) {
72
auto
new_source
=
mergeSources
(*
merging_pair
.first,
merging_pair
.second);
73
group
.addSource(
std::move
(
new_source
));
74
group
.removeSource(
merging_pair
.first);
75
}
76
}
77
}
else
if
(
remaining_sources
.size() == 1) {
78
auto
new_source
=
mergeSources
(*
remaining_sources
[0],
sources_to_clean
);
79
group
.addSource(
std::move
(
new_source
));
80
group
.removeSource(
remaining_sources
[0]);
81
}
82
83
for
(
auto
&
it
:
sources_to_clean
) {
84
group
.removeSource(
it
);
85
}
86
}
87
}
88
89
bool
Cleaning::shouldClean
(
SourceInterface
&
source
,
SourceGroupInterface
&
group
)
const
{
90
const
auto
& pixel_list =
source
.getProperty<
PixelCoordinateList
>().getCoordinateList();
91
92
std::vector<double>
group_influence
(pixel_list.size());
93
94
// iterate through all other sources in the group
95
for
(
auto
it
=
group
.begin();
it
!=
group
.end(); ++
it
) {
96
if
(&(*
it
) == &
source
) {
// skip self
97
continue
;
98
}
99
100
auto
&model =
it
->getProperty<
MoffatModelEvaluator
>();
101
int
i
= 0;
102
for
(
auto
pixel
: pixel_list) {
103
auto
pixel_value
= model.getValue(
pixel
.m_x,
pixel
.m_y);
104
group_influence
[
i
++] +=
pixel_value
;
105
}
106
}
107
108
unsigned
int
still_valid_pixels
= 0;
109
const
auto
&
pixel_values
=
source
.getProperty<
DetectionFramePixelValues
>().getFilteredValues();
110
int
i
= 0;
111
for
(
auto
value :
pixel_values
) {
112
if
(value >
group_influence
[
i
++]) {
113
still_valid_pixels
++;
114
}
115
}
116
117
return
still_valid_pixels
<
m_min_area
;
118
}
119
120
SourceGroupInterface::iterator
Cleaning::findMostInfluentialSource
(
121
SourceInterface
&
source
,
const
std::vector<SourceGroupInterface::iterator>
&
candidates
)
const
{
122
123
const
auto
& pixel_list =
source
.getProperty<
PixelCoordinateList
>().getCoordinateList();
124
125
std::vector<double>
total_influence_of_sources
(
candidates
.size());
126
127
// iterate through all other sources in the group
128
for
(
size_t
i
= 0;
i
<
candidates
.size();
i
++) {
129
auto
&model =
candidates
[
i
]->getProperty<
MoffatModelEvaluator
>();
130
for
(
auto
pixel
: pixel_list) {
131
auto
pixel_value
= model.getValue(
pixel
.m_x,
pixel
.m_y);
132
total_influence_of_sources
[
i
] +=
pixel_value
;
133
}
134
}
135
136
SourceGroupInterface::iterator
most_influential_source
(
candidates
[0]);
137
double
most_influential_source_value
= 0;
138
for
(
size_t
i
= 0;
i
<
candidates
.size();
i
++) {
139
if
(
total_influence_of_sources
[
i
] >=
most_influential_source_value
) {
140
most_influential_source
=
candidates
[
i
];
141
most_influential_source_value
=
total_influence_of_sources
[
i
];
142
}
143
}
144
return
most_influential_source
;
145
}
146
147
std::unique_ptr<SourceInterface>
Cleaning::mergeSources
(
SourceInterface
&
parent
,
148
const
std::vector<SourceGroupInterface::iterator>
children
)
const
{
149
150
// Start with a copy of the pixel list of the parent
151
auto
pixel_list =
parent
.getProperty<
PixelCoordinateList
>().getCoordinateList();
152
153
// Merge the pixel lists of all the child sources
154
for
(
const
auto
&
child
:
children
) {
155
const
auto
&
pixel_list_to_merge
=
child
->getProperty<
PixelCoordinateList
>().getCoordinateList();
156
pixel_list.insert(pixel_list.end(),
pixel_list_to_merge
.begin(),
pixel_list_to_merge
.end());
157
}
158
159
// Create a new source with the minimum necessary properties
160
auto
new_source
=
m_source_factory
->createSource();
161
new_source
->setProperty<
PixelCoordinateList
>(pixel_list);
162
new_source
->setProperty<
DetectionFrame
>(
parent
.getProperty<
DetectionFrame
>().getEncapsulatedFrame());
163
new_source
->setProperty<
SourceId
>(
parent
.getProperty<
SourceId
>().getSourceId());
164
165
return
new_source
;
166
}
167
168
std::set<PropertyId>
Cleaning::requiredProperties
()
const
{
169
return
{
170
PropertyId::create<PixelCoordinateList>(),
171
PropertyId::create<MoffatModelEvaluator>()
172
};
173
}
174
175
}
176
Cleaning.h
DetectionFramePixelValues.h
DetectionFrame.h
MoffatModelEvaluator.h
MoffatModelFitting.h
PixelCoordinateList.h
SourceId.h
SourceXtractor::Cleaning::mergeSources
std::unique_ptr< SourceInterface > mergeSources(SourceInterface &parent, const std::vector< SourceGroupInterface::iterator > children) const
Definition
Cleaning.cpp:147
SourceXtractor::Cleaning::findMostInfluentialSource
SourceGroupInterface::iterator findMostInfluentialSource(SourceInterface &source, const std::vector< SourceGroupInterface::iterator > &candidates) const
Definition
Cleaning.cpp:120
SourceXtractor::Cleaning::m_min_area
unsigned int m_min_area
Definition
Cleaning.h:57
SourceXtractor::Cleaning::deblend
void deblend(SourceGroupInterface &group) const override
Performs the DeblendStep on the SourceGroup.
Definition
Cleaning.cpp:45
SourceXtractor::Cleaning::m_source_factory
std::shared_ptr< SourceFactory > m_source_factory
Definition
Cleaning.h:56
SourceXtractor::Cleaning::requiredProperties
std::set< PropertyId > requiredProperties() const override
Returns properties used by the deblend step.
Definition
Cleaning.cpp:168
SourceXtractor::Cleaning::shouldClean
bool shouldClean(SourceInterface &source, SourceGroupInterface &group) const
Definition
Cleaning.cpp:89
SourceXtractor::DetectionFramePixelValues
The values of a Source's pixels in the detection image. They are returned as a vector in the same ord...
Definition
DetectionFramePixelValues.h:39
SourceXtractor::DetectionFrame
Definition
DetectionFrame.h:33
SourceXtractor::MoffatModelEvaluator
Definition
MoffatModelEvaluator.h:35
SourceXtractor::PixelCoordinateList
Definition
PixelCoordinateList.h:33
SourceXtractor::SourceGroupInterface
Defines the interface used to group sources.
Definition
SourceGroupInterface.h:38
SourceXtractor::SourceGroupInterface::iterator
std::list< SourceWrapper >::iterator iterator
Definition
SourceGroupInterface.h:78
SourceXtractor::SourceId
Definition
SourceId.h:32
SourceXtractor::SourceInterface
The SourceInterface is an abstract "source" that has properties attached to it.
Definition
SourceInterface.h:46
std::function
std::move
T move(T... args)
SourceXtractor
Definition
Aperture.h:30
SourceXtractor::operator<
bool operator<(std::reference_wrapper< const SourceInterface > a, std::reference_wrapper< const SourceInterface > b)
Definition
FlexibleModelFittingParameterManager.h:38
Generated by
1.10.0