def from_dict(cls, data): if type(data) != dict: raise ValidationError(message="'data' must be a dict") if set(data.keys()) != set(["name", "category", "search_space"]): raise ValidationError( message="'name', 'category', 'search_space' keys are mandatory" ) return cls(**data)
def from_dict(cls, data): if data.get("loss") is None: raise ValidationError( message="Loss is mandatory for an observation") if data.get("sample") is None: raise ValidationError( message="Sample is mandatory for an observation") if type(data["sample"]) != dict: raise ValidationError( message="Sample must be a dict of parameter_name/values") return cls(data["sample"], data["loss"], data.get("weight", 1))
def __init__(self, parameters): if type(parameters) != list: raise ValidationError( message="'parameters' must be a list of Parameter") for parameter in parameters: if type(parameter) != Parameter: raise ValidationError( message="'parameters' must be a list of Parameter") if len(set([x.name for x in parameters])) != len(parameters): raise ValidationError( message="Each parameter must have a different name") self.parameters = parameters.copy() self.observations = []
def __init__(self, sample, loss, weight=1): self.loss = loss if type(sample) != dict: raise ValidationError( message="Sample must be a dict of 'parameter_name': value") self.sample = sample self.weight = 1
def validate_normal(search_space): # error = "Expected a type dict with mandatory keys : [mu, sigma] and optional key log or step" if type(search_space) != dict: raise ValidationError(message_key="search_space_type") search_space = search_space.copy() if "mu" not in search_space.keys(): raise ValidationError(message_key="mu_mandatory") if type(search_space["mu"]) not in (int, float): raise ValidationError(message_key="mu_type") if "sigma" not in search_space.keys(): raise ValidationError(message_key="sigma_mandatory") if type(search_space["sigma"]) not in (int, float): raise ValidationError(message_key="sigma_type") if "low" in search_space.keys(): if type(search_space["low"]) not in (int, float): raise ValidationError(message_key="low_type") if "high" in search_space.keys(): if type(search_space["high"]) not in (int, float): raise ValidationError(message_key="high_type") if "high" in search_space.keys() and "low" in search_space.keys(): if search_space["high"] <= search_space["low"]: raise ValidationError(message_key="high_inferior_low") search_space.setdefault("low", -np.inf) search_space.setdefault("high", np.inf) if "step" in search_space.keys(): if search_space["step"] and type( search_space["step"]) not in (int, float): raise ValidationError(message_key="step_type") if search_space["step"] and search_space["step"] >= max( [np.abs(search_space["high"]), np.abs(search_space["low"])]): raise ValidationError(message_key="high_inferior_step") search_space.setdefault("step", None) return search_space
def __init__(self, name, category, search_space): self.name = name if category not in sample_generators.keys(): raise ValidationError(message="Category not recognized.") self.category = category self.search_space = validate_search_space[self.category](search_space)
def from_list(cls, parameters_data): parameters = [] if type(parameters_data) != list: raise ValidationError( message="parameters_data must be a list of dict") for parameter_data in parameters_data: parameters.append(Parameter.from_dict(parameter_data)) return cls(parameters)
def add_observations_from_list(self, observations, raise_exception=False): if type(observations) == list: for observation_data in observations: try: observation = Observation.from_dict(observation_data) self.add_observation(observation, raise_exception=raise_exception) except Exception as e: if raise_exception: raise e else: if raise_exception: raise ValidationError( message="Need to give a list of observations")
def add_observation(self, observation, raise_exception=True): valid, reason = self._check_observation(observation) if valid: self.observations.append(observation) elif raise_exception: raise ValidationError(message=reason)