Skip to content

Use newest version of pyright, ignored a lot of errors, identified some bugs - #1045

Merged
Baschdl merged 31 commits into
mainfrom
847_pyright
Mar 20, 2024
Merged

Baschdl merged 31 commits into
mainfrom
847_pyright

Conversation

@Baschdl

@Baschdl Baschdl commented Mar 19, 2024 •

Copy link
Copy Markdown
Contributor

I worked through the errors pyright raises on the newest version. I used very small commits to be able to cherry pick if we only want to go with some of the decisions I made. You can rerun stuff with pyright locally (you can also find the errors in the CI run).

I believe that the remaining errors are actual errors for which we don't have unit tests currently (I tagged the people who seem to be responsible for those parts of the code):

  • @gmoss13 Error in analysis/conditional_density.py:
    logits, means, precfs, _ = extract_and_transform_mog(net=mdn.net, context=x_o)

    Here you assume that the network of your new DensityEstimator has the same interface as Flow. As an example, I would assume that the general nn.Module doesn't necessarily has the _distribution attribute we access later on:
    dist = net._distribution
  • @gmoss13 Error in neural_nets/mnle.py:
    continuous_net=cont_nle,

    This error currently only says that the __init__ of MixedDensityEstimator doesn't expect aNFlowsFlow:
    continuous_net: flows.Flow,

    The obvious thing would be to add it as a possible type but the problem is that the used calls to sample, e.g.
    continuous_x = self.continuous_net.sample(

    are not compatible to your DensityEstimator.
  • @janfb Errors in utils/user_input_checks.py and user_input_checks_utils.py:
    return self.prior_scipy.var

    You assume that univariate and multivariate random variables have the same interface. I've already added a failing test in b455a7f
  • Maybe it's worth having another look into e9d8dff as it was unclear to me if LazyTransform and AffineTransform | Unconditional are compatible.

Feel free to fix things directly in this PR or create issues for those problems. We won't be able to merge this PR before fixing them.

Fixes #847

Checklist

  • I agree with re-licensing my contribution from AGPLv3 to Apache-2.0.
  • I performed linting and formatting as described in the contribution
    guidelines

Baschdl added 23 commits March 19, 2024 17:02
…n return type of Parallel generator (reportAssignmentType)
… suspect that they are compatible because they're all based on nn.Modules
@gmoss13

gmoss13 commented Mar 20, 2024 •

Copy link
Copy Markdown
Contributor

Thanks @Baschdl, great work! Some comments:

@gmoss13 Error in analysis/conditional_density.py:

sbi/sbi/analysis/conditional_density.py

Line 209 in 1d25046
logits, means, precfs, _ = extract_and_transform_mog(net=mdn.net, context=x_o)

Here you assume that the network of your new DensityEstimator has the same interface as Flow. As an example, I would assume that the general nn.Module doesn't necessarily has the _distribution attribute we access later on:

sbi/sbi/utils/conditional_density_utils.py

Line 179 in 1d25046
dist = net._distribution

These functions only apply to mixture density networks - and should only be called when the density estimator is an MDN. Our implementation of MDN is using normflows, and so this function should only be called when the DensityEstimator is specifically an instance of NFlowsFlow. I can jump in here and explicitly change the type for the mdn argument, and clarify this point in the docstring.

@gmoss13 Error in neural_nets/mnle.py:

sbi/sbi/neural_nets/mnle.py

Line 96 in c347d5c
continuous_net=cont_nle,

This error currently only says that the init of MixedDensityEstimator doesn't expect aNFlowsFlow:

sbi/sbi/neural_nets/mnle.py

Line 217 in c347d5c
continuous_net: flows.Flow,

The obvious thing would be to add it as a possible type but the problem is that the used calls to sample, e.g.

sbi/sbi/neural_nets/mnle.py

Line 265 in c347d5c
continuous_x = self.continuous_net.sample(

are not compatible to your DensityEstimator.

Yes - this should resolved by updating the MixedDensityEstimator to the new DensityEstimator interface (see #968) - so I suggest we wait for #968 to be resolved before merging this @coschroeder .

@michaeldeistler michaeldeistler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Infinite karma for this! A few requests below, but feel free to merge then.

Comment thread pyproject.toml
python_version = "3.8"
reportUnsupportedDunderAll = false
reportGeneralTypeIssues = false
reportInvalidTypeForm = false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this, thank you!

)


class ScipyPytorchWrapper(Distribution):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janfb's assessment was that no one is using this wrapper, so instead of trying to fix this issue he suggested to deprecate it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge it for now but we can still bring it back. Let me know.

@janfb janfb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great effort! 👏
Karma +1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tests are failing because of new pyright release.

4 participants