Skip to content

New method ComparerCollection.merge #513

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
40 changes: 25 additions & 15 deletions modelskill/comparison/_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,7 @@ class ComparerCollection(Mapping, Scoreable):
plotter = ComparerCollectionPlotter

def __init__(self, comparers: Iterable[Comparer]) -> None:
self._comparers: Dict[str, Comparer] = {}

for cmp in comparers:
if cmp.name in self._comparers:
# comparer with this name already exists!
# maybe the user is trying to add a new model
# or a new time period
self._comparers[cmp.name] += cmp
else:
self._comparers[cmp.name] = cmp

self._comparers = {cmp.name: cmp for cmp in comparers}
self.plot = ComparerCollection.plotter(self)
"""Plot using the [](`~modelskill.comparison.ComparerCollectionPlotter`)"""

Expand Down Expand Up @@ -186,6 +176,25 @@ def __repr__(self) -> str:
out.append(f"{index}: {key} - {value.quantity}")
return str.join("\n", out)

def merge(self, other: "ComparerCollection" | Comparer) -> "ComparerCollection":
# make a copy of self to avoid modifying the original
res = self.copy()

if isinstance(other, Comparer):
if other.name in res._comparers:
res._comparers[other.name] += other
else:
res._comparers[other.name] = other
elif isinstance(other, ComparerCollection):
for cmp in other:
if cmp.name in self._comparers:
res._comparers[cmp.name] += cmp
else:
res._comparers[cmp.name] = cmp
else:
raise TypeError(f"Cannot merge {type(other)} with {type(self)}")
return res

def rename(self, mapping: Dict[str, str]) -> "ComparerCollection":
"""Rename observation, model or auxiliary data variables

Expand Down Expand Up @@ -258,10 +267,11 @@ def __add__(
if not isinstance(other, (Comparer, ComparerCollection)):
raise TypeError(f"Cannot add {type(other)} to {type(self)}")

if isinstance(other, Comparer):
return ComparerCollection([*self, other])
elif isinstance(other, ComparerCollection):
return ComparerCollection([*self, *other])
return self.merge(other)
# if isinstance(other, Comparer):
# return ComparerCollection([*self, other])
# elif isinstance(other, ComparerCollection):
# return ComparerCollection([*self, *other])

def sel(
self,
Expand Down
9 changes: 5 additions & 4 deletions tests/test_combine_comparers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_concat_model(o123, mrmike, mrmike2):
assert cc2.mod_names[0] == cc12.mod_names[-1]
assert cc2.end_time == cc12.end_time

cc12b = cc1 + cc2
cc12b = cc1.merge(cc2)
assert cc12b.score() == cc12.score()
assert cc12b.n_points == cc12.n_points

Expand All @@ -77,7 +77,7 @@ def test_concat_model_different_time(o123, mrmike, mr2days):
assert cc2.mod_names[0] == cc12.mod_names[-1]
assert cc2.end_time == cc12.end_time

cc12b = cc1 + cc2
cc12b = cc1.merge(cc2)
assert cc12b.score() == cc12.score()
assert cc12b.n_points == cc12.n_points

Expand Down Expand Up @@ -114,7 +114,7 @@ def test_concat_time_overlap(o123, mrmike):
assert cc1.n_points > cc26.n_points

# cc26 completely contained in cc1
cc12 = cc1 + cc26
cc12 = cc1.merge(cc26)
assert cc1.start_time == cc12.start_time
assert cc1.end_time == cc12.end_time
assert cc1.n_points == cc12.n_points
Expand All @@ -132,10 +132,11 @@ def test_concat_time_overlap(o123, mrmike):
cc2 = ms.match([o1, o2, o3], mrmike)

# cc26 _not_ completely contained in cc2
cc12 = cc26 + cc2
cc12 = cc26.merge(cc2)
assert cc2.start_time > cc12.start_time
assert cc2.end_time == cc12.end_time
assert cc2.n_points < cc12.n_points

# + is supported but not recommended
cc12a = cc2 + cc26
assert cc12a.n_points == cc12.n_points