fitter

Model fitting implementations.

class kmcpy.models.fitting.fitter.BaseFitter[source]

Main class for model fitting

abstractmethod as_dict()[source]

Convert the fitting object to a dictionary

abstractmethod fit(*args, **kwargs)[source]

Fit the model to the data

classmethod from_dict(d)
Parameters:

d – Dict representation.

Returns:

MSONable class.

classmethod load(file_path)

Loads a class from a provided json file.

Parameters:

file_path (os.PathLike) – The json file to load from.

Returns:

An instance of the class being reloaded.

Return type:

MSONable

save(json_path, mkdir=True, json_kwargs=None, pickle_kwargs=None, strict=True)

Utility that uses the standard tools of MSONable to convert the class to json format, but also save it to disk. In addition, this method intelligently uses pickle to individually pickle class objects that are not serializable, saving them separately. This maximizes the readability of the saved class information while allowing _any_ class to be at least partially serializable to disk.

For a fully MSONable class, only a class.json file will be saved to the location {save_dir}/class.json. For a partially MSONable class, additional information will be saved to the save directory at {save_dir}. This includes a pickled object for each attribute that e serialized.

Parameters:
  • file_path (os.PathLike) – The file to which to save the json object. A pickled object of the same name but different extension might also be saved if the class is not entirely MSONable.

  • mkdir (bool) – If True, makes the provided directory, including all parent directories.

  • json_kwargs (dict) – Keyword arguments to pass to the serializer.

  • pickle_kwargs (dict) – Keyword arguments to pass to pickle.dump.

  • strict (bool) – If True, will not allow you to overwrite existing files.

to_json()

Returns a json string representation of the MSONable object.

Return type:

str

unsafe_hash()

Returns an hash of the current object. This uses a generic but low performance method of converting the object to a dictionary, flattening any nested keys, and then performing a hash on the resulting object

classmethod validate_monty_v1(_MSONable__input_value)

Pydantic validator with correct signature for pydantic v1.x

classmethod validate_monty_v2(_MSONable__input_value, _)

Pydantic validator with correct signature for pydantic v2.x

class kmcpy.models.fitting.fitter.LCEFitter[source]

Fitting class for Local Cluster Expansion (LCE) model

as_dict()[source]

Convert the fitting object to a dictionary

fit(alpha, max_iter=1000000, ekra_fname='e_kra.txt', keci_fname='keci.txt', weight_fname='weight.txt', corr_fname='correlation_matrix.txt', lce_params_fname='lce_params.json', lce_params_history_fname='lce_params_history.json', fit_results_fname=None, normalize=True, orbit_fingerprints=None, local_environment_hash=None)[source]

Main fitting function

Parameters:
  • alpha (float) – Alpha value for Lasso regression

  • max_iter (int, optional) – Maximum number of iterations. Defaults to 1000000.

  • ekra_fname (str, optional) – File name for E_KRA storage. Defaults to ‘e_kra.txt’.

  • keci_fname (str, optional) – File name for KECI storage. Defaults to ‘keci.txt’.

  • weight_fname (str, optional) – File name for weight storage. Defaults to ‘weight.txt’.

  • corr_fname (str, optional) – File name for correlation matrix storage. Defaults to ‘correlation_matrix.txt’.

  • lce_params_fname (str, optional) – File name for LCE parameters storage. If None, skip saving.

  • lce_params_history_fname (str, optional) – File name for LCE parameters history storage. Defaults to ‘lce_params_history.json’.

  • fit_results_fname (str | None, optional) – Legacy fitting history file in orient=index JSON format. If None, skip writing.

  • orbit_fingerprints (list[str] | None, optional) – Orbit fingerprints associated with the fitted coefficient order.

  • local_environment_hash (str | None, optional) – Hash of the ordered local environment used to validate fitted parameters later.

Returns:

Fitted model parameters, predicted E_KRA, and DFT-computed E_KRA.

Return type:

tuple[LCEModelParameters, numpy.ndarray, numpy.ndarray]

classmethod from_dict(payload)[source]
Parameters:

d – Dict representation.

Returns:

MSONable class.

classmethod load(file_path)

Loads a class from a provided json file.

Parameters:

file_path (os.PathLike) – The json file to load from.

Returns:

An instance of the class being reloaded.

Return type:

MSONable

save(json_path, mkdir=True, json_kwargs=None, pickle_kwargs=None, strict=True)

Utility that uses the standard tools of MSONable to convert the class to json format, but also save it to disk. In addition, this method intelligently uses pickle to individually pickle class objects that are not serializable, saving them separately. This maximizes the readability of the saved class information while allowing _any_ class to be at least partially serializable to disk.

For a fully MSONable class, only a class.json file will be saved to the location {save_dir}/class.json. For a partially MSONable class, additional information will be saved to the save directory at {save_dir}. This includes a pickled object for each attribute that e serialized.

Parameters:
  • file_path (os.PathLike) – The file to which to save the json object. A pickled object of the same name but different extension might also be saved if the class is not entirely MSONable.

  • mkdir (bool) – If True, makes the provided directory, including all parent directories.

  • json_kwargs (dict) – Keyword arguments to pass to the serializer.

  • pickle_kwargs (dict) – Keyword arguments to pass to pickle.dump.

  • strict (bool) – If True, will not allow you to overwrite existing files.

to_json()

Returns a json string representation of the MSONable object.

Return type:

str

unsafe_hash()

Returns an hash of the current object. This uses a generic but low performance method of converting the object to a dictionary, flattening any nested keys, and then performing a hash on the resulting object

classmethod validate_monty_v1(_MSONable__input_value)

Pydantic validator with correct signature for pydantic v1.x

classmethod validate_monty_v2(_MSONable__input_value, _)

Pydantic validator with correct signature for pydantic v2.x