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

DictOfNamedArrays: better repr, keys #485

Merged
merged 10 commits into from
Nov 25, 2024
Merged

DictOfNamedArrays: better repr, keys #485

merged 10 commits into from
Nov 25, 2024

Conversation

matthiasdiener
Copy link
Collaborator

@matthiasdiener matthiasdiener commented Feb 19, 2024

Please squash

Comment on lines +824 to +859
def keys(self) -> KeysView[str]:
return self._data.keys()
Copy link
Collaborator Author

@matthiasdiener matthiasdiener Feb 19, 2024

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'])

Copy link
Collaborator

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!)

Copy link
Owner

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.

Copy link
Collaborator Author

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).

Copy link
Owner

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.

Copy link
Collaborator Author

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?

@matthiasdiener matthiasdiener marked this pull request as ready for review February 19, 2024 21:21
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) + ")"
Copy link
Collaborator Author

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?

Copy link
Owner

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.

Copy link
Collaborator Author

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
Copy link
Collaborator Author

@matthiasdiener matthiasdiener Feb 23, 2024

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.

Copy link
Owner

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]:
Copy link
Owner

Choose a reason for hiding this comment

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

Add to docs, too.

Copy link
Collaborator Author

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]:
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
def keys(self) -> KeysView[str]:
def keys(self) -> AbstractSet[str]:

Copy link
Collaborator Author

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)

Copy link
Owner

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?

Copy link
Collaborator Author

@matthiasdiener matthiasdiener Feb 23, 2024

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

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.

@matthiasdiener
Copy link
Collaborator Author

This is ready for another look @inducer

@inducer inducer enabled auto-merge (squash) November 25, 2024 06:21
@inducer inducer merged commit 360ff9c into main Nov 25, 2024
11 checks passed
@inducer inducer deleted the dona-repr-keys branch November 25, 2024 06:50
nkoskelo pushed a commit to nkoskelo/pytato that referenced this pull request Nov 25, 2024
* 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]>
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.

3 participants