Skip to content

Commit

Permalink
remove attribute id from MolecularFamily
Browse files Browse the repository at this point in the history
  • Loading branch information
CunliangGeng committed Dec 14, 2023
1 parent 79ec809 commit 4031a73
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
16 changes: 4 additions & 12 deletions src/nplinker/metabolomics/molecular_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ def __init__(self, family_id: str):
"""Class to model molecular family.
Args:
family_id(str): Id for the molecular family.
family_id(str): Unique id for the molecular family.
Attributes:
id(int): Unique id for the molecular family.
family_id(str): Id for the molecular family.
family_id(str): Unique id for the molecular family.
spectra_ids(set[str]): Set of spectrum ids in the molecular family.
"""
self.id: int = -1
self.family_id: str = family_id
self.spectra_ids: set[str] = set()
self._spectra: set[Spectrum] = set()
Expand All @@ -37,17 +35,11 @@ def __repr__(self) -> str:

def __eq__(self, other) -> bool:
if isinstance(other, MolecularFamily):
return self.id == other.id and self.family_id == other.family_id
return self.family_id == other.family_id
return NotImplemented

def __hash__(self) -> int:
"""Hash function for MolecularFamily.
Note that MolecularFamily is a mutable container, so here we hash on
the id and family_id only to avoid the hash value changing when
`self.spectra` is updated.
"""
return hash((self.id, self.family_id))
return hash(self.family_id)

@property
def spectra(self) -> set[Spectrum]:
Expand Down
3 changes: 1 addition & 2 deletions tests/metabolomics/test_molecular_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_init():
"""Test MolecularFamily class initialization."""
mf = MolecularFamily("mf001")
assert mf.family_id == "mf001"
assert mf.id == -1
assert mf.spectra_ids == set()
assert mf.spectra == set()
assert mf.strains == StrainCollection()
Expand All @@ -53,7 +52,7 @@ def test_eq():
def test_hash():
"""Test __hash__ method."""
mf = MolecularFamily("mf001")
assert hash(mf) == hash((-1, "mf001"))
assert hash(mf) == hash("mf001")


def test_spectra(spectrum1):
Expand Down

0 comments on commit 4031a73

Please sign in to comment.