-
Notifications
You must be signed in to change notification settings - Fork 16
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
DictOfNamedArrays: better repr, keys #485
Conversation
9173fb6
to
ab24c94
Compare
def keys(self) -> KeysView[str]: | ||
return self._data.keys() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a super-impactful change, but printing keys is more convenient with this:
print(dona.keys())
before:
KeysView(DictOfNamedArrays({'_pt_out_0_0': Einsum(axes=(Axis(tags=FrozenOrderedSet({DiscretizationElementAxisTag()})), Axis(tags=FrozenOrderedSet({DiscretizationDOFAxisTag()}))), tags={FirstAxisIsElementsTag()}, access_descriptors=((EinsumElementwiseAxis(dim=0), EinsumElementwiseAxis(dim=1)), (EinsumElementwiseAxis(dim=1), EinsumReductionAxis(dim=0)), (EinsumElementwiseAxis(dim=0), EinsumReductionAxis(dim=0))), args=(IndexLambda(axes=(Axis(tags=FrozenOrderedSet({DiscretizationElementAxisTag()})), Axis(tags=FrozenOrderedSet({DiscretizationDOFAxisTag()}))), shape=(512, 1), dtype='float64', expr=Quotient(1.0, Subscript(Variable('_in1'), (Variable('_0'), Variable('_1')))), bindings={'_in1': DataWrapper(axes=(Axis(tags=FrozenOrderedSet({DiscretizationElementAxisTag()})), Axis(tags=FrozenOrderedSet({DiscretizationDOFAxisTag()}))), tags={PrefixNamed(prefix='area_el_vol')}, data=<arraycontext.impl.pyopencl.taggable_cl_array.TaggableCLArray object at 0x1666b2410>, _shape=(512, 1))}), DataWrapper(axes=(Axis(tags=frozenset({DiscretizationDOFAxisTag()})), Axis(tags=frozenset({DiscretizationDOFAxisTag()}))), tags={}, data=<arraycontext.impl.pyopencl.taggable_cl_array.TaggableCLArray object at 0x1666b3690>, _shape=(10, 10)), IndexLambda(axes=(Axis(tags=FrozenOrderedSet({DiscretizationElementAxisTag()})), Axis(tags=FrozenOrderedSet({DiscretizationDOFAxisTag()}))), shape=(512, 10), dtype='float64', expr=Sum((Subscript(Variable('_in0'), (Variable('_0'), Variable('_1'))), Subscript(Variable('_in1'), (Variable('_0'), Variable('_1'))))), bindings={'_in0': IndexLambda(axes=(Axis(tags=FrozenOrderedSet({DiscretizationElementAxisTag()})), Axis(tags=FrozenOrderedSet({DiscretizationDOFAxisTag()}))), shape=(512, 10), dtype='float64', expr=Product((-1.0, Subscript(Variable('_in1'), (Variable('_0'), Variable('_1'))))), bindings={'_in1': Einsum(axes=(Axis(tags=FrozenOrderedSet({DiscretizationElementAxisTag()})), Axis(tags=FrozenOrderedSet({DiscretizationDOFAxisTag()}))), tags={FirstAxisIsElementsTag()}, access_descriptors=((EinsumReductionAxis(dim=0), EinsumReductionAxis(dim=1), EinsumElementwiseAxis(dim=0), EinsumReductionAxis(dim=2)), (EinsumReductionAxis(dim=1), EinsumElementwiseAxis(dim=1), EinsumReductionAxis(dim=2)), (EinsumReductionAxis(dim=0), EinsumElementwiseAxis(dim=0), EinsumReductionAxis(dim=2))), args=((...), (...), (...)), redn_axis_to_redn_descr={EinsumReductionAxis(dim=0): ReductionDescriptor(tags=FrozenOrderedSet({DiscretizationAmbientDimAxisTag()})), EinsumReductionAxis(dim=1): ReductionDescriptor(tags=FrozenOrderedSet({DiscretizationTopologicalDimAxisTag()})), EinsumReductionAxis(dim=2): ReductionDescriptor(tags=FrozenOrderedSet({DiscretizationDOFAxisTag()}))}, index_to_access_descr={'e': EinsumElementwiseAxis(dim=0), 'i': EinsumElementwiseAxis(dim=1), 'j': EinsumReductionAxis(dim=2), 'r': EinsumReductionAxis(dim=1), 'x': EinsumReductionAxis(dim=0)})}), '_in1': Einsum(axes=(Axis(tags=FrozenOrderedSet({DiscretizationElementAxisTag()})), Axis(tags=FrozenOrderedSet({DiscretizationDOFAxisTag()}))), tags={FirstAxisIsElementsTag()}, access_descriptors=((EinsumElementwiseAxis(dim=1), EinsumReductionAxis(dim=0), EinsumReductionAxis(dim=1)), (EinsumReductionAxis(dim=0), EinsumElementwiseAxis(dim=0), EinsumReductionAxis(dim=1)), (EinsumReductionAxis(dim=0), EinsumElementwiseAxis(dim=0), [......]
after:
dict_keys(['_pt_out_0_0', '_pt_out_1_0', '_pt_out_2_0'])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, this is more pleasant to look at! (Thanks!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably support this on AbstractResultWithNamedArrays
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably support this on
AbstractResultWithNamedArrays
.
I'm not sure how I could access the actual data there (ie, self._data
in DictOfNamedArrays).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I meant is add the abstract method to that interface, and add it to the other implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of 914a14b?
pytato/array.py
Outdated
@@ -817,7 +817,12 @@ def __iter__(self) -> Iterator[str]: | |||
return iter(self._data) | |||
|
|||
def __repr__(self) -> str: | |||
return "DictOfNamedArrays(" + str(self._data) + ")" | |||
return "DictOfNamedArrays(" + repr(self._data) + ")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should tags
be added here too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, since it's part of the representation. Though we currently don't use "DONA" tags for anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added in 0278d56
@@ -129,6 +129,10 @@ def __len__(self) -> int: | |||
def __iter__(self) -> Iterator[str]: | |||
return iter(self._result_names) | |||
|
|||
# type-ignore-reason: AbstractResultWithNamedArrays returns a KeysView here | |||
def keys(self) -> FrozenSet[str]: # type: ignore[override] | |||
return self._result_names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could return dict.fromkeys(self._result_names).keys()
here if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AbstractSet
suggestion above should resolve this.
@@ -770,6 +770,10 @@ def __eq__(self, other: Any) -> bool: | |||
from pytato.equality import EqualityComparer | |||
return EqualityComparer()(self, other) | |||
|
|||
@abstractmethod | |||
def keys(self) -> KeysView[str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add to docs, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 8ee0b82
@@ -770,6 +770,10 @@ def __eq__(self, other: Any) -> bool: | |||
from pytato.equality import EqualityComparer | |||
return EqualityComparer()(self, other) | |||
|
|||
@abstractmethod | |||
def keys(self) -> KeysView[str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def keys(self) -> KeysView[str]: | |
def keys(self) -> AbstractSet[str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mypy doesn't really like it:
pytato/array.py:775: error: Return type "AbstractSet[str]" of "keys" incompatible with return type "KeysView[str]" in supertype "Mapping" [override]
(same for typing
and collections.abc
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh. Unless I'm mistaken, that seems to contradict the ABC docs?
How about Collection
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh. Unless I'm mistaken, that seems to contradict the ABC docs?
I guess mypy complains whenever the return types aren't exactly the same as in the superclass?
How about
Collection
?
Same issue: pytato/array.py:776: error: Return type "Collection[str]" of "keys" incompatible with return type "KeysView[str]" in supertype "Mapping" [override]
@@ -129,6 +129,10 @@ def __len__(self) -> int: | |||
def __iter__(self) -> Iterator[str]: | |||
return iter(self._result_names) | |||
|
|||
# type-ignore-reason: AbstractResultWithNamedArrays returns a KeysView here | |||
def keys(self) -> FrozenSet[str]: # type: ignore[override] | |||
return self._result_names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AbstractSet
suggestion above should resolve this.
8ee0b82
to
26ed31d
Compare
26ed31d
to
b5ab8d5
Compare
This is ready for another look @inducer |
* DictOfNamedArrays: better repr, keys * add tags to repr * make keys() abstract * add to doc * fix lint * remove invalid requirements.txt pytools version specification --------- Co-authored-by: Andreas Klöckner <[email protected]>
Please squash