pymc-devs / pymc-devs/pytensor

Implement MaskedTensorVariables and operations

Open
#259 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement NumPy compatibility
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

PyTensor doesn't have a type that behaves like masked arrays. It's not just a problem of wrapping a masked array.

In your example, as soon as you try to do some operation, you will get incorrect values:

import pytensor
import numpy as np

X = np.ma.masked_greater(np.array([1, 2, 3, 4]), 3)
sh_val = pytensor.shared(X)
print(sh_val.eval()) # [1,2,3,4]
sh_val.set_value(X)
print(sh_val.eval()) # [1,2,3,--]
print((sh_val + 1).eval())  # [2 3 4 5]

We can raise explicitly when a user tries to pass a masked array. To actually support numpy-like behavior, we would need to implement something like MaskedTensorVariables and write all the operations to support those types. Similar to how we handle SparseTensorVariables.

Originally posted by @ricardoV94 in https://github.com/pymc-devs/pytensor/issues/258#issuecomment-1488040890

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the pytensor.shared example and compare the proposed MaskedTensorVariables design with the existing SparseTensorVariables approach. Trace how the masked array behaves through an operation such as addition. Done means masked values are preserved correctly across supported operations, rather than producing unmasked numeric results.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.