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

Introduce Named Data Classes for mlos_core #811

Closed
wants to merge 8 commits into from

Conversation

jsfreischuetz
Copy link
Contributor

@jsfreischuetz jsfreischuetz commented Jul 23, 2024

Introduces @dataclass types for return values from mlos_core.Optimizers to improve code readability.

See also: #751

@jsfreischuetz jsfreischuetz marked this pull request as ready for review July 24, 2024 22:49
@jsfreischuetz jsfreischuetz requested a review from a team as a code owner July 24, 2024 22:49
elif left is not None and right is not None:
if not left.equals(right):
return False
return True
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be simplified a bit:

if left is not None and right is not None and left.equals(right):
  return True
return False

def __repr__(self) -> str:
return (
f"Observation(config={self.config}, score={self.score},"
+ " context={self.context}, metadata={self.metadata})"
Copy link
Contributor

Choose a reason for hiding this comment

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

same concat comment here

scores: pd.DataFrame,
context: Optional[pd.DataFrame] = None,
metadata: Optional[pd.DataFrame] = None,
observation: Observation,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
observation: Observation,
observations: Observations,

@@ -150,7 +134,7 @@ def suggest(
*,
context: Optional[pd.DataFrame] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
context: Optional[pd.DataFrame] = None,
context: Optional[pd.Series] = None,

Did this one get replacd by a Series elsewhere?

"""
pass # pylint: disable=unnecessary-pass # pragma: no cover

def get_observations(self) -> Tuple[pd.DataFrame, pd.DataFrame, Optional[pd.DataFrame]]:
def get_observations(self) -> Observation:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def get_observations(self) -> Observation:
def get_observations(self) -> Observations:

docstring needs updating too

).reset_index(drop=True)
return (configs, scores, contexts if len(contexts.columns) > 0 else None)
return Observation(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return Observation(
return Observations(

return Observation(
config=configs,
score=scores,
context=contexts if len(contexts.columns) > 0 else None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
context=contexts if len(contexts.columns) > 0 else None,
context=contexts if len(contexts) > 0 else None,

?


def get_best_observations(
self,
*,
n_max: int = 1,
) -> Tuple[pd.DataFrame, pd.DataFrame, Optional[pd.DataFrame]]:
) -> Observation:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) -> Observation:
) -> Observations:

Copy link
Contributor

Choose a reason for hiding this comment

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

docstring again

return (configs.loc[idx], scores.loc[idx], None if contexts is None else contexts.loc[idx])
observations = self.get_observations()
idx = observations.score.nsmallest(
n_max, columns=self._optimization_targets, keep="first"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
n_max, columns=self._optimization_targets, keep="first"
n_max, columns=self._optimization_targets, keep="first",

trailing comma, format fixups



def compare_optional_dataframe(
left: Optional[pd.DataFrame], right: Optional[pd.DataFrame]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
left: Optional[pd.DataFrame], right: Optional[pd.DataFrame]
left: Optional[pd.DataFrame], right: Optional[pd.DataFrame],

nit: trailing comma and black reformat

@bpkroth
Copy link
Contributor

bpkroth commented Sep 20, 2024

Got confused and started commenting here again.

I think #852 supercedes this one, so let's close this one.

@bpkroth bpkroth closed this Sep 20, 2024
bpkroth added a commit that referenced this pull request Nov 25, 2024
## Title

Refactor mlos_core APIs to encapsulate related data fields.

## Description

Refactors the mlos_core Optimizer APIs to accept new data types
`Observation`, `Observations` and return `Suggestion`, instead of a mess
of `Tuple[DataFrame, DataFrame, Optional[DataFrame],
Optional[DataFrame]]` that must be named and checked everywhere.

Additionally, this makes it more explicit that `_register` is a bulk
operation that is not actually supported currently by the underlying
optimizers, though leaves notes on how we can do that in the future.

## Type of Change

- Refactor

---

## Testing

Usual CI plus some new unit tests for new data type operations.

---

## Additional Notes

A more significant rewrite of named tuple support inside mlos_core.
This is based on comments in #811 as well as conversations with @bpkroth

---

---------

Co-authored-by: Brian Kroth <[email protected]>
Co-authored-by: Brian Kroth <[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.

2 participants