Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpvanderschelling committed Oct 11, 2023
1 parent 3e0ecd9 commit 6ff28c1
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 30 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.1
1.4.2
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
project = 'f3dasm'
author = 'Martin van der Schelling'
copyright = '2022, Martin van der Schelling'
version = '1.4.1'
release = '1.4.1'
version = '1.4.2'
release = '1.4.2'


# -- General configuration ----------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/f3dasm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# =============================================================================


__version__ = '1.4.1'
__version__ = '1.4.2'


# Log welcome message and the version of f3dasm
Expand Down
5 changes: 3 additions & 2 deletions src/f3dasm/_src/datageneration/datagenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ def store(object: Any, name: str, to_disk: bool) -> None:
def job_number(self) -> int:
...


class DataGenerator:
"""Base class for a data generator"""

def pre_process(self, **kwargs) -> None:
def pre_process(self, experiment_sample: ExperimentSample, **kwargs) -> None:
"""Function that handles the pre-processing"""
...

def execute(self, **kwargs) -> None:
"""Function that calls the FEM simulator the pre-processing"""
raise NotImplementedError("No execute function implemented!")

def post_process(self, **kwargs) -> None:
def post_process(self, experiment_sample: ExperimentSample, **kwargs) -> None:
"""Function that handles the post-processing"""
...

Expand Down
20 changes: 14 additions & 6 deletions src/f3dasm/_src/datageneration/functions/function_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Modules
# =============================================================================

from __future__ import annotations

from typing import Any, Dict, Optional

from ...design.domain import Domain
Expand All @@ -19,20 +21,26 @@
#
# =============================================================================

FUNTION_MAPPING: Dict[str, DataGenerator] = {
f.name.lower().replace(' ', '').replace('-', '').replace('_', ''): f for f in _FUNCTIONS}
FUNCTION_MAPPING: Dict[str, DataGenerator] = {
f.name.lower().replace(' ', '').replace('-', '').replace('_', '').replace('.', ''): f for f in _FUNCTIONS}


def datagenerator_factory(data_generator: str, domain: Domain,
def datagenerator_factory(data_generator: str, domain: Domain | int,
kwargs: Optional[Dict[str, Any]] = None) -> DataGenerator:

if isinstance(domain, int):
dim = domain

else:
dim = len(domain)

if kwargs is None:
kwargs = {}

filtered_name = data_generator.lower().replace(' ', '').replace('-', '').replace('_', '')
filtered_name = data_generator.lower().replace(' ', '').replace('-', '').replace('_', '').replace('.', '')

if filtered_name in FUNTION_MAPPING:
return FUNTION_MAPPING[filtered_name](dimensionality=len(domain), **kwargs)
if filtered_name in FUNCTION_MAPPING:
return FUNCTION_MAPPING[filtered_name](dimensionality=dim, **kwargs)

else:
raise KeyError(f"Unknown data generator: {data_generator}")
Expand Down
13 changes: 9 additions & 4 deletions src/f3dasm/_src/design/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ def from_yaml(cls: Type[Domain], yaml: DictConfig) -> Domain:
Domain
Domain object
"""
args = {}
for space, params in yaml.items():
args[space] = {name: instantiate(param, _convert_="all") for name, param in params.items()}
return cls(**args)
return cls({name: instantiate(param, _convert_="all") for name, param in yaml.items()})

@classmethod
def from_dataframe(cls, df: pd.DataFrame) -> Domain:
Expand All @@ -171,9 +168,17 @@ def from_dataframe(cls, df: pd.DataFrame) -> Domain:
space = {}
for name, type in df.dtypes.items():
if type == 'float64':
if float(df[name].min()) == float(df[name].max()):
space[name] = ConstantParameter(value=float(df[name].min()))
continue

space[name] = ContinuousParameter(lower_bound=float(
df[name].min()), upper_bound=float(df[name].max()))
elif type == 'int64':
if int(df[name].min()) == int(df[name].max()):
space[name] = ConstantParameter(value=int(df[name].min()))
continue

space[name] = DiscreteParameter(lower_bound=int(df[name].min()), upper_bound=int(df[name].max()))
else:
space[name] = CategoricalParameter(df[name].unique().tolist())
Expand Down
13 changes: 7 additions & 6 deletions src/f3dasm/_src/experimentdata/experimentdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def from_file(cls: Type[ExperimentData], filename: str = 'experimentdata') -> Ex
ExperimentData object containing the loaded data.
"""
try:
return cls._from_file_attempt(filename)
return cls._from_file_attempt(Path(filename))
except FileNotFoundError:
try:
filename_with_path = Path(get_original_cwd()) / filename
Expand Down Expand Up @@ -239,21 +239,21 @@ def from_yaml(cls, config: DictConfig) -> ExperimentData:
# Option 2: Sample from the domain
elif 'from_sampling' in config.experimentdata:
domain = Domain.from_yaml(config.domain)
return cls.from_sampling(sampler=config.sampler, domain=domain,
return cls.from_sampling(sampler=config.experimentdata.from_sampling.sampler, domain=domain,
n_samples=config.experimentdata.from_sampling.n_samples,
seed=config.experimentdata.from_sampling.seed,
filename=config.experimentdata.from_sampling.filename)
filename=config.experimentdata.name)

else:
return cls(**config)

@classmethod
def _from_file_attempt(cls: Type[ExperimentData], filename: str) -> ExperimentData:
def _from_file_attempt(cls: Type[ExperimentData], filename: Path) -> ExperimentData:
"""Attempt to create an ExperimentData object from .csv and .json files.
Parameters
----------
filename : str
filename : Path
Name of the file, excluding suffix.
Returns
Expand All @@ -268,7 +268,8 @@ def _from_file_attempt(cls: Type[ExperimentData], filename: str) -> ExperimentDa
"""
try:
return cls(domain=Path(f"{filename}{DOMAIN_SUFFIX}"), input_data=Path(f"{filename}{INPUT_DATA_SUFFIX}"),
output_data=Path(f"{filename}{OUTPUT_DATA_SUFFIX}"), jobs=Path(f"{filename}{JOBS_SUFFIX}"))
output_data=Path(f"{filename}{OUTPUT_DATA_SUFFIX}"), jobs=Path(f"{filename}{JOBS_SUFFIX}"),
filename=filename.name)
except FileNotFoundError:
raise FileNotFoundError(f"Cannot find the files from {filename}.")

Expand Down
12 changes: 4 additions & 8 deletions src/f3dasm/_src/run_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import numpy as np
import pandas as pd
import xarray as xr
from pathos.helpers import mp

from f3dasm.design import Domain
from f3dasm.optimization import Optimizer
from pathos.helpers import mp

# Locals
from .datageneration.datagenerator import DataGenerator
Expand Down Expand Up @@ -85,16 +84,13 @@ def to_xarray(self) -> xr.Dataset:
xarr.attrs['realization_seeds']: List[int] = list(self.seeds)

# Benchmark functions
xarr.attrs['function_seed']: int = self.kwargs['seed']
xarr.attrs['function_seed']: int = self.func.seed
xarr.attrs['function_name']: str = self.data_generator
xarr.attrs['function_noise']: str = self.kwargs['noise']
xarr.attrs['function_noise']: str = self.func.noise
xarr.attrs['function_dimensionality']: int = len(self.data[0].domain)

# Global minimum function
func = datagenerator_factory(data_generator=self.data_generator,
domain=self.data[0].domain, kwargs=self.kwargs)

_, g = func.get_global_minimum(d=func.dimensionality)
_, g = self.func.get_global_minimum(d=self.func.dimensionality)
xarr.attrs['function_global_minimum']: float = float(np.array(g if not isinstance(g, list) else g[0])[0, 0])
return xarr

Expand Down

0 comments on commit 6ff28c1

Please sign in to comment.