From 27977f79cfe9da298459da90cbe79696483dfca5 Mon Sep 17 00:00:00 2001 From: Nathan Petersen Date: Thu, 15 Feb 2024 12:43:30 -0600 Subject: [PATCH 1/2] Allow user to transform full_results before saving (e.g., to reduce size) --- mach_opt/mach_opt.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mach_opt/mach_opt.py b/mach_opt/mach_opt.py index 9ba5f6a9..37b73a2d 100644 --- a/mach_opt/mach_opt.py +++ b/mach_opt/mach_opt.py @@ -278,9 +278,10 @@ def bounds(self) -> tuple: class DataHandler(): """ Parent class for data handlers""" - def __init__(self, archive_filepath, designer_filepath): + def __init__(self, archive_filepath, designer_filepath, full_results_transform=None): self.archive_filepath = archive_filepath self.designer_filepath = designer_filepath + self.full_results_transform = full_results_transform def save_to_archive(self, x, design, full_results, objs): """ Save machine evaluation data to optimization archive using Pickle @@ -291,9 +292,15 @@ def save_to_archive(self, x, design, full_results, objs): full_results: Input, output, and results corresponding to each step of an evaluator objs: Fitness values corresponding to a design """ - # assign relevant data to OptiData class attributes + + # Apply transform to full_results (if needed) + if self.full_results_transform is not None: + full_results = self.full_results_transform(full_results) + + # Assign relevant data to OptiData class attributes opti_data = OptiData(x=x, design=design, full_results=full_results, objs=objs) - # write to pkl file. 'ab' indicates binary append + + # Write to pkl file. 'ab' indicates binary append with open(self.archive_filepath, 'ab') as archive: pickle.dump(opti_data, archive, -1) From dec6b5ac8a52c6336a7f96f4b0860543e491b1ef Mon Sep 17 00:00:00 2001 From: Nathan Petersen Date: Fri, 16 Feb 2024 10:41:38 -0600 Subject: [PATCH 2/2] Move full_results transform earlier so it's already applied before get_objectives --- mach_opt/mach_opt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mach_opt/mach_opt.py b/mach_opt/mach_opt.py index 37b73a2d..2eeed2f8 100644 --- a/mach_opt/mach_opt.py +++ b/mach_opt/mach_opt.py @@ -201,6 +201,10 @@ def fitness(self, x: "tuple") -> "tuple": # but make sure by joining it here p.join() + # Apply transform to full_results (if needed) + if self.__dh.full_results_transform is not None: + full_results = self.__dh.full_results_transform(full_results) + objs = self.__design_space.get_objectives(full_results) self.__dh.save_to_archive(x, design, full_results, objs) # print('The fitness values are', objs) @@ -293,10 +297,6 @@ def save_to_archive(self, x, design, full_results, objs): objs: Fitness values corresponding to a design """ - # Apply transform to full_results (if needed) - if self.full_results_transform is not None: - full_results = self.full_results_transform(full_results) - # Assign relevant data to OptiData class attributes opti_data = OptiData(x=x, design=design, full_results=full_results, objs=objs)