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

Edge count fix #323

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions matsciml/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,15 +638,58 @@ class Edge:
def sorted_index(self) -> tuple[int, int]:
return (min(self.src, self.dst), max(self.src, self.dst))

@property
def unsigned_image(self) -> np.ndarray:
"""
Returns the absolute image indices.

This is used when considering parity-inversion,
i.e. two edges are equivalent if they are in
in mirroring cell images.

Returns
-------
np.ndarray
Indices without parity
"""
return np.abs(self.image)

def __eq__(self, other: Edge) -> bool:
index_eq = self.sorted_index == other.sorted_index
image_eq = np.all(self.image == other.image)
image_eq = np.all(self.unsigned_image == other.unsigned_image)
return all([index_eq, image_eq])

def __str__(self) -> str:
return f"Sorted src/dst: {self.sorted_index}, image: {self.image}"
"""Represents the edge without phase or parity information. Mainly for hashing."""
return f"Sorted src/dst: {self.sorted_index}, |image|: {self.unsigned_image}"

def __hash__(self) -> int:
"""
This hash method is primarily intended for use in
``set`` comparisons to check for uniqueness.

The general idea for edge-uniqueness is assuming
the undirected case where ``src`` and ``dst``
is exchangable, and when not considering phase
for image indices.

As an example, the following two edges are
equivalent as they have interchangable ``src``
and ``dst`` indices, **and** the displacement
owing to image offsets is the same - just in
opposite directions,

```
Edge(src=1, dst=7, image=[-1, 0, 0])
Edge(src=7, dst=1, image[1, 0, 0])
```

Returns
-------
int
Permutation invariant hash of this edge.
"""

return hash(str(self))


Expand Down
Loading