Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 31 commits into from
Mar 20, 2024

Conversation

Baschdl
Copy link
Contributor

@Baschdl Baschdl commented Mar 19, 2024

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
Copy link
Contributor

gmoss13 commented Mar 20, 2024

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 .

Copy link
Contributor

@michaeldeistler michaeldeistler left a comment

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.

python_version = "3.8"
reportUnsupportedDunderAll = false
reportGeneralTypeIssues = false
reportInvalidTypeForm = false
Copy link
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!

@@ -101,60 +99,6 @@ def variance(self):
)


class ScipyPytorchWrapper(Distribution):
Copy link
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
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
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.

Copy link
Contributor

@janfb janfb left a comment

Choose a reason for hiding this comment

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

great effort! 👏
Karma +1000

@Baschdl Baschdl merged commit 03a6b2c into main Mar 20, 2024
3 checks passed
@janfb janfb deleted the 847_pyright branch June 20, 2024 10:33
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