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

History #2

Draft
wants to merge 6 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
21 changes: 17 additions & 4 deletions xopt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# from xopt.generators import get_generator_and_defaults
from xopt.pydantic import XoptBaseModel
from xopt.utils import build_generator_from_saved_state
from xopt.vocs import VOCS

__version__ = _version.get_versions()["version"]
Expand Down Expand Up @@ -66,7 +67,8 @@ def __init__(
Initialize Xopt object using either a config dictionary or explicitly

Args:
config: dict, or YAML or JSON str or file. This overrides all other arguments.
config: dict, or YAML or JSON str or file.
This overrides all other arguments.

generator: Generator object
evaluator: Evaluator object
Expand Down Expand Up @@ -310,10 +312,17 @@ def check_components(self):
if self.vocs is None:
raise XoptError("Xopt VOCS is not specified")

def rebuild_from_previous_state(self, index):
"""rebuild generator from saved history"""
if self.options.dump_file is not None:
return build_generator_from_saved_state(
index=index, dump_file=self.options.dump_file
)

def dump_state(self):
"""dump data to file"""
if self.options.dump_file is not None:
output = state_to_dict(self)
output = state_to_dict(self, include_history=True)
with open(self.options.dump_file, "w") as f:
yaml.dump(output, f)
logger.debug(f"Dumped state to YAML file: {self.options.dump_file}")
Expand Down Expand Up @@ -394,7 +403,8 @@ def yaml(self, filename=None, *, include_data=False):

def __repr__(self):
"""
Returns infor about the Xopt object, including the YAML representation without data.
Returns infor about the Xopt object,
including the YAML representation without data.
"""
return f"""
Xopt
Expand Down Expand Up @@ -489,7 +499,7 @@ def xopt_kwargs_from_dict(config: dict) -> dict:
}


def state_to_dict(X, include_data=True):
def state_to_dict(X, include_data=True, include_history=False):
# dump data to dict with config metadata
output = {
"xopt": json.loads(X.options.json()),
Expand All @@ -503,4 +513,7 @@ def state_to_dict(X, include_data=True):
if include_data:
output["data"] = json.loads(X.data.to_json())

if include_history:
output["history"] = json.loads(X.generator.json())

return output
4 changes: 4 additions & 0 deletions xopt/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def __init__(self, **kwargs):
vocs: The vocs to use.
options: The options to use.
"""
print("start")
for key, value in kwargs.items():
print(key, value)
print("end")
super().__init__(**kwargs)
_check_vocs(self.vocs, self.supports_multi_objective)
logger.info(f"Initialized generator {self.name}")
Expand Down
3 changes: 2 additions & 1 deletion xopt/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ def map(self, fn, *iter: Iterable, **kwargs) -> Iterable[Future]:


def get_callable_from_string(callable: str, bind: Any = None) -> Callable:
"""Get callable from a string. In the case that the callable points to a bound method,
"""Get callable from a string.
In the case that the callable points to a bound method,
the function returns a callable taking the bind instance as the first arg.

Args:
Expand Down
18 changes: 18 additions & 0 deletions xopt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import pandas as pd
import torch
import yaml
from pydantic import parse_obj_as

from xopt.generators import get_generator

from .pydantic import get_descriptions_defaults
from .vocs import VOCS
Expand Down Expand Up @@ -172,6 +175,21 @@ def read_xopt_csv(*files):
return pd.concat(dfs)


def build_generator_from_saved_state(index, dump_file):
"""rebuild generator from saved history"""
with open(dump_file, "r") as file:
data = yaml.safe_load(file)

list_of_saved_generators = data["history"]
desired_state = list_of_saved_generators[index]

# desired_state['vocs'] = data['vocs']
generator_class = get_generator(data["generator"].pop("name"))
rebuilt_generator = parse_obj_as(generator_class, desired_state)

return rebuilt_generator


def visualize_model(generator, data, axes=None):
test_x = torch.linspace(*torch.tensor(generator.vocs.bounds.flatten()), 100)
generator.add_data(data)
Expand Down