astropy / astropy/specutils

Bin edges from unequally-spaced centers

Open
#809 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
204
Forks
134
Avg merge
12h 55m
Merged PRs (30d)
1

Description

For unequal spacing of centers, the algorithm
https://github.com/astropy/specutils/blob/57dd2b28e5122e00e28333043192d30ec9f78a9d/specutils/spectra/spectral_axis.py#L45
produces bins where some bin center locations don't match the input.

This seems potentially problematic, or at least misleading. Maybe a warning should be raised?

#### Examples

Including https://github.com/astropy/specutils/issues/176#issue-318981747

```python
def edges_from_centers(centers):
a = np.insert(centers, 0, 2*centers[0] - centers[1])
b = np.append(centers, 2*centers[-1] - centers[-2])
edges = (a + b) / 2

return edges

cases = [
np.r_[1.0, 2.0, 3.0, 4.0], # ok (equally spaced)
np.r_[1.0, 2.0, 2.3, 3.0],
np.r_[1, 2, 4, 6, 8, 9, 10],
]

for case in cases:
edges = edges_from_centers(case)
print(" input centers:", " "*3 + "".join(f"{x:<6}" for x in case))
print(" computed edges:", "".join(f"{x:<6}" for x in edges))
print("implied centers:", " "*3 + "".join(f"{x:<6}" for x in (edges[:-1] + edges[1:])/2))
print()
```
Output:
```
input centers: 1.0 2.0 3.0 4.0
computed edges: 0.5 1.5 2.5 3.5 4.5
implied centers: 1.0 2.0 3.0 4.0

input centers: 1.0 2.0 2.3 3.0
computed edges: 0.5 1.5 2.15 2.65 3.35
implied centers: 1.0 1.825 2.4 3.0

input centers: 1 2 4 6 8 9 10
computed edges: 0.5 1.5 3.0 5.0 7.0 8.5 9.5 10.5
implied centers: 1.0 2.25 4.0 6.0 7.75 9.0 10.0
```

Contributor guide

Open the contributing guide

Research direction

Start with the bin-edge calculation at specutils/spectra/spectral_axis.py#L45 and reproduce the unequal-spacing examples from the issue. Establish the expected handling for mismatched implied centers, then add regression coverage for the demonstrated cases and verify the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.