DiamondLightSource / DiamondLightSource/fastcs-catio
Optimize average() function and improve async exception handling
- Dominant language
- Python
- Stars
- 1
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
## Location
[src/fastcs_catio/utils.py](src/fastcs_catio/utils.py) - `average()` function (lines 107-110)
## Current Implementation
The `average()` function iterates over each field in a numpy structured array and computes the mean individually:
```python
mean_array = np.empty(1, dtype=array.dtype)
for field in array.dtype.fields:
mean_array[field] = np.mean(array[field])
return mean_array
```
## Suggested Improvements
### 1. Vectorize the averaging operation
The current loop-based approach could be replaced with a single numpy call to improve performance. Consider using `numpy.mean()` with structured array views or `np.lib.recfunctions` utilities.
### 2. Async exception reporting
Investigate and implement proper exception reporting for async tasks that use this function. Currently, exceptions may be silently swallowed.
## References
See TODO comments at lines 108-109 in utils.py
Contributor guide
Assessment
This issue has not been assessed yet.