espei package¶

espei.core_utils module¶

Module for handling data

espei.core_utils.build_sitefractions(phase_name, sublattice_configurations, sublattice_occupancies)

Convert nested lists of sublattice configurations and occupancies to a list of dictionaries. The dictionaries map SiteFraction symbols to occupancy values. Note that zero occupancy site fractions will need to be added separately since the total degrees of freedom aren’t known in this function.

Parameters: phase_name (str) – Name of the phase sublattice_configurations ([[str]]) – sublattice configuration sublattice_occupancies ([[float]]) – occupancy of each sublattice a list of site fractions over sublattices [[float]]
espei.core_utils.canonical_sort_key(x)

Wrap strings in tuples so they’ll sort.

Parameters: x ([str]) – list of strings tuple of strings that can be sorted (str)
espei.core_utils.canonicalize(configuration, equivalent_sublattices)

Sort a sequence with symmetry. This routine gives the sequence a deterministic ordering while respecting symmetry.

Parameters: configuration ([str]) – Sublattice configuration to sort. equivalent_sublattices ({{int}}) – Indices of ‘configuration’ which should be equivalent by symmetry, i.e., [[0, 4], [1, 2, 3]] means permuting elements 0 and 4, or 1, 2 and 3, respectively, has no effect on the equivalence of the sequence. sorted tuple that has been canonicalized. str
espei.core_utils.endmembers_from_interaction(configuration)
espei.core_utils.get_data(comps, phase_name, configuration, symmetry, datasets, prop)
espei.core_utils.get_samples(desired_data)
espei.core_utils.list_to_tuple(x)
espei.core_utils.ravel_conditions(values, *conditions, **kwargs)

Broadcast and flatten conditions to the shape dictated by the values.

Special handling for ZPF data that does not have nice array values.

Parameters: values (list) – Multidimensional lists of values conditions (list) – List of conditions to broadcast. Must be the same length as the number of dimensions of the values array. In code, the following must be True: all([s == len(cond) for s, cond in zip(values.shape, conditions)]) zpf (bool, optional) – Whether to consider values as a special case of ZPF data (not an even grid of conditions) Default is False Tuple of ravelled conditions tuple

Notes

The current implementation of ZPF data only has the shape for one condition and this assumption is hardcoded in various places.

Here we try to be as general as possible by explicitly calculating the shape of the ZPF values.

A complication of this is that the user of this function must pass the correct conditions because usually T and P are specified in ZPF (but, again, only one can actually be a condition given the current shape).

espei.core_utils.ravel_zpf_values(desired_data, independent_comps, conditions=None)

Unpack the phases and compositions from ZPF data

Depdendent components are converted to independent components.

Parameters: desired_data (espei.utils.PickleableTinyDB) – The selected data independent_comps (list) – List of indepdendent components. Used for mass balance component conversion conditions (dict) – Conditions to filter for. Right now only considers fixed temperatures A dictonary of list of lists of tuples. Each dictionary key is the number of phases in equilibrium, e.g. a key “2” might have values [ [(PHASE_NAME_1, {‘C1’: X1, ‘C2’: X2}, refkey), (PHASE_NAME_2, {‘C1’: X1, ‘C2’: X2}, refkey)], …] Three would have three inner tuples and so on. dict
espei.core_utils.symmetry_filter(x, config, symmetry)

Return True if the candidate sublattice configuration has any symmetry which matches the phase model symmetry.

Parameters: x (the candidate dataset 'solver' dict. Must contain the "sublattice_configurations" key) – config (the configuratino of interest: e.g. ['AL', ['AL', 'NI'], 'VA']) – symmetry (tuple of tuples where each inner tuple is a group of equivalent) – sublattices. A value of ((0, 1), (2, 3, 4)) means that sublattices at indices 0 and 1 are symmetrically equivalent to each other and sublattices at indices 2, 3, and 4 are symetrically equivalent to each other. bool

espei.datasets module¶

exception espei.datasets.DatasetError

Bases: Exception

Exception raised when datasets are invalid.

espei.datasets.check_dataset(dataset)

Ensure that the dataset is valid and consistent.

Currently supports the following validation checks: * data shape is valid * phases and components used match phases and components entered

Planned validation checks: * all required keys are present * individual shapes of keys, such as ZPF, sublattice configs and site ratios

Note that this follows some of the implicit assumptions in ESPEI at the time of writing, such that conditions are only P, T, configs for single phase and essentially only T for ZPF data.

Parameters: dataset (dict) – Dictionary of the standard ESPEI dataset. None DatasetError – If an error is found in the dataset
espei.datasets.load_datasets(dataset_filenames)

Create a PickelableTinyDB with the data from a list of filenames.

Parameters: dataset_filenames ([str]) – List of filenames to load as datasets PickleableTinyDB
espei.datasets.recursive_glob(start, pattern)

Recursively glob for the given pattern from the start directory.

Parameters: start (str) – Path of the directory to walk while for file globbing pattern (str) – Filename pattern to match in the glob List of matched filenames [str]

espei.espei_script module¶

Automated fitting script.

A minimal run must specify an input.json and a datasets folder containing input files.

espei.espei_script.get_run_settings(input_dict)

Validate settings from a dict of possible input.

Performs the following actions: 1. Normalize (apply defaults) 2. Validate against the schema

Parameters: input_dict (dict) – Dictionary of input settings Validated run settings dict ValueError
espei.espei_script.main()

Handle starting ESPEI from the command line. Parse command line arguments and input file.

espei.espei_script.run_espei(run_settings)

Wrapper around the ESPEI fitting procedure, taking only a settings dictionary.

Parameters: run_settings (dict) – Dictionary of input settings Either a Database (for generate parameters only) or a tuple of (Database, sampler)

espei.mcmc module¶

Module for running MCMC in ESPEI

espei.mcmc.estimate_hyperplane(dbf, comps, phases, current_statevars, comp_dicts, phase_models, parameters)
espei.mcmc.generate_parameter_distribution(parameters, num_samples, std_deviation, deterministic=True)

Return an array of num_samples from a Gaussian distribution about each parameter.

Parameters: parameters (ndarray) – 1D array of initial parameters that will be the mean of the distribution. num_samples (int) – Number of chains to initialize. std_deviation (float) – Fractional standard deviation of the parameters to use for initialization. deterministic (bool) – True if the parameters should be generated deterministically. ndarray
espei.mcmc.lnprob(params, comps=None, dbf=None, phases=None, datasets=None, symbols_to_fit=None, phase_models=None, scheduler=None)

Returns the error from multiphase fitting as a log probability.

espei.mcmc.mcmc_fit(dbf, datasets, mcmc_steps=1000, save_interval=100, chains_per_parameter=2, chain_std_deviation=0.1, scheduler=None, tracefile=None, probfile=None, restart_chain=None, deterministic=True)

Run Markov Chain Monte Carlo on the Database given datasets

Parameters: dbf (Database) – A pycalphad Database to fit with symbols to fit prefixed with VV followed by a number, e.g. VV0001 datasets (PickleableTinyDB) – A database of single- and multi-phase to fit mcmc_steps (int) – Number of chain steps to calculate in MCMC. Note the flattened chain will have (mcmc_steps*DOF) values. Default is 1000 steps. save_interval (int) – interval of steps to save the chain to the tracefile and probfile chains_per_parameter (int) – number of chains for each parameter. Must be an even integer greater or equal to 2. Defaults to 2. chain_std_deviation (float) – standard deviation of normal for parameter initialization as a fraction of each parameter. Must be greater than 0. Default is 0.1, which is 10%. scheduler (callable) – Scheduler to use with emcee. Must implement a map method. tracefile (str) – filename to store the flattened chain with NumPy.save. Array has shape (nwalkers, iterations, nparams) probfile (str) – filename to store the flattened ln probability with NumPy.save restart_chain (np.ndarray) – ndarray of the previous chain. Should have shape (nwalkers, iterations, nparams) deterministic (bool) – If True, the emcee sampler will be seeded to give deterministic sampling draws. This will ensure that the runs with the exact same database, chains_per_parameter, and chain_std_deviation (or restart_chain) will produce exactly the same results. dbf (Database) – Resulting pycalphad database of optimized parameters sampler (EnsembleSampler, ndarray)) – emcee sampler for further data wrangling
espei.mcmc.multi_phase_fit(dbf, comps, phases, datasets, phase_models, parameters=None, scheduler=None)
espei.mcmc.tieline_error(dbf, comps, current_phase, cond_dict, region_chemical_potentials, phase_flag, phase_models, parameters, debug_mode=False)

espei.paramselect module¶

The paramselect module handles automated parameter selection for linear models.

Automated Parameter Selection End-members

Note: All magnetic parameters from literature for now. Note: No fitting below 298 K (so neglect third law issues for now).

For each step, add one parameter at a time and compute AIC with max likelihood.

Cp - TlnT, T**2, T**-1, T**3 - 4 candidate models (S and H only have one required parameter each. Will fit in full MCMC procedure)

Choose parameter set with best AIC score.

1. G (full MCMC) - all parameters selected at least once by above procedure

MCMC uses an EnsembleSampler based on Goodman and Weare, Ensemble Samplers with Affine Invariance. Commun. Appl. Math. Comput. Sci. 5, 65-80 (2010).

espei.paramselect.fit_formation_energy(dbf, comps, phase_name, configuration, symmetry, datasets, features=None)

Find suitable linear model parameters for the given phase. We do this by successively fitting heat capacities, entropies and enthalpies of formation, and selecting against criteria to prevent overfitting. The “best” set of parameters minimizes the error without overfitting.

Parameters: dbf (Database) – pycalphad Database. Partially complete, so we know what degrees of freedom to fix. comps ([str]) – Names of the relevant components. phase_name (str) – Name of the desired phase for which the parameters will be found. configuration (ndarray) – Configuration of the sublattices for the fitting procedure. symmetry ([[int]]) – Symmetry of the sublattice configuration. datasets (PickleableTinyDB) – All the datasets desired to fit to. features (dict) – Maps “property” to a list of features for the linear model. These will be transformed from “GM” coefficients e.g., {“CPM_FORM”: (v.T*sympy.log(v.T), v.T**2, v.T**-1, v.T**3)} (Default value = None) {feature: estimated_value} dict
espei.paramselect.generate_parameters(phase_models, datasets, ref_state, excess_model)

Generate parameters from given phase models and datasets

Parameters: phase_models (dict) – Dictionary of components and phases to fit. datasets (PickleableTinyDB) – database of single- and multi-phase to fit. ref_state (str) – String of the reference data to use, e.g. ‘SGTE91’ or ‘SR2016’ excess_model (str) – String of the type of excess model to fit to, e.g. ‘linear’ Database
espei.paramselect.phase_fit(dbf, phase_name, symmetry, subl_model, site_ratios, datasets, refdata, aliases=None)

Generate an initial CALPHAD model for a given phase and sublattice model.

Parameters: dbf (Database) – pycalphad Database to add parameters to. phase_name (str) – Name of the phase. symmetry ([[int]]) – Sublattice model symmetry. subl_model ([[str]]) – Sublattice model for the phase of interest. site_ratios ([float]) – Number of sites in each sublattice, normalized to one atom. datasets (PickleableTinyDB) – All datasets to consider for the calculation. refdata (dict) – Maps tuple(element, phase_name) -> SymPy object defining energy relative to SER aliases ([str]) – Alternative phase names. Useful for matching against reference data or other datasets. (Default value = None) Modifies the dbf. None

espei.plot module¶

Plotting of input data and calculated database quantities

espei.plot.dataplot(comps, phases, conds, datasets, ax=None, plot_kwargs=None, tieline_plot_kwargs=None)

Plot datapoints corresponding to the components, phases, and conditions.

Parameters: comps (list) – Names of components to consider in the calculation. phases ([]) – Names of phases to consider in the calculation. conds (dict) – Maps StateVariables to values and/or iterables of values. datasets (PickleableTinyDB) – ax (matplotlib.Axes) – Default axes used if not specified. plot_kwargs (dict) – Additional keyword arguments to pass to the matplotlib plot function for points tieline_plot_kwargs (dict) – Additional keyword arguments to pass to the matplotlib plot function for tielines A plot of phase equilibria points as a figure matplotlib.Axes

Examples

>>> from espei.datasets import load_datasets, recursive_glob
>>> from espei.plot import dataplot
>>> my_phases = ['BCC_A2', 'CUMG2', 'FCC_A1', 'LAVES_C15', 'LIQUID']
>>> my_components = ['CU', 'MG' 'VA']
>>> conditions = {v.P: 101325, v.T: (500, 1000, 10), v.X('MG'): (0, 1, 0.01)}
>>> dataplot(my_components, my_phases, conditions, datasets)

espei.plot.eqdataplot(eq, datasets, ax=None, plot_kwargs=None)

Plot datapoints corresponding to the components and phases in the eq Dataset. A convenience function for dataplot.

Parameters: eq (xarray.Dataset) – Result of equilibrium calculation. datasets (PickleableTinyDB) – Database of phase equilibria datasets ax (matplotlib.Axes) – Default axes used if not specified. plot_kwargs (dict) – Keyword arguments to pass to dataplot A plot of phase equilibria points as a figure

Examples

>>> from pycalphad import equilibrium, Database, variables as v
>>> from espei.datasets import load_datasets, recursive_glob
>>> dbf = Database('my_databases.tdb')
>>> my_phases = list(dbf.phases.keys())
>>> eq = equilibrium(dbf, ['CU', 'MG', 'VA'], my_phases, {v.P: 101325, v.T: (500, 1000, 10), v.X('MG'): (0, 1, 0.01)})
>>> ax = eqplot(eq)
>>> ax = eqdataplot(eq, datasets, ax=ax)

espei.plot.multiplot(dbf, comps, phases, conds, datasets, eq_kwargs=None, plot_kwargs=None, data_kwargs=None)

Plot a phase diagram with datapoints described by datasets. This is a wrapper around pycalphad.equilibrium, pycalphad’s eqplot, and dataplot.

Parameters: dbf (Database) – pycalphad thermodynamic database containing the relevant parameters. comps (list) – Names of components to consider in the calculation. phases (list) – Names of phases to consider in the calculation. conds (dict) – Maps StateVariables to values and/or iterables of values. datasets (PickleableTinyDB) – Database of phase equilibria datasets eq_kwargs (dict) – Keyword arguments passed to pycalphad equilibrium() plot_kwargs (dict) – Keyword arguments passed to pycalphad eqplot() data_kwargs (dict) – Keyword arguments passed to dataplot() A phase diagram with phase equilibria data as a figure

Examples

>>> from pycalphad import Database, variables as v
>>> from espei.datasets import load_datasets, recursive_glob
>>> dbf = Database('my_databases.tdb')
>>> my_phases = list(dbf.phases.keys())
>>> multiplot(dbf, ['CU', 'MG', 'VA'], my_phases, {v.P: 101325, v.T: 1000, v.X('MG'): (0, 1, 0.01)}, datasets)

espei.plot.plot_parameters(dbf, comps, phase_name, configuration, symmetry, datasets=None, fig=None, require_data=True)

Plot parameters of interest compared with data in subplots of a single figure

Parameters: dbf (Database) – pycalphad thermodynamic database containing the relevant parameters. comps (list) – Names of components to consider in the calculation. phase_name (str) – Name of the considered phase phase configuration (tuple) – Sublattice configuration to plot, such as (‘CU’, ‘CU’) or ((‘CU’, ‘MG’), ‘CU’) symmetry (list) – List of lists containing indices of symmetric sublattices e.g. [[0, 1], [2, 3]] datasets (PickleableTinyDB) – ESPEI datasets to compare against. If None, nothing is plotted. fig (matplotlib.Figure) – Figure to create with axes as subplots. require_data (bool) – If True, plot parameters that have data corresponding data. Defaults to True. Will raise an error for non-interaction configurations. None

Examples

# plot the LAVES_C15 (Cu)(Mg) endmember >>> plot_parameters(dbf, [‘CU’, ‘MG’], ‘LAVES_C15’, (‘CU’, ‘MG’), symmetry=None, datasets=datasets) # plot the mixing interaction in the first sublattice >>> plot_parameters(dbf, [‘CU’, ‘MG’], ‘LAVES_C15’, ((‘CU’, ‘MG’), ‘MG’), symmetry=None, datasets=datasets)

espei.refdata module¶

Create a new Mock object. Mock takes several optional arguments that specify the behaviour of the Mock object:

• spec: This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. If you pass in an object then a list of strings is formed by calling dir on the object (excluding unsupported magic attributes and methods). Accessing any attribute not in this list will raise an AttributeError.

If spec is an object (rather than a list of strings) then mock.__class__ returns the class of the spec object. This allows mocks to pass isinstance tests.

• spec_set: A stricter variant of spec. If used, attempting to set or get an attribute on the mock that isn’t on the object passed as spec_set will raise an AttributeError.

• side_effect: A function to be called whenever the Mock is called. See the side_effect attribute. Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it returns DEFAULT, the return value of this function is used as the return value.

If side_effect is an iterable then each call to the mock will return the next value from the iterable. If any of the members of the iterable are exceptions they will be raised instead of returned.

• return_value: The value returned when the mock is called. By default this is a new Mock (created on first access). See the return_value attribute.

• wraps: Item for the mock object to wrap. If wraps is not None then calling the Mock will pass the call through to the wrapped object (returning the real result). Attribute access on the mock will return a Mock object that wraps the corresponding attribute of the wrapped object (so attempting to access an attribute that doesn’t exist will raise an AttributeError).

If the mock has an explicit return_value set then calls are not passed to the wrapped object and the return_value is returned instead.

• name: If the mock has a name then it will be used in the repr of the mock. This can be useful for debugging. The name is propagated to child mocks.

Mocks can also be called with arbitrary keyword arguments. These will be used to set attributes on the mock after it is created.

espei.utils module¶

Utilities for ESPEI

Classes and functions defined here should have some reuse potential.

espei.utils.add_bibtex_to_bib_database(bibtex, bib_db=None)

Add entries from a BibTeX file to the bibliography database

Parameters: bibtex (str) – Either a multiline string, a path, or a file-like object of a BibTeX file bib_db (PickleableTinyDB) – Database to put the BibTeX entries. Defaults to a module-level default database The modified bibliographic database
espei.utils.bib_marker_map(bib_keys, markers=None)

Return a dict with reference keys and marker dicts

Parameters: bib_keys – markers (list) – List of 2-tuples of (‘fillstyle’, ‘marker’) e.g. [(‘top’, ‘o’), (‘full’, ‘s’)]. Defaults to cycling through the filled markers, the different fill styles. Dictionary with bib_keys as keys, dict values of formatted strings and marker dicts dict

Examples

>>> bib_marker_map(['otis2016', 'bocklund2018'])
{
'bocklund2018': {
'formatted': 'bocklund2018',
'markers': {'fillstyle': 'full', 'marker': 'o'}
},
'otis2016': {
'formatted': 'otis2016',
'markers': {'fillstyle': 'full', 'marker': 'v'}
}
}

espei.utils.database_symbols_to_fit(dbf, symbol_regex='^V[V]?([0-9]+)\$')

Return names of the symbols to fit that match the regular expression

Parameters: dbf (Database) – pycalphad Database symbol_regex (str) – Regular expression of the fitting symbols. Defaults to V or VV followed by one or more numbers. list
espei.utils.flexible_open_string(obj)

Return the string of a an object that is either file-like, a file path, or the raw string.

Parameters: obj (string-like or file-like) – Either a multiline string, a path, or a file-like object str
espei.utils.optimal_parameters(trace_array, lnprob_array, kth=0)

Return the optimal parameters in the trace based on the highest likelihood. If kth is specified, return the kth set of unique optimal parameters.

Parameters: trace_array (ndarray) – Array of shape (number of chains, iterations, number of parameters) lnprob_array (ndarray) – Array of shape (number of chains, iterations) kth (int) – Zero-indexed optimum. 0 (the default) is the most optimal solution. 1 is the second most optimal, etc.. Only unique solutions will be returned. Array of optimal parameters

Notes

It is ok if the calculation did not finish and the arrays are padded with zeros. The number of chains and iterations in the trace and lnprob arrays must match.

espei.utils.sigfigs(x, n)

Round x to n significant digits