spikemask-rs 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- spikemask_rs-0.1.0/.cargo/config.toml +12 -0
- spikemask_rs-0.1.0/CHANGELOG.rst +8 -0
- spikemask_rs-0.1.0/Cargo.lock +280 -0
- spikemask_rs-0.1.0/Cargo.toml +22 -0
- spikemask_rs-0.1.0/LICENSE +29 -0
- spikemask_rs-0.1.0/PKG-INFO +48 -0
- spikemask_rs-0.1.0/README.md +13 -0
- spikemask_rs-0.1.0/pyproject.toml +134 -0
- spikemask_rs-0.1.0/python/spikemask_rs/__init__.py +11 -0
- spikemask_rs-0.1.0/python/spikemask_rs/_workers.py +55 -0
- spikemask_rs-0.1.0/python/spikemask_rs/detection.py +77 -0
- spikemask_rs-0.1.0/python/spikemask_rs/tests/__init__.py +0 -0
- spikemask_rs-0.1.0/python/spikemask_rs/tests/test_c_parity.py +55 -0
- spikemask_rs-0.1.0/python/spikemask_rs/tests/test_detection.py +118 -0
- spikemask_rs-0.1.0/python/spikemask_rs/tests/test_workers.py +69 -0
- spikemask_rs-0.1.0/src/api.rs +68 -0
- spikemask_rs-0.1.0/src/detect.rs +210 -0
- spikemask_rs-0.1.0/src/lib.rs +16 -0
- spikemask_rs-0.1.0/src/tests/mod.rs +66 -0
- spikemask_rs-0.1.0/src/workers.rs +32 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# See https://pyo3.rs/latest/building-and-distribution.html#macos
|
|
2
|
+
[target.x86_64-apple-darwin]
|
|
3
|
+
rustflags = [
|
|
4
|
+
"-C", "link-arg=-undefined",
|
|
5
|
+
"-C", "link-arg=dynamic_lookup",
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
[target.aarch64-apple-darwin]
|
|
9
|
+
rustflags = [
|
|
10
|
+
"-C", "link-arg=-undefined",
|
|
11
|
+
"-C", "link-arg=dynamic_lookup",
|
|
12
|
+
]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
0.1.0 (2026-11-20)
|
|
2
|
+
==================
|
|
3
|
+
|
|
4
|
+
New Features
|
|
5
|
+
------------
|
|
6
|
+
|
|
7
|
+
- Added ``spikemask_rs.spike_mask``, which flags candidate cosmic-ray hits in a two-dimensional detector exposure by iterative upper sigma clipping. It defaults to a seven-row, five-column window and a cut of 3.0 standard deviations; ``window=(5, 3)`` and ``sigma_upper=2.65`` reproduce the MUSE IDL demo. (#2)
|
|
8
|
+
- Added the ``workers`` option of ``spikemask_rs.spike_mask`` and ``spikemask_rs.set_workers``, which set the number of Rayon threads for one call and for a block of calls. (#2)
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "autocfg"
|
|
7
|
+
version = "1.5.1"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "crossbeam-deque"
|
|
13
|
+
version = "0.8.8"
|
|
14
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
+
checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
|
|
16
|
+
dependencies = [
|
|
17
|
+
"crossbeam-epoch",
|
|
18
|
+
"crossbeam-utils",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[[package]]
|
|
22
|
+
name = "crossbeam-epoch"
|
|
23
|
+
version = "0.9.21"
|
|
24
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
25
|
+
checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
|
|
26
|
+
dependencies = [
|
|
27
|
+
"crossbeam-utils",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[[package]]
|
|
31
|
+
name = "crossbeam-utils"
|
|
32
|
+
version = "0.8.23"
|
|
33
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
34
|
+
checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
|
|
35
|
+
|
|
36
|
+
[[package]]
|
|
37
|
+
name = "either"
|
|
38
|
+
version = "1.19.0"
|
|
39
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
40
|
+
checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
|
|
41
|
+
|
|
42
|
+
[[package]]
|
|
43
|
+
name = "heck"
|
|
44
|
+
version = "0.5.0"
|
|
45
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
46
|
+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
|
47
|
+
|
|
48
|
+
[[package]]
|
|
49
|
+
name = "libc"
|
|
50
|
+
version = "0.2.191"
|
|
51
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
52
|
+
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
|
53
|
+
|
|
54
|
+
[[package]]
|
|
55
|
+
name = "matrixmultiply"
|
|
56
|
+
version = "0.3.11"
|
|
57
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
58
|
+
checksum = "3f607c237553f086e7043417a51df26b2eb899d3caff94e6a67592ff992fedc7"
|
|
59
|
+
dependencies = [
|
|
60
|
+
"autocfg",
|
|
61
|
+
"rawpointer",
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
[[package]]
|
|
65
|
+
name = "ndarray"
|
|
66
|
+
version = "0.17.2"
|
|
67
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
68
|
+
checksum = "520080814a7a6b4a6e9070823bb24b4531daac8c4627e08ba5de8c5ef2f2752d"
|
|
69
|
+
dependencies = [
|
|
70
|
+
"matrixmultiply",
|
|
71
|
+
"num-complex",
|
|
72
|
+
"num-integer",
|
|
73
|
+
"num-traits",
|
|
74
|
+
"portable-atomic",
|
|
75
|
+
"portable-atomic-util",
|
|
76
|
+
"rawpointer",
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
[[package]]
|
|
80
|
+
name = "num-complex"
|
|
81
|
+
version = "0.4.6"
|
|
82
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
83
|
+
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
|
|
84
|
+
dependencies = [
|
|
85
|
+
"num-traits",
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
[[package]]
|
|
89
|
+
name = "num-integer"
|
|
90
|
+
version = "0.1.47"
|
|
91
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
92
|
+
checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b"
|
|
93
|
+
dependencies = [
|
|
94
|
+
"num-traits",
|
|
95
|
+
]
|
|
96
|
+
|
|
97
|
+
[[package]]
|
|
98
|
+
name = "num-traits"
|
|
99
|
+
version = "0.2.19"
|
|
100
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
101
|
+
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
|
102
|
+
dependencies = [
|
|
103
|
+
"autocfg",
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
[[package]]
|
|
107
|
+
name = "numpy"
|
|
108
|
+
version = "0.30.0"
|
|
109
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
110
|
+
checksum = "aa0b9111761885449e894981fa8051dcf69e0bd114aecbd4bcdd830b6be4ed92"
|
|
111
|
+
dependencies = [
|
|
112
|
+
"libc",
|
|
113
|
+
"ndarray",
|
|
114
|
+
"num-complex",
|
|
115
|
+
"num-integer",
|
|
116
|
+
"num-traits",
|
|
117
|
+
"pyo3",
|
|
118
|
+
"pyo3-build-config",
|
|
119
|
+
"rustc-hash",
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
[[package]]
|
|
123
|
+
name = "once_cell"
|
|
124
|
+
version = "1.21.4"
|
|
125
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
126
|
+
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
|
127
|
+
|
|
128
|
+
[[package]]
|
|
129
|
+
name = "portable-atomic"
|
|
130
|
+
version = "1.15.0"
|
|
131
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
132
|
+
checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
|
|
133
|
+
|
|
134
|
+
[[package]]
|
|
135
|
+
name = "portable-atomic-util"
|
|
136
|
+
version = "0.2.8"
|
|
137
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
138
|
+
checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715"
|
|
139
|
+
dependencies = [
|
|
140
|
+
"portable-atomic",
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
[[package]]
|
|
144
|
+
name = "proc-macro2"
|
|
145
|
+
version = "1.0.107"
|
|
146
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
147
|
+
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
|
148
|
+
dependencies = [
|
|
149
|
+
"unicode-ident",
|
|
150
|
+
]
|
|
151
|
+
|
|
152
|
+
[[package]]
|
|
153
|
+
name = "pyo3"
|
|
154
|
+
version = "0.30.1"
|
|
155
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
156
|
+
checksum = "229ca13b8a15bb0d1487b81ab975bfcd97513416499a8312011b6036e42e0de4"
|
|
157
|
+
dependencies = [
|
|
158
|
+
"libc",
|
|
159
|
+
"once_cell",
|
|
160
|
+
"portable-atomic",
|
|
161
|
+
"pyo3-build-config",
|
|
162
|
+
"pyo3-ffi",
|
|
163
|
+
"pyo3-macros",
|
|
164
|
+
]
|
|
165
|
+
|
|
166
|
+
[[package]]
|
|
167
|
+
name = "pyo3-build-config"
|
|
168
|
+
version = "0.30.1"
|
|
169
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
170
|
+
checksum = "70130e2b6dd1ee589a8788026a429561ca5c40fa7a2fa7914c2282eddae24700"
|
|
171
|
+
dependencies = [
|
|
172
|
+
"target-lexicon",
|
|
173
|
+
]
|
|
174
|
+
|
|
175
|
+
[[package]]
|
|
176
|
+
name = "pyo3-ffi"
|
|
177
|
+
version = "0.30.1"
|
|
178
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
179
|
+
checksum = "5e7ff73c555c286d212d4ce7e5fbda845a973c4f61bd5f160f5f2c3c2a04d1c4"
|
|
180
|
+
dependencies = [
|
|
181
|
+
"libc",
|
|
182
|
+
"pyo3-build-config",
|
|
183
|
+
]
|
|
184
|
+
|
|
185
|
+
[[package]]
|
|
186
|
+
name = "pyo3-macros"
|
|
187
|
+
version = "0.30.1"
|
|
188
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
189
|
+
checksum = "c46d2e5862c7bb8207e3b639f713dda3c499933444784154c11d013c5a691b60"
|
|
190
|
+
dependencies = [
|
|
191
|
+
"proc-macro2",
|
|
192
|
+
"pyo3-macros-backend",
|
|
193
|
+
"quote",
|
|
194
|
+
"syn",
|
|
195
|
+
]
|
|
196
|
+
|
|
197
|
+
[[package]]
|
|
198
|
+
name = "pyo3-macros-backend"
|
|
199
|
+
version = "0.30.1"
|
|
200
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
201
|
+
checksum = "38b89f066f2ae72a2aef52d1537ca2fcd104440ebfadee06e97aa90738a25b9b"
|
|
202
|
+
dependencies = [
|
|
203
|
+
"heck",
|
|
204
|
+
"proc-macro2",
|
|
205
|
+
"quote",
|
|
206
|
+
"syn",
|
|
207
|
+
]
|
|
208
|
+
|
|
209
|
+
[[package]]
|
|
210
|
+
name = "quote"
|
|
211
|
+
version = "1.0.47"
|
|
212
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
213
|
+
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
|
214
|
+
dependencies = [
|
|
215
|
+
"proc-macro2",
|
|
216
|
+
]
|
|
217
|
+
|
|
218
|
+
[[package]]
|
|
219
|
+
name = "rawpointer"
|
|
220
|
+
version = "0.2.1"
|
|
221
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
222
|
+
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
|
|
223
|
+
|
|
224
|
+
[[package]]
|
|
225
|
+
name = "rayon"
|
|
226
|
+
version = "1.12.0"
|
|
227
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
228
|
+
checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
|
|
229
|
+
dependencies = [
|
|
230
|
+
"either",
|
|
231
|
+
"rayon-core",
|
|
232
|
+
]
|
|
233
|
+
|
|
234
|
+
[[package]]
|
|
235
|
+
name = "rayon-core"
|
|
236
|
+
version = "1.13.0"
|
|
237
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
238
|
+
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
|
239
|
+
dependencies = [
|
|
240
|
+
"crossbeam-deque",
|
|
241
|
+
"crossbeam-utils",
|
|
242
|
+
]
|
|
243
|
+
|
|
244
|
+
[[package]]
|
|
245
|
+
name = "rustc-hash"
|
|
246
|
+
version = "2.1.3"
|
|
247
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
248
|
+
checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
|
|
249
|
+
|
|
250
|
+
[[package]]
|
|
251
|
+
name = "spikemask-rs"
|
|
252
|
+
version = "0.1.0"
|
|
253
|
+
dependencies = [
|
|
254
|
+
"numpy",
|
|
255
|
+
"pyo3",
|
|
256
|
+
"rayon",
|
|
257
|
+
]
|
|
258
|
+
|
|
259
|
+
[[package]]
|
|
260
|
+
name = "syn"
|
|
261
|
+
version = "3.0.9"
|
|
262
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
263
|
+
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
|
|
264
|
+
dependencies = [
|
|
265
|
+
"proc-macro2",
|
|
266
|
+
"quote",
|
|
267
|
+
"unicode-ident",
|
|
268
|
+
]
|
|
269
|
+
|
|
270
|
+
[[package]]
|
|
271
|
+
name = "target-lexicon"
|
|
272
|
+
version = "0.13.5"
|
|
273
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
274
|
+
checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca"
|
|
275
|
+
|
|
276
|
+
[[package]]
|
|
277
|
+
name = "unicode-ident"
|
|
278
|
+
version = "1.0.27"
|
|
279
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
280
|
+
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "spikemask-rs"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
rust-version = "1.99"
|
|
6
|
+
description = "Detect candidate cosmic-ray spikes in two-dimensional detector exposures"
|
|
7
|
+
license = "BSD-3-Clause"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
|
|
10
|
+
[lib]
|
|
11
|
+
name = "spikemask_rs"
|
|
12
|
+
crate-type = ["cdylib"]
|
|
13
|
+
|
|
14
|
+
[dependencies]
|
|
15
|
+
numpy = "0.30"
|
|
16
|
+
pyo3 = { version = "0.30", features = ["extension-module", "abi3-py312"] }
|
|
17
|
+
rayon = "1"
|
|
18
|
+
|
|
19
|
+
[profile.release]
|
|
20
|
+
lto = true
|
|
21
|
+
codegen-units = 1
|
|
22
|
+
strip = "symbols"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, The MUSE Instrument Team @ LMSAL
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spikemask-rs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Classifier: Development Status :: 3 - Alpha
|
|
5
|
+
Classifier: Intended Audience :: Science/Research
|
|
6
|
+
Classifier: Operating System :: MacOS
|
|
7
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
8
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Rust
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Astronomy
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
13
|
+
Requires-Dist: numpy>=2.0.0
|
|
14
|
+
Requires-Dist: spikemask-rs[tests,docs] ; extra == 'dev'
|
|
15
|
+
Requires-Dist: sphinx>=8 ; extra == 'docs'
|
|
16
|
+
Requires-Dist: furo ; extra == 'docs'
|
|
17
|
+
Requires-Dist: sphinx-copybutton ; extra == 'docs'
|
|
18
|
+
Requires-Dist: pytest ; extra == 'tests'
|
|
19
|
+
Requires-Dist: pytest-doctestplus>=1.0.0 ; extra == 'tests'
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Provides-Extra: docs
|
|
22
|
+
Provides-Extra: tests
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Summary: Detect candidate cosmic-ray spikes in two-dimensional detector exposures
|
|
25
|
+
Keywords: cosmic rays,detector,sigma clipping,solar physics,spikes
|
|
26
|
+
Author-email: Nabil Freij <nabil.freij@gmail.com>
|
|
27
|
+
License-Expression: BSD-3-Clause
|
|
28
|
+
Requires-Python: >=3.12
|
|
29
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
30
|
+
Project-URL: Changelog, https://github.com/LM-SAL/spikemask-rs/blob/main/CHANGELOG.rst
|
|
31
|
+
Project-URL: Documentation, https://spikemask-rs.readthedocs.io
|
|
32
|
+
Project-URL: Issues, https://github.com/LM-SAL/spikemask-rs/issues
|
|
33
|
+
Project-URL: Source, https://github.com/LM-SAL/spikemask-rs
|
|
34
|
+
|
|
35
|
+
# spikemask-rs
|
|
36
|
+
|
|
37
|
+
Detect candidate cosmic-ray hits in a two-dimensional detector exposure using local upper sigma clipping. The Python function returns a Boolean mask and preserves every measured value.
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from spikemask_rs import spike_mask
|
|
41
|
+
|
|
42
|
+
suspected_hits = spike_mask(exposure, window=(7, 5), sigma_upper=3.0, max_iters=10)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Window dimensions are in rows and columns. The detector skips nonfinite pixels and truncates the window at image edges.
|
|
46
|
+
|
|
47
|
+
Install with `python -m pip install spikemask-rs`. See [installation](https://github.com/LM-SAL/spikemask-rs/blob/main/docs/installation.rst) for development commands and [design notes](https://github.com/LM-SAL/spikemask-rs/blob/main/docs/design-notes.rst) for the algorithm and its relationship to the MUSE clipping demo and rsliding.
|
|
48
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# spikemask-rs
|
|
2
|
+
|
|
3
|
+
Detect candidate cosmic-ray hits in a two-dimensional detector exposure using local upper sigma clipping. The Python function returns a Boolean mask and preserves every measured value.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from spikemask_rs import spike_mask
|
|
7
|
+
|
|
8
|
+
suspected_hits = spike_mask(exposure, window=(7, 5), sigma_upper=3.0, max_iters=10)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Window dimensions are in rows and columns. The detector skips nonfinite pixels and truncates the window at image edges.
|
|
12
|
+
|
|
13
|
+
Install with `python -m pip install spikemask-rs`. See [installation](https://github.com/LM-SAL/spikemask-rs/blob/main/docs/installation.rst) for development commands and [design notes](https://github.com/LM-SAL/spikemask-rs/blob/main/docs/design-notes.rst) for the algorithm and its relationship to the MUSE clipping demo and rsliding.
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = [
|
|
3
|
+
"maturin>=1.8,<2.0",
|
|
4
|
+
]
|
|
5
|
+
build-backend = "maturin"
|
|
6
|
+
|
|
7
|
+
[project]
|
|
8
|
+
name = "spikemask-rs"
|
|
9
|
+
# The version lives in Cargo.toml only; maturin reads it from there.
|
|
10
|
+
dynamic = ["version"]
|
|
11
|
+
description = "Detect candidate cosmic-ray spikes in two-dimensional detector exposures"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = "BSD-3-Clause"
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
authors = [
|
|
16
|
+
{ name = "Nabil Freij", email = "nabil.freij@gmail.com" },
|
|
17
|
+
]
|
|
18
|
+
requires-python = ">=3.12"
|
|
19
|
+
dependencies = [
|
|
20
|
+
"numpy>=2.0.0",
|
|
21
|
+
]
|
|
22
|
+
keywords = [
|
|
23
|
+
"cosmic rays",
|
|
24
|
+
"detector",
|
|
25
|
+
"sigma clipping",
|
|
26
|
+
"solar physics",
|
|
27
|
+
"spikes",
|
|
28
|
+
]
|
|
29
|
+
classifiers = [
|
|
30
|
+
"Development Status :: 3 - Alpha",
|
|
31
|
+
"Intended Audience :: Science/Research",
|
|
32
|
+
"Operating System :: MacOS",
|
|
33
|
+
"Operating System :: Microsoft :: Windows",
|
|
34
|
+
"Operating System :: POSIX :: Linux",
|
|
35
|
+
"Programming Language :: Python :: 3",
|
|
36
|
+
"Programming Language :: Rust",
|
|
37
|
+
"Topic :: Scientific/Engineering :: Astronomy",
|
|
38
|
+
"Topic :: Scientific/Engineering :: Physics",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Documentation = "https://spikemask-rs.readthedocs.io"
|
|
43
|
+
Source = "https://github.com/LM-SAL/spikemask-rs"
|
|
44
|
+
Issues = "https://github.com/LM-SAL/spikemask-rs/issues"
|
|
45
|
+
Changelog = "https://github.com/LM-SAL/spikemask-rs/blob/main/CHANGELOG.rst"
|
|
46
|
+
|
|
47
|
+
[project.optional-dependencies]
|
|
48
|
+
docs = [
|
|
49
|
+
"sphinx>=8",
|
|
50
|
+
"furo",
|
|
51
|
+
"sphinx-copybutton",
|
|
52
|
+
]
|
|
53
|
+
tests = [
|
|
54
|
+
"pytest",
|
|
55
|
+
"pytest-doctestplus>=1.0.0",
|
|
56
|
+
]
|
|
57
|
+
dev = [
|
|
58
|
+
"spikemask-rs[tests,docs]",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
[tool.maturin]
|
|
62
|
+
python-source = "python"
|
|
63
|
+
module-name = "spikemask_rs._spikemask_rs"
|
|
64
|
+
exclude = [
|
|
65
|
+
"benchmarks/**",
|
|
66
|
+
".github/**",
|
|
67
|
+
"changelog/**",
|
|
68
|
+
"docs/**",
|
|
69
|
+
".readthedocs.yaml",
|
|
70
|
+
".gitignore",
|
|
71
|
+
".pre-commit-config.yaml",
|
|
72
|
+
"rustfmt.toml",
|
|
73
|
+
"ruff.toml",
|
|
74
|
+
"tox.ini",
|
|
75
|
+
"pytest.ini",
|
|
76
|
+
"AGENTS.md",
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
[tool.cibuildwheel]
|
|
80
|
+
build-frontend = "build"
|
|
81
|
+
skip = ["*-win32", "*-manylinux_i686"]
|
|
82
|
+
test-extras = ["tests"]
|
|
83
|
+
test-command = "pytest --pyargs spikemask_rs"
|
|
84
|
+
|
|
85
|
+
[tool.docformatter]
|
|
86
|
+
recursive = true
|
|
87
|
+
wrap-summaries = 100
|
|
88
|
+
wrap-descriptions = 100
|
|
89
|
+
pre-summary-newline = true
|
|
90
|
+
close-quotes-on-newline = true
|
|
91
|
+
make-summary-multi-line = true
|
|
92
|
+
black = true
|
|
93
|
+
|
|
94
|
+
[tool.towncrier]
|
|
95
|
+
package = "spikemask_rs"
|
|
96
|
+
filename = "CHANGELOG.rst"
|
|
97
|
+
directory = "changelog/"
|
|
98
|
+
issue_format = "#{issue}"
|
|
99
|
+
title_format = "{version} ({project_date})"
|
|
100
|
+
|
|
101
|
+
[[tool.towncrier.type]]
|
|
102
|
+
directory = "breaking"
|
|
103
|
+
name = "Breaking Changes"
|
|
104
|
+
showcontent = true
|
|
105
|
+
|
|
106
|
+
[[tool.towncrier.type]]
|
|
107
|
+
directory = "deprecation"
|
|
108
|
+
name = "Deprecations"
|
|
109
|
+
showcontent = true
|
|
110
|
+
|
|
111
|
+
[[tool.towncrier.type]]
|
|
112
|
+
directory = "removal"
|
|
113
|
+
name = "Removals"
|
|
114
|
+
showcontent = true
|
|
115
|
+
|
|
116
|
+
[[tool.towncrier.type]]
|
|
117
|
+
directory = "feature"
|
|
118
|
+
name = "New Features"
|
|
119
|
+
showcontent = true
|
|
120
|
+
|
|
121
|
+
[[tool.towncrier.type]]
|
|
122
|
+
directory = "bugfix"
|
|
123
|
+
name = "Bug Fixes"
|
|
124
|
+
showcontent = true
|
|
125
|
+
|
|
126
|
+
[[tool.towncrier.type]]
|
|
127
|
+
directory = "doc"
|
|
128
|
+
name = "Documentation"
|
|
129
|
+
showcontent = true
|
|
130
|
+
|
|
131
|
+
[[tool.towncrier.type]]
|
|
132
|
+
directory = "trivial"
|
|
133
|
+
name = "Internal Changes"
|
|
134
|
+
showcontent = true
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Candidate cosmic-ray spike masks for detector exposures.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from importlib.metadata import version as _get_version
|
|
6
|
+
|
|
7
|
+
from ._workers import set_workers
|
|
8
|
+
from .detection import spike_mask
|
|
9
|
+
|
|
10
|
+
__version__ = _get_version("spikemask-rs")
|
|
11
|
+
__all__ = ["__version__", "set_workers", "spike_mask"]
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Control the Rayon workers used for parallel calls.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from contextlib import contextmanager
|
|
8
|
+
from contextvars import ContextVar
|
|
9
|
+
from operator import index
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
from ._spikemask_rs import _WorkerPool
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from collections.abc import Iterator
|
|
18
|
+
|
|
19
|
+
_active: ContextVar[_WorkerPool | None] = ContextVar("spikemask_rs_pool", default=None)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _pool_for_count(workers: int) -> _WorkerPool:
|
|
23
|
+
if isinstance(workers, (bool, np.bool_)):
|
|
24
|
+
msg = "workers must be a positive integer"
|
|
25
|
+
raise TypeError(msg)
|
|
26
|
+
count = index(workers)
|
|
27
|
+
if count < 1:
|
|
28
|
+
msg = "workers must be a positive integer"
|
|
29
|
+
raise ValueError(msg)
|
|
30
|
+
active = _active.get()
|
|
31
|
+
if active is not None and active.num_threads == count:
|
|
32
|
+
return active
|
|
33
|
+
return _WorkerPool(count)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def resolve_pool(workers: int | None) -> _WorkerPool | None:
|
|
37
|
+
"""
|
|
38
|
+
Select the explicit pool, or inherit the current context.
|
|
39
|
+
"""
|
|
40
|
+
return _active.get() if workers is None else _pool_for_count(workers)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@contextmanager
|
|
44
|
+
def set_workers(workers: int) -> Iterator[None]:
|
|
45
|
+
"""
|
|
46
|
+
Reuse a pool of ``workers`` threads for parallel calls in this context.
|
|
47
|
+
|
|
48
|
+
An explicit ``workers=`` on a call overrides the context. Leaving the context restores the
|
|
49
|
+
previous pool, including when a call raises.
|
|
50
|
+
"""
|
|
51
|
+
token = _active.set(_pool_for_count(workers))
|
|
52
|
+
try:
|
|
53
|
+
yield
|
|
54
|
+
finally:
|
|
55
|
+
_active.reset(token)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Upper sigma clipping for detector pixels.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from operator import index
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
from ._spikemask_rs import spike_mask as _spike_mask
|
|
13
|
+
from ._workers import resolve_pool
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from numpy.typing import ArrayLike, NDArray
|
|
17
|
+
|
|
18
|
+
__all__ = ["spike_mask"]
|
|
19
|
+
|
|
20
|
+
_DETECTOR_NDIM = 2
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# Defaults measured on mocked MUSE frames (design notes); the IDL demo uses (5, 3) and 2.65.
|
|
24
|
+
def spike_mask(
|
|
25
|
+
exposure: ArrayLike,
|
|
26
|
+
*,
|
|
27
|
+
window: tuple[int, int] = (7, 5),
|
|
28
|
+
sigma_upper: float = 3.0,
|
|
29
|
+
max_iters: int = 10,
|
|
30
|
+
workers: int | None = None,
|
|
31
|
+
) -> NDArray[np.bool_]:
|
|
32
|
+
"""
|
|
33
|
+
Flag candidate positive spikes in one two-dimensional exposure.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
exposure : array_like
|
|
38
|
+
Real-valued detector image. Nonfinite samples are ignored.
|
|
39
|
+
window : tuple of int, optional
|
|
40
|
+
Odd, positive sizes in (rows, columns) order. By default (7, 5).
|
|
41
|
+
sigma_upper : float, optional
|
|
42
|
+
Flag a pixel when it exceeds the local median by more than this many
|
|
43
|
+
population standard deviations. By default 3.0.
|
|
44
|
+
max_iters : int, optional
|
|
45
|
+
Maximum number of passes. Each pass excludes previously flagged pixels.
|
|
46
|
+
By default 10.
|
|
47
|
+
workers : int, optional
|
|
48
|
+
Number of Rayon threads for this call. By default use the active
|
|
49
|
+
worker context or Rayon's default pool.
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
mask : numpy.ndarray
|
|
54
|
+
Boolean array with the shape of the exposure and True at suspected spikes.
|
|
55
|
+
"""
|
|
56
|
+
image = np.asarray(exposure)
|
|
57
|
+
if image.ndim != _DETECTOR_NDIM or image.dtype.kind not in "iuf":
|
|
58
|
+
msg = "exposure must be a real numeric 2-D array"
|
|
59
|
+
raise ValueError(msg)
|
|
60
|
+
rows, columns = map(index, window)
|
|
61
|
+
if min(rows, columns) <= 0:
|
|
62
|
+
msg = "window dimensions must be positive odd integers"
|
|
63
|
+
raise ValueError(msg)
|
|
64
|
+
max_iters = index(max_iters)
|
|
65
|
+
if max_iters <= 0:
|
|
66
|
+
msg = "max_iters must be positive"
|
|
67
|
+
raise ValueError(msg)
|
|
68
|
+
dtype = image.dtype.newbyteorder("=")
|
|
69
|
+
if dtype not in (np.int32, np.float32):
|
|
70
|
+
dtype = np.float64
|
|
71
|
+
return _spike_mask(
|
|
72
|
+
np.ascontiguousarray(image, dtype=dtype),
|
|
73
|
+
(rows, columns),
|
|
74
|
+
sigma_upper,
|
|
75
|
+
max_iters,
|
|
76
|
+
resolve_pool(workers),
|
|
77
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Compare the public mask with the C-backed clipping kernels in ftoolss.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from spikemask_rs import spike_mask
|
|
9
|
+
|
|
10
|
+
ftoolss = pytest.importorskip("ftoolss", exc_type=ModuleNotFoundError)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def ftoolss_mask(image, window, sigma_upper, max_iters):
|
|
14
|
+
# ftoolss writes NaNs, so integer counts need a floating type that represents every int32.
|
|
15
|
+
data = image.astype(np.float64) if image.dtype == np.int32 else image
|
|
16
|
+
clipped = ftoolss.sigma_clip(
|
|
17
|
+
data,
|
|
18
|
+
window[::-1], # ftoolss specifies (columns, rows).
|
|
19
|
+
sigma_upper=sigma_upper,
|
|
20
|
+
sigma_lower=1000,
|
|
21
|
+
maxiters=max_iters,
|
|
22
|
+
)
|
|
23
|
+
return np.isfinite(image) & np.isnan(clipped)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.mark.parametrize("dtype", [np.int32, np.float32, np.float64])
|
|
27
|
+
# The defaults, then the settings of the MUSE IDL demo in both orientations.
|
|
28
|
+
@pytest.mark.parametrize(("window", "sigma_upper"), [((7, 5), 3.0), ((5, 3), 2.65), ((3, 5), 2.65)])
|
|
29
|
+
@pytest.mark.parametrize("max_iters", [1, 10])
|
|
30
|
+
@pytest.mark.parametrize("workers", [1, 4])
|
|
31
|
+
def test_mask_matches_ftoolss(dtype, window, sigma_upper, max_iters, workers):
|
|
32
|
+
image = np.random.default_rng(2026).normal(100, 3, (35, 49)).astype(dtype)
|
|
33
|
+
image[0, 8] = 700
|
|
34
|
+
image[16:18, 24] = [500, 10000]
|
|
35
|
+
image[27, 38] = -9999
|
|
36
|
+
if dtype != np.int32:
|
|
37
|
+
image[5, ::3] = np.nan
|
|
38
|
+
|
|
39
|
+
expected = ftoolss_mask(image, window, sigma_upper, max_iters)
|
|
40
|
+
assert expected[0, 8]
|
|
41
|
+
assert expected[17, 24]
|
|
42
|
+
assert expected[16, 24] == (max_iters > 1) # The stronger neighbor hides it on pass one.
|
|
43
|
+
assert not expected[27, 38]
|
|
44
|
+
actual = spike_mask(
|
|
45
|
+
image, window=window, sigma_upper=sigma_upper, max_iters=max_iters, workers=workers
|
|
46
|
+
)
|
|
47
|
+
np.testing.assert_array_equal(actual, expected, strict=True)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_int32_single_count_above_float32_precision_matches_ftoolss():
|
|
51
|
+
image = np.full((13, 27), 2**27, dtype=np.int32)
|
|
52
|
+
image[6, 12] += 1
|
|
53
|
+
expected = ftoolss_mask(image, (7, 5), 3.0, 10) # The defaults of spike_mask.
|
|
54
|
+
assert np.argwhere(expected).tolist() == [[6, 12]]
|
|
55
|
+
np.testing.assert_array_equal(spike_mask(image), expected, strict=True)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from spikemask_rs import spike_mask
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def reference(image, window, sigma, passes):
|
|
10
|
+
data = np.array(image, dtype=float, copy=True)
|
|
11
|
+
rows, cols = np.array(window) // 2
|
|
12
|
+
mask = np.zeros(image.shape, dtype=bool)
|
|
13
|
+
for _ in range(passes):
|
|
14
|
+
added = np.zeros_like(mask)
|
|
15
|
+
for y, x in zip(*np.nonzero(np.isfinite(data)), strict=True):
|
|
16
|
+
patch = data[max(y - rows, 0) : y + rows + 1, max(x - cols, 0) : x + cols + 1]
|
|
17
|
+
values = patch[np.isfinite(patch)]
|
|
18
|
+
added[y, x] = data[y, x] - np.median(values) > sigma * np.std(values)
|
|
19
|
+
if not added.any():
|
|
20
|
+
break
|
|
21
|
+
data[added] = np.nan
|
|
22
|
+
mask |= added
|
|
23
|
+
return mask
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.mark.parametrize(
|
|
27
|
+
"window", [(7, 5), (5, 3), (3, 5), (3, 3), (1, 9), (7, 1), (31, 33), (1, 1)]
|
|
28
|
+
)
|
|
29
|
+
@pytest.mark.parametrize(("sigma", "passes"), [(2.65, 10), (1.75, 1), (0.9, 3)])
|
|
30
|
+
def test_matches_a_full_rescan(window, sigma, passes):
|
|
31
|
+
rng = np.random.default_rng(5)
|
|
32
|
+
data = rng.normal(size=(12, 23))
|
|
33
|
+
data[6:8, 10] = [25, 600]
|
|
34
|
+
data[4, 12] = -9999
|
|
35
|
+
data[2, :4] = [np.nan, np.inf, -np.inf, np.nan]
|
|
36
|
+
expected = reference(data, window, sigma, passes)
|
|
37
|
+
original = data.copy()
|
|
38
|
+
actual = spike_mask(data, window=window, sigma_upper=sigma, max_iters=passes)
|
|
39
|
+
assert actual.dtype == bool
|
|
40
|
+
np.testing.assert_array_equal(actual, expected)
|
|
41
|
+
np.testing.assert_array_equal(data, original)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_candidate_mask_preserves_data_and_leaves_missing_values_unflagged():
|
|
45
|
+
data = np.full((30, 32), 100.0)
|
|
46
|
+
data[1, 1] = np.nan
|
|
47
|
+
data[8, 15] = -10000
|
|
48
|
+
data[12, 15] = 10000
|
|
49
|
+
data.setflags(write=False)
|
|
50
|
+
assert np.argwhere(spike_mask(data)).tolist() == [[12, 15]]
|
|
51
|
+
assert data[12, 15] == 10000
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@pytest.mark.parametrize(
|
|
55
|
+
"dtype", [np.float32, np.float64, ">f4", ">f8", np.int16, np.int32, ">i4", np.uint32]
|
|
56
|
+
)
|
|
57
|
+
def test_detector_layout_and_dtype(dtype):
|
|
58
|
+
image = np.full((16, 24), 10, dtype=dtype)
|
|
59
|
+
image[7, 13] = 10000
|
|
60
|
+
for layout in [image, np.asfortranarray(image), image[:, ::-1], image.T]:
|
|
61
|
+
np.testing.assert_array_equal(spike_mask(layout), layout == 10000)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@pytest.mark.parametrize("shape", [(0, 8), (8, 0), (1, 1)])
|
|
65
|
+
def test_empty_or_single_pixel(shape):
|
|
66
|
+
np.testing.assert_array_equal(spike_mask(np.zeros(shape)), np.zeros(shape, dtype=bool))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@pytest.mark.parametrize(
|
|
70
|
+
"options",
|
|
71
|
+
[
|
|
72
|
+
{"window": (4, 3)},
|
|
73
|
+
{"window": (1, -1)},
|
|
74
|
+
{"window": (3, 0)},
|
|
75
|
+
{"window": (3,)},
|
|
76
|
+
{"window": (3.5, 5)},
|
|
77
|
+
{"sigma_upper": 0},
|
|
78
|
+
{"sigma_upper": np.inf},
|
|
79
|
+
{"sigma_upper": np.nan},
|
|
80
|
+
{"max_iters": -1},
|
|
81
|
+
{"max_iters": 0},
|
|
82
|
+
{"max_iters": 1.5},
|
|
83
|
+
],
|
|
84
|
+
)
|
|
85
|
+
def test_invalid_settings(options):
|
|
86
|
+
with pytest.raises((ValueError, TypeError)):
|
|
87
|
+
spike_mask(np.ones((3, 3)), **options)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@pytest.mark.parametrize("bad", [np.zeros((1, 2, 3)), np.ones((3, 3), complex)])
|
|
91
|
+
def test_invalid_detector_data(bad):
|
|
92
|
+
with pytest.raises(ValueError, match="real numeric 2-D"):
|
|
93
|
+
spike_mask(bad)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_no_fractional_rounding_at_the_threshold():
|
|
97
|
+
image = np.array([[0.0, 0.0, 1.0]])
|
|
98
|
+
threshold = 1 / np.std(image)
|
|
99
|
+
for sigma in [threshold, np.nextafter(threshold, 0)]:
|
|
100
|
+
np.testing.assert_array_equal(
|
|
101
|
+
spike_mask(image, window=(1, 5), sigma_upper=sigma),
|
|
102
|
+
reference(image, (1, 5), sigma, 10),
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_independent_calls_from_threads():
|
|
107
|
+
images = [np.random.default_rng(seed).normal(size=(18, 21)) for seed in range(4)]
|
|
108
|
+
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
109
|
+
for parallel, serial in zip(
|
|
110
|
+
pool.map(spike_mask, images), map(spike_mask, images), strict=True
|
|
111
|
+
):
|
|
112
|
+
np.testing.assert_array_equal(parallel, serial)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_int32_counts_resolve_a_single_dn_at_large_offsets():
|
|
116
|
+
image = np.full((13, 27), 2**27, dtype=np.int32)
|
|
117
|
+
image[6, 12] += 1
|
|
118
|
+
assert np.argwhere(spike_mask(image)).tolist() == [[6, 12]]
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
2
|
+
from threading import Barrier
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from spikemask_rs import detection as api
|
|
8
|
+
from spikemask_rs import set_workers
|
|
9
|
+
from spikemask_rs._workers import resolve_pool
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def run(dtype, **options):
|
|
13
|
+
counts = np.full((30, 32), 100, dtype=dtype)
|
|
14
|
+
counts[12, 15] = 10000
|
|
15
|
+
return api.spike_mask(counts, **options)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.mark.parametrize("dtype", [np.int32, np.float32])
|
|
19
|
+
def test_call_and_scoped_workers_reach_the_kernel(monkeypatch, dtype):
|
|
20
|
+
pools = []
|
|
21
|
+
original = api._spike_mask # noqa: SLF001
|
|
22
|
+
|
|
23
|
+
def record(*args):
|
|
24
|
+
assert args[0].dtype == dtype
|
|
25
|
+
pools.append(args[-1])
|
|
26
|
+
return original(*args)
|
|
27
|
+
|
|
28
|
+
monkeypatch.setattr(api, "_spike_mask", record)
|
|
29
|
+
expected = run(dtype)
|
|
30
|
+
|
|
31
|
+
def failing_stage():
|
|
32
|
+
with set_workers(3):
|
|
33
|
+
np.testing.assert_array_equal(run(dtype), expected)
|
|
34
|
+
msg = "invalid stage"
|
|
35
|
+
raise ValueError(msg)
|
|
36
|
+
|
|
37
|
+
with set_workers(np.int32(2)):
|
|
38
|
+
np.testing.assert_array_equal(run(dtype), expected)
|
|
39
|
+
np.testing.assert_array_equal(run(dtype, workers=2), expected)
|
|
40
|
+
np.testing.assert_array_equal(run(dtype, workers=1), expected)
|
|
41
|
+
with pytest.raises(ValueError, match="invalid"):
|
|
42
|
+
failing_stage()
|
|
43
|
+
np.testing.assert_array_equal(run(dtype), expected)
|
|
44
|
+
np.testing.assert_array_equal(run(dtype), expected)
|
|
45
|
+
assert [p.num_threads if p is not None else None for p in pools] == [None, 2, 2, 1, 3, 2, None]
|
|
46
|
+
assert pools[1] is pools[2] is pools[5]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_parallel_contexts_are_independent():
|
|
50
|
+
rendezvous = Barrier(2)
|
|
51
|
+
|
|
52
|
+
def stage(count):
|
|
53
|
+
with set_workers(count):
|
|
54
|
+
rendezvous.wait(timeout=10)
|
|
55
|
+
return resolve_pool(None).num_threads
|
|
56
|
+
|
|
57
|
+
with set_workers(3), ThreadPoolExecutor(max_workers=2) as executor:
|
|
58
|
+
assert list(executor.map(stage, [1, 2])) == [1, 2]
|
|
59
|
+
assert resolve_pool(None).num_threads == 3
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@pytest.mark.parametrize(
|
|
63
|
+
("value", "error"), [(0, ValueError), (-1, ValueError), (True, TypeError), (1.5, TypeError)]
|
|
64
|
+
)
|
|
65
|
+
def test_invalid_worker_counts(value, error):
|
|
66
|
+
with pytest.raises(error):
|
|
67
|
+
run(np.float32, workers=value)
|
|
68
|
+
with pytest.raises(error), set_workers(value):
|
|
69
|
+
pass
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
//! Python binding and validation.
|
|
2
|
+
use numpy::{PyArray1, PyArray2, PyArrayMethods, PyReadonlyArray2, PyUntypedArrayMethods};
|
|
3
|
+
use pyo3::{exceptions::PyValueError, prelude::*};
|
|
4
|
+
|
|
5
|
+
use crate::detect::Detector;
|
|
6
|
+
use crate::workers::{self, WorkerPool};
|
|
7
|
+
|
|
8
|
+
#[derive(FromPyObject)]
|
|
9
|
+
pub(crate) enum Exposure<'py> {
|
|
10
|
+
Int32(PyReadonlyArray2<'py, i32>),
|
|
11
|
+
Float32(PyReadonlyArray2<'py, f32>),
|
|
12
|
+
Float64(PyReadonlyArray2<'py, f64>),
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
fn working_copy<T: numpy::Element + Copy + Into<f64>>(
|
|
16
|
+
exposure: PyReadonlyArray2<'_, T>,
|
|
17
|
+
) -> PyResult<([usize; 2], Vec<f64>)> {
|
|
18
|
+
if !exposure.is_c_contiguous() {
|
|
19
|
+
return Err(PyValueError::new_err("exposure must be C-contiguous"));
|
|
20
|
+
}
|
|
21
|
+
let shape = [exposure.shape()[0], exposure.shape()[1]];
|
|
22
|
+
// Fill the mutable clipping buffer directly from the detector's native dtype.
|
|
23
|
+
let image = exposure
|
|
24
|
+
.as_slice()?
|
|
25
|
+
.iter()
|
|
26
|
+
.copied()
|
|
27
|
+
.map(Into::into)
|
|
28
|
+
.collect();
|
|
29
|
+
Ok((shape, image))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
#[pyfunction]
|
|
33
|
+
#[pyo3(signature = (exposure, window, sigma_upper, max_iters, pool=None))]
|
|
34
|
+
pub(crate) fn spike_mask<'py>(
|
|
35
|
+
py: Python<'py>,
|
|
36
|
+
exposure: Exposure<'py>,
|
|
37
|
+
window: (usize, usize),
|
|
38
|
+
sigma_upper: f64,
|
|
39
|
+
max_iters: usize,
|
|
40
|
+
pool: Option<&WorkerPool>,
|
|
41
|
+
) -> PyResult<Bound<'py, PyArray2<bool>>> {
|
|
42
|
+
if window.0.is_multiple_of(2) || window.1.is_multiple_of(2) {
|
|
43
|
+
return Err(PyValueError::new_err(
|
|
44
|
+
"window dimensions must be positive odd integers",
|
|
45
|
+
));
|
|
46
|
+
}
|
|
47
|
+
if !sigma_upper.is_finite() || sigma_upper <= 0. {
|
|
48
|
+
return Err(PyValueError::new_err(
|
|
49
|
+
"sigma_upper must be positive and finite",
|
|
50
|
+
));
|
|
51
|
+
}
|
|
52
|
+
if max_iters == 0 {
|
|
53
|
+
return Err(PyValueError::new_err("max_iters must be positive"));
|
|
54
|
+
}
|
|
55
|
+
let ([height, width], image) = match exposure {
|
|
56
|
+
Exposure::Int32(a) => working_copy(a)?,
|
|
57
|
+
Exposure::Float32(a) => working_copy(a)?,
|
|
58
|
+
Exposure::Float64(a) => working_copy(a)?,
|
|
59
|
+
};
|
|
60
|
+
let detector = Detector {
|
|
61
|
+
height,
|
|
62
|
+
width,
|
|
63
|
+
window,
|
|
64
|
+
sigma_upper,
|
|
65
|
+
};
|
|
66
|
+
let mask = py.detach(move || workers::run(pool, || detector.mask(image, max_iters)));
|
|
67
|
+
PyArray1::from_vec(py, mask).reshape([height, width])
|
|
68
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
//! Iterative upper sigma clipping on a single detector image.
|
|
2
|
+
use rayon::prelude::*;
|
|
3
|
+
|
|
4
|
+
const LANES: usize = 8;
|
|
5
|
+
|
|
6
|
+
pub(crate) struct Detector {
|
|
7
|
+
pub(crate) height: usize,
|
|
8
|
+
pub(crate) width: usize,
|
|
9
|
+
pub(crate) window: (usize, usize),
|
|
10
|
+
pub(crate) sigma_upper: f64,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
impl Detector {
|
|
14
|
+
fn rows(&self, y: usize) -> std::ops::RangeInclusive<usize> {
|
|
15
|
+
let radius = self.window.0 / 2;
|
|
16
|
+
y.saturating_sub(radius)..=y.saturating_add(radius).min(self.height - 1)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fn columns(&self, x: usize) -> std::ops::RangeInclusive<usize> {
|
|
20
|
+
let radius = self.window.1 / 2;
|
|
21
|
+
x.saturating_sub(radius)..=x.saturating_add(radius).min(self.width - 1)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
fn is_hit(&self, image: &[f64], i: usize, samples: &mut Vec<f64>) -> bool {
|
|
25
|
+
let value = image[i];
|
|
26
|
+
if !value.is_finite() {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
let columns = self.columns(i % self.width);
|
|
30
|
+
samples.clear();
|
|
31
|
+
for row in self.rows(i / self.width) {
|
|
32
|
+
let offset = row * self.width;
|
|
33
|
+
samples.extend(
|
|
34
|
+
image[offset + columns.start()..=offset + columns.end()]
|
|
35
|
+
.iter()
|
|
36
|
+
.copied()
|
|
37
|
+
.filter(|v| v.is_finite()),
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
let count = samples.len();
|
|
41
|
+
let mean = samples.iter().sum::<f64>() / count as f64;
|
|
42
|
+
let variance = samples.iter().map(|v| (v - mean) * (v - mean)).sum::<f64>() / count as f64;
|
|
43
|
+
let threshold = self.sigma_upper * variance.sqrt();
|
|
44
|
+
// The median passes the cut when over half the samples do; only an even split needs it.
|
|
45
|
+
let passing = 2 * samples.iter().filter(|&&v| value - v > threshold).count();
|
|
46
|
+
if passing != count {
|
|
47
|
+
return passing > count;
|
|
48
|
+
}
|
|
49
|
+
let (left, middle, _) = samples.select_nth_unstable_by(count / 2, f64::total_cmp);
|
|
50
|
+
let median = (left.iter().copied().fold(f64::NEG_INFINITY, f64::max) + *middle) / 2.;
|
|
51
|
+
value - median > threshold
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Visits each window offset once for LANES adjacent centers, which LLVM evaluates together.
|
|
55
|
+
#[inline(always)]
|
|
56
|
+
fn lanes(&self, image: &[f64], i: usize, mut visit: impl FnMut(&[f64])) {
|
|
57
|
+
let corner = i - self.window.0 / 2 * self.width - self.window.1 / 2;
|
|
58
|
+
for row in 0..self.window.0 {
|
|
59
|
+
for dx in 0..self.window.1 {
|
|
60
|
+
let start = corner + row * self.width + dx;
|
|
61
|
+
visit(&image[start..start + LANES]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#[inline(always)]
|
|
67
|
+
fn interior(&self, image: &[f64], i: usize, hits: &mut Vec<usize>, scratch: &mut Vec<f64>) {
|
|
68
|
+
let count = self.window.0 * self.window.1;
|
|
69
|
+
let mut sum = [0.; LANES];
|
|
70
|
+
self.lanes(image, i, |s| {
|
|
71
|
+
for j in 0..LANES {
|
|
72
|
+
sum[j] += s[j];
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
let mean = sum.map(|s| s / count as f64);
|
|
76
|
+
let mut variance = [0.; LANES];
|
|
77
|
+
self.lanes(image, i, |s| {
|
|
78
|
+
for j in 0..LANES {
|
|
79
|
+
let delta = s[j] - mean[j];
|
|
80
|
+
variance[j] += delta * delta;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
let threshold = variance.map(|v| self.sigma_upper * (v / count as f64).sqrt());
|
|
84
|
+
let center = &image[i..i + LANES];
|
|
85
|
+
let mut passing = [0; LANES];
|
|
86
|
+
self.lanes(image, i, |s| {
|
|
87
|
+
for j in 0..LANES {
|
|
88
|
+
passing[j] += (center[j] - s[j] > threshold[j]) as usize;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
for j in 0..LANES {
|
|
92
|
+
// A complete window has an odd count, so there is no even split to settle.
|
|
93
|
+
let hit = if sum[j].is_finite() {
|
|
94
|
+
2 * passing[j] > count
|
|
95
|
+
} else {
|
|
96
|
+
self.is_hit(image, i + j, scratch)
|
|
97
|
+
};
|
|
98
|
+
if hit {
|
|
99
|
+
hits.push(i + j);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
#[inline(always)]
|
|
105
|
+
fn row(&self, image: &[f64], y: usize, hits: &mut Vec<usize>, scratch: &mut Vec<f64>) {
|
|
106
|
+
let start = y * self.width;
|
|
107
|
+
let (above, beside) = (self.window.0 / 2, self.window.1 / 2);
|
|
108
|
+
let complete_rows = y >= above && y + above < self.height;
|
|
109
|
+
let mut x = 0;
|
|
110
|
+
while x < self.width {
|
|
111
|
+
if complete_rows && x >= beside && x + LANES + beside <= self.width {
|
|
112
|
+
self.interior(image, start + x, hits, scratch);
|
|
113
|
+
x += LANES;
|
|
114
|
+
} else {
|
|
115
|
+
if self.is_hit(image, start + x, scratch) {
|
|
116
|
+
hits.push(start + x);
|
|
117
|
+
}
|
|
118
|
+
x += 1;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
|
124
|
+
#[target_feature(enable = "avx2")]
|
|
125
|
+
unsafe fn row_avx2(
|
|
126
|
+
&self,
|
|
127
|
+
image: &[f64],
|
|
128
|
+
y: usize,
|
|
129
|
+
hits: &mut Vec<usize>,
|
|
130
|
+
scratch: &mut Vec<f64>,
|
|
131
|
+
) {
|
|
132
|
+
self.row(image, y, hits, scratch);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
fn scan(&self, image: &[f64]) -> Vec<usize> {
|
|
136
|
+
(0..self.height)
|
|
137
|
+
.into_par_iter()
|
|
138
|
+
.with_min_len(4)
|
|
139
|
+
.fold(
|
|
140
|
+
|| (Vec::new(), Vec::new()),
|
|
141
|
+
|(mut hits, mut scratch), y| {
|
|
142
|
+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
|
143
|
+
if is_x86_feature_detected!("avx2") {
|
|
144
|
+
// SAFETY: runtime detection guards the only CPU-specific entry point.
|
|
145
|
+
unsafe { self.row_avx2(image, y, &mut hits, &mut scratch) };
|
|
146
|
+
return (hits, scratch);
|
|
147
|
+
}
|
|
148
|
+
self.row(image, y, &mut hits, &mut scratch);
|
|
149
|
+
(hits, scratch)
|
|
150
|
+
},
|
|
151
|
+
)
|
|
152
|
+
.map(|(hits, _)| hits)
|
|
153
|
+
.reduce(Vec::new, |mut left, right| {
|
|
154
|
+
left.extend(right);
|
|
155
|
+
left
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
fn rescan(&self, image: &[f64], hits: &[usize]) -> Vec<usize> {
|
|
160
|
+
let footprint = self
|
|
161
|
+
.window
|
|
162
|
+
.0
|
|
163
|
+
.min(self.height)
|
|
164
|
+
.saturating_mul(self.window.1.min(self.width));
|
|
165
|
+
// Visiting that many neighbors costs more than testing every pixel once.
|
|
166
|
+
if hits.len().saturating_mul(footprint) >= image.len() {
|
|
167
|
+
return self.scan(image);
|
|
168
|
+
}
|
|
169
|
+
// Only windows that overlap a newly masked sample can change on the next pass.
|
|
170
|
+
let mut next: Vec<usize> = hits
|
|
171
|
+
.par_iter()
|
|
172
|
+
.with_min_len(16)
|
|
173
|
+
.map_init(Vec::new, |scratch, &i| {
|
|
174
|
+
let columns = self.columns(i % self.width);
|
|
175
|
+
self.rows(i / self.width)
|
|
176
|
+
.flat_map(|row| {
|
|
177
|
+
row * self.width + columns.start()..=row * self.width + columns.end()
|
|
178
|
+
})
|
|
179
|
+
.filter(|&j| self.is_hit(image, j, scratch))
|
|
180
|
+
.collect::<Vec<_>>()
|
|
181
|
+
})
|
|
182
|
+
.flatten_iter()
|
|
183
|
+
.collect();
|
|
184
|
+
// Two masked samples can share a neighbor.
|
|
185
|
+
next.sort_unstable();
|
|
186
|
+
next.dedup();
|
|
187
|
+
next
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
pub(crate) fn mask(&self, mut image: Vec<f64>, max_iters: usize) -> Vec<bool> {
|
|
191
|
+
let mut mask = vec![false; image.len()];
|
|
192
|
+
if image.is_empty() || self.window == (1, 1) {
|
|
193
|
+
return mask;
|
|
194
|
+
}
|
|
195
|
+
let mut hits = self.scan(&image);
|
|
196
|
+
for pass in 0..max_iters {
|
|
197
|
+
if hits.is_empty() {
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
for &i in &hits {
|
|
201
|
+
mask[i] = true;
|
|
202
|
+
image[i] = f64::NAN;
|
|
203
|
+
}
|
|
204
|
+
if pass + 1 < max_iters {
|
|
205
|
+
hits = self.rescan(&image, &hits);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
mask
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//! Candidate cosmic-ray spike masks for two-dimensional detector exposures.
|
|
2
|
+
mod api;
|
|
3
|
+
mod detect;
|
|
4
|
+
mod workers;
|
|
5
|
+
|
|
6
|
+
#[cfg(test)]
|
|
7
|
+
mod tests;
|
|
8
|
+
|
|
9
|
+
use pyo3::prelude::*;
|
|
10
|
+
|
|
11
|
+
#[pymodule(gil_used = false)]
|
|
12
|
+
fn _spikemask_rs(module: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
13
|
+
module.add_class::<workers::WorkerPool>()?;
|
|
14
|
+
module.add_function(wrap_pyfunction!(api::spike_mask, module)?)?;
|
|
15
|
+
Ok(())
|
|
16
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
use crate::detect::Detector;
|
|
2
|
+
use crate::workers::{run, WorkerPool};
|
|
3
|
+
use rayon::ThreadPoolBuilder;
|
|
4
|
+
|
|
5
|
+
#[test]
|
|
6
|
+
fn a_nearby_spike_appears_on_the_second_pass() {
|
|
7
|
+
let mut image = vec![100.; 32 * 32];
|
|
8
|
+
image[15 * 32 + 16] = 400.;
|
|
9
|
+
image[16 * 32 + 16] = 10500.;
|
|
10
|
+
let detector = Detector {
|
|
11
|
+
width: 32,
|
|
12
|
+
height: 32,
|
|
13
|
+
window: (5, 3),
|
|
14
|
+
sigma_upper: 2.65,
|
|
15
|
+
};
|
|
16
|
+
let once = detector.mask(image.clone(), 1);
|
|
17
|
+
assert!(once[16 * 32 + 16]);
|
|
18
|
+
assert!(!once[15 * 32 + 16]);
|
|
19
|
+
let twice = detector.mask(image, 2);
|
|
20
|
+
assert!(twice[16 * 32 + 16] && twice[15 * 32 + 16]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
#[test]
|
|
24
|
+
fn an_even_window_split_by_the_cut_is_decided_by_its_median() {
|
|
25
|
+
// Of the samples 0 and 4, only 0 passes the cut for the 4; their median is 2 and sigma is 2.
|
|
26
|
+
let flagged = |sigma_upper| {
|
|
27
|
+
let detector = Detector {
|
|
28
|
+
width: 2,
|
|
29
|
+
height: 1,
|
|
30
|
+
window: (1, 3),
|
|
31
|
+
sigma_upper,
|
|
32
|
+
};
|
|
33
|
+
detector.mask(vec![0., 4.], 1)
|
|
34
|
+
};
|
|
35
|
+
assert_eq!(flagged(0.9), [false, true]);
|
|
36
|
+
assert_eq!(flagged(1.1), [false, false]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
#[test]
|
|
40
|
+
fn rows_use_the_declared_window_and_skip_missing_neighbors() {
|
|
41
|
+
let detector = Detector {
|
|
42
|
+
width: 11,
|
|
43
|
+
height: 9,
|
|
44
|
+
window: (1, 7),
|
|
45
|
+
sigma_upper: 2.,
|
|
46
|
+
};
|
|
47
|
+
let mut image = vec![0.; 99];
|
|
48
|
+
image[11] = f64::NAN;
|
|
49
|
+
image[14] = 100.;
|
|
50
|
+
assert_eq!(
|
|
51
|
+
detector
|
|
52
|
+
.mask(image, 10)
|
|
53
|
+
.into_iter()
|
|
54
|
+
.filter(|hit| *hit)
|
|
55
|
+
.count(),
|
|
56
|
+
1
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
#[test]
|
|
61
|
+
fn the_kernel_inherits_the_selected_pool() {
|
|
62
|
+
let pool = WorkerPool(ThreadPoolBuilder::new().num_threads(2).build().unwrap());
|
|
63
|
+
assert_eq!(run(Some(&pool), rayon::current_num_threads), 2);
|
|
64
|
+
pool.0
|
|
65
|
+
.install(|| assert_eq!(run(None, rayon::current_num_threads), 2));
|
|
66
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//! A Python-owned Rayon pool for one call or stage.
|
|
2
|
+
use pyo3::exceptions::PyRuntimeError;
|
|
3
|
+
use pyo3::prelude::*;
|
|
4
|
+
use rayon::{ThreadPool, ThreadPoolBuilder};
|
|
5
|
+
use std::num::NonZeroUsize;
|
|
6
|
+
|
|
7
|
+
#[pyclass(frozen, name = "_WorkerPool", module = "spikemask_rs._spikemask_rs")]
|
|
8
|
+
pub(crate) struct WorkerPool(pub(crate) ThreadPool);
|
|
9
|
+
|
|
10
|
+
#[pymethods]
|
|
11
|
+
impl WorkerPool {
|
|
12
|
+
#[getter]
|
|
13
|
+
fn num_threads(&self) -> usize {
|
|
14
|
+
self.0.current_num_threads()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#[new]
|
|
18
|
+
fn new(workers: NonZeroUsize) -> PyResult<Self> {
|
|
19
|
+
ThreadPoolBuilder::new()
|
|
20
|
+
.num_threads(workers.get())
|
|
21
|
+
.build()
|
|
22
|
+
.map(Self)
|
|
23
|
+
.map_err(|error| PyRuntimeError::new_err(error.to_string()))
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
pub(crate) fn run<T: Send>(pool: Option<&WorkerPool>, work: impl FnOnce() -> T + Send) -> T {
|
|
28
|
+
match pool {
|
|
29
|
+
Some(pool) => pool.0.install(work),
|
|
30
|
+
None => work(),
|
|
31
|
+
}
|
|
32
|
+
}
|