Skip to content

Commit

Permalink
Clip center_time to be within the time and endtime (#973)
Browse files Browse the repository at this point in the history
* Clip `center_time` to be within the `time` `endtime`

* Fix a corner case where the area is 0
  • Loading branch information
dachengx authored Feb 13, 2025
1 parent 165b533 commit a6ace30
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ psutil = "*"
pymongo = "*"
scipy = "*"
tqdm = ">=4.46.0"
zarr = "*"
zarr = "<3.0.0"
zstd = "*"
sphinx = { version = "*", optional = true }
sphinx_rtd_theme = { version = "*", optional = true }
Expand Down
13 changes: 7 additions & 6 deletions strax/processing/peak_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ def compute_center_time(peaks):
center_time = np.zeros(len(peaks), dtype=np.int64)
for p_i, p in enumerate(peaks):
data = p["data"][: p["length"]]
if data.sum() <= 0:
# Negative or zero-area peaks have centertime at startime
if data.sum() == 0.0:
# Zero-area peaks have centertime at startime
center_time[p_i] = p["time"]
else:
t = np.average(np.arange(p["length"]), weights=data)
center_time[p_i] = (t + 1 / 2) * p["dt"]
center_time[p_i] += p["time"] # converting from float to int, implicit floor
continue
t = np.average(np.arange(p["length"]), weights=data)
center_time[p_i] = (t + 1 / 2) * p["dt"]
center_time[p_i] += p["time"] # converting from float to int, implicit floor
center_time = np.clip(center_time, peaks["time"], strax.endtime(peaks))
return center_time


Expand Down
2 changes: 1 addition & 1 deletion tests/test_peak_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_filled_peaks(peak_length, data_length, n_widths):

# Fill the peaks with random length data
for p in peaks:
length = np.random.randint(0, data_length)
length = np.random.randint(1, data_length)
p["length"] = length
wf = np.random.random(size=length)
p["data"][:length] = wf
Expand Down

0 comments on commit a6ace30

Please sign in to comment.