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

Extend schema validation to support tensor types #1662

Closed
1 task done
kratsg opened this issue Oct 22, 2021 · 1 comment · Fixed by #1665
Closed
1 task done

Extend schema validation to support tensor types #1662

kratsg opened this issue Oct 22, 2021 · 1 comment · Fixed by #1665
Assignees
Labels
feat/enhancement New feature or request schema and spec JSON schema
Projects

Comments

@kratsg
Copy link
Contributor

kratsg commented Oct 22, 2021

Summary

Schema validation for lists looks just at regular python lists. It should be possible to extend it to support the tensor types that pyhf supports.

We can get this working if each tensor backend gives the tensor type: (list, numpy_backend.array_type, jax_backend.array_type, ...).

I'm not sure if we can or can't rely on the backend here. We've got the actual tensor object that we need to validate against... and the validator won't necessarily have access to the current backend -- but we can teach it that if needed.

isinstance(instance, list) or isinstance(instance, pyhf.tensorlib.array_type)

and this should fix some common issues where we need to pass in validate=False explicitly to build the models...

Additional Information

>>> from jsonschema import Draft6Validator
>>> from jsonschema.validators import extend
>>> import numpy as np
>>> def is_array_or_tensor(checker, instance):
...   return isinstance(instance, (list, np.ndarray))
... 
>>> type_checker = Draft6Validator.TYPE_CHECKER
>>> type_checker = Draft6Validator.TYPE_CHECKER.redefine('array', is_array_or_tensor)
>>> Validator = extend(Draft6Validator, type_checker=type_checker)
>>> validator_foo_required = Validator(schema={"type": "array"})
>>> example = [1, 2, 3]
>>> example_np = np.array([1,2,3])
>>> isinstance(example_np, np.ndarray)
True
>>> validator_foo_required.validate(example)
>>> validator_foo_required.validate(example_np)

Inspired by python-jsonschema/jsonschema#727

Code of Conduct

  • I agree to follow the Code of Conduct
@kratsg kratsg added feat/enhancement New feature or request needs-triage Needs a maintainer to categorize and assign labels Oct 22, 2021
@matthewfeickert matthewfeickert added schema and spec JSON schema and removed needs-triage Needs a maintainer to categorize and assign labels Oct 22, 2021
@kratsg kratsg self-assigned this Mar 25, 2022
@kratsg kratsg added this to To do in v0.7.0 via automation Mar 25, 2022
@kratsg
Copy link
Contributor Author

kratsg commented Mar 25, 2022

Also related: #882.

@kratsg kratsg removed this from To do in v0.7.0 Aug 30, 2022
@kratsg kratsg added this to To do in v0.7.1 via automation Aug 30, 2022
v0.7.1 automation moved this from To do to Done Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat/enhancement New feature or request schema and spec JSON schema
Projects
Development

Successfully merging a pull request may close this issue.

2 participants