debugging confusion created by DeferredOperations
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 459
- Forks
- 359
- Avg merge
- 3d 6h
- Merged PRs (30d)
- 73
Description
@nataliejpg ran into an issue trying to debug this code:
```python
demod_list = np.array([getattr(self, 'demod_freq_{}'.format(n)) for n in range(self.res_length)])
demod_mat = np.kron(demod_list, np.ones(self.samples_per_record)).reshape(self.res_length, self.samples_per_record)
integer_mat = np.kron(np.ones(self.res_length), np.arange(self.samples_per_record)).reshape((self.res_length, self.samples_per_record))
angle_mat = np.multiply(2 * np.pi * integer_mat, demod_mat) / self.sample_rate
self.cos_mat = np.cos(angle_mat)
```
Which throws an error:
```
AttributeError: 'DeferredOperations' object has no attribute 'cos'
```
The root cause is that the `getattr` call yields a `Parameter`, not a parameter value, so needs to be evaluated right away:
```
demod_list = np.array([getattr(self, 'demod_freq_{}'.format(n))() for n in range(self.res_length)])
```
But because the `np.multiply` call actually *succeeded* (burying the `Parameter` in a new `DeferredOperations` object), this was tricky to debug.
Had `Parameter` not been a `DeferredOperations` at all, the error would have happened in `np.multiply`, with something like:
```
TypeError: unsupported operand type(s) for *: 'int' and 'StandardParameter'
```
Which is a lot easier to tie back to the root cause. Perhaps we can have `DeferredOperations` override `__getattr__` to point back to its roots with the error message, something like:
```
AttributeError: 'DeferredOperations' object has no attribute 'cos' -
Did you mean to evaluate your parameter earlier?
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the DeferredOperations and Parameter entry points and reproduce the shown NumPy expression to trace where the Parameter becomes deferred. Done means an attempted attribute such as cos reports that the underlying parameter should be evaluated earlier, without hiding the original cause.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100