Skip to content

Commit f106c47

Browse files
authored
Merge pull request #795 from dirac-institute/no_stamp_filter
Remove C++ Stamp Filtering
2 parents 5e98264 + 8eef106 commit f106c47

18 files changed

+40
-655
lines changed

docs/source/user_manual/results_filtering.rst

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,6 @@ Relevant light curve filtering parameters include:
2121
* ``sigmaG_lims`` - The percentiles for sigmaG filtering (default of [25, 75]).
2222

2323

24-
Stamp Filtering
25-
---------------
26-
27-
The stamp filtering stage is only applied if the ``do_stamp_filter`` parameter is set to True. This stage creates a single stamp representing the sum, mean, median, or variance weighted mean of pixel values for the stamps at each time step. The stamp type is defined by the ``stamp_type`` parameter and can take on values ``median``, ``mean``, ``sum``, or ``weighted`` (for variance weighted). All of the stamp types drop masked pixels from their computations. The mean and median sums are computed over only the valid time steps from the light curve filtering phase (dropping stamps with outlier fluxes). The sum coadd uses all the time steps regardless of the first phase of filtering.
28-
29-
The stamps are filtered based on how closely the pixel values in the stamp image represent a Gaussian defined with the parameters:
30-
31-
* ``center_thresh`` - The percentage of flux in the central pixel. For example setting this to 0.9 will require that the central pixel of the stamp has 90 percent of all the flux in the stamp.
32-
* ``peak_offset`` - How far the brightest pixel is from the center of the stamp (in pixels). For example a peak offset of [2.0, 3.0] requires that the brightest pixel in the stamp is at most 2 pixels from the center in the x-dimension and 3-pixels from the center in the y-dimension.
33-
* ``mom_lims`` - Compute the Gaussian moments of the image and compares them to the thresholds.
34-
35-
Relevant stamp filtering parameters include:
36-
37-
* ``center_thresh`` - The percentage of flux in the central pixel.
38-
* ``chunk_size`` - The number of candidate trajectories to filter in a batch. Used to control memory usage.
39-
* ``do_stamp_filter`` - A Boolean indicating whether to generate and filter stamps.
40-
* ``peak_offset`` - A length 2 list indicating how far the peak is from the center of the stamp in each of the x and y dimensions.
41-
* ``mom_lims`` - A length 5 list providing thresholds on the images moments.
42-
* ``stamp_radius`` - The radius of the stamps to use.
43-
44-
Note that stamps are only generated and output into files if ``do_stamp_filter`` is set to true.
45-
46-
4724
Clustering
4825
----------
4926

docs/source/user_manual/search_params.rst

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ Configuration Parameters
3333
+------------------------+-----------------------------+----------------------------------------+
3434
| **Parameter** | **Default Value** | **Interpretation** |
3535
+------------------------+-----------------------------+----------------------------------------+
36-
| ``center_thresh`` | 0.00 | The minimum fraction of total flux |
37-
| | | within a stamp that must be contained |
38-
| | | in the central pixel |
39-
| | | (if ``do_stamp_filter=True``). |
40-
+------------------------+-----------------------------+----------------------------------------+
4136
| ``chunk_size`` | 500000 | The batch size to use when processing |
4237
| | | the results of the on-GPU search. |
4338
+------------------------+-----------------------------+----------------------------------------+
@@ -76,9 +71,6 @@ Configuration Parameters
7671
+------------------------+-----------------------------+----------------------------------------+
7772
| ``do_mask`` | True | Apply the mask to the raw pixels. |
7873
+------------------------+-----------------------------+----------------------------------------+
79-
| ``do_stamp_filter`` | True | Apply post-search filtering on the |
80-
| | | image stamps. |
81-
+------------------------+-----------------------------+----------------------------------------+
8274
| ``encode_num_bytes`` | -1 | The number of bytes to use to encode |
8375
| | | ``psi`` and ``phi`` images on GPU. By |
8476
| | | default a ``float`` encoding is used. |
@@ -113,20 +105,10 @@ Configuration Parameters
113105
| | | computed likelihood above this |
114106
| | | threshold are rejected. |
115107
+------------------------+-----------------------------+----------------------------------------+
116-
| ``mom_lims`` | [35.5, 35.5, 2.0, 0.3, 0.3] | Thresholds for the moments of a |
117-
| | | Gaussian fit to the flux, specified as |
118-
| | | ``[xx, yy, xy, x, y]``. |
119-
| | | If ``do_stamp_filter=True``. |
120-
+------------------------+-----------------------------+----------------------------------------+
121108
| ``num_obs`` | 10 | The minimum number of non-masked |
122109
| | | observations for the object to be |
123110
| | | accepted. |
124111
+------------------------+-----------------------------+----------------------------------------+
125-
| ``peak_offset`` | [2.0, 2.0] | How far, in pixels, the brightest pixel|
126-
| | | in the stamp can be from the central |
127-
| | | pixel in each direction ``[x,y]``. |
128-
| | | If ``do_stamp_filter=True``). |
129-
+------------------------+-----------------------------+----------------------------------------+
130112
| ``psf_val`` | 1.4 | The value for the standard deviation of|
131113
| | | the point spread function (PSF). |
132114
+------------------------+-----------------------------+----------------------------------------+
@@ -148,9 +130,8 @@ Configuration Parameters
148130
| | | creating a stamp for stamp filtering |
149131
| | | (in pixels). |
150132
+------------------------+-----------------------------+----------------------------------------+
151-
| ``stamp_type`` | sum | The type of coadd to use during stamp |
152-
| | | filtering (if ``do_stamp_filter=True``)|
153-
| | | if: |
133+
| ``stamp_type`` | sum | The type of coadd to use as the main |
134+
| | | stamp: |
154135
| | | * ``sum`` - (default) Per pixel sum |
155136
| | | * ``median`` - Per pixel median |
156137
| | | * ``mean`` - Per pixel mean |

src/kbmod/configuration.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def __init__(self):
1717
self._required_params = set()
1818

1919
self._params = {
20-
"center_thresh": 0.00,
2120
"chunk_size": 500000,
2221
"clip_negative": False,
2322
"cluster_eps": 20.0,
@@ -27,7 +26,6 @@ def __init__(self):
2726
"debug": False,
2827
"do_clustering": True,
2928
"do_mask": True,
30-
"do_stamp_filter": True,
3129
"encode_num_bytes": -1,
3230
"generator_config": {
3331
"name": "EclipticCenteredSearch",
@@ -41,9 +39,7 @@ def __init__(self):
4139
"im_filepath": None,
4240
"lh_level": 10.0,
4341
"max_lh": 1000.0,
44-
"mom_lims": [35.5, 35.5, 2.0, 0.3, 0.3],
4542
"num_obs": 10,
46-
"peak_offset": [2.0, 2.0],
4743
"psf_val": 1.4,
4844
"result_filename": None,
4945
"results_per_pixel": 8,

src/kbmod/filters/stamp_filters.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,10 @@ def extract_search_parameters_from_config(config):
6363
else:
6464
raise ValueError(f"Unrecognized stamp type: {stamp_type}")
6565

66-
# Filtering parameters (with validity checking)
67-
params.do_filtering = config["do_stamp_filter"]
68-
params.center_thresh = config["center_thresh"]
69-
70-
peak_offset = config["peak_offset"]
71-
if len(peak_offset) != 2:
72-
raise ValueError(f"Expected length 2 list for peak_offset. Found {peak_offset}")
73-
params.peak_offset_x = peak_offset[0]
74-
params.peak_offset_y = peak_offset[1]
75-
76-
mom_lims = config["mom_lims"]
77-
if len(mom_lims) != 5:
78-
raise ValueError(f"Expected length 5 list for mom_lims. Found {mom_lims}")
79-
params.m20_limit = mom_lims[0]
80-
params.m02_limit = mom_lims[1]
81-
params.m11_limit = mom_lims[2]
82-
params.m10_limit = mom_lims[3]
83-
params.m01_limit = mom_lims[4]
84-
8566
return params
8667

8768

88-
def get_coadds_and_filter_results(result_data, im_stack, stamp_params, chunk_size=1_000_000, colname="stamp"):
69+
def make_coadds(result_data, im_stack, stamp_params, chunk_size=1_000_000, colname="stamp"):
8970
"""Create the co-added postage stamps and filter them based on their statistical
9071
properties. Results with stamps that are similar to a Gaussian are kept.
9172
@@ -157,17 +138,12 @@ def get_coadds_and_filter_results(result_data, im_stack, stamp_params, chunk_siz
157138
# collecting RawImage but leaving a dangling ref to the attribute.
158139
# That's a fix for another time so I'm leaving it as a copy here
159140
for ind, stamp in enumerate(stamps_slice):
160-
if stamp.width > 1:
161-
stamps_to_keep.append(np.array(stamp.image))
162-
keep_row[start_idx + ind] = True
141+
stamps_to_keep.append(np.array(stamp.image))
142+
keep_row[start_idx + ind] = True
163143

164144
# Move to the next chunk.
165145
start_idx += chunk_size
166146

167-
# Do the actual filtering of results
168-
if stamp_params.do_filtering:
169-
result_data.filter_rows(keep_row, label="stamp_filter")
170-
171147
# Append the coadded stamps to the results. We do this after the filtering
172148
# so we are not adding a jagged array.
173149
result_data.table[colname] = np.array(stamps_to_keep)
@@ -195,7 +171,6 @@ def append_coadds(result_data, im_stack, coadd_types, radius, chunk_size=100_000
195171

196172
params = StampParameters()
197173
params.radius = radius
198-
params.do_filtering = False
199174

200175
# Loop through all the coadd types in the list, generating a corresponding stamp.
201176
for coadd_type in coadd_types:
@@ -213,7 +188,7 @@ def append_coadds(result_data, im_stack, coadd_types, radius, chunk_size=100_000
213188
raise ValueError(f"Unrecognized stamp type: {coadd_type}")
214189

215190
# Do the generation (without filtering).
216-
get_coadds_and_filter_results(
191+
make_coadds(
217192
result_data,
218193
im_stack,
219194
params,

src/kbmod/run_search.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
from .configuration import SearchConfiguration
1010
from .filters.clustering_filters import apply_clustering
1111
from .filters.sigma_g_filter import apply_clipped_sigma_g, SigmaGClipping
12-
from .filters.stamp_filters import append_all_stamps, append_coadds, get_coadds_and_filter_results
12+
from .filters.stamp_filters import (
13+
append_all_stamps,
14+
append_coadds,
15+
extract_search_parameters_from_config,
16+
make_coadds,
17+
)
1318

1419
from .results import Results
1520
from .trajectory_generator import create_trajectory_generator
@@ -238,11 +243,6 @@ def run_search(
238243
trj_generator = create_trajectory_generator(config, work_unit=None)
239244
keep = self.do_gpu_search(config, stack, trj_generator)
240245

241-
if config["do_stamp_filter"]:
242-
stack.copy_to_gpu()
243-
get_coadds_and_filter_results(keep, stack, config)
244-
stack.clear_from_gpu()
245-
246246
if config["do_clustering"]:
247247
cluster_timer = kb.DebugTimer("clustering", logger)
248248
mjds = [stack.get_obstime(t) for t in range(stack.img_count())]
@@ -255,12 +255,14 @@ def run_search(
255255
apply_clustering(keep, cluster_params)
256256
cluster_timer.stop()
257257

258-
# Generate additional coadded stamps without filtering.
258+
# Generate coadded stamps without filtering -- both the "stamp" column
259+
# as well as any additional coadds.
260+
stamp_params = extract_search_parameters_from_config(config)
261+
make_coadds(keep, stack, stamp_params, colname="stamp")
259262
if len(config["coadds"]) > 0:
260263
stack.copy_to_gpu()
261264
append_coadds(keep, stack, config["coadds"], config["stamp_radius"])
262265
stack.clear_from_gpu()
263-
logger.info(f"Found {len(keep)} potential trajectories.")
264266

265267
# Extract all the stamps for all time steps and append them onto the result rows.
266268
if config["save_all_stamps"]:

src/kbmod/search/bindings.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ PYBIND11_MODULE(search, m) {
4242
search::stack_search_bindings(m);
4343
search::stamp_creator_bindings(m);
4444
search::trajectory_bindings(m);
45-
search::image_moments_bindings(m);
4645
search::stamp_parameters_bindings(m);
4746
search::psi_phi_array_binding(m);
4847
search::debug_timer_binding(m);

src/kbmod/search/common.h

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -142,50 +142,9 @@ struct SearchParameters {
142142
struct StampParameters {
143143
int radius = 10;
144144
StampType stamp_type = STAMP_SUM;
145-
bool do_filtering = false;
146-
147-
// Thresholds on the location of the image peak.
148-
float center_thresh;
149-
float peak_offset_x;
150-
float peak_offset_y;
151-
152-
// Limits on the moments.
153-
float m01_limit;
154-
float m10_limit;
155-
float m11_limit;
156-
float m02_limit;
157-
float m20_limit;
158-
159-
const std::string to_string() const {
160-
// If filtering is turned off, output the minimal information on a single line.
161-
// Otherwise dump the full statistics on multiple lines.
162-
if (!do_filtering) {
163-
return ("Type: " + std::to_string(stamp_type) + " Radius: " + std::to_string(radius) +
164-
" Filtering: false");
165-
} else {
166-
return ("Type: " + std::to_string(stamp_type) + "\nRadius: " + std::to_string(radius) +
167-
"\nFiltering: true" + "\nCenter Thresh: " + std::to_string(center_thresh) +
168-
"\nPeak Offset: x=" + std::to_string(peak_offset_x) + " y=" +
169-
std::to_string(peak_offset_y) + "\nMoment Limits: m01=" + std::to_string(m01_limit) +
170-
" m10=" + std::to_string(m10_limit) + " m11=" + std::to_string(m11_limit) +
171-
" m02=" + std::to_string(m02_limit) + " m20=" + std::to_string(m02_limit));
172-
}
173-
}
174-
};
175-
176-
// Basic image moments use for analysis.
177-
struct ImageMoments {
178-
float m00;
179-
float m01;
180-
float m10;
181-
float m11;
182-
float m02;
183-
float m20;
184145

185146
const std::string to_string() const {
186-
return "m00: " + std::to_string(m00) + " m01: " + std::to_string(m01) +
187-
" m10: " + std::to_string(m10) + " m11: " + std::to_string(m11) +
188-
" m02: " + std::to_string(m02) + " m20: " + std::to_string(m20);
147+
return ("Type: " + std::to_string(stamp_type) + " Radius: " + std::to_string(radius));
189148
}
190149
};
191150

@@ -225,33 +184,12 @@ static void trajectory_bindings(py::module &m) {
225184
}));
226185
}
227186

228-
static void image_moments_bindings(py::module &m) {
229-
py::class_<ImageMoments>(m, "ImageMoments", pydocs::DOC_ImageMoments)
230-
.def(py::init<>())
231-
.def("__str__", &ImageMoments::to_string)
232-
.def_readwrite("m00", &ImageMoments::m00)
233-
.def_readwrite("m01", &ImageMoments::m01)
234-
.def_readwrite("m10", &ImageMoments::m10)
235-
.def_readwrite("m11", &ImageMoments::m11)
236-
.def_readwrite("m02", &ImageMoments::m02)
237-
.def_readwrite("m20", &ImageMoments::m20);
238-
}
239-
240187
static void stamp_parameters_bindings(py::module &m) {
241188
py::class_<StampParameters>(m, "StampParameters", pydocs::DOC_StampParameters)
242189
.def(py::init<>())
243190
.def("__str__", &StampParameters::to_string)
244191
.def_readwrite("radius", &StampParameters::radius)
245-
.def_readwrite("stamp_type", &StampParameters::stamp_type)
246-
.def_readwrite("do_filtering", &StampParameters::do_filtering)
247-
.def_readwrite("center_thresh", &StampParameters::center_thresh)
248-
.def_readwrite("peak_offset_x", &StampParameters::peak_offset_x)
249-
.def_readwrite("peak_offset_y", &StampParameters::peak_offset_y)
250-
.def_readwrite("m01_limit", &StampParameters::m01_limit)
251-
.def_readwrite("m10_limit", &StampParameters::m10_limit)
252-
.def_readwrite("m11_limit", &StampParameters::m11_limit)
253-
.def_readwrite("m02_limit", &StampParameters::m02_limit)
254-
.def_readwrite("m20_limit", &StampParameters::m20_limit);
192+
.def_readwrite("stamp_type", &StampParameters::stamp_type);
255193
}
256194

257195
#endif /* Py_PYTHON_H */

src/kbmod/search/pydocs/raw_image_docs.h

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -205,50 +205,6 @@ static const auto DOC_RawImage_compute_bounds = R"doc(
205205
A ``(min, max)`` tuple.
206206
)doc";
207207

208-
static const auto DOC_RawImage_find_peak = R"doc(
209-
Returns the pixel coordinates of the maximum value.
210-
211-
Parameters
212-
----------
213-
furthest_from_center : `bool`
214-
When `True`, and multiple identical maxima exist, returns the one that is
215-
at a greatest L2 distance from the center of the image. Otherwise it
216-
returns the last maxima that was found in a row-wise ordered search.
217-
218-
Returns
219-
-------
220-
location : `Index`, optional
221-
Index of the maximum.
222-
)doc";
223-
224-
static const auto DOC_RawImage_find_central_moments = R"doc(
225-
Returns the central moments of the image.
226-
227-
Returns
228-
-------
229-
moments : `ImageMoments`
230-
Image moments.
231-
)doc";
232-
233-
static const auto DOC_RawImage_center_is_local_max = R"doc(
234-
A filter on whether the center of the stamp is a local
235-
maxima and the percentage of the stamp's total flux in this
236-
pixel.
237-
238-
Parameters
239-
----------
240-
local_max : ``bool``
241-
Require the central pixel to be a local maximum.
242-
flux_thresh : ``float``
243-
The fraction of the stamp's total flux that needs to be in
244-
the center pixel [0.0, 1.0].
245-
246-
Returns
247-
-------
248-
keep_row : `bool`
249-
Whether or not the stamp passes the check.
250-
)doc";
251-
252208
static const auto DOC_RawImage_create_stamp = R"doc(
253209
Create an image stamp around a given region.
254210

src/kbmod/search/pydocs/stamp_creator_docs.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,6 @@ static const auto DOC_StampCreator_get_coadded_stamps = R"doc(
141141
142142
)doc";
143143

144-
static const auto DOC_StampCreator_filter_stamp = R"doc(
145-
Filters stamps based on the given parameters.
146-
147-
Applies the following filters: peak position, percent flux at central pixel,
148-
and image moments.
149-
150-
Parameters
151-
----------
152-
img : `RawImage`
153-
The image to test.
154-
params : `StampParameters`
155-
The parameters for stamp generation and filtering.
156-
157-
Returns
158-
-------
159-
`bool`
160-
Whether or not to filter the stamp.
161-
)doc";
162-
163144
static const auto DOC_StampCreator_create_variance_stamps = R"doc(
164145
Create a vector of stamps from the variance layer centered on the
165146
predicted position of an Trajectory at different times.

0 commit comments

Comments
 (0)